mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 08:44:01 +00:00
Replaced usages of Math::floorFloat() with (int) floor() and Math::ceilFloat() with (int) ceil()
Once upon a time, these userland functions were faster than calling builtins, but not anymore. According to my test the Math functions are twice slower in PHP 7.2 with typehints and 50% slower without typehints. Inlining is slightly faster than using builtins, but the difference is very small - not worth making the code look any more ugly than it does already.
This commit is contained in:
parent
71fdd59c4c
commit
4f8f334436
@ -1178,7 +1178,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
||||
}
|
||||
|
||||
if($this->onGround){
|
||||
$friction *= $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($this->y) - 1, Math::floorFloat($this->z))->getFrictionFactor();
|
||||
$friction *= $this->level->getBlockAt((int) floor($this->x), (int) floor($this->y - 1), (int) floor($this->z))->getFrictionFactor();
|
||||
}
|
||||
|
||||
$this->motion->x *= $friction;
|
||||
@ -1190,9 +1190,9 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
||||
return false;
|
||||
}
|
||||
|
||||
$floorX = Math::floorFloat($x);
|
||||
$floorY = Math::floorFloat($y);
|
||||
$floorZ = Math::floorFloat($z);
|
||||
$floorX = (int) floor($x);
|
||||
$floorY = (int) floor($y);
|
||||
$floorZ = (int) floor($z);
|
||||
|
||||
$diffX = $x - $floorX;
|
||||
$diffY = $y - $floorY;
|
||||
@ -1457,7 +1457,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
||||
}
|
||||
|
||||
public function isUnderwater() : bool{
|
||||
$block = $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z));
|
||||
$block = $this->level->getBlockAt((int) floor($this->x), (int) floor($y = ($this->y + $this->getEyeHeight())), (int) floor($this->z));
|
||||
|
||||
if($block instanceof Water){
|
||||
$f = ($block->y + 1) - ($block->getFluidHeightPercent() - 0.1111111);
|
||||
@ -1468,7 +1468,7 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
||||
}
|
||||
|
||||
public function isInsideOfSolid() : bool{
|
||||
$block = $this->level->getBlockAt(Math::floorFloat($this->x), Math::floorFloat($y = ($this->y + $this->getEyeHeight())), Math::floorFloat($this->z));
|
||||
$block = $this->level->getBlockAt((int) floor($this->x), (int) floor($y = ($this->y + $this->getEyeHeight())), (int) floor($this->z));
|
||||
|
||||
return $block->isSolid() and !$block->isTransparent() and $block->collidesWithBB($this->getBoundingBox());
|
||||
}
|
||||
@ -1676,12 +1676,12 @@ abstract class Entity extends Location implements Metadatable, EntityIds{
|
||||
if($this->blocksAround === null){
|
||||
$inset = 0.001; //Offset against floating-point errors
|
||||
|
||||
$minX = Math::floorFloat($this->boundingBox->minX + $inset);
|
||||
$minY = Math::floorFloat($this->boundingBox->minY + $inset);
|
||||
$minZ = Math::floorFloat($this->boundingBox->minZ + $inset);
|
||||
$maxX = Math::floorFloat($this->boundingBox->maxX - $inset);
|
||||
$maxY = Math::floorFloat($this->boundingBox->maxY - $inset);
|
||||
$maxZ = Math::floorFloat($this->boundingBox->maxZ - $inset);
|
||||
$minX = (int) floor($this->boundingBox->minX + $inset);
|
||||
$minY = (int) floor($this->boundingBox->minY + $inset);
|
||||
$minZ = (int) floor($this->boundingBox->minZ + $inset);
|
||||
$maxX = (int) floor($this->boundingBox->maxX - $inset);
|
||||
$maxY = (int) floor($this->boundingBox->maxY - $inset);
|
||||
$maxZ = (int) floor($this->boundingBox->maxZ - $inset);
|
||||
|
||||
$this->blocksAround = [];
|
||||
|
||||
|
@ -164,12 +164,12 @@ class Explosion{
|
||||
}
|
||||
|
||||
$explosionSize = $this->size * 2;
|
||||
$minX = Math::floorFloat($this->source->x - $explosionSize - 1);
|
||||
$maxX = Math::ceilFloat($this->source->x + $explosionSize + 1);
|
||||
$minY = Math::floorFloat($this->source->y - $explosionSize - 1);
|
||||
$maxY = Math::ceilFloat($this->source->y + $explosionSize + 1);
|
||||
$minZ = Math::floorFloat($this->source->z - $explosionSize - 1);
|
||||
$maxZ = Math::ceilFloat($this->source->z + $explosionSize + 1);
|
||||
$minX = (int) floor($this->source->x - $explosionSize - 1);
|
||||
$maxX = (int) ceil($this->source->x + $explosionSize + 1);
|
||||
$minY = (int) floor($this->source->y - $explosionSize - 1);
|
||||
$maxY = (int) ceil($this->source->y + $explosionSize + 1);
|
||||
$minZ = (int) floor($this->source->z - $explosionSize - 1);
|
||||
$maxZ = (int) ceil($this->source->z + $explosionSize + 1);
|
||||
|
||||
$explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
|
||||
|
||||
|
@ -1099,12 +1099,12 @@ class Level implements ChunkManager, Metadatable{
|
||||
* @return Block[]
|
||||
*/
|
||||
public function getCollisionBlocks(AxisAlignedBB $bb, bool $targetFirst = false) : array{
|
||||
$minX = Math::floorFloat($bb->minX - 1);
|
||||
$minY = Math::floorFloat($bb->minY - 1);
|
||||
$minZ = Math::floorFloat($bb->minZ - 1);
|
||||
$maxX = Math::floorFloat($bb->maxX + 1);
|
||||
$maxY = Math::floorFloat($bb->maxY + 1);
|
||||
$maxZ = Math::floorFloat($bb->maxZ + 1);
|
||||
$minX = (int) floor($bb->minX - 1);
|
||||
$minY = (int) floor($bb->minY - 1);
|
||||
$minZ = (int) floor($bb->minZ - 1);
|
||||
$maxX = (int) floor($bb->maxX + 1);
|
||||
$maxY = (int) floor($bb->maxY + 1);
|
||||
$maxZ = (int) floor($bb->maxZ + 1);
|
||||
|
||||
$collides = [];
|
||||
|
||||
@ -1162,12 +1162,12 @@ class Level implements ChunkManager, Metadatable{
|
||||
* @return AxisAlignedBB[]
|
||||
*/
|
||||
public function getCollisionCubes(Entity $entity, AxisAlignedBB $bb, bool $entities = true) : array{
|
||||
$minX = Math::floorFloat($bb->minX - 1);
|
||||
$minY = Math::floorFloat($bb->minY - 1);
|
||||
$minZ = Math::floorFloat($bb->minZ - 1);
|
||||
$maxX = Math::floorFloat($bb->maxX + 1);
|
||||
$maxY = Math::floorFloat($bb->maxY + 1);
|
||||
$maxZ = Math::floorFloat($bb->maxZ + 1);
|
||||
$minX = (int) floor($bb->minX - 1);
|
||||
$minY = (int) floor($bb->minY - 1);
|
||||
$minZ = (int) floor($bb->minZ - 1);
|
||||
$maxX = (int) floor($bb->maxX + 1);
|
||||
$maxY = (int) floor($bb->maxY + 1);
|
||||
$maxZ = (int) floor($bb->maxZ + 1);
|
||||
|
||||
$collides = [];
|
||||
|
||||
@ -1903,10 +1903,10 @@ class Level implements ChunkManager, Metadatable{
|
||||
$nearby = [];
|
||||
|
||||
if($entity === null or $entity->canCollide){
|
||||
$minX = Math::floorFloat($bb->minX - 2) >> 4;
|
||||
$maxX = Math::floorFloat($bb->maxX + 2) >> 4;
|
||||
$minZ = Math::floorFloat($bb->minZ - 2) >> 4;
|
||||
$maxZ = Math::floorFloat($bb->maxZ + 2) >> 4;
|
||||
$minX = ((int) floor($bb->minX - 2)) >> 4;
|
||||
$maxX = ((int) floor($bb->maxX + 2)) >> 4;
|
||||
$minZ = ((int) floor($bb->minZ - 2)) >> 4;
|
||||
$maxZ = ((int) floor($bb->maxZ + 2)) >> 4;
|
||||
|
||||
for($x = $minX; $x <= $maxX; ++$x){
|
||||
for($z = $minZ; $z <= $maxZ; ++$z){
|
||||
@ -1934,10 +1934,10 @@ class Level implements ChunkManager, Metadatable{
|
||||
public function getNearbyEntities(AxisAlignedBB $bb, Entity $entity = null) : array{
|
||||
$nearby = [];
|
||||
|
||||
$minX = Math::floorFloat($bb->minX - 2) >> 4;
|
||||
$maxX = Math::floorFloat($bb->maxX + 2) >> 4;
|
||||
$minZ = Math::floorFloat($bb->minZ - 2) >> 4;
|
||||
$maxZ = Math::floorFloat($bb->maxZ + 2) >> 4;
|
||||
$minX = ((int) floor($bb->minX - 2)) >> 4;
|
||||
$maxX = ((int) floor($bb->maxX + 2)) >> 4;
|
||||
$minZ = ((int) floor($bb->minZ - 2)) >> 4;
|
||||
$maxZ = ((int) floor($bb->maxZ + 2)) >> 4;
|
||||
|
||||
for($x = $minX; $x <= $maxX; ++$x){
|
||||
for($z = $minZ; $z <= $maxZ; ++$z){
|
||||
@ -1965,10 +1965,10 @@ class Level implements ChunkManager, Metadatable{
|
||||
public function getNearestEntity(Vector3 $pos, float $maxDistance, string $entityType = Entity::class, bool $includeDead = false) : ?Entity{
|
||||
assert(is_a($entityType, Entity::class, true));
|
||||
|
||||
$minX = Math::floorFloat($pos->x - $maxDistance) >> 4;
|
||||
$maxX = Math::floorFloat($pos->x + $maxDistance) >> 4;
|
||||
$minZ = Math::floorFloat($pos->z - $maxDistance) >> 4;
|
||||
$maxZ = Math::floorFloat($pos->z + $maxDistance) >> 4;
|
||||
$minX = ((int) floor($pos->x - $maxDistance)) >> 4;
|
||||
$maxX = ((int) floor($pos->x + $maxDistance)) >> 4;
|
||||
$minZ = ((int) floor($pos->z - $maxDistance)) >> 4;
|
||||
$maxZ = ((int) floor($pos->z + $maxDistance)) >> 4;
|
||||
|
||||
$currentTargetDistSq = $maxDistance ** 2;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user