Merge pull request #8 from GameHerobrine/test

test light simulation + ice melting
This commit is contained in:
kotyara 2022-05-11 19:03:57 +00:00 • committed by GitHub
commit cc18d2657d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 106 additions and 16 deletions

View file

@ -0,0 +1,9 @@
<?php
/*
BLOCKS:
BurningFurnace, Fire, GlowingObsidian, GlowingRedstoneOre, Glowstone, Lava, LitPumpkin, Torch
*/
interface LightingBlock{
/*Max light distance (Used to melt ice)*/
public function getMaxLightValue(); /*number of blocks must always be Integer */
}

View file

@ -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();

View file

@ -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){

View file

@ -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){

View file

@ -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){

View file

@ -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;

View file

@ -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;
}
}

View file

@ -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)),

View file

@ -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;

View file

@ -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,

23
src/utils/LightUtils.php Normal file
View file

@ -0,0 +1,23 @@
<?php
class LightUtils {
/*
args: $source: <Block> instanceof LightingBlock, $block: Block
*/
public static function getLightValueFromNearbySource($source, $block){
if($source === null || $block === null){
return 0;
}
$distance = sqrt(LightUtils::sqr($source->x - $block->x)+LightUtils::sqr($source->y - $block->y)+LightUtils::sqr($source->z - $block->z)); /*Distance between 2 3d objects*/
$result = floor($source->getMaxLightValue() - $distance); //i dont need something like 0.2192032434454...
return $result <= 0 ? 0 : $result; //if result is smaller or equals 0 then return 0 else return result
}
/* Used only internally for calculating 2^(2) */
private static function sqr($val){
return pow($val,2);
}
}