PocketMine-MP 5.21.2 git-b2aa6396c3cc2cafdd815eacc360e1ad89599899
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
44use function ceil;
45use function floor;
46use function min;
47use function mt_rand;
48use function sqrt;
49
51 private int $rays = 16;
52 public World $world;
53
55 public array $affectedBlocks = [];
56 public float $stepLen = 0.3;
57
58 private SubChunkExplorer $subChunkExplorer;
59
60 public function __construct(
61 public Position $source,
62 public float $radius,
63 private Entity|Block|null $what = null
64 ){
65 if(!$this->source->isValid()){
66 throw new \InvalidArgumentException("Position does not have a valid world");
67 }
68 $this->world = $this->source->getWorld();
69
70 if($radius <= 0){
71 throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $radius");
72 }
73 $this->subChunkExplorer = new SubChunkExplorer($this->world);
74 }
75
80 public function explodeA() : bool{
81 if($this->radius < 0.1){
82 return false;
83 }
84
85 $blockFactory = RuntimeBlockStateRegistry::getInstance();
86
87 $mRays = $this->rays - 1;
88 for($i = 0; $i < $this->rays; ++$i){
89 for($j = 0; $j < $this->rays; ++$j){
90 for($k = 0; $k < $this->rays; ++$k){
91 if($i === 0 || $i === $mRays || $j === 0 || $j === $mRays || $k === 0 || $k === $mRays){
92 //this could be written as new Vector3(...)->normalize()->multiply(stepLen), but we're avoiding Vector3 for performance here
93 [$shiftX, $shiftY, $shiftZ] = [$i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1];
94 $len = sqrt($shiftX ** 2 + $shiftY ** 2 + $shiftZ ** 2);
95 [$shiftX, $shiftY, $shiftZ] = [($shiftX / $len) * $this->stepLen, ($shiftY / $len) * $this->stepLen, ($shiftZ / $len) * $this->stepLen];
96 $pointerX = $this->source->x;
97 $pointerY = $this->source->y;
98 $pointerZ = $this->source->z;
99
100 for($blastForce = $this->radius * (mt_rand(700, 1300) / 1000); $blastForce > 0; $blastForce -= $this->stepLen * 0.75){
101 $x = (int) $pointerX;
102 $y = (int) $pointerY;
103 $z = (int) $pointerZ;
104 $vBlockX = $pointerX >= $x ? $x : $x - 1;
105 $vBlockY = $pointerY >= $y ? $y : $y - 1;
106 $vBlockZ = $pointerZ >= $z ? $z : $z - 1;
107
108 $pointerX += $shiftX;
109 $pointerY += $shiftY;
110 $pointerZ += $shiftZ;
111
112 if($this->subChunkExplorer->moveTo($vBlockX, $vBlockY, $vBlockZ) === SubChunkExplorerStatus::INVALID){
113 continue;
114 }
115 $subChunk = $this->subChunkExplorer->currentSubChunk;
116 if($subChunk === null){
117 throw new AssumptionFailedError("SubChunkExplorer subchunk should not be null here");
118 }
119
120 $state = $subChunk->getBlockStateId($vBlockX & SubChunk::COORD_MASK, $vBlockY & SubChunk::COORD_MASK, $vBlockZ & SubChunk::COORD_MASK);
121
122 $blastResistance = $blockFactory->blastResistance[$state] ?? 0;
123 if($blastResistance >= 0){
124 $blastForce -= ($blastResistance / 5 + 0.3) * $this->stepLen;
125 if($blastForce > 0){
126 if(!isset($this->affectedBlocks[World::blockHash($vBlockX, $vBlockY, $vBlockZ)])){
127 $_block = $this->world->getBlockAt($vBlockX, $vBlockY, $vBlockZ, true, false);
128 foreach($_block->getAffectedBlocks() as $_affectedBlock){
129 $_affectedBlockPos = $_affectedBlock->getPosition();
130 $this->affectedBlocks[World::blockHash($_affectedBlockPos->x, $_affectedBlockPos->y, $_affectedBlockPos->z)] = $_affectedBlock;
131 }
132 }
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140
141 return true;
142 }
143
148 public function explodeB() : bool{
149 $source = (new Vector3($this->source->x, $this->source->y, $this->source->z))->floor();
150 $yield = min(100, (1 / $this->radius) * 100);
151
152 if($this->what instanceof Entity){
153 $ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield);
154 $ev->call();
155 if($ev->isCancelled()){
156 return false;
157 }else{
158 $yield = $ev->getYield();
159 $this->affectedBlocks = $ev->getBlockList();
160 }
161 }
162
163 $explosionSize = $this->radius * 2;
164 $minX = (int) floor($this->source->x - $explosionSize - 1);
165 $maxX = (int) ceil($this->source->x + $explosionSize + 1);
166 $minY = (int) floor($this->source->y - $explosionSize - 1);
167 $maxY = (int) ceil($this->source->y + $explosionSize + 1);
168 $minZ = (int) floor($this->source->z - $explosionSize - 1);
169 $maxZ = (int) ceil($this->source->z + $explosionSize + 1);
170
171 $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
172
174 $list = $this->world->getNearbyEntities($explosionBB, $this->what instanceof Entity ? $this->what : null);
175 foreach($list as $entity){
176 $entityPos = $entity->getPosition();
177 $distance = $entityPos->distance($this->source) / $explosionSize;
178
179 if($distance <= 1){
180 $motion = $entityPos->subtractVector($this->source)->normalize();
181
182 $impact = (1 - $distance) * ($exposure = 1);
183
184 $damage = (int) ((($impact * $impact + $impact) / 2) * 8 * $explosionSize + 1);
185
186 if($this->what instanceof Entity){
187 $ev = new EntityDamageByEntityEvent($this->what, $entity, EntityDamageEvent::CAUSE_ENTITY_EXPLOSION, $damage);
188 }elseif($this->what instanceof Block){
189 $ev = new EntityDamageByBlockEvent($this->what, $entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
190 }else{
191 $ev = new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
192 }
193
194 $entity->attack($ev);
195 $entity->setMotion($entity->getMotion()->addVector($motion->multiply($impact)));
196 }
197 }
198
199 $air = VanillaItems::AIR();
200 $airBlock = VanillaBlocks::AIR();
201
202 foreach($this->affectedBlocks as $block){
203 $pos = $block->getPosition();
204 if($block instanceof TNT){
205 $block->ignite(mt_rand(10, 30));
206 }else{
207 if(mt_rand(0, 100) < $yield){
208 foreach($block->getDrops($air) as $drop){
209 $this->world->dropItem($pos->add(0.5, 0.5, 0.5), $drop);
210 }
211 }
212 if(($t = $this->world->getTileAt($pos->x, $pos->y, $pos->z)) !== null){
213 $t->onBlockDestroyed(); //needed to create drops for inventories
214 }
215 $this->world->setBlockAt($pos->x, $pos->y, $pos->z, $airBlock);
216 }
217 }
218
219 $this->world->addParticle($source, new HugeExplodeSeedParticle());
220 $this->world->addSound($source, new ExplodeSound());
221
222 return true;
223 }
224}
static blockHash(int $x, int $y, int $z)
Definition World.php:395