From 9cb7fbafaac8adccdfc15fc01902ba439c2df1c3 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 7 Jul 2023 22:49:57 +0300 Subject: [PATCH] StaticBlock class, entities performance boost(i think) --- src/API/EntityAPI.php | 4 +- src/PocketMinecraftServer.php | 1 + src/entity/Entity.php | 47 +++++++++--------- src/entity/ai/EntityAI.php | 1 - src/entity/ai/MobController.php | 28 +++++------ src/entity/monster/Skeleton.php | 8 +-- src/entity/monster/Zombie.php | 6 +-- src/entity/object/Arrow.php | 14 +++--- src/material/StaticBlock.php | 87 +++++++++++++++++++++++++++++++++ src/math/Vector3.php | 82 +++++++++++++++++++++++++++++++ src/world/MobSpawner.php | 12 +++-- 11 files changed, 232 insertions(+), 58 deletions(-) create mode 100644 src/material/StaticBlock.php diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 442cd13d..bb775751 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -70,8 +70,8 @@ class EntityAPI{ } elseif(is_numeric($args[1])){//summon [amount] $amount = (int) $args[1]; - if($amount > 100){ - return "Cannot spawn > 100 mobs"; + if($amount > 1000){ + return "Cannot spawn > 1000 mobs"; } $isBaby = false; if(isset($args[2]) and strtolower($args[2]) === 'baby'){//summon [amount] [baby] diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index bdb35319..f3f71bd0 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -35,6 +35,7 @@ class PocketMinecraftServer{ EntityRegistry::registerEntities(); Feature::init(); Biome::init(); + StaticBlock::init(); define("BOOTUP_RANDOM", Utils::getRandomBytes(16)); $this->serverID = $this->serverID === false ? Utils::readLong(substr(Utils::getUniqueID(true, $this->serverip . $this->port), 8)) : $this->serverID; $this->seed = $this->seed === false ? Utils::readInt(Utils::getRandomBytes(4, false)) : $this->seed; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index dfe80ab2..602c9551 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -413,7 +413,7 @@ class Entity extends Position $blockY = (int) ($this->y + $this->getEyeHeight() + $y); $blockZ = (int) ($this->z + $z); - if(!$this->level->getBlockWithoutVector($blockX, $blockY, $blockZ)->isTransparent){ + if(!StaticBlock::getIsTransparent($this->level->level->getBlockID($blockX, $blockY, $blockZ))){ $this->harm(1, "suffocation"); // Suffocation $hasUpdate = true; break; @@ -424,8 +424,10 @@ class Entity extends Position for ($x = $startX; $x <= $endX; ++$x){ for ($z = $startZ; $z <= $endZ; ++$z){ $pos = new Vector3($x, $y, $z); - $b = $this->level->getBlock($pos); - switch ($b->getID()) { + $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)) { @@ -436,7 +438,7 @@ class Entity extends Position $this->harm(2, "water"); $hasUpdate = true; $waterDone = true; - } elseif ($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1 - (($b->getMetadata() % 8) / 9)) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and !$waterDone) { + } elseif ($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1 - (($meta % 8) / 9)) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and !$waterDone) { $this->air -= 1; $waterDone = true; $this->updateMetadata(); @@ -520,7 +522,7 @@ class Entity extends Position } ++$this->counter; if($this->isStatic === false){ - $startX = floor($this->x - 0.5 - $this->width - 1); + /*$startX = floor($this->x - 0.5 - $this->width - 1); // prefix for flying when player on fence $y = floor($this->y - 1); $yC = ceil($this->y - 1); @@ -536,12 +538,12 @@ class Entity extends Position $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){ + $b = $this->level->level->getBlockID($x, $y, $z); + if(StaticBlock::getIsSolid($b) === 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){ + } elseif(StaticBlock::getIsLiquid($b) or $b === COBWEB or $b === LADDER or $b === FENCE or $b=== STONE_WALL or $b === IRON_BARS){ $isFlying = false; } } elseif($this->isSupport($v1, $this->width)){ @@ -559,7 +561,7 @@ class Entity extends Position break; } } - } + }*/ //this one is not neccessary anymore? if(!$this->isPlayer()){ $update = false; if(Utils::in_range($this->speedX, -0.01, 0.01)){ @@ -594,16 +596,16 @@ class Entity extends Position 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); - if(($b->getID() === WATER || $b->getID() === STILL_WATER)){ - $water = $b->y == ($y1 - 1); + $b = $this->level->level->getBlockID($x, $y, $z); + if(($b === WATER || $b === STILL_WATER)){ + $water = $y == ($y1 - 1); $this->fallDistance = 0; } - if($b != false && $b->isSolid){ - $this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY); - $this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX); - $this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ); + if(StaticBlock::getIsSolid($b)){ + $blockBounds = StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); + $this->speedY = $blockBounds->calculateYOffset($this->boundingBox, $this->speedY); + $this->speedX = $blockBounds->calculateXOffset($this->boundingBox, $this->speedX); + $this->speedZ = $blockBounds->calculateZOffset($this->boundingBox, $this->speedZ); } } } @@ -615,9 +617,9 @@ class Entity extends Position $horizontalMultiplyFactor = 0.91; if($support){ $horizontalMultiplyFactor = 0.54; - $b = $this->level->getBlockWithoutVector(floor($this->x), floor($this->boundingBox->minX) - 1, floor($this->z)); - if($b instanceof Block){ - $horizontalMultiplyFactor = $b->slipperiness * 0.91; + $b = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minX) - 1, floor($this->z)); + if($b > 0){ + $horizontalMultiplyFactor = StaticBlock::getSlipperiness($b) * 0.91; } } if($this->inWater){ @@ -644,7 +646,7 @@ class Entity extends Position $z = (int) ($this->z - 0.5); $lim = (int) floor($ny); for($y = (int) ceil($this->y) - 1; $y >= $lim; -- $y){ - if($this->level->getBlockWithoutVector($x, $y, $z)->isSolid === true){ + if(StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z))){ //$support = true; $this->y = $ny; $fall = $this->level->getBlock(new Vector3(intval($this->x - 0.5), intval(ceil($this->y)), intval($this->z - 0.5))); @@ -697,8 +699,7 @@ class Entity extends Position for($z = floor($this->boundingBox->minZ); $z < ceil($this->boundingBox->maxZ); ++$z){ for($y = floor($this->boundingBox->minY - 1); $y < ceil($this->boundingBox->maxY); ++$y){ if($y <= floor($this->boundingBox->minY) && !$this->onGround){ - $id = $this->level->getBlockWithoutVector($x, $y, $z); - $this->onGround = $id->isSolid; + $this->onGround = StaticBlock::getIsSolid($this->level->level->getBlockID($x, $y, $z)); }else{ $block = $this->level->level->getBlock($x, $y, $z); $id = $block[0]; diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index 73344fc0..90ccf9d9 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -29,7 +29,6 @@ class EntityAI * @param TaskBase $task */ public function addTask(TaskBase $task){ - console("[DEBUG] Adding new task...",true, true, 2); $this->tasks[$task->__toString()] = $task; } public function removeTask($name){ diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index c382ad74..31a12dcb 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -35,20 +35,20 @@ class MobController $xf = $this->entity->x + ($this->entity->getSpeedModifer() * $ox * $this->entity->getSpeed()); $zf = $this->entity->z + ($this->entity->getSpeedModifer() * $oz * $this->entity->getSpeed()); if($this->entity->onGround){ - $bs = [ //TODO simplify(somehow) - $this->entity->level->getBlockWithoutVector(ceil($xf), floor($this->entity->y), ceil($zf)), - $this->entity->level->getBlockWithoutVector(ceil($xf), floor($this->entity->y), $zf - ($oz < 0 ? 1 : 0)), - $this->entity->level->getBlockWithoutVector($xf - ($ox < 0 ? 1 : 0), floor($this->entity->y), $zf - ($oz < 0 ? 1 : 0)), - $this->entity->level->getBlockWithoutVector($xf - ($ox < 0 ? 1 : 0), floor($this->entity->y), ceil($zf)), - ]; - foreach($bs as $b){ - if($b->isSolid){ - if(!$b->getSide(1)->isSolid){ - $oy = 1; - break; - } - } - } + $oy = + StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), floor($this->entity->y), ceil($zf))) && + !StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), floor($this->entity->y) + 1, ceil($zf))) + || + StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), floor($this->entity->y), $zf - ($oz < 0))) && + !StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), floor($this->entity->y) + 1, $zf - ($oz < 0))) + || + StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), floor($this->entity->y), $zf - ($oz < 0))) && + !StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), floor($this->entity->y) + 1, $zf - ($oz < 0))) + || + StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), floor($this->entity->y), ceil($zf))) && + !StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), floor($this->entity->y) + 1, ceil($zf))) + ; + } $this->faceEntity($this->entity->add($ox, $oy, $oz)); if($this->entity->knockbackTime <= 0){ diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 0489353a..087fbc17 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -7,7 +7,7 @@ class Skeleton extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); - $this->ai->addTask(new TaskDestroyServerPerformance()); + //$this->ai->addTask(new TaskDestroyServerPerformance()); $this->setSpeed(0.25); $this->update(); } @@ -22,12 +22,12 @@ class Skeleton extends Monster{ } for($y = $this->y; $y < 129; $y++){ - $block = $this->level->getBlockWithoutVector($this->x, $y, $this->z); - if($block->isSolid){ + $block = $this->level->level->getBlockID($this->x, $y, $this->z); + if(StaticBlock::getIsSolid($block)){ return false; } } - if($block->getID() === AIR){ + if($block === AIR){ $this->fire = 160; //Value from 0.8.1 $this->updateMetadata(); return true; diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 26502a8e..9add4347 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -20,12 +20,12 @@ class Zombie extends Monster{ } for($y = $this->y; $y < 129; $y++){ - $block = $this->level->getBlockWithoutVector($this->x, $y, $this->z); - if($block->isSolid){ + $block = $this->level->level->getBlockID($this->x, $y, $this->z); + if(StaticBlock::getIsSolid($block)){ return false; } } - if($block->getID() === AIR){ + if($block === AIR){ $this->fire = 160; //Value from 0.8.1 $this->updateMetadata(); return true; diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 4cc67473..f158ed54 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -67,6 +67,8 @@ class Arrow extends Projectile{ } } public function update(){ + $this->needsUpdate = false; //TODO reenable + return; //parent::update(); if($this->closed || ($this->x > 255 || $this->x < 0 || $this->y < 0 || $this->z < 0 || $this->z > 255) || $this->groundTicks > 200) { //remove after 10 seconds in wall, idc about vanilla $this->server->api->entity->remove($this->eid); @@ -83,11 +85,10 @@ class Arrow extends Projectile{ $this->yaw = (atan2($this->speedX, $this->speedZ) * 180 / M_PI); $this->pitch = (atan2($this->speedY, $f) * 180 / M_PI); } - - $rt = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ); - for($x = floor($rt->minX); $x <= ceil($rt->maxX); ++$x){ - for($z = floor($rt->minZ); $z <= ceil($rt->maxZ); ++$z){ - for($y = floor($rt->minY); $y <= ceil($rt->maxY); ++$y){ + $rt = $this->boundingBox->shrink(0.4, 0.4, 0.4)->addCoord($this->speedX, $this->speedY, $this->speedZ); + for($x = floor($rt->minX); $x < ceil($rt->maxX); ++$x){ + for($z = floor($rt->minZ); $z < ceil($rt->maxZ); ++$z){ + for($y = ceil($rt->minY); $y < ceil($rt->maxY); ++$y){ $pos = new Vector3($x, $y, $z); $b = $this->level->getBlock($pos); if($b != false && $b->isSolid){ @@ -95,11 +96,11 @@ class Arrow extends Projectile{ $this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX); $this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ); $this->inWall = true; + break; } } } } - if(!$this->inWall){ $bbexp = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)->expand(0, 0.2, 0); foreach($this->level->entityList as $e){ @@ -129,7 +130,6 @@ class Arrow extends Projectile{ $this->sendMotion(); $this->updatePosition(); - $bb = $this->boundingBox->grow(0.1, 0.1, 0.1); } diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php new file mode 100644 index 00000000..0887b3ef --- /dev/null +++ b/src/material/StaticBlock.php @@ -0,0 +1,87 @@ +getID()] = $b->isSolid; + self::$isTransparent[$b->getID()] = $b->isTransparent; + self::$isFlowable[$b->getID()] = $b->isFlowable; + self::$isReplaceable[$b->getID()] = $b->isReplaceable; + self::$isPlaceable[$b->getID()] = $b->isPlaceable; + self::$hasPhysics[$b->getID()] = $b->hasPhysics; + self::$isLiquid[$b->getID()] = $b->isLiquid; + self::$isFullBlock[$b->getID()] = $b->isFullBlock; + + self::$slipperiness[$b->getID()] = $b->slipperiness; + self::$boundingBoxes[$b->getID()] = $b->boundingBox; + } + } + + public static function getBoundingBoxForBlockCoords($id, $x, $y, $z){ + /**@var AxisAlignedBB $bb*/ + $bb = nullsafe(self::$boundingBoxes[$id], false); + if($bb === false){ + return clone self::$NULL_BOUNDS; + } + $bb = clone $bb; + return $bb->setBounds($x + $bb->minX, $y + $bb->minY, $z + $bb->minZ, $x + $bb->maxX, $y + $bb->maxY, $z + $bb->maxZ); + } + + + public static function getSlipperiness($id){ + return nullsafe(self::$slipperiness[$id], StaticBlock::DEFAULT_SLIPPERINESS); + } + + public static function getIsSolid($id){ + return nullsafe(self::$isSolid[$id], false); + } + + public static function getIsTransparent($id){ + return nullsafe(self::$isTransparent[$id], false); + } + + public static function getIsFlowable($id){ + return nullsafe(self::$isFlowable[$id], false); + } + + public static function getIsReplaceable($id){ + return nullsafe(self::$isReplaceable[$id], false); + } + + public static function getIsPlaceable($id){ + return nullsafe(self::$isPlaceable[$id], false); + } + + public static function getHasPhysics($id){ + return nullsafe(self::$hasPhysics[$id], false); + } + + public static function getIsLiquid($id){ + return nullsafe(self::$isLiquid[$id], false); + } + + public static function getIsFullBlock($id){ + return nullsafe(self::$isFullBlock[$id], false); + } +} + diff --git a/src/math/Vector3.php b/src/math/Vector3.php index e472039a..cc183812 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -186,4 +186,86 @@ class Vector3{ public function __toString(){ return "Vector3(x=" . $this->x . ",y=" . $this->y . ",z=" . $this->z . ")"; } + + + /** + * Returns a new vector with x value equal to the second parameter, along the line between this vector and the + * passed in vector, or null if not possible. + * + * @param Vector3 $v + * @param float $x + * + * @return Vector3 + */ + public function getIntermediateWithXValue(Vector3 $v, $x){ + $xDiff = $v->x - $this->x; + $yDiff = $v->y - $this->y; + $zDiff = $v->z - $this->z; + + if(($xDiff * $xDiff) < 0.0000001){ + return null; + } + + $f = ($x - $this->x) / $xDiff; + + if($f < 0 or $f > 1){ + return null; + }else{ + return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + } + } + + /** + * Returns a new vector with y value equal to the second parameter, along the line between this vector and the + * passed in vector, or null if not possible. + * + * @param Vector3 $v + * @param float $y + * + * @return Vector3 + */ + public function getIntermediateWithYValue(Vector3 $v, $y){ + $xDiff = $v->x - $this->x; + $yDiff = $v->y - $this->y; + $zDiff = $v->z - $this->z; + + if(($yDiff * $yDiff) < 0.0000001){ + return null; + } + + $f = ($y - $this->y) / $yDiff; + + if($f < 0 or $f > 1){ + return null; + }else{ + return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + } + } + + /** + * Returns a new vector with z value equal to the second parameter, along the line between this vector and the + * passed in vector, or null if not possible. + * + * @param Vector3 $v + * @param float $z + * + * @return Vector3 + */ + public function getIntermediateWithZValue(Vector3 $v, $z){ + $xDiff = $v->x - $this->x; + $yDiff = $v->y - $this->y; + $zDiff = $v->z - $this->z; + + if(($zDiff * $zDiff) < 0.0000001){ + return null; + } + + $f = ($z - $this->z) / $zDiff; + + if($f < 0 or $f > 1){ + return null; + }else{ + return new Vector3($this->x + $xDiff * $f, $this->y + $yDiff * $f, $this->z + $zDiff * $f); + } + } } \ No newline at end of file diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index a0691ac4..2161cd5c 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -63,10 +63,14 @@ class MobSpawner{ protected function getSafeY($x, $z, $grassOnly = false, $highMob = false){ //first safe block //TODO check boundingbox $allowed = []; for($y = 0; $y < 128; ++$y){ - $b = $this->level->getBlockWithoutVector($x, $y, $z); - $b2 = $this->level->getBlockWithoutVector($x, $y + 1, $z); - $b1 = $this->level->getBlockWithoutVector($x, $y - 1, $z); - if(!$b->isSolid && !$b->isLiquid && ($b1->isSolid && ($grassOnly ? $b1->getID() === GRASS : true) && ($highMob ? !$b2->isSolid && !$b2->isLiquid : true))){ + $b = $this->level->level->getBlockID($x, $y, $z); + $b2 = $this->level->level->getBlockID($x, $y + 1, $z); + $b1 = $this->level->level->getBlockID($x, $y - 1, $z); + if( + !StaticBlock::getIsSolid($b) && !StaticBlock::getIsLiquid($b) && + (StaticBlock::getIsSolid($b1) && ($grassOnly ? $b1 === GRASS : true) && + ($highMob ? !StaticBlock::getIsSolid($b2) && !StaticBlock::getIsLiquid($b2) : true)) + ){ $allowed[] = $y; } }