Spielerando

Health Shows NaN and the Enemy Will Not Die

When health or a score shows NaN, a sum somewhere came out as "not a number", and every sum it touches afterwards is NaN as well. An enemy with NaN health cannot die: NaN is never at or below zero, so a check like health <= 0 stays false for good. Two of our own blueprints could make NaN. Both are fixed, and the test below catches each of them.

What you see

Where does the NaN come from?

The first way was in our damage types blueprint. A hit is made of parts, such as fire and frost, and a character can take one kind in as healing at a set share. An endless amount, which is what a division by zero gives (damage that falls off with distance, at a distance of 0), times a share of 0 is NaN. So is endless damage minus endless healing in the same hit. The code let any amount above zero through, and endless is above zero.

The second way was in our world state flags blueprint. Counters such as "keys found" have names from a list, and the code refused a name the list did not have by asking whether the list's entry was missing. But every object in JavaScript already answers to a few names, such as "constructor" and "toString". For those the answer was a built-in function, not "missing", so a counter called "constructor" was let in. One plus a function is text, and the highest of zero and that text is NaN.

Both came up while we wrote the TLA+, Z3 and Lean checks for these two blueprints, and both were fixed with a test that failed first. Our notes do not say which of the three tools pointed at them.

How to check your game for it

Feed the code the two things that made NaN for us, and ask whether the result is still a number. The first test builds the endless amount the way a game would, by dividing by a distance of 0. The second asks for a counter named "constructor". The last two tests make sure ordinary hits and ordinary counters still work.

The test: fix.test.ts

TypeScript for Node's own test runner. The tests named below as the catch are the ones that fail on the bug.

import assert from "node:assert/strict";
import test from "node:test";
import { addToCounter, afterHit } from "./fix.ts";

test("a hit from point-blank range leaves health a number", () => {
  // Damage that falls off with distance, from a distance of 0: 50 / 0 is endless.
  const distance = 0;
  const amount = 50 / distance;
  const health = afterHit(100, 100, [{ amount, absorbed: true }], 0);
  assert.ok(Number.isFinite(health), `health is ${health}`);
  const both = afterHit(100, 100, [{ amount, absorbed: false }, { amount, absorbed: true }], 0.5);
  assert.ok(Number.isFinite(both), `health is ${both} after endless damage and endless healing`);
});

test("a counter named like something every object has is refused", () => {
  const counters = addToCounter({}, { keys: 3, constructors: 10 }, "constructor", 1);
  assert.deepEqual(counters, {}, `"constructor" was taken for a counter`);
});

test("an ordinary hit hurts, and an absorbed part heals", () => {
  assert.equal(afterHit(100, 100, [{ amount: 30, absorbed: false }, { amount: 10, absorbed: true }], 0.5), 75);
  assert.equal(afterHit(10, 100, [{ amount: 30, absorbed: false }], 0.5), 0);
});

test("a listed counter grows and stops at its top", () => {
  assert.deepEqual(addToCounter({ keys: 2 }, { keys: 3 }, "keys", 5), { keys: 3 });
  assert.deepEqual(addToCounter({}, { keys: 3 }, "kyes", 1), {});
});

The catch:

What it said on the code with the bug put back

✖ a hit from point-blank range leaves health a number
    health is NaN
✖ a counter named like something every object has is refused
    "constructor" was taken for a counter
    got { constructor: NaN }, expected {}
✔ an ordinary hit hurts, and an absorbed part heals
✔ a listed counter grows and stops at its top
4 tests: 2 passed, 2 failed

What it said on the fixed code

✔ a hit from point-blank range leaves health a number
✔ a counter named like something every object has is refused
✔ an ordinary hit hurts, and an absorbed part heals
✔ a listed counter grows and stops at its top
4 tests: 4 passed, 0 failed

Run by pnpm fixes:check on 2026-11-26 with Node v24.15.0. The text above is its output.

The fix

For the hit: count only amounts that are finite and above zero. In our blueprint an endless part now does nothing at all, like a part that is not a number. If you would rather have such a hit kill, cap it at the full health instead of skipping it. Better still, stop the endless number where it starts: never divide by a distance that can be 0.

For the names: look a name up among the list's own entries only, with Object.hasOwn, or keep the list in a Map. This matters most where names come from players, such as a pet's name or a save slot.

What changed

Lines with - are the code with the bug, lines with + the fix.

    let heal = 0;
    for (const part of parts) {
-     if (!(part.amount > 0)) continue;
+     // Only a finite amount above zero counts: endless times a share of 0 is not a number.
+     if (!(Number.isFinite(part.amount) && part.amount > 0)) continue;
      if (part.absorbed) heal += part.amount * absorbShare;
      else damage += part.amount;
…
      listed changes nothing. */
  export function addToCounter(counters: Record<string, number>, tops: Record<string, number>, name: string, by: number): Record<string, number> {
-   if (tops[name] === undefined || !Number.isInteger(by)) return counters;
-   const before = counters[name] ?? 0;
+   // Own names only: every object answers to "constructor" and "toString", and those are no counters.
+   if (!Object.hasOwn(tops, name) || !Number.isInteger(by)) return counters;
+   const before = Object.hasOwn(counters, name) ? counters[name] : 0;
    return { ...counters, [name]: Math.min(tops[name], Math.max(0, before + by)) };
  }

The fixed code: fix.ts

/* Two ways a number in a game turns into NaN ("not a number") and stays that way: an endless amount
   in a hit, and a counter name that every JavaScript object already answers to. From the damage
   types and the world state flags blueprints:
   https://spielerando.com/en/mechanics/damage-types
   https://spielerando.com/en/mechanics/world-state-flags */

export interface Part {
  /** How much this part of the hit deals or, when absorbed, heals. */
  amount: number;
  /** The character takes this kind of damage in as healing. */
  absorbed: boolean;
}

/** Health after one hit. What hurts and what heals are settled together, and health stays
    between 0 and full. */
export function afterHit(health: number, full: number, parts: Part[], absorbShare: number): number {
  let damage = 0;
  let heal = 0;
  for (const part of parts) {
    // Only a finite amount above zero counts: endless times a share of 0 is not a number.
    if (!(Number.isFinite(part.amount) && part.amount > 0)) continue;
    if (part.absorbed) heal += part.amount * absorbShare;
    else damage += part.amount;
  }
  return Math.min(full, Math.max(0, health - damage + heal));
}

/** Adds to a counter the game lists in `tops` (each with its highest value). A name that is not
    listed changes nothing. */
export function addToCounter(counters: Record<string, number>, tops: Record<string, number>, name: string, by: number): Record<string, number> {
  // Own names only: every object answers to "constructor" and "toString", and those are no counters.
  if (!Object.hasOwn(tops, name) || !Number.isInteger(by)) return counters;
  const before = Object.hasOwn(counters, name) ? counters[name] : 0;
  return { ...counters, [name]: Math.min(tops[name], Math.max(0, before + by)) };
}

Paste this to your AI

Something in my game shows NaN ("not a number"): health, a score or a counter. An enemy with NaN health may never die, because NaN <= 0 is false.

1. Before you change anything, list every place where health, damage, a score or a counter is worked out, and what goes into it. Mark every division that can divide by 0, every multiplication where one side can be endless, and every lookup of a name in a plain object.
2. Write tests like the ones below for my code: a hit that divides by 0 (for example damage that falls off with a distance of 0) must leave health a finite number, and a counter named "constructor" must be refused. Run them and paste me the real output.
3. Fix what fails the way the code below does: count only finite amounts, stop the division by 0 where it starts, and look names up with Object.hasOwn or a Map. Run the tests again and paste the output.
4. Let an endless amount through again for a moment, show me the test failing, and undo it.

Do not tell me it is fixed unless the test output shows it.

The button copies this text together with the fixed code and the test above, so your AI has everything.

Where we found it

The blueprint the bug was in, and the test that guards it there:

How we found them: the bugs our proofs found in our own code

Was this page helpful?

One click, no account. It shows us which pages help.

Next step

Blueprint

Health and Damage

Health that counts only finite amounts above zero cannot turn into NaN; this blueprint does that for hits and healing alike, and shows anyone still alive with at least 1.

Last updated: 2026-11-26