X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fmap_objects.c;h=8b84ac136fcd5a07589ebf591b92227fbed805d1;hb=2f0b15e05b0df7e10a2d0bb04ce4648455d980a8;hp=4cd6cb0d8cae2a682196861a18e8a3292d3cd615;hpb=c1a7e6cdb13cd7d883424afdf0fe08e9a10fbc28;p=plomrogue diff --git a/src/map_objects.c b/src/map_objects.c index 4cd6cb0..8b84ac1 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -8,11 +8,14 @@ #include "readwrite.h" /* for [read/write]_uint[8/16/23][_bigendian]() */ #include "misc.h" /* for textfile_sizes(), find_passable_pos() */ #include "main.h" /* for World struct */ +#include "rexit.h" /* for err_exit() */ /* Return pointer to newly allocated map object struct of size "size". If first * in map object chain ("first" pointing to !0), point "start" to it. + * + * Returns NULL instead of MapObj pointer if malloc() failed. */ static struct MapObj * get_next_map_obj(void * start, char * first, size_t size, struct MapObj * map_obj); @@ -87,17 +90,24 @@ static uint8_t read_map_objects_monsterdata (void * start, FILE * file) extern void init_map_object_defs(struct World * world, char * filename) { - world->item_def = 0; - world->monster_def = 0; + char * err_o = "Trouble in init_map_object_defs() with fopen()."; + char * err_s = "Trouble in init_map_object_defs() with textfile_sizes()."; + char * err_m = "Trouble in init_map_object_defs() with malloc()/calloc()."; + char * err_c = "Trouble in init_map_object_defs() with fclose()."; + FILE * file = fopen(filename, "r"); + exit_err(NULL == file, world, err_o); uint16_t linemax; - textfile_sizes (file, &linemax, NULL); + exit_err(textfile_sizes(file, &linemax, NULL), world, err_s); + struct MapObjDef mod; struct ItemDef id; struct MonsterDef md; + world->item_def = 0; + world->monster_def = 0; struct ItemDef * * p_p_id = &world->item_def; struct MonsterDef * * p_p_md = &world->monster_def; - char * defline = malloc(linemax); + char defline[linemax]; char * line_p; char * delim = " "; while (fgets(defline, linemax, file)) @@ -117,11 +127,13 @@ extern void init_map_object_defs(struct World * world, char * filename) line_p = strtok(NULL, delim); } mod.desc = calloc(strlen(line_p), sizeof(char)); + exit_err(NULL == mod.desc, world, err_m); memcpy(mod.desc, line_p, strlen(line_p) - 1); if ('i' == mod.m_or_i) { id.map_obj_def = mod; * p_p_id = malloc(sizeof(struct ItemDef)); + exit_err(NULL == p_p_id, world, err_m); * * p_p_id = id; p_p_id = (struct ItemDef * *) * p_p_id; } @@ -129,12 +141,13 @@ extern void init_map_object_defs(struct World * world, char * filename) { md.map_obj_def = mod; * p_p_md = malloc(sizeof(struct MonsterDef)); + exit_err(NULL == p_p_md, world, err_m); * * p_p_md = md; p_p_md = (struct MonsterDef * *) * p_p_md; } } - free(defline); - fclose(file); + + exit_err(fclose(file), world, err_c); } @@ -193,6 +206,7 @@ extern uint8_t write_map_objects(struct World * world, void * start, extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) { + char * err = "Trouble in read_map_objects() with get_next_map_obj()."; struct MapObj * map_obj; struct MapObjDef * mod; size_t size; @@ -226,6 +240,7 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) size = sizeof(struct Item); } map_obj = get_next_map_obj(start, &first, size, map_obj); + exit_err(NULL == map_obj, world, err); map_obj->type = type; if ( read_uint16_bigendian(file, &map_obj->pos.y) || read_uint16_bigendian(file, &map_obj->pos.x)) @@ -254,6 +269,7 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) extern void * build_map_objects(struct World * world, void * start, char def_id, uint8_t n) { + char * err = "Trouble in build_map_objects() with get_next_map_obj()."; uint8_t i; struct MapObj * mo; char first = 1; @@ -270,6 +286,7 @@ extern void * build_map_objects(struct World * world, void * start, char def_id, for (i = 0; i < n; i++) { mo = get_next_map_obj(start, &first, size, mo); + exit_err(NULL == mo, world, err); mo->pos = find_passable_pos(world->map); if ('i' == mod->m_or_i) { @@ -290,7 +307,31 @@ extern void * build_map_objects(struct World * world, void * start, char def_id, -extern struct MapObjDef * get_map_obj_def (struct World * world, char def_id) +extern void free_items(struct Item * item) +{ + if (0 == item) + { + return; + } + free_items((struct Item *) item->map_obj.next); + free(item); +} + + + +extern void free_monsters(struct Monster * monster) +{ + if (0 == monster) + { + return; + } + free_monsters((struct Monster *) monster->map_obj.next); + free(monster); +} + + + +extern struct MapObjDef * get_map_obj_def(struct World * world, char def_id) { struct MapObjDef * d = NULL; for (d = (struct MapObjDef *) world->monster_def;