Files
zelda-1-prometeu-port/tools/validate_life_captures.py
T

100 lines
5.3 KiB
Python

#!/usr/bin/env python3
"""Validate guest assertions, original heart placement, palettes and gameplay."""
import json
from PIL import Image,ImageChops
from extract_graphics import ROOT
from assemble_link import COLORS
OUT=ROOT/'extracted/life'
def canvas(background,packed,partial,owned,fixture=True):
expected=Image.open(ROOT/'extracted/hud/runtime_initial.png').convert('RGBA')
expected.paste(Image.open(background).convert('RGBA'),(112,15))
hud=Image.open(ROOT/('extracted/hud/hud.png' if owned else 'extracted/hud/hud_unarmed.png')).convert('RGBA')
# Original formatter emits top row backwards, then bottom row backwards.
for y in range(40,56):
for x in range(176,240):hud.putpixel((x,y),(0,0,0,255))
full=packed%16;containers=packed//16
for ordinal in range(16):
status='blank'
if packed and ordinal>=15-containers:
if ordinal<15-full:status='empty'
elif ordinal>15-full or partial>=128:status='full'
elif partial:status='half'
else:status='empty'
x=(7-ordinal if ordinal<8 else 15-ordinal)*8+176
y=40 if ordinal<8 else 48
hud.paste(Image.open(ROOT/f'assets/ui/life/heart-{status}.png').convert('RGBA'),(x,y))
expected.paste(hud,(112,15))
if fixture:expected.putpixel((0,0),(0,255,0,255))
return expected
def body(expected,direction,phase,x,y,palette):
im=Image.open(ROOT/f'assets/characters/link/walk_{direction}_{phase}.png').convert('RGBA')
entries=json.loads((ROOT/'assets/characters/link/asset.json').read_text())['output']['pipeline']['palettes']
colors=entries[palette]['palette']['rgba8888']
colors=[tuple(((c&0xffffffff)>>shift)&255 for shift in (24,16,8,0)) for c in colors]
lookup={color:colors[i] for i,color in enumerate(COLORS)}
im.putdata([lookup[v] for v in im.getdata()]);expected.alpha_composite(im,(112+x,15+y))
def compare(name,expected,path):
actual=Image.open(path).convert('RGBA')
differences=sum(v!=(0,0,0,0) for v in ImageChops.difference(actual,expected).getdata())
assert differences==0,(name,differences)
return {'name':name,'different_pixels':differences}
def main():
meta=json.loads((OUT/'fixture-cases.json').read_text());checks=[]
for item in meta['captures']:
name=item['name'];path=OUT/f'validation/fixture_{name}.png'
assert Image.open(path).convert('RGB').getpixel((0,0))==(0,255,0),name
if 'packed' in item:
expected=canvas(ROOT/'extracted/overworld/start/room.png',item['packed'],item['partial'],False)
palette=4 if '-r1-' in name else 5 if '-r2-' in name else 0
body(expected,'down',0,120,136,palette)
checks.append(compare(name,expected,path))
states={
'hurt':(120,34,127,1,'down','body'),
'immune':(120,34,127,2,'down','body'),
'immune_last':(152,34,127,3,'down','body'),
'hurt_again':(152,33,254,0,'down','body'),
'death_start':(184,32,0,0,'down','body'),
'death_spin':(184,32,0,0,'right','body'),
'death_grey':(184,32,0,6,'down','body'),
'death_spark':(184,32,0,0,'down','small'),
'death_large_spark':(184,32,0,0,'down','large'),
'death_blank':(184,32,0,0,'down','blank'),
'game_over':(184,32,0,0,'down','over'),
'continue_ready':(184,32,0,0,'down','over'),
'continued':(120,34,255,0,'up','continued')}
for name,(x,hv,hp,palette,direction,kind) in states.items():
bg=ROOT/'extracted/overworld/start/room.png'
if kind=='blank':bg=OUT/'death_blank.png'
elif kind=='over':bg=OUT/'game_over.png'
expected=canvas(bg,hv,hp,True)
if kind in ('body','continued'):body(expected,direction,0,x,144 if kind=='continued' else 136,palette)
elif kind in ('small','large'):
glyph='spark-98' if kind=='small' else 'spark-100'
expected.alpha_composite(Image.open(ROOT/f'assets/characters/death_sparks/{glyph}.png').convert('RGBA'),(112+x,149))
checks.append(compare(name,expected,OUT/f'validation/fixture_{name}.png'))
fire_checks=[]
for n in range(1,7):
path=OUT/('validation/play_first_fire_damage.png' if n==1 else f'validation/play_fire_hit_{n}.png')
actual=Image.open(path).convert('RGBA')
expected_status=[['full','full','half'],['full','full','empty'],['full','half','empty'],['full','empty','empty'],['half','empty','empty'],['empty','empty','empty']][n-1]
for i,status in enumerate(expected_status):
assert actual.crop((288+i*8,63,296+i*8,71)).tobytes()==Image.open(ROOT/f'assets/ui/life/heart-{status}.png').convert('RGBA').tobytes(),(n,status)
fire_checks.append({'hit':n,'hearts':expected_status})
expected=canvas(OUT/'game_over.png',32,0,False,False)
compare('natural-game-over',expected,OUT/'validation/play_game_over.png')
expected=canvas(ROOT/'extracted/overworld/start/room.png',34,255,False,False)
body(expected,'up',0,120,144,0)
compare('natural-continue',expected,OUT/'validation/play_continue.png')
report={'guest_arithmetic_cases':meta['arithmetic_cases'],'guest_final_frame':meta['final_frame'],
'fixture_captures':len(checks),'fixture_full_frame_differences':0,'checks':checks,
'natural_fire_hits':fire_checks,'natural_death_continue':True,'healthy_inventory_regression_frames':28,
'scope':'Fixture checks and full frames; real cave fire contacts and Start continue. Other heart/fairy world drops and full NES death fade/menu are outside this wave.'}
(OUT/'validation/results.json').write_text(json.dumps(report,indent=2)+'\n')
print(json.dumps({k:report[k] for k in ('guest_arithmetic_cases','fixture_captures','fixture_full_frame_differences','natural_death_continue','healthy_inventory_regression_frames')}))
if __name__=='__main__':main()