From de11cce15455f9f394164927ffb69d72f33373ba Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Mon, 6 Oct 2014 13:10:59 +0200 Subject: [PATCH] Improved entity ticking --- src/pocketmine/entity/DroppedItem.php | 4 ++- src/pocketmine/entity/Entity.php | 6 +++- src/pocketmine/entity/FallingBlock.php | 2 ++ src/pocketmine/level/Level.php | 41 +++++++++++++++----------- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/pocketmine/entity/DroppedItem.php b/src/pocketmine/entity/DroppedItem.php index 06b1a358b..8968d1ae1 100644 --- a/src/pocketmine/entity/DroppedItem.php +++ b/src/pocketmine/entity/DroppedItem.php @@ -46,6 +46,8 @@ class DroppedItem extends Entity{ protected $gravity = 0.04; protected $drag = 0.02; + public $canCollide = false; + protected function initEntity(){ $this->namedtag->id = new String("id", "Item"); $this->setMaxHealth(5); @@ -87,7 +89,7 @@ class DroppedItem extends Entity{ $friction = 1 - $this->drag; - if($this->onGround){ + if($this->onGround and ($this->motionX != 0 or $this->motionZ != 0)){ $friction = $this->getLevel()->getBlock(new Vector3($this->getFloorX(), $this->getFloorY() - 1, $this->getFloorZ()))->frictionFactor * $friction; } diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 41380ee29..2e3e02d2f 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -134,6 +134,8 @@ abstract class Entity extends Position implements Metadatable{ public $fireTicks; public $airTicks; public $namedtag; + public $canCollide = true; + protected $isStatic = false; protected $isColliding = false; @@ -555,7 +557,9 @@ abstract class Entity extends Position implements Metadatable{ $pk->entities = [ [$this->getID(), $this->motionX, $this->motionY, $this->motionZ] ]; - Server::broadcastPacket($this->hasSpawned, $pk); + foreach($this->hasSpawned as $player){ + $player->dataPacket($pk); + } if($this instanceof Player){ $this->motionX = 0; diff --git a/src/pocketmine/entity/FallingBlock.php b/src/pocketmine/entity/FallingBlock.php index e9851daa9..785b6fa15 100644 --- a/src/pocketmine/entity/FallingBlock.php +++ b/src/pocketmine/entity/FallingBlock.php @@ -43,6 +43,8 @@ class FallingBlock extends Entity{ protected $drag = 0.02; protected $blockId = 0; + public $canCollide = false; + protected function initEntity(){ $this->namedtag->id = new String("id", "FallingSand"); if(isset($this->namedtag->Tile)){ diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 8173078e6..fe2bd908c 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -728,7 +728,10 @@ class Level implements ChunkManager, Metadatable{ for($z = $minZ; $z < $maxZ; ++$z){ for($x = $minX; $x < $maxX; ++$x){ for($y = $minY - 1; $y < $maxY; ++$y){ - $this->getBlock(new Vector3($x, $y, $z))->collidesWithBB($bb, $collides); + $block = $this->getBlock(new Vector3($x, $y, $z)); + if(!($block instanceof Air)){ + $block->collidesWithBB($bb, $collides); + } } } } @@ -767,11 +770,13 @@ class Level implements ChunkManager, Metadatable{ $collides = []; - //TODO: optimize this loop, check collision cube boundaries for($z = $minZ; $z < $maxZ; ++$z){ for($x = $minX; $x < $maxX; ++$x){ for($y = $minY - 1; $y < $maxY; ++$y){ - $this->getBlock(new Vector3($x, $y, $z))->collidesWithBB($bb, $collides); + $block = $this->getBlock(new Vector3($x, $y, $z)); + if(!($block instanceof Air)){ + $block->collidesWithBB($bb, $collides); + } } } } @@ -1233,7 +1238,7 @@ class Level implements ChunkManager, Metadatable{ } /** - * Returns the entities near the current one inside the AxisAlignedBB + * Returns the entities colliding the current one inside the AxisAlignedBB * * @param AxisAlignedBB $bb * @param Entity $entity @@ -1243,16 +1248,18 @@ class Level implements ChunkManager, Metadatable{ public function getCollidingEntities(AxisAlignedBB $bb, Entity $entity = null){ $nearby = []; - $minX = ($bb->minX - 2) >> 4; - $maxX = ($bb->maxX + 2) >> 4; - $minZ = ($bb->minZ - 2) >> 4; - $maxZ = ($bb->maxZ + 2) >> 4; + if($entity->canCollide){ + $minX = Math::floorFloat(($bb->minX - 2) / 16); + $maxX = Math::floorFloat(($bb->maxX - 2) / 16); + $minZ = Math::floorFloat(($bb->minZ - 2) / 16); + $maxZ = Math::floorFloat(($bb->maxZ - 2) / 16); - for($x = $minX; $x <= $maxX; ++$x){ - for($z = $minZ; $z <= $maxZ; ++$z){ - foreach($this->getChunkEntities($x, $z) as $ent){ - if($ent !== $entity and ($entity === null or $entity->canCollideWith($ent)) and $ent->boundingBox->intersectsWith($bb)){ - $nearby[] = $ent; + for($x = $minX; $x <= $maxX; ++$x){ + for($z = $minZ; $z <= $maxZ; ++$z){ + foreach($this->getChunkEntities($x, $z) as $ent){ + if($ent !== $entity and ($entity === null or $entity->canCollideWith($ent)) and $ent->boundingBox->intersectsWith($bb)){ + $nearby[] = $ent; + } } } } @@ -1272,10 +1279,10 @@ class Level implements ChunkManager, Metadatable{ public function getNearbyEntities(AxisAlignedBB $bb, Entity $entity = null){ $nearby = []; - $minX = ($bb->minX - 2) >> 4; - $maxX = ($bb->maxX + 2) >> 4; - $minZ = ($bb->minZ - 2) >> 4; - $maxZ = ($bb->maxZ + 2) >> 4; + $minX = Math::floorFloat(($bb->minX - 2) / 16); + $maxX = Math::floorFloat(($bb->maxX - 2) / 16); + $minZ = Math::floorFloat(($bb->minZ - 2) / 16); + $maxZ = Math::floorFloat(($bb->maxZ - 2) / 16); for($x = $minX; $x <= $maxX; ++$x){ for($z = $minZ; $z <= $maxZ; ++$z){