pathfinder was changed to https://github.com/ppetermann/pathfinder (+small edits to make it work with nc)

This commit is contained in:
GameHerobrine 2022-10-20 15:48:32 +03:00
parent ae8c541452
commit 187704d96e
9 changed files with 130 additions and 44 deletions

View file

@ -62,9 +62,9 @@ class PluginAPI extends stdClass{
public function __destruct(){ public function __destruct(){
foreach($this->plugins as $p){ foreach($this->plugins as $p){
$p[0]->__destruct(); if(method_exists($p[0], "__destruct")){$p[0]->__destruct();};
} }
unset($plugins); unset($this->plugins);
} }
public function getAll(){ public function getAll(){
@ -163,6 +163,7 @@ class PluginAPI extends stdClass{
} }
$phr = "phar://$filePath/"; $phr = "phar://$filePath/";
console("loader:".$pluginInfo["classLoader"]);
include($phr."/src/".$pluginInfo["classLoader"]); include($phr."/src/".$pluginInfo["classLoader"]);
$class = $pluginInfo["CLClass"]; $class = $pluginInfo["CLClass"];
$loader = new $class(); $loader = new $class();

View file

@ -5,13 +5,11 @@ move methods
*/ */
abstract class Creature extends Living{ abstract class Creature extends Living{
const CLASS_TYPE = ENTITY_MOB; const CLASS_TYPE = ENTITY_MOB;
public $moveTime;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic");
//$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type));
$this->width = 1;
$this->moveTime = 0;
} }
public function spawn($player){ public function spawn($player){

View file

@ -6,7 +6,7 @@ class Entity extends Position{
public static $updateOnTick; public static $updateOnTick;
public $isCollidable; public $isCollidable;
public $canBeAttacked; public $canBeAttacked;
public $moveTime;
public $needsUpdate; public $needsUpdate;
/** /**
* @var AxisAlignedBB * @var AxisAlignedBB
@ -52,6 +52,7 @@ class Entity extends Position{
public $width = 1; public $width = 1;
public $height = 1; public $height = 1;
public $random; public $random;
public $radius;
public $inAction = false; public $inAction = false;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->random = new Random(); $this->random = new Random();
@ -91,6 +92,7 @@ class Entity extends Position{
$this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"]:0; $this->pitch = isset($this->data["pitch"]) ? (float) $this->data["pitch"]:0;
$this->position = array("level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch); $this->position = array("level" => $this->level, "x" => &$this->x, "y" => &$this->y, "z" => &$this->z, "yaw" => &$this->yaw, "pitch" => &$this->pitch);
$this->height = 0.98; $this->height = 0.98;
$this->moveTime = 0;
switch($this->class){ switch($this->class){
case ENTITY_PLAYER: case ENTITY_PLAYER:
$this->player = $this->data["player"]; $this->player = $this->data["player"];
@ -131,7 +133,6 @@ class Entity extends Position{
} }
break; break;
} }
$this->radius = &$this->width / 2;
$this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius); $this->boundingBox = new AxisAlignedBB($this->x - $this->radius, $this->y, $this->z - $this->radius, $this->x + $this->radius, $this->y + $this->height, $this->z + $this->radius);
$this->update(); $this->update();
$this->updateLast(); $this->updateLast();
@ -141,10 +142,24 @@ class Entity extends Position{
} }
} }
public function setVelocity($vX, $vY, $vZ){ public function addVelocity($vX, $vY = 0, $vZ = 0){
$this->motionX = $vX; if($vX instanceof Vector3){
$this->motionY = $vY; return $this->addVelocity($vX->x, $vX->y, $vX->z);
$this->motionZ = $vZ; }
$this->speedX += $vX;
$this->speedY += $vY;
$this->speedZ += $vZ;
}
public function isMoving(){
return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
}
public function setVelocity($vX, $vY = 0, $vZ = 0){
if($vX instanceof Vector3){
return $this->setVelocity($vX->x, $vX->y, $vX->z);
}
$this->speedX = $vX;
$this->speedY = $vY;
$this->speedZ = $vZ;
} }
/** /**
@ -204,10 +219,10 @@ class Entity extends Position{
return $this->width; return $this->width;
} }
public function lookOn($target){ public function lookOn(Vector3 $target){
$horizontal = sqrt(pow(($target->entity->x - $this->x), 2) + pow(($target->z - $this->z) , 2)); $horizontal = sqrt(pow(($target->x - $this->x), 2) + pow(($target->z - $this->z) , 2));
$vertical = $target->y - ($this->y + -0.5); /*0.5 = $entity->getEyeHeight()*/ $vertical = $target->y - ($this->y + -0.5); /*0.5 = $entity->getEyeHeight()*/
$pitch = -atan2($vertical, $horizontal) / M_PI * 180; //negative is up, positive is down $pitch = -asin($horizontal) / M_PI * 180; //negative is up, positive is down
$xDist = $target->x - $this->x; $xDist = $target->x - $this->x;
$zDist = $target->z - $this->z; $zDist = $target->z - $this->z;
@ -218,6 +233,7 @@ class Entity extends Position{
} }
$this->yaw = $yaw; $this->yaw = $yaw;
$this->pitch = $pitch; $this->pitch = $pitch;
$this->server->query("UPDATE entities SET pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
} }
public function getDrops(){ public function getDrops(){
@ -443,6 +459,16 @@ class Entity extends Position{
$update = false; $update = false;
if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){ if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){
$drag = 0.2; $drag = 0.2;
if($this->speedX < 0.01 && $this->speedX > -0.01){
$this->speedX = 0;
}
if($this->speedZ < 0.01 && $this->speedZ > -0.01){
$this->speedZ = 0;
}
if($this->speedY < 0.001 && $this->speedY > -0.001){
$this->speedY = 0;
}
if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){ if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){
$blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ)); $blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ));
foreach($blocks as $b){ foreach($blocks as $b){
@ -451,19 +477,17 @@ class Entity extends Position{
$this->speedZ = $b->calculateZOffset($this->boundingBox, $this->speedZ); $this->speedZ = $b->calculateZOffset($this->boundingBox, $this->speedZ);
} }
} }
if($this->speedX != 0){ if($this->speedX != 0){
$this->speedX -= $this->speedX * $drag;
$this->x += $this->speedX; $this->x += $this->speedX;
$this->speedX -= $this->speedX * $drag;
$update = true; $update = true;
} }
if($this->speedZ != 0){ if($this->speedZ != 0){
$this->speedZ -= $this->speedZ * $drag;
$this->z += $this->speedZ; $this->z += $this->speedZ;
$this->speedZ -= $this->speedZ * $drag;
$update = true; $update = true;
} }
if($this->speedY != 0){ if($this->speedY != 0){
$this->speedY -= $this->speedY * $drag;
$ny = $this->y + $this->speedY; $ny = $this->y + $this->speedY;
if($ny <= $this->y){ if($ny <= $this->y){
$x = (int) ($this->x - 0.5); $x = (int) ($this->x - 0.5);
@ -490,14 +514,17 @@ class Entity extends Position{
} }
} }
$this->y = $ny; $this->y = $ny;
$this->speedY -= $this->speedY * $drag;
$update = true; $update = true;
} }
if($this->moveTime > 0){
--$this->moveTime;
}
} }
if($support === false){ if($support === false){
$this->speedY -= $this->class === ENTITY_FALLING ? 0.08 : 0.04; //TODO: replace with $gravity $this->speedY -= $this->class === ENTITY_FALLING ? 0.04 : 0.08; //TODO: replace with $gravity
$update = true; $update = true;
}else{ }else{
//$this->speedX = 0; //$this->speedX = 0;
@ -963,7 +990,7 @@ class Entity extends Position{
$d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01; $d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01;
} }
//attackedAtYaw = (float)((Math.atan2(d1, d) * 180D) / 3.1415927410125732D) - rotationYaw; //attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw;
$this->knockBack($d, $d1); $this->knockBack($d, $d1);
$this->sendMotion(); $this->sendMotion();
@ -1065,6 +1092,7 @@ class Entity extends Position{
public function setSize($w, $h){ public function setSize($w, $h){
$this->width = $w; $this->width = $w;
$this->height = $h; $this->height = $h;
$this->radius = $w / 2;
} }
public function knockBack($d, $d1) public function knockBack($d, $d1)

View file

@ -1,4 +1,5 @@
<?php <?php
abstract class Living extends Entity implements Damageable, Pathfindable{ abstract class Living extends Entity implements Damageable, Pathfindable{
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){

View file

@ -0,0 +1,55 @@
<?php
class PathNavigator
{
public $entity;
public $currentPointIndex;
public function __construct(Creature &$entity){
$this->entity = $entity;
$this->currentPointIndex = 0;
}
public function reset(){
$this->currentPointIndex = 0;
}
public function followPath($path){ //TODO remove local var
if($this->currentPointIndex >= count($path)){
$this->entity->moveTime = mt_rand(100, 200);
try{
return true; //path finished
}finally {
$this->reset();
}
}
$point = $path[$this->currentPointIndex];
$eX = ceil($this->entity->x);
$eY = ceil($this->entity->y);
$eZ = ceil($this->entity->z);
//console($point.":::".$eX.":".$eY.":".$eZ."::::".$this->entity->moveTime);
if($this->entity->moveTime <= 0 && $point->x === $eX && $point->y === $eY && $point->z === $eZ){
++$this->currentPointIndex;
}elseif($this->entity->moveTime <= 0 && !$this->entity->isMoving()){
//console($point.":::".$eX.":".$eY.":".$eZ);
$vX = $point->x - $eX;
$vY = 0;//$point->y - $this->entity->y; //vY is always 0 in current pathfinder\
$vZ = $point->z - $eZ;
//console($vX."::".$vZ);
$this->entity->addVelocity(($vX > 0 ? 0.1 : ($vX < 0 ? -0.1 : 0)), $vY, ($vZ > 0 ? 0.1 : ($vZ < 0 ? -0.1 : 0)));
$this->testLook($point);
//console($this->entity->yaw);
$this->entity->moveTime = 5; //use 20 for debug
}
//$this->entity->moveTime = 5*
return false;
}
public function testLook($pos){
$x = $pos->x - $this->entity->x;
$y = $pos->y - $this->entity->y;
$z = $pos->z - $this->entity->z;
$this->entity->yaw = -atan2($this->entity->speedX, $this->entity->speedZ) * 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.";");
}
}

View file

@ -1,13 +1,14 @@
<?php <?php
class Pig extends Animal{ class Pig extends Animal{
const TYPE = MOB_PIG; const TYPE = MOB_PIG;
public $pathFinder, $pathFound, $path, $server; public $pathFinder, $pathFound, $path, $server, $pathNavigator;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->pathFinder = new AStar($this); //make it before everything else as entity class constructor sends update $this->pathFinder = new AStar($this); //make it before everything else as entity class constructor sends update
$this->pathFound = false; $this->pathFound = false;
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();
$this->pathNavigator = new PathNavigator($this);
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9); $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
$this->setName('Pig'); $this->setName('Pig');
$this->update(); $this->update();
@ -23,17 +24,19 @@ class Pig extends Animal{
} }
public function update(){ public function update(){
if($this->pathFound && $this->path !== false and count($this->path) > 0){ /*if($this->pathFound && $this->path !== false && count($this->path) > 0){
$this->pathFound = !$this->pathNavigator->followPath($this->path);
}else{ }else{
//$start = new MinecraftNode(round($this->x), round($this->y), round($this->z)); $start = new MinecraftNode(ceil($this->x), ceil($this->y), ceil($this->z));
//$stop = $start->add(5,0,5); $stop = $start->add(mt_rand(-10, 10), 0, mt_rand(-10, 10));
//$this->path = $this->pathFinder->findPath($start, $stop); //allows diagonals, path should be ~5 blocks $this->path = $this->pathFinder->findPath($start, $stop); //allows diagonals, path should be ~5 blocks
//var_dump($this->path); //var_dump($this->path);
$this->pathFound = true; if($this->path !== false && count($this->path) > 0){
} $this->pathFound = true;
}else{
$this->moveTime = 30;
}
}*/ //uncomment to enable test moving
parent::update(); parent::update();
} }

View file

@ -62,9 +62,9 @@ class Vector3{
public function subtract($x = 0, $y = 0, $z = 0){ public function subtract($x = 0, $y = 0, $z = 0){
if(($x instanceof Vector3) === true){ if(($x instanceof Vector3) === true){
return $this->add(-$x->x, -$x->y, -$x->z); return $this->subtract($x->x, $x->y, $x->z);
}else{ }else{
return $this->add(-$x, -$y, -$z); return new Vector3($this->x - $x, $this->y - $y, $this->z - $z);
} }
} }

View file

@ -14,19 +14,19 @@ class PharUtils{
$content[$line[0]] = $line[1]; $content[$line[0]] = $line[1];
} }
$pluginData["name"] = $content["name"]; $pluginData["name"] = trim($content["name"]);
$pluginData["description"] = $content["description"]; $pluginData["description"] = trim($content["description"]);
$pluginData["version"] = $content["version"]; $pluginData["version"] = trim($content["version"]);
$pluginData["author"] = $content["author"]; $pluginData["author"] = trim($content["author"]);
$pluginData["mainFile"] = $content["mainFile"]; $pluginData["mainFile"] = trim($content["mainFile"]);
$pluginData["api"] = $content["api"]; $pluginData["api"] = trim($content["api"]);
$pluginData["classLoader"] = $content["classLoader"]; $pluginData["classLoader"] = trim($content["classLoader"]);
$pluginData["CLClass"] = self::getNameSpaceClass($pluginData["classLoader"]); $pluginData["CLClass"] = self::getNameSpaceClass($pluginData["classLoader"]);
return $pluginData; return $pluginData;
} }
public static function getNameSpaceClass($content){ public static function getNameSpaceClass($content){
return substr(str_replace("/", "\\", $content), 0, -4); return trim(substr(str_replace("/", "\\", $content), 0, -4));
} }

View file

@ -44,7 +44,7 @@ class MinecraftNode extends Node
for($z = -1; $z <= 1; $z++){ for($z = -1; $z <= 1; $z++){
$node = new MinecraftNode($x + $this->x, $this->y, $z + $this->z); $node = new MinecraftNode($x + $this->x, $this->y, $z + $this->z);
if(!$node->canMoveHere($pf)){ if(!$node->canMoveHere($pf)){
console("[DEBUG] Obstacle at $node"); //console("[DEBUG] Obstacle at $node");
continue; continue;
} }
$this->nodes[] = $node; $this->nodes[] = $node;