From b3c3f896a3ebafbd53748a71c91a750ed2645f3b Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Fri, 28 Nov 2014 14:44:27 +0100 Subject: [PATCH] Implemented light population, added Level->getFullLight() --- src/pocketmine/block/Block.php | 1 + src/pocketmine/block/BrownMushroom.php | 3 + src/pocketmine/block/BurningFurnace.php | 3 + src/pocketmine/block/EndPortal.php | 3 + src/pocketmine/block/Fire.php | 1 + src/pocketmine/block/GlowingObsidian.php | 3 + src/pocketmine/block/GlowingRedstoneOre.php | 3 + src/pocketmine/block/Glowstone.php | 3 + src/pocketmine/block/Lava.php | 2 + src/pocketmine/block/LitPumpkin.php | 3 + src/pocketmine/block/Torch.php | 3 + src/pocketmine/level/Level.php | 77 +++++++++++++++++++++ 12 files changed, 105 insertions(+) diff --git a/src/pocketmine/block/Block.php b/src/pocketmine/block/Block.php index eb55048d9..7f750b7c9 100644 --- a/src/pocketmine/block/Block.php +++ b/src/pocketmine/block/Block.php @@ -543,6 +543,7 @@ class Block extends Position implements Metadatable{ public $isPlaceable = true; public $hasPhysics = false; public $isFullBlock = true; + public $lightLevel = 0; public $x = 0; public $y = 0; public $z = 0; diff --git a/src/pocketmine/block/BrownMushroom.php b/src/pocketmine/block/BrownMushroom.php index 12cc206dc..addc2294a 100644 --- a/src/pocketmine/block/BrownMushroom.php +++ b/src/pocketmine/block/BrownMushroom.php @@ -26,6 +26,9 @@ use pocketmine\level\Level; use pocketmine\Player; class BrownMushroom extends Flowable{ + + public $lightLevel = 1; + public function __construct(){ parent::__construct(self::BROWN_MUSHROOM, 0, "Brown Mushroom"); $this->hardness = 0; diff --git a/src/pocketmine/block/BurningFurnace.php b/src/pocketmine/block/BurningFurnace.php index 5c9b46d86..90b9450fd 100644 --- a/src/pocketmine/block/BurningFurnace.php +++ b/src/pocketmine/block/BurningFurnace.php @@ -32,6 +32,9 @@ use pocketmine\tile\Furnace; use pocketmine\tile\Tile; class BurningFurnace extends Solid{ + + public $lightLevel = 13; + public function __construct($meta = 0){ parent::__construct(self::BURNING_FURNACE, $meta, "Burning Furnace"); $this->isActivable = true; diff --git a/src/pocketmine/block/EndPortal.php b/src/pocketmine/block/EndPortal.php index 12166f6eb..be2772865 100644 --- a/src/pocketmine/block/EndPortal.php +++ b/src/pocketmine/block/EndPortal.php @@ -24,6 +24,9 @@ namespace pocketmine\block; use pocketmine\math\AxisAlignedBB; class EndPortal extends Solid{ + + public $lightLevel = 1; + public function __construct($meta = 0){ parent::__construct(self::END_PORTAL, $meta, "End Portal"); $this->hardness = 18000000; diff --git a/src/pocketmine/block/Fire.php b/src/pocketmine/block/Fire.php index 7689e6aec..90289cb8d 100644 --- a/src/pocketmine/block/Fire.php +++ b/src/pocketmine/block/Fire.php @@ -32,6 +32,7 @@ use pocketmine\Server; class Fire extends Flowable{ public $hasEntityCollision = true; + public $lightLevel = 15; public function __construct($meta = 0){ parent::__construct(self::FIRE, $meta, "Fire"); diff --git a/src/pocketmine/block/GlowingObsidian.php b/src/pocketmine/block/GlowingObsidian.php index 90fea22db..9367432eb 100644 --- a/src/pocketmine/block/GlowingObsidian.php +++ b/src/pocketmine/block/GlowingObsidian.php @@ -23,6 +23,9 @@ namespace pocketmine\block; class GlowingObsidian extends Solid{ + + public $lightLevel = 12; + public function __construct($meta = 0){ parent::__construct(self::GLOWING_OBSIDIAN, $meta, "Glowing Obsidian"); } diff --git a/src/pocketmine/block/GlowingRedstoneOre.php b/src/pocketmine/block/GlowingRedstoneOre.php index 2ebb26fee..cc647e4d2 100644 --- a/src/pocketmine/block/GlowingRedstoneOre.php +++ b/src/pocketmine/block/GlowingRedstoneOre.php @@ -25,6 +25,9 @@ use pocketmine\item\Item; use pocketmine\level\Level; class GlowingRedstoneOre extends Solid{ + + public $lightLevel = 9; + public function __construct(){ parent::__construct(self::GLOWING_REDSTONE_ORE, 0, "Glowing Redstone Ore"); $this->hardness = 15; diff --git a/src/pocketmine/block/Glowstone.php b/src/pocketmine/block/Glowstone.php index 85df970ec..0e55098c6 100644 --- a/src/pocketmine/block/Glowstone.php +++ b/src/pocketmine/block/Glowstone.php @@ -24,6 +24,9 @@ namespace pocketmine\block; use pocketmine\item\Item; class Glowstone extends Transparent{ + + public $lightLevel = 15; + public function __construct(){ parent::__construct(self::GLOWSTONE_BLOCK, 0, "Glowstone"); $this->hardness = 1.5; diff --git a/src/pocketmine/block/Lava.php b/src/pocketmine/block/Lava.php index 2d4f9542c..8aaeab408 100644 --- a/src/pocketmine/block/Lava.php +++ b/src/pocketmine/block/Lava.php @@ -31,6 +31,8 @@ use pocketmine\Server; class Lava extends Liquid{ + public $lightLevel = 15; + public function __construct($meta = 0){ parent::__construct(self::LAVA, $meta, "Lava"); $this->hardness = 0; diff --git a/src/pocketmine/block/LitPumpkin.php b/src/pocketmine/block/LitPumpkin.php index 465d05224..29fcf0890 100644 --- a/src/pocketmine/block/LitPumpkin.php +++ b/src/pocketmine/block/LitPumpkin.php @@ -25,6 +25,9 @@ use pocketmine\item\Item; use pocketmine\Player; class LitPumpkin extends Solid{ + + public $lightLevel = 15; + public function __construct(){ parent::__construct(self::LIT_PUMPKIN, "Jack o'Lantern"); $this->hardness = 5; diff --git a/src/pocketmine/block/Torch.php b/src/pocketmine/block/Torch.php index 381087bd9..100ee18a1 100644 --- a/src/pocketmine/block/Torch.php +++ b/src/pocketmine/block/Torch.php @@ -26,6 +26,9 @@ use pocketmine\level\Level; use pocketmine\Player; class Torch extends Flowable{ + + public $lightLevel = 15; + public function __construct($meta = 0){ parent::__construct(self::TORCH, $meta, "Torch"); $this->hardness = 0; diff --git a/src/pocketmine/level/Level.php b/src/pocketmine/level/Level.php index 471874e18..204e56f4c 100644 --- a/src/pocketmine/level/Level.php +++ b/src/pocketmine/level/Level.php @@ -881,6 +881,20 @@ class Level implements ChunkManager, Metadatable{ } */ + public function getFullLight(Vector3 $pos){ + $chunk = $this->getChunk($pos->x >> 4, $pos->z >> 4, true); + $level = 0; + if($chunk instanceof FullChunk){ + $level = $chunk->getBlockSkyLight($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f); + //TODO: decrease light level by time of day + if($level < 15){ + $level = max($chunk->getBlockLight($pos->x & 0x0f, $pos->y & 0x7f, $pos->z & 0x0f)); + } + } + + return $level; + } + /** * Gets the Block object on the Vector3 location * @@ -911,6 +925,67 @@ class Level implements ChunkManager, Metadatable{ return $this->blockCache[$index] = Block::get($blockId, $meta, $this->temporalPosition->setComponents($pos->x, $pos->y, $pos->z)); } + public function updateAllLight(Vector3 $pos){ + $this->updateBlockSkyLight($pos); + $this->updateBlockLight($pos); + } + + public function updateBlockSkyLight(Vector3 $pos){ + $block = ($pos instanceof Block) ? $pos : $this->getBlock($pos); + + $current = $this->getBlockSkyLightAt($pos->x, $pos->y, $pos->z); + $decrease = ($block->isSolid and !$block->isTransparent) ? 15 : 0; + + $calculatedLight = $this->computeBlockSkyLight($block, $current, $decrease); + + if($calculatedLight !== $current){ + $this->setBlockSkyLightAt($block->x, $block->y, $block->z, $calculatedLight); + for($side = 0; $side <= 5; ++$side){ + $this->updateBlockSkyLight($block->getSide($side)); + } + } + } + + public function updateBlockLight(Vector3 $pos){ + $block = ($pos instanceof Block) ? $pos : $this->getBlock($pos); + + $current = $this->getBlockLightAt($pos->x, $pos->y, $pos->z); + $decrease = ($block->isSolid and !$block->isTransparent) ? 15 : 1; + + $calculatedLight = $this->computeBlockLight($block, $block->lightLevel, $decrease); + + if($calculatedLight !== $current){ + $this->setBlockLightAt($block->x, $block->y, $block->z, $calculatedLight); + for($side = 0; $side <= 5; ++$side){ + $this->updateBlockLight($block->getSide($side)); + } + } + } + + protected function computeBlockLight(Vector3 $pos, $current, $decrease){ + return max( + $current, + $this->getBlockLightAt($pos->x - 1, $pos->y, $pos->z) - $decrease, + $this->getBlockLightAt($pos->x + 1, $pos->y, $pos->z) - $decrease, + $this->getBlockLightAt($pos->x, $pos->y - 1, $pos->z) - $decrease, + $this->getBlockLightAt($pos->x, $pos->y + 1, $pos->z) - $decrease, + $this->getBlockLightAt($pos->x, $pos->y, $pos->z - 1) - $decrease, + $this->getBlockLightAt($pos->x, $pos->y, $pos->z + 1) - $decrease + ); + } + + protected function computeBlockSkyLight(Vector3 $pos, $current, $decrease){ + return max( + $current, + $this->getBlockSkyLightAt($pos->x - 1, $pos->y, $pos->z) - $decrease, + $this->getBlockSkyLightAt($pos->x + 1, $pos->y, $pos->z) - $decrease, + $this->getBlockSkyLightAt($pos->x, $pos->y - 1, $pos->z) - $decrease, + $this->getBlockSkyLightAt($pos->x, $pos->y + 1, $pos->z) - $decrease, + $this->getBlockSkyLightAt($pos->x, $pos->y, $pos->z - 1) - $decrease, + $this->getBlockSkyLightAt($pos->x, $pos->y, $pos->z + 1) - $decrease + ); + } + /** * Sets on Vector3 the data from a Block object, * does block updates and puts the changes to the send queue. @@ -973,6 +1048,8 @@ class Level implements ChunkManager, Metadatable{ }*/ if($update === true){ + $this->updateAllLight($block); + $this->updateAround($pos); $this->server->getPluginManager()->callEvent($ev = new BlockUpdateEvent($block)); if(!$ev->isCancelled()){