home · contact · privacy
Removed redundancy between record_control() and player_control() by re-writing their...
[plomrogue] / src / control.c
1 /* control.c */
2
3 #include "control.h"
4 #include <stdint.h> /* for uint8_t */
5 #include "windows.h" /* for cycle_active_win(), shift_active_win(), struct Win,
6                       *  struct WinMeta
7                       */
8 #include "keybindings.h" /* for get_keycode_to_action(), mod_selected_keyb(),
9                           * move_keyb_mod_selection()
10                           */
11 #include "map.h" /* for map_scroll() */
12 #include "main.h" /* for world global */
13 #include "rexit.h" /* for exit_err() */
14 #include "wincontrol.h" /* for scroll_pad(), toggle_window(),
15                          * growshrink_active_window(), toggle_winconfig(),
16                          * toggle_win_height_type(), toggle_win_width_type()
17                          */
18 #include "map_object_actions.h" /* for player_wait(), move_player(),
19                                  * player_drop(), player_pick()
20                                  */
21 #include "command_db.h" /* for is_command_id_shortdsc() */
22 #include "misc.h" /* for load_interface_conf(), unload_interface_conf(),
23                    * save_interface_conf(), nav_inventory()
24                    */
25 #include "yx_uint16.h" /* for dir enum */
26 #include "map_objects.h" /* for get_player() */
27
28
29
30 extern uint16_t get_available_keycode_to_action(char * name)
31 {
32     uint16_t keycode = get_keycode_to_action(world.kb_global.kbs, name);
33     if (0 != keycode || 0 == world.wmeta->active)
34     {
35         return keycode;
36     }
37     struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
38     if (0 == wc->view)
39     {
40         keycode = get_keycode_to_action(wc->kb.kbs, name);
41     }
42     else if (1 == wc->view)
43     {
44         keycode = get_keycode_to_action(world.kb_wingeom.kbs, name);
45     }
46     else if (2 == wc->view)
47     {
48         keycode = get_keycode_to_action(world.kb_winkeys.kbs, name);
49     }
50     return keycode;
51 }
52
53
54
55 extern uint8_t player_control_by_id(int action)
56 {
57     if      (is_command_id_shortdsc(action, "wait"))
58     {
59         player_wait();
60     }
61     else if (is_command_id_shortdsc(action, "player_u"))
62     {
63         move_player(NORTH);
64     }
65     else if (is_command_id_shortdsc(action, "player_r"))
66     {
67         move_player(EAST);
68     }
69     else if (is_command_id_shortdsc(action, "player_d"))
70     {
71         move_player(SOUTH);
72     }
73     else if (is_command_id_shortdsc(action, "player_l"))
74     {
75         move_player(WEST);
76     }
77     else if (is_command_id_shortdsc(action, "drop"))
78     {
79         player_drop();
80     }
81     else if (is_command_id_shortdsc(action, "pick"))
82     {
83         player_pick();
84     }
85     else
86     {
87         return 0;
88     }
89     return 1;
90 }
91
92
93
94 extern uint8_t player_control_by_key(int key)
95 {
96     char * action_name = get_func_to_keycode(world.kb_global.kbs, key);
97     if (NULL == action_name && 0 != world.wmeta->active)
98     {
99         struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
100         action_name = get_func_to_keycode(wc->kb.kbs, key);
101     }
102     if (NULL != action_name)
103     {
104         uint8_t action_id = get_command_id(action_name);
105         return player_control_by_id(action_id);
106     }
107     return 0;
108 }
109
110
111
112 extern uint8_t wingeom_control(int key)
113 {
114     char * err_shift  = "Trouble with shift_active_win() in wingeom_control().";
115     char * err_resize = "Trouble with growshrink_active_window() in "
116                         "wingeom_control().";
117     if      (key == get_available_keycode_to_action("to_height_t"))
118     {
119         toggle_win_height_type(world.wmeta->active);
120     }
121     else if (key == get_available_keycode_to_action("to_width_t"))
122     {
123         toggle_win_width_type(world.wmeta->active);
124     }
125     else if (key == get_available_keycode_to_action("grow_h"))
126     {
127         exit_err(growshrink_active_window('*'), err_resize);
128     }
129     else if (key == get_available_keycode_to_action("shri_h"))
130     {
131         exit_err(growshrink_active_window('_'), err_resize);
132     }
133     else if (key == get_available_keycode_to_action("grow_v"))
134     {
135         exit_err(growshrink_active_window('+'), err_resize);
136     }
137     else if (key == get_available_keycode_to_action("shri_v"))
138     {
139         exit_err(growshrink_active_window('-'), err_resize);
140     }
141     else if (key == get_available_keycode_to_action("shift_f"))
142     {
143         exit_err(shift_active_win(world.wmeta, 'f'), err_shift);
144     }
145     else if (key == get_available_keycode_to_action("shift_b"))
146     {
147         exit_err(shift_active_win(world.wmeta, 'b'), err_shift);
148     }
149     else
150     {
151         return 0;
152     }
153     return 1;
154 }
155
156
157
158 extern uint8_t winkeyb_control(int key)
159 {
160     struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
161     if      (key == get_available_keycode_to_action("w_keys_u"))
162     {
163         move_keyb_mod_selection(&wc->kb, 'u');
164     }
165     else if (key == get_available_keycode_to_action("w_keys_d"))
166     {
167         move_keyb_mod_selection(&wc->kb, 'd');
168     }
169     else if (key == get_available_keycode_to_action("w_keys_m"))
170     {
171         mod_selected_keyb(&wc->kb);
172     }
173     else
174     {
175         return 0;
176     }
177     return 1;
178 }
179
180
181
182 extern uint8_t meta_control(int key)
183 {
184     struct WinMeta * win_meta = world.wmeta;
185     struct Win * win_map      = get_win_by_id('m');
186     char * err_toggle = "Trouble with toggle_window() in meta_control().";
187     if      (key == get_available_keycode_to_action("quit"))
188     {
189         return 1;
190     }
191     else if (key == get_available_keycode_to_action("winconf"))
192     {
193         toggle_winconfig(world.wmeta->active);
194     }
195     else if (key == get_available_keycode_to_action("cyc_win_f"))
196     {
197         cycle_active_win(world.wmeta, 'f');
198     }
199     else if (key == get_available_keycode_to_action("cyc_win_b"))
200     {
201         cycle_active_win(world.wmeta, 'b');
202     }
203     else if (key == get_available_keycode_to_action("scrl_r"))
204     {
205         scroll_pad(win_meta, '+');
206     }
207     else if (key == get_available_keycode_to_action("scrl_l"))
208     {
209         scroll_pad(win_meta, '-');
210     }
211     else if (key == get_available_keycode_to_action("to_a_keywin"))
212     {
213        exit_err(toggle_window(win_meta, get_win_by_id('k')), err_toggle);
214     }
215     else if (key == get_available_keycode_to_action("to_g_keywin"))
216     {
217         exit_err(toggle_window(win_meta, get_win_by_id('0')), err_toggle);
218     }
219     else if (key == get_available_keycode_to_action("to_wg_keywin"))
220     {
221         exit_err(toggle_window(win_meta, get_win_by_id('1')), err_toggle);
222     }
223     else if (key == get_available_keycode_to_action("to_wk_keywin"))
224     {
225         exit_err(toggle_window(win_meta, get_win_by_id('2')), err_toggle);
226     }
227     else if (key == get_available_keycode_to_action("to_mapwin"))
228     {
229         exit_err(toggle_window(win_meta, win_map), err_toggle);
230     }
231     else if (key == get_available_keycode_to_action("to_infowin"))
232     {
233         exit_err(toggle_window(win_meta, get_win_by_id('i')), err_toggle);
234     }
235     else if (key == get_available_keycode_to_action("to_inv"))
236     {
237         exit_err(toggle_window(win_meta, get_win_by_id('c')), err_toggle);
238     }
239     else if (key == get_available_keycode_to_action("to_logwin"))
240     {
241         exit_err(toggle_window(win_meta, get_win_by_id('l')), err_toggle);
242     }
243     else if (key == get_available_keycode_to_action("save_conf"))
244     {
245         save_interface_conf();
246     }
247     else if (key == get_available_keycode_to_action("g_keys_u"))
248     {
249         move_keyb_mod_selection(&world.kb_global, 'u');
250     }
251     else if (key == get_available_keycode_to_action("g_keys_d"))
252     {
253         move_keyb_mod_selection(&world.kb_global, 'd');
254     }
255     else if (key == get_available_keycode_to_action("g_keys_m"))
256     {
257         mod_selected_keyb(&world.kb_global);
258     }
259     else if (key == get_available_keycode_to_action("wg_keys_u"))
260     {
261         move_keyb_mod_selection(&world.kb_wingeom, 'u');
262     }
263     else if (key == get_available_keycode_to_action("wg_keys_d"))
264     {
265         move_keyb_mod_selection(&world.kb_wingeom, 'd');
266     }
267     else if (key == get_available_keycode_to_action("wg_keys_m"))
268     {
269         mod_selected_keyb(&world.kb_wingeom);
270     }
271     else if (key == get_available_keycode_to_action("wk_keys_u"))
272     {
273         move_keyb_mod_selection(&world.kb_winkeys, 'u');
274     }
275     else if (key == get_available_keycode_to_action("wk_keys_d"))
276     {
277         move_keyb_mod_selection(&world.kb_winkeys, 'd');
278     }
279     else if (key == get_available_keycode_to_action("wk_keys_m"))
280     {
281         mod_selected_keyb(&world.kb_winkeys);
282     }
283     else if (key == get_available_keycode_to_action("map_u"))
284     {
285         map_scroll(win_map, world.map->size, NORTH);
286     }
287     else if (key == get_available_keycode_to_action("map_d"))
288     {
289         map_scroll(win_map, world.map->size, SOUTH);
290     }
291     else if (key == get_available_keycode_to_action("map_r"))
292     {
293         map_scroll(win_map, world.map->size, EAST);
294     }
295     else if (key == get_available_keycode_to_action("map_l"))
296     {
297         map_scroll(win_map, world.map->size, WEST);
298     }
299     else if (key == get_available_keycode_to_action("map_c"))
300     {
301         struct MapObj * player = get_player();
302         win_map->center = player->pos;
303     }
304     else if (key == get_available_keycode_to_action("inv_u"))
305     {
306         nav_inventory('u');
307     }
308     else if (key == get_available_keycode_to_action("inv_d"))
309     {
310         nav_inventory('d');
311     }
312     else if (key == get_available_keycode_to_action("reload_conf"))
313     {
314         unload_interface_conf();
315         load_interface_conf();
316     }
317     else if (key == get_available_keycode_to_action("winconf"))
318     {
319         toggle_winconfig(world.wmeta->active);
320     }
321     return 0;
322 }