X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fserver%2Fmap_objects.c;h=975c7487fe98906bf4d588c7b4350d02d0da6a13;hb=4266e8b48fc5481ce24c0bf02fdd517217f8390a;hp=0401c76b4339f959b312a81950bef4125bd92182;hpb=6052be616897d692fc57ab15b56266b4b3f4668b;p=plomrogue diff --git a/src/server/map_objects.c b/src/server/map_objects.c index 0401c76..975c748 100644 --- a/src/server/map_objects.c +++ b/src/server/map_objects.c @@ -2,34 +2,23 @@ #include "map_objects.h" #include /* NULL */ -#include /* FILE typedef */ -#include /* uint8_t, uint16_t, UINT8_MAX */ -#include /* free(), atoi() */ -#include /* strlen(), memcpy(), memset() */ -#include "../common/err_try_fgets.h" /* err_try_fgets(), err_line(), - * reset_err_try_fgets_counter() - */ -#include "../common/readwrite.h" /* try_fopen(), try_fclose(), try_fgetc(), - * textfile_sizes() - */ -#include "../common/rexit.h" /* exit_err(), exit_trouble() */ +#include /* uint8_t, uint16_t, UINT16_MAX */ +#include /* free() */ +#include /* memset(), strlen() */ +#include "../common/rexit.h" /* exit_err() */ #include "../common/try_malloc.h" /* try_malloc() */ -#include "../common/yx_uint16.h" /* yx_uint16 struct */ -#include "cleanup.h" /* set_cleanup_flag() */ +#include "../common/yx_uint8.h" /* yx_uint8 struct */ #include "map.h" /* is_passable() */ #include "rrand.h" /* rrand() */ #include "world.h" /* global world */ -#include "yx_uint16.h" /* yx_uint16_cmp() */ +#include "yx_uint8.h" /* yx_uint8_cmp() */ /* 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_uint16 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); @@ -53,19 +42,6 @@ static struct MapObj * get_map_object(struct MapObj * ptr, uint8_t id) -static struct yx_uint16 find_passable_pos() -{ - struct yx_uint16 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()"; @@ -75,14 +51,22 @@ 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_uint16 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) { - if (yx_uint16_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints) + if (yx_uint8_cmp(&pos, &mo_ptr->pos) && 0 != mo_ptr->lifepoints) { clear = 0; break; @@ -101,57 +85,6 @@ static void add_map_object(uint8_t type) -extern void init_map_object_defs() -{ - char * f_name = "init_map_object_defs()"; - char * context = "Failed reading map object definitions file. "; - char * err_toolarge = "Value is too large."; - char * err_uniq = "Declaration of ID already used."; - FILE * file = try_fopen(world.path_map_obj_defs, "r", f_name); - uint16_t linemax = textfile_sizes(file, NULL); - struct MapObjDef ** last_mod_ptr_ptr = &world.map_obj_defs; - char line[linemax + 1]; - reset_err_try_fgets_counter(); - while (1) - { - int test_for_end = try_fgetc(file, f_name); - if (EOF == test_for_end || '\n' == test_for_end) - { - break; - } - exit_trouble(EOF == ungetc(test_for_end, file), f_name, "ungetc()"); - struct MapObjDef * mod = try_malloc(sizeof(struct MapObjDef), f_name); - mod->next = NULL; - err_try_fgets(line, linemax, file, context, "nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); - mod->id = atoi(line); - struct MapObjDef * mod_test = world.map_obj_defs; - for (; NULL != mod_test; mod_test = mod_test->next) - { - err_line(mod->id == mod_test->id, line, context, err_uniq); - } - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); - mod->corpse_id = atoi(line); - err_try_fgets(line, linemax, file, context, "0nfs"); - mod->char_on_map = line[0]; - err_try_fgets(line, linemax, file, context, "0nfi"); - err_line(atoi(line) > UINT8_MAX, line, context, err_toolarge); - mod->lifepoints = atoi(line); - err_try_fgets(line, linemax, file, context, "0nf"); - line[strlen(line) - 1] = '\0'; - mod->name = try_malloc(strlen(line) + 1, f_name); - memcpy(mod->name, line, strlen(line) + 1); - * last_mod_ptr_ptr = mod; - last_mod_ptr_ptr = &mod->next; - err_try_fgets(line, linemax, file, context, "d"); - } - try_fclose(file, f_name); - set_cleanup_flag(CLEANUP_MAP_OBJECT_DEFS); -} - - - extern void free_map_object_defs(struct MapObjDef * mod_start) { if (NULL == mod_start) @@ -184,6 +117,7 @@ extern void free_map_objects(struct MapObj * mo_start) } free_map_objects(mo_start->owns); free_map_objects(mo_start->next); + free(mo_start->fov_map); free(mo_start); if (mo_start == world.map_objs) /* So add_map_objects()' NULL-delimited */ { /* map object iteration loop does not */ @@ -244,7 +178,7 @@ extern struct MapObjDef * get_map_object_def(uint8_t id) -extern void set_object_position(struct MapObj * mo, struct yx_uint16 pos) +extern void set_object_position(struct MapObj * mo, struct yx_uint8 pos) { mo->pos = pos; struct MapObj * owned = mo->owns;