73 lines
3.3 KiB
Python
73 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Render battle-animation assets from ROM data alone (+ locate their palettes in the ROM).
|
|
|
|
usage: anim_extract.py ROM OUTDIR [--captures re/runs/anim]
|
|
|
|
For every power capture p?? it finds the effect upload (VRAM word 1000 <- 7F:xxxx, see anim_analyze.py) and the
|
|
CGRAM rows 11/12 in use while effect sprites are on screen, searches the ROM for those 32-byte rows (raw BGR555),
|
|
and renders the effect sheet from decompress(93:8000 | 9A:C413) with the palette(s) read back from the ROM.
|
|
Also renders the READY/GO letters (OBJ sheet 9F:8000, palette row 15 of the battle CGRAM 85:EC27).
|
|
Writes OUTDIR/<pXX>_pal<row>.png, OUTDIR/readygo.png and OUTDIR/assets.txt (ROM addresses)."""
|
|
import glob, os, sys
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
from gfx_decomp import decompress, lorom # noqa: E402
|
|
from gfx_snes import palette, tile_sheet, write_png # noqa: E402
|
|
|
|
FR = 0x220 + 0x200 + 0x1000
|
|
|
|
|
|
def snes_addr(off):
|
|
return ((off // 0x8000) | 0x80) << 16 | (0x8000 + off % 0x8000)
|
|
|
|
|
|
def main():
|
|
rom = open(sys.argv[1], 'rb').read()
|
|
out = sys.argv[2]
|
|
caps = sys.argv[sys.argv.index('--captures') + 1] if '--captures' in sys.argv else \
|
|
os.path.join(os.path.dirname(__file__), '..', 'runs', 'anim')
|
|
os.makedirs(out, exist_ok=True)
|
|
w93 = decompress(rom, 0x938000)[0]
|
|
w9a = decompress(rom, 0x9AC413)[0]
|
|
rep = []
|
|
for d in sorted(glob.glob(f'{caps}/p??')):
|
|
name = os.path.basename(d)
|
|
up = None
|
|
for l in open(f'{d}/events.txt'):
|
|
e = l.split()
|
|
if e[1] == 'VDMA' and e[2] == 'vaddr=1000' and int(e[0]) > 90:
|
|
src, n = int(e[3][4:], 16), int(e[4][2:], 16)
|
|
if src >> 16 == 0x7F and (src & 0xFFFF) < 0xC000:
|
|
up = (src & 0xFFFF, n, int(e[0]))
|
|
break
|
|
if not up:
|
|
rep.append(f'{name}: no effect sheet upload')
|
|
continue
|
|
o, n, f = up
|
|
data = (w93[o:o + n] if o < 0x8000 else w9a[o - 0x8000:o - 0x8000 + n])
|
|
src = f'93:8000+{o:04X}' if o < 0x8000 else f'9A:C413+{o - 0x8000:04X}'
|
|
frames = open(f'{d}/frames.bin', 'rb').read()
|
|
nfr = len(frames) // FR
|
|
line = f'{name}: sheet {src} len {n:04X} (uploaded rel {f})'
|
|
for row in (11, 12):
|
|
# the row as seen ~30 frames after the upload
|
|
fi = min(f + 30, nfr - 1)
|
|
cg = frames[fi * FR + 0x220 + row * 32:fi * FR + 0x220 + row * 32 + 32]
|
|
hit = rom.find(cg[2:]) # colour 0 may differ (transparent)
|
|
where = f'{snes_addr(hit - 2):06X}' if hit >= 2 else 'runtime/not found'
|
|
w, h, img = tile_sheet(data, 4, palette(cg), cols=16)
|
|
write_png(f'{out}/{name}_pal{row}.png', w, h, img)
|
|
line += f' | row{row} pal @ {where}'
|
|
rep.append(line)
|
|
# READY/GO letters: OBJ name table 1 = decompress(9F:8000); palette 7 -> CGRAM row 15
|
|
obj = decompress(rom, 0x9F8000)[0]
|
|
o = lorom(0x85EC27) + 15 * 32
|
|
w, h, img = tile_sheet(obj[0xD0 * 32:0x100 * 32], 4, palette(rom[o:o + 32]), cols=16)
|
|
write_png(f'{out}/readygo.png', w, h, img)
|
|
rep.append('READY/GO: OBJ 9F:8000 tiles D0-FF, palette 85:EC27 row 15')
|
|
open(f'{out}/assets.txt', 'w').write('\n'.join(rep) + '\n')
|
|
print('\n'.join(rep))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|