Files
Neutron/tools/binary/grappler_camera.py

143 lines
6.2 KiB
Python

#!/usr/bin/env python3
"""Guarded Grappler follow-camera/lifetime projection; never executes originals.
Only the ordinary walking/air follow branches are modeled. Camera flags828/851,
Bubble movement2, scene-entry snap and whole frame scheduling remain separate.
"""
from __future__ import annotations
import argparse
from dataclasses import dataclass, replace
import hashlib
import json
from pathlib import Path
import struct
from checkpoint_script_state import EXE_SHA256, find_case, read_va
from pe_inspect import PE
from rocketship_motion_camera import add_axis, f32, rotate
SPANS = (
('equip',0x4297dd,0x429857,'8d8c915150a29692734293b0048429f77f8467e50ab4e5449e7110f9d3a060eb'),
('attach-camera',0x426d53,0x426da0,'8858a5eec86ee75575aafdfc6eadabc707432821a0db34cd3e6c663d21c423bf'),
('detach',0x429c10,0x429cf7,'aad9706ec19de0d162827fe957e91fb4a513423d6e6901de9a29e4c780f7df97'),
('cleanup',0x4287ac,0x42883b,'7f1a0574e1a04b1893e2599749616af049854408e1296a2f50e0510b6847d1e5'),
('ground-follow',0x439625,0x4398f2,'43ae070dd7f5b3de39cef33e9af25c2847a5e35bfa172dc479292e896ef14a88'),
('air-follow',0x439900,0x43a111,'895f27be2ad03f49230fe34e98753095285a61f52b4adbbb15f1dd7c868b2901'),
('air-eye-transform',0x43a5d0,0x43a750,'d903035a775b5f2436ea203bfa40a812335c7c75d038592ec766a374d78e47a0'),
('camera-ray',0x43b820,0x43b90f,'da79aee91ec334b7cba52edb6b3e0a016e6c553cace3a77a908f13c55af949ad'),
('camera-defaults',0x437740,0x43778e,'8a03dc8f9289f76b96be53a678e54729e345624fe98b6045e93236a303d3cb12'),
('action-guard',0x43aff0,0x43b0b6,'f439e7317ada3ffb0bc2a9dbfad19539e0faf6e97de463ebaa5b9825d27f7d82'),
('constructor-globals',0x422761,0x4227d9,'76d9d28d1a7d655cc48a435af2c77017e86c610a586356859a13e53ea33235df'),
)
VTABLES = ((0x120, 0x43a5d0), (0x124, 0x438bc0), (0x128, 0x439900),
(0x16c, 0x43b820), (0x170, 0x437740), (0x150, 0x43aff0),
(0x204, 0x429c10))
CONSTANTS = ((0x4afce8, 'd', 1.9), (0x4a2ad0, 'd', .2),
(0x4a0ce0, 'd', 1.2), (0x49d5ac, 'f', .75),
(0x4a3138, 'f', 1.5), (0x48e5e8, 'f', 45.511112213134766))
def collect(original):
pe = PE(find_case(Path(original), 'Neutron.exe'))
if hashlib.sha256(pe.data).hexdigest() != EXE_SHA256:
raise ValueError('Unexpected original EXE identity')
spans = []
for name, start, end, expected in SPANS:
digest = hashlib.sha256(read_va(pe, start, end-start)).hexdigest()
if digest != expected:
raise ValueError('Changed source span '+name)
spans.append(dict(name=name, va=hex(start), end=hex(end),
file_offset=hex(pe.offset(start-pe.base)), sha256=digest))
for slot, expected in VTABLES:
if struct.unpack('<I', read_va(pe, 0x4a3bdc+slot, 4))[0] != expected:
raise ValueError('Changed Jimmy virtual '+hex(slot))
for va, fmt, expected in CONSTANTS:
if struct.unpack('<'+fmt, read_va(pe, va, struct.calcsize(fmt)))[0] != expected:
raise ValueError('Changed camera constant '+hex(va))
if read_va(pe, 0x4ef6d8, 6) != b'SWING\0':
raise ValueError('Changed protected action literal')
return dict(source_sha256=EXE_SHA256, spans=spans,
vtables=[dict(slot=hex(s), target=hex(t)) for s,t in VTABLES],
constants=[dict(va=hex(v),type=f,value=x) for v,f,x in CONSTANTS],
scope='Static guards and bounded Python projection; no original execution')
@dataclass(frozen=True)
class CameraLocals:
eye: tuple = (0., 200., -350.)
look: tuple = (0., 80., 0.)
gains: tuple = (1.6, 1., 1.6)
def equip(camera):
"""4297DD writes six locals, leaves gains untouched."""
return replace(camera, eye=(0.,50.,-350.), look=(0.,150.,80.))
def attach(camera):
"""426D writes eye Y/Z and gains; retains eye X and all look coordinates."""
return replace(camera, eye=(camera.eye[0],400.,-650.), gains=(.8,1.,.8))
def detach(camera, attached=True):
"""429C10 is a strict no-op when its attached global is already false."""
if not attached:
return camera
return CameraLocals((0.,50.,-350.),(0.,130.,100.),(1.6,1.,1.6))
def cleanup(camera, attached=True):
"""4287FC detaches then invokes complete170 normal camera defaults."""
return CameraLocals()
def desired(camera, root=(0.,0.,0.), euler=(0.,0.,0.), turn_rate=0.,
movement_mode=0, adjustment=10.):
"""Return desired world eye/look and effective gains for ordinary branches.
Air eye helper43A5D0 flattens pitch and adds turn_rate to logical yaw.
Ground game384 uses complete logical Euler for both points. Values are
projections of guarded instructions, not original x87-bit identity.
"""
if movement_mode == 2:
raise ValueError('Bubble mode2 uses separate air smoothing branches')
if movement_mode == 0:
eye_local=(camera.eye[0],f32(camera.eye[1]-2*adjustment),camera.eye[2])
look_local=(camera.look[0],f32(camera.look[1]+1.9*adjustment),camera.look[2])
eye_rotation=euler
gains=(camera.gains[0],camera.gains[1]+.2,camera.gains[2])
else:
eye_local,look_local=camera.eye,camera.look
eye_rotation=(0.,add_axis(euler[1],turn_rate),euler[2])
gains=(1.2,2.,1.2)
eye=tuple(f32(a+b) for a,b in zip(root,rotate(eye_local,eye_rotation)))
look=tuple(f32(a+b) for a,b in zip(root,rotate(look_local,euler)))
return eye,look,gains
def follow(camera, old_eye, dt, hit=None, **kwargs):
"""One ordinary follow pass. Aim vector intentionally uses PRE-update eye."""
eye,look,gains=desired(camera,**kwargs)
ray_gain=1.
if hit is not None:
eye=tuple(f32(a-(a-b)*.75) for a,b in zip(look,hit))
ray_gain=1.5
updated=tuple(f32(old+(new-old)*gain*ray_gain*dt)
for old,new,gain in zip(old_eye,eye,gains))
return dict(eye=updated,look=look,desired_eye=eye,ray_gain=ray_gain,
aim_vector=tuple(f32(a-b) for a,b in zip(look,old_eye)))
def main():
cli=argparse.ArgumentParser(description=__doc__)
cli.add_argument('original',type=Path)
cli.add_argument('--output',type=Path)
args=cli.parse_args()
result=json.dumps(collect(args.original),indent=2)+'\n'
if args.output: args.output.write_text(result)
else: print(result,end='')
if __name__=='__main__': main()