Monday, August 26, 2019

c++ - How to create adjustable formula for RPG level up requirements?


I'm trying to create a formula that can be modified simply by changing two values: number_of_levels, and last_level_experience. This is to enable people modding the game to change the levelling requirements.


I've got it so that I can specify the number of XP needed for the last level up, but I want to be able to control the XP needed for the first level up, which in this case can differ wildly. For example, if I have 40 levels, and 1,000,000 XP for the last level, the first level up requirement is then 625. But if I change the levels to 80, the first level up becomes 156. In both cases, the last level needs 1,000,000.


There must be some way to get the computer to work out a suitable curve given just these two basic values.


#include 

int main()
{
int levels = 40;

if (levels < 2) levels = 2;

int experience_for_last_level = 1e6;
float fraction = 1.0 / levels;

{
int i = 0;
float fraction_counter = fraction;
int counter = levels;
int total = 0;


for (i = 1; i <= levels; ++i, fraction_counter += fraction, --counter)
{
int a = static_cast(fraction_counter * experience_for_last_level / counter);

std::cout <<"Level "<
total += a;
}


std::cout << "\nTotal Exp: " << total;
}
}

Output:


Level 1:  625   (40)      Level 15: 14423  (26)      Level 29: 60416  (12)
Level 2: 1282 (39) Level 16: 16000 (25) Level 30: 68181 (11)
Level 3: 1973 (38) Level 17: 17708 (24) Level 31: 77499 (10)
Level 4: 2702 (37) Level 18: 19565 (23) Level 32: 88888 (9)
Level 5: 3472 (36) Level 19: 21590 (22) Level 33: 103124 (8)

Level 6: 4285 (35) Level 20: 23809 (21) Level 34: 121428 (7)
Level 7: 5147 (34) Level 21: 26250 (20) Level 35: 145833 (6)
Level 8: 6060 (33) Level 22: 28947 (19) Level 36: 179999 (5)
Level 9: 7031 (32) Level 23: 31944 (18) Level 37: 231249 (4)
Level 10: 8064 (31) Level 24: 35294 (17) Level 38: 316666 (3)
Level 11: 9166 (30) Level 25: 39062 (16) Level 39: 487499 (2)
Level 12: 10344 (29) Level 26: 43333 (15) Level 40: 999999 (1)
Level 13: 11607 (28) Level 27: 48214 (14)
Level 14: 12962 (27) Level 28: 53846 (13)

Answer




Though there are infinitely many ways to choose them, it is common for leveling curves to follow a power rule such as the following one:


f(level) == A * exp(B * level)

The major advantage of this formula can be easily explained: for a given rule, there is a fixed value N such that each level costs N percent more than the previous one.


Your initial variables add the following restrictions:


f(1) - f(0) == experience_for_first_level
f(levels) - f(levels - 1) == experience_for_last_level

Two equations, two unknowns. This looks good. Simple maths give A and B:


B = log(experience_for_last_level / experience_for_first_level) / (levels - 1);

A = experience_for_first_level / (exp(B) - 1);

Resulting in the following code:


#include 
#include

int main(void)
{
int levels = 40;
int xp_for_first_level = 1000;

int xp_for_last_level = 1000000;

double B = log((double)xp_for_last_level / xp_for_first_level) / (levels - 1);
double A = (double)xp_for_first_level / (exp(B) - 1.0);

for (int i = 1; i <= levels; i++)
{
int old_xp = round(A * exp(B * (i - 1)));
int new_xp = round(A * exp(B * i));
std::cout << i << " " << (new_xp - old_xp) << std::endl;

}
}

And the following output:


1 1000          9 4125          17 17012        25 70170        33 289427
2 1193 10 4924 18 20309 26 83768 34 345511
3 1425 11 5878 19 24245 27 100000 35 412462
4 1702 12 7017 20 28943 28 119378 36 492389
5 2031 13 8377 21 34551 29 142510 37 587801
6 2424 14 10000 22 41246 30 170125 38 701704

7 2894 15 11938 23 49239 31 203092 39 837678
8 3455 16 14251 24 58780 32 242446 40 1000000

No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...