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 "map_object_actions.h" /* get_moa_id_by_name() */
9 #include "map_objects.h" /* struct MapObj */
10 #include "world.h" /* global world */
18 /* Write into "neighbors" scores of the N_DIRS immediate neighbors of the
19 * "score_map" cell at "pos_i" (array index), as found in the directions
20 * north-east, east, south-east etc. (clockwise order). Use "max_score" for
21 * illegal neighborhoods (i.e. if direction would lead beyond the map's border).
23 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
24 uint16_t max_score, uint16_t * neighbors);
26 /* Iterate over scored cells in "score_map" of world.map's geometry. Compare
27 * each cell's score against the score of its immediate neighbors in N_DIRS
28 * directions. If it's neighbors are low enough that the result would be lower
29 * than the current value, re-set it to 1 point higher than its lowest-scored
30 * neighbor- Repeat this whole process until all cells have settled on their
31 * final score. Ignore cells whose position in "score_map" fits cells of
32 * unreachable terrain in world.map.cells. Expect "max_score" to be the maximum
33 * score for cells, marking them as unreachable.
35 static void dijkstra_map(uint16_t * score_map, uint16_t max_score);
37 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
38 * shortest path to "mo_origin". If no enemy is around, return 0.
40 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin);
44 static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i,
45 uint16_t max_score, uint16_t * neighbors)
47 uint32_t map_size = world.map.size.y * world.map.size.x;
49 for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
50 uint8_t open_north = pos_i >= world.map.size.x;
51 uint8_t open_east = pos_i + 1 % world.map.size.x;
52 uint8_t open_south = pos_i + world.map.size.x < map_size;
53 uint8_t open_west = pos_i % world.map.size.x;
54 uint8_t is_indented = (pos_i / world.map.size.x) % 2;
55 uint8_t open_diag_west = is_indented || open_west;
56 uint8_t open_diag_east = !is_indented || open_east;
57 if (open_north && open_diag_east)
59 neighbors[0] = score_map[pos_i - world.map.size.x + is_indented];
63 neighbors[1] = score_map[pos_i + 1];
65 if (open_south && open_diag_east)
67 neighbors[2] = score_map[pos_i + world.map.size.x + is_indented];
69 if (open_south && open_diag_west)
71 neighbors[3] = score_map[pos_i + world.map.size.x - !is_indented];
75 neighbors[4] = score_map[pos_i - 1];
77 if (open_north && open_diag_west)
79 neighbors[5] = score_map[pos_i - world.map.size.x - !is_indented];
85 static void dijkstra_map(uint16_t * score_map, uint16_t max_score)
87 uint32_t map_size = world.map.size.y * world.map.size.x;
88 uint16_t pos, i_scans, neighbors[N_DIRS], min_neighbor;
89 uint8_t scores_still_changing = 1;
91 for (i_scans = 0; scores_still_changing; i_scans++)
93 scores_still_changing = 0;
94 for (pos = 0; pos < map_size; pos++)
96 if ('.' == world.map.cells[pos])
98 get_neighbor_scores(score_map, pos, max_score, neighbors);
99 min_neighbor = max_score;
100 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
102 if (min_neighbor > neighbors[i_dirs])
104 min_neighbor = neighbors[i_dirs];
107 if (score_map[pos] > min_neighbor + 1)
109 score_map[pos] = min_neighbor + 1;
110 scores_still_changing = 1;
119 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin)
121 char * f_name = "get_dir_to_nearest_enemy()";
123 /* Calculate for each cell the distance to the nearest map actor that is
124 * not "mo_origin", with movement only possible in the directions of "dir".
125 * (Actors' own cells start with a distance of 0 towards themselves.)
127 uint32_t map_size = world.map.size.y * world.map.size.x;
128 uint16_t max_score = UINT16_MAX;
129 uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), f_name);
131 for (i = 0; i < map_size; i++)
133 score_map[i] = max_score;
135 struct MapObj * mo = world.map_objs;
136 for (; mo != NULL; mo = mo->next)
138 if (!mo->lifepoints || mo == mo_origin)
142 score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0;
144 dijkstra_map(score_map, max_score);
146 /* Return direction of "mo_origin"'s lowest-scored neighbor cell. */
147 uint16_t neighbors[N_DIRS];
148 uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x;
149 get_neighbor_scores(score_map, pos_i, max_score, neighbors);
151 char dir_to_nearest_enemy = 0;
152 uint16_t min_neighbor = max_score;
153 char * dirs = "edcxsw"; /* get_neighbor_scores()'s clockwise dir order.*/
154 for (i = 0; i < N_DIRS; i++)
156 if (min_neighbor > neighbors[i])
158 min_neighbor = neighbors[i];
159 dir_to_nearest_enemy = dirs[i];
162 return dir_to_nearest_enemy;
167 extern void ai(struct MapObj * mo)
169 mo->command = get_moa_id_by_name("wait");
170 char sel = get_dir_to_nearest_enemy(mo);
173 mo->command = get_moa_id_by_name("move");