PocketMine-MP 5.17.1 git-df4ada81e5d74a14046f27cf44a37dcee69d657e
FortuneDropHelper.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\block\utils;
25
28use function max;
29use function min;
30use function mt_getrandmax;
31use function mt_rand;
32
47 public static function weighted(Item $usedItem, int $min, int $maxBase) : int{
48 if($maxBase < $min){
49 throw new \InvalidArgumentException("Maximum drop amount must be greater than or equal to minimum drop amount");
50 }
51
52 $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE());
53
54 return mt_rand($min,
55 $fortuneLevel > 0 && mt_rand() / mt_getrandmax() > 2 / ($fortuneLevel + 2) ?
56 $maxBase * ($fortuneLevel + 1) :
57 $maxBase
58 );
59 }
60
73 public static function binomial(Item $usedItem, int $min, int $minRolls = 3, float $chance = 4 / 7) : int{
74 $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE());
75
76 $count = $min;
77 $rolls = $minRolls + $fortuneLevel;
78 for($i = 0; $i < $rolls; ++$i){
79 if(mt_rand() / mt_getrandmax() < $chance){
80 ++$count;
81 }
82 }
83
84 return $count;
85 }
86
95 public static function discrete(Item $usedItem, int $min, int $maxBase) : int{
96 if($maxBase < $min){
97 throw new \InvalidArgumentException("Minimum base drop amount must be less than or equal to maximum base drop amount");
98 }
99
100 $max = $maxBase + $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE());
101 return mt_rand($min, $max);
102 }
103
113 public static function bonusChanceDivisor(Item $usedItem, int $divisorBase, int $divisorSubtractPerLevel) : bool{
114 $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE());
115 return mt_rand(1, max(1, $divisorBase - ($fortuneLevel * $divisorSubtractPerLevel))) === 1;
116 }
117
124 public static function bonusChanceFixed(Item $usedItem, float $chanceBase, float $addedChancePerLevel) : bool{
125 $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE());
126 $chance = min(1, $chanceBase + ($fortuneLevel * $addedChancePerLevel));
127 return mt_rand() / mt_getrandmax() < $chance;
128 }
129}
static weighted(Item $usedItem, int $min, int $maxBase)
static discrete(Item $usedItem, int $min, int $maxBase)
static binomial(Item $usedItem, int $min, int $minRolls=3, float $chance=4/7)
static bonusChanceDivisor(Item $usedItem, int $divisorBase, int $divisorSubtractPerLevel)
static bonusChanceFixed(Item $usedItem, float $chanceBase, float $addedChancePerLevel)