Catalog typed battery variants (#91)
The battery deploy pieces (s400_launcher, s500_launcher, radar_search/ track/height) were described three times: the menu table and the configuration match in lib.rs, and a chained id list in commands.rs. One catalog decision instead: catalog/content.json gains a variants section (mode/role reuse the existing SamMode/RadarRole save keys), gen_catalog.py generates catalog::UnitVariant rows, and the loader rejects duplicates, unknown kinds/modes and id collisions with kinds or items. The battery menu, its click handler and the deploy completion read the generated table; the historical row order and labels are unchanged. No variant carries its own balance: per-variant ammo profiles stay open pending a confirmed profile and an explicitly labelled game abstraction (PAC-3 note on #91), so no new specs, no new matches and no save/replay change - a variant is only a deploy key over the same kind. Checks: gen_catalog.py --check and gen_models.py --check clean, the python catalog/asset and sound tests pass, airdef-core 234 unit + 8 scenarios, airdef-mod 69 unit + 1 boundary + 5 scenarios (save/load and state round trips included), zero failures. Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
@@ -13,6 +13,10 @@ MUZZLES = {"none", "patriot", "pantsir", "grid", "rails", "cells"}
|
||||
ENGINES = {"prop": {"type", "back", "size"}, "jet": {"type", "up", "back", "size"},
|
||||
"rocket": {"type", "back", "size"}}
|
||||
MODEL = r"airdef:[a-z0-9_]+"
|
||||
# The keys of the existing simulation enums, written once here so the generator and
|
||||
# Rust keep to one encoding. A new enum variant needs its key added here.
|
||||
CONFIG_MODES = {"integrated", "launcher"}
|
||||
CONFIG_ROLES = {"multi", "search", "engagement", "height"}
|
||||
|
||||
|
||||
def load():
|
||||
@@ -29,6 +33,24 @@ def load():
|
||||
referenced.extend(entry["item"] for entry in group if entry["item"] is not None)
|
||||
if len(referenced) != len(set(referenced)) or not set(referenced) <= set(ids):
|
||||
raise ValueError("duplicate or unregistered content item")
|
||||
unit_kinds = {entry["kind"] for entry in data["units"]}
|
||||
taken = set(referenced) | unit_kinds | set(ids)
|
||||
for variant in data["variants"]:
|
||||
where = f"variants[{variant['id']}]"
|
||||
if set(variant) != {"id", "kind", "mode", "role", "label"}:
|
||||
raise ValueError(f"{where}: expected keys id, kind, mode, role, label, got {sorted(variant)}")
|
||||
if not re.fullmatch(r"[a-z0-9_]+", variant["id"]):
|
||||
raise ValueError(f"{where}: invalid variant id {variant['id']!r}")
|
||||
if variant["kind"] not in unit_kinds:
|
||||
raise ValueError(f"{where}: unknown unit kind {variant['kind']}")
|
||||
if variant["mode"] not in CONFIG_MODES or variant["role"] not in CONFIG_ROLES:
|
||||
raise ValueError(f"{where}: unknown configuration {variant['mode']}/{variant['role']}")
|
||||
if not isinstance(variant["label"], str) or not variant["label"]:
|
||||
raise ValueError(f"{where}: expected a menu label")
|
||||
if variant["id"] in taken:
|
||||
raise ValueError(f"{where}: variant id collides with an existing identity or kind")
|
||||
if len({v["id"] for v in data["variants"]}) != len(data["variants"]):
|
||||
raise ValueError("duplicate variant id in content catalog")
|
||||
for item in items:
|
||||
if len(item["color"]) != 3 or any(not isinstance(c, int) or not 0 <= c <= 255 for c in item["color"]):
|
||||
raise ValueError(f"invalid icon color for {item['id']}")
|
||||
|
||||
+19
-1
@@ -14,6 +14,8 @@ CLASSES = {"drone": "Drone", "jet": "Jet", "cruise": "Cruise", "supersonic": "Su
|
||||
"ballistic": "Ballistic"}
|
||||
FAMILIES = {"trainable": "Trainable", "gun": "Gun", "turret": "Turret", "vertical": "Vertical",
|
||||
"static": "Static", "truck": "Truck", "light": "Light"}
|
||||
MODES = {"integrated": "Integrated", "launcher": "LauncherOnly"}
|
||||
ROLES = {"multi": "Multifunction", "search": "Search", "engagement": "Engagement", "height": "Height"}
|
||||
SOUND_PREFIX = "sound::"
|
||||
|
||||
|
||||
@@ -304,16 +306,32 @@ def generate_unit_specs(data):
|
||||
return "\n".join(lines + ["];", ""])
|
||||
|
||||
|
||||
def generate_variants(data):
|
||||
"""A variant of a complex is one record: a deploy id over an existing unit's
|
||||
typed configuration (`battery::SamMode`, `battery::RadarRole`). No balance of
|
||||
its own — everything but the label comes from the unit rows it points at."""
|
||||
lines = ["// Generated by tools/gen_catalog.py from catalog/content.json. Do not edit.",
|
||||
"pub const VARIANTS: &[UnitVariant] = &["]
|
||||
for entry in data["variants"]:
|
||||
mode = MODES[entry["mode"]]
|
||||
role = ROLES[entry["role"]]
|
||||
lines.append(f" UnitVariant {{ id: {text(entry['id'])}, kind: SamKind::{entry['kind']}, "
|
||||
f"mode: SamMode::{mode}, role: RadarRole::{role}, label: {text(entry['label'])} }},")
|
||||
return "\n".join(lines + ["];", ""])
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--check", action="store_true")
|
||||
args = parser.parse_args()
|
||||
sound_output = OUTPUT.with_name("catalog_sound_ids.rs")
|
||||
variant_output = OUTPUT.with_name("unit_variants_generated.rs")
|
||||
specs = load_specs()
|
||||
outputs = ((OUTPUT, generate(load())),
|
||||
(sound_output, generate_sound_ids(load_sounds())),
|
||||
(SPEC_OUTPUT, generate_specs(specs)),
|
||||
(UNIT_SPEC_OUTPUT, generate_unit_specs(specs)))
|
||||
(UNIT_SPEC_OUTPUT, generate_unit_specs(specs)),
|
||||
(variant_output, generate_variants(load())))
|
||||
for path, generated in outputs:
|
||||
if args.check:
|
||||
if not path.exists() or path.read_text(encoding="utf-8") != generated:
|
||||
|
||||
Reference in New Issue
Block a user