95 lines
8.0 KiB
Python
95 lines
8.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Publish room $67 and native Octorok patterns from the verified NES ROM."""
|
|
import hashlib,json,uuid
|
|
from PIL import Image
|
|
from extract_graphics import ROOT,decode
|
|
from nes_object_graphics import descriptor,octorok,ENTRY as DESCRIPTOR_ENTRY
|
|
from extract_start_room import extract,write_json,RGB
|
|
|
|
def prepare():
|
|
d=extract(0x67,'north');start=json.loads((ROOT/'extracted/overworld/start/room.json').read_text());p=(ROOT/'.tmp/zelda-1.nes').read_bytes()[16:]
|
|
regpath=ROOT/'assets/.prometeu/index.json';reg=json.loads(regpath.read_text())
|
|
def register(root):
|
|
for a in reg['assets']:
|
|
if a['root']==root:a['included_in_build']=True;return a['asset_id'],a['asset_uuid']
|
|
n=reg['next_asset_id'];reg['next_asset_id']+=1;u=str(uuid.uuid5(uuid.NAMESPACE_URL,'zelda1/'+root));reg['assets'].append({'asset_id':n,'asset_uuid':u,'root':root,'included_in_build':True});return n,u
|
|
def bank(root,images,colors,size):
|
|
n,u=register(root);folder=ROOT/'assets'/root;folder.mkdir(parents=True,exist_ok=True);arts=[]
|
|
for k,(name,im) in enumerate(images):
|
|
path=folder/(name+'.png');im.save(path);arts.append({'file':path.name,'index':k,'artifact_id':name,'fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()})
|
|
write_json(folder/'asset.json',{'schema_version':1,'asset_uuid':u,'name':root,'type':'glyph_bank','output':{'format':'GLYPH/indexed_v1','codec':'NONE','metadata':{'tile_size':size},'pipeline':{'palettes':[{'index':i,'palette':{'rgba8888':[int.from_bytes(bytes(c),'big',signed=True) for c in row]}} for i,row in enumerate(colors)]}},'preload':{'enabled':True},'artifacts':arts});write_json(folder/'studio.asset.json',{'schema_version':1,'glyph_bank_specialization':'tileset'});return n
|
|
pairs=sorted({(r['tilemap'][y][x],r['palette_map'][y][x]) for r in (d,start) for y in range(8,30) for x in range(32)})
|
|
ims=[]
|
|
for t,pal in pairs:
|
|
source='north' if any(i['tile_id']==t and i['palette_id']==pal for i in d['used_tiles']) else 'start';ims.append((f'tile_{t:02x}_p{pal}',Image.open(ROOT/f'extracted/overworld/{source}/tiles/tile_{t:02x}_p{pal}.png').convert('RGBA')))
|
|
assert {pal for t,pal in pairs}=={2};bid=bank('tilesets/north',ims,[[(*RGB[c],255) for c in d['background_nes_palettes'][2]]],'8x8');g={pair:i for i,pair in enumerate(pairs)}
|
|
def scene(root,rooms):
|
|
_,u=register(root);f=ROOT/'assets'/root;f.mkdir(parents=True,exist_ok=True);write_json(f/'asset.json',{'schema_version':1,'asset_uuid':u,'name':root,'type':'scene_bank','output':{'format':'SCENE/tilemap_v1','codec':'NONE','pipeline':{}},'preload':{'enabled':True}})
|
|
empty={'active':False,'glyph_id':0,'palette_id':0,'flip_x':False,'flip_y':False};tiles=[dict(empty) for _ in range(256)]
|
|
for r in rooms:
|
|
for y in range(8,30):
|
|
for x in range(32):tiles.append({'active':True,'glyph_id':g[(r['tilemap'][y][x],r['palette_map'][y][x])],'palette_id':0,'flip_x':False,'flip_y':False})
|
|
def layer(active,w,h,t):return {'active':active,'glyph_asset_id':bid,'tile_size':8,'parallax_factor_x':1.0,'parallax_factor_y':1.0,'width':w,'height':h,'tiles':t}
|
|
write_json(f/'scene-bank.pack-request.json',{'schema_version':1,'format':'SCENE/tilemap_v1','version':1,'layers':[layer(True,32,8+22*len(rooms),tiles)]+[layer(False,1,1,[empty]) for _ in range(3)]})
|
|
scene('scenes/overworld_north',[d]);scene('scenes/overworld_north_scroll',[d,start])
|
|
# Same raw tile semantics as the starting stage, separate room-owned array.
|
|
source=(ROOT/'src/stages/overworld_start/room.generated.pbs').read_text().replace('StartRoomData','RoomData');a=source.index('[');b=source.index('];',a)
|
|
values=[v for row in d['tilemap'][8:] for v in row];source=source[:a]+'[\n'+',\n'.join(' '+', '.join(map(str,values[i:i+32])) for i in range(0,704,32))+'\n'+source[b:]
|
|
f=ROOT/'src/stages/overworld_north';f.mkdir(parents=True,exist_ok=True);(f/'room.generated.pbs').write_text(source.replace('python3 tools/sync_start_stage.py','python3 tools/prepare_north.py'))
|
|
# DrawObjectWithType increments object type before indexing ObjAnimations.
|
|
heap=p.index(bytes.fromhex('0004080c101014185c9e44ced2d6dace'));anim=p.index(bytes.fromhex('00080b0f13175c601b1b2121'));frames=list(p[heap+p[anim+8]:heap+p[anim+8]+6]);assert frames==[0xb4,0xb0,0xb0,0xb8,0xb2,0xb2],frames
|
|
patterns=bytearray(4096);patterns[:1792]=p[32895:34687];patterns[0x700:0x1000]=p[19892:22196];patterns[0x8e0:]=p[53595:55419]
|
|
rows=[list(p[103171+16+i*4:103175+16+i*4]) for i in range(4)];rgb=dict(RGB);rgb.update({2:(0,0,188),34:(104,136,252),41:(56,184,0),12:(0,64,88),28:(0,136,136),44:(88,248,216)})
|
|
colors=[[(0,0,0,0) if i==0 else (*rgb[c],255) for i,c in enumerate(row)] for row in rows]
|
|
def column(t,flip=False,flip_y=False):
|
|
im=Image.new('RGBA',(8,16));im.putdata([colors[0][v] for k in (t,t+1) for v in decode(patterns[k*16:(k+1)*16])])
|
|
if flip:im=im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
|
|
if flip_y:im=im.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
|
|
return im
|
|
def compose(info,offset=0):
|
|
im=Image.new('RGBA',(16,16))
|
|
for side in range(info['sides']):
|
|
attr=info['attributes'][side];im.paste(column(info['tiles'][side],bool(attr&64),bool(attr&128)),(offset+side*info['x_separation'],0))
|
|
return im
|
|
images=[];records=[]
|
|
for phase in range(2):
|
|
for direction in ('left','up','down','right'):
|
|
info=octorok(direction,phase);im=compose(info)
|
|
images.append((f'octorok_{direction}_{phase}',im));records.append({'direction':direction,'phase':phase,**info})
|
|
# The same original descriptor path handles a half-width rock at X + 4.
|
|
rock_info=descriptor(0x53);rocktile=rock_info['tiles'][0]
|
|
assert rock_info['animation_index']==0x54 and rocktile==0x9e and rock_info['sides']==1
|
|
images.append(('rock',compose(rock_info,4)))
|
|
# Cloud graphic is native monster metastate animation ($5F).
|
|
for t in (0x70,0x72,0x74):
|
|
im=Image.new('RGBA',(16,16));im.paste(column(t),(0,0));im.paste(column(t,True),(8,0));images.append((f'cloud_{t:02x}',im))
|
|
bank('characters/octorok',images,colors,'16x16')
|
|
# Source-room C=$47: count index 1, type $07. FoeCounts starts after palette terminator.
|
|
count=p[103168+36+1];assert count==4
|
|
spawn_sig=bytes.fromhex('6355957688795a9a6c');spawn_offset=p.index(spawn_sig,81920,98304);spawn=list(p[spawn_offset:spawn_offset+9]);safe=[]
|
|
for cell in spawn:
|
|
x=(cell&15)*16;y=(cell&240)|13;row=(y+11-64)//8;col=x//8
|
|
tile=d['tilemap'][row+8][col]
|
|
if tile in (141,145,156,172,173,204,210,213,223):tile=38
|
|
if tile<137:safe.append([x,y])
|
|
assert len(safe)>=4
|
|
write_json(ROOT/'extracted/overworld/north/enemies.json',{'type':7,'name':'Red slow Octorok','count':count,'spawn_list_prg_offset':spawn_offset,'up_entry_spawn_list':spawn,'first_safe_positions':safe[:4],'animation_heap_prg_offset':heap,'frame_tiles':frames,'frames':records,'descriptor_entry_cpu':DESCRIPTOR_ENTRY,'rock_descriptor':rock_info,'rock_tile':rocktile,'speed_q':32,'animation_ticks':6,'turn_rate':112,'monster_bounds':[17,224,78,205],'retained_demo_patterns_prg_offset':19892,'cloud_tiles':[112,114,116],'body_damage':128,'shot_damage':128,'scope':'Native room enemy and graphics extraction. Combat, history and drop rules are documented in extracted/combat.'})
|
|
# Minimap marker moves one overworld row (4 NES pixels) north.
|
|
hud=ROOT/'assets/ui/hud';doc=json.loads((hud/'asset.json').read_text());im=Image.open(hud/'hud_09.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]
|
|
assert pixels
|
|
for x,y in pixels:im.putpixel((x,y),gray)
|
|
for x,y in pixels:im.putpixel((x,y-4),green)
|
|
path=hud/'hud-north-marker.png';im.save(path);artifact={'file':path.name,'index':20,'artifact_id':'hud-north-marker','fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}
|
|
doc['artifacts']=[a for a in doc['artifacts'] if a['index']!=20]+[artifact];write_json(hud/'asset.json',doc)
|
|
|
|
if json.loads(regpath.read_text())!=reg:write_json(regpath,reg)
|
|
print('North and scroll scenes published; 4 red Octoroks:',safe[:4])
|
|
if __name__=='__main__':
|
|
prepare()
|
|
from prepare_overworld import prepare as prepare_world
|
|
prepare_world()
|
|
|
|
from prepare_zora import prepare as prepare_zora
|
|
prepare_zora()
|