X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=src%2Fcontrol.c;h=3116937c1a9eb4d87342f5416b538510851fca21;hb=3edf207d99f9e6e7e4b6beb9939bb9618e4be597;hp=8455aaf041272167dbcbaeeb0ab949f2023bda0d;hpb=e49cd07e79574ba404a0a8fc867344cc630d7794;p=plomrogue diff --git a/src/control.c b/src/control.c index 8455aaf..3116937 100644 --- a/src/control.c +++ b/src/control.c @@ -2,316 +2,275 @@ #include "control.h" #include /* for uint8_t */ +#include /* for strcmp() */ #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win, * struct WinMeta */ #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(), - * move_keyb_mod_selection() + * move_keyb_mod_selection(), get_func_to_keycode() */ -#include "map.h" /* for map_scroll(), map_center_player(), dir enum */ -#include "main.h" /* for World struct */ -#include "rexit.h" /* for exit_err() */ -#include "wincontrol.h" /* for scroll_pad(), toggle_window(), - * growshrink_active_window(), toggle_winconfig(), - * toggle_win_height_type(), toggle_win_width_type() +#include "map.h" /* for map_scroll(), map_center() */ +#include "main.h" /* for world global */ +#include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(), + * growshrink_active_window(),toggle_winconfig(), + * toggle_size_type() */ -#include "map_object_actions.h" /* for player_wait(), move_player() */ +#include "map_object_actions.h" /* for struct MapObjAct, actor_wait(), + * actor_move(), actor_drop(), actor_pick(), + * actor_pick(), get_moa_id_by_name() + */ #include "command_db.h" /* for is_command_id_shortdsc() */ -#include "misc.h" /* for load_interface_conf(), unload_interface_conf(), - * save_interface_conf() +#include "misc.h" /* for reload_interface_conf(), save_interface_conf(), + * nav_inventory(), turn_over() */ +#include "map_objects.h" /* for get_player() */ -extern uint16_t get_available_keycode_to_action(struct World * world, - char * name) +/* If "cmd" matches "match" in get_available_keycode_to_action(), execute "f" + * with provided char arguments and return 1; else only return 0. + */ +static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ()); +static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c); +static uint8_t try_cmd_2args(int cmd, char * match, + void (* f) (char, char), char c1, char c2); + +/* If "action" is id of command named "match", set player->arg, ->command and + * call turn_over(). + */ +static uint8_t try_player_cmd(int action, char * match, char * command, + uint8_t arg); + +/* Return pointer to global keybindings or to keybindings for wingeometry config + * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w"). + */ +static struct KeyBiData * select_keybidata_pointer(char c); + +/* Wrappers to make some functions compatible to try_cmd_* single char args. */ +static void wrap_mod_selected_keyb(char c); +static void wrap_mv_kb_mod(char c1, char c2); + + + +static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ()) { - uint16_t keycode = get_keycode_to_action(world->kb_global.kbs, name); - if (0 != keycode || 0 == world->wmeta->active) - { - return keycode; - } - struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active); - if (0 == wc->view) + if (cmd == get_available_keycode_to_action(match)) { - keycode = get_keycode_to_action(wc->kb.kbs, name); - } - else if (1 == wc->view) - { - keycode = get_keycode_to_action(world->kb_wingeom.kbs, name); - } - else if (2 == wc->view) - { - keycode = get_keycode_to_action(world->kb_winkeys.kbs, name); + f(); + return 1; } - return keycode; + return 0; } -extern void record_control(int action, struct World * world) +static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c) { - if (is_command_id_shortdsc(world, action, "wait")) - { - player_wait(world); - } - else if (is_command_id_shortdsc(world, action, "player_u")) - { - move_player(world, NORTH); - } - else if (is_command_id_shortdsc(world, action, "player_r")) + if (cmd == get_available_keycode_to_action(match)) { - move_player(world, EAST); - } - else if (is_command_id_shortdsc(world, action, "player_d")) - { - move_player(world, SOUTH); - } - else if (is_command_id_shortdsc(world, action, "player_l")) - { - move_player(world, WEST); + f(c); + return 1; } + return 0; } -extern uint8_t player_control(int key, struct World * world) +static uint8_t try_cmd_2args(int cmd, char * match, + void (* f) (char, char), char c1, char c2) { - if (key == get_available_keycode_to_action(world, "player_u")) - { - move_player(world, NORTH); - } - else if (key == get_available_keycode_to_action(world, "player_r")) - { - move_player(world, EAST); - } - else if (key == get_available_keycode_to_action(world, "player_d")) - { - move_player(world, SOUTH); - } - else if (key == get_available_keycode_to_action(world, "player_l")) - { - move_player(world, WEST); - } - else if (key == get_available_keycode_to_action(world, "wait")) + if (cmd == get_available_keycode_to_action(match)) { - player_wait(world); - } - else - { - return 0; + f(c1, c2); + return 1; } - return 1; + return 0; } -extern uint8_t wingeom_control(int key, struct World * world) +static uint8_t try_player_cmd(int action, char * match, char * command, + uint8_t arg) { - char * err_shift = "Trouble with shift_active_win() in wingeom_control()."; - char * err_resize = "Trouble with growshrink_active_window() in " - "wingeom_control()."; - if (key == get_available_keycode_to_action(world, "to_height_t")) - { - toggle_win_height_type(world, world->wmeta->active); - } - else if (key == get_available_keycode_to_action(world, "to_width_t")) + if (is_command_id_shortdsc(action, match)) { - toggle_win_width_type(world, world->wmeta->active); + struct MapObj * player = get_player(); + player->arg = arg; + player->command = get_moa_id_by_name(command); + turn_over(get_command_id(match)); + return 1; } - else if (key == get_available_keycode_to_action(world, "grow_h")) + return 0; +} + + + +static struct KeyBiData * select_keybidata_pointer(char c) +{ + struct KeyBiData * kbd; + kbd = &world.kb_global; + if ('g' == c) { - exit_err(growshrink_active_window(world, '*'), world, err_resize); + kbd = &world.kb_wingeom; } - else if (key == get_available_keycode_to_action(world, "shri_h")) + else if ('k' == c) { - exit_err(growshrink_active_window(world, '_'), world, err_resize); + kbd = &world.kb_winkeys; } - else if (key == get_available_keycode_to_action(world, "grow_v")) + else if ('w' == c) { - exit_err(growshrink_active_window(world, '+'), world, err_resize); + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + kbd = &wc->kb; } - else if (key == get_available_keycode_to_action(world, "shri_v")) + return kbd; +} + + + +static void wrap_mod_selected_keyb(char c) +{ + mod_selected_keyb(select_keybidata_pointer(c)); +} + + + +static void wrap_mv_kb_mod(char c1, char c2) +{ + move_keyb_mod_selection(select_keybidata_pointer(c1), c2); +} + + + +extern uint16_t get_available_keycode_to_action(char * name) +{ + uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name); + if (0 != keycode || 0 == world.wmeta->active) { - exit_err(growshrink_active_window(world, '-'), world, err_resize); + return keycode; } - else if (key == get_available_keycode_to_action(world, "shift_f")) + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + if (0 == wc->view) { - exit_err(shift_active_win(world->wmeta, 'f'), world, err_shift); + keycode = get_keycode_to_action(wc->kb.kbs, name); } - else if (key == get_available_keycode_to_action(world, "shift_b")) + else if (1 == wc->view) { - exit_err(shift_active_win(world->wmeta, 'b'), world, err_shift); + keycode = get_keycode_to_action(world.kb_wingeom.kbs, name); } - else + else if (2 == wc->view) { - return 0; + keycode = get_keycode_to_action(world.kb_winkeys.kbs, name); } - return 1; + return keycode; } -extern uint8_t winkeyb_control(int key, struct World * world) +extern uint8_t player_control_by_key(int key) { - struct WinConf * wc = get_winconf_by_win(world, world->wmeta->active); - if (key == get_available_keycode_to_action(world, "w_keys_u")) - { - move_keyb_mod_selection(&wc->kb, 'u'); - } - else if (key == get_available_keycode_to_action(world, "w_keys_d")) - { - move_keyb_mod_selection(&wc->kb, 'd'); - } - else if (key == get_available_keycode_to_action(world, "w_keys_m")) + char * action_name = get_func_to_keycode(world.kb_global.kbs, key); + if (NULL == action_name && 0 != world.wmeta->active) { - mod_selected_keyb(world, &wc->kb); + struct WinConf * wc = get_winconf_by_win(world.wmeta->active); + action_name = get_func_to_keycode(wc->kb.kbs, key); } - else + if (NULL != action_name) { - return 0; + uint8_t action_id = get_command_id(action_name); + return player_control_by_id(action_id); } - return 1; + return 0; } -extern uint8_t meta_control(int key, struct World * world) + +extern uint8_t player_control_by_id(int action) { - struct WinMeta * win_meta = world->wmeta; - struct Win * win_keys = get_win_by_id(world, '0'); /* Bad hardcoding. */ - struct Win * win_map = get_win_by_id(world, 'm'); /* TODO: Replace. */ - struct Win * win_info = get_win_by_id(world, 'i'); /* */ - struct Win * win_log = get_win_by_id(world, 'l'); /* */ - char * err_toggle = "Trouble with toggle_window() in meta_control()."; - if (key == get_available_keycode_to_action(world, "quit")) + if ( try_player_cmd(action, "wait", "wait", 0) + || try_player_cmd(action, "drop", "drop", world.inventory_select) + || try_player_cmd(action, "pick", "pick_up", 0) + || try_player_cmd(action, "use", "use", world.inventory_select) + || try_player_cmd(action, "player_u", "move", 'N') + || try_player_cmd(action, "player_d", "move", 'S') + || try_player_cmd(action, "player_r", "move", 'E') + || try_player_cmd(action, "player_l", "move", 'W')) { return 1; } - else if (key == get_available_keycode_to_action(world, "winconf")) - { - toggle_winconfig(world, world->wmeta->active); - } - else if (key == get_available_keycode_to_action(world, "cyc_win_f")) - { - cycle_active_win(world->wmeta, 'f'); - } - else if (key == get_available_keycode_to_action(world, "cyc_win_b")) - { - cycle_active_win(world->wmeta, 'b'); - } - else if (key == get_available_keycode_to_action(world, "scrl_r")) - { - scroll_pad(win_meta, '+'); - } - else if (key == get_available_keycode_to_action(world, "scrl_l")) - { - scroll_pad(win_meta, '-'); - } - else if (key == get_available_keycode_to_action(world, "to_a_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'k')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_g_keywin")) - { - exit_err(toggle_window(win_meta, win_keys), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_wg_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, '1')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_wk_keywin")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, '2')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_mapwin")) - { - exit_err(toggle_window(win_meta, win_map), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_infowin")) - { - exit_err(toggle_window(win_meta, win_info), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_inv")) - { - uint8_t test = toggle_window(win_meta, get_win_by_id(world, 'c')); - exit_err(test, world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "to_logwin")) - { - exit_err(toggle_window(win_meta, win_log), world, err_toggle); - } - else if (key == get_available_keycode_to_action(world, "save_conf")) - { - save_interface_conf(world); - } - else if (key == get_available_keycode_to_action(world, "g_keys_u")) - { - move_keyb_mod_selection(&world->kb_global, 'u'); - } - else if (key == get_available_keycode_to_action(world, "g_keys_d")) - { - move_keyb_mod_selection(&world->kb_global, 'd'); - } - else if (key == get_available_keycode_to_action(world, "g_keys_m")) - { - mod_selected_keyb(world, &world->kb_global); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_u")) - { - move_keyb_mod_selection(&world->kb_wingeom, 'u'); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_d")) - { - move_keyb_mod_selection(&world->kb_wingeom, 'd'); - } - else if (key == get_available_keycode_to_action(world, "wg_keys_m")) - { - mod_selected_keyb(world, &world->kb_wingeom); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_u")) - { - move_keyb_mod_selection(&world->kb_winkeys, 'u'); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_d")) - { - move_keyb_mod_selection(&world->kb_winkeys, 'd'); - } - else if (key == get_available_keycode_to_action(world, "wk_keys_m")) - { - mod_selected_keyb(world, &world->kb_winkeys); - } - else if (key == get_available_keycode_to_action(world, "map_u")) - { - map_scroll(world->map, NORTH, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_d")) - { - map_scroll(world->map, SOUTH, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_r")) - { - map_scroll(world->map, EAST, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_l")) - { - map_scroll(world->map, WEST, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "map_c")) - { - map_center_player(world->map, world->player, win_map->frame.size); - } - else if (key == get_available_keycode_to_action(world, "reload_conf")) + return 0; +} + + + +extern uint8_t wingeom_control(int key) +{ + if ( try_cmd_1args(key, "to_height_t", toggle_win_size_type, 'y') + || try_cmd_1args(key, "to_width_t", toggle_win_size_type, 'x') + || try_cmd_1args(key, "grow_h", growshrink_active_window, '*') + || try_cmd_1args(key, "shri_h", growshrink_active_window, '_') + || try_cmd_1args(key, "grow_v", growshrink_active_window, '+') + || try_cmd_1args(key, "shri_v", growshrink_active_window, '-') + || try_cmd_1args(key, "shift_f", shift_active_win, 'f') + || try_cmd_1args(key, "shift_b", shift_active_win, 'b')) { - unload_interface_conf(world); - load_interface_conf(world); + return 1; } - else if (key == get_available_keycode_to_action(world, "winconf")) + return 0; +} + + + +extern uint8_t winkeyb_control(int key) +{ + if ( try_cmd_1args(key, "w_keys_m", wrap_mod_selected_keyb, 'w') + || try_cmd_2args(key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u') + || try_cmd_2args(key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd')) { - toggle_winconfig(world, world->wmeta->active); + return 1; } return 0; } + + + +extern uint8_t meta_control(int key) +{ + uint8_t ret = (key == get_available_keycode_to_action("quit")); + if ( (0 == ret) + && ( try_cmd_0args(key, "winconf", toggle_winconfig) + || try_cmd_0args(key, "reload_conf", reload_interface_conf) + || try_cmd_0args(key, "save_conf", save_interface_conf) + || try_cmd_0args(key, "map_c", map_center) + || try_cmd_1args(key, "scrl_r", scroll_pad, '+') + || try_cmd_1args(key, "scrl_l", scroll_pad, '-') + || try_cmd_1args(key, "to_a_keywin", toggle_window, 'k') + || try_cmd_1args(key, "to_g_keywin", toggle_window, '0') + || try_cmd_1args(key, "to_wg_keywin", toggle_window, '1') + || try_cmd_1args(key, "to_wk_keywin", toggle_window, '2') + || try_cmd_1args(key, "to_mapwin", toggle_window, 'm') + || try_cmd_1args(key, "to_infowin", toggle_window, 'i') + || try_cmd_1args(key, "to_inv", toggle_window, 'c') + || try_cmd_1args(key, "to_logwin", toggle_window, 'l') + || try_cmd_1args(key, "cyc_win_f", cycle_active_win, 'f') + || try_cmd_1args(key, "cyc_win_b", cycle_active_win, 'b') + || try_cmd_1args(key, "g_keys_m", wrap_mod_selected_keyb, 'G') + || try_cmd_1args(key, "wg_keys_m", wrap_mod_selected_keyb, 'g') + || try_cmd_1args(key, "wk_keys_m", wrap_mod_selected_keyb, 'k') + || try_cmd_1args(key, "inv_u", nav_inventory, 'u') + || try_cmd_1args(key, "inv_d", nav_inventory, 'd') + || try_cmd_1args(key, "map_u", map_scroll, 'N') + || try_cmd_1args(key, "map_d", map_scroll, 'S') + || try_cmd_1args(key, "map_r", map_scroll, 'E') + || try_cmd_1args(key, "map_l", map_scroll, 'W') + || try_cmd_2args(key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u') + || try_cmd_2args(key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd') + || try_cmd_2args(key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u') + || try_cmd_2args(key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd') + || try_cmd_2args(key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u') + || try_cmd_2args(key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd'))) + { + ; + } + return ret; +}