project uses tabs instead of spaces(as in CONTRIBUTING.md)

This commit is contained in:
GameHerobrine 2022-12-26 07:01:28 +03:00
parent f00318b6ed
commit 62571f26cd
115 changed files with 2291 additions and 2364 deletions

View file

@ -1,40 +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);
}
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

@ -2,6 +2,6 @@
interface IElement
{
public function __toString();
public function __toString();
}

View file

@ -2,6 +2,6 @@
interface ITileNavigator
{
public function navigate(PathTile $from, PathTile $to);
public function navigate(PathTile $from, PathTile $to);
}

View file

@ -2,11 +2,11 @@
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);
}
}
public static function removeElementFromArray(&$arr, $element, $reindex = true){
if (($key = array_search($element, $arr)) !== false) {
unset($arr[$key]);
if($reindex) $arr = array_values($arr);
}
}
}

View file

@ -2,26 +2,26 @@
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;
}
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;
}
}

View file

@ -2,37 +2,37 @@
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;
}
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;
}
}

View file

@ -2,15 +2,15 @@
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){
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 (isset($path[(string)$current]))
{
@ -20,67 +20,67 @@ class TileNavigator implements ITileNavigator
//foreach(array_unique($path) as $k => $p) console($k.":".$p);
//foreach($totalPath as $k => $p) console($k.":".$p);
array_pop($totalPath);
return array_reverse($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;
}
$visited = [];
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;
}
if(in_array($neighbor, $visited)){
continue;
return array_reverse($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;
}
$visited[] = $neighbor;
$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;
}
$visited = [];
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;
}
if(in_array($neighbor, $visited)){
continue;
}
$visited[] = $neighbor;
$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;
}
}

View file

@ -2,6 +2,6 @@
interface IDistanceAlgorithm
{
public function calculate(PathTile $from, PathTile $to);
public function calculate(PathTile $from, PathTile $to);
}

View file

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

View file

@ -2,11 +2,11 @@
class ManhattanHeuristicAlgorithm implements IDistanceAlgorithm
{
public function calculate(PathTile $from, PathTile $to)
{
return abs($from->x - $to->x) + abs($from->y - $to->y);
}
public function calculate(PathTile $from, PathTile $to)
{
return abs($from->x - $to->x) + abs($from->y - $to->y);
}
}

View file

@ -2,13 +2,13 @@
class Pythagoras3D implements IDistanceAlgorithm
{
public function calculate(PathTile $from, PathTile $to)
public function calculate(PathTile $from, PathTile $to)
{
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
return Utils::distance_noroot($from->asVector(), $to->asVector());
}
return 666;
}
if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
return Utils::distance_noroot($from->asVector(), $to->asVector());
}
return 666;
}
}

View file

@ -2,10 +2,10 @@
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));
}
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 +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;
}
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

@ -2,9 +2,9 @@
class EmptyBlockedProvider implements IBlockedProvider
{
public function isBlocked(PathTile $tile)
{
return false;
}
public function isBlocked(PathTile $tile)
{
return false;
}
}

View file

@ -2,11 +2,11 @@
interface IBlockedProvider
{
/**
* @param Tile $tile
*
* @return boolean
*/
public function isBlocked(PathTile $tile);
/**
* @param Tile $tile
*
* @return boolean
*/
public function isBlocked(PathTile $tile);
}

View file

@ -2,6 +2,6 @@
interface INeighborProvider
{
public function getNeighbors(PathTile $tile);
public function getNeighbors(PathTile $tile);
}

View file

@ -3,14 +3,14 @@
class MCBlockedProvider implements IBlockedProvider
{
public function isBlocked(PathTile $tile)
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;
}
if($tile instanceof PathTileXYZ){
$b = $tile->level->getBlockWithoutVector($tile->x, $tile->y, $tile->z);
return $b instanceof Block && $b->isSolid;
}
return false;
}
}

View file

@ -2,21 +2,21 @@
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)
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 [];
}
if($tile instanceof PathTileXYZ){
$result = [];
foreach(self::$neighbors as $offset){
$t = clone $tile;
$result[] = $t->addOffset($offset);
}
return $result;
}
return [];
}
}