Level-Based Variable Modifiers

I'm mad at this problem. Let's start. I want to add some modifiers to various minecraft events (for example, how much damage has been done in EntityDamageByEntityEvent) according to the level that is stored in the MySQL database.

This level system will have 99 levels (from 1 to 100), up to 50 levels by default; for example, in the case of EntityDamageByEntityEvent, I would like to change the damage like this:

a) If a player had a level of 50, the damage would not have been changed (it would have simply been multiplied by 1).

b) If a player had 1 level, the damage done would be multiplied by 1/3.

c) If a player had a level of 100, the damage would have been increased by 3.

So far, so good, but how can I do this in the case of a player with level 17 or 89? I want to know how to convert, say, a scale from 1 to 100 to 1/3 to 3, beeing 50 = 1 ... A bit of reason ... Thanks in advance, I hope you understand me!

+4
source share
2 answers

If you draw points (1, 1/3), (50, 1) and (100, 3) on the graph, you will see that they do not form a line. Therefore, it is not clear how your damage modifier function should act between these points.

You can do this in a piecewise linear way with linear interpolation from 1 to 50 and (separately) from 50 to 100:

final float scalingFactor;
if (level < 50) {
    // Convert (1..50) range into (0..1) range.
    final float interp = (level - 1) / 49.0;

    // Convert (0..1) range into (1/3..3/3) range.
    scalingFactor = ((interp * 2.0) + 1.0) / 3.0;
}
else {
    // Convert (50..100) range into (0..1) range.
    final float interp = (level - 50) / 50.0;

    // Convert (0..1) range into (1..3) range.
    scalingFactor = (interp * 2.0) + 1.0;
}

return damage * scalingFactor;

Or, if you want to have 1/3 damage at level 0 instead of 1, you can use the exponential function:

// Convert (0..100) range into (-1..+1) range.
final float interp = (level - 50) / 50.0;

// 3^(-1) == 1/3
// 3^0    == 1
// 3^(+1) == 3
final float scalingFactor = Math.pow(3.0, interp);

return damage * scalingFactor;

- 1 50 50 100, 50 51 , 49 50.

, .

+3

- :

@EventHandler
public void damage(EntityDamageEvent e){
  int i = e.getDamage();
  if(e.getDamager() instanceof Player){
    int level = //get players level here
    float multiplier = (level >= 50 ? (1 + ((1/25) * (level - 50)) : ((1/3) + (1/75) * (level - 1)));
    e.setDamage(i * multiplier);
  }
}

1/3, 1, 1, 50, 3, 100. .

:

  • 50 50 () 100 (), 2 , 2/50 1/25 , .

  • , 2/3 50 , 2/150 1/75 , .

, , 5 1/3 + (1/75 * (5 - 1))= 0.386666... = 29/75

0

Source: https://habr.com/ru/post/1541021/


All Articles