// Class counters: the things a class can do a set number of times before a // rest (Rage, Ki, Channel Divinity...). How many there are comes from class, // subclass, level and ability modifiers; the character row keeps only how // many are spent (`resourcesUsed`, { key: used }), as it does for spell slots. // Pure: no DB calls. // // What a counter does is on the sheet in the special abilities; nothing here // feeds another number. const atLeastOne = (n) => Math.max(1, n); // Each entry is offered to the classes (and, where set, the subclass) it // names, from `from` level on (and up to `until`, when it stops being // counted). `max` gets { level, mods, profBonus }, where // `mods` is the ability modifiers by ability. `rest` is the rest that gives // them back: a SHORT one comes back on a long rest too. const RESOURCES = [ // At level 20 a barbarian rages as often as they like: no counter to keep. { key: 'RAGE', name: 'Rage', classes: ['BARBARIAN'], max: ({ level }) => (level >= 17 ? 6 : level >= 12 ? 5 : level >= 6 ? 4 : level >= 3 ? 3 : 2), rest: 'LONG', until: 19 }, { key: 'ZEALOUS_PRESENCE', name: 'Zealous Presence', classes: ['BARBARIAN'], subclass: 'ZEALOT', from: 10, max: () => 1, rest: 'LONG' }, // Font of Inspiration: from level 5 it comes back on a short rest. { key: 'BARDIC_INSPIRATION', name: 'Bardic Inspiration', classes: ['BARD'], max: ({ mods }) => atLeastOne(mods.CHA), rest: ({ level }) => (level >= 5 ? 'SHORT' : 'LONG') }, { key: 'HEALING_HANDS', name: 'Healing Hands', classes: ['CLERIC'], max: () => 1, rest: 'SHORT' }, { key: 'CHANNEL_DIVINITY', name: 'Channel Divinity', classes: ['CLERIC'], from: 2, max: ({ level }) => (level >= 18 ? 3 : level >= 6 ? 2 : 1), rest: 'SHORT' }, { key: 'CHANNEL_DIVINITY', name: 'Channel Divinity', classes: ['PALADIN'], from: 3, max: () => 1, rest: 'SHORT' }, { key: 'WARDING_FLARE', name: 'Warding Flare', classes: ['CLERIC'], subclass: 'LIGHT', from: 2, max: ({ mods }) => atLeastOne(mods.WIS), rest: 'LONG' }, { key: 'WAR_PRIEST', name: 'War Priest', classes: ['CLERIC'], subclass: 'WAR', from: 2, max: ({ mods }) => atLeastOne(mods.WIS), rest: 'LONG' }, // An archdruid (level 20) wild shapes as often as they like. { key: 'WILD_SHAPE', name: 'Wild Shape', classes: ['DRUID'], from: 2, max: () => 2, rest: 'SHORT', until: 19 }, { key: 'NATURAL_RECOVERY', name: 'Natural Recovery', classes: ['DRUID'], subclass: 'LAND', from: 2, max: () => 1, rest: 'LONG' }, { key: 'STAR_MAP', name: 'Star Map (Guiding Bolt)', classes: ['DRUID'], subclass: 'STARS', from: 2, max: ({ profBonus }) => profBonus, rest: 'LONG' }, { key: 'COSMIC_OMEN', name: 'Cosmic Omen', classes: ['DRUID'], subclass: 'STARS', from: 6, max: ({ profBonus }) => profBonus, rest: 'LONG' }, { key: 'SECOND_WIND', name: 'Second Wind', classes: ['FIGHTER'], max: () => 1, rest: 'SHORT' }, { key: 'ACTION_SURGE', name: 'Action Surge', classes: ['FIGHTER'], from: 2, max: ({ level }) => (level >= 17 ? 2 : 1), rest: 'SHORT' }, { key: 'INDOMITABLE', name: 'Indomitable', classes: ['FIGHTER'], from: 9, max: ({ level }) => (level >= 17 ? 3 : level >= 13 ? 2 : 1), rest: 'LONG' }, { key: 'SUPERIORITY_DICE', name: 'Superiority Dice', classes: ['FIGHTER'], subclass: 'BATTLE_MASTER', from: 3, max: ({ level }) => (level >= 15 ? 6 : level >= 7 ? 5 : 4), rest: 'SHORT' }, { key: 'FIGHTING_SPIRIT', name: 'Fighting Spirit', classes: ['FIGHTER'], subclass: 'SAMURAI', from: 3, max: () => 3, rest: 'LONG' }, { key: 'KI', name: 'Ki', classes: ['MONK'], from: 2, max: ({ level }) => level, rest: 'SHORT' }, { key: 'WHOLENESS_OF_BODY', name: 'Wholeness of Body', classes: ['MONK'], subclass: 'OPEN_HAND', from: 6, max: () => 1, rest: 'LONG' }, { key: 'LAY_ON_HANDS', name: 'Lay on Hands (HP)', classes: ['PALADIN'], max: ({ level }) => 5 * level, rest: 'LONG' }, { key: 'DIVINE_SENSE', name: 'Divine Sense', classes: ['PALADIN'], max: ({ mods }) => atLeastOne(1 + mods.CHA), rest: 'LONG' }, { key: 'CLEANSING_TOUCH', name: 'Cleansing Touch', classes: ['PALADIN'], from: 14, max: ({ mods }) => atLeastOne(mods.CHA), rest: 'LONG' }, { key: 'WILD_TALENT', name: 'Wild Talent', classes: ['SORCERER'], max: () => 1, rest: 'LONG' }, { key: 'SORCERY_POINTS', name: 'Sorcery Points', classes: ['SORCERER'], from: 2, max: ({ level }) => level, rest: 'LONG' }, { key: 'TIDES_OF_CHAOS', name: 'Tides of Chaos', classes: ['SORCERER'], subclass: 'WILD_MAGIC', from: 3, max: () => 1, rest: 'LONG' }, { key: 'FEY_PRESENCE', name: 'Fey Presence', classes: ['WARLOCK'], subclass: 'ARCHFEY', from: 3, max: () => 1, rest: 'SHORT' }, { key: 'MISTY_ESCAPE', name: 'Misty Escape', classes: ['WARLOCK'], subclass: 'ARCHFEY', from: 6, max: () => 1, rest: 'SHORT' }, { key: 'PATRONS_LUCK', name: 'Patron’s Luck', classes: ['WARLOCK'], subclass: 'FIEND', from: 6, max: () => 1, rest: 'SHORT' }, { key: 'ENTROPIC_WARD', name: 'Entropic Ward', classes: ['WARLOCK'], subclass: 'GREAT_OLD_ONE', from: 6, max: () => 1, rest: 'SHORT' }, { key: 'ARCANE_RECOVERY', name: 'Arcane Recovery', classes: ['WIZARD'], max: () => 1, rest: 'LONG' }, { key: 'SIGNATURE_SPELLS', name: 'Signature Spells', classes: ['WIZARD'], from: 20, max: () => 2, rest: 'SHORT' }, { key: 'STROKE_OF_LUCK', name: 'Stroke of Luck', classes: ['ROGUE'], from: 20, max: () => 1, rest: 'SHORT' }, { key: 'MYSTIC_ARCANUM', name: 'Mystic Arcanum', classes: ['WARLOCK'], from: 11, max: ({ level }) => (level >= 17 ? 4 : level >= 15 ? 3 : level >= 13 ? 2 : 1), rest: 'LONG' }, { key: 'FLASH_OF_GENIUS', name: 'Flash of Genius', classes: ['ARTIFICER'], from: 7, max: ({ mods }) => atLeastOne(mods.INT), rest: 'LONG' }, { key: 'ILLUSORY_SELF', name: 'Illusory Self', classes: ['WIZARD'], subclass: 'ILLUSION', from: 10, max: () => 1, rest: 'SHORT' }, ]; // The counters a character has, with how many of each are spent and left: // [{ key, name, max, used, remaining, restoresOn }]. `used` is the stored // tally, which may name counters the character no longer has or hold more // than they now allow. function resourceState({ characterClass, subclass = null, level = 1, resourcesUsed = {} }, abilities, profBonus) { const mods = Object.fromEntries(Object.entries(abilities).map(([ability, detail]) => [ability, detail.modifier])); const context = { level, mods, profBonus }; const tally = resourcesUsed && typeof resourcesUsed === 'object' ? resourcesUsed : {}; return RESOURCES.filter( (resource) => resource.classes.includes(characterClass) && level >= (resource.from || 1) && level <= (resource.until || Infinity) && (!resource.subclass || resource.subclass === subclass) ).map((resource) => { const max = resource.max(context); const used = Math.min(Math.max(Math.round(Number(tally[resource.key]) || 0), 0), max); return { key: resource.key, name: resource.name, max, used, remaining: max - used, restoresOn: typeof resource.rest === 'function' ? resource.rest(context) : resource.rest, }; }); } // The tally to store, from a character's counters: only what is spent. function resourceTally(resources) { return Object.fromEntries(resources.filter((resource) => resource.used > 0).map((resource) => [resource.key, resource.used])); } // What a rest gives back: the tally afterwards, and the names of the counters // that were refilled. A long rest refills every counter; a short rest those // that restore on one. function restResources(resources, restType) { const refills = (resource) => resource.used > 0 && (restType === 'LONG' || resource.restoresOn === 'SHORT'); return { resourcesUsed: resourceTally(resources.filter((resource) => !refills(resource))), restored: resources.filter(refills).map((resource) => resource.name), }; } const MAX_EXHAUSTION = 6; module.exports = { RESOURCES, resourceState, resourceTally, restResources, MAX_EXHAUSTION };