Compare commits
8 Commits
feature/du
...
71bdc6d031
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
71bdc6d031 | ||
|
|
6c2257b032 | ||
|
|
cf98636a52 | ||
|
|
d504008030 | ||
|
|
50873e6989 | ||
|
|
ec0de5b0b8 | ||
|
|
4dde4bff99 | ||
|
|
39703ce6b0 |
535
src/App.tsx
535
src/App.tsx
@@ -1,71 +1,502 @@
|
|||||||
|
import React from "react";
|
||||||
|
|
||||||
import { sampleContentPack } from "@/data/sampleContentPack";
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
import { createStartingAdventurer } from "@/rules/character";
|
||||||
|
import {
|
||||||
|
createRunState,
|
||||||
|
enterCurrentRoom,
|
||||||
|
getAvailableMoves,
|
||||||
|
isCurrentRoomCombatReady,
|
||||||
|
resolveRunEnemyTurn,
|
||||||
|
resolveRunPlayerTurn,
|
||||||
|
startCombatInCurrentRoom,
|
||||||
|
travelCurrentExit,
|
||||||
|
} from "@/rules/runState";
|
||||||
|
import type { RunState } from "@/types/state";
|
||||||
|
|
||||||
const planningDocs = [
|
function createDemoRun() {
|
||||||
"Planning/PROJECT_PLAN.md",
|
const adventurer = createStartingAdventurer(sampleContentPack, {
|
||||||
"Planning/GAME_SPEC.md",
|
name: "Aster",
|
||||||
"Planning/content-checklist.json",
|
weaponId: "weapon.short-sword",
|
||||||
"Planning/DATA_MODEL.md",
|
armourId: "armour.leather-vest",
|
||||||
"Planning/IMPLEMENTATION_NOTES.md",
|
scrollId: "scroll.lesser-heal",
|
||||||
];
|
});
|
||||||
|
|
||||||
const nextTargets = [
|
return createRunState({
|
||||||
"Encode Level 1 foundational tables into structured JSON.",
|
content: sampleContentPack,
|
||||||
"Implement dice utilities for D3, D6, 2D6, and D66.",
|
campaignId: "campaign.demo",
|
||||||
"Create character creation state and validation.",
|
adventurer,
|
||||||
"Build deterministic room generation for the Level 1 loop.",
|
});
|
||||||
];
|
}
|
||||||
|
|
||||||
|
function getRoomTitle(run: RunState, roomId?: string) {
|
||||||
|
if (!roomId) {
|
||||||
|
return "Unknown Room";
|
||||||
|
}
|
||||||
|
|
||||||
|
const room = run.dungeon.levels[run.currentLevel]?.rooms[roomId];
|
||||||
|
|
||||||
|
if (!room) {
|
||||||
|
return "Unknown Room";
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
sampleContentPack.roomTemplates.find((template) => template.id === room.templateId)?.title ??
|
||||||
|
room.notes[0] ??
|
||||||
|
room.templateId ??
|
||||||
|
room.id
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDefinitionName(definitionId: string) {
|
||||||
|
const item =
|
||||||
|
sampleContentPack.items.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.weapons.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.armour.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.scrolls.find((candidate) => candidate.id === definitionId) ??
|
||||||
|
sampleContentPack.potions.find((candidate) => candidate.id === definitionId);
|
||||||
|
|
||||||
|
return item?.name ?? definitionId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatInventoryEntry(definitionId: string, quantity: number) {
|
||||||
|
const name = getDefinitionName(definitionId);
|
||||||
|
return quantity > 1 ? `${quantity}x ${name}` : name;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getTreasureItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.itemType === "treasure")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSupportItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.itemType !== "treasure")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getConsumableItemIds() {
|
||||||
|
return new Set(
|
||||||
|
sampleContentPack.items
|
||||||
|
.filter((item) => item.consumable || item.itemType === "ration")
|
||||||
|
.map((item) => item.id),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const treasureItemIds = getTreasureItemIds();
|
||||||
|
const supportItemIds = getSupportItemIds();
|
||||||
|
const consumableItemIds = getConsumableItemIds();
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
const [run, setRun] = React.useState<RunState>(() => createDemoRun());
|
||||||
|
const currentLevel = run.dungeon.levels[run.currentLevel];
|
||||||
|
const currentRoom = run.currentRoomId ? currentLevel?.rooms[run.currentRoomId] : undefined;
|
||||||
|
const availableMoves = getAvailableMoves(run);
|
||||||
|
const combatReadyEncounter = isCurrentRoomCombatReady(run);
|
||||||
|
const carriedTreasure = run.adventurerSnapshot.inventory.carried.filter((entry) =>
|
||||||
|
treasureItemIds.has(entry.definitionId),
|
||||||
|
);
|
||||||
|
const carriedConsumables = run.adventurerSnapshot.inventory.carried.filter(
|
||||||
|
(entry) =>
|
||||||
|
consumableItemIds.has(entry.definitionId) ||
|
||||||
|
entry.definitionId.startsWith("potion.") ||
|
||||||
|
entry.definitionId.startsWith("scroll."),
|
||||||
|
);
|
||||||
|
const carriedGear = run.adventurerSnapshot.inventory.carried.filter(
|
||||||
|
(entry) =>
|
||||||
|
supportItemIds.has(entry.definitionId) &&
|
||||||
|
!consumableItemIds.has(entry.definitionId),
|
||||||
|
);
|
||||||
|
const equippedItems = run.adventurerSnapshot.inventory.equipped;
|
||||||
|
const latestLoot = run.lootedItems.slice(-4).reverse();
|
||||||
|
|
||||||
|
const handleReset = () => {
|
||||||
|
setRun(createDemoRun());
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEnterRoom = () => {
|
||||||
|
setRun((previous) => enterCurrentRoom({ content: sampleContentPack, run: previous }).run);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleStartCombat = () => {
|
||||||
|
setRun((previous) =>
|
||||||
|
startCombatInCurrentRoom({ content: sampleContentPack, run: previous }).run,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleTravel = (direction: "north" | "east" | "south" | "west") => {
|
||||||
|
setRun((previous) =>
|
||||||
|
travelCurrentExit({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run: previous,
|
||||||
|
exitDirection: direction,
|
||||||
|
}).run,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePlayerTurn = (manoeuvreId: string, targetEnemyId: string) => {
|
||||||
|
setRun((previous) =>
|
||||||
|
resolveRunPlayerTurn({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run: previous,
|
||||||
|
manoeuvreId,
|
||||||
|
targetEnemyId,
|
||||||
|
}).run,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleEnemyTurn = () => {
|
||||||
|
setRun((previous) =>
|
||||||
|
resolveRunEnemyTurn({ content: sampleContentPack, run: previous }).run,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="app-shell">
|
<main className="app-shell">
|
||||||
<section className="hero">
|
<section className="hero">
|
||||||
<p className="eyebrow">2D6 Dungeon Web</p>
|
<div>
|
||||||
<h1>Project scaffold is live.</h1>
|
<p className="eyebrow">2D6 Dungeon Web</p>
|
||||||
<p className="lede">
|
<h1>Dungeon Loop Shell</h1>
|
||||||
The app now has a Vite + React + TypeScript foundation, shared type
|
<p className="lede">
|
||||||
models, and Zod schemas that mirror the planning documents.
|
Traverse generated rooms, auto-resolve room entry, and engage combat
|
||||||
</p>
|
when a room reveals a real encounter.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div className="hero-actions">
|
||||||
|
<button className="button button-primary" onClick={handleReset}>
|
||||||
|
Reset Demo Run
|
||||||
|
</button>
|
||||||
|
<div className="status-chip">
|
||||||
|
<span>Run Status</span>
|
||||||
|
<strong>{run.status}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section className="panel-grid">
|
{combatReadyEncounter && !run.activeCombat ? (
|
||||||
<article className="panel">
|
<section className="alert-banner">
|
||||||
<h2>Planning Set</h2>
|
<div>
|
||||||
<ul>
|
<span className="alert-kicker">Encounter Ready</span>
|
||||||
{planningDocs.map((doc) => (
|
<strong>{currentRoom?.encounter?.resultLabel}</strong>
|
||||||
<li key={doc}>{doc}</li>
|
<p>
|
||||||
))}
|
This room contains a combat-ready encounter. Engage now to enter
|
||||||
</ul>
|
tactical resolution.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<button className="button button-primary" onClick={handleStartCombat}>
|
||||||
|
Engage Encounter
|
||||||
|
</button>
|
||||||
|
</section>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<section className="dashboard-grid">
|
||||||
|
<article className="panel panel-highlight">
|
||||||
|
<div className="panel-header">
|
||||||
|
<h2>Adventurer</h2>
|
||||||
|
<span>Level {run.adventurerSnapshot.level}</span>
|
||||||
|
</div>
|
||||||
|
<div className="stat-strip">
|
||||||
|
<div>
|
||||||
|
<span>HP</span>
|
||||||
|
<strong>
|
||||||
|
{run.adventurerSnapshot.hp.current}/{run.adventurerSnapshot.hp.max}
|
||||||
|
</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span>Shift</span>
|
||||||
|
<strong>{run.adventurerSnapshot.stats.shift}</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span>Discipline</span>
|
||||||
|
<strong>{run.adventurerSnapshot.stats.discipline}</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span>Precision</span>
|
||||||
|
<strong>{run.adventurerSnapshot.stats.precision}</strong>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<span>XP</span>
|
||||||
|
<strong>{run.adventurerSnapshot.xp}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<p className="supporting-text">
|
||||||
|
{run.adventurerSnapshot.name} is equipped with a{" "}
|
||||||
|
{sampleContentPack.weapons.find(
|
||||||
|
(weapon) => weapon.id === run.adventurerSnapshot.weaponId,
|
||||||
|
)?.name ?? "weapon"}
|
||||||
|
.
|
||||||
|
</p>
|
||||||
|
<p className="supporting-text">
|
||||||
|
Run rewards: {run.xpGained} XP, {run.goldGained} gold,{" "}
|
||||||
|
{run.defeatedCreatureIds.length} foes defeated.
|
||||||
|
</p>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article className="panel panel-inventory">
|
||||||
|
<div className="panel-header">
|
||||||
|
<h2>Inventory</h2>
|
||||||
|
<span>{run.adventurerSnapshot.inventory.carried.length} carried entries</span>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-summary">
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Gold</span>
|
||||||
|
<strong>{run.adventurerSnapshot.inventory.currency.gold}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Rations</span>
|
||||||
|
<strong>{run.adventurerSnapshot.inventory.rationCount}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Treasure</span>
|
||||||
|
<strong>{carriedTreasure.length}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-badge">
|
||||||
|
<span>Latest Loot</span>
|
||||||
|
<strong>{run.lootedItems.reduce((total, entry) => total + entry.quantity, 0)}</strong>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div className="inventory-grid">
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Equipped</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{equippedItems.map((entry) => (
|
||||||
|
<article key={`equipped-${entry.definitionId}`} className="inventory-card inventory-card-equipped">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Ready for use</span>
|
||||||
|
</article>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Consumables</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedConsumables.length === 0 ? (
|
||||||
|
<p className="supporting-text">No consumables carried.</p>
|
||||||
|
) : (
|
||||||
|
carriedConsumables.map((entry) => (
|
||||||
|
<article key={`consumable-${entry.definitionId}`} className="inventory-card">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Combat or run utility</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Pack Gear</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedGear.length === 0 ? (
|
||||||
|
<p className="supporting-text">No general gear carried.</p>
|
||||||
|
) : (
|
||||||
|
carriedGear.map((entry) => (
|
||||||
|
<article key={`gear-${entry.definitionId}`} className="inventory-card">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Travel and exploration kit</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
<section className="inventory-section">
|
||||||
|
<span className="inventory-label">Treasure Stash</span>
|
||||||
|
<div className="inventory-list">
|
||||||
|
{carriedTreasure.length === 0 ? (
|
||||||
|
<p className="supporting-text">No treasure recovered yet.</p>
|
||||||
|
) : (
|
||||||
|
carriedTreasure.map((entry) => (
|
||||||
|
<article key={`treasure-${entry.definitionId}`} className="inventory-card inventory-card-treasure">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
<span>Sellable dungeon spoils</span>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</section>
|
||||||
|
</div>
|
||||||
|
<div className="loot-ribbon">
|
||||||
|
<span className="inventory-label">Recent Spoils</span>
|
||||||
|
{latestLoot.length === 0 ? (
|
||||||
|
<p className="supporting-text">Win a fight to populate the loot ribbon.</p>
|
||||||
|
) : (
|
||||||
|
<div className="loot-ribbon-list">
|
||||||
|
{latestLoot.map((entry) => (
|
||||||
|
<article key={`recent-${entry.definitionId}`} className="loot-pill">
|
||||||
|
<strong>{formatInventoryEntry(entry.definitionId, entry.quantity)}</strong>
|
||||||
|
</article>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<article className="panel">
|
<article className="panel">
|
||||||
<h2>Immediate Build Targets</h2>
|
<div className="panel-header">
|
||||||
<ol>
|
<h2>Current Room</h2>
|
||||||
{nextTargets.map((target) => (
|
<span>Level {run.currentLevel}</span>
|
||||||
<li key={target}>{target}</li>
|
</div>
|
||||||
))}
|
<h3 className="room-title">
|
||||||
</ol>
|
{getRoomTitle(run, run.currentRoomId)}
|
||||||
|
</h3>
|
||||||
|
<p className="supporting-text">
|
||||||
|
{currentRoom?.notes[1] ?? "No encounter notes available yet."}
|
||||||
|
</p>
|
||||||
|
<div className="room-meta">
|
||||||
|
<span>Entered: {currentRoom?.discovery.entered ? "Yes" : "No"}</span>
|
||||||
|
<span>Cleared: {currentRoom?.discovery.cleared ? "Yes" : "No"}</span>
|
||||||
|
<span>Exits: {currentRoom?.exits.length ?? 0}</span>
|
||||||
|
</div>
|
||||||
|
<div className="button-row">
|
||||||
|
<button className="button" onClick={handleEnterRoom}>
|
||||||
|
Enter Room
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="button button-primary"
|
||||||
|
onClick={handleStartCombat}
|
||||||
|
disabled={!combatReadyEncounter || Boolean(run.activeCombat)}
|
||||||
|
>
|
||||||
|
Start Combat
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="encounter-box">
|
||||||
|
<span className="encounter-label">Encounter</span>
|
||||||
|
<strong>{currentRoom?.encounter?.resultLabel ?? "None"}</strong>
|
||||||
|
</div>
|
||||||
</article>
|
</article>
|
||||||
|
|
||||||
<article className="panel">
|
<article className="panel">
|
||||||
<h2>Sample Content Pack</h2>
|
<div className="panel-header">
|
||||||
<dl className="stats">
|
<h2>Navigation</h2>
|
||||||
<div>
|
<span>{availableMoves.length} exits</span>
|
||||||
<dt>Tables</dt>
|
</div>
|
||||||
<dd>{sampleContentPack.tables.length}</dd>
|
<div className="move-list">
|
||||||
</div>
|
{availableMoves.map((move) => (
|
||||||
<div>
|
<button
|
||||||
<dt>Weapons</dt>
|
key={move.direction}
|
||||||
<dd>{sampleContentPack.weapons.length}</dd>
|
className="move-card"
|
||||||
</div>
|
onClick={() => handleTravel(move.direction)}
|
||||||
<div>
|
disabled={Boolean(run.activeCombat)}
|
||||||
<dt>Manoeuvres</dt>
|
>
|
||||||
<dd>{sampleContentPack.manoeuvres.length}</dd>
|
<span>{move.direction}</span>
|
||||||
</div>
|
<strong>{move.leadsToRoomId ? "Travel" : "Generate"}</strong>
|
||||||
<div>
|
<em>
|
||||||
<dt>Creatures</dt>
|
{move.leadsToRoomId
|
||||||
<dd>{sampleContentPack.creatures.length}</dd>
|
? getRoomTitle(run, move.leadsToRoomId)
|
||||||
</div>
|
: `${move.exitType} exit`}
|
||||||
</dl>
|
</em>
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="mini-map">
|
||||||
|
{currentLevel?.discoveredRoomOrder.map((roomId) => {
|
||||||
|
const room = currentLevel.rooms[roomId];
|
||||||
|
const active = roomId === run.currentRoomId;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<article
|
||||||
|
key={roomId}
|
||||||
|
className={`map-node${active ? " map-node-active" : ""}`}
|
||||||
|
>
|
||||||
|
<span>
|
||||||
|
{room.position.x},{room.position.y}
|
||||||
|
</span>
|
||||||
|
<strong>{getRoomTitle(run, roomId)}</strong>
|
||||||
|
</article>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article className="panel">
|
||||||
|
<div className="panel-header">
|
||||||
|
<h2>Combat</h2>
|
||||||
|
<span>{run.activeCombat ? `Round ${run.activeCombat.round}` : "Inactive"}</span>
|
||||||
|
</div>
|
||||||
|
{run.activeCombat ? (
|
||||||
|
<>
|
||||||
|
<div className="combat-status">
|
||||||
|
<span>Acting Side</span>
|
||||||
|
<strong>{run.activeCombat.actingSide}</strong>
|
||||||
|
</div>
|
||||||
|
<div className="enemy-list">
|
||||||
|
{run.activeCombat.enemies.map((enemy) => (
|
||||||
|
<div key={enemy.id} className="enemy-card">
|
||||||
|
<div>
|
||||||
|
<strong>{enemy.name}</strong>
|
||||||
|
<span>
|
||||||
|
HP {enemy.hpCurrent}/{enemy.hpMax}
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
<div className="enemy-actions">
|
||||||
|
<button
|
||||||
|
className="button"
|
||||||
|
onClick={() =>
|
||||||
|
handlePlayerTurn("manoeuvre.exact-strike", enemy.id)
|
||||||
|
}
|
||||||
|
disabled={
|
||||||
|
run.activeCombat?.actingSide !== "player" ||
|
||||||
|
enemy.hpCurrent <= 0
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Exact Strike
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
className="button"
|
||||||
|
onClick={() => handlePlayerTurn("manoeuvre.guard-break", enemy.id)}
|
||||||
|
disabled={
|
||||||
|
run.activeCombat?.actingSide !== "player" ||
|
||||||
|
enemy.hpCurrent <= 0
|
||||||
|
}
|
||||||
|
>
|
||||||
|
Guard Break
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
<div className="button-row">
|
||||||
|
<button
|
||||||
|
className="button button-primary"
|
||||||
|
onClick={handleEnemyTurn}
|
||||||
|
disabled={run.activeCombat.actingSide !== "enemy"}
|
||||||
|
>
|
||||||
|
Resolve Enemy Turn
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<p className="supporting-text">
|
||||||
|
No active combat. Travel until a room reveals a hostile encounter,
|
||||||
|
then engage it from the banner or room panel.
|
||||||
|
</p>
|
||||||
|
)}
|
||||||
|
</article>
|
||||||
|
|
||||||
|
<article className="panel panel-log">
|
||||||
|
<div className="panel-header">
|
||||||
|
<h2>Run Log</h2>
|
||||||
|
<span>{run.log.length} entries</span>
|
||||||
|
</div>
|
||||||
|
<div className="log-list">
|
||||||
|
{run.log.length === 0 ? (
|
||||||
|
<p className="supporting-text">No events recorded yet.</p>
|
||||||
|
) : (
|
||||||
|
run.log
|
||||||
|
.slice()
|
||||||
|
.reverse()
|
||||||
|
.map((entry) => (
|
||||||
|
<article key={entry.id} className="log-entry">
|
||||||
|
<span>{entry.type}</span>
|
||||||
|
<p>{entry.text}</p>
|
||||||
|
</article>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
</article>
|
</article>
|
||||||
</section>
|
</section>
|
||||||
</main>
|
</main>
|
||||||
|
|||||||
@@ -2,7 +2,12 @@ import { describe, expect, it } from "vitest";
|
|||||||
|
|
||||||
import { lookupTable } from "@/rules/tables";
|
import { lookupTable } from "@/rules/tables";
|
||||||
|
|
||||||
import { findRoomTemplateForLookup, findTableByCode } from "./contentHelpers";
|
import {
|
||||||
|
findCreatureByName,
|
||||||
|
findItemById,
|
||||||
|
findRoomTemplateForLookup,
|
||||||
|
findTableByCode,
|
||||||
|
} from "./contentHelpers";
|
||||||
import { sampleContentPack } from "./sampleContentPack";
|
import { sampleContentPack } from "./sampleContentPack";
|
||||||
|
|
||||||
function createSequenceRoller(values: number[]) {
|
function createSequenceRoller(values: number[]) {
|
||||||
@@ -46,4 +51,18 @@ describe("level 1 content helpers", () => {
|
|||||||
expect(roomTemplate.title).toBe("Slate Shrine");
|
expect(roomTemplate.title).toBe("Slate Shrine");
|
||||||
expect(roomTemplate.roomClass).toBe("large");
|
expect(roomTemplate.roomClass).toBe("large");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("finds a creature definition by display name", () => {
|
||||||
|
const creature = findCreatureByName(sampleContentPack, "Guard");
|
||||||
|
|
||||||
|
expect(creature.id).toBe("creature.level1.guard");
|
||||||
|
expect(creature.hp).toBeGreaterThan(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("finds a loot item definition by id", () => {
|
||||||
|
const item = findItemById(sampleContentPack, "item.silver-clasp");
|
||||||
|
|
||||||
|
expect(item.name).toBe("Silver Clasp");
|
||||||
|
expect(item.itemType).toBe("treasure");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,10 @@
|
|||||||
import type { ContentPack, RoomTemplate, TableDefinition } from "@/types/content";
|
import type {
|
||||||
|
ContentPack,
|
||||||
|
CreatureDefinition,
|
||||||
|
ItemDefinition,
|
||||||
|
RoomTemplate,
|
||||||
|
TableDefinition,
|
||||||
|
} from "@/types/content";
|
||||||
import type { TableLookupResult } from "@/rules/tables";
|
import type { TableLookupResult } from "@/rules/tables";
|
||||||
|
|
||||||
export function findTableByCode(content: ContentPack, code: string): TableDefinition {
|
export function findTableByCode(content: ContentPack, code: string): TableDefinition {
|
||||||
@@ -36,3 +42,46 @@ export function findRoomTemplateForLookup(
|
|||||||
|
|
||||||
return findRoomTemplateById(content, roomReference.id);
|
return findRoomTemplateById(content, roomReference.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function normalizeName(value: string) {
|
||||||
|
return value.trim().toLowerCase().replace(/\s+/g, " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findCreatureByName(
|
||||||
|
content: ContentPack,
|
||||||
|
creatureName: string,
|
||||||
|
): CreatureDefinition {
|
||||||
|
const normalizedTarget = normalizeName(creatureName);
|
||||||
|
const creature = content.creatures.find(
|
||||||
|
(entry) => normalizeName(entry.name) === normalizedTarget,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!creature) {
|
||||||
|
throw new Error(`Unknown creature name: ${creatureName}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return creature;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findCreatureById(
|
||||||
|
content: ContentPack,
|
||||||
|
creatureId: string,
|
||||||
|
): CreatureDefinition {
|
||||||
|
const creature = content.creatures.find((entry) => entry.id === creatureId);
|
||||||
|
|
||||||
|
if (!creature) {
|
||||||
|
throw new Error(`Unknown creature id: ${creatureId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return creature;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function findItemById(content: ContentPack, itemId: string): ItemDefinition {
|
||||||
|
const item = content.items.find((entry) => entry.id === itemId);
|
||||||
|
|
||||||
|
if (!item) {
|
||||||
|
throw new Error(`Unknown item id: ${itemId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
|||||||
@@ -13,6 +13,79 @@ const samplePack = {
|
|||||||
],
|
],
|
||||||
tables: [
|
tables: [
|
||||||
...level1EncounterTables,
|
...level1EncounterTables,
|
||||||
|
{
|
||||||
|
id: "table.level1.humanoid-loot",
|
||||||
|
code: "L1HL",
|
||||||
|
name: "Level 1 Humanoid Loot",
|
||||||
|
category: "loot",
|
||||||
|
level: 1,
|
||||||
|
page: 122,
|
||||||
|
diceKind: "d6",
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
key: "1-2",
|
||||||
|
min: 1,
|
||||||
|
max: 2,
|
||||||
|
label: "Scavenged coins",
|
||||||
|
effects: [{ type: "gain-gold", amount: 1, target: "self" }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "3-4",
|
||||||
|
min: 3,
|
||||||
|
max: 4,
|
||||||
|
label: "Bone Charm",
|
||||||
|
references: [{ type: "item", id: "item.bone-charm" }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "5",
|
||||||
|
exact: 5,
|
||||||
|
label: "Silver Clasp and coins",
|
||||||
|
references: [{ type: "item", id: "item.silver-clasp" }],
|
||||||
|
effects: [{ type: "gain-gold", amount: 2, target: "self" }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "6",
|
||||||
|
exact: 6,
|
||||||
|
label: "Key Ring and coin purse",
|
||||||
|
references: [{ type: "item", id: "item.keeper-keyring" }],
|
||||||
|
effects: [{ type: "gain-gold", amount: 3, target: "self" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
notes: ["Starter loot table for martial and humanoid encounters."],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "table.level1.beast-loot",
|
||||||
|
code: "L1BL",
|
||||||
|
name: "Level 1 Beast Loot",
|
||||||
|
category: "loot",
|
||||||
|
level: 1,
|
||||||
|
page: 122,
|
||||||
|
diceKind: "d6",
|
||||||
|
entries: [
|
||||||
|
{
|
||||||
|
key: "1-4",
|
||||||
|
min: 1,
|
||||||
|
max: 4,
|
||||||
|
label: "Nothing useful",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "5",
|
||||||
|
exact: 5,
|
||||||
|
label: "Trophy Fang",
|
||||||
|
references: [{ type: "item", id: "item.trophy-fang" }],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: "6",
|
||||||
|
exact: 6,
|
||||||
|
label: "Trophy Fang and stray coin",
|
||||||
|
references: [{ type: "item", id: "item.trophy-fang" }],
|
||||||
|
effects: [{ type: "gain-gold", amount: 1, target: "self" }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
notes: ["Starter loot table for basic animal encounters."],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
id: "table.weapon-manoeuvres-1",
|
id: "table.weapon-manoeuvres-1",
|
||||||
code: "WMT1",
|
code: "WMT1",
|
||||||
@@ -123,6 +196,42 @@ const samplePack = {
|
|||||||
consumable: false,
|
consumable: false,
|
||||||
mvp: true,
|
mvp: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "item.bone-charm",
|
||||||
|
name: "Bone Charm",
|
||||||
|
itemType: "treasure",
|
||||||
|
stackable: false,
|
||||||
|
consumable: false,
|
||||||
|
valueGp: 2,
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "item.silver-clasp",
|
||||||
|
name: "Silver Clasp",
|
||||||
|
itemType: "treasure",
|
||||||
|
stackable: false,
|
||||||
|
consumable: false,
|
||||||
|
valueGp: 4,
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "item.keeper-keyring",
|
||||||
|
name: "Keeper Keyring",
|
||||||
|
itemType: "treasure",
|
||||||
|
stackable: false,
|
||||||
|
consumable: false,
|
||||||
|
valueGp: 5,
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "item.trophy-fang",
|
||||||
|
name: "Trophy Fang",
|
||||||
|
itemType: "treasure",
|
||||||
|
stackable: true,
|
||||||
|
consumable: false,
|
||||||
|
valueGp: 1,
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
potions: [
|
potions: [
|
||||||
{
|
{
|
||||||
@@ -163,10 +272,102 @@ const samplePack = {
|
|||||||
numberAppearing: "1-2",
|
numberAppearing: "1-2",
|
||||||
},
|
},
|
||||||
xpReward: 1,
|
xpReward: 1,
|
||||||
|
lootTableCodes: ["L1BL"],
|
||||||
sourcePage: 102,
|
sourcePage: 102,
|
||||||
traits: ["level-1", "sample"],
|
traits: ["level-1", "sample"],
|
||||||
mvp: true,
|
mvp: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
id: "creature.level1.guard",
|
||||||
|
name: "Guard",
|
||||||
|
level: 1,
|
||||||
|
category: "humanoid",
|
||||||
|
hp: 4,
|
||||||
|
attackProfile: {
|
||||||
|
discipline: 1,
|
||||||
|
precision: 1,
|
||||||
|
damage: 1,
|
||||||
|
},
|
||||||
|
defenceProfile: {
|
||||||
|
armour: 1,
|
||||||
|
},
|
||||||
|
xpReward: 2,
|
||||||
|
lootTableCodes: ["L1HL"],
|
||||||
|
sourcePage: 102,
|
||||||
|
traits: ["level-1", "martial"],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "creature.level1.warrior",
|
||||||
|
name: "Warrior",
|
||||||
|
level: 1,
|
||||||
|
category: "humanoid",
|
||||||
|
hp: 5,
|
||||||
|
attackProfile: {
|
||||||
|
discipline: 2,
|
||||||
|
precision: 1,
|
||||||
|
damage: 2,
|
||||||
|
},
|
||||||
|
defenceProfile: {
|
||||||
|
armour: 1,
|
||||||
|
},
|
||||||
|
xpReward: 3,
|
||||||
|
lootTableCodes: ["L1HL"],
|
||||||
|
sourcePage: 102,
|
||||||
|
traits: ["level-1", "martial"],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "creature.level1.thug",
|
||||||
|
name: "Thug",
|
||||||
|
level: 1,
|
||||||
|
category: "humanoid",
|
||||||
|
hp: 3,
|
||||||
|
attackProfile: {
|
||||||
|
discipline: 0,
|
||||||
|
precision: 1,
|
||||||
|
damage: 1,
|
||||||
|
},
|
||||||
|
xpReward: 1,
|
||||||
|
lootTableCodes: ["L1HL"],
|
||||||
|
sourcePage: 102,
|
||||||
|
traits: ["level-1", "martial"],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "creature.level1.work-dog",
|
||||||
|
name: "Work Dog",
|
||||||
|
level: 1,
|
||||||
|
category: "beast",
|
||||||
|
hp: 3,
|
||||||
|
attackProfile: {
|
||||||
|
discipline: 0,
|
||||||
|
precision: 1,
|
||||||
|
damage: 1,
|
||||||
|
},
|
||||||
|
xpReward: 1,
|
||||||
|
lootTableCodes: ["L1BL"],
|
||||||
|
sourcePage: 102,
|
||||||
|
traits: ["level-1", "beast"],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: "creature.level1.guard-dog",
|
||||||
|
name: "Guard Dog",
|
||||||
|
level: 1,
|
||||||
|
category: "beast",
|
||||||
|
hp: 4,
|
||||||
|
attackProfile: {
|
||||||
|
discipline: 1,
|
||||||
|
precision: 1,
|
||||||
|
damage: 1,
|
||||||
|
},
|
||||||
|
xpReward: 2,
|
||||||
|
lootTableCodes: ["L1BL"],
|
||||||
|
sourcePage: 102,
|
||||||
|
traits: ["level-1", "beast"],
|
||||||
|
mvp: true,
|
||||||
|
},
|
||||||
],
|
],
|
||||||
roomTemplates: [
|
roomTemplates: [
|
||||||
{
|
{
|
||||||
|
|||||||
111
src/rules/combat.test.ts
Normal file
111
src/rules/combat.test.ts
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import { startCombatFromRoom } from "./combat";
|
||||||
|
import { createRoomStateFromTemplate } from "./rooms";
|
||||||
|
|
||||||
|
describe("combat bootstrap", () => {
|
||||||
|
it("creates a combat state from a resolved guard encounter", () => {
|
||||||
|
const adventurer = createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Aster",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.guard-room",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.guard-room",
|
||||||
|
);
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a", "b"],
|
||||||
|
creatureNames: ["Guard", "Warrior"],
|
||||||
|
resultLabel: "Guard and Warrior",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = startCombatFromRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
adventurer,
|
||||||
|
room,
|
||||||
|
at: "2026-03-15T13:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.combat.id).toBe("room.level1.guard-room.combat");
|
||||||
|
expect(result.combat.actingSide).toBe("player");
|
||||||
|
expect(result.combat.player.name).toBe("Aster");
|
||||||
|
expect(result.combat.enemies.map((enemy) => enemy.name)).toEqual(["Guard", "Warrior"]);
|
||||||
|
expect(result.combat.enemies[0]?.armourValue).toBe(1);
|
||||||
|
expect(result.room.encounter?.rewardPending).toBe(true);
|
||||||
|
expect(result.logEntries[0]?.text).toContain("Guard and Warrior");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("supports single-creature combat from a crate encounter", () => {
|
||||||
|
const adventurer = createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Bryn",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.storage-area",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.storage-area",
|
||||||
|
);
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1CE",
|
||||||
|
creatureIds: ["a"],
|
||||||
|
creatureNames: ["Giant Rat"],
|
||||||
|
resultLabel: "Giant Rat Pair",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = startCombatFromRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
adventurer,
|
||||||
|
room,
|
||||||
|
at: "2026-03-15T13:05:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.combat.enemies).toHaveLength(1);
|
||||||
|
expect(result.combat.enemies[0]?.sourceDefinitionId).toBe("creature.level1.giant-rat");
|
||||||
|
expect(result.combat.player.hpCurrent).toBe(10);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects rooms without combat-ready encounter names", () => {
|
||||||
|
const adventurer = createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Cyra",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.small.empty",
|
||||||
|
1,
|
||||||
|
"room.level1.small.empty-space",
|
||||||
|
);
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
creatureIds: [],
|
||||||
|
creatureNames: [],
|
||||||
|
resultLabel: "No encounter",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
startCombatFromRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
adventurer,
|
||||||
|
room,
|
||||||
|
}),
|
||||||
|
).toThrow("combat-ready encounter");
|
||||||
|
});
|
||||||
|
});
|
||||||
124
src/rules/combat.ts
Normal file
124
src/rules/combat.ts
Normal file
@@ -0,0 +1,124 @@
|
|||||||
|
import { findCreatureByName } from "@/data/contentHelpers";
|
||||||
|
import type { ContentPack, CreatureDefinition } from "@/types/content";
|
||||||
|
import type { AdventurerState, CombatState, CombatantState, RoomState } from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
export type StartCombatOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
adventurer: AdventurerState;
|
||||||
|
room: RoomState;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type StartCombatResult = {
|
||||||
|
combat: CombatState;
|
||||||
|
room: RoomState;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
};
|
||||||
|
|
||||||
|
function createLogEntry(
|
||||||
|
id: string,
|
||||||
|
at: string,
|
||||||
|
type: LogEntry["type"],
|
||||||
|
text: string,
|
||||||
|
relatedIds: string[],
|
||||||
|
): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type,
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createPlayerCombatant(adventurer: AdventurerState): CombatantState {
|
||||||
|
return {
|
||||||
|
id: adventurer.id,
|
||||||
|
name: adventurer.name,
|
||||||
|
hpCurrent: adventurer.hp.current,
|
||||||
|
hpMax: adventurer.hp.max,
|
||||||
|
shift: adventurer.stats.shift,
|
||||||
|
discipline: adventurer.stats.discipline,
|
||||||
|
precision: adventurer.stats.precision,
|
||||||
|
statuses: [...adventurer.statuses],
|
||||||
|
traits: ["player"],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createEnemyCombatant(
|
||||||
|
room: RoomState,
|
||||||
|
creature: CreatureDefinition,
|
||||||
|
index: number,
|
||||||
|
): CombatantState {
|
||||||
|
return {
|
||||||
|
id: `${room.id}.enemy.${index + 1}`,
|
||||||
|
name: creature.name,
|
||||||
|
sourceDefinitionId: creature.id,
|
||||||
|
hpCurrent: creature.hp,
|
||||||
|
hpMax: creature.hp,
|
||||||
|
shift: 0,
|
||||||
|
discipline: creature.attackProfile.discipline,
|
||||||
|
precision: creature.attackProfile.precision,
|
||||||
|
armourValue: creature.defenceProfile?.armour,
|
||||||
|
statuses: [],
|
||||||
|
traits: creature.traits ?? [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireEncounterNames(room: RoomState) {
|
||||||
|
const creatureNames = room.encounter?.creatureNames?.filter(Boolean);
|
||||||
|
|
||||||
|
if (!room.encounter?.resolved || !creatureNames || creatureNames.length === 0) {
|
||||||
|
throw new Error(`Room ${room.id} does not have a combat-ready encounter.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return creatureNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startCombatFromRoom(
|
||||||
|
options: StartCombatOptions,
|
||||||
|
): StartCombatResult {
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
const creatureNames = requireEncounterNames(options.room);
|
||||||
|
const enemies = creatureNames.map((creatureName, index) =>
|
||||||
|
createEnemyCombatant(
|
||||||
|
options.room,
|
||||||
|
findCreatureByName(options.content, creatureName),
|
||||||
|
index,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
const combat: CombatState = {
|
||||||
|
id: `${options.room.id}.combat`,
|
||||||
|
round: 1,
|
||||||
|
actingSide: "player",
|
||||||
|
player: createPlayerCombatant(options.adventurer),
|
||||||
|
enemies,
|
||||||
|
combatLog: [
|
||||||
|
createLogEntry(
|
||||||
|
`${options.room.id}.combat.start`,
|
||||||
|
at,
|
||||||
|
"combat",
|
||||||
|
`Combat begins in ${options.room.id} against ${creatureNames.join(" and ")}.`,
|
||||||
|
[options.room.id, ...enemies.map((enemy) => enemy.id)],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
|
||||||
|
const room: RoomState = {
|
||||||
|
...options.room,
|
||||||
|
encounter: options.room.encounter
|
||||||
|
? {
|
||||||
|
...options.room.encounter,
|
||||||
|
rewardPending: true,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
combat,
|
||||||
|
room,
|
||||||
|
logEntries: [...combat.combatLog],
|
||||||
|
};
|
||||||
|
}
|
||||||
132
src/rules/combatTurns.test.ts
Normal file
132
src/rules/combatTurns.test.ts
Normal file
@@ -0,0 +1,132 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import { startCombatFromRoom } from "./combat";
|
||||||
|
import { resolveEnemyTurn, resolvePlayerAttack } from "./combatTurns";
|
||||||
|
import { createRoomStateFromTemplate } from "./rooms";
|
||||||
|
|
||||||
|
function createSequenceRoller(values: number[]) {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const next = values[index];
|
||||||
|
index += 1;
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createGuardCombat() {
|
||||||
|
const adventurer = createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Aster",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.guard-room",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.guard-room",
|
||||||
|
);
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a", "b"],
|
||||||
|
creatureNames: ["Guard", "Warrior"],
|
||||||
|
resultLabel: "Guard and Warrior",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
adventurer,
|
||||||
|
...startCombatFromRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
adventurer,
|
||||||
|
room,
|
||||||
|
at: "2026-03-15T13:00:00.000Z",
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("combat turns", () => {
|
||||||
|
it("lets the player hit an enemy and pass initiative", () => {
|
||||||
|
const { adventurer, combat } = createGuardCombat();
|
||||||
|
const targetId = combat.enemies[0]!.id;
|
||||||
|
|
||||||
|
const result = resolvePlayerAttack({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat,
|
||||||
|
adventurer,
|
||||||
|
manoeuvreId: "manoeuvre.exact-strike",
|
||||||
|
targetEnemyId: targetId,
|
||||||
|
roller: createSequenceRoller([6, 6]),
|
||||||
|
at: "2026-03-15T13:01:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.combat.actingSide).toBe("enemy");
|
||||||
|
expect(result.combat.selectedManoeuvreId).toBe("manoeuvre.exact-strike");
|
||||||
|
expect(result.combat.enemies[0]?.hpCurrent).toBeLessThan(result.combat.enemies[0]!.hpMax);
|
||||||
|
expect(result.logEntries[0]?.text).toContain("deals");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("marks an enemy defeated when the player deals lethal damage", () => {
|
||||||
|
const { adventurer, combat } = createGuardCombat();
|
||||||
|
combat.enemies[0]!.hpCurrent = 1;
|
||||||
|
|
||||||
|
const result = resolvePlayerAttack({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat,
|
||||||
|
adventurer,
|
||||||
|
manoeuvreId: "manoeuvre.guard-break",
|
||||||
|
targetEnemyId: combat.enemies[0]!.id,
|
||||||
|
roller: createSequenceRoller([6, 5]),
|
||||||
|
at: "2026-03-15T13:02:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.defeatedEnemyIds).toEqual([combat.enemies[0]!.id]);
|
||||||
|
expect(result.logEntries.map((entry) => entry.text)).toContain("Guard is defeated.");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lets an enemy attack the player and advances the round", () => {
|
||||||
|
const { adventurer, combat } = createGuardCombat();
|
||||||
|
const afterPlayer = resolvePlayerAttack({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat,
|
||||||
|
adventurer,
|
||||||
|
manoeuvreId: "manoeuvre.exact-strike",
|
||||||
|
targetEnemyId: combat.enemies[0]!.id,
|
||||||
|
roller: createSequenceRoller([4, 4]),
|
||||||
|
at: "2026-03-15T13:03:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = resolveEnemyTurn({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat: afterPlayer.combat,
|
||||||
|
adventurer,
|
||||||
|
roller: createSequenceRoller([6, 6]),
|
||||||
|
at: "2026-03-15T13:04:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.combat.round).toBe(2);
|
||||||
|
expect(result.combat.actingSide).toBe("player");
|
||||||
|
expect(result.combat.player.hpCurrent).toBeLessThan(result.combat.player.hpMax);
|
||||||
|
expect(result.logEntries[0]?.text).toContain("attacks");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("rejects player turns that target a defeated enemy", () => {
|
||||||
|
const { adventurer, combat } = createGuardCombat();
|
||||||
|
combat.enemies[0]!.hpCurrent = 0;
|
||||||
|
|
||||||
|
expect(() =>
|
||||||
|
resolvePlayerAttack({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat,
|
||||||
|
adventurer,
|
||||||
|
manoeuvreId: "manoeuvre.exact-strike",
|
||||||
|
targetEnemyId: combat.enemies[0]!.id,
|
||||||
|
}),
|
||||||
|
).toThrow("Unknown or defeated target");
|
||||||
|
});
|
||||||
|
});
|
||||||
255
src/rules/combatTurns.ts
Normal file
255
src/rules/combatTurns.ts
Normal file
@@ -0,0 +1,255 @@
|
|||||||
|
import type {
|
||||||
|
ArmourDefinition,
|
||||||
|
ContentPack,
|
||||||
|
ManoeuvreDefinition,
|
||||||
|
WeaponDefinition,
|
||||||
|
} from "@/types/content";
|
||||||
|
import type { AdventurerState, CombatState, CombatantState } from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
import { roll2D6, type DiceRoller } from "./dice";
|
||||||
|
|
||||||
|
export type ResolvePlayerAttackOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
combat: CombatState;
|
||||||
|
adventurer: AdventurerState;
|
||||||
|
manoeuvreId: string;
|
||||||
|
targetEnemyId: string;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolveEnemyTurnOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
combat: CombatState;
|
||||||
|
adventurer: AdventurerState;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type CombatTurnResult = {
|
||||||
|
combat: CombatState;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
defeatedEnemyIds: string[];
|
||||||
|
combatEnded: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
const BASE_TARGET_NUMBER = 7;
|
||||||
|
|
||||||
|
function requireWeapon(content: ContentPack, weaponId: string): WeaponDefinition {
|
||||||
|
const weapon = content.weapons.find((entry) => entry.id === weaponId);
|
||||||
|
|
||||||
|
if (!weapon) {
|
||||||
|
throw new Error(`Unknown weapon id: ${weaponId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return weapon;
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireManoeuvre(content: ContentPack, manoeuvreId: string): ManoeuvreDefinition {
|
||||||
|
const manoeuvre = content.manoeuvres.find((entry) => entry.id === manoeuvreId);
|
||||||
|
|
||||||
|
if (!manoeuvre) {
|
||||||
|
throw new Error(`Unknown manoeuvre id: ${manoeuvreId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return manoeuvre;
|
||||||
|
}
|
||||||
|
|
||||||
|
function findArmour(content: ContentPack, armourId?: string): ArmourDefinition | undefined {
|
||||||
|
if (!armourId) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
return content.armour.find((entry) => entry.id === armourId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneCombatant(combatant: CombatantState): CombatantState {
|
||||||
|
return {
|
||||||
|
...combatant,
|
||||||
|
statuses: [...combatant.statuses],
|
||||||
|
traits: [...combatant.traits],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneCombat(combat: CombatState): CombatState {
|
||||||
|
return {
|
||||||
|
...combat,
|
||||||
|
player: cloneCombatant(combat.player),
|
||||||
|
enemies: combat.enemies.map(cloneCombatant),
|
||||||
|
combatLog: combat.combatLog.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
relatedIds: entry.relatedIds ? [...entry.relatedIds] : undefined,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createLogEntry(
|
||||||
|
id: string,
|
||||||
|
at: string,
|
||||||
|
text: string,
|
||||||
|
relatedIds: string[],
|
||||||
|
): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type: "combat",
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getPlayerArmourValue(content: ContentPack, adventurer: AdventurerState) {
|
||||||
|
return findArmour(content, adventurer.armourId)?.armourValue ?? 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isAlive(combatant: CombatantState) {
|
||||||
|
return combatant.hpCurrent > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLivingEnemies(combat: CombatState) {
|
||||||
|
return combat.enemies.filter(isAlive);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolvePlayerAttack(
|
||||||
|
options: ResolvePlayerAttackOptions,
|
||||||
|
): CombatTurnResult {
|
||||||
|
if (options.combat.actingSide !== "player") {
|
||||||
|
throw new Error("It is not currently the player's turn.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const combat = cloneCombat(options.combat);
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
const weapon = requireWeapon(options.content, options.adventurer.weaponId);
|
||||||
|
const manoeuvre = requireManoeuvre(options.content, options.manoeuvreId);
|
||||||
|
|
||||||
|
if (!options.adventurer.manoeuvreIds.includes(manoeuvre.id)) {
|
||||||
|
throw new Error(`Adventurer cannot use manoeuvre ${manoeuvre.id}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!manoeuvre.weaponCategories.includes(weapon.category)) {
|
||||||
|
throw new Error(`Manoeuvre ${manoeuvre.id} is not compatible with ${weapon.id}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const target = combat.enemies.find((enemy) => enemy.id === options.targetEnemyId);
|
||||||
|
|
||||||
|
if (!target || !isAlive(target)) {
|
||||||
|
throw new Error(`Unknown or defeated target enemy: ${options.targetEnemyId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const roll = roll2D6(options.roller);
|
||||||
|
const accuracy =
|
||||||
|
(roll.total ?? 0) +
|
||||||
|
combat.player.precision +
|
||||||
|
(manoeuvre.precisionModifier ?? 0);
|
||||||
|
const targetNumber = BASE_TARGET_NUMBER + (target.armourValue ?? 0);
|
||||||
|
const hit = accuracy >= targetNumber;
|
||||||
|
const rawDamage = hit
|
||||||
|
? weapon.baseDamage +
|
||||||
|
Math.max(0, combat.player.discipline + (manoeuvre.damageModifier ?? 0))
|
||||||
|
: 0;
|
||||||
|
const damage = hit ? Math.max(1, rawDamage) : 0;
|
||||||
|
|
||||||
|
if (hit) {
|
||||||
|
target.hpCurrent = Math.max(0, target.hpCurrent - damage);
|
||||||
|
}
|
||||||
|
|
||||||
|
combat.selectedManoeuvreId = manoeuvre.id;
|
||||||
|
combat.lastRoll = roll;
|
||||||
|
combat.actingSide = getLivingEnemies(combat).length === 0 ? "player" : "enemy";
|
||||||
|
|
||||||
|
const logEntries: LogEntry[] = [
|
||||||
|
createLogEntry(
|
||||||
|
`${combat.id}.player.${combat.combatLog.length + 1}`,
|
||||||
|
at,
|
||||||
|
hit
|
||||||
|
? `${combat.player.name} uses ${manoeuvre.name} on ${target.name}, rolls ${roll.total}, and deals ${damage} damage.`
|
||||||
|
: `${combat.player.name} uses ${manoeuvre.name} on ${target.name}, rolls ${roll.total}, and misses.`,
|
||||||
|
[combat.player.id, target.id],
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
const defeatedEnemyIds = !isAlive(target) ? [target.id] : [];
|
||||||
|
|
||||||
|
if (defeatedEnemyIds.length > 0) {
|
||||||
|
logEntries.push(
|
||||||
|
createLogEntry(
|
||||||
|
`${combat.id}.player.${combat.combatLog.length + 2}`,
|
||||||
|
at,
|
||||||
|
`${target.name} is defeated.`,
|
||||||
|
[target.id],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
combat.combatLog.push(...logEntries);
|
||||||
|
|
||||||
|
return {
|
||||||
|
combat,
|
||||||
|
logEntries,
|
||||||
|
defeatedEnemyIds,
|
||||||
|
combatEnded: getLivingEnemies(combat).length === 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveEnemyTurn(
|
||||||
|
options: ResolveEnemyTurnOptions,
|
||||||
|
): CombatTurnResult {
|
||||||
|
if (options.combat.actingSide !== "enemy") {
|
||||||
|
throw new Error("It is not currently the enemy's turn.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const combat = cloneCombat(options.combat);
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
const attacker = getLivingEnemies(combat)[0];
|
||||||
|
|
||||||
|
if (!attacker) {
|
||||||
|
throw new Error("No living enemies are available to act.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const roll = roll2D6(options.roller);
|
||||||
|
const armourValue = getPlayerArmourValue(options.content, options.adventurer);
|
||||||
|
const accuracy = (roll.total ?? 0) + attacker.precision;
|
||||||
|
const targetNumber = BASE_TARGET_NUMBER + armourValue;
|
||||||
|
const hit = accuracy >= targetNumber;
|
||||||
|
const rawDamage = hit ? Math.max(1, 1 + attacker.discipline) : 0;
|
||||||
|
|
||||||
|
if (hit) {
|
||||||
|
combat.player.hpCurrent = Math.max(0, combat.player.hpCurrent - rawDamage);
|
||||||
|
}
|
||||||
|
|
||||||
|
combat.lastRoll = roll;
|
||||||
|
combat.actingSide = combat.player.hpCurrent > 0 ? "player" : "enemy";
|
||||||
|
combat.round += 1;
|
||||||
|
|
||||||
|
const logEntries: LogEntry[] = [
|
||||||
|
createLogEntry(
|
||||||
|
`${combat.id}.enemy.${combat.combatLog.length + 1}`,
|
||||||
|
at,
|
||||||
|
hit
|
||||||
|
? `${attacker.name} attacks ${combat.player.name}, rolls ${roll.total}, and deals ${rawDamage} damage.`
|
||||||
|
: `${attacker.name} attacks ${combat.player.name}, rolls ${roll.total}, and misses.`,
|
||||||
|
[attacker.id, combat.player.id],
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (combat.player.hpCurrent === 0) {
|
||||||
|
logEntries.push(
|
||||||
|
createLogEntry(
|
||||||
|
`${combat.id}.enemy.${combat.combatLog.length + 2}`,
|
||||||
|
at,
|
||||||
|
`${combat.player.name} is defeated.`,
|
||||||
|
[combat.player.id],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
combat.combatLog.push(...logEntries);
|
||||||
|
|
||||||
|
return {
|
||||||
|
combat,
|
||||||
|
logEntries,
|
||||||
|
defeatedEnemyIds: [],
|
||||||
|
combatEnded: combat.player.hpCurrent === 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -49,7 +49,15 @@ function cloneRoom(room: RoomState): RoomState {
|
|||||||
dimensions: { ...room.dimensions },
|
dimensions: { ...room.dimensions },
|
||||||
exits: room.exits.map((exit) => ({ ...exit })),
|
exits: room.exits.map((exit) => ({ ...exit })),
|
||||||
discovery: { ...room.discovery },
|
discovery: { ...room.discovery },
|
||||||
encounter: room.encounter ? { ...room.encounter, creatureIds: [...room.encounter.creatureIds] } : undefined,
|
encounter: room.encounter
|
||||||
|
? {
|
||||||
|
...room.encounter,
|
||||||
|
creatureIds: [...room.encounter.creatureIds],
|
||||||
|
creatureNames: room.encounter.creatureNames
|
||||||
|
? [...room.encounter.creatureNames]
|
||||||
|
: undefined,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
objects: room.objects.map((object) => ({
|
objects: room.objects.map((object) => ({
|
||||||
...object,
|
...object,
|
||||||
effects: object.effects ? [...object.effects] : undefined,
|
effects: object.effects ? [...object.effects] : undefined,
|
||||||
|
|||||||
73
src/rules/encounters.test.ts
Normal file
73
src/rules/encounters.test.ts
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createRoomStateFromTemplate } from "./rooms";
|
||||||
|
import { resolveRoomEncounter } from "./encounters";
|
||||||
|
|
||||||
|
function createSequenceRoller(values: number[]) {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const next = values[index];
|
||||||
|
index += 1;
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("encounter resolution", () => {
|
||||||
|
it("marks explicitly empty rooms as having no encounter", () => {
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.small.empty",
|
||||||
|
1,
|
||||||
|
"room.level1.small.empty-space",
|
||||||
|
);
|
||||||
|
room.notes.push("Nothing here.");
|
||||||
|
|
||||||
|
const result = resolveRoomEncounter(sampleContentPack, room);
|
||||||
|
|
||||||
|
expect(result.encounter?.creatureIds).toEqual([]);
|
||||||
|
expect(result.encounter?.resultLabel).toBe("No encounter");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("uses room context to resolve a guard encounter table", () => {
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.guard-room",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.guard-room",
|
||||||
|
);
|
||||||
|
room.notes.push("Use the guards encounter table if occupied.");
|
||||||
|
|
||||||
|
const result = resolveRoomEncounter(
|
||||||
|
sampleContentPack,
|
||||||
|
room,
|
||||||
|
createSequenceRoller([6]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.lookup?.entry.label).toBe("Guard and Warrior");
|
||||||
|
expect(result.encounter?.sourceTableCode).toBe("L1G");
|
||||||
|
expect(result.encounter?.creatureNames).toEqual(["Guard", "Warrior"]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves crate events from room context when the room hints at crate rolls", () => {
|
||||||
|
const room = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.storage-area",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.storage-area",
|
||||||
|
);
|
||||||
|
room.notes.push("Roll for food, drink, or crate events while searching.");
|
||||||
|
|
||||||
|
const result = resolveRoomEncounter(
|
||||||
|
sampleContentPack,
|
||||||
|
room,
|
||||||
|
createSequenceRoller([2]),
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.lookup?.entry.label).toBe("Giant Rat Pair");
|
||||||
|
expect(result.encounter?.sourceTableCode).toBe("L1CE");
|
||||||
|
expect(result.encounter?.creatureNames).toEqual(["Giant Rat Pair"]);
|
||||||
|
});
|
||||||
|
});
|
||||||
110
src/rules/encounters.ts
Normal file
110
src/rules/encounters.ts
Normal file
@@ -0,0 +1,110 @@
|
|||||||
|
import { findTableByCode } from "@/data/contentHelpers";
|
||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type { EncounterState, RoomState } from "@/types/state";
|
||||||
|
|
||||||
|
import { lookupTable, type TableLookupResult } from "./tables";
|
||||||
|
import type { DiceRoller } from "./dice";
|
||||||
|
|
||||||
|
export type EncounterResolutionResult = {
|
||||||
|
room: RoomState;
|
||||||
|
encounter?: EncounterState;
|
||||||
|
lookup?: TableLookupResult;
|
||||||
|
};
|
||||||
|
|
||||||
|
const ROOM_HINT_TO_TABLE: Array<{ pattern: RegExp; tableCode: string }> = [
|
||||||
|
{ pattern: /\bdogs?\b/i, tableCode: "L1D" },
|
||||||
|
{ pattern: /\bguards?\b/i, tableCode: "L1G" },
|
||||||
|
{ pattern: /\bmartial\b/i, tableCode: "L1M" },
|
||||||
|
{ pattern: /\bworkers?\b/i, tableCode: "L1W" },
|
||||||
|
{ pattern: /\blabourers?\b/i, tableCode: "L1P" },
|
||||||
|
{ pattern: /\bpreacher\b/i, tableCode: "L1P" },
|
||||||
|
{ pattern: /\banimals?\b/i, tableCode: "L1A" },
|
||||||
|
{ pattern: /\bsnakes?\b/i, tableCode: "L1A" },
|
||||||
|
{ pattern: /\bfungal\b/i, tableCode: "L1F" },
|
||||||
|
{ pattern: /\bslimy\b/i, tableCode: "L1F" },
|
||||||
|
{ pattern: /\bcrate\b/i, tableCode: "L1CE" },
|
||||||
|
];
|
||||||
|
|
||||||
|
function splitEncounterLabel(label: string) {
|
||||||
|
return label
|
||||||
|
.split(/\sand\s/i)
|
||||||
|
.map((part) => part.trim())
|
||||||
|
.filter(Boolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
function inferEncounterTableCode(room: RoomState) {
|
||||||
|
const noteText = room.notes.join(" ");
|
||||||
|
const flagText = room.flags.join(" ");
|
||||||
|
const combinedText = `${noteText} ${flagText}`;
|
||||||
|
|
||||||
|
for (const mapping of ROOM_HINT_TO_TABLE) {
|
||||||
|
if (mapping.pattern.test(combinedText)) {
|
||||||
|
return mapping.tableCode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isNonEncounterRoom(room: RoomState) {
|
||||||
|
const noteText = room.notes.join(" ").toLowerCase();
|
||||||
|
return noteText.includes("nothing here") || noteText.includes("no encounter");
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveRoomEncounter(
|
||||||
|
content: ContentPack,
|
||||||
|
room: RoomState,
|
||||||
|
roller?: DiceRoller,
|
||||||
|
): EncounterResolutionResult {
|
||||||
|
if (room.encounter?.resolved) {
|
||||||
|
return {
|
||||||
|
room,
|
||||||
|
encounter: room.encounter,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isNonEncounterRoom(room)) {
|
||||||
|
const emptyEncounter: EncounterState = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
creatureIds: [],
|
||||||
|
creatureNames: [],
|
||||||
|
resultLabel: "No encounter",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
room: {
|
||||||
|
...room,
|
||||||
|
encounter: emptyEncounter,
|
||||||
|
},
|
||||||
|
encounter: emptyEncounter,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const tableCode = inferEncounterTableCode(room);
|
||||||
|
|
||||||
|
if (!tableCode) {
|
||||||
|
return { room };
|
||||||
|
}
|
||||||
|
|
||||||
|
const table = findTableByCode(content, tableCode);
|
||||||
|
const lookup = lookupTable(table, { roller });
|
||||||
|
const creatureNames = splitEncounterLabel(lookup.entry.label);
|
||||||
|
const encounter: EncounterState = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: tableCode,
|
||||||
|
creatureIds: creatureNames.map((_, index) => `${room.id}.creature.${index + 1}`),
|
||||||
|
creatureNames,
|
||||||
|
resultLabel: lookup.entry.label,
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
room: {
|
||||||
|
...room,
|
||||||
|
encounter,
|
||||||
|
},
|
||||||
|
encounter,
|
||||||
|
lookup,
|
||||||
|
};
|
||||||
|
}
|
||||||
91
src/rules/loot.test.ts
Normal file
91
src/rules/loot.test.ts
Normal file
@@ -0,0 +1,91 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import { startCombatFromRoom } from "./combat";
|
||||||
|
import { resolveCombatLoot } from "./loot";
|
||||||
|
|
||||||
|
function createSequenceRoller(values: number[]) {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const next = values[index];
|
||||||
|
index += 1;
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createAdventurer() {
|
||||||
|
return createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Aster",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("loot resolution", () => {
|
||||||
|
it("awards loot from defeated creatures into carried inventory", () => {
|
||||||
|
const room = {
|
||||||
|
id: "room.level1.start",
|
||||||
|
level: 1,
|
||||||
|
position: { x: 0, y: 0 },
|
||||||
|
dimensions: { width: 4, height: 4 },
|
||||||
|
roomClass: "start" as const,
|
||||||
|
exits: [],
|
||||||
|
discovery: {
|
||||||
|
generated: true,
|
||||||
|
entered: true,
|
||||||
|
cleared: false,
|
||||||
|
searched: false,
|
||||||
|
},
|
||||||
|
encounter: {
|
||||||
|
id: "room.level1.start.encounter",
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["room.level1.start.creature.1"],
|
||||||
|
creatureNames: ["Guard"],
|
||||||
|
resultLabel: "Guard",
|
||||||
|
resolved: true,
|
||||||
|
},
|
||||||
|
objects: [],
|
||||||
|
notes: ["Entry Chamber"],
|
||||||
|
flags: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
const started = startCombatFromRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
room,
|
||||||
|
at: "2026-03-15T14:02:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
started.combat.enemies[0]!.hpCurrent = 0;
|
||||||
|
|
||||||
|
const loot = resolveCombatLoot({
|
||||||
|
content: sampleContentPack,
|
||||||
|
combat: started.combat,
|
||||||
|
inventory: createAdventurer().inventory,
|
||||||
|
roller: createSequenceRoller([5]),
|
||||||
|
at: "2026-03-15T14:03:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(loot.goldAwarded).toBe(2);
|
||||||
|
expect(loot.itemsAwarded).toEqual([
|
||||||
|
{
|
||||||
|
definitionId: "item.silver-clasp",
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(loot.inventory.currency.gold).toBe(2);
|
||||||
|
expect(loot.inventory.carried).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
definitionId: "item.silver-clasp",
|
||||||
|
quantity: 1,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(loot.logEntries.some((entry) => entry.type === "loot")).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
172
src/rules/loot.ts
Normal file
172
src/rules/loot.ts
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
import { findCreatureById, findItemById, findTableByCode } from "@/data/contentHelpers";
|
||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type { CombatState, InventoryEntry, InventoryState } from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
import type { DiceRoller } from "./dice";
|
||||||
|
import { lookupTable } from "./tables";
|
||||||
|
|
||||||
|
export type ResolveCombatLootOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
combat: CombatState;
|
||||||
|
inventory: InventoryState;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolveCombatLootResult = {
|
||||||
|
inventory: InventoryState;
|
||||||
|
itemsAwarded: InventoryEntry[];
|
||||||
|
goldAwarded: number;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
};
|
||||||
|
|
||||||
|
function cloneInventory(inventory: InventoryState): InventoryState {
|
||||||
|
return {
|
||||||
|
...inventory,
|
||||||
|
carried: inventory.carried.map((entry) => ({ ...entry })),
|
||||||
|
equipped: inventory.equipped.map((entry) => ({ ...entry })),
|
||||||
|
stored: inventory.stored.map((entry) => ({ ...entry })),
|
||||||
|
currency: { ...inventory.currency },
|
||||||
|
lightSources: inventory.lightSources.map((entry) => ({ ...entry })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createInventoryEntry(definitionId: string, quantity = 1): InventoryEntry {
|
||||||
|
return {
|
||||||
|
definitionId,
|
||||||
|
quantity,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function mergeInventoryEntry(
|
||||||
|
content: ContentPack,
|
||||||
|
entries: InventoryEntry[],
|
||||||
|
definitionId: string,
|
||||||
|
quantity: number,
|
||||||
|
) {
|
||||||
|
const item = findItemById(content, definitionId);
|
||||||
|
|
||||||
|
if (item.stackable) {
|
||||||
|
const existing = entries.find((entry) => entry.definitionId === definitionId);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
existing.quantity += quantity;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
entries.push(createInventoryEntry(definitionId, quantity));
|
||||||
|
}
|
||||||
|
|
||||||
|
function createLootLog(
|
||||||
|
id: string,
|
||||||
|
at: string,
|
||||||
|
text: string,
|
||||||
|
relatedIds: string[],
|
||||||
|
): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type: "loot",
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function summarizeLoot(
|
||||||
|
goldAwarded: number,
|
||||||
|
itemsAwarded: InventoryEntry[],
|
||||||
|
content: ContentPack,
|
||||||
|
) {
|
||||||
|
const parts: string[] = [];
|
||||||
|
|
||||||
|
if (goldAwarded > 0) {
|
||||||
|
parts.push(`${goldAwarded} gold`);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const item of itemsAwarded) {
|
||||||
|
const itemName = findItemById(content, item.definitionId).name;
|
||||||
|
parts.push(item.quantity > 1 ? `${item.quantity}x ${itemName}` : itemName);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts.length > 0 ? parts.join(", ") : "nothing useful";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveCombatLoot(
|
||||||
|
options: ResolveCombatLootOptions,
|
||||||
|
): ResolveCombatLootResult {
|
||||||
|
const inventory = cloneInventory(options.inventory);
|
||||||
|
const itemsAwarded: InventoryEntry[] = [];
|
||||||
|
const logEntries: LogEntry[] = [];
|
||||||
|
let goldAwarded = 0;
|
||||||
|
|
||||||
|
const defeatedCreatures = options.combat.enemies.filter(
|
||||||
|
(enemy) => enemy.hpCurrent === 0 && enemy.sourceDefinitionId,
|
||||||
|
);
|
||||||
|
|
||||||
|
for (const enemy of defeatedCreatures) {
|
||||||
|
const creature = findCreatureById(options.content, enemy.sourceDefinitionId!);
|
||||||
|
const lootTableCodes = creature.lootTableCodes ?? [];
|
||||||
|
|
||||||
|
for (const tableCode of lootTableCodes) {
|
||||||
|
const table = findTableByCode(options.content, tableCode);
|
||||||
|
const lookup = lookupTable(table, { roller: options.roller });
|
||||||
|
const rollTotal = lookup.roll.modifiedTotal ?? lookup.roll.total;
|
||||||
|
|
||||||
|
logEntries.push({
|
||||||
|
id: `${options.combat.id}.${creature.id}.${tableCode}.roll`,
|
||||||
|
at: options.at,
|
||||||
|
type: "roll",
|
||||||
|
text: `Rolled loot ${lookup.roll.diceKind} [${lookup.roll.rolls.join(", ")}] on ${tableCode} for ${rollTotal}: ${lookup.entry.label}.`,
|
||||||
|
relatedIds: [options.combat.id, creature.id, tableCode],
|
||||||
|
});
|
||||||
|
|
||||||
|
let creatureGold = 0;
|
||||||
|
const creatureItems: InventoryEntry[] = [];
|
||||||
|
|
||||||
|
for (const effect of lookup.entry.effects ?? []) {
|
||||||
|
if (effect.type === "gain-gold" && effect.amount) {
|
||||||
|
creatureGold += effect.amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (effect.type === "add-item" && effect.referenceId) {
|
||||||
|
mergeInventoryEntry(options.content, inventory.carried, effect.referenceId, effect.amount ?? 1);
|
||||||
|
mergeInventoryEntry(options.content, itemsAwarded, effect.referenceId, effect.amount ?? 1);
|
||||||
|
mergeInventoryEntry(options.content, creatureItems, effect.referenceId, effect.amount ?? 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const reference of lookup.entry.references ?? []) {
|
||||||
|
if (reference.type !== "item") {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
mergeInventoryEntry(options.content, inventory.carried, reference.id, 1);
|
||||||
|
mergeInventoryEntry(options.content, itemsAwarded, reference.id, 1);
|
||||||
|
mergeInventoryEntry(options.content, creatureItems, reference.id, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (creatureGold > 0) {
|
||||||
|
inventory.currency.gold += creatureGold;
|
||||||
|
goldAwarded += creatureGold;
|
||||||
|
}
|
||||||
|
|
||||||
|
logEntries.push(
|
||||||
|
createLootLog(
|
||||||
|
`${options.combat.id}.${creature.id}.${tableCode}.loot`,
|
||||||
|
options.at,
|
||||||
|
`${creature.name} yielded ${summarizeLoot(creatureGold, creatureItems, options.content)}.`,
|
||||||
|
[options.combat.id, creature.id, tableCode],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
inventory,
|
||||||
|
itemsAwarded,
|
||||||
|
goldAwarded,
|
||||||
|
logEntries,
|
||||||
|
};
|
||||||
|
}
|
||||||
115
src/rules/roomEntry.test.ts
Normal file
115
src/rules/roomEntry.test.ts
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { expandLevelFromExit, initializeDungeonLevel } from "./dungeon";
|
||||||
|
import { createRoomStateFromTemplate } from "./rooms";
|
||||||
|
import { enterRoom } from "./roomEntry";
|
||||||
|
|
||||||
|
function createSequenceRoller(values: number[]) {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const next = values[index];
|
||||||
|
index += 1;
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("room entry flow", () => {
|
||||||
|
it("marks a newly generated room as entered and resolves an empty encounter", () => {
|
||||||
|
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||||
|
const expanded = expandLevelFromExit({
|
||||||
|
content: sampleContentPack,
|
||||||
|
levelState,
|
||||||
|
fromRoomId: "room.level1.start",
|
||||||
|
exitDirection: "north",
|
||||||
|
roomTableCode: "L1SR",
|
||||||
|
roller: createSequenceRoller([1, 1]),
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = enterRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
levelState: expanded.levelState,
|
||||||
|
roomId: expanded.createdRoom.id,
|
||||||
|
at: "2026-03-15T12:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.firstEntry).toBe(true);
|
||||||
|
expect(result.encounterResolved).toBe(true);
|
||||||
|
expect(result.room.discovery.entered).toBe(true);
|
||||||
|
expect(result.room.discovery.cleared).toBe(true);
|
||||||
|
expect(result.room.encounter?.resultLabel).toBe("No encounter");
|
||||||
|
expect(result.logEntries.map((entry) => entry.text)).toEqual([
|
||||||
|
"Entered Empty Space.",
|
||||||
|
"Empty Space is quiet.",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves a guard encounter once and records the lookup roll", () => {
|
||||||
|
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||||
|
const guardRoom = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.guard-room",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.guard-room",
|
||||||
|
{ x: 0, y: -1 },
|
||||||
|
);
|
||||||
|
|
||||||
|
levelState.rooms[guardRoom.id] = guardRoom;
|
||||||
|
levelState.discoveredRoomOrder.push(guardRoom.id);
|
||||||
|
|
||||||
|
const result = enterRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
levelState,
|
||||||
|
roomId: guardRoom.id,
|
||||||
|
roller: createSequenceRoller([6]),
|
||||||
|
at: "2026-03-15T12:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.room.templateId).toBe("room.level1.normal.guard-room");
|
||||||
|
expect(result.lookup?.entry.label).toBe("Guard and Warrior");
|
||||||
|
expect(result.room.encounter?.creatureNames).toEqual(["Guard", "Warrior"]);
|
||||||
|
expect(result.room.discovery.cleared).toBe(false);
|
||||||
|
expect(result.logEntries.map((entry) => entry.type)).toEqual(["room", "roll", "room"]);
|
||||||
|
expect(result.logEntries[1]?.text).toContain("Guard and Warrior");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not reroll an encounter when re-entering a room", () => {
|
||||||
|
const levelState = initializeDungeonLevel({ content: sampleContentPack });
|
||||||
|
const guardRoom = createRoomStateFromTemplate(
|
||||||
|
sampleContentPack,
|
||||||
|
"room.level1.guard-room",
|
||||||
|
1,
|
||||||
|
"room.level1.normal.guard-room",
|
||||||
|
{ x: 0, y: -1 },
|
||||||
|
);
|
||||||
|
|
||||||
|
levelState.rooms[guardRoom.id] = guardRoom;
|
||||||
|
levelState.discoveredRoomOrder.push(guardRoom.id);
|
||||||
|
|
||||||
|
const firstEntry = enterRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
levelState,
|
||||||
|
roomId: guardRoom.id,
|
||||||
|
roller: createSequenceRoller([4]),
|
||||||
|
at: "2026-03-15T12:00:00.000Z",
|
||||||
|
});
|
||||||
|
const secondEntry = enterRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
levelState: firstEntry.levelState,
|
||||||
|
roomId: guardRoom.id,
|
||||||
|
roller: createSequenceRoller([6]),
|
||||||
|
at: "2026-03-15T12:05:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(firstEntry.room.encounter?.resultLabel).toBe("Guard");
|
||||||
|
expect(secondEntry.room.encounter?.resultLabel).toBe("Guard");
|
||||||
|
expect(secondEntry.lookup).toBeUndefined();
|
||||||
|
expect(secondEntry.firstEntry).toBe(false);
|
||||||
|
expect(secondEntry.logEntries.map((entry) => entry.text)).toEqual([
|
||||||
|
"Re-entered Guard Room.",
|
||||||
|
"Encounter in Guard Room: Guard.",
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
});
|
||||||
164
src/rules/roomEntry.ts
Normal file
164
src/rules/roomEntry.ts
Normal file
@@ -0,0 +1,164 @@
|
|||||||
|
import { findRoomTemplateById } from "@/data/contentHelpers";
|
||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type { DungeonLevelState, RoomState } from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
import type { DiceRoller } from "./dice";
|
||||||
|
import { resolveRoomEncounter } from "./encounters";
|
||||||
|
import type { TableLookupResult } from "./tables";
|
||||||
|
|
||||||
|
export type EnterRoomOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
levelState: DungeonLevelState;
|
||||||
|
roomId: string;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type EnterRoomResult = {
|
||||||
|
levelState: DungeonLevelState;
|
||||||
|
room: RoomState;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
lookup?: TableLookupResult;
|
||||||
|
firstEntry: boolean;
|
||||||
|
encounterResolved: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
function cloneRoom(room: RoomState): RoomState {
|
||||||
|
return {
|
||||||
|
...room,
|
||||||
|
position: { ...room.position },
|
||||||
|
dimensions: { ...room.dimensions },
|
||||||
|
exits: room.exits.map((exit) => ({ ...exit })),
|
||||||
|
discovery: { ...room.discovery },
|
||||||
|
encounter: room.encounter
|
||||||
|
? {
|
||||||
|
...room.encounter,
|
||||||
|
creatureIds: [...room.encounter.creatureIds],
|
||||||
|
creatureNames: room.encounter.creatureNames
|
||||||
|
? [...room.encounter.creatureNames]
|
||||||
|
: undefined,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
objects: room.objects.map((object) => ({
|
||||||
|
...object,
|
||||||
|
effects: object.effects ? [...object.effects] : undefined,
|
||||||
|
})),
|
||||||
|
notes: [...room.notes],
|
||||||
|
flags: [...room.flags],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneLevel(levelState: DungeonLevelState): DungeonLevelState {
|
||||||
|
return {
|
||||||
|
...levelState,
|
||||||
|
rooms: Object.fromEntries(
|
||||||
|
Object.entries(levelState.rooms).map(([roomId, room]) => [roomId, cloneRoom(room)]),
|
||||||
|
),
|
||||||
|
discoveredRoomOrder: [...levelState.discoveredRoomOrder],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRoomLabel(content: ContentPack, room: RoomState) {
|
||||||
|
if (room.templateId) {
|
||||||
|
return findRoomTemplateById(content, room.templateId).title;
|
||||||
|
}
|
||||||
|
|
||||||
|
return room.notes[0] ?? room.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createLogEntry(
|
||||||
|
id: string,
|
||||||
|
at: string,
|
||||||
|
type: LogEntry["type"],
|
||||||
|
text: string,
|
||||||
|
relatedIds: string[],
|
||||||
|
): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type,
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatRollText(lookup: TableLookupResult) {
|
||||||
|
const total = lookup.roll.modifiedTotal ?? lookup.roll.total;
|
||||||
|
const rolledValues = lookup.roll.rolls.join(", ");
|
||||||
|
|
||||||
|
return `Rolled ${lookup.roll.diceKind} [${rolledValues}] on ${lookup.tableId} for ${total}: ${lookup.entry.label}.`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function enterRoom(options: EnterRoomOptions): EnterRoomResult {
|
||||||
|
const levelState = cloneLevel(options.levelState);
|
||||||
|
const room = levelState.rooms[options.roomId];
|
||||||
|
|
||||||
|
if (!room) {
|
||||||
|
throw new Error(`Unknown room id: ${options.roomId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
const roomLabel = getRoomLabel(options.content, room);
|
||||||
|
const firstEntry = !room.discovery.entered;
|
||||||
|
room.discovery.entered = true;
|
||||||
|
|
||||||
|
const resolution = resolveRoomEncounter(options.content, room, options.roller);
|
||||||
|
const nextRoom = resolution.room;
|
||||||
|
const encounterResolved = Boolean(resolution.encounter);
|
||||||
|
|
||||||
|
if (resolution.encounter && resolution.encounter.creatureIds.length === 0) {
|
||||||
|
nextRoom.discovery.cleared = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
levelState.rooms[nextRoom.id] = nextRoom;
|
||||||
|
|
||||||
|
const entryVerb = firstEntry ? "Entered" : "Re-entered";
|
||||||
|
const logEntries: LogEntry[] = [
|
||||||
|
createLogEntry(
|
||||||
|
`${nextRoom.id}.entry.${firstEntry ? "first" : "repeat"}`,
|
||||||
|
at,
|
||||||
|
"room",
|
||||||
|
`${entryVerb} ${roomLabel}.`,
|
||||||
|
[nextRoom.id],
|
||||||
|
),
|
||||||
|
];
|
||||||
|
|
||||||
|
if (resolution.lookup) {
|
||||||
|
logEntries.push(
|
||||||
|
createLogEntry(
|
||||||
|
`${nextRoom.id}.entry.roll`,
|
||||||
|
at,
|
||||||
|
"roll",
|
||||||
|
formatRollText(resolution.lookup),
|
||||||
|
[nextRoom.id, resolution.lookup.tableId],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resolution.encounter) {
|
||||||
|
const encounterText =
|
||||||
|
resolution.encounter.creatureIds.length === 0
|
||||||
|
? `${roomLabel} is quiet.`
|
||||||
|
: `Encounter in ${roomLabel}: ${resolution.encounter.resultLabel ?? "Unknown threat"}.`;
|
||||||
|
|
||||||
|
logEntries.push(
|
||||||
|
createLogEntry(
|
||||||
|
`${nextRoom.id}.entry.encounter`,
|
||||||
|
at,
|
||||||
|
"room",
|
||||||
|
encounterText,
|
||||||
|
[nextRoom.id, resolution.encounter.id],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
levelState,
|
||||||
|
room: nextRoom,
|
||||||
|
logEntries,
|
||||||
|
lookup: resolution.lookup,
|
||||||
|
firstEntry,
|
||||||
|
encounterResolved,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -136,7 +136,9 @@ export function createRoomStateFromTemplate(
|
|||||||
},
|
},
|
||||||
encounter: undefined,
|
encounter: undefined,
|
||||||
objects: [],
|
objects: [],
|
||||||
notes: [template.text ?? template.title].filter(Boolean),
|
notes: [template.text ?? template.title, template.encounterText].filter(
|
||||||
|
(note): note is string => Boolean(note),
|
||||||
|
),
|
||||||
flags: [
|
flags: [
|
||||||
`table:${template.tableCode}`,
|
`table:${template.tableCode}`,
|
||||||
`entry:${template.tableEntryKey}`,
|
`entry:${template.tableEntryKey}`,
|
||||||
|
|||||||
272
src/rules/runState.test.ts
Normal file
272
src/rules/runState.test.ts
Normal file
@@ -0,0 +1,272 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import {
|
||||||
|
createRunState,
|
||||||
|
enterCurrentRoom,
|
||||||
|
getAvailableMoves,
|
||||||
|
isCurrentRoomCombatReady,
|
||||||
|
resolveRunEnemyTurn,
|
||||||
|
resolveRunPlayerTurn,
|
||||||
|
startCombatInCurrentRoom,
|
||||||
|
travelCurrentExit,
|
||||||
|
} from "./runState";
|
||||||
|
|
||||||
|
function createSequenceRoller(values: number[]) {
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
const next = values[index];
|
||||||
|
index += 1;
|
||||||
|
return next;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function createAdventurer() {
|
||||||
|
return createStartingAdventurer(sampleContentPack, {
|
||||||
|
name: "Aster",
|
||||||
|
weaponId: "weapon.short-sword",
|
||||||
|
armourId: "armour.leather-vest",
|
||||||
|
scrollId: "scroll.lesser-heal",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("run state flow", () => {
|
||||||
|
it("creates a run anchored in the start room", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(run.currentLevel).toBe(1);
|
||||||
|
expect(run.currentRoomId).toBe("room.level1.start");
|
||||||
|
expect(run.dungeon.levels["1"]?.rooms["room.level1.start"]).toBeDefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("enters the current room and appends room logs", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = enterCurrentRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run,
|
||||||
|
at: "2026-03-15T14:01:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.run.log).toHaveLength(1);
|
||||||
|
expect(result.run.log[0]?.text).toContain("Re-entered Entry Chamber");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("starts combat from the current room and stores the active combat state", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
const levelState = run.dungeon.levels["1"]!;
|
||||||
|
const room = levelState.rooms["room.level1.start"]!;
|
||||||
|
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a", "b"],
|
||||||
|
creatureNames: ["Guard", "Warrior"],
|
||||||
|
resultLabel: "Guard and Warrior",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const result = startCombatInCurrentRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run,
|
||||||
|
at: "2026-03-15T14:02:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.run.activeCombat?.enemies).toHaveLength(2);
|
||||||
|
expect(result.run.log.at(-1)?.text).toContain("Combat begins");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("resolves player and enemy turns through run state", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
const levelState = run.dungeon.levels["1"]!;
|
||||||
|
const room = levelState.rooms["room.level1.start"]!;
|
||||||
|
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a", "b"],
|
||||||
|
creatureNames: ["Guard", "Warrior"],
|
||||||
|
resultLabel: "Guard and Warrior",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const withCombat = startCombatInCurrentRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run,
|
||||||
|
at: "2026-03-15T14:02:00.000Z",
|
||||||
|
}).run;
|
||||||
|
const targetEnemyId = withCombat.activeCombat!.enemies[0]!.id;
|
||||||
|
|
||||||
|
const afterPlayer = resolveRunPlayerTurn({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run: withCombat,
|
||||||
|
manoeuvreId: "manoeuvre.exact-strike",
|
||||||
|
targetEnemyId,
|
||||||
|
roller: createSequenceRoller([6, 6]),
|
||||||
|
at: "2026-03-15T14:03:00.000Z",
|
||||||
|
}).run;
|
||||||
|
|
||||||
|
expect(afterPlayer.activeCombat?.actingSide).toBe("enemy");
|
||||||
|
expect(afterPlayer.log.at(-1)?.text).toContain("deals");
|
||||||
|
|
||||||
|
const afterEnemy = resolveRunEnemyTurn({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run: afterPlayer,
|
||||||
|
roller: createSequenceRoller([6, 6]),
|
||||||
|
at: "2026-03-15T14:04:00.000Z",
|
||||||
|
}).run;
|
||||||
|
|
||||||
|
expect(afterEnemy.activeCombat?.actingSide).toBe("player");
|
||||||
|
expect(afterEnemy.adventurerSnapshot.hp.current).toBeLessThan(
|
||||||
|
createAdventurer().hp.current,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("clears the room and ends active combat when the last enemy falls", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
const room = run.dungeon.levels["1"]!.rooms["room.level1.start"]!;
|
||||||
|
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a"],
|
||||||
|
creatureNames: ["Guard"],
|
||||||
|
resultLabel: "Guard",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const withCombat = startCombatInCurrentRoom({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run,
|
||||||
|
at: "2026-03-15T14:02:00.000Z",
|
||||||
|
}).run;
|
||||||
|
|
||||||
|
withCombat.activeCombat!.enemies[0]!.hpCurrent = 1;
|
||||||
|
|
||||||
|
const result = resolveRunPlayerTurn({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run: withCombat,
|
||||||
|
manoeuvreId: "manoeuvre.guard-break",
|
||||||
|
targetEnemyId: withCombat.activeCombat!.enemies[0]!.id,
|
||||||
|
roller: createSequenceRoller([6, 6, 5]),
|
||||||
|
at: "2026-03-15T14:03:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.run.activeCombat).toBeUndefined();
|
||||||
|
expect(result.run.dungeon.levels["1"]!.rooms["room.level1.start"]!.discovery.cleared).toBe(true);
|
||||||
|
expect(result.run.dungeon.levels["1"]!.rooms["room.level1.start"]!.encounter?.rewardPending).toBe(
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
expect(result.run.adventurerSnapshot.xp).toBe(2);
|
||||||
|
expect(result.run.xpGained).toBe(2);
|
||||||
|
expect(result.run.adventurerSnapshot.inventory.currency.gold).toBe(2);
|
||||||
|
expect(result.run.goldGained).toBe(2);
|
||||||
|
expect(result.run.defeatedCreatureIds).toEqual(["creature.level1.guard"]);
|
||||||
|
expect(result.run.lootedItems).toEqual([
|
||||||
|
{
|
||||||
|
definitionId: "item.silver-clasp",
|
||||||
|
quantity: 1,
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
expect(result.run.adventurerSnapshot.inventory.carried).toEqual(
|
||||||
|
expect.arrayContaining([
|
||||||
|
expect.objectContaining({
|
||||||
|
definitionId: "item.silver-clasp",
|
||||||
|
quantity: 1,
|
||||||
|
}),
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
expect(result.run.log.at(-1)?.text).toContain("Victory rewards");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("lists available traversable exits for the current room", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(getAvailableMoves(run)).toEqual([
|
||||||
|
expect.objectContaining({
|
||||||
|
direction: "north",
|
||||||
|
generated: false,
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("travels through an unresolved exit, generates a room, and enters it", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = travelCurrentExit({
|
||||||
|
content: sampleContentPack,
|
||||||
|
run,
|
||||||
|
exitDirection: "north",
|
||||||
|
roller: createSequenceRoller([1, 1]),
|
||||||
|
at: "2026-03-15T14:05:00.000Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(result.run.currentRoomId).toBe("room.level1.room.002");
|
||||||
|
expect(result.run.dungeon.levels["1"]!.discoveredRoomOrder).toEqual([
|
||||||
|
"room.level1.start",
|
||||||
|
"room.level1.room.002",
|
||||||
|
]);
|
||||||
|
expect(result.run.dungeon.levels["1"]!.rooms["room.level1.room.002"]!.discovery.entered).toBe(
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
expect(result.run.log[0]?.text).toContain("Travelled north");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("flags combat-ready rooms once entry resolves a hostile encounter", () => {
|
||||||
|
const run = createRunState({
|
||||||
|
content: sampleContentPack,
|
||||||
|
campaignId: "campaign.1",
|
||||||
|
adventurer: createAdventurer(),
|
||||||
|
at: "2026-03-15T14:00:00.000Z",
|
||||||
|
});
|
||||||
|
const room = run.dungeon.levels["1"]!.rooms["room.level1.start"]!;
|
||||||
|
|
||||||
|
room.encounter = {
|
||||||
|
id: `${room.id}.encounter`,
|
||||||
|
sourceTableCode: "L1G",
|
||||||
|
creatureIds: ["a", "b"],
|
||||||
|
creatureNames: ["Guard", "Warrior"],
|
||||||
|
resultLabel: "Guard and Warrior",
|
||||||
|
resolved: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(isCurrentRoomCombatReady(run)).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
580
src/rules/runState.ts
Normal file
580
src/rules/runState.ts
Normal file
@@ -0,0 +1,580 @@
|
|||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type {
|
||||||
|
AdventurerState,
|
||||||
|
CombatState,
|
||||||
|
DungeonState,
|
||||||
|
RunState,
|
||||||
|
} from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
import { findCreatureById } from "@/data/contentHelpers";
|
||||||
|
|
||||||
|
import { startCombatFromRoom } from "./combat";
|
||||||
|
import { resolveCombatLoot } from "./loot";
|
||||||
|
import {
|
||||||
|
resolveEnemyTurn,
|
||||||
|
resolvePlayerAttack,
|
||||||
|
type ResolveEnemyTurnOptions,
|
||||||
|
type ResolvePlayerAttackOptions,
|
||||||
|
} from "./combatTurns";
|
||||||
|
import {
|
||||||
|
expandLevelFromExit,
|
||||||
|
getUnresolvedExits,
|
||||||
|
initializeDungeonLevel,
|
||||||
|
} from "./dungeon";
|
||||||
|
import type { DiceRoller } from "./dice";
|
||||||
|
import { enterRoom } from "./roomEntry";
|
||||||
|
|
||||||
|
export type CreateRunOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
campaignId: string;
|
||||||
|
adventurer: AdventurerState;
|
||||||
|
runId?: string;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type EnterCurrentRoomOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
run: RunState;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type StartCurrentCombatOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
run: RunState;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolveRunPlayerTurnOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
run: RunState;
|
||||||
|
manoeuvreId: string;
|
||||||
|
targetEnemyId: string;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ResolveRunEnemyTurnOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
run: RunState;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type TravelCurrentExitOptions = {
|
||||||
|
content: ContentPack;
|
||||||
|
run: RunState;
|
||||||
|
exitDirection: "north" | "east" | "south" | "west";
|
||||||
|
roomTableCode?: string;
|
||||||
|
roller?: DiceRoller;
|
||||||
|
at?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type AvailableMove = {
|
||||||
|
direction: "north" | "east" | "south" | "west";
|
||||||
|
exitType: string;
|
||||||
|
discovered: boolean;
|
||||||
|
leadsToRoomId?: string;
|
||||||
|
generated: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type RunTransitionResult = {
|
||||||
|
run: RunState;
|
||||||
|
logEntries: LogEntry[];
|
||||||
|
};
|
||||||
|
|
||||||
|
function cloneCombat(combat: CombatState): CombatState {
|
||||||
|
return {
|
||||||
|
...combat,
|
||||||
|
player: {
|
||||||
|
...combat.player,
|
||||||
|
statuses: [...combat.player.statuses],
|
||||||
|
traits: [...combat.player.traits],
|
||||||
|
},
|
||||||
|
enemies: combat.enemies.map((enemy) => ({
|
||||||
|
...enemy,
|
||||||
|
statuses: [...enemy.statuses],
|
||||||
|
traits: [...enemy.traits],
|
||||||
|
})),
|
||||||
|
combatLog: combat.combatLog.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
relatedIds: entry.relatedIds ? [...entry.relatedIds] : undefined,
|
||||||
|
})),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function cloneRun(run: RunState): RunState {
|
||||||
|
return {
|
||||||
|
...run,
|
||||||
|
adventurerSnapshot: {
|
||||||
|
...run.adventurerSnapshot,
|
||||||
|
hp: { ...run.adventurerSnapshot.hp },
|
||||||
|
stats: { ...run.adventurerSnapshot.stats },
|
||||||
|
favour: { ...run.adventurerSnapshot.favour },
|
||||||
|
statuses: run.adventurerSnapshot.statuses.map((status) => ({ ...status })),
|
||||||
|
inventory: {
|
||||||
|
...run.adventurerSnapshot.inventory,
|
||||||
|
carried: run.adventurerSnapshot.inventory.carried.map((entry) => ({ ...entry })),
|
||||||
|
equipped: run.adventurerSnapshot.inventory.equipped.map((entry) => ({ ...entry })),
|
||||||
|
stored: run.adventurerSnapshot.inventory.stored.map((entry) => ({ ...entry })),
|
||||||
|
currency: { ...run.adventurerSnapshot.inventory.currency },
|
||||||
|
lightSources: run.adventurerSnapshot.inventory.lightSources.map((entry) => ({ ...entry })),
|
||||||
|
},
|
||||||
|
progressionFlags: [...run.adventurerSnapshot.progressionFlags],
|
||||||
|
manoeuvreIds: [...run.adventurerSnapshot.manoeuvreIds],
|
||||||
|
},
|
||||||
|
dungeon: {
|
||||||
|
levels: Object.fromEntries(
|
||||||
|
Object.entries(run.dungeon.levels).map(([level, levelState]) => [
|
||||||
|
level,
|
||||||
|
{
|
||||||
|
...levelState,
|
||||||
|
rooms: Object.fromEntries(
|
||||||
|
Object.entries(levelState.rooms).map(([roomId, room]) => [
|
||||||
|
roomId,
|
||||||
|
{
|
||||||
|
...room,
|
||||||
|
position: { ...room.position },
|
||||||
|
dimensions: { ...room.dimensions },
|
||||||
|
exits: room.exits.map((exit) => ({ ...exit })),
|
||||||
|
discovery: { ...room.discovery },
|
||||||
|
encounter: room.encounter
|
||||||
|
? {
|
||||||
|
...room.encounter,
|
||||||
|
creatureIds: [...room.encounter.creatureIds],
|
||||||
|
creatureNames: room.encounter.creatureNames
|
||||||
|
? [...room.encounter.creatureNames]
|
||||||
|
: undefined,
|
||||||
|
}
|
||||||
|
: undefined,
|
||||||
|
objects: room.objects.map((object) => ({
|
||||||
|
...object,
|
||||||
|
effects: object.effects ? [...object.effects] : undefined,
|
||||||
|
})),
|
||||||
|
notes: [...room.notes],
|
||||||
|
flags: [...room.flags],
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
),
|
||||||
|
discoveredRoomOrder: [...levelState.discoveredRoomOrder],
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
) as DungeonState["levels"],
|
||||||
|
revealedPercentByLevel: { ...run.dungeon.revealedPercentByLevel },
|
||||||
|
globalFlags: [...run.dungeon.globalFlags],
|
||||||
|
},
|
||||||
|
activeCombat: run.activeCombat ? cloneCombat(run.activeCombat) : undefined,
|
||||||
|
defeatedCreatureIds: [...run.defeatedCreatureIds],
|
||||||
|
xpGained: run.xpGained,
|
||||||
|
goldGained: run.goldGained,
|
||||||
|
lootedItems: run.lootedItems.map((entry) => ({ ...entry })),
|
||||||
|
log: run.log.map((entry) => ({
|
||||||
|
...entry,
|
||||||
|
relatedIds: entry.relatedIds ? [...entry.relatedIds] : undefined,
|
||||||
|
})),
|
||||||
|
pendingEffects: run.pendingEffects.map((effect) => ({ ...effect })),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireCurrentLevel(run: RunState) {
|
||||||
|
const levelState = run.dungeon.levels[run.currentLevel];
|
||||||
|
|
||||||
|
if (!levelState) {
|
||||||
|
throw new Error(`Run is missing level ${run.currentLevel}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return levelState;
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireCurrentRoomId(run: RunState) {
|
||||||
|
if (!run.currentRoomId) {
|
||||||
|
throw new Error("Run does not have a current room.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return run.currentRoomId;
|
||||||
|
}
|
||||||
|
|
||||||
|
function requireCurrentRoom(run: RunState) {
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const room = levelState.rooms[roomId];
|
||||||
|
|
||||||
|
if (!room) {
|
||||||
|
throw new Error(`Unknown room id: ${roomId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return room;
|
||||||
|
}
|
||||||
|
|
||||||
|
function inferNextRoomTableCode(run: RunState) {
|
||||||
|
const room = requireCurrentRoom(run);
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
|
||||||
|
if (room.roomClass === "start") {
|
||||||
|
return "L1LR";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (room.roomClass === "small") {
|
||||||
|
return "L1LR";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (room.roomClass === "large") {
|
||||||
|
return "L1SR";
|
||||||
|
}
|
||||||
|
|
||||||
|
return levelState.discoveredRoomOrder.length % 2 === 0 ? "L1LR" : "L1SR";
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncPlayerToAdventurer(run: RunState) {
|
||||||
|
if (!run.activeCombat) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
run.adventurerSnapshot.hp.current = run.activeCombat.player.hpCurrent;
|
||||||
|
run.adventurerSnapshot.statuses = run.activeCombat.player.statuses.map((status) => ({ ...status }));
|
||||||
|
}
|
||||||
|
|
||||||
|
function appendLogs(run: RunState, logEntries: LogEntry[]) {
|
||||||
|
run.log.push(...logEntries);
|
||||||
|
}
|
||||||
|
|
||||||
|
function createRewardLog(
|
||||||
|
id: string,
|
||||||
|
at: string,
|
||||||
|
text: string,
|
||||||
|
relatedIds: string[],
|
||||||
|
): LogEntry {
|
||||||
|
return {
|
||||||
|
id,
|
||||||
|
at,
|
||||||
|
type: "progression",
|
||||||
|
text,
|
||||||
|
relatedIds,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function applyCombatRewards(
|
||||||
|
content: ContentPack,
|
||||||
|
run: RunState,
|
||||||
|
completedCombat: CombatState,
|
||||||
|
roller: DiceRoller | undefined,
|
||||||
|
at: string,
|
||||||
|
) {
|
||||||
|
const defeatedCreatureIds = completedCombat.enemies
|
||||||
|
.filter((enemy) => enemy.hpCurrent === 0 && enemy.sourceDefinitionId)
|
||||||
|
.map((enemy) => enemy.sourceDefinitionId!);
|
||||||
|
const xpAwarded = defeatedCreatureIds.reduce((total, creatureId) => {
|
||||||
|
return total + (findCreatureById(content, creatureId).xpReward ?? 0);
|
||||||
|
}, 0);
|
||||||
|
|
||||||
|
run.defeatedCreatureIds.push(...defeatedCreatureIds);
|
||||||
|
run.xpGained += xpAwarded;
|
||||||
|
run.adventurerSnapshot.xp += xpAwarded;
|
||||||
|
|
||||||
|
const lootResult = resolveCombatLoot({
|
||||||
|
content,
|
||||||
|
combat: completedCombat,
|
||||||
|
inventory: run.adventurerSnapshot.inventory,
|
||||||
|
roller,
|
||||||
|
at,
|
||||||
|
});
|
||||||
|
|
||||||
|
run.adventurerSnapshot.inventory = lootResult.inventory;
|
||||||
|
run.goldGained += lootResult.goldAwarded;
|
||||||
|
|
||||||
|
for (const item of lootResult.itemsAwarded) {
|
||||||
|
const existing = run.lootedItems.find(
|
||||||
|
(entry) => entry.definitionId === item.definitionId,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
existing.quantity += item.quantity;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
run.lootedItems.push({ ...item });
|
||||||
|
}
|
||||||
|
const rewardLogs = [...lootResult.logEntries];
|
||||||
|
|
||||||
|
if (xpAwarded === 0 && lootResult.goldAwarded === 0 && lootResult.itemsAwarded.length === 0) {
|
||||||
|
return rewardLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
rewardLogs.push(
|
||||||
|
createRewardLog(
|
||||||
|
`${completedCombat.id}.rewards`,
|
||||||
|
at,
|
||||||
|
`Victory rewards: gained ${xpAwarded} XP, ${lootResult.goldAwarded} gold, and ${lootResult.itemsAwarded.reduce((total, item) => total + item.quantity, 0)} loot item${lootResult.itemsAwarded.reduce((total, item) => total + item.quantity, 0) === 1 ? "" : "s"} from ${defeatedCreatureIds.length} defeated creature${defeatedCreatureIds.length === 1 ? "" : "s"}.`,
|
||||||
|
[completedCombat.id, ...defeatedCreatureIds],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
return rewardLogs;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function createRunState(options: CreateRunOptions): RunState {
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
const levelState = initializeDungeonLevel({
|
||||||
|
content: options.content,
|
||||||
|
level: 1,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
id: options.runId ?? "run.active",
|
||||||
|
campaignId: options.campaignId,
|
||||||
|
status: "active",
|
||||||
|
startedAt: at,
|
||||||
|
currentLevel: 1,
|
||||||
|
currentRoomId: "room.level1.start",
|
||||||
|
dungeon: {
|
||||||
|
levels: {
|
||||||
|
1: levelState,
|
||||||
|
},
|
||||||
|
revealedPercentByLevel: {
|
||||||
|
1: 0,
|
||||||
|
},
|
||||||
|
globalFlags: [],
|
||||||
|
},
|
||||||
|
adventurerSnapshot: options.adventurer,
|
||||||
|
defeatedCreatureIds: [],
|
||||||
|
xpGained: 0,
|
||||||
|
goldGained: 0,
|
||||||
|
lootedItems: [],
|
||||||
|
log: [],
|
||||||
|
pendingEffects: [],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function enterCurrentRoom(
|
||||||
|
options: EnterCurrentRoomOptions,
|
||||||
|
): RunTransitionResult {
|
||||||
|
const run = cloneRun(options.run);
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const entry = enterRoom({
|
||||||
|
content: options.content,
|
||||||
|
levelState,
|
||||||
|
roomId,
|
||||||
|
roller: options.roller,
|
||||||
|
at: options.at,
|
||||||
|
});
|
||||||
|
|
||||||
|
run.dungeon.levels[run.currentLevel] = entry.levelState;
|
||||||
|
appendLogs(run, entry.logEntries);
|
||||||
|
|
||||||
|
return {
|
||||||
|
run,
|
||||||
|
logEntries: entry.logEntries,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getAvailableMoves(run: RunState): AvailableMove[] {
|
||||||
|
const room = requireCurrentRoom(run);
|
||||||
|
|
||||||
|
return room.exits
|
||||||
|
.filter((exit) => exit.traversable)
|
||||||
|
.map((exit) => ({
|
||||||
|
direction: exit.direction,
|
||||||
|
exitType: exit.exitType,
|
||||||
|
discovered: exit.discovered,
|
||||||
|
leadsToRoomId: exit.leadsToRoomId,
|
||||||
|
generated: Boolean(exit.leadsToRoomId),
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isCurrentRoomCombatReady(run: RunState) {
|
||||||
|
const room = requireCurrentRoom(run);
|
||||||
|
|
||||||
|
return Boolean(
|
||||||
|
room.encounter?.resolved &&
|
||||||
|
room.encounter.creatureNames &&
|
||||||
|
room.encounter.creatureNames.length > 0,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function travelCurrentExit(
|
||||||
|
options: TravelCurrentExitOptions,
|
||||||
|
): RunTransitionResult {
|
||||||
|
const run = cloneRun(options.run);
|
||||||
|
|
||||||
|
if (run.activeCombat) {
|
||||||
|
throw new Error("Cannot travel while combat is active.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const room = requireCurrentRoom(run);
|
||||||
|
const exit = room.exits.find((candidate) => candidate.direction === options.exitDirection);
|
||||||
|
|
||||||
|
if (!exit) {
|
||||||
|
throw new Error(`Current room does not have an exit to the ${options.exitDirection}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!exit.traversable) {
|
||||||
|
throw new Error(`Exit ${exit.id} is not traversable.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
let nextLevelState = levelState;
|
||||||
|
let destinationRoomId = exit.leadsToRoomId;
|
||||||
|
const at = options.at ?? new Date().toISOString();
|
||||||
|
|
||||||
|
if (!destinationRoomId) {
|
||||||
|
const unresolvedExits = getUnresolvedExits(levelState);
|
||||||
|
const matchingExit = unresolvedExits.find(
|
||||||
|
(candidate) =>
|
||||||
|
candidate.roomId === roomId && candidate.direction === options.exitDirection,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!matchingExit) {
|
||||||
|
throw new Error(`Exit ${exit.id} is no longer available for generation.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const expansion = expandLevelFromExit({
|
||||||
|
content: options.content,
|
||||||
|
levelState,
|
||||||
|
fromRoomId: roomId,
|
||||||
|
exitDirection: options.exitDirection,
|
||||||
|
roomTableCode: options.roomTableCode ?? inferNextRoomTableCode(run),
|
||||||
|
roller: options.roller,
|
||||||
|
});
|
||||||
|
|
||||||
|
nextLevelState = expansion.levelState;
|
||||||
|
destinationRoomId = expansion.createdRoom.id;
|
||||||
|
}
|
||||||
|
|
||||||
|
run.dungeon.levels[run.currentLevel] = nextLevelState;
|
||||||
|
run.currentRoomId = destinationRoomId;
|
||||||
|
|
||||||
|
const movedLog: LogEntry = {
|
||||||
|
id: `${roomId}.travel.${options.exitDirection}.${run.log.length + 1}`,
|
||||||
|
at,
|
||||||
|
type: "room",
|
||||||
|
text: `Travelled ${options.exitDirection} from ${room.id} to ${destinationRoomId}.`,
|
||||||
|
relatedIds: [room.id, destinationRoomId],
|
||||||
|
};
|
||||||
|
|
||||||
|
appendLogs(run, [movedLog]);
|
||||||
|
|
||||||
|
const entered = enterCurrentRoom({
|
||||||
|
content: options.content,
|
||||||
|
run,
|
||||||
|
roller: options.roller,
|
||||||
|
at,
|
||||||
|
});
|
||||||
|
|
||||||
|
return {
|
||||||
|
run: entered.run,
|
||||||
|
logEntries: [movedLog, ...entered.logEntries],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function startCombatInCurrentRoom(
|
||||||
|
options: StartCurrentCombatOptions,
|
||||||
|
): RunTransitionResult {
|
||||||
|
const run = cloneRun(options.run);
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const room = levelState.rooms[roomId];
|
||||||
|
|
||||||
|
if (!room) {
|
||||||
|
throw new Error(`Unknown room id: ${roomId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const started = startCombatFromRoom({
|
||||||
|
content: options.content,
|
||||||
|
adventurer: run.adventurerSnapshot,
|
||||||
|
room,
|
||||||
|
at: options.at,
|
||||||
|
});
|
||||||
|
|
||||||
|
levelState.rooms[roomId] = started.room;
|
||||||
|
run.activeCombat = started.combat;
|
||||||
|
appendLogs(run, started.logEntries);
|
||||||
|
|
||||||
|
return {
|
||||||
|
run,
|
||||||
|
logEntries: started.logEntries,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveRunPlayerTurn(
|
||||||
|
options: ResolveRunPlayerTurnOptions,
|
||||||
|
): RunTransitionResult {
|
||||||
|
const run = cloneRun(options.run);
|
||||||
|
|
||||||
|
if (!run.activeCombat) {
|
||||||
|
throw new Error("Run does not have an active combat.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = resolvePlayerAttack({
|
||||||
|
content: options.content,
|
||||||
|
combat: run.activeCombat,
|
||||||
|
adventurer: run.adventurerSnapshot,
|
||||||
|
manoeuvreId: options.manoeuvreId,
|
||||||
|
targetEnemyId: options.targetEnemyId,
|
||||||
|
roller: options.roller,
|
||||||
|
at: options.at,
|
||||||
|
} satisfies ResolvePlayerAttackOptions);
|
||||||
|
|
||||||
|
run.activeCombat = result.combat;
|
||||||
|
syncPlayerToAdventurer(run);
|
||||||
|
appendLogs(run, result.logEntries);
|
||||||
|
|
||||||
|
if (result.combatEnded) {
|
||||||
|
const completedCombat = result.combat;
|
||||||
|
const levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const room = levelState.rooms[roomId];
|
||||||
|
const rewardLogs = applyCombatRewards(
|
||||||
|
options.content,
|
||||||
|
run,
|
||||||
|
completedCombat,
|
||||||
|
options.roller,
|
||||||
|
options.at ?? new Date().toISOString(),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (room?.encounter) {
|
||||||
|
room.encounter.rewardPending = false;
|
||||||
|
room.discovery.cleared = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
run.activeCombat = undefined;
|
||||||
|
appendLogs(run, rewardLogs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
run,
|
||||||
|
logEntries: result.logEntries,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveRunEnemyTurn(
|
||||||
|
options: ResolveRunEnemyTurnOptions,
|
||||||
|
): RunTransitionResult {
|
||||||
|
const run = cloneRun(options.run);
|
||||||
|
|
||||||
|
if (!run.activeCombat) {
|
||||||
|
throw new Error("Run does not have an active combat.");
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = resolveEnemyTurn({
|
||||||
|
content: options.content,
|
||||||
|
combat: run.activeCombat,
|
||||||
|
adventurer: run.adventurerSnapshot,
|
||||||
|
roller: options.roller,
|
||||||
|
at: options.at,
|
||||||
|
} satisfies ResolveEnemyTurnOptions);
|
||||||
|
|
||||||
|
run.activeCombat = result.combat;
|
||||||
|
syncPlayerToAdventurer(run);
|
||||||
|
appendLogs(run, result.logEntries);
|
||||||
|
|
||||||
|
if (result.combatEnded) {
|
||||||
|
run.status = "failed";
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
run,
|
||||||
|
logEntries: result.logEntries,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -110,6 +110,8 @@ export const encounterStateSchema = z.object({
|
|||||||
id: z.string().min(1),
|
id: z.string().min(1),
|
||||||
sourceTableCode: z.string().optional(),
|
sourceTableCode: z.string().optional(),
|
||||||
creatureIds: z.array(z.string()),
|
creatureIds: z.array(z.string()),
|
||||||
|
creatureNames: z.array(z.string()).optional(),
|
||||||
|
resultLabel: z.string().optional(),
|
||||||
resolved: z.boolean(),
|
resolved: z.boolean(),
|
||||||
surprise: z.boolean().optional(),
|
surprise: z.boolean().optional(),
|
||||||
rewardPending: z.boolean().optional(),
|
rewardPending: z.boolean().optional(),
|
||||||
@@ -212,6 +214,10 @@ export const runStateSchema = z.object({
|
|||||||
dungeon: dungeonStateSchema,
|
dungeon: dungeonStateSchema,
|
||||||
adventurerSnapshot: adventurerStateSchema,
|
adventurerSnapshot: adventurerStateSchema,
|
||||||
activeCombat: combatStateSchema.optional(),
|
activeCombat: combatStateSchema.optional(),
|
||||||
|
defeatedCreatureIds: z.array(z.string()),
|
||||||
|
xpGained: z.number().int().nonnegative(),
|
||||||
|
goldGained: z.number().int().nonnegative(),
|
||||||
|
lootedItems: z.array(inventoryEntrySchema),
|
||||||
log: z.array(logEntrySchema),
|
log: z.array(logEntrySchema),
|
||||||
pendingEffects: z.array(ruleEffectSchema),
|
pendingEffects: z.array(ruleEffectSchema),
|
||||||
});
|
});
|
||||||
|
|||||||
483
src/styles.css
483
src/styles.css
@@ -1,9 +1,10 @@
|
|||||||
:root {
|
:root {
|
||||||
font-family: "Segoe UI", "Aptos", sans-serif;
|
font-family: "Trebuchet MS", "Segoe UI", sans-serif;
|
||||||
color: #f3f1e8;
|
color: #f4efe3;
|
||||||
background:
|
background:
|
||||||
radial-gradient(circle at top, rgba(179, 121, 59, 0.35), transparent 30%),
|
radial-gradient(circle at top, rgba(177, 91, 29, 0.25), transparent 26%),
|
||||||
linear-gradient(180deg, #17130f 0%, #0d0b09 100%);
|
radial-gradient(circle at 20% 20%, rgba(227, 188, 101, 0.12), transparent 18%),
|
||||||
|
linear-gradient(180deg, #160f0b 0%, #0b0807 100%);
|
||||||
line-height: 1.5;
|
line-height: 1.5;
|
||||||
font-weight: 400;
|
font-weight: 400;
|
||||||
color-scheme: dark;
|
color-scheme: dark;
|
||||||
@@ -23,108 +24,464 @@ body {
|
|||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
button,
|
||||||
|
input,
|
||||||
|
textarea,
|
||||||
|
select {
|
||||||
|
font: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
#root {
|
#root {
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
.app-shell {
|
.app-shell {
|
||||||
width: min(1100px, calc(100% - 2rem));
|
width: min(1200px, calc(100% - 2rem));
|
||||||
margin: 0 auto;
|
margin: 0 auto;
|
||||||
padding: 3rem 0 4rem;
|
padding: 1.5rem 0 3rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hero {
|
.hero {
|
||||||
padding: 2rem;
|
display: flex;
|
||||||
border: 1px solid rgba(243, 241, 232, 0.16);
|
justify-content: space-between;
|
||||||
background: rgba(21, 18, 14, 0.72);
|
gap: 1.5rem;
|
||||||
box-shadow: 0 30px 80px rgba(0, 0, 0, 0.35);
|
align-items: end;
|
||||||
backdrop-filter: blur(12px);
|
padding: 1.75rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.12);
|
||||||
|
background:
|
||||||
|
linear-gradient(135deg, rgba(62, 34, 17, 0.92), rgba(24, 18, 15, 0.86)),
|
||||||
|
rgba(18, 14, 12, 0.9);
|
||||||
|
box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
|
||||||
}
|
}
|
||||||
|
|
||||||
.eyebrow {
|
.eyebrow {
|
||||||
margin: 0 0 0.75rem;
|
margin: 0 0 0.85rem;
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.14em;
|
letter-spacing: 0.18em;
|
||||||
color: #d8b27a;
|
color: #f1ba73;
|
||||||
font-size: 0.8rem;
|
font-size: 0.76rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hero h1 {
|
.hero h1 {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
font-size: clamp(2.5rem, 7vw, 4.5rem);
|
font-size: clamp(2.6rem, 6vw, 4.8rem);
|
||||||
line-height: 0.95;
|
line-height: 0.92;
|
||||||
}
|
}
|
||||||
|
|
||||||
.lede {
|
.lede {
|
||||||
width: min(55ch, 100%);
|
width: min(56ch, 100%);
|
||||||
margin: 1.25rem 0 0;
|
margin: 1rem 0 0;
|
||||||
color: rgba(243, 241, 232, 0.82);
|
color: rgba(244, 239, 227, 0.78);
|
||||||
font-size: 1.05rem;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel-grid {
|
.hero-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
gap: 0.85rem;
|
||||||
|
align-items: flex-end;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-chip {
|
||||||
|
display: flex;
|
||||||
|
gap: 0.65rem;
|
||||||
|
align-items: center;
|
||||||
|
padding: 0.75rem 1rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.12);
|
||||||
|
background: rgba(255, 231, 196, 0.05);
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.08em;
|
||||||
|
font-size: 0.78rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.status-chip strong {
|
||||||
|
color: #f7d59d;
|
||||||
|
font-size: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-banner {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
gap: 1rem;
|
||||||
|
align-items: center;
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding: 1rem 1.2rem;
|
||||||
|
border: 1px solid rgba(255, 176, 94, 0.3);
|
||||||
|
background:
|
||||||
|
linear-gradient(90deg, rgba(117, 43, 21, 0.92), rgba(48, 22, 18, 0.92)),
|
||||||
|
rgba(48, 22, 18, 0.92);
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-kicker {
|
||||||
|
display: block;
|
||||||
|
color: #ffbf78;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.12em;
|
||||||
|
font-size: 0.74rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-banner strong {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
font-size: 1.15rem;
|
||||||
|
color: #fff4de;
|
||||||
|
}
|
||||||
|
|
||||||
|
.alert-banner p {
|
||||||
|
margin: 0.35rem 0 0;
|
||||||
|
color: rgba(244, 239, 227, 0.82);
|
||||||
|
}
|
||||||
|
|
||||||
|
.dashboard-grid {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
|
grid-template-columns: repeat(12, minmax(0, 1fr));
|
||||||
gap: 1rem;
|
gap: 1rem;
|
||||||
margin-top: 1rem;
|
margin-top: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel {
|
.panel {
|
||||||
padding: 1.25rem;
|
grid-column: span 4;
|
||||||
border: 1px solid rgba(243, 241, 232, 0.14);
|
padding: 1.2rem;
|
||||||
background: rgba(29, 24, 19, 0.82);
|
border: 1px solid rgba(255, 231, 196, 0.1);
|
||||||
|
background: rgba(25, 19, 16, 0.86);
|
||||||
|
box-shadow: inset 0 1px 0 rgba(255, 231, 196, 0.03);
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel h2 {
|
.panel-highlight {
|
||||||
margin-top: 0;
|
background:
|
||||||
|
linear-gradient(180deg, rgba(101, 52, 28, 0.28), rgba(25, 19, 16, 0.92)),
|
||||||
|
rgba(25, 19, 16, 0.9);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-inventory {
|
||||||
|
grid-column: span 8;
|
||||||
|
background:
|
||||||
|
linear-gradient(180deg, rgba(43, 32, 24, 0.92), rgba(22, 17, 14, 0.92)),
|
||||||
|
rgba(25, 19, 16, 0.9);
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-log {
|
||||||
|
grid-column: span 12;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-header {
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: baseline;
|
||||||
|
gap: 1rem;
|
||||||
margin-bottom: 0.75rem;
|
margin-bottom: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.panel-header h2 {
|
||||||
|
margin: 0;
|
||||||
font-size: 1rem;
|
font-size: 1rem;
|
||||||
color: #f6d49e;
|
color: #f8d79f;
|
||||||
}
|
}
|
||||||
|
|
||||||
.panel ul,
|
.panel-header span {
|
||||||
.panel ol {
|
color: rgba(244, 239, 227, 0.58);
|
||||||
margin: 0;
|
font-size: 0.83rem;
|
||||||
padding-left: 1.1rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.panel li + li {
|
|
||||||
margin-top: 0.45rem;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stats {
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
||||||
gap: 0.75rem;
|
|
||||||
margin: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.stats div {
|
|
||||||
padding: 0.9rem;
|
|
||||||
background: rgba(243, 241, 232, 0.05);
|
|
||||||
}
|
|
||||||
|
|
||||||
.stats dt {
|
|
||||||
color: rgba(243, 241, 232, 0.62);
|
|
||||||
font-size: 0.8rem;
|
|
||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
letter-spacing: 0.08em;
|
letter-spacing: 0.08em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.stats dd {
|
.stat-strip {
|
||||||
margin: 0.3rem 0 0;
|
display: grid;
|
||||||
font-size: 1.7rem;
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
font-weight: 700;
|
gap: 0.75rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (max-width: 640px) {
|
.stat-strip div,
|
||||||
|
.inventory-badge,
|
||||||
|
.encounter-box,
|
||||||
|
.combat-status {
|
||||||
|
padding: 0.9rem;
|
||||||
|
background: rgba(255, 245, 223, 0.04);
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-strip span,
|
||||||
|
.inventory-badge span,
|
||||||
|
.inventory-label,
|
||||||
|
.encounter-label,
|
||||||
|
.combat-status span,
|
||||||
|
.room-meta span,
|
||||||
|
.log-entry span {
|
||||||
|
display: block;
|
||||||
|
color: rgba(244, 239, 227, 0.56);
|
||||||
|
font-size: 0.76rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.08em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-strip strong,
|
||||||
|
.inventory-badge strong,
|
||||||
|
.encounter-box strong,
|
||||||
|
.combat-status strong {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.3rem;
|
||||||
|
font-size: 1.45rem;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.supporting-text {
|
||||||
|
margin: 0.9rem 0 0;
|
||||||
|
color: rgba(244, 239, 227, 0.76);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-summary,
|
||||||
|
.inventory-grid {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-summary {
|
||||||
|
grid-template-columns: repeat(4, minmax(0, 1fr));
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-grid {
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-section {
|
||||||
|
padding: 1rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background:
|
||||||
|
linear-gradient(180deg, rgba(255, 245, 223, 0.04), rgba(255, 245, 223, 0.02));
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-label {
|
||||||
|
display: block;
|
||||||
|
margin-bottom: 0.7rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-list,
|
||||||
|
.loot-ribbon-list {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.65rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card,
|
||||||
|
.loot-pill {
|
||||||
|
padding: 0.85rem 0.9rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background: rgba(11, 8, 7, 0.32);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card strong,
|
||||||
|
.loot-pill strong {
|
||||||
|
display: block;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card span {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.25rem;
|
||||||
|
color: rgba(244, 239, 227, 0.62);
|
||||||
|
font-size: 0.84rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card-equipped {
|
||||||
|
border-color: rgba(113, 176, 152, 0.35);
|
||||||
|
background: rgba(56, 86, 73, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-card-treasure,
|
||||||
|
.loot-pill {
|
||||||
|
border-color: rgba(214, 168, 86, 0.35);
|
||||||
|
background: rgba(111, 76, 20, 0.18);
|
||||||
|
}
|
||||||
|
|
||||||
|
.loot-ribbon {
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
}
|
||||||
|
|
||||||
|
.loot-ribbon-list {
|
||||||
|
grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
|
||||||
|
margin-top: 0.7rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.room-title {
|
||||||
|
margin: 0 0 0.35rem;
|
||||||
|
font-size: 1.5rem;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.room-meta {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.9rem;
|
||||||
|
margin-top: 0.95rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-row,
|
||||||
|
.enemy-actions {
|
||||||
|
display: flex;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
gap: 0.65rem;
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button {
|
||||||
|
border: 1px solid rgba(255, 217, 163, 0.24);
|
||||||
|
background: rgba(255, 245, 223, 0.04);
|
||||||
|
color: #f4efe3;
|
||||||
|
padding: 0.72rem 1rem;
|
||||||
|
cursor: pointer;
|
||||||
|
transition:
|
||||||
|
transform 140ms ease,
|
||||||
|
background 140ms ease,
|
||||||
|
border-color 140ms ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button:hover:enabled {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
background: rgba(255, 217, 163, 0.09);
|
||||||
|
border-color: rgba(255, 217, 163, 0.35);
|
||||||
|
}
|
||||||
|
|
||||||
|
.button:disabled {
|
||||||
|
opacity: 0.42;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-primary {
|
||||||
|
background: linear-gradient(180deg, #c36b2d, #8d4617);
|
||||||
|
border-color: rgba(255, 217, 163, 0.32);
|
||||||
|
color: #fff4e1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.button-primary:hover:enabled {
|
||||||
|
background: linear-gradient(180deg, #d97833, #9f501b);
|
||||||
|
}
|
||||||
|
|
||||||
|
.encounter-box,
|
||||||
|
.combat-status {
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-list,
|
||||||
|
.mini-map,
|
||||||
|
.enemy-list {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mini-map,
|
||||||
|
.enemy-list {
|
||||||
|
margin-top: 1rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card,
|
||||||
|
.map-node,
|
||||||
|
.enemy-card {
|
||||||
|
width: 100%;
|
||||||
|
text-align: left;
|
||||||
|
padding: 0.95rem;
|
||||||
|
border: 1px solid rgba(255, 231, 196, 0.08);
|
||||||
|
background: rgba(255, 245, 223, 0.04);
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card {
|
||||||
|
cursor: pointer;
|
||||||
|
transition:
|
||||||
|
transform 140ms ease,
|
||||||
|
background 140ms ease,
|
||||||
|
border-color 140ms ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card:hover:enabled {
|
||||||
|
transform: translateY(-1px);
|
||||||
|
background: rgba(255, 217, 163, 0.09);
|
||||||
|
border-color: rgba(255, 217, 163, 0.35);
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card:disabled {
|
||||||
|
opacity: 0.42;
|
||||||
|
cursor: not-allowed;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card span,
|
||||||
|
.map-node span {
|
||||||
|
display: block;
|
||||||
|
color: rgba(244, 239, 227, 0.56);
|
||||||
|
font-size: 0.74rem;
|
||||||
|
text-transform: uppercase;
|
||||||
|
letter-spacing: 0.08em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card strong,
|
||||||
|
.map-node strong,
|
||||||
|
.enemy-card strong {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.3rem;
|
||||||
|
font-size: 1rem;
|
||||||
|
color: #fff2d6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.move-card em,
|
||||||
|
.enemy-card span {
|
||||||
|
display: block;
|
||||||
|
margin-top: 0.3rem;
|
||||||
|
font-style: normal;
|
||||||
|
color: rgba(244, 239, 227, 0.7);
|
||||||
|
}
|
||||||
|
|
||||||
|
.map-node-active {
|
||||||
|
border-color: rgba(243, 186, 115, 0.55);
|
||||||
|
background: rgba(243, 186, 115, 0.12);
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-list {
|
||||||
|
display: grid;
|
||||||
|
gap: 0.75rem;
|
||||||
|
max-height: 340px;
|
||||||
|
overflow: auto;
|
||||||
|
padding-right: 0.2rem;
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-entry {
|
||||||
|
padding: 0.9rem;
|
||||||
|
border-left: 3px solid rgba(243, 186, 115, 0.7);
|
||||||
|
background: rgba(255, 245, 223, 0.04);
|
||||||
|
}
|
||||||
|
|
||||||
|
.log-entry p {
|
||||||
|
margin: 0.35rem 0 0;
|
||||||
|
color: #f4efe3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 980px) {
|
||||||
|
.panel,
|
||||||
|
.panel-log {
|
||||||
|
grid-column: span 12;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@media (max-width: 720px) {
|
||||||
.app-shell {
|
.app-shell {
|
||||||
width: min(100% - 1rem, 1100px);
|
width: min(100% - 1rem, 1200px);
|
||||||
padding-top: 1rem;
|
padding-top: 0.75rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
.hero,
|
.hero,
|
||||||
.panel {
|
.alert-banner {
|
||||||
padding: 1rem;
|
flex-direction: column;
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.hero-actions {
|
||||||
|
align-items: stretch;
|
||||||
|
}
|
||||||
|
|
||||||
|
.stat-strip {
|
||||||
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||||
|
}
|
||||||
|
|
||||||
|
.inventory-summary,
|
||||||
|
.inventory-grid {
|
||||||
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,6 +111,8 @@ export type EncounterState = {
|
|||||||
id: string;
|
id: string;
|
||||||
sourceTableCode?: string;
|
sourceTableCode?: string;
|
||||||
creatureIds: string[];
|
creatureIds: string[];
|
||||||
|
creatureNames?: string[];
|
||||||
|
resultLabel?: string;
|
||||||
resolved: boolean;
|
resolved: boolean;
|
||||||
surprise?: boolean;
|
surprise?: boolean;
|
||||||
rewardPending?: boolean;
|
rewardPending?: boolean;
|
||||||
@@ -213,6 +215,10 @@ export type RunState = {
|
|||||||
dungeon: DungeonState;
|
dungeon: DungeonState;
|
||||||
adventurerSnapshot: AdventurerState;
|
adventurerSnapshot: AdventurerState;
|
||||||
activeCombat?: CombatState;
|
activeCombat?: CombatState;
|
||||||
|
defeatedCreatureIds: string[];
|
||||||
|
xpGained: number;
|
||||||
|
goldGained: number;
|
||||||
|
lootedItems: InventoryEntry[];
|
||||||
log: LogEntry[];
|
log: LogEntry[];
|
||||||
pendingEffects: RuleEffect[];
|
pendingEffects: RuleEffect[];
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user