Compare commits
4 Commits
feature/en
...
feature/ma
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d504008030 | ||
|
|
50873e6989 | ||
|
|
ec0de5b0b8 | ||
|
|
4dde4bff99 |
@@ -2,7 +2,11 @@ import { describe, expect, it } from "vitest";
|
|||||||
|
|
||||||
import { lookupTable } from "@/rules/tables";
|
import { lookupTable } from "@/rules/tables";
|
||||||
|
|
||||||
import { findRoomTemplateForLookup, findTableByCode } from "./contentHelpers";
|
import {
|
||||||
|
findCreatureByName,
|
||||||
|
findRoomTemplateForLookup,
|
||||||
|
findTableByCode,
|
||||||
|
} from "./contentHelpers";
|
||||||
import { sampleContentPack } from "./sampleContentPack";
|
import { sampleContentPack } from "./sampleContentPack";
|
||||||
|
|
||||||
function createSequenceRoller(values: number[]) {
|
function createSequenceRoller(values: number[]) {
|
||||||
@@ -46,4 +50,11 @@ 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);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import type { ContentPack, RoomTemplate, TableDefinition } from "@/types/content";
|
import type {
|
||||||
|
ContentPack,
|
||||||
|
CreatureDefinition,
|
||||||
|
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 +41,23 @@ 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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -167,6 +167,92 @@ const samplePack = {
|
|||||||
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,
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
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}`,
|
||||||
|
|||||||
186
src/rules/runState.test.ts
Normal file
186
src/rules/runState.test.ts
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
|
||||||
|
import { sampleContentPack } from "@/data/sampleContentPack";
|
||||||
|
|
||||||
|
import { createStartingAdventurer } from "./character";
|
||||||
|
import {
|
||||||
|
createRunState,
|
||||||
|
enterCurrentRoom,
|
||||||
|
resolveRunEnemyTurn,
|
||||||
|
resolveRunPlayerTurn,
|
||||||
|
startCombatInCurrentRoom,
|
||||||
|
} 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]),
|
||||||
|
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,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
335
src/rules/runState.ts
Normal file
335
src/rules/runState.ts
Normal file
@@ -0,0 +1,335 @@
|
|||||||
|
import type { ContentPack } from "@/types/content";
|
||||||
|
import type {
|
||||||
|
AdventurerState,
|
||||||
|
CombatState,
|
||||||
|
DungeonState,
|
||||||
|
RunState,
|
||||||
|
} from "@/types/state";
|
||||||
|
import type { LogEntry } from "@/types/rules";
|
||||||
|
|
||||||
|
import { startCombatFromRoom } from "./combat";
|
||||||
|
import {
|
||||||
|
resolveEnemyTurn,
|
||||||
|
resolvePlayerAttack,
|
||||||
|
type ResolveEnemyTurnOptions,
|
||||||
|
type ResolvePlayerAttackOptions,
|
||||||
|
} from "./combatTurns";
|
||||||
|
import { 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 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,
|
||||||
|
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 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
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 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 levelState = requireCurrentLevel(run);
|
||||||
|
const roomId = requireCurrentRoomId(run);
|
||||||
|
const room = levelState.rooms[roomId];
|
||||||
|
|
||||||
|
if (room?.encounter) {
|
||||||
|
room.encounter.rewardPending = false;
|
||||||
|
room.discovery.cleared = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
run.activeCombat = undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
};
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user