From 3c89749125901afe237b5f5c9215e1f4ebe25e67 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 7 Jan 2024 22:49:46 +0300 Subject: [PATCH 01/34] added entity pushing(disabled by default) --- src/API/EntityAPI.php | 9 +++++++++ src/Player.php | 6 ++++-- src/PocketMinecraftServer.php | 2 ++ src/entity/Entity.php | 9 +-------- src/entity/Living.php | 26 +++++++++++++++++++----- src/world/Level.php | 37 ++++++++++++++++++++++++++++++++++- 6 files changed, 73 insertions(+), 16 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index f956db1a..328bc261 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -162,6 +162,9 @@ class EntityAPI{ }else{ $this->entities[$eid] = new Entity($level, $eid, $class, $type, $data); } + $cX = (int)$this->entities[$eid]->x >> 4; + $cZ = (int)$this->entities[$eid]->z >> 4; + $level->entityListPositioned["$cX $cZ"][$eid] = $eid; $level->entityList[$eid] = &$this->entities[$eid]; $this->server->handle("entity.add", $this->entities[$eid]); return $this->entities[$eid]; @@ -182,6 +185,12 @@ class EntityAPI{ public function remove($eid){ if(isset($this->entities[$eid])){ $this->entities[$eid]->closed = true; + if($this->entities[$eid]->level instanceof Level){ + $cX = (int)$this->entities[$eid]->x >> 4; + $cZ = (int)$this->entities[$eid]->z >> 4; + $index = "$cX $cZ"; + unset($this->entities[$eid]->level->entityListPositioned[$index][$eid]); + } if($this->entities[$eid]->isPlayer()){ $pk = new RemovePlayerPacket; $pk->eid = $eid; diff --git a/src/Player.php b/src/Player.php index aefc726c..49b97b5f 100755 --- a/src/Player.php +++ b/src/Player.php @@ -110,8 +110,9 @@ class Player{ $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; - $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); + $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); + $this->evid[] = $this->server->event("server.close", [$this, "close"]); console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2); } @@ -1365,6 +1366,7 @@ class Player{ } public function entityTick(){ if($this->isSleeping) ++$this->sleepingTime; + $this->handlePacketQueues(); } public function handleDataPacket(RakNetDataPacket $packet){ if($this->connected === false){ @@ -1567,7 +1569,7 @@ class Player{ console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); - break; + return false; //return false so it will not be updated by tickerFunction case ProtocolInfo::READY_PACKET: if($this->loggedIn === false){ break; diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 78bdb022..fe89c973 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -89,10 +89,12 @@ class PocketMinecraftServer{ "16x16x16_chunk_sending" => false, "experimental-mob-ai" => false, "force-20-tps" => false, + "enable-mob-pushing" => Living::$entityPushing ]); Player::$smallChunks = $this->extraprops->get("16x16x16_chunk_sending"); Living::$despawnMobs = $this->extraprops->get("despawn-mobs"); Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks"); + Living::$entityPushing = $this->extraprops->get("enable-mob-pushing"); self::$FORCE_20_TPS = $this->extraprops->get("force-20-tps"); PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data"); MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai"); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index cef84853..c86486f3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1117,14 +1117,7 @@ class Entity extends Position public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) { - if($pos instanceof Position and $pos->level instanceof Level and $this->level !== $pos->level){ - $this->level = $pos->level; - $this->server->preparedSQL->entity->setLevel->reset(); - $this->server->preparedSQL->entity->setLevel->clear(); - $this->server->preparedSQL->entity->setLevel->bindValue(":level", $this->level->getName(), SQLITE3_TEXT); - $this->server->preparedSQL->entity->setLevel->bindValue(":eid", $this->eid, SQLITE3_INTEGER); - $this->server->preparedSQL->entity->setLevel->execute(); - } + $this->x = $pos->x; $this->y = $pos->y; $this->z = $pos->z; diff --git a/src/entity/Living.php b/src/entity/Living.php index 955f3a6a..b8bb76e6 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -2,7 +2,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ - public static $despawnMobs, $despawnTimer, $entityPushing; + public static $despawnMobs, $despawnTimer, $entityPushing = false; public $target, $ai; public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower; @@ -42,7 +42,23 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ return true; } public function collideHandler(){ - + $rd = ceil($this->radius); + $minChunkX = ((int)($this->x - $rd)) >> 4; + $minChunkZ = ((int)($this->z - $rd)) >> 4; + $maxChunkX = ((int)($this->x + $rd)) >> 4; + $maxChunkZ = ((int)($this->z + $rd)) >> 4; + + //TODO also index by chunkY? + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ + if($this->level->entityList[$entid] instanceof Entity){ + $this->applyCollision($this->level->entityList[$entid]); + } + } + } + } } public function applyCollision(Entity $collided){ @@ -83,9 +99,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ $this->ai->mobController->rotateTick(); $this->ai->mobController->movementTick(); - /*if(self::$entityPushing){ - $this->collideHandler(); - }*/ + if(self::$entityPushing){ + $this->collideHandler(); + } if($this->onGround){ //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); diff --git a/src/world/Level.php b/src/world/Level.php index 50466363..ab5ed1d5 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -11,6 +11,7 @@ class Level{ */ public $entityList; + public $entityListPositioned = []; public $entitiesInLove = []; public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0; @@ -321,7 +322,6 @@ class Level{ } public function onTick(PocketMinecraftServer $server, $currentTime){ - //$ents = $server->api->entity->getAll($this); if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ @@ -343,11 +343,16 @@ class Level{ $this->totalMobsAmount = 0; $post = []; foreach($this->entityList as $k => $e){ + if(!($e instanceof Entity)){ unset($this->entityList[$k]); unset($this->server->entities[$k]); + //TODO try to remove from $entityListPositioned? continue; } + $curChunkX = (int)$e->x >> 4; + $curChunkZ = (int)$e->z >> 4; + if($e->class === ENTITY_MOB && !$e->isPlayer()){ ++$this->totalMobsAmount; } @@ -355,6 +360,17 @@ class Level{ $e->update($currentTime); if(!$e->isPlayer()) $post[] = $k; } + + if($e instanceof Entity){ + $newChunkX = (int)$e->x >> 4; + $newChunkZ = (int)$e->z >> 4; + if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ + $index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow + $newIndex = "$newChunkX $newChunkZ"; + unset($this->entityListPositioned[$index][$e->eid]); + $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks + } + } } $this->checkSleep(); @@ -387,6 +403,25 @@ class Level{ } } + public function getEntitiesNearby(Entity $e, $radius = 5){ + $minChunkX = ((int)($e->x - $radius)) >> 4; + $minChunkZ = ((int)($e->z - $radius)) >> 4; + $maxChunkX = ((int)($e->x + $radius)) >> 4; + $maxChunkZ = ((int)($e->z + $radius)) >> 4; + $ents = []; + //TODO also index by chunkY? + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($this->entityListPositioned[$ind] ?? [] as $entid){ + $ents[$entid] = $this->entityList[$entid]; + } + } + } + unset($ents[$e->eid]); + return $ents; + } + public function addBlockToSendQueue($x, $y, $z, $id, $meta){ $this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta]; } From 1df2204490012779b4e1ba8c0d502141de797611 Mon Sep 17 00:00:00 2001 From: GameHerobrine <55945072+GameHerobrine@users.noreply.github.com> Date: Mon, 8 Jan 2024 18:31:48 +0300 Subject: [PATCH 02/34] fixed tickerFunction x3 --- src/PocketMinecraftServer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index fe89c973..349219ba 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -622,7 +622,7 @@ class PocketMinecraftServer{ ++$actionCount; try{ $return = @call_user_func($this->schedule[$cid][0] ?? function(){}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null - }catch(ValueError $e){ + }catch(Throwable $e){ $return = false; } }else{ From fa91bbd98a4a1bfeefc108b38f46640ba766ab66 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 00:06:33 +0300 Subject: [PATCH 03/34] fixed block update queue --- src/world/Level.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/world/Level.php b/src/world/Level.php index ab5ed1d5..1cafeabe 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -390,7 +390,7 @@ class Level{ } $player->sendEntityMovementUpdateQueue(); - foreach($this->queuedBlockUpdates as $update){ + foreach($this->queuedBlockUpdates as $ind => $update){ $x = $update[0]; $y = $update[1]; $z = $update[2]; @@ -398,6 +398,7 @@ class Level{ $id = $idmeta[0]; $meta = $idmeta[1]; $player->addBlockUpdateIntoQueue($x, $y, $z, $id, $meta); + unset($this->queuedBlockUpdates[$ind]); } $player->sendBlockUpdateQueue(); } From af7293d4d73bda05deee131bc63a40eaf4e5a8e0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 00:23:38 +0300 Subject: [PATCH 04/34] fixed x2 --- src/world/Level.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/world/Level.php b/src/world/Level.php index 1cafeabe..31f2b3ee 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -398,10 +398,11 @@ class Level{ $id = $idmeta[0]; $meta = $idmeta[1]; $player->addBlockUpdateIntoQueue($x, $y, $z, $id, $meta); - unset($this->queuedBlockUpdates[$ind]); } $player->sendBlockUpdateQueue(); } + + $this->queuedBlockUpdates = []; } public function getEntitiesNearby(Entity $e, $radius = 5){ From 1bbe5c7582807ae9da6d5230a6777feba55adffe Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 05:19:36 +0300 Subject: [PATCH 05/34] removed useless classes, entities are not in database anymore --- src/API/EntityAPI.php | 23 ++++++++++++++--------- src/API/PlayerAPI.php | 18 ++++++------------ src/Player.php | 3 +++ src/PocketMinecraftServer.php | 6 ++---- src/dependencies.php | 3 --- src/entity/Attachable.php | 5 ----- src/entity/Damageable.php | 5 ----- src/entity/Entity.php | 16 ---------------- src/entity/Hanging.php | 5 ----- src/entity/Human.php | 5 ----- src/entity/Living.php | 2 +- src/entity/Projectile.php | 1 - src/entity/object/Painting.php | 3 ++- src/world/Level.php | 25 +++++++++++++++---------- 14 files changed, 43 insertions(+), 77 deletions(-) delete mode 100644 src/entity/Attachable.php delete mode 100644 src/entity/Damageable.php delete mode 100644 src/entity/Hanging.php delete mode 100644 src/entity/Human.php diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index 328bc261..f6e63043 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -204,22 +204,27 @@ class EntityAPI{ $this->server->api->dhandle("entity.remove", $this->entities[$eid]); unset($this->entities[$eid]->level->entityList[$eid]); unset($this->entities[$eid]); - $this->server->query("DELETE FROM entities WHERE EID = " . $eid . ";"); } } public function getRadius(Position $center, $radius = 15, $class = false){ - $entities = []; - $l = $this->server->query("SELECT EID FROM entities 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;"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->get($e["EID"]); - if($e instanceof Entity){ - $entities[$e->eid] = $e; + $minChunkX = ((int)($center->x - $radius)) >> 4; + $minChunkZ = ((int)($center->z - $radius)) >> 4; + $maxChunkX = ((int)($center->x + $radius)) >> 4; + $maxChunkZ = ((int)($center->z - $radius)) >> 4; + $ents = []; + //TODO also index by chunkY? + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($center->level->entityListPositioned[$ind] ?? [] as $entid){ + if($this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ + $ents[$entid] = $this->entities[$entid]; + } } } } - return $entities; + return $ents; } public function heal($eid, $heal, $cause){ diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index 938dda3e..d8797ea5 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -32,7 +32,7 @@ class PlayerAPI{ $this->server->api->console->cmdWhitelist("ping"); $this->server->api->console->cmdWhitelist("spawn"); $this->server->api->console->cmdWhitelist("loc"); - $this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = " . ENTITY_PLAYER . " AND health < 20;"); + //$this->server->preparedSQL->selectPlayersToHeal = $this->server->database->prepare("SELECT EID FROM entities WHERE class = " . ENTITY_PLAYER . " AND health < 20;"); } public function registerCmd($cmd, $help = ""){ @@ -43,7 +43,7 @@ class PlayerAPI{ switch($event){ case "server.regeneration": if($this->server->difficulty === 0){ - $result = $this->server->preparedSQL->selectPlayersToHeal->execute(); + //$result = $this->server->preparedSQL->selectPlayersToHeal->execute(); if($result !== false){ while(($player = $result->fetchArray()) !== false){ if(($player = $this->server->api->entity->get($player["EID"])) !== false){ @@ -365,6 +365,7 @@ class PlayerAPI{ "z" => $player->level->getSpawn()->z, ]); } + $player->level->players[$CID] = $player; $this->server->query("INSERT OR REPLACE INTO players (CID, ip, port, name) VALUES (" . $player->CID . ", '" . $player->ip . "', " . $player->port . ", '" . strtolower($player->username) . "');"); } } @@ -434,16 +435,8 @@ class PlayerAPI{ public function getAll($level = null){ if($level instanceof Level){ - $clients = []; - $l = $this->server->query("SELECT EID FROM entities WHERE level = '" . $level->getName() . "' AND class = '" . ENTITY_PLAYER . "';"); - if($l !== false and $l !== true){ - while(($e = $l->fetchArray(SQLITE3_ASSOC)) !== false){ - $e = $this->getByEID($e["EID"]); - if($e instanceof Player){ - $clients[$e->CID] = $e; - } - } - } return $clients; + //safe_var_dump($level->players); + return $level->players; } return $this->server->clients; @@ -487,6 +480,7 @@ class PlayerAPI{ } $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); $this->server->api->entity->remove($player->eid); + unset($player->level->players[$this->CID]); if($player->entity instanceof Entity){ unset($player->entity->player); //unset($player->entity); diff --git a/src/Player.php b/src/Player.php index 49b97b5f..647092d7 100755 --- a/src/Player.php +++ b/src/Player.php @@ -204,7 +204,10 @@ class Player{ } $this->level->freeAllChunks($this); + unset($this->level->players[$this->CID]); $this->level = $pos->level; + + $this->level->players[$this->CID] = $this; $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); $this->server->api->entity->spawnAll($this); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 349219ba..3b9c8333 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -729,10 +729,8 @@ class PocketMinecraftServer{ $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage() / 1024) / 1024, 2) . "MB"; $info["memory_peak_usage"] = round((memory_get_peak_usage() / 1024) / 1024, 2) . "MB"; - $info["entities"] = $this->query("SELECT count(EID) as count FROM entities;", true); - $info["entities"] = $info["entities"]["count"]; - $info["players"] = $this->query("SELECT count(CID) as count FROM players;", true); - $info["players"] = $info["players"]["count"]; + $info["entities"] = count($this->entities); + $info["players"] = count($this->clients); $info["events"] = count($this->eventsID); $info["handlers"] = $this->query("SELECT count(ID) as count FROM handlers;", true); $info["handlers"] = $info["handlers"]["count"]; diff --git a/src/dependencies.php b/src/dependencies.php index d5fb697f..0119e58d 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -99,10 +99,7 @@ require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php"); require_once(FILE_PATH . "/src/entity/Rideable.php"); -require_once(FILE_PATH . "/src/entity/Attachable.php"); -require_once(FILE_PATH . "/src/entity/Damageable.php"); require_once(FILE_PATH . "/src/entity/Breedable.php"); -require_once(FILE_PATH . "/src/entity/Damageable.php"); require_once(FILE_PATH . "/src/entity/Pathfindable.php"); require_once(FILE_PATH . "/src/entity/Entity.php"); require_once(FILE_PATH . "/src/entity/Living.php"); diff --git a/src/entity/Attachable.php b/src/entity/Attachable.php deleted file mode 100644 index b3600271..00000000 --- a/src/entity/Attachable.php +++ /dev/null @@ -1,5 +0,0 @@ -gravity = 0.08; $this->state = $this->data["State"] = isset($this->data["State"]) ? $this->data["State"] : 0; $this->tickCounter = 0; - $this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health, hasUpdate) VALUES (" . $this->eid . ", '" . $this->level->getName() . "', " . $this->type . ", " . $this->class . ", " . $this->health . ", 0);"); $this->x = isset($this->data["x"]) ? (float) $this->data["x"] : 0; $this->y = isset($this->data["y"]) ? (float) $this->data["y"] : 0; $this->z = isset($this->data["z"]) ? (float) $this->data["z"] : 0; @@ -1075,7 +1074,6 @@ class Entity extends Position public function setName($name) { $this->name = $name; - // $this->server->query("UPDATE entities SET name = '".str_replace("'", "", $this->name)."' WHERE EID = ".$this->eid.";"); is this neccessary for database? } public function look($pos2) @@ -1084,7 +1082,6 @@ class Entity extends Position $angle = Utils::angle3D($pos2, $pos); $this->yaw = $angle["yaw"]; $this->pitch = $angle["pitch"]; - $this->server->query("UPDATE entities SET pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); } public function move(Vector3 $pos, $yaw = 0, $pitch = 0) @@ -1096,7 +1093,6 @@ class Entity extends Position $this->yaw %= 360; $this->pitch += $pitch; $this->pitch %= 90; - $this->server->query("UPDATE entities SET x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); $this->updateAABB(); } @@ -1110,14 +1106,12 @@ class Entity extends Position public function updatePosition() { - $this->server->query("UPDATE entities SET level = '" . $this->level->getName() . "', x = " . $this->x . ", y = " . $this->y . ", z = " . $this->z . ", pitch = " . $this->pitch . ", yaw = " . $this->yaw . " WHERE EID = " . $this->eid . ";"); $this->sendMoveUpdate(); $this->updateAABB(); } public function setPosition(Vector3 $pos, $yaw = false, $pitch = false) { - $this->x = $pos->x; $this->y = $pos->y; $this->z = $pos->z; @@ -1127,15 +1121,6 @@ class Entity extends Position if($pitch !== false){ $this->pitch = $pitch; } - $this->server->preparedSQL->entity->setPosition->reset(); - $this->server->preparedSQL->entity->setPosition->clear(); - $this->server->preparedSQL->entity->setPosition->bindValue(":x", $this->x, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":y", $this->y, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":z", $this->z, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":pitch", $this->pitch, SQLITE3_TEXT); - $this->server->preparedSQL->entity->setPosition->bindValue(":yaw", $this->yaw, SQLITE3_TEXT); - $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) { @@ -1371,7 +1356,6 @@ class Entity extends Position "cause" => $cause )) !== false or $force === true){ $this->health = min(127, max(- 127, $health)); - $this->server->query("UPDATE entities SET health = " . $this->health . " WHERE EID = " . $this->eid . ";"); if($harm === true){ $pk = new EntityEventPacket; $pk->eid = $this->eid; diff --git a/src/entity/Hanging.php b/src/entity/Hanging.php deleted file mode 100644 index 554abb69..00000000 --- a/src/entity/Hanging.php +++ /dev/null @@ -1,5 +0,0 @@ -x >> 4; $newChunkZ = (int)$e->z >> 4; - if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ + if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); + }else if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ $index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow $newIndex = "$newChunkX $newChunkZ"; unset($this->entityListPositioned[$index][$e->eid]); $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks } - } + }else if(isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ + unset($this->entityListPositioned["$curChunkX $curChunkZ"][$k]); + } } $this->checkSleep(); @@ -405,22 +409,23 @@ class Level{ $this->queuedBlockUpdates = []; } - public function getEntitiesNearby(Entity $e, $radius = 5){ - $minChunkX = ((int)($e->x - $radius)) >> 4; - $minChunkZ = ((int)($e->z - $radius)) >> 4; - $maxChunkX = ((int)($e->x + $radius)) >> 4; - $maxChunkZ = ((int)($e->z + $radius)) >> 4; + public function getEntitiesInAABB(AxisAlignedBB $bb){ + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->maxX)) >> 4; + $maxChunkZ = ((int)($bb->maxZ)) >> 4; $ents = []; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->entityListPositioned[$ind] ?? [] as $entid){ - $ents[$entid] = $this->entityList[$entid]; + foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ + if($this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + $ents[$entid] = $this->entityList[$entid]; + } } } } - unset($ents[$e->eid]); return $ents; } From 80d78faecd21fa1b60a021bdc8d4ff72c4430b4f Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 05:28:24 +0300 Subject: [PATCH 06/34] removed advanced cache --- src/API/QueryAPI.php | 2 +- src/API/ServerAPI.php | 5 +---- src/PocketMinecraftServer.php | 3 +-- src/utils/Cache.php | 36 ----------------------------------- src/world/Level.php | 20 ------------------- 5 files changed, 3 insertions(+), 63 deletions(-) delete mode 100644 src/utils/Cache.php diff --git a/src/API/QueryAPI.php b/src/API/QueryAPI.php index 46357c6f..531b66cb 100644 --- a/src/API/QueryAPI.php +++ b/src/API/QueryAPI.php @@ -2,7 +2,7 @@ class QueryAPI{ - public static $NOT_FOUND_VALUE; + public static $NOT_FOUND_VALUE; //TODO just use null private $queryData; private $server; diff --git a/src/API/ServerAPI.php b/src/API/ServerAPI.php index 7003300d..8443fdb0 100644 --- a/src/API/ServerAPI.php +++ b/src/API/ServerAPI.php @@ -143,11 +143,8 @@ class ServerAPI{ Entity::$allowedAI = $this->getProperty("enable-mob-ai", true); //Load advanced properties define("DEBUG", $this->getProperty("debug", 1)); - define("ADVANCED_CACHE", $this->getProperty("enable-advanced-cache", false)); + define("ADVANCED_CACHE", false); //define("MAX_CHUNK_RATE", 20 / $this->getProperty("max-chunks-per-second", 8)); //Default rate ~512 kB/s - if(ADVANCED_CACHE == true){ - console("[INFO] Advanced cache enabled"); - } MobSpawner::$spawnAnimals = $this->getProperty("spawn-animals"); MobSpawner::$spawnMobs = $this->getProperty("spawn-mobs"); if($this->getProperty("upnp-forwarding") == true){ diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 3b9c8333..a70a0965 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -439,7 +439,6 @@ class PocketMinecraftServer{ } $this->schedule(20 * 15, [$this, "checkTicks"], [], true); $this->schedule(20 * 60, [$this, "checkMemory"], [], true); - $this->schedule(20 * 45, "Cache::cleanup", [], true); $this->schedule(20, [$this, "asyncOperationChecker"], [], true); } @@ -488,7 +487,7 @@ class PocketMinecraftServer{ } elseif($this->tick() > 0){ $lastLoop = 0; } else{ - ++ $lastLoop; + ++$lastLoop; if($lastLoop < 16){ usleep(1); } elseif($lastLoop < 128){ diff --git a/src/utils/Cache.php b/src/utils/Cache.php deleted file mode 100644 index 9091e6f5..00000000 --- a/src/utils/Cache.php +++ /dev/null @@ -1,36 +0,0 @@ - $data){ - if($data[1] < $time){ - unset(self::$cached[$index]); - } - } - } -} \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index 7da4b4c6..1dad5c63 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -292,9 +292,6 @@ class Level{ } $block->position($pos); $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); - if(ADVANCED_CACHE == true){ - Cache::remove("world:{$this->name}:" . ($pos->x >> 4) . ":" . ($pos->z >> 4)); - } if(!isset($this->changedBlocks[$i])){ $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; @@ -451,9 +448,6 @@ class Level{ $this->changedBlocks[$i] = []; $this->changedCount[$i] = 0; } - if(ADVANCED_CACHE == true){ - Cache::remove("world:{$this->name}:" . ($pos->x >> 4) . ":" . ($pos->z >> 4)); - } $this->changedBlocks[$i][] = [$block->x, $block->y, $block->z, $block->id, $block->getMetadata()]; ++$this->changedCount[$i]; } @@ -482,9 +476,6 @@ class Level{ return false; } $this->changedCount[$X . ":" . $Y . ":" . $Z] = 4096; - if(ADVANCED_CACHE == true){ - Cache::remove("world:{$this->name}:$X:$Z"); - } return $this->level->setMiniChunk($X, $Z, $Y, $data); } @@ -503,7 +494,6 @@ class Level{ if($force !== true and $this->isSpawnChunk($X, $Z)){ return false; } - Cache::remove("world:{$this->name}:$X:$Z"); return $this->level->unloadChunk($X, $Z, $this->server->saveEnabled); } @@ -511,13 +501,6 @@ class Level{ if(!isset($this->level)){ return false; } - if(ADVANCED_CACHE == true and $Yndex == 0xff){ - $identifier = "world:{$this->name}:$X:$Z"; - if(($cache = Cache::get($identifier)) !== false){ - return $cache; - } - } - $raw = []; for($Y = 0; $Y < 8; ++$Y){ @@ -534,9 +517,6 @@ class Level{ $ordered .= substr($mini, $j << 5, 24); //16 + 8 } } - if(ADVANCED_CACHE == true and $Yndex == 0xff){ - Cache::add($identifier, $ordered, 60); - } return $ordered; } From 8684e3092a9906a2e3042ca87a10f3ef940c7b11 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 05:51:06 +0300 Subject: [PATCH 07/34] fixed ice collisions --- src/entity/Entity.php | 5 +++-- src/material/block/solid/IceBlock.php | 5 +++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index b7bef8b6..adbf0725 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -566,9 +566,10 @@ class Entity extends Position for($x = $x0; $x <= $x1; ++$x){ for($y = $y0; $y <= $y1; ++$y){ for($z = $z0; $z <= $z1; ++$z){ + $b = $this->level->level->getBlockID($x, $y, $z); - if(($b === WATER || $b === STILL_WATER)){ - $water = $y == ($y1 - 1); + if(($b == WATER || $b == STILL_WATER)){ + $water |= $y == ($y1 - 1); $this->fallDistance = 0; } if($b != 0){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 43f878b0..40be3d16 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -6,6 +6,11 @@ class IceBlock extends TransparentBlock{ $this->hardness = 2.5; $this->slipperiness = 0.98; } + + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ + return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; + } + public function place(Item $item, Player $player, Block $block, Block $target, $face, $fx, $fy, $fz){ $ret = $this->level->setBlock($this, $this, true, false, true); $this->level->scheduleBlockUpdate(new Position($this, 0, 0, $this->level), Utils::getRandomUpdateTicks(), BLOCK_UPDATE_RANDOM); From 5161d087ba05183222995a825b08a66703e26da1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 06:44:19 +0300 Subject: [PATCH 08/34] ported 1.5.2 way of mob movement but broke everything else --- src/entity/Animal.php | 6 +- src/entity/Creature.php | 8 +- src/entity/Entity.php | 166 +++++++++++++++++--------------- src/entity/Living.php | 78 +++++++++++++-- src/entity/ai/MobController.php | 4 +- 5 files changed, 169 insertions(+), 93 deletions(-) diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 4a59a725..fd9678e7 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -12,9 +12,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{ $this->setAge(-24000); } - $this->ai->addTask(new TaskTempt()); - $this->ai->addTask(new TaskPanic()); - $this->ai->addTask(new TaskMate()); + //$this->ai->addTask(new TaskTempt()); + //$this->ai->addTask(new TaskPanic()); + //$this->ai->addTask(new TaskMate()); } public function harm($dmg, $cause = "generic", $force = false){ diff --git a/src/entity/Creature.php b/src/entity/Creature.php index be6b2366..d07a875e 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -11,10 +11,10 @@ abstract class Creature extends Living{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 1, "generic"); $this->enableAutojump = true; //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); - $this->ai->addTask(new TaskLookAround()); - $this->ai->addTask(new TaskRandomWalk()); - $this->ai->addTask(new TaskLookAtPlayer()); - $this->ai->addTask(new TaskSwimming()); + //$this->ai->addTask(new TaskLookAround()); + //$this->ai->addTask(new TaskRandomWalk()); + //$this->ai->addTask(new TaskLookAtPlayer()); + //$this->ai->addTask(new TaskSwimming()); } public function createSaveData(){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index adbf0725..9d03314f 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -6,6 +6,7 @@ class Entity extends Position const TYPE = - 1; const CLASS_TYPE = - 1; const MIN_POSSIBLE_SPEED = 1/8000; //anything below will send 0 to player + public $counter = 0; public $fallDistance = 0; public static $updateOnTick, $allowedAI; @@ -486,9 +487,91 @@ class Entity extends Position return $hasUpdate; } + public function moveFlying($strafe, $forward, $speed){ //TODO rename? + $v4 = $strafe*$strafe + $forward*$forward; + + if($v4 >= 1.0e-4){ //TODO dont use ugly scienfitic notation + $v4 = sqrt($v4); + if($v4 < 1) $v4 = 1; + + $v4 = $speed / $v4; + $strafe *= $v4; + $forward *= $v4; + + $v5 = sin($this->yaw * M_PI/180); + $v6 = cos($this->yaw * M_PI/180); + + $this->speedX += $strafe*$v6 - $forward*$v5; + $this->speedZ += $forward*$v6 + $strafe*$v5; + + //float var5 = MathHelper.sin(this.rotationYaw * (float)Math.PI / 180.0F); + //float var6 = MathHelper.cos(this.rotationYaw * (float)Math.PI / 180.0F); + //this.motionX += (double)(strafe * var6 - forward * var5); + //this.motionZ += (double)(forward * var6 + strafe * var5); + } + } + public function updateEntityMovement(){} - + public function move($dx, $dy, $dz){ + //TODO ySize? + $movX = $this->x; + $movY = $this->y; + $movZ = $this->z; + + //TODO isInWeb + $savedDX = $dx; + $savedDY = $dy; + $savedDZ = $dz; + + $oldBB = clone $this->boundingBox; + + $aABB = $this->boundingBox->addCoord($dx, $dy, $dz); + $x0 = floor($aABB->minX); + $x1 = ceil($aABB->maxX); + $y0 = floor($aABB->minY); + $y1 = ceil($aABB->maxY); + $z0 = floor($aABB->minZ); + $z1 = ceil($aABB->maxZ); + + for($x = $x0; $x <= $x1; ++$x){ + for($y = $y0; $y <= $y1; ++$y){ + for($z = $z0; $z <= $z1; ++$z){ + $b = $this->level->level->getBlockID($x, $y, $z); + if($b != 0){ //TODO might be better to use other method + $blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); + foreach($blockBounds as $blockBound){ + $dy = $blockBound->calculateYOffset($this->boundingBox, $dy); + $dx = $blockBound->calculateXOffset($this->boundingBox, $dx); + $dz = $blockBound->calculateZOffset($this->boundingBox, $dz); + } + } + } + } + } + $this->boundingBox->offset($dx, $dy, $dz); + $fallingFlag = $this->onGround || $savedDY != $dy && $savedDY < 0; + + //TODO step + $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; + $this->y = $this->boundingBox->minY; //TODO + $this->yOffset - //TODO $this->ySize; + $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; + //$this.isCollidedHorizontally = savedDX != dx || savedDZ != dz; + //$this.isCollidedVertically = savedDY != dy; + $this->onGround = $savedDY != $dy && $savedDY < 0.0; + //$this.isCollided = this.isCollidedHorizontally || this.isCollidedVertically; + + if($savedDX != $dx) $this->speedX = 0; + if($savedDY != $dy) $this->speedY = 0; + if($savedDZ != $dz) $this->speedZ = 0; + + //TODO more stuff + + //TODO block collisions? + //TODO deal fire damage here + + } + public function isInVoid(){ return $this->y < -1.6; } @@ -513,7 +596,7 @@ class Entity extends Position if(!$this->isPlayer()){ $this->updateLast(); $this->updateEntityMovement(); - $update = false; + /*$update = false; if($this->speedX > -self::MIN_POSSIBLE_SPEED && $this->speedX < self::MIN_POSSIBLE_SPEED){ $this->speedX = 0; } @@ -554,37 +637,8 @@ class Entity extends Position if($this->hasGravity){ $this->speedY -= $this->inWater ? 0.02 : ($this->gravity); } - $savedSpeedY = $this->speedY; - $aABB = $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ); - $x0 = floor($aABB->minX); - $x1 = ceil($aABB->maxX); - $y0 = floor($aABB->minY); - $y1 = ceil($aABB->maxY); - $z0 = floor($aABB->minZ); - $z1 = ceil($aABB->maxZ); - - for($x = $x0; $x <= $x1; ++$x){ - for($y = $y0; $y <= $y1; ++$y){ - for($z = $z0; $z <= $z1; ++$z){ - - $b = $this->level->level->getBlockID($x, $y, $z); - if(($b == WATER || $b == STILL_WATER)){ - $water |= $y == ($y1 - 1); - $this->fallDistance = 0; - } - if($b != 0){ - $blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); - foreach($blockBounds as $blockBound){ - $this->speedY = $blockBound->calculateYOffset($this->boundingBox, $this->speedY); - $this->speedX = $blockBound->calculateXOffset($this->boundingBox, $this->speedX); - $this->speedZ = $blockBound->calculateZOffset($this->boundingBox, $this->speedZ); - } - } - } - } - } - $this->boundingBox->offset($this->speedX, $this->speedY, $this->speedZ); - $this->inWater = $water; + $this->move($this->speedX, $this->speedY, $this->speedZ); + $this->inWater = $water; TODO $fallingFlag = $this->onGround || $savedSpeedY != $this->speedY && $savedSpeedY < 0.0; $beforeStepSpeedY = $this->speedY; @@ -636,39 +690,7 @@ class Entity extends Position $this->ai->mobController->setJumping(false); } } - - $support = $savedSpeedY != $this->speedY && $savedSpeedY < 0; - if($this->speedX != 0){ - $this->x += $this->speedX; - $update = true; - } - if($this->speedZ != 0){ - $this->z += $this->speedZ; - $update = true; - } - if($this->speedY != 0){ - $ny = $this->y + $this->speedY; - if($stepSuccess) $this->speedY = $beforeStepSpeedY; - if($this->class === ENTITY_FALLING){ - if($support){ - $this->level->fastSetBlockUpdate($this->x, $this->y, $this->z, $this->data["Tile"], 0); - $this->close(); - return; - } - $id = $this->level->level->getBlockID($this->x, $this->y, $this->z); - if($id > 0 && !StaticBlock::getIsSolid($id) && !StaticBlock::getIsLiquid($id)){ - $this->server->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); - $this->close(); - return; - } - - } - $this->y = $ny; - - $update = true; - } - - $this->onGround = $support; + }*/ if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ @@ -1085,18 +1107,6 @@ class Entity extends Position $this->pitch = $angle["pitch"]; } - public function move(Vector3 $pos, $yaw = 0, $pitch = 0) - { - $this->x += $pos->x; - $this->y += $pos->y; - $this->z += $pos->z; - $this->yaw += $yaw; - $this->yaw %= 360; - $this->pitch += $pitch; - $this->pitch %= 90; - $this->updateAABB(); - } - public function updateAABB() { $this->boundingBox->setBounds( diff --git a/src/entity/Living.php b/src/entity/Living.php index 2b6662cd..da86d7e5 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -3,10 +3,14 @@ abstract class Living extends Entity implements Pathfindable{ public static $despawnMobs, $despawnTimer, $entityPushing = false; - + public $moveStrafing, $moveForward; public $target, $ai; public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower; public $ticksExisted = 0; + + public $landMovementFactor = 0.1; //TODO may not be constant + public $jumpMovementFactor = 0.02; //TODO may not be constant + public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; $this->ai = new EntityAI($this); @@ -99,15 +103,77 @@ abstract class Living extends Entity implements Pathfindable{ $this->ai->mobController->rotateTick(); $this->ai->mobController->movementTick(); + + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; + if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; + if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; + + $this->moveStrafing *= 0.98; + $this->moveForward *= 0.98; + $savedLandFactor = $this->landMovementFactor; + $this->landMovementFactor *= $this->getSpeedModifer(); + $this->moveEntityWithHeading($this->moveStrafing, $this->moveForward); + $this->landMovementFactor = $savedLandFactor; + if(self::$entityPushing){ $this->collideHandler(); } - if($this->onGround){ - //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ - // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); - //} - //$this->pathFollower->followPath(); + } + + public function moveEntityWithHeading($strafe, $forward){ + //TODO lava, water + //If not in lava in water -> + { + $friction = 0.91; + + if($this->onGround){ + $friction = 0.546; + $blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); + + if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91; + } + + $var8 = 0.16277136 / ($friction*$friction*$friction); + + if($this->onGround){ + //TODO if AIEnabled + { + //TODO $var5 = getAIMoveSpeed + } + //else + { + $var5 = $this->landMovementFactor; + } + $var5 *= $var8; + }else{ + $var5 = $this->jumpMovementFactor; + } + + $this->moveFlying($strafe, $forward, $var5); + //1.5.2 recalculates friction, might be not neccessary + $friction = 0.91; + + if($this->onGround){ + $friction = 0.546; + $blockAt = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); + + if($blockAt > 0) $friction = StaticBlock::getSlipperiness($blockAt) * 0.91; + } + + //TODO onLadder + console($this->speedY); + $this->move($this->speedX, $this->speedY, $this->speedZ); + //TODO onLadder + isCollidedHorizonatlly => speedY = 0.2 + + $this->speedY -= 0.08; //gravity + + $this->speedY *= 0.98; + $this->speedX *= $friction; + $this->speedZ *= $friction; } + + //TODO limbYaw - is it even needed? + } public function sendMoveUpdate(){ diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index eec88fe3..0c82b112 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -65,8 +65,8 @@ class MobController ); } - while(self::$ADVANCED){ - if($this->isDangerous($a) || $this->isDangerous($b) || $this->isDangerous($c) || $this->isDangerous($d)){ + while(self::$ADVANCED){ + if($this->isDangerous($a) || $this->isDangerous($b) || $this->isDangerous($c) || $this->isDangerous($d)){ return false; } $id1 = $this->entity->level->level->getBlockID($xf, $this->entity->y + $oy - 1, $zf); From 1ac7179a971fa630b51b3ca36ea5e3537a8bc94b Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 07:36:14 +0300 Subject: [PATCH 09/34] test movement added --- src/entity/Creature.php | 2 +- src/entity/Entity.php | 2 +- src/entity/Living.php | 19 ++- src/entity/ai/MobController.php | 121 ++++++++---------- src/entity/ai/tasks/TaskAttackPlayer.php | 2 +- .../ai/tasks/TaskDestroyServerPerformance.php | 2 +- src/entity/ai/tasks/TaskMate.php | 2 +- src/entity/ai/tasks/TaskPanic.php | 2 +- src/entity/ai/tasks/TaskRandomWalk.php | 21 ++- src/entity/ai/tasks/TaskTempt.php | 2 +- src/entity/animal/Pig.php | 3 + 11 files changed, 91 insertions(+), 87 deletions(-) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index d07a875e..84fec613 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -24,7 +24,7 @@ abstract class Creature extends Living{ } public function getSpeedModifer(){ - return $this->speedModifer * ($this->inPanic ? 1.5 : 1.0); + return $this->speedModifer; } public function getArmorValue(){ return 2; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 9d03314f..709c6ae1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -91,7 +91,7 @@ class Entity extends Position $this->canBeAttacked = false; $this->hasKnockback = false; $this->level = $level; - $this->speedModifer = 0.7; + $this->speedModifer = 1; $this->fallY = false; $this->fallStart = false; $this->server = ServerAPI::request(); diff --git a/src/entity/Living.php b/src/entity/Living.php index da86d7e5..8783a82a 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -10,6 +10,8 @@ abstract class Living extends Entity implements Pathfindable{ public $landMovementFactor = 0.1; //TODO may not be constant public $jumpMovementFactor = 0.02; //TODO may not be constant + public $aiMoveSpeed; + public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; @@ -28,6 +30,17 @@ abstract class Living extends Entity implements Pathfindable{ $this->harm($dmg, "fall"); } } + + public function getAIMoveSpeed(){ + return $this->aiMoveSpeed; + } + + public function setAIMoveSpeed($speed){ + console("set spedd to $speed"); + $this->aiMoveSpeed = $speed; + $this->moveForward = $speed; + } + public function hasPath(){ return $this->path != null; } @@ -100,10 +113,10 @@ abstract class Living extends Entity implements Pathfindable{ if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { $this->ai->updateTasks(); } - $this->ai->mobController->rotateTick(); + $this->ai->mobController->movementTick(); - - + $this->ai->mobController->rotateTick(); + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 0c82b112..82d39a23 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -11,7 +11,7 @@ class MobController FIRE => true, ]; /** - * @var Entity + * @var Living */ public $entity; @@ -20,6 +20,10 @@ class MobController protected $jumping; protected $jumpTimeout; + public $moveToX, $moveToY, $moveToZ; + public $speed; + public $updateMove = false; + public function __construct($e){ $this->entity = $e; } @@ -38,89 +42,66 @@ class MobController return $this->finalHeadYaw === $this->entity->headYaw; } - public function moveNonInstant($x, $y, $z, $camera = true){ - if($x == 0 && $y == 0 && $z == 0){ - return false; - } - - $ox = $x <=> 1; - $oy = $y <=> 1; - $oz = $z <=> 1; - $xf = $this->entity->x + ($this->entity->getSpeedModifer() * $ox * $this->entity->getSpeed()); - $zf = $this->entity->z + ($this->entity->getSpeedModifer() * $oz * $this->entity->getSpeed()); - $a = $b = $c = $d = 0; - if(self::$AUTOJUMP){ - $oy = $this->entity->onGround && ( - StaticBlock::getIsSolid(($a = $this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y), ceil($zf)))) && - !StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y) + 1, ceil($zf))) - || - StaticBlock::getIsSolid(($b = $this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y), $zf - ($oz < 0)))) && - !StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y) + 1, $zf - ($oz < 0))) - || - StaticBlock::getIsSolid(($c = $this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y), $zf - ($oz < 0)))) && - !StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y) + 1, $zf - ($oz < 0))) - || - StaticBlock::getIsSolid(($d = $this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y), ceil($zf)))) && - !StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y) + 1, ceil($zf))) - ); - } - - while(self::$ADVANCED){ - if($this->isDangerous($a) || $this->isDangerous($b) || $this->isDangerous($c) || $this->isDangerous($d)){ - return false; - } - $id1 = $this->entity->level->level->getBlockID($xf, $this->entity->y + $oy - 1, $zf); - if($this->isDangerous($id1)) return false; - if(!StaticBlock::getIsSolid($id1)){ - $id2 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 2, $zf); - $id3 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 3, $zf); - $id4 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 4, $zf); - $s2 = StaticBlock::getIsSolid($id2); - $s3 = StaticBlock::getIsSolid($id3); - $s4 = StaticBlock::getIsSolid($id4); - if($this->isDangerous($id2)) return false; - if($s2) break; //i cant goto label which wasnt declared before - - if($this->isDangerous($id3)) return false; - if($s3) break; - - if($this->isDangerous($id4)) return false; - if($s4) break; - - if(!($this->entity instanceof Chicken)){ - return false; - } - } - break; - } - if($camera) $this->faceEntity($ox, $oy, $oz); - if($this->entity->knockbackTime <= 0 && $this->isRotationCompleted()){ - $this->entity->moveEntityWithOffset($ox, $oy, $oz); - } - return true; + public function setMovingOffset($x, $y, $z, $speed){ + $this->moveToX = $this->entity->x + ($x); + $this->moveToY = $this->entity->y + ($y); + $this->moveToZ = $this->entity->z + ($z); + $this->speed = $speed; + $this->updateMove = true; } public function canJump(){ return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround; } + public static function limitAngle($old, $newA, $limit){ + $v4 = Utils::wrapAngleTo180($newA - $old); + + if($v4 > $limit) $v4 = $limit; + if($v4 < -$limit) $v4 = -$limit; + + return $old + $v4; + } + public function movementTick(){ - if($this->canJump()){ - $this->jumpTimeout = 10; - $this->entity->speedY = 0.50; + $this->entity->moveForward = 0; + if($this->updateMove){ + $this->updateMove = false; + $v1 = floor($this->entity->boundingBox->minY + 0.5); + + $diffX = $this->moveToX - $this->entity->x; + $diffZ = $this->moveToZ - $this->entity->z; + $diffY = $this->moveToY - $v1; + + $v8 = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; + + if($v8 >= 2.500000277905201E-7){ //TODO convert notation + $v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; + console("$v10: ".$v10); + $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); + console("yaw: {$this->entity->yaw}"); + $this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer()); + + if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); + } } - if($this->jumpTimeout > 0) --$this->jumpTimeout; + //TODO handle jumps somewhere + //if($this->canJump()){ + // $this->jumpTimeout = 10; + // $this->entity->speedY = 0.50; + //} + //if($this->jumpTimeout > 0) --$this->jumpTimeout; } public function rotateTick(){ //TODO handle more rotation - $this->entity->lastHeadYaw = $this->entity->headYaw; - $w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); - $w180min = min(abs($w180), 20)*Utils::getSign($w180); - $this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); + //$this->entity->lastHeadYaw = $this->entity->headYaw; + //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); + //$w180min = min(abs($w180), 20)*Utils::getSign($w180); + //$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); } public function moveTo($x, $y, $z, $camera = true){ - return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z), $camera); + //TODO fix return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z), $camera); } public function faceEntity($x, $y, $z){ diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index e6ec7e1d..5f886853 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -16,7 +16,7 @@ class TaskAttackPlayer extends TaskTempt return; } - $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); + //TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); $ai->mobController->lookOn($this->target); $mult = $ai->entity instanceof Spider ? 1 : 2; $radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult); diff --git a/src/entity/ai/tasks/TaskDestroyServerPerformance.php b/src/entity/ai/tasks/TaskDestroyServerPerformance.php index a73804d3..8903f303 100644 --- a/src/entity/ai/tasks/TaskDestroyServerPerformance.php +++ b/src/entity/ai/tasks/TaskDestroyServerPerformance.php @@ -21,7 +21,7 @@ class TaskDestroyServerPerformance extends TaskTempt return; } if(Utils::distance_noroot($this->target, $ai->entity) > 100){ - $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false); + //TODO fix $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false); }else{ } diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php index c0e4842f..8f8a093f 100644 --- a/src/entity/ai/tasks/TaskMate.php +++ b/src/entity/ai/tasks/TaskMate.php @@ -24,7 +24,7 @@ class TaskMate extends \TaskBase $this->onEnd($ai); return; } - $ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false); + //TODO fix $ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false); $ai->mobController->lookOn($movTo); --$this->selfCounter; diff --git a/src/entity/ai/tasks/TaskPanic.php b/src/entity/ai/tasks/TaskPanic.php index c3f52233..55c426d5 100644 --- a/src/entity/ai/tasks/TaskPanic.php +++ b/src/entity/ai/tasks/TaskPanic.php @@ -28,7 +28,7 @@ class TaskPanic extends TaskBase { --$this->selfCounter; if($this->selfCounter % 20 === 0) $this->regenerateRandXZ(); - $ai->mobController->moveNonInstant($this->randX, 0, $this->randZ); + //TODO fix $ai->mobController->moveNonInstant($this->randX, 0, $this->randZ); } public function canBeExecuted(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index e4bae095..9a32b16b 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -3,10 +3,15 @@ class TaskRandomWalk extends TaskBase { private $x, $z; + public $moveSpeed; + public function __construct($moveSpeed){ + $this->moveSpeed = 0.25; + } + public function onStart(EntityAI $ai) { - $this->x = mt_rand(-16, 16); - $this->z = mt_rand(-16, 16); + $this->x = mt_rand(-10, 10); + $this->z = mt_rand(-10, 10); if($this->x === 0 && $this->z === 0){ $this->reset(); return false; @@ -26,15 +31,17 @@ class TaskRandomWalk extends TaskBase return false; //TODO Better way: block movement } --$this->selfCounter; - $ai->mobController->moveNonInstant($this->x, 0, $this->z); + $ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->moveSpeed); + //TODO fix $ai->mobController->moveNonInstant($this->x, 0, $this->z); } public function canBeExecuted(EntityAI $ai) { - if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { - return false; - } // i really need mutexBits - return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; + return true; + //if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { + // return false; + //} // i really need mutexBits + //return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; } diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php index 3afae162..0106c95a 100644 --- a/src/entity/ai/tasks/TaskTempt.php +++ b/src/entity/ai/tasks/TaskTempt.php @@ -23,7 +23,7 @@ class TaskTempt extends TaskBase return; } - $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); + //TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); $ai->mobController->lookOn($this->target); } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index d0fe432b..96a3e468 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -9,6 +9,9 @@ class Pig extends Animal implements Rideable{ $this->server = ServerAPI::request(); $this->setName("Pig"); $this->setSpeed(0.25); + + $this->ai->addTask(new TaskRandomWalk(0.25)); + } /** * @return boolean From 9d6322f58732ad372bcbd1eff5abb17197a6d545 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 08:23:37 +0300 Subject: [PATCH 10/34] head yaw fixed --- src/entity/Entity.php | 1 + src/entity/Living.php | 4 +++ src/entity/ai/MobController.php | 42 ++++++++++++++++++++++++-- src/entity/ai/tasks/TaskRandomWalk.php | 6 ++-- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 709c6ae1..367818de 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1242,6 +1242,7 @@ class Entity extends Position $this->last[3] = $this->yaw; $this->last[4] = $this->pitch; $this->last[5] = microtime(true); + $this->lastHeadYaw = $this->headYaw; } public function getPosition($round = false) diff --git a/src/entity/Living.php b/src/entity/Living.php index 8783a82a..67b864b8 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -12,6 +12,7 @@ abstract class Living extends Entity implements Pathfindable{ public $jumpMovementFactor = 0.02; //TODO may not be constant public $aiMoveSpeed; + public $renderYawOffset = 0.0; //used by head rotation, TODO better name public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; @@ -131,6 +132,9 @@ abstract class Living extends Entity implements Pathfindable{ if(self::$entityPushing){ $this->collideHandler(); } + + //Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too + $this->ai->mobController->updateHeadYaw(); } public function moveEntityWithHeading($strafe, $forward){ diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 82d39a23..58aceb51 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -17,6 +17,9 @@ class MobController public $finalYaw, $finalPitch, $finalHeadYaw; + public $headYaw; + public $someTicker = 0; + protected $jumping; protected $jumpTimeout; @@ -53,7 +56,14 @@ class MobController public function canJump(){ return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround; } - + public static function limitAngle2($old, $newA, $limit){ + $v4 = Utils::wrapAngleTo180($old - $newA); + + if($v4 < -$limit) $v4 = -$limit; + if($v4 >= $limit) $v4 = $limit; + + return $old - $v4; + } public static function limitAngle($old, $newA, $limit){ $v4 = Utils::wrapAngleTo180($newA - $old); @@ -77,9 +87,7 @@ class MobController if($v8 >= 2.500000277905201E-7){ //TODO convert notation $v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; - console("$v10: ".$v10); $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); - console("yaw: {$this->entity->yaw}"); $this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer()); if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); @@ -93,7 +101,35 @@ class MobController //if($this->jumpTimeout > 0) --$this->jumpTimeout; } + public function updateHeadYaw(){ + $diffX = $this->entity->x - $this->entity->lastX; + $diffZ = $this->entity->z - $this->entity->lastZ; + if(($diffX*$diffX + $diffZ*$diffZ) > 2.500000277905201E-7){ //TODO convert notation + $this->entity->renderYawOffset = $this->entity->yaw; + $this->entity->headYaw = self::limitAngle2($this->entity->renderYawOffset, $this->entity->headYaw, 75); + $this->headYaw = $this->entity->headYaw; + $this->someTicker = 0; + }else{ + $v5 = 75; + if(abs($this->entity->headYaw - $this->headYaw) > 15){ + $this->someTicker = 0; + $this->headYaw = $this->entity->headYaw; + }else{ + ++$this->someTicker; + + if($this->someTicker > 10){ + $v5 = max(1 - ($this->someTicker - 10) / 10, 0) * 75; + } + } + $this->entity->renderYawOffset = self::limitAngle2($this->entity->headYaw, $this->entity->renderYawOffset, $v5); + } + $this->entity->headYaw = $this->entity->renderYawOffset; + } + public function rotateTick(){ //TODO handle more rotation + + + //$this->entity->lastHeadYaw = $this->entity->headYaw; //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); //$w180min = min(abs($w180), 20)*Utils::getSign($w180); diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 9a32b16b..05aac7f7 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -10,13 +10,13 @@ class TaskRandomWalk extends TaskBase public function onStart(EntityAI $ai) { - $this->x = mt_rand(-10, 10); - $this->z = mt_rand(-10, 10); + $this->x = mt_rand(-1, 1); + $this->z = mt_rand(-1, 1); if($this->x === 0 && $this->z === 0){ $this->reset(); return false; } - $this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z))); + $this->selfCounter = mt_rand(80, 100); } public function onEnd(EntityAI $ai) From 50c0bf81045257fed1f0bf1c10b9b9d772d20f31 Mon Sep 17 00:00:00 2001 From: GameHerobrine <55945072+GameHerobrine@users.noreply.github.com> Date: Tue, 9 Jan 2024 08:28:49 +0300 Subject: [PATCH 11/34] fixed invalid variable access --- src/API/PlayerAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index d8797ea5..661c7708 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -480,7 +480,7 @@ class PlayerAPI{ } $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); $this->server->api->entity->remove($player->eid); - unset($player->level->players[$this->CID]); + unset($player->level->players[$player->CID]); if($player->entity instanceof Entity){ unset($player->entity->player); //unset($player->entity); From 88cec22404c2f44c238c523048341933543dbb9c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 08:51:15 +0300 Subject: [PATCH 12/34] real head yaw fixed --- src/API/PlayerAPI.php | 2 +- src/entity/Living.php | 16 ++++++++++------ src/entity/ai/MobController.php | 26 +++++++++++++++++++++++++- src/utils/Utils.php | 7 +++++-- 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/src/API/PlayerAPI.php b/src/API/PlayerAPI.php index d8797ea5..661c7708 100644 --- a/src/API/PlayerAPI.php +++ b/src/API/PlayerAPI.php @@ -480,7 +480,7 @@ class PlayerAPI{ } $this->server->query("DELETE FROM players WHERE name = '" . $player->username . "';"); $this->server->api->entity->remove($player->eid); - unset($player->level->players[$this->CID]); + unset($player->level->players[$player->CID]); if($player->entity instanceof Entity){ unset($player->entity->player); //unset($player->entity); diff --git a/src/entity/Living.php b/src/entity/Living.php index 67b864b8..0f7011cd 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -135,6 +135,8 @@ abstract class Living extends Entity implements Pathfindable{ //Yaw rotation in 1.5 is handled in a bit different place but hopefully this will work too $this->ai->mobController->updateHeadYaw(); + + } public function moveEntityWithHeading($strafe, $forward){ @@ -153,15 +155,17 @@ abstract class Living extends Entity implements Pathfindable{ $var8 = 0.16277136 / ($friction*$friction*$friction); if($this->onGround){ + //all mobs have ai enabled in nc for now + $var5 = $this->getAIMoveSpeed(); //TODO if AIEnabled - { + //{ //TODO $var5 = getAIMoveSpeed - } + //} //else - { - $var5 = $this->landMovementFactor; - } - $var5 *= $var8; + //{ + // $var5 = $this->landMovementFactor; + //} + //$var5 *= $var8; }else{ $var5 = $this->jumpMovementFactor; } diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 58aceb51..86725b9a 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -26,6 +26,8 @@ class MobController public $moveToX, $moveToY, $moveToZ; public $speed; public $updateMove = false; + public $isLooking = false; + public function __construct($e){ $this->entity = $e; @@ -123,12 +125,34 @@ class MobController } $this->entity->renderYawOffset = self::limitAngle2($this->entity->headYaw, $this->entity->renderYawOffset, $v5); } - $this->entity->headYaw = $this->entity->renderYawOffset; } public function rotateTick(){ //TODO handle more rotation + $this->entity->pitch = 0; + if($this->isLooking){ + $this->isLooking = false; + //TODO + }else{ + $this->entity->headYaw = self::limitAngle($this->entity->headYaw, $this->entity->renderYawOffset, 10); + } + /* Some stuff for pathfinder - nc doesnt have it now + * float var11 = MathHelper.wrapAngleTo180_float(this.entity.rotationYawHead - this.entity.renderYawOffset); + + if (!this.entity.getNavigator().noPath()) + { + if (var11 < -75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset - 75.0F; + } + + if (var11 > 75.0F) + { + this.entity.rotationYawHead = this.entity.renderYawOffset + 75.0F; + } + } + */ //$this->entity->lastHeadYaw = $this->entity->headYaw; //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index 7c7247ff..5c8983ec 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -69,8 +69,11 @@ class Utils{ public static function wrapAngleTo180($angle) { - $angle = fmod($angle+180, 360); - return $angle < 0 ? $angle + 360 : $angle - 180; + $angle = fmod($angle, 360); + + if($angle >= 180) $angle -= 360; + if($angle < -180) $angle += 360; + return $angle; } public static function getSign($v){ return $v <=> 0; From 46898097942f00ce2faa1241e9c66b7bc94d4afd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 09:26:02 +0300 Subject: [PATCH 13/34] randomwalking for all mobs --- src/entity/Creature.php | 2 +- src/entity/Monster.php | 2 +- src/entity/ai/tasks/TaskRandomWalk.php | 2 +- src/entity/animal/Chicken.php | 17 +++++++++++++---- src/entity/animal/Cow.php | 2 +- src/entity/animal/Sheep.php | 3 +++ src/entity/monster/Creeper.php | 2 ++ src/entity/monster/Skeleton.php | 3 ++- src/entity/monster/Spider.php | 1 + src/entity/monster/Zombie.php | 2 ++ 10 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 84fec613..5aacff56 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -24,7 +24,7 @@ abstract class Creature extends Living{ } public function getSpeedModifer(){ - return $this->speedModifer; + return 0.7; //$this->speedModifer; //should be 0.7? } public function getArmorValue(){ return 2; diff --git a/src/entity/Monster.php b/src/entity/Monster.php index 969de944..c9831eba 100644 --- a/src/entity/Monster.php +++ b/src/entity/Monster.php @@ -2,7 +2,7 @@ class Monster extends Creature{ public function __construct(Level $level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); - $this->ai->addTask(new TaskAttackPlayer()); + //$this->ai->addTask(new TaskAttackPlayer()); } public function getAttackDamage(){ diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 05aac7f7..685af3c5 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -5,7 +5,7 @@ class TaskRandomWalk extends TaskBase private $x, $z; public $moveSpeed; public function __construct($moveSpeed){ - $this->moveSpeed = 0.25; + $this->moveSpeed = $moveSpeed; } public function onStart(EntityAI $ai) diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 7d60201a..d775a94c 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -10,19 +10,28 @@ class Chicken extends Animal{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setName('Chicken'); $this->setSpeed(0.25); + + $this->ai->addTask(new TaskRandomWalk(0.25)); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; } + public function updateEntityMovement(){ + parent::updateEntityMovement(); + + if($this->onGround && $this->speedY < 0) $this->speedY *= 0.6; + + if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){ + $this->dropAnEgg(); + $this->timeUntilEgg = mt_rand(0,6000) + 6000; + } + } + public function fall(){} //chickens have no fall dmg? public function update($now){ parent::update($now); - if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){ - $this->dropAnEgg(); - $this->timeUntilEgg = mt_rand(0,6000) + 6000; - } } public function dropAnEgg(){ diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index 814251ac..b185db20 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -7,7 +7,7 @@ class Cow extends Animal{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Cow"); - $this->setSpeed(0.2); + $this->ai->addTask(new TaskRandomWalk(0.2)); } public function isFood($id){ diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index f1957bf7..720b88b3 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -12,7 +12,10 @@ class Sheep extends Animal{ $this->data["Sheared"] = isset($this->data["Sheared"]) ? $this->data["Sheared"] : 0; $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->setSpeed(0.25); + + $this->ai->addTask(new TaskRandomWalk(0.25)); //in 1.5.2 it is 0.23, but according to ida it should be 1.0 which is 0.25 if converted to 1.5.2? $this->ai->addTask(new TaskEatTileGoal()); + } public function createSaveData(){ diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index c1c5c38d..68b67f80 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -11,6 +11,8 @@ class Creeper extends Monster{ $this->ignited = 0; $this->setSpeed(0.25); $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; + + $this->ai->addTask(new TaskRandomWalk(0.25)); } public function getAttackDamage(){ diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 23a23d13..b9a0344e 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -8,7 +8,8 @@ class Skeleton extends Monster{ $this->setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); //$this->ai->addTask(new TaskDestroyServerPerformance()); - $this->setSpeed(0.25); + + $this->ai->addTask(new TaskRandomWalk(0.25)); } public function getAttackDamage(){ diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 2c078b92..6af9c1c5 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -7,6 +7,7 @@ class Spider extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setName("Spider"); $this->setSpeed(0.3); + $this->ai->addTask(new TaskRandomWalk(0.25)); //TODO this mob doesnt use newAI -> speed is not correct } public function getDrops(){ diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index f34e1bbf..1497f048 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -7,6 +7,8 @@ class Zombie extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic"); $this->setName("Zombie"); $this->setSpeed(0.23); + + $this->ai->addTask(new TaskRandomWalk(0.25)); } public function getArmorValue(){ From bcf7ef7c794175ce9831c3cec31c7f8a0a2656b6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 09:40:27 +0300 Subject: [PATCH 14/34] ported step into new moveEntity function] --- src/entity/Entity.php | 97 +++++++++++++++++++++---------------------- 1 file changed, 47 insertions(+), 50 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 367818de..f31ff819 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -552,6 +552,52 @@ class Entity extends Position $this->boundingBox->offset($dx, $dy, $dz); $fallingFlag = $this->onGround || $savedDY != $dy && $savedDY < 0; + if($this->stepHeight > 0 && $fallingFlag && /*($this->ySize < 0.05) && TODO ySuze*/ ($savedDX != $dx || $savedDZ != $dz)){ + $cx = $dx; + $cy = $dy; + $cz = $dz; + + $dx = $savedDX; + $dy = $this->stepHeight; + $dz = $savedDZ; + $aabb1 = clone $this->boundingBox; + $this->boundingBox->setBB($oldBB); + + $aaBBs = $this->level->getCubes($this, $this->boundingBox->addCoord($dx, $dy, $dz)); + + foreach($aaBBs as $bb){ //TODO optimize + $dy = $bb->calculateYOffset($this->boundingBox, $dy); + } + $this->boundingBox->offset(0, $dy, 0); + + foreach($aaBBs as $bb){ + $dx = $bb->calculateXOffset($this->boundingBox, $dx); + } + $this->boundingBox->offset($dx, 0, 0); + + foreach($aaBBs as $bb){ + $dz = $bb->calculateZOffset($this->boundingBox, $dz); + } + $this->boundingBox->offset(0, 0, $dz); + + $dy = -$this->stepHeight; + foreach($aaBBs as $bb){ //TODO optimize + $dy = $bb->calculateYOffset($this->boundingBox, $dy); + } + $this->boundingBox->offset(0, $dy, 0); + + if ($cx*$cx + $cz*$cz >= $dx*$dx + $dz*$dz) + { + $dx = $cx; + $dy = $cy; + $dz = $cz; + $this->boundingBox->setBB($aabb1); + }//else{ TODO autojump + // $stepSuccess = true; + //} + + } + //TODO step $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; $this->y = $this->boundingBox->minY; //TODO + $this->yOffset - //TODO $this->ySize; @@ -565,6 +611,7 @@ class Entity extends Position if($savedDY != $dy) $this->speedY = 0; if($savedDZ != $dz) $this->speedZ = 0; + //TODO more stuff //TODO block collisions? @@ -632,56 +679,6 @@ class Entity extends Position $ultraMegaOldBB = clone $this->boundingBox; $stepSuccess = false; $beforeStepSpeedY = 0; - if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM || ($this->class === ENTITY_OBJECT && $this->type === OBJECT_PRIMEDTNT) || $this->class === ENTITY_FALLING){ - $water = false; - if($this->hasGravity){ - $this->speedY -= $this->inWater ? 0.02 : ($this->gravity); - } - $this->move($this->speedX, $this->speedY, $this->speedZ); - $this->inWater = $water; TODO - $fallingFlag = $this->onGround || $savedSpeedY != $this->speedY && $savedSpeedY < 0.0; - $beforeStepSpeedY = $this->speedY; - - if($this->stepHeight > 0 && $fallingFlag && ($this->speedX != $savedSpeedX || $this->speedZ != $savedSpeedZ)){ - $cx = $this->speedX; - $cy = $this->speedY; - $cz = $this->speedZ; - - $this->speedX = $savedSpeedX; - $this->speedY = $this->stepHeight; - $this->speedZ = $savedSpeedZ; - - $aabb = clone $this->boundingBox; - $this->boundingBox->setBB($ultraMegaOldBB); - $aaBBs = $this->level->getCubes($this, $this->boundingBox->addCoord($this->speedX, $this->speedY, $this->speedZ)); - - foreach($aaBBs as $bb){ //TODO optimize - $this->speedY = $bb->calculateYOffset($this->boundingBox, $this->speedY); - } - $this->boundingBox->offset(0, $this->speedY, 0); - - foreach($aaBBs as $bb){ - $this->speedX = $bb->calculateXOffset($this->boundingBox, $this->speedX); - } - $this->boundingBox->offset($this->speedX, 0, 0); - - foreach($aaBBs as $bb){ - $this->speedZ = $bb->calculateZOffset($this->boundingBox, $this->speedZ); - } - $this->boundingBox->offset(0, 0, $this->speedZ); - - if ($cx*$cx + $cz*$cz >= $this->speedX*$this->speedX + $this->speedZ*$this->speedZ) - { - $this->speedX = $cx; - $this->speedY = $cy; - $this->speedZ = $cz; - $this->boundingBox->setBB($aabb); - }else{ - $stepSuccess = true; - } - } - - } if($this->enableAutojump && $this instanceof Creature){ if(!$stepSuccess && $fallingFlag && ($this->speedX != $savedSpeedX || $this->speedZ != $savedSpeedZ)){ From 2636aa985d949b3825b3ed2dcd27c37ac0987a14 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 10:18:22 +0300 Subject: [PATCH 15/34] fixed more tasks --- src/entity/Entity.php | 6 ++++-- src/entity/Living.php | 6 ++++-- src/entity/ai/MobController.php | 24 +++++++++++++++++++--- src/entity/ai/tasks/TaskLookAtPlayer.php | 12 +++++++---- src/entity/ai/tasks/TaskPanic.php | 12 ++++++++--- src/entity/ai/tasks/TaskRandomWalk.php | 9 ++++---- 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/entity/object/FallingSand.php | 1 + src/entity/object/ItemEntity.php | 1 + src/entity/object/Minecart.php | 1 + src/entity/object/PrimedTNT.php | 2 ++ src/material/item/generic/SpawnEggItem.php | 2 +- 15 files changed, 64 insertions(+), 20 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index f31ff819..ec0e9af1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -84,6 +84,8 @@ class Entity extends Position public $stepHeight = 0.5; public $enableAutojump = false; + public $yOffset = 0.0; + public $noClip = false; function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { $this->random = new Random(); @@ -552,7 +554,7 @@ class Entity extends Position $this->boundingBox->offset($dx, $dy, $dz); $fallingFlag = $this->onGround || $savedDY != $dy && $savedDY < 0; - if($this->stepHeight > 0 && $fallingFlag && /*($this->ySize < 0.05) && TODO ySuze*/ ($savedDX != $dx || $savedDZ != $dz)){ + if($this->stepHeight > 0 && $fallingFlag && ($savedDX != $dx || $savedDZ != $dz)){ $cx = $dx; $cy = $dy; $cz = $dz; @@ -600,7 +602,7 @@ class Entity extends Position //TODO step $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; - $this->y = $this->boundingBox->minY; //TODO + $this->yOffset - //TODO $this->ySize; + $this->y = $this->boundingBox->minY + $this->yOffset;// - //TODO - used when player is sneaking $this->ySize; $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; //$this.isCollidedHorizontally = savedDX != dx || savedDZ != dz; //$this.isCollidedVertically = savedDY != dy; diff --git a/src/entity/Living.php b/src/entity/Living.php index 0f7011cd..1b6ba5b6 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -36,8 +36,11 @@ abstract class Living extends Entity implements Pathfindable{ return $this->aiMoveSpeed; } + public function getVerticalFaceSpeed(){ + return 40; //unused in 0.8 but may be useful in 0.9/0.10 + } + public function setAIMoveSpeed($speed){ - console("set spedd to $speed"); $this->aiMoveSpeed = $speed; $this->moveForward = $speed; } @@ -182,7 +185,6 @@ abstract class Living extends Entity implements Pathfindable{ } //TODO onLadder - console($this->speedY); $this->move($this->speedX, $this->speedY, $this->speedZ); //TODO onLadder + isCollidedHorizonatlly => speedY = 0.2 diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 86725b9a..fbe744fc 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -26,6 +26,9 @@ class MobController public $moveToX, $moveToY, $moveToZ; public $speed; public $updateMove = false; + + public $lookX, $lookY, $lookZ; + public $deltaLookYaw, $deltaLookPitch; public $isLooking = false; @@ -132,7 +135,17 @@ class MobController $this->entity->pitch = 0; if($this->isLooking){ $this->isLooking = false; - //TODO + + $diffX = $this->lookX - $this->entity->x; + $diffY = $this->lookY - ($this->entity->y + $this->entity->getEyeHeight()); + $diffZ = $this->lookZ - $this->entity->z; + $distance = sqrt($diffX*$diffX + $diffZ*$diffZ); + + $v9 = (atan2($diffZ, $diffX)*180 / M_PI) - 90; + $v10 = -(atan2($diffY, $distance)*180 / M_PI); + + $this->entity->pitch = self::limitAngle($this->entity->pitch, $v10, $this->deltaLookPitch); + $this->entity->headYaw = self::limitAngle($this->entity->headYaw, $v9, $this->deltaLookYaw); }else{ $this->entity->headYaw = self::limitAngle($this->entity->headYaw, $this->entity->renderYawOffset, 10); } @@ -160,8 +173,13 @@ class MobController //$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); } - public function moveTo($x, $y, $z, $camera = true){ - //TODO fix return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z), $camera); + public function setLookPosition($posX, $posY, $posZ, $lookYaw, $lookPitch){ + $this->lookX = $posX; + $this->lookY = $posY; + $this->lookZ = $posZ; + $this->deltaLookYaw = $lookYaw; + $this->deltaLookPitch = $lookPitch; + $this->isLooking = true; } public function faceEntity($x, $y, $z){ diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php index aa8ab776..522bcc8b 100644 --- a/src/entity/ai/tasks/TaskLookAtPlayer.php +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -2,7 +2,11 @@ class TaskLookAtPlayer extends TaskBase{ public $target = false; - private $state, $yaw, $pitch; + public $distance; + public function __construct($distance){ + $this->distance = $distance; + } + public function canBeExecuted(EntityAI $ai){ return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath(); } @@ -37,14 +41,13 @@ class TaskLookAtPlayer extends TaskBase{ } public function onStart(EntityAI $ai){ - $this->target = $this->findTarget($ai->entity, 6); //TODO max distance for different mobs + $this->target = $this->findTarget($ai->entity, $this->distance); if(!($this->target instanceof Entity) || !$this->target->isPlayer()){ $this->reset(); $this->onEnd($ai); return; } - $this->yaw = $ai->entity->yaw; - $this->pitch = $ai->entity->pitch; + $this->selfCounter = mt_rand(20, 60); } @@ -54,6 +57,7 @@ class TaskLookAtPlayer extends TaskBase{ $this->onEnd($ai); return; } + $ai->mobController->setLookPosition($this->target->x, $this->target->y + $this->target->getEyeHeight(), $this->target->z, 10, $ai->entity->getVerticalFaceSpeed()); $ai->mobController->lookOn($this->target); $this->selfCounter--; } diff --git a/src/entity/ai/tasks/TaskPanic.php b/src/entity/ai/tasks/TaskPanic.php index 55c426d5..5793708f 100644 --- a/src/entity/ai/tasks/TaskPanic.php +++ b/src/entity/ai/tasks/TaskPanic.php @@ -3,6 +3,12 @@ class TaskPanic extends TaskBase { protected $randX = 2, $randZ = 2; + + public $moveSpeed; + public function __construct($moveSpeed){ + $this->moveSpeed = $moveSpeed; + } + public function onStart(EntityAI $ai) { $this->selfCounter = 60; @@ -16,8 +22,8 @@ class TaskPanic extends TaskBase } public function regenerateRandXZ(){ - $this->randX = (mt_rand(0, 1) ? -1 : 1) * 2; - $this->randZ = (mt_rand(0, 1) ? -1 : 1) * 2; + $this->randX = (mt_rand(0, 1) ? -1 : 1); + $this->randZ = (mt_rand(0, 1) ? -1 : 1); } public function reset(){ @@ -28,7 +34,7 @@ class TaskPanic extends TaskBase { --$this->selfCounter; if($this->selfCounter % 20 === 0) $this->regenerateRandXZ(); - //TODO fix $ai->mobController->moveNonInstant($this->randX, 0, $this->randZ); + $ai->mobController->setMovingOffset($this->randX, 0, $this->randZ, $this->moveSpeed); } public function canBeExecuted(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 685af3c5..28f8a5c2 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -37,11 +37,10 @@ class TaskRandomWalk extends TaskBase public function canBeExecuted(EntityAI $ai) { - return true; - //if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { - // return false; - //} // i really need mutexBits - //return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; + if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { + return false; + } // i really need mutexBits + return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index d775a94c..4d3dafe5 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -12,6 +12,8 @@ class Chicken extends Animal{ $this->setSpeed(0.25); $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskLookAtPlayer(6)); + $this->ai->addTask(new TaskPanic(0.375)); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index b185db20..baff016b 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -8,6 +8,8 @@ class Cow extends Animal{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Cow"); $this->ai->addTask(new TaskRandomWalk(0.2)); + $this->ai->addTask(new TaskLookAtPlayer(6)); + $this->ai->addTask(new TaskPanic(0.375)); } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 96a3e468..3083da60 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -11,6 +11,8 @@ class Pig extends Animal implements Rideable{ $this->setSpeed(0.25); $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskLookAtPlayer(6)); + $this->ai->addTask(new TaskPanic(0.375)); } /** diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 720b88b3..ecaba25e 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -14,6 +14,8 @@ class Sheep extends Animal{ $this->setSpeed(0.25); $this->ai->addTask(new TaskRandomWalk(0.25)); //in 1.5.2 it is 0.23, but according to ida it should be 1.0 which is 0.25 if converted to 1.5.2? + $this->ai->addTask(new TaskLookAtPlayer(6)); + $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskEatTileGoal()); } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 108a6ee1..42ad573c 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -8,6 +8,7 @@ class FallingSand extends Entity{ $this->setHealth(PHP_INT_MAX, "generic"); $this->height = 0.98; $this->width = 0.98; + $this->yOffset = $this->height / 2; $this->hasGravity = true; $this->gravity = 0.04; } diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index e138dc7a..bc1120c3 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -10,6 +10,7 @@ class ItemEntity extends Entity{ { parent::__construct($level, $eid, $class, $type, $data); $this->setSize(0.25, 0.25); + $this->yOffset = $this->height / 2; if(isset($data["item"]) and ($data["item"] instanceof Item)){ $this->meta = $this->data["item"]->getMetadata(); $this->stack = $this->data["item"]->count; diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index c257fcf4..6bd37451 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -65,6 +65,7 @@ class Minecart extends Vehicle{ $this->setHealth(1, "generic"); //orig: 3 $this->width = 0.98; $this->height = 0.7; + $this->yOffset = $this->height / 2; $this->update(); } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index ca9cb62b..27768a05 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -10,6 +10,8 @@ class PrimedTNT extends Entity{ } $this->hasGravity = true; $this->gravity = 0.04; + $this->setSize(0.98, 0.98); + $this->yOffset = $this->height / 2; $this->setHealth(10000000, "generic"); $this->server->schedule(5, [$this, "updateFuse"], [], true); } diff --git a/src/material/item/generic/SpawnEggItem.php b/src/material/item/generic/SpawnEggItem.php index 3aac8d9c..f41e9a01 100644 --- a/src/material/item/generic/SpawnEggItem.php +++ b/src/material/item/generic/SpawnEggItem.php @@ -13,7 +13,7 @@ class SpawnEggItem extends Item{ "x" => $block->x + 0.5, "y" => $block->y, "z" => $block->z + 0.5, - "IsBaby" => $ageable ? Utils::chance(5) ? 1 : 0 : 0 + "IsBaby" => $ageable ? mt_rand(0, 5) == 0 ? 1 : 0 : 0 ); $e = ServerAPI::request()->api->entity->add($block->level, ENTITY_MOB, $this->meta, $data); ServerAPI::request()->api->entity->spawnToAll($e); From 403eb71aa6ae897fc38b4d7165c2d49eec1264d1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 9 Jan 2024 10:49:17 +0300 Subject: [PATCH 16/34] fixed item and tnt movement(kinda) --- src/Player.php | 2 +- src/entity/object/ItemEntity.php | 34 ++++++++++++++++++++++++++ src/entity/object/PrimedTNT.php | 41 ++++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/Player.php b/src/Player.php index 647092d7..049d0d26 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1218,7 +1218,7 @@ class Player{ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; - $move->y = $e instanceof ItemEntity ? $e->boundingBox->maxY : $e->y; //TODO fix items getting into farmland in client side somehow else + $move->y = $e->y; //TODO fix items getting into farmland in client side somehow else $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index bc1120c3..81488e33 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -24,4 +24,38 @@ class ItemEntity extends Entity{ $this->delayBeforePickup = 20; $this->stepHeight = 0; } + + public function updateEntityMovement(){ + //TODO it is weird on client side for some reason + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; + if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; + if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; + + $this->speedY -= 0.04; + $this->noClip = false; //TODO pushOutofBlocks + $this->move($this->speedX, $this->speedY, $this->speedZ); + + $var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ; + if($var1/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) + //TODO check material + + //TODO search for other items + } + + $friction = 0.98; + + if($this->onGround){ + $friction = 0.588; + $v3 = $this->level->level->getBlockID($this->x, floor($this->boundingBox->minY) - 1, $this->z); + if($v3 > 0) $friction = StaticBlock::getSlipperiness($v3); + } + + $this->speedX *= $friction; + $this->speedY *= 0.98; + $this->speedZ *= $friction; + + //if($this->onGround) $this->speedY *= -0.5; + + //TODO ++age; despawn after age >= 6000; + } } diff --git a/src/entity/object/PrimedTNT.php b/src/entity/object/PrimedTNT.php index 27768a05..ee0a2ded 100644 --- a/src/entity/object/PrimedTNT.php +++ b/src/entity/object/PrimedTNT.php @@ -13,12 +13,12 @@ class PrimedTNT extends Entity{ $this->setSize(0.98, 0.98); $this->yOffset = $this->height / 2; $this->setHealth(10000000, "generic"); - $this->server->schedule(5, [$this, "updateFuse"], [], true); + //$this->server->schedule(5, [$this, "updateFuse"], [], true); } public function getMetadata(){ $d = parent::getMetadata(); - $d[16]["value"] = (int) max(0, $this->data["fuse"] - (microtime(true) - $this->spawntime) * 20); + $d[16]["value"] = (int) $this->data["fuse"]; return $d; } public function createSaveData(){ @@ -29,7 +29,40 @@ class PrimedTNT extends Entity{ return $data; } - public function updateFuse(){ + + public function update($now){ + if($this->closed) return; + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + + $this->speedY -= 0.04; + $this->move($this->speedX, $this->speedY, $this->speedZ); + $this->speedX *- 0.98; + $this->speedY *= 0.98; + $this->speedZ *= 0.98; + + if($this->onGround){ + $this->speedX *= 0.6; + $this->speedZ *= 0.6; + + $this->speedY *= -0.5; + } + + $tickDiff = ($now - $this->lastUpdate) / 0.05; + + $this->data["fuse"] -= $tickDiff; + $this->updateMetadata(); + if($this->data["fuse"] <= 0){ + $this->close(); + $explosion = new Explosion($this, $this->data["power"]); + $explosion->explode(); + } + + $this->lastUpdate = $now; + } + + /*public function updateFuse(){ if($this->closed === true){ return false; } @@ -41,7 +74,7 @@ class PrimedTNT extends Entity{ $explosion->explode(); } } - } + }*/ public function spawn($player){ $pk = new AddEntityPacket; From cf1ab454cf026c783558dea8c08c76026a9a08b0 Mon Sep 17 00:00:00 2001 From: GameHerobrine <55945072+GameHerobrine@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:40:06 +0300 Subject: [PATCH 17/34] fixed incorrect radius --- src/API/EntityAPI.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index f6e63043..a953a91a 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -211,7 +211,7 @@ class EntityAPI{ $minChunkX = ((int)($center->x - $radius)) >> 4; $minChunkZ = ((int)($center->z - $radius)) >> 4; $maxChunkX = ((int)($center->x + $radius)) >> 4; - $maxChunkZ = ((int)($center->z - $radius)) >> 4; + $maxChunkZ = ((int)($center->z + $radius)) >> 4; $ents = []; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ From bf1169b3f239e917e15266a1a957f246603a948d Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 12 Jan 2024 04:59:29 +0300 Subject: [PATCH 18/34] fixed entities falling through the ground --- src/API/EntityAPI.php | 2 +- src/Player.php | 2 +- src/PocketMinecraftServer.php | 2 +- src/entity/Entity.php | 50 +++++++++++++----------- src/entity/object/ItemEntity.php | 16 +++++--- src/material/block/solid/LeavesBlock.php | 5 +++ src/world/Level.php | 27 ++++++++++--- 7 files changed, 66 insertions(+), 38 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index f6e63043..a953a91a 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -211,7 +211,7 @@ class EntityAPI{ $minChunkX = ((int)($center->x - $radius)) >> 4; $minChunkZ = ((int)($center->z - $radius)) >> 4; $maxChunkX = ((int)($center->x + $radius)) >> 4; - $maxChunkZ = ((int)($center->z - $radius)) >> 4; + $maxChunkZ = ((int)($center->z + $radius)) >> 4; $ents = []; //TODO also index by chunkY? for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ diff --git a/src/Player.php b/src/Player.php index 049d0d26..212ce852 100755 --- a/src/Player.php +++ b/src/Player.php @@ -110,7 +110,7 @@ class Player{ $this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0); $this->blockUpdateQueue->data = []; - $this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); + //$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true); $this->server->schedule(1, [$this, "handlePacketQueues"], [], true); $this->evid[] = $this->server->event("server.close", [$this, "close"]); diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index a70a0965..71c710c8 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -728,7 +728,7 @@ class PocketMinecraftServer{ $info["tps"] = $this->getTPS(); $info["memory_usage"] = round((memory_get_usage() / 1024) / 1024, 2) . "MB"; $info["memory_peak_usage"] = round((memory_get_peak_usage() / 1024) / 1024, 2) . "MB"; - $info["entities"] = count($this->entities); + $info["entities"] = count($this->api->entity->entities); $info["players"] = count($this->clients); $info["events"] = count($this->eventsID); $info["handlers"] = $this->query("SELECT count(ID) as count FROM handlers;", true); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ec0e9af1..bec78e66 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -329,7 +329,7 @@ class Entity extends Position if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $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){ + if(!$item->closed && $item->spawntime > 0 && $item->delayBeforePickup <= 0){ if($item->boundingBox->intersectsWith($myBB)){ if((($this->player->gamemode & 0x01) === 1 || $this->player->hasSpace($item->type, $item->meta, $item->stack) === true) && $this->server->api->dhandle("player.pickup", array( "eid" => $this->player->eid, @@ -530,11 +530,11 @@ class Entity extends Position $aABB = $this->boundingBox->addCoord($dx, $dy, $dz); $x0 = floor($aABB->minX); - $x1 = ceil($aABB->maxX); + $x1 = floor($aABB->maxX) + 1; $y0 = floor($aABB->minY); - $y1 = ceil($aABB->maxY); + $y1 = floor($aABB->maxY) + 1; $z0 = floor($aABB->minZ); - $z1 = ceil($aABB->maxZ); + $z1 = floor($aABB->maxZ) + 1; for($x = $x0; $x <= $x1; ++$x){ for($y = $y0; $y <= $y1; ++$y){ @@ -583,7 +583,7 @@ class Entity extends Position $this->boundingBox->offset(0, 0, $dz); $dy = -$this->stepHeight; - foreach($aaBBs as $bb){ //TODO optimize + foreach($aaBBs as $bb){ $dy = $bb->calculateYOffset($this->boundingBox, $dy); } $this->boundingBox->offset(0, $dy, 0); @@ -600,25 +600,31 @@ class Entity extends Position } - //TODO step $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; - $this->y = $this->boundingBox->minY + $this->yOffset;// - //TODO - used when player is sneaking $this->ySize; + $this->y = $this->boundingBox->minY + $this->yOffset; $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; //$this.isCollidedHorizontally = savedDX != dx || savedDZ != dz; //$this.isCollidedVertically = savedDY != dy; $this->onGround = $savedDY != $dy && $savedDY < 0.0; //$this.isCollided = this.isCollidedHorizontally || this.isCollidedVertically; + $this->updateFallState($this->speedY); + if($savedDX != $dx) $this->speedX = 0; if($savedDY != $dy) $this->speedY = 0; if($savedDZ != $dz) $this->speedZ = 0; - //TODO more stuff + + //TODO more stuff -> onEntityWalking //TODO block collisions? - //TODO deal fire damage here + + //TODO deal fire damage here + if($this->level->isBoundingBoxOnFire($this->boundingBox->contract(0.001, 0.001, 0.001))){ + + } } public function isInVoid(){ @@ -644,6 +650,7 @@ class Entity extends Position if($this->isStatic === false){ if(!$this->isPlayer()){ $this->updateLast(); + $this->updatePosition(); //TODO shouldnt be called in Entity $this->updateEntityMovement(); /*$update = false; if($this->speedX > -self::MIN_POSSIBLE_SPEED && $this->speedX < self::MIN_POSSIBLE_SPEED){ @@ -698,14 +705,11 @@ class Entity extends Position }elseif ($this->lastYaw != $this->yaw || $this->lastPitch != $this->pitch || $this->lastHeadYaw != $this->headYaw) { $update = true; } - + if($update === true){ $hasUpdate = true; - $this->lastSpeedZ = $this->speedZ; - $this->lastSpeedY = $this->speedY; - $this->lastSpeedX = $this->speedX; } - $this->updateFallState($this->speedY); + } elseif($this->player instanceof Player){ $prevGroundState = $this->onGround; $this->onGround = false; @@ -829,10 +833,7 @@ class Entity extends Position $this->server->api->player->broadcastPacket($players, $pk); } } - } else{ - $this->updatePosition(); } - } $this->lastUpdate = $now; } @@ -1235,13 +1236,16 @@ class Entity extends Position public function updateLast() { - $this->last[0] = $this->x; - $this->last[1] = $this->y; - $this->last[2] = $this->z; - $this->last[3] = $this->yaw; - $this->last[4] = $this->pitch; - $this->last[5] = microtime(true); + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + $this->lastYaw = $this->yaw; + $this->lastPitch = $this->pitch; + $this->lastTime = microtime(true); $this->lastHeadYaw = $this->headYaw; + $this->lastSpeedZ = $this->speedZ; + $this->lastSpeedY = $this->speedY; + $this->lastSpeedX = $this->speedX; } public function getPosition($round = false) diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 81488e33..1bf7ea01 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -25,12 +25,12 @@ class ItemEntity extends Entity{ $this->stepHeight = 0; } + public function counterUpdate(){ + parent::counterUpdate(); + if($this->delayBeforePickup > 0) --$this->delayBeforePickup; + } + public function updateEntityMovement(){ - //TODO it is weird on client side for some reason - if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; - if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; - if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; - $this->speedY -= 0.04; $this->noClip = false; //TODO pushOutofBlocks $this->move($this->speedX, $this->speedY, $this->speedZ); @@ -54,8 +54,12 @@ class ItemEntity extends Entity{ $this->speedY *= 0.98; $this->speedZ *= $friction; - //if($this->onGround) $this->speedY *= -0.5; + if($this->onGround) $this->speedY *= -0.5; //TODO ++age; despawn after age >= 6000; + + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; + if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; + if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; } } diff --git a/src/material/block/solid/LeavesBlock.php b/src/material/block/solid/LeavesBlock.php index 60d4dfbf..b03e726e 100644 --- a/src/material/block/solid/LeavesBlock.php +++ b/src/material/block/solid/LeavesBlock.php @@ -15,6 +15,11 @@ class LeavesBlock extends TransparentBlock{ $this->name = $names[$this->meta & 0x03]; $this->hardness = 1; } + + public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ + return [new AxisAlignedBB($x, $y, $z, $x + 1, $y + 1, $z + 1)]; + } + public static function createIndex($x, $y, $z){ return $x.".".$y.".".$z; } diff --git a/src/world/Level.php b/src/world/Level.php index 1dad5c63..4cfc4f1c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -89,12 +89,6 @@ class Level{ $y1 = ceil($aABB->maxY); $z0 = floor($aABB->minZ); $z1 = ceil($aABB->maxZ); - $x0 = $x0 < 0 ? 0 : $x0; - $y0 = $y0 < 0 ? 0 : $y0; - $z0 = $z0 < 0 ? 0 : $z0; - $x1 = $x1 > 256 ? 256 : $x1; - $y1 = $y1 > 128 ? 128 : $y1; - $z1 = $z1 > 256 ? 256 : $z1; for($x = $x0; $x <= $x1; ++$x) { for($y = $y0; $y <= $y1; ++$y) { @@ -361,6 +355,7 @@ class Level{ if($e instanceof Entity){ $newChunkX = (int)$e->x >> 4; $newChunkZ = (int)$e->z >> 4; + if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); }else if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ @@ -406,6 +401,26 @@ class Level{ $this->queuedBlockUpdates = []; } + public function isBoundingBoxOnFire(AxisAlignedBB $bb){ + $minX = floor($bb->minX); + $maxX = ceil($bb->maxX); + $minY = floor($bb->minY); + $maxY = ceil($bb->maxY); + $minZ = floor($bb->minZ); + $maxZ = ceil($bb->maxZ); + + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ + $blockAt = $this->level->getBlockID($x, $y, $z); + if($blockAt == FIRE || $blockAt == STILL_LAVA || $blockAt == LAVA) return true; + } + } + } + + return false; + } + public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int)($bb->minX)) >> 4; $minChunkZ = ((int)($bb->minZ)) >> 4; From b7f9d3f1c56838d4128ec5d9bdb138dc41ef11f8 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 12 Jan 2024 05:16:12 +0300 Subject: [PATCH 19/34] fixed item motion on client side --- src/entity/Entity.php | 6 +++--- src/entity/object/ItemEntity.php | 4 ---- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index bec78e66..5c68dff3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -540,7 +540,7 @@ class Entity extends Position for($y = $y0; $y <= $y1; ++$y){ for($z = $z0; $z <= $z1; ++$z){ $b = $this->level->level->getBlockID($x, $y, $z); - if($b != 0){ //TODO might be better to use other method + if($b != 0){ $blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); foreach($blockBounds as $blockBound){ $dy = $blockBound->calculateYOffset($this->boundingBox, $dy); @@ -1110,8 +1110,8 @@ class Entity extends Position public function updateAABB() { $this->boundingBox->setBounds( - $this->x - $this->radius, $this->y, $this->z - $this->radius, - $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius + $this->x - $this->radius, $this->y - $this->yOffset, $this->z - $this->radius, + $this->x + $this->radius, $this->y + $this->height - $this->yOffset, $this->z + $this->radius ); } diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 1bf7ea01..4992d57f 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -57,9 +57,5 @@ class ItemEntity extends Entity{ if($this->onGround) $this->speedY *= -0.5; //TODO ++age; despawn after age >= 6000; - - if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; - if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; - if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; } } From 3d1dba6b86d8c14b315023b514b3e2f3e4f1ffbc Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Fri, 12 Jan 2024 05:54:19 +0300 Subject: [PATCH 20/34] fixed tasklookaround --- src/entity/Living.php | 9 +++++++-- src/entity/ai/MobController.php | 8 +++++++- src/entity/ai/path/PathFollower.php | 17 +++++++++++------ src/entity/ai/tasks/TaskLookAround.php | 22 ++++++++++------------ src/entity/animal/Chicken.php | 1 + src/entity/animal/Cow.php | 3 +++ src/entity/animal/Pig.php | 2 +- src/entity/animal/Sheep.php | 2 +- src/entity/monster/Creeper.php | 1 + src/entity/monster/PigZombie.php | 3 +++ src/entity/monster/Skeleton.php | 1 + src/entity/monster/Spider.php | 1 + src/entity/monster/Zombie.php | 1 + src/math/AxisAlignedBB.php | 11 ++++++++++- 14 files changed, 58 insertions(+), 24 deletions(-) diff --git a/src/entity/Living.php b/src/entity/Living.php index 1b6ba5b6..3124197d 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -115,9 +115,14 @@ abstract class Living extends Entity implements Pathfindable{ public function updateEntityMovement(){ if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { - $this->ai->updateTasks(); + //$this->ai->updateTasks(); } - + //if($this->onGround){ + // if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ + // $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); + // } + // $this->pathFollower->followPath(); + //} $this->ai->mobController->movementTick(); $this->ai->mobController->rotateTick(); diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index fbe744fc..342fb168 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -49,7 +49,13 @@ class MobController public function isRotationCompleted(){ return $this->finalHeadYaw === $this->entity->headYaw; } - + public function setMovingTarget($x, $y, $z, $speed){ + $this->moveToX = $x; + $this->moveToY = $y; + $this->moveToZ = $z; + $this->speed = $speed; + $this->updateMove = true; + } public function setMovingOffset($x, $y, $z, $speed){ $this->moveToX = $this->entity->x + ($x); $this->moveToY = $this->entity->y + ($y); diff --git a/src/entity/ai/path/PathFollower.php b/src/entity/ai/path/PathFollower.php index 24545186..fa462b7e 100644 --- a/src/entity/ai/path/PathFollower.php +++ b/src/entity/ai/path/PathFollower.php @@ -13,13 +13,18 @@ class PathFollower{ public function followPath(){ if(!isset($this->entity) || !($this->entity instanceof Living)) return; + if(!$this->entity->hasPath()) return; - if($this->entity->path != null && (is_array($this->entity->path) && count($this->entity->path) <= 0 || $this->entity->currentIndex >= count($this->entity->path))){ - $this->entity->path = null; - $this->entity->currentIndex = 0; - $this->entity->currentNode = false; - }elseif($this->entity->path != null && ($this->entity->currentNode == false || $this->entity->boundingBox->isXYZInside($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z) || $this->entity->ai->mobController->moveTo($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z) === false)){ - $this->entity->currentNode = $this->entity->path[$this->entity->currentIndex++]; + if($this->entity->currentNode == null){ + $this->entity->currentNode = $this->entity->path[$this->entity->currentIndex]; + } + + $this->entity->ai->mobController->setMovingTarget($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z, 0.25); + console($this->entity->currentNode.":".$this->entity->boundingBox); + if($this->entity->boundingBox->isXYZInsideNS($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z)){ + ++$this->entity->currentIndex; + console("next"); + $this->entity->currentNode = null; } } diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php index 78d342bd..bb25edc5 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -2,31 +2,29 @@ class TaskLookAround extends TaskBase { - private $rotation; + public $lookOffsetX, $lookOffsetZ; + public function onStart(EntityAI $ai) { - $this->selfCounter = 1; - $this->rotation = mt_rand(-120, 120); + $this->selfCounter = 20 + lcg_value(); + $v = (M_PI * 2) * lcg_value(); + $this->lookOffsetX = cos($v); + $this->lookOffsetZ = sin($v); } public function onEnd(EntityAI $ai) { - $ai->entity->idleTime = mt_rand(20, 40); + //$ai->entity->idleTime = mt_rand(20, 40); } public function canBeExecuted(EntityAI $ai) { return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/ } - + public function onUpdate(EntityAI $ai) { - if($this->rotation === 0){ - $this->selfCounter = 0; - } - $v = min(Utils::getSign($this->rotation) * 10, $this->rotation); - $ai->entity->headYaw += $v; - $this->rotation -= $v; - + --$this->selfCounter; + $ai->mobController->setLookPosition($ai->entity->x + $this->lookOffsetX, $ai->entity->y + $ai->entity->getEyeHeight(), $ai->entity->z + $this->lookOffsetZ, 10, $ai->entity->getVerticalFaceSpeed()); } } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 4d3dafe5..d1049460 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -14,6 +14,7 @@ class Chicken extends Animal{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskLookAround()); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index baff016b..ed592207 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -10,6 +10,9 @@ class Cow extends Animal{ $this->ai->addTask(new TaskRandomWalk(0.2)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskLookAround()); + + //$this->ai->addTask(new TaskSwimming()); } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 3083da60..a69bdfd3 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -13,7 +13,7 @@ class Pig extends Animal implements Rideable{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); - + $this->ai->addTask(new TaskLookAround()); } /** * @return boolean diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index ecaba25e..2b002cf4 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -17,7 +17,7 @@ class Sheep extends Animal{ $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskEatTileGoal()); - + $this->ai->addTask(new TaskLookAround()); } public function createSaveData(){ diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 68b67f80..93011ba9 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -13,6 +13,7 @@ class Creeper extends Monster{ $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskLookAround()); } public function getAttackDamage(){ diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index bfc5371c..1e8878c9 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -7,6 +7,9 @@ class PigZombie extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic"); $this->setName("Pigman"); $this->setSpeed(0.25); + + $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskRandomWalk(0.25)); } public function getAttackDamage(){ return 5; diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index b9a0344e..46471322 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -10,6 +10,7 @@ class Skeleton extends Monster{ //$this->ai->addTask(new TaskDestroyServerPerformance()); $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskLookAround()); } public function getAttackDamage(){ diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 6af9c1c5..69dab308 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -8,6 +8,7 @@ class Spider extends Monster{ $this->setName("Spider"); $this->setSpeed(0.3); $this->ai->addTask(new TaskRandomWalk(0.25)); //TODO this mob doesnt use newAI -> speed is not correct + $this->ai->addTask(new TaskLookAround()); //TODO this mob doesnt use newAI } public function getDrops(){ diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 1497f048..0364f6f5 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -9,6 +9,7 @@ class Zombie extends Monster{ $this->setSpeed(0.23); $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskLookAround()); } public function getArmorValue(){ diff --git a/src/math/AxisAlignedBB.php b/src/math/AxisAlignedBB.php index 93026a4f..7eba2e67 100644 --- a/src/math/AxisAlignedBB.php +++ b/src/math/AxisAlignedBB.php @@ -188,7 +188,16 @@ class AxisAlignedBB{ public function intersectsWith(AxisAlignedBB $bb){ return $bb->maxX > $this->minX and $bb->minX < $this->maxX && $bb->maxZ > $this->minZ and $bb->minZ < $this->maxZ && $bb->maxY > $this->minY and $bb->minY < $this->maxY; } - + public function isXYZInsideNS($x, $y, $z){ + if($x < $this->minX or $x > $this->maxX){ + return false; + } + if($y < $this->minY or $y > $this->maxY){ + return false; + } + + return $z > $this->minZ and $z < $this->maxZ; + } public function isXYZInside($x, $y, $z){ if($x <= $this->minX or $x >= $this->maxX){ return false; From 6eb13e5bd412add6ffe16375c64fdf3fe590172a Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 13 Jan 2024 05:13:06 +0300 Subject: [PATCH 21/34] swimming fixed --- src/entity/Entity.php | 21 ++++++++++-- src/entity/Living.php | 48 ++++++++++++++++++++++---- src/entity/ai/MobController.php | 6 +++- src/entity/ai/tasks/TaskSwimming.php | 3 -- src/entity/animal/Chicken.php | 1 + src/entity/animal/Cow.php | 3 +- src/entity/animal/Pig.php | 1 + src/entity/animal/Sheep.php | 1 + src/entity/monster/Creeper.php | 1 + src/entity/monster/PigZombie.php | 1 + src/entity/monster/Skeleton.php | 3 +- src/entity/monster/Spider.php | 1 + src/entity/monster/Zombie.php | 3 +- src/entity/object/FallingSand.php | 51 ++++++++++++++++++++++++++++ src/material/Block.php | 1 + src/world/Level.php | 41 ++++++++++++++++++++++ 16 files changed, 170 insertions(+), 16 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 5c68dff3..4602d727 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -319,7 +319,19 @@ class Entity extends Position $this->server->api->entity->drop($this, BlockAPI::getItem($drop[0] & 0xFFFF, $drop[1] & 0xFFFF, $drop[2] & 0xFF), true); } } - + + public function handleWaterMovement(){ + if($this->level->handleMaterialAcceleration($this->boundingBox->expand(0, -0.4, 0)->contract(0.001, 0.001, 0.001), 0, $this)){ + $this->fallDistance = 0; + $this->inWater = true; + $this->fire = 0; + }else{ + $this->inWater = false; + } + + return $this->inWater; + } + public function environmentUpdate($time) { $hasUpdate = $this->class === ENTITY_MOB; // force true for mobs @@ -372,7 +384,10 @@ class Entity extends Position $this->outOfWorld(); $hasUpdate = true; } - + if(!$this->isPlayer()){ + $this->handleWaterMovement(); + } + if($this->fire > 0){ if(($this->fire % 20) === 0){ $this->harm(1, "burning"); @@ -411,6 +426,8 @@ class Entity extends Position } } + + //air damage if($this->isPlayer() || $this instanceof Living){ $d = $this->y + $this->getEyeHeight(); diff --git a/src/entity/Living.php b/src/entity/Living.php index 3124197d..382c3357 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -13,7 +13,7 @@ abstract class Living extends Entity implements Pathfindable{ public $aiMoveSpeed; public $renderYawOffset = 0.0; //used by head rotation, TODO better name - + public $jumping, $jumpTicks; public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->target = false; $this->ai = new EntityAI($this); @@ -115,7 +115,7 @@ abstract class Living extends Entity implements Pathfindable{ public function updateEntityMovement(){ if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { - //$this->ai->updateTasks(); + $this->ai->updateTasks(); } //if($this->onGround){ // if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ @@ -123,9 +123,27 @@ abstract class Living extends Entity implements Pathfindable{ // } // $this->pathFollower->followPath(); //} + + + //not exactly here + if($this->jumping){ + if(!$this->inWater){ //TODO also lava + if($this->onGround && $this->jumpTicks <= 0){ + //TODO jump $this->jump(); + $this->jumpTicks = 10; + } + }else{ + $this->speedY += 0.04; + } + }else{ + $this->jumpTicks = 0; + } + + $this->ai->mobController->movementTick(); $this->ai->mobController->rotateTick(); - + $this->ai->mobController->jumpTick(); + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; @@ -147,10 +165,28 @@ abstract class Living extends Entity implements Pathfindable{ } + public function counterUpdate(){ + parent::counterUpdate(); + if($this->jumpTicks > 0) --$this->jumpTicks; + } + public function moveEntityWithHeading($strafe, $forward){ - //TODO lava, water - //If not in lava in water -> - { + if($this->inWater){ //also check is player, and can it fly or not + $this->moveFlying($strafe, $forward, 0.04); //TODO speed: this.isAIEnabled() ? 0.04F : 0.02F + $this->move($this->speedX, $this->speedY, $this->speedZ); + $this->speedX *= 0.8; + $this->speedY *= 0.8; + $this->speedZ *= 0.8; + $this->speedY -= 0.02; + + //TODO: static speedY + //if (this.isCollidedHorizontally && this.isOffsetPositionInLiquid(this.motionX, this.motionY + 0.6 - this.posY + var9, this.motionZ)) + //{ + // this.motionY = 0.3; + //} + } + //TODO lava + else{ $friction = 0.91; if($this->onGround){ diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 342fb168..1c30d638 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -21,7 +21,6 @@ class MobController public $someTicker = 0; protected $jumping; - protected $jumpTimeout; public $moveToX, $moveToY, $moveToZ; public $speed; @@ -84,6 +83,11 @@ class MobController return $old + $v4; } + public function jumpTick(){ + $this->entity->jumping = $this->jumping; + $this->jumping = false; + } + public function movementTick(){ $this->entity->moveForward = 0; if($this->updateMove){ diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index 159c7ff3..59b5700a 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -16,15 +16,12 @@ class TaskSwimming extends TaskBase { if(!$ai->entity->inWater){ $this->reset(); - $ai->mobController->setJumping(false); return; } if(lcg_value() < 0.8){ #1.5.2 method $ai->mobController->setJumping(true); } - - } public function canBeExecuted(EntityAI $ai) diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index d1049460..5e0c2c4b 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -15,6 +15,7 @@ class Chicken extends Animal{ $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index ed592207..02bd7f69 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -11,8 +11,7 @@ class Cow extends Animal{ $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskLookAround()); - - //$this->ai->addTask(new TaskSwimming()); + $this->ai->addTask(new TaskSwimming()); } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index a69bdfd3..0c152f82 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -14,6 +14,7 @@ class Pig extends Animal implements Rideable{ $this->ai->addTask(new TaskLookAtPlayer(6)); $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } /** * @return boolean diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 2b002cf4..79e66056 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -18,6 +18,7 @@ class Sheep extends Animal{ $this->ai->addTask(new TaskPanic(0.375)); $this->ai->addTask(new TaskEatTileGoal()); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } public function createSaveData(){ diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 93011ba9..750d80e8 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -14,6 +14,7 @@ class Creeper extends Monster{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } public function getAttackDamage(){ diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index 1e8878c9..6be561e8 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -10,6 +10,7 @@ class PigZombie extends Monster{ $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskSwimming()); } public function getAttackDamage(){ return 5; diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 46471322..749f0d06 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -11,6 +11,7 @@ class Skeleton extends Monster{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } public function getAttackDamage(){ @@ -18,7 +19,7 @@ class Skeleton extends Monster{ } public function updateBurning(){ - if($this->fire > 0 or !$this->level->isDay()){ + if($this->fire > 0 or !$this->level->isDay() || $this->inWater){ //TODO fix burning in water return false; } diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 69dab308..d9adf87e 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -9,6 +9,7 @@ class Spider extends Monster{ $this->setSpeed(0.3); $this->ai->addTask(new TaskRandomWalk(0.25)); //TODO this mob doesnt use newAI -> speed is not correct $this->ai->addTask(new TaskLookAround()); //TODO this mob doesnt use newAI + $this->ai->addTask(new TaskSwimming()); } public function getDrops(){ diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 0364f6f5..e864ada3 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -10,6 +10,7 @@ class Zombie extends Monster{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAround()); + $this->ai->addTask(new TaskSwimming()); } public function getArmorValue(){ @@ -17,7 +18,7 @@ class Zombie extends Monster{ } public function updateBurning(){ - if($this->fire > 0 or !$this->level->isDay()){ + if($this->fire > 0 or !$this->level->isDay() || $this->inWater){ //TODO fix burning in water return false; } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 42ad573c..6a9a8607 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -3,6 +3,9 @@ class FallingSand extends Entity{ const TYPE = FALLING_SAND; const CLASS_TYPE = ENTITY_FALLING; + + public $fallTime = 0; + public function __construct($level, $eid, $class, $type = 0, $data = []){ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(PHP_INT_MAX, "generic"); @@ -12,4 +15,52 @@ class FallingSand extends Entity{ $this->hasGravity = true; $this->gravity = 0.04; } + + public function update($now){ + if($this->closed) return; + + if( $this->data["Tile"] == AIR) $this->close(); + else { + $this->lastX = $this->x; + $this->lastY = $this->y; + $this->lastZ = $this->z; + ++$this->fallTime; + $this->speedY -= 0.04; + $this->move($this->speedX, $this->speedY, $this->speedZ); + + $this->speedX *= 0.98; + $this->speedY *= 0.98; + $this->speedZ *= 0.98; + + $x = floor($this->x); + $y = floor($this->y); + $z = floot($this->z); + + if($this->fallTime == 1){ + /* i dont think this is needed here? + * if (this.worldObj.getBlockId(var1, var2, var3) != this.blockID) + { + this.setDead(); + return; + } + + this.worldObj.setBlockToAir(var1, var2, var3); + */ + } + + if($this->onGround){ + $this->speedX *= 0.7; + $this->speedZ *= 0.7; + $this->speedY *= -0.5; + + //1.5.2 has piston check, skipping + $this->close(); + //if (!this.isBreakingAnvil && this.worldObj.canPlaceEntityOnSide(this.blockID, var1, var2, var3, true, 1, (Entity)null, (ItemStack)null) && !BlockSand.canFallBelow(this.worldObj, var1, var2 - 1, var3) && this.worldObj.setBlock(var1, var2, var3, this.blockID, this.metadata, 3)) + // + //} + //a check if block has tilentity, might be added after tile entities rewrite + + } + } + } } \ No newline at end of file diff --git a/src/material/Block.php b/src/material/Block.php index f15cf946..e4b4965e 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -155,6 +155,7 @@ abstract class Block extends Position{ public $z = 0; public $slipperiness; + public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){} public static function onRandomTick(Level $level, $x, $y, $z){} public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){} public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ diff --git a/src/world/Level.php b/src/world/Level.php index 4cfc4f1c..cf4e1a10 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -76,6 +76,47 @@ class Level{ return true; } + //TODO public function canPlaceEntityOnSide() + + public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ + $minX = floor($aabb->minX); + $maxX = ceil($aabb->maxX); + $minY = floor($aabb->minY); + $maxY = ceil($aabb->maxY); + $minZ = floor($aabb->minZ); + $maxZ = ceil($aabb->maxZ); + + //1.5.2 checks that all chunks exist, not needed here i think + + $appliedVelocity = false; + $velocityVec = new Vector3(0, 0, 0); + + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ + [$block, $meta] = $this->level->getBlock($x, $y, $z); + if(($materialType == 0 && ($block == WATER || $block == STILL_WATER)) || ($materialType == 1 && ($block == LAVA || $block == STILL_LAVA))){ //TODO better material system + $v16 = ($y + 1) - LiquidBlock::getPercentAir($meta); + if($maxY >= $v16){ + $appliedVelocity = true; + Block::$class[$block]::addVelocityToEntity($this, $x, $y, $z, $entity, $velocityVec); + } + } + } + } + } + + if($velocityVec->length() > 0){ //also checks is player flying + $velocityVec = $velocityVec->normalize(); //TODO do not use vec methods + $v18 = 0.014; + $entity->speedX += $velocityVec->x * $v18; + $entity->speedY += $velocityVec->y * $v18; + $entity->speedZ += $velocityVec->z * $v18; + } + + return $appliedVelocity; + } + /** * @param Entity $e * @param AxisAlignedBB $aABB From eda78f9e1793fe0b8dd731a0adb256241af2d1fd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 14 Jan 2024 06:29:49 +0300 Subject: [PATCH 22/34] fixed falling sand --- src/entity/Entity.php | 5 ----- src/entity/monster/Zombie.php | 2 +- src/entity/object/FallingSand.php | 17 +++++++++-------- src/material/Block.php | 14 ++++++++++++++ src/material/StaticBlock.php | 28 ++++++++++++++++++++++++++++ src/world/Level.php | 3 ++- 6 files changed, 54 insertions(+), 15 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 4602d727..80fe53df 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -522,11 +522,6 @@ class Entity extends Position $this->speedX += $strafe*$v6 - $forward*$v5; $this->speedZ += $forward*$v6 + $strafe*$v5; - - //float var5 = MathHelper.sin(this.rotationYaw * (float)Math.PI / 180.0F); - //float var6 = MathHelper.cos(this.rotationYaw * (float)Math.PI / 180.0F); - //this.motionX += (double)(strafe * var6 - forward * var5); - //this.motionZ += (double)(forward * var6 + strafe * var5); } } diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index e864ada3..a07458f1 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -38,7 +38,7 @@ class Zombie extends Monster{ } public function getAttackDamage(){ - return 4; //TODO vanillafy(zombies might be able to hold items) + return 4; } public function update($now){ diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 6a9a8607..2a43338f 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -34,7 +34,7 @@ class FallingSand extends Entity{ $x = floor($this->x); $y = floor($this->y); - $z = floot($this->z); + $z = floor($this->z); if($this->fallTime == 1){ /* i dont think this is needed here? @@ -52,14 +52,15 @@ class FallingSand extends Entity{ $this->speedX *= 0.7; $this->speedZ *= 0.7; $this->speedY *= -0.5; - - //1.5.2 has piston check, skipping $this->close(); - //if (!this.isBreakingAnvil && this.worldObj.canPlaceEntityOnSide(this.blockID, var1, var2, var3, true, 1, (Entity)null, (ItemStack)null) && !BlockSand.canFallBelow(this.worldObj, var1, var2 - 1, var3) && this.worldObj.setBlock(var1, var2, var3, this.blockID, this.metadata, 3)) - // - //} - //a check if block has tilentity, might be added after tile entities rewrite - + //TODO vanilla-like checking? + $this->level->fastSetBlockUpdate($x, $y, $z, $this->data["Tile"], 0); //TODO add metadata + }else if(($blockAt = $this->level->level->getBlockID($x, $y, $z)) != 0){ + //TODO vanilla-like checking? + if(StaticBlock::getIsTransparent($blockAt) && !StaticBlock::getIsLiquid($blockAt)){ + $this->close(); + ServerAPI::request()->api->entity->drop($this, BlockAPI::getItem($this->data["Tile"], 0, 1)); //TODO add metadata + } } } } diff --git a/src/material/Block.php b/src/material/Block.php index e4b4965e..17c89272 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -155,6 +155,20 @@ abstract class Block extends Position{ public $z = 0; public $slipperiness; + public static $minX = 0, $minY = 0, $minZ = 0, $maxX = 1, $maxY = 1, $maxZ = 1; + public static function setBlockBounds($minX, $minY, $minZ, $maxX, $maxY, $maxZ){ //TODO call for some specific blocks + static::$minX = $minX; + static::$minY = $minY; + static::$minZ = $minZ; + static::$maxX = $maxX; + static::$maxY = $maxY; + static::$maxZ = $maxZ; + } + public static function getAABB(Level $level, $x, $y, $z){ + return new AxisAlignedBB(static::$minX + $x, static::$minY + $y, static::$minZ + $z, static::$maxX + $x, static::$maxY + $y, static::$maxZ + $z); + } + + public static function addVelocityToEntity(Level $level, $x, $y, $z, Entity $entity, Vector3 $velocityVector){} public static function onRandomTick(Level $level, $x, $y, $z){} public static function fallOn(Level $level, $x, $y, $z, Entity $entity, $fallDistance){} diff --git a/src/material/StaticBlock.php b/src/material/StaticBlock.php index d74ea36c..fd42c6b5 100644 --- a/src/material/StaticBlock.php +++ b/src/material/StaticBlock.php @@ -37,6 +37,33 @@ class StaticBlock self::$boundingBoxes[$b->getID()] = $b->boundingBox; self::$hardness[$b->getID()] = $b->getHardness(); } + + Block::$class[BED_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.5625, 1); + //Cake: has bounds based on world state + //Chest: has bounds based on world state + Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); + Block::$class[CARROT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); + Block::$class[POTATO_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); + Block::$class[BEETROOT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); //i assume it extends class Crops + Block::$class[WHEAT_BLOCK]::setBlockBounds(0, 0, 0, 1, 0.25, 1); + Block::$class[DEAD_BUSH]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); + Block::$class[DOOR_BLOCK]::setBlockBounds(0, 0, 0, 1, 1, 1); //has bounds based on rotation + Block::$class[FARMLAND]::setBlockBounds(0, 0, 0, 1, 0.9375, 1); + //Fence: bounds on state + //Fence Gate: bounds on state + Block::$class[TALL_GRASS]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); + Block::$class[MELON_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); + Block::$class[PUMPKIN_STEM]::setBlockBounds(0.5 - 0.125, 0, 0.5 - 0.125, 0.5 + 0.125, 0.25, 0.5 + 0.125); + Block::$class[SAPLING]::setBlockBounds(0.1, 0, 0.1, 0.9, 0.8, 0.9); + Block::$class[BROWN_MUSHROOM]::setBlockBounds(0.3, 0, 0.3, 0.8, 0.4, 0.8); + Block::$class[SLAB]::setBlockBounds(0, 0, 0, 1, 0.5, 1); + //Ladder: based on rotation + //Glass Pane: based on state + RailBaseBlock::setBlockBounds(0, 0, 0, 1, 0.125, 1); + Block::$class[SUGARCANE_BLOCK]::setBlockBounds(0.5 - 0.375, 0, 0.5 - 0.375, 0.5 + 0.375, 1, 0.5 + 0.375); + Block::$class[SNOW_LAYER]::setBlockBounds(0, 0, 0, 1, 0.125, 1); + //Stairs: based on different factors + //Stone wall: based on state } public static function getBlock($id){ @@ -47,6 +74,7 @@ class StaticBlock return self::$hardness[$id] ?? StaticBlock::DEFAULT_HARDNESS; } + //TODO: use static block min/max public static function getBoundingBoxForBlockCoords($id, $x, $y, $z){ /**@var AxisAlignedBB $bb*/ $bb = self::$boundingBoxes[$id] ?? false; diff --git a/src/world/Level.php b/src/world/Level.php index cf4e1a10..87a2dbe1 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -76,7 +76,8 @@ class Level{ return true; } - //TODO public function canPlaceEntityOnSide() + //TODO mayPlace + public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ $minX = floor($aabb->minX); From 6ba62666c37f06a3ae942e231b26c32a094ac9e4 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 14 Jan 2024 06:43:48 +0300 Subject: [PATCH 23/34] fixed multiworld --- src/Player.php | 8 +++++--- src/entity/Entity.php | 4 ++++ src/world/Level.php | 15 +++++++++++++-- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/Player.php b/src/Player.php index 212ce852..5481cfbb 100755 --- a/src/Player.php +++ b/src/Player.php @@ -205,8 +205,11 @@ class Player{ $this->level->freeAllChunks($this); unset($this->level->players[$this->CID]); + unset($this->level->entityListPositioned["{$this->entity->chunkX} {$this->entity->chunkZ}"][$this->eid]); + unset($this->level->entityList[$this->eid]); $this->level = $pos->level; - + $this->entity->level = $this->level; + $this->level->entityList[$this->entity->eid] = $this->entity; $this->level->players[$this->CID] = $this; $this->chunksLoaded = []; $this->server->api->entity->spawnToAll($this->entity); @@ -1369,7 +1372,6 @@ class Player{ } public function entityTick(){ if($this->isSleeping) ++$this->sleepingTime; - $this->handlePacketQueues(); } public function handleDataPacket(RakNetDataPacket $packet){ if($this->connected === false){ @@ -1572,7 +1574,7 @@ class Player{ console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")"); - return false; //return false so it will not be updated by tickerFunction + break; case ProtocolInfo::READY_PACKET: if($this->loggedIn === false){ break; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 80fe53df..a52f1fd6 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -86,6 +86,10 @@ class Entity extends Position public $enableAutojump = false; public $yOffset = 0.0; public $noClip = false; + + public $chunkX = 0; + public $chunkZ = 0; + function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { $this->random = new Random(); diff --git a/src/world/Level.php b/src/world/Level.php index 87a2dbe1..7029b0ee 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -355,6 +355,8 @@ class Level{ } public function onTick(PocketMinecraftServer $server, $currentTime){ + safe_var_dump($this->players); + console($this->getName()); if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ @@ -385,7 +387,6 @@ class Level{ } $curChunkX = (int)$e->x >> 4; $curChunkZ = (int)$e->z >> 4; - if($e->class === ENTITY_MOB && !$e->isPlayer()){ ++$this->totalMobsAmount; } @@ -397,7 +398,17 @@ class Level{ if($e instanceof Entity){ $newChunkX = (int)$e->x >> 4; $newChunkZ = (int)$e->z >> 4; - + if($e->chunkX != $newChunkX || $e->chunkZ != $newChunkZ){ + $oldIndex = "{$e->chunkX} {$e->chunkZ}"; + unset($this->entityListPositioned[$oldIndex][$e->eid]); + + if($e->level == $this){ + $e->chunkX = $newChunkX; + $e->chunkZ = $newChunkZ; + $newIndex = "$newChunkX $newChunkZ"; + $this->entityListPositioned[$newIndex][$e->eid] = $e->eid; + } + } if($e->level != $this && isset($this->entityListPositioned["$curChunkX $curChunkZ"])){ unset($this->entityListPositioned["$curChunkX $curChunkZ"][$e->eid]); }else if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){ From 4b17edfc0523f0fcaee2de40340a62a017e38ab1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 30 Jan 2024 06:33:21 +0300 Subject: [PATCH 24/34] item entities will try to combine with neatby items of the same type --- src/API/EntityAPI.php | 7 ++-- src/entity/Entity.php | 20 ----------- src/entity/object/ItemEntity.php | 59 ++++++++++++++++++++++++++++++-- src/world/Level.php | 31 ++++++++++++++--- 4 files changed, 88 insertions(+), 29 deletions(-) diff --git a/src/API/EntityAPI.php b/src/API/EntityAPI.php index a953a91a..95f55ead 100644 --- a/src/API/EntityAPI.php +++ b/src/API/EntityAPI.php @@ -217,9 +217,12 @@ class EntityAPI{ for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($center->level->entityListPositioned[$ind] ?? [] as $entid){ - if($this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ + foreach($center->level->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entities[$entid]) && $this->entities[$entid] instanceof Entity && ($class === false || $this->entities[$entid]->class == $class)){ $ents[$entid] = $this->entities[$entid]; + }else if(!isset($this->entities[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($center->level->entityListPositioned[$ind][$ind2]); } } } diff --git a/src/entity/Entity.php b/src/entity/Entity.php index a52f1fd6..0342eef1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1029,26 +1029,6 @@ class Entity extends Position $player->dataPacket($pk); $this->player->sendArmor($player); break; - case ENTITY_ITEM: - $pk = new AddItemEntityPacket(); - $pk->eid = $this->eid; - $pk->x = $this->x; - $pk->y = $this->y; - $pk->z = $this->z; - $pk->yaw = $this->yaw; - $pk->pitch = $this->pitch; - $pk->roll = 0; - $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); - $pk->metadata = $this->getMetadata(); - $player->dataPacket($pk); - - $pk = new SetEntityMotionPacket(); - $pk->eid = $this->eid; - $pk->speedX = $this->speedX; - $pk->speedY = $this->speedY; - $pk->speedZ = $this->speedZ; - $player->dataPacket($pk); - break; case ENTITY_FALLING: $pk = new AddEntityPacket(); $pk->eid = $this->eid; diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 4992d57f..194bd731 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -3,6 +3,7 @@ class ItemEntity extends Entity{ const TYPE = "itemSpecial"; const CLASS_TYPE = ENTITY_ITEM; + public static $searchRadiusX = 0.5, $searchRadiusY = 0.0, $searchRadiusZ = 0.5; public $meta, $stack; @@ -30,18 +31,69 @@ class ItemEntity extends Entity{ if($this->delayBeforePickup > 0) --$this->delayBeforePickup; } + public function searchForOtherItemsNearby(){ + $ents = $this->level->getEntitiesInAABBOfType($this->boundingBox->expand(self::$searchRadiusX, self::$searchRadiusY, self::$searchRadiusZ), ENTITY_ITEM); + + foreach($ents as $e){ + $this->tryCombining($e); + } + + } + + public function spawn($player) + { + $pk = new AddItemEntityPacket(); + $pk->eid = $this->eid; + $pk->x = $this->x; + $pk->y = $this->y; + $pk->z = $this->z; + $pk->yaw = $this->yaw; + $pk->pitch = $this->pitch; + $pk->roll = 0; + $pk->item = BlockAPI::getItem($this->type, $this->meta, $this->stack); + $pk->metadata = $this->getMetadata(); + $player->dataPacket($pk); + + $pk = new SetEntityMotionPacket(); + $pk->eid = $this->eid; + $pk->speedX = $this->speedX; + $pk->speedY = $this->speedY; + $pk->speedZ = $this->speedZ; + $player->dataPacket($pk); + } + + + public function tryCombining(Entity $another){ + + if($another->eid == $this->eid) return false; + + if(!$another->closed && !$this->closed){ + if($another->type == $this->type && $another->meta == $this->meta){ + if(($another->stack + $this->stack) > 64) return false; //TODO dynamic stack size + + $another->stack += $this->stack; + $another->age = min($this->age, $another->age); + $this->close(); + //TODO respawn another entity? + } + } + } + public function updateEntityMovement(){ $this->speedY -= 0.04; $this->noClip = false; //TODO pushOutofBlocks $this->move($this->speedX, $this->speedY, $this->speedZ); $var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ; - if($var1/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) + + if($var1 || $this->counter % 25 == 0/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) //TODO check material - //TODO search for other items + $this->searchForOtherItemsNearby(); //not in vanilla 0.8.1 } + if($this->closed) return; + $friction = 0.98; if($this->onGround){ @@ -56,6 +108,7 @@ class ItemEntity extends Entity{ if($this->onGround) $this->speedY *= -0.5; - //TODO ++age; despawn after age >= 6000; + ++$this->age; + //TODO despawn after age >= 6000 ?; } } diff --git a/src/world/Level.php b/src/world/Level.php index 7029b0ee..e794a354 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -355,8 +355,6 @@ class Level{ } public function onTick(PocketMinecraftServer $server, $currentTime){ - safe_var_dump($this->players); - console($this->getName()); if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ for($cZ = 0; $cZ < 16; ++$cZ){ @@ -473,6 +471,28 @@ class Level{ return false; } + public function getEntitiesInAABBOfType(AxisAlignedBB $bb, $class){ + $minChunkX = ((int)($bb->minX)) >> 4; + $minChunkZ = ((int)($bb->minZ)) >> 4; + $maxChunkX = ((int)($bb->maxX)) >> 4; + $maxChunkZ = ((int)($bb->maxZ)) >> 4; + $ents = []; + console("nya $minChunkX $minChunkZ $maxChunkX $maxChunkZ"); + for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ + for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ + $ind = "$chunkX $chunkZ"; + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->class === $class && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + $ents[$entid] = $this->entityList[$entid]; + }else if(!isset($this->entityList[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($this->entityListPositioned[$ind][$ind2]); + } + } + } + } + return $ents; + } public function getEntitiesInAABB(AxisAlignedBB $bb){ $minChunkX = ((int)($bb->minX)) >> 4; @@ -484,9 +504,12 @@ class Level{ for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; - foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){ - if($this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ + foreach($this->entityListPositioned[$ind] ?? [] as $ind2 => $entid){ + if(isset($this->entityList[$entid]) && $this->entityList[$entid] instanceof Entity && $this->entityList[$entid]->boundingBox->intersectsWith($bb)){ $ents[$entid] = $this->entityList[$entid]; + }else if(!isset($this->entityList[$entid])){ + ConsoleAPI::debug("Removing entity from level array at index $ind/$ind2: $entid"); + unset($this->entityListPositioned[$ind][$ind2]); } } } From d622c5e5cff23f5369b30699abdeb8fcfdc76680 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Tue, 30 Jan 2024 06:57:56 +0300 Subject: [PATCH 25/34] better movement in water, tnt will not fall through grass now --- src/Player.php | 2 +- src/entity/Entity.php | 43 +++++++++++++++++++++++++++----- src/entity/Living.php | 11 ++++---- src/entity/object/ItemEntity.php | 1 - src/world/Level.php | 1 - 5 files changed, 44 insertions(+), 14 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5481cfbb..fa4eb5f4 100755 --- a/src/Player.php +++ b/src/Player.php @@ -1221,7 +1221,7 @@ class Player{ $move = new MoveEntityPacket_PosRot(); $move->eid = $e->eid; $move->x = $e->x; - $move->y = $e->y; //TODO fix items getting into farmland in client side somehow else + $move->y = $e->y; $move->z = $e->z; $move->yaw = $e->yaw; $move->pitch = $e->pitch; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 0342eef1..088283be 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -90,6 +90,8 @@ class Entity extends Position public $chunkX = 0; public $chunkZ = 0; + public $isCollidedHorizontally, $isCollidedVertically, $isCollided; + function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { $this->random = new Random(); @@ -552,9 +554,9 @@ class Entity extends Position $z0 = floor($aABB->minZ); $z1 = floor($aABB->maxZ) + 1; - for($x = $x0; $x <= $x1; ++$x){ - for($y = $y0; $y <= $y1; ++$y){ - for($z = $z0; $z <= $z1; ++$z){ + for($x = $x0; $x < $x1; ++$x){ + for($y = $y0; $y < $y1; ++$y){ + for($z = $z0; $z < $z1; ++$z){ $b = $this->level->level->getBlockID($x, $y, $z); if($b != 0){ $blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); @@ -619,10 +621,10 @@ class Entity extends Position $this->x = ($this->boundingBox->minX + $this->boundingBox->maxX) / 2; $this->y = $this->boundingBox->minY + $this->yOffset; $this->z = ($this->boundingBox->minZ + $this->boundingBox->maxZ) / 2; - //$this.isCollidedHorizontally = savedDX != dx || savedDZ != dz; - //$this.isCollidedVertically = savedDY != dy; + $this->isCollidedHorizontally = $savedDX != $dx || $savedDZ != $dz; + $this->isCollidedVertically = $savedDY != $dy; $this->onGround = $savedDY != $dy && $savedDY < 0.0; - //$this.isCollided = this.isCollidedHorizontally || this.isCollidedVertically; + $this->isCollided = $this->isCollidedHorizontally || $this->isCollidedVertically; $this->updateFallState($this->speedY); @@ -643,6 +645,35 @@ class Entity extends Position } } + //in MCP it is called isOffsetPositionInLiquid, in 0.8 - isFree + public function isFree($offsetX, $offsetY, $offsetZ){ + $offsetBB = $this->boundingBox->getOffsetBoundingBox($offsetX, $offsetY, $offsetZ); + + $minX = floor($offsetBB->minX); + $minY = floor($offsetBB->minY); + $minZ = floor($offsetBB->minZ); + $maxX = floor($offsetBB->maxX + 1); + $maxY = floor($offsetBB->maxY + 1); + $maxZ = floor($offsetBB->maxZ + 1); + + $hasLiquid = false; + $result = false; + for($x = $minX; $x < $maxX; ++$x){ + for($z = $minZ; $z < $maxZ; ++$z){ + for($y = $minY - 1; $y < $maxY; ++$y){ + $v12 = $this->level->level->getBlockID($x, $y, $z); + if($y != ($minY - 1)) $hasLiquid |= StaticBlock::getIsLiquid($v12); + if($v12 != 0){ + $result = true; + break 3; + } + } + } + } + if($result) return !$hasLiquid; + return false; + } + public function isInVoid(){ return $this->y < -1.6; } diff --git a/src/entity/Living.php b/src/entity/Living.php index 382c3357..0f836bc7 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -170,8 +170,10 @@ abstract class Living extends Entity implements Pathfindable{ if($this->jumpTicks > 0) --$this->jumpTicks; } + public function moveEntityWithHeading($strafe, $forward){ if($this->inWater){ //also check is player, and can it fly or not + $prevPosY = $this->y; $this->moveFlying($strafe, $forward, 0.04); //TODO speed: this.isAIEnabled() ? 0.04F : 0.02F $this->move($this->speedX, $this->speedY, $this->speedZ); $this->speedX *= 0.8; @@ -179,11 +181,10 @@ abstract class Living extends Entity implements Pathfindable{ $this->speedZ *= 0.8; $this->speedY -= 0.02; - //TODO: static speedY - //if (this.isCollidedHorizontally && this.isOffsetPositionInLiquid(this.motionX, this.motionY + 0.6 - this.posY + var9, this.motionZ)) - //{ - // this.motionY = 0.3; - //} + + if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){ + $this->speedY = 0.3; + } } //TODO lava else{ diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 194bd731..4d036849 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -95,7 +95,6 @@ class ItemEntity extends Entity{ if($this->closed) return; $friction = 0.98; - if($this->onGround){ $friction = 0.588; $v3 = $this->level->level->getBlockID($this->x, floor($this->boundingBox->minY) - 1, $this->z); diff --git a/src/world/Level.php b/src/world/Level.php index e794a354..12e80a1b 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -477,7 +477,6 @@ class Level{ $maxChunkX = ((int)($bb->maxX)) >> 4; $maxChunkZ = ((int)($bb->maxZ)) >> 4; $ents = []; - console("nya $minChunkX $minChunkZ $maxChunkX $maxChunkZ"); for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){ for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){ $ind = "$chunkX $chunkZ"; From 5f1774b8ce1eebd8f68a41248b9236877294d648 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 31 Jan 2024 04:26:54 +0300 Subject: [PATCH 26/34] inWeb, fire damage" --- src/Player.php | 1 + src/entity/Entity.php | 71 +++++++++++++++++++--- src/entity/object/ItemEntity.php | 9 ++- src/material/Block.php | 3 + src/material/block/nonfull/CobwebBlock.php | 6 +- src/world/Level.php | 6 +- 6 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/Player.php b/src/Player.php index fa4eb5f4..5caa83f5 100755 --- a/src/Player.php +++ b/src/Player.php @@ -2332,6 +2332,7 @@ class Player{ $this->entity->linkedEntity->setPosition($this->entity); $this->entity->linkedEntity->sendMoveUpdate(); + //$this->entity->linkedEntity->moveFlying($packet->moveStrafe, $packet->moveForward, 1); } break; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 088283be..99fec561 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -91,6 +91,12 @@ class Entity extends Position public $chunkZ = 0; public $isCollidedHorizontally, $isCollidedVertically, $isCollided; + /** + * Amount of ticks you can be in fire until you start receiving damage + * @var integer + */ + public $fireResistance = 1; + public $inWeb; function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { @@ -156,6 +162,7 @@ class Entity extends Position $this->hasKnockback = true; $this->hasGravity = true; $this->canBeAttacked = true; + $this->fireResistance = 20; //TODO fire resiatance for player break; case ENTITY_OBJECT: $this->x = isset($this->data["TileX"]) ? $this->data["TileX"] : $this->x; @@ -191,6 +198,11 @@ class Entity extends Position } + public function setInWeb(){ + $this->inWeb = true; + $this->fallDistance = 0; + } + public function addVelocity($vX, $vY = 0, $vZ = 0) { if($vX instanceof Vector3){ @@ -394,7 +406,7 @@ class Entity extends Position $this->handleWaterMovement(); } - if($this->fire > 0){ + if($this->fire > 0){ //TODO move somewhere if(($this->fire % 20) === 0){ $this->harm(1, "burning"); } @@ -446,7 +458,7 @@ class Entity extends Position $f = LiquidBlock::getPercentAir($this->level->level->getBlockDamage($x, $y, $z)) - 0.1111111; $f1 = ($y + 1) - $f; if($d < $f1){ - $this->air -= 1*$tickDiff; + $this->air -= $tickDiff; if($this->air <= -20){ $this->harm(2, "water"); $this->air = 0; //harm every 1 second @@ -515,7 +527,7 @@ class Entity extends Position public function moveFlying($strafe, $forward, $speed){ //TODO rename? $v4 = $strafe*$strafe + $forward*$forward; - if($v4 >= 1.0e-4){ //TODO dont use ugly scienfitic notation + if($v4 >= 0.0001){ $v4 = sqrt($v4); if($v4 < 1) $v4 = 1; @@ -532,14 +544,44 @@ class Entity extends Position } public function updateEntityMovement(){} - + + public function doBlocksCollision(){ + $minX = floor($this->boundingBox->minX + 0.001); + $minY = floor($this->boundingBox->minY + 0.001); + $minZ = floor($this->boundingBox->minZ + 0.001); + $maxX = floor($this->boundingBox->maxX - 0.001); + $maxY = floor($this->boundingBox->maxY - 0.001); + $maxZ = floor($this->boundingBox->maxZ - 0.001); + + for($x = $minX; $x <= $maxX; ++$x){ + for($y = $minY; $y <= $maxY; ++$y){ + for($z = $minZ; $z <= $maxZ; ++$z){ + $id = $this->level->level->getBlockID($x, $y, $z); + + if($id > 0){ + Block::$class[$id]::onEntityCollidedWithBlock($this->level, $x, $y, $z, $this); + } + } + } + } + + } + public function move($dx, $dy, $dz){ - //TODO ySize? $movX = $this->x; $movY = $this->y; $movZ = $this->z; - //TODO isInWeb + if($this->inWeb){ + $this->inWeb = false; + $dx *= 0.25; + $dy *= 0.05; + $dz *= 0.25; + + $this->speedX = $this->speedY = $this->speedZ = 0; + } + + $savedDX = $dx; $savedDY = $dy; $savedDZ = $dz; @@ -636,12 +678,21 @@ class Entity extends Position //TODO more stuff -> onEntityWalking - //TODO block collisions? + $this->doBlocksCollision(); - //TODO deal fire damage here if($this->level->isBoundingBoxOnFire($this->boundingBox->contract(0.001, 0.001, 0.001))){ - + $this->harm(1, "fire"); + if($this->inWater){ + ++$this->fire; + if($this->fire == 0) $this->fire = 8; + } + }else if($this->fire <= 0){ + $this->fire = -$this->fireResistance; + } + + if($this->inWater && $this->fire > 0){ + $this->fire = -$this->fireResistance; } } @@ -1285,7 +1336,7 @@ class Entity extends Position if (! $this->canBeAttacked) { return false; } - $dmg = $this->applyArmor($dmg, $cause); //TODO HURTCAM + $dmg = $this->applyArmor($dmg, $cause); $ret = $this->setHealth(max(- 128, $this->getHealth() - ((int) $dmg)), $cause, $force); if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) { diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 4d036849..7e8d711d 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -86,8 +86,13 @@ class ItemEntity extends Entity{ $var1 = (int)$this->x != (int)$this->lastX || (int)$this->y != (int)$this->lastY || (int)$this->z != (int)$this->lastZ; - if($var1 || $this->counter % 25 == 0/* || $this->ticksExisted % 25 == 0*/){ //TODO ticksExisted(should be alternative in nc already) - //TODO check material + if($var1 || $this->counter % 25 == 0){ + $blockIDAt = $this->level->level->getBlockID(floor($this->x), floor($this->y), floor($this->z)); + if($blockIDAt == LAVA || $blockIDAt == STILL_LAVA){ + $this->speedY = 0.2; + $this->speedX = (lcg_value() - lcg_value()) * 0.2; + $this->speedZ = (lcg_value() - lcg_value()) * 0.2; + } $this->searchForOtherItemsNearby(); //not in vanilla 0.8.1 } diff --git a/src/material/Block.php b/src/material/Block.php index 17c89272..4884c85e 100755 --- a/src/material/Block.php +++ b/src/material/Block.php @@ -175,6 +175,9 @@ abstract class Block extends Position{ public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ return [new AxisAlignedBB($x, $y, $z, $x, $y, $z)]; } + public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){} + + public function __construct($id, $meta = 0, $name = "Unknown"){ $this->id = (int) $id; $this->meta = (int) $meta; diff --git a/src/material/block/nonfull/CobwebBlock.php b/src/material/block/nonfull/CobwebBlock.php index b969f4a4..0982b960 100644 --- a/src/material/block/nonfull/CobwebBlock.php +++ b/src/material/block/nonfull/CobwebBlock.php @@ -18,5 +18,9 @@ class CobwebBlock extends FlowableBlock{ array(COBWEB, 0, 1), ]; } - } + } + + public static function onEntityCollidedWithBlock(Level $level, $x, $y, $z, Entity $entity){ + $entity->setInWeb(); + } } \ No newline at end of file diff --git a/src/world/Level.php b/src/world/Level.php index 12e80a1b..0c1a8eb7 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -454,11 +454,11 @@ class Level{ public function isBoundingBoxOnFire(AxisAlignedBB $bb){ $minX = floor($bb->minX); - $maxX = ceil($bb->maxX); + $maxX = floor($bb->maxX + 1); $minY = floor($bb->minY); - $maxY = ceil($bb->maxY); + $maxY = floor($bb->maxY + 1); $minZ = floor($bb->minZ); - $maxZ = ceil($bb->maxZ); + $maxZ = floor($bb->maxZ + 1); for($x = $minX; $x < $maxX; ++$x){ for($y = $minY; $y < $maxY; ++$y){ From f200683643dfb0ff3c1e9edc0d9664b6da27f81c Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 31 Jan 2024 05:41:39 +0300 Subject: [PATCH 27/34] fixed minecart --- src/PocketMinecraftServer.php | 2 +- src/entity/object/Minecart.php | 1 - src/material/item/generic/MinecartItem.php | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 71c710c8..bc2c0d7f 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -621,7 +621,7 @@ class PocketMinecraftServer{ ++$actionCount; try{ $return = @call_user_func($this->schedule[$cid][0] ?? function(){}, $this->schedule[$cid][1], $this->schedule[$cid][2]); //somehow args can be null - }catch(Throwable $e){ + }catch(TypeError $e){ $return = false; } }else{ diff --git a/src/entity/object/Minecart.php b/src/entity/object/Minecart.php index 6bd37451..32145414 100644 --- a/src/entity/object/Minecart.php +++ b/src/entity/object/Minecart.php @@ -66,7 +66,6 @@ class Minecart extends Vehicle{ $this->width = 0.98; $this->height = 0.7; $this->yOffset = $this->height / 2; - $this->update(); } public function getDrops(){ diff --git a/src/material/item/generic/MinecartItem.php b/src/material/item/generic/MinecartItem.php index d1fd8104..a1924adb 100755 --- a/src/material/item/generic/MinecartItem.php +++ b/src/material/item/generic/MinecartItem.php @@ -15,7 +15,7 @@ class MinecartItem extends Item{ "x" => $target->getX() + 0.5, "y" => $target->getY() + 0.8, "z" => $target->getZ() + 0.5, - ]; + ]; $e = $server->api->entity->add($level, ENTITY_OBJECT, OBJECT_MINECART, $data); $server->api->entity->spawnToAll($e); if(($player->gamemode & 0x01) === 0x00){ From 0ca6becc6a4d4daab2718a2bf2274c17ff6388cd Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 31 Jan 2024 05:58:05 +0300 Subject: [PATCH 28/34] lava movement --- src/entity/Entity.php | 66 +++++++--------------------- src/entity/Living.php | 16 +++++-- src/entity/ai/tasks/TaskSwimming.php | 4 +- src/entity/object/FallingSand.php | 12 ----- src/world/Level.php | 21 ++++++++- 5 files changed, 51 insertions(+), 68 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 99fec561..03d536b1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -97,7 +97,7 @@ class Entity extends Position */ public $fireResistance = 1; public $inWeb; - + public $inLava; function __construct(Level $level, $eid, $class, $type = 0, $data = array()) { $this->random = new Random(); @@ -338,6 +338,11 @@ class Entity extends Position } } + public function handleLavaMovement(){ //TODO maybe try merging with water? + $this->inLava = $this->level->isLavaInBB($this->boundingBox->expand(-0.1, -0.4, -0.1)); + return $this->inLava; + } + public function handleWaterMovement(){ if($this->level->handleMaterialAcceleration($this->boundingBox->expand(0, -0.4, 0)->contract(0.001, 0.001, 0.001), 0, $this)){ $this->fallDistance = 0; @@ -419,6 +424,13 @@ class Entity extends Position } } + if(!$this->isPlayer()) { + if($this->handleLavaMovement()){ + //TODO this.setOnFireFromLava(); + $this->fallDistance *= 0.5; + } + } + $startX = floor($this->boundingBox->minX); $startY = floor($this->boundingBox->minY); $startZ = floor($this->boundingBox->minZ); @@ -590,11 +602,11 @@ class Entity extends Position $aABB = $this->boundingBox->addCoord($dx, $dy, $dz); $x0 = floor($aABB->minX); - $x1 = floor($aABB->maxX) + 1; + $x1 = floor($aABB->maxX + 1); $y0 = floor($aABB->minY); - $y1 = floor($aABB->maxY) + 1; + $y1 = floor($aABB->maxY + 1); $z0 = floor($aABB->minZ); - $z1 = floor($aABB->maxZ) + 1; + $z1 = floor($aABB->maxZ + 1); for($x = $x0; $x < $x1; ++$x){ for($y = $y0; $y < $y1; ++$y){ @@ -750,52 +762,6 @@ class Entity extends Position $this->updateLast(); $this->updatePosition(); //TODO shouldnt be called in Entity $this->updateEntityMovement(); - /*$update = false; - if($this->speedX > -self::MIN_POSSIBLE_SPEED && $this->speedX < self::MIN_POSSIBLE_SPEED){ - $this->speedX = 0; - } - if($this->speedZ > -self::MIN_POSSIBLE_SPEED && $this->speedZ < self::MIN_POSSIBLE_SPEED){ - $this->speedZ = 0; - } - if($this->speedY > -self::MIN_POSSIBLE_SPEED && $this->speedY < self::MIN_POSSIBLE_SPEED){ - $this->speedY = 0; - } - - $horizontalMultiplyFactor = 0.91; - if($this->onGround){ - $horizontalMultiplyFactor = 0.54; - $b = $this->level->level->getBlockID(floor($this->x), floor($this->boundingBox->minY) - 1, floor($this->z)); - if($b > 0){ - $horizontalMultiplyFactor = StaticBlock::getSlipperiness($b) * 0.91; - } - } - - if($this->inWater){ - $this->speedX *= 0.8; - $this->speedY *= 0.8; - $this->speedZ *= 0.8; - }else{ - $this->speedX *= $horizontalMultiplyFactor; - $this->speedY *= 0.98; - $this->speedZ *= $horizontalMultiplyFactor; - } - - $savedSpeedY = $this->speedY; - $savedSpeedX = $this->speedX; - $savedSpeedZ = $this->speedZ; - $ultraMegaOldBB = clone $this->boundingBox; - $stepSuccess = false; - $beforeStepSpeedY = 0; - - if($this->enableAutojump && $this instanceof Creature){ - if(!$stepSuccess && $fallingFlag && ($this->speedX != $savedSpeedX || $this->speedZ != $savedSpeedZ)){ - $this->ai->mobController->setJumping(true); - }else{ - $this->ai->mobController->setJumping(false); - } - } - }*/ - if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ $this->server->api->handle("entity.move", $this); diff --git a/src/entity/Living.php b/src/entity/Living.php index 0f836bc7..4bbcb5cd 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -185,9 +185,19 @@ abstract class Living extends Entity implements Pathfindable{ if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){ $this->speedY = 0.3; } - } - //TODO lava - else{ + }else if($this->inLava){ + $prevPosY = $this->y; + $this->moveFlying($strafe, $forward, 0.02); + $this->move($this->speedX, $this->speedY, $this->speedZ); + $this->speedX *= 0.5; + $this->speedY *= 0.5; + $this->speedZ *= 0.5; + $this->speedY -= 0.02; + + if($this->isCollidedHorizontally && $this->isFree($this->speedX, $this->speedY + 0.6 - $this->y + $prevPosY, $this->speedZ)){ + $this->speedY = 0.3; + } + }else{ $friction = 0.91; if($this->onGround){ diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index 59b5700a..f074a9a9 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -14,7 +14,7 @@ class TaskSwimming extends TaskBase public function onUpdate(EntityAI $ai) { - if(!$ai->entity->inWater){ + if(!$ai->entity->inWater || !$ai->entity->inLava){ $this->reset(); return; } @@ -26,7 +26,7 @@ class TaskSwimming extends TaskBase public function canBeExecuted(EntityAI $ai) { - return $ai->entity->inWater; + return $ai->entity->inWater || $ai->entity->inLava; } } diff --git a/src/entity/object/FallingSand.php b/src/entity/object/FallingSand.php index 2a43338f..c29a8f94 100644 --- a/src/entity/object/FallingSand.php +++ b/src/entity/object/FallingSand.php @@ -36,18 +36,6 @@ class FallingSand extends Entity{ $y = floor($this->y); $z = floor($this->z); - if($this->fallTime == 1){ - /* i dont think this is needed here? - * if (this.worldObj.getBlockId(var1, var2, var3) != this.blockID) - { - this.setDead(); - return; - } - - this.worldObj.setBlockToAir(var1, var2, var3); - */ - } - if($this->onGround){ $this->speedX *= 0.7; $this->speedZ *= 0.7; diff --git a/src/world/Level.php b/src/world/Level.php index 0c1a8eb7..3e39ccd9 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -77,7 +77,26 @@ class Level{ } //TODO mayPlace - + public function isLavaInBB($aabb){ + $minX = floor($aabb->minX); + $maxX = floor($aabb->maxX + 1); + $minY = floor($aabb->minY); + $maxY = floor($aabb->maxY + 1); + $minZ = floor($aabb->minZ); + $maxZ = floor($aabb->maxZ + 1); + + for($x = $minX; $x < $maxX; ++$x){ + for($y = $minY; $y < $maxY; ++$y){ + for($z = $minZ; $z < $maxZ; ++$z){ + $blockId = $this->level->getBlockID($x, $y, $z); + + if($blockId == LAVA || $blockId == STILL_LAVA) return true; + } + } + } + + return false; + } public function handleMaterialAcceleration(AxisAlignedBB $aabb, $materialType, Entity $entity){ $minX = floor($aabb->minX); From d0a262a3da9433424d4386d212e555416557b3e6 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 31 Jan 2024 07:14:13 +0300 Subject: [PATCH 29/34] attackplayer task fix --- src/dependencies.php | 1 - src/entity/ai/MobController.php | 1 - src/entity/ai/tasks/TaskAttackPlayer.php | 115 ++++++++++++++---- .../ai/tasks/TaskDestroyServerPerformance.php | 89 -------------- src/entity/ai/tasks/TaskMate.php | 72 ----------- src/entity/ai/tasks/TaskSwimming.php | 2 +- src/entity/ai/tasks/TaskTempt.php | 74 ----------- src/entity/monster/Zombie.php | 1 + src/world/Level.php | 7 +- 9 files changed, 100 insertions(+), 262 deletions(-) delete mode 100644 src/entity/ai/tasks/TaskDestroyServerPerformance.php delete mode 100644 src/entity/ai/tasks/TaskMate.php delete mode 100644 src/entity/ai/tasks/TaskTempt.php diff --git a/src/dependencies.php b/src/dependencies.php index 0119e58d..89733a8b 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -96,7 +96,6 @@ require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php"); require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php"); -require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php"); require_once(FILE_PATH . "/src/entity/Rideable.php"); require_once(FILE_PATH . "/src/entity/Breedable.php"); diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 1c30d638..591993b7 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -104,7 +104,6 @@ class MobController $v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); $this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer()); - if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); } } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index 5f886853..31f357ed 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -1,42 +1,111 @@ moveSpeed = $speed; + $this->rangeSquared = $range*$range; + } + public function onStart(EntityAI $ai) { $this->selfCounter = 1; + console("found!"); } + + public function onUpdate(EntityAI $ai) { - if(!($this->target instanceof Entity) || ($ai->entity instanceof Spider && $ai->entity->level->isDay()) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 256) || $this->target->level->getName() != $ai->entity->level->getName()){ + if(!$this->isTargetValid($ai)){ $this->reset(); - return; + return false; } + $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->moveSpeed); + $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); - //TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); - $ai->mobController->lookOn($this->target); - $mult = $ai->entity instanceof Spider ? 1 : 2; - $radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult); - if(Utils::distance_noroot($this->target, $ai->entity) <= $radius && $this->attackTime <= 0){ - $ai->entity->attackEntity($this->target); - $this->attackTime = 20; + --$this->attackCounter; + $v1 = $ai->entity->width * $ai->entity->width * 4; + + $e = $ai->entity; + $t = $e->target; + $xDiff = ($t->x - $e->x); + $yDiff = ($t->y - $e->y); + $zDiff = ($t->z - $e->z); + $dist = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); + if($dist <= $v1){ + if($this->attackCounter <= 0){ + $this->attackCounter = 20; + + $e->attackEntity($t); + + } } - --$this->attackTime; } - public function canBeExecuted(EntityAI $ai) - { - $target = $this->findTarget($ai->entity, 16); - if(($ai->entity instanceof Spider && !$ai->entity->level->isDay()) || $target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ - $this->target = $target; //TODO get rid of it - $ai->entity->target = $target; - return true; + + + public function isTargetValid(EntityAI $ai){ + $e = $ai->entity; //TODO sometimes it keeps being valid even after getting invalid (/gm 1 -> /gm 0 switch) + if($e->target instanceof Entity){ + $t = $e->target; + $xDiff = ($t->x - $e->x); + $yDiff = ($t->y - $e->y); + $zDiff = ($t->z - $e->z); + return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared; } - return false; } + + public function tryTargeting(EntityAI $ai){ + $e = $ai->entity; + if($e->target instanceof Entity){ + $t = $e->target; + $xDiff = ($t->x - $e->x); + $yDiff = ($t->y - $e->y); + $zDiff = ($t->z - $e->z); + if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= $this->rangeSquared){ + return true; + } + } + $bestTargetDistance = INF; + $closestTarget = null; + foreach($e->level->players as $p){ + if($p->spawned){ + $pt = $p->entity; + $xDiff = $pt->x - $e->x; + $yDiff = $pt->y - $e->y; + $zDiff = $pt->z - $e->z; + $d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff); + if($d <= $this->rangeSquared){ + if($bestTargetDistance >= $d){ + $closestTarget = $pt; + $bestTargetDistance = $d; + } + } + } + } + + if($closestTarget != null){ + $e->target = $closestTarget; //TODO dont save entity object ? + return true; + } + return false; + } + + public function canBeExecuted(EntityAI $ai) + { + return $this->tryTargeting($ai); + } + + public function onEnd(EntityAI $ai) + { + + } + } diff --git a/src/entity/ai/tasks/TaskDestroyServerPerformance.php b/src/entity/ai/tasks/TaskDestroyServerPerformance.php deleted file mode 100644 index 8903f303..00000000 --- a/src/entity/ai/tasks/TaskDestroyServerPerformance.php +++ /dev/null @@ -1,89 +0,0 @@ -server = ServerAPI::request(); - } - - public function onStart(EntityAI $ai) - { - $this->selfCounter = 1; - } - - public function onUpdate(EntityAI $ai) - { - if(!($this->target instanceof Entity) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 256) || $this->target->level->getName() != $ai->entity->level->getName()){ - $this->reset(); - return; - } - if(Utils::distance_noroot($this->target, $ai->entity) > 100){ - //TODO fix $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false); - }else{ - - } - $ai->mobController->lookOn($this->target); - - if(--$this->attackTime <= 0){ - $this->rangedAttack($ai->entity, $this->target); - $this->attackTime = 60; - } - } - - /** - * - * @param Entity $selfEntity - * @param Entity $target - */ - public function rangedAttack($selfEntity, $target){ - $d = [ - "x" => $selfEntity->x, - "y" => $selfEntity->y + 1.6, - "z" => $selfEntity->z, - "yaw" => $selfEntity->yaw, - "pitch" => $selfEntity->pitch - ]; - /** - * @var Arrow $arrow - */ - $arrow = $this->server->api->entity->add($selfEntity->level, ENTITY_OBJECT, OBJECT_ARROW, $d); - $arrow->shotByEntity = true; - $arrow->shooterEID = $selfEntity->eid; - $posY = ($target->y + $target->getEyeHeight() - 0.1); - $diffX = $target->x - $selfEntity->x; - $diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY; - $diffZ = $target->z - $selfEntity->z; - $v12 = sqrt($diffX * $diffX + $diffZ * $diffZ); - if($v12 >= 1.0e-7){ - $yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90; - $pitch = -((atan2($diffY, $v12) * 180) / M_PI); - $v16 = $diffX / $v12; - $v18 = $diffZ / $v12; - $arrow->x = $selfEntity->x + $v16; - $arrow->y = $posY; - $arrow->z = $selfEntity->z + $v18; - $arrow->yaw = $yaw; - $arrow->pitch = $pitch; - $arrow->updateAABB(); - $v20 = $v12 * 0.2; - $arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12); - $this->server->api->entity->spawnToAll($arrow); - } - } - - public function canBeExecuted(EntityAI $ai) - { - $target = $this->findTarget($ai->entity, 16); - if(($ai->entity instanceof Spider && !$ai->entity->level->isDay()) || $target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){ - $this->target = $target; //TODO get rid of it - $ai->entity->target = $target; - return true; - } - - return false; - } -} - diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php deleted file mode 100644 index 8f8a093f..00000000 --- a/src/entity/ai/tasks/TaskMate.php +++ /dev/null @@ -1,72 +0,0 @@ -selfCounter = 60; - } - - public function onEnd(EntityAI $ai) - { - $this->selfCounter = 0; - $this->targetMateID = null; - $ai->entity->pitch = 0; - } - - public function onUpdate(EntityAI $ai) - { - $movTo = $ai->entity->level->entityList[$this->targetMateID] ?? false; - if(!($movTo instanceof Entity) || $movTo->dead || !$movTo->isInLove()){ - $this->reset(); - $this->onEnd($ai); - return; - } - //TODO fix $ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false); - $ai->mobController->lookOn($movTo); - - --$this->selfCounter; - if($this->selfCounter <= 0 && $ai->entity->distanceSquared($movTo) < 9){ - $this->createBaby($ai, $movTo); - } - } - public function createBaby(EntityAI $ai, Entity $entpar2){ - /** - * @var Animal $baby - */ - $baby = $ai->entity->breed(); - //this.theAnimal.setGrowingAge(6000); TODO growing age? - //this.targetMate.setGrowingAge(6000); - - $ai->entity->resetInLove(); - $entpar2->resetInLove(); - $baby->setAge(-24000); - } - - public function findMate(EntityAI $ai){ - $distance = 8.0*8.0; - $minDist = $distance;; //TODO check bb intersection? - $matedEID = null; - - foreach($ai->entity->level->entitiesInLove as $eid => $_){ - $e = $ai->entity->level->entityList[$eid] ?? false; - if($e instanceof Entity){ - $d = $ai->entity->distanceSquared($e); - if($ai->entity->canMate($e) && $d < $minDist){ - $minDist = $d; - $matedEID = $e->eid; - } - } - } - return $matedEID; - } - - public function canBeExecuted(EntityAI $ai) - { - return $ai->entity->isInLove() && ($this->targetMateID = $this->findMate($ai)) != null; - } - -} - diff --git a/src/entity/ai/tasks/TaskSwimming.php b/src/entity/ai/tasks/TaskSwimming.php index f074a9a9..61842afb 100644 --- a/src/entity/ai/tasks/TaskSwimming.php +++ b/src/entity/ai/tasks/TaskSwimming.php @@ -14,7 +14,7 @@ class TaskSwimming extends TaskBase public function onUpdate(EntityAI $ai) { - if(!$ai->entity->inWater || !$ai->entity->inLava){ + if(!$ai->entity->inWater && !$ai->entity->inLava){ $this->reset(); return; } diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php deleted file mode 100644 index 0106c95a..00000000 --- a/src/entity/ai/tasks/TaskTempt.php +++ /dev/null @@ -1,74 +0,0 @@ -selfCounter = 1; - } - - public function onEnd(EntityAI $ai) - { - unset($this->target); - unset($ai->entity->target); - $ai->entity->pitch = 0; - } - - public function onUpdate(EntityAI $ai) - { - if(!($this->target instanceof Entity) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 100) || !$ai->entity->isFood($this->target->player->getHeldItem()->getID()) || $this->target->level->getName() != $ai->entity->level->getName()){ - $this->reset(); - return; - } - - //TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); - $ai->mobController->lookOn($this->target); - } - - public function canBeExecuted(EntityAI $ai) - { - if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){ - return false; - } - $target = $this->findTarget($ai->entity, 10); - if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer() && $ai->entity->isFood($target->player->getHeldItem()->getID())){ - $this->target = $target; - $ai->entity->target = $target; - return true; - } - - return false; - } - - protected function findTarget($e, $r){ - $svd = null; - $svdDist = -1; - foreach($e->level->players as $p){ - $p = $p->entity; - if(Utils::distance_noroot($e, $p) > $r*$r){ - continue; - } - if($svdDist === -1){ - $svdDist = Utils::distance_noroot($e, $p); - $svd = $p; - continue; - } - if($svd != null && $svdDist === 0){ - $svd = $p; - } - if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){ - $svdDist = $cd; - $svd = $p; - } - } - - if($svd == null){ - return null; - } - - return $svd; - } -} - diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index a07458f1..16153e9a 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -11,6 +11,7 @@ class Zombie extends Monster{ $this->ai->addTask(new TaskRandomWalk(0.25)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); + $this->ai->addTask(new TaskAttackPlayer(0.25, 16)); } public function getArmorValue(){ diff --git a/src/world/Level.php b/src/world/Level.php index 3e39ccd9..fd77961c 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -14,7 +14,12 @@ class Level{ public $entityListPositioned = []; public $entitiesInLove = []; - public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0; + /** + * @var Player[] + */ + public $players = []; + + public $tiles, $blockUpdates, $nextSave, $level, $mobSpawner, $totalMobsAmount = 0; private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime; public $randInt1, $randInt2; From dee744e7091c980dfb851e9075e3021f7df484cb Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 1 Feb 2024 07:19:59 +0300 Subject: [PATCH 30/34] simple autojump, more vanilla speed --- src/entity/Entity.php | 8 +++----- src/entity/Living.php | 12 +++++++++++- src/entity/ai/MobController.php | 15 +++++++++++---- src/entity/ai/tasks/TaskAttackPlayer.php | 6 +++--- src/entity/ai/tasks/TaskPanic.php | 8 ++++---- src/entity/ai/tasks/TaskRandomWalk.php | 8 ++++---- src/entity/animal/Chicken.php | 4 ++-- src/entity/animal/Cow.php | 5 +++-- src/entity/animal/Pig.php | 4 ++-- src/entity/animal/Sheep.php | 4 ++-- src/entity/monster/Creeper.php | 2 +- src/entity/monster/PigZombie.php | 2 +- src/entity/monster/Skeleton.php | 3 ++- src/entity/monster/Spider.php | 2 +- src/entity/monster/Zombie.php | 4 ++-- 15 files changed, 52 insertions(+), 35 deletions(-) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 03d536b1..23e16b43 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -666,9 +666,7 @@ class Entity extends Position $dy = $cy; $dz = $cz; $this->boundingBox->setBB($aabb1); - }//else{ TODO autojump - // $stepSuccess = true; - //} + } } @@ -762,7 +760,7 @@ class Entity extends Position $this->updateLast(); $this->updatePosition(); //TODO shouldnt be called in Entity $this->updateEntityMovement(); - + $update = false; if($this->lastX != $this->x || $this->lastZ != $this->z || $this->lastY != $this->z){ $this->server->api->handle("entity.move", $this); $update = true; @@ -1214,7 +1212,7 @@ class Entity extends Position $this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0); } - public function getSpeed() + public function getSpeed() //TODO rename to getBaseSpeed { return $this->speed; } diff --git a/src/entity/Living.php b/src/entity/Living.php index 4bbcb5cd..c1f09d43 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -129,7 +129,7 @@ abstract class Living extends Entity implements Pathfindable{ if($this->jumping){ if(!$this->inWater){ //TODO also lava if($this->onGround && $this->jumpTicks <= 0){ - //TODO jump $this->jump(); + $this->jump(); $this->jumpTicks = 10; } }else{ @@ -165,6 +165,12 @@ abstract class Living extends Entity implements Pathfindable{ } + public function jump(){ + $this->speedY = 0.42; + + //TODO also set AirBorne? + } + public function counterUpdate(){ parent::counterUpdate(); if($this->jumpTicks > 0) --$this->jumpTicks; @@ -245,6 +251,10 @@ abstract class Living extends Entity implements Pathfindable{ $this->speedY *= 0.98; $this->speedX *= $friction; $this->speedZ *= $friction; + + if($this->isCollidedHorizontally && $this->enableAutojump){ + $this->ai->mobController->setJumping(true); //non vanilla + } } //TODO limbYaw - is it even needed? diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php index 591993b7..519db655 100644 --- a/src/entity/ai/MobController.php +++ b/src/entity/ai/MobController.php @@ -23,7 +23,7 @@ class MobController protected $jumping; public $moveToX, $moveToY, $moveToZ; - public $speed; + public $speedMultiplier; public $updateMove = false; public $lookX, $lookY, $lookZ; @@ -52,14 +52,14 @@ class MobController $this->moveToX = $x; $this->moveToY = $y; $this->moveToZ = $z; - $this->speed = $speed; + $this->speedMultiplier = $speed; $this->updateMove = true; } public function setMovingOffset($x, $y, $z, $speed){ $this->moveToX = $this->entity->x + ($x); $this->moveToY = $this->entity->y + ($y); $this->moveToZ = $this->entity->z + ($z); - $this->speed = $speed; + $this->speedMultiplier = $speed; $this->updateMove = true; } @@ -103,7 +103,14 @@ class MobController if($v8 >= 2.500000277905201E-7){ //TODO convert notation $v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90; $this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30); - $this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer()); + $this->entity->setAIMoveSpeed($this->speedMultiplier * $this->entity->getSpeed() * $this->entity->getSpeedModifer()); + //TODO 0.8.1 doesnt seem to make 2 multiplications here + /* + * v15 = this->controlledEntity; + speedMultiplier = this->speedMultiplier; + v17 = v15->vtable->_virt_getBaseSpeed(v15); + Mob::setSpeed(v15, speedMultiplier * v17); + */ if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true); } } diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index 31f357ed..cc8a4bc6 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -2,13 +2,13 @@ class TaskAttackPlayer extends TaskBase { - public $moveSpeed; + public $speedMultiplier; public $rangeSquared; public $attackCounter; public function __construct($speed, $range){ - $this->moveSpeed = $speed; + $this->speedMultiplier = $speed; $this->rangeSquared = $range*$range; } @@ -25,7 +25,7 @@ class TaskAttackPlayer extends TaskBase $this->reset(); return false; } - $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->moveSpeed); + $ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->speedMultiplier); $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); --$this->attackCounter; diff --git a/src/entity/ai/tasks/TaskPanic.php b/src/entity/ai/tasks/TaskPanic.php index 5793708f..a010802d 100644 --- a/src/entity/ai/tasks/TaskPanic.php +++ b/src/entity/ai/tasks/TaskPanic.php @@ -4,9 +4,9 @@ class TaskPanic extends TaskBase { protected $randX = 2, $randZ = 2; - public $moveSpeed; - public function __construct($moveSpeed){ - $this->moveSpeed = $moveSpeed; + public $speedMultiplier; + public function __construct($speedMultiplier){ + $this->speedMultiplier = $speedMultiplier; } public function onStart(EntityAI $ai) @@ -34,7 +34,7 @@ class TaskPanic extends TaskBase { --$this->selfCounter; if($this->selfCounter % 20 === 0) $this->regenerateRandXZ(); - $ai->mobController->setMovingOffset($this->randX, 0, $this->randZ, $this->moveSpeed); + $ai->mobController->setMovingOffset($this->randX, 0, $this->randZ, $this->speedMultiplier); } public function canBeExecuted(EntityAI $ai) diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index 28f8a5c2..cb9d38ee 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -3,9 +3,9 @@ class TaskRandomWalk extends TaskBase { private $x, $z; - public $moveSpeed; - public function __construct($moveSpeed){ - $this->moveSpeed = $moveSpeed; + public $speedMultiplier; + public function __construct($speedMultiplier){ + $this->speedMultiplier = $speedMultiplier; } public function onStart(EntityAI $ai) @@ -31,7 +31,7 @@ class TaskRandomWalk extends TaskBase return false; //TODO Better way: block movement } --$this->selfCounter; - $ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->moveSpeed); + $ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->speedMultiplier); //TODO fix $ai->mobController->moveNonInstant($this->x, 0, $this->z); } diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 5e0c2c4b..5fd27a3a 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -11,9 +11,9 @@ class Chicken extends Animal{ $this->setName('Chicken'); $this->setSpeed(0.25); - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskPanic(1.5)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index 02bd7f69..fdcd4779 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -7,9 +7,10 @@ class Cow extends Animal{ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Cow"); - $this->ai->addTask(new TaskRandomWalk(0.2)); + $this->setSpeed(0.2); + $this->ai->addTask(new TaskRandomWalk(0.8)); $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskPanic(1.5)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 0c152f82..f307c523 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -10,9 +10,9 @@ class Pig extends Animal implements Rideable{ $this->setName("Pig"); $this->setSpeed(0.25); - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskPanic(1.5)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 79e66056..3108d954 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -13,9 +13,9 @@ class Sheep extends Animal{ $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->setSpeed(0.25); - $this->ai->addTask(new TaskRandomWalk(0.25)); //in 1.5.2 it is 0.23, but according to ida it should be 1.0 which is 0.25 if converted to 1.5.2? + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAtPlayer(6)); - $this->ai->addTask(new TaskPanic(0.375)); + $this->ai->addTask(new TaskPanic(1.5)); $this->ai->addTask(new TaskEatTileGoal()); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 750d80e8..6d0ca6ad 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -12,7 +12,7 @@ class Creeper extends Monster{ $this->setSpeed(0.25); $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index 6be561e8..e4cc616e 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -9,7 +9,7 @@ class PigZombie extends Monster{ $this->setSpeed(0.25); $this->ai->addTask(new TaskLookAround()); - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskSwimming()); } public function getAttackDamage(){ diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 749f0d06..0d79e269 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -7,9 +7,10 @@ class Skeleton extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Skeleton"); $this->ai->removeTask("TaskAttackPlayer"); + $this->setSpeed(0.25); //$this->ai->addTask(new TaskDestroyServerPerformance()); - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index d9adf87e..9087321f 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -7,7 +7,7 @@ class Spider extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setName("Spider"); $this->setSpeed(0.3); - $this->ai->addTask(new TaskRandomWalk(0.25)); //TODO this mob doesnt use newAI -> speed is not correct + $this->ai->addTask(new TaskRandomWalk(1.0)); //TODO this mob doesnt use newAI -> speed is not correct $this->ai->addTask(new TaskLookAround()); //TODO this mob doesnt use newAI $this->ai->addTask(new TaskSwimming()); } diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 16153e9a..55e3d51e 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -8,10 +8,10 @@ class Zombie extends Monster{ $this->setName("Zombie"); $this->setSpeed(0.23); - $this->ai->addTask(new TaskRandomWalk(0.25)); + $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskSwimming()); - $this->ai->addTask(new TaskAttackPlayer(0.25, 16)); + $this->ai->addTask(new TaskAttackPlayer(1.0, 16)); //TODO fix range? } public function getArmorValue(){ From 908cff1317b87ef3dbd2d3db0450d04b8451d1d1 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 Feb 2024 04:22:20 +0300 Subject: [PATCH 31/34] fixed packet spam, removed item merging, various fixes --- src/Player.php | 33 ++++++++++++------- src/entity/Entity.php | 6 ++-- src/entity/animal/Chicken.php | 2 +- src/entity/object/ItemEntity.php | 6 +++- .../protocol/packet/LevelEventPacket.php | 4 +++ src/world/Level.php | 17 ++++++---- src/world/generator/WorldGenerator.php | 2 ++ 7 files changed, 46 insertions(+), 24 deletions(-) diff --git a/src/Player.php b/src/Player.php index 5caa83f5..2007b54a 100755 --- a/src/Player.php +++ b/src/Player.php @@ -594,8 +594,6 @@ class Player{ } public function sendInventorySlot($s){ - $this->sendInventory(); - return; $s = (int) $s; if(!isset($this->inventory[$s])){ $pk = new ContainerSetSlotPacket; @@ -754,7 +752,7 @@ class Player{ $pk->target = $data["entity"]->eid; $this->dataPacket($pk); if(($this->gamemode & 0x01) === 0x00){ - $this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack, false); + $this->addItem($data["entity"]->type, $data["entity"]->meta, $data["entity"]->stack, true); } switch($data["entity"]->type){ case WOOD: @@ -916,6 +914,15 @@ class Player{ } if($m !== ""){ + + /*$pk = new LevelEventPacket(); + $pk->evid = LevelEventPacket::EVENT_OPEN_DOOR_SOUND; + $pk->x = $this->entity->x; + $pk->y = $this->entity->y; + $pk->z = $this->entity->z; + $pk->data = 0; + $this->dataPacket($pk);*/ + $pk = new MessagePacket; $pk->source = ($author instanceof Player) ? $author->username : $author; $pk->message = TextFormat::clean($m); //Colors not implemented :( @@ -1207,15 +1214,17 @@ class Player{ $moveSent = false; $headSent = false; if($e->speedX != 0 || $e->speedY != 0 || $e->speedZ != 0 || $e->speedY != $e->lastSpeedY || $e->speedX != $e->lastSpeedX || $e->speedZ != $e->lastSpeedZ){ - $motion = new SetEntityMotionPacket(); - $motion->eid = $e->eid; - $motion->speedX = $e->speedX; - $motion->speedY = $e->speedY; - $motion->speedZ = $e->speedZ; - $motion->encode(); - $len += 1 + strlen($motion->buffer); - ++$packets; - $motionSent = true; + if(!($e->speedY < 0 && $e->onGround)){ + $motion = new SetEntityMotionPacket(); + $motion->eid = $e->eid; + $motion->speedX = $e->speedX; + $motion->speedY = $e->speedY; + $motion->speedZ = $e->speedZ; + $motion->encode(); + $len += 1 + strlen($motion->buffer); + ++$packets; + $motionSent = true; + } } if($e->x != $e->lastX || $e->y != $e->lastY || $e->z != $e->lastZ || $e->yaw != $e->lastYaw || $e->pitch != $e->lastPitch){ $move = new MoveEntityPacket_PosRot(); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 23e16b43..aa7882f1 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -441,7 +441,7 @@ class Entity extends Position 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; + $y = ((($i >> 1) % 2) - 0.5) * 0.1; $z = ((($i >> 2) % 2) - 0.5) * $this->width * 0.8; $blockX = (int) ($this->x + $x); @@ -609,8 +609,8 @@ class Entity extends Position $z1 = floor($aABB->maxZ + 1); for($x = $x0; $x < $x1; ++$x){ - for($y = $y0; $y < $y1; ++$y){ - for($z = $z0; $z < $z1; ++$z){ + for($z = $z0; $z < $z1; ++$z){ + for($y = $y0 - 1; $y < $y1; ++$y){ $b = $this->level->level->getBlockID($x, $y, $z); if($b != 0){ $blockBounds = Block::$class[$b]::getCollisionBoundingBoxes($this->level, $x, $y, $z, $this); diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 5fd27a3a..0cc62f63 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -24,7 +24,7 @@ class Chicken extends Animal{ public function updateEntityMovement(){ parent::updateEntityMovement(); - if($this->onGround && $this->speedY < 0) $this->speedY *= 0.6; + if(!$this->onGround && $this->speedY < 0) $this->speedY *= 0.6; if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){ $this->dropAnEgg(); diff --git a/src/entity/object/ItemEntity.php b/src/entity/object/ItemEntity.php index 7e8d711d..60bac8eb 100644 --- a/src/entity/object/ItemEntity.php +++ b/src/entity/object/ItemEntity.php @@ -94,7 +94,7 @@ class ItemEntity extends Entity{ $this->speedZ = (lcg_value() - lcg_value()) * 0.2; } - $this->searchForOtherItemsNearby(); //not in vanilla 0.8.1 + //$this->searchForOtherItemsNearby(); //not in vanilla 0.8.1, TODO reenable after fixing not correct count in inv } if($this->closed) return; @@ -112,6 +112,10 @@ class ItemEntity extends Entity{ if($this->onGround) $this->speedY *= -0.5; + if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; + if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; + if(abs($this->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0; + ++$this->age; //TODO despawn after age >= 6000 ?; } diff --git a/src/network/protocol/packet/LevelEventPacket.php b/src/network/protocol/packet/LevelEventPacket.php index dcddd653..50727e45 100644 --- a/src/network/protocol/packet/LevelEventPacket.php +++ b/src/network/protocol/packet/LevelEventPacket.php @@ -1,6 +1,10 @@ true, FARMLAND => true, @@ -150,15 +152,15 @@ class Level{ public function getCubes(Entity $e, AxisAlignedBB $aABB) { $aABBs = []; $x0 = floor($aABB->minX); - $x1 = ceil($aABB->maxX); + $x1 = floor($aABB->maxX + 1); $y0 = floor($aABB->minY); - $y1 = ceil($aABB->maxY); + $y1 = floor($aABB->maxY + 1); $z0 = floor($aABB->minZ); - $z1 = ceil($aABB->maxZ); + $z1 = floor($aABB->maxZ + 1); - for($x = $x0; $x <= $x1; ++$x) { - for($y = $y0; $y <= $y1; ++$y) { - for($z = $z0; $z <= $z1; ++$z) { + for($x = $x0; $x < $x1; ++$x) { + for($z = $z0; $z < $z1; ++$z) { + for($y = $y0 - 1; $y < $y1; ++$y) { $bid = $this->level->getBlockID($x, $y, $z); if($bid > 0){ $blockBounds = Block::$class[$bid]::getCollisionBoundingBoxes($this, $x, $y, $z, $e); //StaticBlock::getBoundingBoxForBlockCoords($b, $x, $y, $z); @@ -541,7 +543,8 @@ class Level{ } public function addBlockToSendQueue($x, $y, $z, $id, $meta){ - $this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta]; + if(!$this->forceDisableBlockQueue) + $this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta]; } public function setBlock(Vector3 $pos, Block $block, $update = true, $tiles = false, $direct = false){ diff --git a/src/world/generator/WorldGenerator.php b/src/world/generator/WorldGenerator.php index b0f2978b..18c0d1d7 100644 --- a/src/world/generator/WorldGenerator.php +++ b/src/world/generator/WorldGenerator.php @@ -29,6 +29,7 @@ class WorldGenerator{ } public function generate(){ + $this->level->forceDisableBlockQueue = true; $this->generator->init($this->level, $this->random); for($Z = 0; $Z < $this->width; ++$Z){ for($X = 0; $X < $this->width; ++$X){ @@ -49,6 +50,7 @@ class WorldGenerator{ $this->level->setSpawn($this->generator->getSpawn()); $this->level->save(true, true); + $this->level->forceDisableBlockQueue = false; } public function close(){ From cbc65e1f45c74d683fc3eed316a8d264208d6435 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 Feb 2024 05:11:06 +0300 Subject: [PATCH 32/34] fixed item entity saving --- src/entity/Entity.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/entity/Entity.php b/src/entity/Entity.php index aa7882f1..abc1d488 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -1272,6 +1272,8 @@ class Entity extends Position "Damage" => $this->meta, "Count" => $this->stack, ]; + + $data["id"] = 64; //ty shoghicp } return $data; } From c2e53d91c25c92a820a8ea8ed2088c9c5c560e50 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 Feb 2024 05:18:34 +0300 Subject: [PATCH 33/34] fixed spider and pigman ai --- src/entity/ai/tasks/TaskAttackPlayer.php | 3 ++- src/entity/monster/PigZombie.php | 3 ++- src/entity/monster/Spider.php | 1 + src/material/block/solid/IceBlock.php | 4 ---- 4 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index cc8a4bc6..bdea62c8 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -29,7 +29,8 @@ class TaskAttackPlayer extends TaskBase $ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed()); --$this->attackCounter; - $v1 = $ai->entity->width * $ai->entity->width * 4; + $mult = $ai->entity instanceof Spider ? 1 : 2; + $v1 = $ai->entity->width * $ai->entity->width * $mult*$mult; $e = $ai->entity; $t = $e->target; diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index e4cc616e..3d6859a7 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -1,5 +1,5 @@ setSize(0.3, 1.95); @@ -11,6 +11,7 @@ class PigZombie extends Monster{ $this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskRandomWalk(1.0)); $this->ai->addTask(new TaskSwimming()); + $this->ai->addTask(new TaskAttackPlayer(1.0, 16)); //TODO fix range? } public function getAttackDamage(){ return 5; diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index 9087321f..a6128c3b 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -10,6 +10,7 @@ class Spider extends Monster{ $this->ai->addTask(new TaskRandomWalk(1.0)); //TODO this mob doesnt use newAI -> speed is not correct $this->ai->addTask(new TaskLookAround()); //TODO this mob doesnt use newAI $this->ai->addTask(new TaskSwimming()); + $this->ai->addTask(new TaskAttackPlayer(1.0, 16)); //TODO fix range? } public function getDrops(){ diff --git a/src/material/block/solid/IceBlock.php b/src/material/block/solid/IceBlock.php index 6203a727..e4eb6d59 100644 --- a/src/material/block/solid/IceBlock.php +++ b/src/material/block/solid/IceBlock.php @@ -17,10 +17,6 @@ class IceBlock extends TransparentBlock{ return $ret; } - public static function getCollisionBoundingBoxes(Level $level, $x, $y, $z, Entity $entity){ - return [new AxisAlignedBB($x, $y, $z, $x+1, $y+1, $z+1)]; - } - public function onBreak(Item $item, Player $player){ if(($player->gamemode & 0x01) === 0){ $this->level->setBlock($this, new WaterBlock(), true, false, true); From 76942eeae185bedee44f6d3c3701854ff08cdcb0 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Wed, 14 Feb 2024 05:20:00 +0300 Subject: [PATCH 34/34] also removed debug msg --- src/entity/ai/tasks/TaskAttackPlayer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/entity/ai/tasks/TaskAttackPlayer.php b/src/entity/ai/tasks/TaskAttackPlayer.php index bdea62c8..9253bd57 100644 --- a/src/entity/ai/tasks/TaskAttackPlayer.php +++ b/src/entity/ai/tasks/TaskAttackPlayer.php @@ -15,7 +15,6 @@ class TaskAttackPlayer extends TaskBase public function onStart(EntityAI $ai) { $this->selfCounter = 1; - console("found!"); }