3 * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4 * or any later version. For details on its copyright, license, and warranties,
5 * see the file NOTICE in the root directory of the PlomRogue source package.
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, int16_t, UINT16_MAX */
10 #include <stdlib.h> /* free() */
11 #include "../common/try_malloc.h" /* try_malloc() */
12 #include "hardcoded_strings.h" /* s */
13 #include "rrand.h" /* rrand() */
14 #include "thing_actions.h" /* get_thing_action_id_by_name() */
15 #include "things.h" /* Thing, ThingType, ThingInMemory, get_thing_type() */
16 #include "world.h" /* world */
22 /* Return "score_map"["pos"] unless "check_inhabitant" and cell is inhabited. */
23 static uint16_t set_neighbor_val(uint16_t * score_map, uint8_t check_inhabitant,
24 uint16_t kill_score, uint16_t pos);
26 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
27 * "score_map" cell at "pos_i" (array index), as found in the directions
28 * north-east, east, south-east etc. (clockwise order). Use "kill_score" for
29 * illegal neighborhoods (i.e. if direction would lead beyond the map's border,
30 * or, if "check_inhabitants" is non-zero, into animate-inhabited cell).
32 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
33 uint16_t kill_score, uint16_t * neighbors,
34 uint8_t check_inhabitants);
36 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
37 * each cell's score against the score of its immediate neighbors in N_DIRS
38 * directions. If any neighbor's score is at least two points lower than the
39 * current cell's score, re-set it to 1 point higher than its lowest-scored
40 * neighbor. Repeat this whole process until all cells have settled on their
41 * final score. Ignore cells whose score is greater than "max_score". Expect
42 * "max_score" to be the maximum score for cells, marking them as unreachable.
44 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
46 /* Helpers to init_score_map(), realizing individual filters. */
47 static uint8_t score_map_filter_attack(uint8_t filter, uint16_t * score_map,
48 struct Thing * t_eye);
49 static uint8_t score_map_filter_flee(uint8_t filter, uint16_t * score_map,
50 struct Thing * t_eye);
51 static uint8_t score_map_filter_consume(uint8_t filter, uint16_t * score_map,
52 struct Thing * t_eye);
53 static uint8_t score_map_filter_search(uint8_t filter, uint16_t * score_map,
54 struct Thing * t_eye);
56 /* get_dir_to_nearest_target() helper: Prepare "score_map" for dijkstra_map(). */
57 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
58 struct Thing * t_eye);
60 /* From "targets" select random "cmp" match as directory by order in "dirs". */
61 static char rand_target_dir(char * dirs, uint16_t cmp, uint16_t * targets);
63 /* Helper to get_dir_to_nearest_target(). */
64 static char get_dir_from_neighbors(char filter, struct Thing * t_eye,
65 uint16_t * score_map);
67 /* Set (if possible) as "t_eye"'s command a move to the path to the path-wise
68 * nearest target that is not "t_eye" and fits criteria set by "filter". On
69 * success, return !0, else 0. Values for "filter":
70 * "a": thing in FOV is below a certain distance, animate, but of a type that is
71 * not "t_eye"'s, and starts out weaker than it is; build path as avoiding
72 * things of "t_eye"'s type
73 * "f": neighbor cell (not inhabited by any animate thing) further away from
74 * animate thing not further than x steps away and in FOV and of a type
75 * that is not "t_eye"'s, and starts out stronger or as strong as "t_eye"
76 * is currently; or (cornered), if no such flight cell, but thing of above
77 * criteria is too near, a cell closer to it, or, if less near, just wait
78 * "c": thing in memorized map is consumable
79 * "s": memory map cell with greatest-reachable degree of unexploredness
81 static uint8_t get_dir_to_nearest_target(struct Thing * t_eye, char filter);
83 /* Return 1 if any thing not "t_eye" is known and fulfills some criteria defined
84 * by "filter", else 0. Values for "filter":
85 * "a" or "f": thing in FOV is animate, but of type that not that of "t_eye",
86 * and starts out weaker ("a") / stronger ("f") than "t_eye" is
87 * "c" : thing in memorized map is consumable
89 static uint8_t seeing_thing(struct Thing * t_eye, char filter);
91 /* Return slot ID of strongest consumable in "t_owner"'s inventory, else -1. */
92 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner);
94 /* Return 1 if "t_standing" is standing on a consumable, else 0. */
95 static uint8_t standing_on_consumable(struct Thing * t_standing);
99 static uint16_t set_neighbor_val(uint16_t * score_map, uint8_t check_inhabitant,
100 uint16_t kill_score, uint16_t pos)
102 if (check_inhabitant)
104 struct Thing * t = world.things;
105 for (; t; t = t->next)
107 if (t->lifepoints && pos == t->pos.y * world.map.length + t->pos.x)
113 return score_map[pos];
118 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
119 uint16_t kill_score, uint16_t * neighbors,
120 uint8_t check_inhabitants)
122 uint32_t map_size = world.map.length * world.map.length;
123 uint8_t open_north = pos_i >= world.map.length;
124 uint8_t open_east = pos_i + 1 % world.map.length;
125 uint8_t open_south = pos_i + world.map.length < map_size;
126 uint8_t open_west = pos_i % world.map.length;
127 uint8_t is_indented = (pos_i / world.map.length) % 2;
128 uint8_t open_diag_west = is_indented || open_west;
129 uint8_t open_diag_east = !is_indented || open_east;
130 neighbors[0] = !(open_north && open_diag_east) ? kill_score :
131 set_neighbor_val(score_map, check_inhabitants, kill_score,
132 pos_i - world.map.length + is_indented);
133 neighbors[1] = !(open_east) ? kill_score :
134 set_neighbor_val(score_map, check_inhabitants, kill_score,
136 neighbors[2] = !(open_south && open_diag_east) ? kill_score :
137 set_neighbor_val(score_map, check_inhabitants, kill_score,
138 pos_i + world.map.length + is_indented);
139 neighbors[3] = !(open_south && open_diag_west) ? kill_score :
140 set_neighbor_val(score_map, check_inhabitants, kill_score,
141 pos_i + world.map.length - !is_indented);
142 neighbors[4] = !(open_west) ? kill_score :
143 set_neighbor_val(score_map, check_inhabitants, kill_score,
145 neighbors[5] = !(open_north && open_diag_west) ? kill_score :
146 set_neighbor_val(score_map, check_inhabitants, kill_score,
147 pos_i - world.map.length - !is_indented);
152 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
154 uint32_t map_size = world.map.length * world.map.length;
156 uint16_t i_scans, neighbors[N_DIRS], min_neighbor;
157 uint8_t scores_still_changing = 1;
159 for (i_scans = 0; scores_still_changing; i_scans++)
161 scores_still_changing = 0;
162 for (pos = 0; pos < map_size; pos++)
164 if (score_map[pos] <= max_score)
166 get_neighbor_scores(score_map, pos, max_score, neighbors, 0);
167 min_neighbor = max_score;
168 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
170 if (min_neighbor > neighbors[i_dirs])
172 min_neighbor = neighbors[i_dirs];
175 if (score_map[pos] > min_neighbor + 1)
177 score_map[pos] = min_neighbor + 1;
178 scores_still_changing = 1;
187 static uint8_t score_map_filter_attack(uint8_t filter, uint16_t * score_map,
188 struct Thing * t_eye)
194 struct Thing * t = world.things;
195 for (; t; t = t->next)
197 if ( t != t_eye && t->lifepoints && t->type != t_eye->type
198 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x]
199 && get_thing_type(t->type)->lifepoints < t_eye->lifepoints)
201 score_map[t->pos.y * world.map.length + t->pos.x] = 0;
203 else if (t->type == t_eye->type)
205 score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
213 static uint8_t score_map_filter_flee(uint8_t filter, uint16_t * score_map,
214 struct Thing * t_eye)
220 struct Thing * t = world.things;
221 for (; t; t = t->next)
223 if ( t->lifepoints && t->type != t_eye->type
224 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x]
225 && get_thing_type(t->type)->lifepoints >= t_eye->lifepoints)
227 score_map[t->pos.y * world.map.length + t->pos.x] = 0;
235 static uint8_t score_map_filter_consume(uint8_t filter, uint16_t * score_map,
236 struct Thing * t_eye)
242 struct ThingInMemory * tm = t_eye->t_mem;
243 for (; tm; tm = tm->next)
245 if ( ' ' != t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x]
246 && get_thing_type(tm->type)->consumable)
248 score_map[tm->pos.y * world.map.length + tm->pos.x] = 0;
256 static uint8_t score_map_filter_search(uint8_t filter, uint16_t * score_map,
257 struct Thing * t_eye)
259 if (!(('0' < filter && '9' >= filter) || ' ' == filter))
264 for (i = 0; i < (uint32_t) (world.map.length * world.map.length); i++)
266 score_map[i] = filter == t_eye->mem_depth_map[i] ? 0 : score_map[i];
273 static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size,
274 struct Thing * t_eye)
277 for (i = 0; i < map_size; i++)
279 score_map[i] = UINT16_MAX;
280 if ('.' == t_eye->mem_map[i])
282 score_map[i] = UINT16_MAX-1;
285 if ( score_map_filter_attack(filter, score_map, t_eye)
286 || score_map_filter_flee(filter, score_map, t_eye)
287 || score_map_filter_consume(filter, score_map, t_eye)
288 || score_map_filter_search(filter, score_map, t_eye))
294 static char rand_target_dir(char * dirs, uint16_t cmp, uint16_t * targets)
296 char candidates[N_DIRS];
297 uint8_t n_candidates = 0;
299 for (i = 0; i < N_DIRS; i++)
301 if (cmp == targets[i])
303 candidates[n_candidates] = dirs[i];
307 return n_candidates ? candidates[rrand() % n_candidates] : 0;
312 static char get_dir_from_neighbors(char filter, struct Thing * t_eye,
313 uint16_t * score_map)
315 char dir_to_nearest_target = 0;
316 uint16_t pos_i = (t_eye->pos.y * world.map.length) + t_eye->pos.x;
317 char * dirs = "edcxsw"; /* get_neighbor_scores()'s clockwise dir order. */
318 uint16_t neighbors[N_DIRS];
319 get_neighbor_scores(score_map, pos_i, UINT16_MAX, neighbors, 'f'==filter);
320 uint16_t minmax_start = 'f' == filter ? 0 : UINT16_MAX-1;
321 uint16_t minmax_neighbor = minmax_start;
323 for (i = 0; i < N_DIRS; i++)
325 if ( ( 'f' == filter && score_map[pos_i] < neighbors[i]
326 && minmax_neighbor < neighbors[i] && UINT16_MAX != neighbors[i])
327 || ('f' != filter && minmax_neighbor > neighbors[i]))
329 minmax_neighbor = neighbors[i];
332 if (minmax_neighbor != minmax_start)
334 dir_to_nearest_target = rand_target_dir(dirs,minmax_neighbor,neighbors);
338 if (!dir_to_nearest_target)
340 if (1 == score_map[pos_i]) /* Attack if cornered too closely. */
342 dir_to_nearest_target = rand_target_dir(dirs, 0, neighbors);
344 else if (3 >= score_map[pos_i]) /* If less closely, just wait. */
346 t_eye->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
350 else if (dir_to_nearest_target && 3 < score_map[pos_i]) /* Don't flee */
352 dir_to_nearest_target = 0; /* a certain */
355 else if ('a' == filter && 10 <= score_map[pos_i])
357 dir_to_nearest_target = 0;
359 return dir_to_nearest_target;
364 static uint8_t get_dir_to_nearest_target(struct Thing * t_eye, char filter)
366 char dir_to_nearest_target = 0;
367 uint8_t mem_depth_char = ' ';
368 uint8_t run_i = 's' == filter ? 9 /* max explored mem depth age */ + 1 : 1;
369 while ( run_i && !dir_to_nearest_target
370 && ('s' == filter || seeing_thing(t_eye, filter)))
373 uint32_t map_size = world.map.length * world.map.length;
374 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t),__func__);
375 init_score_map('s' == filter ? mem_depth_char : filter,
376 score_map, map_size, t_eye);
377 mem_depth_char = ' ' == mem_depth_char ? '9' : mem_depth_char - 1;
378 dijkstra_map(score_map, UINT16_MAX-1);
379 dir_to_nearest_target = get_dir_from_neighbors(filter,t_eye,score_map);
381 if (dir_to_nearest_target && 1 != dir_to_nearest_target)
383 t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
384 t_eye->arg = dir_to_nearest_target;
387 return dir_to_nearest_target;
392 static uint8_t seeing_thing(struct Thing * t_eye, char filter)
394 if (t_eye->fov_map && ('a' == filter || 'f' == filter))
396 struct Thing * t = world.things;
397 for (; t; t = t->next)
399 if ( t != t_eye && t->lifepoints && t->type != t_eye->type
400 && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
402 struct ThingType * tt = get_thing_type(t->type);
403 if ( ('f' == filter && tt->lifepoints >= t_eye->lifepoints)
404 || ('a' == filter && tt->lifepoints < t_eye->lifepoints))
411 else if (t_eye->mem_map && 'c' == filter)
413 struct ThingInMemory * tm = t_eye->t_mem;
414 for (; tm; tm = tm->next)
416 if ( ' ' != t_eye->mem_map[tm->pos.y*world.map.length+tm->pos.x]
417 && get_thing_type(tm->type)->consumable)
428 static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
430 uint8_t compare_consumability = 0;
431 int16_t selection = -1;
432 struct Thing * t = t_owner->owns;;
434 for (i = 0; t; t = t->next, i++)
436 struct ThingType * tt = get_thing_type(t->type);
437 if (tt->consumable > compare_consumability)
439 compare_consumability = tt->consumable;
448 static uint8_t standing_on_consumable(struct Thing * t_standing)
450 struct Thing * t = world.things;
451 for (; t; t = t->next)
454 && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x
455 && get_thing_type(t->type)->consumable)
465 extern void ai(struct Thing * t)
467 t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
468 if (!get_dir_to_nearest_target(t, 'f'))
470 int16_t sel = get_inventory_slot_to_consume(t);
473 t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
474 t->arg = (uint8_t) sel;
476 else if (standing_on_consumable(t))
478 t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
480 else if ( !get_dir_to_nearest_target(t, 'c')
481 && !get_dir_to_nearest_target(t, 'a'))
483 get_dir_to_nearest_target(t, 's');