From 945730e148bb01a515d8ecf4205e47e780ce0f2f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 11 May 2022 19:45:37 +0300 Subject: [PATCH] test light simulation + ice melting --- src/material/block/attachable/Torch.php | 6 ++- src/material/block/liquid/Lava.php | 6 ++- src/material/block/misc/Fire.php | 6 ++- src/material/block/ore/GlowingRedstoneOre.php | 6 ++- src/material/block/solid/BurningFurnace.php | 6 ++- src/material/block/solid/GlowingObsidian.php | 6 ++- src/material/block/solid/Glowstone.php | 8 +++- src/material/block/solid/Ice.php | 38 ++++++++++++++++++- src/material/block/solid/LitPumpkin.php | 8 +++- 9 files changed, 74 insertions(+), 16 deletions(-) diff --git a/src/material/block/attachable/Torch.php b/src/material/block/attachable/Torch.php index c9594da2..835fec7a 100644 --- a/src/material/block/attachable/Torch.php +++ b/src/material/block/attachable/Torch.php @@ -19,12 +19,14 @@ * */ -class TorchBlock extends FlowableBlock{ +class TorchBlock extends FlowableBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(TORCH, $meta, "Torch"); $this->hardness = 0; } - + public function getMaxLightValue(){ + return 15; + } public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ $side = $this->getMetadata(); diff --git a/src/material/block/liquid/Lava.php b/src/material/block/liquid/Lava.php index d66f0515..222bd3a0 100644 --- a/src/material/block/liquid/Lava.php +++ b/src/material/block/liquid/Lava.php @@ -19,7 +19,7 @@ * */ -class LavaBlock extends LiquidBlock{ +class LavaBlock extends LiquidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(LAVA, $meta, "Lava"); $this->hardness = 0; @@ -31,6 +31,10 @@ class LavaBlock extends LiquidBlock{ return $ret; } + public function getMaxLightValue(){ + return 15; + } + public function getSourceCount(){ $count = 0; for($side = 2; $side <= 5; ++$side){ diff --git a/src/material/block/misc/Fire.php b/src/material/block/misc/Fire.php index 6706476c..e4d52a1f 100644 --- a/src/material/block/misc/Fire.php +++ b/src/material/block/misc/Fire.php @@ -19,7 +19,7 @@ * */ -class FireBlock extends FlowableBlock{ +class FireBlock extends FlowableBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(FIRE, $meta, "Fire"); $this->isReplaceable = true; @@ -31,7 +31,9 @@ class FireBlock extends FlowableBlock{ public function getDrops(Item $item, Player $player){ return array(); } - + public function getMaxLightValue(){ + return 15; + } public function onUpdate($type){ if($type === BLOCK_UPDATE_NORMAL){ for($s = 0; $s <= 5; ++$s){ diff --git a/src/material/block/ore/GlowingRedstoneOre.php b/src/material/block/ore/GlowingRedstoneOre.php index 37d0cab9..43b408f7 100644 --- a/src/material/block/ore/GlowingRedstoneOre.php +++ b/src/material/block/ore/GlowingRedstoneOre.php @@ -19,7 +19,7 @@ * */ -class GlowingRedstoneOreBlock extends SolidBlock{ +class GlowingRedstoneOreBlock extends SolidBlock implements LightingBlock{ public function __construct(){ parent::__construct(GLOWING_REDSTONE_ORE, 0, "Glowing Redstone Ore"); $this->hardness = 15; @@ -34,7 +34,9 @@ class GlowingRedstoneOreBlock extends SolidBlock{ } return false; } - + public function getMaxLightValue(){ + return 9; + } public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ diff --git a/src/material/block/solid/BurningFurnace.php b/src/material/block/solid/BurningFurnace.php index d47fca17..99288505 100644 --- a/src/material/block/solid/BurningFurnace.php +++ b/src/material/block/solid/BurningFurnace.php @@ -19,7 +19,7 @@ * */ -class BurningFurnaceBlock extends SolidBlock{ +class BurningFurnaceBlock extends SolidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(BURNING_FURNACE, $meta, "Burning Furnace"); $this->isActivable = true; @@ -37,7 +37,9 @@ class BurningFurnaceBlock extends SolidBlock{ $this->level->setBlock($block, $this, true, false, true); return true; } - + public function getMaxLightValue(){ + return 13; + } public function onBreak(Item $item, Player $player){ $this->level->setBlock($this, new AirBlock(), true, true, true); return true; diff --git a/src/material/block/solid/GlowingObsidian.php b/src/material/block/solid/GlowingObsidian.php index 5c9d3517..77da23e5 100644 --- a/src/material/block/solid/GlowingObsidian.php +++ b/src/material/block/solid/GlowingObsidian.php @@ -19,9 +19,13 @@ * */ -class GlowingObsidianBlock extends SolidBlock{ +class GlowingObsidianBlock extends SolidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(GLOWING_OBSIDIAN, $meta, "Glowing Obsidian"); } + public function getMaxLightValue(){ + return 12; + } + } \ No newline at end of file diff --git a/src/material/block/solid/Glowstone.php b/src/material/block/solid/Glowstone.php index 3f1cd121..e49bb9de 100644 --- a/src/material/block/solid/Glowstone.php +++ b/src/material/block/solid/Glowstone.php @@ -19,12 +19,16 @@ * */ -class GlowstoneBlock extends TransparentBlock{ +class GlowstoneBlock extends TransparentBlock implements LightingBlock{ public function __construct(){ parent::__construct(GLOWSTONE_BLOCK, 0, "Glowstone"); $this->hardness = 1.5; } - + + public function getMaxLightValue(){ + return 15; + } + public function getDrops(Item $item, Player $player){ return array( array(GLOWSTONE_DUST, 0, mt_rand(2, 4)), diff --git a/src/material/block/solid/Ice.php b/src/material/block/solid/Ice.php index d128775c..79e510e1 100644 --- a/src/material/block/solid/Ice.php +++ b/src/material/block/solid/Ice.php @@ -24,7 +24,11 @@ class IceBlock extends TransparentBlock{ parent::__construct(ICE, 0, "Ice"); $this->hardness = 2.5; } - + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ + $ret = $this->level->setBlock($this, $this, true, false, true); + $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); + return $ret; + } public function onBreak(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0){ $this->level->setBlock($this, new WaterBlock(), true, false, true); @@ -34,7 +38,37 @@ class IceBlock extends TransparentBlock{ } return true; } - + /* + Scan for blocks which can emit light + args: offsetX: int, offsetY: int, offsetZ: int + do not set offsets more than 15 : no lighting sources are giving so much light + */ + private function scanForNearbyLightSources($offsetX, $offsetY, $offsetZ){ + for($x = -$offsetX; $x <= $offsetX; ++$x){ //i hope it is possible to optimize it + for($z = -$offsetZ; $z <= $offsetZ; ++$z){ + for($y = -$offsetY; $y <= $offsetY; ++$y){ + $pX = $this->x+$x; + $pY = $this->y+$y; + $pZ = $this->z+$z; + $block = $this->level->getBlock(new Vector3($pX, $pY, $pZ)); + if($block instanceof LightingBlock){ //idk is it possible to make it better + return $block; + } + } + } + } + } + public function onUpdate($type){ /*Taken from https://github.com/PocketMine/PocketMine-MP/issues/3249*/ + if($type === BLOCK_UPDATE_RANDOM){ + $light = $this->scanForNearbyLightSources(3,3,3); + if(LightUtils::getLightValueFromNearbySource($light,$this) > 12){ + $this->level->setBlock($this, new WaterBlock(), true); + ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), 10, BLOCK_UPDATE_NORMAL); //additional request for update + } + return BLOCK_UPDATE_RANDOM; + } + return false; + } public function getBreakTime(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0x01){ return 0.20; diff --git a/src/material/block/solid/LitPumpkin.php b/src/material/block/solid/LitPumpkin.php index df6fdaef..eaf66ba3 100644 --- a/src/material/block/solid/LitPumpkin.php +++ b/src/material/block/solid/LitPumpkin.php @@ -19,12 +19,16 @@ * */ -class LitPumpkinBlock extends SolidBlock{ +class LitPumpkinBlock extends SolidBlock implements LightingBlock{ public function __construct($meta = 0){ parent::__construct(LIT_PUMPKIN, $meta, "Jack o'Lantern"); $this->hardness = 5; } - + + public function getMaxLightValue(){ + return 15; + } + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $faces = array( 0 => 1,