fixed bug with flying detection, added panic task

This commit is contained in:
GameHerobrine 2023-02-18 19:27:47 +03:00
parent 7c47c852e2
commit d7561ed1ba
10 changed files with 70 additions and 17 deletions

View file

@ -32,7 +32,6 @@ class EntityAI
console("[DEBUG] Adding new task...",true, true, 2);
$this->tasks[$task->__toString()] = $task;
}
public function removeTask($name){
if(isset($this->tasks[$name])){
unset($this->tasks[$name]);

View file

@ -12,7 +12,7 @@ class MobController
public function moveNonInstant($x, $y, $z){
if($x == 0 && $y == 0 && $z == 0){
return false; //failed
return false;
}
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
@ -58,7 +58,7 @@ class MobController
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$thetaOffset = $dz < 0 ? 90 : 270;
$calcYaw = ($thetaOffset + $tan);
$this->entity->yaw = $this->entity->headYaw = $calcYaw;
$this->entity->headYaw = $this->entity->yaw = $calcYaw;
}
public function lookOffset($x, $y, $z, $pitch = true){

View file

@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
return !$ai->entity->inPanic && !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)

View file

@ -1,32 +1,38 @@
<?php
class TaskLookAtPlayer extends TaskBase{
const S_LOOK = 0x1;
const S_STOPLOOK = 0x2;
public $target = false;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){
return Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
}
protected function findTarget($e, $r){
$svd = null;
$svdDist = -1;
foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){
foreach($e->level->players as $p){
$p = $p->entity;
if(Utils::distance_noroot($e, $p) > $r*$r){
continue;
}
if($svdDist === -1){
$svdDist = Utils::manh_distance($e, $p);
$svdDist = Utils::distance_noroot($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
if($svd == null){
return null;
}
return $svd;
}

View file

@ -0,0 +1,41 @@
<?php
class TaskPanic extends TaskBase
{
protected $randX = 2, $randZ = 2;
public function onStart(EntityAI $ai)
{
$this->selfCounter = mt_rand(60, 120);
$this->regenerateRandXZ();
}
public function onEnd(EntityAI $ai)
{
$ai->entity->inPanic = false;
$this->reset();
}
public function regenerateRandXZ(){
$this->randX = (mt_rand(0, 1) ? -1 : 1) * 2;
$this->randZ = (mt_rand(0, 1) ? -1 : 1) * 2;
}
public function reset(){
$this->randX = $this->randZ = 2;
}
public function onUpdate(EntityAI $ai)
{
--$this->selfCounter;
if($this->selfCounter % 20 === 0) $this->regenerateRandXZ();
$ai->mobController->moveNonInstant($this->randX, 0, $this->randZ);
}
public function canBeExecuted(EntityAI $ai)
{
return $ai->entity instanceof Animal && $ai->entity->inPanic && $ai->entity->knockbackTime <= 0;
}
}

View file

@ -34,7 +34,7 @@ class TaskRandomWalk extends TaskBase
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt")) {
return false;
}
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
return !$ai->entity->inPanic && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}

View file

@ -29,7 +29,7 @@ class TaskTempt extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
if(!($ai->entity instanceof Breedable)){ //TODO Work with path
if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic){ //TODO Work with path
return false;
}
$target = $this->findTarget($ai->entity, 10);