BlueGit

timfa questward

Files

questward / shared / skills / progression.js

progression.js

progression.js 1.5 KiB Download Raw
1export const MAX_SKILL_LEVEL = 99;
2
3/** XP needed to advance from level L to L+1 (doubles each level). */
4export const SKILL_XP_BASE = 50;
5export const SKILL_XP_GROWTH = 2;
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 return totalXpForLevel(MAX_SKILL_LEVEL + 1) - 1;
29}
30
31/** Derive level and in-level progress from lifetime XP. */
32export function xpProgress (totalXp)
33{
34 let xp = Math.max(0, Math.floor(Number(totalXp) || 0));
35 let level = 1;
36 while (level < MAX_SKILL_LEVEL)
37 {
38 const need = xpToAdvanceFromLevel(level);
39 if (xp < need)
40 {
41 return {
42 level,
43 xpInLevel: xp,
44 xpToNext: need
45 };
46 }
47 xp -= need;
48 level += 1;
49 }
50 return {
51 level: MAX_SKILL_LEVEL,
52 xpInLevel: 0,
53 xpToNext: 0
54 };
55}
56
57export function levelFromTotalXp (totalXp)
58{
59 return xpProgress(totalXp).level;
60}
61
62/** Split combat hit XP equally between hit and damage skills. */
63export const COMBAT_HIT_XP_TOTAL = 10;
64
65export function combatHitXpPerSkill ()
66{
67 return COMBAT_HIT_XP_TOTAL / 2;
68}