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