X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fmisc.c;h=cf69c8a8ecc91b7813c50d0585c10124b91e8c81;hb=a71cdad2127723325c5ab170c1cd1dfa7075b0a3;hp=8998d0a462f807f6485fae2ff752e1940033682b;hpb=c7cc7fe6896f27762e2dd9b65de57f81bd1f33d4;p=plomrogue diff --git a/src/misc.c b/src/misc.c index 8998d0a..cf69c8a 100644 --- a/src/misc.c +++ b/src/misc.c @@ -1,9 +1,9 @@ /* misc.c */ #include "misc.h" -#include /* for exit(), EXIT_SUCCESS define, calloc(), free() */ +#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,33 +11,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 "map_objects.h" /* for struct Monster, write_map_objects(), - * write_map_objects_monsterdata() - */ +#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 */ - - - -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 "rrand.h" /* for rrand(), rrand_seed() */ +#include "rexit.h" /* for exit_err() */ extern void textfile_sizes(FILE * file, uint16_t * linemax_p, @@ -74,22 +55,6 @@ extern void textfile_sizes(FILE * file, uint16_t * linemax_p, -extern uint16_t rrand(char use_seed, uint32_t new_seed) -{ - static uint32_t seed; - if (0 != use_seed) - { - seed = new_seed; - } - - /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */ - seed = ((seed * 1103515245) + 12345) % 2147483648; - - return (seed / 65536); /* TODO: Use bit-shifting for ignoring the less */ -} /* random least significant 16 bits. */ - - - extern void update_log(struct World * world, char * text) { static char * last_msg; @@ -150,11 +115,11 @@ 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++; - rrand(1, world->seed * world->turn); + rrand_seed(world->seed * world->turn); struct Monster * monster; for (monster = world->monster; monster != 0; @@ -168,15 +133,21 @@ extern void turn_over(struct World * world, char action) extern void save_game(struct World * world) { + char * err_msg = "Error saving game."; + 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->monster, file, write_map_objects_monsterdata); - write_map_objects (world->item, file, NULL); - fclose(file); + exit_err(0 == file, world, err_msg); + + uint8_t err; + err = write_uint32_bigendian(world->seed, file); + err = err | write_uint32_bigendian(world->turn, file); + err = err | write_uint16_bigendian(world->player->pos.y + 1, file); + err = err | write_uint16_bigendian(world->player->pos.x + 1, file); + err = err | write_uint8(world->player->hitpoints, file); + err = err | write_map_objects(world, world->monster, file); + err = err | write_map_objects(world, world->item, file); + exit_err(err, world, err_msg); + exit_err(fclose(file), world, err_msg); } @@ -241,20 +212,18 @@ extern struct yx_uint16 find_passable_pos(struct Map * map) struct yx_uint16 pos; for (pos.y = pos.x = 0; 0 == is_passable(map, pos);) { - pos.y = rrand(0, 0) % map->size.y; - pos.x = rrand(0, 0) % map->size.x; + pos.y = rrand() % map->size.y; + pos.x = rrand() % map->size.x; } return pos; } -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")) {