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 <iostream>
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<int>(fraction_counter * experience_for_last_level / counter);
std::cout <<"Level "<<i<<": "<<a<<" ("<<counter<<")"<<"\n";
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)