pathfinder will avoid fall damage and jump

This commit is contained in:
GameHerobrine 2024-04-14 05:51:04 +03:00
parent 27f50cc4ab
commit 0263afb0b6
2 changed files with 56 additions and 10 deletions

View file

@ -6,12 +6,16 @@ class TileNavigator
[0, 0, 1], [0, 0, 1],
[0, 0, -1], [0, 0, -1],
[1, 0, 0], [1, 0, 0],
[-1, 0, 0] [-1, 0, 0],
]; ];
public static $pathfinderAccessed = 0; public static $pathfinderAccessed = 0;
public $entity;
public function __construct(){ public $avoidFallDamage = true;
public $tall = false;
public function __construct(Living $entity){
$this->entity = $entity;
$this->tall = $entity->height > 1;
} }
public function reconstructPath($path, $current){ public function reconstructPath($path, $current){
@ -40,7 +44,16 @@ class TileNavigator
$blockY = $loc & 0xf; $blockY = $loc & 0xf;
$id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index); $id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index);
return StaticBlock::getIsSolid($id); $b = StaticBlock::getIsSolid($id);
if($this->tall){
$loc += 1;
$chunkY = $loc >> 4 & 0xf;
$blockY = $loc & 0xf;
$id = $level->fastGetBlockID($chunkX, $chunkY, $chunkZ, $blockX, $blockY, $blockZ, $index);
return $b || StaticBlock::getIsSolid($id);
}
return $b;
} }
@ -76,6 +89,7 @@ class TileNavigator
if ($current == $to){ if ($current == $to){
return $this->reconstructPath($path, $current); return $this->reconstructPath($path, $current);
} }
$points = []; $points = [];
foreach(self::OFFSETS as $offset){ foreach(self::OFFSETS as $offset){
$newX = $offset[0] + $currentX; $newX = $offset[0] + $currentX;
@ -85,6 +99,34 @@ class TileNavigator
$new = $newX << 16 | $newZ << 8 | $newY; $new = $newX << 16 | $newZ << 8 | $newY;
if(!$this->isLocBlocked($pmfLevel, $new)){ if(!$this->isLocBlocked($pmfLevel, $new)){
if($newY > 0 && !$this->isLocBlocked($pmfLevel, $new - 1)){
if(!$this->avoidFallDamage){
$new -= 1;
$newY -= 1;
}else{
if($this->isLocBlocked($pmfLevel, $new - 2)){
$new -= 1;
$newY -= 1;
}else if($this->isLocBlocked($pmfLevel, $new - 3)){
$new -= 2;
$newY -= 2;
}else if($this->isLocBlocked($pmfLevel, $new - 4)){
$new -= 3;
$newY -= 3;
}else{
continue;
}
}
}
$dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ);
if($dist < -$maxDist || $dist > $maxDist){
continue;
}
$points[] = $new;
}else if(!$this->isLocBlocked($pmfLevel, $current+1) && !$this->isLocBlocked($pmfLevel, $new+1)){
$new += 1;
$newY += 1;
$dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ); $dist = ($fromX - $newX)*($fromX - $newX) + ($fromY - $newY)*($fromY - $newY) + ($fromZ - $newZ)*($fromZ - $newZ);
if($dist < -$maxDist || $dist > $maxDist){ if($dist < -$maxDist || $dist > $maxDist){

View file

@ -3,6 +3,9 @@
abstract class Living extends Entity implements Pathfindable{ abstract class Living extends Entity implements Pathfindable{
public static $despawnMobs, $despawnTimer, $entityPushing = false; public static $despawnMobs, $despawnTimer, $entityPushing = false;
public static $pathfinderTest = false;
public $moveStrafing, $moveForward; public $moveStrafing, $moveForward;
/** /**
* @var Entity $target * @var Entity $target
@ -21,7 +24,7 @@ abstract class Living extends Entity implements 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(); $this->pathFinder = new TileNavigator($this);
$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;
@ -71,6 +74,7 @@ abstract class Living extends Entity implements Pathfindable{
unset($this->ai->mobController->entity); unset($this->ai->mobController->entity);
unset($this->ai); unset($this->ai);
unset($this->parent); unset($this->parent);
unset($this->pathFinder->entity);
} }
public function canBeShot(){ public function canBeShot(){
@ -117,10 +121,10 @@ abstract class Living extends Entity implements Pathfindable{
private static $lastPathEID = 10000000; private static $lastPathEID = 10000000;
public static $pathfind = true; public static $pathfind = true;
public function updateEntityMovement(){ public function updateEntityMovement(){
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0 && !self::$pathfinderTest) {
$this->ai->updateTasks(); $this->ai->updateTasks();
} }
/*if($this->onGround){ if(self::$pathfinderTest){
if(!$this->hasPath()){ if(!$this->hasPath()){
//$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level); //$target = new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level);
$pl = null; $pl = null;
@ -128,7 +132,7 @@ abstract class Living extends Entity implements Pathfindable{
$pl = array_values($this->level->players)[0]; $pl = array_values($this->level->players)[0];
//$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level); //$target = new PathTileXYZ($pl->entity->x, $pl->entity->y, $pl->entity->z, $this->level);
} }
if($pl != null && self::$pathfind){ if(self::$pathfind){
$this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10); $this->path = $this->pathFinder->navigate($this->level, (int)$this->x, (int)$this->y, (int)$this->z, (int)$pl->entity->x, (int)$pl->entity->y, (int)$pl->entity->z, 10);
} }
@ -155,7 +159,7 @@ abstract class Living extends Entity implements Pathfindable{
} }
} }
$this->pathFollower->followPath(); $this->pathFollower->followPath();
}*/ }
//not exactly here //not exactly here