Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Experience to Level Algorithm PHP [closed]

Tags:

algorithm

php

I am developing a Game and I didn't want an array full of levels that would most likely increase page load when iterating through them to find the users Level. I needed an algorithm to work this out independently with only the User's experience to hand. This is because I want the max Level to be around 1,000, and an array that long would take some time to go through.

I have, with some help, come up with the following algorithm for the difference between each level:

difference = difference + (difference / 7)

And it works well on the lower-levels, but this graph is the Levels 1-141, and as you can see, it's extremely easy up until about half way, then it shoots up and carries on shooting up, until it gets to the point 1,500,000,000 Experience is Level ~250, which isn't good enough.

The Graph for Levels 1-141

So, my question is, what do you think is a better way to do the difference between each level? Any advice is welcome, I will test all algorithms.

Thanks.

like image 616
oyed Avatar asked Feb 09 '12 16:02

oyed


2 Answers

First off, there's the linear system.

Level = Experience / 1000

Every 1,000 experience equates to one level. To make leveling slow down, you decrease the rate at which experience is acquired. This has the added bonus of making super-easy activities count as zero experience later, so you can't become epic by slaying rats.

Then there are decreasing-returns curves of various sorts. For example:

Level = (Experience/1000)^(1/scale)

With a scale of 2 (sqrt curve), for level 1, you need 1,000 experience. For two, you need four thousand. For three, nine thousand. Each level gets harder and harder. If you make the scale smaller, the rate at which the difficulty ramps up decreases (the slope gets less steep).

I suggest building the function itself, rather than building the first derivative (as you seem to have tried to do). That'll give you a better idea about the graph before you integrate.

like image 122
Borealid Avatar answered Sep 28 '22 06:09

Borealid


You probably want something that That has a horizontal asymptote at y = 1,000, which grows more quickly for small x than it does for large x. For instance, we might want to solve the system

dy/dx = a/x
lim (x->infinity) y = 1,000

We get that

y = C_0 - a/(x^2)

Using the limit, we get that

lim (x->infinity) y = C_0

So we get the equation

level = 1,000 - a/(exp^2)

We can make all users start with one experience, or simply change this to

level = 1,000 - a/(exp^2 + 1)

Either way, users would start out at level

level(0) = 1,000 - a

So if you want users to start at level 0, let a = 1,000; if users start at level 1, let a = 999. So I might recommend the following:

level(0) = 1,000 - 1,000(exp^2 + 1)

For experience starting at 0. You can change the rate of convergence by changing the power of exp; higher powers mean faster convergence to the max experience, lower powers mean longer convergence. The power 2 is just a suggestion; any positive real power will be fine.

Note that this is just one approach to constructing such a function. You can integrate any dy/dx which is larger for small x than for large x, and you will get another function that you can parameterize in more or less the same fashion.

like image 20
Patrick87 Avatar answered Sep 28 '22 06:09

Patrick87