tweaks to pathfinder and pathfollower(basically nothing)

This commit is contained in:
GameHerobrine 2024-04-14 03:41:24 +03:00
parent da28b3b0ff
commit cb0fa06e12
5 changed files with 94 additions and 25 deletions

View file

@ -2174,6 +2174,11 @@ class Player{
$this->sendChat("Your message contains illegal characters");
break;
}
//if($message == "pf"){
// Living::$pathfind = !Living::$pathfind;
//}
if($this->server->api->handle("player.chat", $data) !== false){
$this->server->send2Discord("<" . $this->username . "> " . $message);
if(isset($data["message"])){

View file

@ -26,8 +26,9 @@ class TileNavigator implements ITileNavigator
public function navigate(PathTile $from, PathTile $to, $maxDist)
{
$open = [0 => $from];
$open = new SplPriorityQueue();
//$open->insert(, 0);
$open->insert($from, 0);
$path = [];
$gScore = [];
$gScore[(string) $from] = 0;
@ -37,9 +38,10 @@ class TileNavigator implements ITileNavigator
}
$visited = [];
$maxDist*=$maxDist; //no square root
while(count($open) > 0)
while(!$open->isEmpty())
{
$current = array_pop($open);
$current = $open->top();
$open->next();
if ($current == $to){
return $this->reconstructPath($path, $current);
}
@ -58,7 +60,9 @@ class TileNavigator implements ITileNavigator
$tentativeG = $gScore[(string) $current] + $distbetweenCost;
if (!isset($has[(string)$neighbor]))
{
$open[-$tentativeG] = $neighbor;
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
//$open[-$tentativeG] = $neighbor;
$open->insert($neighbor, -$tentativeG);
$has[(string)$neighbor] = true;
}
elseif ($tentativeG >= $gScore[(string) $neighbor])

View file

@ -3,9 +3,10 @@
class MCDiagonalProvider implements INeighborProvider
{
private static $neighbors = array(
[ -1, 0, -1 ], [-1, 0, 0], [-1, 0, 1],
[0, 0, -1], [0, 0, 1],
[1, 0, -1], [1, 0, 0], [1, 0, -1]
[-1, 0, 0],
[0, 0, -1],
[1, 0, 0],
[0, 0, 1],
);
private static $jumpOffset = [0, 1, 0];
private static $moveDownOffset = [0, -1, 0];
@ -15,17 +16,26 @@ class MCDiagonalProvider implements INeighborProvider
$pnts = [];
foreach(MCDiagonalProvider::$neighbors as $offset){
$pnt = $tile->addOffset($offset);
if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){
if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y - 1, $pnt->z))){
--$pnt->y;
}
$pnts[] = $pnt;
}else{
if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y + 1, $pnt->z))){
++$pnt->y;
$pnts[] = $pnt;
}
}
//if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){
//
//}
//if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){
// if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y - 1, $pnt->z))){
// --$pnt->y;
// }
// $pnts[] = $pnt;
//}else{
// if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y + 1, $pnt->z))){
// ++$pnt->y;
// $pnts[] = $pnt;
// }
//}
}
return $pnts;
}

View file

@ -21,7 +21,7 @@ abstract class Living extends Entity implements Pathfindable{
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false;
$this->ai = new EntityAI($this);
$this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new ManhattanHeuristic3D());
$this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new Pythagoras3D());
$this->pathFollower = new PathFollower($this);
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
@ -113,16 +113,50 @@ abstract class Living extends Entity implements Pathfindable{
parent::update($now);
}
public $pathEIDS = [];
private static $lastPathEID = 10000000;
public static $pathfind = true;
public function updateEntityMovement(){
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks();
}
//if($this->onGround){
// if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
// $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 + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10);
// }
// $this->pathFollower->followPath();
//}
/*if($this->onGround){
if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
$me = new PathTileXYZ($this->x, $this->y, $this->z, $this->level);
//$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level);
if(count($this->level->players) > 0){
$pl = array_values($this->level->players)[0];
$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level);
}
if(self::$pathfind){
$this->path = $this->pathFinder->navigate($me, $target, 10);
}
if($this->path){
console("Found path to $target from $me of length ".count($this->path));
foreach($this->path as $node){
$eid = self::$lastPathEID++;
$this->pathEIDS[] = $eid;
$pk = new AddItemEntityPacket();
$pk->eid = $eid;
$pk->item = BlockAPI::getItem(GOLD_BLOCK, 0, 1);
$pk->x = $node->x + 0.5;
$pk->y = $node->y;
$pk->z = $node->z + 0.5;
$pk->yaw = $pk->pitch = 0;
$pk->roll = 0;
foreach($this->level->players as $player){
$player->dataPacket($pk);
}
}
}
}
$this->pathFollower->followPath();
}*/
//not exactly here

View file

@ -19,13 +19,29 @@ class PathFollower{
$this->entity->currentNode = $this->entity->path[$this->entity->currentIndex];
}
$this->entity->ai->mobController->setMovingTarget($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z, 0.25);
console($this->entity->currentNode.":".$this->entity->boundingBox);
if($this->entity->boundingBox->isXYZInsideNS($this->entity->currentNode->x, $this->entity->currentNode->y, $this->entity->currentNode->z)){
$this->entity->ai->mobController->setMovingTarget($this->entity->currentNode->x + 0.5, $this->entity->currentNode->y, $this->entity->currentNode->z + 0.5, 1.0);
if($this->entity->boundingBox->isXYZInsideNS($this->entity->currentNode->x + 0.5, $this->entity->currentNode->y, $this->entity->currentNode->z + 0.5)){
++$this->entity->currentIndex;
console("next");
$this->entity->currentNode = null;
}
if($this->entity->currentIndex >= count($this->entity->path)){
console("path finished.");
$this->entity->currentNode = null;
$this->entity->path = null;
$this->entity->currentIndex = 0;
foreach($this->entity->pathEIDS as $eid){
$pk = new RemoveEntityPacket();
$pk->eid = $eid;
foreach($this->entity->level->players as $player){
$player->dataPacket($pk);
}
}
}
}
}