mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
pathfinder optimizations
This commit is contained in:
parent
cba1879947
commit
60c8603c8b
13 changed files with 58 additions and 145 deletions
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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(){
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
<?php
|
||||
|
||||
class EmptyBlockedProvider implements IBlockedProvider
|
||||
{
|
||||
public function isBlocked(PathTile $tile)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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 [];
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue