+static char get_dir_to_nearest_enemy(struct MapObj * mo_target)
+{
+ /* Calculate for each cell the distance to the nearest map actor that is
+ * not "mo_target", with movement only possible in the directions of "dir".
+ * (Actor's own cells start with a distance of 0 towards themselves.)
+ */
+ uint8_t max_score = UINT8_MAX; /* Score for cells treated as unreachable. */
+ char * dirs = "NESW";
+ uint8_t score_map[world.map.size.y * world.map.size.x];
+ memset(score_map, max_score, world.map.size.y * world.map.size.x);
+ struct MapObj * mo = world.map_objs;
+ for (; mo != NULL; mo = mo->next)
+ {
+ if (!mo->lifepoints || mo == mo_target)
+ {
+ continue;
+ }
+ score_map[(mo->pos.y * world.map.size.x) + mo->pos.x] = 0;
+ }
+ dijkstra_map(dirs, score_map, max_score);
+
+ /* Return direction of "mo_target"'s lowest-scored neighbor cell. */
+ uint8_t len_dirs = strlen(dirs);
+ uint32_t pos_i = (mo_target->pos.y * world.map.size.x) + mo_target->pos.x;
+ uint8_t neighbor_scores[len_dirs];
+ get_neighbor_scores(dirs, len_dirs, score_map, mo_target->pos, pos_i,
+ max_score, neighbor_scores);
+ char dir_to_nearest_enemy = 0;
+ uint8_t min_neighbor_score = max_score;
+ uint8_t i_dirs;
+ for (i_dirs = 0; i_dirs < len_dirs; i_dirs++)
+ {
+ if (min_neighbor_score > neighbor_scores[i_dirs])
+ {
+ min_neighbor_score = neighbor_scores[i_dirs];
+ dir_to_nearest_enemy = dirs[i_dirs];
+ }
+ }
+ return dir_to_nearest_enemy;
+}
+
+
+
+extern void ai(struct MapObj * mo)