mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-06-19 18:04:07 +00:00
# Conflicts: # composer.lock # resources/vanilla # src/entity/Living.php # src/pocketmine/Player.php # src/pocketmine/VersionInfo.php # src/pocketmine/block/Potato.php # src/pocketmine/block/Sugarcane.php # src/pocketmine/entity/Entity.php # src/pocketmine/item/Item.php # src/pocketmine/level/format/Chunk.php # src/pocketmine/level/format/io/leveldb/LevelDB.php # src/world/generator/GeneratorRegisterTask.php # tests/phpstan/configs/check-explicit-mixed-baseline.neon # tests/phpstan/configs/l7-baseline.neon # tests/phpstan/configs/l8-baseline.neon # tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMainLoggerTest.php # tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskMemoryLeakTest.php # tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/tests/AsyncTaskPublishProgressRaceTest.php
88 lines
2.3 KiB
PHP
88 lines
2.3 KiB
PHP
<?php
|
|
|
|
/*
|
|
*
|
|
* ____ _ _ __ __ _ __ __ ____
|
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* @author PocketMine Team
|
|
* @link http://www.pocketmine.net/
|
|
*
|
|
*
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace pocketmine\entity\utils;
|
|
|
|
use pocketmine\math\Math;
|
|
use pocketmine\utils\AssumptionFailedError;
|
|
use function max;
|
|
|
|
abstract class ExperienceUtils{
|
|
|
|
/**
|
|
* Calculates and returns the amount of XP needed to get from level 0 to level $level
|
|
*/
|
|
public static function getXpToReachLevel(int $level) : int{
|
|
if($level <= 16){
|
|
return $level ** 2 + $level * 6;
|
|
}elseif($level <= 31){
|
|
return (int) ($level ** 2 * 2.5 - 40.5 * $level + 360);
|
|
}
|
|
|
|
return (int) ($level ** 2 * 4.5 - 162.5 * $level + 2220);
|
|
}
|
|
|
|
/**
|
|
* Returns the amount of XP needed to reach $level + 1.
|
|
*/
|
|
public static function getXpToCompleteLevel(int $level) : int{
|
|
if($level <= 15){
|
|
return 2 * $level + 7;
|
|
}elseif($level <= 30){
|
|
return 5 * $level - 38;
|
|
}else{
|
|
return 9 * $level - 158;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Calculates and returns the number of XP levels the specified amount of XP points are worth.
|
|
* This returns a floating-point number, the decimal part being the progress through the resulting level.
|
|
*/
|
|
public static function getLevelFromXp(int $xp) : float{
|
|
if($xp < 0){
|
|
throw new \InvalidArgumentException("XP must be at least 0");
|
|
}
|
|
if($xp <= self::getXpToReachLevel(16)){
|
|
$a = 1;
|
|
$b = 6;
|
|
$c = 0;
|
|
}elseif($xp <= self::getXpToReachLevel(31)){
|
|
$a = 2.5;
|
|
$b = -40.5;
|
|
$c = 360;
|
|
}else{
|
|
$a = 4.5;
|
|
$b = -162.5;
|
|
$c = 2220;
|
|
}
|
|
|
|
$x = Math::solveQuadratic($a, $b, $c - $xp);
|
|
if(count($x) === 0){
|
|
throw new AssumptionFailedError("Expected at least 1 solution");
|
|
}
|
|
|
|
return max($x); //we're only interested in the positive solution
|
|
}
|
|
}
|