protected function checkBlockCollision() { foreach ($blocksaround = $this->getBlocksAround() as $block) { $block->onEntityCollide($this); if ($this->getServer()->redstoneEnabled) { if ($block instanceof PressurePlate) { $this->activatedPressurePlates[Level::blockHash($block->x, $block->y, $block->z)] = $block; } } } if ($this->getServer()->redstoneEnabled) { /** @var \pocketmine\block\PressurePlate $block * */ foreach ($this->activatedPressurePlates as $key => $block) { if (!isset($blocksaround[$key])) { $block->checkActivation(); } } } }
/** * Sets the raw block metadata. * * @param int $x * @param int $y * @param int $z * @param int $data 0-15 */ public function setBlockDataAt($x, $y, $z, $data) { unset($this->blockCache[Level::blockHash($x, $y, $z)]); $this->getChunk($x >> 4, $z >> 4, true)->setBlockData($x & 0xf, $y & 0x7f, $z & 0xf, $data & 0xf); if (!isset($this->changedBlocks[$index = Level::chunkHash($x >> 4, $z >> 4)])) { $this->changedBlocks[$index] = []; } $this->changedBlocks[$index][Level::blockHash($x, $y, $z)] = $v = new Vector3($x, $y, $z); foreach ($this->getChunkLoaders($x >> 4, $z >> 4) as $loader) { $loader->onBlockChanged($v); } }
public function deactivateTorch(array $ignore = [], array $notCheck = []) { if ($this->canCalc()) { $this->activated = false; /** @var Door $block */ $sides = [Vector3::SIDE_EAST, Vector3::SIDE_WEST, Vector3::SIDE_SOUTH, Vector3::SIDE_NORTH]; foreach ($sides as $side) { if (!in_array($side, $ignore)) { $block = $this->getSide($side); if (!in_array($hash = Level::blockHash($block->x, $block->y, $block->z), $notCheck)) { if (!$this->checkPower($block)) { if ($block instanceof Door or $block instanceof Trapdoor) { if ($block->isOpened()) { $block->onActivate(new Item(0)); } } /** @var ActiveRedstoneLamp $block */ if ($block->getId() == Block::ACTIVE_REDSTONE_LAMP) { $block->turnOff(); } } if ($block->getId() == Block::REDSTONE_WIRE) { /** @var RedstoneWire $wire */ $wire = $block; $wire->calcSignal(0, RedstoneWire::OFF); } } } } if (!in_array(Vector3::SIDE_DOWN, $ignore)) { $block = $this->getSide(Vector3::SIDE_DOWN); if (!in_array($hash = Level::blockHash($block->x, $block->y, $block->z), $notCheck)) { if (!$this->checkPower($block)) { if ($block->getId() == Block::ACTIVE_REDSTONE_LAMP) { $block->turnOff(); } } $block = $this->getSide(Vector3::SIDE_DOWN, 2); if (!$this->checkPower($block)) { if ($block instanceof Door or $block instanceof Trapdoor) { if ($block->isOpened()) { $block->onActivate(new Item(0)); } } if ($block->getId() == Block::ACTIVE_REDSTONE_LAMP) { $block->turnOff(); } } if ($block->getId() == Block::REDSTONE_WIRE) { /** @var RedstoneWire $wire */ $wire = $block; $wire->calcSignal(0, RedstoneWire::OFF); } } } //$this->lastUpdateTime = $this->getLevel()->getServer()->getTick(); } }
public function calcSignal($strength = 15, $type = self::ON, array $hasUpdated = []) { //This algorithm is provided by Stary and written by PeratX $hash = Level::blockHash($this->x, $this->y, $this->z); if (!in_array($hash, $hasUpdated)) { $hasUpdated[] = $hash; if ($type == self::DESTROY or $type == self::OFF) { $this->meta = $strength; $this->getLevel()->setBlock($this, $this, true, false); if ($type == self::DESTROY) { $this->getLevel()->setBlock($this, new Air(), true, false); } if ($strength <= 0) { $this->deactivate(); } $powers = $this->getPowerSources($this, [], [], true); /** @var RedstoneSource $power */ foreach ($powers[0] as $power) { $power->activate(); } } else { if ($strength <= 0) { return $hasUpdated; } if ($type == self::PLACE) { $strength = $this->getHighestStrengthAround() - 1; } if ($type == self::ON) { $type = self::PLACE; } if ($this->getStrength() < $strength) { $this->meta = $strength; $this->getLevel()->setBlock($this, $this, true, false); $this->activate(); $hasChecked = [Vector3::SIDE_WEST => false, Vector3::SIDE_EAST => false, Vector3::SIDE_NORTH => false, Vector3::SIDE_SOUTH => false]; foreach ($hasChecked as $side => $bool) { $needUpdate = $this->getSide($side); if (!in_array(Level::blockHash($needUpdate->x, $needUpdate->y, $needUpdate->z), $hasUpdated)) { $result = $this->updateNormalWire($needUpdate, $strength - 1, $type, $hasUpdated); if (count($result) != count($hasUpdated)) { $hasUpdated = $result; $hasChecked[$side] = true; } } } $baseBlock = $this->add(0, 1, 0); foreach ($hasChecked as $side => $bool) { if (!$bool) { $needUpdate = $this->getLevel()->getBlock($baseBlock->getSide($side)); if (!in_array(Level::blockHash($needUpdate->x, $needUpdate->y, $needUpdate->z), $hasUpdated)) { $result = $this->updateNormalWire($needUpdate, $strength - 1, $type, $hasUpdated); if (count($result) != count($hasUpdated)) { $hasUpdated = $result; $hasChecked[$side] = true; } } } } $baseBlock = $this->add(0, -1, 0); foreach ($hasChecked as $side => $bool) { if (!$bool) { $needUpdate = $this->getLevel()->getBlock($baseBlock->getSide($side)); if (!in_array(Level::blockHash($needUpdate->x, $needUpdate->y, $needUpdate->z), $hasUpdated)) { $result = $this->updateNormalWire($needUpdate, $strength - 1, $type, $hasUpdated); if (count($result) != count($hasUpdated)) { $hasUpdated = $result; $hasChecked[$side] = true; } } } } } } } return $hasUpdated; }
private function computeSpreadBlockLight($x, $y, $z, $currentLight, \SplQueue $queue, array &$visited) { $current = $this->getBlockLightAt($x, $y, $z); if ($current < $currentLight) { $this->setBlockLightAt($x, $y, $z, $currentLight); if (!isset($visited[$index = Level::blockHash($x, $y, $z)])) { $visited[$index] = true; if ($currentLight > 1) { $queue->enqueue(new Vector3($x, $y, $z)); } } } }
public function explodeB() { $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) { $this->level->getServer()->getPluginManager()->callEvent($ev = new EntityExplodeEvent($this->what, $this->source, $this->affectedBlocks, $yield)); if ($ev->isCancelled()) { return false; } else { $yield = $ev->getYield(); $this->affectedBlocks = $ev->getBlockList(); } } $explosionSize = $this->size * 2; $minX = Math::floorFloat($this->source->x - $explosionSize - 1); $maxX = Math::ceilFloat($this->source->x + $explosionSize + 1); $minY = Math::floorFloat($this->source->y - $explosionSize - 1); $maxY = Math::ceilFloat($this->source->y + $explosionSize + 1); $minZ = Math::floorFloat($this->source->z - $explosionSize - 1); $maxZ = Math::ceilFloat($this->source->z + $explosionSize + 1); $explosionBB = new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ); $list = $this->level->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->getFinalDamage(), $ev); $entity->setMotion($motion->multiply($impact)); } } $air = Item::get(Item::AIR); foreach ($this->affectedBlocks as $block) { if ($block->getId() === Block::TNT) { $mot = (new Random())->nextSignedFloat() * M_PI * 2; $tnt = Entity::createEntity("PrimedTNT", $this->level->getChunk($block->x >> 4, $block->z >> 4), new Compound("", ["Pos" => new Enum("Pos", [new Double("", $block->x + 0.5), new Double("", $block->y), new Double("", $block->z + 0.5)]), "Motion" => new Enum("Motion", [new Double("", -sin($mot) * 0.02), new Double("", 0.2), new Double("", -cos($mot) * 0.02)]), "Rotation" => new Enum("Rotation", [new Float("", 0), new Float("", 0)]), "Fuse" => new Byte("Fuse", mt_rand(10, 30))])); $tnt->spawnToAll(); } elseif (mt_rand(0, 100) < $yield) { foreach ($block->getDrops($air) as $drop) { $this->level->dropItem($block->add(0.5, 0.5, 0.5), Item::get(...$drop)); } } $this->level->setBlockIdAt($block->x, $block->y, $block->z, 0); $pos = new Vector3($block->x, $block->y, $block->z); for ($side = 0; $side < 5; $side++) { $sideBlock = $pos->getSide($side); if (!isset($this->affectedBlocks[$index = Level::blockHash($sideBlock->x, $sideBlock->y, $sideBlock->z)]) and !isset($updateBlocks[$index])) { $this->level->getServer()->getPluginManager()->callEvent($ev = new BlockUpdateEvent($this->level->getBlock($sideBlock))); if (!$ev->isCancelled()) { $targetBlock = $ev->getBlock(); $targetBlock->onUpdate(Level::BLOCK_UPDATE_NORMAL); if ($targetBlock instanceof Redstone) { $targetBlock->onRedstoneUpdate(Level::REDSTONE_UPDATE_BREAK, 0); } } $updateBlocks[$index] = true; } } $send[] = new Vector3($block->x - $source->x, $block->y - $source->y, $block->z - $source->z); } $pk = new ExplodePacket(); $pk->x = $this->source->x; $pk->y = $this->source->y; $pk->z = $this->source->z; $pk->radius = $this->size; $pk->records = $send; $this->level->addChunkPacket($source->x >> 4, $source->z >> 4, $pk); return true; }
public function checkTorchOff(Block $pos, array $ignore = []) { $sides = [Vector3::SIDE_EAST, Vector3::SIDE_WEST, Vector3::SIDE_SOUTH, Vector3::SIDE_NORTH, Vector3::SIDE_UP]; foreach ($sides as $side) { if (!in_array($side, $ignore)) { /** @var RedstoneTorch $block */ $block = $pos->getSide($side); if ($block->getId() == self::UNLIT_REDSTONE_TORCH) { $faces = [1 => 4, 2 => 5, 3 => 2, 4 => 3, 5 => 0, 6 => 0, 0 => 0]; if ($this->isRightPlace($block->getSide($faces[$block->meta]), $pos)) { $ignoreBlock = $this->getSide($this->getOppositeSide($faces[$block->meta])); $block->turnOn(Level::blockHash($ignoreBlock->x, $ignoreBlock->y, $ignoreBlock->z)); } } } } }
public function getHash() { return Level::blockHash($this->x, $this->y, $this->z); }
/** * @return bool */ public function explodeA() { if ($this->size < 0.1) { return false; } $vector = new Vector3(0, 0, 0); $vBlock = new Vector3(0, 0, 0); $mRays = intval($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 ($vBlock->y < 0 or $vBlock->y > 127) { break; } $block = $this->level->getBlock($vBlock); if ($block->getId() !== 0) { $blastForce -= ($block->getHardness() / 5 + 0.3) * $this->stepLen; if ($blastForce > 0) { if (!isset($this->affectedBlocks[$index = Level::blockHash($block->x, $block->y, $block->z)])) { $this->affectedBlocks[$index] = $block; } } } $pointerX += $vector->x; $pointerY += $vector->y; $pointerZ += $vector->z; } } } } } return true; }
public function isPowered() { $hash = Level::blockHash($this->x, $this->y, $this->z); if ($this instanceof Transparent) { if ($this->isPointCharged()) { return true; } } elseif ($this->isCharged($hash)) { return true; } for ($side = 0; $side <= 5; $side++) { $near = $this->getSide($side); if ($near->isCharged($hash)) { return true; } } return false; }
protected function checkBlockCollision() { $minX = Math::ceilFloat($this->boundingBox->minX); $minY = Math::ceilFloat($this->boundingBox->minY); $minZ = Math::ceilFloat($this->boundingBox->minZ); $maxX = Math::floorFloat($this->boundingBox->maxX); $maxY = Math::floorFloat($this->boundingBox->maxY); $maxZ = Math::floorFloat($this->boundingBox->maxZ); $blocksInside = []; for ($z = $minZ; $z <= $maxZ; ++$z) { for ($x = $minX; $x <= $maxX; ++$x) { for ($y = $minY; $y <= $maxY; ++$y) { $block = $this->level->getBlock($this->temporalVector->setComponents($x, $y, $z)); if ($block->hasEntityCollision()) { $blocksInside[Level::blockHash($block->x, $block->y, $block->z)] = $block; } } } } foreach ($blocksInside as $block) { $block->onEntityCollide($this); } }
public function deactivateTorch(array $ignore = [], array $notCheck = []) { if ($this->canCalc()) { $this->activated = false; /** @var Door $block */ $sides = [Vector3::SIDE_EAST, Vector3::SIDE_WEST, Vector3::SIDE_SOUTH, Vector3::SIDE_NORTH]; foreach ($sides as $side) { if (!in_array($side, $ignore)) { $block = $this->getSide($side); if (!in_array($hash = Level::blockHash($block->x, $block->y, $block->z), $notCheck)) { $this->deactivateBlock($block); } } } if (!in_array(Vector3::SIDE_DOWN, $ignore)) { $block = $this->getSide(Vector3::SIDE_DOWN); if (!in_array($hash = Level::blockHash($block->x, $block->y, $block->z), $notCheck)) { if (!$this->checkPower($block)) { /** @var $block ActiveRedstoneLamp */ if ($block->getId() == Block::ACTIVE_REDSTONE_LAMP) { $block->turnOff(); } } $block = $this->getSide(Vector3::SIDE_DOWN, 2); $this->deactivateBlock($block); } } //$this->lastUpdateTime = $this->getLevel()->getServer()->getTick(); } }
public function setRedstoneUpdateList($type, $power) { if ($type === Level::REDSTONE_UPDATE_NORMAL) { if ($power > $this->getPower() + 1) { $this_hash = Level::blockHash($this->x, $this->y, $this->z); $this->getLevel()->RedstoneUpdaters[$this_hash] = false; $thispower = $power - 1; $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => $thispower]; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter and $this->getSide(1) instanceof Transparent) { $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->getLevel()->RedstoneUpdaters[$this_hash] = true; $this->doRedstoneListUpdate(); } else { return; } } if ($type === Level::REDSTONE_UPDATE_REPOWER) { $type = Level::REDSTONE_UPDATE_NORMAL; $this_hash = Level::blockHash($this->x, $this->y, $this->z); unset($this->getLevel()->RedstoneRepowers[$this_hash]); $thispower = $this->getPower(); $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => $thispower]; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter and $this->getSide(1) instanceof Transparent) { $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->doRedstoneListUpdate(); } if ($type === Level::REDSTONE_UPDATE_LOSTPOWER) { if ($this->getPower() !== 0 and $power >= $this->getPower + 1) { $thispower = $this->getPower(); $this_hash = Level::blockHash($this->x, $this->y, $this->z); $this->getLevel()->RedstoneUpdateList[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z, 'power' => 0]; $FetchedMaxPower = $this->fetchMaxPower(); if ($FetchedMaxPower == Block::REDSTONESOURCEPOWER) { unset($this->getLevel()->RedstoneUpdateList[$this_hash]); $this->getLevel()->RedstoneRepowers[$this_hash] = ['x' => $this->x, 'y' => $this->y, 'z' => $this->z]; return; } $this->getLevel()->RedstoneUpdaters[$this_hash] = false; for ($side = 2; $side <= 5; $side++) { $near = $this->getSide($side); if ($near instanceof RedstoneTransmitter) { $near_hash = Level::blockHash($near->x, $near->y, $near->z); if (isset($this->getLevel()->RedstoneUpdateList[$near_hash])) { continue; } $near->setRedstoneUpdateList($type, $thispower); } else { $around_down = $near->getSide(0); $around_up = $near->getSide(1); if ($near->getId() == Block::AIR and $around_down instanceof RedstoneTransmitter) { $around_down_hash = Level::blockHash($around_down->x, $around_down->y, $around_down->z); if (isset($this->getLevel()->RedstoneUpdateList[$around_down_hash])) { continue; } $around_down->setRedstoneUpdateList($type, $thispower); } elseif (!$near instanceof Transparent and $around_up instanceof RedstoneTransmitter and $this->getSide(1) instanceof Transparent) { $around_up_hash = Level::blockHash($around_up->x, $around_up->y, $around_up->z); if (isset($this->getLevel()->RedstoneUpdateList[$around_up_hash])) { continue; } $around_up->setRedstoneUpdateList($type, $thispower); } } } $this->getLevel()->RedstoneUpdaters[$this_hash] = true; $this->doRedstoneListUpdate(); } } }
/** * Sets the raw block metadata. * * @param int $x * @param int $y * @param int $z * @param int $data 0-15 */ public function setBlockDataAt($x, $y, $z, $data) { unset($this->blockCache[Level::blockHash($x, $y, $z)]); $this->getChunk($x >> 4, $z >> 4, true)->setBlockData($x & 0xf, $y & 0x7f, $z & 0xf, $data & 0xf); }
protected function checkBlockCollision() { $vector = new Vector3(0, 0, 0); foreach ($blocksaround = $this->getBlocksAround() as $block) { $block->onEntityCollide($this); if ($this->getLevel()->getServer()->redstoneEnabled and !$this->isPlayer) { if ($block instanceof PressurePlate) { $this->activatedPressurePlates[Level::blockHash($block->x, $block->y, $block->z)] = $block; } } $block->addVelocityToEntity($this, $vector); } if ($this->getLevel()->getServer()->redstoneEnabled and !$this->isPlayer) { /** @var \pocketmine\block\PressurePlate $block **/ foreach ($this->activatedPressurePlates as $key => $block) { if (!isset($blocksaround[$key])) { $block->checkActivation(); } } } if ($vector->lengthSquared() > 0) { $vector = $vector->normalize(); $d = 0.014; $this->motionX += $vector->x * $d; $this->motionY += $vector->y * $d; $this->motionZ += $vector->z * $d; } }