Dans mon jeu, XP nécessaire pour atteindre le niveau suivant est Niveau actuel × Seuil du niveau . Dans ce cas, comment puis-je obtenir mon niveau actuel sur le total des XP jamais gagnés?
Par exemple:
Level Threshold = 50
Current Level = 1
À partir du niveau 1, il me faudrait (1 × 50) = 50 XP pour atteindre le niveau 2, etc.
Level 1: 50 XP needed to reach level 2
Level 2: 100 more XP needed to reach level 3
Level 3: 150 more XP needed to reach level 4
En d'autres termes, la table de progression est la suivante:
Level 1: 0 XP to 49 XP
Level 2: 50 XP to 149 XP
Level 3: 150 XP to 299 XP
Level 4: 300 XP to 499 XP
Si j'ai 300 XP, je viens juste d’atteindre le niveau 4. Comment puis-je calculer cela en général?
levels
mathematics
progression
Jay van Diyk
la source
la source
Réponses:
En calculant et en résolvant pour le
Level
conditionnel sur l'expérienceXP
, on obtient:Par exemple, quel est le niveau du joueur pour ?XP=300
Comme demandé.
Ou, à quoi sert le niveau
XP = 100000
?Plus généralement, pour un seuil de départ arbitraire au niveau 1:
Vous pouvez également faire l’inverse et calculer le
XP
nécessaire pour n’importe quel niveau en résolvant la formule ci-dessus pour XP.Notez que la formule ci-dessus fonctionne avec des fractions, mais vous devez arrondir à la valeur entière suivante. Par exemple, en C ++ / C #, vous pouvez utiliser (int) Level.
Pour obtenir la formule ci-dessus, j'ai utilisé des équations aux différences, la sommation de Gauss et une formule quadratique.
Si vous êtes intéressé par la solution de cette formule étape par étape ...
Nous faisons un algorithme récursif en commençant nos considérations que finalement
Experience(next_level) = Experience(current_level) + current_level*50
.Par exemple, pour obtenir nous avons:XPLevel3
Où,
2*50
vient de la demande du PO que l'expérience nécessaire pour atteindre le niveau suivant est le niveau actuel * 50.Maintenant, on remplace avec la même logique dans la formule. C'est:XpLevel2
Remplacez par la formule ci-dessus:XPLevel2=XPLevel1+2×50
et n’est que de 50, ce qui est notre point de départ. Par conséquentXpLevel1
Nous pouvons reconnaître une régularité permettant de calculer récursivement des niveaux plus élevés et une chaîne finie de sommations.
Où N est le niveau à atteindre. Pour obtenir l'XP du niveau N, il faut résoudre pour N.
Maintenant , le côté droit est tout simplement une somme de 1 à N-1, qui peut être exprimé par la fameuse sommation gaussienne . Par conséquentN×(N+1)÷2−N
ou juste
Enfin, tout mettre de côté:
Il s’agit maintenant d’une formule quadratique donnant une solution négative et positive, seule la réponse positive étant pertinente en l’absence de niveaux négatifs. Nous obtenons maintenant:
The current level conditional on XP and linear threshold is therefore:
Note Knowing these steps can be useful to solve for even more complex progressions. In the RPG realm you will see besides a linear progression as here, the actually more common fractional power or square relationship, e.g.Level=XP√5.0 . However, for game implementation itself, I believe this solution to be less optimal as ideally you should know all your level progressions beforehand instead of calculating them at runtime. For my own engine, I use therefore pre-processed experience tables, which are more flexible and often faster. However, to write those tables first, or, to just merely ask yourself what
XP
is needed to, let's say, obtainLevel 100
, this formula provides the quickest way aimed at answering the OP's specific question.Edit: This formula is fully working as it should and it outputs correctly the current
level
conditional onXP
with a linear threshold progression as requested by the OP. (The previous formula outputted "level+1" by assuming the player started from Level 0, which was my erring--I had solved it on my lunch break by writing on a small tissue! :)la source
The simple and generic solution, if you don't need to repeat this calculation millions of times per second (and if you do, you're probably doing something wrong), is just to use a loop:
The big advantage of this method, besides requiring no complicated mathematics, is that it works for any arbitrary exp-per-level function. If you like, you can even just make up arbitrary exp values per level, and store them in a table.
If you do (for some strange reason) need an even faster solution, you can also precalculate the total amount of exp needed to reach each level, store this in a table, and use a binary search to find the player's level. The precalculation still takes time proportional to the total number of levels, but the lookup then requires only time proportional to the logarithm of the number of levels.
la source
The question has been answered with code, but I think it should be answered with math. Someone might want to understand instead of just copy and paste.
Your system is easily described by a recurrence relation:
Which offers a nice simple closed form solution forXP :
Which we can solve forLevel :
(truncate to integer, because the player needs all the required XP to get any of the level-up bonus)
la source
XP_Level(0) = 50
and then we can just avoid solving? Any benefits, pros, cons? I think it'd be good to touch on this answer. +1Here's one approach to solving the problem using basic algebra. If you don't care about the steps, skip to the bottom.
An easy thing to come up with is, given a level
n
, the total experiencee
needed to obtain that level:The
t
term stands for the increase in XP needed per level - 50, in the example.We can solve the above using the formula for arithmetic sequences (sum identity):
However, we want the opposite formula - the player's level given their total experience. What we really want to do is solve for the level,
n
. First, let's group the terms:Now we can use the quadratic formula:
Final equation:
la source
All the math involved here is very important to know for all sorts of reasons, some of them applicable to game development.
BUT
This is a game development site, not a math site. So let's discuss how these things work not as algorithmic series, but as sets, because that is the sort of math that applies to leveling in games you might actually develop to sell, and this is the system that underlies most (but not all) leveling systems (at least historically).
Players tend to prefer nice, round numbers that are easy to remember and visualize, and nowhere is this more important than in a level-based game system where the player need X amounts of xp to advance to level Y.
There are two good reasons for picking round numbers:
Round numbers are enjoyable. The purpose of games is to be enjoyable. Pleasantness is important, especially since the game dilemmas will often be anything but pleasant by design.
Why is this important to keep in mind?
Most compound series algorithms do not produce nice, round numbers
Most series do not stop at a pretty point (every answer I've seen here so far just goes on forever). So, what do we do? We approximate, and then we determine what set of levels should apply to the game system.
How do we know what approximations are appropriate? We consider what the point of leveling is within the game system.
Most games have level caps that kick in at some point. There are a few ways this can play out:
XP + (XP * Modifier)
or whatever).There are some game systems where there is no level cap and the system is algorithmically determined. Usually systems like this use some X-powers-of-Y system to make the numbers explode quickly. This makes it very easy to get to level L-1, reasonably expected that most players will get to level L, inordinately difficult to get to level L+1, and players will grow old and die before reaching L+2. In this case "L" is a level you have decided is the target level appropriate for play and at which normally would have capped the system but leave the option open for people to delude themselves into thinking its a good idea to XP forever. (Sinister!) In this sort of system the math found here makes perfect sense. But it is a very narrow, and rarely encountered case in actual games.
So what do we do?
Calculate the levels and XP? No. Determine the levels and XP? Yes.
You determine what levels mean, then decide what the available set of levels should be available. This decision comes down to either granularity within the game system (Is there a huge difference in power between levels? Does each level confer a new ability? etc.) and whether or not levels are used themselves as a gating system ("Can't go to the next town until you're lvl 10, kid.", or a competitive ladder system enforces level-based tiers, etc.).
The code for this is pretty straightforward, and is just range determination:
Or with an if statement, or a case, or a chain of if/elif, or whatever the language you happen to be using supports (This part is the least interesting element of any game system, I just provide two ways because I happen to be in Erlang mode right now, and the above syntax may not be obvious to everyone.):
Is it amazing math? No. Not at all. Is it manual implementation of set element determination? Yep. That's all it is, and this is pretty much the way I've seen it actually done in most games throughout the years.
As a side note, this should not be done every time the player gains experience. Usually you keep track of "XP to go" as one value, and once the player either exhausts or surpasses the "to go" value (whichever way you're doing it) then you calculate this once to figure out where the player is really at, store that, calculate the next "to go" minus the leftover (if carrying XP forward is allowed) and repeat.
la source