small entity optimizations

This commit is contained in:
GameHerobrine 2022-11-28 21:33:55 +03:00
parent 54bdb8a244
commit 019e7f26ea
5 changed files with 383 additions and 263 deletions

View file

@ -14,7 +14,6 @@ class EntityAPI{
} }
public function init(){ public function init(){
$this->server->schedule(25, [$this, "updateEntities"], [], true);
$this->server->api->console->register("summon", "<mob>", [$this, "commandHandler"]); $this->server->api->console->register("summon", "<mob>", [$this, "commandHandler"]);
$this->server->api->console->register("spawnmob", "<mob>", [$this, "commandHandler"]); $this->server->api->console->register("spawnmob", "<mob>", [$this, "commandHandler"]);
$this->server->api->console->register("despawn", "", [$this, "CommandHandler"]); $this->server->api->console->register("despawn", "", [$this, "CommandHandler"]);
@ -102,7 +101,13 @@ class EntityAPI{
break; break;
}else{ }else{
if($args[0] === "all"){ if($args[0] === "all"){
$l = $this->server->query("SELECT EID FROM entities WHERE class = 2 or class = 3 or class = 4 or class = 5;"); $cnt = 0;
foreach($this->entities as $e){
if(!$e->isPlayer()){ //if player, not despawning
$this->remove($e->eid);
$cnt++;
}
}
}else{ }else{
$array = explode(",", strtolower($args[0])); $array = explode(",", strtolower($args[0]));
if(count($array) > 4){ if(count($array) > 4){
@ -117,8 +122,6 @@ class EntityAPI{
} }
$despawning = substr($list, 0, -4); $despawning = substr($list, 0, -4);
$l = $this->server->query("SELECT EID FROM entities WHERE class = ".$despawning.";"); $l = $this->server->query("SELECT EID FROM entities WHERE class = ".$despawning.";");
}
}
if($l !== false and $l !== true){ if($l !== false and $l !== true){
while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){
$e = $this->get($e["EID"]); $e = $this->get($e["EID"]);
@ -128,7 +131,10 @@ class EntityAPI{
} }
} }
} }
$output .= "$cnt entities have been despawned!"; }
}
$output = "$cnt entities have been despawned!";
break; break;
} }
return $output; return $output;
@ -174,38 +180,21 @@ class EntityAPI{
public function remove($eid){ public function remove($eid){
if(isset($this->entities[$eid])){ if(isset($this->entities[$eid])){
$entity = $this->entities[$eid]; $this->entities[$eid]->closed = true;
$this->entities[$eid] = null; if($this->entities[$eid]->isPlayer()){
unset($this->entities[$eid]);
$entity->closed = true;
$this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";");
if($entity->class === ENTITY_PLAYER){
$pk = new RemovePlayerPacket; $pk = new RemovePlayerPacket;
$pk->eid = $entity->eid; $pk->eid = $eid;
$pk->clientID = 0; $pk->clientID = 0;
$this->server->api->player->broadcastPacket($this->server->api->player->getAll(), $pk); $this->server->api->player->broadcastPacket($this->entities[$eid]->level->players, $pk);
}else{ }else{
$pk = new RemoveEntityPacket; $pk = new RemoveEntityPacket;
$pk->eid = $entity->eid; $pk->eid = $eid;
$this->server->api->player->broadcastPacket($this->server->api->player->getAll($entity->level), $pk); $this->server->api->player->broadcastPacket($this->entities[$eid]->level->players, $pk);
}
$this->server->api->dhandle("entity.remove", $entity);
$entity = null;
unset($entity);
}
}
public function updateEntities(){
$l = $this->server->query("SELECT EID FROM entities WHERE hasUpdate = 1;");
if($l !== false and $l !== true){
while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){
$e = $this->get($e["EID"]);
if($e instanceof Entity){
$e->update();
$this->server->query("UPDATE entities SET hasUpdate = 0 WHERE EID = " . $e->eid . ";");
}
} }
$this->server->api->dhandle("entity.remove", $this->entities[$eid]);
unset($this->entities[$eid]);
$this->entities[$eid] = null;
$this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";");
} }
} }

View file

@ -284,7 +284,7 @@ class Player{
$packet->messageIndex = $this->counter[3]++; $packet->messageIndex = $this->counter[3]++;
$packet->reliability = 2; $packet->reliability = 2;
$this->buffer->data[] = $packet; @$this->buffer->data[] = $packet;
$this->bufferLen += 6 + $len; $this->bufferLen += 6 + $len;
return []; return [];
} }
@ -760,7 +760,7 @@ class Player{
if($data->eid === $this->eid or $data->level !== $this->level){ if($data->eid === $this->eid or $data->level !== $this->level){
break; break;
} }
if($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0){ //causer of packet flood is eliminated. if(($data->speedX === 0 && $data->speedY === 0 && $data->speedZ === 0) || ($data->speedX === $data->lastSpeedX && $data->speedY === $data->lastSpeedY && $data->lastSpeedZ === $data->speedZ)){ //causer of packet flood is eliminated.
break; break;
} }
$pk = new SetEntityMotionPacket; $pk = new SetEntityMotionPacket;
@ -769,6 +769,9 @@ class Player{
$pk->speedY = $data->speedY; $pk->speedY = $data->speedY;
$pk->speedZ = $data->speedZ; $pk->speedZ = $data->speedZ;
$this->dataPacket($pk); $this->dataPacket($pk);
$data->lastSpeedZ = $data->speedZ;
$data->lastSpeedY = $data->speedY;
$data->lastSpeedX = $data->speedX;
break; break;
case "entity.animate": case "entity.animate":
if($data["eid"] === $this->eid or $data["entity"]->level->getName() === $this->level->getName()){ if($data["eid"] === $this->eid or $data["entity"]->level->getName() === $this->level->getName()){

View file

@ -24,14 +24,21 @@ class Entity extends Position
public $eid; public $eid;
public $type; public $type;
public $name; public $name;
public $x; public $x, $y, $z;
public $y; public $speedX, $speedY, $speedZ, $speed;
public $z; public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0, $lastSpeedX = 0, $lastSpeedY = 0, $lastSpeedZ = 0;
public $speedX; /**
public $speedY; * 0 = lastX,
public $speedZ; * 1 = lastY,
public $speed; * 2 = lastZ,
public $lastX = 0, $lastY = 0, $lastZ = 0, $lastYaw = 0, $lastPitch = 0, $lastTime = 0, $lastHeadYaw = 0; * 3 = lastYaw,
* 4 = lastPitch,
* 5 = lastTime.
*
* It is not recommended to use this and it is left as a backwards compability.
*
* @var array
*/
public $last; public $last;
public $yaw, $headYaw; public $yaw, $headYaw;
public $pitch; public $pitch;
@ -393,6 +400,10 @@ class Entity extends Position
$endY = $startY + 2; $endY = $startY + 2;
$endZ = $startZ + 2; $endZ = $startZ + 2;
$waterDone = false; $waterDone = false;
if($this->isPlayer()){
}
if($this->isPlayer()){
for($y = $startY; $y <= $endY; ++ $y){ for($y = $startY; $y <= $endY; ++ $y){
for($x = $startX; $x <= $endX; ++ $x){ for($x = $startX; $x <= $endX; ++ $x){
for($z = $startZ; $z <= $endZ; ++ $z){ for($z = $startZ; $z <= $endZ; ++ $z){
@ -450,6 +461,8 @@ class Entity extends Position
} }
} }
} }
}
return $hasUpdate; return $hasUpdate;
} }
@ -491,6 +504,7 @@ class Entity extends Position
$endZ = ceil($this->z - 0.5 + $this->width + 1); $endZ = ceil($this->z - 0.5 + $this->width + 1);
$support = false; $support = false;
$isFlying = true; $isFlying = true;
if($this->isPlayer()){
for($z = $startZ; $z <= $endZ; ++ $z){ for($z = $startZ; $z <= $endZ; ++ $z){
for($x = $startX; $x <= $endX; ++ $x){ for($x = $startX; $x <= $endX; ++ $x){
$v = new Vector3($x, $y, $z); $v = new Vector3($x, $y, $z);
@ -519,7 +533,9 @@ class Entity extends Position
break; break;
} }
} }
if($this->class !== ENTITY_PLAYER){ }
if(!$this->isPlayer()){
$update = false; $update = false;
$drag = 0.2; $drag = 0.2;
if(Utils::in_range($this->speedX, - 0.01, 0.01)){ if(Utils::in_range($this->speedX, - 0.01, 0.01)){
@ -532,14 +548,119 @@ class Entity extends Position
$this->speedY = 0; $this->speedY = 0;
} }
if(($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)) && ($this->speedX != 0 || $this->speedY != 0 || $this->speedZ != 0)){ if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT)){
$blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ)); $aABB = $this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ);
foreach($blocks as $b){ $x0 = floor($aABB->minX);
$this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX); $x1 = ceil($aABB->maxX);
$this->speedY = $b->calculateYOffset($this->boundingBox, $this->speedY); $y0 = floor($aABB->minY);
$this->speedZ = $b->calculateZOffset($this->boundingBox, $this->speedZ); $y1 = round($aABB->maxY);
$z0 = floor($aABB->minZ);
$z1 = ceil($aABB->maxZ);
$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;
$waterDone = false;
for($x = $x0; $x < $x1; ++ $x){
for($y = $y0; $y < $y1; ++ $y){
for($z = $z0; $z < $z1; ++ $z){
$pos = new Vector3($x, $y, $z);
$b = $this->level->getBlock($pos);
switch($b->getID()) {
case WATER:
case STILL_WATER: // Drowing
if($this->fire > 0 and $this->inBlock($pos)){
$this->fire = 0;
$this->updateMetadata();
}
if($this->air <= 0){
$this->harm(2, "water");
$hasUpdate = true;
} elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and ! $waterDone){
$this->air -= 1;
$waterDone = true;
$this->updateMetadata();
$hasUpdate = true;
}
break;
case LAVA: // Lava damage
case STILL_LAVA:
if($this->inBlock($pos)){
$this->harm(5, "lava");
$this->fire = 300;
$this->updateMetadata();
$hasUpdate = true;
}
break;
case FIRE: // Fire block damage
if($this->inBlock($pos)){
$this->harm(1, "fire");
$this->fire = 300;
$this->updateMetadata();
$hasUpdate = true;
}
break;
case CACTUS: // Cactus damage
if($this->touchingBlock($pos)){
$this->harm(1, "cactus");
$hasUpdate = true;
}
break;
default:
if($this->inBlock($pos, 0.7) and $y == $y1 and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){
$this->harm(1, "suffocation"); // Suffocation
$hasUpdate = true;
} elseif($x == ($endX - 1) and $y == $y1 and $z == ($endZ - 1)){
$this->air = 300; // Breathing
}
break;
}
if($b != false && $b->boundingBox->intersectsWith($aABB) && $b->isSolid && $this->isMoving()){
$this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX);
$lastY = $this->speedY;
$this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY);
if($this->speedY == 0 && $lastY > 0){
$support = true;
}
$this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ);
} }
} }
}
}
}
for($z = $startZ; $z <= $endZ; ++ $z){
for($x = $startX; $x <= $endX; ++ $x){
$v = new Vector3($x, $y, $z);
$v1 = new Vector3($x, $yC, $z);
if($this->isSupport($v, $this->width)){
$b = $this->level->getBlock($v);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
} elseif($this->isSupport($v1, $this->width)){
$b = $this->level->getBlock($v1);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
}
}
if($support === true){
break;
}
}
if($this->speedX != 0){ if($this->speedX != 0){
$this->x += $this->speedX; $this->x += $this->speedX;
$this->speedX -= $this->speedX * $drag; $this->speedX -= $this->speedX * $drag;
@ -557,6 +678,7 @@ class Entity extends Position
$z = (int) ($this->z - 0.5); $z = (int) ($this->z - 0.5);
$lim = (int) floor($ny); $lim = (int) floor($ny);
for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){
if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){ if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){
$support = true; $support = true;
if($this->class === ENTITY_FALLING){ if($this->class === ENTITY_FALLING){
@ -580,7 +702,7 @@ class Entity extends Position
$this->speedY -= $this->speedY * $drag; $this->speedY -= $this->speedY * $drag;
$update = true; $update = true;
} }
$this->onGround = $support;
if($this->hasGravity && $support === false){ if($this->hasGravity && $support === false){
$this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity $this->speedY -= ($this->class === ENTITY_FALLING) ? 0.04 : ($this->class === ENTITY_ITEM ? 0.06 : 0.08); // TODO: replace with $gravity
$update = true; $update = true;
@ -589,9 +711,6 @@ class Entity extends Position
// $this->speedY = 0; // $this->speedY = 0;
// $this->speedZ = 0; // $this->speedZ = 0;
$this->server->api->handle("entity.move", $this); $this->server->api->handle("entity.move", $this);
if(($this->class === ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $this->speedY <= 0.1){
$this->server->api->handle("entity.motion", $this);
}
$update = true; $update = true;
} }
@ -1391,6 +1510,13 @@ class Entity extends Position
return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})"; return "Entity(x={$this->x},y={$this->y},z={$this->z},level=" . $this->level->getName() . ",class={$this->class},type={$this->type})";
} }
/**
* Debug
*/
public function printSpeed(){
console("{$this->speedX}:{$this->speedY}:{$this->speedZ}");
}
/* /*
* Deprecated methods. * Deprecated methods.
* Those methods were left only for compability with older plugins * Those methods were left only for compability with older plugins

View file

@ -76,7 +76,7 @@ class WaterBlock extends LiquidBlock{
//Has Source or Its Source //Has Source or Its Source
if($from !== null || $level == 0x00){ if($from !== null || $level == 0x00){
if($down instanceof AirBlock || $down instanceof WaterBlock){ if($down instanceof AirBlock || $down instanceof WaterBlock){
$this->level->setBlock($down, new WaterBlock(0x01), false, false, true); $this->level->setBlock($down, new WaterBlock(0x00), false, false, true);
ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($down, 0, 0, $this->level), 5, BLOCK_UPDATE_NORMAL); ServerAPI::request()->api->block->scheduleBlockUpdate(new Position($down, 0, 0, $this->level), 5, BLOCK_UPDATE_NORMAL);
}elseif($level !== 0x07){{ }elseif($level !== 0x07){{
for($side = 2; $side <= 5; ++$side){ for($side = 2; $side <= 5; ++$side){

View file

@ -89,9 +89,11 @@ class Level{
if($extra !== false){ if($extra !== false){
$entities = []; $entities = [];
foreach($this->server->api->entity->getAll($this) as $entity){ foreach($this->entityList as $entity){
if($entity instanceof Entity){
$entities[] = $entity->createSaveData(); $entities[] = $entity->createSaveData();
} }
}
$this->entities->setAll($entities); $this->entities->setAll($entities);
$this->entities->save(); $this->entities->save();
$tiles = []; $tiles = [];