├── .poggit.yml ├── README.md ├── plugin.yml └── src └── SalmonDE └── PathfindingExample ├── Main.php └── PathVisualizerTask.php /.poggit.yml: -------------------------------------------------------------------------------- 1 | --- # Poggit-CI Manifest. Open the CI at https://poggit.pmmp.io/ci/SalmonDE/Pathfinding-Example 2 | branches: 3 | - master 4 | projects: 5 | Pathfinding-Example: 6 | path: "" 7 | libs: 8 | - src: SalmonDE/Pathfinding/Pathfinding 9 | version: ^0.1.0 10 | ... 11 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pathfinding-Example 2 | Example usage of [my pathfinding virion](https://poggit.pmmp.io/ci/SalmonDE/Pathfinding/~). 3 | You can find pre-built builds on poggit: [https://poggit.pmmp.io/ci/SalmonDE/Pathfinding-Example/](https://poggit.pmmp.io/ci/SalmonDE/Pathfinding-Example/~). 4 | -------------------------------------------------------------------------------- /plugin.yml: -------------------------------------------------------------------------------- 1 | name: PathfinderExample 2 | version: 1.1.0 3 | author: SalmonDE 4 | main: SalmonDE\PathfindingExample\Main 5 | api: [4.0.0] 6 | commands: 7 | a*: 8 | usage: /a* <1|2|calculate|repeat|clear> [2d] 9 | permission: astar 10 | permissions: 11 | astar: 12 | default: op 13 | -------------------------------------------------------------------------------- /src/SalmonDE/PathfindingExample/Main.php: -------------------------------------------------------------------------------- 1 | pos1 = $sender->getPosition()->floor(); 29 | $sender->sendMessage('[A*] Pos 1 set'); 30 | }elseif($params[0] === '2'){ 31 | $this->pos2 = $sender->getPosition()->floor(); 32 | $sender->sendMessage('[A*] Pos 2 set'); 33 | }elseif($params[0] === 'calculate'){ 34 | if($this->pos1 === null || $this->pos2 === null){ 35 | return false; 36 | } 37 | 38 | $sender->getWorld()->setBlock($this->pos1, VanillaBlocks::AIR()); 39 | $sender->getWorld()->setBlock($this->pos2, VanillaBlocks::AIR()); 40 | 41 | $pathfinder = new Pathfinder($sender->getWorld(), $this->pos1, $this->pos2); 42 | $pathfinder->setMaxJumpHeight(1); 43 | 44 | if(isset($params[1])){ 45 | if(strtolower($params[1]) === '2d'){ 46 | $pathfinder->getAlgorithm()->setNeighbourSelector(new NeighbourSelectorXZ()); 47 | } 48 | } 49 | 50 | if($this->lastPathResult !== null){ 51 | $sender->sendMessage("[A*] Clearing last path ..."); 52 | $this->clearLastPath(); 53 | } 54 | 55 | $sender->sendMessage('[A*] Calculating ...'); 56 | $pathfinder->findPath(); 57 | $sender->sendMessage('[A*] Done.'); 58 | 59 | $this->lastPathResult = $pathfinder->getPathResult(); 60 | $this->lastPathWorld = $sender->getWorld(); 61 | if($this->lastPathResult === null){ 62 | $sender->sendMessage('[A*] Null.'); 63 | return true; 64 | }else{ 65 | foreach($this->lastPathResult as $blockPos){ 66 | $sender->getWorld()->setBlock($blockPos, VanillaBlocks::GOLD()); 67 | } 68 | } 69 | 70 | $sender->getWorld()->setBlock($this->pos1, VanillaBlocks::DIAMOND()); 71 | $sender->getWorld()->setBlock($this->pos2, VanillaBlocks::EMERALD()); 72 | }elseif($params[0] === 'repeat'){ 73 | if($this->pos1 === null || $this->pos2 === null){ 74 | return false; 75 | } 76 | 77 | $pathfinder = new Pathfinder($sender->getWorld(), $this->pos1, $this->pos2); 78 | 79 | if(isset($params[1])){ 80 | if(strtolower($params[1]) === '2d'){ 81 | $pathfinder->getAlgorithm()->setNeighbourSelector(new NeighbourSelectorXZ()); 82 | } 83 | } 84 | 85 | $this->getScheduler()->scheduleRepeatingTask(new PathVisualizerTask($pathfinder), 40); 86 | $sender->sendMessage('Visualizing ...'); 87 | }elseif($params[0] === 'clear'){ 88 | $this->getScheduler()->cancelAllTasks(); 89 | $sender->sendMessage('[A*] Cleared.'); 90 | }else{ 91 | return false; 92 | } 93 | 94 | return true; 95 | } 96 | 97 | public function clearLastPath(): void{ 98 | if($this->lastPathResult === null){ 99 | return; 100 | } 101 | 102 | foreach($this->lastPathResult as $blockPos){ 103 | if($this->lastPathWorld->getBlock($blockPos)->getId() === Ids::GOLD_BLOCK){ 104 | $this->lastPathWorld->setBlock($blockPos, VanillaBlocks::AIR()); 105 | } 106 | } 107 | 108 | $this->lastPathResult = null; 109 | 110 | if($this->lastPathWorld->getBlock($this->pos1)->getId() === Ids::DIAMOND_BLOCK){ 111 | $this->lastPathWorld->setBlock($this->pos1, VanillaBlocks::AIR()); 112 | } 113 | 114 | if($this->lastPathWorld->getBlock($this->pos2)->getId() === Ids::EMERALD_BLOCK){ 115 | $this->lastPathWorld->setBlock($this->pos2, VanillaBlocks::AIR()); 116 | } 117 | 118 | $this->lastPathWorld = null; 119 | } 120 | 121 | public function onDisable(): void{ 122 | if($this->lastPathResult !== null){ 123 | $this->clearLastPath(); 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /src/SalmonDE/PathfindingExample/PathVisualizerTask.php: -------------------------------------------------------------------------------- 1 | pathfinder = $pathfinder; 19 | } 20 | 21 | public function onRun(): void{ 22 | $this->pathfinder->getAlgorithm()->resetPathResult(); 23 | $this->pathfinder->findPath(); 24 | $path = $this->pathfinder->getPathResult(); 25 | 26 | if(!($path instanceof PathResult)){ 27 | return; 28 | } 29 | 30 | $world = $this->pathfinder->getAlgorithm()->getWorld(); 31 | $particle = new LavaDripParticle(); 32 | foreach($path as $pos){ 33 | $world->addParticle($pos->add(0.5, 0.5, 0.5), $particle); 34 | } 35 | 36 | $particle = new DustParticle(new Colour(0, 0, 255)); 37 | $world->addParticle($this->pathfinder->getAlgorithm()->getStartPos()->add(0.5, 0.5, 0.5), $particle); 38 | 39 | $particle = new DustParticle(new Colour(0, 255, 0)); 40 | $world->addParticle($this->pathfinder->getAlgorithm()->getTargetPos()->add(0.5, 0.5, 0.5), $particle); 41 | } 42 | } 43 | --------------------------------------------------------------------------------