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