#!/usr/bin/env python3 """Publish native room $66 by extending existing banks, without extra scenes.""" import json,hashlib from PIL import Image from extract_graphics import ROOT from extract_start_room import extract,write_json,RGB from nes_object_graphics import P,descriptor def prepare(): rooms=[extract(i,name) for i,name in [(0x66,'northwest'),(0x65,'northwest_2'),(0x64,'bomb_field')]];d=rooms[0];north=json.loads((ROOT/'extracted/overworld/north/room.json').read_text());start=json.loads((ROOT/'extracted/overworld/start/room.json').read_text()) assert d['room_attributes'][2]==0x69 count=P[103168+36+(0x69>>6)];assert count==4 # Type $09 uses the same native frame graphics as $07, with blue palette. for frame in range(6): assert descriptor(9,frame)['tiles']==descriptor(7,frame)['tiles'] folder=ROOT/'assets/tilesets/north';doc=json.loads((folder/'asset.json').read_text());glyph={a['artifact_id']:a['index'] for a in doc['artifacts']} for r,name in zip(rooms,['northwest','northwest_2','bomb_field']): for used in r['used_tiles']: t=used['tile_id'];pal=used['palette_id'];assert pal in (2,3) key=f'tile_{t:02x}_p{pal}' if key not in glyph: path=folder/(key+'.png');im=Image.open(ROOT/f'extracted/overworld/{name}/tiles/{key}.png'); if pal==3: original=[(*RGB[c],255) for c in d['background_nes_palettes'][3]];canonical=[(*RGB[c],255) for c in d['background_nes_palettes'][2]];lut=dict(zip(original,canonical));im=im.convert('RGBA');im.putdata([lut[c] for c in im.getdata()]) im.save(path);idx=len(doc['artifacts']);glyph[key]=idx;doc['artifacts'].append({'file':path.name,'index':idx,'artifact_id':key,'fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}) doc['output']['pipeline']['palettes']=[doc['output']['pipeline']['palettes'][0],{'index':1,'palette':{'rgba8888':[int.from_bytes(bytes((*RGB[c],255)),'big',signed=True) for c in d['background_nes_palettes'][3]]}}];write_json(folder/'asset.json',doc) blank={'active':False,'glyph_id':0,'palette_id':0,'flip_x':False,'flip_y':False} def room(r):return [{'active':True,'glyph_id':glyph[f"tile_{r['tilemap'][y][x]:02x}_p{r['palette_map'][y][x]}"],'palette_id':int(r['palette_map'][y][x]==3),'flip_x':False,'flip_y':False} for y in range(8,30) for x in range(32)] n=room(north);w=room(d);s=room(start);western=[room(r) for r in rooms] def publish(root,width,tiles): f=ROOT/f'assets/scenes/{root}/scene-bank.pack-request.json';v=json.loads(f.read_text());v['layers'][0].update(width=width,height=len(tiles)//width,tiles=tiles);write_json(f,v) publish('overworld_north',32,[dict(blank)]*256+n+sum(([dict(blank)]*256+r for r in western),[])) # Existing vertical strip occupies y=0..415. Horizontal strip starts at y=416. vertical=[dict(blank)]*256+n+s;tiles=[] for row in range(52):tiles+=vertical[row*32:(row+1)*32]+[dict(blank)]*32 for index,left in enumerate(western): right=n if index==0 else western[index-1] tiles += [dict(blank)]*(8*64) for row in range(22):tiles+=left[row*32:(row+1)*32]+right[row*32:(row+1)*32] publish('overworld_north_scroll',64,tiles) src=(ROOT/'src/stages/overworld_north/room.generated.pbs').read_text();a=src.index('[');b=src.index('];',a);values=[v for row in d['tilemap'][8:] for v in row];src=src[:a]+'['+','.join(map(str,values))+src[b:] f=ROOT/'src/stages/overworld_northwest';f.mkdir(parents=True,exist_ok=True);(f/'room.generated.pbs').write_text(src) (f/'mod.barrel').write_text('pub service OverworldNorthwest;\nmod struct RoomData;\n') (f/'main.pbs').write_text('import { Composer } from @sdk:composer;\ndeclare global room: RoomData = new RoomData();\ndeclare service OverworldNorthwest {\n fn render() -> bool { let status=Composer.bind_scene(13); return status==0; }\n fn tile_at(column: int, row: int) -> int { return room.tile_at(column,row); }\n fn contains_player(x: int,y: int) -> bool { return room.contains_player(x,y); }\n}\n') hud=ROOT/'assets/ui/hud';v=json.loads((hud/'asset.json').read_text());im=Image.open(hud/'hud-north-marker.png').convert('RGBA');green=(56,184,0,255);gray=(124,124,124,255);pixels=[(x,y) for y in range(32) for x in range(32) if im.getpixel((x,y))==green] for x,y in pixels:im.putpixel((x,y),gray) for x,y in pixels:im.putpixel((x-2,y),green) path=hud/'hud-northwest-marker.png';im.save(path);v['artifacts']=[a for a in v['artifacts'] if a['index']!=21]+[{'file':path.name,'index':21,'artifact_id':'hud-northwest-marker','fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}];write_json(hud/'asset.json',v) hp=P.index(bytes.fromhex('0643253112248114224200a98f20003f'),7*16384) assert P[hp+4]&15==2 # Full object list IDs include the high bit in attribute D. directory=83775;route=[] for r in rooms: c=r['room_attributes'][2];attr_d=r['room_attributes'][3];list_id=(c&63)+(64 if attr_d&128 else 0);nfoes=P[103204+(c>>6)] ptr=int.from_bytes(P[directory+(list_id-98)*2:directory+(list_id-98)*2+2],'little');off=81920+ptr-32768;types=list(P[off:off+nfoes]);route.append({'room':r['room_id'],'list_id':list_id,'object_list_prg':off,'types':types}) assert [r['types'] for r in route]==[[8,7,8,7],[8,7,8,7],[8,9,8,9,8]] write_json(ROOT/'extracted/overworld/northwest/bomb-route.json',{'route':route,'from_start':['north','west','west','west'],'hp_pairs_prg':hp,'web_source':'https://www.zeldadungeon.net/the-legend-of-zelda-walkthrough/the-gathering/','bomb_item_id':0,'bomb_quantity':4}) # Individual stages own their collision data, while their scenes share one atlas. for index,(r,name) in enumerate(zip(rooms,['overworld_northwest','overworld_northwest_2','overworld_bomb_field'])): folder=ROOT/'src/stages'/name;folder.mkdir(exist_ok=True,parents=True);service=['OverworldNorthwest','OverworldNorthwest2','OverworldBombField'][index] source=(ROOT/'src/stages/overworld_north/room.generated.pbs').read_text();a=source.index('[');b=source.index('];',a);values=[v for row in r['tilemap'][8:] for v in row];source=source[:a]+'['+','.join(map(str,values))+source[b:];(folder/'room.generated.pbs').write_text(source) (folder/'mod.barrel').write_text(f'pub service {service};\nmod struct RoomData;\n');(folder/'main.pbs').write_text(f'import {{ Composer }} from @sdk:composer;\ndeclare global room: RoomData = new RoomData();\ndeclare service {service} {{\n fn render() -> bool {{ let status=Composer.bind_scene(13); return status==0; }}\n fn tile_at(column: int,row: int) -> int {{ return room.tile_at(column,row); }}\n fn contains_player(x: int,y: int) -> bool {{ return room.contains_player(x,y); }}\n}}\n') # HUD marker for each native column. base=Image.open(hud/'hud-north-marker.png').convert('RGBA') for index in range(3): im=base.copy() for x,y in pixels:im.putpixel((x,y),gray) for x,y in pixels:im.putpixel((x-2*(index+1),y),green) path=hud/f'hud-west-{index}.png';im.save(path);idx=21+index;v['artifacts']=[a for a in v['artifacts'] if a['index']!=idx]+[{'file':path.name,'index':idx,'artifact_id':path.stem,'fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}] write_json(hud/'asset.json',v) print('Published rooms $66/$65/$64 and native mixed lists; existing scenes reused') if __name__=='__main__': prepare() from prepare_overworld import prepare as prepare_world prepare_world()