X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fai.c;h=a0f4793fc9881dc95041341bde469685fe55040c;hb=6245b251886853269262bc77d0f0378c28e241bc;hp=c25c277dcb73b70101c823e41e0f360b5698f204;hpb=cdb90723dd636591bcfa98ebb165cf74a0cdeec7;p=plomrogue diff --git a/src/server/ai.c b/src/server/ai.c index c25c277..a0f4793 100644 --- a/src/server/ai.c +++ b/src/server/ai.c @@ -3,6 +3,8 @@ #include "ai.h" #include /* NULL */ #include /* uint8_t, uint16_t, uint32_t, UINT16_MAX */ +#include /* free() */ +#include "../common/try_malloc.h" /* try_malloc() */ #include "map_object_actions.h" /* get_moa_id_by_name() */ #include "map_objects.h" /* struct MapObj */ #include "world.h" /* global world */ @@ -134,13 +136,15 @@ static void dijkstra_map(uint32_t * score_map, uint32_t max_score) static char get_dir_to_nearest_enemy(struct MapObj * mo_origin) { + char * f_name = "get_dir_to_nearest_enemy()"; + /* Calculate for each cell the distance to the nearest map actor that is * not "mo_origin", with movement only possible in the directions of "dir". * (Actors' own cells start with a distance of 0 towards themselves.) */ uint32_t map_size = world.map.size.y * world.map.size.x; uint32_t max_score = UINT32_MAX - (world.map.dist_diagonal + 1); - uint32_t score_map[map_size]; + uint32_t * score_map = try_malloc(map_size * sizeof(uint32_t), f_name); uint32_t i; for (i = 0; i < map_size; i++) { @@ -161,6 +165,7 @@ static char get_dir_to_nearest_enemy(struct MapObj * mo_origin) uint32_t neighbors[N_DIRS]; uint16_t pos_i = (mo_origin->pos.y * world.map.size.x) + mo_origin->pos.x; get_neighbor_scores(score_map, pos_i, max_score, neighbors); + free(score_map); char dir_to_nearest_enemy = 0; uint32_t min_neighbor = max_score; char * dirs = "89632147"; /* get_neighbor_scores()'s clockwise dir order.*/