+ if (minmax_neighbor != minmax_start)
+ {
+ dir_to_nearest_target = rand_target_dir(dirs,minmax_neighbor,neighbors);
+ }
+ if ('f' == filter)
+ {
+ if (!dir_to_nearest_target)
+ {
+ if (1 == score_map[pos_i]) /* Attack if cornered too closely. */
+ {
+ dir_to_nearest_target = rand_target_dir(dirs, 0, neighbors);
+ }
+ else if (3 >= score_map[pos_i]) /* If less closely, just wait. */
+ {
+ t_eye->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
+ return 1;
+ }
+ }
+ else if (dir_to_nearest_target && 3 < score_map[pos_i]) /* Don't flee */
+ { /* enemy of */
+ dir_to_nearest_target = 0; /* a certain */
+ } /* distance. */
+ }
+ else if ('a' == filter && 10 <= score_map[pos_i])
+ {
+ dir_to_nearest_target = 0;
+ }
+ return dir_to_nearest_target;
+}
+
+
+
+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);
+ dir_to_nearest_target = get_dir_from_neighbors(filter,t_eye,score_map);
+ free(score_map);
+ if (dir_to_nearest_target && 1 != 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 && ('a' == filter || 'f' == filter))
+ {
+ struct Thing * t = world.things;
+ for (; t; t = t->next)
+ {
+ if ( t != t_eye && t->lifepoints && t->type != t_eye->type
+ && 'v' == t_eye->fov_map[t->pos.y*world.map.length + t->pos.x])
+ {
+ struct ThingType * tt = get_thing_type(t->type);
+ if ( ('f' == filter && tt->lifepoints >= t_eye->lifepoints)
+ || ('a' == filter && tt->lifepoints < t_eye->lifepoints))
+ {
+ 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]
+ && get_thing_type(tm->type)->consumable)
+ {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+
+
+static int16_t get_inventory_slot_to_consume(struct Thing * t_owner)
+{
+ uint8_t compare_consumability = 0;
+ int16_t selection = -1;
+ struct Thing * t = t_owner->owns;;
+ uint8_t i;
+ for (i = 0; t; t = t->next, i++)
+ {
+ struct ThingType * tt = get_thing_type(t->type);
+ if (tt->consumable > compare_consumability)
+ {
+ compare_consumability = tt->consumable;
+ selection = i;
+ }
+ }
+ return selection;
+}
+
+
+
+static uint8_t standing_on_consumable(struct Thing * t_standing)
+{
+ struct Thing * t = world.things;
+ for (; t; t = t->next)
+ {
+ if ( t != t_standing
+ && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x
+ && get_thing_type(t->type)->consumable)
+ {
+ return 1;
+ }
+ }
+ return 0;