Zesstra | b6ac9f6 | 2020-01-21 11:11:16 +0100 | [diff] [blame] | 1 | SYNOPSIS |
| 2 | #include <commands.h> |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 3 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 4 | void add_action(string|closure fun, string cmd) |
| 5 | void add_action(string|closure fun, string cmd, int flag) |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 6 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 7 | DESCRIPTION |
| 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 12 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 13 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 16 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 17 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 21 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 22 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 27 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 28 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 34 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 35 | Actions can also be removed on demand with the remove_actions() efun. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 36 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 37 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 40 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 41 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 47 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 48 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 51 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 52 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 54 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 55 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 59 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 60 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 61 | EXAMPLES |
| 62 | add_action("GoInside", "enter"); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 63 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 64 | When typing "enter" the function GoInside() will be invoked. |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 65 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 66 | add_action("DisFunc", "dis", AA_SHORT); |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 67 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 68 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 73 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 74 | add_action("DisFunc", "disconnect", AA_NOSPACE); |
Zesstra | b6ac9f6 | 2020-01-21 11:11:16 +0100 | [diff] [blame] | 75 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 76 | 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 User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 81 | |
MG Mud User | 88f1247 | 2016-06-24 23:31:02 +0200 | [diff] [blame] | 82 | |
Zesstra | 715ec20 | 2025-07-09 22:18:31 +0200 | [diff] [blame] | 83 | 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 | |
| 114 | HISTORY |
| 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 | |
| 121 | SEE ALSO |
| 122 | query_verb(E), query_command(E), remove_action(E), init(A) |