home · contact · privacy
Add "look" mode to query things on any cell via new THINGS_HERE command.
[plomrogue] / src / client / control.c
1 /* src/client/control.c
2  *
3  * This file is part of PlomRogue. PlomRogue is licensed under the GPL version 3
4  * or any later version. For details on its copyright, license, and warranties,
5  * see the file NOTICE in the root directory of the PlomRogue source package.
6  */
7
8 #include "control.h"
9 #include <stdint.h> /* uint8_t, uint16_t, uint32_t, UINT32_MAX */
10 #include <stdlib.h> /* free() */
11 #include <stdio.h> /* sprintf() */
12 #include <string.h> /* strlen(), strcmp(), strncmp() */
13 #include "../common/rexit.h" /* exit_err(), exit_trouble() */
14 #include "../common/try_malloc.h" /* try_malloc() */
15 #include "interface_conf.h" /* reload_interface_conf(), save_interface_conf() */
16 #include "io.h" /* send() */
17 #include "keybindings.h" /* get_command_to_keycode(), get_keycode_to_command(),
18                           * mod_selected_keyb(), move_keyb_selection()
19                           */
20 #include "map.h" /* map_scroll(), map_center(), toggle_autofocus(),
21                   * toggle_lookmode(), lookmode_nav()
22                   */
23 #include "wincontrol.h" /* shift_active_win(), resize_active_win(),
24                          * toggle_win_size_type(), toggle_window(),
25                          * cycle_active_win(), scroll_v_screen(),
26                          * toggle_linebreak_type(), toggle_winconfig()
27                          */
28 #include "windows.h" /* get_win_by_id() */
29 #include "world.h" /* world */
30
31
32
33 /* Move world.inventory_sel up ("dir"="u") or down (else) as far as possible. */
34 static void nav_inventory(char dir);
35
36 /* If "command"'s .dsc_short fits "match", apply "f" with provided char
37  * arguments and return 1; else, return 0.
38  */
39 static uint8_t try_0args(struct Command * command, char * match, void (* f) ());
40 static uint8_t try_1args(struct Command * command, char * match,
41                          void (* f) (char), char c);
42
43 /* If "command" fits pattern "keyb_XY" with Y a proper keybinding list ID char
44  * and X one of "u" (for "up"), "d" (for "down") or "m" (for "modify"), move up
45  * or down or modify entry in the selected keybinding list.
46  */
47 static uint8_t try_kb_manip(char * command);
48
49 /* Try if "command" matches a hard-coded list of client-only commands and, if
50  * successful, execute the match and return 1. Else, return 0.
51  */
52 static uint8_t try_client_commands(struct Command * command);
53
54 /* If c == c_to_match, set "string" to "string_to_set". */
55 static uint8_t set_string_if_char_match(char c, char c_to_match,
56                                         char ** string, char * string_to_set);
57
58 /* Transform "command" to server command + argument string (free externally). */
59 static char * build_server_message_with_argument(struct Command * command);
60
61 /* Try out "command" as one for server messaging; sending is .server_msg,
62  * followed by either a string representing "command"'s .arg, or, if .arg is
63  * 'i', world.player_inventory_select, or, if .arg is '0', nothing. Return 1 on
64  * success, 0 on failure.
65  */
66 static uint8_t try_server_commands(struct Command * command);
67
68
69
70 static void nav_inventory(char dir)
71 {
72     if ('u' == dir)
73     {
74         world.player_inventory_select = world.player_inventory_select
75                                         - (world.player_inventory_select > 0);
76         return;
77     }
78     uint8_t n_elems = 0;
79     uint32_t i;
80     char * err = "Inventory string is too large.";
81     exit_err(UINT32_MAX <= strlen(world.player_inventory), err);
82     for (i = 0; '\0' != world.player_inventory[i]; i++)
83     {
84         n_elems = n_elems + ('\n' == world.player_inventory[i]);
85     }
86     world.player_inventory_select = world.player_inventory_select
87                                     + (world.player_inventory_select < n_elems);
88 }
89
90
91
92 static uint8_t try_0args(struct Command * command, char * match, void (* f) ())
93 {
94     if (!strcmp(command->dsc_short, match))
95     {
96         f();
97         return 1;
98     }
99     return 0;
100 }
101
102
103
104 static uint8_t try_1args(struct Command * command, char * match,
105                              void (* f) (char), char c)
106 {
107     if (!strcmp(command->dsc_short, match))
108     {
109         f(c);
110         return 1;
111     }
112     return 0;
113 }
114
115
116
117 static uint8_t try_kb_manip(char * command)
118 {
119     char * cmp = "keyb_";
120     if (strlen(command) == strlen(cmp)+2 && !strncmp(command, cmp, strlen(cmp)))
121     {
122         if ('m' == command[strlen(cmp)])
123         {
124             mod_selected_keyb(command[strlen(cmp) + 1]);
125         }
126         else if ('u' == command[strlen(cmp)] || 'd' == command[strlen(cmp)])
127         {
128             move_keyb_selection(command[strlen(cmp) + 1], command[strlen(cmp)]);
129         }
130         return 1;
131     }
132     return 0;
133 }
134
135
136
137 static uint8_t try_client_commands(struct Command * command)
138 {
139     return (   try_0args(command, "map_c", map_center)
140             || try_0args(command, "to_autofocus", toggle_autofocus)
141             || try_1args(command, "map_u", map_scroll, '8')
142             || try_1args(command, "map_d", map_scroll, '2')
143             || try_1args(command, "map_r", map_scroll, '6')
144             || try_1args(command, "map_l", map_scroll, '4')
145             || try_1args(command, "inv_u", nav_inventory, 'u')
146             || try_1args(command, "inv_d", nav_inventory, 'd')
147             || try_1args(command, "cyc_win_f", cycle_active_win, 'f')
148             || try_1args(command, "cyc_win_b", cycle_active_win, 'b')
149             || try_1args(command, "scrl_r", scroll_v_screen, '+')
150             || try_1args(command, "scrl_l", scroll_v_screen, '-')
151             || try_1args(command, "to_g_keywin", toggle_window, '0')
152             || try_1args(command, "to_wg_keywin", toggle_window, '1')
153             || try_1args(command, "to_wk_keywin", toggle_window, '2')
154             || try_1args(command, "to_mapwin", toggle_window, 'm')
155             || try_1args(command, "to_infowin", toggle_window, 'i')
156             || try_1args(command, "to_inv", toggle_window, 'c')
157             || try_1args(command, "to_logwin", toggle_window, 'l')
158             || try_1args(command, "to_terrain", toggle_window, 's')
159             || try_0args(command, "winconf", toggle_winconfig)
160             || try_1args(command, "grow_h", resize_active_win, '*')
161             || try_1args(command, "shri_h", resize_active_win, '_')
162             || try_1args(command, "grow_v", resize_active_win, '+')
163             || try_1args(command, "shri_v", resize_active_win, '-')
164             || try_0args(command, "to_break", toggle_linebreak_type)
165             || try_1args(command, "to_height_t", toggle_win_size_type, 'y')
166             || try_1args(command, "to_width_t", toggle_win_size_type, 'x')
167             || try_1args(command, "shift_f", shift_active_win, 'f')
168             || try_1args(command, "shift_b", shift_active_win, 'b')
169             || try_0args(command, "reload_conf", reload_interface_conf)
170             || try_0args(command, "save_conf", save_interface_conf)
171             || try_0args(command, "to_look", toggle_lookmode)
172             || try_kb_manip(command->dsc_short));
173 }
174
175
176
177 static uint8_t set_string_if_char_match(char c, char c_to_match,
178                                         char ** string, char * string_to_set)
179 {
180     if (c == c_to_match)
181     {
182         *string = string_to_set;
183         return 1;
184     }
185     return 0;
186 }
187
188
189
190 static char * build_server_message_with_argument(struct Command * cmd)
191 {
192     uint8_t command_size = strlen(cmd->server_msg);
193     char * arg_str = "";
194     uint8_t arg_size = 0;
195     if ('i' == cmd->arg)
196     {
197         arg_size = 3;
198         arg_str = try_malloc(arg_size + 1, __func__);
199         int test = sprintf(arg_str, "%d",world.player_inventory_select);
200         exit_trouble(test < 0, __func__, "sprintf");
201     }
202     else if (   set_string_if_char_match(cmd->arg, 'd', &arg_str, "east")
203              || set_string_if_char_match(cmd->arg, 'c', &arg_str, "south-east")
204              || set_string_if_char_match(cmd->arg, 'x', &arg_str, "south-west")
205              || set_string_if_char_match(cmd->arg, 's', &arg_str, "west")
206              || set_string_if_char_match(cmd->arg, 'w', &arg_str, "north-west")
207              || set_string_if_char_match(cmd->arg, 'e', &arg_str, "north-east"))
208     {
209         arg_size = strlen(arg_str);
210     }
211     else
212     {
213         exit_err(1, "Illegal server command argument.");
214     }
215     char * msg = try_malloc(command_size + 1 + arg_size + 1, __func__);
216     int test = sprintf(msg, "%s %s", cmd->server_msg, arg_str);
217     exit_trouble(test < 0, __func__, "sprintf");
218     if ('i' == cmd->arg)
219     {
220         free(arg_str);
221     }
222     return msg;
223 }
224
225
226
227 static uint8_t try_server_commands(struct Command * command)
228 {
229     if (command->server_msg)
230     {
231         if ('0' == command->arg)
232         {
233             send(command->server_msg);
234         }
235         else
236         {
237             char * msg = build_server_message_with_argument(command);
238             send(msg);
239             free(msg);
240         }
241         return 1;
242     }
243     return 0;
244 }
245
246
247
248 extern uint8_t try_key(uint16_t key)
249 {
250     struct Command * command = get_command_to_keycode(&world.kb_global, key);
251     if (!command && world.winDB.active)
252     {
253         struct Win * w = get_win_by_id(world.winDB.active);
254         if      (0 == w->view)
255         {
256             command = get_command_to_keycode(&w->kb, key);
257         }
258         else if (1 == w->view)
259         {
260             command = get_command_to_keycode(&world.kb_wingeom, key);
261         }
262         else if (2 == w->view)
263         {
264             command = get_command_to_keycode(&world.kb_winkeys, key);
265         }
266     }
267     if (command)
268     {
269         if (world.look && lookmode_nav(command->dsc_short))
270         {
271             return 1;
272         }
273         else if (try_client_commands(command))
274         {
275             return 1;
276         }
277         else if (try_server_commands(command))
278         {
279             return 1;
280         }
281         else if (!strcmp("quit", command->dsc_short))
282         {
283             return 2;
284         }
285     }
286     return 0;
287 }