X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=src%2Fmisc.c;h=711e049db2c067527924aaad8fab26b67ab394f8;hb=26fb22669c47b1191264d840ad240f2b901327d5;hp=6aeb8b45303a229b120593848a5a8ebd5c9c3241;hpb=1afe61cf5429407b416177893c7c86424ba31ff4;p=plomrogue diff --git a/src/misc.c b/src/misc.c index 6aeb8b4..711e049 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,9 +1,11 @@ /* misc.c */ #include "misc.h" -#include /* for exit(), EXIT_SUCCESS define, calloc(), free() */ +#include /* for rename() */ +#include /* for unlink(), acess() */ +#include /* for calloc(), free() */ #include /* for strlen(), strcmp(), memcpy() */ -#include /* for endwin() */ +#include /* for uint8_t */ #include "windows.h" /* for suspend_win(), append_win(), reset_pad_offset(), * resize_active_win(), cycle_active_win(), * shift_active_win(), struct Win, struct WinMeta @@ -11,32 +13,14 @@ #include "keybindings.h" /* for get_action_key(), save_keybindings(), * keyswin_move_selection(), keyswin_mod_key() */ -#include "readwrite.h" /* for write_uint16_bigendian(), write_uint32_bigendian() - */ +#include "readwrite.h" /* for [read/write]_uint[8/16/32][_bigendian]() */ #include "map_objects.h" /* for struct Monster, write_map_objects(), */ #include "map_object_actions.h" /* for is_passable(), move_monster() */ #include "map.h" /* for map_scroll(),map_center_player(), Map struct,dir enum */ #include "main.h" /* for World struct */ #include "yx_uint16.h" /* for yx_uint16 */ #include "rrand.h" /* for rrand(), rrand_seed() */ - - - -extern void exit_game(struct World * world, struct Map * map) -{ - endwin(); - free(map->cells); - uint16_t key; - for (key = 0; key <= world->keyswindata->max; key++) - { - free(world->keybindings[key].name); - } - free(world->keybindings); - free(world->keyswindata); - free(world->log); - exit (EXIT_SUCCESS); -} - +#include "rexit.h" /* for exit_err() */ extern void textfile_sizes(FILE * file, uint16_t * linemax_p, @@ -133,7 +117,7 @@ extern void turn_over(struct World * world, char action) if (1 == world->interactive) { FILE * file = fopen("record", "a"); - fputc(action, file); + exit_err(write_uint8(action, file), world, "Record writing failure."); fclose(file); } world->turn++; @@ -151,15 +135,36 @@ extern void turn_over(struct World * world, char action) extern void save_game(struct World * world) { - FILE * file = fopen("savefile", "w"); - write_uint32_bigendian(world->seed, file); - write_uint32_bigendian(world->turn, file); - write_uint16_bigendian(world->player->pos.y + 1, file); - write_uint16_bigendian(world->player->pos.x + 1, file); - fputc(world->player->hitpoints, file); - write_map_objects(world, world->monster, file); - write_map_objects(world, world->item, file); - fclose(file); + char * savefile_tmp = "savefile_tmp"; + char * savefile = "savefile"; + char * err_open = "Error saving game: " + "Unable to open 'savefile_new' for writing."; + char * err_write = "Error saving game: " + "Trouble writing to opened 'savefile_new'."; + char * err_close = "Error saving game: " + "Unable to close opened 'savefile_new'."; + char * err_unl = "Error saving game: " + "Unable to unlink old 'savefile'."; + char * err_move = "Error saving game: " + "Unable to rename 'savefile_tmp' to 'savefile'."; + FILE * file = fopen(savefile_tmp, "w"); + exit_err(0 == file, world, err_open); + if ( write_uint32_bigendian(world->seed, file) + || write_uint32_bigendian(world->turn, file) + || write_uint16_bigendian(world->player->pos.y + 1, file) + || write_uint16_bigendian(world->player->pos.x + 1, file) + || write_uint8(world->player->hitpoints, file) + || write_map_objects(world, world->monster, file) + || write_map_objects(world, world->item, file)) + { + exit_err(1, world, err_write); + } + exit_err(fclose(file), world, err_close); + if (!access(savefile, F_OK)) + { + exit_err(unlink(savefile), world, err_unl); + } + exit_err(rename(savefile_tmp, savefile), world, err_move); } @@ -232,12 +237,10 @@ extern struct yx_uint16 find_passable_pos(struct Map * map) -extern unsigned char meta_keys(int key, struct World * world, - struct WinMeta * win_meta, - struct Win * win_keys, - struct Win * win_map, - struct Win * win_info, - struct Win * win_log) +extern uint8_t meta_keys(int key, struct World * world, + struct WinMeta * win_meta, struct Win * win_keys, + struct Win * win_map, struct Win * win_info, + struct Win * win_log) { if (key == get_action_key(world->keybindings, "quit")) {