| 1 | export const MAX_SKILL_LEVEL = 99; |
| 2 | |
| 3 | /** XP needed to advance from level L to L+1 (+10% per level). */ |
| 4 | export const SKILL_XP_BASE = 50; |
| 5 | export const SKILL_XP_GROWTH = 1.1; |
| 6 | |
| 7 | export function xpToAdvanceFromLevel (level) |
| 8 | { |
| 9 | const lv = Math.max(1, Math.floor(Number(level) || 1)); |
| 10 | if (lv >= MAX_SKILL_LEVEL) |
| 11 | { return 0; } |
| 12 | return Math.max(1, Math.floor(SKILL_XP_BASE * (SKILL_XP_GROWTH ** (lv - 1)))); |
| 13 | } |
| 14 | |
| 15 | /** Total XP required to reach the given level (level 1 = 0 XP). */ |
| 16 | export function totalXpForLevel (level) |
| 17 | { |
| 18 | const target = Math.max(1, Math.floor(Number(level) || 1)); |
| 19 | let total = 0; |
| 20 | for (let lv = 1; lv < target; lv++) |
| 21 | { total += xpToAdvanceFromLevel(lv); } |
| 22 | return total; |
| 23 | } |
| 24 | |
| 25 | /** Total XP while still at max level (extra XP is discarded). */ |
| 26 | export function maxTotalSkillXp () |
| 27 | { |
| 28 | const lastAdvance = Math.max( |
| 29 | 1, |
| 30 | Math.floor(SKILL_XP_BASE * (SKILL_XP_GROWTH ** (MAX_SKILL_LEVEL - 1))) |
| 31 | ); |
| 32 | return totalXpForLevel(MAX_SKILL_LEVEL) + lastAdvance - 1; |
| 33 | } |
| 34 | |
| 35 | /** Derive level and in-level progress from lifetime XP. */ |
| 36 | export function xpProgress (totalXp) |
| 37 | { |
| 38 | let xp = Math.max(0, Math.floor(Number(totalXp) || 0)); |
| 39 | let level = 1; |
| 40 | while (level < MAX_SKILL_LEVEL) |
| 41 | { |
| 42 | const need = xpToAdvanceFromLevel(level); |
| 43 | if (xp < need) |
| 44 | { |
| 45 | return { |
| 46 | level, |
| 47 | xpInLevel: xp, |
| 48 | xpToNext: need |
| 49 | }; |
| 50 | } |
| 51 | xp -= need; |
| 52 | level += 1; |
| 53 | } |
| 54 | return { |
| 55 | level: MAX_SKILL_LEVEL, |
| 56 | xpInLevel: 0, |
| 57 | xpToNext: 0 |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | export function levelFromTotalXp (totalXp) |
| 62 | { |
| 63 | return xpProgress(totalXp).level; |
| 64 | } |
| 65 | |
| 66 | /** XP from damage dealt — applied to each related combat skill on a hit. */ |
| 67 | export function combatDamageXp (damage) |
| 68 | { |
| 69 | return Math.max(0, Math.floor(Number(damage) || 0)); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * XP for a successful spell cast (Magic skill). |
| 74 | * Scales with spell difficulty die size. |
| 75 | */ |
| 76 | export function spellCastXpFromDifficulty (difficulty) |
| 77 | { |
| 78 | return Math.max(1, Math.floor(Number(difficulty) || 1) * 2); |
| 79 | } |