Add a sprint button: a setting that makes Link sprint without rolling first, as a toggle or while held.

This commit is contained in:
kendo
2026-11-12 20:50:16 +00:00
committed by chrg6
parent f3856c8dca
commit 6faf4bd7f5
14 changed files with 477 additions and 5 deletions

188
tests/sprint_check.c Normal file
View File

@@ -0,0 +1,188 @@
// The sprint button (runtime/sprint.c) against a stand-in Link: the presses and the button the game gets from each
// controller poll (sprint_pad), and the dash input the patch set's two host functions put into Link's flags, frame by
// frame, for toggle and hold, each button, Link's states and a Link the player does not control. Built by
// tests/sprint_check.py, which writes the stand-in layout.h and patches.h.
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
uint64_t test_main_base; // layout.h's MOD_MAIN_BASE: a buffer standing in for main's image
const int rt_game_version = 111;
#include "sprint.c"
static const char *button = "off", *mode = "toggle";
static GuestFn link_fn, stance_fn;
const char *cfg_value(const char *key) {
if (!strcmp(key, "NX2NAVI_SPRINT_BUTTON")) return button;
if (!strcmp(key, "NX2NAVI_SPRINT_MODE")) return mode;
return "";
}
int rt_patch_set(const char *name) { return strcmp(name, "sprint") ? -1 : 3; }
int rt_patch_enabled(int set) { return set == 3; }
void rt_register_host(uint64_t addr, GuestFn fn) {
if (addr == RT_PATCH_PAGE + 0xf40) link_fn = fn;
else if (addr == RT_PATCH_PAGE + 0xf50) stance_fn = fn;
else abort();
}
// Link: vtable, +0x18 -> owner whose +0x270 -> a with +0x188 -> b with +0x1c0 -> the state, id at +0x28
static uint64_t link[8], owner[0x50], a[0x40], b[0x40], state_obj[0x20], other[8];
static uint64_t manager[0x90], controlled[8];
static void set_state(int32_t id) { memcpy((char *)state_obj + 0x28, &id, 4); }
// One frame: a controller poll with these buttons, then Link's update. Returns what the game got from the poll.
static uint64_t poll_buttons, flags_x10, flags_w8;
static uint64_t frame(uint64_t buttons, int32_t state, uint64_t *who) {
set_state(state);
sprint_pad(&buttons);
poll_buttons = buttons;
Cpu c;
memset(&c, 0, sizeof c);
c.x[19] = (uintptr_t)who;
c.x[10] = 0x40; // other flag bits stay
link_fn(&c);
flags_x10 = c.x[10];
c.x[8] = 0x01;
stance_fn(&c);
flags_w8 = c.x[8];
return buttons;
}
static int dashing(void) { return (flags_x10 & 0x10) && (flags_w8 & 0x10) && (flags_x10 & 0x40) && (flags_w8 & 1); }
enum { WALK = 1, JUMP = 13, SLIDE = 35, WAIT_ID = 40, ROLL = 99, ATTACK = 112, CUTSCENE = 0 };
#define L3 (1u << 4)
#define BB (1u << 1)
#define LL (1u << 6)
#define ZL (1u << 8)
static void reset(const char *btn, const char *md) {
button = btn, mode = md;
for (int i = 0; i < 12; i++) frame(0, CUTSCENE, link); // the settings change and every sprint ends
assert(!dashing());
}
int main(void) {
char *img = calloc(1, 0x4c80000);
assert(img);
test_main_base = (uintptr_t)img;
assert(link_fn && stance_fn && sprint_available());
link[0] = test_main_base + 0x4b4dfc0, link[3] = (uintptr_t)owner;
other[0] = test_main_base + 0x4b4dfc0, other[3] = (uintptr_t)a; // Link's class, another owner
owner[0x270 / 8] = (uintptr_t)a, a[0x188 / 8] = (uintptr_t)b, b[0x1c0 / 8] = (uintptr_t)state_obj;
// the actor manager names the controlled Link (the one whose +0x18 is owner)
uint64_t *slot = (uint64_t *)(img + 0x4c7e0d0);
uint64_t mgr_ptr = (uintptr_t)manager;
*slot = (uintptr_t)&mgr_ptr;
manager[0] = test_main_base + 0x4bdfe68, manager[0x470 / 8] = (uintptr_t)controlled;
controlled[0] = test_main_base + 0x4b49698, controlled[3] = (uintptr_t)owner;
// off: nothing changes, the button reaches the game
reset("off", "toggle");
assert(frame(L3, WALK, link) == L3 && !dashing());
// toggle on L3: a press while walking starts it; the button never reaches the game during play
reset("l3", "toggle");
assert(frame(L3, WALK, link) == 0 && dashing());
assert(frame(L3, WALK, link) == 0 && dashing()); // held: no second press
assert(frame(0, WALK, link) == 0 && dashing());
assert(frame(0, JUMP, link) == 0 && dashing()); // on through a jump
assert(frame(0, SLIDE, link) == 0 && dashing());
frame(L3, WALK, link); // the next press ends it
assert(!dashing());
frame(0, WALK, link);
assert(!dashing());
// toggle: standing still ends it after 4 frames once it has moved
frame(L3, WALK, link), frame(0, WALK, link);
for (int i = 0; i < 4; i++) assert((frame(0, WAIT_ID, link), dashing()));
frame(0, WAIT_ID, link);
assert(!dashing());
// ... a press while standing waits up to 90 frames for Link to move
frame(L3, WAIT_ID, link), frame(0, WAIT_ID, link);
assert(dashing());
for (int i = 0; i < 60; i++) frame(0, WAIT_ID, link);
assert(dashing());
frame(0, WALK, link);
assert(dashing());
frame(L3, WALK, link);
assert(!dashing());
frame(L3, WAIT_ID, link), frame(0, WAIT_ID, link);
for (int i = 0; i < 90; i++) frame(0, WAIT_ID, link);
assert(!dashing());
// toggle: an attack, a roll, targeting or a cutscene ends it at once
const int32_t enders[] = {ATTACK, ROLL, CUTSCENE};
for (int k = 0; k < 3; k++) {
frame(L3, WALK, link), frame(0, WALK, link);
assert(dashing());
frame(0, enders[k], link);
assert(!dashing());
frame(0, WALK, link);
assert(!dashing());
}
frame(L3, WALK, link), frame(0, WALK, link);
assert(frame(ZL, WALK, link) == ZL && !dashing()); // ZL reaches the game and targets
// a press during an attack does not start it
frame(L3, ATTACK, link);
assert(!dashing());
// Link's class without the player's control: no dash input, and the controlled Link keeps his sprint
frame(0, WALK, link), frame(L3, WALK, link);
assert(dashing());
frame(0, WALK, other);
assert(!(flags_x10 & 0x10) && !(flags_w8 & 0x10));
frame(0, WALK, link);
assert(dashing());
frame(L3, WALK, link);
// a menu or a load: Link's update stops running, and after 8 polls the button reaches the game again
{
uint64_t got = 0;
sprint_pad(&got); // released first, so the next poll is a press
for (int i = 0; i < 9; i++) {
uint64_t bt = L3;
sprint_pad(&bt);
got = bt;
}
assert(got == L3);
frame(0, WALK, link); // the next update takes the press made meanwhile
assert(dashing());
frame(L3, WALK, link);
assert(!dashing());
}
// toggle on the bottom button: a sword press while not moving reaches the game and starts nothing
reset("b", "toggle");
assert(frame(BB, WAIT_ID, link) == BB && !dashing());
frame(0, WALK, link); // Link moves: from the next poll the button is the sprint's
assert(frame(BB, WALK, link) == 0 && dashing());
assert(frame(0, WALK, link) == 0 && dashing());
frame(BB, WALK, link);
assert(!dashing() && poll_buttons == 0);
frame(0, ATTACK, link);
assert(frame(BB, ATTACK, link) == BB && !dashing()); // not moving: an attack as usual
// hold on L: sprint while held; L never reaches the game during play, R does
reset("l", "hold");
assert(frame(LL, WALK, link) == 0 && dashing());
assert(frame(LL, WAIT_ID, link) == 0 && dashing());
assert(frame(0, WALK, link) == 0 && !dashing());
assert(frame(1u << 7, WALK, link) == (1u << 7) && !dashing());
// hold on the bottom button: starts only while moving, keeps going while held
reset("b", "hold");
frame(0, WALK, link);
assert(frame(BB, WALK, link) == 0 && dashing());
assert((frame(BB, JUMP, link), dashing()));
assert(frame(0, WALK, link) == 0 && !dashing());
frame(0, WAIT_ID, link);
assert(frame(BB, WAIT_ID, link) == BB && !dashing());
puts("sprint_check: ok");
free(img);
return 0;
}

53
tests/sprint_check.py Normal file
View File

@@ -0,0 +1,53 @@
"""Build and run tests/sprint_check.c: the sprint button (runtime/sprint.c) against a stand-in Link, with the rest of
the runtime stubbed. No game files: the module layout and patch page the runtime includes are stand-ins written to the
test's build folder.
Usage: sprint_check.py
"""
import os
import shutil
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parents[1]
MSYS_BIN = Path(r'C:\msys64\ucrt64\bin')
def compiler():
if os.environ.get('CC'):
return os.environ['CC']
if os.name == 'nt' and (MSYS_BIN / 'clang.exe').exists():
return str(MSYS_BIN / 'clang.exe')
return shutil.which('clang') or 'clang'
LAYOUT = '''// A stand-in for the generated layout.h (tests/sprint_check.py): main's image is a buffer of the test.
#pragma once
#include <stdint.h>
extern uint64_t test_main_base;
#define MOD_MAIN_BASE test_main_base
'''
PATCHES = '''// A stand-in for the generated patches.h (tests/sprint_check.py).
#pragma once
#define RT_PATCH_PAGE 0x10000ull
'''
def main():
work = ROOT / 'build' / 'test' / 'sprint'
work.mkdir(parents=True, exist_ok=True)
(work / 'layout.h').write_text(LAYOUT)
(work / 'patches.h').write_text(PATCHES)
exe = work / ('sprint_check.exe' if os.name == 'nt' else 'sprint_check')
env = dict(os.environ)
if os.name == 'nt' and MSYS_BIN.exists():
env['PATH'] = str(MSYS_BIN) + os.pathsep + env.get('PATH', '')
subprocess.check_call([compiler(), '-O1', '-Wall', '-Wno-unused-function', '-fno-strict-aliasing',
'-I', str(work), '-I', str(ROOT / 'runtime'),
str(ROOT / 'tests' / 'sprint_check.c'), '-o', str(exe)], env=env)
return subprocess.run([str(exe)], env=env).returncode
if __name__ == '__main__':
sys.exit(main())