First Game With Lock-On Targeting? Zelda in 1998, and Before
GamesRadar names The Legend of Zelda: Ocarina of Time, released in Japan on 21 November 1998, as the game where lock-on targeting was first seen, under the name Z-targeting. Mega Man Legends already had lock-on targeting eleven months earlier, and arcade shooters locked missiles onto targets by 1986, so the answer depends on what kind of lock you mean.
Who claims what?
| Source | Claim |
|---|---|
| GamesRadar (2010) | "First seen in: The Legend of Zelda: Ocarina of Time (1998)". Lock-on focused the camera on one enemy and let you strafe around it |
| Wikipedia, Ocarina of Time | More careful: the game "popularized" a target lock system, which has since become common in 3D adventure games |
| Wikipedia, Mega Man Legends | Released in Japan on 18 December 1997, with lock-on targeting among mechanics it compares to Tomb Raider, citing Next Generation magazine of October 1997 |
| Wikipedia, After Burner | A magazine review of November 1987 noted that its lock-on worked like the one in Data East's arcade game Lock-On from 1986 |
The claims cover different things. Ocarina's is about a button that holds the camera and the hero on one enemy in a 3D fight. The shooters' lock is about marking a target for homing shots.
Where did Z-targeting come from?
In an Iwata Asks interview for the remake on the Nintendo 3DS, two of Ocarina's developers remember it differently, and both are on the record.
- Toru Osawa says the problem was older: in Super Mario 64, trying to read a sign could leave you walking around it in circles. He and Koizumi went to Toei Kyoto Studio Park to watch sword-fight shows, and in one a ninja circled a samurai with a chain weapon pulled tight. His idea was "a kusarigama you can't see" between Link and the opponent: push the stick forward to close in, sideways to circle round.
- Yoshiaki Koizumi says they wanted a good way to hit an opponent in front of you while making Super Mario 64 and could not do it. What struck him at the studio was that the enemies "don't all attack at once. First, one attacks while the others wait." So "Z Targeting flags one particular opponent, telling the other enemies to wait", and after one falls you switch to the next.
Nintendo's own note in the interview adds that the button also let Link talk to characters at a distance. It was never only for fighting.
How did lock-on change over the years?
| When | Game | What changed | Source |
|---|---|---|---|
| 1986 | Lock-On (Data East, arcade) | Homing missiles that need a lock on the target first. By November 1987 a magazine compared After Burner's lock to it | Wikipedia, After Burner |
| February 1994 | RayForce | Its designer chose a lock-on laser that destroys several enemies at once, inspired by the bomb reticle in Xevious | Wikipedia, citing GAMEgene |
| 10 March 1995 | Panzer Dragoon | A homing laser locks onto up to eight enemies while the player sweeps the reticle over them and holds fire | Wikipedia, citing IGN |
| 24 October 1996 | Tomb Raider | When weapons are drawn, the hero locks onto nearby targets by herself, with no button. The sentence has no footnote | Wikipedia |
| 18 December 1997 | Mega Man Legends | A lock-on in a 3D action game with a camera the player controls | Wikipedia, citing Next Generation |
| 21 November 1998 | Ocarina of Time | Z-targeting: the camera follows the target and Link always faces it, and one enemy at a time is the one you fight | Wikipedia, Iwata Asks |
| 15 November 2001 | Halo: Combat Evolved | Help that is hidden on purpose. Designer Jaime Griesemer: "If it's an enemy, we can assume that when you slow down, you're trying to aim", and "We tried to conceal how much help we're giving the player" | Game Developer (2017), Wikipedia for the date |
| 18 November 2002 | Metroid Prime | Lock-on in a first-person game, letting the player circle while staying aimed | Wikipedia, citing GameSpot and IGN |
GamesRadar's 2010 article lists Metroid Prime among the games that took the idea from Ocarina, along with third-person shooters. A 2015 essay on CritPoints notes that Dark Souls has a hard lock but designs its kick and jump attacks to work without it.
What counts as lock-on?
The CritPoints essay separates a hard lock from a soft one, and shooters add a third kind. Each gives a different earliest game.
- Hard lock: a button turns character and camera to face a target. Mega Man Legends, December 1997, is the earliest in the sources we read; Ocarina of Time, November 1998, is the one GamesRadar names.
- Soft lock: the character turns to nearby enemies on its own. Tomb Raider, 1996, fits, on a sentence without a footnote.
- A weapon lock: a target is marked, then homing shots fly. Lock-On, 1986, fits, and by 1987 the press treated it as a known mechanic. We found no source for an earlier one.
What can we say with confidence?
Ocarina of Time, 1998, is the game the sources we read connect most with lock-on targeting, and its developers' own account of Z-targeting is on record. It is not the earliest game we can document with a lock: Mega Man Legends had a lock-on in December 1997, and arcade shooters locked missiles onto targets by 1986. What Ocarina's account adds is the reason: one enemy at a time, the others waiting.
Build it today
Our lock-on targeting blueprint is a hard lock in the sense above. A press fixes the camera and attacks on one target, a flick of the stick moves to the next, the way Koizumi describes switching after each fight, and a second press lets go. The first pick weighs what the player looks at against what is nearest, the lock survives a short loss of sight behind a pillar, and it never keeps a target that is gone. Two parts of this history are not in it. Telling the other enemies to wait is a rule for your enemies, not for the lock, and Halo's hidden aim help or a multi-target missile lock would be changes to ask your AI for on top.
- 19 tests pass on the reference code.
- The state diagram was checked against the code, at the default values and at every listed low and high value.
- Checked with TLA+, Z3 and Lean, with what the tools printed
Add "Lock-on Targeting" to my game.
What it should do: In a fight with several enemies, the player should not have to aim by hand while dodging. Locking onto one enemy makes attacks and the camera follow it, and the rules for picking and keeping that enemy decide whether the lock feels helpful or fights the player.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> free
free: No target
locked: Locked on
losing: Losing it
free --> free: nothing to lock
free --> locked: press locks a target
locked --> locked: keeps or switches
locked --> free: released or none left
locked --> losing: target out of reach
losing --> losing: waits for it
losing --> locked: back in sight or replaced
losing --> free: grace ran out or let go
```
Use these starting values:
- lockRange: 12 units
- breakRange: 16 units
- coneHalfAngle: 50 degrees
- graceTime: 1.5 s
- angleWeight: 1
Watch out for these common mistakes:
- Holding the button: Tell your AI: find the press yourself by comparing with the last tick, and act only on the new press.
- Holding the stick: Tell your AI: one push is one switch, and the next switch needs the stick back in the middle.
- The seam in the angles: Tell your AI: wrap the difference of two angles into the range minus 180 to 180 before comparing.
- A target that is gone: Tell your AI: look the target up every tick, and when it is gone, take the nearest one in reach or let go.
- Dropping the lock at the first pillar: Tell your AI: give the lock a grace time, and start it over when the target is back.
- Two enemies in a line: Tell your AI: allow a target with the same angle as a switch target, the nearer one first.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.
The code, from https://spielerando.com/en/mechanics/lock-on-targeting (TypeScript, tested):
```ts
/* Lock-on targeting: a button locks the camera and attacks onto one target, a flick of the stick
moves to the next one, and the lock holds until it is released, the target is gone or it has been
out of reach too long. Choosing a first target prefers what the player looks at over what is
merely nearest: distance and angle both count. Angles are compared around the circle, so looking
at the left edge still finds a target on either side of it. The button and the stick come in as
levels (held or not); the mechanic finds the press itself, so holding a button locks once and
holding the stick switches once.
Order inside one tick: the press is handled first (it always lets go of a lock), then what became
of the current target, then a switch. A target that is no longer in the list is gone: the lock
moves to the nearest one in reach, or lets go. It never keeps an id that is not there. */
export interface Params {
/** How far away a target can be to be locked onto, units. */
lockRange: number;
/** How far away the locked target can be before it counts as out of reach, units. Counts as at least lockRange. */
breakRange: number;
/** How far to either side of the aim a first lock looks, degrees. */
coneHalfAngle: number;
/** How long a target may stay out of reach or out of sight before the lock lets go, s. */
graceTime: number;
/** How much the angle counts against distance when choosing the first target. 0 is nearest only. */
angleWeight: number;
}
export const DEFAULT_PARAMS: Params = { lockRange: 12, breakRange: 16, coneHalfAngle: 50, graceTime: 1.5, angleWeight: 1 };
export interface Target {
id: number;
x: number;
y: number;
/** A clear line to it. */
visible: boolean;
}
export interface State {
/** The locked target, or null. */
targetId: number | null;
/** The locked target is out of reach or out of sight right now. */
losing: boolean;
/** Seconds the locked target has been out of reach or sight. */
lostFor: number;
/** The stick was back in the middle since the last switch. */
switchReady: boolean;
/** The lock button was held in the last tick. */
lockWasHeld: boolean;
}
export interface Input {
/** The lock button is held down. */
lockHeld: boolean;
/** The stick pushed to a side: 1 turns counterclockwise (to the left when looking along the x axis), -1 clockwise, 0 is the middle. */
switchDir: -1 | 0 | 1;
/** Where the player looks, in radians. 0 is along the x axis, counterclockwise. */
aim: number;
player: { x: number; y: number };
targets: readonly Target[];
}
export type Phase = "free" | "locked" | "losing";
export function initialState(): State {
return { targetId: null, losing: false, lostFor: 0, switchReady: true, lockWasHeld: false };
}
export function phase(state: State): Phase {
if (state.targetId === null) return "free";
return state.losing ? "losing" : "locked";
}
/** The shortest signed turn from b to a, from just above -PI up to PI. */
export function wrap(angle: number): number {
const full = 2 * Math.PI;
let a = angle % full;
if (a > Math.PI) a -= full;
if (a <= -Math.PI) a += full;
return a;
}
const finite = (t: Target) => Number.isFinite(t.x) && Number.isFinite(t.y);
const distance = (input: Input, t: Target) => Math.hypot(t.x - input.player.x, t.y - input.player.y);
const bearing = (input: Input, t: Target) => Math.atan2(t.y - input.player.y, t.x - input.player.x);
const inReach = (input: Input, t: Target, params: Params) => finite(t) && t.visible && distance(input, t) <= params.lockRange;
const keepable = (input: Input, t: Target, params: Params) =>
finite(t) && t.visible && distance(input, t) <= Math.max(params.breakRange, params.lockRange);
/** The best first target: close to the aim and not too far. Ties go to the lower id. */
function acquire(input: Input, params: Params): Target | null {
if (!Number.isFinite(input.aim)) return null;
const cone = (params.coneHalfAngle * Math.PI) / 180;
let best: Target | null = null;
let bestScore = Infinity;
for (const t of input.targets) {
if (!inReach(input, t, params)) continue;
const off = Math.abs(wrap(bearing(input, t) - input.aim));
if (off > cone) continue;
const score = distance(input, t) / params.lockRange + (params.angleWeight * off) / cone;
if (score < bestScore - 1e-12 || (Math.abs(score - bestScore) <= 1e-12 && best !== null && t.id < best.id)) {
best = t;
bestScore = score;
}
}
return best;
}
/** The nearest target in reach, whatever the aim. */
function nearest(input: Input, params: Params): Target | null {
let best: Target | null = null;
for (const t of input.targets) {
if (!inReach(input, t, params)) continue;
if (best === null || distance(input, t) < distance(input, best) || (distance(input, t) === distance(input, best) && t.id < best.id)) best = t;
}
return best;
}
/** The next target around the player in a direction, starting from the current one. */
function neighbour(input: Input, params: Params, current: Target, dir: 1 | -1): Target | null {
let best: Target | null = null;
let bestTurn = Infinity;
for (const t of input.targets) {
if (t.id === current.id || !inReach(input, t, params)) continue;
const turn = dir * wrap(bearing(input, t) - bearing(input, current));
if (turn < -1e-9) continue;
if (turn < bestTurn - 1e-9 || (Math.abs(turn - bestTurn) <= 1e-9 && best !== null && distance(input, t) < distance(input, best))) {
best = t;
bestTurn = turn;
}
}
return best;
}
export function step(state: State, input: Input, params: Params, dt: number): State {
const time = Number.isFinite(dt) && dt > 0 ? dt : 0;
const pressed = input.lockHeld && !state.lockWasHeld;
const flick = input.switchDir !== 0 && state.switchReady;
let { targetId, losing, lostFor } = state;
if (targetId !== null && pressed) {
// Pressing again lets go, wherever the target is.
targetId = null;
losing = false;
lostFor = 0;
} else if (targetId === null) {
if (pressed) targetId = acquire(input, params)?.id ?? null;
} else {
const current = input.targets.find((t) => t.id === targetId);
if (!current) {
// The target is gone: take the nearest one in reach, or let go.
targetId = nearest(input, params)?.id ?? null;
losing = false;
lostFor = 0;
} else if (!keepable(input, current, params)) {
losing = true;
lostFor += time;
if (lostFor >= params.graceTime) {
targetId = null;
losing = false;
lostFor = 0;
}
} else {
losing = false;
lostFor = 0;
if (flick && (input.switchDir === 1 || input.switchDir === -1)) targetId = neighbour(input, params, current, input.switchDir)?.id ?? targetId;
}
}
return { targetId, losing, lostFor, switchReady: input.switchDir === 0, lockWasHeld: input.lockHeld };
}
```
Add "Lock-on Targeting" to my game.
What it should do: In a fight with several enemies, the player should not have to aim by hand while dodging. Locking onto one enemy makes attacks and the camera follow it, and the rules for picking and keeping that enemy decide whether the lock feels helpful or fights the player.
Build it exactly as this state machine. First check that it fits my game, then build it like this:
```mermaid
stateDiagram-v2
[*] --> free
free: No target
locked: Locked on
losing: Losing it
free --> free: nothing to lock
free --> locked: press locks a target
locked --> locked: keeps or switches
locked --> free: released or none left
locked --> losing: target out of reach
losing --> losing: waits for it
losing --> locked: back in sight or replaced
losing --> free: grace ran out or let go
```
Use these starting values:
- lockRange: 12 units
- breakRange: 16 units
- coneHalfAngle: 50 degrees
- graceTime: 1.5 s
- angleWeight: 1
Watch out for these common mistakes:
- Holding the button: Tell your AI: find the press yourself by comparing with the last tick, and act only on the new press.
- Holding the stick: Tell your AI: one push is one switch, and the next switch needs the stick back in the middle.
- The seam in the angles: Tell your AI: wrap the difference of two angles into the range minus 180 to 180 before comparing.
- A target that is gone: Tell your AI: look the target up every tick, and when it is gone, take the nearest one in reach or let go.
- Dropping the lock at the first pillar: Tell your AI: give the lock a grace time, and start it over when the target is back.
- Two enemies in a line: Tell your AI: allow a target with the same angle as a switch target, the nearer one first.
Do not change anything else in the game. Do not claim anything without proof: model what it takes, and show me what your solution rests on and how I can see that it works. If you cannot show something, say you do not know. When you are done, tell me in plain words how to try it.The tested code (TypeScript)
/* Lock-on targeting: a button locks the camera and attacks onto one target, a flick of the stick
moves to the next one, and the lock holds until it is released, the target is gone or it has been
out of reach too long. Choosing a first target prefers what the player looks at over what is
merely nearest: distance and angle both count. Angles are compared around the circle, so looking
at the left edge still finds a target on either side of it. The button and the stick come in as
levels (held or not); the mechanic finds the press itself, so holding a button locks once and
holding the stick switches once.
Order inside one tick: the press is handled first (it always lets go of a lock), then what became
of the current target, then a switch. A target that is no longer in the list is gone: the lock
moves to the nearest one in reach, or lets go. It never keeps an id that is not there. */
export interface Params {
/** How far away a target can be to be locked onto, units. */
lockRange: number;
/** How far away the locked target can be before it counts as out of reach, units. Counts as at least lockRange. */
breakRange: number;
/** How far to either side of the aim a first lock looks, degrees. */
coneHalfAngle: number;
/** How long a target may stay out of reach or out of sight before the lock lets go, s. */
graceTime: number;
/** How much the angle counts against distance when choosing the first target. 0 is nearest only. */
angleWeight: number;
}
export const DEFAULT_PARAMS: Params = { lockRange: 12, breakRange: 16, coneHalfAngle: 50, graceTime: 1.5, angleWeight: 1 };
export interface Target {
id: number;
x: number;
y: number;
/** A clear line to it. */
visible: boolean;
}
export interface State {
/** The locked target, or null. */
targetId: number | null;
/** The locked target is out of reach or out of sight right now. */
losing: boolean;
/** Seconds the locked target has been out of reach or sight. */
lostFor: number;
/** The stick was back in the middle since the last switch. */
switchReady: boolean;
/** The lock button was held in the last tick. */
lockWasHeld: boolean;
}
export interface Input {
/** The lock button is held down. */
lockHeld: boolean;
/** The stick pushed to a side: 1 turns counterclockwise (to the left when looking along the x axis), -1 clockwise, 0 is the middle. */
switchDir: -1 | 0 | 1;
/** Where the player looks, in radians. 0 is along the x axis, counterclockwise. */
aim: number;
player: { x: number; y: number };
targets: readonly Target[];
}
export type Phase = "free" | "locked" | "losing";
export function initialState(): State {
return { targetId: null, losing: false, lostFor: 0, switchReady: true, lockWasHeld: false };
}
export function phase(state: State): Phase {
if (state.targetId === null) return "free";
return state.losing ? "losing" : "locked";
}
/** The shortest signed turn from b to a, from just above -PI up to PI. */
export function wrap(angle: number): number {
const full = 2 * Math.PI;
let a = angle % full;
if (a > Math.PI) a -= full;
if (a <= -Math.PI) a += full;
return a;
}
const finite = (t: Target) => Number.isFinite(t.x) && Number.isFinite(t.y);
const distance = (input: Input, t: Target) => Math.hypot(t.x - input.player.x, t.y - input.player.y);
const bearing = (input: Input, t: Target) => Math.atan2(t.y - input.player.y, t.x - input.player.x);
const inReach = (input: Input, t: Target, params: Params) => finite(t) && t.visible && distance(input, t) <= params.lockRange;
const keepable = (input: Input, t: Target, params: Params) =>
finite(t) && t.visible && distance(input, t) <= Math.max(params.breakRange, params.lockRange);
/** The best first target: close to the aim and not too far. Ties go to the lower id. */
function acquire(input: Input, params: Params): Target | null {
if (!Number.isFinite(input.aim)) return null;
const cone = (params.coneHalfAngle * Math.PI) / 180;
let best: Target | null = null;
let bestScore = Infinity;
for (const t of input.targets) {
if (!inReach(input, t, params)) continue;
const off = Math.abs(wrap(bearing(input, t) - input.aim));
if (off > cone) continue;
const score = distance(input, t) / params.lockRange + (params.angleWeight * off) / cone;
if (score < bestScore - 1e-12 || (Math.abs(score - bestScore) <= 1e-12 && best !== null && t.id < best.id)) {
best = t;
bestScore = score;
}
}
return best;
}
/** The nearest target in reach, whatever the aim. */
function nearest(input: Input, params: Params): Target | null {
let best: Target | null = null;
for (const t of input.targets) {
if (!inReach(input, t, params)) continue;
if (best === null || distance(input, t) < distance(input, best) || (distance(input, t) === distance(input, best) && t.id < best.id)) best = t;
}
return best;
}
/** The next target around the player in a direction, starting from the current one. */
function neighbour(input: Input, params: Params, current: Target, dir: 1 | -1): Target | null {
let best: Target | null = null;
let bestTurn = Infinity;
for (const t of input.targets) {
if (t.id === current.id || !inReach(input, t, params)) continue;
const turn = dir * wrap(bearing(input, t) - bearing(input, current));
if (turn < -1e-9) continue;
if (turn < bestTurn - 1e-9 || (Math.abs(turn - bestTurn) <= 1e-9 && best !== null && distance(input, t) < distance(input, best))) {
best = t;
bestTurn = turn;
}
}
return best;
}
export function step(state: State, input: Input, params: Params, dt: number): State {
const time = Number.isFinite(dt) && dt > 0 ? dt : 0;
const pressed = input.lockHeld && !state.lockWasHeld;
const flick = input.switchDir !== 0 && state.switchReady;
let { targetId, losing, lostFor } = state;
if (targetId !== null && pressed) {
// Pressing again lets go, wherever the target is.
targetId = null;
losing = false;
lostFor = 0;
} else if (targetId === null) {
if (pressed) targetId = acquire(input, params)?.id ?? null;
} else {
const current = input.targets.find((t) => t.id === targetId);
if (!current) {
// The target is gone: take the nearest one in reach, or let go.
targetId = nearest(input, params)?.id ?? null;
losing = false;
lostFor = 0;
} else if (!keepable(input, current, params)) {
losing = true;
lostFor += time;
if (lostFor >= params.graceTime) {
targetId = null;
losing = false;
lostFor = 0;
}
} else {
losing = false;
lostFor = 0;
if (flick && (input.switchDir === 1 || input.switchDir === -1)) targetId = neighbour(input, params, current, input.switchDir)?.id ?? targetId;
}
}
return { targetId, losing, lostFor, switchReady: input.switchDir === 0, lockWasHeld: input.lockHeld };
}
The tests the reference code passes
import assert from "node:assert/strict";
import { readFileSync } from "node:fs";
import test from "node:test";
import { chance, forAll, int, pick, real, seeded, type Rng } from "../../../proofs/property.ts";
import { DEFAULT_PARAMS, type Input, type Params, type State, type Target, initialState, phase, step, wrap } from "./reference.ts";
const P = DEFAULT_PARAMS;
const DT = 1 / 60;
const at = (id: number, x: number, y: number, visible = true): Target => ({ id, x, y, visible });
const view = (targets: Target[], over: Partial<Input> = {}): Input => ({ lockHeld: false, switchDir: 0, aim: 0, player: { x: 0, y: 0 }, targets, ...over });
const press = (targets: Target[], over: Partial<Input> = {}) => view(targets, { lockHeld: true, ...over });
const locked = (id: number, over: Partial<State> = {}): State => ({ ...initialState(), targetId: id, ...over });
/** A press, then the button let go again, so the next press counts. */
function lockOn(targets: Target[], over: Partial<Input> = {}): State {
return step(step(initialState(), press(targets, over), P, DT), view(targets, over), P, DT);
}
test("locks-on-the-best-target-in-front", () => {
// Target 2 is nearer, but target 1 is straight ahead.
const s = step(initialState(), press([at(2, 4, 4), at(1, 6, 0)]), P, DT);
assert.equal(s.targetId, 1);
assert.equal(phase(s), "locked");
});
test("ignores-targets-outside-the-cone", () => {
assert.equal(step(initialState(), press([at(1, 0, 5), at(2, -5, 0), at(3, 20, 0)]), P, DT).targetId, null);
assert.equal(step(initialState(), press([at(1, 5, 5)]), P, DT).targetId, 1, "45 degrees is inside a cone of 50");
});
test("aim-counts-across-the-left-edge", () => {
// Looking almost straight left (about 174 degrees) at a target just below that line.
const s = step(initialState(), press([at(1, -6, -0.3)], { aim: Math.PI - 0.1 }), P, DT);
assert.equal(s.targetId, 1);
near(wrap(3 * Math.PI), Math.PI);
near(wrap(-3 * Math.PI), Math.PI);
near(wrap(-Math.PI / 2 - 2 * Math.PI), -Math.PI / 2);
});
function near(a: number, b: number) {
assert.ok(Math.abs(a - b) < 1e-9, `${a} is not ${b}`);
}
test("holding-the-button-locks-once", () => {
const targets = [at(1, 6, 0)];
let s = initialState();
for (let i = 0; i < 30; i++) s = step(s, press(targets), P, DT);
assert.equal(s.targetId, 1, "holding the button let go again");
});
test("pressing-again-releases", () => {
const targets = [at(1, 6, 0)];
let s = lockOn(targets);
assert.equal(s.targetId, 1);
s = step(s, press(targets), P, DT);
assert.equal(s.targetId, null);
assert.equal(phase(s), "free");
});
test("flick-switches-to-the-next-target-in-that-direction", () => {
const targets = [at(1, 6, 0), at(2, 6, 3), at(3, 6, -3)];
let s = locked(1);
s = step(s, view(targets, { switchDir: 1 }), P, DT);
assert.equal(s.targetId, 2, "counterclockwise goes to the target above");
s = step(s, view(targets), P, DT);
s = step(s, view(targets, { switchDir: -1 }), P, DT);
assert.equal(s.targetId, 1, "back the other way");
s = step(s, view(targets), P, DT);
s = step(s, view(targets, { switchDir: -1 }), P, DT);
assert.equal(s.targetId, 3);
});
test("holding-the-stick-switches-once", () => {
const targets = [at(1, 6, 0), at(2, 6, 3), at(3, 3, 6)];
let s = locked(1);
for (let i = 0; i < 40; i++) s = step(s, view(targets, { switchDir: 1 }), P, DT);
assert.equal(s.targetId, 2);
s = step(s, view(targets), P, DT);
s = step(s, view(targets, { switchDir: 1 }), P, DT);
assert.equal(s.targetId, 3);
});
test("target-out-of-sight-gets-a-grace-time", () => {
const hidden = [at(1, 6, 0, false)];
let s = locked(1);
for (let i = 0; i < 84; i++) s = step(s, view(hidden), P, DT);
assert.equal(s.targetId, 1);
assert.equal(phase(s), "losing");
s = step(s, view([at(1, 6, 0)]), P, DT);
assert.equal(phase(s), "locked");
assert.equal(s.lostFor, 0, "the grace time must start over");
});
test("grace-ran-out-releases", () => {
const hidden = [at(1, 6, 0, false)];
let s = locked(1);
for (let i = 0; i < 91; i++) s = step(s, view(hidden), P, DT);
assert.equal(s.targetId, null);
assert.equal(phase(s), "free");
});
test("a-removed-target-passes-the-lock-on-or-lets-go", () => {
const others = [at(2, 9, 0), at(3, 4, 4)];
const next = step(locked(1), view(others), P, DT);
assert.equal(next.targetId, 3, "the nearest one in reach");
const none = step(locked(1), view([]), P, DT);
assert.equal(none.targetId, null, "it must not keep a target that is gone");
});
test("keeps-the-lock-up-to-the-break-range", () => {
assert.equal(step(initialState(), press([at(1, 14, 0)]), P, DT).targetId, null, "too far to lock onto");
assert.equal(phase(step(locked(1), view([at(1, 14, 0)]), P, DT)), "locked");
assert.equal(phase(step(locked(1), view([at(1, 17, 0)]), P, DT)), "losing");
});
test("edge: A press with nothing to lock onto", () => {
const s = step(initialState(), press([]), P, DT);
assert.equal(s.targetId, null);
const later = step(s, press([at(1, 6, 0)]), P, DT);
assert.equal(later.targetId, null, "the press is over; only a new press locks");
});
test("edge: Release in the same tick the target is lost", () => {
const s = step(locked(1, { lockWasHeld: false }), press([at(1, 40, 0)]), P, DT);
assert.equal(s.targetId, null);
});
test("edge: Two targets in the same line", () => {
const targets = [at(1, 5, 0), at(2, 9, 0)];
const s = step(locked(1), view(targets, { switchDir: 1 }), P, DT);
assert.equal(s.targetId, 2, "a target behind the first can still be reached");
});
test("edge: The only target", () => {
const s = step(locked(1), view([at(1, 6, 0)], { switchDir: 1 }), P, DT);
assert.equal(s.targetId, 1);
});
test("edge: Strange values", () => {
assert.equal(step(initialState(), press([at(1, 6, 0)], { aim: Number.NaN }), P, DT).targetId, null);
assert.equal(step(initialState(), press([at(1, Number.NaN, 0), at(2, 6, 0)]), P, DT).targetId, 2);
assert.equal(lockOn([at(1, 0, 0)]).targetId, 1, "a target on the player has no angle but can be locked");
const frozen = step(locked(1), view([at(1, 40, 0)]), P, Number.NaN);
assert.equal(frozen.lostFor, 0);
});
test("edge: A break range smaller than the lock range", () => {
const params: Params = { ...P, breakRange: 4 };
assert.equal(phase(step(locked(1), view([at(1, 10, 0)]), params, DT)), "locked");
});
function randomWorld(r: Rng, targets: Target[]): Target[] {
let list = targets.map((t) => (chance(r, 0.04) ? { ...t, x: real(r, -20, 20), y: real(r, -20, 20) } : chance(r, 0.03) ? { ...t, visible: !t.visible } : t));
if (chance(r, 0.02) && list.length > 0) list = list.filter((_, i) => i !== int(r, 0, list.length - 1));
if (chance(r, 0.02) && list.length < 5) list = [...list, at(int(r, 0, 1000), real(r, -20, 20), real(r, -20, 20), chance(r, 0.8))];
return list;
}
function randomInput(r: Rng, previous: Input, targets: Target[]): Input {
return {
lockHeld: chance(r, 0.15) ? !previous.lockHeld : previous.lockHeld,
switchDir: chance(r, 0.2) ? pick(r, [-1, 0, 0, 1] as const) : previous.switchDir,
aim: chance(r, 0.05) ? real(r, -Math.PI, Math.PI) : previous.aim,
player: previous.player,
targets,
};
}
function randomParams(r: Rng): Params {
return { lockRange: real(r, 3, 40), breakRange: real(r, 4, 60), coneHalfAngle: real(r, 10, 90), graceTime: real(r, 0.2, 5), angleWeight: real(r, 0, 4) };
}
test("property: the lock is never a target that is not there, and only changes for a reason", (t) => {
forAll(t, 121, 300, (r) => {
const params = randomParams(r);
let targets: Target[] = [at(1, real(r, -20, 20), real(r, -20, 20)), at(2, real(r, -20, 20), real(r, -20, 20))];
let input = view(targets, { aim: real(r, -Math.PI, Math.PI) });
let s = initialState();
for (let i = 0; i < 400; i++) {
targets = randomWorld(r, targets);
input = randomInput(r, input, targets);
const before = s;
s = step(before, input, params, pick(r, [DT, 0.25, 1]));
if (s.targetId !== null) assert.ok(targets.some((x) => x.id === s.targetId), "the lock holds a target that is gone");
assert.ok(s.lostFor >= 0 && s.lostFor < params.graceTime + 1e-9);
if (!s.losing) assert.equal(s.lostFor, 0);
if (before.targetId !== null && s.targetId !== null && s.targetId !== before.targetId) {
const gone = !targets.some((x) => x.id === before.targetId);
assert.ok(gone || (input.switchDir !== 0 && before.switchReady), "the lock moved without a reason");
}
if (before.targetId === null && s.targetId !== null) assert.ok(input.lockHeld && !before.lockWasHeld, "locked without a press");
}
});
});
test("model-matches-code", () => {
const spec = JSON.parse(readFileSync(new URL("./mechanic.json", import.meta.url), "utf8"));
const states = new Set<string>(spec.model.states.map((s: { id: string }) => s.id));
const listed = new Set<string>(spec.model.moves.map((m: { from: string; to: string }) => `${m.from}>${m.to}`));
const start = spec.model.states.find((s: { start?: boolean }) => s.start).id;
assert.equal(phase(initialState()), start);
const sets: Params[] = [P];
for (const p of spec.spec.parameters as { name: keyof Params; min: number; max: number }[]) {
sets.push({ ...P, [p.name]: p.min }, { ...P, [p.name]: p.max });
}
const seenMoves = new Set<string>();
const seenStates = new Set<string>([start]);
sets.forEach((params, k) => {
for (let run = 0; run < 40; run++) {
const r = seeded(k * 1000 + run);
let targets: Target[] = [at(1, real(r, -12, 12), real(r, -12, 12)), at(2, real(r, -12, 12), real(r, -12, 12)), at(3, real(r, -12, 12), real(r, -12, 12))];
let input = view(targets, { aim: real(r, -Math.PI, Math.PI) });
let s = initialState();
for (let i = 0; i < 600; i++) {
targets = randomWorld(r, targets);
input = randomInput(r, input, targets);
const before = phase(s);
s = step(s, input, params, pick(r, [DT, DT, 0.25, 1]));
const after = phase(s);
seenStates.add(after);
seenMoves.add(`${before}>${after}`);
}
}
});
for (const move of seenMoves) assert.ok(listed.has(move), `the code does ${move}, the model does not list it`);
for (const move of listed) assert.ok(seenMoves.has(move), `the model lists ${move}, the code never does it`);
for (const id of states) assert.ok(seenStates.has(id), `state ${id} never happens`);
for (const id of seenStates) assert.ok(states.has(id), `state ${id} is not in the model`);
});
Was this page helpful?
Next step
Sources
- GamesRadar, Gaming's most important evolutions, page 7, 2010
- Wikipedia, The Legend of Zelda: Ocarina of Time
- Iwata Asks, The Legend of Zelda: Ocarina of Time 3D, Original Development Staff Part 1, 3: Let's Go to Toei Kyoto Studio Park! (Nintendo)
- Wikipedia, Mega Man Legends (video game)
- Wikipedia, After Burner
- Wikipedia, RayForce
- Wikipedia, Panzer Dragoon (video game)
- Wikipedia, Tomb Raider (1996 video game)
- Alissa McAloon, Halo devs share the decades-long story of the series' creation and legacy, Game Developer, 30 May 2017
- Wikipedia, Halo: Combat Evolved
- Wikipedia, Metroid Prime
- Celia Wagar, What is your ideal form of lock-on in action games?, CritPoints, 24 May 2015