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 eight immediate 2D neighbors of the
19 * "score_map" cell at "pos_i" (array index), as found in the directions north,
20 * north-east, east etc. (clockwise order). "max_score" is used for illegal
21 * neighborhoods (i.e. if the direction would lead beyond the map's border).
23 static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i,
24 uint32_t max_score, uint32_t * neighbors);
26 /* Iterate over scored cells in "score_map" of world.map's 2D 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 world.map.dist_orthogonal points higher
30 * than its lowest-scored orthogonal neighbor or world.map.dist_diagonal points
31 * higher than its lowest-scored diagonal neighbor (whatever would result in a
32 * lower value). Repeat this whole process until all cells have settled on their
33 * final score. Ignore cells whose position in "score_map" fits cells of
34 * unreachable terrain in world.map.cells. Expect "max_score" to be the maximum
35 * score for cells, marking them as unreachable.
37 static void dijkstra_map(uint32_t * score_map, uint32_t max_score);
39 /* Return numpad char of direction ("8", "6", "2", "4" etc.) of enemy with the
40 * shortest path to "mo_origin". If no enemy is around, return 0.
42 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin);
46 static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i,
47 uint32_t max_score, uint32_t * neighbors)
49 uint32_t map_size = world.map.size.y * world.map.size.x;
51 for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++);
52 uint8_t open_north = pos_i >= world.map.size.x;
53 uint8_t open_east = pos_i + 1 % world.map.size.x;
54 uint8_t open_south = pos_i + world.map.size.x < map_size;
55 uint8_t open_west = pos_i % world.map.size.x;
58 neighbors[0] = score_map[pos_i - world.map.size.x];
60 if (open_north && open_east)
62 neighbors[1] = score_map[pos_i - world.map.size.x + 1];
66 neighbors[2] = score_map[pos_i + 1];
68 if (open_east && open_south)
70 neighbors[3] = score_map[pos_i + 1 + world.map.size.x];
74 neighbors[4] = score_map[pos_i + world.map.size.x];
76 if (open_south && open_west)
78 neighbors[5] = score_map[pos_i + world.map.size.x - 1];
82 neighbors[6] = score_map[pos_i - 1];
84 if (open_west && open_north)
86 neighbors[7] = score_map[pos_i - 1 - world.map.size.x];
92 static void dijkstra_map(uint32_t * score_map, uint32_t max_score)
94 uint32_t i_scans, neighbors[N_DIRS], min_neighbor_o, min_neighbor_d;
95 uint32_t map_size = world.map.size.y * world.map.size.x;
97 uint8_t scores_still_changing = 1;
99 for (i_scans = 0; scores_still_changing; i_scans++)
101 scores_still_changing = 0;
102 for (pos = 0; pos < map_size; pos++)
104 if ('.' == world.map.cells[pos])
106 get_neighbor_scores(score_map, pos, max_score, neighbors);
107 min_neighbor_d = max_score;
108 min_neighbor_o = max_score;
109 for (i_dirs = 0; i_dirs < N_DIRS; i_dirs++)
111 if (!(i_dirs % 2) && min_neighbor_o > neighbors[i_dirs])
113 min_neighbor_o = neighbors[i_dirs];
115 else if (i_dirs % 2 && min_neighbor_d > neighbors[i_dirs])
117 min_neighbor_d = neighbors[i_dirs];
120 if (score_map[pos] > min_neighbor_o + world.map.dist_orthogonal)
122 score_map[pos] = min_neighbor_o + world.map.dist_orthogonal;
123 scores_still_changing = 1;
125 if (score_map[pos] > min_neighbor_d + world.map.dist_diagonal)
127 score_map[pos] = min_neighbor_d + world.map.dist_diagonal;
128 scores_still_changing = 1;
137 static char get_dir_to_nearest_enemy(struct MapObj * mo_origin)
139 char * f_name = "get_dir_to_nearest_enemy()";
141 /* Calculate for each cell the distance to the nearest map actor that is
142 * not "mo_origin", with movement only possible in the directions of "dir".
143 * (Actors' own cells start with a distance of 0 towards themselves.)
145 uint32_t map_size = world.map.size.y * world.map.size.x;
146 uint32_t max_score = UINT32_MAX - (world.map.dist_diagonal + 1);
147 uint32_t * score_map = try_malloc(map_size * sizeof(uint32_t), f_name);
149 for (i = 0; i < map_size; i++)
151 score_map[i] = max_score;
153 struct MapObj * mo = world.map_objs;
154 for (; mo != NULL; mo = mo->next)
156 if (!mo->lifepoints || mo == mo_origin)
160 score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0;
162 dijkstra_map(score_map, max_score);
164 /* Return direction of "mo_origin"'s lowest-scored neighbor cell. */
165 uint32_t neighbors[N_DIRS];
166 uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x;
167 get_neighbor_scores(score_map, pos_i, max_score, neighbors);
169 char dir_to_nearest_enemy = 0;
170 uint32_t min_neighbor = max_score;
171 char * dirs = "89632147"; /* get_neighbor_scores()'s clockwise dir order.*/
172 for (i = 0; i < N_DIRS; i++)
174 if (min_neighbor > neighbors[i])
176 min_neighbor = neighbors[i];
177 dir_to_nearest_enemy = dirs[i];
180 return dir_to_nearest_enemy;
185 extern void ai(struct MapObj * mo)
187 mo->command = get_moa_id_by_name("wait");
188 char sel = get_dir_to_nearest_enemy(mo);
191 mo->command = get_moa_id_by_name("move");