mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 19:49:44 +00:00
pathfinder rewrite
This commit is contained in:
parent
cb0fa06e12
commit
22f3c3f640
16 changed files with 89 additions and 263 deletions
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
|
||||
interface IElement
|
||||
{
|
||||
public function __toString();
|
||||
}
|
||||
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
|
||||
interface ITileNavigator
|
||||
{
|
||||
public function navigate(PathTile $from, PathTile $to, $maxDist);
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
class PHPUtils
|
||||
{
|
||||
public static function removeElementFromArray(&$arr, $element, $reindex = true){
|
||||
if (($key = array_search($element, $arr)) !== false) {
|
||||
unset($arr[$key]);
|
||||
if($reindex) $arr = array_values($arr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,27 +0,0 @@
|
|||
<?php
|
||||
|
||||
class PathTile implements IElement
|
||||
{
|
||||
public $x, $y;
|
||||
|
||||
public function __construct($x, $y){
|
||||
$this->x = $x;
|
||||
$this->y = $y;
|
||||
}
|
||||
|
||||
public function equals(PathTile $t){
|
||||
return $t->x === $this->x && $t->y === $this->y;
|
||||
}
|
||||
|
||||
public function addOffset($offset){
|
||||
$this->x += $offset[0];
|
||||
$this->y += $offset[1];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return $this->x.":".$this->y;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
<?php
|
||||
|
||||
class PathTileXYZ extends PathTile
|
||||
{
|
||||
public $x, $y, $z;
|
||||
|
||||
/**
|
||||
* @var Level
|
||||
*/
|
||||
public $level;
|
||||
|
||||
public function __construct($x, $y, $z, $level){
|
||||
$this->x = floor($x);
|
||||
$this->y = floor($y);
|
||||
$this->z = floor($z);
|
||||
$this->level = $level;
|
||||
}
|
||||
|
||||
public function equals(PathTile $t){
|
||||
return $t->x === $this->x && $t->y === $this->y && $t->z === $this->z;
|
||||
}
|
||||
|
||||
public function asVector(){
|
||||
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){
|
||||
return new PathTileXYZ($this->x + $offset[0], $this->y + $offset[1], $this->z + $offset[2], $this->level);
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return $this->x.":".$this->y.":".$this->z;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -1,14 +1,17 @@
|
|||
<?php
|
||||
class TileNavigator implements ITileNavigator
|
||||
class TileNavigator
|
||||
{
|
||||
private $blockedProvider, $neighborProvider, $distanceAlgorithm;
|
||||
|
||||
|
||||
const OFFSETS = [
|
||||
[0, 0, 1],
|
||||
[0, 0, -1],
|
||||
[1, 0, 0],
|
||||
[-1, 0, 0]
|
||||
];
|
||||
|
||||
public static $pathfinderAccessed = 0;
|
||||
|
||||
public function __construct(IBlockedProvider $blockedProvider, INeighborProvider $neighborProvider, IDistanceAlgorithm $distanceAlgorithm){
|
||||
$this->blockedProvider = $blockedProvider;
|
||||
$this->neighborProvider = $neighborProvider;
|
||||
$this->distanceAlgorithm = $distanceAlgorithm;
|
||||
public function __construct(){
|
||||
}
|
||||
|
||||
public function reconstructPath($path, $current){
|
||||
|
|
@ -23,9 +26,31 @@ class TileNavigator implements ITileNavigator
|
|||
array_pop($totalPath);
|
||||
return array_reverse($totalPath);
|
||||
}
|
||||
|
||||
public function isLocBlocked(PMFLevel $level, $loc){
|
||||
|
||||
$chunkX = $loc >> 20 & 0xf;
|
||||
$chunkZ = $loc >> 12 & 0xf;
|
||||
$chunkY = $loc >> 4 & 0xf;
|
||||
|
||||
$index = $chunkZ << 4 | $chunkX;
|
||||
|
||||
$blockX = $loc >> 16 & 0xf;
|
||||
$blockZ = $loc >> 8 & 0xf;
|
||||
$blockY = $loc & 0xf;
|
||||
|
||||
$id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index);
|
||||
return StaticBlock::getIsSolid($id);
|
||||
}
|
||||
|
||||
public function navigate(PathTile $from, PathTile $to, $maxDist)
|
||||
|
||||
public function navigate(Level $level, $fromX, $fromY, $fromZ, $toX, $toY, $toZ, $maxDist)
|
||||
{
|
||||
$from = $fromX << 16 | $fromZ << 8 | $fromY;
|
||||
$to = $toX << 16 | $toZ << 8 | $toY;
|
||||
|
||||
$pmfLevel = $level->level;
|
||||
|
||||
$open = new SplPriorityQueue();
|
||||
//$open->insert(, 0);
|
||||
$open->insert($from, 0);
|
||||
|
|
@ -33,7 +58,7 @@ class TileNavigator implements ITileNavigator
|
|||
$gScore = [];
|
||||
$gScore[(string) $from] = 0;
|
||||
$has = [(string)$from, true];
|
||||
if($this->blockedProvider->isBlocked($to)){
|
||||
if($this->isLocBlocked($pmfLevel, $to)){
|
||||
return null;
|
||||
}
|
||||
$visited = [];
|
||||
|
|
@ -41,39 +66,61 @@ class TileNavigator implements ITileNavigator
|
|||
while(!$open->isEmpty())
|
||||
{
|
||||
$current = $open->top();
|
||||
$currentX = $current >> 16 & 0xff;
|
||||
$currentY = $current & 0xff;
|
||||
$currentZ = $current >> 8 & 0xff;
|
||||
$open->next();
|
||||
if ($current == $to){
|
||||
return $this->reconstructPath($path, $current);
|
||||
}
|
||||
foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
|
||||
{
|
||||
$dist = ($from->x - $neighbor->x)*($from->x - $neighbor->x) + ($from->y - $neighbor->y)*($from->y - $neighbor->y) + ($from->z - $neighbor->z)*($from->z - $neighbor->z);
|
||||
if($dist < -$maxDist || $dist > $maxDist){ //Utils::distance_noroot($neighbor->asArray(), $from->asArray())
|
||||
continue;
|
||||
}
|
||||
if(isset($visited[(string)$neighbor])){
|
||||
continue;
|
||||
}
|
||||
$points = [];
|
||||
foreach(self::OFFSETS as $offset){
|
||||
$newX = $offset[0] + $currentX;
|
||||
$newY = $offset[1] + $currentY;
|
||||
$newZ = $offset[2] + $currentZ;
|
||||
if($newX < 0 || $newX > 255 || $newY < 0 || $newY > 255 || $newZ < 0 || $newZ > 255) continue;
|
||||
$new = $newX << 16 | $newZ << 8 | $newY;
|
||||
|
||||
$visited[(string)$neighbor] = $neighbor;
|
||||
$distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor);
|
||||
$tentativeG = $gScore[(string) $current] + $distbetweenCost;
|
||||
if (!isset($has[(string)$neighbor]))
|
||||
if(!$this->isLocBlocked($pmfLevel, $new)){
|
||||
|
||||
$dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ);
|
||||
if($dist < -$maxDist || $dist > $maxDist){
|
||||
continue;
|
||||
}
|
||||
|
||||
$points[] = $new;
|
||||
}
|
||||
}
|
||||
|
||||
foreach($points as $neighbor)
|
||||
{
|
||||
if(isset($visited[$neighbor])){
|
||||
continue;
|
||||
}
|
||||
$visited[$neighbor] = true;
|
||||
|
||||
$diffX = ($currentX - ($neighbor >> 16 & 0xff));
|
||||
$diffY = ($currentY - ($neighbor & 0xff));
|
||||
$diffZ = ($currentZ - ($neighbor >> 8 & 0xff));
|
||||
|
||||
$distbetweenCost = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ;
|
||||
$tentativeG = $gScore[$current] + $distbetweenCost;
|
||||
if (!isset($has[$neighbor]))
|
||||
{
|
||||
//if(isset($open[-$tentativeG])) console("overwriting $tentativeG");
|
||||
//$open[-$tentativeG] = $neighbor;
|
||||
$open->insert($neighbor, -$tentativeG);
|
||||
$has[(string)$neighbor] = true;
|
||||
$has[$neighbor] = true;
|
||||
}
|
||||
elseif ($tentativeG >= $gScore[(string) $neighbor])
|
||||
elseif ($tentativeG >= $gScore[$neighbor])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if(!isset($gScore[(string) $neighbor]) || $distbetweenCost < $gScore[(string) $neighbor]){
|
||||
$path[(string) $neighbor] = $current;
|
||||
if(!isset($gScore[$neighbor]) || $distbetweenCost < $gScore[$neighbor]){
|
||||
$path[$neighbor] = $current;
|
||||
}
|
||||
|
||||
$gScore[(string) $neighbor] = $tentativeG;
|
||||
$gScore[$neighbor] = $tentativeG;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
|
||||
interface IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to);
|
||||
}
|
||||
|
||||
|
|
@ -1,14 +0,0 @@
|
|||
<?php
|
||||
|
||||
class ManhattanHeuristic3D implements IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to)
|
||||
{
|
||||
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
|
||||
return Utils::manh_distance($from->asArray(), $to->asArray());
|
||||
}
|
||||
return 666;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* No sqrt distance
|
||||
*/
|
||||
class Pythagoras3D implements IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to)
|
||||
{
|
||||
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
|
||||
return sqrt(($to->x - $from->x) * ($to->x - $from->x) + ($to->y - $from->y) * ($to->y - $from->y) + ($to->z - $from->z) * ($to->z - $from->z));
|
||||
}
|
||||
return INF;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
interface IBlockedProvider
|
||||
{
|
||||
/**
|
||||
* @param Tile $tile
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isBlocked(PathTile $tile);
|
||||
}
|
||||
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<?php
|
||||
|
||||
interface INeighborProvider
|
||||
{
|
||||
public function getNeighbors(PathTile $tile);
|
||||
}
|
||||
|
||||
|
|
@ -1,12 +0,0 @@
|
|||
<?php
|
||||
|
||||
|
||||
class MCBlockedProvider implements IBlockedProvider
|
||||
{
|
||||
public function isBlocked(PathTile $tile)
|
||||
{
|
||||
return $tile instanceof PathTileXYZ && StaticBlock::getIsSolid($tile->level->level->getBlockID($tile->x, $tile->y, $tile->z));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,47 +0,0 @@
|
|||
<?php
|
||||
|
||||
class MCDiagonalProvider implements INeighborProvider
|
||||
{
|
||||
private static $neighbors = array(
|
||||
[-1, 0, 0],
|
||||
[0, 0, -1],
|
||||
[1, 0, 0],
|
||||
[0, 0, 1],
|
||||
);
|
||||
private static $jumpOffset = [0, 1, 0];
|
||||
private static $moveDownOffset = [0, -1, 0];
|
||||
public function getNeighbors(PathTile $tile)
|
||||
{
|
||||
if($tile instanceof PathTileXYZ){
|
||||
$pnts = [];
|
||||
foreach(MCDiagonalProvider::$neighbors as $offset){
|
||||
$pnt = $tile->addOffset($offset);
|
||||
|
||||
if(!StaticBlock::getIsSolid($tile->level->level->getBlockID($pnt->x, $pnt->y, $pnt->z))){
|
||||
$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;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -77,21 +77,6 @@ require_once(FILE_PATH . "/src/math/Vector3.php");
|
|||
require_once(FILE_PATH . "/src/world/Position.php");
|
||||
require_once(FILE_PATH . "/src/pmf/PMF.php");
|
||||
|
||||
require_once(FILE_PATH . "/src/astarnavigator/IElement.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/PHPUtils.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/PathTile.php");
|
||||
|
||||
require_once(FILE_PATH . "/src/astarnavigator/algorithms/IDistanceAlgorithm.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/algorithms/Pythagoras3D.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/algorithms/ManhattanHeuristic3D.php");
|
||||
|
||||
require_once(FILE_PATH . "/src/astarnavigator/providers/INeighborProvider.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/providers/MCDiagonalProvider.php");
|
||||
|
||||
require_once(FILE_PATH . "/src/astarnavigator/providers/IBlockedProvider.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/providers/MCBlockedProvider.php");
|
||||
|
||||
require_once(FILE_PATH . "/src/astarnavigator/ITileNavigator.php");
|
||||
require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php");
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -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 Pythagoras3D());
|
||||
$this->pathFinder = new TileNavigator();
|
||||
$this->pathFollower = new PathFollower($this);
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->canBeAttacked = true;
|
||||
|
|
@ -121,21 +121,20 @@ abstract class Living extends Entity implements Pathfindable{
|
|||
$this->ai->updateTasks();
|
||||
}
|
||||
/*if($this->onGround){
|
||||
if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
|
||||
$me = new PathTileXYZ($this->x, $this->y, $this->z, $this->level);
|
||||
if(!$this->hasPath()){
|
||||
//$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level);
|
||||
|
||||
$pl = null;
|
||||
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);
|
||||
//$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($pl != null && self::$pathfind){
|
||||
$this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10);
|
||||
}
|
||||
|
||||
|
||||
if($this->path){
|
||||
console("Found path to $target from $me of length ".count($this->path));
|
||||
console("Found path of length ".count($this->path));
|
||||
|
||||
foreach($this->path as $node){
|
||||
$eid = self::$lastPathEID++;
|
||||
|
|
@ -144,9 +143,9 @@ abstract class Living extends Entity implements Pathfindable{
|
|||
$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->x = ($node >> 16 & 0xff) + 0.5;
|
||||
$pk->y = $node & 0xff;
|
||||
$pk->z = ($node >> 8 & 0xff) + 0.5;
|
||||
$pk->yaw = $pk->pitch = 0;
|
||||
$pk->roll = 0;
|
||||
foreach($this->level->players as $player){
|
||||
|
|
|
|||
|
|
@ -18,9 +18,11 @@ class PathFollower{
|
|||
if($this->entity->currentNode == null){
|
||||
$this->entity->currentNode = $this->entity->path[$this->entity->currentIndex];
|
||||
}
|
||||
|
||||
$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)){
|
||||
$moveX = ($this->entity->currentNode >> 16 & 0xff) + 0.5;
|
||||
$moveY = $this->entity->currentNode & 0xff;
|
||||
$moveZ = ($this->entity->currentNode >> 8 & 0xff) + 0.5;
|
||||
$this->entity->ai->mobController->setMovingTarget($moveX + 0.5, $moveY, $moveZ + 0.5, 1.0);
|
||||
if($this->entity->boundingBox->isXYZInsideNS($moveX + 0.5, $moveY, $moveZ + 0.5)){
|
||||
++$this->entity->currentIndex;
|
||||
console("next");
|
||||
$this->entity->currentNode = null;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue