// Temporary hit points are a buffer: damage comes off them first, and only // what is left over reaches real HP. Takes the character's HP and temp HP as // they are and the HP a damage/heal asked for (which may be below zero), and // returns what both should become. Healing never touches temp HP. function absorbDamage(currentHp, tempHp, requestedHp) { const damage = currentHp - requestedHp; if (damage <= 0 || tempHp <= 0) return { currentHp: requestedHp, tempHp }; const absorbed = Math.min(tempHp, damage); return { currentHp: requestedHp + absorbed, tempHp: tempHp - absorbed }; } // --------------------------------------------------------------------------- // At 0 HP — death saves // --------------------------------------------------------------------------- // // A character at 0 HP is dying: they roll a d20 each turn, and three successes // or three failures settle it. The tallies and the "stable" flag live on the // Character row, next to the HP they belong to. // Three successes steady a character; three failures kill them. const DEATH_SAVE_LIMIT = 3; const NO_DEATH_SAVES = { deathSaveSuccesses: 0, deathSaveFailures: 0, stable: false }; // UP: on their feet. DYING: at 0 HP and rolling death saves. STABLE: at 0 HP // but no longer rolling. DEAD: three failures, or one blow too big to survive. function lifeState(character) { if (character.deathSaveFailures >= DEATH_SAVE_LIMIT) return 'DEAD'; if (character.currentHp > 0) return 'UP'; return character.stable ? 'STABLE' : 'DYING'; } // What the API sends about it, the same shape wherever a character's HP goes. function deathSaveInfo(character) { return { state: lifeState(character), successes: character.deathSaveSuccesses, failures: character.deathSaveFailures }; } // Turns a pair of tallies into what gets stored: the third failure is death, // and the third success makes the character stable, which clears the tallies. function settleDeathSaves(successes, failures) { if (failures >= DEATH_SAVE_LIMIT) return { deathSaveSuccesses: successes, deathSaveFailures: DEATH_SAVE_LIMIT, stable: false }; if (successes >= DEATH_SAVE_LIMIT) return { ...NO_DEATH_SAVES, stable: true }; return { deathSaveSuccesses: successes, deathSaveFailures: failures, stable: false }; } // Damage or healing on a finished character. `requestedHp` is the HP that was // asked for, which may be below zero or above max. Returns every column that // should be written: HP and temp HP, plus the death-save columns when the // change touches them. // // - Damage comes off temp HP first. // - Dropping to 0 starts the death saves from a clean slate. // - Damage taken while already at 0 is one failed save, and ends "stable". // - Damage that would leave as much as max HP still to take once HP is at 0 // kills outright. // - Any healing from 0 puts the character back up and clears the tallies. function changeHp(character, requestedHp, maxHp) { const soaked = absorbDamage(character.currentHp, character.tempHp, requestedHp); const after = { currentHp: Math.min(Math.max(soaked.currentHp, 0), maxHp), tempHp: soaked.tempHp }; if (after.currentHp > 0) return { ...after, ...NO_DEATH_SAVES }; const damage = character.currentHp - soaked.currentHp; // what got past temp HP if (damage <= 0) return after; if (-soaked.currentHp >= maxHp) return { ...after, ...settleDeathSaves(0, DEATH_SAVE_LIMIT) }; if (character.currentHp > 0) return { ...after, ...NO_DEATH_SAVES }; return { ...after, ...settleDeathSaves(character.deathSaveSuccesses, character.deathSaveFailures + 1) }; } // A dying character's death save, from what the d20 showed: 10 or more is a // success, less is a failure, a 1 is two failures, and a 20 puts them back on // their feet with 1 HP. function rollDeathSave(character, roll) { if (roll === 20) return { currentHp: 1, ...NO_DEATH_SAVES }; if (roll >= 10) return settleDeathSaves(character.deathSaveSuccesses + 1, character.deathSaveFailures); return settleDeathSaves(character.deathSaveSuccesses, character.deathSaveFailures + (roll === 1 ? 2 : 1)); } module.exports = { absorbDamage, DEATH_SAVE_LIMIT, NO_DEATH_SAVES, lifeState, deathSaveInfo, settleDeathSaves, changeHp, rollDeathSave };