From 0af3dfedd53886af3bc002b60b24b544bd83cef2 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Sat, 11 Oct 2014 17:35:13 +0200 Subject: [PATCH] Improved Living entity ticking --- src/pocketmine/block/Block.php | 1 + src/pocketmine/block/Cactus.php | 3 +++ src/pocketmine/block/Cobweb.php | 3 +++ src/pocketmine/block/Fire.php | 3 +++ src/pocketmine/block/Ladder.php | 4 ++++ src/pocketmine/block/Lava.php | 1 + src/pocketmine/block/Liquid.php | 2 ++ src/pocketmine/block/Vine.php | 3 +++ src/pocketmine/entity/Entity.php | 20 ++++++++++++++------ src/pocketmine/level/Level.php | 11 ++++++++--- 10 files changed, 42 insertions(+), 9 deletions(-) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index c22273b46..85b97fab1 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -513,6 +513,7 @@ abstract class Block extends Position implements Metadatable{ protected $name = "Unknown"; protected $breakTime = 0.20; protected $hardness = 10; + public $hasEntityCollision = false; public $isActivable = false; public $breakable = true; public $isFlowable = false; diff --git a/src/pocketmine/block/Cactus.php b/src/pocketmine/block/Cactus.php index d94f70a4b..959053688 100644 --- a/src/pocketmine/block/Cactus.php +++ b/src/pocketmine/block/Cactus.php @@ -32,6 +32,9 @@ use pocketmine\Player; use pocketmine\Server; class Cactus extends Transparent{ + + public $hasEntityCollision = true; + public function __construct($meta = 0){ parent::__construct(self::CACTUS, $meta, "Cactus"); $this->isFullBlock = false; diff --git a/src/pocketmine/block/Cobweb.php b/src/pocketmine/block/Cobweb.php index e033c55ec..96c711525 100644 --- a/src/pocketmine/block/Cobweb.php +++ b/src/pocketmine/block/Cobweb.php @@ -25,6 +25,9 @@ use pocketmine\entity\Entity; use pocketmine\item\Item; class Cobweb extends Flowable{ + + public $hasEntityCollision = true; + public function __construct(){ parent::__construct(self::COBWEB, 0, "Cobweb"); $this->isSolid = true; diff --git a/src/pocketmine/block/Fire.php b/src/pocketmine/block/Fire.php index a2226f8f1..7670c4c29 100644 --- a/src/pocketmine/block/Fire.php +++ b/src/pocketmine/block/Fire.php @@ -30,6 +30,9 @@ use pocketmine\level\Level; use pocketmine\Server; class Fire extends Flowable{ + + public $hasEntityCollision = true; + public function __construct($meta = 0){ parent::__construct(self::FIRE, $meta, "Fire"); $this->isReplaceable = true; diff --git a/src/pocketmine/block/Ladder.php b/src/pocketmine/block/Ladder.php index 7fe2a3096..71cf358a0 100644 --- a/src/pocketmine/block/Ladder.php +++ b/src/pocketmine/block/Ladder.php @@ -28,6 +28,9 @@ use pocketmine\math\AxisAlignedBB; use pocketmine\Player; class Ladder extends Transparent{ + + public $hasEntityCollision = true; + public function __construct($meta = 0){ parent::__construct(self::LADDER, $meta, "Ladder"); $this->isSolid = false; @@ -37,6 +40,7 @@ class Ladder extends Transparent{ public function onEntityCollide(Entity $entity){ $entity->fallDistance = 0; + $entity->onGround = true; } public function getBoundingBox(){ diff --git a/src/pocketmine/block/Lava.php b/src/pocketmine/block/Lava.php index 9ea68fe96..b1bcbadb6 100644 --- a/src/pocketmine/block/Lava.php +++ b/src/pocketmine/block/Lava.php @@ -30,6 +30,7 @@ use pocketmine\Player; use pocketmine\Server; class Lava extends Liquid{ + public function __construct($meta = 0){ parent::__construct(self::LAVA, $meta, "Lava"); $this->hardness = 0; diff --git a/src/pocketmine/block/Liquid.php b/src/pocketmine/block/Liquid.php index 2d4858611..489224e6a 100644 --- a/src/pocketmine/block/Liquid.php +++ b/src/pocketmine/block/Liquid.php @@ -28,6 +28,8 @@ use pocketmine\level\Level; use pocketmine\math\Vector3; abstract class Liquid extends Transparent{ + public $hasEntityCollision = true; + public $isLiquid = true; public $breakable = false; public $isReplaceable = true; diff --git a/src/pocketmine/block/Vine.php b/src/pocketmine/block/Vine.php index 63dd3adaf..d7b2e5ba3 100644 --- a/src/pocketmine/block/Vine.php +++ b/src/pocketmine/block/Vine.php @@ -29,6 +29,9 @@ use pocketmine\math\AxisAlignedBB; use pocketmine\Player; class Vine extends Transparent{ + + public $hasEntityCollision = true; + public function __construct($meta = 0){ parent::__construct(self::VINE, $meta, "Vines"); $this->isSolid = false; diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 350f47482..b69e59a42 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -749,11 +749,17 @@ abstract class Entity extends Position implements Metadatable{ } public function isInsideOfSolid(){ + $blocks = []; for($i = 0; $i < 8; ++$i){ $x = (($i % 2) - 0.5) * $this->width * 0.8; $y = ((($i >> 1) % 2) - 0.5) * 0.1; $z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; - $block = $this->getLevel()->getBlock((new Vector3($this->x + $x, $this->y + $this->getEyeHeight() + $y, $this->z + $z))->floor()); + $v = (new Vector3($this->x + $x, $this->y + $this->getEyeHeight() + $y, $this->z + $z))->floor(); + $blocks["{$v->x}:{$v->y}:{$v->z}"] = $v; + } + + foreach($blocks as $vector){ + $block = $this->getLevel()->getBlock($vector); $bb = $block->getBoundingBox(); @@ -830,7 +836,7 @@ abstract class Entity extends Position implements Metadatable{ //TODO: big messy loop }*/ - $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, $dy, $dz)); + $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($dx, $dy, $dz), false); foreach($list as $bb){ @@ -884,7 +890,7 @@ abstract class Entity extends Position implements Metadatable{ $this->boundingBox->setBB($axisalignedbb); - $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($movX, $dy, $movZ)); + $list = $this->getLevel()->getCollisionCubes($this, $this->boundingBox->getOffsetBoundingBox($movX, $dy, $movZ), false); foreach($list as $bb){ $dy = $bb->calculateYOffset($this->boundingBox, $dy); @@ -955,7 +961,9 @@ abstract class Entity extends Position implements Metadatable{ if($this instanceof Player){ if(($this->onGround and $movY != 0) or (!$this->onGround and $movY <= 0)){ - if(count($this->getLevel()->getCollisionBlocks($this->boundingBox->getOffsetBoundingBox(0, $movY - 0.1, 0)->expand(0.01, 0, 0.01))) > 0){ + $bb = clone $this->boundingBox; + $bb->maxY = $bb->minY + 0.5; + if(count($this->getLevel()->getCollisionBlocks($bb->expand(0.01, 0.01, 0.01))) > 0){ $isColliding = true; }else{ $isColliding = false; @@ -1003,7 +1011,7 @@ abstract class Entity extends Position implements Metadatable{ for($x = $minX; $x <= $maxX; ++$x){ for($y = $minY; $y <= $maxY; ++$y){ $block = $this->getLevel()->getBlock(new Vector3($x, $y, $z)); - if($block !== null and $block->getID() > 0){ + if($block !== null and $block->getID() > 0 and $block->hasEntityCollision){ $block->onEntityCollide($this); if(!($this instanceof Player)){ $block->addVelocityToEntity($this, $vector); @@ -1013,7 +1021,7 @@ abstract class Entity extends Position implements Metadatable{ } } - if($vector->length() > 0){ + if(!($this instanceof Player) and $vector->length() > 0){ $vector = $vector->normalize(); $d = 0.014; $this->motionX += $vector->x * $d; diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 52c1ee07e..ca7bfac90 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -756,10 +756,11 @@ class Level implements ChunkManager, Metadatable{ /** * @param Entity $entity * @param AxisAlignedBB $bb + * @param boolean $entities * * @return AxisAlignedBB[] */ - public function getCollisionCubes(Entity $entity, AxisAlignedBB $bb){ + public function getCollisionCubes(Entity $entity, AxisAlignedBB $bb, $entities = true){ $minX = Math::floorFloat($bb->minX); $minY = Math::floorFloat($bb->minY); $minZ = Math::floorFloat($bb->minZ); @@ -780,8 +781,10 @@ class Level implements ChunkManager, Metadatable{ } } - foreach($this->getCollidingEntities($bb->grow(0.25, 0.25, 0.25), $entity) as $ent){ - $collides[] = clone $ent->boundingBox; + if($entities){ + foreach($this->getCollidingEntities($bb->grow(0.25, 0.25, 0.25), $entity) as $ent){ + $collides[] = clone $ent->boundingBox; + } } return $collides; @@ -1690,6 +1693,8 @@ class Level implements ChunkManager, Metadatable{ } unset($this->chunkSendQueue[$index]); unset($this->chunkSendTasks[$index]); + }else{ + var_dump("ARGH"); } }