diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 14:44:20 +0200 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-24 14:44:20 +0200 |
commit | afc66d3013b7d47c6c22d6a99809bc3e7d1ff0dc (patch) | |
tree | de50baa5d2f87cc8a416cbd321f7c7f430b03613 /PathfindingContext.cpp | |
parent | 1e0a52b03fe655d7073ef20703dbb2e7646f74d3 (diff) |
implement breadth-first search pathfinding
Diffstat (limited to 'PathfindingContext.cpp')
-rw-r--r-- | PathfindingContext.cpp | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/PathfindingContext.cpp b/PathfindingContext.cpp index cfebb44..bcc9f37 100644 --- a/PathfindingContext.cpp +++ b/PathfindingContext.cpp @@ -2,19 +2,36 @@ #include "XY.h" #include "Museum.h" #include "NullTileBehavior.h" +#include "ToggleMuseumPauseCommand.h" + +#include "DijkstraPathfinder.h" +#include "BreadthFirstPathfinder.h" using namespace std; -PathfindingContext::PathfindingContext(Museum & m) : museum(m) {} +PathfindingContext::PathfindingContext(Museum & m) : museum(m) { + // this->solvers.push_back(unique_ptr<Pathfinder>(new DijkstraPathfinder(m))); + this->solvers.push_back(unique_ptr<Pathfinder>(new BreadthFirstPathfinder(m))); +} + +void PathfindingContext::cycle_solver() { + this->solver_index = (this->solver_index + 1) % this->solvers.size(); +} + +Pathfinder & PathfindingContext::get_solver() { + return *this->solvers[this->solver_index]; +} void PathfindingContext::set_start(const XY & point) { - if (!this->valid_point(point)) return; + if (!this->valid_point(point)) return; this->start_point = point; + this->update(); } void PathfindingContext::set_end(const XY & point) { - if (!this->valid_point(point)) return; + if (!this->valid_point(point)) return; this->end_point = point; + this->update(); } bool PathfindingContext::valid_point(const XY & point) { @@ -31,3 +48,20 @@ bool PathfindingContext::valid_point(const XY & point) { return true; } +void PathfindingContext::update() { + bool valid = true; + if (!this->valid_point(this->start_point)) { + this->start_point = { -1, -1 }; + valid = false; + } + if (!this->valid_point(this->end_point)) { + this->end_point = { -1, -1 }; + valid = false; + } + if (!valid) return; + + ToggleMuseumPauseCommand(this->museum, true).execute(); + Pathfinder & solver = this->get_solver(); + solver.find_between(this->start_point, this->end_point); +} + |