Avoid repeatedly cloning history during Vanuatu replay
This commit is contained in:
parent
cf5b66cca7
commit
8b9b36cd0b
3 changed files with 29 additions and 17 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@boardgamers/vanuatu",
|
||||
"version": "0.1.9",
|
||||
"version": "0.1.10",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"main": "dist/engine.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", () => {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue