aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKirill Petrashin <kirill8201@yandex.ru>2026-04-16 22:15:51 +0300
committerKirill Petrashin <kirill8201@yandex.ru>2026-04-16 22:15:51 +0300
commitf2a99859f0c66ba17765c036a173e7f29927327a (patch)
tree88f05aaf4944f0fa5a575f9bc2ea852fa1d71e1c
parentc6db3a75f756d790ede966c34adc3dbc793803d1 (diff)
downloadastar-f2a99859f0c66ba17765c036a173e7f29927327a.tar.xz
Fix costs
-rw-r--r--config.h2
-rw-r--r--main.c32
-rw-r--r--map.c24
-rw-r--r--maps/10x1011
-rw-r--r--maps/cost_wall20
-rw-r--r--path.c1
6 files changed, 54 insertions, 36 deletions
diff --git a/config.h b/config.h
index 391be5f..85b63fe 100644
--- a/config.h
+++ b/config.h
@@ -18,7 +18,7 @@
#define CURSOR_CHAR_1 '<'
#define CURSOR_CHAR_2 '>'
-#define MESSAGE_MAX_SIZE 128
+#define MESSAGE_MAX_SIZE 256
#define FILENAME_BUF_SIZE 128
#define FILENAME_PROMPT "Filename:"
diff --git a/main.c b/main.c
index 93ce49f..b216c3b 100644
--- a/main.c
+++ b/main.c
@@ -141,7 +141,7 @@ int main(int argc, char **argv) {
char **visited = visited_new(width, height);
Path path = NULL;
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
if (bmp_only) {
map_to_bmp(map, width, height, start_pos, end_pos, path, visited, bmp_filename);
@@ -191,7 +191,7 @@ int main(int argc, char **argv) {
else anim = 1;
path_free(path, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
clear_message();
break;
@@ -199,7 +199,7 @@ int main(int argc, char **argv) {
if (path_func == astar_path) { set_message("Dijkstra"); path_func = &dijkstra_path; }
else { set_message("A*"); path_func = &astar_path; };
path_free(path, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
case 'y':
@@ -216,6 +216,8 @@ int main(int argc, char **argv) {
map_free(map, height);
visited_free(visited, height);
path_free(path, height);
+ cost_free(cell_costs, height);
+ cell_costs = NULL;
height = mheight * 2 - 1;
width = mwidth * 2 - 1;
@@ -224,7 +226,7 @@ int main(int argc, char **argv) {
map = rbt_maze_map(mwidth, mheight, rand());
visited = visited_new(width, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
@@ -256,11 +258,13 @@ int main(int argc, char **argv) {
map_free(map, height);
path_free(path, height);
+ cost_free(cell_costs, height);
+ cell_costs = NULL;
visited = visited_new(width, height);
map = file_plaintext_map(filename, &width, &height, &start_pos, &end_pos);
visited = visited_new(width, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
set_message("Loaded map from %s", filename); print_message(height);
@@ -272,9 +276,11 @@ int main(int argc, char **argv) {
case 'n':
if (is_maze) {
map_free(map, height);
- map = rbt_maze_map(mwidth, mheight, rand());
path_free(path, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ cost_free(cell_costs, height);
+ cell_costs = NULL;
+ map = rbt_maze_map(mwidth, mheight, rand());
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
}
break;
@@ -286,33 +292,33 @@ int main(int argc, char **argv) {
print_message(height);
char cost_filename[FILENAME_BUF_SIZE] = "\0";
mvgetnstr(height + map_offset_y + 1, map_offset_x - 2 + sizeof(FILENAME_PROMPT), cost_filename, FILENAME_BUF_SIZE - 1);
+ curs_set(0); /* Hide the cursor */
+ noecho(); /* Don't echo characters */
cell_costs = file_plaintext_costs(cost_filename, width, height);
path_free(path, height);
path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
- /* TODO: figure out how to print actual costs */
- cost_free(cell_costs, height);
- curs_set(0); /* Hide the cursor */
- noecho(); /* Don't echo characters */
break;
case 'e':
is_maze = 0;
visited_free(visited, height);
path_free(path, height);
+ cost_free(cell_costs, height);
+ cell_costs = NULL;
map_editor(&map, &width, &height, &start_pos, &end_pos, dirs);
set_message("You've left the map editor"); print_message(height);
wrefresh(curscr);
visited = visited_new(width, height);
- path = path_func(dirs, map, NULL, width, height, start_pos, end_pos, visited, anim);
+ path = path_func(dirs, map, cell_costs, width, height, start_pos, end_pos, visited, anim);
break;
case KEY_RESIZE: clear(); break;
- case 'q': map_free(map, height); path_free(path, height); endwin(); return 0;
+ case 'q': cost_free(cell_costs, height); visited_free(visited, height); map_free(map, height); path_free(path, height); endwin(); return 0;
}
}
diff --git a/map.c b/map.c
index a0d0f23..121970c 100644
--- a/map.c
+++ b/map.c
@@ -37,7 +37,7 @@ unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4],
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -46,7 +46,7 @@ unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4],
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -55,7 +55,7 @@ unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4],
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y - 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x];
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -64,7 +64,7 @@ unsigned int neighbours_4dir(Position neighbour_array[4], size_t cost_array[4],
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y + 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x];
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -80,7 +80,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x - 1] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -89,7 +89,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y][pos.x + 1] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -98,7 +98,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y - 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x];
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -107,7 +107,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x;
neighbour_array[cur].y = pos.y + 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x];
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x] * COST_ORTHOGONAL;
else cost_array[cur] = COST_ORTHOGONAL;
}
cur += 1;
@@ -117,7 +117,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y - 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x - 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x - 1] * COST_DIAGONAL;
else cost_array[cur] = COST_DIAGONAL;
}
cur += 1;
@@ -126,7 +126,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y - 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x + 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y - 1][pos.x + 1] * COST_DIAGONAL;
else cost_array[cur] = COST_DIAGONAL;
}
cur += 1;
@@ -135,7 +135,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x + 1;
neighbour_array[cur].y = pos.y + 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x + 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x + 1] * COST_DIAGONAL;
else cost_array[cur] = COST_DIAGONAL;
}
cur += 1;
@@ -144,7 +144,7 @@ unsigned int neighbours_8dir(Position neighbour_array[8], size_t cost_array[8],
neighbour_array[cur].x = pos.x - 1;
neighbour_array[cur].y = pos.y + 1;
if (cost_array != NULL) {
- if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x - 1];
+ if (costs != NULL) cost_array[cur] = costs[pos.y + 1][pos.x - 1] * COST_DIAGONAL;
else cost_array[cur] = COST_DIAGONAL;
}
cur += 1;
diff --git a/maps/10x10 b/maps/10x10
new file mode 100644
index 0000000..7c59765
--- /dev/null
+++ b/maps/10x10
@@ -0,0 +1,11 @@
+10x10
+.#@x
+..........
+..........
+..........
+..........
+..........
+@......x..
+..........
+..........
+..........
diff --git a/maps/cost_wall b/maps/cost_wall
index 2f3c089..e8d18da 100644
--- a/maps/cost_wall
+++ b/maps/cost_wall
@@ -1,11 +1,11 @@
10x10
-0 0 0 0 0 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
-0 0 0 0 9 0 0 0 0 0
+1 1 1 1 1 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
+1 1 1 1 9 1 1 1 1 1
diff --git a/path.c b/path.c
index 85f7804..6909adc 100644
--- a/path.c
+++ b/path.c
@@ -228,6 +228,7 @@ Path astar_path(int dirs, Map map, size_t **cell_costs, size_t width, size_t hei
/* The Russian constitution doesn't allow walking on walls */
if (map[na[i].y][na[i].x] == WALL) continue;
+ /* Dijkstra's works with cell_costs, why does this not? */
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;