diff --git a/src/Player.php b/src/Player.php index 0a3e7951..864a099a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1879,7 +1879,7 @@ class Player{ * powerMax is 1, powerMin is 0.1 * args: xvel, yvel, zvel, (power+power)*1.5, 1.0 */ - $e->critical = ($power === 1); + $e->critical = ($power == 1); $e->shoot($e->speedX, $e->speedY, $e->speedZ, ($power+$power) * 1.5, 1.0); $this->server->api->entity->spawnToAll($e); } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index e1dd9c88..f3a635f9 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -10,7 +10,6 @@ class Entity extends Position public $counter = 0; public $fallDistance = 0; public static $updateOnTick, $allowedAI; - public $isCollidable; public $canBeAttacked; public $moveTime, $lookTime, $idleTime, $knockbackTime = 0; public $needsUpdate = true; @@ -167,6 +166,7 @@ class Entity extends Position $this->hasGravity = true; $this->canBeAttacked = true; $this->fireResistance = 20; + break; case ENTITY_OBJECT: $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; @@ -367,6 +367,14 @@ class Entity extends Position } } + /** + * Can entity be collided with or not + * @return boolean + */ + public function isPickable(){ + return $this->isPlayer() ? !$this->dead : false; + } + public function handleLavaMovement(){ //TODO maybe try merging with water? $this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1)); return $this->inLava; diff --git a/src/entity/Living.php b/src/entity/Living.php index 5ed6ac24..c912c351 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -30,6 +30,10 @@ abstract class Living extends Entity implements Pathfindable{ //if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 } + public function isPickable(){ + return !$this->dead; + } + public function fall(){ $dmg = ceil($this->fallDistance - 3); if($dmg > 0){ diff --git a/src/entity/ai/tasks/TaskEatTileGoal.php b/src/entity/ai/tasks/TaskEatTileGoal.php index 3dc4a9ec..880f06d3 100644 --- a/src/entity/ai/tasks/TaskEatTileGoal.php +++ b/src/entity/ai/tasks/TaskEatTileGoal.php @@ -15,7 +15,12 @@ class TaskEatTileGoal extends TaskBase public function onUpdate(EntityAI $ai) { - if (--$this->selfCounter === 4) + if($ai->isStarted("TaskEatTileGoal")){ + $this->reset(); + return false; + } + + if (--$this->selfCounter == 4) { $id = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y, $ai->entity->z); $idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z); @@ -32,7 +37,7 @@ class TaskEatTileGoal extends TaskBase public function canBeExecuted(EntityAI $ai) { - if($ai->isStarted("TaskRandomWalk")) return false; + if($ai->isStarted("TaskRandomWalk") || $ai->isStarted("TaskEatTileGoal")) return false; if(mt_rand(0, ($ai->entity instanceof Ageable && $ai->entity->isBaby()) ? 50 : 1000) == 0){ $idm = $ai->entity->level->level->getBlock($ai->entity->x, $ai->entity->y, $ai->entity->z); $idb = $ai->entity->level->level->getBlockID($ai->entity->x, $ai->entity->y - 1, $ai->entity->z); diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index f2e07c61..5e5993c0 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -58,19 +58,7 @@ class Arrow extends Projectile{ //$this->update(); //TODO i guess? $ticksInGround = 0; } - public function sendMoveUpdate() - { - $pk = new MoveEntityPacket_PosRot(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - foreach($this->level->players as $p){ //sending packets directly makes movement less laggy - $p->directDataPacket(clone $pk); - } - } + public function update($now){ $this->lastX = $this->x; $this->lastY = $this->y; @@ -115,20 +103,13 @@ class Arrow extends Projectile{ $this->lastPitch = $this->pitch = atan2($this->speedY, $v1) * 180 / M_PI; } - $v16 = $this->level->level->getBlockID($this->xTile, $this->yTile, $this->zTile); - if($v16 > 0){ - //TODO this->inGround = true + checks - } - if($this->shake > 0){ --$this->shake; } if($this->inGround){ - - $this->speedZ = $this->speedX = $this->speedY = 0; - [$blockID, $blockMeta] = $this->level->level->getBlock($this->xTile, $this->yTile, $this->zTile); + $this->speedZ = $this->speedX = $this->speedY = 0; if($blockID == $this->inTile && $blockMeta == $this->inData){ ++$this->groundTicks; @@ -159,10 +140,54 @@ class Arrow extends Projectile{ $end = new Vector3($this->x + $this->speedX, $this->y + $this->speedY, $this->z + $this->speedZ); } + + $entities = $this->level->getEntitiesInAABB($this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(1, 1, 1)); + $bestDist = 0; + $bestEnt = null; + + foreach($entities as $eid => $ent){ + if($eid != $this->eid && $ent->isPickable() && ($eid != $this->shooterEID || $this->airTicks >= 5)){ + + $v12 = $ent->boundingBox->expand(0.3, 0.3, 0.3); + $v13 = $v12->calculateIntercept($start, $end); + + if($v13 != null){ + $dist = $start->distance($v13->hitVector); + + if($dist < $bestDist || $bestDist == 0){ + $bestEnt = $ent; + } + } + } + } + + if($bestEnt != null){ + $v4 = MovingObjectPosition::fromEntity($bestEnt); + } + //TODO entity collisions if($v4 != null){ if($v4->entityHit != null){ //TODO entity hit + $v49 = sqrt($this->speedY*$this->speedY + $this->speedX*$this->speedX + $this->speedZ*$this->speedZ); + $damage = ceil($v49+$v49); + + if($this->criticial){ + $damage += mt_rand(0, $damage / 2 + 1); + } + + + if($v4->entityHit->harm($damage, $this->eid)){ + //vanilla seems to increase arrow count if $v4->entity is mob + $this->close(); + }else{ + $this->speedX *= -0.1; + $this->speedY *= -0.1; + $this->speedZ *= -0.1; + $this->yaw += 180; + $this->lastYaw += 180; + $this->airTicks = 0; + } }else{ $this->xTile = $v4->blockX; $this->yTile = $v4->blockY; diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 10291dca..d9e76137 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -16,6 +16,10 @@ class FallingSand extends Entity{ $this->gravity = 0.04; } + public function isPickable(){ + return !$this->dead; + } + public function update($now){ if($this->closed) return; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 32145414..8fc8156c 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -68,6 +68,10 @@ class Minecart extends Vehicle{ $this->yOffset = $this->height / 2; } + public function isPickable(){ + return !$this->dead; + } + public function getDrops(){ return [ [MINECART, 0, 1] diff --git a/src/entity/object/Painting.php b/src/entity/object/Painting.php index c43b402e..5c43fe41 100644 --- a/src/entity/object/Painting.php +++ b/src/entity/object/Painting.php @@ -20,6 +20,10 @@ class Painting extends Entity{ ]; } + public function isPickable(){ + return true; + } + public function createSaveData(){ $data = parent::createSaveData(); diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 34cbe170..b25578a3 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -16,6 +16,10 @@ class PrimedTNT extends Entity{ //$this->server->schedule(5, [$this, "updateFuse"], [], true); } + public function isPickable(){ + return !$this->dead; + } + public function getMetadata(){ $d = parent::getMetadata(); $d[16]["value"] = (int) $this->data["fuse"]; diff --git a/src/entity/object/TripodCamera.php b/src/entity/object/TripodCamera.php index ec4baed9..6855cd3c 100644 --- a/src/entity/object/TripodCamera.php +++ b/src/entity/object/TripodCamera.php @@ -8,6 +8,10 @@ class TripodCamera extends Entity{ $this->setHealth(PHP_INT_MAX, "generic"); } + public function isPickable(){ + return !$this->dead; + } + public function interactWith(Entity $e, $action){ if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){ //todo diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index 5415c001..7708fe3b 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -220,14 +220,19 @@ class AxisAlignedBB{ public function isVectorInXY(Vector3 $vector){ return $vector->x >= $this->minX and $vector->x <= $this->maxX and $vector->y >= $this->minY and $vector->y <= $this->maxY; } - + + /** + * @param Vector3 $pos1 + * @param Vector3 $pos2 + * @return NULL|MovingObjectPosition + */ public function calculateIntercept(Vector3 $pos1, Vector3 $pos2){ - $v1 = $pos1->getIntermediateWithXValue($pos2, $this->minX); - $v2 = $pos1->getIntermediateWithXValue($pos2, $this->maxX); - $v3 = $pos1->getIntermediateWithYValue($pos2, $this->minY); - $v4 = $pos1->getIntermediateWithYValue($pos2, $this->maxY); - $v5 = $pos1->getIntermediateWithZValue($pos2, $this->minZ); - $v6 = $pos1->getIntermediateWithZValue($pos2, $this->maxZ); + $v1 = $pos1->clipX($pos2, $this->minX); + $v2 = $pos1->clipX($pos2, $this->maxX); + $v3 = $pos1->clipY($pos2, $this->minY); + $v4 = $pos1->clipY($pos2, $this->maxY); + $v5 = $pos1->clipZ($pos2, $this->minZ); + $v6 = $pos1->clipZ($pos2, $this->maxZ); if($v1 !== null and !$this->isVectorInYZ($v1)){ $v1 = null; diff --git a/src/math/Vector3.php b/src/math/Vector3.php index 852ccf2e..f72e0461 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -215,7 +215,7 @@ class Vector3{ } } public function getIntermediateWithXValue(Vector3 $v, $y){ - return $this->clipZ($v, $y); + return $this->clipX($v, $y); } /** diff --git a/src/world/Level.php b/src/world/Level.php index 939564d6..3f3cfb5b 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -630,7 +630,10 @@ class Level{ } return $ents; } - + /** + * @param AxisAlignedBB $bb + * @return Entity[] + */ public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int)($bb->minX)) >> 4; $minChunkZ = ((int)($bb->minZ)) >> 4;