aboutsummaryrefslogtreecommitdiffstats
path: root/src/astar.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/astar.cpp')
-rw-r--r--src/astar.cpp28
1 files changed, 17 insertions, 11 deletions
diff --git a/src/astar.cpp b/src/astar.cpp
index 5374bea..9739e72 100644
--- a/src/astar.cpp
+++ b/src/astar.cpp
@@ -23,8 +23,6 @@ AStar::AStar(int *curr_graph, const unsigned int& width, const unsigned int& hei
}
bool AStar::start_algorithm(int *curr_graph, const unsigned int& width, const unsigned int& height) {
- Node n;
-
graph_width = width;
graph_height = height;
graph = curr_graph;
@@ -37,8 +35,9 @@ bool AStar::start_algorithm(int *curr_graph, const unsigned int& width, const un
start_node.x = i % graph_width;
start_node.y = i / graph_width;
start_node.g_score = 0;
- start_node.previous_node = NULL;
calc_heuristic(start_node);
+ start_node.x_prev = -1;
+ start_node.y_prev = -1;
calc_f(start_node);
} else if(graph[i] == 2) {
end_node.x = i % graph_width;
@@ -69,22 +68,21 @@ bool AStar::start_algorithm() {
if(!open_set.check_item(n) && !check_item_vec(n)) {
n.g_score = cost;
calc_f(n);
- n.previous_node = &current;
+ n.x_prev = current.x;
+ n.y_prev = current.y;
open_set.push(n);
}
}
}
}
- /*std::cout << open_set.get_first().x << ", " << open_set.get_first().y << std::endl;
- * std::cout << open_set.get_first().previous_node->x << ", " << open_set.get_first().previous_node->y << std::endl;
- * std::cout << open_set.get_first().previous_node->previous_node->x << ", " << open_set.get_first().previous_node->previous_node->y << std::endl;*/
+ recreate_path(open_set.get_first());
return true;
}
void AStar::recreate_path(Node n) {
- while(n.previous_node != NULL) {
- graph[n.x + n.y * graph_width] = 9;
- n = *n.previous_node;
+ while(n.x != -1 && n.y != -1) {
+ graph[n.x + n.y * graph_width] = 8;
+ n = find_node(n.x_prev, n.y_prev);
}
}
@@ -105,7 +103,6 @@ Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) {
n.y = n_in.y;
}
- n.previous_node = &n_in;
calc_heuristic(n);
return n;
@@ -140,3 +137,12 @@ void AStar::remove_from_vec(const Node& n) {
tmp_set.push_back(closed_set[i]);
closed_set = tmp_set;
}
+
+Node AStar::find_node(const unsigned int& x, const unsigned int& y) {
+ Node n;
+
+ for(unsigned int i = 0; i < closed_set.size(); ++i)
+ if(closed_set[i].x == x && closed_set[i].y == y)
+ n = closed_set[i];
+ return n;
+}