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, ThingInMemory */
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 not "t_eye" and fits criteria set by "filter". On
43 * success, return 1, else 0. Values for "filter":
44 * "e": thing in FOV is animate, but not of "t_eye"'s thing type; build path as
45 * avoiding things of "t_eye"'s type
46 * "c": thing in memorized map 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 known and fulfills some criteria defined
51 * by "filter", else 0. Values for "filter":
52 * "e": thing in FOV is animate, but not of "t_eye"'s thing type
53 * "c": thing in memorized map 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;
155 struct Thing * t = world.things;
156 for (; t; t = t->next)
158 if ( t==t_eye || !t->lifepoints
159 || 'H' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
163 else if (t->lifepoints && t->type == t_eye->type)
165 score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
168 score_map[t->pos.y * world.map.length + t->pos.x] = 0;
171 else if ('c' == filter)
173 struct ThingInMemory * tm = t_eye->t_mem;
174 for (; tm; tm = tm->next)
176 if (' ' == t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
180 struct ThingType * tt = get_thing_type(tm->type);
185 score_map[tm->pos.y * world.map.length + tm->pos.x] = 0;
192 static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter)
194 char dir_to_nearest_enemy = 0;
195 if (seeing_thing(t_eye, filter))
197 uint32_t map_size = world.map.length * world.map.length;
198 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t),__func__);
199 init_score_map(filter, score_map, map_size, t_eye);
200 dijkstra_map(score_map, UINT16_MAX-1);
201 uint16_t neighbors[N_DIRS];
202 uint16_t pos_i = (t_eye->pos.y * world.map.length) + t_eye->pos.x;
203 get_neighbor_scores(score_map, pos_i, UINT16_MAX-1, neighbors);
205 uint16_t min_neighbor = UINT16_MAX-1;
206 char * dirs = "edcxsw";/* get_neighbor_scores()'s clockwise dir order.*/
208 for (i = 0; i < N_DIRS; i++)
210 if (min_neighbor > neighbors[i])
212 min_neighbor = neighbors[i];
213 dir_to_nearest_enemy = dirs[i];
217 if (dir_to_nearest_enemy)
219 t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
220 t_eye->arg = dir_to_nearest_enemy;
228 static uint8_t seeing_thing(struct Thing * t_eye, char filter)
230 if (t_eye->fov_map && 'e' == filter)
232 struct Thing * t = world.things;
233 for (; t; t = t->next)
236 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
238 if (t->lifepoints && t->type != t_eye->type)
245 else if (t_eye->mem_map && 'c' == filter)
247 struct ThingInMemory * tm = t_eye->t_mem;
248 for (; tm; tm = tm->next)
250 if (' ' != t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
252 struct ThingType * tt = get_thing_type(tm->type);
265 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
267 uint8_t compare_consumability = 0;
268 int16_t selection = -1;
269 struct Thing * t = t_owner->owns;;
271 for (i = 0; t != NULL; t = t->next, i++)
273 struct ThingType * tt = get_thing_type(t->type);
274 if (tt->consumable > compare_consumability)
276 compare_consumability = tt->consumable;
285 static uint8_t standing_on_consumable(struct Thing * t_standing)
287 struct Thing * t = world.things;
288 for (; t != NULL; t = t->next)
291 && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x)
293 struct ThingType * tt = get_thing_type(t->type);
305 extern void ai(struct Thing * t)
307 t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
308 if (!get_dir_to_nearest_thing(t, 'e'))
310 int16_t sel = get_inventory_slot_to_consume(t);
313 t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
314 t->arg = (uint8_t) sel;
316 else if (standing_on_consumable(t))
318 t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
322 get_dir_to_nearest_thing(t, 'c');