From 8d454e5ec9bb79e1fab4af21a9fb61363404bd01 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Thu, 20 Oct 2022 19:30:04 +0300 Subject: [PATCH] entity ai classes, pigs should look around --- src/dependencies.php | 1 - src/entity/Animal.php | 2 ++ src/entity/Entity.php | 14 ++++++-- src/entity/Living.php | 19 ++++++++--- src/entity/Pathfindable.php | 7 ---- src/entity/ai/EntityAI.php | 46 +++++++++++++++++++++++++ src/entity/ai/tasks/TaskBase.php | 27 +++++++++++++++ src/entity/ai/tasks/TaskFindPath.php | 26 ++++++++++++++ src/entity/ai/tasks/TaskLookAround.php | 47 ++++++++++++++++++++++++++ src/entity/ai/tasks/TaskRandomWalk.php | 26 ++++++++++++++ src/entity/animal/Pig.php | 24 ++----------- src/utils/Utils.php | 5 +++ 12 files changed, 206 insertions(+), 38 deletions(-) delete mode 100644 src/entity/Pathfindable.php create mode 100644 src/entity/ai/EntityAI.php create mode 100644 src/entity/ai/tasks/TaskBase.php create mode 100644 src/entity/ai/tasks/TaskFindPath.php create mode 100644 src/entity/ai/tasks/TaskLookAround.php create mode 100644 src/entity/ai/tasks/TaskRandomWalk.php diff --git a/src/dependencies.php b/src/dependencies.php index dd0aa8d2..852573eb 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -85,7 +85,6 @@ 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"); require_once(FILE_PATH . "/src/entity/Creature.php"); diff --git a/src/entity/Animal.php b/src/entity/Animal.php index d1b55561..6a1f17b1 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -5,6 +5,8 @@ move methods */ abstract class Animal extends Creature implements Ageable, Breedable{ + public $parent; + public function isBaby(){ if(!isset($this->data["IsBaby"])){ $this->data["IsBaby"] = false; diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 246629c3..9619ef8c 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 $moveTime, $lookTime, $idleTime; public $needsUpdate; /** * @var AxisAlignedBB @@ -93,6 +93,7 @@ class Entity extends Position{ $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; + $this->lookTime = 0; switch($this->class){ case ENTITY_PLAYER: $this->player = $this->data["player"]; @@ -150,8 +151,9 @@ class Entity extends Position{ $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); + return $this->moveTime > 0 || ($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){ @@ -520,6 +522,13 @@ class Entity extends Position{ if($this->moveTime > 0){ --$this->moveTime; } + if($this->lookTime > 0){ + --$this->lookTime; + } + if($this->idleTime > 0){ + --$this->idleTime; + } + } @@ -627,7 +636,6 @@ class Entity extends Position{ } } }else{ - $this->updatePosition($this->x, $this->y, $this->z, $this->yaw, $this->pitch); } } diff --git a/src/entity/Living.php b/src/entity/Living.php index 82b3c03d..68d09ce3 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -1,13 +1,22 @@ pathFinder = new AStar($this); + $this->pathFound = false; + $this->target = false; + $this->path = null; + $this->ai = new EntityAI($this); + $this->pathNavigator = new PathNavigator($this); parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; } - public function getPathfinder(){ //Placeholder - return $this->pathFinder; - } + public function update(){ + + $this->ai->updateTasks(); + parent::update(); + } + } \ No newline at end of file diff --git a/src/entity/Pathfindable.php b/src/entity/Pathfindable.php deleted file mode 100644 index 46b35445..00000000 --- a/src/entity/Pathfindable.php +++ /dev/null @@ -1,7 +0,0 @@ -entity = $entity; + $this->tasks = []; + } + + /** + * Add a task for entity + * @param TaskBase $task + */ + public function addTask(TaskBase $task){ + console("[DEBUG] Adding new task...",true, true, 2); + $this->tasks[] = $task; + } + + public function updateTasks(){ + foreach($this->tasks as $t){ + if($t->wasExecuted($this)){ + $t->onUpdate($this); + continue; + } + if($t->shouldBeExecuted($this)){ + $t->onStart($this); + $t->onUpdate($this); + $this->lastTask = $this; + } + } + } + +} + diff --git a/src/entity/ai/tasks/TaskBase.php b/src/entity/ai/tasks/TaskBase.php new file mode 100644 index 00000000..8fd41c65 --- /dev/null +++ b/src/entity/ai/tasks/TaskBase.php @@ -0,0 +1,27 @@ +entity->pathFound && $ai->entity->target instanceof Entity && $ai->entity->moveTime <= 0; + } + + public function onUpdate(EntityAI $ai) + { + console("find path!"); + } + public function wasExecuted(EntityAI $ai) + { + return $ai->entity->pathFound; + } + public function onStart(EntityAI $ai) + { + //todo? + } + + + +} + diff --git a/src/entity/ai/tasks/TaskLookAround.php b/src/entity/ai/tasks/TaskLookAround.php new file mode 100644 index 00000000..cafe726c --- /dev/null +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -0,0 +1,47 @@ +entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0; + } + + public function wasExecuted(EntityAI $ai){ + + return $ai->entity->lookTime > 0; + } + + public function onStart(EntityAI $ai){ + $ai->entity->lookTime = 120; + $this->finYaw = $ai->entity->yaw; + } + + public function onUpdate(EntityAI $ai) + { + //90 - 75 = -45deg rot + //60 - 30 = 90deg rot + //30 - 15 = -45deg rot + if($ai->entity->lookTime === 1){ + $ai->entity->idleTime = mt_rand(40, 200); + } + if(Utils::in_range($ai->entity->lookTime, 105, 120) || Utils::in_range($ai->entity->lookTime, 15, 30)){ + $ai->entity->yaw -= 3; + } + if(Utils::in_range($ai->entity->lookTime, 50, 80)){ + $ai->entity->yaw += 3; + } + + $pk = new RotateHeadPacket(); + $pk->eid = $ai->entity->eid; + $pk->yaw = $ai->entity->yaw; + $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); + } + public function testLook($pos){ + + + + } +} + diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php new file mode 100644 index 00000000..11e85cf8 --- /dev/null +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -0,0 +1,26 @@ +entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1; + } + + public function onUpdate(EntityAI $ai) + { + console("random walk"); + } + public function wasExecuted(EntityAI $ai) + { + return $ai->entity->moveTime > 0; + } + public function onStart(EntityAI $ai) + { + $ai->entity->moveTime = 100; + } + + + +} + diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index ca0a388c..d3902479 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -1,16 +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->ai->addTask(new TaskLookAround()); $this->update(); } public function isFood($id){ @@ -22,22 +20,4 @@ class Pig extends Animal{ array(($this->fire > 0 ? COOKED_PORKCHOP:RAW_PORKCHOP), 0, mt_rand(0,2)), ); } - - public function update(){ - /*if($this->pathFound && $this->path !== false && count($this->path) > 0){ - $this->pathFound = !$this->pathNavigator->followPath($this->path); - }else{ - $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); - 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/utils/Utils.php b/src/utils/Utils.php index 04e6850b..d99c9e0a 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -48,6 +48,11 @@ class Utils{ return sha1(strtolower($variable)); } } + + public static function in_range($num, $min, $max){ + return $num >= $min && $num <= $max; + } + public function xrange($start, $limit, $step = 1) { if ($start <= $limit) { if ($step <= 0) {