BlueGit

timfa questward

Files

questward / shared / skills / progression.js

progression.js

progression.js 1.8 KiB Download Raw
1export const MAX_SKILL_LEVEL = 99;
2
3/** XP needed to advance from level L to L+1 (+10% per level). */
4export const SKILL_XP_BASE = 50;
5export const SKILL_XP_GROWTH = 1.1;
6
7export 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). */
16export 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). */
26export 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. */
36export 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
61export 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. */
67export 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 */
76export function spellCastXpFromDifficulty (difficulty)
77{
78 return Math.max(1, Math.floor(Number(difficulty) || 1) * 2);
79}