Initial commit
This commit is contained in:
@@ -0,0 +1,250 @@
|
||||
// D&D 5e rules calculation engine — the ONLY place these formulas may live.
|
||||
// See /docs/GAME_RULES_REFERENCE.md for the spec this implements.
|
||||
// Pure and dependency-free on purpose: no DB calls, no framework imports.
|
||||
|
||||
const { SPECIES } = require('./data/species');
|
||||
const { CLASSES } = require('./data/classes');
|
||||
const { SKILLS } = require('./data/skills');
|
||||
|
||||
const ABILITIES = ['STR', 'DEX', 'CON', 'INT', 'WIS', 'CHA'];
|
||||
|
||||
function abilityModifier(score) {
|
||||
return Math.floor((score - 10) / 2);
|
||||
}
|
||||
|
||||
function proficiencyBonus(level) {
|
||||
return 2 + Math.floor((level - 1) / 4);
|
||||
}
|
||||
|
||||
// Half-Elf's flexible +1/+1 lands on whichever two abilities the player picked
|
||||
// (halfElfBonusAbilities), on top of the fixed +2 CHA in the species table.
|
||||
function speciesAbilityBonus(ability, species, halfElfBonusAbilities = []) {
|
||||
const speciesData = species ? SPECIES[species] : null;
|
||||
if (!speciesData) return 0;
|
||||
let bonus = speciesData.abilityBonuses[ability] || 0;
|
||||
if (species === 'HALF_ELF' && halfElfBonusAbilities.includes(ability)) {
|
||||
bonus += 1;
|
||||
}
|
||||
return bonus;
|
||||
}
|
||||
|
||||
function finalAbilityScore(rolledScore, ability, species, halfElfBonusAbilities = []) {
|
||||
return rolledScore + speciesAbilityBonus(ability, species, halfElfBonusAbilities);
|
||||
}
|
||||
|
||||
function speciesSpeed(species) {
|
||||
return species && SPECIES[species] ? SPECIES[species].speed : 30;
|
||||
}
|
||||
|
||||
function effectiveSpeed(species, speedOverride) {
|
||||
return speedOverride != null ? speedOverride : speciesSpeed(species);
|
||||
}
|
||||
|
||||
function saveBonus(abilityMod, proficient, profBonus) {
|
||||
return abilityMod + (proficient ? profBonus : 0);
|
||||
}
|
||||
|
||||
// expertise (e.g. Rogue) doubles the proficiency portion of the bonus.
|
||||
function skillBonus(abilityMod, proficient, profBonus, expertise = false) {
|
||||
if (!proficient) return abilityMod;
|
||||
return abilityMod + profBonus * (expertise ? 2 : 1);
|
||||
}
|
||||
|
||||
function passivePerception(perceptionSkillBonus) {
|
||||
return 10 + perceptionSkillBonus;
|
||||
}
|
||||
|
||||
function armorClass(dexMod, armorBonus = 0) {
|
||||
return 10 + dexMod + armorBonus;
|
||||
}
|
||||
|
||||
function initiative(dexMod) {
|
||||
return dexMod;
|
||||
}
|
||||
|
||||
// Level 1 always uses the maximum possible Hit Die roll; every level after
|
||||
// that uses the fixed "average" value (floor(hitDieSize / 2) + 1). Do not
|
||||
// regress this to a flat average-for-all-levels formula — that was a bug
|
||||
// caught while building the reference Excel calculator.
|
||||
function hpMax(level, hitDieSize, conMod) {
|
||||
const base = hitDieSize + conMod;
|
||||
if (level <= 1) return base;
|
||||
const perLevelAverage = Math.floor(hitDieSize / 2) + 1;
|
||||
return base + (level - 1) * (perLevelAverage + conMod);
|
||||
}
|
||||
|
||||
function spellSaveDC(profBonus, spellAbilityMod) {
|
||||
return 8 + profBonus + spellAbilityMod;
|
||||
}
|
||||
|
||||
function spellAttackBonus(profBonus, spellAbilityMod) {
|
||||
return profBonus + spellAbilityMod;
|
||||
}
|
||||
|
||||
// attackAbility is "STR" | "DEX" | "FINESSE" (finesse = better of STR/DEX).
|
||||
function weaponAbilityModifier(attackAbility, strMod, dexMod) {
|
||||
if (attackAbility === 'STR') return strMod;
|
||||
if (attackAbility === 'DEX') return dexMod;
|
||||
if (attackAbility === 'FINESSE') return Math.max(strMod, dexMod);
|
||||
throw new Error(`Unknown attackAbility: ${attackAbility}`);
|
||||
}
|
||||
|
||||
function weaponAttackBonus({ attackAbility, strMod, dexMod, proficient, profBonus }) {
|
||||
const abilityMod = weaponAbilityModifier(attackAbility, strMod, dexMod);
|
||||
return abilityMod + (proficient ? profBonus : 0);
|
||||
}
|
||||
|
||||
function weaponDamageBonus({ attackAbility, strMod, dexMod }) {
|
||||
return weaponAbilityModifier(attackAbility, strMod, dexMod);
|
||||
}
|
||||
|
||||
function getGoverningAbilityForSkill(skill) {
|
||||
const data = SKILLS[skill];
|
||||
if (!data) throw new Error(`Unknown skill: ${skill}`);
|
||||
return data.ability;
|
||||
}
|
||||
|
||||
// Saving throw proficiencies are fixed per class, not a player choice —
|
||||
// use this at character-build finalize time to seed CharacterSaveProficiency.
|
||||
function getClassSaveProficiencies(characterClass) {
|
||||
const data = characterClass ? CLASSES[characterClass] : null;
|
||||
return data ? data.saveProficiencies : [];
|
||||
}
|
||||
|
||||
function getClassHitDieSize(characterClass) {
|
||||
const data = characterClass ? CLASSES[characterClass] : null;
|
||||
return data ? data.hitDie : 8;
|
||||
}
|
||||
|
||||
function getClassSpellcastingAbility(characterClass) {
|
||||
const data = characterClass ? CLASSES[characterClass] : null;
|
||||
return data ? data.spellcastingAbility : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembles the full derived character sheet from raw stored data. This is
|
||||
* the single function the API and quick-stats view should call rather than
|
||||
* re-deriving any of the above by hand.
|
||||
*
|
||||
* @param {object} character - rolled scores, species/class, level, armorBonus, etc.
|
||||
* @param {Array<{skill: string, proficient: boolean, expertise: boolean}>} skillProficiencies
|
||||
* @param {Array<{ability: string, proficient: boolean}>} saveProficiencies
|
||||
*/
|
||||
function computeCharacterSheet(character, skillProficiencies = [], saveProficiencies = []) {
|
||||
const {
|
||||
level = 1,
|
||||
species = null,
|
||||
characterClass = null,
|
||||
strScore = 10,
|
||||
dexScore = 10,
|
||||
conScore = 10,
|
||||
intScore = 10,
|
||||
wisScore = 10,
|
||||
chaScore = 10,
|
||||
halfElfBonusAbility1 = null,
|
||||
halfElfBonusAbility2 = null,
|
||||
armorBonus = 0,
|
||||
speedOverride = null,
|
||||
} = character;
|
||||
|
||||
const halfElfBonusAbilities = [halfElfBonusAbility1, halfElfBonusAbility2].filter(Boolean);
|
||||
const rolled = { STR: strScore, DEX: dexScore, CON: conScore, INT: intScore, WIS: wisScore, CHA: chaScore };
|
||||
|
||||
const abilities = {};
|
||||
for (const ability of ABILITIES) {
|
||||
const final = finalAbilityScore(rolled[ability], ability, species, halfElfBonusAbilities);
|
||||
abilities[ability] = {
|
||||
rolled: rolled[ability],
|
||||
final,
|
||||
modifier: abilityModifier(final),
|
||||
};
|
||||
}
|
||||
|
||||
const profBonus = proficiencyBonus(level);
|
||||
|
||||
const saves = {};
|
||||
for (const ability of ABILITIES) {
|
||||
const entry = saveProficiencies.find((s) => s.ability === ability);
|
||||
const proficient = entry ? entry.proficient : false;
|
||||
saves[ability] = { proficient, bonus: saveBonus(abilities[ability].modifier, proficient, profBonus) };
|
||||
}
|
||||
|
||||
const skills = {};
|
||||
for (const skillKey of Object.keys(SKILLS)) {
|
||||
const ability = getGoverningAbilityForSkill(skillKey);
|
||||
const entry = skillProficiencies.find((s) => s.skill === skillKey);
|
||||
const proficient = entry ? entry.proficient : false;
|
||||
const expertise = entry ? Boolean(entry.expertise) : false;
|
||||
skills[skillKey] = {
|
||||
ability,
|
||||
proficient,
|
||||
expertise,
|
||||
bonus: skillBonus(abilities[ability].modifier, proficient, profBonus, expertise),
|
||||
};
|
||||
}
|
||||
|
||||
const hitDieSize = getClassHitDieSize(characterClass);
|
||||
const spellcastingAbility = getClassSpellcastingAbility(characterClass);
|
||||
|
||||
return {
|
||||
abilities,
|
||||
proficiencyBonus: profBonus,
|
||||
armorClass: armorClass(abilities.DEX.modifier, armorBonus),
|
||||
initiative: initiative(abilities.DEX.modifier),
|
||||
speed: effectiveSpeed(species, speedOverride),
|
||||
hp: { max: hpMax(level, hitDieSize, abilities.CON.modifier) },
|
||||
hitDice: { size: hitDieSize, count: level },
|
||||
saves,
|
||||
skills,
|
||||
passivePerception: passivePerception(skills.PERCEPTION.bonus),
|
||||
spellcasting: spellcastingAbility
|
||||
? {
|
||||
ability: spellcastingAbility,
|
||||
saveDC: spellSaveDC(profBonus, abilities[spellcastingAbility].modifier),
|
||||
attackBonus: spellAttackBonus(profBonus, abilities[spellcastingAbility].modifier),
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Attack/damage bonus for a specific weapon against a character's abilities —
|
||||
* used by the quick-stats panel and weapon list.
|
||||
*/
|
||||
function computeWeaponStats(weapon, abilities, profBonus, proficient = true) {
|
||||
const strMod = abilities.STR.modifier;
|
||||
const dexMod = abilities.DEX.modifier;
|
||||
return {
|
||||
attackBonus: weaponAttackBonus({ attackAbility: weapon.attackAbility, strMod, dexMod, proficient, profBonus }),
|
||||
damageBonus: weaponDamageBonus({ attackAbility: weapon.attackAbility, strMod, dexMod }),
|
||||
damageDice: weapon.damageDice,
|
||||
damageType: weapon.damageType,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
ABILITIES,
|
||||
abilityModifier,
|
||||
proficiencyBonus,
|
||||
speciesAbilityBonus,
|
||||
finalAbilityScore,
|
||||
speciesSpeed,
|
||||
effectiveSpeed,
|
||||
saveBonus,
|
||||
skillBonus,
|
||||
passivePerception,
|
||||
armorClass,
|
||||
initiative,
|
||||
hpMax,
|
||||
spellSaveDC,
|
||||
spellAttackBonus,
|
||||
weaponAbilityModifier,
|
||||
weaponAttackBonus,
|
||||
weaponDamageBonus,
|
||||
getGoverningAbilityForSkill,
|
||||
getClassSaveProficiencies,
|
||||
getClassHitDieSize,
|
||||
getClassSpellcastingAbility,
|
||||
computeCharacterSheet,
|
||||
computeWeaponStats,
|
||||
};
|
||||
Reference in New Issue
Block a user