#!/usr/bin/env python3 """Publish native font labels and a Link memcard icon, reusing current banks.""" import json,hashlib from PIL import Image from extract_graphics import ROOT,decode from nes_object_graphics import P from extract_start_room import write_json def prepare(): folder=ROOT/'assets/ui/continue';doc=json.loads((folder/'asset.json').read_text());palette=doc['output']['pipeline']['palettes'][0]['palette']['rgba8888'];colors=[tuple((v&0xffffffff)>>s&255 for s in (24,16,8,0)) for v in palette];ids={a['file']:a['index'] for a in doc['artifacts']} patterns=P[34687:34687+1792] for tile in range(37): name=f'bg_{tile:02x}.png' if name in ids:continue im=Image.new('RGBA',(8,8));im.putdata([colors[v] for v in decode(patterns[tile*16:(tile+1)*16])]);path=folder/name;im.save(path);index=len(doc['artifacts']);ids[name]=index;doc['artifacts'].append({'file':name,'index':index,'artifact_id':f'continue-bg-{tile:02x}','fingerprint':hashlib.sha256(path.read_bytes()).hexdigest()}) write_json(folder/'asset.json',doc) # One existing menu scene becomes the reusable black UI background. for n in range(3): p=ROOT/f'assets/scenes/continue_{n}/scene-bank.pack-request.json';s=json.loads(p.read_text()) for cell in s['layers'][0]['tiles']:cell.update(glyph_id=ids['bg_24.png'],palette_id=0) write_json(p,s) labels={'heading':'SELECT GAME','continue_game':'CONTINUE','new_game':'NEW GAME','save':'SAVE','retry':'RETRY','empty':'NO SAVE FOUND','invalid':'SAVE DATA INVALID','version':'SAVE VERSION UNSUPPORTED','read_error':'CANNOT READ SAVE','hint':'SELECT OPENS SAVE MENU','saving':'SAVING','saved':'GAME SAVED','memory':'MEMORY ONLY','not_saved':'NOT SAVED TO CARD','failed':'SAVE FAILED','unknown':'SAVE STATUS UNKNOWN','blocked':'CARD NOT WRITABLE','start':'PRESS START','retry_hint':'START RETURNS TO MENU','no_change':'PREVIOUS SAVE KEPT'} def codes(text):return [36 if c==' ' else ord(c)-ord('A')+10 if c.isalpha() else int(c) for c in text] lines=['import { Scene } from @zelda1:scene;'] for name,label in labels.items():lines.append(f'declare global text_{name}: array = ['+','.join(map(str,codes(label)))+'];') lines+=['declare global save_font: array = ['+','.join(str(ids[f'bg_{i:02x}.png']) for i in range(37))+'];','declare service SaveGraphics {',' fn line(chars: array, count: int, x: int,y: int,palette: int) -> void {',' for i: int from 0 until count { let code=chars[i] else 36; let glyph=save_font[code] else 9; Scene.emit_ui_sprite(glyph,palette,x+i*8,y,9,false,false,1); }',' }'] for name,label in labels.items():lines.append(f' fn {name}(x: int,y: int,palette: int) -> void {{ SaveGraphics.line(text_{name},{len(label)},x,y,palette); }}') lines+=[' fn cursor(x: int,y: int) -> void { Scene.emit_ui_sprite(10,6,x,y,9,false,false,2); }','}'] (ROOT/'src/screens/save/graphics.generated.pbs').write_text('\n'.join(lines)+'\n') # 32x32 4bpp icon, native Link sprite enlarged solely for memcard metadata. asset=ROOT/'assets/characters/link';art=json.loads((asset/'asset.json').read_text())['artifacts'][0];im=Image.open(asset/art['file']).convert('RGBA').crop((0,0,16,16)).resize((32,32),Image.Resampling.NEAREST) colors=[(0,0,0)];indices=[] for r,g,b,a in im.getdata(): c=(r,g,b) if a else (0,0,0) if c not in colors:colors.append(c) indices.append(colors.index(c)) assert len(colors)<=16 colors += [(0,0,0)]*(16-len(colors));data=[v for c in colors for v in c]+[(indices[i]<<4)|indices[i+1] for i in range(0,1024,2)];assert len(data)==560 lines=['declare global save_icon_bytes: array = ['+','.join(map(str,data))+'];','declare struct SaveIcon() {',' fn byte_at(i: int) -> int { return save_icon_bytes[i] else 0; }','}'];(ROOT/'src/save/icon.generated.pbs').write_text('\n'.join(lines)+'\n');print('Native save font and 560-byte memcard icon published') if __name__=='__main__':prepare()