From 2ad5a5650ef0bf69968653208be2c4cc6c89fef9 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 29 Jul 2023 09:01:44 +0300 Subject: [PATCH] block ticking & entity optimizations --- src/PocketMinecraftServer.php | 6 +++--- src/entity/Entity.php | 7 ------- src/entity/Living.php | 2 +- src/entity/ai/MobController.php | 21 +++++++++++++------- src/functions.php | 1 + src/material/block/nonfull/FarmlandBlock.php | 12 +++++------ src/pmf/PMFLevel.php | 18 +++++++++++++---- src/world/Level.php | 10 +++++----- 8 files changed, 44 insertions(+), 33 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 6a7cf432..5dd2123e 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -611,11 +611,11 @@ class PocketMinecraftServer{ $this->preparedSQL->updateAction->bindValue(":time", $time, SQLITE3_FLOAT); $this->preparedSQL->updateAction->bindValue(":id", $cid, SQLITE3_INTEGER); $this->preparedSQL->updateAction->execute(); - if(!isset($this->schedule[$cid]) || !isset($this->schedule[$cid][0]) || !@is_callable($this->schedule[$cid][0])){ - $return = false; - }else{ + if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])){ ++$actionCount; $return = call_user_func($this->schedule[$cid][0], $this->schedule[$cid][1], $this->schedule[$cid][2]); + }else{ + $return = false; } if($action["repeat"] == 0 or $return === false){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 54341b80..fd6d80b7 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -539,13 +539,6 @@ class Entity extends Position $ywMin = floor($aABB->minY + 0.4); $ywMax = ceil($aABB->maxY - 0.4); - - $x0 = $x0 < 0 ? 0 : $x0; - $y0 = $y0 < 0 ? 0 : $y0; - $z0 = $z0 < 0 ? 0 : $z0; - $x1 = $x1 > 256 ? 256 : $x1; - $y1 = $y1 > 128 ? 128 : $y1; - $z1 = $z1 > 256 ? 256 : $z1; for($x = $x0; $x < $x1; ++$x){ for($y = $y0; $y < $y1; ++$y){ for($z = $z0; $z < $z1; ++$z){ diff --git a/src/entity/Living.php b/src/entity/Living.php index 09414412..1d1101f0 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -78,7 +78,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); //} - $this->pathFollower->followPath(); + //$this->pathFollower->followPath(); } diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 31a12dcb..d1b3f0d5 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -50,7 +50,7 @@ class MobController ; } - $this->faceEntity($this->entity->add($ox, $oy, $oz)); + $this->faceEntity($ox, $oy, $oz); if($this->entity->knockbackTime <= 0){ $this->entity->moveEntityWithOffset($ox, $oy, $oz); } @@ -76,10 +76,17 @@ class MobController return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z)); } - public function faceEntity(Vector3 $v){ - $d = $v->subtract($this->entity)->normalize(); - $dx = $d->x; - $dz = $d->z; + public function faceEntity($x, $y, $z){ + $len = sqrt($x*$x + $z*$z + $y*$y); + //$d = $len == 0 ?//$v->subtract($this->entity)->normalize(); + if($len == 0){ + $dx = 0; + $dz = 0; + }else{ + $dx = $x / $len; + $dz = $z / $len; + } + $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $thetaOffset = $dz < 0 ? 90 : 270; @@ -97,10 +104,10 @@ class MobController if($pitch){ $diff = sqrt($x * $x + $z * $z); $calcPitch = $diff == 0 ? ($y < 0 ? -90 : 90) : rad2deg(atan($y / $diff)); - $this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch; + $this->entity->pitch = $calcPitch; } - $this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";"); + //$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";"); return true; } diff --git a/src/functions.php b/src/functions.php index 49983ebb..ab350fd6 100644 --- a/src/functions.php +++ b/src/functions.php @@ -11,6 +11,7 @@ if(!function_exists("cli_set_process_title")){ } } + function dummy(){ } diff --git a/src/material/block/nonfull/FarmlandBlock.php b/src/material/block/nonfull/FarmlandBlock.php index d6f52fd9..59ef2b9e 100644 --- a/src/material/block/nonfull/FarmlandBlock.php +++ b/src/material/block/nonfull/FarmlandBlock.php @@ -27,18 +27,18 @@ class FarmlandBlock extends TransparentBlock{ public static function onRandomTick(Level $level, $x, $y, $z){ $meta = $level->level->getBlockDamage($x, $y, $z); $b = $level->level->getBlockID($x, $y + 1, $z); - if($meta === 0 && mt_rand(0, 5) === 0){ + if(!StaticBlock::getIsFlowable($b)){ + $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); + }else if($meta === 0 && mt_rand(0, 5) === 0){ $water = self::checkWaterStatic($level, $x, $y, $z); if($water){ - $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(FARMLAND, 1), true, false, true); + $level->fastSetBlockUpdate($x, $y, $z, FARMLAND, 1, true); }elseif($b != 0 && StaticBlock::getIsTransparent($b)){ - $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(DIRT, 0), true, false, true); + $level->fastSetBlockUpdate($x, $y, $z, DIRT, 0, true); } } - if(!StaticBlock::getIsFlowable($b)){ - $level->setBlock(new Position($x, $y, $z, $level), BlockAPI::get(DIRT, 0), true, false, true); - } + } public static function checkWaterStatic(Level $level, $x, $y, $z){ diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 8c52a229..ccf2e299 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -318,11 +318,21 @@ class PMFLevel extends PMF{ $this->locationTable[$index][0] |= 1 << $Y; return true; } - private function report(){ - console("[ERROR] A weird error in PMFLevel just happeneed. Values: "); - var_dump(func_get_args()); - console("[NOTICE] If you see this message, you should send the log with error to the devs."); + /** + * This method is faster, but may cause a lot of problems with unchecked values + * @param integer $chunkX chunk(0-16) + * @param integer $chunkY chunk(0-8) + * @param integer $chunkZ chunk(0-16) + * @param integer $blockX block(0-16) + * @param integer $blockY block(0-16) + * @param integer $blockZ block(0-16) + * @param integer $index chunk index + * @return number + */ + public function fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index){ + return ($this->chunks[$index][$chunkY] === false) ? 0 : ord($this->chunks[$index][$chunkY][$blockY + ($blockX << 5) + ($blockZ << 9)]); } + public function getBlockID($x, $y, $z){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return 0; diff --git a/src/world/Level.php b/src/world/Level.php index 1c7e2254..2235d30f 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -301,7 +301,7 @@ class Level{ } } - public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlock = false){ + public function fastSetBlockUpdate($x, $y, $z, $id, $meta, $updateBlocksAround = false){ $this->level->setBlock($x, $y, $z, $id, $meta); $pk = new UpdateBlockPacket; $pk->x = $x; @@ -310,7 +310,7 @@ class Level{ $pk->block = $id; $pk->meta = $meta; $this->server->api->player->broadcastPacket($this->players, $pk); - if($updateBlock){ + if($updateBlocksAround){ $this->server->api->block->blockUpdateAround(new Position($x, $y, $z, $this), BLOCK_UPDATE_NORMAL, 1); } } @@ -320,12 +320,14 @@ class Level{ if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ + $index = $this->level->getIndex($cX, $cZ); + if(!isset($this->level->chunks[$index]) || $this->level->chunks[$index] === false) continue; for($c = 0; $c <= 20; ++$c){ $xyz = mt_rand(0, 0xffffffff) >> 2;//TODO fix php5 $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)); + $id = $this->level->fastGetBlockID($cX, $y >> 4, $cZ, $x, $y & 0xf, $z, $index); //$this->level->getBlockID(($cX << 4) + $x, $y, $z + ($cZ << 4)); if(isset(self::$randomUpdateBlocks[$id])){ $cl = Block::$class[$id]; $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); @@ -340,11 +342,9 @@ class Level{ unset($this->server->entities[$k]); continue; } - if($e->class === ENTITY_MOB && !$e->isPlayer()){ ++$this->totalMobsAmount; } - if($e->needsUpdate){ $e->update(); }