Files
Crystal/tests/battle-world.test.ts
T
Sébastien QUEROLandClaude Opus 5.5 796fe8f6d5
Checks / web (push) Canceled after 0s
Open Route 29 and Cherrygrove City
- Enable Route 29, its Route 46 gate, Cherrygrove City and its buildings.
- Run item balls through FindItemInBallScript and fruit trees through
  FruitTreeScript, with a native daily reset of the daily engine flags.
- Port the Pokémon Center nurse (HealParty with the PP Up clamp,
  CheckPokerus, phone-call checks) and record the whiteout spawn on
  entering a Pokémon Center, as EnterMapWarp does.
- Port standard Poké Marts with keyboard, D-pad and pointer menus.
- Add the recoil battle command (Struggle) and VAR_BOXSPACE.
- Save fruit trees, the daily reset day and the last spawn map; older
  version-3 backups migrate by absence.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 17:25:02 +02:00

155 lines
6.2 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { existsSync, readFileSync } from "node:fs";
import { CrystalAssets } from "../src/game/rom/assets";
import { battleFixtureWorld, ENCOUNTER_WALK, type BattleScenario } from "./battle-fixture";
import type { GameWorld } from "../src/game/world/world";
import { generatePartyMon, calcStats } from "../src/game/party/pokemon";
const romPath = process.env.CRYSTAL_ROM_PATH ?? "Pokemon - Crystal Version (UE) (V1.1) [C][!].gbc";
const fixture = (scenario: BattleScenario = "win") =>
battleFixtureWorld(new CrystalAssets(new Uint8Array(readFileSync(romPath))), scenario);
function encounter(world: GameWorld) {
for (const direction of ENCOUNTER_WALK) expect(world.move(direction)).toBe(true);
expect(world.battle).toBeDefined();
}
function finish(world: GameWorld, action: "fight" | "run" = "fight") {
const messages = new Set<string>();
for (let tick = 0; tick < 10000; tick++) {
if (world.battle) {
const view = world.battle;
if (view.message) messages.add(view.message.text);
if (view.prompt?.kind === "action") world.battleAction({ action, move: 0 });
else if (view.prompt?.kind === "yesno") world.battleChoose(1);
else if (view.prompt?.kind === "forget") world.battleChoose(3);
else if (view.prompt?.kind === "stats" || view.message?.prompt) world.interact();
else world.tick();
} else if (world.dialogue) {
messages.add(world.dialogue.pages[world.dialogue.index]);
world.advanceDialogue();
world.tick();
} else if (world.busy) world.tick();
else return [...messages].join("\n");
}
throw new Error(`Battle did not settle: ${world.diagnostic}`);
}
describe.skipIf(!existsSync(romPath))("world battle integration with the local ROM", () => {
it("commits a victory, level-up, move learning and Pokédex sighting", () => {
const world = fixture();
encounter(world);
const before = world.save();
expect(before.party[0].level).toBe(5);
expect(before.pokedex.seen).toEqual([155]);
const messages = finish(world);
expect(world.notice, world.diagnostic).toBe("");
expect(messages).toContain("SMOKESCREEN");
expect(world.state.party[0].level).toBe(6);
expect(world.state.party[0].moves).toContain(108);
expect(world.state.pokedex.seen.has(16)).toBe(true);
expect(world.state.pokedex.caught.has(16)).toBe(false);
const saved = world.save();
world.restore(saved);
expect(world.save()).toEqual(saved);
});
it("struggles with recoil once every move is out of PP", () => {
const world = fixture();
world.state.party[0].pp = [0, 0, 0, 0];
encounter(world);
const messages = finish(world);
expect(world.notice, world.diagnostic).toBe("");
expect(messages).toMatch(/STRUGGLE/);
expect(messages).toMatch(/recoil/i);
});
it("escapes without awarding experience and returns to walking", () => {
const world = fixture();
encounter(world);
expect(finish(world, "run")).toMatch(/Got away safely/);
expect(world.notice, world.diagnostic).toBe("");
expect(world.state.party[0].exp).toBe(178);
expect(world.state.pokedex.seen.has(16)).toBe(true);
expect(world.move("down")).toBe(true);
});
it("restores earned stat experience before the next stat recalculation", () => {
const world = fixture();
const mon = generatePartyMon(world.assets.pokemon, {
species: 155, level: 100, item: 0, otId: world.playerId,
ot: "CHRIS", dvs: 0x823b, caughtData: 0,
});
mon.moves = [33, 0, 0, 0];
mon.pp = [35, 0, 0, 0];
world.state.party = [mon];
encounter(world);
finish(world);
expect(world.notice, world.diagnostic).toBe("");
const saved = world.save();
expect(saved.party[0].statExp.some((value) => value > 0)).toBe(true);
expect(saved.party[0].stats).not.toEqual(calcStats(world.assets.pokemon, 155, mon.dvs, 100, saved.party[0].statExp));
expect(() => world.restore(saved)).not.toThrow();
expect(world.save()).toEqual(saved);
const corrupt = structuredClone(saved);
corrupt.party[0].stats[1] = 999;
expect(() => world.restore(corrupt)).toThrow("Invalid native save");
});
it("whiteout heals the party, halves money and returns home", () => {
const world = fixture("loss");
encounter(world);
const messages = finish(world);
expect(world.notice, world.diagnostic).toBe("");
expect(messages).toMatch(/whited\s+out/i);
expect(world.save()).toMatchObject({ map: "24:7", x: 3, y: 3, money: 1500 });
const mon = world.state.party[0];
expect(mon.hp).toBe(mon.stats[0]);
expect(mon.pp[0]).toBe(35);
expect(mon.status).toBe(0);
});
it("a mid-battle save restores the committed party and position", () => {
const world = fixture();
encounter(world);
const committed = world.save();
for (let tick = 0; tick < 5000; tick++) {
if (world.battle?.prompt?.kind === "action") {
world.battleAction({ action: "fight", move: 0 });
break;
}
if (world.battle?.message?.prompt) world.interact();
else world.tick();
}
expect(world.save()).toEqual(committed);
world.restore(committed);
expect(world.battle).toBeUndefined();
expect(world.save()).toEqual(committed);
expect(world.move("right")).toBe(true);
});
it("a failed whiteout map callback cannot commit healing or money loss", () => {
const world = fixture("loss");
encounter(world);
const before = world.save();
// A malformed callback models a destination whose logic is not supported.
world.assets.map("24:7").callbacks.push({ kind: 5, script: world.assets.reader.bytes.length });
finish(world);
expect(world.notice).toContain("not been ported");
expect(world.save().money).toBe(before.money);
expect(world.save().party).toEqual(before.party);
expect(world.save().pokedex).toEqual(before.pokedex);
});
it("unsupported effects discard all battle party and Pokédex changes", () => {
const world = fixture("unsupported");
encounter(world);
const before = world.save();
finish(world);
expect(world.notice).toContain("not been ported");
expect(world.save().party).toEqual(before.party);
expect(world.save().pokedex).toEqual(before.pokedex);
expect(world.save().money).toBe(before.money);
});
});