upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/shims/gameinput_script.h
diff options
context:
space:
mode:
authorklikso <204526810+klikso@users.noreply.github.com>2026-11-25 22:32:55 +0000
committerklikso <204526810+klikso@users.noreply.github.com>2026-11-26 04:58:19 +0000
commitf0e79141a41b63e737560fbd4a051e3ab34dad7d (patch)
treec362afe3666e198ab112055ab794866b290a6687 /runtime/shims/gameinput_script.h
parenta73a57e1339be072baf5522351c8ec7f1482b56b (diff)
gameinput: a scripted press can be held longer (button@seconds:hold)
At world-start frame rates the retail title reads its pad every few seconds, so a 0.3 s scripted press falls between two reads and never arrives. An entry may now give its own hold time; without one the press stays 0.3 s. Holds of 0 or less, over 600 s or that are not a number reject the entry. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/gameinput_script.h')
-rw-r--r--runtime/shims/gameinput_script.h27
1 files changed, 18 insertions, 9 deletions
diff --git a/runtime/shims/gameinput_script.h b/runtime/shims/gameinput_script.h
index 759506750..f9042a2ae 100644
--- a/runtime/shims/gameinput_script.h
+++ b/runtime/shims/gameinput_script.h
@@ -5,10 +5,12 @@
5// 5//
6// LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120" 6// LEONIDA_INPUT_SCRIPT="a@70,a@85,start@120"
7// 7//
8// Each entry is button@seconds, the seconds counted from the shim's first update. A press is held for kHoldSeconds, 8// Each entry is button@seconds[:hold], the seconds counted from the shim's first update. A press is held for `hold`
9// then released. Buttons: a b x y start back lb rb up down left right; rsup and rsdown throw the right stick fully up 9// seconds (kHoldSeconds when the entry has none), then released: a longer hold reaches a title that reads its pad
10// only every few seconds, as the retail title does at world-start frame rates. Buttons: a b x y start back lb rb up down left right; rsup and rsdown throw the right stick fully up
10// or down for the same time (they replay the stick throws the press log writes). Spaces around an entry are allowed; 11// or down for the same time (they replay the stick throws the press log writes). Spaces around an entry are allowed;
11// empty entries, unknown buttons and invalid times (negative, non-finite, trailing text) are counted as rejected. 12// empty entries, unknown buttons, invalid times (negative, non-finite, trailing text) and invalid holds (zero,
13// negative, non-finite, more than kMaxHoldSeconds) are counted as rejected.
12#pragma once 14#pragma once
13#include <algorithm> 15#include <algorithm>
14#include <cctype> 16#include <cctype>
@@ -24,6 +26,7 @@
24namespace rt::gameinput::script { 26namespace rt::gameinput::script {
25 27
26constexpr double kHoldSeconds = 0.3; 28constexpr double kHoldSeconds = 0.3;
29constexpr double kMaxHoldSeconds = 600; // a typo such as b@10:1100 should not hold B for the whole run
27 30
28// One scriptable button: its name in the script and the press log, its GameInput gamepad bit, and its SDL3 31// One scriptable button: its name in the script and the press log, its GameInput gamepad bit, and its SDL3
29// SDL_GamepadButton number, which the "scripted press of button <n>" line has always carried (tools/stage_timeline.py 32// SDL_GamepadButton number, which the "scripted press of button <n>" line has always carried (tools/stage_timeline.py
@@ -46,6 +49,7 @@ struct Press {
46 uint32_t button = 0; // a GameInput gamepad bit, or 0 for a stick throw 49 uint32_t button = 0; // a GameInput gamepad bit, or 0 for a stick throw
47 int stick = 0; // +1 rsup, -1 rsdown (GameInput's +Y is up) 50 int stick = 0; // +1 rsup, -1 rsdown (GameInput's +Y is up)
48 double at = 0; // seconds 51 double at = 0; // seconds
52 double hold = kHoldSeconds;
49}; 53};
50 54
51struct Script { 55struct Script {
@@ -72,17 +76,22 @@ inline Script parse(std::string text) {
72 const char* time = item.c_str() + sep + 1; 76 const char* time = item.c_str() + sep + 1;
73 char* tail; 77 char* tail;
74 const double seconds = std::strtod(time, &tail); 78 const double seconds = std::strtod(time, &tail);
75 if (tail == time || !std::isfinite(seconds) || seconds < 0 || 79 if (tail == time || !std::isfinite(seconds) || seconds < 0) continue;
76 item.find_first_not_of(whitespace, static_cast<size_t>(tail - item.c_str())) != std::string::npos) 80 double hold = kHoldSeconds;
77 continue; 81 if (*tail == ':') {
82 const char* hold_text = tail + 1;
83 hold = std::strtod(hold_text, &tail);
84 if (tail == hold_text || !std::isfinite(hold) || hold <= 0 || hold > kMaxHoldSeconds) continue;
85 }
86 if (item.find_first_not_of(whitespace, static_cast<size_t>(tail - item.c_str())) != std::string::npos) continue;
78 const auto is = [&](const char* name) { return item.compare(first, sep - first, name) == 0; }; 87 const auto is = [&](const char* name) { return item.compare(first, sep - first, name) == 0; };
79 if (is("rsup") || is("rsdown")) { 88 if (is("rsup") || is("rsdown")) {
80 out.presses.push_back({0, is("rsup") ? 1 : -1, seconds}); 89 out.presses.push_back({0, is("rsup") ? 1 : -1, seconds, hold});
81 continue; 90 continue;
82 } 91 }
83 for (const Button& b : kButtons) { 92 for (const Button& b : kButtons) {
84 if (is(b.name)) { 93 if (is(b.name)) {
85 out.presses.push_back({b.bit, 0, seconds}); 94 out.presses.push_back({b.bit, 0, seconds, hold});
86 out.named |= b.bit; 95 out.named |= b.bit;
87 break; 96 break;
88 } 97 }
@@ -102,7 +111,7 @@ struct Held {
102inline Held held_at(const Script& script, double seconds) { 111inline Held held_at(const Script& script, double seconds) {
103 Held h; 112 Held h;
104 for (const Press& p : script.presses) { 113 for (const Press& p : script.presses) {
105 if (seconds < p.at || seconds >= p.at + kHoldSeconds) continue; 114 if (seconds < p.at || seconds >= p.at + p.hold) continue;
106 if (p.stick) h.stick = p.stick; 115 if (p.stick) h.stick = p.stick;
107 else h.buttons |= p.button; 116 else h.buttons |= p.button;
108 } 117 }