Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the implementation of getTotalExpToFullLevel. #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/util/Leveling.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ class Leveling {
const BASE = 10000;
const GROWTH = 2500;

/* Constants to generate the total amount of XP to complete a level */
const HALF_GROWTH = 0.5 * Leveling::GROWTH;

/* Constants to look up the level from the total amount of XP */
const REVERSE_PQ_PREFIX = -(Leveling::BASE - 0.5 * Leveling::GROWTH) / Leveling::GROWTH;
const REVERSE_CONST = Leveling::REVERSE_PQ_PREFIX * Leveling::REVERSE_PQ_PREFIX;
Expand Down Expand Up @@ -106,7 +103,7 @@ static function getTotalExpToLevel(float $level) {
$lv = floor($level);
$x0 = Leveling::getTotalExpToFullLevel($lv);
if ($level == $lv) return $x0;
return (Leveling:: getTotalExpToFullLevel($lv + 1) - $x0) * ($level % 1) + $x0;
return (Leveling::getTotalExpToFullLevel($lv + 1) - $x0) * ($level % 1) + $x0;
}

/**
Expand All @@ -117,7 +114,12 @@ static function getTotalExpToLevel(float $level) {
* @return float Experience to reach the given level
*/
static function getTotalExpToFullLevel(float $level) {
return (Leveling::HALF_GROWTH * ($level - 2) + Leveling::BASE) * ($level - 1);
if ($level == 1) return 0;
return Leveling::BASE * ($level - 1) + Leveling::GROWTH * Leveling::sumToN($level - 2);
}

static function sumToN(int $n) {
return $n * ($n + 1) / 2;
}

/**
Expand Down