Implement the 025-display-initiative feature that adds initiative modifier and passive initiative display to creature stat blocks, calculated as DEX modifier + (proficiency multiplier × proficiency bonus) from bestiary data, shown in MM 2024 format on the AC line
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
30
packages/domain/src/initiative.ts
Normal file
30
packages/domain/src/initiative.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
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 };
|
||||
}
|
||||
|
||||
/**
|
||||
* 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)}`;
|
||||
}
|
||||
Reference in New Issue
Block a user