How do I determine the remaining ratio in this equation?

59 Views Asked by At

I'm developing an experience system for a game, where the experience is gained per hit.

Anyways, I need to calculate how much EXP a user should get each time they hit a monster.

So for example if a user hits 1000 on a monster with 1000 hp they get 100% of the EXP. But if a user hits 500 on a monster with 1000 hp they get 50% of the EXP.

Now the problem is that if one user hits 300 on a monster with 1000 HP they will get 30% of the exp.

Then if another user hits 800 or higher on a monster they receive 100% of the EXP (which they shouldn't since the monster was already 30% dead they should only receive 70% of the EXP).

This is how I calculate it currently...

        damage_rate = damage / maxMonsterHealth;
        if (damage > maxMonsterHealth)
        {
            damage_rate = 1.0;
        }
        gained_exp = m_exp * damage_rate;

Any suggestions, I'm not the best at this kind of problem solving... Probably a super simple solution.

Solution... exp_damage = damage; if (exp_damage > original_CurrentHealth) { exp_damage = original_health; } damage_rate = exp_damage / m_maxHealth; gained_exp = m_exp * damage_rate;

1

There are 1 best solutions below

1
On BEST ANSWER

You could re-set the value of maxMonsterHealth each time somebody hits the monster. Alternatively, you could set up another variable, currentMonsterHealth, which starts off having the same value as maxMonsterHealth and updates its value each time somebody gains exp from hitting the monster, and use currentMonsterHealth in your calculation instead.

I'd probably go with the latter; seems less likely to interfere with the rest of the program then.

Something like

    currentMonsterHealth = maxMonsterHealth;
    damage_rate = damage / currentMonsterHealth;
    if (damage > currentMonsterHealth)
    {
        damage_rate = 1.0;
    }
    gained_exp = m_exp * damage_rate;
    currentMonsterHealth = currentMonsterHealth - damage;

As I'm not sure what you're programming in, I have no idea if the last line makes sense in the language, but hopefully you get what it means.