diff --git a/README.md b/README.md index 642c7c65..fe9f76bf 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ https://discord.gg/uSHEWe6NC3 * __[PHP Sockets](http://php.net/manual/en/book.sockets.php)__ * __[PHP SQLite3](http://php.net/manual/en/book.sqlite3.php)__ * __[PHP BCMath](http://php.net/manual/en/book.bc.php)__ -* __[PHP pthreads](http://pthreads.org/)__ by _[krakjoe](https://github.com/krakjoe)_: Threading for PHP - Share Nothing, Do Everything. +* __[PHP pthreads](https://github.com/krakjoe/pthreads)__ by _[krakjoe](https://github.com/krakjoe)_: Threading for PHP - Share Nothing, Do Everything. * __[PHP YAML](https://code.google.com/p/php-yaml/)__ by _Bryan Davis_: The Yaml PHP Extension provides a wrapper to the LibYAML library. * __[LibYAML](http://pyyaml.org/wiki/LibYAML)__ by _Kirill Simonov_: A YAML 1.1 parser and emitter written in C. * __[mintty](https://code.google.com/p/mintty/)__ : xterm Terminal Emulator diff --git a/src/API/ConsoleAPI.php b/src/API/ConsoleAPI.php index 568a1f5a..510a207f 100644 --- a/src/API/ConsoleAPI.php +++ b/src/API/ConsoleAPI.php @@ -273,6 +273,11 @@ class ConsoleAPI{ return $output; } + public static function debug($msg){ + console($msg, true, true, 2); + } + + } class ConsoleLoop extends Thread{ diff --git a/src/entity/Creature.php b/src/entity/Creature.php index 93a7dd8b..7b8f402b 100644 --- a/src/entity/Creature.php +++ b/src/entity/Creature.php @@ -15,7 +15,8 @@ abstract class Creature extends Living{ //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); if(Entity::$updateOnTick){ $this->ai->addTask(new TaskLookAround()); - $this->ai->addTask(new TaskRandomWalk()); + $this->ai->addTask(new TaskRandomWalk()); + //$this->ai->addTask(new TaskLookAtPlayer()); } } @@ -66,4 +67,4 @@ abstract class Creature extends Living{ $player->dataPacket($pk); } -} \ No newline at end of file +} diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index 8ab2993d..4a1fb922 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -41,6 +41,7 @@ class EntityAI return $this->tasks[$id] ?: false; //i never saw this operator before } + public function updateTasks(){ foreach($this->tasks as $t){ if(!$t->isStarted && $t->canBeExecuted($this)){ diff --git a/src/entity/ai/tasks/TaskLookAtPlayer.php b/src/entity/ai/tasks/TaskLookAtPlayer.php new file mode 100644 index 00000000..337ba3d0 --- /dev/null +++ b/src/entity/ai/tasks/TaskLookAtPlayer.php @@ -0,0 +1,58 @@ +getTask("TaskLookAround")->wasExecuted && mt_rand(0,5) === 0; + } + + protected function findTarget($e, $r){ + $ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER); + + return count($ents) <= 0 ? false : $ents[array_rand($ents)]; + } + + public function onStart(EntityAI $ai){ + $this->target = $this->findTarget($ai->entity, 5); + if(!($this->target instanceof Entity) || !$this->target->isPlayer()){ + $this->reset(); + return; + } + $this->yaw = $ai->entity->yaw; + $this->pitch = $ai->entity->pitch; + $this->selfCounter = mt_rand(40, 120) + 40; /*last 40 = return back*/ + } + + public function onUpdate(EntityAI $ai){ + if(!($this->target instanceof Entity)){ + $this->reset(); + return; + } //TODO MobController lookAt method + $dx = $this->target->x - $ai->entity->x; + $dy = ($ai->entity->y + $ai->entity->height) - ($this->target->y + 1.62); + $dz = $this->target->z - $ai ->entity->z; + $diff = sqrt($dx * $dx + $dz * $dz); + if($diff === 0){ + return; + } + $tan = 90 - rad2deg(atan($dx / $dz)); + $thetaOffset = $dz < 0 ? 45 : 180; + $calcYaw = $tan + $thetaOffset; + $calcPitch = rad2deg(atan($dy / $diff)); + $ai->entity->yaw = $calcYaw; + $ai->entity->pitch = $calcPitch; + $pk = new RotateHeadPacket(); + $pk->eid = $ai->entity->eid; + $pk->yaw = $calcYaw; + $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); + $this->selfCounter--; + } + + public function onEnd(EntityAI $ai){ + unset($this->target); + } +} +