Create EXP, point and stat functions

This commit is contained in:
Sky Johnson 2024-12-20 22:57:42 -06:00
parent abe827eabd
commit c689f37afc

98
src/math.php Normal file
View File

@ -0,0 +1,98 @@
<?php
namespace Math;
/*
Internal math functions, such as to calculate EXP, HP, stats, etc.
*/
/**
* Calculates the ***total*** EXP required at a particular level in order to level up.
*/
function calculate_exp(int $level, int $growthRate): int
{
if ($level < 1) throw new \InvalidArgumentException("Level must be 1 or greater");
// Growth rates:
// 0 = Erratic
// 1 = Fast
// 2 = Medium Fast
// 3 = Medium Slow
// 4 = Slow
// 5 = Fluctuating
if ($growthRate < 0 || $growthRate > 5) throw new \InvalidArgumentException("Growth rate must be between 0 and 5");
return match($growthRate) {
0 => calculate_erratic_exp($level),
1 => (4 * pow($level, 3)) / 5,
2 => pow($level, 3),
3 => ((6 * pow($level, 3)) / 5) - (15 * pow($level, 2)) + (100 * $level) - 140,
4 => (5 * pow($level, 3)) / 4,
5 => calculate_fluctuating_exp($level),
};
}
/**
* Calculate the ***total*** EXP for a given level in the Erratic formula.
*/
function calculate_erratic_exp(int $level): int
{
if ($level <= 50) {
return (pow($level, 3) * (100 - $level)) / 50;
} elseif ($level <= 68) {
return (pow($level, 3) * (150 - $level)) / 100;
} elseif ($level <= 98) {
return (pow($level, 3) * ((1911 - (10 * $level)) / 3)) / 500;
} else {
return (pow($level, 3) * (160 - $level)) / 100;
}
}
/**
* Calculate the ***total*** EXP for a given level in the Fluctuating formula.
*/
function calculate_fluctuating_exp(int $level): int
{
if ($level <= 15) {
return pow($level, 3) * ((((($level + 1) / 3) + 24) / 50));
} elseif ($level <= 36) {
return pow($level, 3) * (($level + 14) / 50);
} else {
return pow($level, 3) * ((($level / 2) + 32) / 50);
}
}
/**
* Calculate a points total from a base. Modes: 1 (weak), 2 (normal), 3 (strong)
*/
function calculate_points(int $base_points, int $level, int $mode = 2): int
{
if ($level < 1) throw new \InvalidArgumentException("Level must be 1 or greater");
$growth_multiplier = match($mode) {
1 => 0.75,
2 => 1.0,
3 => 1.5,
default => throw new \InvalidArgumentException("Invalid mode. Use 1 (weak), 2 (normal), or 3 (strong)")
};
return floor((2 * $base_points * $level * $growth_multiplier) / 100) + $level + 10;
}
/**
* Calculate a stat total from a base. Modes: 1 (weak), 2 (normal), 3 (strong)
*/
function calculate_stat(int $base_stat, int $level, int $mode = 2): int
{
if ($level < 1) throw new \InvalidArgumentException("Level must be 1 or greater");
$growth_multiplier = match($mode) {
1 => 0.75,
2 => 1.0,
3 => 1.5,
default => throw new \InvalidArgumentException("Invalid mode. Use 1 (weak), 2 (normal), or 3 (strong)")
};
return floor((2 * $base_stat * $level * $growth_multiplier) / 100) + 5;
}