armegalo
armegalo

Reputation: 23

Formula for game levelup based on points

I know there is a simple answer to this - I just cant think of it. I'm developing a strategy game where scenery upgrades its levels based on points. So the result I'm looking for is this...

Level from points...

1 <= 1  
2 <= 2,3  
3 <= 4,5,6  
4 <= 7,8,9,10  
5 <= 11,12,13,14,15

you can see where this is going. So can anyone remind me of the simple formula to do this so I can go away and bow my head in shame?

Upvotes: 2

Views: 168

Answers (4)

Rodrigo Rodrigues
Rodrigo Rodrigues

Reputation: 8556

The sequence you describe is a very known one, named triangular numbers sequence, that is defined by the formula:

maxpt = (lv * (lv + 1)) / 2

In your specific case, this formula gives the maximum of points maxpt you can have and still be at level lv.

But you want the inverse of that. You want to know wich level lv you will be for a given number of points pt. This is named the triangular root of the number (as an analogy to the square root).

You can directly invert the formula through the Bhaskara equation, but you'll need to do some tricks to correct the base index. That gives:

lv = (int)((Math.sqrt(8 * (pt - 1) + 1) - 1) / 2) + 1

That works for pt >= 1. For pt = 0 just return 0 to avoid a float point error (square root of a negative number).

You can surely find more elegant solutions to this over the internet, though.

Upvotes: 2

pseudoabdul
pseudoabdul

Reputation: 636

Here is a pretty sexy algorithm. Don't feel ashamed, I've never seen anything like this before and I had to sit down with some paper to suss it out. If you find while loops a bit gross, you could pull it off with recursion.

protected int GetLevel(int points)
{
    int i = points;
    int s = 0;
    while(i > 0)
    {
        s++;
        i -= s;          
    }
    return s;
}

Hope that helps!

Upvotes: 1

Fredrik Widerberg
Fredrik Widerberg

Reputation: 3108

Try this

int maxLevel = 10;

int GetLevel(int points) 
{
    int maxPointsPerLevel = 0;

    for(int level=1; level<maxLevel; level++) {
        maxPointsPerLevel  += level;
        if(points <= maxPointsPerLevel )
        {
            return level;
        }
    }

    return maxLevel;
}

Upvotes: 1

dome12b
dome12b

Reputation: 623

Hmm I could not find any algorythm doing this.

You can auto generate an array with this matching at the Awake() of your game:

public int startLevel = 1;
public int scoreLimit = 100;
private int currentLevel = 0;
public int[] matchingArray;

void Awake()
{
    matchingArray = new int[scoreLimit + 1];
    currentLevel = startLevel;
    int currentScore = 1;
    int availableScoreSteps = 1;

    while (currentScore <= scoreLimit)
    {
        while (availableScoreSteps > 0 && currentScore <= scoreLimit)
        {
            matchingArray[currentScore] = currentLevel;
            availableScoreSteps--;
            currentScore++;
        }
        currentLevel++;
        availableScoreSteps = currentLevel;
    }
}

Now you can access your level via matchingArray[yourscore] and it will return the level.

Upvotes: 1

Related Questions