X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fmap_objects.c;h=4cd6cb0d8cae2a682196861a18e8a3292d3cd615;hb=c1a7e6cdb13cd7d883424afdf0fe08e9a10fbc28;hp=c4b90485068f1580b2d85f53f17b4bb8d6ecb017;hpb=b9082c113c43afe5c6a11c2b72f845ee2f8c6aea;p=plomrogue diff --git a/src/map_objects.c b/src/map_objects.c index c4b9048..4cd6cb0 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -2,8 +2,9 @@ #include "map_objects.h" #include /* for malloc(), calloc(), free(), atoi() */ +#include /* for uint8_t */ #include /* for FILE typedef */ -#include /* for strchr(), strlen(), memcpy() */ +#include /* for strchr(), strlen(), memcpy(), strtok() */ #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 */ @@ -98,24 +99,25 @@ extern void init_map_object_defs(struct World * world, char * filename) struct MonsterDef * * p_p_md = &world->monster_def; char * defline = malloc(linemax); char * line_p; + char * delim = " "; while (fgets(defline, linemax, file)) { mod.next = 0; - mod.id = atoi(defline); - line_p = strchr(defline, ' ') + 1; - mod.m_or_i = * line_p; - mod.mapchar = * (line_p + 2); + mod.id = atoi(strtok(defline, delim)); + mod.m_or_i = * strtok(NULL, delim); + mod.mapchar = * strtok(NULL, delim); if ('i' == mod.m_or_i) { - line_p = line_p + 5; + line_p = strtok(NULL, delim); } else { - md.hitpoints_start = atoi (line_p + 4); - line_p = strchr (line_p + 4, ' ') + 1; + md.corpse_id = atoi(strtok(NULL, delim)); + md.hitpoints_start = atoi(strtok(NULL, delim)); + line_p = strtok(NULL, delim); } - mod.desc = calloc (strlen (line_p), sizeof(char)); - memcpy (mod.desc, line_p, strlen(line_p) - 1); + mod.desc = calloc(strlen(line_p), sizeof(char)); + memcpy(mod.desc, line_p, strlen(line_p) - 1); if ('i' == mod.m_or_i) { id.map_obj_def = mod; @@ -133,7 +135,32 @@ extern void init_map_object_defs(struct World * world, char * filename) } free(defline); fclose(file); -}; +} + + + +extern void free_item_defs(struct ItemDef * id_start) +{ + if (0 != id_start->map_obj_def.next) + { + free_item_defs((struct ItemDef *) id_start->map_obj_def.next); + } + free(id_start->map_obj_def.desc); + free(id_start); +} + + + + +extern void free_monster_defs(struct MonsterDef * md_start) +{ + if (0 != md_start->map_obj_def.next) + { + free_monster_defs((struct MonsterDef *) md_start->map_obj_def.next); + } + free(md_start->map_obj_def.desc); + free(md_start); +} @@ -142,19 +169,24 @@ extern uint8_t write_map_objects(struct World * world, void * start, { struct MapObj * map_obj; struct MapObjDef * mod; - uint8_t fail = 0; for (map_obj = start; map_obj != 0; map_obj = map_obj->next) { - fail = fail | write_uint8(map_obj->type, file); - fail = fail | write_uint16_bigendian(map_obj->pos.y + 1, file); - fail = fail | write_uint16_bigendian(map_obj->pos.x + 1, file); + if ( write_uint8(map_obj->type, file) + || write_uint16_bigendian(map_obj->pos.y + 1, file) + || write_uint16_bigendian(map_obj->pos.x + 1, file)) + { + return 1; + } mod = get_map_obj_def(world, map_obj->type); if ('m' == mod->m_or_i) { - fail = fail | write_map_objects_monsterdata(map_obj, file); + if (write_map_objects_monsterdata(map_obj, file)) + { + return 1; + } } } - return (fail | write_uint16_bigendian(0, file)); + return write_uint16_bigendian(0, file); } @@ -168,17 +200,22 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) char first = 1; long pos; uint16_t read_uint16 = 0; - uint8_t fail = 0; while (1) { pos = ftell(file); - fail = fail | read_uint16_bigendian(file, &read_uint16); + if (read_uint16_bigendian(file, &read_uint16)) + { + return 1; + } if (0 == read_uint16) { break; } fseek(file, pos, SEEK_SET); - fail = fail | read_uint8(file, &type); + if (read_uint8(file, &type)) + { + return 1; + } mod = get_map_obj_def(world, type); if ('m' == mod->m_or_i) { @@ -190,28 +227,34 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file) } map_obj = get_next_map_obj(start, &first, size, map_obj); map_obj->type = type; - fail = fail | read_uint16_bigendian(file, &map_obj->pos.y); - fail = fail | read_uint16_bigendian(file, &map_obj->pos.x); + if ( read_uint16_bigendian(file, &map_obj->pos.y) + || read_uint16_bigendian(file, &map_obj->pos.x)) + { + return 1; + } map_obj->pos.y--; map_obj->pos.x--; if ('m' == mod->m_or_i) { - fail = fail | read_map_objects_monsterdata(map_obj, file); + if (read_map_objects_monsterdata(map_obj, file)) + { + return 1; + } } } if (!first) { map_obj->next = 0; } - return fail; + return 0; } extern void * build_map_objects(struct World * world, void * start, char def_id, - unsigned char n) + uint8_t n) { - unsigned char i; + uint8_t i; struct MapObj * mo; char first = 1; struct MapObjDef * mod = get_map_obj_def(world, def_id);