matrix-shooter-train / native-eiffel /test_lockstep_chunk.py
peih's picture
backup native-eiffel
5dd27d0 verified
Raw History Blame Contribute Delete
2.9 kB
"""Burst capture vs the browser renderer: chunk-level input, game state on every frame.
In a burst all of a chunk's input enters at its first frame, so both sides get
each chunk's input on the first drain only. No press and release share a chunk,
so nothing is split across frames on either side.
"""
import json, os, sys
import numpy as np
sys.path.insert(0, '/root/autodl-tmp/edenlive')
os.environ['EDEN_NATIVE_CAPTURE'] = '1'
from eden.apps.game2v import game_renderer, session
from eden.apps.game2v.game_renderer import GameCaptureWorker, GameRenderer
from eden.apps.game2v.game_timeline import GameTimeline
from capture_compat import install_capture_context
import native_capture
install_capture_context(game_renderer)
native_capture.install(game_renderer, session)
_l = GameRenderer._gpu_launch_options
GameRenderer._gpu_launch_options = lambda r: {**_l(r), 'executable_path': os.environ['GBDATA_CHROMIUM']}
P = lambda x, y, **k: dict(action='pointer', phase=k.get('phase', 'move'), x=x, y=y, button=k.get('button'), pressed=k.get('pressed'))
SCRIPT = {0: [{'action': 'enter', 'phase': 'down'}], 1: [{'action': 'enter', 'phase': 'up'}],
3: [{'action': 'w', 'phase': 'down'}, P(0.5, 0.5)], 5: [P(0.57, 0.48)], 6: [{'action': 'w', 'phase': 'up'}, P(0.62, 0.46)],
8: [P(0.5, 0.5, phase='button', button=0, pressed=True)], 9: [P(0.5, 0.5, phase='button', button=0, pressed=False)],
11: [{'action': 'k', 'phase': 'down'}], 13: [{'action': 'k', 'phase': 'up'}, P(0.44, 0.52)],
15: [{'action': 'a', 'phase': 'down'}], 17: [{'action': 'a', 'phase': 'up'}]}
def run(native):
os.environ['EDEN_NATIVE_CAPTURE'] = '1' if native else '0'
worker = GameCaptureWorker('eiffel-assault', width=960, height=544)
tl, out = GameTimeline('eiffel-assault'), []
for c in range(20):
events = list(SCRIPT.get(c, []))
def drain():
nonlocal events
batch, events = events, []
return batch
out.append(worker.capture(tl.begin_chunk(9 if c == 0 else 12, 24), duration_s=0.0, drain_events=drain))
kind = type(worker._renderer).__name__
worker.close()
return kind, out
kb, browser = run(False)
kn, native = run(True)
keys = ('x', 'z', 'y', 'yaw', 'pitch')
diffs = []
for bp, np_ in zip(browser, native):
for bf, nf in zip(bp.frames, np_.frames):
a, b = bf.ui_state or {}, nf.ui_state or {}
diffs.append(max([abs((a.get('player') or {}).get(k, 0) - (b.get('player') or {}).get(k, 0)) for k in keys] +
[float(a.get(k) != b.get(k)) for k in ('state', 'shots', 'ads', 'health', 'ammo', 'roundId', 'kills')]))
last = lambda p: {k: p[-1].frames[-1].ui_state.get(k) for k in ('state', 'shots', 'ads', 'health', 'ammo')}
print(json.dumps(dict(browser=kb, native=kn, frames=len(diffs), max_state_diff=max(diffs), browser_end=last(browser), native_end=last(native))))