block ticking & entity optimizations

This commit is contained in:
GameHerobrine 2023-07-29 09:01:44 +03:00
parent 177d8b53ab
commit 2ad5a5650e
8 changed files with 44 additions and 33 deletions

View file

@ -611,11 +611,11 @@ class PocketMinecraftServer{
$this->preparedSQL->updateAction->bindValue(":time", $time, SQLITE3_FLOAT); $this->preparedSQL->updateAction->bindValue(":time", $time, SQLITE3_FLOAT);
$this->preparedSQL->updateAction->bindValue(":id", $cid, SQLITE3_INTEGER); $this->preparedSQL->updateAction->bindValue(":id", $cid, SQLITE3_INTEGER);
$this->preparedSQL->updateAction->execute(); $this->preparedSQL->updateAction->execute();
if(!isset($this->schedule[$cid]) || !isset($this->schedule[$cid][0]) || !@is_callable($this->schedule[$cid][0])){ if(isset($this->schedule[$cid]) && is_array($this->schedule[$cid]) && isset($this->schedule[$cid][0]) && is_callable($this->schedule[$cid][0])){
$return = false;
}else{
++$actionCount; ++$actionCount;
$return = call_user_func($this->schedule[$cid][0], $this->schedule[$cid][1], $this->schedule[$cid][2]); $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){ if($action["repeat"] == 0 or $return === false){

View file

@ -539,13 +539,6 @@ class Entity extends Position
$ywMin = floor($aABB->minY + 0.4); $ywMin = floor($aABB->minY + 0.4);
$ywMax = ceil($aABB->maxY - 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($x = $x0; $x < $x1; ++$x){
for($y = $y0; $y < $y1; ++$y){ for($y = $y0; $y < $y1; ++$y){
for($z = $z0; $z < $z1; ++$z){ for($z = $z0; $z < $z1; ++$z){

View file

@ -78,7 +78,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ //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->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();
} }

View file

@ -50,7 +50,7 @@ class MobController
; ;
} }
$this->faceEntity($this->entity->add($ox, $oy, $oz)); $this->faceEntity($ox, $oy, $oz);
if($this->entity->knockbackTime <= 0){ if($this->entity->knockbackTime <= 0){
$this->entity->moveEntityWithOffset($ox, $oy, $oz); $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)); return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z));
} }
public function faceEntity(Vector3 $v){ public function faceEntity($x, $y, $z){
$d = $v->subtract($this->entity)->normalize(); $len = sqrt($x*$x + $z*$z + $y*$y);
$dx = $d->x; //$d = $len == 0 ?//$v->subtract($this->entity)->normalize();
$dz = $d->z; 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))); $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$thetaOffset = $dz < 0 ? 90 : 270; $thetaOffset = $dz < 0 ? 90 : 270;
@ -97,10 +104,10 @@ class MobController
if($pitch){ if($pitch){
$diff = sqrt($x * $x + $z * $z); $diff = sqrt($x * $x + $z * $z);
$calcPitch = $diff == 0 ? ($y < 0 ? -90 : 90) : rad2deg(atan($y / $diff)); $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; return true;
} }

View file

@ -11,6 +11,7 @@ if(!function_exists("cli_set_process_title")){
} }
} }
function dummy(){ function dummy(){
} }

View file

@ -27,18 +27,18 @@ class FarmlandBlock extends TransparentBlock{
public static function onRandomTick(Level $level, $x, $y, $z){ public static function onRandomTick(Level $level, $x, $y, $z){
$meta = $level->level->getBlockDamage($x, $y, $z); $meta = $level->level->getBlockDamage($x, $y, $z);
$b = $level->level->getBlockID($x, $y + 1, $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); $water = self::checkWaterStatic($level, $x, $y, $z);
if($water){ 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)){ }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){ public static function checkWaterStatic(Level $level, $x, $y, $z){

View file

@ -318,11 +318,21 @@ class PMFLevel extends PMF{
$this->locationTable[$index][0] |= 1 << $Y; $this->locationTable[$index][0] |= 1 << $Y;
return true; return true;
} }
private function report(){ /**
console("[ERROR] A weird error in PMFLevel just happeneed. Values: "); * This method is faster, but may cause a lot of problems with unchecked values
var_dump(func_get_args()); * @param integer $chunkX chunk(0-16)
console("[NOTICE] If you see this message, you should send the log with error to the devs."); * @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){ public function getBlockID($x, $y, $z){
if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
return 0; return 0;

View file

@ -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); $this->level->setBlock($x, $y, $z, $id, $meta);
$pk = new UpdateBlockPacket; $pk = new UpdateBlockPacket;
$pk->x = $x; $pk->x = $x;
@ -310,7 +310,7 @@ class Level{
$pk->block = $id; $pk->block = $id;
$pk->meta = $meta; $pk->meta = $meta;
$this->server->api->player->broadcastPacket($this->players, $pk); $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); $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; if(!$this->stopTime) ++$this->time;
for($cX = 0; $cX < 16; ++$cX){ for($cX = 0; $cX < 16; ++$cX){
for($cZ = 0; $cZ < 16; ++$cZ){ 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){ for($c = 0; $c <= 20; ++$c){
$xyz = mt_rand(0, 0xffffffff) >> 2;//TODO fix php5 $xyz = mt_rand(0, 0xffffffff) >> 2;//TODO fix php5
$x = $xyz & 0xf; $x = $xyz & 0xf;
$z = ($xyz >> 8) & 0xf; //TODO might be possible to make some micro optmizations $z = ($xyz >> 8) & 0xf; //TODO might be possible to make some micro optmizations
$y = ($xyz >> 16) & 0x7f; $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])){ if(isset(self::$randomUpdateBlocks[$id])){
$cl = Block::$class[$id]; $cl = Block::$class[$id];
$cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4)); $cl::onRandomTick($this, ($cX << 4) + $x, $y, $z + ($cZ << 4));
@ -340,11 +342,9 @@ class Level{
unset($this->server->entities[$k]); unset($this->server->entities[$k]);
continue; continue;
} }
if($e->class === ENTITY_MOB && !$e->isPlayer()){ if($e->class === ENTITY_MOB && !$e->isPlayer()){
++$this->totalMobsAmount; ++$this->totalMobsAmount;
} }
if($e->needsUpdate){ if($e->needsUpdate){
$e->update(); $e->update();
} }