mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
hopefully fixed yaw/pitch rotation + new ai task can be randomly executed now
This commit is contained in:
parent
1b85ffd4c6
commit
d9c096357a
7 changed files with 69 additions and 44 deletions
|
|
@ -16,7 +16,7 @@ abstract class Creature extends Living{
|
|||
if(Entity::$updateOnTick){
|
||||
$this->ai->addTask(new TaskLookAround());
|
||||
$this->ai->addTask(new TaskRandomWalk());
|
||||
//$this->ai->addTask(new TaskLookAtPlayer());
|
||||
$this->ai->addTask(new TaskLookAtPlayer());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -839,6 +839,10 @@ class Entity extends Position
|
|||
}
|
||||
}
|
||||
|
||||
public function getEyeHeight(){
|
||||
return $this->isPlayer() ? 1.62 : $this->width;
|
||||
}
|
||||
|
||||
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)){
|
||||
|
|
|
|||
|
|
@ -15,20 +15,49 @@ class MobController
|
|||
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)));
|
||||
$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;
|
||||
}
|
||||
|
||||
public function lookAt($x, $y = 0, $z = 0){
|
||||
if($x instanceof Entity){
|
||||
return $this->lookAt($x->x, $x->y, $x->z);
|
||||
public function faceEntity(Vector3 $v){
|
||||
$d = $v->subtract($this->entity)->normalize();
|
||||
$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.";");
|
||||
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(){
|
||||
unset($this->entity);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,18 +2,16 @@
|
|||
|
||||
class TaskRandomWalk extends TaskBase
|
||||
{
|
||||
private $x, $z, $setupLook = false;
|
||||
private $x, $z;
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$this->x = mt_rand(-7, 7);
|
||||
$this->z = mt_rand(-7, 7);
|
||||
if($this->x = 0 && $this->z = 0){
|
||||
$this->x = mt_rand(-10, 10);
|
||||
$this->z = mt_rand(-10, 10);
|
||||
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)
|
||||
|
|
@ -29,14 +27,6 @@ class TaskRandomWalk extends TaskBase
|
|||
}
|
||||
--$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;
|
||||
$ai->entity->idleTime = 10;
|
||||
$this->setupLook = true;
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function canBeExecuted(EntityAI $ai)
|
||||
|
|
@ -44,7 +34,7 @@ class TaskRandomWalk extends TaskBase
|
|||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
|
||||
return false;
|
||||
}
|
||||
return !$ai->getTask("TaskLookAround")->isStarted;
|
||||
return !@$ai->getTask("TaskLookAround")->isStarted && !@$ai->getTask("TaskLookAtPlayer")->isStarted && mt_rand(0, 120) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,7 +3,13 @@
|
|||
class RotateHeadPacket extends RakNetDataPacket{
|
||||
public $eid;
|
||||
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(){
|
||||
return ProtocolInfo::ROTATE_HEAD_PACKET;
|
||||
}
|
||||
|
|
@ -15,7 +21,7 @@ class RotateHeadPacket extends RakNetDataPacket{
|
|||
public function encode(){
|
||||
$this->reset();
|
||||
$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
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -39,6 +39,12 @@ class Utils{
|
|||
return $id;
|
||||
}
|
||||
|
||||
public static function wrapAngleTo180_float($par0)
|
||||
{
|
||||
$par0 %= 360.0;
|
||||
return $par0 >= 180 ? $par0 - 360 : ($par0 + 360);
|
||||
}
|
||||
|
||||
public static function getSign($v){
|
||||
return $v > 0 ? 1 : ($v < 0 ? -1 : 0);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue