home · contact · privacy
Made single World struct a global variable, fitted a lot of code to this change,...
[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 void record_control(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 }
86
87
88
89 extern uint8_t player_control(int key)
90 {
91     if      (key == get_available_keycode_to_action("wait"))
92     {
93         player_wait();
94     }
95     else if (key == get_available_keycode_to_action("player_u"))
96     {
97         move_player(NORTH);
98     }
99     else if (key == get_available_keycode_to_action("player_r"))
100     {
101         move_player(EAST);
102     }
103     else if (key == get_available_keycode_to_action("player_d"))
104     {
105         move_player(SOUTH);
106     }
107     else if (key == get_available_keycode_to_action("player_l"))
108     {
109         move_player(WEST);
110     }
111     else if (key == get_available_keycode_to_action("drop"))
112     {
113         player_drop();
114     }
115     else if (key == get_available_keycode_to_action("pick"))
116     {
117         player_pick();
118     }
119     else
120     {
121         return 0;
122     }
123     return 1;
124 }
125
126
127
128 extern uint8_t wingeom_control(int key)
129 {
130     char * err_shift  = "Trouble with shift_active_win() in wingeom_control().";
131     char * err_resize = "Trouble with growshrink_active_window() in "
132                         "wingeom_control().";
133     if      (key == get_available_keycode_to_action("to_height_t"))
134     {
135         toggle_win_height_type(world.wmeta->active);
136     }
137     else if (key == get_available_keycode_to_action("to_width_t"))
138     {
139         toggle_win_width_type(world.wmeta->active);
140     }
141     else if (key == get_available_keycode_to_action("grow_h"))
142     {
143         exit_err(growshrink_active_window('*'), err_resize);
144     }
145     else if (key == get_available_keycode_to_action("shri_h"))
146     {
147         exit_err(growshrink_active_window('_'), err_resize);
148     }
149     else if (key == get_available_keycode_to_action("grow_v"))
150     {
151         exit_err(growshrink_active_window('+'), err_resize);
152     }
153     else if (key == get_available_keycode_to_action("shri_v"))
154     {
155         exit_err(growshrink_active_window('-'), err_resize);
156     }
157     else if (key == get_available_keycode_to_action("shift_f"))
158     {
159         exit_err(shift_active_win(world.wmeta, 'f'), err_shift);
160     }
161     else if (key == get_available_keycode_to_action("shift_b"))
162     {
163         exit_err(shift_active_win(world.wmeta, 'b'), err_shift);
164     }
165     else
166     {
167         return 0;
168     }
169     return 1;
170 }
171
172
173
174 extern uint8_t winkeyb_control(int key)
175 {
176     struct WinConf * wc = get_winconf_by_win(world.wmeta->active);
177     if      (key == get_available_keycode_to_action("w_keys_u"))
178     {
179         move_keyb_mod_selection(&wc->kb, 'u');
180     }
181     else if (key == get_available_keycode_to_action("w_keys_d"))
182     {
183         move_keyb_mod_selection(&wc->kb, 'd');
184     }
185     else if (key == get_available_keycode_to_action("w_keys_m"))
186     {
187         mod_selected_keyb(&wc->kb);
188     }
189     else
190     {
191         return 0;
192     }
193     return 1;
194 }
195
196
197
198 extern uint8_t meta_control(int key)
199 {
200     struct WinMeta * win_meta = world.wmeta;
201     struct Win * win_map      = get_win_by_id('m');
202     char * err_toggle = "Trouble with toggle_window() in meta_control().";
203     if      (key == get_available_keycode_to_action("quit"))
204     {
205         return 1;
206     }
207     else if (key == get_available_keycode_to_action("winconf"))
208     {
209         toggle_winconfig(world.wmeta->active);
210     }
211     else if (key == get_available_keycode_to_action("cyc_win_f"))
212     {
213         cycle_active_win(world.wmeta, 'f');
214     }
215     else if (key == get_available_keycode_to_action("cyc_win_b"))
216     {
217         cycle_active_win(world.wmeta, 'b');
218     }
219     else if (key == get_available_keycode_to_action("scrl_r"))
220     {
221         scroll_pad(win_meta, '+');
222     }
223     else if (key == get_available_keycode_to_action("scrl_l"))
224     {
225         scroll_pad(win_meta, '-');
226     }
227     else if (key == get_available_keycode_to_action("to_a_keywin"))
228     {
229        exit_err(toggle_window(win_meta, get_win_by_id('k')), err_toggle);
230     }
231     else if (key == get_available_keycode_to_action("to_g_keywin"))
232     {
233         exit_err(toggle_window(win_meta, get_win_by_id('0')), err_toggle);
234     }
235     else if (key == get_available_keycode_to_action("to_wg_keywin"))
236     {
237         exit_err(toggle_window(win_meta, get_win_by_id('1')), err_toggle);
238     }
239     else if (key == get_available_keycode_to_action("to_wk_keywin"))
240     {
241         exit_err(toggle_window(win_meta, get_win_by_id('2')), err_toggle);
242     }
243     else if (key == get_available_keycode_to_action("to_mapwin"))
244     {
245         exit_err(toggle_window(win_meta, win_map), err_toggle);
246     }
247     else if (key == get_available_keycode_to_action("to_infowin"))
248     {
249         exit_err(toggle_window(win_meta, get_win_by_id('i')), err_toggle);
250     }
251     else if (key == get_available_keycode_to_action("to_inv"))
252     {
253         exit_err(toggle_window(win_meta, get_win_by_id('c')), err_toggle);
254     }
255     else if (key == get_available_keycode_to_action("to_logwin"))
256     {
257         exit_err(toggle_window(win_meta, get_win_by_id('l')), err_toggle);
258     }
259     else if (key == get_available_keycode_to_action("save_conf"))
260     {
261         save_interface_conf();
262     }
263     else if (key == get_available_keycode_to_action("g_keys_u"))
264     {
265         move_keyb_mod_selection(&world.kb_global, 'u');
266     }
267     else if (key == get_available_keycode_to_action("g_keys_d"))
268     {
269         move_keyb_mod_selection(&world.kb_global, 'd');
270     }
271     else if (key == get_available_keycode_to_action("g_keys_m"))
272     {
273         mod_selected_keyb(&world.kb_global);
274     }
275     else if (key == get_available_keycode_to_action("wg_keys_u"))
276     {
277         move_keyb_mod_selection(&world.kb_wingeom, 'u');
278     }
279     else if (key == get_available_keycode_to_action("wg_keys_d"))
280     {
281         move_keyb_mod_selection(&world.kb_wingeom, 'd');
282     }
283     else if (key == get_available_keycode_to_action("wg_keys_m"))
284     {
285         mod_selected_keyb(&world.kb_wingeom);
286     }
287     else if (key == get_available_keycode_to_action("wk_keys_u"))
288     {
289         move_keyb_mod_selection(&world.kb_winkeys, 'u');
290     }
291     else if (key == get_available_keycode_to_action("wk_keys_d"))
292     {
293         move_keyb_mod_selection(&world.kb_winkeys, 'd');
294     }
295     else if (key == get_available_keycode_to_action("wk_keys_m"))
296     {
297         mod_selected_keyb(&world.kb_winkeys);
298     }
299     else if (key == get_available_keycode_to_action("map_u"))
300     {
301         map_scroll(win_map, world.map->size, NORTH);
302     }
303     else if (key == get_available_keycode_to_action("map_d"))
304     {
305         map_scroll(win_map, world.map->size, SOUTH);
306     }
307     else if (key == get_available_keycode_to_action("map_r"))
308     {
309         map_scroll(win_map, world.map->size, EAST);
310     }
311     else if (key == get_available_keycode_to_action("map_l"))
312     {
313         map_scroll(win_map, world.map->size, WEST);
314     }
315     else if (key == get_available_keycode_to_action("map_c"))
316     {
317         struct MapObj * player = get_player();
318         win_map->center = player->pos;
319     }
320     else if (key == get_available_keycode_to_action("inv_u"))
321     {
322         nav_inventory('u');
323     }
324     else if (key == get_available_keycode_to_action("inv_d"))
325     {
326         nav_inventory('d');
327     }
328     else if (key == get_available_keycode_to_action("reload_conf"))
329     {
330         unload_interface_conf();
331         load_interface_conf();
332     }
333     else if (key == get_available_keycode_to_action("winconf"))
334     {
335         toggle_winconfig(world.wmeta->active);
336     }
337     return 0;
338 }