PocketMine-MP 5.35.1 git-e32e836dad793a3a3c8ddd8927c00e112b1e576a
Loading...
Searching...
No Matches
AreaEffectCloud.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\object;
25
43use function count;
44use function max;
45use function round;
46
47class AreaEffectCloud extends Entity{
48
49 public const DEFAULT_DURATION = 600; // in ticks
50 public const DEFAULT_DURATION_CHANGE_ON_USE = 0; // in ticks
51
52 public const UPDATE_DELAY = 10; // in ticks
53 public const REAPPLICATION_DELAY = 40; // in ticks
54
55 public const DEFAULT_RADIUS = 3.0; // in blocks
56 public const DEFAULT_RADIUS_CHANGE_ON_PICKUP = -0.5; // in blocks
57 public const DEFAULT_RADIUS_CHANGE_ON_USE = -0.5; // in blocks
58 public const DEFAULT_RADIUS_CHANGE_PER_TICK = -(self::DEFAULT_RADIUS / self::DEFAULT_DURATION); // in blocks
59
60 protected const TAG_POTION_ID = "PotionId"; //TAG_Short
61 protected const TAG_SPAWN_TICK = "SpawnTick"; //TAG_Long
62 protected const TAG_DURATION = "Duration"; //TAG_Int
63 protected const TAG_PICKUP_COUNT = "PickupCount"; //TAG_Int
64 protected const TAG_DURATION_ON_USE = "DurationOnUse"; //TAG_Int
65 protected const TAG_REAPPLICATION_DELAY = "ReapplicationDelay"; //TAG_Int
66 protected const TAG_INITIAL_RADIUS = "InitialRadius"; //TAG_Float
67 protected const TAG_RADIUS = "Radius"; //TAG_Float
68 protected const TAG_RADIUS_CHANGE_ON_PICKUP = "RadiusChangeOnPickup"; //TAG_Float
69 protected const TAG_RADIUS_ON_USE = "RadiusOnUse"; //TAG_Float
70 protected const TAG_RADIUS_PER_TICK = "RadiusPerTick"; //TAG_Float
71 protected const TAG_EFFECTS = "mobEffects"; //TAG_List
72
73 public function getNetworkTypeId() : string{ return EntityIds::AREA_EFFECT_CLOUD; }
74
75 protected int $age = 0;
76
77 protected EffectCollection $effectCollection;
78
80 protected array $victims = [];
81
82 protected int $maxAge = self::DEFAULT_DURATION;
83 protected int $maxAgeChangeOnUse = self::DEFAULT_DURATION_CHANGE_ON_USE;
84
85 protected int $reapplicationDelay = self::REAPPLICATION_DELAY;
86
87 protected int $pickupCount = 0;
88 protected float $radiusChangeOnPickup = self::DEFAULT_RADIUS_CHANGE_ON_PICKUP;
89
90 protected float $initialRadius = self::DEFAULT_RADIUS;
91 protected float $radius = self::DEFAULT_RADIUS;
92 protected float $radiusChangeOnUse = self::DEFAULT_RADIUS_CHANGE_ON_USE;
93 protected float $radiusChangePerTick = self::DEFAULT_RADIUS_CHANGE_PER_TICK;
94
95 public function __construct(
96 Location $location,
97 ?CompoundTag $nbt = null
98 ){
99 parent::__construct($location, $nbt);
100 }
101
102 protected function getInitialSizeInfo() : EntitySizeInfo{ return new EntitySizeInfo(0.5, $this->radius * 2); }
103
104 protected function getInitialDragMultiplier() : float{ return 0.0; }
105
106 protected function getInitialGravity() : float{ return 0.0; }
107
108 protected function initEntity(CompoundTag $nbt) : void{
109 parent::initEntity($nbt);
110
111 $this->effectCollection = new EffectCollection();
112 $this->effectCollection->getEffectAddHooks()->add(function() : void{ $this->networkPropertiesDirty = true; });
113 $this->effectCollection->getEffectRemoveHooks()->add(function() : void{ $this->networkPropertiesDirty = true; });
114 $this->effectCollection->setEffectFilterForBubbles(static fn(EffectInstance $e) : bool => $e->isVisible());
115
116 $worldTime = $this->getWorld()->getTime();
117 $this->age = max($worldTime - $nbt->getLong(self::TAG_SPAWN_TICK, $worldTime), 0);
118 $this->maxAge = $nbt->getInt(self::TAG_DURATION, self::DEFAULT_DURATION);
119 $this->maxAgeChangeOnUse = $nbt->getInt(self::TAG_DURATION_ON_USE, self::DEFAULT_DURATION_CHANGE_ON_USE);
120 $this->pickupCount = $nbt->getInt(self::TAG_PICKUP_COUNT, 0);
121 $this->reapplicationDelay = $nbt->getInt(self::TAG_REAPPLICATION_DELAY, self::REAPPLICATION_DELAY);
122
123 $this->initialRadius = $nbt->getFloat(self::TAG_INITIAL_RADIUS, self::DEFAULT_RADIUS);
124 $this->setRadius($nbt->getFloat(self::TAG_RADIUS, $this->initialRadius));
125 $this->radiusChangeOnPickup = $nbt->getFloat(self::TAG_RADIUS_CHANGE_ON_PICKUP, self::DEFAULT_RADIUS_CHANGE_ON_PICKUP);
126 $this->radiusChangeOnUse = $nbt->getFloat(self::TAG_RADIUS_ON_USE, self::DEFAULT_RADIUS_CHANGE_ON_USE);
127 $this->radiusChangePerTick = $nbt->getFloat(self::TAG_RADIUS_PER_TICK, self::DEFAULT_RADIUS_CHANGE_PER_TICK);
128
129 $effectsTag = $nbt->getListTag(self::TAG_EFFECTS, CompoundTag::class);
130 if($effectsTag !== null){
131 foreach($effectsTag as $e){
132 $effect = EffectIdMap::getInstance()->fromId($e->getByte("Id"));
133 if($effect === null){
134 continue;
135 }
136
137 $this->effectCollection->add(new EffectInstance(
138 $effect,
139 $e->getInt("Duration"),
140 Binary::unsignByte($e->getByte("Amplifier")),
141 $e->getByte("ShowParticles", 1) !== 0,
142 $e->getByte("Ambient", 0) !== 0
143 ));
144 }
145 }
146 }
147
148 public function saveNBT() : CompoundTag{
149 $nbt = parent::saveNBT();
150
151 $nbt->setLong(self::TAG_SPAWN_TICK, $this->getWorld()->getTime() - $this->age);
152 $nbt->setShort(self::TAG_POTION_ID, PotionTypeIds::WATER); //not used, mobEffects is used exclusively in Bedrock
153 $nbt->setInt(self::TAG_DURATION, $this->maxAge);
154 $nbt->setInt(self::TAG_DURATION_ON_USE, $this->maxAgeChangeOnUse);
155 $nbt->setInt(self::TAG_PICKUP_COUNT, $this->pickupCount);
156 $nbt->setInt(self::TAG_REAPPLICATION_DELAY, $this->reapplicationDelay);
157 $nbt->setFloat(self::TAG_INITIAL_RADIUS, $this->initialRadius);
158 $nbt->setFloat(self::TAG_RADIUS, $this->radius);
159 $nbt->setFloat(self::TAG_RADIUS_CHANGE_ON_PICKUP, $this->radiusChangeOnPickup);
160 $nbt->setFloat(self::TAG_RADIUS_ON_USE, $this->radiusChangeOnUse);
161 $nbt->setFloat(self::TAG_RADIUS_PER_TICK, $this->radiusChangePerTick);
162
163 if(count($this->effectCollection->all()) > 0){
164 $effects = [];
165 foreach($this->effectCollection->all() as $effect){
166 $effects[] = CompoundTag::create()
167 ->setByte("Id", EffectIdMap::getInstance()->toId($effect->getType()))
168 ->setByte("Amplifier", Binary::signByte($effect->getAmplifier()))
169 ->setInt("Duration", $effect->getDuration())
170 ->setByte("Ambient", $effect->isAmbient() ? 1 : 0)
171 ->setByte("ShowParticles", $effect->isVisible() ? 1 : 0);
172 }
173 $nbt->setTag(self::TAG_EFFECTS, new ListTag($effects));
174 }
175
176 return $nbt;
177 }
178
179 public function isFireProof() : bool{
180 return true;
181 }
182
183 public function canBeCollidedWith() : bool{
184 return false;
185 }
186
190 public function getAge() : int{
191 return $this->age;
192 }
193
194 public function getEffects() : EffectCollection{
195 return $this->effectCollection;
196 }
197
201 public function getInitialRadius() : float{
202 return $this->initialRadius;
203 }
204
208 public function getRadius() : float{
209 return $this->radius;
210 }
211
215 protected function setRadius(float $radius) : void{
216 $this->radius = $radius;
217 $this->setSize($this->getInitialSizeInfo());
218 $this->networkPropertiesDirty = true;
219 }
220
227 public function getRadiusChangeOnPickup() : float{
228 return $this->radiusChangeOnPickup;
229 }
230
237 public function setRadiusChangeOnPickup(float $radiusChangeOnPickup) : void{
238 $this->radiusChangeOnPickup = $radiusChangeOnPickup;
239 }
240
245 public function getRadiusChangeOnUse() : float{
246 return $this->radiusChangeOnUse;
247 }
248
253 public function setRadiusChangeOnUse(float $radiusChangeOnUse) : void{
254 $this->radiusChangeOnUse = $radiusChangeOnUse;
255 }
256
261 public function getRadiusChangePerTick() : float{
262 return $this->radiusChangePerTick;
263 }
264
268 public function setRadiusChangePerTick(float $radiusChangePerTick) : void{
269 $this->radiusChangePerTick = $radiusChangePerTick;
270 }
271
275 public function getMaxAge() : int{
276 return $this->maxAge;
277 }
278
282 public function setMaxAge(int $maxAge) : void{
283 $this->maxAge = $maxAge;
284 }
285
290 public function getMaxAgeChangeOnUse() : int{
291 return $this->maxAgeChangeOnUse;
292 }
293
298 public function setMaxAgeChangeOnUse(int $maxAgeChangeOnUse) : void{
299 $this->maxAgeChangeOnUse = $maxAgeChangeOnUse;
300 }
301
305 public function getReapplicationDelay() : int{
306 return $this->reapplicationDelay;
307 }
308
312 public function setReapplicationDelay(int $delay) : void{
313 $this->reapplicationDelay = $delay;
314 }
315
316 protected function entityBaseTick(int $tickDiff = 1) : bool{
317 $hasUpdate = parent::entityBaseTick($tickDiff);
318
319 $this->age += $tickDiff;
320 $radius = $this->radius + ($this->radiusChangePerTick * $tickDiff);
321 if($radius < 0.5){
322 $this->flagForDespawn();
323 return true;
324 }
325 $this->setRadius($radius);
326 if($this->age >= self::UPDATE_DELAY && ($this->age % self::UPDATE_DELAY) === 0){
327 if($this->age > $this->maxAge){
328 $this->flagForDespawn();
329 return true;
330 }
331
332 foreach($this->victims as $entityId => $expiration){
333 if($this->age >= $expiration){
334 unset($this->victims[$entityId]);
335 }
336 }
337
338 $entities = [];
339 $radiusChange = 0.0;
340 $maxAgeChange = 0;
341 foreach($this->getWorld()->getCollidingEntities($this->getBoundingBox(), $this) as $entity){
342 if(!$entity instanceof Living || isset($this->victims[$entity->getId()])){
343 continue;
344 }
345 $entityPosition = $entity->getPosition();
346 $xDiff = $entityPosition->getX() - $this->location->getX();
347 $zDiff = $entityPosition->getZ() - $this->location->getZ();
348 if(($xDiff ** 2 + $zDiff ** 2) > $this->radius ** 2){
349 continue;
350 }
351 $entities[] = $entity;
352 if($this->radiusChangeOnUse !== 0.0){
353 $radiusChange += $this->radiusChangeOnUse;
354 if($this->radius + $radiusChange <= 0){
355 break;
356 }
357 }
358 if($this->maxAgeChangeOnUse !== 0){
359 $maxAgeChange += $this->maxAgeChangeOnUse;
360 if($this->maxAge + $maxAgeChange <= 0){
361 break;
362 }
363 }
364 }
365 if(count($entities) === 0){
366 return $hasUpdate;
367 }
368
369 $ev = new AreaEffectCloudApplyEvent($this, $entities);
370 $ev->call();
371 if($ev->isCancelled()){
372 return $hasUpdate;
373 }
374
375 foreach($ev->getAffectedEntities() as $entity){
376 foreach($this->effectCollection->all() as $effect){
377 $effect = clone $effect; //avoid accidental modification
378 if($effect->getType() instanceof InstantEffect){
379 $effect->getType()->applyEffect($entity, $effect, 0.5, $this);
380 }else{
381 $entity->getEffects()->add($effect->setDuration((int) round($effect->getDuration() / 4)));
382 }
383 }
384 if($this->reapplicationDelay !== 0){
385 $this->victims[$entity->getId()] = $this->age + $this->reapplicationDelay;
386 }
387 }
388
389 $radius = $this->radius + $radiusChange;
390 $maxAge = $this->maxAge + $maxAgeChange;
391 if($radius <= 0 || $maxAge <= 0){
392 $this->flagForDespawn();
393 return true;
394 }
395 $this->setRadius($radius);
396 $this->setMaxAge($maxAge);
397 $hasUpdate = true;
398 }
399
400 return $hasUpdate;
401 }
402
403 protected function syncNetworkData(EntityMetadataCollection $properties) : void{
404 parent::syncNetworkData($properties);
405
406 //visual properties
407 $properties->setFloat(EntityMetadataProperties::AREA_EFFECT_CLOUD_RADIUS, $this->radius);
408 $properties->setInt(EntityMetadataProperties::POTION_COLOR, Binary::signInt((
409 count($this->effectCollection->all()) === 0 ? PotionSplashParticle::DEFAULT_COLOR() : $this->effectCollection->getBubbleColor()
410 )->toARGB()));
411
412 //these are properties the client expects, and are used for client-sided logic, which we don't want
413 $properties->setByte(EntityMetadataProperties::POTION_AMBIENT, 0);
414 $properties->setInt(EntityMetadataProperties::AREA_EFFECT_CLOUD_DURATION, -1);
415 $properties->setFloat(EntityMetadataProperties::AREA_EFFECT_CLOUD_RADIUS_CHANGE_ON_PICKUP, 0);
416 $properties->setFloat(EntityMetadataProperties::AREA_EFFECT_CLOUD_RADIUS_PER_TICK, 0);
417 $properties->setInt(EntityMetadataProperties::AREA_EFFECT_CLOUD_SPAWN_TIME, 0);
418 $properties->setFloat(EntityMetadataProperties::AREA_EFFECT_CLOUD_PICKUP_COUNT, 0);
419 $properties->setInt(EntityMetadataProperties::AREA_EFFECT_CLOUD_WAITING, 0);
420 }
421
422 protected function destroyCycles() : void{
423 //wipe out callback refs
424 $this->effectCollection = new EffectCollection();
425 parent::destroyCycles();
426 }
427}
setRadiusChangePerTick(float $radiusChangePerTick)
setRadiusChangeOnUse(float $radiusChangeOnUse)
setRadiusChangeOnPickup(float $radiusChangeOnPickup)
setInt(string $name, int $value)
setLong(string $name, int $value)
setTag(string $name, Tag $tag)
getListTag(string $name, string $tagClass=Tag::class)
setFloat(string $name, float $value)
setShort(string $name, int $value)