getEnchantmentLevel(VanillaEnchantments::FORTUNE()); return mt_rand($min, $fortuneLevel > 0 && mt_rand() / mt_getrandmax() > 2 / ($fortuneLevel + 2) ? $maxBase * ($fortuneLevel + 1) : $maxBase ); } /** * Increases the drop amount according to a binomial distribution. The function will roll maxBase+level times, and add 1 * if a random number between 0-1 is less than the given probability. Each level of fortune adds one extra roll. * * As many as maxBase+level items can be dropped. This applies even if the fortune level is 0. * * @param float $chance The chance of adding 1 to the amount for each roll, must be in the range 0-1 * @param int $min Minimum amount * @param int $minRolls Number of rolls if fortune level is 0, added to fortune level to calculate total rolls * * @return int the number of items to drop */ public static function binomial(Item $usedItem, int $min, int $minRolls = 3, float $chance = 4 / 7) : int{ $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE()); $count = $min; $rolls = $minRolls + $fortuneLevel; for($i = 0; $i < $rolls; ++$i){ if(mt_rand() / mt_getrandmax() < $chance){ ++$count; } } return $count; } /** * Grass have a fixed chance to drop wheat seed. * Fortune level increases the maximum number of seeds that can be dropped. * A discrete uniform distribution is used to determine the number of seeds dropped. * * TODO: I'm not sure this really belongs here, but it's preferable not to duplicate this code between grass and * tall grass. * * @return Item[] */ public static function grass(Item $usedItem) : array{ if(FortuneDropHelper::bonusChanceDivisor($usedItem, 8, 2)){ return [ VanillaItems::WHEAT_SEEDS() ]; } return []; } /** * Adds the fortune level to the base max and picks a random number between the minimim and adjusted maximum. * Each amount in the range has an equal chance of being picked. * * @param int $maxBase Maximum base amount, as if the fortune level was 0 * * @return int the number of items to drop */ public static function discrete(Item $usedItem, int $min, int $maxBase) : int{ if($maxBase < $min){ throw new \InvalidArgumentException("Minimum base drop amount must be less than or equal to maximum base drop amount"); } $max = $maxBase + $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE()); return mt_rand($min, $max); } /** * Calculates a chance of getting an extra bonus drop by reducing the chance divisor by a given amount per fortune * level. * * @param int $divisorBase The number to divide 1 by to get the chance, as if the fortune level was 0 * @param int $divisorSubtractPerLevel The amount to subtract from the divisor for each level of fortune * * @return bool whether the bonus drop should be added */ public static function bonusChanceDivisor(Item $usedItem, int $divisorBase, int $divisorSubtractPerLevel) : bool{ $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE()); return mt_rand(1, max(1, $divisorBase - ($fortuneLevel * $divisorSubtractPerLevel))) === 1; } /** * Calculates a chance of getting an extra bonus drop by increasing the chance by a fixed amount per fortune level. * * @param float $chanceBase The base chance of getting a bonus drop, as if the fortune level was 0 * @param float $addedChancePerLevel The amount to add to the chance for each level of fortune */ public static function bonusChanceFixed(Item $usedItem, float $chanceBase, float $addedChancePerLevel) : bool{ $fortuneLevel = $usedItem->getEnchantmentLevel(VanillaEnchantments::FORTUNE()); $chance = min(1, $chanceBase + ($fortuneLevel * $addedChancePerLevel)); return mt_rand() / mt_getrandmax() < $chance; } }