diff --git a/agrf/lib/cargo.py b/agrf/lib/cargo.py index 13125aa4..cf5906c2 100644 --- a/agrf/lib/cargo.py +++ b/agrf/lib/cargo.py @@ -7,8 +7,9 @@ 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", @@ -16,15 +17,22 @@ def get_sprites(self, g): "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)) diff --git a/agrf/split_action/meta_sprite_mixin.py b/agrf/split_action/meta_sprite_mixin.py index 663f967e..586dbd02 100644 --- a/agrf/split_action/meta_sprite_mixin.py +++ b/agrf/split_action/meta_sprite_mixin.py @@ -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): @@ -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 @@ -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 @@ -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) diff --git a/industry/aegis_gen.py b/industry/aegis_gen.py index 615e06bd..6486a846 100644 --- a/industry/aegis_gen.py +++ b/industry/aegis_gen.py @@ -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) @@ -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 diff --git a/industry/lib/cargo.py b/industry/lib/cargo.py index b1a09399..00506718 100644 --- a/industry/lib/cargo.py +++ b/industry/lib/cargo.py @@ -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: @@ -13,10 +16,18 @@ 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, @@ -24,9 +35,17 @@ def __init__(self, id, label, cargo_class, capacity_multiplier=0x100, weight=16, **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()}"] @@ -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("