197 lines
8.3 KiB
Python
197 lines
8.3 KiB
Python
#!/usr/bin/env python3
|
|
"""Power cut-in character animation (80:BC27 .. 80:BE98): decode the per-character metasprite tables, dump them,
|
|
render every frame from ROM data, and validate against Mesen OAM/screenshot captures (re/lua/gfx_cast.lua).
|
|
|
|
usage: gfx_cutin.py ROM [--dump] [--render OUTDIR] [--validate CAPDIR_PATTERN]
|
|
--validate 're/runs/gfx/cutin_c{c}' one capture per character (TBG_POKE=221=4,E0=c, TBG_OAMLOG=1)
|
|
|
|
Tables (all bank $80, 16-bit pointers):
|
|
80:BD5B + 8c : FRAMES, SEQ, END (END unused) c = character 0-9
|
|
FRAMES : 16-bit pointers, one per frame (count = max frame index used by SEQ + 1)
|
|
frame : 6-byte sprite records until a record whose byte 0 is 00:
|
|
+0 flags (bit 0: 1 = large, 0 = small; nonzero = present). The cut-in runs with OBSEL size
|
|
mode 0, so large = 16x16 and small = 8x8
|
|
+1 unused by the cut-in code
|
|
+2 dx (8-bit, added to base X with wrap) +3 dy (8-bit, added to base Y)
|
|
+4 tile name (low 8 bits; name table 2) +5 OAM attribute: kept bits 7,6 (flip v/h);
|
|
the code forces priority 3, palette 3 (CGRAM row 11) and name-table bit 0 (AND F1 / OR 37)
|
|
SEQ : (frame index, duration) byte pairs until duration == 0
|
|
Placement: sprite k of a frame goes to OAM slot k; base X = $30 (caster P1) / $A0 (P2) ($7E:32AA), base Y = $40
|
|
($7E:32AB). Sprite sizes are set once from frame 0's records (80:BE99) and kept for the whole animation.
|
|
"""
|
|
import os
|
|
import 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, write_png # noqa: E402
|
|
|
|
TABLE = 0x80BD5B
|
|
SHEET_TABLE = 0x80BD07
|
|
PAL_TABLE = 0x8296F9
|
|
BASE_X = {0: 0x30, 1: 0xA0}
|
|
BASE_Y = 0x40
|
|
NAMES = ['Halloween', 'Mirurun', 'Shaman', 'Aladdin', 'Princess', 'Bit', 'Ninja', 'Wolfman', 'Dragon', 'Queen']
|
|
|
|
|
|
def w16(rom, a):
|
|
o = lorom(a)
|
|
return rom[o] | (rom[o + 1] << 8)
|
|
|
|
|
|
def ptr24(rom, a):
|
|
o = lorom(a)
|
|
return rom[o] | (rom[o + 1] << 8) | (rom[o + 2] << 16)
|
|
|
|
|
|
def records(rom, addr):
|
|
out = []
|
|
o = lorom(addr)
|
|
while rom[o] != 0:
|
|
out.append({'large': rom[o] & 1, 'flags': rom[o], 'b1': rom[o + 1], 'dx': rom[o + 2], 'dy': rom[o + 3],
|
|
'tile': rom[o + 4], 'attr': rom[o + 5]})
|
|
o += 6
|
|
return out
|
|
|
|
|
|
def character(rom, c):
|
|
frames_p, seq_p, end_p = (0x800000 | w16(rom, TABLE + 8 * c + 2 * k) for k in range(3))
|
|
seq = []
|
|
o = lorom(seq_p)
|
|
while rom[o + 1] != 0:
|
|
seq.append((rom[o], rom[o + 1]))
|
|
o += 2
|
|
nframes = max(f for f, _ in seq) + 1 if seq else 1
|
|
frames = [records(rom, 0x800000 | w16(rom, frames_p + 2 * k)) for k in range(nframes)]
|
|
frame_addrs = [0x800000 | w16(rom, frames_p + 2 * k) for k in range(nframes)]
|
|
return {'frames_ptr': frames_p, 'seq_ptr': seq_p, 'seq': seq, 'frames': frames, 'frame_addrs': frame_addrs}
|
|
|
|
|
|
def oam_for(ch, frame_index, player=0):
|
|
"""Predicted OAM low entries (x, y, tile, attr) and size bits for a displayed frame."""
|
|
sizes = [r['large'] for r in ch['frames'][0]]
|
|
out = []
|
|
for k, r in enumerate(ch['frames'][frame_index]):
|
|
attr = (r['attr'] & 0xF1) | 0x37
|
|
out.append(((r['dx'] + BASE_X[player]) & 0xFF, (r['dy'] + BASE_Y) & 0xFF, r['tile'], attr,
|
|
sizes[k] if k < len(sizes) else 0))
|
|
return out
|
|
|
|
|
|
def sheet_and_palette(rom, c, alt=False):
|
|
d, _, _ = decompress(rom, ptr24(rom, SHEET_TABLE + 4 * c))
|
|
n = c + 12 if alt else c
|
|
base = ptr24(rom, PAL_TABLE + 4 * (n >> 2)) + (n & 3) * 128
|
|
o = lorom(base)
|
|
return d, palette(rom[o:o + 32])
|
|
|
|
|
|
def render_frame(rom, ch, c, fi, scale=3):
|
|
"""Draw one frame on a 128x128 canvas around the base point (base at 32,32)."""
|
|
sheet, pal = sheet_and_palette(rom, c)
|
|
W = H = 128
|
|
img = bytearray(b'\x30\x30\x40' * (W * H))
|
|
sizes = [r['large'] for r in ch['frames'][0]]
|
|
# OAM order: lower slots in front -> draw from the last slot to the first
|
|
recs = list(enumerate(ch['frames'][fi]))
|
|
for k, r in reversed(recs):
|
|
size = 16 if (sizes[k] if k < len(sizes) else 0) else 8 # OBSEL size mode 0 during the cut-in
|
|
attr = r['attr']
|
|
hf, vf = attr & 0x40, attr & 0x80
|
|
x0 = (r['dx'] if r['dx'] < 128 else r['dx'] - 256) + 32
|
|
y0 = (r['dy'] if r['dy'] < 128 else r['dy'] - 256) + 32
|
|
for dy in range(size):
|
|
for dx in range(size):
|
|
tx = size - 1 - dx if hf else dx
|
|
ty = size - 1 - dy if vf else dy
|
|
tn = r['tile']
|
|
tn = (tn & 0xF0) + ((tn & 0x0F) + (tx >> 3)) % 16 + (ty >> 3) * 16
|
|
tn &= 0xFF
|
|
px = tile(sheet, tn * 32, 4)[(ty & 7) * 8 + (tx & 7)]
|
|
if px == 0:
|
|
continue
|
|
X, Y = x0 + dx, y0 + dy
|
|
if 0 <= X < W and 0 <= Y < H:
|
|
img[(Y * W + X) * 3:(Y * W + X) * 3 + 3] = bytes(pal[px])
|
|
if scale > 1:
|
|
S = scale
|
|
out = bytearray(W * S * H * S * 3)
|
|
for y in range(H * S):
|
|
for x in range(W * S):
|
|
o = ((y // S) * W + x // S) * 3
|
|
out[(y * W * S + x) * 3:(y * W * S + x) * 3 + 3] = img[o:o + 3]
|
|
return W * S, H * S, out
|
|
return W, H, img
|
|
|
|
|
|
def validate(rom, pattern):
|
|
"""For each logged frame, find which SEQ frame the OAM shows and check every cut-in sprite exactly."""
|
|
total_ok = total = 0
|
|
for c in range(10):
|
|
d = pattern.format(c=c)
|
|
p = os.path.join(d, 'oam.bin')
|
|
if not os.path.exists(p):
|
|
print(f'{NAMES[c]}: no capture')
|
|
continue
|
|
ch = character(rom, c)
|
|
data = open(p, 'rb').read()
|
|
nfr = len(data) // 0x220
|
|
shown = []
|
|
ok = bad = 0
|
|
for f in range(nfr):
|
|
o = data[f * 0x220:(f + 1) * 0x220]
|
|
# cut-in sprites: name table 2 + palette 3 + priority 3 => attr & 0x3F == 0x37
|
|
slots = [(o[4 * k], o[4 * k + 1], o[4 * k + 2], o[4 * k + 3], (o[512 + k // 4] >> ((k % 4) * 2 + 1)) & 1)
|
|
for k in range(128)]
|
|
if slots[0][3] & 0x3F != 0x37: # cut-in sprites start at OAM slot 0
|
|
continue
|
|
match = None
|
|
for fi in range(len(ch['frames'])):
|
|
pred = oam_for(ch, fi)
|
|
if slots[:len(pred)] == pred:
|
|
match = fi
|
|
break
|
|
if match is None:
|
|
bad += 1
|
|
else:
|
|
ok += 1
|
|
if not shown or shown[-1][0] != match:
|
|
shown.append([match, 1])
|
|
else:
|
|
shown[-1][1] += 1
|
|
seq_obs = ' '.join(f'{fi}x{n}' for fi, n in shown)
|
|
print(f'{c} {NAMES[c]:9s}: {ok}/{ok + bad} cut-in OAM frames match a table frame exactly; shown: {seq_obs}')
|
|
print(f' SEQ table: {" ".join(f"{fi}x{dur}" for fi, dur in ch["seq"])}')
|
|
total_ok += ok
|
|
total += ok + bad
|
|
print(f'total {total_ok}/{total}')
|
|
|
|
|
|
def dump(rom):
|
|
for c in range(10):
|
|
ch = character(rom, c)
|
|
print(f'== {c} {NAMES[c]} FRAMES {ch["frames_ptr"]:06X} SEQ {ch["seq_ptr"]:06X} '
|
|
f'sheet {ptr24(rom, SHEET_TABLE + 4 * c):06X}')
|
|
print(' seq (frame x duration): ' + ' '.join(f'{f}x{d}' for f, d in ch['seq']))
|
|
for fi, (fa, recs) in enumerate(zip(ch['frame_addrs'], ch['frames'])):
|
|
print(f' frame {fi} @ {fa:06X}: ' + ' '.join(
|
|
f'({"L" if r["large"] else "s"} {r["dx"] - 256 if r["dx"] > 127 else r["dx"]:+d},'
|
|
f'{r["dy"] - 256 if r["dy"] > 127 else r["dy"]:+d} t{r["tile"]:02X}'
|
|
f'{" h" if r["attr"] & 0x40 else ""}{" v" if r["attr"] & 0x80 else ""})' for r in recs))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
rom = open(sys.argv[1], 'rb').read()
|
|
if '--dump' in sys.argv:
|
|
dump(rom)
|
|
if '--render' in sys.argv:
|
|
out = sys.argv[sys.argv.index('--render') + 1]
|
|
os.makedirs(out, exist_ok=True)
|
|
for c in range(10):
|
|
ch = character(rom, c)
|
|
for fi in range(len(ch['frames'])):
|
|
w, h, img = render_frame(rom, ch, c, fi)
|
|
write_png(f'{out}/cutin_{c}_{NAMES[c].lower()}_f{fi}.png', w, h, img)
|
|
if '--validate' in sys.argv:
|
|
validate(rom, sys.argv[sys.argv.index('--validate') + 1])
|