Files
initiative/packages/domain/src/initiative.ts
Lukas e62c49434c
All checks were successful
CI / check (push) Successful in 2m21s
CI / build-image (push) Successful in 24s
Add Pathfinder 2e game system mode
Implements PF2e as an alternative game system alongside D&D 5e/5.5e.
Settings modal "Game System" selector switches conditions, bestiary,
stat block layout, and initiative calculation between systems.

- Valued conditions with increment/decrement UX (Clumsy 2, Frightened 3)
- 2,502 PF2e creatures from bundled search index (77 sources)
- PF2e stat block: level, traits, Perception, Fort/Ref/Will, ability mods
- Perception-based initiative rolling
- System-scoped source cache (D&D and PF2e sources don't collide)
- Backwards-compatible condition rehydration (ConditionId[] → ConditionEntry[])
- Difficulty indicator hidden in PF2e mode (excluded from MVP)

Closes dostulata/initiative#19

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-07 01:26:22 +02:00

39 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { proficiencyBonus } from "./creature-types.js";
export interface InitiativeResult {
readonly modifier: number;
readonly passive: number;
}
/**
* Calculates initiative modifier and passive initiative from creature stats.
* Returns undefined for combatants without bestiary creature data.
*/
export function calculateInitiative(creature: {
readonly dexScore: number;
readonly cr: string;
readonly initiativeProficiency: number;
}): InitiativeResult {
const dexMod = Math.floor((creature.dexScore - 10) / 2);
const pb = proficiencyBonus(creature.cr);
const modifier = dexMod + (creature.initiativeProficiency ?? 0) * pb;
return { modifier, passive: 10 + modifier };
}
/**
* Returns the PF2e initiative result directly from the Perception modifier.
* No proficiency bonus calculation — PF2e uses Perception as-is.
*/
export function calculatePf2eInitiative(perception: number): InitiativeResult {
return { modifier: perception, passive: perception };
}
/**
* Formats an initiative modifier with explicit sign.
* Uses U+2212 () for negative values.
*/
export function formatInitiativeModifier(modifier: number): string {
if (modifier >= 0) return `+${modifier}`;
return `\u2212${Math.abs(modifier)}`;
}