From 2d9b37aa9c85d8bf0eb82e56dea0982af93db4cd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 28 Jul 2023 02:50:23 +0300 Subject: [PATCH] more entity optimizations --- src/API/ConsoleAPI.php | 11 +++--- src/API/EntityAPI.php | 6 +-- src/PocketMinecraftServer.php | 2 +- src/entity/Animal.php | 4 +- src/entity/Entity.php | 37 ++++++++----------- src/entity/Living.php | 6 ++- src/entity/ai/tasks/TaskLookAround.php | 2 +- src/entity/ai/tasks/TaskLookAtPlayer.php | 2 +- src/entity/ai/tasks/TaskSwimming.php | 2 +- src/entity/animal/Sheep.php | 16 -------- .../block/misc/NetherReactorBlock.php | 16 ++++---- src/material/block/solid/GrassBlock.php | 2 +- 12 files changed, 44 insertions(+), 62 deletions(-) diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 18a036d6..e24a64f3 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -211,11 +211,11 @@ class ConsoleAPI{ break; case "status": 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(); - $output .= "TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . ")\n"; - break; + return "TPS: " . $info["tps"] . ", Memory usage: " . $info["memory_usage"] . " (Peak " . $info["memory_peak_usage"] . ")"; case "stop": $this->loop->stop = true; $output .= "Stopping the server\n"; @@ -228,8 +228,7 @@ class ConsoleAPI{ break; } $this->server->api->setProperty("difficulty", (int) $s); - $output .= "Difficulty changed to " . $this->server->difficulty . "\n"; - break; + return "Difficulty changed to " . $this->server->difficulty . "\n"; case "?": if($issuer !== "console" and $issuer !== "rcon"){ break; diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 58ed97e1..c17a5d87 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -191,7 +191,7 @@ class EntityAPI{ } 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){ @@ -254,9 +254,9 @@ class EntityAPI{ "y" => $pos->y + 0.19, "z" => $pos->z + mt_rand(-10, 10) / 50, "level" => $pos->level, - "speedX" => Utils::randomFloat() * 0.2 - 0.1, + "speedX" => lcg_value() * 0.2 - 0.1, "speedY" => 0.2, - "speedZ" => Utils::randomFloat() * 0.2 - 0.1, + "speedZ" => lcg_value() * 0.2 - 0.1, "item" => $item, ]; if($this->server->api->handle("item.drop", $data) !== false){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 518bed5c..6a7cf432 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -730,7 +730,7 @@ class PocketMinecraftServer{ $info["garbage"] = gc_collect_cycles(); $this->handle("server.debug", $info); 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; } diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 02dcdf7a..bc803c8f 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -60,9 +60,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{ public function spawnChild() { 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, - "z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1), + "z" => $this->z + lcg_value() * mt_rand(-1, 1), "IsBaby" => true, "Age" => -24000, ]); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 72760df7..54341b80 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -423,14 +423,13 @@ class Entity extends Position for ($y = $startY; $y <= $endY; ++$y){ for ($x = $startX; $x <= $endX; ++$x){ for ($z = $startZ; $z <= $endZ; ++$z){ - $pos = new Vector3($x, $y, $z); $b = $this->level->level->getBlock($x, $y, $z); $id = $b[0]; $meta = $b[1]; switch ($id) { case WATER: 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->updateMetadata(); } @@ -447,7 +446,7 @@ class Entity extends Position break; case LAVA: // Lava damage case STILL_LAVA: - if ($this->inBlock($pos)) { + if ($this->inBlockNonVector($x, $y, $z)) { $this->harm(5, "lava"); $this->fire = 300; $this->updateMetadata(); @@ -455,7 +454,7 @@ class Entity extends Position } break; case FIRE: // Fire block damage - if ($this->inBlock($pos)) { + if ($this->inBlockNonVector($x, $y, $z)) { $this->harm(1, "fire"); $this->fire = 300; $this->updateMetadata(); @@ -463,14 +462,15 @@ class Entity extends Position } break; case CACTUS: // Cactus damage - if ($this->touchingBlock($pos)) { + if ($this->inBlockNonVector($x, $y, $z)) { $this->harm(1, "cactus"); $hasUpdate = true; } break; 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)){} - elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){ + 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)){ $this->air = 200; } 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->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) { $me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5); @@ -1154,15 +1157,7 @@ class Entity extends Position public function resetSpeed() { - $this->speedMeasure = array( - 0, - 0, - 0, - 0, - 0, - 0, - 0 - ); + $this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0); } 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) { $d = $entity->x - $this->x; - for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) { - $d = (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 = (lcg_value() - lcg_value()) * 0.01; } // attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) > $this->knockBack($d, $d1); @@ -1469,9 +1464,9 @@ class Entity extends Position $f1 = 0.4; $this->speedX /= 2; $this->speedZ /= 2; - $this->speedX -= ($d / (double) $f) * (double) $f1; + $this->speedX -= ($d / $f) * $f1; $this->speedY += 0.40000000596046448; - $this->speedZ -= ($d1 / (double) $f) * (double) $f1; + $this->speedZ -= ($d1 / $f) * $f1; if($this->speedY > 0.40000000596046448){ $this->speedY = 0.40000000596046448; } diff --git a/src/entity/Living.php b/src/entity/Living.php index d02d38f4..09414412 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -6,6 +6,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ public $target, $ai; public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower; + public $ticksExisted = 0; public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; $this->ai = new EntityAI($this); @@ -15,7 +16,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ $this->canBeAttacked = true; $this->hasGravity = 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(){ $dmg = floor($this->fallDistance - 3); @@ -62,6 +63,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ } public function update(){ + if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){ + $this->close(); + } if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { $this->ai->updateTasks(); } diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index 2a010ddc..af13cff6 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase 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) diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index 17fb49b0..cf9c624c 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; private $state, $yaw, $pitch; 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){ diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index 3ece25c4..159c7ff3 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -20,7 +20,7 @@ class TaskSwimming extends TaskBase return; } - if(Utils::randomFloat() < 0.8){ #1.5.2 method + if(lcg_value() < 0.8){ #1.5.2 method $ai->mobController->setJumping(true); } diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index a088d644..93444bfa 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -12,11 +12,6 @@ class Sheep extends Animal{ $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->setSpeed(0.25); $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()); } public function createSaveData(){ @@ -27,17 +22,6 @@ class Sheep extends Animal{ } 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); if($this->isBaby()){ $age = $this->getAge() + 1200; diff --git a/src/material/block/misc/NetherReactorBlock.php b/src/material/block/misc/NetherReactorBlock.php index 72972c97..1abe5482 100644 --- a/src/material/block/misc/NetherReactorBlock.php +++ b/src/material/block/misc/NetherReactorBlock.php @@ -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($b = $bOne; $b < $bTwo; $b += $bThree) { 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->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]; $server = ServerAPI::request(); if(!$forceAmount){ - $spawnNumber = $minAmount + floor(Utils::randomFloat()*($maxAmount-$minAmount+1)); + $spawnNumber = $minAmount + floor(lcg_value()*($maxAmount-$minAmount+1)); } else{ $spawnNumber = $maxAmount; } for($i = 0; $i < $spawnNumber; $i++) { - $randomRange = floor(Utils::randomFloat()*5+3); - $shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); - $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); + $randomRange = floor(lcg_value()*5+3); + $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); + $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); if(Utils::chance(5)) $randomID = $this->rarePossibleLoot[array_rand($this->rarePossibleLoot)]; 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)); } for($i = 0; $i < $pigmen; $i++) { - $randomRange = floor(Utils::randomFloat()*5+3); - $shiftX = cos(floor(Utils::randomFloat()*360)*(pi()/180)); - $shiftZ = sin(floor(Utils::randomFloat()*360)*(pi()/180)); + $randomRange = floor(lcg_value()*5+3); + $shiftX = cos(floor(lcg_value()*360)*(pi()/180)); + $shiftZ = sin(floor(lcg_value()*360)*(pi()/180)); $data = array( "x" => $x+($shiftX*$randomRange)+0.5, "y" => $y, diff --git a/src/material/block/solid/GrassBlock.php b/src/material/block/solid/GrassBlock.php index 4d4a8f22..59501f5e 100644 --- a/src/material/block/solid/GrassBlock.php +++ b/src/material/block/solid/GrassBlock.php @@ -35,7 +35,7 @@ class GrassBlock extends SolidBlock{ } public function seedsDrop(){ - $chance = Utils::randomFloat() * 100; + $chance = lcg_value() * 100; 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)); return;