From 70a36f8e6b250bc9558bbf03c2fbb75f97bd6352 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 28 Aug 2022 18:42:52 +0300 Subject: [PATCH] Test Pathfinder --- src/PocketMinecraftServer.php | 1 + src/dependencies.php | 9 ++++++- src/entity/Living.php | 5 +++- src/entity/animal/Pig.php | 46 +++++++++++++++++++++++++++++++++-- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 57da7e2e..1bc7f03c 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -67,6 +67,7 @@ class PocketMinecraftServer{ if(!defined("NO_THREADS")){ $this->asyncThread = new AsyncMultipleQueue(); } + console("[INFO] Loading extra.properties..."); $this->extraprops = new Config(DATA_PATH . "extra.properties", CONFIG_PROPERTIES, [ "version" => "5", diff --git a/src/dependencies.php b/src/dependencies.php index de8077fb..1265f8f0 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -76,9 +76,16 @@ $sha1sum = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"; require_once(FILE_PATH . "/src/math/Vector3.php"); require_once(FILE_PATH . "/src/world/Position.php"); require_once(FILE_PATH . "/src/pmf/PMF.php"); -require_once(FILE_PATH . "/src/entity/Entity.php"); + +require_once(FILE_PATH . "/src/utils/pathfinder/PathNode.php"); +require_once(FILE_PATH . "/src/utils/pathfinder/Path.php"); +require_once(FILE_PATH . "/src/utils/pathfinder/PathFinder.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"); require_once(FILE_PATH . "/src/entity/Creature.php"); require_once(FILE_PATH . "/src/entity/Ageable.php"); diff --git a/src/entity/Living.php b/src/entity/Living.php index a177696d..d95ff4b7 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -1,3 +1,6 @@ pathFinder; + } } \ No newline at end of file diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index b90422a8..10778b05 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -1,10 +1,14 @@ setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); + $this->pathFinder = new Pathfinder($this); $this->update(); + $this->server = ServerAPI::request(); + //$this->server->schedule(60, array($this, "testMovement")); //$this->setName('Pig'); $this->size = $this->isBaby() ? 0.60 : 1.1875; } @@ -17,5 +21,43 @@ class Pig extends Animal{ array(($this->fire > 0 ? COOKED_PORKCHOP:RAW_PORKCHOP), 0, mt_rand(0,2)), ); } - + public function testMovement(){ //test movement, move 1 block each 10 seconds, looks terrible ingame + $this->pathFinder->updateEntityPos(ceil($this->x), ceil($this->y), ceil($this->z)); + $this->path = $this->pathFinder->buildTo(new Vector3(ceil($this->x) + mt_rand(0,4), ceil($this->y), ceil($this->z) + mt_rand(0,4))); + $this->followPath(); + } + + public function followPath(){ + $point = null; + if($this->path instanceof Path){ + $point = $this->path->increaseIndexAndGetPoint(); + }else{ + $this->server->schedule(40, array($this, "testMovement"), false); + return; + } + if($point != null){ + console($point); + $xDiff = $point->x - $this->x; + $zDiff = $point->z - $this->z; + console($this); + $this->move(new Vector3($xDiff, 0, $zDiff), $this->yaw, $this->pitch); + $this->updateMovement(); + $this->handleUpdate($this); + $this->server->schedule(10, array($this, "followPath"), false); + }else{ + $this->server->schedule(40, array($this, "testMovement"), false); + } + + } + public function handleUpdate($entity){ + $players = $this->server->api->player->getAll($entity->level); + $pk = new MoveEntityPacket_PosRot; + $pk->eid = $entity->eid; + $pk->x = $entity->x; + $pk->y = $entity->y; + $pk->z = $entity->z; + $pk->yaw = $entity->yaw; + $pk->pitch = $entity->pitch; + $this->server->api->player->broadcastPacket($players, $pk); + } }