Clarify worker placement and automate equivalent plantation assignments

This commit is contained in:
coyotte508 2026-10-06 23:51:57 +02:00
commit 7bf0df6cdb
8 changed files with 102 additions and 11 deletions

View file

@ -12,6 +12,7 @@ without resetting their position or history.
| Pass with no available action | Automatic, using the normal phase transition. |
| Production with zero output | Automatic only when no optional point or Bohío choice remains. |
| Assignment with no people | Automatic. |
| Assignment to a single plantation or identical plantations | Automatic only with no buildings and no worker/citizen choice. |
| Storage with no goods | Automatic. |
| Worker placement, production quantities, storage quantities | Manual: a single suggested move still accepts custom choices. |
| Planting, building, trading, private ships, bonus workers/goods | Manual whenever declining or choosing an alternative is legal. |

View file

@ -1018,7 +1018,21 @@ function forcedAction(s, t, a) {
if (choices.length === 1) {
const m = choices[0];
if (["pass", "recruit", "discardWorker", "ship"].includes(m.type)) return m;
if (m.type === "assign" && people(a) === 0) return m;
if (m.type === "assign") {
if (people(a) === 0) return m;
// Identical plantations are interchangeable only without buildings or a
// worker/citizen tradeoff. Never apply a general suggested allocation.
const estates = a.estates.filter((e) => e.id !== "forest");
const w = a.reserve.w + sum(a.estates.map((e) => e.w));
const c = a.reserve.c + sum(a.estates.map((e) => e.c));
if (
!a.buildings.length &&
estates.length &&
estates.every((e) => e.id === estates[0].id) &&
!(w && c)
)
return m;
}
if (m.type === "store" && !GOODS.some((g) => a.goods[g])) return m;
}
if (
@ -1204,7 +1218,10 @@ function sameMove(a, b) {
return JSON.stringify(normalize(a)) === JSON.stringify(normalize(b));
}
export function move(state, m, p) {
let s = applyMove(state, m, p);
return resolveForcedActions(applyMove(state, m, p));
}
export function resolveForcedActions(state) {
let s = state;
if (!s.options.autoForcedActions) return s;
for (let guard = 0; guard < 1000; guard++) {
if (s.finished || !s.tasks.length) return s;

View file

@ -1,6 +1,6 @@
{
"name": "@boardgamers/puerto-rico",
"version": "0.2.3",
"version": "0.2.4",
"private": true,
"type": "module",
"description": "Puerto Rico 1897 Special Edition for Boardgamers Space",

View file

@ -125,9 +125,30 @@ try {
let s = E.move(original, m, 0);
while (s.tasks[0].kind !== "assign") s = E.moveAI(s);
s.tasks[0].p = 0;
s.players[0].estates = [
{ id: "corn", w: 0, c: 0 },
{ id: "fruit", w: 0, c: 0 },
];
s.players[0].buildings = [];
s.players[0].reserve = { w: 1, c: 0 };
await page.evaluate((s) => host.emit("state", s), E.stripSecret(s, 0));
await page.locator("[data-confirm-assign]").waitFor({ state: "visible" });
assert.ok(await page.locator("[data-slot]").count());
assert.equal(
await page.locator("[data-slot].w").count(),
0,
"new workers are not preassigned",
);
assert.equal(
await page.locator("[data-confirm-assign]").isEnabled(),
false,
);
await page.locator('[data-slot="e0:0"]').click();
assert.equal(await page.locator('[data-slot="e1:0"]').isEnabled(), false);
assert.equal(await page.locator('[data-slot="e1:0"]').innerText(), "·");
await page.locator('[data-slot="e0:0"]').click();
assert.equal(await page.locator('[data-slot="e1:0"]').isEnabled(), true);
await page.locator('[data-slot="e1:0"]').click();
assert.equal(await page.locator('[data-slot="e0:0"]').isEnabled(), false);
await page.locator("[data-confirm-assign]").click();
const allocation = await page.evaluate(() => played.at(-1));
assert.equal(allocation.type, "assign");

View file

@ -67,7 +67,10 @@ test("empty trade, production, assignment and storage need no confirmation", ()
test("suggested assignment, production and storage do not hide custom choices", () => {
for (const kind of ["assign", "produce", "store"]) {
const s = fixture([{ kind, p: 0 }, boundary]);
s.players[0].estates = [{ id: "corn", w: 1, c: 0 }];
s.players[0].estates = [
{ id: "corn", w: 1, c: 0 },
{ id: "fruit", w: 0, c: 0 },
];
s.players[0].goods.corn = 2;
const r = start(s);
assert.equal(r.tasks[0].kind, kind);
@ -138,3 +141,35 @@ test("Bohío alternatives prevent automatically passing a blocked trade", () =>
assert.equal(r.tasks[0].kind, "trade");
assert(E.legal(r).some((m) => m.type === "bohio"));
});
test("interchangeable plantation placements are automatic, strategic alternatives remain", () => {
for (const variant of [
"single",
"same",
"different",
"mixedPeople",
"building",
]) {
const s = fixture([{ kind: "assign", p: 0 }, boundary]);
s.players[0].estates = [{ id: "corn", w: 0, c: 0 }];
if (variant !== "single")
s.players[0].estates.push({
id: variant === "different" ? "fruit" : "corn",
w: 0,
c: 0,
});
s.players[0].reserve = { w: 1, c: variant === "mixedPeople" ? 1 : 0 };
if (variant === "building")
s.players[0].buildings = [{ id: "smallMarket", w: 0, c: 0 }];
const r = start(s);
assert.equal(
r.tasks[0].kind,
["single", "same"].includes(variant) ? "role" : "assign",
);
if (["single", "same"].includes(variant))
assert.equal(
r.players[0].estates.reduce((n, e) => n + e.w, 0),
1,
);
}
});

View file

@ -37,6 +37,7 @@ state ??= newGame();
// Keep existing local games while enabling the current interaction policy.
state.options.autoForcedActions = true;
state.config.options.autoForcedActions = true;
state = engine.resolveForcedActions(state);
const ui = mountGame(document.getElementById("game"), {
automatedSetup: true,
async onMove(move) {

View file

@ -1690,3 +1690,9 @@
.pr-game .achievement-reward .metric {
margin: 0;
}
.pr-game button.worker-slot:disabled {
opacity: 0.35;
border-style: dashed;
background: transparent;
}

View file

@ -164,8 +164,14 @@ export function mountGame(
if (key === draftKey) return;
draftKey = key;
draft = null;
if (enabled && seat === task?.p && task?.kind === "assign")
draft = autoAssignment(state, seat);
if (enabled && seat === task?.p && task?.kind === "assign") {
const a = state.players[seat];
draft = {
estates: a.estates.map(({ w, c }) => ({ w, c })),
buildings: a.buildings.map(({ w, c }) => ({ w, c })),
};
tool = a.reserve.w ? "w" : a.reserve.c ? "c" : "w";
}
if (enabled && seat === task?.p && task?.kind === "store")
draft = autoStorage(state, seat);
}
@ -322,7 +328,7 @@ export function mountGame(
})
.join("")}</div>`;
if (task.kind === "assign")
return `<div class="assignment-tools" role="group" aria-label="${esc(t("workers"))}">${["w", "c", "erase"].map((k) => `<button data-tool="${k}" class="${tool === k ? "selected" : ""}" aria-pressed="${tool === k}" aria-label="${esc(t(k === "erase" ? "erase" : k === "w" ? "workers" : "citizens"))}" title="${esc(t(k === "erase" ? "erase" : k === "w" ? "workers" : "citizens"))}">${icon(k === "w" ? "worker" : k === "c" ? "citizen" : "erase")}${k === "erase" ? "" : remaining()[k]}</button>`).join("")}<button class="icon-button" data-auto title="${esc(t("autoAssign"))}" aria-label="${esc(t("autoAssign"))}">${icon("autoAssign")}</button><span>${t("workerTool")}</span></div>`;
return `<div class="assignment-tools" role="group" aria-label="${esc(t("workers"))}">${["w", "c", "erase"].map((k) => `<button data-tool="${k}" class="${tool === k ? "selected" : ""}" aria-pressed="${tool === k}" aria-label="${esc(t(k === "erase" ? "erase" : k === "w" ? "workers" : "citizens"))}" title="${esc(t(k === "erase" ? "erase" : k === "w" ? "workers" : "citizens"))}">${icon(k === "w" ? "worker" : k === "c" ? "citizen" : "erase")}${k === "erase" ? "" : remaining()[k]}</button>`).join("")}<button class="icon-button" data-auto title="${esc(t("autoAssign"))}" aria-label="${esc(t("autoAssign"))}">${icon("autoAssign")}</button><span>${remaining().w + remaining().c === 0 ? `${t("noWorkers")}. ` : ""}${t("workerTool")}</span></div>`;
if (task.kind === "store") return storageEditor();
if (["role", "draft", "build", "plant"].includes(task.kind)) return "";
return `<div class="action-choices">${moves
@ -384,8 +390,10 @@ export function mountGame(
: x;
return `<div class="worker-slots">${Array.from({ length: n }, (_, i) => {
const k = i < values.c ? "c" : i < values.c + values.w ? "w" : null;
const unavailable =
editable && !k && (tool === "erase" || remaining()[tool] === 0);
return editable
? `<button class="worker-slot ${k ?? ""}" data-slot="${key}:${i}" aria-label="${esc(`${t(k === "c" ? "citizens" : k === "w" ? "workers" : "empty")} · ${t(x.id)}`)}">${k ? icon(k === "c" ? "citizen" : "worker", 20) : "+"}</button>`
? `<button class="worker-slot ${k ?? ""}" data-slot="${key}:${i}" ${unavailable ? "disabled" : ""} aria-label="${esc(`${t(k === "c" ? "citizens" : k === "w" ? "workers" : "empty")} · ${t(x.id)}`)}">${k ? icon(k === "c" ? "citizen" : "worker", 20) : unavailable ? "·" : "+"}</button>`
: `<span class="worker-slot ${k ?? ""}">${k ? icon(k === "c" ? "citizen" : "worker", 20) : "·"}</span>`;
}).join("")}</div>`;
}
@ -879,8 +887,10 @@ export function mountGame(
draft[key[0] === "e" ? "estates" : "buildings"][Number(key.slice(1))],
index = Number(ix);
const occupied = index < x.c ? "c" : index < x.c + x.w ? "w" : null;
if (occupied) x[occupied]--;
else if (tool !== "erase" && remaining()[tool] > 0) x[tool]++;
if (occupied) {
x[occupied]--;
tool = occupied;
} else if (tool !== "erase" && remaining()[tool] > 0) x[tool]++;
render();
return;
}