diff --git a/psprecomp/games/dissidia/docs/testing.md b/psprecomp/games/dissidia/docs/testing.md index 7629818..a8ecb7d 100644 --- a/psprecomp/games/dissidia/docs/testing.md +++ b/psprecomp/games/dissidia/docs/testing.md @@ -1,7 +1,13 @@ # Automated battle tests (no menus, no display) -`tools/battle_test.py` boots the game headless (its own Xvfb display, real GPU if the box has one), walks the menus to a -Quick Battle (Squall vs a CPU fighter), plays a scripted pad input or starts moves directly, and quits. A private copy of the +`tools/battle_test.py` boots the game headless (its own Xvfb display, real GPU if the box has one), gets to a +Quick Battle (Squall vs a CPU fighter), plays a scripted pad input or starts moves directly, and quits. + +**Getting to the fight: the recorded walk is the default, the menu walk is off.** `run`, `compare`, `actions` and `scan` replay +the recorded walk (`--direct`, ~30 s, see "Skipping the menu walk" below). The picture-by-picture menu walk takes ~100 s and +is disabled: `walk_menus` raises unless `BT_MENU_WALK=1` is set (`battle_test.py ... --menu-walk` sets it). Use it only when +really needed: recording a walk for another character or stage, a runtime without `direct_battle.cpp`, or a tool that has no +`--direct` yet (`deck_battle.py`, `combo_timing.py`; `android_battle.py` without `--direct`). A private copy of the work folder's memory stick and a private settings file are used, so the user's saves and settings are never touched. Needs: Xvfb, python3 + Pillow, a save in `/ms0` (Quick Battle unlocked) and a runtime built with the test driver. @@ -218,12 +224,12 @@ Measured 2026-11-11 while building the Jecht combo timing tests (`tools/combo_ti (20 m) never reaches the dummy, so `place 3` first; HP attack is Square, bravery Circle (not Triangle). - Background long runs with `nohup` and send a Gotify message at the end (see ~/CLAUDE.md). -## Skipping the menu walk (`battle_test.py run --direct`) +## Skipping the menu walk (`--direct`, the default) The picture-by-picture menu walk takes about 100 s to reach the fight. `--direct` replays a recorded pad sequence inside the game (`runtime/direct_battle.cpp`) and gets to `[TEST] battle started` in about 30 s (Squall vs Lightning, stage 12, no screenshots until the HUD is up): - battle_test.py run --work W --script S --out /abs/dir --direct # tests/replays/quick_battle_walk.txt + battle_test.py run --work W --script S --out /abs/dir # tests/replays/quick_battle_walk.txt (the default) battle_test.py run ... --direct my_walk.txt --ff-menus 4 # another recording; --ff-menus defaults to 4 with --direct - **How**: the game polls the pad once per frame, and the poll number is the one clock that does not change with fast-forward. The first part is rule @@ -233,7 +239,7 @@ The picture-by-picture menu walk takes about 100 s to reach the fight. `--direct - **Speed limit**: 4x. The replay finishes at the same poll at 8x and unthrottled too (the poll count is deterministic), but loads run in host time, so the recorded gaps get too short and the game falls back to the menu or never enters the fight. 6x failed in a try as well. - **Flags** (test only, off by default): `PSPRECOMP_TEST_DIRECT_BATTLE=` replays a recording; `PSPRECOMP_TEST_PAD_RECORD=1` makes a normal harness walk - log its pad (`[PADREC] `, and `[PADREC] loaded `). Another character or stage: record a walk (`battle_test.py run --char X --stage N ...` with + log its pad (`[PADREC] `, and `[PADREC] loaded `). Another character or stage: record a walk (`battle_test.py run --menu-walk --char X --stage N ...` with `PSPRECOMP_TEST_PAD_RECORD=1`), then `tools/walk_extract.py run_30.log walk.txt` and pass `--direct walk.txt`. - The pad filter is the one in `framerate.cpp` (`jecht_grid_filter` calls `dissidia_walk_filter` first), and the save events are two counters in `psp_hle_utility.cpp` (`g_savedata_list_opened`, `g_savedata_completed`). diff --git a/psprecomp/games/dissidia/tools/battle_test.py b/psprecomp/games/dissidia/tools/battle_test.py index 4d725e6..5680c77 100755 --- a/psprecomp/games/dissidia/tools/battle_test.py +++ b/psprecomp/games/dissidia/tools/battle_test.py @@ -1,7 +1,11 @@ #!/usr/bin/env python3 -"""Automated battle tests: boot the game headless, walk the menus to a Quick Battle (Squall vs a CPU fighter), play a +"""Automated battle tests: boot the game headless, get to a Quick Battle (Squall vs a CPU fighter), play a scripted pad input in game time, quit, and compare the same script at several frame rates. +The fight is reached by replaying a recorded pad walk inside the game (--direct, the default: ~30 s). The old +picture-by-picture menu walk (~100 s) is off unless asked for with --menu-walk or BT_MENU_WALK=1; use it only when +really needed (recording a walk for another character or stage, a runtime without direct_battle.cpp). + battle_test.py run --work DIR --script FILE [--fps 120] [--out DIR] [--char Squall] [--timeout 240] battle_test.py compare --work DIR --script FILE [--rates 30,120] [--out DIR] battle_test.py actions --work DIR --actions 2422,2423,... | --all [--rates 30,120] [--gap 4500] [--who 0] [--hold TRIANGLE --stick 'x,y;x,y'] @@ -233,8 +237,28 @@ def walk_direct(g, ff_menus=4): raise RuntimeError('the battle never started (direct)') +DIRECT_WALK = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tests', 'replays', 'quick_battle_walk.txt') +MENU_WALK_OFF = ('the picture-by-picture menu walk is off: it takes ~100 s to reach the fight (and needs the HUD probes to match). ' + 'Use the recorded walk instead (battle_test.py run/compare/actions/scan do by default; --direct FILE for another recording). ' + 'Only when it is really needed (recording a walk for another character or stage, a runtime without direct_battle.cpp, ' + 'a tool with no --direct): BT_MENU_WALK=1 or battle_test.py --menu-walk. docs/testing.md "Skipping the menu walk".') + + +def direct_walk(a): + """the recorded walk a run replays (None = the menu walk, only with --menu-walk / BT_MENU_WALK=1)""" + if getattr(a, 'menu_walk', False) or (os.environ.get('BT_MENU_WALK') and not getattr(a, 'direct', None)): + os.environ['BT_MENU_WALK'] = '1' + return None + if not getattr(a, 'direct', None) and getattr(a, 'stage', 12) != 12: + print('[battle_test] note: the default recorded walk is stage 12; --stage %d needs a walk recorded for it (--direct FILE) ' + 'or --menu-walk' % a.stage, file=sys.stderr) + return getattr(a, 'direct', None) or DIRECT_WALK + + def walk_menus(g, char, tries=14, stage=12, ff_menus=0): - """boot -> battle. Raises when a stage does not come up.""" + """boot -> battle. Raises when a stage does not come up. Slow (~100 s): off unless BT_MENU_WALK=1 (see MENU_WALK_OFF).""" + if not os.environ.get('BT_MENU_WALK'): + raise RuntimeError(MENU_WALK_OFF) t_start = time.time() lap = lambda what: print('[battle_test] +%5.1f s %s' % (time.time() - t_start, what), file=sys.stderr) if os.environ.get('BT_TRACE') else None g.wait_log(r'Debug socket listening', 60) @@ -311,18 +335,28 @@ def walk_menus(g, char, tries=14, stage=12, ff_menus=0): raise RuntimeError('the battle never started') +def idle_script(out): + """a test script that does nothing: the runtime's test driver (and with it '[TEST] battle started', which every walk + waits for) only runs with PSPRECOMP_TEST_SCRIPT set, so a run with only --shots needs one""" + p = os.path.join(os.path.abspath(out), 'idle_script.txt') + with open(p, 'w') as f: + f.write('0 mark idle\n') + return p + + def run(a, fps=None): fps = fps or a.fps os.makedirs(a.out, exist_ok=True) + direct = direct_walk(a) xp, disp = start_xvfb() - g = Game(os.path.abspath(a.work), fps, a.out, disp, free_port(), script=os.path.abspath(a.script) if a.script else None, + g = Game(os.path.abspath(a.work), fps, a.out, disp, free_port(), script=os.path.abspath(a.script) if a.script else idle_script(a.out), record=os.path.abspath(a.record) if getattr(a, 'record', None) else None, extra_env=dict(({'PSPRECOMP_TEST_PLAYER_CHAR': str(CHAR_ID[a.char.lower()])} if a.char.lower() in CHAR_ID else {}), - **dict(({'PSPRECOMP_TEST_DIRECT_BATTLE': os.path.abspath(a.direct)} if getattr(a, 'direct', None) else {}), **(getattr(a, 'env', None) or {})))) + **dict(({'PSPRECOMP_TEST_DIRECT_BATTLE': os.path.abspath(direct)} if direct else {}), **(getattr(a, 'env', None) or {})))) try: g.start() t0 = time.time() - if getattr(a, 'direct', None): + if direct: walk_direct(g, ff_menus=4 if getattr(a, 'ff_menus', None) is None else a.ff_menus) else: walk_menus(g, a.char, stage=getattr(a, 'stage', 12), ff_menus=getattr(a, 'ff_menus', None) or 0) @@ -609,13 +643,18 @@ def scan_run(a, fps): """one battle at `fps`; RAM snapshots at the game times of --at (ms); returns {gt: bytes}""" os.makedirs(a.out, exist_ok=True) lo, hi = (int(x, 16) for x in a.range.split('-')) + direct = direct_walk(a) xp, disp = start_xvfb() g = Game(os.path.abspath(a.work), fps, a.out, disp, free_port(), script=os.path.abspath(a.script) if a.script else None, - extra_env={'PSPRECOMP_TEST_PLAYER_CHAR': str(CHAR_ID[a.char.lower()])}) + extra_env=dict({'PSPRECOMP_TEST_PLAYER_CHAR': str(CHAR_ID[a.char.lower()])}, + **({'PSPRECOMP_TEST_DIRECT_BATTLE': os.path.abspath(direct)} if direct else {}))) snaps = {} try: g.start() - walk_menus(g, a.char, stage=a.stage) + if direct: + walk_direct(g) + else: + walk_menus(g, a.char, stage=a.stage) for t in [int(x) for x in a.at.split(',')]: end = time.time() + a.timeout while g.alive() and game_ms(g) < t and time.time() < end: @@ -715,13 +754,15 @@ def main(argv): p.add_argument('--char', default='squall') p.add_argument('--stage', type=int, default=12, help='Quick Battle stage, 1-20 (1 = Random); default 12') p.add_argument('--timeout', type=int, default=240) + p.add_argument('--direct', nargs='?', const=DIRECT_WALK, + help='the recorded walk to replay inside the game (the default: tests/replays/quick_battle_walk.txt, ' + 'Squall vs Lightning, stage 12); needs a runtime with direct_battle.cpp') + p.add_argument('--menu-walk', dest='menu_walk', action='store_true', + help='the slow (~100 s) picture-by-picture menu walk instead; only when really needed (recording a walk for another --char / --stage)') + p.add_argument('--ff-menus', dest='ff_menus', type=int, default=None, help='menu trip at N x real time (recorded walk default 4; the menu walk default 0)') if name == 'run': p.add_argument('--fps', type=int, default=30) p.add_argument('--record', help='write the pad input of this session as a test script') - p.add_argument('--direct', nargs='?', const=os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'tests', 'replays', 'quick_battle_walk.txt'), - help='skip the menu walk: replay a recorded pad sequence inside the game (default tests/replays/quick_battle_walk.txt; ' - 'records Squall vs Lightning, stage 12); needs a runtime with direct_battle.cpp') - p.add_argument('--ff-menus', dest='ff_menus', type=int, default=None, help='menu trip at N x real time (--direct default 4; the picture walk default 0)') p.add_argument('--ff', action='store_true', help='fast-forward the test script too (as fast as frames render)') p.add_argument('--shots', help='comma separated seconds after the fight starts at which to save a screenshot (battle__.tga), then quit') p.add_argument('--watch', help='hex guest address: log which functions change it ([MEM_WATCH] lines)') @@ -746,6 +787,7 @@ def main(argv): p.add_argument('--speed-tol', dest='speed_tol', type=float, default=1.35) p.add_argument('--dist-tol', dest='dist_tol', type=float, default=1.5) p.add_argument('--record', default=None) + p.add_argument('--menu-walk', dest='menu_walk', action='store_true', help='the slow picture-by-picture menu walk (see run --menu-walk)') p = sub.add_parser('analyze') p.add_argument('logs', nargs='+') p.add_argument('--tolerance', type=float, default=0.5) @@ -761,6 +803,7 @@ def main(argv): p.add_argument('--timeout', type=int, default=120) p.add_argument('--ratio', type=float, default=1.5) p.add_argument('--min-rate', dest='min_rate', type=float, default=0.001) + p.add_argument('--menu-walk', dest='menu_walk', action='store_true', help='the slow picture-by-picture menu walk (see run --menu-walk)') p = sub.add_parser('gauge-drain', help='assist gauge idle drain takes the same time at every frame rate (parallel runs)') p.add_argument('--work', required=True) p.add_argument('--out', default=tempfile.mkdtemp(prefix='battle_test_'))