Skip to content

3D engine that AI agents drive

Fix one-shot tiny timers and stale or misspelled input bindings (#2396)

Nguyen Xuan An52160fed6e99
diff --git a/changes/2396.md b/changes/2396.md
new file mode 100644
index 00000000..4d6f9ca7
--- /dev/null
+++ b/changes/2396.md
@@ -0,0 +1 @@
+- A one-shot game timer shorter than a nanosecond now fires, `unbindAction` and `unbindAxis` drop the last sample at once, and `bindAxis` refuses a misspelled field such as `deadzone` (#2396, #2397, #2398)
diff --git a/docs/input.md b/docs/input.md
index d4c9afbe..f57441e6 100644
--- a/docs/input.md
+++ b/docs/input.md
@@ -40,7 +40,7 @@ Events change the live state at any time, but game code reads a snapshot taken b
## Actions, axes and rebinding
-`bindAction(name, controls)` and `bindAxis(name, { bindings, deadZone })` add or replace a mapping at any time, which is rebinding. A binding is a control name or `{ control, scale }`. `unbindAction` and `unbindAxis` remove one. `bindAxis` also takes `smoothing` from 0 (default, none) up to but not including 1: each sample the axis moves `1 - smoothing` of the way to its target, so `0.5` halves the gap per fixed step. It is per sample, so it stays deterministic for a replay. Rebinding resets it.
+`bindAction(name, controls)` and `bindAxis(name, { bindings, deadZone })` add or replace a mapping at any time, which is rebinding. A binding is a control name or `{ control, scale }`. `unbindAction` and `unbindAxis` remove one, and reading the removed name throws at once. An axis definition with a field other than `bindings`, `deadZone` and `smoothing` is an error. `bindAxis` also takes `smoothing` from 0 (default, none) up to but not including 1: each sample the axis moves `1 - smoothing` of the way to its target, so `0.5` halves the gap per fixed step. It is per sample, so it stays deterministic for a replay. Rebinding resets it.
Reading a name that was never bound throws an error that shows the `bindAction` call to add.
diff --git a/src/game-rules.js b/src/game-rules.js
index d7098384..37b43a62 100644
--- a/src/game-rules.js
+++ b/src/game-rules.js
@@ -453,13 +453,11 @@ export function createGameRules(document, options = {}) {
}
function matchTimer(trigger, previousSeconds) {
- if (trigger.repeat) {
- const before = Math.floor(previousSeconds / trigger.seconds + TIMER_EPSILON);
- const now = Math.floor(elapsedSeconds / trigger.seconds + TIMER_EPSILON);
- return now > before ? {} : null;
- }
- const crossed = previousSeconds < trigger.seconds - TIMER_EPSILON && elapsedSeconds >= trigger.seconds - TIMER_EPSILON;
- return crossed ? {} : null;
+ // Counting whole intervals keeps a duration shorter than TIMER_EPSILON firing, which subtracting the epsilon would not.
+ const before = Math.floor(previousSeconds / trigger.seconds + TIMER_EPSILON);
+ const now = Math.floor(elapsedSeconds / trigger.seconds + TIMER_EPSILON);
+ if (trigger.repeat) return now > before ? {} : null;
+ return before < 1 && now >= 1 ? {} : null;
}
// A variable trigger fires when its expression turns true, not on every step it stays true.
diff --git a/src/input.js b/src/input.js
index a7818f62..12eca304 100644
--- a/src/input.js
+++ b/src/input.js
@@ -16,6 +16,8 @@ export const INPUT_LIMITS = Object.freeze({
defaultDeadZone: 0.15,
});
+const AXIS_FIELDS = ['bindings', 'deadZone', 'smoothing'];
+
function describe(value) {
return value === null ? 'null' : typeof value;
}
@@ -188,6 +190,10 @@ export function createInput(options = {}) {
function bindAxis(name, definition) {
assertAlive();
requireName(name, 'bindAxis name');
+ if (definition !== null && typeof definition === 'object' && !Array.isArray(definition)) {
+ const unknown = Object.keys(definition).find(field => !AXIS_FIELDS.includes(field));
+ if (unknown !== undefined) throw new RangeError(`Axis "${name}" has the unknown field "${unknown}". Use one of: ${AXIS_FIELDS.join(', ')}.`);
+ }
const bindings = Array.isArray(definition) ? definition : definition?.bindings;
const deadZone = (Array.isArray(definition) ? undefined : definition?.deadZone) ?? INPUT_LIMITS.defaultDeadZone;
const smoothing = (Array.isArray(definition) ? undefined : definition?.smoothing) ?? 0;
@@ -278,8 +284,14 @@ export function createInput(options = {}) {
unregisterSource,
bindAction,
bindAxis,
- unbindAction: name => actionBindings.delete(name),
- unbindAxis: name => axisBindings.delete(name),
+ unbindAction(name) {
+ snapshot.actions.delete(name);
+ return actionBindings.delete(name);
+ },
+ unbindAxis(name) {
+ snapshot.axes.delete(name);
+ return axisBindings.delete(name);
+ },
setControl,
press: controlName => setControl(controlName, 1),
release: controlName => setControl(controlName, 0),
diff --git a/tests/game-input-hunt.test.js b/tests/game-input-hunt.test.js
new file mode 100644
index 00000000..7827de65
--- /dev/null
+++ b/tests/game-input-hunt.test.js
@@ -0,0 +1,51 @@
+import test from 'node:test';
+import assert from 'node:assert/strict';
+import { createGameRules } from '../src/game-rules.js';
+import { createInput } from '../src/input.js';
+
+const STEP = 1 / 60;
+
+function countTimerFirings(seconds, repeat, steps) {
+ const rules = createGameRules({
+ version: 1,
+ variables: { fired: 0 },
+ rules: [{ when: { type: 'timer', seconds, repeat }, do: [{ type: 'add', variable: 'fired', value: 1 }] }],
+ });
+ for (let step = 0; step < steps; step++) rules.fixedUpdate(STEP);
+ return rules.state.variables.fired;
+}
+
+test('a one-shot timer shorter than a nanosecond fires once on the first step', () => {
+ assert.equal(countTimerFirings(1e-10, false, 10), 1);
+});
+
+test('one-shot timers fire once, on the step that reaches their time', () => {
+ assert.equal(countTimerFirings(0.05, false, 10), 1);
+ assert.equal(countTimerFirings(1, false, 59), 0);
+ assert.equal(countTimerFirings(1, false, 60), 1);
+ assert.equal(countTimerFirings(1, false, 600), 1);
+});
+
+test('unbindAction and unbindAxis drop the last sample at once', () => {
+ const input = createInput();
+ input.bindAction('jump', ['keyboard/Space']).bindAxis('move', ['keyboard/KeyD']);
+ input.press('keyboard/Space');
+ input.press('keyboard/KeyD');
+ input.sample();
+ assert.equal(input.wasPressed('jump'), true);
+ assert.equal(input.getAxis('move'), 1);
+ input.unbindAction('jump');
+ input.unbindAxis('move');
+ assert.throws(() => input.wasPressed('jump'), /No action named "jump"/);
+ assert.throws(() => input.getAxis('move'), /No axis named "move"/);
+});
+
+test('bindAxis refuses a misspelled field and names the fields to use', () => {
+ const input = createInput();
+ assert.throws(
+ () => input.bindAxis('move', { bindings: ['keyboard/KeyD'], deadzone: 0.5 }),
+ /Axis "move" has the unknown field "deadzone"\. Use one of: bindings, deadZone, smoothing\./,
+ );
+ assert.throws(() => createInput({ axes: { look: { bindings: ['mouse/x'], smoothness: 0.5 } } }), /unknown field "smoothness"/);
+ input.bindAxis('move', { bindings: ['keyboard/KeyD'], deadZone: 0.5, smoothing: 0 });
+});