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(":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){

View file

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

View file

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

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){
$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;
}

View file

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

View file

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

View file

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

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);
$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();
}