Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce Utils::getRandomFloat() #6532

Merged
merged 6 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/block/Anvil.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions src/block/Farmland.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()){
Expand Down
6 changes: 3 additions & 3 deletions src/block/ItemFrame.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions src/block/Liquid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions src/entity/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.3);

$this->motion = match($direction){
Facing::WEST => $this->motion->withComponents(-$force, null, null),
Expand Down
6 changes: 3 additions & 3 deletions src/entity/Living.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(0.0, 2.0))));
$this->armorInventory->setHelmet($helmet);
}
}
Expand Down Expand Up @@ -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){
Expand Down
4 changes: 2 additions & 2 deletions src/item/Armor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/item/Durable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/item/RottenFlesh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Expand All @@ -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)
];
Expand Down
4 changes: 2 additions & 2 deletions src/item/SpawnEgg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
9 changes: 9 additions & 0 deletions src/utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
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;
Expand Down Expand Up @@ -675,4 +677,11 @@ 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);
}
}
10 changes: 5 additions & 5 deletions src/world/World.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down