mob fixes related to time and headYaw

This commit is contained in:
GameHerobrine 2023-01-11 11:53:04 +03:00
parent 0a36a67f78
commit 21d0530d39
9 changed files with 35 additions and 27 deletions

View file

@ -134,7 +134,8 @@ class ServerAPI{
if(ADVANCED_CACHE == true){ if(ADVANCED_CACHE == true){
console("[INFO] Advanced cache enabled"); console("[INFO] Advanced cache enabled");
} }
MobSpawner::$spawnAnimals = $this->getProperty("spawn-animals");
MobSpawner::$spawnMobs = $this->getProperty("spawn-mobs");
if($this->getProperty("upnp-forwarding") == true){ if($this->getProperty("upnp-forwarding") == true){
console("[INFO] [UPnP] Trying to port forward..."); console("[INFO] [UPnP] Trying to port forward...");
UPnP_PortForward($this->getProperty("server-port")); UPnP_PortForward($this->getProperty("server-port"));

View file

@ -197,10 +197,13 @@ class Entity extends Position
$this->speedY += $vY; $this->speedY += $vY;
$this->speedZ += $vZ; $this->speedZ += $vZ;
} }
public function isMovingHorizontally()
{
return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01);
}
public function isMoving() public function isMoving()
{ {
return ($this->speedX > 0.01 || $this->speedX < - 0.01) || ($this->speedY > 0.007 || $this->speedY < - 0.007) || ($this->speedZ > 0.01 || $this->speedZ < - 0.01); return $this->isMovingHorizontally() || ($this->speedY > 0.007 || $this->speedY < - 0.007);
} }
public function setVelocity($vX, $vY = 0, $vZ = 0) public function setVelocity($vX, $vY = 0, $vZ = 0)
@ -756,14 +759,6 @@ class Entity extends Position
$this->updateMovement(); $this->updateMovement();
} }
if($this->headYaw != $this->lastHeadYaw) {
$this->lastHeadYaw = $this->headYaw;
$pk = new RotateHeadPacket();
$pk->eid = $this->eid;
$pk->yaw = $this->headYaw;
$this->server->api->player->broadcastPacket($this->level->players, $pk);
}
$this->needsUpdate = $hasUpdate; $this->needsUpdate = $hasUpdate;
$this->lastUpdate = $now; $this->lastUpdate = $now;
} }
@ -774,13 +769,13 @@ class Entity extends Position
return false; return false;
} }
$now = microtime(true); $now = microtime(true);
if($this->isStatic === false and ($this->last[0] != $this->x or $this->last[1] != $this->y or $this->last[2] != $this->z or $this->last[3] != $this->yaw or $this->last[4] != $this->pitch)){ if($this->isStatic === false and ($this->lastX != $this->x or $this->lastY != $this->y or $this->lastZ != $this->z or $this->lastYaw != $this->yaw or $this->lastPitch != $this->pitch or $this->lastHeadYaw != $this->headYaw)){
if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){ if($this->class === ENTITY_PLAYER or ($this->last[5] + 8) < $now){
if($this->server->api->handle("entity.move", $this) === false){ if($this->server->api->handle("entity.move", $this) === false){
if($this->class === ENTITY_PLAYER){ if($this->class === ENTITY_PLAYER){
$this->player->teleport(new Vector3($this->last[0], $this->last[1], $this->last[2]), $this->last[3], $this->last[4]); $this->player->teleport(new Vector3($this->last[0], $this->last[1], $this->last[2]), $this->last[3], $this->last[4]);
} else{ } else{
$this->setPosition($this->last[0], $this->last[1], $this->last[2], $this->last[3], $this->last[4]); //TODO fix $this->setPosition($this->last[0], $this->last[1], $this->last[2], $this->last[3], $this->last[4]);
} }
} else{ } else{
$this->updateLast(); $this->updateLast();
@ -797,6 +792,7 @@ class Entity extends Position
$pk->bodyYaw = $this->yaw; $pk->bodyYaw = $this->yaw;
$this->server->api->player->broadcastPacket($players, $pk); $this->server->api->player->broadcastPacket($players, $pk);
} else{ } else{
$pk = new MoveEntityPacket_PosRot(); $pk = new MoveEntityPacket_PosRot();
$pk->eid = $this->eid; $pk->eid = $this->eid;
$pk->x = $this->x; $pk->x = $this->x;
@ -814,6 +810,8 @@ class Entity extends Position
} }
$this->updateLast(); $this->updateLast();
} }
} }
$this->lastUpdate = $now; $this->lastUpdate = $now;

View file

@ -35,14 +35,17 @@ class MobController
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz))); $tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
$thetaOffset = $dz < 0 ? 90 : 270; $thetaOffset = $dz < 0 ? 90 : 270;
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan); $calcYaw = ($thetaOffset + $tan);
$this->entity->yaw = $this->entity->headYaw = $calcYaw;
} }
public function lookOffset($x, $y, $z, $pitch = true){ public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? ($x < 0 ? 180 : 0) : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/ $tan = $z == 0 ? ($x < 0 ? 180 : 0) : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270; $thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset; $calcYaw = $tan + $thetaOffset;
$this->entity->headYaw = $calcYaw;
$this->entity->yaw = $this->entity->headYaw = $calcYaw;
if($pitch){ if($pitch){
$diff = sqrt($x * $x + $z * $z); $diff = sqrt($x * $x + $z * $z);
$calcPitch = rad2deg(atan($y / $diff)); $calcPitch = rad2deg(atan($y / $diff));

View file

@ -6,7 +6,7 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false; public $target = false;
private $state, $yaw, $pitch; private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){ public function canBeExecuted(EntityAI $ai){
return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); return Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
} }
protected function findTarget($e, $r){ protected function findTarget($e, $r){

View file

@ -11,7 +11,7 @@ class Skeleton extends Monster{
} }
public function updateBurning(){ public function updateBurning(){
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){ if($this->fire > 0 or !$this->level->isDay()){
return false; return false;
} }

View file

@ -11,7 +11,7 @@ class Zombie extends Monster{
} }
public function updateBurning(){ public function updateBurning(){
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){ if($this->fire > 0 or !$this->level->isDay()){
return false; return false;
} }

View file

@ -42,7 +42,7 @@ class Utils{
public static function wrapAngleTo180_float($par0) public static function wrapAngleTo180_float($par0)
{ {
$par0 %= 360.0; $par0 %= 360.0;
return $par0 >= 180 ? $par0 - 360 : ($par0 + 360); return $par0 >= 180 ? $par0 - 360 : (($par0 < -180) ? ($par0 + 360) : $par0);
} }
public static function getSign($v){ public static function getSign($v){

View file

@ -81,6 +81,13 @@ class Level{
unset($this->mobSpawner->level); unset($this->mobSpawner->level);
} }
public function isDay(){
return $this->getTime() % 19200 < TimeAPI::$phases["sunset"];
}
public function isNight(){
$t = $this->getTime() % 19200;
return $t < TimeAPI::$phases["sunrise"] && $t > TimeAPI::$phases["sunset"];
}
public function save($force = false, $extra = true){ public function save($force = false, $extra = true){
if(!isset($this->level)){ if(!isset($this->level)){
return false; return false;

View file

@ -1,6 +1,7 @@
<?php <?php
class MobSpawner{ class MobSpawner{
public static $spawnAnimals = false, $spawnMobs = false;
private $server; private $server;
public $level; public $level;
const MOB_LIMIT = 50; //constant for now const MOB_LIMIT = 50; //constant for now
@ -11,10 +12,8 @@ class MobSpawner{
public function countEntities(){ public function countEntities(){
$ents = 0; $ents = 0;
foreach($this->level->entityList as $e){ //TODO no for loop foreach($this->level->entityList as $e){
if(!$e->isPlayer() && $e->class === ENTITY_MOB){ if(!$e->isPlayer() && $e->class === ENTITY_MOB) ++$ents;
++$ents;
}
} }
return $ents; return $ents;
} }
@ -28,12 +27,10 @@ class MobSpawner{
public function spawnMobs(){ public function spawnMobs(){
$t = $this->level->getTime(); if(self::$spawnAnimals && $this->level->isDay()){ //Animal
if($this->server->api->getProperty("spawn-animals") && $t <= 9500){ //Animal
$type = mt_rand(10, 13); $type = mt_rand(10, 13);
$baby = false; //TODO baby $baby = false; //TODO baby
}else }elseif(self::$spawnMobs && $this->level->isNight()){ //Monster
if($this->server->api->getProperty("spawn-mobs") && $t >= 10000){ //Monster
$type = mt_rand(32, 35); $type = mt_rand(32, 35);
$baby = 2; $baby = 2;
}else{ }else{
@ -47,7 +44,9 @@ class MobSpawner{
} }
$data = $this->genPosData($x, $y + 1, $z); $data = $this->genPosData($x, $y + 1, $z);
if($baby != 2) $data["IsBaby"] = $baby; if($baby != 2) $data["IsBaby"] = $baby;
$e = $this->server->api->entity->add($this->level, 2, $type, $data); $e = $this->server->api->entity->add($this->level, 2, $type, $data);
if($e instanceof Entity){ if($e instanceof Entity){
$this->server->api->entity->spawnToAll($e); $this->server->api->entity->spawnToAll($e);
//console("[DEBUG] $type spawned at $x, $y, $z"); //console("[DEBUG] $type spawned at $x, $y, $z");