aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c4
-rw-r--r--map.c8
-rw-r--r--map.h3
-rw-r--r--path.c10
-rw-r--r--path.h2
-rw-r--r--priority_queue.c13
-rw-r--r--priority_queue.h1
7 files changed, 40 insertions, 1 deletions
diff --git a/main.c b/main.c
index 74f6784..74d2af8 100644
--- a/main.c
+++ b/main.c
@@ -128,11 +128,13 @@ int main(int argc, char **argv) {
case 'n':
if (is_maze) {
//FIXME: free it all before generating a new one
+ map_free(map, height);
map = rbt_maze_map(mwidth, mheight, (unsigned int) time(NULL));
+ path_free(path, height);
path = breadth_first_search_path_8dir(map, width, height, start_pos, end_pos, visited, anim);
}
break;
- case 'q': endwin(); return 0;
+ case 'q': map_free(map, height); path_free(path, height); endwin(); return 0;
}
}
diff --git a/map.c b/map.c
index 9f1d206..2966437 100644
--- a/map.c
+++ b/map.c
@@ -313,6 +313,14 @@ void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y,
doupdate();
}
+void map_free(Map map, size_t height) {
+ if (map == NULL) return;
+ for (size_t i = 0; i < height; i++) {
+ free(map[i]);
+ }
+ free(map);
+}
+
void print_map_out(Map map, size_t width, size_t height) {
for (size_t i = 0; i < height; i++) {
for (size_t j = 0; j < width; j++) {
diff --git a/map.h b/map.h
index ef76a7c..b0cb6b1 100644
--- a/map.h
+++ b/map.h
@@ -43,6 +43,9 @@ Map file_plaintext_map(char *filename, size_t *width, size_t *height, Position *
* path could be NULL to draw a map with no path. So can cursor, frontier and visited */
void draw_map(Map map, size_t width, size_t height, int offset_x, int offset_y, Position start, Position goal, Position *cursor, Path path, char visited[height][width], PositionPQ *frontier);
+/* Frees all the memory reserved for the map */
+void map_free(Map map, size_t height);
+
void print_map_out(Map map, size_t width, size_t height);
#endif /*MAP_H_ */
diff --git a/path.c b/path.c
index 37f2dbf..6f00ee8 100644
--- a/path.c
+++ b/path.c
@@ -38,6 +38,7 @@ Path breadth_first_search_path_4dir(Map map, size_t width, size_t height, Positi
visited[cur.y][cur.x] = 1;
if (cur.x == end.x && cur.y == end.y) {
+ ppq_free(frontier);
return path; /* Found path */
}
@@ -79,6 +80,7 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi
visited[cur.y][cur.x] = 1;
if (cur.x == end.x && cur.y == end.y) {
+ ppq_free(frontier);
return path; /* Found path */
}
@@ -136,3 +138,11 @@ size_t manhattan_distance(Position a, Position b) {
}
return d;
}
+
+void path_free(Path path, size_t height) {
+ if (path == NULL) return;
+ for (size_t i = 0; i < height; i++) {
+ free(path[i]);
+ }
+ free(path);
+}
diff --git a/path.h b/path.h
index d4efe6d..624516c 100644
--- a/path.h
+++ b/path.h
@@ -10,4 +10,6 @@ Path breadth_first_search_path_8dir(Map map, size_t width, size_t height, Positi
Path astar_path_4dir(Map map, size_t width, size_t height, Position start, Position end);
size_t manhattan_distance(Position a, Position b);
+void path_free(Path path, size_t height);
+
#endif /* ASTAR_H_ */
diff --git a/priority_queue.c b/priority_queue.c
index d2517a6..d4be6e2 100644
--- a/priority_queue.c
+++ b/priority_queue.c
@@ -24,6 +24,7 @@ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority) {
}
PositionPQ *n = ppq_new(pos, priority);
+
if (start->priority > priority) {
n->next = start;
start = n;
@@ -34,11 +35,13 @@ int ppq_insert(PositionPQ **ppq, Position pos, size_t priority) {
while(temp->next != NULL && temp->next->priority <= priority) {
if (temp->pos.x == pos.x && temp->pos.y == pos.y && temp->priority <= priority) {
+ free(n);
return 3; /* pos is already in ppq with a fine priority */
}
temp = temp->next;
}
if (temp->pos.x == pos.x && temp->pos.y == pos.y && temp->priority <= priority) {
+ free(n);
return 3; /* pos is already in ppq with a fine priority */
}
@@ -82,3 +85,13 @@ void ppq_print(PositionPQ *ppq) {
}
printf("%i - %li: %li, %li\n", i++, ppq->priority, ppq->pos.x, ppq->pos.y);
}
+
+void ppq_free(PositionPQ *ppq) {
+ if (ppq == NULL) return;
+ while(ppq->next != NULL) {
+ PositionPQ *t = ppq;
+ ppq = ppq->next;
+ free(t);
+ }
+ if (ppq != NULL) free(ppq);
+}
diff --git a/priority_queue.h b/priority_queue.h
index 2ac580e..63e9bbc 100644
--- a/priority_queue.h
+++ b/priority_queue.h
@@ -30,5 +30,6 @@ void ppq_reprioritize(PositionPQ *ppq, Position pos, size_t priority);
void ppq_print(PositionPQ *ppq);
+void ppq_free(PositionPQ *ppq);
#endif /* PRIORITY_QUEUE_H_ */