pathfinder optimizations

This commit is contained in:
GameHerobrine 2023-01-05 02:31:56 +03:00
parent cba1879947
commit 60c8603c8b
13 changed files with 58 additions and 145 deletions

View file

@ -1,40 +0,0 @@
<?php
class ArrayList{
public $arr = [];
public function add(IElement $e){
$this->arr[$e->__toString()] = $e;
return $this;
}
public function addAll($es){
foreach($es as $e){
$this->add($this->add($e));
}
}
public function getFirst(){
return array_values($this->arr)[0];
}
public function remove(IElement $e){
unset($this->arr[$e->__toString()]);
return $this;
}
public function __isset($e){
return isset($this->arr[$e->__toString()]);
}
public function has(IElement $e){
return isset($this->arr[$e->__toString()]);
}
public function sortWith($func){
return uasort($this->arr, $func);
}
public function countElements(){
return count($this->arr);
}
}

View file

@ -29,10 +29,7 @@ class PathTileXYZ extends PathTile
}
public function addOffset($offset){
$this->x += $offset[0];
$this->y += $offset[1];
$this->z += $offset[2];
return $this;
return new PathTileXYZ($this->x + $offset[0], $this->y + $offset[1], $this->z + $offset[2], $this->level);
}
public function __toString(){

View file

@ -23,64 +23,63 @@ class TileNavigator implements ITileNavigator
return array_reverse($totalPath);
}
public function navigate(PathTile $from, PathTile $to, $maxDist) //TODO optimizations, no arraylist
public function navigate(PathTile $from, PathTile $to, $maxDist)
{
$closed = new ArrayList();
$open = new ArrayList();
$open->add($from);
$path = []; //new Dictionary<Tile, Tile>();
$gScore = []; //new Dictionary<Tile, double>();
$open = new SplPriorityQueue();
$open->insert($from, 0);
$path = [];
$gScore = [];
$gScore[(string) $from] = 0;
$has = [(string)$from, true];
/**
* @var array[Tile] $fScore
*/
$fScore = [];//new Dictionary<Tile, double>();
$fScore = [];
$fScore[(string) $from] = $this->heuristicAlgorithm->calculate($from, $to);
if($this->blockedProvider->isBlocked($to)){
return null;
}
$visited = [];
while($open->countElements() > 0)
while(!$open->isEmpty())
{
//sleep(1);
$open->sortWith(function($k, $k1) use ($fScore){
return $fScore[(string)$k] === $fScore[(string)$k1] ? 0 : ($fScore[(string)$k] > $fScore[(string)$k1] ? 1 : -1);
});
$current = $open->getFirst();
if ($current->equals($to)){
$current = $open->top();
$open->next();
if ($current == $to){
return $this->reconstructPath($path, $current);
}
$open->remove($current);
$closed->add($current);
foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
{
if(in_array($neighbor, $visited)){
continue;
}
$visited[] = $neighbor;
if ($closed->has($neighbor) || $this->blockedProvider->isBlocked($neighbor))
{
continue;
}
if(!Utils::in_range(Utils::distance($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){
continue;
}
if(isset($visited[(string)$neighbor])){
continue;
}
$tentativeG = $gScore[(string) $current] + $this->distanceAlgorithm->calculate($current, $neighbor);
if (!$open->has($neighbor))
$visited[(string)$neighbor] = $neighbor;
if ($this->blockedProvider->isBlocked($neighbor))
{
$open->add($neighbor);
continue;
}
$distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor);
$tentativeG = $gScore[(string) $current] + $distbetweenCost;
$tentativeF = $distbetweenCost + $this->heuristicAlgorithm->calculate($neighbor, $to);
if (!isset($has[(string)$neighbor]))
{
$open->insert($neighbor, -$tentativeF);
$has[(string)$neighbor] = true;
}
else if ($tentativeG >= $gScore[(string) $neighbor])
{
continue;
}
$path[(string) $neighbor] = $current;
if(!isset($gScore[(string) $neighbor]) || $distbetweenCost < $gScore[(string) $neighbor]){
$path[(string) $neighbor] = $current;
}
$gScore[(string) $neighbor] = $tentativeG;
$fScore[(string) $neighbor] = $gScore[(string) $neighbor] + $this->heuristicAlgorithm->calculate($neighbor, $to);
$fScore[(string) $neighbor] = $tentativeF;
}
}

View file

@ -5,7 +5,7 @@ class ManhattanHeuristic3D implements IDistanceAlgorithm
public function calculate(PathTile $from, PathTile $to)
{
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
return Utils::manh_distance($from->asVector(), $to->asVector());
return Utils::manh_distance($from->asArray(), $to->asArray());
}
return 666;
}

View file

@ -1,12 +0,0 @@
<?php
class ManhattanHeuristicAlgorithm implements IDistanceAlgorithm
{
public function calculate(PathTile $from, PathTile $to)
{
return abs($from->x - $to->x) + abs($from->y - $to->y);
}
}

View file

@ -7,7 +7,7 @@ class Pythagoras3D implements IDistanceAlgorithm
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
return Utils::distance($from->asArray(), $to->asArray());
}
return 666;
return INF;
}
}

View file

@ -1,11 +0,0 @@
<?php
class PythagorasAlgorithm implements IDistanceAlgorithm
{
public function calculate(PathTile $from, PathTile $to)
{
return sqrt(pow($to->x - $from->x, 2) + pow($to->y - $from->y, 2));
}
}

View file

@ -1,17 +0,0 @@
<?php
class DiagonalNeighborProvider implements INeighborProvider
{
private static $neighbors = array([ 0, -1 ], [ 1, 0 ], [ 0, 1 ], [ -1, 0 ], [ -1, -1 ], [ 1, -1 ], [ 1, 1 ], [ -1, 1 ]);
public function getNeighbors(PathTile $tile)
{
$result = [];
foreach(self::$neighbors as $offset){
$t = clone $tile;
$result[] = $t->addOffset($offset);
}
return $result;
}
}

View file

@ -1,10 +0,0 @@
<?php
class EmptyBlockedProvider implements IBlockedProvider
{
public function isBlocked(PathTile $tile)
{
return false;
}
}

View file

@ -7,12 +7,9 @@ class MCDiagonalProvider implements INeighborProvider
{
if($tile instanceof PathTileXYZ){
$result = [];
foreach(self::$neighbors as $offset){
$t = clone $tile;
$result[] = $t->addOffset($offset);
}
return $result;
return array_map(function($offset) use($tile){
return $tile->addOffset($offset);
}, MCDiagonalProvider::$neighbors);
}
return [];
}

View file

@ -80,17 +80,16 @@ 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/ArrayList.php");
require_once(FILE_PATH . "/src/astarnavigator/algorithms/IDistanceAlgorithm.php");
require_once(FILE_PATH . "/src/astarnavigator/algorithms/PythagorasAlgorithm.php");
require_once(FILE_PATH . "/src/astarnavigator/algorithms/ManhattanHeuristicAlgorithm.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/DiagonalNeighborProvider.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/EmptyBlockedProvider.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");

View file

@ -32,9 +32,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
}
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);
}*/
//if(!$this->hasPath() && $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();
}

View file

@ -423,7 +423,11 @@ class Utils{
}
return $data;
}
public static function php8_nulcoal(&$arr, $else){
return isset($arr) ? $arr : $else;
}
public static function readTriad($str){
return @unpack("N", "\x00" . $str)[1];
}
@ -556,8 +560,15 @@ class Utils{
/**
* manhattan distance, less accurate, but still useful sometimes
*/
public static function manh_distance(Vector3 $pos1, Vector3 $pos2){
return abs($pos2->x - $pos1->x) + abs($pos2->y - $pos1->y) + abs($pos2->z - $pos1->z);
public static function manh_distance($pos1, $pos2){
if($pos1 instanceof Vector3){
$pos1 = $pos1->toArray();
}
if($pos2 instanceof Vector3){
$pos2 = $pos2->toArray();
}
return abs($pos2["x"] - $pos1["x"]) + abs($pos2["y"] - $pos1["y"]) + abs($pos2["z"] - $pos1["z"]);
}
/**