This commit is contained in:
GameHerobrine 2022-12-31 21:59:40 +03:00
parent 0f72601891
commit 2573e4f90d
14 changed files with 65 additions and 57 deletions

View file

@ -274,10 +274,17 @@ class ConsoleAPI{
} }
public static function debug($msg){ public static function debug($msg){
console($msg, true, true, 2); console("[DEBUG] ".$msg, true, true, 2);
}
public static function info($msg){
console("[INFO] ".$msg);
}
public static function warn($msg){
console("[WARNING] ".$msg);
}
public static function error($msg){
console("[ERROR] ".$msg);
} }
} }
class ConsoleLoop extends Thread{ class ConsoleLoop extends Thread{

View file

@ -2,6 +2,6 @@
interface ITileNavigator interface ITileNavigator
{ {
public function navigate(PathTile $from, PathTile $to); public function navigate(PathTile $from, PathTile $to, $maxDist);
} }

View file

@ -24,6 +24,10 @@ class PathTileXYZ extends PathTile
return new Vector3($this->x, $this->y, $this->z); return new Vector3($this->x, $this->y, $this->z);
} }
public function asArray(){
return ["x" => $this->x, "y" => $this->y, "z" => $this->z];
}
public function addOffset($offset){ public function addOffset($offset){
$this->x += $offset[0]; $this->x += $offset[0];
$this->y += $offset[1]; $this->y += $offset[1];

View file

@ -23,7 +23,7 @@ class TileNavigator implements ITileNavigator
return array_reverse($totalPath); return array_reverse($totalPath);
} }
public function navigate(PathTile $from, PathTile $to) public function navigate(PathTile $from, PathTile $to, $maxDist) //TODO optimizations, no arraylist
{ {
$closed = new ArrayList(); $closed = new ArrayList();
$open = new ArrayList(); $open = new ArrayList();
@ -54,14 +54,20 @@ class TileNavigator implements ITileNavigator
$closed->add($current); $closed->add($current);
foreach($this->neighborProvider->getNeighbors($current) as $neighbor) foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
{ {
if(in_array($neighbor, $visited)){
continue;
}
$visited[] = $neighbor;
if ($closed->has($neighbor) || $this->blockedProvider->isBlocked($neighbor)) if ($closed->has($neighbor) || $this->blockedProvider->isBlocked($neighbor))
{ {
continue; continue;
} }
if(in_array($neighbor, $visited)){ if(!Utils::in_range(Utils::distance($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){
continue; continue;
} }
$visited[] = $neighbor;
$tentativeG = $gScore[(string) $current] + $this->distanceAlgorithm->calculate($current, $neighbor); $tentativeG = $gScore[(string) $current] + $this->distanceAlgorithm->calculate($current, $neighbor);
if (!$open->has($neighbor)) if (!$open->has($neighbor))

View file

@ -780,6 +780,15 @@ class Entity extends Position
if($this->class !== ENTITY_PLAYER && $update){ if($this->class !== ENTITY_PLAYER && $update){
$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;
} }

View file

@ -31,7 +31,13 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->ai->updateTasks(); $this->ai->updateTasks();
} }
$this->pathFollower->followPath(); if($this->onGround){
/*if($this->path === null && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){
$this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y, $this->z + mt_rand(-10, 10), $this->level), 10);
}*/
$this->pathFollower->followPath();
}
parent::update(); parent::update();
} }

View file

@ -21,10 +21,6 @@ class MobController
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0)); $oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
$this->entity->moveEntityWithOffset($ox, $oy, $oz); $this->entity->moveEntityWithOffset($ox, $oy, $oz);
$this->faceEntity($this->entity->add($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;
} }
@ -46,7 +42,7 @@ class MobController
$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->yaw = $calcYaw; $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

@ -25,11 +25,7 @@ class TaskLookAround extends TaskBase
$this->selfCounter = 0; $this->selfCounter = 0;
} }
$v = min(Utils::getSign($this->rotation) * 10, $this->rotation); $v = min(Utils::getSign($this->rotation) * 10, $this->rotation);
$ai->entity->yaw += $v; $ai->entity->headYaw += $v;
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->rotation -= $v; $this->rotation -= $v;
} }

View file

@ -47,10 +47,6 @@ class TaskLookAtPlayer extends TaskBase{
return; return;
} }
$ai->mobController->lookOn($this->target); $ai->mobController->lookOn($this->target);
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->selfCounter--; $this->selfCounter--;
} }

View file

@ -23,10 +23,6 @@ class TaskTempt extends TaskBase
} }
//TODO following //TODO following
$ai->mobController->lookOn($this->target); $ai->mobController->lookOn($this->target);
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
} }
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)

View file

@ -50,6 +50,6 @@ class UDPSocket{
return false; return false;
} }
return socket_sendto($this->sock, $data, strlen($data), MSG_DONTROUTE, $dest, $port); return @socket_sendto($this->sock, $data, strlen($data), MSG_DONTROUTE, $dest, $port);
} }
} }

View file

@ -87,11 +87,11 @@ class RakNetCodec{
++$pointer; ++$pointer;
if($type === false){ if($type === false){
$payload .= "\x00"; $payload .= "\x00";
$payload .= strrev(Utils::writeTriad($start)); $payload .= Utils::writeLTriad($start);
$payload .= strrev(Utils::writeTriad($end)); $payload .= Utils::writeLTriad($end);
}else{ }else{
$payload .= Utils::writeBool(true); $payload .= Utils::writeBool(true);
$payload .= strrev(Utils::writeTriad($start)); $payload .= Utils::writeLTriad($start);
} }
++$records; ++$records;
} }
@ -126,7 +126,7 @@ class RakNetCodec{
} }
protected function putLTriad($v){ protected function putLTriad($v){
$this->buffer .= strrev(Utils::writeTriad($v)); $this->buffer .= Utils::writeLTriad($v);
} }
private function encodeDataPacket(RakNetDataPacket $pk){ private function encodeDataPacket(RakNetDataPacket $pk){

View file

@ -72,7 +72,7 @@ abstract class RakNetDataPacket extends stdClass{
} }
protected function putLTriad($v){ protected function putLTriad($v){
$this->buffer .= strrev(Utils::writeTriad($v)); $this->buffer .= Utils::writeLTriad($v);
} }
protected function getDataArray($len = 10){ protected function getDataArray($len = 10){

View file

@ -66,7 +66,7 @@ class Utils{
} }
public static function in_range($num, $min, $max){ public static function in_range($num, $min, $max){
return $num >= $min && $num <= $max; return $num >= $min && $num <= $max;
} }
public function xrange($start, $limit, $step = 1) { public function xrange($start, $limit, $step = 1) {
@ -274,9 +274,6 @@ class Utils{
} }
public static function writeLShort($value){ public static function writeLShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("v", $value); return pack("v", $value);
} }
@ -296,9 +293,6 @@ class Utils{
} }
public static function writeShort($value){ public static function writeShort($value){
if($value < 0){
$value += 0x10000;
}
return pack("n", $value); return pack("n", $value);
} }
@ -402,11 +396,11 @@ class Utils{
} }
public static function readLInt($str){ public static function readLInt($str){
list(, $unpacked) = @unpack("V", $str); if(PHP_INT_SIZE === 8){
if($unpacked >= 2147483648){ return @unpack("V", $str)[1] << 32 >> 32;
$unpacked -= 4294967296; }else{
return @unpack("V", $str)[1];
} }
return (int) $unpacked;
} }
public static function readLFloat($str){ public static function readLFloat($str){
@ -427,8 +421,7 @@ class Utils{
} }
public static function readTriad($str){ public static function readTriad($str){
list(, $unpacked) = @unpack("N", "\x00" . $str); return @unpack("N", "\x00" . $str)[1];
return $unpacked;
} }
public static function writeDataArray($data){ public static function writeDataArray($data){
@ -440,6 +433,10 @@ class Utils{
return $raw; return $raw;
} }
public static function writeLTriad($value){
return substr(pack("V", $value), 0, -1);
}
public static function writeTriad($value){ public static function writeTriad($value){
return substr(pack("N", $value), 1); return substr(pack("N", $value), 1);
} }
@ -563,7 +560,7 @@ class Utils{
* Euclidian distance, but without square roots * Euclidian distance, but without square roots
*/ */
public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){ public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){
return pow($pos2->x - $pos1->x, 2) + pow($pos2->z - $pos1->z, 2) + pow($pos2->z - $pos1->z, 2); return ($pos2->x - $pos1->x)*($pos2->x - $pos1->x) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z);
} }
/** /**
@ -579,7 +576,7 @@ class Utils{
if($pos2 instanceof Vector3){ if($pos2 instanceof Vector3){
$pos2 = $pos2->toArray(); $pos2 = $pos2->toArray();
} }
return sqrt(pow($pos1["x"] - $pos2["x"], 2) + pow($pos1["y"] - $pos2["y"], 2) + pow($pos1["z"] - $pos2["z"], 2)); return sqrt(($pos1["x"] - $pos2["x"])*($pos1["x"] - $pos2["x"]) + ($pos1["y"] - $pos2["y"])*($pos1["y"] - $pos2["y"]) + ($pos1["z"] - $pos2["z"])*($pos1["z"] - $pos2["z"]));
} }
public static function angle3D($pos1, $pos2){ public static function angle3D($pos1, $pos2){
@ -637,16 +634,11 @@ class Utils{
} }
public static function readInt($str){ public static function readInt($str){
if(strlen($str) === 0){ if(PHP_INT_SIZE === 8){
return; return @unpack("N", $str)[1] << 32 >> 32;
}else{
return @unpack("N", $str)[1];
} }
list(, $unpacked) = unpack("N", $str);
if($unpacked >= 2147483648){
$unpacked -= 4294967296;
}
return (int) $unpacked;
} }
public static function writeInt($value){ public static function writeInt($value){