The Move That Quartered Crits — JavaScript Bug Hunt
Modelled on the Focus Energy bug in Pokémon Red and Blue (1996).
- Language: JavaScript
- Layer: Frontend
- Difficulty: Easy
- Concepts: Game Logic, Bitwise
- Modelled on: Pokémon Red/Blue · 1996
- Visible tests: an unfocused attacker uses half its base speed; Focus Energy raises the threshold
- Reward: 50 XP for a complete fix
Incident
- Severity: SEV-3 (priority: Medium)
- Ticket: BUG-FOCUS
- Reported by: competitive players
- Time to fix: 10 minutes, the target for an easy incident
Failing checks on the shipped build
- Focus Energy raises the threshold — 4x, not a quarter: expected 200 but got 12
How to work this incident
- Read the report: write down the expected behaviour and the actual one in a sentence each.
- Reproduce it: run the visible tests and find the failing check that matches the report.
- Read the logs and the failing checks for the first value that is wrong — the earliest wrong value is nearer the cause than the loudest error.
- Follow the value the screen shows back through the state and the pure helpers that compute it; the view is usually only the messenger.
- Fix the cause rather than the symptom, run the visible tests again, and submit against the hidden ones — then write the root cause down.
Briefing
Modelled on the Focus Energy bug in Pokémon Red and Blue (1996). The move was meant to raise the user's chance of landing a critical hit, but the game code shifted the critical-hit value the wrong way, so using it actually made critical hits less likely. The bug shipped in the Generation I games.
This battle engine computes a critical-hit threshold from the attacker's base speed. Its Focus Energy branch shifts in the wrong direction.
Fix critThreshold so Focus Energy raises the threshold.
Bug report
BUG-FOCUS · Priority: Medium · Reported by: competitive players
critThreshold(baseSpeed, focused):
- the threshold starts at floor(baseSpeed / 2)
- Focus Energy (focused = true) multiplies it by 4 (shift LEFT by rules.FOCUS_SHIFT)
- the result is capped at rules.MAX_THRESHOLD (255)
isCritical(baseSpeed, focused, roll) is true when roll (0-255) < threshold.
Observed: a base-speed-100 attacker has threshold 50 normally but 12 after using Focus Energy.
Logs
[battle] crit threshold speed=100 focused=false -> 50
[battle] crit threshold speed=100 focused=true -> 12The code as shipped
src/battle/critical.js (editable)
var rules = require("./rules");
exports.critThreshold = function (baseSpeed, focused) {
var threshold = Math.floor(baseSpeed / 2);
if (focused) threshold = threshold >> rules.FOCUS_SHIFT;
return Math.min(threshold, rules.MAX_THRESHOLD);
};
exports.isCritical = function (baseSpeed, focused, roll) {
return roll < exports.critThreshold(baseSpeed, focused);
};
Read-only context: src/battle/rules.js.
Open the hunt to start the incident clock, edit the files, run the visible tests and submit against the hidden ones, then write the root cause down. More JavaScript bug hunts.