Files

40 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""Dark (power 0x29) flashlight: compose the 42-sprite light cone (86:C48D) from ROM data and print/save its
pixel mask. usage: anim_dark.py ROM CAPDIR OUT.png (CAPDIR = an anim_cap.lua capture of power 29, for tile/flip per sprite)"""
import struct, sys, importlib.util, os, zlib
here = os.path.dirname(os.path.abspath(__file__))
def mod(name):
s = importlib.util.spec_from_file_location(name, os.path.join(here, name + '.py')); m = importlib.util.module_from_spec(s); s.loader.exec_module(m); return m
gd = mod('gfx_decomp')
rom = open(sys.argv[1], 'rb').read()
off = gd.lorom
dx = [struct.unpack('<h', rom[off(0x86C596) + 2 * i:][:2])[0] for i in range(42)]
dy = [struct.unpack('<h', rom[off(0x86C5EA) + 2 * i:][:2])[0] for i in range(42)]
sheet = gd.decompress(rom, 0x9AC413)[0][0x0800:0x0A00] # uploaded to OBJ name table 2 (tile 0x100 + n)
# tile/flip of each sprite slot from one captured frame (the code writes them once, 86:C4FF table)
FR = 0x220 + 0x200 + 0x800 + 0x800
cap = open(os.path.join(sys.argv[2], 'frames.bin'), 'rb').read()[180 * FR:180 * FR + 0x220]
slots = []
for i in range(128):
x, y, t, a = cap[4 * i:4 * i + 4]
if (a >> 1) & 7 == 4: slots.append((x, y, t, a))
x0 = min(s[0] for s in slots); y0 = min(s[1] for s in slots)
W, H = 48, 80
mask = [[0] * W for _ in range(H)]
def tile_px(n, px, py):
b = sheet[32 * n:32 * n + 32]
bit = 7 - px
return (b[2*py] >> bit & 1) | (b[2*py+1] >> bit & 1) << 1 | (b[16+2*py] >> bit & 1) << 2 | (b[17+2*py] >> bit & 1) << 3
for x, y, t, a in slots:
hf, vf = a >> 6 & 1, a >> 7 & 1
for py in range(8):
for px in range(8):
v = tile_px(t, 7 - px if hf else px, 7 - py if vf else py)
if v: mask[y - y0 + py][x - x0 + px] = v
for row in mask: print(''.join('.' if not v else ('Y' if v == 1 else 'd') for v in row))
# PNG (grey = colour index)
raw = b''.join(b'\0' + bytes(v * 17 for v in row) for row in mask)
def chunk(k, d): return struct.pack('>I', len(d)) + k + d + struct.pack('>I', zlib.crc32(k + d) & 0xffffffff)
png = b'\x89PNG\r\n\x1a\n' + chunk(b'IHDR', struct.pack('>IIBBBBB', W, H, 8, 0, 0, 0, 0)) + chunk(b'IDAT', zlib.compress(raw)) + chunk(b'IEND', b'')
open(sys.argv[3], 'wb').write(png)