From 8b9b36cd0b2a0423e2d4ef535ed9e9203d37c861 Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Fri, 30 Oct 2026 16:43:05 +0100 Subject: [PATCH] Avoid repeatedly cloning history during Vanuatu replay --- engine/index.js | 29 ++++++++++++++--------------- package.json | 2 +- test/engine.test.js | 15 ++++++++++++++- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/engine/index.js b/engine/index.js index e14bda4..48d2e42 100644 --- a/engine/index.js +++ b/engine/index.js @@ -993,29 +993,27 @@ function automate(s) { check(guard < 100, "Automatic turn limit"); } function applyMove(data, input, p) { - check(!data.finished, "The game has ended"); + return applyMoveInPlace(copy(data), input, p); +} +function applyMoveInPlace(s, input, p) { + check(!s.finished, "The game has ended"); check( - Number.isInteger(p) && p >= 0 && p < data.players.length, + Number.isInteger(p) && p >= 0 && p < s.players.length, "Unknown player", ); const m = normalizeMove(input); - check( - p === data.actor || ["beg", "treasure"].includes(m.type), - "Not your turn", - ); - const legal = availableMoves(data, p).find((x) => - matches(normalizeMove(x), m), - ); + check(p === s.actor || ["beg", "treasure"].includes(m.type), "Not your turn"); + const legal = availableMoves(s, p).find((x) => matches(normalizeMove(x), m)); check(legal, "That move is not available"); - const s = copy(data); + const autoRetrieve = s.players.map( + (_, q) => playerSettings(s, q).autoRetrieve, + ); execute(s, legal, p); automate(s); s._history.push({ p, move: copy(legal), - autoRetrieve: data.players.map( - (_, q) => playerSettings(data, q).autoRetrieve, - ), + autoRetrieve, }); saveFrame(s); return s; @@ -1121,7 +1119,7 @@ export function replay(s, { to = logLength(s) } = {}) { "Invalid replay position", ); const b = s._setup; - let r = initialize( + const r = initialize( b.count, b.expansions, b.options, @@ -1133,7 +1131,8 @@ export function replay(s, { to = logLength(s) } = {}) { r.players.forEach((p, i) => { p.settings = { ...p.settings, autoRetrieve: h.autoRetrieve?.[i] ?? true }; }); - r = applyMove(r, h.move, h.p); + // Replay owns this fresh state; cloning its accumulated frames per move is quadratic. + applyMoveInPlace(r, h.move, h.p); } s.players.forEach((p, i) => { r.players[i].name = p.name; diff --git a/package.json b/package.json index c265de5..cca62f3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@boardgamers/vanuatu", - "version": "0.1.9", + "version": "0.1.10", "private": true, "type": "module", "main": "dist/engine.js", diff --git a/test/engine.test.js b/test/engine.test.js index 4fcf837..d577f34 100644 --- a/test/engine.test.js +++ b/test/engine.test.js @@ -184,14 +184,27 @@ test("hidden deck, future RNG and rest choice are not exposed", () => { }); test("deterministic replay reconstructs every history boundary", () => { let s = E.init(3, [], {}, "replay"); - for (let n = 0; n < 14; n++) s = E.moveAI(s, s.actor); + const states = [s]; + for (let n = 0; n < 14; n++) { + const before = structuredClone(s); + s = E.moveAI(s, s.actor); + assert.deepEqual(states.at(-1), before); + states.push(s); + } + const before = structuredClone(s); for (let n = 1; n <= E.logLength(s); n++) { const r = E.createAnalysis(s, { to: n }); assert.equal(E.logLength(r), n); + assert.deepEqual(r._history, states[n - 1]._history); + assert.deepEqual(r.events, states[n - 1].events); + assert.equal(r._rng, states[n - 1]._rng); assert.deepEqual( E.stripSecret(r, 0).board, E.logSlice(s, { player: 0, start: n - 1, end: n - 1 }).frames[0].board, ); + r.players[0].money = -100; + r._frames[0].players[0].money = -100; + assert.deepEqual(s, before); } }); test("Rising Waters floods after its action countdown and dikes protect edges", () => {