diff --git a/src/API/PermissionAPI.php b/src/API/PermissionAPI.php new file mode 100644 index 00000000..20cfc556 --- /dev/null +++ b/src/API/PermissionAPI.php @@ -0,0 +1,46 @@ +server = ServerAPI::request(); + } + public function init(){ + $this->addGroup(new DefaultGroup()); + $this->addGroup(new OperatorGroup()); + } + + public function getGroupByName($name){ + return $this->groups[$name]; + } + + public function addGroup(GroupBase $group){ + if($this->groupExists($group->name)){ + return false; + } + $this->groups[$group->name] = $group; + return true; + } + + public function groupExists($name){ + return isset($this->groups[$name]); + } + + public function removeGroup(GroupBase $group){ + if(!$this->groupExists($group->name)){ + return false; + } + unset($this->groups[$group->name]); + return true; + } + +} + diff --git a/src/entity/Living.php b/src/entity/Living.php index 89d762a3..c4957579 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -17,7 +17,9 @@ abstract class Living extends Entity implements Damageable{ public function update(){ - $this->ai->updateTasks(); + if($this->idleTime <= 0) { + $this->ai->updateTasks(); + } parent::update(); } diff --git a/src/entity/ai/EntityAI.php b/src/entity/ai/EntityAI.php index 3eff7ea6..8ab2993d 100644 --- a/src/entity/ai/EntityAI.php +++ b/src/entity/ai/EntityAI.php @@ -2,6 +2,10 @@ class EntityAI { + /** + * @var MobController + */ + public $mobController; /** * @var Living */ @@ -17,6 +21,7 @@ class EntityAI public function __construct($entity){ $this->entity = $entity; $this->tasks = []; + $this->mobController = new MobController($entity); } /** @@ -25,22 +30,36 @@ class EntityAI */ public function addTask(TaskBase $task){ console("[DEBUG] Adding new task...",true, true, 2); - $this->tasks[] = $task; + $this->tasks[$task->__toString()] = $task; + } + /** + * + * @param mixed $id classname + * @return TaskBase | false + */ + public function getTask($id){ + return $this->tasks[$id] ?: false; //i never saw this operator before } public function updateTasks(){ foreach($this->tasks as $t){ - if($t->wasExecuted($this)){ - $t->onUpdate($this); - continue; - } - if($t->shouldBeExecuted($this)){ + if(!$t->isStarted && $t->canBeExecuted($this)){ + $t->isStarted = true; $t->onStart($this); + } + if($t->isStarted){ $t->onUpdate($this); - $this->lastTask = $this; + if($t->selfCounter <= 0){ + $t->isStarted = false; + $t->onEnd($this); + } } } } + public function __destruct(){ + unset($this->entity); + } + } diff --git a/src/entity/ai/MobController.php b/src/entity/ai/MobController.php new file mode 100644 index 00000000..63b538f8 --- /dev/null +++ b/src/entity/ai/MobController.php @@ -0,0 +1,37 @@ +entity = $e; + } + + public function moveNonInstant($x, $y, $z){ + if($x === $this->entity->x && $y === $this->entity->y && $z === $this->entity->z){ + return false; //failed + } + $this->entity->moveEntityWithOffset(($x > 0 ? 1 : ($x < 0 ? -1 : 0)), ($y > 0 ? 1 : ($y < 0 ? -1 : 0)), ($z > 0 ? 1 : ($z < 0 ? -1 : 0))); + return true; + } + + public function lookAt($x, $y = 0, $z = 0){ + if($x instanceof Entity){ + return $this->lookAt($x->x, $x->y, $x->z); + } + $this->entity->yaw = -atan2($x, $z) * 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.";"); + return true; + } + + + public function __destruct(){ + unset($this->entity); + } +} + diff --git a/src/entity/ai/tasks/TaskBase.php b/src/entity/ai/tasks/TaskBase.php index 8fd41c65..9e4206f1 100644 --- a/src/entity/ai/tasks/TaskBase.php +++ b/src/entity/ai/tasks/TaskBase.php @@ -2,26 +2,46 @@ abstract class TaskBase { - /** - * @param EntityAI $ai - * @return bool - */ - abstract function shouldBeExecuted(EntityAI $ai); - - /** - * @param EntityAI $ai - * @return bool - */ - abstract function wasExecuted(EntityAI $ai); + public $isStarted = false, $selfCounter = 0; /** + * Executed when entity starts task. It is recommended to set counter here. * @param EntityAI $ai */ abstract function onStart(EntityAI $ai); /** + * Every tick until counter will be not equal to 0. It is recommended to update value of counter here. * @param EntityAI $ai */ abstract function onUpdate(EntityAI $ai); + + /** + * On task end. + * @param EntityAI $ai + */ + abstract function onEnd(EntityAI $ai); + + /** + * Can start the task + * @param EntityAI $ai + */ + abstract function canBeExecuted(EntityAI $ai); + + /** + * @return number ticks + */ + function getIdleTimeAfterEnd(){ + return 0; + } + + function reset(){ + $this->isStarted = false; + $this->selfCounter = 0; + } + + final function __toString(){ + return get_class($this); + } } diff --git a/src/entity/ai/tasks/TaskFindPath.php b/src/entity/ai/tasks/TaskFindPath.php deleted file mode 100644 index 9fd728a3..00000000 --- a/src/entity/ai/tasks/TaskFindPath.php +++ /dev/null @@ -1,26 +0,0 @@ -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 index 9b9ecc3b..395f2ba8 100644 --- a/src/entity/ai/tasks/TaskLookAround.php +++ b/src/entity/ai/tasks/TaskLookAround.php @@ -2,37 +2,35 @@ class TaskLookAround extends TaskBase { - private $rotation = 0; - public function shouldBeExecuted(EntityAI $ai) + private $rotation; + public function onStart(EntityAI $ai) { - return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && Utils::randomFloat() < 0.02 && $ai->entity->idleTime <= 0; - } - - public function wasExecuted(EntityAI $ai){ - - return $ai->entity->lookTime > 0 && $ai->entity->moveTime <= 0; - } - - public function onStart(EntityAI $ai){ - $ai->entity->lookTime = 20 + mt_rand(0, 20); + $this->selfCounter = 1; $this->rotation = mt_rand(-180,180); } - + + public function onEnd(EntityAI $ai) + { + $ai->entity->idleTime = mt_rand(20, 40); + } + + public function canBeExecuted(EntityAI $ai) + { + return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02; + } + 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(10, 40); + if($this->rotation === 0){ + $this->selfCounter = 0; } - $v = Utils::getSign($this->rotation) * 10; + $v = min(Utils::getSign($this->rotation) * 10, $this->rotation); $ai->entity->yaw += $v; - $pk = new RotateHeadPacket(); + $pk = new RotateHeadPacket(); //TODO headYaw auto update $pk->eid = $ai->entity->eid; $pk->yaw = $ai->entity->yaw; $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); - $this->rotation -= $v; + } } diff --git a/src/entity/ai/tasks/TaskRandomWalk.php b/src/entity/ai/tasks/TaskRandomWalk.php index d7c446c6..7fa50ced 100644 --- a/src/entity/ai/tasks/TaskRandomWalk.php +++ b/src/entity/ai/tasks/TaskRandomWalk.php @@ -2,49 +2,50 @@ class TaskRandomWalk extends TaskBase { - private $x, $z, $delay = 10, $newPos, $looking = false; - public function shouldBeExecuted(EntityAI $ai) - { - if($ai->entity instanceof Creeper && $ai->entity->isIgnited()){ - return false; - } - return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && $ai->entity->idleTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1; - } - - public function wasExecuted(EntityAI $ai) - { - return $ai->entity->moveTime > 0 && ($this->x != 0 || $this->z != 0); - } + private $x, $z, $setupLook = false; public function onStart(EntityAI $ai) { $this->x = mt_rand(-7, 7); $this->z = mt_rand(-7, 7); - $this->newPos = $ai->entity->add($this->x, 0, $this->z); - $ai->entity->moveTime = floor(5 * Utils::distance($ai->entity, $this->newPos)); - $this->looking = true; + if($this->x = 0 && $this->z = 0){ + $this->reset(); + console("rip task"); + return false; + } + $this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z))); + $this->setupLook = false; + } + + public function onEnd(EntityAI $ai) + { + $this->x = $this->z = 0; + $ai->entity->idleTime = mt_rand(20, 120); } public function onUpdate(EntityAI $ai) { - if($ai->entity->moveTime <= 1){ - $ai->entity->idleTime = mt_rand(10,40); - $this->x = 0; - $this->z = 0; - $this->looking = false; + if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { + return false; //TODO Better way: block movement } - $ai->entity->moveEntityWithOffset(($this->x > 0 ? 1 : ($this->x < 0 ? -1 : 0)), 0, ($this->z > 0 ? 1 : ($this->z < 0 ? -1 : 0))); - if($this->looking){ - $this->testLook($ai, $ai->entity->speedX, $ai->entity->speedZ); + --$this->selfCounter; + $ai->mobController->moveNonInstant($this->x, 0, $this->z); + if(!$this->setupLook){ + $ai->mobController->lookAt($ai->entity->speedX, 0, $ai->entity->speedZ); $ai->entity->headYaw = $ai->entity->yaw; - $this->looking = false; + $ai->entity->idleTime = 10; + $this->setupLook = true; + return; } + } - public function updateLook($ai){ - $ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";"); - } - public function testLook($ai, $vX, $vZ){ - $ai->entity->yaw = -atan2($vX, $vZ) * 180 / M_PI; - //nopitch $ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2)))); - $ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";"); + + public function canBeExecuted(EntityAI $ai) + { + if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { + return false; + } + return !$ai->getTask("TaskLookAround")->isStarted; } + + } \ No newline at end of file diff --git a/src/world/MobSpawner.php b/src/world/MobSpawner.php index 85194f90..3bfca39c 100644 --- a/src/world/MobSpawner.php +++ b/src/world/MobSpawner.php @@ -20,6 +20,7 @@ class MobSpawner{ } public function handle(){ + return false; if($this->countEntities() > self::MOB_LIMIT && count($this->level->players) <= 0){ return false; //not spawning }