+static char get_dir_to_nearest_enemy(struct Thing * t_origin)
+{
+ /* Calculate for each cell distance to visibly nearest enemy, with movement
+ * possible in the directions or "dir". (Actor's own cells start with 0
+ * distance towards themselves. Cells of actors of own type are invisible.)
+ */
+ uint32_t map_size = world.map.length * world.map.length;
+ uint16_t max_score = UINT16_MAX - 1;
+ uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t), __func__);
+ uint32_t i;
+ for (i = 0; i < map_size; i++)
+ {
+ score_map[i] = UINT16_MAX;
+ if ('.' == t_origin->mem_map[i])
+ {
+ score_map[i] = max_score;
+ }
+ }
+ struct Thing * t = world.things;
+ for (; t != NULL; t = t->next)
+ {
+ if ( !t->lifepoints || t == t_origin
+ || 'H' == t_origin->fov_map[t->pos.y * world.map.length + t->pos.x])
+ {
+ continue;
+ }
+ if (t->lifepoints && t->type == t_origin->type)
+ {
+ score_map[t->pos.y * world.map.length + t->pos.x] = UINT16_MAX;
+ continue;
+ }
+ score_map[t->pos.y * world.map.length + t->pos.x] = 0;
+ }
+ dijkstra_map(score_map, max_score);
+
+ /* Return direction of "t_origin"'s lowest-scored neighbor cell. */
+ uint16_t neighbors[N_DIRS];
+ uint16_t pos_i = (t_origin->pos.y * world.map.length) + t_origin->pos.x;
+ get_neighbor_scores(score_map, pos_i, max_score, neighbors);
+ free(score_map);
+ char dir_to_nearest_enemy = 0;
+ uint16_t min_neighbor = max_score;
+ char * dirs = "edcxsw"; /* get_neighbor_scores()'s clockwise dir order.*/
+ for (i = 0; i < N_DIRS; i++)
+ {
+ if (min_neighbor > neighbors[i])
+ {
+ min_neighbor = neighbors[i];
+ dir_to_nearest_enemy = dirs[i];
+ }
+ }
+ return dir_to_nearest_enemy;
+}
+
+
+
+static uint8_t seeing_enemies(struct Thing * t_origin)