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