Skip to content

Commit

Permalink
Refactor combat level calculations to use closed-form formulas. (rune…
Browse files Browse the repository at this point in the history
…lite#8874)

* Refactor combat level calculations to use closed-form formulas.

Also move most calculations to the Experience utility class.
Fixes runelite#7411.

* Add new test for magic levels that barely reach the next combat level.

* Add another test that breaks on master.
  • Loading branch information
emerald000 authored and Adam- committed May 20, 2019
1 parent 8890d5b commit 8026785
Show file tree
Hide file tree
Showing 3 changed files with 404 additions and 294 deletions.
117 changes: 109 additions & 8 deletions runelite-api/src/main/java/net/runelite/api/Experience.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@
*/
package net.runelite.api;

import static java.lang.Math.floor;
import static java.lang.Math.max;

/**
* A utility class used for calculating experience related values.
* <p>
Expand Down Expand Up @@ -127,6 +124,15 @@ else if (xp > xpForLevel)
return high + 1;
}

private static double getMeleeRangeOrMagicCombatLevelContribution(int attackLevel, int strengthLevel, int magicLevel, int rangeLevel)
{
double melee = 0.325 * (attackLevel + strengthLevel);
double range = 0.325 * (Math.floor(rangeLevel / 2) + rangeLevel);
double magic = 0.325 * (Math.floor(magicLevel / 2) + magicLevel);

return Math.max(melee, Math.max(range, magic));
}

/**
* Calculates a non-virtual high-precision combat level without integer
* rounding.
Expand All @@ -146,13 +152,11 @@ public static double getCombatLevelPrecise(int attackLevel, int strengthLevel,
int defenceLevel, int hitpointsLevel, int magicLevel,
int rangeLevel, int prayerLevel)
{
double base = 0.25 * (defenceLevel + hitpointsLevel + floor(prayerLevel / 2));
double base = 0.25 * (defenceLevel + hitpointsLevel + Math.floor(prayerLevel / 2));

double melee = 0.325 * (attackLevel + strengthLevel);
double range = 0.325 * (floor(rangeLevel / 2) + rangeLevel);
double magic = 0.325 * (floor(magicLevel / 2) + magicLevel);
double typeContribution = getMeleeRangeOrMagicCombatLevelContribution(attackLevel, strengthLevel, magicLevel, rangeLevel);

return base + max(melee, max(range, magic));
return base + typeContribution;
}

/**
Expand All @@ -173,4 +177,101 @@ public static int getCombatLevel(int attackLevel, int strengthLevel,
{
return (int) getCombatLevelPrecise(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel);
}

/**
* Calculate number of attack/strength levels required to increase combat level.
*
* @param attackLevel the attack level
* @param strengthLevel the strength level
* @param defenceLevel the defence level
* @param hitpointsLevel the hitpoints level
* @param magicLevel the magic level
* @param rangeLevel the range level
* @param prayerLevel the prayer level
* @return the number of levels required
*/
public static int getNextCombatLevelMelee(int attackLevel, int strengthLevel, int defenceLevel, int hitpointsLevel,
int magicLevel, int rangeLevel, int prayerLevel)
{
int nextCombatLevel = Experience.getCombatLevel(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel) + 1;
return (int) Math.ceil(-10. / 13 * (defenceLevel + hitpointsLevel + Math.floor(prayerLevel / 2) - 4 * nextCombatLevel)) - strengthLevel - attackLevel;
}

/**
* Calculate number of hitpoints/defence levels required to increase combat level.
*
* @param attackLevel the attack level
* @param strengthLevel the strength level
* @param defenceLevel the defence level
* @param hitpointsLevel the hitpoints level
* @param magicLevel the magic level
* @param rangeLevel the range level
* @param prayerLevel the prayer level
* @return the number of levels required
*/
public static int getNextCombatLevelHpDef(int attackLevel, int strengthLevel, int defenceLevel, int hitpointsLevel,
int magicLevel, int rangeLevel, int prayerLevel)
{
int nextCombatLevel = Experience.getCombatLevel(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel) + 1;
double typeContribution = Experience.getMeleeRangeOrMagicCombatLevelContribution(attackLevel, strengthLevel, magicLevel, rangeLevel);
return (int) Math.ceil(4 * nextCombatLevel - Math.floor(prayerLevel / 2) - 4 * typeContribution) - hitpointsLevel - defenceLevel;
}

/**
* Calculate number of magic levels required to increase combat level.
*
* @param attackLevel the attack level
* @param strengthLevel the strength level
* @param defenceLevel the defence level
* @param hitpointsLevel the hitpoints level
* @param magicLevel the magic level
* @param rangeLevel the range level
* @param prayerLevel the prayer level
* @return the number of levels required
*/
public static int getNextCombatLevelMagic(int attackLevel, int strengthLevel, int defenceLevel, int hitpointsLevel,
int magicLevel, int rangeLevel, int prayerLevel)
{
int nextCombatLevel = Experience.getCombatLevel(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel) + 1;
return (int) Math.ceil(2. / 3 * Math.ceil(-10. / 13 * (hitpointsLevel + defenceLevel - 4 * nextCombatLevel + Math.floor(prayerLevel / 2)))) - magicLevel;
}

/**
* Calculate number of ranged levels required to increase combat level.
*
* @param attackLevel the attack level
* @param strengthLevel the strength level
* @param defenceLevel the defence level
* @param hitpointsLevel the hitpoints level
* @param magicLevel the magic level
* @param rangeLevel the range level
* @param prayerLevel the prayer level
* @return the number of levels required
*/
public static int getNextCombatLevelRange(int attackLevel, int strengthLevel, int defenceLevel, int hitpointsLevel,
int magicLevel, int rangeLevel, int prayerLevel)
{
int nextCombatLevel = Experience.getCombatLevel(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel) + 1;
return (int) Math.ceil(2. / 3 * Math.ceil(-10. / 13 * (hitpointsLevel + defenceLevel - 4 * nextCombatLevel + Math.floor(prayerLevel / 2)))) - rangeLevel;
}

/**
* Calculate number of prayer levels required to increase combat level.
*
* @param attackLevel the attack level
* @param strengthLevel the strength level
* @param defenceLevel the defence level
* @param hitpointsLevel the hitpoints level
* @param magicLevel the magic level
* @param rangeLevel the range level
* @param prayerLevel the prayer level
* @return the number of levels required
*/
public static int getNextCombatLevelPrayer(int attackLevel, int strengthLevel, int defenceLevel, int hitpointsLevel,
int magicLevel, int rangeLevel, int prayerLevel)
{
int nextCombatLevel = Experience.getCombatLevel(attackLevel, strengthLevel, defenceLevel, hitpointsLevel, magicLevel, rangeLevel, prayerLevel) + 1;
double typeContribution = Experience.getMeleeRangeOrMagicCombatLevelContribution(attackLevel, strengthLevel, magicLevel, rangeLevel);
return 2 * (int) Math.ceil(-hitpointsLevel - defenceLevel + 4 * nextCombatLevel - 4 * typeContribution) - prayerLevel;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
*/
package net.runelite.client.plugins.combatlevel;

import com.google.common.annotations.VisibleForTesting;
import net.runelite.api.Client;
import net.runelite.api.Experience;
import net.runelite.api.Skill;
Expand All @@ -43,11 +42,6 @@
class CombatLevelOverlay extends Overlay
{
private static final Color COMBAT_LEVEL_COLOUR = new Color(0xff981f);
private static final double PRAY_MULT = 0.125;
static final double ATT_STR_MULT = 0.325;
static final double DEF_HP_MULT = 0.25;
static final double RANGE_MAGIC_LEVEL_MULT = 1.5;
static final double RANGE_MAGIC_MULT = 0.325;

private final Client client;
private final CombatLevelConfig config;
Expand Down Expand Up @@ -95,23 +89,20 @@ private String getLevelsUntilTooltip()
int defenceLevel = client.getRealSkillLevel(Skill.DEFENCE);
int hitpointsLevel = client.getRealSkillLevel(Skill.HITPOINTS);
int magicLevel = client.getRealSkillLevel(Skill.MAGIC);
int rangedLevel = client.getRealSkillLevel(Skill.RANGED);
int rangeLevel = client.getRealSkillLevel(Skill.RANGED);
int prayerLevel = client.getRealSkillLevel(Skill.PRAYER);

// calculate initial required numbers
double base = DEF_HP_MULT * (defenceLevel + hitpointsLevel + Math.floor(prayerLevel / 2));
double melee = ATT_STR_MULT * (attackLevel + strengthLevel);
double range = RANGE_MAGIC_MULT * Math.floor(rangedLevel * RANGE_MAGIC_LEVEL_MULT);
double mage = RANGE_MAGIC_MULT * Math.floor(magicLevel * RANGE_MAGIC_LEVEL_MULT);
double max = Math.max(melee, Math.max(range, mage));

// find the needed levels until level up
int next = client.getLocalPlayer().getCombatLevel() + 1;
int meleeNeed = calcLevels(base + melee, next, ATT_STR_MULT);
int hpdefNeed = calcLevels(base + max, next, DEF_HP_MULT);
int prayNeed = calcLevelsPray(base + max, next, prayerLevel);
int rangeNeed = calcLevelsRM(rangedLevel, next, base);
int magicNeed = calcLevelsRM(magicLevel, next, base);
int meleeNeed = Experience.getNextCombatLevelMelee(attackLevel, strengthLevel, defenceLevel, hitpointsLevel,
magicLevel, rangeLevel, prayerLevel);
int hpDefNeed = Experience.getNextCombatLevelHpDef(attackLevel, strengthLevel, defenceLevel, hitpointsLevel,
magicLevel, rangeLevel, prayerLevel);
int rangeNeed = Experience.getNextCombatLevelRange(attackLevel, strengthLevel, defenceLevel, hitpointsLevel,
magicLevel, rangeLevel, prayerLevel);
int magicNeed = Experience.getNextCombatLevelMagic(attackLevel, strengthLevel, defenceLevel, hitpointsLevel,
magicLevel, rangeLevel, prayerLevel);
int prayerNeed = Experience.getNextCombatLevelPrayer(attackLevel, strengthLevel, defenceLevel, hitpointsLevel,
magicLevel, rangeLevel, prayerLevel);

// create tooltip string
StringBuilder sb = new StringBuilder();
Expand All @@ -121,85 +112,23 @@ private String getLevelsUntilTooltip()
{
sb.append(meleeNeed).append(" Attack/Strength</br>");
}
if ((hitpointsLevel + defenceLevel + hpdefNeed) <= Experience.MAX_REAL_LEVEL * 2)
if ((hitpointsLevel + defenceLevel + hpDefNeed) <= Experience.MAX_REAL_LEVEL * 2)
{
sb.append(hpdefNeed).append(" Defence/Hitpoints</br>");
sb.append(hpDefNeed).append(" Defence/Hitpoints</br>");
}
if ((rangedLevel + rangeNeed) <= Experience.MAX_REAL_LEVEL)
if ((rangeLevel + rangeNeed) <= Experience.MAX_REAL_LEVEL)
{
sb.append(rangeNeed).append(" Ranged</br>");
}
if ((magicLevel + magicNeed) <= Experience.MAX_REAL_LEVEL)
{
sb.append(magicNeed).append(" Magic</br>");
}
if ((prayerLevel + prayNeed) <= Experience.MAX_REAL_LEVEL)
if ((prayerLevel + prayerNeed) <= Experience.MAX_REAL_LEVEL)
{
sb.append(prayNeed).append(" Prayer");
sb.append(prayerNeed).append(" Prayer");
}
return sb.toString();
}

/**
* Calculate skill levels required for increasing combat level, meant
* for all combat skills besides prayer, ranged, and magic.
* @param start initial value
* @param end ending value (combat level + 1)
* @param multiple how much adding one skill level will change combat
* @return levels required for a specific skill to level up combat
*/
@VisibleForTesting
static int calcLevels(double start, int end, double multiple)
{
return (int) Math.ceil(calcMultipliedLevels(start, end, multiple));
}

/**
* Calculate skill levels for increasing combat level, meant ONLY for the Prayer skill.
* <p>
* Note: Prayer is a special case, only leveling up upon even level numbers. This is accounted
* for in this function.
* </p>
* @param start current combat level
* @param end ending value (combat level + 1)
* @param prayerLevel the player's current prayer level
* @return Prayer levels required to level up combat
*/
@VisibleForTesting
static int calcLevelsPray(double start, int end, int prayerLevel)
{
int neededLevels = (int) Math.ceil(calcMultipliedLevels(start, end, PRAY_MULT));

if (prayerLevel % 2 != 0)
{
neededLevels--;
}

if ((prayerLevel + neededLevels) % 2 != 0)
{
return neededLevels + 1;
}

return neededLevels;
}

private static double calcMultipliedLevels(double start, int end, double multiple)
{
return (end - start) / multiple;
}

/**
* Calculate skill levels required for increasing combat level, meant
* ONLY for Ranged and Magic skills.
* @param start either the current ranged or magic level
* @param end ending value (combat level + 1)
* @param dhp defence, hitpoints, and prayer; this is the initial calculated "base" value
* @return levels required for a specific skill to level up combat
*/
@VisibleForTesting
static int calcLevelsRM(double start, int end, double dhp)
{
start = Math.floor(start * RANGE_MAGIC_LEVEL_MULT) * RANGE_MAGIC_MULT;
return (int) Math.ceil((end - dhp - start) / (RANGE_MAGIC_MULT * RANGE_MAGIC_LEVEL_MULT));
}
}
Loading

0 comments on commit 8026785

Please sign in to comment.