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

View file

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

View file

@ -12,14 +12,15 @@ class MCDiagonalProvider implements INeighborProvider
$pnts = [];
foreach(MCDiagonalProvider::$neighbors as $offset){
$pnt = $tile->addOffset($offset);
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y, $pnt->z)->isSolid){
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y - 1, $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, false)->isSolid){
--$pnt->y;
}
$pnts[] = $pnt;
}else{
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z)->isSolid){
$pnts[] = $pnt->addOffset(self::$jumpOffset);
if(!$tile->level->getBlockWithoutVector($pnt->x, $pnt->y + 1, $pnt->z, false)->isSolid){
++$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()){
$this->target = false;
$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);
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
@ -34,7 +34,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->ai->updateTasks();
}
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->pathFollower->followPath();

View file

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

View file

@ -574,8 +574,17 @@ class Utils{
/**
* Euclidian distance, but without square roots
*/
public static function distance_noroot(Vector3 $pos1, Vector3 $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);
public static function distance_noroot($pos1, $pos2){
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 $y
* @param number $z
* @param boolean $positionfy assign coordinates to block or not
* @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);
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);
}
/**