From caf49372224632506a883905d718c07d37c520a1 Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Wed, 26 Apr 2017 18:55:48 +0100 Subject: [PATCH] Fixed arrow flight and spawning bugs, fixed critical trail, close #420 --- src/pocketmine/Player.php | 5 +- src/pocketmine/entity/Arrow.php | 31 ++++++++--- src/pocketmine/entity/Entity.php | 2 +- src/pocketmine/entity/Projectile.php | 77 +++++++++++++++------------- 4 files changed, 69 insertions(+), 46 deletions(-) diff --git a/src/pocketmine/Player.php b/src/pocketmine/Player.php index 43aec00cb..1b1521cdc 100644 --- a/src/pocketmine/Player.php +++ b/src/pocketmine/Player.php @@ -2620,8 +2620,9 @@ class Player extends Human implements CommandSender, InventoryHolder, ChunkLoade new DoubleTag("", cos($this->yaw / 180 * M_PI) * cos($this->pitch / 180 * M_PI)) ]), "Rotation" => new ListTag("Rotation", [ - new FloatTag("", $this->yaw), - new FloatTag("", $this->pitch) + //yaw/pitch for arrows taken crosswise, not along the arrow shaft. + new FloatTag("", ($this->yaw > 180 ? 360 : 0) - $this->yaw), //arrow yaw must range from -180 to +180 + new FloatTag("", -$this->pitch) ]), "Fire" => new ShortTag("Fire", $this->isOnFire() ? 45 * 60 : 0) ]); diff --git a/src/pocketmine/entity/Arrow.php b/src/pocketmine/entity/Arrow.php index a31a66b9e..4935ff29a 100644 --- a/src/pocketmine/entity/Arrow.php +++ b/src/pocketmine/entity/Arrow.php @@ -44,6 +44,24 @@ class Arrow extends Projectile{ public function __construct(Level $level, CompoundTag $nbt, Entity $shootingEntity = null, $critical = false){ $this->isCritical = (bool) $critical; parent::__construct($level, $nbt, $shootingEntity); + $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL, $critical); + } + + public function isCritical() : bool{ + return $this->getDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL); + } + + public function setCritical(bool $value = true){ + $this->setDataFlag(self::DATA_FLAGS, self::DATA_FLAG_CRITICAL, $value); + } + + public function getResultDamage() : int{ + $base = parent::getResultDamage(); + if($this->isCritical()){ + return ($base + mt_rand(0, (int) ($base / 2) + 1)); + }else{ + return $base; + } } public function onUpdate($currentTick){ @@ -55,17 +73,12 @@ class Arrow extends Projectile{ $hasUpdate = parent::onUpdate($currentTick); - if(!$this->hadCollision and $this->isCritical){ - $this->level->addParticle(new CriticalParticle($this->add( - $this->width / 2 + mt_rand(-100, 100) / 500, - $this->height / 2 + mt_rand(-100, 100) / 500, - $this->width / 2 + mt_rand(-100, 100) / 500))); - }elseif($this->onGround){ - $this->isCritical = false; + if($this->onGround or $this->hadCollision){ + $this->setCritical(false); } if($this->age > 1200){ - $this->kill(); + $this->close(); $hasUpdate = true; } @@ -84,6 +97,8 @@ class Arrow extends Projectile{ $pk->speedX = $this->motionX; $pk->speedY = $this->motionY; $pk->speedZ = $this->motionZ; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; $pk->metadata = $this->dataProperties; $player->dataPacket($pk); diff --git a/src/pocketmine/entity/Entity.php b/src/pocketmine/entity/Entity.php index 8bebf5d46..2a7d2aead 100644 --- a/src/pocketmine/entity/Entity.php +++ b/src/pocketmine/entity/Entity.php @@ -1118,7 +1118,7 @@ abstract class Entity extends Location implements Metadatable{ $this->lastYaw = $this->yaw; $this->lastPitch = $this->pitch; - $this->level->addEntityMovement($this->chunk->getX(), $this->chunk->getZ(), $this->id, $this->x, $this->y + $this->getEyeHeight(), $this->z, $this->yaw, $this->pitch, $this->yaw); + $this->level->addEntityMovement($this->chunk->getX(), $this->chunk->getZ(), $this->id, $this->x, $this->y, $this->z, $this->yaw, $this->pitch, $this->yaw); } if($diffMotion > 0.0025 or ($diffMotion > 0.0001 and $this->getMotion()->lengthSquared() <= 0.0001)){ //0.05 ** 2 diff --git a/src/pocketmine/entity/Projectile.php b/src/pocketmine/entity/Projectile.php index fc21eb32e..33fdb737c 100644 --- a/src/pocketmine/entity/Projectile.php +++ b/src/pocketmine/entity/Projectile.php @@ -64,13 +64,46 @@ abstract class Projectile extends Entity{ if(isset($this->namedtag->Age)){ $this->age = $this->namedtag["Age"]; } - } public function canCollideWith(Entity $entity){ return $entity instanceof Living and !$this->onGround; } + /** + * Returns the amount of damage this projectile will deal to the entity it hits. + * @return int + */ + public function getResultDamage() : int{ + return ceil(sqrt($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2) * $this->damage); + } + + public function onCollideWithEntity(Entity $entity){ + $this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); + + $damage = $this->getResultDamage(); + + if($this->shootingEntity === null){ + $ev = new EntityDamageByEntityEvent($this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage); + }else{ + $ev = new EntityDamageByChildEntityEvent($this->shootingEntity, $this, $entity, EntityDamageEvent::CAUSE_PROJECTILE, $damage); + } + + $entity->attack($ev->getFinalDamage(), $ev); + + $this->hadCollision = true; + + if($this->fireTicks > 0){ + $ev = new EntityCombustByEntityEvent($this, $entity, 5); + $this->server->getPluginManager()->callEvent($ev); + if(!$ev->isCancelled()){ + $entity->setOnFire($ev->getDuration()); + } + } + + $this->close(); + } + public function saveNBT(){ parent::saveNBT(); $this->namedtag->Age = new ShortTag("Age", $this->age); @@ -133,42 +166,14 @@ abstract class Projectile extends Entity{ if($movingObjectPosition !== null){ if($movingObjectPosition->entityHit !== null){ - - $this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); - - $motion = sqrt($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2); - $damage = ceil($motion * $this->damage); - - if($this instanceof Arrow and $this->isCritical){ - $damage += mt_rand(0, (int) ($damage / 2) + 1); - } - - if($this->shootingEntity === null){ - $ev = new EntityDamageByEntityEvent($this, $movingObjectPosition->entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage); - }else{ - $ev = new EntityDamageByChildEntityEvent($this->shootingEntity, $this, $movingObjectPosition->entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage); - } - - $movingObjectPosition->entityHit->attack($ev->getFinalDamage(), $ev); - - $this->hadCollision = true; - - if($this->fireTicks > 0){ - $ev = new EntityCombustByEntityEvent($this, $movingObjectPosition->entityHit, 5); - $this->server->getPluginManager()->callEvent($ev); - if(!$ev->isCancelled()){ - $movingObjectPosition->entityHit->setOnFire($ev->getDuration()); - } - } - - $this->kill(); - return true; + $this->onCollideWithEntity($movingObjectPosition->entityHit); + return false; } } $this->move($this->motionX, $this->motionY, $this->motionZ); - if($this->isCollided and !$this->hadCollision){ + if($this->isCollided and !$this->hadCollision){ //Collided with a block $this->hadCollision = true; $this->motionX = 0; @@ -176,11 +181,14 @@ abstract class Projectile extends Entity{ $this->motionZ = 0; $this->server->getPluginManager()->callEvent(new ProjectileHitEvent($this)); - }elseif(!$this->isCollided and $this->hadCollision){ + return false; + }elseif(!$this->isCollided and $this->hadCollision){ //Collided with block, but block later removed + //This currently doesn't work because the arrow's motion is all zeros when it's hit a block, so move() doesn't do any collision checks. + //TODO: fix this $this->hadCollision = false; } - if(!$this->onGround or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001){ + if(!$this->hadCollision or abs($this->motionX) > 0.00001 or abs($this->motionY) > 0.00001 or abs($this->motionZ) > 0.00001){ $f = sqrt(($this->motionX ** 2) + ($this->motionZ ** 2)); $this->yaw = (atan2($this->motionX, $this->motionZ) * 180 / M_PI); $this->pitch = (atan2($this->motionY, $f) * 180 / M_PI); @@ -188,7 +196,6 @@ abstract class Projectile extends Entity{ } $this->updateMovement(); - } return $hasUpdate;