- With the campaign setting on, the level-up checklist asks which class gains the level; a new class needs both classes' ability requirements (2014 rules) - HP, Hit Dice, features, Boons, subclasses and counters go by each class's own level; spell slots add up to a caster level, pact slots apart - A first level in a new class writes its level-1 gifts and what it teaches - Eldritch Knight and Arcane Trickster cast from level 3 with real slots - The sheet shows each class's road to 20 and suggested multiclass routes (Hexadin, Sorlock, fighter dip...) with the character's progress and scores Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
304 lines
15 KiB
JavaScript
304 lines
15 KiB
JavaScript
// Levelling up: the Hollowcrown book's "Levelling-Up Checklist" (Rules Cheat
|
||
// Sheet). Works out what a character gains at a level, and rewrites their
|
||
// special abilities to say so. Pure: no DB calls.
|
||
|
||
const { LEVEL_FEATURES, MAX_FEATURE_LEVEL, CANTRIP_LEVEL, CANTRIP_FEATURE, BOONS } = require('./data/levelFeatures');
|
||
const { SUBCLASS_LEVEL, subclassesOf, findSubclass, defaultSubclass } = require('./data/subclasses');
|
||
const { CLASS_FEATURES } = require('./data/characterOptions');
|
||
const { CASTER_KIND } = require('./data/spellSlots');
|
||
const { getClassSpellcastingAbility, maxSpellLevel, scaledDamageDice, MAX_GEAR_LEVEL } = require('./calculations');
|
||
const { classLevels, classOptions, joiningFeatures } = require('./multiclass');
|
||
|
||
// A choice that cannot be made: the message is for whoever made it.
|
||
class LevelUpError extends Error {}
|
||
|
||
// What a class gains on reaching `level`, in the order it is written down:
|
||
// the class's own features, then those of the subclass (its key) when one is
|
||
// given. An entry is { name, text, upgrade? } or { boon: true }.
|
||
function featuresAt(characterClass, level, subclassKey = null) {
|
||
const features = [...((LEVEL_FEATURES[characterClass] || {})[level] || [])];
|
||
const subclass = findSubclass(characterClass, subclassKey);
|
||
if (subclass) features.push(...(subclass.features[level] || []));
|
||
if (level === CANTRIP_LEVEL && getClassSpellcastingAbility(characterClass)) features.push(CANTRIP_FEATURE);
|
||
return features;
|
||
}
|
||
|
||
// Special abilities are one per line, "Name — what it does". The builder
|
||
// writes a long dash; a DM typing by hand may use "--".
|
||
const featureLine = (feature) => `${feature.name} — ${feature.text}`;
|
||
|
||
function lineName(line) {
|
||
const match = /^\s*(.+?)\s*(?:—|–|--)\s/.exec(line);
|
||
return match ? match[1].toLowerCase() : null;
|
||
}
|
||
|
||
const toLines = (specialAbilities) => (specialAbilities ? specialAbilities.split(/\r?\n/) : []);
|
||
const fromLines = (lines) => lines.join('\n') || null;
|
||
const findLine = (lines, name) => lines.findIndex((line) => lineName(line) === name.toLowerCase());
|
||
|
||
// Writes features into the text. An upgrade takes the place of the line with
|
||
// its name (Sneak Attack growing a die); anything else goes on the end, unless
|
||
// that very line is there already.
|
||
function addFeatures(specialAbilities, features) {
|
||
const lines = toLines(specialAbilities);
|
||
for (const feature of features) {
|
||
const line = featureLine(feature);
|
||
const at = feature.upgrade ? findLine(lines, feature.name) : -1;
|
||
if (at >= 0) lines[at] = line;
|
||
else if (!lines.includes(line)) lines.push(line);
|
||
}
|
||
return fromLines(lines);
|
||
}
|
||
|
||
// ---------------------------------------------------------------------------
|
||
// Subclasses (data/subclasses.js). A class chooses one at SUBCLASS_LEVEL. Until
|
||
// it has, a line on the sheet says the choice is owed.
|
||
// ---------------------------------------------------------------------------
|
||
|
||
const SUBCLASS_OWED = 'Subclass';
|
||
|
||
// Whether a class at this level should have a subclass.
|
||
function owesSubclass(characterClass, level) {
|
||
return subclassesOf(characterClass).length > 0 && level >= SUBCLASS_LEVEL[characterClass];
|
||
}
|
||
|
||
const subclassOwedFeature = (characterClass) => ({
|
||
name: SUBCLASS_OWED,
|
||
text: `Choose one of the ${subclassesOf(characterClass).length} with your DM (the level-up checklist lists them all).`,
|
||
});
|
||
|
||
// Everything a subclass has given by `level`, in the order it came.
|
||
function subclassFeaturesThrough(subclass, level) {
|
||
return Object.keys(subclass.features)
|
||
.map(Number)
|
||
.filter((at) => at <= level)
|
||
.sort((a, b) => a - b)
|
||
.flatMap((at) => subclass.features[at]);
|
||
}
|
||
|
||
// Writes a newly chosen subclass onto the sheet: everything it has given up
|
||
// to the character's level, in place of the line saying a choice is owed.
|
||
function withSubclass(specialAbilities, characterClass, level, subclassKey) {
|
||
const subclass = findSubclass(characterClass, subclassKey);
|
||
if (!subclass) throw new LevelUpError('That is not a subclass of this class.');
|
||
const lines = toLines(specialAbilities).filter((line) => lineName(line) !== SUBCLASS_OWED.toLowerCase());
|
||
return addFeatures(fromLines(lines), subclassFeaturesThrough(subclass, level));
|
||
}
|
||
|
||
// Takes a subclass's lines back off the sheet, found by their names. A line
|
||
// the class itself also writes under that name is left alone.
|
||
function withoutSubclass(specialAbilities, characterClass, subclassKey) {
|
||
const subclass = findSubclass(characterClass, subclassKey);
|
||
if (!subclass) return specialAbilities;
|
||
const classNames = new Set(
|
||
[...(CLASS_FEATURES[characterClass] || []), ...Object.values(LEVEL_FEATURES[characterClass] || {}).flat()]
|
||
.filter((feature) => feature.name)
|
||
.map((feature) => feature.name.toLowerCase())
|
||
);
|
||
const names = new Set(
|
||
Object.values(subclass.features)
|
||
.flat()
|
||
.map((feature) => feature.name.toLowerCase())
|
||
.filter((name) => !classNames.has(name))
|
||
);
|
||
return fromLines(toLines(specialAbilities).filter((line) => !names.has(lineName(line))));
|
||
}
|
||
|
||
// The DM changing (or setting, or clearing) a character's subclass after the
|
||
// fact: the old one's lines go, the new one's are written.
|
||
function switchSubclass(specialAbilities, characterClass, level, fromKey, toKey) {
|
||
const cleared = withoutSubclass(specialAbilities, characterClass, fromKey);
|
||
if (toKey) return withSubclass(cleared, characterClass, level, toKey);
|
||
return owesSubclass(characterClass, level) ? addFeatures(cleared, [subclassOwedFeature(characterClass)]) : cleared;
|
||
}
|
||
|
||
// The subclasses a character may pick from on reaching `level`, each with
|
||
// what it would write; null when there is no choice to make (too low a level,
|
||
// or one is chosen already).
|
||
function subclassChoice(characterClass, level, currentKey) {
|
||
if (!owesSubclass(characterClass, level) || findSubclass(characterClass, currentKey)) return null;
|
||
return {
|
||
level: SUBCLASS_LEVEL[characterClass],
|
||
options: subclassesOf(characterClass).map((subclass) => ({
|
||
key: subclass.key,
|
||
name: subclass.name,
|
||
summary: subclass.summary,
|
||
features: subclassFeaturesThrough(subclass, level).map(({ name, text }) => ({ name, text })),
|
||
})),
|
||
};
|
||
}
|
||
|
||
// How many times a Boon has been taken, read off its line ("Hardy — +10
|
||
// maximum HP…" is twice).
|
||
function boonTaken(specialAbilities, boon) {
|
||
const lines = toLines(specialAbilities);
|
||
const at = findLine(lines, boon.name);
|
||
if (at < 0) return 0;
|
||
const amount = /\+\s*(\d+)/.exec(lines[at]);
|
||
return Math.max(1, Math.round((amount ? Number(amount[1]) : boon.per) / boon.per));
|
||
}
|
||
|
||
// Every Boon, with what taking it now would write and whether it still can be.
|
||
function boonOptions(specialAbilities) {
|
||
return BOONS.map((boon) => {
|
||
const taken = boonTaken(specialAbilities, boon);
|
||
return { key: boon.key, name: boon.name, taken, max: boon.max, available: taken < boon.max, text: boon.text((Math.min(taken, boon.max - 1) + 1) * boon.per) };
|
||
});
|
||
}
|
||
|
||
// Temp HP the Hardy Boon gives after each long rest; 0 without it.
|
||
function hardyTempHp(specialAbilities) {
|
||
const hardy = BOONS.find((boon) => boon.key === 'HARDY');
|
||
return boonTaken(specialAbilities, hardy) * hardy.per;
|
||
}
|
||
|
||
// What reaching `level` does to a character's special abilities. `boonKey` is
|
||
// the Boon picked, at a level that grants one; without it the line says a
|
||
// choice is still owed. `subclass` is { key, chosen }: the character's
|
||
// subclass, and whether it is being picked at this very level (then everything
|
||
// it has given so far is written). Without one, from the level the class
|
||
// chooses at, a line says that choice is owed too. Also returns what the Boon
|
||
// does beyond the text: how much Extra AC it adds, and the Temp HP it gives
|
||
// straight away.
|
||
function advance(specialAbilities, characterClass, level, boonKey, { key: subclassKey = null, chosen = false } = {}) {
|
||
const entries = featuresAt(characterClass, level, chosen ? null : subclassKey);
|
||
let text = addFeatures(specialAbilities, entries.filter((entry) => !entry.boon));
|
||
if (chosen) text = withSubclass(text, characterClass, level, subclassKey);
|
||
else if (!findSubclass(characterClass, subclassKey) && owesSubclass(characterClass, level)) text = addFeatures(text, [subclassOwedFeature(characterClass)]);
|
||
const result = { armorBonus: 0, tempHp: 0 };
|
||
|
||
if (entries.some((entry) => entry.boon)) {
|
||
if (!boonKey) {
|
||
text = addFeatures(text, [{ name: `Boon (level ${level})`, text: `Choose one with your DM: ${BOONS.map((boon) => boon.name).join(', ')}.` }]);
|
||
} else {
|
||
const boon = BOONS.find((b) => b.key === boonKey);
|
||
if (!boon) throw new LevelUpError('That is not one of the Boons.');
|
||
const taken = boonTaken(text, boon);
|
||
if (taken >= boon.max) {
|
||
throw new LevelUpError(`${boon.name} can be taken ${boon.max === 1 ? 'only once' : `at most ${boon.max} times`}. Pick a different Boon.`);
|
||
}
|
||
const amount = (taken + 1) * boon.per;
|
||
text = addFeatures(text, [{ name: boon.name, text: boon.text(amount), upgrade: true }]);
|
||
if (boon.key === 'TOUGHENED') result.armorBonus = 1;
|
||
if (boon.key === 'HARDY') result.tempHp = amount;
|
||
}
|
||
}
|
||
return { specialAbilities: text, ...result };
|
||
}
|
||
|
||
// The book's unlock schedule: what a caster may be granted on reaching
|
||
// `level`. Null for a class with no spells.
|
||
function spellsGainedAt(characterClass, level) {
|
||
const kind = CASTER_KIND[characterClass];
|
||
if (!kind) return null;
|
||
const highest = maxSpellLevel(characterClass, level);
|
||
// Half casters: two spells at level 2, then one at each odd level.
|
||
const halfCasterSpells = level === 2 ? 2 : level > 2 && level % 2 === 1 ? 1 : 0;
|
||
return {
|
||
maxSpellLevel: highest,
|
||
// Set when this level opens up a new level of spells.
|
||
unlocksSpellLevel: highest > maxSpellLevel(characterClass, level - 1) ? highest : null,
|
||
newSpells: kind === 'HALF' ? halfCasterSpells : characterClass === 'WIZARD' ? 2 : 1,
|
||
newCantrips: kind !== 'HALF' && (level === 4 || level === 10) ? 1 : 0,
|
||
};
|
||
}
|
||
|
||
// In a campaign with subclasses switched off nobody chooses: on reaching the
|
||
// level their class would choose at, a character is put on the class's first
|
||
// subclass. Returns that subclass's key when this is the moment, else null.
|
||
function subclassByDefault(characterClass, level, currentKey) {
|
||
if (!owesSubclass(characterClass, level) || findSubclass(characterClass, currentKey)) return null;
|
||
return defaultSubclass(characterClass).key;
|
||
}
|
||
|
||
// The checklist for a character's next level. Takes the character with its
|
||
// weapons and class levels loaded (each weapon with its pool entry).
|
||
// `subclasses` is the campaign's setting: off, there is no subclass to pick,
|
||
// and the class's first one is taken without asking (`subclassTaken`), its
|
||
// features listed with the rest. `multiclass` (the campaign's setting) lists
|
||
// the classes the level could go to (`classChoice`, by the final ability
|
||
// `scores`), and `toClass` is the one it goes to: the character's first class
|
||
// unless another is named. Features, Boons, spells and the subclass all go by
|
||
// that class's own level.
|
||
function levelUpPlan(character, { subclasses = true, multiclass = false, toClass = null, scores = {} } = {}) {
|
||
const toLevel = character.level + 1;
|
||
const characterClass = toClass || character.characterClass;
|
||
const entry = classLevels(character).find((e) => e.characterClass === characterClass) || { characterClass, level: 0, subclass: null };
|
||
const classLevel = entry.level + 1;
|
||
const joining = entry.level === 0;
|
||
const taken = subclasses || joining ? null : subclassByDefault(characterClass, classLevel, entry.subclass);
|
||
const entries = joining
|
||
? joiningFeatures(characterClass)
|
||
: [
|
||
...featuresAt(characterClass, classLevel, taken ? null : entry.subclass),
|
||
...(taken ? subclassFeaturesThrough(findSubclass(characterClass, taken), classLevel) : []),
|
||
];
|
||
return {
|
||
fromLevel: character.level,
|
||
toLevel,
|
||
// The class taking the level, and its own level once it has.
|
||
characterClass,
|
||
classLevel,
|
||
// True when this is the character's first level in this class.
|
||
joining,
|
||
classChoice: multiclass ? { options: classOptions(character, scores) } : null,
|
||
// Set when a subclass has to be picked on the way to this level.
|
||
subclass: subclasses ? subclassChoice(characterClass, classLevel, entry.subclass) : null,
|
||
subclassTaken: taken,
|
||
features: entries.filter((e) => !e.boon).map(({ name, text, upgrade }) => ({ name, text, upgrade: Boolean(upgrade) })),
|
||
boon: entries.some((e) => e.boon) ? boonOptions(character.specialAbilities) : null,
|
||
spells: spellsGainedAt(characterClass, classLevel),
|
||
// The free upgrade: one weapon of standard growth goes up a level, never
|
||
// past its holder.
|
||
weapons: (character.weapons || [])
|
||
.filter((cw) => cw.weapon.damageScaling === 1 && cw.level < Math.min(toLevel, MAX_GEAR_LEVEL))
|
||
.map((cw) => ({
|
||
weaponId: cw.weaponId,
|
||
name: cw.weapon.name,
|
||
equipped: cw.equipped,
|
||
level: cw.level,
|
||
damageDice: scaledDamageDice(cw.weapon.damageDice, cw.level, cw.weapon.damageScaling, cw.weapon.minLevel),
|
||
nextDamageDice: scaledDamageDice(cw.weapon.damageDice, cw.level + 1, cw.weapon.damageScaling, cw.weapon.minLevel),
|
||
})),
|
||
// The class tables stop at level 20; past it only the numbers move.
|
||
beyondTables: classLevel > MAX_FEATURE_LEVEL,
|
||
};
|
||
}
|
||
|
||
// What taking a planned level does to a character's special abilities (see
|
||
// advance): a first level in a new class writes what that class grants at
|
||
// level 1 and teaches; any other goes through advance at the class's own level.
|
||
function advanceIn(specialAbilities, plan, boonKey, { key = null, chosen = false } = {}) {
|
||
if (plan.joining) return { specialAbilities: addFeatures(specialAbilities, joiningFeatures(plan.characterClass)), armorBonus: 0, tempHp: 0 };
|
||
return advance(specialAbilities, plan.characterClass, plan.classLevel, boonKey, { key, chosen });
|
||
}
|
||
|
||
// The road from level 1 to 20 for a class (and subclass): what every level
|
||
// gives, as the progression timeline shows it. Level 1 is what the builder
|
||
// writes; a level that grants a Boon says so. `fromSubclass` marks the
|
||
// subclass's own lines.
|
||
function progression(characterClass, subclassKey = null) {
|
||
const subclass = findSubclass(characterClass, subclassKey);
|
||
const levels = [];
|
||
for (let level = 1; level <= MAX_FEATURE_LEVEL; level++) {
|
||
const own = level === 1 ? CLASS_FEATURES[characterClass] || [] : featuresAt(characterClass, level);
|
||
const fromSubclass = subclass ? subclass.features[level] || [] : [];
|
||
const slots = maxSpellLevel(characterClass, level);
|
||
levels.push({
|
||
level,
|
||
features: [
|
||
...own.filter((entry) => !entry.boon).map(({ name, text }) => ({ name, text, fromSubclass: false })),
|
||
...fromSubclass.map(({ name, text }) => ({ name, text, fromSubclass: true })),
|
||
],
|
||
boon: own.some((entry) => entry.boon),
|
||
// The level a subclass is chosen, when none is yet.
|
||
choosesSubclass: !subclass && subclassesOf(characterClass).length > 0 && level === SUBCLASS_LEVEL[characterClass],
|
||
// Set when this level opens up a new level of spells.
|
||
unlocksSpellLevel: slots > (level === 1 ? 0 : maxSpellLevel(characterClass, level - 1)) ? slots : null,
|
||
});
|
||
}
|
||
return levels;
|
||
}
|
||
|
||
module.exports = { LevelUpError, progression, advanceIn, featuresAt, owesSubclass, subclassChoice, subclassByDefault, switchSubclass, addFeatures, boonTaken, boonOptions, hardyTempHp, advance, spellsGainedAt, levelUpPlan };
|