export const MAX_SKILL_LEVEL = 99; /** XP needed to advance from level L to L+1 (+10% per level). */ export const SKILL_XP_BASE = 50; export const SKILL_XP_GROWTH = 1.1; export function xpToAdvanceFromLevel (level) { const lv = Math.max(1, Math.floor(Number(level) || 1)); if (lv >= MAX_SKILL_LEVEL) { return 0; } return Math.max(1, Math.floor(SKILL_XP_BASE * (SKILL_XP_GROWTH ** (lv - 1)))); } /** Total XP required to reach the given level (level 1 = 0 XP). */ export function totalXpForLevel (level) { const target = Math.max(1, Math.floor(Number(level) || 1)); let total = 0; for (let lv = 1; lv < target; lv++) { total += xpToAdvanceFromLevel(lv); } return total; } /** Total XP while still at max level (extra XP is discarded). */ export function maxTotalSkillXp () { const lastAdvance = Math.max( 1, Math.floor(SKILL_XP_BASE * (SKILL_XP_GROWTH ** (MAX_SKILL_LEVEL - 1))) ); return totalXpForLevel(MAX_SKILL_LEVEL) + lastAdvance - 1; } /** Derive level and in-level progress from lifetime XP. */ export function xpProgress (totalXp) { let xp = Math.max(0, Math.floor(Number(totalXp) || 0)); let level = 1; while (level < MAX_SKILL_LEVEL) { const need = xpToAdvanceFromLevel(level); if (xp < need) { return { level, xpInLevel: xp, xpToNext: need }; } xp -= need; level += 1; } return { level: MAX_SKILL_LEVEL, xpInLevel: 0, xpToNext: 0 }; } export function levelFromTotalXp (totalXp) { return xpProgress(totalXp).level; } /** XP from damage dealt — applied to each related combat skill on a hit. */ export function combatDamageXp (damage) { return Math.max(0, Math.floor(Number(damage) || 0)); } /** * XP for a successful spell cast (Magic skill). * Scales with spell difficulty die size. */ export function spellCastXpFromDifficulty (difficulty) { return Math.max(1, Math.floor(Number(difficulty) || 1) * 2); }