X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/cards/%7B%7Bcard_id%7D%7D/static/gitweb.js?a=blobdiff_plain;f=src%2Fserver%2Fai.c;h=83ae79839afd643879769e1daa92acfa13a74000;hb=ddb4196d9b87b71af5988657bcda9407557d3acd;hp=cdefca844bb78dc87277b409d25f2f12ce183137;hpb=3dedf6344c941891491773d1cc5d647aa664b218;p=plomrogue diff --git a/src/server/ai.c b/src/server/ai.c index cdefca8..83ae798 100644 --- a/src/server/ai.c +++ b/src/server/ai.c @@ -1,7 +1,11 @@ -/* src/server/ai.c */ +/* src/server/ai.c + * + * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3 + * or any later version. For details on its copyright, license, and warranties, + * see the file NOTICE in the root directory of the PlomRogue source package. + */ #include "ai.h" -#include /* NULL */ #include /* uint8_t, uint16_t, uint32_t, int16_t, UINT16_MAX */ #include /* free() */ #include "../common/try_malloc.h" /* try_malloc() */ @@ -34,18 +38,19 @@ static void get_neighbor_scores(uint16_t * score_map, uint16_t pos_i, */ static void dijkstra_map(uint16_t * score_map, uint16_t max_score); -/* get_dir_to_nearest_thing() helper: Prepare "score_map" for dijkstra_map(). */ +/* get_dir_to_nearest_target() helper: Prepare "score_map" for dijkstra_map(). */ static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size, struct Thing * t_eye); /* Set (if possible) as "t_eye"'s command a move to the path to the path-wise - * nearest thing that is not "t_eye" and fits criteria set by "filter". On - * success, return 1, else 0. Values for "filter": + * nearest target that is not "t_eye" and fits criteria set by "filter". On + * success, return !0, else 0. Values for "filter": * "e": thing in FOV is animate, but not of "t_eye"'s thing type; build path as * avoiding things of "t_eye"'s type - * "c": thing in memorized map is consumable. + * "c": thing in memorized map is consumable + * "s": memory map cell with greatest-reachable degree of unexploredness */ -static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter); +static uint8_t get_dir_to_nearest_target(struct Thing * t_eye, char filter); /* Return 1 if any thing not "t_eye" is known and fulfills some criteria defined * by "filter", else 0. Values for "filter": @@ -185,18 +190,32 @@ static void init_score_map(char filter, uint16_t * score_map, uint32_t map_size, 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_thing(struct Thing * t_eye, char filter) +static uint8_t get_dir_to_nearest_target(struct Thing * t_eye, char filter) { - char dir_to_nearest_enemy = 0; - if (seeing_thing(t_eye, 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(filter, score_map, map_size, t_eye); + 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; @@ -210,17 +229,16 @@ static uint8_t get_dir_to_nearest_thing(struct Thing * t_eye, char filter) if (min_neighbor > neighbors[i]) { min_neighbor = neighbors[i]; - dir_to_nearest_enemy = dirs[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; + } } - if (dir_to_nearest_enemy) - { - t_eye->command = get_thing_action_id_by_name(s[S_CMD_MOVE]); - t_eye->arg = dir_to_nearest_enemy; - return 1; - } - return 0; + return dir_to_nearest_target; } @@ -268,7 +286,7 @@ static int16_t get_inventory_slot_to_consume(struct Thing * t_owner) int16_t selection = -1; struct Thing * t = t_owner->owns;; uint8_t i; - for (i = 0; t != NULL; t = t->next, i++) + for (i = 0; t; t = t->next, i++) { struct ThingType * tt = get_thing_type(t->type); if (tt->consumable > compare_consumability) @@ -285,7 +303,7 @@ static int16_t get_inventory_slot_to_consume(struct Thing * t_owner) static uint8_t standing_on_consumable(struct Thing * t_standing) { struct Thing * t = world.things; - for (; t != NULL; t = t->next) + for (; t; t = t->next) { if ( t != t_standing && t->pos.y == t_standing->pos.y && t->pos.x == t_standing->pos.x) @@ -305,7 +323,7 @@ static uint8_t standing_on_consumable(struct Thing * t_standing) extern void ai(struct Thing * t) { t->command = get_thing_action_id_by_name(s[S_CMD_WAIT]); - if (!get_dir_to_nearest_thing(t, 'e')) + if (!get_dir_to_nearest_target(t, 'e')) { int16_t sel = get_inventory_slot_to_consume(t); if (-1 != sel) @@ -317,9 +335,9 @@ extern void ai(struct Thing * t) { t->command = get_thing_action_id_by_name(s[S_CMD_PICKUP]); } - else + else if (!get_dir_to_nearest_target(t, 'c')) { - get_dir_to_nearest_thing(t, 'c'); + get_dir_to_nearest_target(t, 's'); } } }