4 #include <stddef.h> /* NULL */
5 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t, UINT16_MAX */
6 #include <stdlib.h> /* free() */
7 #include "../common/try_malloc.h" /* try_malloc() */
8 #include "hardcoded_strings.h" /* s */
9 #include "thing_actions.h" /* get_thing_action_id_by_name() */
10 #include "things.h" /* Thing, ThingType */
11 #include "world.h" /* 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 any neighbor's score is at least two points lower than the
30 * current cell's score, 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 score is greater than "max_score". Expect
33 * "max_score" to be the maximum score for cells, marking them as unreachable.
35 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
37 /* get_dir_to_nearest_thing() helper: Prepare "score_map" for dijkstra_map(). */
38 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
39 struct Thing * t_eye);
41 /* Set (if possible) as "t_eye"'s command a move to the path to the path-wise
42 * nearest thing that is in "t_eye"'s field of view and not "t_eye" and fits
43 * criteria set by "filter". On success, return 1, else 0. Values for "filter":
44 * "e": thing searched for animate, but not of "t_eye"'s thing type; build
45 * path as avoiding things of "t_eye"'s type
46 * "c": thing searched for is consumable.
48 static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter);
50 /* Return 1 if any thing not "t_eye" is in its FOV and fulfills some criterion
51 * defined by "filter", else 0. Values for "filter":
52 * "e": thing searched for is animate, but not of "t_eye"'s thing type
53 * "c": thing searched for is consumable
55 static uint8_t seeing_thing(struct Thing * t_eye, char filter);
57 /* Return slot ID of strongest consumable in "t_owner"'s inventory, else -1. */
58 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner);
60 /* Return 1 if "t_standing" is standing on a consumable, else 0. */
61 static uint8_t standing_on_consumable(struct Thing * t_standing);
65 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
66 uint16_t max_score, uint16_t * neighbors)
68 uint32_t map_size = world.map.length * world.map.length;
70 for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
71 uint8_t open_north = pos_i >= world.map.length;
72 uint8_t open_east = pos_i + 1 % world.map.length;
73 uint8_t open_south = pos_i + world.map.length < map_size;
74 uint8_t open_west = pos_i % world.map.length;
75 uint8_t is_indented = (pos_i / world.map.length) % 2;
76 uint8_t open_diag_west = is_indented || open_west;
77 uint8_t open_diag_east = !is_indented || open_east;
78 if (open_north && open_diag_east)
80 neighbors[0] = score_map[pos_i - world.map.length + is_indented];
84 neighbors[1] = score_map[pos_i + 1];
86 if (open_south && open_diag_east)
88 neighbors[2] = score_map[pos_i + world.map.length + is_indented];
90 if (open_south && open_diag_west)
92 neighbors[3] = score_map[pos_i + world.map.length - !is_indented];
96 neighbors[4] = score_map[pos_i - 1];
98 if (open_north && open_diag_west)
100 neighbors[5] = score_map[pos_i - world.map.length - !is_indented];
106 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
108 uint32_t map_size = world.map.length * world.map.length;
110 uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
111 uint8_t scores_still_changing = 1;
113 for (i_scans = 0; scores_still_changing; i_scans++)
115 scores_still_changing = 0;
116 for (pos = 0; pos < map_size; pos++)
118 if (score_map[pos] <= max_score)
120 get_neighbor_scores(score_map, pos, max_score, neighbors);
121 min_neighbor = max_score;
122 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
124 if (min_neighbor > neighbors[i_dirs])
126 min_neighbor = neighbors[i_dirs];
129 if (score_map[pos] > min_neighbor + 1)
131 score_map[pos] = min_neighbor + 1;
132 scores_still_changing = 1;
141 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
142 struct Thing * t_eye)
145 for (i = 0; i < map_size; i++)
147 score_map[i] = UINT16_MAX;
148 if ('.' == t_eye->mem_map[i])
150 score_map[i] = UINT16_MAX-1;
153 struct Thing * t = world.things;
154 for (; t != NULL; t = t->next)
156 if (t==t_eye || 'H'==t_eye->fov_map[t->pos.y*world.map.length+t->pos.x])
166 else if (t->lifepoints && t->type == t_eye->type)
168 score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
172 else if ('c' == filter)
174 struct ThingType * tt = get_thing_type(t->type);
180 score_map[t->pos.y * world.map.length + t->pos.x] = 0;
186 static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter)
188 char dir_to_nearest_enemy = 0;
189 if (seeing_thing(t_eye, filter))
191 uint32_t map_size = world.map.length * world.map.length;
192 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
193 init_score_map(filter, score_map, map_size, t_eye);
194 dijkstra_map(score_map, UINT16_MAX-1);
195 uint16_t neighbors[N_DIRS];
196 uint16_t pos_i = (t_eye->pos.y * world.map.length) + t_eye->pos.x;
197 get_neighbor_scores(score_map, pos_i, UINT16_MAX-1, neighbors);
199 uint16_t min_neighbor = UINT16_MAX-1;
200 char * dirs = "edcxsw";/* get_neighbor_scores()'s clockwise dir order.*/
202 for (i = 0; i < N_DIRS; i++)
204 if (min_neighbor > neighbors[i])
206 min_neighbor = neighbors[i];
207 dir_to_nearest_enemy = dirs[i];
211 if (dir_to_nearest_enemy)
213 t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
214 t_eye->arg = dir_to_nearest_enemy;
222 static uint8_t seeing_thing(struct Thing * t_eye, char filter)
226 struct Thing * t = world.things;
227 for (; t != NULL; t = t->next)
230 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
232 if ('e' == filter && t->lifepoints && t->type != t_eye->type)
236 else if ('c' == filter)
238 struct ThingType * tt = get_thing_type(t->type);
252 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
254 uint8_t compare_consumability = 0;
255 int16_t selection = -1;
256 struct Thing * t = t_owner->owns;;
258 for (i = 0; t != NULL; t = t->next, i++)
260 struct ThingType * tt = get_thing_type(t->type);
261 if (tt->consumable > compare_consumability)
263 compare_consumability = tt->consumable;
272 static uint8_t standing_on_consumable(struct Thing * t_standing)
274 struct Thing * t = world.things;
275 for (; t != NULL; t = t->next)
278 && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x)
280 struct ThingType * tt = get_thing_type(t->type);
292 extern void ai(struct Thing * t)
294 t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
295 if (!get_dir_to_nearest_thing(t, 'e'))
297 int16_t sel = get_inventory_slot_to_consume(t);
300 t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
301 t->arg = (uint8_t) sel;
303 else if (standing_on_consumable(t))
305 t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
309 get_dir_to_nearest_thing(t, 'c');