mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
changed pathfinder(hopefully one last time)
This commit is contained in:
parent
8c12e0de26
commit
e11c40f645
28 changed files with 401 additions and 416 deletions
40
src/astarnavigator/ArrayList.php
Normal file
40
src/astarnavigator/ArrayList.php
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
7
src/astarnavigator/IElement.php
Normal file
7
src/astarnavigator/IElement.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
interface IElement
|
||||
{
|
||||
public function __toString();
|
||||
}
|
||||
|
||||
7
src/astarnavigator/ITileNavigator.php
Normal file
7
src/astarnavigator/ITileNavigator.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
interface ITileNavigator
|
||||
{
|
||||
public function navigate(PathTile $from, PathTile $to);
|
||||
}
|
||||
|
||||
12
src/astarnavigator/PHPUtils.php
Normal file
12
src/astarnavigator/PHPUtils.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
27
src/astarnavigator/PathTile.php
Normal file
27
src/astarnavigator/PathTile.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
38
src/astarnavigator/PathTileXYZ.php
Normal file
38
src/astarnavigator/PathTileXYZ.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?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 addOffset($offset){
|
||||
$this->x += $offset[0];
|
||||
$this->y += $offset[1];
|
||||
$this->z += $offset[2];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function __toString(){
|
||||
return $this->x.":".$this->y.":".$this->z;
|
||||
}
|
||||
}
|
||||
|
||||
84
src/astarnavigator/TileNavigator.php
Normal file
84
src/astarnavigator/TileNavigator.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
class TileNavigator implements ITileNavigator
|
||||
{
|
||||
private $blockedProvider, $neighborProvider, $distanceAlgorithm, $heuristicAlgorithm;
|
||||
public function __construct(IBlockedProvider $blockedProvider, INeighborProvider $neighborProvider, IDistanceAlgorithm $distanceAlgorithm, IDistanceAlgorithm $heuristicAlgorithm){
|
||||
$this->blockedProvider = $blockedProvider;
|
||||
$this->neighborProvider = $neighborProvider;
|
||||
$this->distanceAlgorithm = $distanceAlgorithm;
|
||||
$this->heuristicAlgorithm = $heuristicAlgorithm;
|
||||
}
|
||||
|
||||
public function reconstructPath($path, $current){
|
||||
$totalPath = [$current];
|
||||
while (array_key_exists((string)$current, $path))
|
||||
{
|
||||
$current = $path[(string)$current];
|
||||
$totalPath[] = $current;
|
||||
}
|
||||
|
||||
$totalPath = array_reverse($totalPath);
|
||||
unset($totalPath[0]);
|
||||
|
||||
return array_values($totalPath);
|
||||
}
|
||||
|
||||
public function navigate(PathTile $from, PathTile $to)
|
||||
{
|
||||
$closed = new ArrayList();
|
||||
$open = new ArrayList();
|
||||
$open->add($from);
|
||||
$path = []; //new Dictionary<Tile, Tile>();
|
||||
$gScore = []; //new Dictionary<Tile, double>();
|
||||
$gScore[(string) $from] = 0;
|
||||
/**
|
||||
* @var array[Tile] $fScore
|
||||
*/
|
||||
$fScore = [];//new Dictionary<Tile, double>();
|
||||
$fScore[(string) $from] = $this->heuristicAlgorithm->calculate($from, $to);
|
||||
if($this->blockedProvider->isBlocked($to)){
|
||||
return null;
|
||||
}
|
||||
while($open->countElements() > 0)
|
||||
{
|
||||
//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)){
|
||||
return $this->reconstructPath($path, $current);
|
||||
}
|
||||
$open->remove($current);
|
||||
$closed->add($current);
|
||||
foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
|
||||
{
|
||||
|
||||
if ($closed->has($neighbor) || $this->blockedProvider->isBlocked($neighbor))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
$tentativeG = $gScore[(string) $current] + $this->distanceAlgorithm->calculate($current, $neighbor);
|
||||
|
||||
if (!$open->has($neighbor))
|
||||
{
|
||||
$open->add($neighbor);
|
||||
}
|
||||
else if ($tentativeG >= $gScore[(string) $neighbor])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
$path[(string) $neighbor] = $current;
|
||||
$gScore[(string) $neighbor] = $tentativeG;
|
||||
$fScore[(string) $neighbor] = $gScore[(string) $neighbor] + $this->heuristicAlgorithm->calculate($neighbor, $to);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
7
src/astarnavigator/algorithms/IDistanceAlgorithm.php
Normal file
7
src/astarnavigator/algorithms/IDistanceAlgorithm.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
interface IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to);
|
||||
}
|
||||
|
||||
14
src/astarnavigator/algorithms/ManhattanHeuristic3D.php
Normal file
14
src/astarnavigator/algorithms/ManhattanHeuristic3D.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
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 666;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
class ManhattanHeuristicAlgorithm implements IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to)
|
||||
{
|
||||
return abs($from->x - $to->x) + abs($from->y - $to->y);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
14
src/astarnavigator/algorithms/Pythagoras3D.php
Normal file
14
src/astarnavigator/algorithms/Pythagoras3D.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
class Pythagoras3D implements IDistanceAlgorithm
|
||||
{
|
||||
public function calculate(PathTile $from, PathTile $to)
|
||||
{
|
||||
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
|
||||
return Utils::distance_noroot($from->asVector(), $to->asVector());
|
||||
}
|
||||
return 666;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
11
src/astarnavigator/algorithms/PythagorasAlgorithm.php
Normal file
11
src/astarnavigator/algorithms/PythagorasAlgorithm.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?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));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
17
src/astarnavigator/providers/DiagonalNeighborProvider.php
Normal file
17
src/astarnavigator/providers/DiagonalNeighborProvider.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
10
src/astarnavigator/providers/EmptyBlockedProvider.php
Normal file
10
src/astarnavigator/providers/EmptyBlockedProvider.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
class EmptyBlockedProvider implements IBlockedProvider
|
||||
{
|
||||
public function isBlocked(PathTile $tile)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
12
src/astarnavigator/providers/IBlockedProvider.php
Normal file
12
src/astarnavigator/providers/IBlockedProvider.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
interface IBlockedProvider
|
||||
{
|
||||
/**
|
||||
* @param Tile $tile
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function isBlocked(PathTile $tile);
|
||||
}
|
||||
|
||||
7
src/astarnavigator/providers/INeighborProvider.php
Normal file
7
src/astarnavigator/providers/INeighborProvider.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
|
||||
interface INeighborProvider
|
||||
{
|
||||
public function getNeighbors(PathTile $tile);
|
||||
}
|
||||
|
||||
16
src/astarnavigator/providers/MCBlockedProvider.php
Normal file
16
src/astarnavigator/providers/MCBlockedProvider.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
|
||||
class MCBlockedProvider implements IBlockedProvider
|
||||
{
|
||||
public function isBlocked(PathTile $tile)
|
||||
{
|
||||
if($tile instanceof PathTileXYZ){
|
||||
$b = $tile->level->getBlockWithoutVector($tile->x, $tile->y, $tile->z);
|
||||
return $b instanceof Block && $b->isSolid;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
22
src/astarnavigator/providers/MCDiagonalProvider.php
Normal file
22
src/astarnavigator/providers/MCDiagonalProvider.php
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
class MCDiagonalProvider implements INeighborProvider
|
||||
{
|
||||
private static $neighbors = array([ 0, 0, -1 ], [ 1, 0, 0 ], [ 0, 0, 1 ], [ -1, 0, 0 ], [ -1, 0, -1 ], [ 1, 0, -1 ], [ 1, 0, 1 ], [ -1, 0, 1 ]);
|
||||
public function getNeighbors(PathTile $tile)
|
||||
{
|
||||
|
||||
if($tile instanceof PathTileXYZ){
|
||||
$result = [];
|
||||
foreach(self::$neighbors as $offset){
|
||||
$t = clone $tile;
|
||||
$result[] = $t->addOffset($offset);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue