#!/usr/bin/env python3 """Publish original heart tiles, damage palettes, death sparks and GAME OVER.""" import hashlib import json from PIL import Image from extract_graphics import ROOT,ROM_SHA256,decode from extract_start_room import RGB,write_json from prepare_hud import prepare as prepare_hud LIFE_UUID='8ad788e0-bc32-44f4-90cf-72a017f9b785' BLANK_UUID='9ae973a9-2004-4e4a-8467-b9911731d631' OVER_UUID='55d7d709-132a-477e-a89d-6940f6c24e90' SPARK_UUID='c64d63c9-fbff-4dd0-9c4b-8f456498d635' def prepare(): prepare_hud() rom=(ROOT/'.tmp/zelda-1.nes').read_bytes();assert hashlib.sha256(rom).hexdigest()==ROM_SHA256 p=rom[16:];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),0x32:(60,188,252),0x10:(188,188,188)}) def rgba(colors):return [int.from_bytes(bytes(c),'big',signed=True) for c in colors] nes=[list(p[103187+i*4:103191+i*4]) for i in range(4)] colors=[[(0,0,0,0)]+[(*rgb[c],255) for c in row[1:]] for row in nes] colors += [[(0,0,0,0),(*rgb[0x32],255),(*rgb[0x27],255),(*rgb[0x17],255)], [(0,0,0,0),(*rgb[0x16],255),(*rgb[0x27],255),(*rgb[0x17],255)], [(0,0,0,0),(*rgb[0x10],255),(*rgb[0x30],255),(*rgb[0],255)]] palettes=[{'index':i,'palette':{'rgba8888':rgba(c)}} for i,c in enumerate(colors)] for root in ('characters/link','characters/link_door','characters/link_pickup'): path=ROOT/'assets'/root/'asset.json';asset=json.loads(path.read_text());asset['output']['pipeline']['palettes']=palettes;write_json(path,asset) bg=bytearray(4096);bg[:1792]=p[34687:34687+1792];bg[1792:0xf20]=p[51515:51515+2080];bg[0xf20:]=p[36479:36479+224] def tile(n,colors,patterns=bg): im=Image.new('RGBA',(8,8));im.putdata([colors[v] for v in decode(patterns[n*16:(n+1)*16])]);return im out=ROOT/'extracted/life';out.mkdir(parents=True,exist_ok=True) registry_path=ROOT/'assets/.prometeu/index.json';registry=json.loads(registry_path.read_text()) def register(root,uuid): found=[a for a in registry['assets'] if a['asset_uuid']==uuid or a['root']==root] if found: assert len(found)==1 and found[0]['asset_uuid']==uuid and found[0]['root']==root found[0]['included_in_build']=True;return found[0]['asset_id'] n=registry['next_asset_id'];registry['next_asset_id']+=1;registry['assets'].append({'asset_id':n,'asset_uuid':uuid,'root':root,'included_in_build':True});return n life_id=register('ui/life',LIFE_UUID) register('scenes/death_blank',BLANK_UUID);register('scenes/game_over',OVER_UUID) register('characters/death_sparks',SPARK_UUID) bank=ROOT/'assets/ui/life';bank.mkdir(parents=True,exist_ok=True) heart_colors=[(*rgb[c],255) for c in [15,22,39,54]] # One shared palette also includes white letters, black background and life red. sheet_colors=[(0,0,0,255),(*rgb[22],255),(*rgb[39],255),(*rgb[54],255),(*rgb[48],255)] arts=[] def artifact(root,name,image,items): path=root/(name+'.png');image.save(path);n=len(items);items.append({'file':path.name,'index':n,'artifact_id':name,'fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()});return n for n,name in [(242,'full'),(101,'half'),(102,'empty'),(36,'blank')]:artifact(bank,'heart-'+name,tile(n,heart_colors),arts) letters=[16,10,22,14,36,24,31,14,27] for i,n in enumerate(letters):artifact(bank,'game-over-'+str(i),tile(n,[(0,0,0,255),(*rgb[48],255),(*rgb[0],255),(*rgb[18],255)]),arts) write_json(bank/'asset.json',{'schema_version':1,'asset_uuid':LIFE_UUID,'name':'NES hearts and GAME OVER','type':'glyph_bank','output':{'format':'GLYPH/indexed_v1','codec':'NONE','pipeline':{'palettes':[{'index':0,'palette':{'rgba8888':rgba(sheet_colors)}}]},'metadata':{'tile_size':'8x8'}},'preload':{'enabled':True},'artifacts':arts}) write_json(bank/'studio.asset.json',{'schema_version':1,'glyph_bank_specialization':'tileset'}) for name,uuid,show in [('death_blank',BLANK_UUID,False),('game_over',OVER_UUID,True)]: root=ROOT/'assets/scenes'/name;root.mkdir(parents=True,exist_ok=True) tiles=[];canvas=Image.new('RGBA',(256,240),(0,0,0,255)) for y in range(30): for x in range(32): glyph=4+x-12 if show and y==18 and 12<=x<=20 else 3 tiles.append({'active':y>=8,'glyph_id':glyph,'palette_id':0,'flip_x':False,'flip_y':False}) if show and y==18 and 12<=x<=20:canvas.paste(Image.open(bank/('game-over-'+str(x-12)+'.png')),(x*8,y*8)) canvas.save(out/(name+'.png')) empty={'active':False,'glyph_id':3,'palette_id':0,'flip_x':False,'flip_y':False} def layer(active,w,h,t):return {'active':active,'glyph_asset_id':life_id,'tile_size':8,'parallax_factor_x':1.0,'parallax_factor_y':1.0,'width':w,'height':h,'tiles':t} write_json(root/'asset.json',{'schema_version':1,'asset_uuid':uuid,'name':name,'type':'scene_bank','output':{'format':'SCENE/tilemap_v1','codec':'NONE','pipeline':{}},'preload':{'enabled':True}}) write_json(root/'scene-bank.pack-request.json',{'schema_version':1,'format':'SCENE/tilemap_v1','version':1,'layers':[layer(True,32,30,tiles)]+[layer(False,1,1,[empty]) for _ in range(3)]}) spark=ROOT/'assets/characters/death_sparks';spark.mkdir(parents=True,exist_ok=True);spark_arts=[] for n in (98,100): im=Image.new('RGBA',(16,16));left=Image.new('RGBA',(8,16)) for dy in (0,8):left.paste(tile(n+dy//8,colors[1],p[32895:32895+1792]),(0,dy)) im.paste(left,(0,0));im.paste(left.transpose(Image.Transpose.FLIP_LEFT_RIGHT),(8,0));artifact(spark,'spark-'+str(n),im,spark_arts) write_json(spark/'asset.json',{'schema_version':1,'asset_uuid':SPARK_UUID,'name':'NES death sparks','type':'glyph_bank','output':{'format':'GLYPH/indexed_v1','codec':'NONE','pipeline':{'palettes':[{'index':0,'palette':{'rgba8888':rgba(colors[1])}}]},'metadata':{'tile_size':'16x16'}},'preload':{'enabled':True},'artifacts':spark_arts}) write_json(spark/'studio.asset.json',{'schema_version':1,'glyph_bank_specialization':'sprites'}) write_json(registry_path,registry) write_json(out/'rom-data.json',{'rom_sha256':ROM_SHA256,'heart_tiles':[242,101,102,36],'invincibility_counter':24,'invincibility_tick_divisor':2,'damage_palettes':nes,'ring_link_colors':[41,50,22],'life_slots':[24,25],'damage_fire':128,'source':'Link_BeHarmed, FormatHeartsInTextBuf, TakeHeartsNoSound, DecrementInvincibilityTimer, UpdateMode11Death in local disassembly.'}) print('Published dynamic hearts, seven Link palettes, death scenes and sparks') if __name__=='__main__':prepare()