From: Christian Heller Date: Thu, 29 Aug 2013 01:15:25 +0000 (+0200) Subject: Commands are now to be managed by a Command DB, not by passing around arbitrary strings. X-Git-Tag: tce~1011 X-Git-Url: https://plomlompom.com/repos/?a=commitdiff_plain;h=58f0067336c9239750009b3f35d18031bec8f7b5;p=plomrogue Commands are now to be managed by a Command DB, not by passing around arbitrary strings. --- diff --git a/config/commands b/config/commands new file mode 100644 index 0000000..f946af0 --- /dev/null +++ b/config/commands @@ -0,0 +1,29 @@ +1 quit quit +2 save_keys save keys +3 scrl_l scroll pad left +4 scrl_r scroll pad right +5 keys_d keys nav down +6 keys_u keys nav up +7 keys_m keys mod +8 to_keywin toggle keys window +9 to_mapwin toggle map window +10 to_infowin toggle info window +11 to_logwin toggle log window +12 cyc_win_f cycle forwards +13 cyc_win_b cycle backwards +14 shift_f shift forwards +15 shift_b shift backwards +16 grow_h grow horizontally +17 shri_h shrink horizontally +18 grow_v grow vertically +19 shri_v shrink vertically +20 map_u map up +21 map_d map down +22 map_l map left +23 map_r map right +24 map_c map center player +25 player_u player up +26 player_d player down +27 player_l player left +28 player_r player right +29 wait wait / next turn diff --git a/config/keybindings b/config/keybindings index 6884e61..2718818 100644 --- a/config/keybindings +++ b/config/keybindings @@ -1,29 +1,29 @@ -81 quit -75 save keys -260 scroll pad left -261 scroll pad right -258 keys nav down -259 keys nav up -10 keys mod -265 toggle keys window -266 toggle map window -267 toggle info window -268 toggle log window -62 cycle forwards -60 cycle backwards -121 shift forwards -89 shift backwards -42 grow horizontally -95 shrink horizontally -43 grow vertically -45 shrink vertically -119 map up -120 map down -97 map left -100 map right -115 map center player -87 player up -88 player down -65 player left -68 player right -83 wait / next turn +90 quit +75 save_keys +260 scrl_l +261 scrl_r +258 keys_d +259 keys_u +10 keys_m +265 to_keywin +266 to_mapwin +267 to_infowin +268 to_logwin +62 cyc_win_f +60 cyc_win_b +121 shift_f +89 shift_b +42 grow_h +95 shri_h +43 grow_v +45 shri_v +119 map_u +120 map_d +97 map_l +100 map_r +115 map_c +87 player_u +88 player_d +65 player_l +68 player_r +83 wait diff --git a/src/command_db.c b/src/command_db.c new file mode 100644 index 0000000..cbf6959 --- /dev/null +++ b/src/command_db.c @@ -0,0 +1,83 @@ +/* command.c */ + +#include "command_db.h" +#include /* for malloc() */ +#include /* for FILE typedef, fopen(), fclose(), fgets() */ +#include /* for uint8_t */ +#include /* for strlen(), strtok() */ +#include "main.h" /* for World struct */ +#include "rexit.h" /* for exit_err() */ +#include "misc.h" /* for textfile_sizes() */ + + + +/* Build string pointed to by "ch_ptr" from next token delimited by "delim", + * exit_err() with "err" as error message on malloc() failure. + */ +static void copy_tokenized_string(struct World * world, + char ** ch_ptr, char * delim, char * err) +{ + char * dsc_ptr = strtok(NULL, delim); + * ch_ptr = malloc(strlen(dsc_ptr) + 1); + exit_err(NULL == * ch_ptr, world, err); + memcpy(* ch_ptr, dsc_ptr, strlen(dsc_ptr) + 1); +} + + + +extern char * get_command_longdsc(struct World * world, char * dsc_short) +{ + struct Command * cmd_ptr = world->cmd_db->cmds; + while (1) + { + if (0 == strcmp(dsc_short, cmd_ptr->dsc_short)) + { + return cmd_ptr->dsc_long; + } + cmd_ptr = &cmd_ptr[1]; + } +} + + + +extern void init_command_db(struct World * world) +{ + char * err = "Trouble in init_cmds() with fopen() on file 'commands'."; + FILE * file = fopen("config/commands", "r"); + exit_err(NULL == file, world, err); + uint16_t lines, linemax; + textfile_sizes(file, &linemax, &lines); + err = "Trouble in init_cmds() with malloc()."; + char * line = malloc(linemax); + exit_err(NULL == line, world, err); + struct Command * cmds = malloc(lines * sizeof(struct Command)); + exit_err(NULL == line, world, err); + uint8_t i = 0; + while (fgets(line, linemax, file)) + { + cmds[i].id = atoi(strtok(line, " ")); + copy_tokenized_string(world, &cmds[i].dsc_short, " ", err); + copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err); + i++; + } + world->cmd_db = malloc(sizeof(struct CommandDB)); + world->cmd_db->cmds = cmds; + world->cmd_db->n = lines; + err = "Trouble in init_cmds() with fclose() on file 'commands'."; + exit_err(fclose(file), world, err); +} + + + +extern void free_command_db(struct World * world) +{ + uint8_t i = 0; + while (i < world->cmd_db->n) + { + free(world->cmd_db->cmds[i].dsc_short); + free(world->cmd_db->cmds[i].dsc_long); + i++; + } + free(world->cmd_db->cmds); + free(world->cmd_db); +} diff --git a/src/command_db.h b/src/command_db.h new file mode 100644 index 0000000..efa342a --- /dev/null +++ b/src/command_db.h @@ -0,0 +1,48 @@ +/* command.h + * + * The Command DB collects all commands the user can give. It only contains + * identifiers and descriptions of commands, not the functions executing these + * commands. Coupling with those happens elsewhere. + */ + +#ifndef COMMAND_DB_H +#define COMMAND_DB_H + + + +#include /* for uint8_t */ +struct World; + + + +struct Command +{ + uint8_t id; /* unique identifier of command */ + char * dsc_short; /* short string name of command to be used internally */ + char * dsc_long; /* long string description of command for the user */ +}; + +struct CommandDB +{ + uint8_t n; /* number of Command structs in database*/ + struct Command * cmds; /* pointer to first Command struct in database */ +}; + + + +/* Give short description of command ("dsc_short"), get long descrption. */ +extern char * get_command_longdsc(struct World * world, char * dsc_short); + + + +/* Read in CommandDB from file "config/commands" to world.cmd_db. */ +extern void init_command_db(struct World * world); + + + +/* Free all memory allocated with init_command_db. */ +extern void free_command_db(struct World * world); + + + +#endif diff --git a/src/control.c b/src/control.c index 35a8c31..9915891 100644 --- a/src/control.c +++ b/src/control.c @@ -46,23 +46,23 @@ extern void record_control(int action, struct World * world) extern uint8_t player_control(int key, struct World * world) { - if (key == get_action_key(world->keybindings, "player up")) + if (key == get_action_key(world->keybindings, "player_u")) { move_player(world, NORTH); } - else if (key == get_action_key(world->keybindings, "player right")) + else if (key == get_action_key(world->keybindings, "player_r")) { move_player(world, EAST); } - else if (key == get_action_key(world->keybindings, "player down")) + else if (key == get_action_key(world->keybindings, "player_d")) { move_player(world, SOUTH); } - else if (key == get_action_key(world->keybindings, "player left")) + else if (key == get_action_key(world->keybindings, "player_l")) { move_player(world, WEST); } - else if (key == get_action_key(world->keybindings, "wait / next turn")) + else if (key == get_action_key(world->keybindings, "wait")) { player_wait(world); } @@ -90,95 +90,95 @@ extern uint8_t meta_control(int key, struct World * world) { return 1; } - else if (key == get_action_key(world->keybindings, "scroll pad right")) + else if (key == get_action_key(world->keybindings, "scrl_r")) { scroll_pad(win_meta, '+'); } - else if (key == get_action_key(world->keybindings, "scroll pad left")) + else if (key == get_action_key(world->keybindings, "scrl_l")) { scroll_pad(win_meta, '-'); } - else if (key == get_action_key(world->keybindings, "toggle keys window")) + else if (key == get_action_key(world->keybindings, "to_keywin")) { exit_err(toggle_window(win_meta, win_keys), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "toggle map window")) + else if (key == get_action_key(world->keybindings, "to_mapwin")) { exit_err(toggle_window(win_meta, win_map), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "toggle info window")) + else if (key == get_action_key(world->keybindings, "to_infowin")) { exit_err(toggle_window(win_meta, win_info), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "toggle log window")) + else if (key == get_action_key(world->keybindings, "to_logwin")) { exit_err(toggle_window(win_meta, win_log), world, err_toggle); } - else if (key == get_action_key(world->keybindings, "cycle forwards")) + else if (key == get_action_key(world->keybindings, "cyc_win_f")) { cycle_active_win(win_meta, 'f'); } - else if (key == get_action_key(world->keybindings, "cycle backwards")) + else if (key == get_action_key(world->keybindings, "cyc_win_b")) { cycle_active_win(win_meta, 'b'); } - else if (key == get_action_key(world->keybindings, "shift forwards")) + else if (key == get_action_key(world->keybindings, "shift_f")) { exit_err(shift_active_win(win_meta, 'f'), world, err_shift); } - else if (key == get_action_key(world->keybindings, "shift backwards")) + else if (key == get_action_key(world->keybindings, "shift_b")) { exit_err(shift_active_win(win_meta, 'b'), world, err_shift); } - else if (key == get_action_key(world->keybindings, "grow horizontally")) + else if (key == get_action_key(world->keybindings, "grow_h")) { exit_err(growshrink_active_window(win_meta, '*'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "shrink horizontally")) + else if (key == get_action_key(world->keybindings, "shri_h")) { exit_err(growshrink_active_window(win_meta, '_'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "grow vertically")) + else if (key == get_action_key(world->keybindings, "grow_v")) { exit_err(growshrink_active_window(win_meta, '+'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "shrink vertically")) + else if (key == get_action_key(world->keybindings, "shri_v")) { exit_err(growshrink_active_window(win_meta, '-'), world, err_resize); } - else if (key == get_action_key(world->keybindings, "save keys")) + else if (key == get_action_key(world->keybindings, "save_keys")) { save_keybindings(world); } - else if (key == get_action_key(world->keybindings, "keys nav up")) + else if (key == get_action_key(world->keybindings, "keys_u")) { - keyswin_move_selection (world, 'u'); + keyswin_move_selection(world, 'u'); } - else if (key == get_action_key(world->keybindings, "keys nav down")) + else if (key == get_action_key(world->keybindings, "keys_d")) { - keyswin_move_selection (world, 'd'); + keyswin_move_selection(world, 'd'); } - else if (key == get_action_key(world->keybindings, "keys mod")) + else if (key == get_action_key(world->keybindings, "keys_m")) { - keyswin_mod_key (world, win_meta); + keyswin_mod_key(world, win_meta); } - else if (key == get_action_key(world->keybindings, "map up")) + else if (key == get_action_key(world->keybindings, "map_u")) { map_scroll(world->map, NORTH, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map down")) + else if (key == get_action_key(world->keybindings, "map_d")) { map_scroll(world->map, SOUTH, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map right")) + else if (key == get_action_key(world->keybindings, "map_r")) { map_scroll(world->map, EAST, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map left")) + else if (key == get_action_key(world->keybindings, "map_l")) { map_scroll(world->map, WEST, win_map->frame.size); } - else if (key == get_action_key(world->keybindings, "map center player")) + else if (key == get_action_key(world->keybindings, "map_c")) { map_center_player (world->map, world->player, win_map->frame.size); } diff --git a/src/draw_wins.c b/src/draw_wins.c index cb640f0..b15e174 100644 --- a/src/draw_wins.c +++ b/src/draw_wins.c @@ -12,6 +12,7 @@ #include "map.h" /* for Map struct */ #include "main.h" /* for World struct */ #include "rexit.h" /* for err_exit() */ +#include "command_db.h" /* for get_command_longdesc() */ @@ -233,6 +234,7 @@ extern void draw_keys_win(struct Win * win) char * keydesc = malloc(keydescwidth), * keyname; char * err_hint = "Trouble with draw_scroll_hint() in draw_keys_win()."; attr_t attri; + char * cmd_dsc; for (y = 0; y <= world->keyswindata->max && y < win->frame.size.y; y++) { if (0 == y && offset > 0) @@ -263,19 +265,19 @@ extern void draw_keys_win(struct Win * win) keyname = get_keyname(world->keybindings[y + offset].key); snprintf(keydesc, keydescwidth, "%-9s", keyname); free(keyname); + cmd_dsc = get_command_longdsc(world, + world->keybindings[y + offset].name); for (x = 0; x < win->frame.size.x; x++) { if (x < strlen(keydesc)) { mvwaddch(win->frame.curses_win, y, x, keydesc[x] | attri); } - else if (strlen(keydesc) < x - && x < strlen(world->keybindings[y + offset].name) - + strlen(keydesc) + 1) + else if ( strlen(keydesc) < x + && x < strlen(cmd_dsc) + strlen(keydesc) + 1) { mvwaddch(win->frame.curses_win, y, x, - world->keybindings[y + offset] - .name[x - strlen(keydesc) - 1] | attri); + cmd_dsc[x - strlen(keydesc) - 1] | attri); } else { diff --git a/src/main.c b/src/main.c index 38e67b8..f240975 100644 --- a/src/main.c +++ b/src/main.c @@ -27,6 +27,7 @@ #include "rrand.h" /* for rrand(), rrand_seed() */ #include "rexit.h" /* for exit_game() */ #include "control.h" /* for meta_control() */ +#include "command_db.h" /* for init_command_db() */ @@ -34,6 +35,9 @@ int main(int argc, char *argv[]) { struct World world; + init_command_db(&world); + set_cleanup_flag(CLEANUP_COMMAND_DB); + /* Check for corrupted savefile / recordfile savings. */ char * recordfile = "record"; char * savefile = "savefile"; @@ -244,7 +248,7 @@ int main(int argc, char *argv[]) draw_all_wins(&win_meta); key = getch(); if ( EOF != action - && key == get_action_key(world.keybindings, "wait / next turn")) + && key == get_action_key(world.keybindings, "wait")) { action = getc(file); if (EOF != action) diff --git a/src/main.h b/src/main.h index 2ae8133..0bd2cd8 100644 --- a/src/main.h +++ b/src/main.h @@ -45,6 +45,7 @@ struct World struct MonsterDef * monster_def; /* Pointer to the monster definitions. */ struct Monster * monster; /* Pointer to the monsters' data. */ struct Player * player; /* Pointer to the player data. */ + struct CommandDB * cmd_db; /* Pointer to the command database. */ struct Wins wins; /* Pointers to WinMeta, individual Wins.*/ }; diff --git a/src/rexit.c b/src/rexit.c index 71ec130..2497c03 100644 --- a/src/rexit.c +++ b/src/rexit.c @@ -9,6 +9,7 @@ #include "main.h" /* for World struct */ #include "map.h" /* for Map struct */ #include "keybindings.h" /* for KeysWinData, KeyBinding structs */ +#include "command_db.h" /* for free_command_db() */ @@ -42,6 +43,10 @@ static void cleanup(struct World * world) { free(world->log); } + if (cleanup_flags & CLEANUP_COMMAND_DB) + { + free_command_db(world); + } } diff --git a/src/rexit.h b/src/rexit.h index f81d50f..fcb7155 100644 --- a/src/rexit.h +++ b/src/rexit.h @@ -24,7 +24,8 @@ enum cleanup_flag CLEANUP_NCURSES = 0x01, CLEANUP_MAP = 0x02, CLEANUP_KEYBINDINGS = 0x04, - CLEANUP_LOG = 0x08 + CLEANUP_LOG = 0x08, + CLEANUP_COMMAND_DB = 0x10 }; extern void set_cleanup_flag(enum cleanup_flag flag);