extern void turn_over(struct World * world, char action)
{
+ char * err_open = "Error recording move: "
+ "Unable to open 'record_tmp' for appending.";
+ char * err_write = "Error recording move: "
+ "Trouble writing to opened 'record_tmp'.";
+ char * err_close = "Error recording move: "
+ "Unable to close opened 'record_tmp'.";
+ char * err_unl = "Error recording move: "
+ "Unable to unlink old 'record' file.";
+ char * err_move = "Error recording move: "
+ "Unable to rename 'record_tmp' to 'record'.";
+ char * recordfile_tmp = "record_tmp";
+ char * recordfile = "record";
if (1 == world->interactive)
{
- FILE * file = fopen("record", "a");
- exit_err(write_uint8(action, file), world, "Record writing failure.");
- fclose(file);
+ FILE * file = fopen(recordfile_tmp, "a");
+ exit_err(0 == file, world, err_open);
+ exit_err(write_uint8(action, file), world, err_write);
+ exit_err(fclose(file), world, err_close);
+ exit_err(unlink(recordfile), world, err_unl);
+ exit_err(rename(recordfile_tmp, recordfile), world, err_move);
}
+
world->turn++;
rrand_seed(world->seed * world->turn);
struct Monster * monster;
extern void save_game(struct World * world)
{
+ char * err_open = "Error saving game: "
+ "Unable to open 'savefile_tmp' for writing.";
+ char * err_write = "Error saving game: "
+ "Trouble writing to opened 'savefile_tmp'.";
+ char * err_close = "Error saving game: "
+ "Unable to close opened 'savefile_tmp'.";
+ char * err_unl = "Error saving game: "
+ "Unable to unlink old 'savefile'.";
+ char * err_move = "Error saving game: "
+ "Unable to rename 'savefile_tmp' to 'savefile'.";
char * savefile_tmp = "savefile_tmp";
- char * savefile = "savefile";
+ char * savefile = "savefile";
FILE * file = fopen(savefile_tmp, "w");
- exit_err(0 == file, world,
- "Error saving game: Unable to open new savefile for writing.");
+ 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_map_objects(world, world->monster, file)
|| write_map_objects(world, world->item, file))
{
- exit_err(1, world,
- "Error saving game: Trouble writing to opened new savefile.");
+ exit_err(1, world, err_write);
}
- exit_err(fclose(file), world,
- "Error saving game: Unable to close opened new savefile.");
+ exit_err(fclose(file), world, err_close);
if (!access(savefile, F_OK))
{
- exit_err(unlink(savefile), world,
- "Error saving game: Unable to unlink old savefile.");
+ exit_err(unlink(savefile), world, err_unl);
}
- exit_err(rename(savefile_tmp, savefile), world,
- "Error saving game: Unable to rename 'savefile_tmp' to "
- "'savefile'.");
+ exit_err(rename(savefile_tmp, savefile), world, err_move);
}