#!/usr/bin/env python3 """Generate Link's NES-phase lookup from Studio bindings and verified ROM tables.""" import json from pathlib import Path ROOT = Path(__file__).resolve().parents[1] def sync(): bank = ROOT / "assets/characters/link" document = json.loads((bank / "animations.studio.json").read_text()) asset = json.loads((bank / "asset.json").read_text()) if document["bank_asset_uuid"] != asset["asset_uuid"]: raise ValueError("Animation bank mismatch") selected = {a["artifact_id"]: a for a in asset["artifacts"]} import hashlib for binding in document["bindings"]: artifact = selected[binding["artifact_id"]] if (binding["glyph_id"] != artifact["index"] or binding["fingerprint"] != hashlib.sha256((bank / artifact["file"]).read_bytes()).hexdigest()): raise ValueError("Animation bindings drifted; rebind explicitly in Studio first") bindings = {b["artifact_id"]: b["glyph_id"] for b in document["bindings"]} animator = next(a for a in document["animators"] if a["id"] == "link") lines = ["// Generated from assets/characters/link/animations.studio.json.", "// Regenerate with: python3 tools/sync_link_animation.py", "// Internal Link-module lookup; shared through mod.barrel.", "// Use direct glyph lookup so the NES owns the shared animation phase.", "fn link_walk_glyph(direction: int, phase: int) -> int {"] clips = {clip["name"]: clip for clip in animator["clips"]} for name, direction in (("right", 1), ("left", 2), ("down", 4), ("up", 8)): frames = clips[f"walk_{name}"]["frames"] if len(frames) != 2 or any(f["duration_ticks"] != 6 for f in frames): raise ValueError("NES movement requires two frames of 6 ticks per direction") glyphs = [bindings[f["artifact_id"]] for f in frames] lines.extend([f" if direction == {direction} {{", f" return if phase == 0 {{ {glyphs[0]} }} else {{ {glyphs[1]} }};", " }"]) lines.extend([" return 0;", "}", ""]) lines.append("fn link_attack_glyph(direction: int, phase: int) -> int {") for name, direction in (("right", 1), ("left", 2), ("down", 4), ("up", 8)): frames = clips[f"attack_{name}"]["frames"] glyphs = [bindings[f["artifact_id"]] for f in frames] lines.extend([f" if direction == {direction} {{", f" return if phase == 0 {{ {glyphs[0]} }} else {{ {glyphs[1]} }};", " }"]) lines.extend([" return 0;", "}", ""]) data_path = ROOT / "extracted/link/attack-data.json" if data_path.exists(): data = json.loads(data_path.read_text()) lines.append("fn sword_glyph(direction: int) -> int {") for name, direction in (("right", 1), ("left", 2), ("down", 4), ("up", 8)): lines.append(f" if direction == {direction} {{ return {bindings['link-sword_' + name]}; }}") lines.extend([" return 0;", "}", ""]) for axis in ("x", "y"): lines.append(f"fn sword_offset_{axis}(direction: int, weapon_state: int) -> int {{") for state in range(1, 5): lines.append(f" if weapon_state == {state} {{") for name, direction in (("right", 1), ("left", 2), ("down", 4), ("up", 8)): index = (state - 1) * 4 + data["offset_direction_order"].index(name) lines.append(f" if direction == {direction} {{ return {data['offsets_' + axis][index]}; }}") lines.append(" }") lines.extend([" return 0;", "}", ""]) path = ROOT / "src/link/graphics.generated.pbs" path.write_text("// Generated from Studio bindings and verified ROM tables.\n" "// Regenerate with: python3 tools/sync_link_animation.py\n" "declare struct LinkGraphics() {\n" + "\n".join(" " + line if line else "" for line in lines) + "}\n") print("Synchronized Link frame lookup from the saved Studio definition") if __name__ == "__main__": sync()