Files
zelda-1-prometeu-port/tools/prepare_hud.py
T

136 lines
6.9 KiB
Python

#!/usr/bin/env python3
"""Extract a fixed overworld HUD from the verified ROM and publish its render."""
import hashlib
import json
from PIL import Image
from extract_graphics import ROOT, ROM_SHA256, decode
from extract_start_room import RGB, write_json
UUID = '3cd67989-7c68-458a-a414-b551203eaee5'
OUT = ROOT / 'extracted/hud'
BANK = ROOT / 'assets/ui/hud'
def prepare():
rom = (ROOT / '.tmp/zelda-1.nes').read_bytes()
assert hashlib.sha256(rom).hexdigest() == ROM_SHA256
prg = rom[16:]
memory = bytearray(0x4000)
memory[0x2000:0x23C0] = bytes([0x24]) * 960
transfers = []
def transfer(offset):
start = offset
while prg[offset] != 0xFF:
address = prg[offset] << 8 | prg[offset + 1]
control = prg[offset + 2]
offset += 3
count, stride = control & 0x3F, 32 if control & 0x80 else 1
for i in range(count):
memory[address + i * stride] = prg[offset if control & 0x40 else offset + i]
offset += 1 if control & 0x40 else count
transfers.append({'prg_offset': start, 'hex': prg[start:offset + 1].hex()})
# Original PPU update records: static boxes/labels/icons, OW minimap, counts.
static = prg.index(bytes.fromhex('23c20e4000004455550000040000445555'))
text = prg.index(bytes.fromhex('20b608242424242424242420d608'))
transfer(static)
transfer(103247)
transfer(text)
# Fixed presentation fixture: zero rupees/keys/bombs, 3 full containers.
for i in range(3): memory[0x20D6 + i] = 0xF2
bg = bytearray(4096)
bg[:0x700] = prg[34687:34687 + 1792]
bg[0x700:0xF20] = prg[51515:51515 + 2080]
bg[0xF20:] = prg[36479:36479 + 224]
palettes = [list(prg[103171 + i * 4:103175 + i * 4]) for i in range(8)]
# Same explicit digital palette approximations as the terrain/Link exports.
rgb = dict(RGB)
rgb.update({0x29: (56, 184, 0), 0x02: (0, 0, 188), 0x22: (104, 136, 252),
0x0C: (0, 64, 88), 0x1C: (0, 136, 136), 0x2C: (88, 248, 216)})
canvas = Image.new('RGBA', (256, 64), (0, 0, 0, 255))
tiles = []
for y in range(8):
for x in range(32):
tile = memory[0x2000 + y * 32 + x]
attr = memory[0x23C0 + (y // 4) * 8 + x // 4]
shift = ((y % 4) // 2) * 4 + ((x % 4) // 2) * 2
palette = (attr >> shift) & 3
pixels = decode(bg[tile * 16:(tile + 1) * 16])
image = Image.new('RGBA', (8, 8))
image.putdata([(*rgb[palettes[palette][v]], 255) for v in pixels])
canvas.paste(image, (x * 8, y * 8))
tiles.append({'x': x, 'y': y, 'tile_id': tile, 'palette_id': palette})
def sprite(tile, x, y, palette):
image = Image.new('RGBA', (8, 8))
values = decode(prg[32895 + tile * 16:32895 + (tile + 1) * 16])
image.putdata([(*rgb[palettes[palette][v]], 255) if v else (0, 0, 0, 0) for v in values])
canvas.alpha_composite(image, (x, y))
# UpdatePositionMarker: room $77 -> OAM ($2D,$33), visible Y=$34.
sprite(0x3E, 45, 52, 4)
unarmed = canvas.copy()
# Wooden sword slot 0, frame tile $20; narrow item adds 4 to $94.
# DrawStatusBarSword OAM Y=$1F, visible Y=$20; 8x16 sprite uses $20/$21.
sprite(0x20, 152, 32, 4)
sprite(0x21, 152, 40, 4)
OUT.mkdir(parents=True, exist_ok=True)
BANK.mkdir(parents=True, exist_ok=True)
canvas.save(OUT / 'hud.png')
unarmed.save(OUT / 'hud_unarmed.png')
canvas.resize((768, 192), Image.Resampling.NEAREST).save(OUT / 'hud@3x.png')
colors = sorted(set(canvas.getdata()))
assert len(colors) <= 16
rgba = [int.from_bytes(bytes(c), 'big', signed=True) for c in colors]
artifacts = []
for y in range(2):
for x in range(8):
index = y * 8 + x
path = BANK / f'hud_{index:02d}.png'
canvas.crop((x * 32, y * 32, (x + 1) * 32, (y + 1) * 32)).save(path)
artifacts.append({'file': path.name, 'index': index, 'artifact_id': f'hud-{index:02d}',
'fingerprint': hashlib.sha256(path.read_bytes()).hexdigest()})
# Keep glyphs 0..15 stable; only tile 12 changes when Items[0] is zero.
path = BANK / 'hud_16_unarmed.png'
unarmed.crop((128, 32, 160, 64)).save(path)
artifacts.append({'file': path.name, 'index': 16, 'artifact_id': 'hud-unarmed-slot-a',
'fingerprint': hashlib.sha256(path.read_bytes()).hexdigest()})
# Runtime life is emitted separately as 8x8 glyphs; retain the A-box edge.
heartless = canvas.copy()
for y in range(40, 56):
for x in range(176, 240): heartless.putpixel((x, y), (0, 0, 0, 255))
for i in range(3):
path = BANK / f'hud-life-base-{i}.png'
heartless.crop((160 + i * 32, 32, 192 + i * 32, 64)).save(path)
artifacts.append({'file': path.name, 'index': 17 + i, 'artifact_id': f'hud-life-base-{i}',
'fingerprint': hashlib.sha256(path.read_bytes()).hexdigest()})
write_json(BANK / 'asset.json', {'schema_version': 1, 'asset_uuid': UUID,
'name': 'NES overworld HUD', 'type': 'glyph_bank',
'output': {'format': 'GLYPH/indexed_v1', 'codec': 'NONE',
'pipeline': {'palettes': [{'index': 0, 'palette': {'rgba8888': rgba}}]},
'metadata': {'tile_size': '32x32'}},
'preload': {'enabled': True}, 'artifacts': artifacts})
write_json(BANK / 'studio.asset.json', {'schema_version': 1, 'glyph_bank_specialization': 'tileset'})
registry_path = ROOT / 'assets/.prometeu/index.json'
registry = json.loads(registry_path.read_text())
found = [a for a in registry['assets'] if a['asset_uuid'] == UUID or a['root'] == 'ui/hud']
if found:
assert len(found) == 1 and found[0]['asset_uuid'] == UUID and found[0]['root'] == 'ui/hud'
found[0]['included_in_build'] = True
else:
registry['assets'].append({'asset_id': registry['next_asset_id'], 'asset_uuid': UUID,
'root': 'ui/hud', 'included_in_build': True})
registry['next_asset_id'] += 1
write_json(registry_path, registry)
write_json(OUT / 'provenance.json', {'rom_sha256': ROM_SHA256, 'transfers': transfers,
'background_tiles': tiles, 'nes_palettes': palettes,
'fixture': {'room_id': 119, 'hearts': 3, 'rupees': 0, 'keys': 0, 'bombs': 0,
'item_a': 'wooden sword', 'item_b': None},
'sprites': [{'tile': 62, 'x': 45, 'y': 52, 'palette': 4},
{'tile': 32, 'x': 152, 'y': 32, 'palette': 4},
{'tile': 33, 'x': 152, 'y': 40, 'palette': 4}],
'rgb_approximations': {hex(k): v for k, v in rgb.items()},
'variants': {'unarmed': 'hud_unarmed.png', 'wooden_sword': 'hud.png'},
'live_inventory_slots': [0, 24, 25],
'scope': 'ROM-derived HUD; Items[0] chooses slot A and Items[24/25] drive runtime heart glyphs. Other counters remain fixed.'})
print(f'HUD extracted: {len(colors)} colors, 20 tiles, ROM PPU records at {static}, 103247, {text}')
if __name__ == '__main__': prepare()