aboutsummaryrefslogtreecommitdiff
path: root/map.c
diff options
context:
space:
mode:
Diffstat (limited to 'map.c')
-rw-r--r--map.c80
1 files changed, 58 insertions, 22 deletions
diff --git a/map.c b/map.c
index 143329c..22b88ec 100644
--- a/map.c
+++ b/map.c
@@ -31,30 +31,42 @@ Map empty_map(size_t width, size_t height) {
/* When we allow maps to have costs, these two neighbours functions will have to use the instead of COST_* defines */
/* TODO: maybe merge them, add `dirs` parameter, like we do in path.c? */
unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4], Position pos, size_t width, size_t height, \
- char **visited) {
+ char **visited, size_t **costs) {
size_t cur = 0;
if (pos.x > 0 && !visited[pos.y][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.x + 1 < width && !visited[pos.y][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.y > 0 && !visited[pos.y - 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.y + 1 < height && !visited[pos.y + 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
@@ -62,55 +74,79 @@ unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4],
}
unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8], Position pos, size_t width, size_t height, \
- char **visited) {
+ char **visited, size_t **costs) {
size_t cur = 0;
if (pos.x > 0 && !visited[pos.y][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.x + 1 < width && !visited[pos.y][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.y > 0 && !visited[pos.y - 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.y + 1 < height && !visited[pos.y + 1][pos.x]) {
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = COST_ORTHOGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x];
+ else cost_array[cur] = COST_ORTHOGONAL;
+ }
cur += 1;
}
if (pos.x > 0 && pos.y > 0 && !visited[pos.y - 1][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x - 1];
+ else cost_array[cur] = COST_DIAGONAL;
+ }
cur += 1;
}
if (pos.x + 1 < width && pos.y > 0 && !visited[pos.y - 1][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y - 1;
- if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x + 1];
+ else cost_array[cur] = COST_DIAGONAL;
+ }
cur += 1;
}
if (pos.x + 1 < width && pos.y + 1 < height && !visited[pos.y + 1][pos.x + 1]) {
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x + 1];
+ else cost_array[cur] = COST_DIAGONAL;
+ }
cur += 1;
}
if (pos.x > 0 && pos.y + 1 < height && !visited[pos.y + 1][pos.x - 1]) {
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y + 1;
- if (cost_array != NULL) cost_array[cur] = COST_DIAGONAL;
+ if (cost_array != NULL) {
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x - 1];
+ else cost_array[cur] = COST_DIAGONAL;
+ }
cur += 1;
}
@@ -149,7 +185,7 @@ Map rbt_maze_map(size_t width, size_t height, unsigned int seed) {
srand(seed);
do {
- int nc = neighbours_4dir(na, NULL, ps_peek(ps), width, height, visited);
+ int nc = neighbours_4dir(na, NULL, ps_peek(ps), width, height, visited, NULL);
if (nc > 0) {
Position next = na[rand() % nc];
Position prev = ps_peek(ps);
@@ -477,7 +513,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) {
set_message("Will pathfind");
visited = visited_new(*width, *height);
- path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
} else {
set_message("Won't pathfind");
visited_free(visited, *height); visited = NULL;
@@ -505,7 +541,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; }
else { set_message("A*"); path_func = &astar_path; };
path_free(path, *height);
- if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
break;
case 's':
@@ -542,7 +578,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (row < *height && col < *width && (*map)[row][col] != WALL && (col != goal->x || row != goal->y)) {
start->x = col;
start->y = row;
- if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
@@ -553,7 +589,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (row < *height && col < *width && (*map)[row][col] != WALL && (col != start->x || row != start->y)) {
goal->x = col;
goal->y = row;
- if (should_pathfind) path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ if (should_pathfind) path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}
}
@@ -596,7 +632,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) {
visited = visited_new(*width, *height);
- path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else if (col == *width) { /* Resize horizontally */
@@ -642,7 +678,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
if (should_pathfind) {
visited = visited_new(*width, *height);
- path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
} else { /* Start drawing */
@@ -665,7 +701,7 @@ void map_editor(Map *map, size_t *width, size_t *height, Position *start, Positi
(*map)[row][col] = cur;
path_free(path, *height);
if (should_pathfind) {
- path = path_func(dirs, *map, *width, *height, *start, *goal, visited, 0);
+ path = path_func(dirs, *map, NULL, *width, *height, *start, *goal, visited, 0);
}
draw_map(*map, *width, *height, *start, *goal, NULL, path, visited, NULL);
}