blob: 000bf545c245646ea2952320dbea1396d1fb9627 [file] [log] [blame]
Zesstrab6ac9f62020-01-21 11:11:16 +01001SYNOPSIS
2 #include <commands.h>
MG Mud User88f12472016-06-24 23:31:02 +02003
Zesstra715ec202025-07-09 22:18:31 +02004 void add_action(string|closure fun, string cmd)
5 void add_action(string|closure fun, string cmd, int flag)
MG Mud User88f12472016-06-24 23:31:02 +02006
Zesstra715ec202025-07-09 22:18:31 +02007DESCRIPTION
8 Set up a local function fun to be called when user input
9 matches the command cmd. Functions called by a user command
10 will get the arguments as a string. It must then return 0 if
11 it was the wrong command, otherwise 1.
MG Mud User88f12472016-06-24 23:31:02 +020012
Zesstra715ec202025-07-09 22:18:31 +020013 If it was the wrong command, the parser will continue
14 searching for another command, until one returns 1 or give an
15 error message to the user.
MG Mud User88f12472016-06-24 23:31:02 +020016
Zesstra715ec202025-07-09 22:18:31 +020017 For example, there can be a wand and a rod. Both of these
18 objects define as command "wave". One of them will be randomly
19 called first, and it must look at the argument, and match
20 against "wand" or "rod" respectively.
MG Mud User88f12472016-06-24 23:31:02 +020021
Zesstra715ec202025-07-09 22:18:31 +020022 The function associated to a command will be called with a
23 string as argument which stands for the given words behind the
24 typed command. The verb entered can be retrieved using the
25 query_verb() efun and is always the first word in the input
26 line up to the first space.
MG Mud User88f12472016-06-24 23:31:02 +020027
Zesstra715ec202025-07-09 22:18:31 +020028 Always have add_action() called only from an init() routine.
29 The object defining these commands must be present to the
30 user, either being the user, being carried by the user,
31 being the room around the user, or being an object in the
32 same room as the user. If the player leaves this vicinity of the
33 object, the actions are automatically removed.
MG Mud User88f12472016-06-24 23:31:02 +020034
Zesstra715ec202025-07-09 22:18:31 +020035 Actions can also be removed on demand with the remove_actions() efun.
MG Mud User88f12472016-06-24 23:31:02 +020036
Zesstra715ec202025-07-09 22:18:31 +020037 If argument <flag> is AA_SHORT (1), then the arguments may
38 follow the verb without separating space. Any arguments after
39 the first space are passed as argument string.
MG Mud User88f12472016-06-24 23:31:02 +020040
Zesstra715ec202025-07-09 22:18:31 +020041 If argument <flag> is AA_NOSPACE (2), then again the arguments
42 may follow the verb without separating space. In contrast to
43 AA_SHORT, all characters following the verb are passed as
44 the argument string. However, note that the characters immediately
45 following the given verb are passed as argument AND as result
46 of query_verb().
MG Mud User88f12472016-06-24 23:31:02 +020047
Zesstra715ec202025-07-09 22:18:31 +020048 If argument <flag> is AA_IMM_ARGS (3), then again the arguments
49 may follow the verb without separating space. All characters following
50 the given verb are passed as argument, and only as argument.
MG Mud User88f12472016-06-24 23:31:02 +020051
Zesstra715ec202025-07-09 22:18:31 +020052 If argument <flag> is negative, the verb given by the player
53 has to match only the leading -<flag> characters of the verb <cmd>.
MG Mud User88f12472016-06-24 23:31:02 +020054
Zesstra715ec202025-07-09 22:18:31 +020055 Never use one of the functions 'create' 'reset' 'init' 'exit'
56 'heart_beat' etc as the first argument to add_action(). In
57 general, a function with a name defined in /doc/applied should
58 have the behaviour defined there.
MG Mud User88f12472016-06-24 23:31:02 +020059
MG Mud User88f12472016-06-24 23:31:02 +020060
Zesstra715ec202025-07-09 22:18:31 +020061EXAMPLES
62 add_action("GoInside", "enter");
MG Mud User88f12472016-06-24 23:31:02 +020063
Zesstra715ec202025-07-09 22:18:31 +020064 When typing "enter" the function GoInside() will be invoked.
MG Mud User88f12472016-06-24 23:31:02 +020065
Zesstra715ec202025-07-09 22:18:31 +020066 add_action("DisFunc", "dis", AA_SHORT);
MG Mud User88f12472016-06-24 23:31:02 +020067
Zesstra715ec202025-07-09 22:18:31 +020068 Whenever you type in a command which starts with "dis" the
69 function DisFunc() will be called. To get the real word which
70 was typed in (because until now you only know that it was a
71 command beginning with "dis") you have to call the efun
72 query_verb().
MG Mud User88f12472016-06-24 23:31:02 +020073
Zesstra715ec202025-07-09 22:18:31 +020074 add_action("DisFunc", "disconnect", AA_NOSPACE);
Zesstrab6ac9f62020-01-21 11:11:16 +010075
Zesstra715ec202025-07-09 22:18:31 +020076 The function DisFunc() will be called for all commands which
77 use "disconnect" or a shortened form like "di", "dis" or
78 "discon" as verb. The command 'disconnecting' will _not_
79 be recognized. To get the real word which was typed in
80 you have to call the efun query_verb().
MG Mud User88f12472016-06-24 23:31:02 +020081
MG Mud User88f12472016-06-24 23:31:02 +020082
Zesstra715ec202025-07-09 22:18:31 +020083 add_action("...", "cmd");
84 add_action("...", "xcmd", AA_SHORT);
85 add_action("...", "scmd", AA_NOSPACE);
86 add_action("...", "icmd", AA_IMM_ARGS);
87
88 When given the following commands, the driver will parse it
89 as described below. 'verb' is what query_verb() would return,
90 'arg' is what would be passed to the command function.
91
92 "cmd" -> verb "cmd", arg 0
93 "cmd foo bar" -> verb "cmd", arg "foo bar"
94
95 "xcmd" -> verb "xcmd", arg 0
96 "xcmd foo" -> verb "xcmd", arg "foo"
97 "xcmdfoo" -> verb "xcmdfoo", arg 0
98 "xcmd foo bar" -> verb "xcmd", arg "foo bar"
99 "xcmdfoo bar" -> verb "xcmdfoo", arg "bar"
100
101 "scmd" -> verb "scmd", arg 0
102 "scmd foo" -> verb "scmd", arg " foo"
103 "scmdfoo" -> verb "scmdfoo", arg "foo"
104 "scmd foo bar" -> verb "scmd", arg " foo bar"
105 "scmdfoo bar" -> verb "scmdfoo", arg "foo bar"
106
107 "icmd" -> verb "icmd", arg 0
108 "icmd foo" -> verb "icmd", arg " foo"
109 "icmdfoo" -> verb "icmd", arg "foo"
110 "icmd foo bar" -> verb "icmd", arg " foo bar"
111 "icmdfoo bar" -> verb "icmd", arg "foo bar"
112
113
114HISTORY
115 The flag < 0 argument was supported since 3.2@127, but not
116 really implemented before LDMud 3.2.8.
117 LDMud 3.2.9 introduced the AA_IMM_ARGS flag.
118 LDMud 3.3 removed the historical add_action(fun) notation.
119 Since LDMud 3.5 the function can be given as a closure.
120
121SEE ALSO
122 query_verb(E), query_command(E), remove_action(E), init(A)