4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT16_MAX */
6 #include <stdlib.h> /* free() */
7 #include "../common/try_malloc.h" /* try_malloc() */
8 #include "field_of_view.h" /* build_fov_map() */
9 #include "map_object_actions.h" /* get_moa_id_by_name() */
10 #include "map_objects.h" /* struct MapObj */
11 #include "world.h" /* global world */
19 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
20 * "score_map" cell at "pos_i" (array index), as found in the directions
21 * north-east, east, south-east etc. (clockwise order). Use "max_score" for
22 * illegal neighborhoods (i.e. if direction would lead beyond the map's border).
24 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
25 uint16_t max_score, uint16_t * neighbors);
27 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
28 * each cell's score against the score of its immediate neighbors in N_DIRS
29 * directions. If it's neighbors are low enough that the result would be lower
30 * than the current value, re-set it to 1 point higher than its lowest-scored
31 * neighbor. Repeat this whole process until all cells have settled on their
32 * final score. Ignore cells whose position in "score_map" fits cells of
33 * unreachable terrain in world.map.cells or whose score is greater than
34 * "max_score". Expect "max_score" to be the maximum score for cells, marking
35 * them as unreachable.
37 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
39 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
40 * shortest path visible to "mo_origin". If no enemy is around, return 0.
42 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin);
46 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
47 uint16_t max_score, uint16_t * neighbors)
49 uint32_t map_size = world.map.size.y * world.map.size.x;
51 for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
52 uint8_t open_north = pos_i >= world.map.size.x;
53 uint8_t open_east = pos_i + 1 % world.map.size.x;
54 uint8_t open_south = pos_i + world.map.size.x < map_size;
55 uint8_t open_west = pos_i % world.map.size.x;
56 uint8_t is_indented = (pos_i / world.map.size.x) % 2;
57 uint8_t open_diag_west = is_indented || open_west;
58 uint8_t open_diag_east = !is_indented || open_east;
59 if (open_north && open_diag_east)
61 neighbors[0] = score_map[pos_i - world.map.size.x + is_indented];
65 neighbors[1] = score_map[pos_i + 1];
67 if (open_south && open_diag_east)
69 neighbors[2] = score_map[pos_i + world.map.size.x + is_indented];
71 if (open_south && open_diag_west)
73 neighbors[3] = score_map[pos_i + world.map.size.x - !is_indented];
77 neighbors[4] = score_map[pos_i - 1];
79 if (open_north && open_diag_west)
81 neighbors[5] = score_map[pos_i - world.map.size.x - !is_indented];
87 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
89 uint32_t map_size = world.map.size.y * world.map.size.x;
90 uint16_t pos, i_scans, neighbors[N_DIRS], min_neighbor;
91 uint8_t scores_still_changing = 1;
93 for (i_scans = 0; scores_still_changing; i_scans++)
95 scores_still_changing = 0;
96 for (pos = 0; pos < map_size; pos++)
98 if ('.' == world.map.cells[pos] && score_map[pos] <= max_score)
100 get_neighbor_scores(score_map, pos, max_score, neighbors);
101 min_neighbor = max_score;
102 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
104 if (min_neighbor > neighbors[i_dirs])
106 min_neighbor = neighbors[i_dirs];
109 if (score_map[pos] > min_neighbor + 1)
111 score_map[pos] = min_neighbor + 1;
112 scores_still_changing = 1;
121 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin)
123 char * f_name = "get_dir_to_nearest_enemy()";
125 /* Calculate for each cell the distance to the visibly nearest map actor not
126 * "mo_origin", with movement only possible in the directions of "dir".
127 * (Actors' own cells start with a distance of 0 towards themselves.)
129 uint32_t map_size = world.map.size.y * world.map.size.x;
130 uint16_t max_score = UINT16_MAX - 1;
131 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), f_name);
132 uint8_t * fov_map = world.enemy_fov ? build_fov_map(mo_origin) : NULL;
134 for (i = 0; i < map_size; i++)
138 score_map[i] = fov_map[i] & VISIBLE ? max_score : UINT16_MAX;
142 score_map[i] = max_score;
146 struct MapObj * mo = world.map_objs;
147 for (; mo != NULL; mo = mo->next)
149 if (!mo->lifepoints || mo == mo_origin)
153 score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0;
155 dijkstra_map(score_map, max_score);
157 /* Return direction of "mo_origin"'s lowest-scored neighbor cell. */
158 uint16_t neighbors[N_DIRS];
159 uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x;
160 get_neighbor_scores(score_map, pos_i, max_score, neighbors);
162 char dir_to_nearest_enemy = 0;
163 uint16_t min_neighbor = max_score;
164 char * dirs = "edcxsw"; /* get_neighbor_scores()'s clockwise dir order.*/
165 for (i = 0; i < N_DIRS; i++)
167 if (min_neighbor > neighbors[i])
169 min_neighbor = neighbors[i];
170 dir_to_nearest_enemy = dirs[i];
173 return dir_to_nearest_enemy;
178 extern void ai(struct MapObj * mo)
180 mo->command = get_moa_id_by_name("wait");
181 char sel = get_dir_to_nearest_enemy(mo);
184 mo->command = get_moa_id_by_name("move");