PocketMine-MP 5.36.1 git-eaa7c4834c8fe2f379d24e7f0ee6cc63cfb18ccc
Loading...
Searching...
No Matches
Explosion.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\world;
25
29use pocketmine\block\utils\SupportType;
49use function ceil;
50use function floor;
51use function min;
52use function mt_rand;
53use function sqrt;
54
56 public const DEFAULT_FIRE_CHANCE = 1.0 / 3.0;
57
58 private int $rays = 16;
59 public World $world;
60
65 public array $affectedBlocks = [];
66 public float $stepLen = 0.3;
68 private array $fireIgnitions = [];
69
70 private SubChunkExplorer $subChunkExplorer;
71
72 public function __construct(
73 public Position $source,
74 public float $radius,
75 private Entity|Block|null $what = null,
76 private float $fireChance = 0.0
77 ){
78 if(!$this->source->isValid()){
79 throw new \InvalidArgumentException("Position does not have a valid world");
80 }
81 $this->world = $this->source->getWorld();
82 Utils::checkFloatNotInfOrNaN("fireChance", $fireChance);
83 if($fireChance < 0.0 || $fireChance > 1.0){
84 throw new \InvalidArgumentException("Fire chance must be a number between 0 and 1.");
85 }
86 if($radius <= 0){
87 throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $radius");
88 }
89 $this->subChunkExplorer = new SubChunkExplorer($this->world);
90 }
91
96 public function explodeA() : bool{
97 if($this->radius < 0.1){
98 return false;
99 }
100
101 $blockFactory = RuntimeBlockStateRegistry::getInstance();
102
103 $mRays = $this->rays - 1;
104 $incendiary = $this->fireChance > 0;
105 for($i = 0; $i < $this->rays; ++$i){
106 for($j = 0; $j < $this->rays; ++$j){
107 for($k = 0; $k < $this->rays; ++$k){
108 if($i === 0 || $i === $mRays || $j === 0 || $j === $mRays || $k === 0 || $k === $mRays){
109 //this could be written as new Vector3(...)->normalize()->multiply(stepLen), but we're avoiding Vector3 for performance here
110 [$shiftX, $shiftY, $shiftZ] = [$i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1];
111 $len = sqrt($shiftX ** 2 + $shiftY ** 2 + $shiftZ ** 2);
112 [$shiftX, $shiftY, $shiftZ] = [($shiftX / $len) * $this->stepLen, ($shiftY / $len) * $this->stepLen, ($shiftZ / $len) * $this->stepLen];
113 $pointerX = $this->source->x;
114 $pointerY = $this->source->y;
115 $pointerZ = $this->source->z;
116
117 for($blastForce = $this->radius * (mt_rand(700, 1300) / 1000); $blastForce > 0; $blastForce -= $this->stepLen * 0.75){
118 $x = (int) $pointerX;
119 $y = (int) $pointerY;
120 $z = (int) $pointerZ;
121 $vBlockX = $pointerX >= $x ? $x : $x - 1;
122 $vBlockY = $pointerY >= $y ? $y : $y - 1;
123 $vBlockZ = $pointerZ >= $z ? $z : $z - 1;
124
125 $pointerX += $shiftX;
126 $pointerY += $shiftY;
127 $pointerZ += $shiftZ;
128
129 if($this->subChunkExplorer->moveTo($vBlockX, $vBlockY, $vBlockZ) === SubChunkExplorerStatus::INVALID){
130 continue;
131 }
132 $subChunk = $this->subChunkExplorer->currentSubChunk;
133 if($subChunk === null){
134 throw new AssumptionFailedError("SubChunkExplorer subchunk should not be null here");
135 }
136
137 $state = $subChunk->getBlockStateId($vBlockX & SubChunk::COORD_MASK, $vBlockY & SubChunk::COORD_MASK, $vBlockZ & SubChunk::COORD_MASK);
138
139 $blastResistance = $blockFactory->blastResistance[$state] ?? 0;
140 if($blastResistance >= 0){
141 $blastForce -= ($blastResistance / 5 + 0.3) * $this->stepLen;
142 if($blastForce > 0){
143 if(!isset($this->affectedBlocks[World::blockHash($vBlockX, $vBlockY, $vBlockZ)])){
144 $_block = $this->world->getBlockAt($vBlockX, $vBlockY, $vBlockZ, true, false);
145 foreach($_block->getAffectedBlocks() as $_affectedBlock){
146 $_affectedBlockPos = $_affectedBlock->getPosition();
147 $posHash = World::blockHash($_affectedBlockPos->x, $_affectedBlockPos->y, $_affectedBlockPos->z);
148 $this->affectedBlocks[$posHash] = $_affectedBlock;
149
150 if($incendiary && Utils::getRandomFloat() <= $this->fireChance){
151 $this->fireIgnitions[$posHash] = $_affectedBlock;
152 }
153 }
154 }
155 }
156 }
157 }
158 }
159 }
160 }
161 }
162
163 return true;
164 }
165
170 public function explodeB() : bool{
171 $source = (new Vector3($this->source->x, $this->source->y, $this->source->z))->floor();
172 $yield = min(100, (1 / $this->radius) * 100);
173
174 if($this->what instanceof Entity){
175 $ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield, $this->fireIgnitions);
176
177 $ev->call();
178 if($ev->isCancelled()){
179 return false;
180 }
181
182 $yield = $ev->getYield();
183 $this->affectedBlocks = $ev->getBlockList();
184 $this->fireIgnitions = $ev->getIgnitions();
185 }elseif($this->what instanceof Block){
186 $ev = new BlockExplodeEvent(
187 $this->what,
188 $this->source,
189 $this->affectedBlocks,
190 $yield,
191 $this->fireIgnitions,
192 );
193
194 $ev->call();
195 if($ev->isCancelled()){
196 return false;
197 }else{
198 $yield = $ev->getYield();
199 $this->affectedBlocks = $ev->getAffectedBlocks();
200 $this->fireIgnitions = $ev->getIgnitions();
201 }
202 }
203
204 $explosionSize = $this->radius * 2;
205 $minX = (int) floor($this->source->x - $explosionSize - 1);
206 $maxX = (int) ceil($this->source->x + $explosionSize + 1);
207 $minY = (int) floor($this->source->y - $explosionSize - 1);
208 $maxY = (int) ceil($this->source->y + $explosionSize + 1);
209 $minZ = (int) floor($this->source->z - $explosionSize - 1);
210 $maxZ = (int) ceil($this->source->z + $explosionSize + 1);
211
212 $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
213
215 $list = $this->world->getNearbyEntities($explosionBB, $this->what instanceof Entity ? $this->what : null);
216 foreach($list as $entity){
217 $entityPos = $entity->getPosition();
218 $distance = $entityPos->distance($this->source) / $explosionSize;
219
220 if($distance <= 1){
221 $motion = $entityPos->subtractVector($this->source)->normalize();
222 $exposure = $this->getExposure($this->source, $entity);
223
224 $impact = (1 - $distance) * $exposure;
225
226 $damage = (int) ((($impact * $impact + $impact) / 2) * 8 * $explosionSize + 1);
227
228 if($this->what instanceof Entity){
229 $ev = new EntityDamageByEntityEvent($this->what, $entity, EntityDamageEvent::CAUSE_ENTITY_EXPLOSION, $damage);
230 }elseif($this->what instanceof Block){
231 $ev = new EntityDamageByBlockEvent($this->what, $entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
232 }else{
233 $ev = new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
234 }
235
236 $entity->attack($ev);
237 $entity->setMotion($entity->getMotion()->addVector($motion->multiply($impact)));
238 }
239 }
240
241 $air = VanillaItems::AIR();
242 $airBlock = VanillaBlocks::AIR();
243 $fireBlock = VanillaBlocks::FIRE();
244
245 foreach($this->affectedBlocks as $hash => $block){
246 $pos = $block->getPosition();
247 if($block instanceof TNT){
248 $block->ignite(mt_rand(10, 30));
249 }else{
250 if(mt_rand(0, 100) < $yield){
251 foreach($block->getDrops($air) as $drop){
252 $this->world->dropItem($pos->add(0.5, 0.5, 0.5), $drop);
253 }
254 }
255 if(($t = $this->world->getTileAt($pos->x, $pos->y, $pos->z)) !== null){
256 $t->onBlockDestroyed(); //needed to create drops for inventories
257 }
258 $targetBlock =
259 isset($this->fireIgnitions[$hash]) &&
260 $block->getSide(Facing::DOWN)->getSupportType(Facing::UP) === SupportType::FULL ?
261 $fireBlock :
262 $airBlock;
263
264 $this->world->setBlockAt($pos->x, $pos->y, $pos->z, $targetBlock);
265 }
266 }
267
268 $this->world->addParticle($source, new HugeExplodeSeedParticle());
269 $this->world->addSound($source, new ExplodeSound());
270
271 return true;
272 }
273
277 private function getExposure(Vector3 $origin, Entity $entity) : float{
278 $bb = $entity->getBoundingBox();
279
280 $diff = (new Vector3($bb->getXLength(), $bb->getYLength(), $bb->getZLength()))->multiply(2)->add(1, 1, 1);
281 $step = new Vector3(1.0 / $diff->x, 1.0 / $diff->y, 1.0 / $diff->z);
282
283 $xOffset = (1.0 - (floor($diff->x) / $diff->x)) / 2.0;
284 $zOffset = (1.0 - (floor($diff->z) / $diff->z)) / 2.0;
285
286 $checks = 0.0;
287 $hits = 0.0;
288
289 for($x = 0.0; $x <= 1.0; $x += $step->x){
290 for($y = 0.0; $y <= 1.0; $y += $step->y){
291 for($z = 0.0; $z <= 1.0; $z += $step->z){
292 $point = new Vector3(
293 self::lerp($x, $bb->minX, $bb->maxX) + $xOffset,
294 self::lerp($y, $bb->minY, $bb->maxY),
295 self::lerp($z, $bb->minZ, $bb->maxZ) + $zOffset
296 );
297
298 $intercepted = false;
299
300 foreach(VoxelRayTrace::betweenPoints($origin, $point) as $pos){
301 $block = $this->world->getBlock($pos);
302 if($block->calculateIntercept($origin, $point) !== null){
303 $intercepted = true;
304 break;
305 }
306 }
307
308 if(!$intercepted){
309 $hits++;
310 }
311 $checks++;
312 }
313 }
314 }
315
316 return $checks > 0.0 ? $hits / $checks : 0.0;
317 }
318
325 public function setFireChance(float $fireChance) : void{
326 Utils::checkFloatNotInfOrNaN("fireChance", $fireChance);
327 if($fireChance < 0.0 || $fireChance > 1.0){
328 throw new \InvalidArgumentException("Fire chance must be a number between 0 and 1.");
329 }
330 $this->fireChance = $fireChance;
331 }
332
333 private static function lerp(float $scale, float $a, float $b) : float{
334 return $a + $scale * ($b - $a);
335 }
336}
setFireChance(float $fireChance)
static blockHash(int $x, int $y, int $z)
Definition World.php:403