So I'm not the greatest when it comes to math in Lua, so I'm going to ask for some help here. I was looking up how to set up a system where after each level, the amount of experience needed to get to the next level is changed (but not by a specific amount, and can be altered based on the amount of levels, if I add more in). After googling a bit, I found a C++ equivalent of something I need (found
here). Basically, it looked like this:
#include <cmath>
#include <iostream>
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;
}
}
I tried to convert this into Lua, but as you can probably see it probably isn't right:
function CalcXPRequired()
local levels = #EXP.Config.Levels
local xp_for_first_level = 1000
local xp_for_last_level = 14391160
local B = math.log( xp_for_last_level / xp_for_first_level ) / ( levels - 1 )
local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
local required
for i = 1, levels do
local old_xp = math.Round( A * math.exp( B * ( i - 1 ) ) )
local new_xp = math.Round( A * math.exp( B * i ) )
As you can see, it isn't even finished, as I don't think that's correct and I don't know where to go from here. I was wondering if anyone could help me set up a system that, where the max experience is 'xp_for_last_level' and the experience needed to level up varies between each level, and can be altered to a lower amount (between each level) if more levels are added, and / or higher if the 'xp_for_last_level' is increased.
If you need more clarification, I can try to help explain.
(Unless, of course, this is right. I don't know if it's right or wrong but I don't know how to finish it so I can't test it [emoji14] )
EDIT:
I did some messing around, and I think I figured out how to get it right. I have this (edited to work in
codepad.org)
for i = 1, 22 do
local xp_for_first_level = 1000
local xp_for_last_level = 14391160
local B = math.log( xp_for_last_level / xp_for_first_level ) / ( 22 - 1 )
local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
local old_xp = math.floor( A * math.exp( B * ( i - 1 ) ) )
local new_xp = math.floor( A * math.exp( B * i ) )
print( new_xp - old_xp )
end
Output:
1000
1577
2489
3927
6195
9772
15418
24324
38374
60540
95509
150678
237714
375025
591648
933402
1472560
2323151
3665069
5782114
9122025
14391160
Then I tried changing the amount of levels:
for i = 1, 100 do
local xp_for_first_level = 1000
local xp_for_last_level = 14391160
local B = math.log( xp_for_last_level / xp_for_first_level ) / ( 100 - 1 )
local A = xp_for_first_level / ( math.exp( B ) - 1.0 )
local old_xp = math.floor( A * math.exp( B * ( i - 1 ) ) )
local new_xp = math.floor( A * math.exp( B * i ) )
print( new_xp - old_xp )
end
Output:
1000
1101
1214
1336
1473
1621
1787
1968
2168
2387
2631
2897
3192
3515
3873
4266
4699
5176
5702
6281
6919
7621
8395
9247
10187
11221
12360
13615
14998
16521
18198
20046
22082
24323
26794
29515
32511
35812
39450
43454
47868
52727
58082
63980
70476
77633
85515
94199
103764
114300
125907
138691
152774
168288
185375
204199
224933
247774
272933
300647
331176
364803
401847
442650
487598
537110
591648
651726
717903
790800
871100
959552
1056987
1164316
1282542
1412773
1556229
1714252
1888319
2080063
2291276
2523936
2780222
3062529
3373505
3716056
4093391
4509041
4966897
5471244
6026805
6638776
7312890
8055454
8873418
9774441
10766955
11860251
13064561
14391160
So, I think I figured it out. Now I just have to check the amount of experience a person has VS the experience required to level up, which shouldn't be too hard.