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

@ -16,7 +16,7 @@ abstract class Creature extends Living{
if(Entity::$updateOnTick){ if(Entity::$updateOnTick){
$this->ai->addTask(new TaskLookAround()); $this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskRandomWalk()); $this->ai->addTask(new TaskRandomWalk());
//$this->ai->addTask(new TaskLookAtPlayer()); $this->ai->addTask(new TaskLookAtPlayer());
} }
} }

View file

@ -839,6 +839,10 @@ class Entity extends Position
} }
} }
public function getEyeHeight(){
return $this->isPlayer() ? 1.62 : $this->width;
}
public function interactWith(Entity $e, $action) public function interactWith(Entity $e, $action)
{ {
if($this->class === ENTITY_PLAYER and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($e->player->gamemode & 0x01) === 0x01)){ if($this->class === ENTITY_PLAYER and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($e->player->gamemode & 0x01) === 0x01)){

View file

@ -15,20 +15,49 @@ class MobController
if($x === $this->entity->x && $y === $this->entity->y && $z === $this->entity->z){ if($x === $this->entity->x && $y === $this->entity->y && $z === $this->entity->z){
return false; //failed 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))); $ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
$this->faceEntity($this->entity->add($ox, $oy, $oz));
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $this->entity->eid;
$pk->yaw = $this->entity->yaw;
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
return true; return true;
} }
public function lookAt($x, $y = 0, $z = 0){ public function faceEntity(Vector3 $v){
if($x instanceof Entity){ $d = $v->subtract($this->entity)->normalize();
return $this->lookAt($x->x, $x->y, $x->z); $dx = $d->x;
$dz = $d->z;
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
$thetaOffset = $dz < 0 ? 90 : 270;
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
}
public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $calcYaw;
if($pitch){
$diff = sqrt($x * $x + $z * $z);
$calcPitch = rad2deg(atan($y / $diff));
$this->entity->pitch = $calcPitch;
} }
$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.";"); $this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
return true; return true;
} }
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
if($x instanceof Vector3){
return $this->lookOn($x->x, $x->y + ($x->getEyeHeight() ?: 0), $x->z);
}
return $this->lookOffset($x - $this->entity->x, $this->entity->y + $this->entity->height - $y, $z - $this->entity->z, $pitch);
}
public function __destruct(){ public function __destruct(){
unset($this->entity); unset($this->entity);

View file

@ -4,14 +4,14 @@ class TaskLookAtPlayer extends TaskBase{
const S_LOOK = 0x1; const S_LOOK = 0x1;
const S_STOPLOOK = 0x2; const S_STOPLOOK = 0x2;
public $target = false; public $target = false;
private $state, $yaw, $pitfh; private $nextExecCounter = 0;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){ 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){ 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)]; return count($ents) <= 0 ? false : $ents[array_rand($ents)];
} }
@ -23,36 +23,26 @@ class TaskLookAtPlayer extends TaskBase{
} }
$this->yaw = $ai->entity->yaw; $this->yaw = $ai->entity->yaw;
$this->pitch = $ai->entity->pitch; $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){ public function onUpdate(EntityAI $ai){
if(!($this->target instanceof Entity)){ if(!($this->target instanceof Entity)){
$this->reset(); $this->reset();
return; 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)); $ai->mobController->lookOn($this->target);
$thetaOffset = $dz < 0 ? 45 : 180; $pk = new RotateHeadPacket(); //TODO headYaw auto update
$calcYaw = $tan + $thetaOffset;
$calcPitch = rad2deg(atan($dy / $diff));
$ai->entity->yaw = $calcYaw;
$ai->entity->pitch = $calcPitch;
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid; $pk->eid = $ai->entity->eid;
$pk->yaw = $calcYaw; $pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk); $ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->selfCounter--; $this->selfCounter--;
} }
public function onEnd(EntityAI $ai){ public function onEnd(EntityAI $ai){
unset($this->target); unset($this->target);
$this->nextExecCounter = mt_rand(100, 200);
$ai->entity->pitch = 0;
} }
} }

View file

@ -2,18 +2,16 @@
class TaskRandomWalk extends TaskBase class TaskRandomWalk extends TaskBase
{ {
private $x, $z, $setupLook = false; private $x, $z;
public function onStart(EntityAI $ai) public function onStart(EntityAI $ai)
{ {
$this->x = mt_rand(-7, 7); $this->x = mt_rand(-10, 10);
$this->z = mt_rand(-7, 7); $this->z = mt_rand(-10, 10);
if($this->x = 0 && $this->z = 0){ if($this->x === 0 && $this->z === 0){
$this->reset(); $this->reset();
console("rip task");
return false; return false;
} }
$this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z))); $this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z)));
$this->setupLook = false;
} }
public function onEnd(EntityAI $ai) public function onEnd(EntityAI $ai)
@ -29,14 +27,6 @@ class TaskRandomWalk extends TaskBase
} }
--$this->selfCounter; --$this->selfCounter;
$ai->mobController->moveNonInstant($this->x, 0, $this->z); $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;
$ai->entity->idleTime = 10;
$this->setupLook = true;
return;
}
} }
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)
@ -44,7 +34,7 @@ class TaskRandomWalk extends TaskBase
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) { if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false; return false;
} }
return !$ai->getTask("TaskLookAround")->isStarted; return !@$ai->getTask("TaskLookAround")->isStarted && !@$ai->getTask("TaskLookAtPlayer")->isStarted && mt_rand(0, 120) == 0;
} }

View file

@ -3,7 +3,13 @@
class RotateHeadPacket extends RakNetDataPacket{ class RotateHeadPacket extends RakNetDataPacket{
public $eid; public $eid;
public $yaw; public $yaw;
/**
* Should yaw be modifed<br>
* false = <b>yaw / 360 / 0.0039062</b><br>
* true = <b>yaw</b>
* @var boolean
*/
public $rawYaw = false;
public function pid(){ public function pid(){
return ProtocolInfo::ROTATE_HEAD_PACKET; return ProtocolInfo::ROTATE_HEAD_PACKET;
} }
@ -15,7 +21,7 @@ class RotateHeadPacket extends RakNetDataPacket{
public function encode(){ public function encode(){
$this->reset(); $this->reset();
$this->putInt($this->eid); $this->putInt($this->eid);
$this->putByte($this->yaw); @$this->putByte($this->rawYaw ? $this->yaw : $this->yaw / 360 / 0.0039062); //dont ask me why does chr count float TODO find a better way
} }
} }

View file

@ -39,6 +39,12 @@ class Utils{
return $id; return $id;
} }
public static function wrapAngleTo180_float($par0)
{
$par0 %= 360.0;
return $par0 >= 180 ? $par0 - 360 : ($par0 + 360);
}
public static function getSign($v){ public static function getSign($v){
return $v > 0 ? 1 : ($v < 0 ? -1 : 0); return $v > 0 ? 1 : ($v < 0 ? -1 : 0);
} }