1 /**
2  * \file cmd-core.h
3  * \brief Handles the queueing of game commands.
4  *
5  * Copyright (c) 2008-9 Antony Sidwell
6  * Copyright (c) 2014 Andi Sidwell
7  *
8  * This work is free software; you can redistribute it and/or modify it
9  * under the terms of either:
10  *
11  * a) the GNU General Public License as published by the Free Software
12  *    Foundation, version 2, or
13  *
14  * b) the "Angband licence":
15  *    This software may be copied and distributed for educational, research,
16  *    and not for profit purposes provided that this copyright and statement
17  *    are included in all such copies.  Other copyrights may also apply.
18  */
19 
20 #ifndef INCLUDED_CMD_CORE_H
21 #define INCLUDED_CMD_CORE_H
22 
23 #include "object.h"
24 #include "z-type.h"
25 
26 /**
27  * All valid game commands.  Not all implemented yet.
28  */
29 typedef enum cmd_code {
30 	CMD_NULL = 0,	/* A "do nothing" command so that there's something
31 					   UIs can use as a "no command yet" sentinel. */
32 	/*
33 	 * Splash screen commands
34 	 */
35 	CMD_LOADFILE,
36 	CMD_NEWGAME,
37 
38 	/*
39 	 * Birth commands
40 	 */
41 	CMD_BIRTH_INIT,
42 	CMD_BIRTH_RESET,
43 	CMD_CHOOSE_RACE,
44 	CMD_CHOOSE_CLASS,
45 	CMD_BUY_STAT,
46 	CMD_SELL_STAT,
47 	CMD_RESET_STATS,
48 	CMD_ROLL_STATS,
49 	CMD_PREV_STATS,
50 	CMD_NAME_CHOICE,
51 	CMD_HISTORY_CHOICE,
52 	CMD_ACCEPT_CHARACTER,
53 
54 	/*
55 	 * The main game commands
56 	 */
57 	CMD_GO_UP,
58 	CMD_GO_DOWN,
59 	CMD_WALK,
60 	CMD_JUMP,
61 	CMD_PATHFIND,
62 
63 	CMD_INSCRIBE,
64 	CMD_UNINSCRIBE,
65 	CMD_AUTOINSCRIBE,
66 	CMD_TAKEOFF,
67 	CMD_WIELD,
68 	CMD_DROP,
69 	CMD_BROWSE_SPELL,
70 	CMD_STUDY,
71 	CMD_CAST, /* Casting a spell /or/ praying. */
72 	CMD_USE_STAFF,
73 	CMD_USE_WAND,
74 	CMD_USE_ROD,
75 	CMD_ACTIVATE,
76 	CMD_EAT,
77 	CMD_QUAFF,
78 	CMD_READ_SCROLL,
79 	CMD_REFILL,
80 	CMD_USE,
81 	CMD_FIRE,
82 	CMD_THROW,
83 	CMD_PICKUP,
84 	CMD_AUTOPICKUP,
85 	CMD_IGNORE,
86 	CMD_DISARM,
87 	CMD_REST,
88 	CMD_TUNNEL,
89 	CMD_OPEN,
90 	CMD_CLOSE,
91 	CMD_RUN,
92 	CMD_HOLD,
93 	CMD_ALTER,
94 	CMD_STEAL,
95 	CMD_SLEEP,
96 
97     /* Store commands */
98 	CMD_SELL,
99 	CMD_BUY,
100 	CMD_STASH,
101 	CMD_RETRIEVE,
102 
103 	/* Hors categorie Commands */
104 	CMD_SUICIDE,
105 
106 	CMD_HELP,
107 	CMD_REPEAT,
108 	CMD_COMMAND_MONSTER
109 } cmd_code;
110 
111 typedef enum cmd_context {
112 	CTX_INIT,
113 	CTX_BIRTH,
114 	CTX_GAME,
115 	CTX_STORE,
116 	CTX_DEATH
117 } cmd_context;
118 
119 /**
120  * ------------------------------------------------------------------------
121  * Argument structures
122  * ------------------------------------------------------------------------ */
123 
124 /**
125  * The data of the argument
126  */
127 union cmd_arg_data {
128 	const char *string;
129 
130 	int choice;
131 	struct object *obj;
132 	int number;
133 	int direction;
134 
135 	struct loc point;
136 };
137 
138 /**
139  * The type of the data
140  */
141 enum cmd_arg_type {
142 	arg_NONE = 0,
143 	arg_STRING = 1,
144 	arg_CHOICE,
145 	arg_ITEM,
146 	arg_NUMBER,
147 	arg_DIRECTION,
148 	arg_TARGET,
149 	arg_POINT
150 };
151 
152 /**
153  * A single argument
154  */
155 struct cmd_arg {
156 	enum cmd_arg_type type;
157 	union cmd_arg_data data;
158 	char name[20];		/* Better than dynamic allocation */
159 };
160 
161 
162 /**
163  * Maximum number of arguments a command needs to take.
164  */
165 #define CMD_MAX_ARGS 4
166 
167 
168 
169 /**
170  * The struct command type is used to return details of the command the
171  * game should carry out.
172  *
173  * 'command' should always have a valid cmd_code value, the other entries
174  * may or may not be significant depending on the command being returned.
175  */
176 struct command {
177 	/* What context this is happening in */
178 	cmd_context context;
179 
180 	/* A valid command code. */
181 	cmd_code code;
182 
183 	/* Number of times to attempt to repeat command. */
184 	int nrepeats;
185 
186 	/* Arguments */
187 	struct cmd_arg arg[CMD_MAX_ARGS];
188 };
189 
190 
191 /**
192  * Return codes for cmd_get_arg()
193  */
194 enum cmd_return_codes {
195 	CMD_OK = 0,
196 	CMD_ARG_NOT_PRESENT = -1,
197 	CMD_ARG_WRONG_TYPE = -2,
198 	CMD_ARG_ABORTED = -3
199 };
200 
201 
202 /**
203  * Command handlers will take a pointer to the command structure
204  * so that they can access any arguments supplied.
205  */
206 typedef void (*cmd_handler_fn)(struct command *cmd);
207 
208 
209 /**
210  * ------------------------------------------------------------------------
211  * Command type functions
212  * ------------------------------------------------------------------------ */
213 
214 /* Return the verb that goes alongside the given command. */
215 const char *cmd_verb(cmd_code cmd);
216 
217 
218 /**
219  * ------------------------------------------------------------------------
220  * Command queue functions
221  * ------------------------------------------------------------------------ */
222 
223 /**
224  * Returns the top command on the queue.
225  */
226 struct command *cmdq_peek(void);
227 
228 /**
229  * A function called by the game to get a command from the UI.
230  */
231 extern errr (*cmd_get_hook)(cmd_context c);
232 
233 /**
234  * Gets the next command from the queue and processes it
235  */
236 bool cmdq_pop(cmd_context c);
237 
238 /**
239  * Insert commands in the queue.
240  */
241 errr cmdq_push_copy(struct command *cmd);
242 errr cmdq_push_repeat(cmd_code c, int nrepeats);
243 errr cmdq_push(cmd_code c);
244 
245 
246 /**
247  * Process all commands presently in the queue.
248  */
249 void cmdq_execute(cmd_context ctx);
250 
251 /**
252  * Remove all commands from the queue.
253  */
254 void cmdq_flush(void);
255 
256 /**
257  * Return true if the previous command used an item from the floor.
258  * Otherwise, return false.
259  */
260 bool cmdq_does_previous_use_floor_item(void);
261 
262 /**
263  * ------------------------------------------------------------------------
264  * Command repeat manipulation
265  * ------------------------------------------------------------------------ */
266 
267 /**
268  * Remove any pending repeats from the current command.
269  */
270 void cmd_cancel_repeat(void);
271 
272 /**
273  * Update the number of repeats pending for the current command.
274  */
275 void cmd_set_repeat(int nrepeats);
276 
277 /**
278  * Call to disallow the current command from being repeated with the
279  * "Repeat last command" command.
280  */
281 void cmd_disable_repeat(void);
282 
283 /**
284  * Returns the number of repeats left for the current command.
285  * i.e. zero if not repeating.
286  */
287 int cmd_get_nrepeats(void);
288 
289 
290 
291 /**
292  * ------------------------------------------------------------------------
293  * Command type functions
294  * ------------------------------------------------------------------------ */
295 
296 /**
297  * Set the args of a command.
298  */
299 void cmd_set_arg_choice(struct command *cmd, const char *arg, int choice);
300 void cmd_set_arg_string(struct command *cmd, const char *arg, const char *str);
301 void cmd_set_arg_direction(struct command *cmd, const char *arg, int dir);
302 void cmd_set_arg_target(struct command *cmd, const char *arg, int target);
303 void cmd_set_arg_point(struct command *cmd, const char *arg, struct loc grid);
304 void cmd_set_arg_item(struct command *cmd, const char *arg, struct object *obj);
305 void cmd_set_arg_number(struct command *cmd, const char *arg, int amt);
306 
307 
308 /**
309  * Get the args of a command.
310  */
311 int cmd_get_arg_choice(struct command *cmd, const char *arg, int *choice);
312 int cmd_get_arg_string(struct command *cmd, const char *arg, const char **str);
313 int cmd_get_arg_direction(struct command *cmd, const char *arg, int *dir);
314 int cmd_get_arg_target(struct command *cmd, const char *arg, int *target);
315 int cmd_get_arg_point(struct command *cmd, const char *arg, struct loc *grid);
316 int cmd_get_arg_item(struct command *cmd, const char *arg, struct object **obj);
317 int cmd_get_arg_number(struct command *cmd, const char *arg, int *amt);
318 
319 /**
320  * Try a bit harder.
321  */
322 int cmd_get_direction(struct command *cmd, const char *arg, int *dir,
323 					  bool allow_5);
324 int cmd_get_target(struct command *cmd, const char *arg, int *target);
325 int cmd_get_item(struct command *cmd, const char *arg, struct object **obj,
326 				 const char *prompt, const char *reject, item_tester filter,
327 				 int mode);
328 int cmd_get_quantity(struct command *cmd, const char *arg, int *amt, int max);
329 int cmd_get_string(struct command *cmd, const char *arg, const char **str,
330 				   const char *initial, const char *title, const char *prompt);
331 int cmd_get_spell(struct command *cmd, const char *arg, int *spell,
332 				  const char *verb, item_tester book_filter, const char *error,
333 				  bool (*spell_filter)(int spell));
334 
335 #endif
336