68 lines
3.1 KiB
Python
68 lines
3.1 KiB
Python
#!/usr/bin/env python3
|
|
"""Extract the wooden sword and original weapon offsets, preserving Link assets."""
|
|
import hashlib
|
|
import json
|
|
|
|
from PIL import Image
|
|
from extract_graphics import ROOT, ROM_SHA256, decode
|
|
from assemble_link import COLORS
|
|
|
|
|
|
def assemble():
|
|
rom = (ROOT / ".tmp/zelda-1.nes").read_bytes()
|
|
if hashlib.sha256(rom).hexdigest() != ROM_SHA256:
|
|
raise ValueError("Unverified ROM")
|
|
prg = rom[16:]
|
|
bank = ROOT / "assets/characters/link"
|
|
manifest_path = bank / "asset.json"
|
|
manifest = json.loads(manifest_path.read_text())
|
|
patterns = bytearray(4096)
|
|
patterns[:1792] = prg[32895:34687]
|
|
# Boot demo patterns remain in $0700..$08DF when level patterns load.
|
|
patterns[0x700:0x8E0] = prg[19892:19892 + 0x1E0]
|
|
|
|
def column(tile):
|
|
image = Image.new("RGBA", (8, 16))
|
|
pixels = decode(patterns[tile * 16:(tile + 1) * 16])
|
|
pixels += decode(patterns[(tile + 1) * 16:(tile + 2) * 16])
|
|
image.putdata([COLORS[n] for n in pixels])
|
|
return image
|
|
|
|
vertical = Image.new("RGBA", (16, 16))
|
|
vertical.paste(column(0x20), (4, 0)) # Narrow-item centering in Anim_WriteSpecificItemSprites.
|
|
horizontal = Image.new("RGBA", (16, 16))
|
|
horizontal.paste(column(0x82), (0, 0))
|
|
horizontal.paste(column(0x84), (8, 0))
|
|
images = {"up": vertical, "down": vertical.transpose(Image.Transpose.FLIP_TOP_BOTTOM),
|
|
"right": horizontal, "left": horizontal.transpose(Image.Transpose.FLIP_LEFT_RIGHT)}
|
|
for direction, image in images.items():
|
|
name = f"sword_{direction}"
|
|
path = bank / f"{name}.png"
|
|
image.save(path)
|
|
artifact_id = f"link-{name}"
|
|
existing = [a for a in manifest["artifacts"] if a["artifact_id"] == artifact_id]
|
|
if existing:
|
|
artifact = existing[0]
|
|
else:
|
|
artifact = {"file": path.name, "index": len(manifest["artifacts"]), "artifact_id": artifact_id}
|
|
manifest["artifacts"].append(artifact)
|
|
artifact["fingerprint"] = hashlib.sha256(path.read_bytes()).hexdigest()
|
|
manifest_path.write_text(json.dumps(manifest, indent=2) + "\n")
|
|
x = prg[128841:128857]
|
|
y = prg[128857:128873]
|
|
if x.hex() != "ff0100f8ff01f50bff01f907ff01fd03" or y.hex() != "f7f2f5f5f60d0303f7090303ff050303":
|
|
raise ValueError("Weapon offset tables differ from the reference")
|
|
data = {"rom_sha256": ROM_SHA256, "offset_direction_order": ["up", "down", "left", "right"],
|
|
"offsets_x": [n if n < 128 else n - 256 for n in x],
|
|
"offsets_y": [n if n < 128 else n - 256 for n in y],
|
|
"offsets_prg": {"x": 128841, "y": 128857},
|
|
"patterns": {"vertical": [0x20, 0x21], "horizontal": [0x82, 0x83, 0x84, 0x85]},
|
|
"pattern_prg_offsets": {"vertical": 32895 + 0x200, "horizontal": 19892 + 0x120},
|
|
"timers": {"initial": 5, "extended": 8, "retract_1": 1, "retract_2": 1, "hidden": 1}}
|
|
(ROOT / "extracted/link/attack-data.json").write_text(json.dumps(data, indent=2) + "\n")
|
|
print("Added four sword glyphs and extracted original weapon offset tables")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
assemble()
|