From 7724da61dc9689f285e975944203270f4fc01318 Mon Sep 17 00:00:00 2001 From: Akmal Fairuz <35138228+AkmalFairuz@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:21:07 +0700 Subject: [PATCH 1/5] Replace lcg_value() with Utils::getRandomFloat() --- src/block/Anvil.php | 4 ++-- src/block/Farmland.php | 4 ++-- src/block/ItemFrame.php | 6 +++--- src/block/Liquid.php | 4 ++-- src/entity/Entity.php | 3 +-- src/entity/Living.php | 6 +++--- src/item/Armor.php | 4 ++-- src/item/Durable.php | 4 ++-- src/item/RottenFlesh.php | 4 ++-- src/item/SpawnEgg.php | 4 ++-- src/utils/Utils.php | 10 ++++++++++ src/world/World.php | 10 +++++----- 12 files changed, 36 insertions(+), 27 deletions(-) diff --git a/src/block/Anvil.php b/src/block/Anvil.php index 4b4afef6153..0a1a4707005 100644 --- a/src/block/Anvil.php +++ b/src/block/Anvil.php @@ -35,10 +35,10 @@ use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; +use pocketmine\utils\Utils; use pocketmine\world\BlockTransaction; use pocketmine\world\sound\AnvilFallSound; use pocketmine\world\sound\Sound; -use function lcg_value; use function round; class Anvil extends Transparent implements Fallable{ @@ -97,7 +97,7 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo } public function onHitGround(FallingBlock $blockEntity) : bool{ - if(lcg_value() < 0.05 + (round($blockEntity->getFallDistance()) - 1) * 0.05){ + if(Utils::getRandomFloat() < 0.05 + (round($blockEntity->getFallDistance()) - 1) * 0.05){ if($this->damage !== self::VERY_DAMAGED){ $this->damage = $this->damage + 1; }else{ diff --git a/src/block/Farmland.php b/src/block/Farmland.php index a17a220f0b5..b7a2500a8b5 100644 --- a/src/block/Farmland.php +++ b/src/block/Farmland.php @@ -31,8 +31,8 @@ use pocketmine\item\Item; use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; +use pocketmine\utils\Utils; use function intdiv; -use function lcg_value; class Farmland extends Transparent{ public const MAX_WETNESS = 7; @@ -148,7 +148,7 @@ public function onRandomTick() : void{ } public function onEntityLand(Entity $entity) : ?float{ - if($entity instanceof Living && lcg_value() < $entity->getFallDistance() - 0.5){ + if($entity instanceof Living && Utils::getRandomFloat() < $entity->getFallDistance() - 0.5){ $ev = new EntityTrampleFarmlandEvent($entity, $this); $ev->call(); if(!$ev->isCancelled()){ diff --git a/src/block/ItemFrame.php b/src/block/ItemFrame.php index b5b6093c497..c03806a3b55 100644 --- a/src/block/ItemFrame.php +++ b/src/block/ItemFrame.php @@ -31,13 +31,13 @@ use pocketmine\math\Facing; use pocketmine\math\Vector3; use pocketmine\player\Player; +use pocketmine\utils\Utils; use pocketmine\world\BlockTransaction; use pocketmine\world\sound\ItemFrameAddItemSound; use pocketmine\world\sound\ItemFrameRemoveItemSound; use pocketmine\world\sound\ItemFrameRotateItemSound; use function is_infinite; use function is_nan; -use function lcg_value; class ItemFrame extends Flowable{ use AnyFacingTrait; @@ -154,7 +154,7 @@ public function onAttack(Item $item, int $face, ?Player $player = null) : bool{ return false; } $world = $this->position->getWorld(); - if(lcg_value() <= $this->itemDropChance){ + if(Utils::getRandomFloat() <= $this->itemDropChance){ $world->dropItem($this->position->add(0.5, 0.5, 0.5), clone $this->framedItem); $world->addSound($this->position, new ItemFrameRemoveItemSound()); } @@ -185,7 +185,7 @@ public function place(BlockTransaction $tx, Item $item, Block $blockReplace, Blo public function getDropsForCompatibleTool(Item $item) : array{ $drops = parent::getDropsForCompatibleTool($item); - if($this->framedItem !== null && lcg_value() <= $this->itemDropChance){ + if($this->framedItem !== null && Utils::getRandomFloat() <= $this->itemDropChance){ $drops[] = clone $this->framedItem; } diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 6404cf9081f..2320feb3df5 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -33,9 +33,9 @@ use pocketmine\math\AxisAlignedBB; use pocketmine\math\Facing; use pocketmine\math\Vector3; +use pocketmine\utils\Utils; use pocketmine\world\sound\FizzSound; use pocketmine\world\sound\Sound; -use function lcg_value; abstract class Liquid extends Transparent{ public const MAX_DECAY = 7; @@ -368,7 +368,7 @@ protected function checkForHarden() : bool{ protected function liquidCollide(Block $cause, Block $result) : bool{ if(BlockEventHelper::form($this, $result, $cause)){ - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (lcg_value() - lcg_value()) * 0.8)); + $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + Utils::getRandomFloat(-0.8, 0.8))); } return true; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c55a8716cae..c4f9066c537 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -74,7 +74,6 @@ use function floor; use function fmod; use function get_class; -use function lcg_value; use function sin; use function spl_object_id; use const M_PI_2; @@ -906,7 +905,7 @@ protected function checkObstruction(float $x, float $y, float $z) : bool{ return false; } - $force = lcg_value() * 0.2 + 0.1; + $force = Utils::getRandomFloat(0.1, 0.2); $this->motion = match($direction){ Facing::WEST => $this->motion->withComponents(-$force, null, null), diff --git a/src/entity/Living.php b/src/entity/Living.php index 81f46424f17..1f27a5cacf4 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -58,6 +58,7 @@ use pocketmine\player\Player; use pocketmine\timings\Timings; use pocketmine\utils\Binary; +use pocketmine\utils\Utils; use pocketmine\world\sound\BurpSound; use pocketmine\world\sound\EntityLandSound; use pocketmine\world\sound\EntityLongFallSound; @@ -69,7 +70,6 @@ use function count; use function floor; use function ksort; -use function lcg_value; use function max; use function min; use function mt_getrandmax; @@ -490,7 +490,7 @@ protected function applyPostDamageEffects(EntityDamageEvent $source) : void{ $helmet = $this->armorInventory->getHelmet(); if($helmet instanceof Armor){ $finalDamage = $source->getFinalDamage(); - $this->damageItem($helmet, (int) round($finalDamage * 4 + lcg_value() * $finalDamage * 2)); + $this->damageItem($helmet, (int) round($finalDamage * 4 + Utils::getRandomFloat() * $finalDamage * 2)); $this->armorInventory->setHelmet($helmet); } } @@ -697,7 +697,7 @@ protected function doAirSupplyTick(int $tickDiff) : bool{ $this->setBreathing(false); if(($respirationLevel = $this->armorInventory->getHelmet()->getEnchantmentLevel(VanillaEnchantments::RESPIRATION())) <= 0 || - lcg_value() <= (1 / ($respirationLevel + 1)) + Utils::getRandomFloat() <= (1 / ($respirationLevel + 1)) ){ $ticks -= $tickDiff; if($ticks <= -20){ diff --git a/src/item/Armor.php b/src/item/Armor.php index 417c57f75ca..63a8003adc9 100644 --- a/src/item/Armor.php +++ b/src/item/Armor.php @@ -33,7 +33,7 @@ use pocketmine\nbt\tag\IntTag; use pocketmine\player\Player; use pocketmine\utils\Binary; -use function lcg_value; +use pocketmine\utils\Utils; use function mt_rand; class Armor extends Durable{ @@ -129,7 +129,7 @@ protected function getUnbreakingDamageReduction(int $amount) : int{ $chance = 1 / ($unbreakingLevel + 1); for($i = 0; $i < $amount; ++$i){ - if(mt_rand(1, 100) > 60 && lcg_value() > $chance){ //unbreaking only applies to armor 40% of the time at best + if(mt_rand(1, 100) > 60 && Utils::getRandomFloat() > $chance){ //unbreaking only applies to armor 40% of the time at best $negated++; } } diff --git a/src/item/Durable.php b/src/item/Durable.php index f110f6ea517..069a01202ed 100644 --- a/src/item/Durable.php +++ b/src/item/Durable.php @@ -25,7 +25,7 @@ use pocketmine\item\enchantment\VanillaEnchantments; use pocketmine\nbt\tag\CompoundTag; -use function lcg_value; +use pocketmine\utils\Utils; use function min; abstract class Durable extends Item{ @@ -87,7 +87,7 @@ protected function getUnbreakingDamageReduction(int $amount) : int{ $chance = 1 / ($unbreakingLevel + 1); for($i = 0; $i < $amount; ++$i){ - if(lcg_value() > $chance){ + if(Utils::getRandomFloat() > $chance){ $negated++; } } diff --git a/src/item/RottenFlesh.php b/src/item/RottenFlesh.php index 4cecc67fcd3..2ea3ee9559c 100644 --- a/src/item/RottenFlesh.php +++ b/src/item/RottenFlesh.php @@ -25,7 +25,7 @@ use pocketmine\entity\effect\EffectInstance; use pocketmine\entity\effect\VanillaEffects; -use function lcg_value; +use pocketmine\utils\Utils; class RottenFlesh extends Food{ @@ -38,7 +38,7 @@ public function getSaturationRestore() : float{ } public function getAdditionalEffects() : array{ - if(lcg_value() <= 0.8){ + if(Utils::getRandomFloat() <= 0.8){ return [ new EffectInstance(VanillaEffects::HUNGER(), 600) ]; diff --git a/src/item/SpawnEgg.php b/src/item/SpawnEgg.php index 51dcceebd2a..0455c82192a 100644 --- a/src/item/SpawnEgg.php +++ b/src/item/SpawnEgg.php @@ -27,15 +27,15 @@ use pocketmine\entity\Entity; use pocketmine\math\Vector3; use pocketmine\player\Player; +use pocketmine\utils\Utils; use pocketmine\world\World; -use function lcg_value; abstract class SpawnEgg extends Item{ abstract protected function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity; public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, array &$returnedItems) : ItemUseResult{ - $entity = $this->createEntity($player->getWorld(), $blockReplace->getPosition()->add(0.5, 0, 0.5), lcg_value() * 360, 0); + $entity = $this->createEntity($player->getWorld(), $blockReplace->getPosition()->add(0.5, 0, 0.5), Utils::getRandomFloat(0.0, 360.0), 0); if($this->hasCustomName()){ $entity->setNameTag($this->getCustomName()); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index ef3f2d2499e..6172cb5bd67 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -67,6 +67,8 @@ use function is_object; use function is_string; use function mb_check_encoding; +use function mt_rand; +use function mt_getrandmax; use function ob_end_clean; use function ob_get_contents; use function ob_start; @@ -675,4 +677,12 @@ function_exists('opcache_get_status') && //jit not available return null; } + + public static function getRandomFloat(float $min = 0.0, float $max = 1.0) : float{ + if($min > $max){ + throw new \InvalidArgumentException("Minimum value cannot be greater than maximum value"); + } + + return $min + (mt_rand() / mt_getrandmax()) * ($max - $min); + } } diff --git a/src/world/World.php b/src/world/World.php index 187f7ab8b89..ab1ae6558e6 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -83,6 +83,7 @@ use pocketmine\utils\AssumptionFailedError; use pocketmine\utils\Limits; use pocketmine\utils\ReversePriorityQueue; +use pocketmine\utils\Utils; use pocketmine\world\biome\Biome; use pocketmine\world\biome\BiomeRegistry; use pocketmine\world\format\Chunk; @@ -120,7 +121,6 @@ use function gettype; use function is_a; use function is_object; -use function lcg_value; use function max; use function microtime; use function min; @@ -1998,10 +1998,10 @@ public function dropItem(Vector3 $source, Item $item, ?Vector3 $motion = null, i return null; } - $itemEntity = new ItemEntity(Location::fromObject($source, $this, lcg_value() * 360, 0), $item); + $itemEntity = new ItemEntity(Location::fromObject($source, $this, Utils::getRandomFloat(0.0, 360.0), 0), $item); $itemEntity->setPickupDelay($delay); - $itemEntity->setMotion($motion ?? new Vector3(lcg_value() * 0.2 - 0.1, 0.2, lcg_value() * 0.2 - 0.1)); + $itemEntity->setMotion($motion ?? new Vector3(Utils::getRandomFloat(-0.1, 0.1), 0.2, Utils::getRandomFloat(-0.1, 0.1))); $itemEntity->spawnToAll(); return $itemEntity; @@ -2018,9 +2018,9 @@ public function dropExperience(Vector3 $pos, int $amount) : array{ $orbs = []; foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){ - $orb = new ExperienceOrb(Location::fromObject($pos, $this, lcg_value() * 360, 0), $split); + $orb = new ExperienceOrb(Location::fromObject($pos, $this, Utils::getRandomFloat(0.0, 360.0), 0), $split); - $orb->setMotion(new Vector3((lcg_value() * 0.2 - 0.1) * 2, lcg_value() * 0.4, (lcg_value() * 0.2 - 0.1) * 2)); + $orb->setMotion(new Vector3(Utils::getRandomFloat(-0.2, 0.2), Utils::getRandomFloat(0.0, 0.4), Utils::getRandomFloat(-0.2, 0.2))); $orb->spawnToAll(); $orbs[] = $orb; From 11a22cf6a5aac913ad5fc4cb04ffeb3f0939c4dc Mon Sep 17 00:00:00 2001 From: Akmal Fairuz <35138228+AkmalFairuz@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:27:37 +0700 Subject: [PATCH 2/5] fix --- src/entity/Entity.php | 2 +- src/utils/Utils.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index c4f9066c537..0ef446a7beb 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -905,7 +905,7 @@ protected function checkObstruction(float $x, float $y, float $z) : bool{ return false; } - $force = Utils::getRandomFloat(0.1, 0.2); + $force = Utils::getRandomFloat(0.1, 0.3); $this->motion = match($direction){ Facing::WEST => $this->motion->withComponents(-$force, null, null), diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 6172cb5bd67..cc93cfd1fd4 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -67,8 +67,8 @@ use function is_object; use function is_string; use function mb_check_encoding; -use function mt_rand; use function mt_getrandmax; +use function mt_rand; use function ob_end_clean; use function ob_get_contents; use function ob_start; From 5d54db66d22b8d75a0f2a7ad381082304116d0af Mon Sep 17 00:00:00 2001 From: Akmal Fairuz <35138228+AkmalFairuz@users.noreply.github.com> Date: Sun, 24 Nov 2024 18:32:30 +0700 Subject: [PATCH 3/5] fix --- src/entity/Living.php | 2 +- src/utils/Utils.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index 1f27a5cacf4..e3b290f1ce8 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -490,7 +490,7 @@ protected function applyPostDamageEffects(EntityDamageEvent $source) : void{ $helmet = $this->armorInventory->getHelmet(); if($helmet instanceof Armor){ $finalDamage = $source->getFinalDamage(); - $this->damageItem($helmet, (int) round($finalDamage * 4 + Utils::getRandomFloat() * $finalDamage * 2)); + $this->damageItem($helmet, (int) round($finalDamage * (4 + Utils::getRandomFloat(0.0, 2.0)))); $this->armorInventory->setHelmet($helmet); } } diff --git a/src/utils/Utils.php b/src/utils/Utils.php index cc93cfd1fd4..2653a5113a4 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -682,7 +682,6 @@ public static function getRandomFloat(float $min = 0.0, float $max = 1.0) : floa if($min > $max){ throw new \InvalidArgumentException("Minimum value cannot be greater than maximum value"); } - return $min + (mt_rand() / mt_getrandmax()) * ($max - $min); } } From 0bd4668c5e3aa527b948f852368691241d121332 Mon Sep 17 00:00:00 2001 From: AkmalFairuz Date: Sun, 24 Nov 2024 19:28:42 +0700 Subject: [PATCH 4/5] Remove parameters --- src/block/Liquid.php | 2 +- src/entity/Entity.php | 2 +- src/entity/Living.php | 2 +- src/item/SpawnEgg.php | 2 +- src/utils/Utils.php | 10 +++++----- src/world/World.php | 8 ++++---- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/block/Liquid.php b/src/block/Liquid.php index 2320feb3df5..a37019d650d 100644 --- a/src/block/Liquid.php +++ b/src/block/Liquid.php @@ -368,7 +368,7 @@ protected function checkForHarden() : bool{ protected function liquidCollide(Block $cause, Block $result) : bool{ if(BlockEventHelper::form($this, $result, $cause)){ - $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + Utils::getRandomFloat(-0.8, 0.8))); + $this->position->getWorld()->addSound($this->position->add(0.5, 0.5, 0.5), new FizzSound(2.6 + (Utils::getRandomFloat() - Utils::getRandomFloat()) * 0.8)); } return true; } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0ef446a7beb..a702ceabd22 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -905,7 +905,7 @@ protected function checkObstruction(float $x, float $y, float $z) : bool{ return false; } - $force = Utils::getRandomFloat(0.1, 0.3); + $force = Utils::getRandomFloat() * 0.2 + 0.1; $this->motion = match($direction){ Facing::WEST => $this->motion->withComponents(-$force, null, null), diff --git a/src/entity/Living.php b/src/entity/Living.php index e3b290f1ce8..1f27a5cacf4 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -490,7 +490,7 @@ protected function applyPostDamageEffects(EntityDamageEvent $source) : void{ $helmet = $this->armorInventory->getHelmet(); if($helmet instanceof Armor){ $finalDamage = $source->getFinalDamage(); - $this->damageItem($helmet, (int) round($finalDamage * (4 + Utils::getRandomFloat(0.0, 2.0)))); + $this->damageItem($helmet, (int) round($finalDamage * 4 + Utils::getRandomFloat() * $finalDamage * 2)); $this->armorInventory->setHelmet($helmet); } } diff --git a/src/item/SpawnEgg.php b/src/item/SpawnEgg.php index 0455c82192a..ab4f0e14983 100644 --- a/src/item/SpawnEgg.php +++ b/src/item/SpawnEgg.php @@ -35,7 +35,7 @@ abstract class SpawnEgg extends Item{ abstract protected function createEntity(World $world, Vector3 $pos, float $yaw, float $pitch) : Entity; public function onInteractBlock(Player $player, Block $blockReplace, Block $blockClicked, int $face, Vector3 $clickVector, array &$returnedItems) : ItemUseResult{ - $entity = $this->createEntity($player->getWorld(), $blockReplace->getPosition()->add(0.5, 0, 0.5), Utils::getRandomFloat(0.0, 360.0), 0); + $entity = $this->createEntity($player->getWorld(), $blockReplace->getPosition()->add(0.5, 0, 0.5), Utils::getRandomFloat() * 360, 0); if($this->hasCustomName()){ $entity->setNameTag($this->getCustomName()); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 2653a5113a4..f3276f954d8 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -678,10 +678,10 @@ function_exists('opcache_get_status') && return null; } - public static function getRandomFloat(float $min = 0.0, float $max = 1.0) : float{ - if($min > $max){ - throw new \InvalidArgumentException("Minimum value cannot be greater than maximum value"); - } - return $min + (mt_rand() / mt_getrandmax()) * ($max - $min); + /** + * Returns a random float between 0.0 and 1.0 + */ + public static function getRandomFloat() : float{ + return mt_rand() / mt_getrandmax(); } } diff --git a/src/world/World.php b/src/world/World.php index ab1ae6558e6..01385cae044 100644 --- a/src/world/World.php +++ b/src/world/World.php @@ -1998,10 +1998,10 @@ public function dropItem(Vector3 $source, Item $item, ?Vector3 $motion = null, i return null; } - $itemEntity = new ItemEntity(Location::fromObject($source, $this, Utils::getRandomFloat(0.0, 360.0), 0), $item); + $itemEntity = new ItemEntity(Location::fromObject($source, $this, Utils::getRandomFloat() * 360, 0), $item); $itemEntity->setPickupDelay($delay); - $itemEntity->setMotion($motion ?? new Vector3(Utils::getRandomFloat(-0.1, 0.1), 0.2, Utils::getRandomFloat(-0.1, 0.1))); + $itemEntity->setMotion($motion ?? new Vector3(Utils::getRandomFloat() * 0.2 - 0.1, 0.2, Utils::getRandomFloat() * 0.2 - 0.1)); $itemEntity->spawnToAll(); return $itemEntity; @@ -2018,9 +2018,9 @@ public function dropExperience(Vector3 $pos, int $amount) : array{ $orbs = []; foreach(ExperienceOrb::splitIntoOrbSizes($amount) as $split){ - $orb = new ExperienceOrb(Location::fromObject($pos, $this, Utils::getRandomFloat(0.0, 360.0), 0), $split); + $orb = new ExperienceOrb(Location::fromObject($pos, $this, Utils::getRandomFloat() * 360, 0), $split); - $orb->setMotion(new Vector3(Utils::getRandomFloat(-0.2, 0.2), Utils::getRandomFloat(0.0, 0.4), Utils::getRandomFloat(-0.2, 0.2))); + $orb->setMotion(new Vector3((Utils::getRandomFloat() * 0.2 - 0.1) * 2, Utils::getRandomFloat() * 0.4, (Utils::getRandomFloat() * 0.2 - 0.1) * 2)); $orb->spawnToAll(); $orbs[] = $orb; From 30b981992fd1b877a759ce3d3ca75770cea1af9c Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Tue, 26 Nov 2024 11:26:41 +0000 Subject: [PATCH 5/5] Update Utils.php --- src/utils/Utils.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/utils/Utils.php b/src/utils/Utils.php index a060ba1915b..c8be174d6a1 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -693,6 +693,7 @@ function_exists('opcache_get_status') && /** * Returns a random float between 0.0 and 1.0 + * Drop-in replacement for lcg_value() */ public static function getRandomFloat() : float{ return mt_rand() / mt_getrandmax();