#!/usr/bin/env python3 """Extract Items tables, new-game defaults and sword-lift graphics from the ROM.""" import hashlib import json from PIL import Image from extract_graphics import ROOT, ROM_SHA256, decode from assemble_link import COLORS from extract_start_room import write_json UUID='6cc0a4cb-5d87-4dbf-bbb0-721f52256ade' def prepare(): rom=(ROOT/'.tmp/zelda-1.nes').read_bytes();assert hashlib.sha256(rom).hexdigest()==ROM_SHA256 p=rom[16:] slot_at=p.index(bytes.fromhex('01000000060504040202030d090c1b1c')) descriptor_at=p.index(bytes.fromhex('14212223010121222122010101010115')) slots=list(p[slot_at:slot_at+36]);descriptors=list(p[descriptor_at:descriptor_at+36]) # New-game code proves Items=$0657, HeartValues=$066F, HeartPartial=$0670, # MaxBombs=$067C; keep the complete original $28-byte layout. signature=bytes.fromhex('a0279957068810faa9228d6f06ce7006a9088d7c06') init_at=p.index(signature) initial=[0]*40;initial[0x18]=0x22;initial[0x19]=255;initial[0x25]=8 module=ROOT/'src/inventory';module.mkdir(parents=True,exist_ok=True) lines=['// Generated from verified ItemIdToSlot / ItemIdToDescriptor and new-game defaults.', 'declare global item_slots: array = ['+', '.join(map(str,slots))+'];', 'declare global item_descriptors: array = ['+', '.join(map(str,descriptors))+'];', 'declare global new_game_items: array = ['+', '.join(map(str,initial))+'];','', 'declare struct ItemData() {', ' fn slot_for(item: int) -> int {', ' if item < 0 || item >= 36 { return -1; }', ' return item_slots[item] else -1;',' }', ' fn descriptor_for(item: int) -> int {', ' if item < 0 || item >= 36 { return 0; }', ' return item_descriptors[item] else 0;',' }', ' fn initial_value(slot: int) -> int { return new_game_items[slot] else 0; }','}',''] (module/'items.generated.pbs').write_text('\n'.join(lines)) def column(tile): # Demo CHR survives in $0700..$08DF when OW CHR overwrites $08E0+. offset=32895+tile*16 if tile<0x70 else 19892+(tile*16-0x700) result=Image.new('RGBA',(8,16)) result.putdata([COLORS[v] for n in (0,16) for v in decode(p[offset+n:offset+n+16])]) return result offsets_at=p.index(bytes.fromhex('0003070a0b0c0d0e0f11121314151617')) frames_at=p.index(bytes.fromhex('20823c3470727428863c2a262422404a')) lift_tile=p[frames_at+p[offsets_at+0x21]];assert lift_tile==0x78 # Anim_WriteMirroredSpritePair sets right OAM attribute $40. # DrawLinkLiftingItem replaces its tile with $08 but preserves that flip. body=Image.new('RGBA',(16,16)) body.paste(column(lift_tile),(0,0)) body.paste(column(8).transpose(Image.Transpose.FLIP_LEFT_RIGHT),(8,0)) item=Image.new('RGBA',(16,16));item.paste(column(0x20),(0,0)) bank=ROOT/'assets/characters/link_pickup';bank.mkdir(parents=True,exist_ok=True) artifacts=[] for index,(name,image) in enumerate([('lift_sword',body),('wooden_sword',item)]): path=bank/(name+'.png');image.save(path) artifacts.append({'file':path.name,'index':index,'artifact_id':'pickup-'+name, 'fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}) output=json.loads((ROOT/'assets/characters/link/asset.json').read_text())['output'] write_json(bank/'asset.json',{'schema_version':1,'asset_uuid':UUID,'name':'Link NES sword pickup', 'type':'glyph_bank','output':output,'preload':{'enabled':True},'artifacts':artifacts}) write_json(bank/'studio.asset.json',{'schema_version':1,'glyph_bank_specialization':'sprites'}) registry_path=ROOT/'assets/.prometeu/index.json';registry=json.loads(registry_path.read_text()) matches=[a for a in registry['assets'] if a['asset_uuid']==UUID or a['root']=='characters/link_pickup'] if matches:assert len(matches)==1 and matches[0]['asset_uuid']==UUID else: registry['assets'].append({'asset_id':registry['next_asset_id'],'asset_uuid':UUID, 'root':'characters/link_pickup','included_in_build':True});registry['next_asset_id']+=1 write_json(registry_path,registry) out=ROOT/'extracted/inventory';out.mkdir(parents=True,exist_ok=True) body.resize((128,128),Image.Resampling.NEAREST).save(out/'lift_sword@8x.png') write_json(out/'rom-data.json',{'rom_sha256':ROM_SHA256,'items_length':40,'item_slots':slots, 'item_descriptors':descriptors,'new_game_items':initial,'prg_offsets':{'item_slots':slot_at, 'item_descriptors':descriptor_at,'new_game':init_at},'lift':{'body_left_tile':120, 'body_right_tile':8,'body_left_flip_x':False,'body_right_flip_x':True,'item_tile':32,'timer':128,'residual_chr_source':'demo_sprites', 'left_tile_prg_offset':19892+128},'cave_pickup':{'room_id':119,'room_item_mask':16, 'x':120,'item_y':152,'vertical_distance_exclusive':6,'person_disappear_timer':64}, 'scope':'NES Items layout/tables, sword grades 0..3, OW room flags and wooden sword acquisition. Other item effects and disk persistence are not implemented.'}) print('Generated NES Items tables (36 IDs / 40 slots), initial bytes and sword lift') if __name__=='__main__':prepare()