aboutsummaryrefslogtreecommitdiff
path: root/path.c
diff options
context:
space:
mode:
Diffstat (limited to 'path.c')
-rw-r--r--path.c38
1 files changed, 34 insertions, 4 deletions
diff --git a/path.c b/path.c
index 589edd6..2b07c55 100644
--- a/path.c
+++ b/path.c
@@ -118,8 +118,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star
PositionPQ *frontier = ppq_new(start, 0);
visited_clear(visited, width, height);
- size_t cost_so_far[height][width];
- memset(cost_so_far, 0xFF, sizeof(size_t) * height * width);
+ size_t **cost_so_far = cost_so_far_new(width, height);
cost_so_far[start.y][start.x] = 0;
while (frontier != NULL) {
@@ -128,6 +127,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star
if (cur.x == end.x && cur.y == end.y) {
ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return path; /* Found path */
}
@@ -151,12 +151,16 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star
if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) {
clear();
path_free(path, height);
+ ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return NULL;
}
}
}
path_free(path, height);
+ ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return NULL;
}
@@ -185,8 +189,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start,
PositionPQ *frontier = ppq_new(start, 0);
visited_clear(visited, width, height);
- size_t cost_so_far[height][width];
- memset(cost_so_far, 0xFF, sizeof(size_t) * height * width);
+ size_t **cost_so_far = cost_so_far_new(width, height);
cost_so_far[start.y][start.x] = 0;
while (frontier != NULL) {
@@ -195,6 +198,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start,
if (cur.x == end.x && cur.y == end.y) {
ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return path; /* Found path */
}
@@ -219,12 +223,16 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start,
if (anim(map, width, height, start, end, &cur, visited, frontier) == -1) {
clear();
path_free(path, height);
+ ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return NULL;
}
}
}
path_free(path, height);
+ ppq_free(frontier);
+ cost_so_far_free(cost_so_far, height);
return NULL;
}
@@ -313,3 +321,25 @@ void visited_free(char **visited, size_t height) {
free(visited);
return;
}
+
+size_t **cost_so_far_new(size_t width, size_t height) {
+ size_t **cost_so_far = malloc(sizeof(size_t*) * height);
+ if (cost_so_far == NULL) return NULL;
+
+ for (size_t row = 0; row < height; row++) {
+ cost_so_far[row] = malloc(sizeof(size_t) * width);
+ if (cost_so_far[row] == NULL) return NULL;
+ memset(cost_so_far[row], 0xFF, width*sizeof(size_t));
+ }
+
+ return cost_so_far;
+}
+
+void cost_so_far_free(size_t **cost_so_far, size_t height) {
+ if (cost_so_far == NULL) return;
+ for (size_t row = 0; row < height; row++) {
+ free(cost_so_far[row]);
+ }
+ free(cost_so_far);
+ return;
+}