From: Christian Heller Date: Wed, 9 Apr 2014 03:53:40 +0000 (+0200) Subject: Server: Limit iterations for map generation and object start placement. X-Git-Tag: tce~774 X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/%7B%7Byoutube_prefix%7D%7D%7B%7Bresult.id%7D%7D?a=commitdiff_plain;h=4ee05e4f640430b572c628a38cf0e90ea8d0e755;p=plomrogue Server: Limit iterations for map generation and object start placement. --- diff --git a/src/server/map.c b/src/server/map.c index 0edd955..95d7e0f 100644 --- a/src/server/map.c +++ b/src/server/map.c @@ -1,7 +1,8 @@ /* src/server/map.c */ #include "map.h" -#include /* uint8_t, uint16_t, uint32_t */ +#include /* uint8_t, uint16_t, uint32_t, UINT16_MAX */ +#include "../common/rexit.h" /* exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ #include "../common/yx_uint8.h" /* struct yx_uint8 */ #include "rrand.h" /* rrand() */ @@ -24,7 +25,9 @@ extern void init_map() uint8_t add_half_width = !(world.map.size.y % 2) * (world.map.size.x / 2); world.map.cells[(size / 2) + add_half_width] = '.'; uint16_t curpos; - while (1) + char * err = "Map generation reached iteration limit. Change map size?"; + uint32_t i; + for (i = 0; ; i++, exit_err(256 * UINT16_MAX == i, err)) { y = rrand() % world.map.size.y; x = rrand() % world.map.size.x; diff --git a/src/server/map_objects.c b/src/server/map_objects.c index ff062d7..ac36394 100644 --- a/src/server/map_objects.c +++ b/src/server/map_objects.c @@ -2,7 +2,7 @@ #include "map_objects.h" #include /* NULL */ -#include /* uint8_t, uint16_t */ +#include /* uint8_t, uint16_t, UINT16_MAX */ #include /* free() */ #include /* memset(), strlen() */ #include "../common/rexit.h" /* exit_err() */ @@ -18,10 +18,7 @@ /* Return pointer to map object of "id" in chain starting at "ptr". */ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id); -/* Return random passable (as by is_passable()) position on world.map. */ -static struct yx_uint8 find_passable_pos(); - -/* Add object of "type" to map on random position. Don't place actor on actor.*/ +/* Add object of "type" to map on passable position. Don't put actor on actor.*/ static void add_map_object(uint8_t type); @@ -45,19 +42,6 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id) -static struct yx_uint8 find_passable_pos() -{ - struct yx_uint8 pos; - for (pos.y = pos.x = 0; 0 == is_passable(pos);) - { - pos.y = rrand() % world.map.size.y; - pos.x = rrand() % world.map.size.x; - } - return pos; -} - - - static void add_map_object(uint8_t type) { char * f_name = "add_map_object()"; @@ -67,9 +51,17 @@ static void add_map_object(uint8_t type) mo->id = world.map_obj_count++; mo->type = mod->id; mo->lifepoints = mod->lifepoints; + char * err = "Space to put map object on too hard to find. Map too small?"; + uint16_t i = 0; while (1) { - struct yx_uint8 pos = find_passable_pos(world.map); + struct yx_uint8 pos; + for (pos.y = pos.x = 0; 0 == is_passable(pos); i++) + { + exit_err(UINT16_MAX == i, err); + pos.y = rrand() % world.map.size.y; + pos.x = rrand() % world.map.size.x; + } struct MapObj * mo_ptr; uint8_t clear = 1; for (mo_ptr = world.map_objs; mo_ptr != NULL; mo_ptr = mo_ptr->next)