hopefully fixed yaw/pitch rotation + new ai task can be randomly executed now

This commit is contained in:
GameHerobrine 2022-12-20 19:23:21 +03:00
parent 1b85ffd4c6
commit d9c096357a
7 changed files with 69 additions and 44 deletions

View file

@ -4,14 +4,14 @@ class TaskLookAtPlayer extends TaskBase{
const S_LOOK = 0x1;
const S_STOPLOOK = 0x2;
public $target = false;
private $state, $yaw, $pitfh;
private $nextExecCounter = 0;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){
return !@$ai->getTask("TaskLookAround")->wasExecuted && mt_rand(0,5) === 0;
return !$ai->entity->isMoving() && --$this->nextExecCounter <= 0 && !@$ai->getTask("TaskLookAround")->wasExecuted && mt_rand(0,5) === 0 && Utils::randomFloat() >= 0.02;
}
protected function findTarget($e, $r){
$ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER);
$ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER); //TODO sort by nearest and select nearest
return count($ents) <= 0 ? false : $ents[array_rand($ents)];
}
@ -23,36 +23,26 @@ class TaskLookAtPlayer extends TaskBase{
}
$this->yaw = $ai->entity->yaw;
$this->pitch = $ai->entity->pitch;
$this->selfCounter = mt_rand(40, 120) + 40; /*last 40 = return back*/
$this->selfCounter = mt_rand(20, 40);
}
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();
$ai->mobController->lookOn($this->target);
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $calcYaw;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->selfCounter--;
}
public function onEnd(EntityAI $ai){
unset($this->target);
$this->nextExecCounter = mt_rand(100, 200);
$ai->entity->pitch = 0;
}
}