From 03f9f9cc2e632066c2f5774bee5f26abde168e37 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 15 Feb 2026 18:23:09 +0200 Subject: [PATCH] calculating heuristics in a function ending pathpoint generation early for empty paths --- pathfinder.cpp | 9 +++++++++ pathfinder.h | 1 + vehicle.cpp | 8 ++++---- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pathfinder.cpp b/pathfinder.cpp index 0d52d0d..c0b3924 100644 --- a/pathfinder.cpp +++ b/pathfinder.cpp @@ -16,6 +16,15 @@ namespace battleship{ return pathfinder; } + vector Pathfinder::calcHeuristics(vector &cells, int dest){ + vector heuristics; + + for(Map::Cell &cell : cells) + heuristics.push_back(145 * (cells[dest].pos.getDistanceFrom(cell.pos))); + + return heuristics; + } + vector Pathfinder::findPath(vector &cells, vector &heuristics, int source, int dest, Vehicle *vehicle){ const int size = cells.size(); u32 *distances = new u32[size]; diff --git a/pathfinder.h b/pathfinder.h index 13f38af..7a63c33 100644 --- a/pathfinder.h +++ b/pathfinder.h @@ -13,6 +13,7 @@ namespace battleship{ class Pathfinder{ public: static Pathfinder* getSingleton(); + std::vector calcHeuristics(std::vector&, int); std::vector findPath(std::vector&, std::vector&, int, int, Vehicle* = nullptr); inline vb01::u32 getImpassibleNodeVal(){return impassibleNodeVal;} inline void setImpassibleNodeVal(vb01::u32 val){this->impassibleNodeVal = val;} diff --git a/vehicle.cpp b/vehicle.cpp index 9e8f074..d61f979 100644 --- a/vehicle.cpp +++ b/vehicle.cpp @@ -343,12 +343,12 @@ namespace battleship{ } } - vector heuristics; + Pathfinder *pf = Pathfinder::getSingleton(); + vector heuristics = pf->calcHeuristics(cells, dest); + vector path = pf->findPath(cells, heuristics, source, dest, this); - for(Map::Cell &cell : cells) - heuristics.push_back(145 * (cells[dest].pos.getDistanceFrom(cell.pos))); + if(path.empty()) return; - vector path = Pathfinder::getSingleton()->findPath(cells, heuristics, source, dest, this); path.erase(path.begin()); bool pathTruncated = false;