From: Christian Heller Date: Wed, 27 Nov 2013 03:07:02 +0000 (+0100) Subject: Refactorisation and comment improvements in misc library. X-Git-Tag: tce~903 X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/conditions?a=commitdiff_plain;h=8f60274cc94ea4b2d3a0bb71169e50b0d06ae629;p=plomrogue Refactorisation and comment improvements in misc library. --- diff --git a/src/control.c b/src/control.c index 3116937..26b0e2d 100644 --- a/src/control.c +++ b/src/control.c @@ -188,9 +188,9 @@ extern uint8_t player_control_by_key(int key) extern uint8_t player_control_by_id(int action) { if ( try_player_cmd(action, "wait", "wait", 0) - || try_player_cmd(action, "drop", "drop", world.inventory_select) + || try_player_cmd(action, "drop", "drop", world.inventory_sel) || try_player_cmd(action, "pick", "pick_up", 0) - || try_player_cmd(action, "use", "use", world.inventory_select) + || try_player_cmd(action, "use", "use", world.inventory_sel) || try_player_cmd(action, "player_u", "move", 'N') || try_player_cmd(action, "player_d", "move", 'S') || try_player_cmd(action, "player_r", "move", 'E') diff --git a/src/draw_wins.c b/src/draw_wins.c index 624a240..b589a30 100644 --- a/src/draw_wins.c +++ b/src/draw_wins.c @@ -386,13 +386,13 @@ extern void draw_win_inventory(struct Win * win) add_line(win, "(none)", 0, 0); return; } - win->center.y = world.inventory_select; + win->center.y = world.inventory_sel; struct MapObj * owned = player->owns; uint8_t y; for (y = 0; NULL != owned; y++) { attr_t attri = 0; - if (y == world.inventory_select) + if (y == world.inventory_sel) { attri = A_REVERSE; } diff --git a/src/main.c b/src/main.c index 2d09636..74ef761 100644 --- a/src/main.c +++ b/src/main.c @@ -159,7 +159,7 @@ int main(int argc, char *argv[]) win_map->center = player->pos; /* Initialize player's inventory selection index to start position. */ - world.inventory_select = 0; + world.inventory_sel = 0; /* Replay mode. */ int key; @@ -179,7 +179,7 @@ int main(int argc, char *argv[]) if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - world.inventory_select = try_fgetc_noeof(file, f_name); + world.inventory_sel = try_fgetc_noeof(file, f_name); } player_control_by_id(action); } @@ -203,7 +203,7 @@ int main(int argc, char *argv[]) if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - world.inventory_select = try_fgetc_noeof(file, f_name); + world.inventory_sel = try_fgetc_noeof(file, f_name); } player_control_by_id(action); } diff --git a/src/main.h b/src/main.h index 5ef6a0e..1ade736 100644 --- a/src/main.h +++ b/src/main.h @@ -36,7 +36,7 @@ struct World uint8_t map_obj_count; /* Counts map objects generated so far. */ struct MapObjDef * map_obj_defs; /* Map object type definitions chain. */ struct MapObj * map_objs; /* Pointer to map objects chain start. */ - uint8_t inventory_select; /* Player's inventory selection index. */ + uint8_t inventory_sel; /* Player's inventory selection index. */ struct MapObjAct * map_obj_acts; /* Pointer to map object actions chain. */ } world; diff --git a/src/map_object_actions.c b/src/map_object_actions.c index 9541214..7059863 100644 --- a/src/map_object_actions.c +++ b/src/map_object_actions.c @@ -27,7 +27,7 @@ static uint8_t try_func_name(struct MapObjAct * moa, static void actor_hits_actor(struct MapObj * hitter, struct MapObj * hitted); /* Bonus stuff to actor_*() to happen if actor==player. Mostly writing of log - * messages; _pick and _drop also decrement world.inventory_select by 1 if >0. + * messages; _pick and _drop also decrement world.inventory_sel by 1 if >0. */ static void playerbonus_wait(); static void playerbonus_move(char d, uint8_t passable); @@ -135,9 +135,9 @@ static void playerbonus_drop(uint8_t owns_none) else { update_log("\nYou drop an object."); - if (0 < world.inventory_select) + if (0 < world.inventory_sel) { - world.inventory_select--; + world.inventory_sel--; } } } @@ -171,9 +171,9 @@ static void playerbonus_use(uint8_t no_object, uint8_t wrong_object) else { update_log("\nYou consume MAGIC MEAT."); - if (0 < world.inventory_select) + if (0 < world.inventory_sel) { - world.inventory_select--; + world.inventory_sel--; } } } diff --git a/src/misc.c b/src/misc.c index 97c9fa6..122f404 100644 --- a/src/misc.c +++ b/src/misc.c @@ -27,8 +27,7 @@ extern uint16_t rrand() -{ - /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */ +{ /* Constants as recommended by POSIX.1-2001 (see man page rand(3)). */ world.seed = ((world.seed * 1103515245) + 12345) % 4294967296; return (world.seed >> 16); /* Ignore less random least significant bits. */ } @@ -169,17 +168,17 @@ extern void update_log(char * text) -extern uint16_t center_offset(uint16_t pos, uint16_t mapsize, +extern uint16_t center_offset(uint16_t position, uint16_t mapsize, uint16_t framesize) { uint16_t offset = 0; if (mapsize > framesize) { - if (pos > framesize / 2) + if (position > framesize / 2) { - if (pos < mapsize - (framesize / 2)) + if (position < mapsize - (framesize / 2)) { - offset = pos - (framesize / 2); + offset = position - (framesize / 2); } else { @@ -213,7 +212,7 @@ extern void turn_over(char action) if ( is_command_id_shortdsc(action, "drop") || is_command_id_shortdsc(action, "use")) { - try_fputc(world.inventory_select, file_new, f_name); + try_fputc(world.inventory_sel, file_new, f_name); } try_fclose_unlink_rename(file_new, recordfile_tmp, recordfile, f_name); } @@ -316,10 +315,7 @@ extern void nav_inventory(char dir) { if ('u' == dir) { - if (world.inventory_select > 0) - { - world.inventory_select--; - } + world.inventory_sel = world.inventory_sel - (world.inventory_sel > 0); return; } struct MapObj * player = get_player(); @@ -330,8 +326,5 @@ extern void nav_inventory(char dir) } uint8_t n_owned = 0; for (; NULL != owned->next; owned = owned->next, n_owned++); - if (world.inventory_select < n_owned) - { - world.inventory_select++; - } + world.inventory_sel = world.inventory_sel + (world.inventory_sel < n_owned); } diff --git a/src/misc.h b/src/misc.h index 24f36b5..80bb1cd 100644 --- a/src/misc.h +++ b/src/misc.h @@ -20,52 +20,40 @@ struct Map; */ extern uint16_t rrand(); -/* Wrappers to malloc(), calloc() from function called "f" calling exit_err() - * with trouble_msg() error message if necessary. - */ +/* Wrappers to malloc(), calloc() from function "f"; exit_trouble() on error. */ extern void * try_malloc(size_t size, char * f); extern void * try_calloc(size_t nmemb, size_t size, char * f); -/* Check if tempfile "path" exists, and if so, exit with explanation that. */ -extern void check_tempfile(char * path); - /* If one and only one of files at "p1", "p2" exists, fail with explanation. */ extern void check_files_xor(char * p1, char * p2); +/* Check if tempfile "path" exists, and if so, exit with explanation that. */ +extern void check_tempfile(char * path); + /* Save / load / unload (free) / reload interface configuration data. */ extern void save_interface_conf(); extern void load_interface_conf(); extern void unload_interface_conf(); extern void reload_interface_conf(); -/* Update game log by appending "text", or by appending a "." if "text" is the - * same as the last one passed. - */ +/* Append "text" to game log, or a "." if "text" is the same as the last one. */ extern void update_log(char * text); -/* Return the offset necessary to center a map of "mapsize" on position "pos" in - * a frame of "framesize. - */ -extern uint16_t center_offset(uint16_t pos, +/* Return offset nto center map of "mapsize" on "position" in "framesize". */ +extern uint16_t center_offset(uint16_t position, uint16_t mapsize, uint16_t framesize); -/* Record last player "action" in game record file "record, increment the game - * turn and trigger enemy movement. - */ +/* Record "action" in record file, do all movements until player's next turn. */ extern void turn_over(char action); /* Save or load current game data to / from file "savefile". */ extern void save_game(); extern void load_game(); -/* Return a random position on the map "map" that is passable (as determined by - * is_passable(). - */ +/* Return random passable (as by is_passable()) position on "map". */ extern struct yx_uint16 find_passable_pos(struct Map * map); -/* Navigate (as far as possible) up (dir=='u') or (else) down in player's - * inventory selection. - */ +/* Move world.inventory_sel up ("dir"="u") or down (else) as far as possible. */ extern void nav_inventory(char dir); diff --git a/src/readwrite.c b/src/readwrite.c index 06a6344..e6660ae 100644 --- a/src/readwrite.c +++ b/src/readwrite.c @@ -1,6 +1,7 @@ /* readwrite.c */ #include "readwrite.h" +#include /* for size_t */ #include /* for FILE typedef, fopen(), fgetc(), fputc(), fseek(), * sprintf(), fwrite(), ferror() */ @@ -8,7 +9,6 @@ #include /* for strlen() */ #include /* for unlink() */ #include "rexit.h" /* for exit_err(), exit_trouble() */ -#include "main.h" /* for world global */