PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
Projectile.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\entity\projectile;
25
46use function assert;
47use function atan2;
48use function ceil;
49use function count;
50use function sqrt;
51use const M_PI;
52use const PHP_INT_MAX;
53
54abstract class Projectile extends Entity{
55 private const TAG_STUCK_ON_BLOCK_POS = "StuckToBlockPos";
56 private const TAG_DAMAGE = "damage"; //TAG_Double
57 private const TAG_TILE_X = "tileX"; //TAG_Int
58 private const TAG_TILE_Y = "tileY"; //TAG_Int
59 private const TAG_TILE_Z = "tileZ"; //TAG_Int
60
61 protected float $damage = 0.0;
62 protected ?Vector3 $blockHit = null;
63
64 public function __construct(Location $location, ?Entity $shootingEntity, ?CompoundTag $nbt = null){
65 parent::__construct($location, $nbt);
66 if($shootingEntity !== null){
67 $this->setOwningEntity($shootingEntity);
68 }
69 }
70
71 public function attack(EntityDamageEvent $source) : void{
72 if($source->getCause() === EntityDamageEvent::CAUSE_VOID){
73 parent::attack($source);
74 }
75 }
76
77 protected function initEntity(CompoundTag $nbt) : void{
78 parent::initEntity($nbt);
79
80 $this->setMaxHealth(1);
81 $this->setHealth(1);
82 $this->damage = $nbt->getDouble(self::TAG_DAMAGE, $this->damage);
83
84 if(($stuckOnBlockPosTag = $nbt->getListTag(self::TAG_STUCK_ON_BLOCK_POS)) !== null){
85 if($stuckOnBlockPosTag->getTagType() !== NBT::TAG_Int || count($stuckOnBlockPosTag) !== 3){
86 throw new SavedDataLoadingException(self::TAG_STUCK_ON_BLOCK_POS . " tag should be a list of 3 TAG_Int");
87 }
88
90 $values = $stuckOnBlockPosTag->getValue();
91
92 $this->blockHit = new Vector3($values[0]->getValue(), $values[1]->getValue(), $values[2]->getValue());
93 }elseif(($tileXTag = $nbt->getTag(self::TAG_TILE_X)) instanceof IntTag && ($tileYTag = $nbt->getTag(self::TAG_TILE_Y)) instanceof IntTag && ($tileZTag = $nbt->getTag(self::TAG_TILE_Z)) instanceof IntTag){
94 $this->blockHit = new Vector3($tileXTag->getValue(), $tileYTag->getValue(), $tileZTag->getValue());
95 }
96 }
97
98 public function canCollideWith(Entity $entity) : bool{
99 return $entity instanceof Living && !$this->onGround;
100 }
101
102 public function canBeCollidedWith() : bool{
103 return false;
104 }
105
110 public function getBaseDamage() : float{
111 return $this->damage;
112 }
113
117 public function setBaseDamage(float $damage) : void{
118 $this->damage = $damage;
119 }
120
124 public function getResultDamage() : int{
125 return (int) ceil($this->damage);
126 }
127
128 public function saveNBT() : CompoundTag{
129 $nbt = parent::saveNBT();
130
131 $nbt->setDouble(self::TAG_DAMAGE, $this->damage);
132
133 if($this->blockHit !== null){
134 $nbt->setTag(self::TAG_STUCK_ON_BLOCK_POS, new ListTag([
135 new IntTag($this->blockHit->getFloorX()),
136 new IntTag($this->blockHit->getFloorY()),
137 new IntTag($this->blockHit->getFloorZ())
138 ]));
139 }
140
141 return $nbt;
142 }
143
144 protected function applyDragBeforeGravity() : bool{
145 return true;
146 }
147
148 public function onNearbyBlockChange() : void{
149 if($this->blockHit !== null && $this->getWorld()->isInLoadedTerrain($this->blockHit)){
150 $blockHit = $this->getWorld()->getBlock($this->blockHit);
151 if(!$blockHit->collidesWithBB($this->getBoundingBox()->expandedCopy(0.001, 0.001, 0.001))){
152 $this->blockHit = null;
153 }
154 }
155
156 parent::onNearbyBlockChange();
157 }
158
159 public function hasMovementUpdate() : bool{
160 return $this->blockHit === null && parent::hasMovementUpdate();
161 }
162
163 protected function move(float $dx, float $dy, float $dz) : void{
164 $this->blocksAround = null;
165
166 Timings::$projectileMove->startTiming();
167 Timings::$projectileMoveRayTrace->startTiming();
168
169 $start = $this->location->asVector3();
170 $end = $start->add($dx, $dy, $dz);
171
172 $blockHit = null;
173 $entityHit = null;
174 $hitResult = null;
175
176 $world = $this->getWorld();
177 foreach(VoxelRayTrace::betweenPoints($start, $end) as $vector3){
178 $block = $world->getBlockAt($vector3->x, $vector3->y, $vector3->z);
179
180 $blockHitResult = $this->calculateInterceptWithBlock($block, $start, $end);
181 if($blockHitResult !== null){
182 $end = $blockHitResult->hitVector;
183 $blockHit = $block;
184 $hitResult = $blockHitResult;
185 break;
186 }
187 }
188
189 $entityDistance = PHP_INT_MAX;
190
191 $newDiff = $end->subtractVector($start);
192 foreach($world->getCollidingEntities($this->boundingBox->addCoord($newDiff->x, $newDiff->y, $newDiff->z)->expand(1, 1, 1), $this) as $entity){
193 if($entity->getId() === $this->getOwningEntityId() && $this->ticksLived < 5){
194 continue;
195 }
196
197 $entityBB = $entity->boundingBox->expandedCopy(0.3, 0.3, 0.3);
198 $entityHitResult = $entityBB->calculateIntercept($start, $end);
199
200 if($entityHitResult === null){
201 continue;
202 }
203
204 $distance = $this->location->distanceSquared($entityHitResult->hitVector);
205
206 if($distance < $entityDistance){
207 $entityDistance = $distance;
208 $entityHit = $entity;
209 $hitResult = $entityHitResult;
210 $end = $entityHitResult->hitVector;
211 }
212 }
213
214 Timings::$projectileMoveRayTrace->stopTiming();
215
216 $this->location = Location::fromObject(
217 $end,
218 $this->location->world,
219 $this->location->yaw,
220 $this->location->pitch
221 );
222 $this->recalculateBoundingBox();
223
224 if($hitResult !== null){
226 $ev = null;
227 if($entityHit !== null){
228 $ev = new ProjectileHitEntityEvent($this, $hitResult, $entityHit);
229 }elseif($blockHit !== null){
230 $ev = new ProjectileHitBlockEvent($this, $hitResult, $blockHit);
231 }else{
232 assert(false, "unknown hit type");
233 }
234
235 if($ev !== null){
236 $ev->call();
237 $this->onHit($ev);
238
239 if($ev instanceof ProjectileHitEntityEvent){
240 $this->onHitEntity($ev->getEntityHit(), $ev->getRayTraceResult());
241 }elseif($ev instanceof ProjectileHitBlockEvent){
242 $this->onHitBlock($ev->getBlockHit(), $ev->getRayTraceResult());
243 }
244 }
245
246 $this->isCollided = $this->onGround = true;
247 $this->motion = Vector3::zero();
248 }else{
249 $this->isCollided = $this->onGround = false;
250 $this->blockHit = null;
251
252 //recompute angles...
253 $f = sqrt(($this->motion->x ** 2) + ($this->motion->z ** 2));
254 $this->setRotation(
255 atan2($this->motion->x, $this->motion->z) * 180 / M_PI,
256 atan2($this->motion->y, $f) * 180 / M_PI
257 );
258 }
259
260 $world->onEntityMoved($this);
261 $this->checkBlockIntersections();
262
263 Timings::$projectileMove->stopTiming();
264 }
265
273 protected function calculateInterceptWithBlock(Block $block, Vector3 $start, Vector3 $end) : ?RayTraceResult{
274 return $block->calculateIntercept($start, $end);
275 }
276
281 protected function onHit(ProjectileHitEvent $event) : void{
282
283 }
284
288 protected function onHitEntity(Entity $entityHit, RayTraceResult $hitResult) : void{
289 $damage = $this->getResultDamage();
290
291 if($damage >= 0){
292 if($this->getOwningEntity() === null){
293 $ev = new EntityDamageByEntityEvent($this, $entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
294 }else{
295 $ev = new EntityDamageByChildEntityEvent($this->getOwningEntity(), $this, $entityHit, EntityDamageEvent::CAUSE_PROJECTILE, $damage);
296 }
297
298 $entityHit->attack($ev);
299
300 if($this->isOnFire()){
301 $ev = new EntityCombustByEntityEvent($this, $entityHit, 5);
302 $ev->call();
303 if(!$ev->isCancelled()){
304 $entityHit->setOnFire($ev->getDuration());
305 }
306 }
307 }
308
309 $this->flagForDespawn();
310 }
311
315 protected function onHitBlock(Block $blockHit, RayTraceResult $hitResult) : void{
316 $this->blockHit = $blockHit->getPosition()->asVector3();
317 $blockHit->onProjectileHit($this, $hitResult);
318 }
319}
onProjectileHit(Projectile $projectile, RayTraceResult $hitResult)
Definition: Block.php:895
setOwningEntity(?Entity $owner)
Definition: Entity.php:414
setHealth(float $amount)
Definition: Entity.php:590
onHitBlock(Block $blockHit, RayTraceResult $hitResult)
Definition: Projectile.php:315
calculateInterceptWithBlock(Block $block, Vector3 $start, Vector3 $end)
Definition: Projectile.php:273
onHit(ProjectileHitEvent $event)
Definition: Projectile.php:281
setTag(string $name, Tag $tag)
setDouble(string $name, float $value)