+ score_map[i] = UINT16_MAX;
+ if ('.' == t_eye->mem_map[i])
+ {
+ score_map[i] = UINT16_MAX-1;
+ }
+ }
+ if ('e' == filter)
+ {
+ struct Thing * t = world.things;
+ for (; t; t = t->next)
+ {
+ if ( t==t_eye || !t->lifepoints
+ || 'H' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
+ {
+ continue;
+ }
+ else if (t->lifepoints && t->type == t_eye->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;
+ }
+ }
+ else if ('c' == filter)
+ {
+ struct ThingInMemory * tm = t_eye->t_mem;
+ for (; tm; tm = tm->next)
+ {
+ if (' ' == t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
+ {
+ continue;
+ }
+ struct ThingType * tt = get_thing_type(tm->type);
+ if (!tt->consumable)
+ {
+ continue;
+ }
+ score_map[tm->pos.y * world.map.length + tm->pos.x] = 0;
+ }
+ }
+ else if (('0' < filter && '9' >= filter) || ' ' == filter)
+ {
+ uint32_t i;
+ for (i = 0; i < (uint32_t) (world.map.length * world.map.length); i++)
+ {
+ score_map[i] = filter == t_eye->mem_depth_map[i] ? 0 : score_map[i];
+ }
+ }
+}
+
+
+
+static uint8_t get_dir_to_nearest_target(struct Thing * t_eye, char filter)
+{
+ char dir_to_nearest_target = 0;
+ uint8_t mem_depth_char = ' ';
+ uint8_t run_i = 's' == filter ? 9 /* max explored mem depth age */ + 1 : 1;
+ while ( run_i && !dir_to_nearest_target
+ && ('s' == filter || seeing_thing(t_eye, filter)))
+ {
+ run_i--;
+ uint32_t map_size = world.map.length * world.map.length;
+ uint16_t * score_map = try_malloc(map_size * sizeof(uint16_t),__func__);
+ init_score_map('s' == filter ? mem_depth_char : filter,
+ score_map, map_size, t_eye);
+ mem_depth_char = ' ' == mem_depth_char ? '9' : mem_depth_char - 1;
+ dijkstra_map(score_map, UINT16_MAX-1);
+ uint16_t neighbors[N_DIRS];
+ uint16_t pos_i = (t_eye->pos.y * world.map.length) + t_eye->pos.x;
+ get_neighbor_scores(score_map, pos_i, UINT16_MAX-1, neighbors);
+ free(score_map);
+ uint16_t min_neighbor = UINT16_MAX-1;
+ char * dirs = "edcxsw";/* get_neighbor_scores()'s clockwise dir order.*/
+ uint8_t i;
+ for (i = 0; i < N_DIRS; i++)
+ {
+ if (min_neighbor > neighbors[i])
+ {
+ min_neighbor = neighbors[i];
+ dir_to_nearest_target = dirs[i];
+ }
+ }
+ if (dir_to_nearest_target)
+ {
+ t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]);
+ t_eye->arg = dir_to_nearest_target;
+ }
+ }
+ return dir_to_nearest_target;
+}
+
+
+
+static uint8_t seeing_thing(struct Thing * t_eye, char filter)
+{
+ if (t_eye->fov_map && 'e' == filter)
+ {
+ struct Thing * t = world.things;
+ for (; t; t = t->next)
+ {
+ if ( t != t_eye
+ && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
+ {
+ if (t->lifepoints && t->type != t_eye->type)
+ {
+ return 1;
+ }
+ }
+ }
+ }
+ else if (t_eye->mem_map && 'c' == filter)
+ {
+ struct ThingInMemory * tm = t_eye->t_mem;
+ for (; tm; tm = tm->next)
+ {
+ if (' ' != t_eye->mem_map[tm->pos.y * world.map.length + tm->pos.x])
+ {
+ struct ThingType * tt = get_thing_type(tm->type);
+ if (tt->consumable)
+ {
+ return 1;
+ }
+ }
+ }