Debian Codegraph @

src/doc/command.txt

raw
1 Angband's command system
2 ========================
3
4 The command system in Angband is one of the two key ways that the game stays separate from the interface that displays it. The other is the event system, which is currently underutilised.
5
6 The idea beyond the command system is that every action the player can perform in-game is encoded as an atomic command sent to the game. These commands form the basic dialect of Angband.
7
8 Note that the game command system does not cover 'UI actions', such as adjusting the game panel, looking around, and similar. Targetting can be accomplished by sending a command ("target monster:'small kobold'") but the UI has to do the work to allow that command to be formed (allow the cursor to scroll around the screen, show what monster is currently highlighted, etc. - these things do not fall within the remit of the game core).
9
10
11 The anatomy of a command
12 ------------------------
13
14 A command is made up of:
15 * a command type, e.g. 'eat', 'read'
16 * a list of arguments, which have a type and a name
17
18 Arguments can have any of the following types:
19 || null | no data held
20 || string | a string encoded in UTF-8
21 || choice | an integer indicating a selection from a known set of values
22 || item | a pointer to the struct object describing an item or stack
23 || number | an integer indicating a quantity, power, level, ...
24 || direction | one of the 9 directions
25 || target | one of the directions or -1 for a target
26 || point | a struct loc referring to a position in the current level
27
28 In C a command is represented by the type 'struct command'. Command arguments can be set using the cmd_set_arg_*() functions and can be retrieved with the
29 cmd_get_arg_*() functions.
30
31 Limitations:
32 There can be at most 4 parameters.
33 The lifetimes for arguments referenced through pointers (i.e. string and item)
34 has to be at least as long as the lifetime of the command. The command system
35 provides little to help control the lifetime of the arguments and to prevent
36 either dangling references or leaks of dynamically allocated resources.
37
38
39 Command handlers
40 ----------------
41
42 Each command type has a command handler function, which takes the command and acts upon it. A command handler must take the form:
43
44 void command_handler(struct command *);
45
46 There is a table, a file-level static named game_cmds in cmd-core.c, that
47 relates command types to command values.
48
49 Inside the command handler, code can access the arguments using the cmd_get_arg_*() functions. Those will return CMD_OK if there's a parameter matching the name and type provided. The command handler could also use the cmd_get_{direction,target,item,quantity,string,spell,effect_from_list}() functions which will use the cmd_get_arg_*() functions when the argument is available and will query the user interface for it when it is not. In the case where the argument was already set or was successfully queried from the user interface, those will return CMD_OK, record the selection made in the command, and pass back the requested value. In the case where the user interface aborts the selection, those will return CMD_ARG_ABORTED.
50
51 If the data type has a validity filter (e.g. cmd_get_item() has both an item type filter and a mode argument to constrain possible item locations) then the getter will ensure that the argument returned satisfies that filter.
52
53 For example:
54
55 /* Gain a random spell from the given book (for priests) */
56 void do_cmd_study_book(struct command *cmd) {
57 struct object *book_obj;
58
59 if (cmd_get_item(
60 /* Our command */ cmd,
61 /* Name of parameter we're looking for */ "item",
62 /* Item to return */ &book_obj,
63 /* Prompt */ "Study which book? ",
64 /* Error */ "You cannot learn any new spells from the books you have.",
65 /* Only allow objects passing this filter */ obj_can_study,
66 /* Where are valid choices? */ USE_INVEN | USE_FLOOR) != CMD_OK);
67 return;
68
69 /* ... learn from 'item' */
70 }
71
72
73
74
75 UI handlers
76 -----------
77
78 game-input.h declares a set function pointers (get_string_hook is one) for requesting information from the player. A UI assigns a specific implementation to a hook (i.e. "get_string_hook = textui_get_string;") to supply its own way of requesting that information. The hooks that are used by the core of the command system are get_aim_dir_hook, get_effect_from_list_hook, get_item_hook, get_quantity_hook, get_rep_dir_hook, get_spell_hook, get_spell_from_book_hook, and get_string_hook. The only dispatcher for those hooks which does something potentially useful when no hook is provided is get_effect_from_list(): when get_effect_from_list_hook is NULL it will use a random selection if that is allowed.
79
80
81 Partial list of commands and their arguments
82 --------------------------------------------
83
84 "activate"
85 Constant: CMD_ACTIVATE
86 Arguments:
87 - 'item' (type item): reference to the item to activate
88
89 "aim"
90 Constant: CMD_USE_WAND
91 Arguments:
92 - 'item' (type item): the stack of wands to use
93
94 "alter"
95 Constant: CMD_ALTER
96 Arguments:
97 - 'direction' (type direction): the direction to alter
98
99 "autoinscribe"
100 Constant: CMD_AUTOINSCRIBE
101 Arguments:
102 - none
103
104 "autopickup"
105 Constant: CMD_AUTOPICKUP
106 Arguments:
107 - none
108
109 "buy"
110 Constant: CMD_BUY
111 Arguemnts:
112 - 'item' (type item): the stack from which to buy
113 - 'quantity' (type number): the number of items to buy from the stack
114
115 "cast"
116 Constant: CMD_CAST
117 Arguments:
118 - 'spell' (type choice): index of the spell to cast
119 - 'book' (type item): reference to the stack of books to use; only accessed if 'spell' is not set
120
121 "close"
122 Constant: CMD_CLOSE
123 Arguments:
124 - 'direction' (type direction): the direction to close
125
126 "disarm"
127 Constant: CMD_DISARM
128 Arguments:
129 - 'direction' (type direction): the direction to disarm
130
131 "drop"
132 Constant: CMD_DROP
133 Arguments:
134 - 'item' (type item): reference to that stack from which to drop
135 - 'quantity' (type number): the number of items to drop
136
137 "eat"
138 Constant: CMD_EAT
139 Arguments:
140 - 'item' (type item): the stack of food to use
141
142 "fire"
143 Constant: CMD_FIRE
144 Arguments:
145 - 'item' (type item): the stack of ammunition to use
146 - 'target' (type target): the direction or target for the attack
147
148 "go down stairs"
149 Constant: CMD_GO_DOWN
150 Arguments:
151 - none
152
153 "go up stairs"
154 Constant: CMD_GO_UP
155 Arguments:
156 - none
157
158 "hold"
159 Constant: CMD_HOLD
160 Arguments:
161 - none
162
163 "inscribe"
164 Constant: CMD_INSCRIBE
165 Arguments:
166 - 'item' (type item): the stack to inscribe
167 - 'inscription' (type string): the inscription to use
168
169 "jump"
170 Constant: CMD_JUMP
171 Arguments:
172 - 'direction' (type direction): the direction to walk
173
174 "open"
175 Constant: CMD_OPEN
176 Arguments:
177 - 'direction' (type direction): the direction to open
178
179 "pickup"
180 Constant: CMD_PICKUP
181 Arguments:
182 - 'item' (type item): the item to pickup
183
184 "quaff"
185 Constant: CMD_QUAFF
186 Arguments:
187 - 'item' (type item): the stack of potions to use
188
189 "read"
190 Constant: CMD_READ_SCROLL
191 Arguments:
192 - 'item' (type item): the stack of scrolls to use
193
194 "refuel with"
195 Constant: CMD_REFILL
196 Arguments:
197 - 'item' (type item): the stack to use to refuel the currently equipped light
198
199 "rest"
200 Constant: CMD_REST
201 Arguments:
202 - 'choice' (type choice): is either the number of turns to rest or one of the special values given by the REST_* constants in player-util.h
203
204 "retrieve"
205 Constant: CMD_RETRIEVE
206 Arguments:
207 - 'item' (type item): the stack to retrieve
208 - 'quantity' (type number): the number of items to retrieve from the stack
209
210 "run"
211 Constant: CMD_RUN
212 Arguments:
213 - 'direction' (type direction): the direction to move
214
215 "sell"
216 Constant: CMD_SELL
217 Arguments:
218 - 'item' (type item): the stack from which to sell
219 - 'quantity' (type number): the number of items to sell from the stack
220
221 "sleep"
222 Constant: CMD_SLEEP
223 Arguments:
224 - none
225
226 "stash"
227 Constant: CMD_STASH
228 Arguments:
229 - 'item' (type item): the stack to stash
230 - 'quantity' (type number): the number of items from the stack to stash
231
232 "steal"
233 Constant: CMD_STEAL
234 Arguments:
235 - 'direction' (type direction): the direction to steal from
236
237 "take off"
238 Constant: CMD_TAKEOFF
239 Arguments:
240 - 'item' (type item): reference to the equipped item to remove
241
242 "throw"
243 Constant: CMD_THROW
244 Arguments:
245 - 'item' (type item): the stack of items from which to draw one for throwing
246 - 'target' (type target): the direction or target for the attack
247
248 "tunnel"
249 Constant: CMD_TUNNEL
250 Arguments:
251 - 'direction (type direction): the direction to tunnel
252
253 "un-inscribe"
254 Constant: CMD_UNINSCRIBE
255 Arguments:
256 - 'item' (type item): the stack to uninscribe
257
258 "use"
259 Constant: CMD_USE_STAFF
260 Arguments:
261 - 'item' (type item): the stack of staffs to use
262
263 "walk"
264 Constant: CMD_WALK
265 - 'direction' (type direction): the direction to move
266
267 "walk"
268 Constant: CMD_PATHFIND
269 - 'point' (type point): the destination to run to
270
271 "wield"
272 Constant: CMD_WIELD
273 - 'item' (type item): the stack from which to draw one item
274 - 'replace' (type item): the ring to replace; only used if 'item' is a ring and all ring slots are filled
275
276 "zap"
277 Constant: CMD_USE_ROD
278 Arguments:
279 - 'item' (type item): the stack of rods to use
280
281
282 Design problems to be resolved
283 ------------------------------
284
285 Besides the limitations mentioned above for the arguments, the logic for
286 handling repeated commands is fragile: various places call cmd_disable_repeat()
287 or cmd_disable_repeat_floor_item() when they think a change would prevent
288 repetition of a command. Some of those are unduly pessimistic (if a stack in the inventory goes away it only would prevent command repetition if that stack was referenced by a command). Others are not strict enough (https://github.com/angband/angband/issues/5122 is an example of an open issue at the time this was written). Tnere may be places that should be calling cmd_disable_repeat() which do not.
289
290 If commands referred to objects by tval, sval, and location (inventory or floor) that perhaps could help with some of the repetition issues or possibilities for dangling object references. It could also allow for keymaps to be replaced by commands chains in the text files, since you could then specify "item:inventory:tval=mushroom,sval=Blue" instead of "item:-4", and similarly:
291
292 map '2' => { walk(direction: down) }
293 map 'M' => { cast(spell: "Magic Missile") }
294 map '^E' => { read(item: "Phase Door") && quaff(item: "Healing") }
295
296
297
298 Implementation plan
299 -------------------
300
301 DONE:
302
303 0a. Move check_state somewhere useful, refactor it so that its data is kept in 3rd normal form.
304 0b. Move cmd2.c(count_feats + coords_to_dir) => cave.c
305
306 1. Clarify split between command and command queue functions (prefixes cmd_ and cmdq_).
307
308 2. Refactor get_item() to not use item_tester_hook.
309
310 3. Change the argument system
311
312 3a. Replace 'cmd_code code, cmd_arg args[]' in command handlers with 'struct command *', remove direct arg access (replace with a halfway house - e.g. cmd_get_arg_item(cmd, 0) )
313
314 3b. Slowly switch things across to using the new system, which allows compilation and testing, without breaking things. This involves relocating the 'fill-in' code from game-cmd.c to the various do_cmd_* functions as well as writing new cmd_get_* functions that check validity of inputs, etc. as above.
315
316 3c. Switch from numbered to named arguments.
317
318 NOT DONE:
319
320 4. Reimplement the following in terms of a core-UI split:
321
322 - ignore
323
324 This is currently almost entirely done in UI.
325
326 - resting
327
328 Instead of having magic numbers and using the 'choice' type, it should take strings (e.g. "*" or "mana" or "hp") or numbers.
329
330 - pickup
331
332 This is currently just a "pickup" command. It should be separated into a "pick up item x" command and the UI frontend to it. This might be impossible until we get an event-driven game.
333
334 - targetting
335
336 This currently doesn't use the command system at all.
337
338
339 5. Reorganise files:
340
341 {cmd-misc.c + cmd-cave.c(textui_cmd_rest)} -> [ui-misc.c XXX]
342 cmd-pickup.c(move_player) -> cmd-cave.c
343
344
345 Later
346 -----
347
348 The get_*() functions in game-input.c have been refactored so they act as dispatchers to the appropriate UI handler. In some cases (get_quantity(), for instance, which could be implented based on get_string()), it may be desirable to provide a default implementation when the UI does not have one since that could save some code repetition between different UIs.