micro entity optimizations, fixed getEyeHeight, rewrote inWall, static hitboxes

This commit is contained in:
GameHerobrine 2024-02-16 06:36:21 +03:00
parent 0469550ca2
commit 2927fe1094
9 changed files with 54 additions and 94 deletions

View file

@ -540,7 +540,6 @@ class BlockAPI{
$level = $block->onUpdate($type); $level = $block->onUpdate($type);
if($level === BLOCK_UPDATE_NORMAL){ if($level === BLOCK_UPDATE_NORMAL){
$this->blockUpdateAround($block, $level); $this->blockUpdateAround($block, $level);
$this->server->api->entity->updateRadius($pos, 1);
} }
return $level; return $level;

View file

@ -213,6 +213,27 @@ class Entity extends Position
$this->speedZ += $vZ; $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() 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); 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; $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); $myBB = $this->boundingBox->grow(1, 0.5, 1);
foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){ foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){
if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){
@ -439,20 +460,11 @@ class Entity extends Position
$endZ = ceil($this->boundingBox->maxZ); $endZ = ceil($this->boundingBox->maxZ);
if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix if(!($this instanceof Painting) && !($this->isPlayer() && $this->player->isSleeping !== false)){ //TODO better way to fix
for($i = 0; $i < 8; ++$i){ $x = floor($this->x);
$x = ((($i >> 0) % 2) - 0.5) * $this->width * 0.8; $y = floor($this->y + $this->getEyeHeight());
$y = ((($i >> 1) % 2) - 0.5) * 0.1; $z = floor($this->z);
$z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; if(!StaticBlock::getIsTransparent($this->level->level->getBlockID($x, $y, $z))){
$this->harm(1, "suffocation");
$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;
}
} }
} }
@ -761,7 +773,7 @@ class Entity extends Position
$this->updateEntityMovement(); $this->updateEntityMovement();
$update = false; $update = false;
if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ 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; $update = true;
}elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) {
$update = true; $update = true;
@ -771,7 +783,7 @@ class Entity extends Position
$hasUpdate = true; $hasUpdate = true;
} }
} elseif($this->player instanceof Player){ } else{
$prevGroundState = $this->onGround; $prevGroundState = $this->onGround;
$this->onGround = false; $this->onGround = false;
$this->speedX = -($this->lastX - $this->x); $this->speedX = -($this->lastX - $this->x);
@ -873,7 +885,7 @@ class Entity extends Position
} }
$now = microtime(true); $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->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->server->api->handle("entity.move", $this) === false){
if($this->class === ENTITY_PLAYER){ 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]); $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 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) public function interactWith(Entity $e, $action)

View file

@ -76,7 +76,7 @@ abstract class Living extends Entity implements Pathfindable{
foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){
if($this->level->entityList[$entid] instanceof Entity){ if($this->level->entityList[$entid] instanceof Entity){
if($bb->intersectsWith($this->level->entityList[$entid]->boundingBox)){ 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){ public function update($now){
if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){ if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){
$this->close(); $this->close();

View file

@ -5,7 +5,7 @@ class Chicken extends Animal{
public $timeUntilEgg; public $timeUntilEgg;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){ function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->timeUntilEgg = mt_rand(0,6000) + 6000; $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); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken'); $this->setName('Chicken');

View file

@ -3,7 +3,7 @@
class Cow extends Animal{ class Cow extends Animal{
const TYPE = MOB_COW; const TYPE = MOB_COW;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){ 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); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
$this->setName("Cow"); $this->setName("Cow");

View file

@ -3,7 +3,7 @@ class Pig extends Animal implements Rideable{
const TYPE = MOB_PIG; const TYPE = MOB_PIG;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ 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); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();

View file

@ -5,7 +5,7 @@ class Sheep extends Animal{
const TYPE = MOB_SHEEP; const TYPE = MOB_SHEEP;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){ 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); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
$this->setName("Sheep"); $this->setName("Sheep");

View file

@ -355,13 +355,12 @@ class PMFLevel extends PMF{
if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
return false; return false;
} }
$X = $x >> 4; $X = $x >> 4;
$Z = $z >> 4; $Z = $z >> 4;
$Y = $y >> 4; $Y = $y >> 4;
$block &= 0xFF; $block &= 0xFF;
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
return false;
}
$index = self::getIndex($X, $Z); $index = self::getIndex($X, $Z);
$aX = $x & 0xf; $aX = $x & 0xf;
$aZ = $z & 0xf; $aZ = $z & 0xf;
@ -391,7 +390,7 @@ class PMFLevel extends PMF{
$aZ = $z & 0xf; $aZ = $z & 0xf;
$aY = $y & 0xf; $aY = $y & 0xf;
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); $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){ public function setBlockDamage($x, $y, $z, $damage){
@ -402,9 +401,7 @@ class PMFLevel extends PMF{
$Z = $z >> 4; $Z = $z >> 4;
$Y = $y >> 4; $Y = $y >> 4;
$damage &= 0x0F; $damage &= 0x0F;
if($X >= 32 or $Z >= 32 or $Y >= $this->levelData["height"] or $y < 0){
return false;
}
$index = self::getIndex($X, $Z); $index = self::getIndex($X, $Z);
$aX = $x & 0xf; $aX = $x & 0xf;
$aZ = $z & 0xf; $aZ = $z & 0xf;
@ -431,13 +428,14 @@ class PMFLevel extends PMF{
} }
public function getBlock($x, $y, $z){ 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){ if($x < 0 || $x > 255 || $z < 0 || $z > 255 || $y < 0 || $y > 127){
return [AIR, 0]; return [AIR, 0];
} }
$X = $x >> 4;
$Z = $z >> 4;
$Y = $y >> 4;
$index = self::getIndex($X, $Z); $index = self::getIndex($X, $Z);
if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){ if(!isset($this->chunks[$index]) || $this->chunks[$index] === false){
if($this->loadChunk($X, $Z) === false){ if($this->loadChunk($X, $Z) === false){
@ -450,17 +448,12 @@ class PMFLevel extends PMF{
$aX = $x & 0xf; $aX = $x & 0xf;
$aZ = $z & 0xf; $aZ = $z & 0xf;
$aY = $y & 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))]); $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 = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]);
$m = $m & 0x0F; $m = ($y & 1) ? $m >> 4 : $m & 0xf;
}else{
$m = $m >> 4;
}
return [$b, $m]; return [$b, $m];
} }
@ -488,11 +481,8 @@ class PMFLevel extends PMF{
$mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9)); $mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9));
$old_b = ord($this->chunks[$index][$Y][$bindex]); $old_b = ord($this->chunks[$index][$Y][$bindex]);
$old_m = ord($this->chunks[$index][$Y][$mindex]); $old_m = ord($this->chunks[$index][$Y][$mindex]);
if(($y & 1) === 0){
$m = ($old_m & 0xF0) | $meta; $m = ($y & 1) ? (($meta << 4) | ($old_m & 0x0F)) : (($old_m & 0xF0) | $meta);
}else{
$m = ($meta << 4) | ($old_m & 0x0F);
}
if($old_b !== $block or $old_m !== $m){ if($old_b !== $block or $old_m !== $m){
$this->chunks[$index][$Y][$bindex] = chr($block); $this->chunks[$index][$Y][$bindex] = chr($block);

View file

@ -401,29 +401,11 @@ class Level{
} }
$this->totalMobsAmount = 0; $this->totalMobsAmount = 0;
$post = []; $post = [];
//console("ELP {$this->getName()}"); //console($this->getName());
//var_dump($this->entityListPositioned);
foreach($this->entityList as $k => $e){ foreach($this->entityList as $k => $e){
$dd = CORRECT_ENTITY_CLASSES[$e->class] ?? false;
if(!isset(CORRECT_ENTITY_CLASSES[$e->class])){ if($dd === false || ($dd !== true && !isset($dd[$e->type]))){
ConsoleAPI::warn("Entity $k has invalid class type! {$e->class}"); ConsoleAPI::warn("Entity $k has invalid entity! {$e->class} {$e->type}");
$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}");
$e->close(); $e->close();
unset($this->entityList[$k]); unset($this->entityList[$k]);
@ -440,8 +422,6 @@ class Level{
continue; continue;
} }
if(!($e instanceof Entity)){ if(!($e instanceof Entity)){
unset($this->entityList[$k]); unset($this->entityList[$k]);
unset($this->server->entities[$k]); unset($this->server->entities[$k]);