From de72c83e90cbf8156165610a7c9acbd16f183a2d Mon Sep 17 00:00:00 2001 From: coyotte508 Date: Tue, 8 Sep 2026 17:58:34 +0200 Subject: [PATCH] Pace expedition playback with sound and refine journal, equipment choices and AI --- docs/audio-credits.md | 10 + docs/rules-status.md | 8 +- package.json | 2 +- packages/engine/src/ai.ts | 46 ++++- packages/engine/src/game.spec.ts | 91 +++++++++ packages/engine/src/game.ts | 38 +++- packages/engine/src/types.ts | 9 + packages/viewer/src/App.svelte | 190 +++++++++++++++---- packages/viewer/src/assets/dice-roll.mp3 | Bin 0 -> 22663 bytes packages/viewer/src/dev.ts | 14 +- packages/viewer/src/lib/EquipmentIcon.svelte | 33 ++++ packages/viewer/src/lib/JournalEntry.svelte | 70 ++++++- packages/viewer/src/lib/Landscape.svelte | 14 +- packages/viewer/src/lib/sound.ts | 144 ++++++++++++++ packages/viewer/src/lib/store.svelte.ts | 96 +++++++++- packages/viewer/src/lib/theme.css | 150 +++++++++++++++ packages/viewer/src/viewer.ts | 1 + tests/bgs-bundle-smoke.mjs | 26 +++ tests/browser-smoke.mjs | 3 + tests/presentation-smoke.mjs | 100 ++++++++++ 20 files changed, 989 insertions(+), 56 deletions(-) create mode 100644 docs/audio-credits.md create mode 100644 packages/viewer/src/assets/dice-roll.mp3 create mode 100644 packages/viewer/src/lib/EquipmentIcon.svelte create mode 100644 packages/viewer/src/lib/sound.ts create mode 100644 tests/presentation-smoke.mjs diff --git a/docs/audio-credits.md b/docs/audio-credits.md new file mode 100644 index 0000000..0cb56c0 --- /dev/null +++ b/docs/audio-credits.md @@ -0,0 +1,10 @@ +# Audio credits + +`packages/viewer/src/assets/dice-roll.mp3` is adapted from `dice-29.wav` in **2 dice roll (29 throws)** by **RPG**, published 30 August 2013 on OpenGameArt. + +- Source: https://opengameart.org/content/2-dice-roll-29-throws +- Original archive: https://opengameart.org/sites/default/files/Dice%20sound.7z +- License: CC0 1.0, https://creativecommons.org/publicdomain/zero/1.0/ +- Adaptation: retain the first 1.42 seconds, remove low-frequency rumble, raise level by 2 dB, fade the final 60 ms, encode as mono MP3. + +Movement, lava and equipment cues are synthesized by the viewer and use no external recordings. All cues follow the platform `preferences.sound` setting. diff --git a/docs/rules-status.md b/docs/rules-status.md index 995d4ab..971ec97 100644 --- a/docs/rules-status.md +++ b/docs/rules-status.md @@ -64,6 +64,12 @@ Automatic equipment passes use only public eligibility (cards, phase, injuries), ### Baseline AI -`moveAI(state, player)` is exported from both the engine and BGS wrapper and returns a new state through `applyMove`. The local harness and full-game simulations call the same implementation. Decisions use `stripSecret` for that player, with no access to hidden teammate dice or the random seed. The route heuristic prioritizes destinations outside the next eruption, then balances own matching total, village distance, distance from lava, pickups and eruption markers. Rerolls target nonmatching dice; unused rerolls yield Gatherer bars. It spends the minimum bars that turn a failed comparison into success. It does not optimize equipment use or Gatherer bar farming. +`moveAI(state, player)` is exported from both the engine and BGS wrapper and returns a new state through `applyMove`. The local harness and full-game simulations call the same implementation. Decisions use `stripSecret` for that player, with no access to hidden teammate dice or the random seed. The route heuristic prioritizes destinations outside the next eruption, then balances expected comparison lead after the available rerolls, village distance, distance from lava, pickups and eruption markers. Expectations use the published die faces and only publicly visible teammate faces. Rerolls include low matching faces when rolling again improves expected contribution; unused rerolls yield Gatherer bars. This is a heuristic, not a guarantee of movement or a full model of teammates’ future rerolls. It spends the minimum bars that turn a failed comparison into success. It does not optimize equipment use or Gatherer bar farming. The wrapper's `currentPlayer` excludes players with confirmed planning intentions, so the platform bot driver does not repeatedly play an already-ready bot. Provisional confirmations remain live updates; the platform's stored clock behavior is unchanged. + +## Viewer event pacing + +Public movement paths and eruption cell lists in journal events allow the viewer to sequence automatic resolution without extra engine moves or saved intermediate states. The map and journal play events in order, with a skip control; the authoritative received state and platform clocks are unchanged. Initial loads and seat changes show the current state immediately. Reduced motion skips intermediate path positions. The local bot driver waits for playback and uses a 2.2-second action cadence. Confirmed-route journal records preserve the original terrain, equipment and eruption markers and reroll allowance. Equipment is public (rulebook page 8, face-up cards) and appears as pictograms with hover previews on player panels. + +Sound effects combine a CC0 recording of real dice throws (see `audio-credits.md`) with synthesized Web Audio foley synchronized with presented events (steps, lava, dice, equipment), governed by the platform's reserved `preferences.sound` boolean (absent means enabled, per platform PR #503). No game-specific preference overrides the platform mute. The harness offers a local sound toggle, individual cue previews, and a random-seed restart. Copy equipment, shovel values, preparation dice and local game configuration expose their choices directly without select controls. diff --git a/package.json b/package.json index c3dc89c..94e320f 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "fmt": "oxfmt", "fmt:check": "oxfmt --check", "simulate": "pnpm --filter fuji-engine build && node scripts/simulate.mjs", - "test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs" + "test:browser": "node tests/browser-smoke.mjs && node tests/bgs-bundle-smoke.mjs && node tests/presentation-smoke.mjs" }, "devDependencies": { "oxfmt": "0.61.0", diff --git a/packages/engine/src/ai.ts b/packages/engine/src/ai.ts index defe39c..15031bf 100644 --- a/packages/engine/src/ai.ts +++ b/packages/engine/src/ai.ts @@ -7,14 +7,29 @@ import { neighbors, paths, cell, - total, + rerollAllowance, activeDice, face, threatened, } from "./game.js"; -import { terrain, SKILLS, matches } from "./data.js"; +import { terrain, SKILLS, matches, DICE, type Requirement } from "./data.js"; import type { State, View, Move } from "./types.js"; +// Expected contribution after rolling, with the option to keep a result or +// try again. This uses the published die faces, never a teammate's hidden roll. +function rerollValue(type: number, requirement: Requirement, attempts: number): number { + let value = 0; + for (let i = 0; i < attempts; i++) { + const continuation = value; + value = + DICE[type]!.reduce((sum, color, index) => { + const contribution = matches({ color, value: index + 1 }, requirement) ? index + 1 : 0; + return sum + Math.max(contribution, continuation); + }, 0) / 6; + } + return value; +} + // Conservative playtest policy: spend the minimum that changes a failed // comparison into a success, rather than saving every bar indefinitely. export function choosePowerBars(state: State | View, helper: number): number { @@ -53,10 +68,33 @@ export function chooseMove(game: View, seat: number): Move { const lavaDistance = Math.min( ...game.board.filter((tile) => tile.lava).map((tile) => Math.abs(c.x - tile.x) + Math.abs(c.y - tile.y)) ); + const requirement = terrain(c.terrain).requirement; + const attempts = rerollAllowance(game, seat, path); + const expected = activeDice(p).reduce( + (sum, d) => + sum + Math.max(matches(face(d), requirement) ? d.face : 0, rerollValue(d.type, requirement, attempts)), + p.bonus + ); + const opposition = neighbors(game, seat).map((i) => + activeDice(game.players[i]!).reduce( + (sum, d) => + sum + (d.face ? (matches(face(d), requirement) ? d.face : 0) : rerollValue(d.type, requirement, 1)), + 0 + ) + ); + if (game.players.length === 2) + opposition.push( + game.ghost.reduce( + (sum, d) => + sum + (d.face ? (matches(face(d), requirement) ? d.face : 0) : rerollValue(d.type, requirement, 1)), + 0 + ) + ); + const expectedLead = expected - Math.max(...opposition); return ( (danger.has(c.id) ? -1000 : 0) - 12 / Math.max(1, lavaDistance) + - total(p, c.terrain) * 0.65 - + expectedLead * 1 - closestVillage * 4 + (terrain(c.terrain).kind === "village" ? 8 : 0) + (c.equipment ? 1 : 0) - @@ -72,7 +110,7 @@ export function chooseMove(game: View, seat: number): Move { if (game.phase === "reroll") { const r = terrain(cell(game, p.path.at(-1)!).terrain).requirement; const ids = activeDice(p) - .filter((d) => !matches(face(d), r)) + .filter((d) => (matches(face(d), r) ? d.face : 0) < rerollValue(d.type, r, p.rerolls)) .map((d) => d.id); return p.rerolls && ids.length ? { action: "reroll", ids } : { action: "finishRerolls" }; } diff --git a/packages/engine/src/game.spec.ts b/packages/engine/src/game.spec.ts index 253288f..c44f557 100644 --- a/packages/engine/src/game.spec.ts +++ b/packages/engine/src/game.spec.ts @@ -770,3 +770,94 @@ test("compact journey snapshots distinguish planned stays, failed comparisons an ); } }); + +test("AI rerolls weak matching dice but keeps strong matches without consulting hidden rolls", async () => { + const { chooseMove } = await import("../index.js"); + const s = withPhase("reroll"); + const p = s.players[1]!; + cell(s, p.path.at(-1)!).terrain = 4; + p.rerolls = 1; + p.dice.forEach((d) => (d.face = 1)); + const weak = p.dice.find((d) => d.type === 1)!; + const strong = p.dice.find((d) => d.id !== weak.id)!; + strong.face = 6; + const choice = chooseMove(stripSecret(s, 1), 1); + assert.equal(choice.action, "reroll"); + assert((choice.ids as string[]).includes(weak.id)); + assert(!(choice.ids as string[]).includes(strong.id)); + s.players[0]!.dice.forEach((d) => (d.face = 6)); + assert.deepEqual(chooseMove(stripSecret(s, 1), 1), choice); + p.rerolls = 0; + assert.equal(chooseMove(stripSecret(s, 1), 1).action, "finishRerolls"); +}); + +test("AI weighs reroll opportunities before committing to a weak three-step route", async () => { + const { chooseMove } = await import("../index.js"); + const s = withPhase("planning"); + s.players.forEach((p) => { + p.position = "0,0"; + p.path = ["0,0"]; + p.dice.forEach((d) => (d.face = 1)); + }); + s.board = [0, 1, 2, 3, 4].map((x) => ({ + id: `${x},0`, + x, + y: 0, + terrain: x === 4 ? 30 : 4, + lava: false, + equipment: false, + eruption: 0, + })); + const choice = chooseMove(stripSecret(s, 1), 1); + const path = choice.action === "plan" ? (choice.path as string[]) : s.players[1]!.path; + assert(path.length < 4); + assert(rerollAllowance(s, 1, path) > 0); + s.players[0]!.dice.forEach((d) => (d.face = 6)); + assert.deepEqual(chooseMove(stripSecret(s, 1), 1), choice); +}); + +test("confirmed route journal preserves terrain, pickups and untriggered eruptions", () => { + let s = withPhase("planning"); + s.players[1]!.position = "0,3"; + s.players[1]!.path = ["0,3"]; + s.players[2]!.position = "1,2"; + s.players[2]!.path = ["1,2"]; + s.players[0]!.path = ["0,3", "1,3"]; + cell(s, "1,3").eruption = 1; + cell(s, "1,3").equipment = true; + s.players[0]!.ready = true; + s.players[1]!.ready = true; + const expectedRerolls = rerollAllowance(s, 0); + s = applyMove(s, { action: "ready" }, 2); + const route = s.log.find((e) => e.route?.name === s.players[0]!.name)!.route!; + assert.equal(route.rerolls, expectedRerolls); + assert.deepEqual(route.cells.at(-1), { terrain: cell(s, "1,3").terrain, eruption: 1, equipment: true }); + cell(s, "1,3").eruption = 0; + cell(s, "1,3").equipment = false; + assert.equal(route.cells.at(-1)!.eruption, 1); + assert.equal(route.cells.at(-1)!.equipment, true); +}); + +test("set-aside journal snapshots reveal only the chosen dice", () => { + for (const kind of ["buddy", "machete", "knife"] as const) { + let s = withPhase(kind === "buddy" ? "reroll" : "equipment"); + const ids = s.players[0]!.dice.slice(0, kind === "buddy" ? 1 : 2).map((d) => d.id); + if (kind === "buddy") s = applyMove(s, { action: "buddy", ids }, 0); + else { + if (kind === "knife") s.players[1]!.cards = [{ id: "machete", used: 0, availableRound: 0 }]; + s = equip(s, 0, kind, { ids, ...(kind === "knife" ? { copy: "machete" } : {}) }); + } + const revealed = s.log.find((e) => e.setAside)!.setAside!; + assert.deepEqual( + revealed.map((d) => d.id), + ids + ); + assert(revealed.every((d) => d.face > 0 && d.aside)); + const frozen = structuredClone(revealed); + s.players[0]!.dice[0]!.face = 1; + assert.deepEqual(revealed, frozen); + const publicView = stripSecret(s, 1); + assert.deepEqual(publicView.log.find((e) => e.setAside)!.setAside, frozen); + assert(publicView.players[0]!.dice.filter((d) => !d.aside).every((d) => d.face === 0)); + } +}); diff --git a/packages/engine/src/game.ts b/packages/engine/src/game.ts index 84412bd..5518cf4 100644 --- a/packages/engine/src/game.ts +++ b/packages/engine/src/game.ts @@ -148,6 +148,7 @@ function phase(s: State, next: State["phase"]) { }[next], "phase" ); + if (next === "planning") s.log.at(-1)!.sound = "dice"; if (next === "movement") { for (const p of s.players) diceEvent(s, `${p.name} revealed their dice`, p.dice); if (s.ghost.length) diceEvent(s, "Neutral dice revealed", s.ghost); @@ -184,6 +185,7 @@ function erupt(s: State) { const next = threatened(s); for (const c of s.board) if (next.includes(c.id)) c.lava = true; event(s, `Lava consumed ${next.length} location${next.length === 1 ? "" : "s"}.`, "eruption"); + s.log.at(-1)!.animation = { kind: "eruption", cells: next }; const victim = s.players.find((p) => cell(s, p.position).lava); if (victim) end(s, "lost", `${victim.name} was caught by the lava.`); } @@ -390,6 +392,7 @@ function useEquipment(s: State, seat: number, m: Move) { if (!(info.phases as readonly number[]).includes(s.phase === "planning" ? 2 : 4)) throw Error("This equipment is not usable in this phase."); const target = () => integer(m.target, 0, s.players.length - 1); + let setAside: Die[] | undefined; switch (id) { case "binoculars": { const ids = strings(m.tiles, 2); @@ -416,6 +419,8 @@ function useEquipment(s: State, seat: number, m: Move) { const c = cell(s, ids[0]!); if (!walkable(c) || terrain(c.terrain).kind !== "land" || distance(cell(s, p.position), c) !== 1) throw Error("Choose an adjacent land location."); + event(s, `${p.name} moved to ${terrain(c.terrain).name} using Rope.`); + s.log.at(-1)!.animation = { kind: "move", seat, path: [p.position, c.id] }; p.position = c.id; collect(s, p); trigger(s, [c.id]); @@ -453,7 +458,8 @@ function useEquipment(s: State, seat: number, m: Move) { break; } case "machete": - for (const d of selectedDice(p, m.ids, 1, 2)) d.aside = true; + setAside = selectedDice(p, m.ids, 1, 2); + for (const d of setAside) d.aside = true; break; case "lighter": { const donor = target(); @@ -480,6 +486,8 @@ function useEquipment(s: State, seat: number, m: Move) { } resetReady(s); event(s, `${p.name} used ${equipment(card.id).name}${id !== card.id ? ` as ${info.name}` : ""}.`, "equipment"); + if (id === "torch") s.log.at(-1)!.sound = "dice"; + if (setAside) s.log.at(-1)!.setAside = structuredClone(setAside); } function execute(s: State, m: Move, seat: number) { if (s.outcome) throw Error("The expedition has ended."); @@ -506,6 +514,7 @@ function execute(s: State, m: Move, seat: number) { } } else throw Error("Resolve the equipment effect first."); event(s, `${p.name} resolved an equipment effect.`, "equipment"); + if (pending.kind === "reroll") s.log.at(-1)!.sound = "dice"; finishPending(s); return; } @@ -550,7 +559,21 @@ function execute(s: State, m: Move, seat: number) { if (s.phase !== "planning") event(s, `${p.name} is ready.`, "move", true); if (s.players.every((p) => p.ready)) { if (s.phase === "planning") { - for (const player of s.players) event(s, `${player.name} chose ${player.path.join(" → ")}.`); + for (const [seat, player] of s.players.entries()) { + event( + s, + `${player.name} chose ${terrain(cell(s, player.path.at(-1)!).terrain).name} · ${player.path.length - 1} steps · ${rerollAllowance(s, seat)} rerolls.` + ); + s.log.at(-1)!.route = { + name: player.name, + character: player.character, + rerolls: rerollAllowance(s, seat), + cells: player.path.map((id) => { + const c = cell(s, id); + return { terrain: c.terrain, eruption: c.eruption, equipment: c.equipment }; + }), + }; + } phase(s, "reroll"); s.players.forEach((p, i) => { p.rerolls = rerollAllowance(s, i); @@ -565,6 +588,7 @@ function execute(s: State, m: Move, seat: number) { roll(s, dice); p.rerolls--; event(s, `${p.name} rerolled.`); + s.log.at(-1)!.sound = "dice"; break; } case "finishRerolls": { @@ -578,9 +602,11 @@ function execute(s: State, m: Move, seat: number) { case "buddy": { if (s.phase !== "reroll" || p.ready || !hasSkill(p, "buddy") || p.buddyUsed) throw Error("This skill is unavailable."); - selectedDice(p, m.ids, 1, 1)[0]!.aside = true; + const die = selectedDice(p, m.ids, 1, 1)[0]!; + die.aside = true; p.buddyUsed = true; event(s, `${p.name} set a die aside.`); + s.log.at(-1)!.setAside = [structuredClone(die)]; break; } case "give": { @@ -694,6 +720,7 @@ function execute(s: State, m: Move, seat: number) { `${p.name} reached ${terrain(cell(s, dest).terrain).name} (${result.own} vs ${Math.max(...result.peers.map((x) => x.total))}).` ); s.log.at(-1)!.journey = journey; + if (journey.moved) s.log.at(-1)!.animation = { kind: "move", seat, path: [...route] }; win(s); if (s.outcome) break; collect(s, p); @@ -934,7 +961,10 @@ export function applyMove(data: State, input: unknown, seat: number): State { advanceForcedActions(s); s.revision++; const live = - data.phase === "planning" && s.phase === "planning" && (move.action === "plan" || move.action === "ready"); + data.phase === "planning" && + s.phase === "planning" && + s.round === data.round && + (move.action === "plan" || move.action === "ready"); if (live) { s.liveUpdate = true; s.planningSnapshot = { diff --git a/packages/engine/src/types.ts b/packages/engine/src/types.ts index 392b096..d73ec02 100644 --- a/packages/engine/src/types.ts +++ b/packages/engine/src/types.ts @@ -44,6 +44,15 @@ export interface Cell { eruption: number; } export interface Event { + setAside?: Die[]; + sound?: "dice"; + route?: { + name: string; + character: number; + rerolls: number; + cells: { terrain: number; eruption: number; equipment: boolean }[]; + }; + animation?: { kind: "move"; seat: number; path: string[] } | { kind: "eruption"; cells: string[] }; detail?: boolean; transfer?: { id: EquipmentId; from: string; to: string }; journey?: { diff --git a/packages/viewer/src/App.svelte b/packages/viewer/src/App.svelte index 13a353d..c28378a 100644 --- a/packages/viewer/src/App.svelte +++ b/packages/viewer/src/App.svelte @@ -27,6 +27,7 @@ import { art } from "./lib/assets"; import Landscape from "./lib/Landscape.svelte"; import Die from "./lib/Die.svelte"; + import EquipmentIcon from "./lib/EquipmentIcon.svelte"; import JournalEntry from "./lib/JournalEntry.svelte"; import PhaseIcon from "./lib/PhaseIcon.svelte"; import RequirementDisplay from "./lib/RequirementDisplay.svelte"; @@ -56,7 +57,7 @@ let help = $state(false); let journal = $state(true); const journalEntries = $derived( - s?.log + store.journal .filter((entry) => !entry.detail) .slice() .reverse() ?? [] @@ -81,7 +82,18 @@ const focusTerrain = $derived(focus ? terrain(focus.terrain) : undefined); const currentRoute = $derived(me?.path ?? []); const destination = $derived(s && currentRoute.length ? terrain(cell(s, currentRoute.at(-1)!).terrain) : undefined); - const actionTool = $derived(tool === "knife" ? copied : tool); + const copyOptions = $derived( + EQUIPMENT.filter( + (info) => + info.id !== "knife" && + (info.phases as readonly number[]).includes(s?.phase === "planning" ? 2 : 4) && + s?.players.some((p, i) => i !== seat && p.cards.some((c) => c.id === info.id && c.availableRound <= s.round)) + ) + ); + $effect(() => { + if (tool === "knife" && !copyOptions.some((c) => c.id === copied) && copyOptions[0]) copied = copyOptions[0].id; + }); + const actionTool = $derived(tool === "knife" ? (copyOptions.some((c) => c.id === copied) ? copied : null) : tool); const pendingMe = $derived(s?.pending && seat !== undefined && s.pending.players.includes(seat)); const revealed = $derived(s?.phase === "movement" || s?.phase === "eruption" || s?.phase === "ended"); const resolvingSeat = $derived(s?.activeResolution ?? seat); @@ -271,8 +283,35 @@ onchange={(e) => store.setAutoTeammates(e.currentTarget.checked)} />Automatically play teammates - + + +
+ Test sounds +
+ + + + +
+
+ {/if} @@ -326,6 +365,30 @@ : ""} + {#if p.cards.length} + {#each p.cards as c}{@const info = EQUIPMENT.find((e) => e.id === c.id)!} + s.round} + title={`${info.name}: ${info.description}`} + > + + + {info.name} + {info.name}{info.description} + {#each info.phases as phase}{phase === 2 ? "Plan" : "Equip"}{/each} + {#if c.availableRound > s.round}Available from round {c.availableRound}{/if} + + + + {/each} + {/if}