Skip to content

Commit

Permalink
(Aegis & station) extend SplitAction to cargos; add station
Browse files Browse the repository at this point in the history
  • Loading branch information
ahyangyi committed Nov 16, 2023
1 parent a78a217 commit 567cea8
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 17 deletions.
24 changes: 16 additions & 8 deletions agrf/lib/cargo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,32 @@ def __init__(self, id, **props):
self._props = props
self.callbacks = grf.make_callback_manager(grf.CARGO, {})

def get_sprites(self, g):
extra_props = {}
@staticmethod
def translate_strings(props, g):
props = props.copy()
for p in [
"type_text",
"unit_text",
"one_text",
"many_text",
"abbr_text",
]:
if self._props.get(p):
s = self._props[p]
if props.get(p):
s = props[p]
if isinstance(s, grf.StringRef):
extra_props[p] = s.get_persistent_id()
props[p] = s.get_persistent_id()
else:
extra_props[p] = g.strings.add(s).get_persistent_id()
props[p] = g.strings.add(s).get_persistent_id()
return props

def get_definitions(self, g):
return [grf.Define(feature=grf.CARGO, id=self.id, props=Cargo.translate_strings(self._props, g))]

res = []
res.append(definition := grf.Define(feature=grf.CARGO, id=self.id, props={**self._props, **extra_props}))
def get_sprites(self, g):
res = self.get_definitions(g)
if len(res) == 0:
return []
definition = res[-1]
self.callbacks.graphics = 0
res.append(self.callbacks.make_map_action(definition))

Expand Down
10 changes: 5 additions & 5 deletions agrf/split_action/meta_sprite_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@


class MetaSpriteMixin:
# FIXME: handle make this an actual mixin
def __init__(self, feature, props_hash, parameter_list, priority_props=()):
def __init__(self, feature, props_hash, parameter_list):
self.feature = feature
self.props_hash = props_hash
self._parameter_list = parameter_list
self._priority_props = priority_props

@property
def dynamic_prop_variables(self):
Expand All @@ -20,6 +18,9 @@ def dynamic_prop_variables(self):
ret.add(v)
return list(sorted(ret))

def postprocess_props(self, props):
return props

def resolve_props(self, parameters):
new_props = {}
miss = False
Expand All @@ -35,7 +36,6 @@ def resolve_props(self, parameters):
new_props[k] = v
if new_props.get("exists", True):
assert not miss
new_props = {**{p: new_props[p] for p in self._priority_props if p in new_props}, **new_props}
else:
new_props = {"exists": False}
return new_props
Expand All @@ -50,7 +50,7 @@ def dynamic_definitions(self, all_choices, parameters, i=0):
grf.Define(
feature=self.feature,
id=self.id,
props=resolved_props,
props=self.postprocess_props(resolved_props),
)
]
], self.props_hash(resolved_props)
Expand Down
10 changes: 10 additions & 0 deletions industry/aegis_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def initialize_metadata():
("ECONOMY", "POLICY", "PRIMARY_INDUSTRY_GROWTH", "WORKFORCE", "SEA_INDUSTRY", "TOWN_GOODS"), {}
)

for cargo in all_cargos:
cargo._props["exists"] = SplitDefinition(
("ECONOMY", "POLICY", "PRIMARY_INDUSTRY_GROWTH", "WORKFORCE", "SEA_INDUSTRY", "TOWN_GOODS"), {}
)

for i, meta_economy in enumerate(all_economies):
for variation in parameter_choices.iterate_variations():
economy = meta_economy.get_economy(variation)
Expand All @@ -57,6 +62,11 @@ def initialize_metadata():
industry._props["exists"].branches[index] = True
else:
industry._props["exists"].branches[index] = False
for cargo in all_cargos:
if cargo in economy.cargos:
cargo._props["exists"].branches[index] = True
else:
cargo._props["exists"].branches[index] = False
for industry, flow_desc in economy.graph.items():
industry._props["production_types"].branches[index] = flow_desc.translated_produces
industry._props["acceptance_types"].branches[index] = flow_desc.translated_accepts
Expand Down
24 changes: 22 additions & 2 deletions industry/lib/cargo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import grf
import struct
from industry.lib.parameters import parameter_list
from agrf.lib.cargo import Cargo
from cargos import cargos as cargo_table
from agrf.strings import get_translation
from agrf.split_action import SplitDefinition, MetaSpriteMixin


class CargoUnit:
Expand All @@ -13,20 +16,36 @@ class CargoUnit:
CRATE = 0x54


class ACargo(Cargo):
# FIXME: merge with the other props_hash
def props_hash(parameters):
ret = []
for k, v in sorted(parameters.items()):
ret.append((k, v))
return hash(tuple(ret))


class ACargo(Cargo, MetaSpriteMixin):
def __init__(self, id, label, cargo_class, capacity_multiplier=0x100, weight=16, **props):
super().__init__(
id,
id=id,
**{
"classes": cargo_class,
"capacity_mult": capacity_multiplier,
"weight": weight,
**props,
},
)
MetaSpriteMixin.__init__(self, grf.CARGO, props_hash, parameter_list)
self.label = label
self.cargo_class = cargo_class

def postprocess_props(self, props):
return ACargo.translate_strings(props, self._g)

def get_definitions(self, g):
res, _ = self.dynamic_definitions(self.dynamic_prop_variables, {}, 0)
return [sprite for sprite_group in res for sprite in sprite_group]

def get_sprites(self, g):
s = g.strings
self._props["type_text"] = s[f"STR_CARGO_NAME_{self.label.decode()}"]
Expand All @@ -36,6 +55,7 @@ def get_sprites(self, g):
self._props["abbr_text"] = s[f"STR_CARGO_NAME_{self.label.decode()}"]
self._props["bit_number"] = self.id
self._props["label"] = struct.unpack("<I", self.label)[0]
self._g = g # FIXME?
return super().get_sprites(g)

@property
Expand Down
7 changes: 5 additions & 2 deletions industry/lib/industry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ def props_hash(parameters):
return hash(tuple(ret))


class AIndustry(MetaSpriteMixin, grf.SpriteGenerator):
class AIndustry(grf.SpriteGenerator, MetaSpriteMixin):
def __init__(self, *, translation_name, id=None, callbacks={}, **props):
super().__init__(grf.INDUSTRY, props_hash, parameter_list, ("substitute_type",))
MetaSpriteMixin.__init__(self, grf.INDUSTRY, props_hash, parameter_list)
if "override_type" in props:
assert id is None
id = props["override_type"]
Expand All @@ -32,6 +32,9 @@ def __init__(self, *, translation_name, id=None, callbacks={}, **props):
self._props = props
self.callbacks = grf.make_callback_manager(grf.INDUSTRY, callbacks)

def postprocess_props(self, props):
return {"substitute_type": props["substitute_type"], **props}

def get_sprites(self, g):
self._props["name"] = g.strings[f"STR_INDUSTRY_NAME_{self.translation_name}"].get_persistent_id()
res, _ = self.dynamic_definitions(self.dynamic_prop_variables, {}, 0)
Expand Down
4 changes: 4 additions & 0 deletions station/dovemere_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def main():
preferred_blitter=grf.NewGRF.BLITTER_BPP_32,
)

import station.stations.dovemere_2018

g.add(station.stations.dovemere_2018.the_station)

g.write("station.grf")


Expand Down
42 changes: 42 additions & 0 deletions station/files/gorender.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"lighting_angle": 60,
"lighting_elevation": 65,
"depth_influence": 0,
"tiled_normals": true,
"solid_base": true,
"soften_edges": 0.5,
"fade_to_black": false,
"sampler": "square",
"accuracy": 5,
"overlap": 0,
"size": {
"x": 128,
"y": 128,
"z": 128
},
"render_elevation": 30,
"sprites": [
{
"angle": 45,
"width": 64,
"flip": true
},
{
"angle": 135,
"width": 64,
"flip": true
},
{
"angle": 225,
"width": 64,
"flip": true
},
{
"angle": 315,
"width": 64,
"flip": true
}
],
"agrf_bpps": [8, 32],
"agrf_scales": [1, 2, 4]
}
43 changes: 43 additions & 0 deletions station/lib/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import grf


class AStation(grf.SpriteGenerator):
def __init__(self, *, id, sprites, callbacks={}, **props):
super().__init__()
self.id = id
self.sprites = sprites
self.callbacks = grf.make_callback_manager(grf.STATION, callbacks)
self._props = props

def get_sprites(self, g):
res = []

if self.sprites:
layouts = []
for i, sprite in enumerate(self.sprites):
layouts.append(
grf.GenericSpriteLayout(
ent1=[i],
ent2=[i],
feature=grf.STATION,
)
)
assert len(layouts) == 1
self.callbacks.graphics = layouts[0]

res.append(definition := grf.Define(feature=grf.STATION, id=self.id, props={**self._props}))
if self.sprites:
res.append(
grf.Action1(
feature=grf.STATION,
set_count=len(self.sprites),
sprite_count=1,
)
)

for s in self.sprites:
res.append(s)

res.append(self.callbacks.make_map_action(definition))

return res
17 changes: 17 additions & 0 deletions station/stations/dovemere_2018.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from station.lib import AStation
from agrf.graphics.voxel import LazyVoxel, LazySpriteSheet

vox = LazyVoxel(
"dovemere_2018",
prefix=f"station/voxels/render/dovemere_2018",
voxel_getter=lambda: f"station/voxels/dovemere_2018.vox",
load_from="station/files/gorender.json",
)
vox.render()
voxels = [LazySpriteSheet([vox], [(0, 0)])]

the_station = AStation(
id=0x00,
sprites=[s for v in voxels for s in v.spritesheet(0, 0)],
**{"class": b"2018"},
)
Binary file added station/voxels/dovemere_2018.vox
Binary file not shown.

0 comments on commit 567cea8

Please sign in to comment.