X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;f=src%2Fserver%2Fai.c;h=a0f4793fc9881dc95041341bde469685fe55040c;hb=0ff9d0a37959c6f297d33c23fed0eb889114c4bc;hp=50c5031018810d5baca6348299e394ae5329cb98;hpb=e03020342a74aef143b1ec38c18966dac64181b5;p=plomrogue diff --git a/src/server/ai.c b/src/server/ai.c index 50c5031..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 */ @@ -44,7 +46,7 @@ static char get_dir_to_nearest_enemy(struct MapObj * mo_origin); static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i, uint32_t max_score, uint32_t * neighbors) { - uint16_t map_size = world.map.size.y * world.map.size.x; + uint32_t map_size = world.map.size.y * world.map.size.x; uint8_t i_dir; for (i_dir = 0; i_dir < N_DIRS; neighbors[i_dir] = max_score, i_dir++); uint8_t open_north = pos_i >= world.map.size.x; @@ -90,7 +92,7 @@ static void get_neighbor_scores(uint32_t * score_map, uint16_t pos_i, static void dijkstra_map(uint32_t * score_map, uint32_t max_score) { uint32_t i_scans, neighbors[N_DIRS], min_neighbor_o, min_neighbor_d; - uint16_t map_size = world.map.size.y * world.map.size.x; + uint32_t map_size = world.map.size.y * world.map.size.x; uint16_t pos; uint8_t scores_still_changing = 1; uint8_t i_dirs; @@ -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.) */ - uint16_t map_size = world.map.size.y * world.map.size.x; + 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.*/