Introduce Utils::getRandomFloat() (#6532)

Drop-in replacement for lcg_value() for PHP 8.4
This commit is contained in:
Akmal Fairuz 2024-11-26 18:33:29 +07:00 committed by GitHub
parent ae750b60d0
commit 269effcecf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 36 additions and 27 deletions

View File

@ -35,10 +35,10 @@ use pocketmine\math\AxisAlignedBB;
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 @@ class Anvil extends Transparent implements Fallable{
}
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{

View File

@ -31,8 +31,8 @@ use pocketmine\event\entity\EntityTrampleFarmlandEvent;
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 @@ class Farmland extends Transparent{
}
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()){

View File

@ -31,13 +31,13 @@ use pocketmine\item\Item;
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 @@ class ItemFrame extends Flowable{
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 @@ class ItemFrame extends Flowable{
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;
}

View File

@ -33,9 +33,9 @@ use pocketmine\item\Item;
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 @@ abstract class Liquid extends Transparent{
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() - Utils::getRandomFloat()) * 0.8));
}
return true;
}

View File

@ -75,7 +75,6 @@ use function deg2rad;
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;
@ -910,7 +909,7 @@ abstract class Entity{
return false;
}
$force = lcg_value() * 0.2 + 0.1;
$force = Utils::getRandomFloat() * 0.2 + 0.1;
$this->motion = match($direction){
Facing::WEST => $this->motion->withComponents(-$force, null, null),

View File

@ -58,6 +58,7 @@ use pocketmine\network\mcpe\protocol\types\entity\EntityMetadataProperties;
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 ceil;
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 @@ abstract class Living extends Entity{
$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 @@ abstract class Living extends Entity{
$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){

View File

@ -33,7 +33,7 @@ use pocketmine\nbt\tag\CompoundTag;
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 @@ class Armor extends Durable{
$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++;
}
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\item;
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 @@ abstract class Durable extends Item{
$chance = 1 / ($unbreakingLevel + 1);
for($i = 0; $i < $amount; ++$i){
if(lcg_value() > $chance){
if(Utils::getRandomFloat() > $chance){
$negated++;
}
}

View File

@ -25,7 +25,7 @@ namespace pocketmine\item;
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 @@ class RottenFlesh extends Food{
}
public function getAdditionalEffects() : array{
if(lcg_value() <= 0.8){
if(Utils::getRandomFloat() <= 0.8){
return [
new EffectInstance(VanillaEffects::HUNGER(), 600)
];

View File

@ -27,15 +27,15 @@ use pocketmine\block\Block;
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() * 360, 0);
if($this->hasCustomName()){
$entity->setNameTag($this->getCustomName());

View File

@ -67,6 +67,8 @@ use function is_nan;
use function is_object;
use function is_string;
use function mb_check_encoding;
use function mt_getrandmax;
use function mt_rand;
use function ob_end_clean;
use function ob_get_contents;
use function ob_start;
@ -688,4 +690,12 @@ final class Utils{
//jit not available
return null;
}
/**
* 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();
}
}

View File

@ -83,6 +83,7 @@ use pocketmine\ServerConfigGroup;
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 get_class;
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 @@ class World implements ChunkManager{
return null;
}
$itemEntity = new ItemEntity(Location::fromObject($source, $this, lcg_value() * 360, 0), $item);
$itemEntity = new ItemEntity(Location::fromObject($source, $this, Utils::getRandomFloat() * 360, 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.2 - 0.1, 0.2, Utils::getRandomFloat() * 0.2 - 0.1));
$itemEntity->spawnToAll();
return $itemEntity;
@ -2018,9 +2018,9 @@ class World implements ChunkManager{
$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() * 360, 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.1) * 2, Utils::getRandomFloat() * 0.4, (Utils::getRandomFloat() * 0.2 - 0.1) * 2));
$orb->spawnToAll();
$orbs[] = $orb;