home · contact · privacy
Each map object action now take different numbers of turns to complete. Re-wrote...
[plomrogue] / src / control.c
1 /* control.c */
2
3 #include "control.h"
4 #include <stdint.h> /* for uint8_t */
5 #include <string.h> /* for strcmp() */
6 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
7                       *  struct WinMeta
8                       */
9 #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
10                           * move_keyb_mod_selection(), get_func_to_keycode()
11                           */
12 #include "map.h" /* for map_scroll(), map_center() */
13 #include "main.h" /* for world global */
14 #include "rexit.h" /* for exit_err() */
15 #include "wincontrol.h" /* for struct WinConf, scroll_pad(), toggle_window(),
16                          * growshrink_active_window(),toggle_winconfig(),
17                          * toggle_win_height_type(), toggle_win_width_type()
18                          */
19 #include "map_object_actions.h" /* for struct MapObjAct, actor_wait(),
20                                  * actor_move(), actor_drop(), actor_pick(),
21                                  * actor_pick()
22                                  */
23 #include "command_db.h" /* for is_command_id_shortdsc() */
24 #include "misc.h" /* for reload_interface_conf(), save_interface_conf(),
25                    * nav_inventory(), turn_over()
26                    */
27 #include "map_objects.h" /* for get_player() */
28
29
30
31 /* If "cmd" matches "match" in get_available_keycode_to_action(), execute "f"
32  * with provided char arguments and return 1; else only return 0.
33  */
34 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ());
35 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c);
36 static uint8_t try_cmd_2args(int cmd, char * match,
37                              void (* f) (char, char), char c1, char c2);
38
39 /* try_player_cmd() helper, returns world.map_obj_acts action id for "name". */
40 static uint8_t get_moa_id_by_name(char * name);
41
42 /* If "action" is id of command named "match", set player->arg, ->command and
43  * call turn_over().
44  */
45 static uint8_t try_player_cmd(int action, char * match, char * command,
46                               uint8_t arg);
47
48 /* Return pointer to global keybindings or to keybindings for wingeometry config
49  * (c = "g") or winkeys config (c = "k") or active window's keybindings ("w").
50  */
51 static struct KeyBiData * select_keybidata_pointer(char c);
52
53 /* Wrappers to make some functions compatible to try_cmd_* single char args. */
54 static void wrap_mod_selected_keyb(char c);
55 static void wrap_mv_kb_mod(char c1, char c2);
56
57
58
59 static uint8_t try_cmd_0args(int cmd, char * match, void (* f) ())
60 {
61     if (cmd == get_available_keycode_to_action(match))
62     {
63         f();
64         return 1;
65     }
66     return 0;
67 }
68
69
70
71 static uint8_t try_cmd_1args(int cmd, char * match, void (* f) (char), char c)
72 {
73     if (cmd == get_available_keycode_to_action(match))
74     {
75         f(c);
76         return 1;
77     }
78     return 0;
79 }
80
81
82
83 static uint8_t try_cmd_2args(int cmd, char * match,
84                              void (* f) (char, char), char c1, char c2)
85 {
86     if (cmd == get_available_keycode_to_action(match))
87     {
88         f(c1, c2);
89         return 1;
90     }
91     return 0;
92 }
93
94
95
96 static uint8_t get_moa_id_by_name(char * name)
97 {
98     struct MapObjAct * moa = world.map_obj_acts;
99     while (NULL != moa)
100     {
101         if (0 == strcmp(moa->name, name))
102         {
103             break;
104         }
105         moa = moa->next;
106     }
107     exit_err(NULL == moa, "get_moa_id_name() did not find map object action.");
108     return moa->id;
109 }
110
111
112
113 static uint8_t try_player_cmd(int action, char * match, char * command,
114                               uint8_t arg)
115 {
116     if (is_command_id_shortdsc(action, match))
117     {
118         struct MapObj * player = get_player();
119         player->arg = arg;
120         player->command = get_moa_id_by_name(command);
121         turn_over(get_command_id(match));
122         return 1;
123     }
124     return 0;
125 }
126
127
128
129 static struct KeyBiData * select_keybidata_pointer(char c)
130 {
131     struct KeyBiData * kbd;
132     kbd = &world.kb_global;
133     if      ('g' == c)
134     {
135         kbd = &world.kb_wingeom;
136     }
137     else if ('k' == c)
138     {
139         kbd = &world.kb_winkeys;
140     }
141     else if ('w' == c)
142     {
143         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
144         kbd = &wc->kb;
145     }
146     return kbd;
147 }
148
149
150
151 static void wrap_mod_selected_keyb(char c)
152 {
153         mod_selected_keyb(select_keybidata_pointer(c));
154 }
155
156
157
158 static void wrap_mv_kb_mod(char c1, char c2)
159 {
160         move_keyb_mod_selection(select_keybidata_pointer(c1), c2);
161 }
162
163
164
165 extern uint16_t get_available_keycode_to_action(char * name)
166 {
167     uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
168     if (0 != keycode || 0 == world.wmeta->active)
169     {
170         return keycode;
171     }
172     struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
173     if (0 == wc->view)
174     {
175         keycode = get_keycode_to_action(wc->kb.kbs, name);
176     }
177     else if (1 == wc->view)
178     {
179         keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
180     }
181     else if (2 == wc->view)
182     {
183         keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
184     }
185     return keycode;
186 }
187
188
189
190 extern uint8_t player_control_by_key(int key)
191 {
192     char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
193     if (NULL == action_name && 0 != world.wmeta->active)
194     {
195         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
196         action_name = get_func_to_keycode(wc->kb.kbs, key);
197     }
198     if (NULL != action_name)
199     {
200         uint8_t action_id = get_command_id(action_name);
201         return player_control_by_id(action_id);
202     }
203     return 0;
204 }
205
206
207
208
209 extern uint8_t player_control_by_id(int action)
210 {
211     if (   try_player_cmd(action, "wait", "wait", 0)
212         || try_player_cmd(action, "drop", "drop", world.inventory_select)
213         || try_player_cmd(action, "pick", "pick_up", 0)
214         || try_player_cmd(action, "use", "use", world.inventory_select)
215         || try_player_cmd(action, "player_u", "move", 'N')
216         || try_player_cmd(action, "player_d", "move", 'S')
217         || try_player_cmd(action, "player_r", "move", 'E')
218         || try_player_cmd(action, "player_l", "move", 'W'))
219     {
220         return 1;
221     }
222     return 0;
223 }
224
225
226
227 extern uint8_t wingeom_control(int key)
228 {
229     if (   try_cmd_0args(key, "to_height_t", toggle_win_height_type)
230         || try_cmd_0args(key, "to_width_t", toggle_win_width_type)
231         || try_cmd_1args(key, "grow_h", growshrink_active_window, '*')
232         || try_cmd_1args(key, "shri_h", growshrink_active_window, '_')
233         || try_cmd_1args(key, "grow_v", growshrink_active_window, '+')
234         || try_cmd_1args(key, "shri_v", growshrink_active_window, '-')
235         || try_cmd_1args(key, "shift_f", shift_active_win, 'f')
236         || try_cmd_1args(key, "shift_b", shift_active_win, 'b'))
237     {
238         return 1;
239     }
240     return 0;
241 }
242
243
244
245 extern uint8_t winkeyb_control(int key)
246 {
247     if (   try_cmd_1args(key, "w_keys_m", wrap_mod_selected_keyb, 'w')
248         || try_cmd_2args(key, "w_keys_u", wrap_mv_kb_mod, 'w', 'u')
249         || try_cmd_2args(key, "w_keys_d", wrap_mv_kb_mod, 'w', 'd'))
250     {
251         return 1;
252     }
253     return 0;
254 }
255
256
257
258 extern uint8_t meta_control(int key)
259 {
260     uint8_t ret = (key == get_available_keycode_to_action("quit"));
261     if (   (0 == ret)
262         && (   try_cmd_0args(key, "winconf", toggle_winconfig)
263             || try_cmd_0args(key, "reload_conf", reload_interface_conf)
264             || try_cmd_0args(key, "save_conf", save_interface_conf)
265             || try_cmd_0args(key, "map_c", map_center)
266             || try_cmd_1args(key, "scrl_r", scroll_pad, '+')
267             || try_cmd_1args(key, "scrl_l", scroll_pad, '-')
268             || try_cmd_1args(key, "to_a_keywin", toggle_window, 'k')
269             || try_cmd_1args(key, "to_g_keywin", toggle_window, '0')
270             || try_cmd_1args(key, "to_wg_keywin", toggle_window, '1')
271             || try_cmd_1args(key, "to_wk_keywin", toggle_window, '2')
272             || try_cmd_1args(key, "to_mapwin", toggle_window, 'm')
273             || try_cmd_1args(key, "to_infowin", toggle_window, 'i')
274             || try_cmd_1args(key, "to_inv", toggle_window, 'c')
275             || try_cmd_1args(key, "to_logwin", toggle_window, 'l')
276             || try_cmd_1args(key, "cyc_win_f", cycle_active_win, 'f')
277             || try_cmd_1args(key, "cyc_win_b", cycle_active_win, 'b')
278             || try_cmd_1args(key, "g_keys_m", wrap_mod_selected_keyb, 'G')
279             || try_cmd_1args(key, "wg_keys_m", wrap_mod_selected_keyb, 'g')
280             || try_cmd_1args(key, "wk_keys_m", wrap_mod_selected_keyb, 'k')
281             || try_cmd_1args(key, "inv_u", nav_inventory, 'u')
282             || try_cmd_1args(key, "inv_d", nav_inventory, 'd')
283             || try_cmd_1args(key, "map_u", map_scroll, 'N')
284             || try_cmd_1args(key, "map_d", map_scroll, 'S')
285             || try_cmd_1args(key, "map_r", map_scroll, 'E')
286             || try_cmd_1args(key, "map_l", map_scroll, 'W')
287             || try_cmd_2args(key, "g_keys_u", wrap_mv_kb_mod, 'G', 'u')
288             || try_cmd_2args(key, "g_keys_d", wrap_mv_kb_mod, 'G', 'd')
289             || try_cmd_2args(key, "wg_keys_u", wrap_mv_kb_mod, 'g', 'u')
290             || try_cmd_2args(key, "wg_keys_d", wrap_mv_kb_mod, 'g', 'd')
291             || try_cmd_2args(key, "wk_keys_u", wrap_mv_kb_mod, 'k', 'u')
292             || try_cmd_2args(key, "wk_keys_d", wrap_mv_kb_mod, 'k', 'd')))
293     {
294         ;
295     }
296     return ret;
297 }