2025-08-14 13:55:37 -04:00

121 lines
3.6 KiB
PHP

<?php
declare(strict_types=1);
/*
* This file is a part of the Dragon-Knight project.
*
* Copyright (c) 2024-present Sharkk
*
* This file is subject to the MIT license that is bundled
* with this source code in the LICENSE.md file.
*/
namespace DragonKnight;
class Math
{
/*
Internal math public static functions, such as to calculate EXP, HP, stats, etc.
*/
/**
* Calculates the ***total*** EXP required at a particular level in order to level up.
*/
public static function calculate_exp(int $level, int $growth_rate): 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 ($growth_rate < 0 || $growth_rate > 5) {
throw new \InvalidArgumentException('Growth rate must be between 0 and 5');
}
return match ($growth_rate) {
0 => Math::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 => Math::calculate_fluctuating_exp($level),
};
}
/**
* Calculate the ***total*** EXP for a given level in the Erratic formula.
*/
public static 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;
}
return (pow($level, 3) * (160 - $level)) / 100;
}
/**
* Calculate the ***total*** EXP for a given level in the Fluctuating formula.
*/
public static 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);
}
return pow($level, 3) * ((($level / 2) + 32) / 50);
}
/**
* Calculate a points total from a base. Modes: 1 (weak), 2 (normal), 3 (strong).
*/
public static 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.15,
2 => 0.3,
3 => 0.6,
default => throw new \InvalidArgumentException('Invalid mode. Use 1 (weak), 2 (normal), or 3 (strong)')
};
return (int) floor((2 * $base_points * $level * $growth_multiplier) / 100) + $level + 10;
}
/**
* Calculate a stat total from a base. Modes: 1 (weak), 2 (normal), 3 (strong).
*/
public static 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.15,
2 => 0.3,
3 => 0.6,
default => throw new \InvalidArgumentException('Invalid mode. Use 1 (weak), 2 (normal), or 3 (strong)')
};
return (int) floor((2 * $base_stat * $level * $growth_multiplier) / 100) + 5;
}
}