/
baseCommand.c
280 lines (237 loc) · 8.29 KB
/
baseCommand.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//*****************************************************************************
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
#include <mtypes.h>
private string MaterialAttributes = "lib/modules/materialAttributes.c";
private string MessageParser = "/lib/core/messageParser.c";
private string *commands = ({});
/////////////////////////////////////////////////////////////////////////////
private nomask string prepCommandRegExp(string command)
{
string ret = regreplace(command, "\\] *\\[", ")*( ", 1);
ret = regreplace(ret, " \\[(.+)\\]", "( \\1)\*", 1);
return "^" + ret + "$";
}
/////////////////////////////////////////////////////////////////////////////
public nomask string commandRegExp()
{
string ret = 0;
if(sizeof(commands))
{
ret = "(";
foreach(string command in commands)
{
if (sizeof(ret) > 1)
{
ret += "|";
}
ret += prepCommandRegExp(command);
}
ret += ")";
ret = regreplace(ret, "##(Target|Environment|Item|Value)##", ".+", 1);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask int canExecuteCommand(string passedCommand)
{
int ret = 0;
if(passedCommand && stringp(passedCommand))
{
string commandRegexp = commandRegExp();
if(commandRegexp)
{
ret = sizeof(regexp(({ passedCommand }), commandRegexp));
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
protected nomask string commandString(string passedCommand)
{
string ret = 0;
if(sizeof(commands))
{
foreach(string command in commands)
{
string commandRegexp = prepCommandRegExp(command);
string commandCheck = regreplace(commandRegexp, "##(Target|Environment|Item|Value)##", ".+", 1);
if(sizeof(regexp(({ passedCommand }), commandCheck)))
{
ret = regreplace(commandRegexp, "##(Target|Environment|Item|Value)##", "", 1);
ret -="$";
break;
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
protected nomask object getTarget(object owner, string command)
{
object ret = 0;
string template = commandString(command);
if (command && stringp(command) && template && stringp(template) &&
environment(owner))
{
string *targetId = regexplode(command, template);
if (sizeof(targetId) == 3)
{
ret = present(targetId[2], environment(owner)) ||
present(targetId[2], owner);
if (!ret && environment(owner)->isEnvironmentalElement(targetId[2]))
{
ret = environment(owner)->getEnvironmentalElement(targetId[2]);
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
protected nomask string getTargetString(object owner, string command)
{
string ret = 0;
string template = commandString(command);
if (command && stringp(command) && template && stringp(template))
{
string *targetId = regexplode(command, template);
if (sizeof(targetId) == 3)
{
ret = targetId[2];
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public int execute(string command, object initiator)
{
return 0;
}
/////////////////////////////////////////////////////////////////////////////
public nomask int addCommandTemplate(string command)
{
int ret = 0;
if (member(commands, command) == -1)
{
commands += ({ command });
ret = 1;
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string displayUsageDetails()
{
string ret = "";
if (sizeof(commands))
{
string *commandText = ({});
foreach(string command in commands)
{
commandText += ({ regreplace(command, "##([^#]+)##", "<\\1>", 1) });
}
ret = implode(commandText, "\n ");
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask object messageParser()
{
return load_object(MessageParser);
}
/////////////////////////////////////////////////////////////////////////////
private nomask int isValidLiving(object livingCheck)
{
return (livingCheck && objectp(livingCheck) &&
(member(inherit_list(livingCheck), MaterialAttributes) > -1));
}
/////////////////////////////////////////////////////////////////////////////
protected nomask string formatText(string text, int colorInfo, object viewer)
{
return color(viewer->query("term"), viewer, colorInfo, format(text, 78));
}
/////////////////////////////////////////////////////////////////////////////
protected nomask string parseTemplate(string template, string perspective,
object initiator, object target)
{
string message = template;
// ##Infinitive::verb##
// ##InitiatorName## - Initiator's name
// ##InitiatorPossessive[::Name]## - Initiator possessive / your / Name's
// ##InitiatorObjective## - Initiator's objective / you
// ##InitiatorSubjective## - Initiator's subjective/pronoun / you
// ##Target[NPOS]## - Target's (one of above 4)
// ##HitDictionary## - random word from the hit dictionary (slash/slashes,
// chop/chops)
// ##SimileDictionary## - random word from the simile dictionary
// dictionary calls must be done first!
int isSecondPerson = (perspective == "initiator");
if(initiator && objectp(initiator))
{
message = messageParser()->parseSimileDictionary(message, initiator);
message = messageParser()->parseVerbDictionary(message,
"HitDictionary", initiator);
message = messageParser()->parseVerbs(message, isSecondPerson);
}
if(isValidLiving(initiator))
{
message = messageParser()->parseTargetInfo(message, "Initiator",
initiator, isSecondPerson);
}
if(isValidLiving(target))
{
isSecondPerson = (perspective == "target");
message = messageParser()->parseTargetInfo(message, "Target",
target, isSecondPerson);
}
message = messageParser()->capitalizeSentences(message);
return message;
}
/////////////////////////////////////////////////////////////////////////////
protected nomask varargs void displayMessage(string message, object initiator,
object target)
{
// This annoying loop handles the fact that everyone has different
// setting for color.
if (environment(initiator))
{
foreach(object person in all_inventory(environment(initiator)))
{
if (person && objectp(person))// && interactive(person))
{
string parsedMessage;
int colorInfo = C_EMOTES;
if (person == initiator)
{
parsedMessage = parseTemplate(message, "initiator", initiator,
target);
}
else if (person == target)
{
parsedMessage = parseTemplate(message, "target",
initiator, target);
}
else
{
parsedMessage = parseTemplate(message, "other",
initiator, target);
}
tell_object(person, formatText(parsedMessage, colorInfo,
person));
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
protected nomask void displayMessageToSelf(string message, object initiator)
{
if (initiator && objectp(initiator))
{
string parsedMessage;
int colorInfo = C_EMOTES;
parsedMessage = parseTemplate(message, "initiator", initiator,
initiator);
tell_object(initiator, formatText(message, colorInfo,
initiator));
}
}