// Multiclassing (the 2014 Player’s Handbook rules): a character takes a level // in another class instead of the one they have. Their levels add up to their // character level; hit dice, hit points, class features and counters go by // each class's own level, and spell slots by the levels of every class that // casts. Pure: no DB calls. // // A character's first class is Character.characterClass (with its subclass); // the others are CharacterClassLevel rows. The first class's own level is the // character level less the others'. const { CLASSES } = require('./data/classes'); const { FULL_CASTER_SLOTS, PACT_SLOTS } = require('./data/spellSlots'); const { CLASS_FEATURES } = require('./data/characterOptions'); const { resourceState } = require('./classResources'); const calculations = require('./calculations'); // What a class asks of the scores before anyone can take a level in it (or // leave it for another). Each entry is a list of alternatives; every ability // in an alternative must be 13 or more. const MULTICLASS_PREREQS = { ARTIFICER: [['INT']], BARBARIAN: [['STR']], BARD: [['CHA']], CLERIC: [['WIS']], DRUID: [['WIS']], FIGHTER: [['STR'], ['DEX']], MONK: [['DEX', 'WIS']], PALADIN: [['STR', 'CHA']], RANGER: [['DEX', 'WIS']], ROGUE: [['DEX']], SORCERER: [['CHA']], WARLOCK: [['CHA']], WIZARD: [['INT']], }; const PREREQ_SCORE = 13; const ABILITY_NAMES = { STR: 'Strength', DEX: 'Dexterity', CON: 'Constitution', INT: 'Intelligence', WIS: 'Wisdom', CHA: 'Charisma' }; // "Strength 13 or Dexterity 13", "Dexterity 13 and Wisdom 13". function prereqText(characterClass) { return (MULTICLASS_PREREQS[characterClass] || []) .map((all) => all.map((ability) => `${ABILITY_NAMES[ability]} ${PREREQ_SCORE}`).join(' and ')) .join(' or '); } // Whether final scores ({ STR: 14, ... }) meet a class's requirement. function meetsPrereq(characterClass, scores) { return (MULTICLASS_PREREQS[characterClass] || []).some((all) => all.every((ability) => (scores[ability] || 0) >= PREREQ_SCORE)); } // What a class teaches someone who comes to it from another (the 2014 // multiclassing table), as a line for the special abilities. const MULTICLASS_PROFICIENCIES = { ARTIFICER: 'light and medium armor, shields, thieves’ tools and tinker’s tools', BARBARIAN: 'shields, simple and martial weapons', BARD: 'light armor, one skill of your choice and one musical instrument', CLERIC: 'light and medium armor and shields', DRUID: 'light and medium armor and shields (no metal)', FIGHTER: 'light and medium armor, shields, simple and martial weapons', MONK: 'simple weapons and shortswords', PALADIN: 'light and medium armor, shields, simple and martial weapons', RANGER: 'light and medium armor, shields, simple and martial weapons, and one skill from the ranger’s list', ROGUE: 'light armor, one skill from the rogue’s list, and thieves’ tools', SORCERER: 'nothing new: a sorcerer’s magic is in the blood', WARLOCK: 'light armor and simple weapons', WIZARD: 'nothing new: a wizard’s training is the spellbook', }; // The lines a character's first level in another class writes: what the class // grants at level 1 (multiclassing does not bring its saves or skills), and // what it teaches. function joiningFeatures(characterClass) { return [ ...(CLASS_FEATURES[characterClass] || []), { name: `${CLASSES[characterClass].name} Training`, text: `Multiclassing into ${CLASSES[characterClass].name} teaches ${MULTICLASS_PROFICIENCIES[characterClass]}.` }, ]; } // Every class a character has levels in, the first first: // [{ characterClass, level, subclass, first }]. Takes a character with its // classLevels (an empty list or none: a single class). function classLevels(character) { const others = [...(character.classLevels || [])] .filter((row) => row.level > 0 && row.characterClass !== character.characterClass) .sort((a, b) => new Date(a.createdAt || 0) - new Date(b.createdAt || 0)); const elsewhere = others.reduce((sum, row) => sum + row.level, 0); return [ { characterClass: character.characterClass, level: Math.max(1, (character.level || 1) - elsewhere), subclass: character.subclass || null, first: true }, ...others.map((row) => ({ characterClass: row.characterClass, level: row.level, subclass: row.subclass || null, first: false })), ]; } // The classes a character could put their next level in: those they have // (always), and those whose requirement they and every class they have meet. // `scores` are the final ability scores. function classOptions(character, scores) { const have = classLevels(character); const leaving = have.filter((entry) => !meetsPrereq(entry.characterClass, scores)); return Object.keys(CLASSES).map((characterClass) => { const current = have.find((entry) => entry.characterClass === characterClass); if (current) return { characterClass, level: current.level, eligible: true, problems: [] }; const problems = []; if (!meetsPrereq(characterClass, scores)) problems.push(`${CLASSES[characterClass].name} needs ${prereqText(characterClass)}.`); for (const entry of leaving) problems.push(`Leaving ${CLASSES[entry.characterClass].name} for another class needs ${prereqText(entry.characterClass)}.`); return { characterClass, level: 0, eligible: problems.length === 0, problems }; }); } // The character as it would be with one more level in `characterClass`: the // level and the class rows moved on. For working out the sheet afterwards. function withLevelIn(character, characterClass, subclass = undefined) { const next = { ...character, level: character.level + 1 }; if (characterClass === character.characterClass) { if (subclass !== undefined) next.subclass = subclass; return next; } const rows = [...(character.classLevels || [])]; const at = rows.findIndex((row) => row.characterClass === characterClass); if (at >= 0) rows[at] = { ...rows[at], level: rows[at].level + 1, ...(subclass !== undefined ? { subclass } : {}) }; else rows.push({ characterClass, level: 1, subclass: subclass ?? null, createdAt: new Date() }); next.classLevels = rows; return next; } // Hit points: the first class's die in full at level 1, then each level the // average of its own class's die (rounded up), plus Constitution every level. function multiclassHpMax(classes, conMod) { let total = 0; classes.forEach((entry, i) => { const die = calculations.getClassHitDieSize(entry.characterClass); const average = Math.floor(die / 2) + 1; const levels = entry.level; total += i === 0 ? die + conMod + (levels - 1) * (average + conMod) : levels * (average + conMod); }); return total; } // Hit Dice: one per level, of each class's own size. Spent ones are counted // together; `size` is the first class's, the one rests roll. function multiclassHitDice(classes, used = 0) { const count = classes.reduce((sum, entry) => sum + entry.level, 0); const spent = Math.min(Math.max(Number(used) || 0, 0), count); return { size: calculations.getClassHitDieSize(classes[0].characterClass), count, used: spent, remaining: count - spent, pools: classes.map((entry) => ({ characterClass: entry.characterClass, size: calculations.getClassHitDieSize(entry.characterClass), count: entry.level })), }; } // How many levels a class adds to the shared caster level: all of a full // caster's, half a half caster's (an artificer's rounded up), a third of a // casting subclass's, and none of a warlock's (pact slots stand apart). function casterLevelOf(entry) { const kind = calculations.casterKind(entry.characterClass, entry.subclass); if (kind === 'FULL') return entry.level; if (kind === 'HALF') return entry.characterClass === 'ARTIFICER' ? Math.ceil(entry.level / 2) : Math.floor(entry.level / 2); if (kind === 'THIRD') return Math.floor(entry.level / 3); return 0; } // The spell slots of a multiclassed character (pact slots aside): a single // casting class keeps its own table; two or more add up to a caster level on // the full caster table. A character whose only casting is a warlock's has // those pact slots. Null for none. function multiclassSpellSlots(classes) { const casters = classes.filter((entry) => { const kind = calculations.casterKind(entry.characterClass, entry.subclass); return kind && kind !== 'PACT'; }); if (casters.length === 0) { const warlock = classes.find((entry) => calculations.casterKind(entry.characterClass) === 'PACT'); return warlock ? calculations.spellSlotsFor('WARLOCK', warlock.level) : null; } if (casters.length === 1) return calculations.spellSlotsFor(casters[0].characterClass, casters[0].level, casters[0].subclass); const casterLevel = casters.reduce((sum, entry) => sum + casterLevelOf(entry), 0); if (casterLevel < 1) return null; return { kind: 'FULL', restoresOn: 'LONG', max: [...FULL_CASTER_SLOTS[Math.min(casterLevel, FULL_CASTER_SLOTS.length) - 1]] }; } // A multiclassed warlock's pact slots, kept apart from the others' (they come // back on a short rest): { count, slotLevel }, or null when the warlock's are // the only slots (they are then the sheet's spell slots) or there is none. function pactSlotsOf(classes) { const warlock = classes.find((entry) => calculations.casterKind(entry.characterClass) === 'PACT'); if (!warlock) return null; const others = classes.some((entry) => { const kind = calculations.casterKind(entry.characterClass, entry.subclass); return kind && kind !== 'PACT'; }); if (!others) return null; const [count, slotLevel] = PACT_SLOTS[Math.min(warlock.level, PACT_SLOTS.length) - 1]; return { count, slotLevel }; } // The class counters of every class, each by its own class level. Where two // classes give the same counter (Channel Divinity), the larger one counts. function multiclassResources(classes, resourcesUsed, abilities, profBonus) { const byKey = new Map(); for (const entry of classes) { for (const resource of resourceState({ ...entry, resourcesUsed }, abilities, profBonus)) { const had = byKey.get(resource.key); if (!had || resource.max > had.max) byKey.set(resource.key, resource); } } return [...byKey.values()]; } // Added to (not replacing) the exports object: calculations.js holds it from // before this file has finished loading. Object.assign(module.exports, { MULTICLASS_PREREQS, PREREQ_SCORE, prereqText, meetsPrereq, joiningFeatures, classLevels, classOptions, withLevelIn, multiclassHpMax, multiclassHitDice, casterLevelOf, multiclassSpellSlots, pactSlotsOf, multiclassResources, });