From 4544b7bb6369afd3a6dee77f484d4d187ce88ea8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 23 Jun 2023 20:52:50 +0300 Subject: [PATCH] decreased block updates per chunk from 80 to 20, PMFLevel block methods should be a bit faster now, grass spread rework, leaf decay fix --- src/Player.php | 1 - src/dependencies.php | 1 + src/material/block/misc/FireBlock.php | 1 - src/material/block/solid/DirtBlock.php | 4 +- src/material/block/solid/GrassBlock.php | 12 ++++++ src/material/block/solid/LeavesBlock.php | 6 +-- src/material/block/solid/WoodBlock.php | 18 ++++++++- src/pmf/PMFLevel.php | 47 +++++++++--------------- src/world/Level.php | 43 ++++++++++------------ 9 files changed, 73 insertions(+), 60 deletions(-) diff --git a/src/Player.php b/src/Player.php index 442fb7b8..26ed483f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2168,7 +2168,6 @@ class Player{ if($this->entity->linkedEntity instanceof Entity){ $pk = new SetEntityMotionPacket; $pk->eid = $this->entity->linkedEntity->eid; - var_dump($packet); $pk->speedX = ($this->entity->x - $this->entity->linkedEntity->x)*30; $pk->speedY = ($this->entity->y - $this->entity->linkedEntity->y)*30; $pk->speedZ = ($this->entity->z - $this->entity->linkedEntity->z)*30; diff --git a/src/dependencies.php b/src/dependencies.php index d680bd7d..71e71536 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -128,6 +128,7 @@ require_once(FILE_PATH . "/src/plugin/OtherPluginRequirement.php"); require_once(FILE_PATH . "/src/plugin/DummyPlugin.php"); require_once(FILE_PATH . "/src/plugin/phar/IClassLoader.php"); require_once(FILE_PATH . "/src/plugin/phar/PharUtils.php"); +require_once(FILE_PATH . "/src/world/generator/vanilla/biome/Biome.php"); require_once(FILE_PATH . "/src/world/generator/vanilla/feature/Feature.php"); require_all(FILE_PATH . "src/"); diff --git a/src/material/block/misc/FireBlock.php b/src/material/block/misc/FireBlock.php index fdd07e61..02d5da18 100644 --- a/src/material/block/misc/FireBlock.php +++ b/src/material/block/misc/FireBlock.php @@ -10,7 +10,6 @@ class FireBlock extends FlowableBlock implements LightingBlock{ } public static function onRandomTick(Level $level, $x, $y, $z){ - console("Fire tick at $x $y $z"); if($level->level->getBlockID($x, $y - 1, $z) !== NETHERRACK){ $level->setBlock(new Position($x, $y, $z, $level), new AirBlock(), true, false, true); } diff --git a/src/material/block/solid/DirtBlock.php b/src/material/block/solid/DirtBlock.php index ccb09b66..a3ca0e4d 100644 --- a/src/material/block/solid/DirtBlock.php +++ b/src/material/block/solid/DirtBlock.php @@ -21,13 +21,13 @@ class DirtBlock extends SolidBlock{ return false; } public static function onRandomTick(Level $level, $x, $y, $z){ - if(mt_rand(0, 3) == 0){ + /*if(mt_rand(0, 3) == 0){ $up = $level->getBlockWithoutVector($x, $y + 1, $z, false); //$this->getSide(1); if(($up->isTransparent === false) or ($up->isLiquid) or ($up->getID() == 60)) return false; if(self::getGrassInRadius($level, $x, $y, $z)){ $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(GRASS), true, false, true); } - } + }*/ } public function getBlockID($x, $y, $z){ diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 1775789b..dc7cb0e2 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -49,6 +49,18 @@ class GrassBlock extends SolidBlock{ public static function onRandomTick(Level $level, $x, $y, $z){ if(!$level->getBlockWithoutVector($x, $y + 1, $z, false)->isTransparent && mt_rand(0, 2) == 1){ $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0); + }else{ + for($cnt = 0; $cnt < 4; ++$cnt){ + $x = $x + mt_rand(0, 2) - 1; + $y = $y + mt_rand(0, 4) - 3; + $z = $z + mt_rand(0, 2) - 1; + + $blockUp = $level->getBlockWithoutVector($x, $y + 1, $z, false); + if($blockUp->isTransparent && !$blockUp->isLiquid && !($blockUp->getID() === 60) && $level->level->getBlockID($x, $y, $z) === DIRT){ + $level->fastSetBlockUpdate($x, $y, $z, GRASS, 0); + } + + } } } diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index a9c31bed..b0655a6d 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -42,7 +42,6 @@ class LeavesBlock extends TransparentBlock{ $b = $level->level->getBlock($x, $y, $z); $id = $b[0]; $meta = $b[1]; - if(($meta & 0b00001100) === 0x08){ $meta &= 0x03; $visited = array(); @@ -55,7 +54,8 @@ class LeavesBlock extends TransparentBlock{ if(($meta & 0x03) === LeavesBlock::OAK and mt_rand(1,200) === 1){ //Apples ServerAPI::request()->api->entity->drop(new Position($x, $y, $z, $level), BlockAPI::getItem(APPLE, 0, 1)); } - return BLOCK_UPDATE_NORMAL; + }else{ + $level->fastSetBlockUpdate($x, $y, $z, $id, $meta); } } } @@ -63,7 +63,7 @@ class LeavesBlock extends TransparentBlock{ if($type === BLOCK_UPDATE_NORMAL){ if(($this->meta & 0b00001100) === 0){ $this->meta |= 0x08; - $this->level->setBlock($this, $this, false, false, true); + $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->id, $this->meta); } } return false; diff --git a/src/material/block/solid/WoodBlock.php b/src/material/block/solid/WoodBlock.php index 2ee7cd0c..c82d434f 100644 --- a/src/material/block/solid/WoodBlock.php +++ b/src/material/block/solid/WoodBlock.php @@ -32,7 +32,23 @@ class WoodBlock extends SolidBlock{ $this->level->setBlock($block, $this, true, false, true); return true; } - + public function onBreak(Item $item, Player $player){ + parent::onBreak($item, $player); + for($xOff = -4; $xOff <= 4; ++$xOff){ + for($yOff = -4; $yOff <= 4; ++$yOff){ + for($zOff = -4; $zOff <= 4; ++$zOff){ + $b = $player->level->level->getBlock($this->x + $xOff, $this->y + $yOff, $this->z + $zOff); + $id = $b[0]; + $meta = $b[1]; + if($id === LEAVES){ + if(($meta & 0x8) === 0){ + $player->level->fastSetBlockUpdate($this->x + $xOff, $this->y + $yOff, $this->z + $zOff, $id, $meta | 8); + } + } + } + } + } + } public function getDrops(Item $item, Player $player){ return array( array($this->id, $this->meta & 0x03, 1), diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index ae3bda7d..8c52a229 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -203,9 +203,7 @@ class PMFLevel extends PMF{ } public function getIndex($X, $Z){ - $X = (int) $X; - $Z = (int) $Z; - return ($Z << $this->log) + $X; + return ((int) $Z << 4) + (int) $X; //statically 4, setting it to something else would destroy everything } public function saveChunk($X, $Z){ @@ -333,15 +331,14 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $index = $this->getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){ + return 0; + } $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ - $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); - }else{ //php8 fix - $b = 0; - } + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); return $b; } @@ -379,21 +376,14 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $index = $this->getIndex($X, $Z); + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false || ($this->chunks[$index][$Y] === false)){ + return 0; + } $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ - $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); - }else{ //php8 fix - $m = 0; - } - - if(($y & 1) === 0){ - $m = $m & 0x0F; - }else{ - $m = $m >> 4; - } - return $m; + $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); + return ($y & 1) === 0 ? $m & 0x0F : $m >> 4; } public function setBlockDamage($x, $y, $z, $damage){ @@ -439,25 +429,24 @@ class PMFLevel extends PMF{ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return [AIR, 0]; } + $index = $this->getIndex($X, $Z); - if(!isset($this->chunks[$index]) or $this->chunks[$index] === false){ + if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ if($this->loadChunk($X, $Z) === false){ return [AIR, 0]; } - }elseif($this->chunks[$index][$Y] === false){ + } + if($this->chunks[$index][$Y] === false){ return [AIR, 0]; } $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; #Need to fix. But idk how. - if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix - $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); - $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); - }else{ - $b = 0; - $m = 0; - } + + //if(is_string($this->chunks[$index][$Y])){ //PHP8 warn fix + $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); + $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); if(($y & 1) === 0){ $m = $m & 0x0F; diff --git a/src/world/Level.php b/src/world/Level.php index abee0b5b..eef80eb7 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -301,31 +301,28 @@ class Level{ public function onTick(PocketMinecraftServer $server){ //$ents = $server->api->entity->getAll($this); if(!$this->stopTime) ++$this->time; - $p = new Position(0, 0, 0, $this); - for($i = 0; $i < 256; ++$i){ //0000 0000 x z - $cZ = $i & 0xf; - $cX = $i >> 4; - for($c = 0; $c <= 80; ++$c){ - $this->randInt1 = $this->randInt1 * 3 + $this->randInt2; - $xyz = $this->randInt1 >> 2; - $this->randInt1 = $this->randInt1 & 0xffffffff; //bad php i hate u why no uint - $x = $xyz & 0xf; - $z = ($xyz >> 8) & 0xf; //TODO might be possible to make some micro optmizations - $y = ($xyz >> 16) & 0x7f; - $idmeta = $this->level->getBlock(($cX << 4) + $x, $y, $z + ($cZ << 4)); - $id = $idmeta[0]; - if(isset(self::$randomUpdateBlocks[$id])){ - $c = nullsafe(Block::$class[$id], false); - if($c !== false){ - $c::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); + for($cX = 0; $cX < 16; ++$cX){ + for($cZ = 0; $cZ < 16; ++$cZ){ + for($c = 0; $c <= 20; ++$c){ + $this->randInt1 = $this->randInt1 * 3 + $this->randInt2; + $xyz = $this->randInt1 >> 2; + $this->randInt1 = $this->randInt1 & 0xffffffff; //bad php i hate u why no uint + $x = $xyz & 0xf; + $z = ($xyz >> 8) & 0xf; //TODO might be possible to make some micro optmizations + $y = ($xyz >> 16) & 0x7f; + $id = $this->level->getBlockID(($cX << 4) + $x, $y, $z + ($cZ << 4)); + if(isset(self::$randomUpdateBlocks[$id])){ + $cl = Block::$class[$id]; + //if($cl !== false){ + $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); + //} + //$p->setXYZ(($cX << 4) + $x, $y, $z + ($cZ << 4)); + //$b = BlockAPI::get($idmeta[0], $idmeta[1], $p); //$this->getBlockWithoutVector(); + //if($b instanceof Block){ + // $b->onUpdate(BLOCK_UPDATE_RANDOM); + //} } - //$p->setXYZ(($cX << 4) + $x, $y, $z + ($cZ << 4)); - //$b = BlockAPI::get($idmeta[0], $idmeta[1], $p); //$this->getBlockWithoutVector(); - //if($b instanceof Block){ - // $b->onUpdate(BLOCK_UPDATE_RANDOM); - //} } - } } foreach($this->entityList as $k => $e){