mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-09-06 09:56:06 +00:00
Removed pocketmine subdirectory, map PSR-4 style
This commit is contained in:
250
src/world/Explosion.php
Normal file
250
src/world/Explosion.php
Normal file
@ -0,0 +1,250 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\world;
|
||||
|
||||
use pocketmine\block\Block;
|
||||
use pocketmine\block\BlockFactory;
|
||||
use pocketmine\block\TNT;
|
||||
use pocketmine\block\VanillaBlocks;
|
||||
use pocketmine\entity\Entity;
|
||||
use pocketmine\event\block\BlockUpdateEvent;
|
||||
use pocketmine\event\entity\EntityDamageByBlockEvent;
|
||||
use pocketmine\event\entity\EntityDamageByEntityEvent;
|
||||
use pocketmine\event\entity\EntityDamageEvent;
|
||||
use pocketmine\event\entity\EntityExplodeEvent;
|
||||
use pocketmine\item\ItemFactory;
|
||||
use pocketmine\math\AxisAlignedBB;
|
||||
use pocketmine\math\Facing;
|
||||
use pocketmine\math\Vector3;
|
||||
use pocketmine\network\mcpe\protocol\ExplodePacket;
|
||||
use pocketmine\world\particle\HugeExplodeSeedParticle;
|
||||
use pocketmine\world\sound\ExplodeSound;
|
||||
use pocketmine\world\utils\SubChunkIteratorManager;
|
||||
use function ceil;
|
||||
use function floor;
|
||||
use function mt_rand;
|
||||
|
||||
class Explosion{
|
||||
/** @var int */
|
||||
private $rays = 16;
|
||||
/** @var World */
|
||||
public $world;
|
||||
/** @var Position */
|
||||
public $source;
|
||||
/** @var float */
|
||||
public $size;
|
||||
|
||||
/** @var Block[] */
|
||||
public $affectedBlocks = [];
|
||||
/** @var float */
|
||||
public $stepLen = 0.3;
|
||||
/** @var Entity|Block */
|
||||
private $what;
|
||||
|
||||
/** @var SubChunkIteratorManager */
|
||||
private $subChunkHandler;
|
||||
|
||||
/**
|
||||
* @param Position $center
|
||||
* @param float $size
|
||||
* @param Entity|Block $what
|
||||
*/
|
||||
public function __construct(Position $center, float $size, $what = null){
|
||||
if(!$center->isValid()){
|
||||
throw new \InvalidArgumentException("Position does not have a valid world");
|
||||
}
|
||||
$this->source = $center;
|
||||
$this->world = $center->getWorld();
|
||||
|
||||
if($size <= 0){
|
||||
throw new \InvalidArgumentException("Explosion radius must be greater than 0, got $size");
|
||||
}
|
||||
$this->size = $size;
|
||||
|
||||
$this->what = $what;
|
||||
$this->subChunkHandler = new SubChunkIteratorManager($this->world);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function explodeA() : bool{
|
||||
if($this->size < 0.1){
|
||||
return false;
|
||||
}
|
||||
|
||||
$vector = new Vector3(0, 0, 0);
|
||||
$vBlock = new Position(0, 0, 0, $this->world);
|
||||
|
||||
$currentChunk = null;
|
||||
$currentSubChunk = null;
|
||||
|
||||
$mRays = (int) ($this->rays - 1);
|
||||
for($i = 0; $i < $this->rays; ++$i){
|
||||
for($j = 0; $j < $this->rays; ++$j){
|
||||
for($k = 0; $k < $this->rays; ++$k){
|
||||
if($i === 0 or $i === $mRays or $j === 0 or $j === $mRays or $k === 0 or $k === $mRays){
|
||||
$vector->setComponents($i / $mRays * 2 - 1, $j / $mRays * 2 - 1, $k / $mRays * 2 - 1);
|
||||
$vector->setComponents(($vector->x / ($len = $vector->length())) * $this->stepLen, ($vector->y / $len) * $this->stepLen, ($vector->z / $len) * $this->stepLen);
|
||||
$pointerX = $this->source->x;
|
||||
$pointerY = $this->source->y;
|
||||
$pointerZ = $this->source->z;
|
||||
|
||||
for($blastForce = $this->size * (mt_rand(700, 1300) / 1000); $blastForce > 0; $blastForce -= $this->stepLen * 0.75){
|
||||
$x = (int) $pointerX;
|
||||
$y = (int) $pointerY;
|
||||
$z = (int) $pointerZ;
|
||||
$vBlock->x = $pointerX >= $x ? $x : $x - 1;
|
||||
$vBlock->y = $pointerY >= $y ? $y : $y - 1;
|
||||
$vBlock->z = $pointerZ >= $z ? $z : $z - 1;
|
||||
|
||||
if(!$this->subChunkHandler->moveTo($vBlock->x, $vBlock->y, $vBlock->z, false)){
|
||||
continue;
|
||||
}
|
||||
|
||||
$state = $this->subChunkHandler->currentSubChunk->getFullBlock($vBlock->x & 0x0f, $vBlock->y & 0x0f, $vBlock->z & 0x0f);
|
||||
|
||||
if($state !== 0){
|
||||
$blastForce -= (BlockFactory::$blastResistance[$state] / 5 + 0.3) * $this->stepLen;
|
||||
if($blastForce > 0){
|
||||
if(!isset($this->affectedBlocks[$index = World::blockHash($vBlock->x, $vBlock->y, $vBlock->z)])){
|
||||
$this->affectedBlocks[$index] = BlockFactory::fromFullBlock($state, $vBlock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$pointerX += $vector->x;
|
||||
$pointerY += $vector->y;
|
||||
$pointerZ += $vector->z;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function explodeB() : bool{
|
||||
$send = [];
|
||||
$updateBlocks = [];
|
||||
|
||||
$source = (new Vector3($this->source->x, $this->source->y, $this->source->z))->floor();
|
||||
$yield = (1 / $this->size) * 100;
|
||||
|
||||
if($this->what instanceof Entity){
|
||||
$ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield);
|
||||
$ev->call();
|
||||
if($ev->isCancelled()){
|
||||
return false;
|
||||
}else{
|
||||
$yield = $ev->getYield();
|
||||
$this->affectedBlocks = $ev->getBlockList();
|
||||
}
|
||||
}
|
||||
|
||||
$explosionSize = $this->size * 2;
|
||||
$minX = (int) floor($this->source->x - $explosionSize - 1);
|
||||
$maxX = (int) ceil($this->source->x + $explosionSize + 1);
|
||||
$minY = (int) floor($this->source->y - $explosionSize - 1);
|
||||
$maxY = (int) ceil($this->source->y + $explosionSize + 1);
|
||||
$minZ = (int) floor($this->source->z - $explosionSize - 1);
|
||||
$maxZ = (int) ceil($this->source->z + $explosionSize + 1);
|
||||
|
||||
$explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
|
||||
|
||||
$list = $this->world->getNearbyEntities($explosionBB, $this->what instanceof Entity ? $this->what : null);
|
||||
foreach($list as $entity){
|
||||
$distance = $entity->distance($this->source) / $explosionSize;
|
||||
|
||||
if($distance <= 1){
|
||||
$motion = $entity->subtract($this->source)->normalize();
|
||||
|
||||
$impact = (1 - $distance) * ($exposure = 1);
|
||||
|
||||
$damage = (int) ((($impact * $impact + $impact) / 2) * 8 * $explosionSize + 1);
|
||||
|
||||
if($this->what instanceof Entity){
|
||||
$ev = new EntityDamageByEntityEvent($this->what, $entity, EntityDamageEvent::CAUSE_ENTITY_EXPLOSION, $damage);
|
||||
}elseif($this->what instanceof Block){
|
||||
$ev = new EntityDamageByBlockEvent($this->what, $entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
|
||||
}else{
|
||||
$ev = new EntityDamageEvent($entity, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, $damage);
|
||||
}
|
||||
|
||||
$entity->attack($ev);
|
||||
$entity->setMotion($motion->multiply($impact));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$air = ItemFactory::air();
|
||||
$airBlock = VanillaBlocks::AIR();
|
||||
|
||||
foreach($this->affectedBlocks as $block){
|
||||
if($block instanceof TNT){
|
||||
$block->ignite(mt_rand(10, 30));
|
||||
}else{
|
||||
if(mt_rand(0, 100) < $yield){
|
||||
foreach($block->getDrops($air) as $drop){
|
||||
$this->world->dropItem($block->add(0.5, 0.5, 0.5), $drop);
|
||||
}
|
||||
}
|
||||
if(($t = $this->world->getTileAt($block->x, $block->y, $block->z)) !== null){
|
||||
$t->onBlockDestroyed(); //needed to create drops for inventories
|
||||
}
|
||||
$this->world->setBlockAt($block->x, $block->y, $block->z, $airBlock, false); //TODO: should updating really be disabled here?
|
||||
$this->world->updateAllLight($block);
|
||||
}
|
||||
|
||||
$pos = new Vector3($block->x, $block->y, $block->z);
|
||||
|
||||
foreach(Facing::ALL as $side){
|
||||
$sideBlock = $pos->getSide($side);
|
||||
if(!$this->world->isInWorld($sideBlock->x, $sideBlock->y, $sideBlock->z)){
|
||||
continue;
|
||||
}
|
||||
if(!isset($this->affectedBlocks[$index = World::blockHash($sideBlock->x, $sideBlock->y, $sideBlock->z)]) and !isset($updateBlocks[$index])){
|
||||
$ev = new BlockUpdateEvent($this->world->getBlockAt($sideBlock->x, $sideBlock->y, $sideBlock->z));
|
||||
$ev->call();
|
||||
if(!$ev->isCancelled()){
|
||||
foreach($this->world->getNearbyEntities(AxisAlignedBB::one()->offset($sideBlock->x, $sideBlock->y, $sideBlock->z)->expand(1, 1, 1)) as $entity){
|
||||
$entity->onNearbyBlockChange();
|
||||
}
|
||||
$ev->getBlock()->onNearbyBlockChange();
|
||||
}
|
||||
$updateBlocks[$index] = true;
|
||||
}
|
||||
}
|
||||
$send[] = new Vector3($block->x - $source->x, $block->y - $source->y, $block->z - $source->z);
|
||||
}
|
||||
|
||||
$this->world->broadcastPacketToViewers($source, ExplodePacket::create($this->source->asVector3(), $this->size, $send));
|
||||
|
||||
$this->world->addParticle($source, new HugeExplodeSeedParticle());
|
||||
$this->world->addSound($source, new ExplodeSound());
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user