PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
ExperienceUtils.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity\utils;
25
28use function count;
29use function max;
30
31abstract class ExperienceUtils{
32
36 public static function getXpToReachLevel(int $level) : int{
37 if($level <= 16){
38 return $level ** 2 + $level * 6;
39 }elseif($level <= 31){
40 return (int) ($level ** 2 * 2.5 - 40.5 * $level + 360);
41 }
42
43 return (int) ($level ** 2 * 4.5 - 162.5 * $level + 2220);
44 }
45
49 public static function getXpToCompleteLevel(int $level) : int{
50 if($level <= 15){
51 return 2 * $level + 7;
52 }elseif($level <= 30){
53 return 5 * $level - 38;
54 }else{
55 return 9 * $level - 158;
56 }
57 }
58
63 public static function getLevelFromXp(int $xp) : float{
64 if($xp < 0){
65 throw new \InvalidArgumentException("XP must be at least 0");
66 }
67 if($xp <= self::getXpToReachLevel(16)){
68 $a = 1;
69 $b = 6;
70 $c = 0;
71 }elseif($xp <= self::getXpToReachLevel(31)){
72 $a = 2.5;
73 $b = -40.5;
74 $c = 360;
75 }else{
76 $a = 4.5;
77 $b = -162.5;
78 $c = 2220;
79 }
80
81 $x = Math::solveQuadratic($a, $b, $c - $xp);
82 if(count($x) === 0){
83 throw new AssumptionFailedError("Expected at least 1 solution");
84 }
85
86 return max($x); //we're only interested in the positive solution
87 }
88}