mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 19:49:44 +00:00
ai changes
This commit is contained in:
parent
4835c15747
commit
5a3aeecd93
9 changed files with 196 additions and 98 deletions
46
src/API/PermissionAPI.php
Normal file
46
src/API/PermissionAPI.php
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
class PermissionAPI
|
||||
{
|
||||
/**
|
||||
* @var GroupBase[]
|
||||
*/
|
||||
public $groups = [];
|
||||
/**
|
||||
* @var PocketMinecraftServer
|
||||
*/
|
||||
private $server;
|
||||
public function __construct(){
|
||||
$this->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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
|||
37
src/entity/ai/MobController.php
Normal file
37
src/entity/ai/MobController.php
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
class MobController
|
||||
{
|
||||
/**
|
||||
* @var Entity
|
||||
*/
|
||||
public $entity;
|
||||
|
||||
public function __construct($e){
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,26 +0,0 @@
|
|||
<?php
|
||||
|
||||
class TaskFindPath extends TaskBase
|
||||
{
|
||||
public function shouldBeExecuted(EntityAI $ai)
|
||||
{
|
||||
return !$ai->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?
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue