more entity optimizations

This commit is contained in:
GameHerobrine 2023-07-28 02:50:23 +03:00
parent 2b0ad36e14
commit 2d9b37aa9c
12 changed files with 44 additions and 62 deletions

View file

@ -211,11 +211,11 @@ class ConsoleAPI{
break; break;
case "status": case "status":
if(!($issuer instanceof Player) and $issuer === "console"){ if(!($issuer instanceof Player) and $issuer === "console"){
$this->server->debugInfo(true); $info = $this->server->debugInfo(true);
}else{
$info = $this->server->debugInfo();
} }
$info = $this->server->debugInfo(); return "TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . ")";
$output .= "TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . ")\n";
break;
case "stop": case "stop":
$this->loop->stop = true; $this->loop->stop = true;
$output .= "Stopping the server\n"; $output .= "Stopping the server\n";
@ -228,8 +228,7 @@ class ConsoleAPI{
break; break;
} }
$this->server->api->setProperty("difficulty", (int) $s); $this->server->api->setProperty("difficulty", (int) $s);
$output .= "Difficulty changed to " . $this->server->difficulty . "\n"; return "Difficulty changed to " . $this->server->difficulty . "\n";
break;
case "?": case "?":
if($issuer !== "console" and $issuer !== "rcon"){ if($issuer !== "console" and $issuer !== "rcon"){
break; break;

View file

@ -191,7 +191,7 @@ class EntityAPI{
} }
public function updateRadius(Position $center, $radius = 15, $class = false){ public function updateRadius(Position $center, $radius = 15, $class = false){
$this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;"); //useless now? $this->server->query("UPDATE entities SET hasUpdate = 1 WHERE level = '" . $center->level->getName() . "' " . ($class !== false ? "AND class = $class " : "") . "AND abs(x - {$center->x}) <= $radius AND abs(y - {$center->y}) <= $radius AND abs(z - {$center->z}) <= $radius;");
} }
public function getRadius(Position $center, $radius = 15, $class = false){ public function getRadius(Position $center, $radius = 15, $class = false){
@ -254,9 +254,9 @@ class EntityAPI{
"y" => $pos->y + 0.19, "y" => $pos->y + 0.19,
"z" => $pos->z + mt_rand(-10, 10) / 50, "z" => $pos->z + mt_rand(-10, 10) / 50,
"level" => $pos->level, "level" => $pos->level,
"speedX" => Utils::randomFloat() * 0.2 - 0.1, "speedX" => lcg_value() * 0.2 - 0.1,
"speedY" => 0.2, "speedY" => 0.2,
"speedZ" => Utils::randomFloat() * 0.2 - 0.1, "speedZ" => lcg_value() * 0.2 - 0.1,
"item" => $item, "item" => $item,
]; ];
if($this->server->api->handle("item.drop", $data) !== false){ if($this->server->api->handle("item.drop", $data) !== false){

View file

@ -730,7 +730,7 @@ class PocketMinecraftServer{
$info["garbage"] = gc_collect_cycles(); $info["garbage"] = gc_collect_cycles();
$this->handle("server.debug", $info); $this->handle("server.debug", $info);
if($console === true){ if($console === true){
console("[DEBUG] TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . "), Entities: " . $info["entities"] . ", Events: " . $info["events"] . ", Handlers: " . $info["handlers"] . ", Actions: " . $info["actions"] . ", Garbage: " . $info["garbage"], true, true, 2); console("[INFO] TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . "), Entities: " . $info["entities"] . ", Events: " . $info["events"] . ", Handlers: " . $info["handlers"] . ", Actions: " . $info["actions"] . ", Garbage: " . $info["garbage"], true, true);
} }
return $info; return $info;
} }

View file

@ -60,9 +60,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{
public function spawnChild() public function spawnChild()
{ {
return $this->server->api->entity->add($this->level, $this->class, $this->type, [ return $this->server->api->entity->add($this->level, $this->class, $this->type, [
"x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1), "x" => $this->x + lcg_value() * mt_rand(-1, 1),
"y" => $this->y, "y" => $this->y,
"z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1), "z" => $this->z + lcg_value() * mt_rand(-1, 1),
"IsBaby" => true, "IsBaby" => true,
"Age" => -24000, "Age" => -24000,
]); ]);

View file

@ -423,14 +423,13 @@ class Entity extends Position
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){
$pos = new Vector3($x, $y, $z);
$b = $this->level->level->getBlock($x, $y, $z); $b = $this->level->level->getBlock($x, $y, $z);
$id = $b[0]; $id = $b[0];
$meta = $b[1]; $meta = $b[1];
switch ($id) { switch ($id) {
case WATER: case WATER:
case STILL_WATER: // Drowing case STILL_WATER: // Drowing
if ($this->fire > 0 and $this->inBlock($pos)) { if ($this->fire > 0 and $this->inBlockNonVector($x, $y, $z)) {
$this->fire = 0; $this->fire = 0;
$this->updateMetadata(); $this->updateMetadata();
} }
@ -447,7 +446,7 @@ class Entity extends Position
break; break;
case LAVA: // Lava damage case LAVA: // Lava damage
case STILL_LAVA: case STILL_LAVA:
if ($this->inBlock($pos)) { if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(5, "lava"); $this->harm(5, "lava");
$this->fire = 300; $this->fire = 300;
$this->updateMetadata(); $this->updateMetadata();
@ -455,7 +454,7 @@ class Entity extends Position
} }
break; break;
case FIRE: // Fire block damage case FIRE: // Fire block damage
if ($this->inBlock($pos)) { if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(1, "fire"); $this->harm(1, "fire");
$this->fire = 300; $this->fire = 300;
$this->updateMetadata(); $this->updateMetadata();
@ -463,14 +462,15 @@ class Entity extends Position
} }
break; break;
case CACTUS: // Cactus damage case CACTUS: // Cactus damage
if ($this->touchingBlock($pos)) { if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(1, "cactus"); $this->harm(1, "cactus");
$hasUpdate = true; $hasUpdate = true;
} }
break; break;
default: default:
if($this->inBlock(new Vector3($x, $y, $z), 0.7) and $y == $endY and !StaticBlock::getIsTransparent($id) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){} if($this->inBlockNonVector($x, $y, $z, 0.7) && $y == $endY and !StaticBlock::getIsTransparent($id) && ($this->class === ENTITY_MOB || $this->class === ENTITY_PLAYER)){
elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){
}elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){
$this->air = 200; $this->air = 200;
} }
break; break;
@ -1123,7 +1123,10 @@ class Entity extends Position
$this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER); $this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER);
$this->server->preparedSQL->entity->setPosition->execute(); $this->server->preparedSQL->entity->setPosition->execute();
} }
public function inBlockNonVector($x, $y, $z, $radius = 0.8)
{
return $y == ceil($this->y) || $y == (ceil($this->y)+1) && max(abs($x - ($this->x-0.5)), abs($z - ($this->z-0.5)));
}
public function inBlock(Vector3 $block, $radius = 0.8) public function inBlock(Vector3 $block, $radius = 0.8)
{ {
$me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5);
@ -1154,15 +1157,7 @@ class Entity extends Position
public function resetSpeed() public function resetSpeed()
{ {
$this->speedMeasure = array( $this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0);
0,
0,
0,
0,
0,
0,
0
);
} }
public function getSpeed() public function getSpeed()
@ -1256,8 +1251,8 @@ class Entity extends Position
if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) { if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) {
$d = $entity->x - $this->x; $d = $entity->x - $this->x;
for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) { for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (lcg_value() - lcg_value()) * 0.01) {
$d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; $d = (lcg_value() - lcg_value()) * 0.01;
} }
// attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) > // attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) >
$this->knockBack($d, $d1); $this->knockBack($d, $d1);
@ -1469,9 +1464,9 @@ class Entity extends Position
$f1 = 0.4; $f1 = 0.4;
$this->speedX /= 2; $this->speedX /= 2;
$this->speedZ /= 2; $this->speedZ /= 2;
$this->speedX -= ($d / (double) $f) * (double) $f1; $this->speedX -= ($d / $f) * $f1;
$this->speedY += 0.40000000596046448; $this->speedY += 0.40000000596046448;
$this->speedZ -= ($d1 / (double) $f) * (double) $f1; $this->speedZ -= ($d1 / $f) * $f1;
if($this->speedY > 0.40000000596046448){ if($this->speedY > 0.40000000596046448){
$this->speedY = 0.40000000596046448; $this->speedY = 0.40000000596046448;
} }

View file

@ -6,6 +6,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
public $target, $ai; public $target, $ai;
public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower; public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower;
public $ticksExisted = 0;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false; $this->target = false;
$this->ai = new EntityAI($this); $this->ai = new EntityAI($this);
@ -15,7 +16,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->canBeAttacked = true; $this->canBeAttacked = true;
$this->hasGravity = true; $this->hasGravity = true;
$this->hasKnockback = true; $this->hasKnockback = true;
if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20 //if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20
} }
public function fall(){ public function fall(){
$dmg = floor($this->fallDistance - 3); $dmg = floor($this->fallDistance - 3);
@ -62,6 +63,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
} }
public function update(){ public function update(){
if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){
$this->close();
}
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks(); $this->ai->updateTasks();
} }

View file

@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)
{ {
return !$ai->entity->inPanic && !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
} }
public function onUpdate(EntityAI $ai) public function onUpdate(EntityAI $ai)

View file

@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false; public $target = false;
private $state, $yaw, $pitch; private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){ public function canBeExecuted(EntityAI $ai){
return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
} }
protected function findTarget($e, $r){ protected function findTarget($e, $r){

View file

@ -20,7 +20,7 @@ class TaskSwimming extends TaskBase
return; return;
} }
if(Utils::randomFloat() < 0.8){ #1.5.2 method if(lcg_value() < 0.8){ #1.5.2 method
$ai->mobController->setJumping(true); $ai->mobController->setJumping(true);
} }

View file

@ -12,11 +12,6 @@ class Sheep extends Animal{
$this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor();
$this->setSpeed(0.25); $this->setSpeed(0.25);
$this->update(); $this->update();
/*if($this->isSheared() and Entity::$updateOnTick){ //if ai enabled
$this->server->schedule(mt_rand(60, 400), [$this, "eatGrass"], null, true); //unknown time
}elseif($this->isBaby() and Entity::$updateOnTick){
$this->server->schedule(mt_rand(400, 1000), [$this, "eatGrass"], null, true); //unknown time
}*/
$this->ai->addTask(new TaskEatTileGoal()); $this->ai->addTask(new TaskEatTileGoal());
} }
public function createSaveData(){ public function createSaveData(){
@ -27,17 +22,6 @@ class Sheep extends Animal{
} }
public function eatGrass(){ public function eatGrass(){
/*$downBlock = $this->level->getBlock(new Vector3($this->x, $this->y-1, $this->z));
if($downBlock->getID() !== GRASS){
//console("eww i don't wanna eat this $downBlock");
return false;
}
$pk = new EntityEventPacket;
$pk->eid = $this->eid;
$pk->event = EntityEventPacket::ENTITY_ANIM_10;
$this->server->api->player->broadcastPacket($this->level->players, $pk);
$this->server->schedule(38, [$this, "setSheared"], false);
$this->level->setblock(new Position($this->x, $this->y-1, $this->z, $this->level), new DirtBlock());*/
$this->setSheared(0); $this->setSheared(0);
if($this->isBaby()){ if($this->isBaby()){
$age = $this->getAge() + 1200; $age = $this->getAge() + 1200;

View file

@ -61,7 +61,7 @@ class NetherReactorBlock extends SolidBlock{
for($a = $aOne; $a < $aTwo; $a += $aThree) { //wth those cycles are? TODO simplify if possible(makes server lag) for($a = $aOne; $a < $aTwo; $a += $aThree) { //wth those cycles are? TODO simplify if possible(makes server lag)
for($b = $bOne; $b < $bTwo; $b += $bThree) { for($b = $bOne; $b < $bTwo; $b += $bThree) {
for($c = $cOne; $c < $cTwo; $c += $cThree) { for($c = $cOne; $c < $cTwo; $c += $cThree) {
if ($this->level->level->getBlockID($x+$a, $y+$b, $z+$c) === NETHERRACK && Utils::randomFloat() > 0.75){ if ($this->level->level->getBlockID($x+$a, $y+$b, $z+$c) === NETHERRACK && lcg_value() > 0.75){
//$this->level->setBlock(new Vector3($x+$a, $y+$b, $z+$c), new AirBlock()); //$this->level->setBlock(new Vector3($x+$a, $y+$b, $z+$c), new AirBlock());
$this->level->fastSetBlockUpdate($x+$a, $y+$b, $z+$c, 0, 0); //TODO might be better to set not directly? $this->level->fastSetBlockUpdate($x+$a, $y+$b, $z+$c, 0, 0); //TODO might be better to set not directly?
} }
@ -91,23 +91,23 @@ class NetherReactorBlock extends SolidBlock{
$forceAmount = $data[3]; $forceAmount = $data[3];
$server = ServerAPI::request(); $server = ServerAPI::request();
if(!$forceAmount){ if(!$forceAmount){
$spawnNumber = $minAmount + floor(Utils::randomFloat()*($maxAmount-$minAmount+1)); $spawnNumber = $minAmount + floor(lcg_value()*($maxAmount-$minAmount+1));
} }
else{ else{
$spawnNumber = $maxAmount; $spawnNumber = $maxAmount;
} }
for($i = 0; $i < $spawnNumber; $i++) { for($i = 0; $i < $spawnNumber; $i++) {
$randomRange = floor(Utils::randomFloat()*5+3); $randomRange = floor(lcg_value()*5+3);
$shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftX = cos(floor(lcg_value()*360)*(pi()/180));
$shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftZ = sin(floor(lcg_value()*360)*(pi()/180));
if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)]; if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)];
else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)]; else $randomID = $this->possibleLoot[array_rand($this->possibleLoot)];
$server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1)); $server->api->entity->drop(new Position($x+($shiftX*$randomRange)+0.5, $y, $z+($shiftZ*$randomRange)+0.5, $this->level), BlockAPI::getItem($randomID, 0, 1));
} }
for($i = 0; $i < $pigmen; $i++) { for($i = 0; $i < $pigmen; $i++) {
$randomRange = floor(Utils::randomFloat()*5+3); $randomRange = floor(lcg_value()*5+3);
$shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftX = cos(floor(lcg_value()*360)*(pi()/180));
$shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); $shiftZ = sin(floor(lcg_value()*360)*(pi()/180));
$data = array( $data = array(
"x" => $x+($shiftX*$randomRange)+0.5, "x" => $x+($shiftX*$randomRange)+0.5,
"y" => $y, "y" => $y,

View file

@ -35,7 +35,7 @@ class GrassBlock extends SolidBlock{
} }
public function seedsDrop(){ public function seedsDrop(){
$chance = Utils::randomFloat() * 100; $chance = lcg_value() * 100;
if($chance <= 1){ if($chance <= 1){
ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y+1, $this->z+0.5, $this->level), BlockAPI::getItem(458,0,1)); ServerAPI::request()->api->entity->drop(new Position($this->x+0.5, $this->y+1, $this->z+0.5, $this->level), BlockAPI::getItem(458,0,1));
return; return;