#!/usr/bin/env python3 """P45 character-animation analysis. Reads the 4x `p45-char-{a,b,c}.png` crops (real LV_P45_SHOT screenshots, cropped around the avatar cell) + the shot log and writes docs/evidence/p45-analysis.txt: 1. character source cell + on-screen dest rect + facing/frame per frame; 2. pairwise inter-frame difference: Δchanged_px (max-channel > 16) and mean |Δ|; 3. the deterministic frame sequence table (frame index -> source cell) per facing; 4. a palette check that the original pixel-art character really is in each crop. Exit 0 when the differences are significant, 1 otherwise, 2 on usage error. """ import re import sys from pathlib import Path import numpy as np from PIL import Image CELL_W = 32 CELL_H = 48 VARIANT_STRAW_HAT = 4 DIR_SOUTH = 1 DIR_EAST = 3 CHANGED_MIN = 400 LINE_RE = re.compile(r"p45-shot=(\S+) tick=(\d+) facing=(\S+) moving=(\w+) dest=(\d+),(\d+),(\d+),(\d+) frame=(.+)") SKIN = np.array([234, 194, 152]) ROBE = np.array([146, 96, 70]) STRAW = np.array([216, 190, 120]) def source_x(variant, direction, frame): return ((variant * 4 + direction) * 6 + frame) * CELL_W def load(path): return np.asarray(Image.open(path).convert("RGB"), dtype=int) def palette_hits(img): def near(ref): return (np.abs(img - ref).max(axis=2) <= 18).sum() return int(near(SKIN) + near(ROBE) + near(STRAW)) def main(argv): if len(argv) != 4: sys.stderr.write(__doc__) return 2 record_dir = Path(argv[1]) log_text = Path(argv[2]).read_text(encoding="utf-8") out_txt = Path(argv[3]) shots = [] for line in log_text.splitlines(): m = LINE_RE.search(line) if m: shots.append( { "name": m.group(1), "tick": int(m.group(2)), "facing": m.group(3), "moving": m.group(4) == "true", "dest": tuple(int(m.group(i)) for i in range(5, 9)), "frame": m.group(9), } ) if len(shots) < 3: sys.stderr.write("need three p45-shot frames\n") return 1 images = {s["name"]: load(record_dir / f"{s['name']}.png") for s in shots} lines = ["P45 character animation analysis (LV_P45_SHOT=1, 4x NEAREST crops)", ""] failures = [] for s in shots: x, y, w, h = s["dest"] hits = palette_hits(images[s["name"]]) lines.append(f"[{s['name']}] facing={s['facing']} moving={s['moving']} frame={s['frame']} " f"dest=({x},{y},{w}x{h}) palette_hits={hits}") if w != CELL_W or h != CELL_H: failures.append(f"{s['name']} dest cell is not 32x48") if hits < 50: failures.append(f"{s['name']} character palette not found ({hits})") lines.append("") lines.append("inter-frame delta (4x crop, aligned on the same avatar cell):") names = [s["name"] for s in shots] for i in range(len(names)): for j in range(i + 1, len(names)): a, b = images[names[i]], images[names[j]] d = np.abs(a - b) changed = int((d.max(axis=2) > 16).sum()) mean = float(d.mean()) lines.append(f" {names[i]} vs {names[j]}: changed_px(>16)={changed} mean_abs_diff={mean:.2f}") if changed < CHANGED_MIN: failures.append(f"{names[i]}-{names[j]} changed_px {changed} < {CHANGED_MIN}") lines.append("") lines.append("deterministic frame sequence (source cell x offsets, StrawHat variant):") for label, direction in (("SouthFacing", DIR_SOUTH), ("EastFacing", DIR_EAST)): offsets = [source_x(VARIANT_STRAW_HAT, direction, f) for f in range(6)] lines.append(f" {label}: walk1..4+idle1..2 -> {offsets}") lines.append("") lines.append("verdict = " + ("PASS" if not failures else "FAIL")) out_txt.parent.mkdir(parents=True, exist_ok=True) out_txt.write_text("\n".join(lines) + "\n", encoding="utf-8") sys.stdout.write("\n".join(lines) + "\n") if failures: for f in failures: sys.stderr.write(f"FAIL {f}\n") return 1 return 0 if __name__ == "__main__": raise SystemExit(main(sys.argv))