From 2927fe1094c13548c0f47f02492cd312b8376cac Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 16 Feb 2024 06:36:21 +0300 Subject: [PATCH] micro entity optimizations, fixed getEyeHeight, rewrote inWall, static hitboxes --- src/API/BlockAPI.php | 1 - src/entity/Entity.php | 52 +++++++++++++++++++++-------------- src/entity/Living.php | 23 +--------------- src/entity/animal/Chicken.php | 2 +- src/entity/animal/Cow.php | 2 +- src/entity/animal/Pig.php | 2 +- src/entity/animal/Sheep.php | 2 +- src/pmf/PMFLevel.php | 36 +++++++++--------------- src/world/Level.php | 28 +++---------------- 9 files changed, 54 insertions(+), 94 deletions(-) diff --git a/src/API/BlockAPI.php b/src/API/BlockAPI.php index 19532d58..28b99147 100755 --- a/src/API/BlockAPI.php +++ b/src/API/BlockAPI.php @@ -540,7 +540,6 @@ class BlockAPI{ $level = $block->onUpdate($type); if($level === BLOCK_UPDATE_NORMAL){ $this->blockUpdateAround($block, $level); - $this->server->api->entity->updateRadius($pos, 1); } return $level; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0d11f209..c90a8e35 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -213,6 +213,27 @@ class Entity extends Position $this->speedZ += $vZ; } + public function applyCollision(Entity $collided){ + if(!($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){ + $diffX = $collided->x - $this->x; + $diffZ = $collided->z - $this->z; + $maxDiff = max(abs($diffX), abs($diffZ)); + if($maxDiff > 0.01){ + $sqrtMax = sqrt($maxDiff); + $diffX /= $sqrtMax; + $diffZ /= $sqrtMax; + + $col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v); + $diffX *= $col; + $diffZ *= $col; + $diffX *= 0.05; + $diffZ *= 0.05; + $this->addVelocity(-$diffX, 0, -$diffZ); + $collided->addVelocity($diffX, 0, $diffZ); + } + } + } + public function isMovingHorizontally() { return ($this->speedX >= self::MIN_POSSIBLE_SPEED || $this->speedX <= -self::MIN_POSSIBLE_SPEED) || ($this->speedZ >= self::MIN_POSSIBLE_SPEED || $this->speedZ <= -self::MIN_POSSIBLE_SPEED); @@ -361,7 +382,7 @@ class Entity extends Position $tickDiff = ($time - $this->lastUpdate) / 0.05; - if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $this->player->blocked !== true && ! $this->dead){ + if($this->isPlayer() && $this->player->spawned === true && $this->player->blocked !== true && !$this->dead){ $myBB = $this->boundingBox->grow(1, 0.5, 1); foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){ if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ @@ -438,21 +459,12 @@ class Entity extends Position $endY = ceil($this->boundingBox->maxY); $endZ = ceil($this->boundingBox->maxZ); - if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix - for($i = 0; $i < 8; ++$i){ - $x = ((($i >> 0) % 2) - 0.5) * $this->width * 0.8; - $y = ((($i >> 1) % 2) - 0.5) * 0.1; - $z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; - - $blockX = (int) ($this->x + $x); - $blockY = (int) ($this->y + $this->getEyeHeight() + $y); - $blockZ = (int) ($this->z + $z); - - if(!StaticBlock::getIsTransparent($this->level->level->getBlockID($blockX, $blockY, $blockZ))){ - $this->harm(1, "suffocation"); // Suffocation - $hasUpdate = true; - break; - } + if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix + $x = floor($this->x); + $y = floor($this->y + $this->getEyeHeight()); + $z = floor($this->z); + if(!StaticBlock::getIsTransparent($this->level->level->getBlockID($x, $y, $z))){ + $this->harm(1, "suffocation"); } } @@ -761,7 +773,7 @@ class Entity extends Position $this->updateEntityMovement(); $update = false; if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ - $this->server->api->handle("entity.move", $this); + //$this->server->api->handle("entity.move", $this); $update = true; }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { $update = true; @@ -771,7 +783,7 @@ class Entity extends Position $hasUpdate = true; } - } elseif($this->player instanceof Player){ + } else{ $prevGroundState = $this->onGround; $this->onGround = false; $this->speedX = -($this->lastX - $this->x); @@ -873,7 +885,7 @@ class Entity extends Position } $now = microtime(true); if($this->isStatic === false and ($this->lastX != $this->x or $this->lastY != $this->y or $this->lastZ != $this->z or $this->lastYaw != $this->yaw or $this->lastPitch != $this->pitch or $this->lastHeadYaw != $this->headYaw)){ - if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){ + if($this->class === ENTITY_PLAYER){ if($this->server->api->handle("entity.move", $this) === false){ if($this->class === ENTITY_PLAYER){ $this->player->teleport(new Vector3($this->last[0], $this->last[1], $this->last[2]), $this->last[3], $this->last[4]); @@ -912,7 +924,7 @@ class Entity extends Position } public function getEyeHeight(){ //TODO in vanilla player's eyeHeight is 0.12 - return $this->isPlayer() ? $this->height - 0.12 : $this->width * 0.85; + return $this->isPlayer() ? $this->height - 0.12 : $this->height * 0.85; } public function interactWith(Entity $e, $action) diff --git a/src/entity/Living.php b/src/entity/Living.php index 826a55c7..3ce58603 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -76,7 +76,7 @@ abstract class Living extends Entity implements Pathfindable{ foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ if($this->level->entityList[$entid] instanceof Entity){ if($bb->intersectsWith($this->level->entityList[$entid]->boundingBox)){ - $this->applyCollision($this->level->entityList[$entid]); + $this->level->entityList[$entid]->applyCollision($this); } } } @@ -84,27 +84,6 @@ abstract class Living extends Entity implements Pathfindable{ } } - public function applyCollision(Entity $collided){ - if(!($this->isPlayer() && $collided->isPlayer()) && $this->eid != $collided->eid){ - $diffX = $collided->x - $this->x; - $diffZ = $collided->z - $this->z; - $maxDiff = max(abs($diffX), abs($diffZ)); - if($maxDiff > 0.01){ - $sqrtMax = sqrt($maxDiff); - $diffX /= $sqrtMax; - $diffZ /= $sqrtMax; - - $col = (($v = 1 / $sqrtMax) > 1 ? 1 : $v); - $diffX *= $col; - $diffZ *= $col; - $diffX *= 0.05; - $diffZ *= 0.05; - $this->addVelocity(-$diffX, 0, -$diffZ); - $collided->addVelocity($diffX, 0, $diffZ); - } - } - } - public function update($now){ if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){ $this->close(); diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 0cc62f63..278d5b1b 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -5,7 +5,7 @@ class Chicken extends Animal{ public $timeUntilEgg; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->timeUntilEgg = mt_rand(0,6000) + 6000; - $this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7); + $this->setSize(0.4, 0.7); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setName('Chicken'); diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index fdcd4779..a3a9fb1c 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -3,7 +3,7 @@ class Cow extends Animal{ const TYPE = MOB_COW; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ - $this->setSize($this->isBaby() ? 0.45 : 0.9 , $this->isBaby() ? 0.7 : 1.4); + $this->setSize(0.9, 1.3); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Cow"); diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index f307c523..3f6ba532 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -3,7 +3,7 @@ class Pig extends Animal implements Rideable{ const TYPE = MOB_PIG; public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ - $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9); + $this->setSize(0.9, 0.9); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); $this->server = ServerAPI::request(); diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 3108d954..0a9d2812 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -5,7 +5,7 @@ class Sheep extends Animal{ const TYPE = MOB_SHEEP; function __construct(Level $level, $eid, $class, $type = 0, $data = []){ - $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3); + $this->setSize(0.9, 1.3); parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setName("Sheep"); diff --git a/src/pmf/PMFLevel.php b/src/pmf/PMFLevel.php index 9c6a22d5..859af005 100644 --- a/src/pmf/PMFLevel.php +++ b/src/pmf/PMFLevel.php @@ -355,13 +355,12 @@ class PMFLevel extends PMF{ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return false; } + $X = $x >> 4; $Z = $z >> 4; $Y = $y >> 4; $block &= 0xFF; - if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){ - return false; - } + $index = self::getIndex($X, $Z); $aX = $x & 0xf; $aZ = $z & 0xf; @@ -391,7 +390,7 @@ class PMFLevel extends PMF{ $aZ = $z & 0xf; $aY = $y & 0xf; $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); - return ($y & 1) === 0 ? $m & 0x0F : $m >> 4; + return $y & 1 ? $m >> 4 : $m & 0x0F; } public function setBlockDamage($x, $y, $z, $damage){ @@ -402,9 +401,7 @@ class PMFLevel extends PMF{ $Z = $z >> 4; $Y = $y >> 4; $damage &= 0x0F; - if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){ - return false; - } + $index = self::getIndex($X, $Z); $aX = $x & 0xf; $aZ = $z & 0xf; @@ -431,13 +428,14 @@ class PMFLevel extends PMF{ } public function getBlock($x, $y, $z){ - $X = $x >> 4; - $Z = $z >> 4; - $Y = $y >> 4; if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ return [AIR, 0]; } + $X = $x >> 4; + $Z = $z >> 4; + $Y = $y >> 4; + $index = self::getIndex($X, $Z); if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ if($this->loadChunk($X, $Z) === false){ @@ -450,17 +448,12 @@ class PMFLevel extends PMF{ $aX = $x & 0xf; $aZ = $z & 0xf; $aY = $y & 0xf; - #Need to fix. But idk how. - //if(is_string($this->chunks[$index][$Y])){ //PHP8 warn fix $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]); + $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); - - if(($y & 1) === 0){ - $m = $m & 0x0F; - }else{ - $m = $m >> 4; - } + $m = ($y & 1) ? $m >> 4 : $m & 0xf; + return [$b, $m]; } @@ -488,11 +481,8 @@ class PMFLevel extends PMF{ $mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9)); $old_b = ord($this->chunks[$index][$Y][$bindex]); $old_m = ord($this->chunks[$index][$Y][$mindex]); - if(($y & 1) === 0){ - $m = ($old_m & 0xF0) | $meta; - }else{ - $m = ($meta << 4) | ($old_m & 0x0F); - } + + $m = ($y & 1) ? (($meta << 4) | ($old_m & 0x0F)) : (($old_m & 0xF0) | $meta); if($old_b !== $block or $old_m !== $m){ $this->chunks[$index][$Y][$bindex] = chr($block); diff --git a/src/world/Level.php b/src/world/Level.php index 6b8fa9f3..31016f53 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -401,29 +401,11 @@ class Level{ } $this->totalMobsAmount = 0; $post = []; - //console("ELP {$this->getName()}"); - //var_dump($this->entityListPositioned); + //console($this->getName()); foreach($this->entityList as $k => $e){ - - if(!isset(CORRECT_ENTITY_CLASSES[$e->class])){ - ConsoleAPI::warn("Entity $k has invalid class type! {$e->class}"); - $e->close(); - unset($this->entityList[$k]); - unset($this->server->entities[$k]); - - $curChunkX = (int)$e->x >> 4; - $curChunkZ = (int)$e->z >> 4; - $index = "$curChunkX $curChunkZ"; - - if(isset($this->entityListPositioned[$index]) && isset($this->entityListPositioned[$index][$k])){ - unset($this->entityListPositioned[$index][$k]); - } - - continue; - } - $dd = CORRECT_ENTITY_CLASSES[$e->class]; - if($dd !== true && !isset($dd[$e->type])){ - ConsoleAPI::warn("Entity $k has invalid entity type! {$e->class} {$e->type}"); + $dd = CORRECT_ENTITY_CLASSES[$e->class] ?? false; + if($dd === false || ($dd !== true && !isset($dd[$e->type]))){ + ConsoleAPI::warn("Entity $k has invalid entity! {$e->class} {$e->type}"); $e->close(); unset($this->entityList[$k]); @@ -440,8 +422,6 @@ class Level{ continue; } - - if(!($e instanceof Entity)){ unset($this->entityList[$k]); unset($this->server->entities[$k]);