mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-21 06:10:42 +00:00
2e6f70c098
## Summary Follow-up to #4230. Two more core-sim optimizations — these are **behavior-affecting in controlled ways** (unlike #4230, which was hash-identical), so both come with dedicated test coverage written before the change. Combined results (`npm run perf:game`, same machine, before → after): | run | mean tick | ticks/sec | p99 | peak heap | |---|---|---|---|---| | default (world, 400 bots, 1800 ticks) | 7.98 → **6.96 ms** | 125 → **144** | 21.2 → **19.0 ms** | 438 → **294 MB** | | giantworldmap, 600 ticks | 17.4 → **15.2 ms** | 58 → **66** | 32.6 → 30.5 ms | | Cumulative with #4230 vs. the original baseline: default run mean 9.04 → 6.96 ms (111 → 144 ticks/sec); giantworldmap 22.5 → 15.2 ms (44 → 66 ticks/sec, max tick 52.8 → 40.1 ms). ### 1. `PseudoRandom`: seedrandom ARC4 → inline sfc32 - ARC4 was ~4% of profiled self time. The new engine is sfc32 with splitmix32 seed expansion and a warmup, using only 32-bit integer ops — sequences are identical across platforms. The class API is unchanged. - This **removes the `seedrandom` dependency entirely**, making `src/core` actually dependency-free (the import was the only violation of that rule). - ⚠️ **The random stream differs, so the deterministic game-state hash changes.** All clients run the same code, so cross-client sync is unaffected; the harness reproduces the same hash on repeated runs per seed. New reference hashes: - `--map world --ticks 200 --bots 100` → `5607618202213430` - default run → `29309648281599524` - `--map giantworldmap --ticks 600` → `39945089450032050` - New `tests/PseudoRandom.test.ts` (15 tests) pins the engine-agnostic contract: per-seed determinism, ranges, uniformity, adjacent-seed decorrelation, and every API method. The tests were verified green against the old engine first, then the swap. - The stream change exposed a test that passed **by RNG luck**: in `AiAttackBehavior.test.ts`, "nation cannot attack allied player" was actually being blocked by the difficulty dice gate in `shouldAttack`, not the alliance check — hiding that the test's `AiAttackBehavior` was constructed without its `NationEmojiBehavior`. The test now supplies one and verifies the real protection layer (`AttackExecution`'s alliance check), robust to any dice outcome. ### 2. `PlayerImpl.toFullUpdate`: allocation-free empty collections - `toFullUpdate` runs for every player every tick and allocated ~10 collections each (allies, embargoes Set, attacks, alliance views, …) even when all were empty — the common case for most of 472 players. Because `lastSentUpdate` retains each snapshot for a full tick, these objects survived minor GC, got promoted, and accumulated as old-space garbage between major GCs — that's the peak-heap drop. - Empty collections now reuse shared **frozen** module-level singletons, so `diffPlayerUpdate`'s existing `a === b` fast paths skip structural comparison entirely. Non-empty collections build in single passes. Freezing makes accidental in-worker mutation throw loudly instead of silently corrupting every player; consumers across the worker boundary get mutable structured clones as before. (`Set` cannot be frozen — `EMPTY_EMBARGOES` is documented as never-mutate.) - Value-identical: the game-state hash is unchanged by this part (verified against the post-PRNG baseline). - New `tests/PlayerUpdateDiff.test.ts` (8 tests): full-snapshot shape, null-when-unchanged, embargo/alliance/target/attack diffs through the real tick pipeline, and the freeze contract. ### Verification - Full suite passes: 124 files / 1408 tests (23 new) + server tests; lint and prettier clean. - Hash reproducibility confirmed: repeated runs with identical args produce identical hashes on all three configs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
164 lines
4.5 KiB
TypeScript
164 lines
4.5 KiB
TypeScript
import { NationEmojiBehavior } from "../src/core/execution/nation/NationEmojiBehavior";
|
|
import { AiAttackBehavior } from "../src/core/execution/utils/AiAttackBehavior";
|
|
import { Game, Player, PlayerInfo, PlayerType } from "../src/core/game/Game";
|
|
import { PseudoRandom } from "../src/core/PseudoRandom";
|
|
import { setup } from "./util/Setup";
|
|
|
|
describe("Ai Attack Behavior", () => {
|
|
let game: Game;
|
|
let bot: Player;
|
|
let human: Player;
|
|
let attackBehavior: AiAttackBehavior;
|
|
|
|
// Helper function for basic test setup
|
|
async function setupTestEnvironment() {
|
|
const testGame = await setup("big_plains", {
|
|
infiniteGold: true,
|
|
instantBuild: true,
|
|
infiniteTroops: true,
|
|
});
|
|
|
|
// Add players
|
|
const botInfo = new PlayerInfo(
|
|
"bot_test",
|
|
PlayerType.Bot,
|
|
null,
|
|
"bot_test",
|
|
);
|
|
const humanInfo = new PlayerInfo(
|
|
"human_test",
|
|
PlayerType.Human,
|
|
null,
|
|
"human_test",
|
|
);
|
|
testGame.addPlayer(botInfo);
|
|
testGame.addPlayer(humanInfo);
|
|
|
|
const testBot = testGame.player("bot_test");
|
|
const testHuman = testGame.player("human_test");
|
|
|
|
// Assign territories
|
|
let landTileCount = 0;
|
|
testGame.map().forEachTile((tile) => {
|
|
if (!testGame.map().isLand(tile)) return;
|
|
(landTileCount++ % 2 === 0 ? testBot : testHuman).conquer(tile);
|
|
});
|
|
|
|
// Add troops
|
|
testBot.addTroops(5000);
|
|
testHuman.addTroops(5000);
|
|
|
|
const behavior = new AiAttackBehavior(
|
|
new PseudoRandom(42),
|
|
testGame,
|
|
testBot,
|
|
0.5,
|
|
0.5,
|
|
0.2,
|
|
);
|
|
|
|
return { testGame, testBot, testHuman, behavior };
|
|
}
|
|
|
|
// Helper functions for tile assignment
|
|
function assignAlternatingLandTiles(
|
|
game: Game,
|
|
players: Player[],
|
|
totalTiles: number,
|
|
) {
|
|
let assigned = 0;
|
|
game.map().forEachTile((tile) => {
|
|
if (assigned >= totalTiles) return;
|
|
if (!game.map().isLand(tile)) return;
|
|
const player = players[assigned % players.length];
|
|
player.conquer(tile);
|
|
assigned++;
|
|
});
|
|
}
|
|
|
|
beforeEach(async () => {
|
|
const env = await setupTestEnvironment();
|
|
game = env.testGame;
|
|
bot = env.testBot;
|
|
human = env.testHuman;
|
|
attackBehavior = env.behavior;
|
|
});
|
|
|
|
test("bot cannot attack allied player", () => {
|
|
// Form alliance (bot creates request to human)
|
|
const allianceRequest = bot.createAllianceRequest(human);
|
|
allianceRequest?.accept();
|
|
|
|
expect(bot.isAlliedWith(human)).toBe(true);
|
|
|
|
// Count attacks before attempting attack
|
|
const attacksBefore = bot.outgoingAttacks().length;
|
|
|
|
// Attempt attack (should be blocked)
|
|
attackBehavior.sendAttack(human);
|
|
|
|
// Execute a few ticks to process the attacks
|
|
for (let i = 0; i < 5; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
|
|
expect(bot.isAlliedWith(human)).toBe(true);
|
|
expect(human.incomingAttacks()).toHaveLength(0);
|
|
// Should be same number of attacks (no new attack created)
|
|
expect(bot.outgoingAttacks()).toHaveLength(attacksBefore);
|
|
});
|
|
|
|
test("nation cannot attack allied player", () => {
|
|
// Create nation
|
|
const nationInfo = new PlayerInfo(
|
|
"nation_test",
|
|
PlayerType.Nation,
|
|
null,
|
|
"nation_test",
|
|
);
|
|
game.addPlayer(nationInfo);
|
|
const nation = game.player("nation_test");
|
|
|
|
// Use helper for tile assignment
|
|
assignAlternatingLandTiles(game, [bot, human, nation], 21); // 21 to ensure each gets 7 tiles
|
|
|
|
nation.addTroops(1000);
|
|
|
|
// Provide an emoji behavior so sendAttack can run the full Nation code
|
|
// path; the attack on an ally must be blocked by AttackExecution's
|
|
// alliance check regardless of what the AI decides.
|
|
const nationRandom = new PseudoRandom(42);
|
|
const nationBehavior = new AiAttackBehavior(
|
|
nationRandom,
|
|
game,
|
|
nation,
|
|
0.5,
|
|
0.5,
|
|
0.2,
|
|
undefined,
|
|
new NationEmojiBehavior(nationRandom, game, nation),
|
|
);
|
|
|
|
// Alliance between nation and human
|
|
const allianceRequest = nation.createAllianceRequest(human);
|
|
allianceRequest?.accept();
|
|
|
|
expect(nation.isAlliedWith(human)).toBe(true);
|
|
|
|
const attacksBefore = nation.outgoingAttacks().length;
|
|
nation.addTroops(50_000);
|
|
|
|
// Force the attack past shouldAttack's dice gate so the alliance check
|
|
// in AttackExecution is the layer under test, regardless of RNG outcome.
|
|
nationBehavior.sendAttack(human, true);
|
|
|
|
// Execute a few ticks to process the attacks
|
|
for (let i = 0; i < 5; i++) {
|
|
game.executeNextTick();
|
|
}
|
|
|
|
expect(nation.isAlliedWith(human)).toBe(true);
|
|
expect(nation.outgoingAttacks()).toHaveLength(attacksBefore);
|
|
});
|
|
});
|