+ 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)
+ {
+ 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;
+}
+
+
+
+extern void ai(struct Thing * t)
+{
+ t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]);
+ if (!get_dir_to_nearest_target(t, 'f'))
+ {
+ int16_t sel = get_inventory_slot_to_consume(t);
+ if (-1 != sel)
+ {
+ t->command = get_thing_action_id_by_name(s[S_CMD_USE]);
+ t->arg = (uint8_t) sel;
+ }
+ else if (standing_on_consumable(t))
+ {
+ t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]);
+ }
+ else if ( !get_dir_to_nearest_target(t, 'c')
+ && !get_dir_to_nearest_target(t, 'a'))
+ {
+ get_dir_to_nearest_target(t, 's');
+ }