From: Christian Heller Date: Sat, 31 Aug 2013 00:35:23 +0000 (+0200) Subject: While adding cleaning up / freeing of map objects, fixed bug that initialized map... X-Git-Tag: tce~991 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/conditions?a=commitdiff_plain;h=6907fb5902bd50d590b6886e6b30090db5b47444;p=plomrogue While adding cleaning up / freeing of map objects, fixed bug that initialized map objects twice if game was started in first round, but savefile already existed. --- diff --git a/src/main.c b/src/main.c index 27613bf..786e72e 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,7 @@ int main(int argc, char *argv[]) { struct World world; + world.turn = 0; /* Turns to 1 when map and objects are initalized. */ init_command_db(&world); set_cleanup_flag(CLEANUP_COMMAND_DB); @@ -128,9 +129,11 @@ int main(int argc, char *argv[]) { exit_err(1, &world, err_r); } + set_cleanup_flag(CLEANUP_MAP_OBJECTS); exit_err(fclose(file), &world, err_c); player.pos.y--; player.pos.x--; + world.turn = 1; } /* For non-interactive mode, try to load world state from record file. */ @@ -140,7 +143,6 @@ int main(int argc, char *argv[]) "opening file 'record' for reading."; err_r = "Trouble loading record file (read_uint32_bigendian() in " "main()) / reading from opened file 'record'."; - world.turn = 1; if (0 == world.interactive) { file = fopen(recordfile, "r"); @@ -172,7 +174,6 @@ int main(int argc, char *argv[]) } } - /* Generate map from seed and, if newly generated world, start positions of * actors. */ @@ -180,7 +181,7 @@ int main(int argc, char *argv[]) struct Map map = init_map(); world.map = ↦ set_cleanup_flag(CLEANUP_MAP); - if (1 == world.turn) + if (0 == world.turn) { player.pos = find_passable_pos(world.map); void * foo; @@ -189,6 +190,8 @@ int main(int argc, char *argv[]) build_map_objects(&world, foo, 3, 1 + rrand() % 3); foo = build_map_objects(&world, &world.item, 4, 1 + rrand() % 3); build_map_objects(&world, foo, 5, 1 + rrand() % 3); + set_cleanup_flag(CLEANUP_MAP_OBJECTS); + world.turn = 1; } /* Initialize window system and windows. */ diff --git a/src/map_objects.c b/src/map_objects.c index 4cd6cb0..f0f002c 100644 --- a/src/map_objects.c +++ b/src/map_objects.c @@ -290,7 +290,29 @@ 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->map_obj.next) + { + free_items((struct Item *) item->map_obj.next); + } + free(item); +} + + + +extern void free_monsters(struct Monster * monster) +{ + if (0 != monster->map_obj.next) + { + 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; diff --git a/src/map_objects.h b/src/map_objects.h index 83ae7c5..bddd5ff 100644 --- a/src/map_objects.h +++ b/src/map_objects.h @@ -104,6 +104,9 @@ extern uint8_t read_map_objects(struct World * world, void * start, FILE * file); +/* Free items / monsters in map object chain starting at "item" / "monster". */ +extern void free_items(struct Item * item); +extern void free_monsters(struct Monster * monster); /* Get pointer to the map object definition of identifier "def_id". */ extern struct MapObjDef * get_map_obj_def(struct World * world, char def_id); diff --git a/src/rexit.c b/src/rexit.c index e0d029f..2907705 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -70,6 +70,11 @@ static void cleanup(struct World * world) free_item_defs(world->item_def); free_monster_defs(world->monster_def); } + if (cleanup_flags & CLEANUP_MAP_OBJECTS) + { + free_items(world->item); + free_monsters(world->monster); + } } diff --git a/src/rexit.h b/src/rexit.h index 6336788..f376e5e 100644 --- a/src/rexit.h +++ b/src/rexit.h @@ -26,11 +26,12 @@ enum cleanup_flag CLEANUP_KEYBINDINGS = 0x0004, CLEANUP_LOG = 0x0008, CLEANUP_COMMAND_DB = 0x0010, - CLEANUP_MAP_OBJECT_DEFS = 0x0020, - CLEANUP_WIN_INFO = 0x0040, - CLEANUP_WIN_LOG = 0x0080, - CLEANUP_WIN_MAP = 0x0100, - CLEANUP_WIN_KEYS = 0x0200 + CLEANUP_MAP_OBJECTS = 0x0020, + CLEANUP_MAP_OBJECT_DEFS = 0x0040, + CLEANUP_WIN_INFO = 0x0080, + CLEANUP_WIN_LOG = 0x0100, + CLEANUP_WIN_MAP = 0x0200, + CLEANUP_WIN_KEYS = 0x0400 }; extern void set_cleanup_flag(enum cleanup_flag flag);