From 0263afb0b66e041dcf55365b8327f3bb397e3915 Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sun, 14 Apr 2024 05:51:04 +0300 Subject: [PATCH] pathfinder will avoid fall damage and jump --- src/astarnavigator/TileNavigator.php | 52 +++++++++++++++++++++++++--- src/entity/Living.php | 14 +++++--- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/astarnavigator/TileNavigator.php b/src/astarnavigator/TileNavigator.php index 77f5f398..85c3b8a9 100644 --- a/src/astarnavigator/TileNavigator.php +++ b/src/astarnavigator/TileNavigator.php @@ -6,12 +6,16 @@ class TileNavigator [0, 0, 1], [0, 0, -1], [1, 0, 0], - [-1, 0, 0] + [-1, 0, 0], ]; public static $pathfinderAccessed = 0; - - public function __construct(){ + public $entity; + public $avoidFallDamage = true; + public $tall = false; + public function __construct(Living $entity){ + $this->entity = $entity; + $this->tall = $entity->height > 1; } public function reconstructPath($path, $current){ @@ -40,7 +44,16 @@ class TileNavigator $blockY = $loc & 0xf; $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){ return $this->reconstructPath($path, $current); } + $points = []; foreach(self::OFFSETS as $offset){ $newX = $offset[0] + $currentX; @@ -85,6 +99,34 @@ class TileNavigator $new = $newX << 16 | $newZ << 8 | $newY; 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); if($dist < -$maxDist || $dist > $maxDist){ @@ -115,7 +157,7 @@ class TileNavigator $distbetweenCost = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ; $tentativeG = $gScore[$current] + $distbetweenCost; if (!isset($has[$neighbor])) - { + { //if(isset($open[-$tentativeG])) console("overwriting $tentativeG"); //$open[-$tentativeG] = $neighbor; diff --git a/src/entity/Living.php b/src/entity/Living.php index ed238dd5..e4cc7f10 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -3,6 +3,9 @@ abstract class Living extends Entity implements Pathfindable{ public static $despawnMobs, $despawnTimer, $entityPushing = false; + + public static $pathfinderTest = false; + public $moveStrafing, $moveForward; /** * @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()){ $this->target = false; $this->ai = new EntityAI($this); - $this->pathFinder = new TileNavigator(); + $this->pathFinder = new TileNavigator($this); $this->pathFollower = new PathFollower($this); parent::__construct($level, $eid, $class, $type, $data); $this->canBeAttacked = true; @@ -71,6 +74,7 @@ abstract class Living extends Entity implements Pathfindable{ unset($this->ai->mobController->entity); unset($this->ai); unset($this->parent); + unset($this->pathFinder->entity); } public function canBeShot(){ @@ -117,10 +121,10 @@ abstract class Living extends Entity implements Pathfindable{ private static $lastPathEID = 10000000; public static $pathfind = true; public function updateEntityMovement(){ - if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { + if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0 && !self::$pathfinderTest) { $this->ai->updateTasks(); } - /*if($this->onGround){ + if(self::$pathfinderTest){ 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); $pl = null; @@ -128,7 +132,7 @@ abstract class Living extends Entity implements Pathfindable{ $pl = array_values($this->level->players)[0]; //$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); } @@ -155,7 +159,7 @@ abstract class Living extends Entity implements Pathfindable{ } } $this->pathFollower->followPath(); - }*/ + } //not exactly here