mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 20:19:44 +00:00
ai changes
This commit is contained in:
parent
4835c15747
commit
5a3aeecd93
9 changed files with 196 additions and 98 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue