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" /* HIDDEN */
9 #include "hardcoded_strings.h" /* s */
10 #include "thing_actions.h" /* get_thing_action_id_by_name() */
11 #include "things.h" /* struct Thing */
12 #include "world.h" /* global world */
20 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
21 * "score_map" cell at "pos_i" (array index), as found in the directions
22 * north-east, east, south-east etc. (clockwise order). Use "max_score" for
23 * illegal neighborhoods (i.e. if direction would lead beyond the map's border).
25 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
26 uint16_t max_score, uint16_t * neighbors);
28 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
29 * each cell's score against the score of its immediate neighbors in N_DIRS
30 * directions. If any neighbor's score is at least two points lower than the
31 * current cell's score, re-set it to 1 point higher than its lowest-scored
32 * neighbor. Repeat this whole process until all cells have settled on their
33 * final score. Ignore cells whose score is greater than "max_score". Expect
34 * "max_score" to be the maximum score for cells, marking them as unreachable.
36 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
38 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
39 * shortest path visible to "t_origin". If no enemy is around, return 0.
41 static char get_dir_to_nearest_enemy(struct Thing * thing_origin);
45 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
46 uint16_t max_score, uint16_t * neighbors)
48 uint32_t map_size = world.map.length * world.map.length;
50 for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
51 uint8_t open_north = pos_i >= world.map.length;
52 uint8_t open_east = pos_i + 1 % world.map.length;
53 uint8_t open_south = pos_i + world.map.length < map_size;
54 uint8_t open_west = pos_i % world.map.length;
55 uint8_t is_indented = (pos_i / world.map.length) % 2;
56 uint8_t open_diag_west = is_indented || open_west;
57 uint8_t open_diag_east = !is_indented || open_east;
58 if (open_north && open_diag_east)
60 neighbors[0] = score_map[pos_i - world.map.length + is_indented];
64 neighbors[1] = score_map[pos_i + 1];
66 if (open_south && open_diag_east)
68 neighbors[2] = score_map[pos_i + world.map.length + is_indented];
70 if (open_south && open_diag_west)
72 neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
76 neighbors[4] = score_map[pos_i - 1];
78 if (open_north && open_diag_west)
80 neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
86 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
88 uint32_t map_size = world.map.length * world.map.length;
90 uint16_t 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 (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 Thing * t_origin)
123 /* Calculate for each cell distance to visibly nearest enemy, with movement
124 * possible in the directions or "dir". (Actor's own cells start with 0
125 * distance towards themselves. Cells of actors of own type are invisible.)
127 uint32_t map_size = world.map.length * world.map.length;
128 uint16_t max_score = UINT16_MAX - 1;
129 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
131 for (i = 0; i < map_size; i++)
133 score_map[i] = UINT16_MAX;
134 if ('.' == t_origin->mem_map[i])
136 score_map[i] = max_score;
139 struct Thing * t = world.things;
140 for (; t != NULL; t = t->next)
142 if ( !t->lifepoints || t == t_origin
143 || HIDDEN == t_origin->fov_map[t->pos.y*world.map.length+t->pos.x])
147 if (t->lifepoints && t->type == t_origin->type)
149 score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
152 score_map[t->pos.y * world.map.length + t->pos.x] = 0;
154 dijkstra_map(score_map, max_score);
156 /* Return direction of "t_origin"'s lowest-scored neighbor cell. */
157 uint16_t neighbors[N_DIRS];
158 uint16_t pos_i = (t_origin->pos.y * world.map.length) + t_origin->pos.x;
159 get_neighbor_scores(score_map, pos_i, max_score, neighbors);
161 char dir_to_nearest_enemy = 0;
162 uint16_t min_neighbor = max_score;
163 char * dirs = "edcxsw"; /* get_neighbor_scores()'s clockwise dir order.*/
164 for (i = 0; i < N_DIRS; i++)
166 if (min_neighbor > neighbors[i])
168 min_neighbor = neighbors[i];
169 dir_to_nearest_enemy = dirs[i];
172 return dir_to_nearest_enemy;
177 extern void ai(struct Thing * t)
179 t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
180 char sel = t->fov_map ? get_dir_to_nearest_enemy(t) : 0;/* t->fov_map may */
181 if (0 != sel) /* be absent due */
182 { /* to god command.*/
183 t->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);