From 187704d96e9c1fb5632158cad2adce26eb2c8852 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 20 Oct 2022 15:48:32 +0300 Subject: [PATCH] pathfinder was changed to https://github.com/ppetermann/pathfinder (+small edits to make it work with nc) --- src/API/PluginAPI.php | 5 +- src/entity/Creature.php | 4 +- src/entity/Entity.php | 64 ++++++++++++++++++-------- src/entity/Living.php | 1 + src/entity/ai/path/PathNavigator.php | 55 ++++++++++++++++++++++ src/entity/animal/Pig.php | 23 +++++---- src/math/Vector3.php | 4 +- src/phar/PharUtils.php | 16 +++---- src/utils/pathfinder/MinecraftNode.php | 2 +- 9 files changed, 130 insertions(+), 44 deletions(-) create mode 100644 src/entity/ai/path/PathNavigator.php diff --git a/src/API/PluginAPI.php b/src/API/PluginAPI.php index afb5edeb..0dd11884 100644 --- a/src/API/PluginAPI.php +++ b/src/API/PluginAPI.php @@ -62,9 +62,9 @@ class PluginAPI extends stdClass{ public function __destruct(){ foreach($this->plugins as $p){ - $p[0]->__destruct(); + if(method_exists($p[0], "__destruct")){$p[0]->__destruct();}; } - unset($plugins); + unset($this->plugins); } public function getAll(){ @@ -163,6 +163,7 @@ class PluginAPI extends stdClass{ } $phr = "phar://$filePath/"; + console("loader:".$pluginInfo["classLoader"]); include($phr."/src/".$pluginInfo["classLoader"]); $class = $pluginInfo["CLClass"]; $loader = new $class(); diff --git a/src/entity/Creature.php b/src/entity/Creature.php index bcb0be92..6a3a4d3f 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -5,13 +5,11 @@ move methods */ abstract class Creature extends Living{ const CLASS_TYPE = ENTITY_MOB; - public $moveTime; public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic"); //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); - $this->width = 1; - $this->moveTime = 0; + } public function spawn($player){ diff --git a/src/entity/Entity.php b/src/entity/Entity.php index ed99d8b8..246629c3 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -6,7 +6,7 @@ class Entity extends Position{ public static $updateOnTick; public $isCollidable; public $canBeAttacked; - + public $moveTime; public $needsUpdate; /** * @var AxisAlignedBB @@ -52,6 +52,7 @@ class Entity extends Position{ public $width = 1; public $height = 1; public $random; + public $radius; public $inAction = false; function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ $this->random = new Random(); @@ -91,6 +92,7 @@ class Entity extends Position{ $this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"]:0; $this->position = array("level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch); $this->height = 0.98; + $this->moveTime = 0; switch($this->class){ case ENTITY_PLAYER: $this->player = $this->data["player"]; @@ -131,7 +133,6 @@ class Entity extends Position{ } break; } - $this->radius = &$this->width / 2; $this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); $this->update(); $this->updateLast(); @@ -141,10 +142,24 @@ class Entity extends Position{ } } - public function setVelocity($vX, $vY, $vZ){ - $this->motionX = $vX; - $this->motionY = $vY; - $this->motionZ = $vZ; + public function addVelocity($vX, $vY = 0, $vZ = 0){ + if($vX instanceof Vector3){ + return $this->addVelocity($vX->x, $vX->y, $vX->z); + } + $this->speedX += $vX; + $this->speedY += $vY; + $this->speedZ += $vZ; + } + public function isMoving(){ + return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01); + } + public function setVelocity($vX, $vY = 0, $vZ = 0){ + if($vX instanceof Vector3){ + return $this->setVelocity($vX->x, $vX->y, $vX->z); + } + $this->speedX = $vX; + $this->speedY = $vY; + $this->speedZ = $vZ; } /** @@ -204,10 +219,10 @@ class Entity extends Position{ return $this->width; } - public function lookOn($target){ - $horizontal = sqrt(pow(($target->entity->x - $this->x), 2) + pow(($target->z - $this->z) , 2)); + public function lookOn(Vector3 $target){ + $horizontal = sqrt(pow(($target->x - $this->x), 2) + pow(($target->z - $this->z) , 2)); $vertical = $target->y - ($this->y + -0.5); /*0.5 = $entity->getEyeHeight()*/ - $pitch = -atan2($vertical, $horizontal) / M_PI * 180; //negative is up, positive is down + $pitch = -asin($horizontal) / M_PI * 180; //negative is up, positive is down $xDist = $target->x - $this->x; $zDist = $target->z - $this->z; @@ -218,6 +233,7 @@ class Entity extends Position{ } $this->yaw = $yaw; $this->pitch = $pitch; + $this->server->query("UPDATE entities SET pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";"); } public function getDrops(){ @@ -443,6 +459,16 @@ class Entity extends Position{ $update = false; if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){ $drag = 0.2; + if($this->speedX < 0.01 && $this->speedX > -0.01){ + $this->speedX = 0; + } + if($this->speedZ < 0.01 && $this->speedZ > -0.01){ + $this->speedZ = 0; + } + if($this->speedY < 0.001 && $this->speedY > -0.001){ + $this->speedY = 0; + } + if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){ $blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ)); foreach($blocks as $b){ @@ -451,19 +477,17 @@ class Entity extends Position{ $this->speedZ = $b->calculateZOffset($this->boundingBox, $this->speedZ); } } - if($this->speedX != 0){ - $this->speedX -= $this->speedX * $drag; $this->x += $this->speedX; + $this->speedX -= $this->speedX * $drag; $update = true; } if($this->speedZ != 0){ - $this->speedZ -= $this->speedZ * $drag; $this->z += $this->speedZ; + $this->speedZ -= $this->speedZ * $drag; $update = true; } if($this->speedY != 0){ - $this->speedY -= $this->speedY * $drag; $ny = $this->y + $this->speedY; if($ny <= $this->y){ $x = (int) ($this->x - 0.5); @@ -490,14 +514,17 @@ class Entity extends Position{ } } $this->y = $ny; + $this->speedY -= $this->speedY * $drag; $update = true; } - + if($this->moveTime > 0){ + --$this->moveTime; + } } if($support === false){ - $this->speedY -= $this->class === ENTITY_FALLING ? 0.08 : 0.04; //TODO: replace with $gravity + $this->speedY -= $this->class === ENTITY_FALLING ? 0.04 : 0.08; //TODO: replace with $gravity $update = true; }else{ //$this->speedX = 0; @@ -875,9 +902,9 @@ class Entity extends Position{ public function resetSpeed(){ $this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0); } - + public function getSpeed(){ - return $this->speed; + return $this->speed; } public function getSpeedMeasure(){ @@ -963,7 +990,7 @@ class Entity extends Position{ $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; } - //attackedAtYaw = (float)((Math.atan2(d1, d) * 180D) / 3.1415927410125732D) - rotationYaw; + //attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw; $this->knockBack($d, $d1); $this->sendMotion(); @@ -1065,6 +1092,7 @@ class Entity extends Position{ public function setSize($w, $h){ $this->width = $w; $this->height = $h; + $this->radius = $w / 2; } public function knockBack($d, $d1) diff --git a/src/entity/Living.php b/src/entity/Living.php index ee4156f1..82b3c03d 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -1,4 +1,5 @@ entity = $entity; + $this->currentPointIndex = 0; + } + + public function reset(){ + $this->currentPointIndex = 0; + } + + public function followPath($path){ //TODO remove local var + if($this->currentPointIndex >= count($path)){ + $this->entity->moveTime = mt_rand(100, 200); + try{ + return true; //path finished + }finally { + $this->reset(); + } + } + $point = $path[$this->currentPointIndex]; + $eX = ceil($this->entity->x); + $eY = ceil($this->entity->y); + $eZ = ceil($this->entity->z); + //console($point.":::".$eX.":".$eY.":".$eZ."::::".$this->entity->moveTime); + if($this->entity->moveTime <= 0 && $point->x === $eX && $point->y === $eY && $point->z === $eZ){ + ++$this->currentPointIndex; + }elseif($this->entity->moveTime <= 0 && !$this->entity->isMoving()){ + //console($point.":::".$eX.":".$eY.":".$eZ); + $vX = $point->x - $eX; + $vY = 0;//$point->y - $this->entity->y; //vY is always 0 in current pathfinder\ + $vZ = $point->z - $eZ; + //console($vX."::".$vZ); + $this->entity->addVelocity(($vX > 0 ? 0.1 : ($vX < 0 ? -0.1 : 0)), $vY, ($vZ > 0 ? 0.1 : ($vZ < 0 ? -0.1 : 0))); + $this->testLook($point); + //console($this->entity->yaw); + $this->entity->moveTime = 5; //use 20 for debug + } + //$this->entity->moveTime = 5* + return false; + } + + public function testLook($pos){ + $x = $pos->x - $this->entity->x; + $y = $pos->y - $this->entity->y; + $z = $pos->z - $this->entity->z; + $this->entity->yaw = -atan2($this->entity->speedX, $this->entity->speedZ) * 180 / M_PI; + $this->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2)))); + $this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";"); + } +} + diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 4b0ec6c9..ca0a388c 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -1,13 +1,14 @@ pathFinder = new AStar($this); //make it before everything else as entity class constructor sends update $this->pathFound = false; parent::__construct($level, $eid, $class, $type, $data); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); $this->server = ServerAPI::request(); + $this->pathNavigator = new PathNavigator($this); $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9); $this->setName('Pig'); $this->update(); @@ -23,17 +24,19 @@ class Pig extends Animal{ } public function update(){ - if($this->pathFound && $this->path !== false and count($this->path) > 0){ - + /*if($this->pathFound && $this->path !== false && count($this->path) > 0){ + $this->pathFound = !$this->pathNavigator->followPath($this->path); }else{ - //$start = new MinecraftNode(round($this->x), round($this->y), round($this->z)); - //$stop = $start->add(5,0,5); - //$this->path = $this->pathFinder->findPath($start, $stop); //allows diagonals, path should be ~5 blocks + $start = new MinecraftNode(ceil($this->x), ceil($this->y), ceil($this->z)); + $stop = $start->add(mt_rand(-10, 10), 0, mt_rand(-10, 10)); + $this->path = $this->pathFinder->findPath($start, $stop); //allows diagonals, path should be ~5 blocks //var_dump($this->path); - $this->pathFound = true; - } - - + if($this->path !== false && count($this->path) > 0){ + $this->pathFound = true; + }else{ + $this->moveTime = 30; + } + }*/ //uncomment to enable test moving parent::update(); } diff --git a/src/math/Vector3.php b/src/math/Vector3.php index d7511717..387de79a 100644 --- a/src/math/Vector3.php +++ b/src/math/Vector3.php @@ -62,9 +62,9 @@ class Vector3{ public function subtract($x = 0, $y = 0, $z = 0){ if(($x instanceof Vector3) === true){ - return $this->add(-$x->x, -$x->y, -$x->z); + return $this->subtract($x->x, $x->y, $x->z); }else{ - return $this->add(-$x, -$y, -$z); + return new Vector3($this->x - $x, $this->y - $y, $this->z - $z); } } diff --git a/src/phar/PharUtils.php b/src/phar/PharUtils.php index 19c79ac9..674fdb53 100644 --- a/src/phar/PharUtils.php +++ b/src/phar/PharUtils.php @@ -14,19 +14,19 @@ class PharUtils{ $content[$line[0]] = $line[1]; } - $pluginData["name"] = $content["name"]; - $pluginData["description"] = $content["description"]; - $pluginData["version"] = $content["version"]; - $pluginData["author"] = $content["author"]; - $pluginData["mainFile"] = $content["mainFile"]; - $pluginData["api"] = $content["api"]; - $pluginData["classLoader"] = $content["classLoader"]; + $pluginData["name"] = trim($content["name"]); + $pluginData["description"] = trim($content["description"]); + $pluginData["version"] = trim($content["version"]); + $pluginData["author"] = trim($content["author"]); + $pluginData["mainFile"] = trim($content["mainFile"]); + $pluginData["api"] = trim($content["api"]); + $pluginData["classLoader"] = trim($content["classLoader"]); $pluginData["CLClass"] = self::getNameSpaceClass($pluginData["classLoader"]); return $pluginData; } public static function getNameSpaceClass($content){ - return substr(str_replace("/", "\\", $content), 0, -4); + return trim(substr(str_replace("/", "\\", $content), 0, -4)); } diff --git a/src/utils/pathfinder/MinecraftNode.php b/src/utils/pathfinder/MinecraftNode.php index 2af4c2ee..36ade307 100644 --- a/src/utils/pathfinder/MinecraftNode.php +++ b/src/utils/pathfinder/MinecraftNode.php @@ -44,7 +44,7 @@ class MinecraftNode extends Node for($z = -1; $z <= 1; $z++){ $node = new MinecraftNode($x + $this->x, $this->y, $z + $this->z); if(!$node->canMoveHere($pf)){ - console("[DEBUG] Obstacle at $node"); + //console("[DEBUG] Obstacle at $node"); continue; } $this->nodes[] = $node;