small tweaks

This commit is contained in:
GameHerobrine 2023-01-22 17:25:23 +03:00
parent c41e31e237
commit 9f5304bdad
7 changed files with 52 additions and 47 deletions

View file

@ -1,13 +1,14 @@
<?php <?php
class TileNavigator implements ITileNavigator class TileNavigator implements ITileNavigator
{ {
private $blockedProvider, $neighborProvider, $distanceAlgorithm, $heuristicAlgorithm; private $blockedProvider, $neighborProvider, $distanceAlgorithm;
public function __construct(IBlockedProvider $blockedProvider, INeighborProvider $neighborProvider, IDistanceAlgorithm $distanceAlgorithm, IDistanceAlgorithm $heuristicAlgorithm){
public static $pathfinderAccessed = 0;
public function __construct(IBlockedProvider $blockedProvider, INeighborProvider $neighborProvider, IDistanceAlgorithm $distanceAlgorithm){
$this->blockedProvider = $blockedProvider; $this->blockedProvider = $blockedProvider;
$this->neighborProvider = $neighborProvider; $this->neighborProvider = $neighborProvider;
$this->distanceAlgorithm = $distanceAlgorithm; $this->distanceAlgorithm = $distanceAlgorithm;
$this->heuristicAlgorithm = $heuristicAlgorithm;
} }
public function reconstructPath($path, $current){ public function reconstructPath($path, $current){
@ -31,15 +32,11 @@ class TileNavigator implements ITileNavigator
$gScore = []; $gScore = [];
$gScore[(string) $from] = 0; $gScore[(string) $from] = 0;
$has = [(string)$from, true]; $has = [(string)$from, true];
/**
* @var array[Tile] $fScore
*/
$fScore = [];
$fScore[(string) $from] = $this->heuristicAlgorithm->calculate($from, $to);
if($this->blockedProvider->isBlocked($to)){ if($this->blockedProvider->isBlocked($to)){
return null; return null;
} }
$visited = []; $visited = [];
$maxDist*=$maxDist; //no square root
while(!$open->isEmpty()) while(!$open->isEmpty())
{ {
$current = $open->top(); $current = $open->top();
@ -50,7 +47,7 @@ class TileNavigator implements ITileNavigator
} }
foreach($this->neighborProvider->getNeighbors($current) as $neighbor) foreach($this->neighborProvider->getNeighbors($current) as $neighbor)
{ {
if(!Utils::in_range(Utils::distance($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){ if(!Utils::in_range(Utils::distance_noroot($neighbor->asArray(), $from->asArray()), -$maxDist, $maxDist)){
continue; continue;
} }
if(isset($visited[(string)$neighbor])){ if(isset($visited[(string)$neighbor])){
@ -58,16 +55,11 @@ class TileNavigator implements ITileNavigator
} }
$visited[(string)$neighbor] = $neighbor; $visited[(string)$neighbor] = $neighbor;
//if ($this->blockedProvider->isBlocked($neighbor)) shouldnt be neccessary now
//{
// continue;
//}
$distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor); $distbetweenCost = $this->distanceAlgorithm->calculate($current, $neighbor);
$tentativeG = $gScore[(string) $current] + $distbetweenCost; $tentativeG = $gScore[(string) $current] + $distbetweenCost;
$tentativeF = $distbetweenCost + $this->heuristicAlgorithm->calculate($neighbor, $to);
if (!isset($has[(string)$neighbor])) if (!isset($has[(string)$neighbor]))
{ {
$open->insert($neighbor, -$tentativeF); $open->insert($neighbor, -$tentativeG);
$has[(string)$neighbor] = true; $has[(string)$neighbor] = true;
} }
else if ($tentativeG >= $gScore[(string) $neighbor]) else if ($tentativeG >= $gScore[(string) $neighbor])
@ -79,7 +71,6 @@ class TileNavigator implements ITileNavigator
} }
$gScore[(string) $neighbor] = $tentativeG; $gScore[(string) $neighbor] = $tentativeG;
$fScore[(string) $neighbor] = $tentativeF;
} }
} }

View file

@ -1,11 +1,13 @@
<?php <?php
/**
* No sqrt distance
*/
class Pythagoras3D implements IDistanceAlgorithm 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){ if($from instanceof PathTileXYZ && $to instanceof PathTileXYZ){
return Utils::distance($from->asArray(), $to->asArray()); return Utils::distance_noroot($from->asArray(), $to->asArray());
} }
return INF; return INF;
} }

View file

@ -12,14 +12,15 @@ class MCDiagonalProvider implements INeighborProvider
$pnts = []; $pnts = [];
foreach(MCDiagonalProvider::$neighbors as $offset){ foreach(MCDiagonalProvider::$neighbors as $offset){
$pnt = $tile->addOffset($offset); $pnt = $tile->addOffset($offset);
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z)->isSolid){ if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z, false)->isSolid){
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $pnt->z)->isSolid){ if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $pnt->z, false)->isSolid){
--$pnt->y; --$pnt->y;
} }
$pnts[] = $pnt; $pnts[] = $pnt;
}else{ }else{
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z)->isSolid){ if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z, false)->isSolid){
$pnts[] = $pnt->addOffset(self::$jumpOffset); ++$pnt->y;
$pnts[] = $pnt;
} }
} }
} }

View file

@ -9,7 +9,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false; $this->target = false;
$this->ai = new EntityAI($this); $this->ai = new EntityAI($this);
$this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new Pythagoras3D(), new ManhattanHeuristic3D()); $this->pathFinder = new TileNavigator(new MCBlockedProvider(), new MCDiagonalProvider(), new ManhattanHeuristic3D());
$this->pathFollower = new PathFollower($this); $this->pathFollower = new PathFollower($this);
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true; $this->canBeAttacked = true;
@ -34,7 +34,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->ai->updateTasks(); $this->ai->updateTasks();
} }
if($this->onGround){ if($this->onGround){
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){ //if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
// $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 + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10); // $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 + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10);
//} //}
$this->pathFollower->followPath(); $this->pathFollower->followPath();

View file

@ -333,11 +333,12 @@ class PMFLevel extends PMF{
$Z = $z >> 4; $Z = $z >> 4;
$Y = $y >> 4; $Y = $y >> 4;
$index = $this->getIndex($X, $Z); $index = $this->getIndex($X, $Z);
$aX = $x - ($X << 4); $aX = $x & 0xf;
$aZ = $z - ($Z << 4); $aZ = $z & 0xf;
$aY = $y - ($Y << 4); $aY = $y & 0xf;
if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){
$b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]);
}else{ //php8 fix }else{ //php8 fix
$b = 0; $b = 0;
} }
@ -357,9 +358,9 @@ class PMFLevel extends PMF{
return false; return false;
} }
$index = $this->getIndex($X, $Z); $index = $this->getIndex($X, $Z);
$aX = $x - ($X << 4); $aX = $x & 0xf;
$aZ = $z - ($Z << 4); $aZ = $z & 0xf;
$aY = $y - ($Y << 4); $aY = $y & 0xf;
$this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))] = chr($block); $this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))] = chr($block);
if(!isset($this->chunkChange[$index][$Y])){ if(!isset($this->chunkChange[$index][$Y])){
$this->chunkChange[$index][$Y] = 1; $this->chunkChange[$index][$Y] = 1;
@ -378,9 +379,9 @@ class PMFLevel extends PMF{
$Z = $z >> 4; $Z = $z >> 4;
$Y = $y >> 4; $Y = $y >> 4;
$index = $this->getIndex($X, $Z); $index = $this->getIndex($X, $Z);
$aX = $x - ($X << 4); $aX = $x & 0xf;
$aZ = $z - ($Z << 4); $aZ = $z & 0xf;
$aY = $y - ($Y << 4); $aY = $y & 0xf;
if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){ if(is_array($this->chunks) && isset($this->chunks[$index]) && is_array($this->chunks[$index]) && isset($this->chunks[$index][$Y]) && is_string($this->chunks[$index][$Y])){
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); $m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]);
}else{ //php8 fix }else{ //php8 fix
@ -407,9 +408,9 @@ class PMFLevel extends PMF{
return false; return false;
} }
$index = $this->getIndex($X, $Z); $index = $this->getIndex($X, $Z);
$aX = $x - ($X << 4); $aX = $x & 0xf;
$aZ = $z - ($Z << 4); $aZ = $z & 0xf;
$aY = $y - ($Y << 4); $aY = $y & 0xf;
$mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9)); $mindex = (int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9));
$old_m = ord($this->chunks[$index][$Y][$mindex]); $old_m = ord($this->chunks[$index][$Y][$mindex]);
if(($y & 1) === 0){ if(($y & 1) === 0){
@ -446,13 +447,13 @@ class PMFLevel extends PMF{
}elseif($this->chunks[$index][$Y] === false){ }elseif($this->chunks[$index][$Y] === false){
return [AIR, 0]; return [AIR, 0];
} }
$aX = $x - ($X << 4); $aX = $x & 0xf;
$aZ = $z - ($Z << 4); $aZ = $z & 0xf;
$aY = $y - ($Y << 4); $aY = $y & 0xf;
#Need to fix. But idk how. #Need to fix. But idk how.
if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix
$b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]); $b = ord($this->chunks[$index][$Y][($aY + ($aX << 5) + ($aZ << 9))]);
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]); $m = ord($this->chunks[$index][$Y][(($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]);
}else{ }else{
$b = 0; $b = 0;
$m = 0; $m = 0;

View file

@ -574,8 +574,17 @@ class Utils{
/** /**
* Euclidian distance, but without square roots * Euclidian distance, but without square roots
*/ */
public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){ public static function distance_noroot($pos1, $pos2){
return ($pos2->x - $pos1->x)*($pos2->x - $pos1->x) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z) + ($pos2->z - $pos1->z)*($pos2->z - $pos1->z); if($pos1 instanceof Vector3){
$pos1 = $pos1->toArray();
}
if($pos2 instanceof Vector3){
$pos2 = $pos2->toArray();
}
$pX = ($pos1["x"] - $pos2["x"]);
$pY = ($pos1["y"] - $pos2["y"]);
$pZ = ($pos1["z"] - $pos2["z"]);
return ($pX*$pX) + ($pY*$pY) + ($pZ*$pZ);
} }
/** /**

View file

@ -456,12 +456,13 @@ class Level{
* @param number $x * @param number $x
* @param number $y * @param number $y
* @param number $z * @param number $z
* @param boolean $positionfy assign coordinates to block or not
* @return GenericBlock | false if failed * @return GenericBlock | false if failed
*/ */
public function getBlockWithoutVector($x, $y, $z){ public function getBlockWithoutVector($x, $y, $z, $positionfy = true){
$b = $this->level->getBlock($x, $y, $z); $b = $this->level->getBlock($x, $y, $z);
return BlockAPI::get($b[0], $b[1], new Position($x, $y, $z, $this)); return BlockAPI::get($b[0], $b[1], $positionfy ? new Position($x, $y, $z, $this) : false);
} }
/** /**