Erstes Spiel mit Lock-on-Zielerfassung? Zelda 1998, und davor
GamesRadar nennt The Legend of Zelda: Ocarina of Time, am 21. November 1998 in Japan erschienen, als das Spiel, in dem die Lock-on-Zielerfassung zuerst zu sehen war, dort unter dem Namen Z-Targeting. Mega Man Legends hatte schon elf Monate früher eine Lock-on-Zielerfassung, und Arcade-Shooter ließen Raketen ab 1986 Ziele erfassen, die Antwort hängt also davon ab, welche Art Zielerfassung man meint.
Wer behauptet was?
| Quelle | Aussage |
|---|---|
| GamesRadar (2010) | „First seen in: The Legend of Zelda: Ocarina of Time (1998)“. Die Zielerfassung hielt die Kamera auf einem Gegner und ließ einen um ihn herum seitwärts laufen |
| Wikipedia, Ocarina of Time | Vorsichtiger: Das Spiel habe ein Zielerfassungssystem „popularized“, also bekannt gemacht, das seither in 3D-Abenteuern üblich ist |
| Wikipedia, Mega Man Legends | Am 18. Dezember 1997 in Japan erschienen, mit Lock-on-Zielerfassung unter den Mechaniken, die der Artikel mit Tomb Raider vergleicht, gestützt auf das Magazin Next Generation vom Oktober 1997 |
| Wikipedia, After Burner | Ein Magazintest vom November 1987 stellte fest, dass die Zielerfassung so funktioniert wie im Arcade-Spiel Lock-On von Data East aus dem Jahr 1986 |
Die Aussagen meinen Verschiedenes. Bei Ocarina geht es um eine Taste, die Kamera und Held in einem 3D-Kampf auf einem Gegner hält. Bei den Shootern geht es darum, ein Ziel für zielsuchende Schüsse zu markieren.
Woher kam das Z-Targeting?
In einem Interview der Reihe Iwata fragt zur Neuauflage für den Nintendo 3DS erinnern sich zwei Entwickler von Ocarina unterschiedlich, und beide stehen so im Protokoll.
- Toru Osawa sagt, das Problem sei älter: In Super Mario 64 konnte man beim Lesen eines Schildes im Kreis darum herumlaufen. Er und Koizumi gingen in den Toei Kyoto Studio Park, um Schwertkampf-Vorführungen zu sehen, und in einer umkreiste ein Ninja einen Samurai mit straff gespannter Kettenwaffe. Seine Idee war eine unsichtbare Kettensichel zwischen Link und dem Gegner: Stick nach vorn, um näherzukommen, zur Seite, um im Kreis herumzugehen.
- Yoshiaki Koizumi sagt, sie hätten schon bei Super Mario 64 eine gute Art gesucht, einen Gegner vor sich zu treffen, und es nicht geschafft. Im Studiopark fiel ihm auf, dass die Gegner „don't all attack at once. First, one attacks while the others wait.“ Also: „Z Targeting flags one particular opponent, telling the other enemies to wait“, und wenn einer fällt, wechselt man zum nächsten.
Nintendos eigene Anmerkung im Interview ergänzt, dass Link mit der Taste auch aus der Entfernung mit Figuren sprechen kann. Sie war nie nur für den Kampf da.
Wie hat sich die Zielerfassung über die Jahre verändert?
| Wann | Spiel | Was sich änderte | Quelle |
|---|---|---|---|
| 1986 | Lock-On (Data East, Arcade) | Zielsuchende Raketen, die erst ein erfasstes Ziel brauchen. Im November 1987 verglich ein Magazin die Zielerfassung von After Burner damit | Wikipedia, After Burner |
| Februar 1994 | RayForce | Sein Designer wählte einen Lock-on-Laser, der mehrere Gegner auf einmal zerstört, angeregt vom Bombenfadenkreuz in Xevious | Wikipedia, gestützt auf GAMEgene |
| 10.03.1995 | Panzer Dragoon | Ein zielsuchender Laser erfasst bis zu acht Gegner, während der Spieler das Fadenkreuz über sie führt und den Feuerknopf hält | Wikipedia, gestützt auf IGN |
| 24.10.1996 | Tomb Raider | Mit gezogenen Waffen erfasst die Heldin Ziele in der Nähe von selbst, ohne Taste. Der Satz hat keine Fußnote | Wikipedia |
| 18.12.1997 | Mega Man Legends | Eine Zielerfassung in einem 3D-Actionspiel mit einer Kamera, die der Spieler steuert | Wikipedia, gestützt auf Next Generation |
| 21.11.1998 | Ocarina of Time | Z-Targeting: Die Kamera folgt dem Ziel, Link schaut es immer an, und gekämpft wird mit einem Gegner nach dem anderen | Wikipedia, Iwata fragt |
| 15.11.2001 | Halo: Combat Evolved | Hilfe, die absichtlich verborgen bleibt. Designer Jaime Griesemer: „If it's an enemy, we can assume that when you slow down, you're trying to aim“, und „We tried to conceal how much help we're giving the player“ | Game Developer (2017), Wikipedia für das Datum |
| 18.11.2002 | Metroid Prime | Zielerfassung in einem Ego-Spiel, mit der man im Kreis laufen und trotzdem zielen kann | Wikipedia, gestützt auf GameSpot und IGN |
Der GamesRadar-Artikel von 2010 zählt Metroid Prime zu den Spielen, die die Idee von Ocarina übernahmen, neben Third-Person-Shootern. Ein Essay auf CritPoints von 2015 merkt an, dass Dark Souls eine harte Zielerfassung hat, seine Tritt- und Sprungangriffe aber so gebaut sind, dass sie ohne sie funktionieren.
Was zählt als Zielerfassung?
Der CritPoints-Essay unterscheidet eine harte von einer weichen Zielerfassung, und Shooter fügen eine dritte Art hinzu. Jede ergibt ein anderes frühestes Spiel.
- Harte Zielerfassung: Eine Taste dreht Figur und Kamera zum Ziel. Mega Man Legends, Dezember 1997, ist der früheste Fall in den gelesenen Quellen; Ocarina of Time, November 1998, ist der, den GamesRadar nennt.
- Weiche Zielerfassung: Die Figur dreht sich von selbst zu nahen Gegnern. Tomb Raider, 1996, passt, gestützt auf einen Satz ohne Fußnote.
- Waffenerfassung: Ein Ziel wird markiert, dann fliegen zielsuchende Schüsse. Lock-On, 1986, passt, und 1987 behandelte die Presse das schon als bekannte Mechanik. Eine Quelle für ein früheres Spiel haben wir nicht gefunden.
Was lässt sich sicher sagen?
Ocarina of Time, 1998, ist das Spiel, das die gelesenen Quellen am stärksten mit der Lock-on-Zielerfassung verbinden, und der Bericht seiner Entwickler über das Z-Targeting ist veröffentlicht. Das früheste Spiel mit Zielerfassung, das wir belegen können, ist es nicht: Mega Man Legends hatte im Dezember 1997 eine, und Arcade-Shooter ließen Raketen ab 1986 Ziele erfassen. Was der Bericht zu Ocarina hinzufügt, ist der Grund: ein Gegner nach dem anderen, während die übrigen warten.
Heute selbst bauen
Unser Blueprint für die Lock-on-Zielerfassung ist eine harte Zielerfassung im Sinn von oben. Ein Druck hält Kamera und Angriffe auf einem Ziel, ein Schnippen des Sticks wechselt zum nächsten, so wie Koizumi das Weiterschalten nach jedem Kampf beschreibt, und ein zweiter Druck lässt los. Die erste Wahl wiegt ab, wohin der Spieler schaut und was am nächsten ist, die Erfassung hält, wenn das Ziel kurz hinter einer Säule verschwindet, und sie behält nie ein Ziel, das weg ist. Zwei Teile dieser Geschichte sind nicht dabei. Die übrigen Gegner warten zu lassen, ist eine Regel für deine Gegner, nicht für die Zielerfassung, und Halos verborgene Zielhilfe oder eine Raketenerfassung mehrerer Ziele wären Änderungen, um die du deine KI obendrauf bittest.
- 19 Tests bestehen auf dem Referenzcode.
- Das Zustandsdiagramm wurde gegen den Code geprüft, mit den Standardwerten und mit jedem niedrigsten und höchsten Wert der Liste.
- Geprüft mit TLA+, Z3 und Lean, mit dem, was die Werkzeuge ausgaben
Baue „Zielerfassung“ in mein Spiel ein.
Das soll es bewirken: In einem Kampf mit mehreren Gegnern soll der Spieler nicht von Hand zielen müssen, während er ausweicht. Mit der Erfassung folgen Angriffe und Kamera einem Gegner, und die Regeln für Auswahl und Halten dieses Gegners entscheiden, ob sie dem Spieler hilft oder ihn behindert.
Baue es genau nach diesem Automaten. Prüfe zuerst, ob er zu meinem Spiel passt, und bau ihn dann so:
```mermaid
stateDiagram-v2
[*] --> free
free: Kein Ziel
locked: Erfasst
losing: Verliert es
free --> free: nichts zu erfassen
free --> locked: Druck erfasst ein Ziel
locked --> locked: hält oder wechselt
locked --> free: gelöst oder keins übrig
locked --> losing: Ziel außer Reichweite
losing --> losing: wartet auf es
losing --> locked: zurück oder ersetzt
losing --> free: Schonzeit um oder gelöst
```
Nimm diese Startwerte:
- lockRange: 12 units
- breakRange: 16 units
- coneHalfAngle: 50 degrees
- graceTime: 1.5 s
- angleWeight: 1
Achte auf diese typischen Fehler:
- Gehaltener Knopf: Sag deiner KI: Erkenne den Druck selbst durch den Vergleich mit dem letzten Schritt und handle nur beim neuen Druck.
- Gehaltener Stick: Sag deiner KI: Ein Anstoß ist ein Wechsel, und der nächste Wechsel braucht den Stick wieder in der Mitte.
- Die Naht bei den Winkeln: Sag deiner KI: Bring den Unterschied zweier Winkel in den Bereich minus 180 bis 180, bevor du vergleichst.
- Ein Ziel, das weg ist: Sag deiner KI: Such das Ziel in jedem Schritt heraus, und wenn es weg ist, nimm das nächste in Reichweite oder lass los.
- Die Erfassung am ersten Pfeiler verlieren: Sag deiner KI: Gib der Erfassung eine Schonzeit und beginne sie neu, wenn das Ziel zurück ist.
- Zwei Gegner in einer Linie: Sag deiner KI: Lass ein Ziel mit demselben Winkel als Wechselziel zu, das nähere zuerst.
Ändere nichts anderes im Spiel. Behaupte nichts, ohne es zu belegen: Modelliere, was dafür nötig ist, und zeig mir, worauf deine Lösung beruht und woran ich sehe, dass sie klappt. Wenn du etwas nicht zeigen kannst, sag, dass du es nicht weißt. Sag mir danach in einfachen Worten, wie ich es ausprobiere.
Der Code, von https://spielerando.com/de/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 };
}
```
Baue „Zielerfassung“ in mein Spiel ein.
Das soll es bewirken: In einem Kampf mit mehreren Gegnern soll der Spieler nicht von Hand zielen müssen, während er ausweicht. Mit der Erfassung folgen Angriffe und Kamera einem Gegner, und die Regeln für Auswahl und Halten dieses Gegners entscheiden, ob sie dem Spieler hilft oder ihn behindert.
Baue es genau nach diesem Automaten. Prüfe zuerst, ob er zu meinem Spiel passt, und bau ihn dann so:
```mermaid
stateDiagram-v2
[*] --> free
free: Kein Ziel
locked: Erfasst
losing: Verliert es
free --> free: nichts zu erfassen
free --> locked: Druck erfasst ein Ziel
locked --> locked: hält oder wechselt
locked --> free: gelöst oder keins übrig
locked --> losing: Ziel außer Reichweite
losing --> losing: wartet auf es
losing --> locked: zurück oder ersetzt
losing --> free: Schonzeit um oder gelöst
```
Nimm diese Startwerte:
- lockRange: 12 units
- breakRange: 16 units
- coneHalfAngle: 50 degrees
- graceTime: 1.5 s
- angleWeight: 1
Achte auf diese typischen Fehler:
- Gehaltener Knopf: Sag deiner KI: Erkenne den Druck selbst durch den Vergleich mit dem letzten Schritt und handle nur beim neuen Druck.
- Gehaltener Stick: Sag deiner KI: Ein Anstoß ist ein Wechsel, und der nächste Wechsel braucht den Stick wieder in der Mitte.
- Die Naht bei den Winkeln: Sag deiner KI: Bring den Unterschied zweier Winkel in den Bereich minus 180 bis 180, bevor du vergleichst.
- Ein Ziel, das weg ist: Sag deiner KI: Such das Ziel in jedem Schritt heraus, und wenn es weg ist, nimm das nächste in Reichweite oder lass los.
- Die Erfassung am ersten Pfeiler verlieren: Sag deiner KI: Gib der Erfassung eine Schonzeit und beginne sie neu, wenn das Ziel zurück ist.
- Zwei Gegner in einer Linie: Sag deiner KI: Lass ein Ziel mit demselben Winkel als Wechselziel zu, das nähere zuerst.
Ändere nichts anderes im Spiel. Behaupte nichts, ohne es zu belegen: Modelliere, was dafür nötig ist, und zeig mir, worauf deine Lösung beruht und woran ich sehe, dass sie klappt. Wenn du etwas nicht zeigen kannst, sag, dass du es nicht weißt. Sag mir danach in einfachen Worten, wie ich es ausprobiere.Der getestete 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 };
}
Die Tests, die der Referenzcode besteht
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`);
});
War diese Seite hilfreich?
Nächster Schritt
Quellen
- GamesRadar, Gaming's most important evolutions, Seite 7, 2010
- Wikipedia, The Legend of Zelda: Ocarina of Time
- Iwata fragt, 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. Mai 2017
- Wikipedia, Halo: Combat Evolved
- Wikipedia, Metroid Prime
- Celia Wagar, What is your ideal form of lock-on in action games?, CritPoints, 24. Mai 2015