From db5320aa188d773c8edc32eee844127457ef280c Mon Sep 17 00:00:00 2001 From: Kirill Petrashin Date: Tue, 14 Apr 2026 21:41:33 +0300 Subject: Improve the Path type --- path.c | 53 ++++++++++++++++++++++++----------------------------- 1 file changed, 24 insertions(+), 29 deletions(-) (limited to 'path.c') diff --git a/path.c b/path.c index 442ca14..fbd599c 100644 --- a/path.c +++ b/path.c @@ -71,14 +71,8 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P default: error("Tried to call breadth_first_search_path with wrong direction amount\n"); } - Path path = malloc(sizeof(PathNode)*height); - if (path == NULL) return NULL; - - for (size_t row = 0; row < height; row++) { - path[row] = malloc(sizeof(PathNode) * width); - if (path[row] == NULL) return NULL; - memset(path[row], 0, width * sizeof(PathNode)); - } + Path path = path_new(width, height); + if (path == NULL) error("Path was NULL\n"); PositionPQ *frontier = ppq_new(start, 0); @@ -104,7 +98,7 @@ Path breadth_first_search_path(int dirs, Map map, size_t width, size_t height, P if (map[na[i].y][na[i].x] == WALL) continue; if (ppq_insert(&frontier, na[i], 0) != PPQ_INSERT_ALREADY) { - path[na[i].y][na[i].x].parent = cur; + path[na[i].y][na[i].x] = cur; } } @@ -137,14 +131,8 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star default: error("Tried to call dijkstra_path with wrong direction amount\n"); } - Path path = malloc(sizeof(PathNode)*height); - if (path == NULL) return NULL; - - for (size_t row = 0; row < height; row++) { - path[row] = malloc(sizeof(PathNode) * width); - if (path[row] == NULL) return NULL; - memset(path[row], 0, width * sizeof(PathNode)); - } + Path path = path_new(width, height); + if (path == NULL) error("Path was NULL\n"); PositionPQ *frontier = ppq_new(start, 0); @@ -173,7 +161,7 @@ Path dijkstra_path(int dirs, Map map, size_t width, size_t height, Position star size_t new_cost = cost_so_far[cur.y][cur.x] + costs[i]; if (new_cost < cost_so_far[na[i].y][na[i].x]) { cost_so_far[na[i].y][na[i].x] = new_cost; - path[na[i].y][na[i].x].parent = cur; + path[na[i].y][na[i].x] = cur; ppq_insert(&frontier, na[i], new_cost); } } @@ -210,14 +198,8 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, default: error("Tried to call astar_path with wrong direction amount\n"); } - Path path = malloc(sizeof(PathNode)*height); - if (path == NULL) return NULL; - - for (size_t row = 0; row < height; row++) { - path[row] = malloc(sizeof(PathNode) * width); - if (path[row] == NULL) return NULL; - memset(path[row], 0, width * sizeof(PathNode)); - } + Path path = path_new(width, height); + if (path == NULL) error("Path was NULL\n"); PositionPQ *frontier = ppq_new(start, 0); @@ -246,7 +228,7 @@ Path astar_path(int dirs, Map map, size_t width, size_t height, Position start, size_t new_cost = cost_so_far[cur.y][cur.x] + costs[i]; if (new_cost < cost_so_far[na[i].y][na[i].x]) { cost_so_far[na[i].y][na[i].x] = new_cost; - path[na[i].y][na[i].x].parent = cur; + path[na[i].y][na[i].x] = cur; size_t priority = new_cost + heuristic(na[i], end); ppq_insert(&frontier, na[i], priority); } @@ -308,17 +290,30 @@ size_t path_length(Path path, Position start, Position goal) { if (path != NULL) { Position cur = goal; while (cur.x != start.x || cur.y != start.y) { - if (cur.x - path[cur.y][cur.x].parent.x == 0 || cur.y - path[cur.y][cur.x].parent.y == 0) { + if (cur.x - path[cur.y][cur.x].x == 0 || cur.y - path[cur.y][cur.x].y == 0) { length += COST_ORTHOGONAL; } else { length += COST_DIAGONAL; } - cur = path[cur.y][cur.x].parent; + cur = path[cur.y][cur.x]; } } return length; } +Path path_new(size_t width, size_t height) { + Path path = malloc(sizeof(Position*)*height); + if (path == NULL) return NULL; + + for (size_t row = 0; row < height; row++) { + path[row] = malloc(sizeof(Position) * width); + if (path[row] == NULL) return NULL; + memset(path[row], 0, width * sizeof(Position)); + } + + return path; +} + void path_free(Path path, size_t height) { if (path == NULL) return; for (size_t i = 0; i < height; i++) { -- cgit v1.2.3