forked from realms-mud/core-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
messageParser.c
444 lines (404 loc) · 14.8 KB
/
messageParser.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//*****************************************************************************
// Class: messageParser
// File Name: messageParser.c
//
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
private string MaterialAttributes = "lib/modules/materialAttributes.c";
/////////////////////////////////////////////////////////////////////////////
private nomask string formatText(string text, int colorInfo, object viewer)
{
return color(viewer->query("term"), viewer, colorInfo, format(text, 78));
}
/////////////////////////////////////////////////////////////////////////////
private nomask int isValidAttacker(object attacker)
{
return (attacker && objectp(attacker) &&
(member(inherit_list(attacker), MaterialAttributes) > -1));
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getName(object actor, int isSecondPerson)
{
string ret = "you";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = capitalize(actor->RealName());
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getObjective(object actor, int isSecondPerson)
{
string ret = "you";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = actor->Objective();
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getSubjective(object actor, int isSecondPerson)
{
string ret = "you";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = actor->Pronoun();
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getPossessiveName(object actor, int isSecondPerson)
{
string ret = "your";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = sprintf("%s's", actor->Name());
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getPossessive(object actor, int isSecondPerson)
{
string ret = "your";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = actor->Possessive();
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask varargs string getReflexive(object actor, int isSecondPerson)
{
string ret = "yourself";
if(!isSecondPerson && isValidAttacker(actor))
{
ret = actor->Reflexive();
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string getBodyPart(object actor)
{
// This breaks down for weird creatures.... need to fix eventually.
string *parts = ({ "head", "torso", "mouth", "stomach", "abdomen", "chest", "back", "leg", "neck" });
return parts[random(sizeof(parts))];
}
/////////////////////////////////////////////////////////////////////////////
private nomask string extractVerb(string match)
{
string ret = "ERROR";
string query = regreplace(match, "##(.*)##", "\\1");
string *arguments = explode(query, "::");
if((sizeof(arguments) > 1) && arguments[1] && stringp(arguments[1]))
{
ret = arguments[1];
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string verbSecondPerson(string match)
{
string ret = extractVerb(match);
if (ret == "be")
{
ret = "are";
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string getSingularThirdPersonVerb(string verb)
{
// This will be imperfect, only converting regular forms right now.
// TODO might include adding irregulars other than 'to be'
string ret = verb;
if (ret == "be")
{
ret = "is";
}
else
{
ret = regreplace(ret, "(s|ch|sh|x|z|dg|o)$", "\\1e");
ret = regreplace(ret, "([^aeiou])y$", "\\1ie");
ret += "s";
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string verbThirdPerson(string match)
{
return getSingularThirdPersonVerb(extractVerb(match));
}
/////////////////////////////////////////////////////////////////////////////
public nomask string parseSimileDictionary(string message, object messageItem)
{
// ##SimileDictionary## - random word from the simile dictionary
string ret = message;
if(message && stringp(message) && messageItem && objectp(messageItem) &&
function_exists("getRandomSimile", messageItem))
{
string tmpMsg;
// Looping allows each instance of ##SimileDictionary to be different
do
{
tmpMsg = ret;
ret = regreplace(ret, "##SimileDictionary##",
messageItem->getRandomSimile());
} while(tmpMsg != ret);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string parseVerbDictionary(string message,
string stringToReplace, object messageItem)
{
// ##<stringToReplace>## - random word from a verb dictionary
// This is converted into ##Infinitive::verb## for parsing
// with parseVerbs
string ret = message;
if(message && stringp(message) && messageItem && objectp(messageItem) &&
function_exists("getRandomVerb", messageItem))
{
string tmpMsg;
// Looping allows each instance of ##SimileDictionary to be different
do
{
tmpMsg = ret;
ret = regreplace(ret, sprintf("##%s##", stringToReplace),
messageItem->getRandomVerb());
} while(tmpMsg != ret);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string parseVerbs(string message, int isSecondPerson)
{
// ##Infinitive::verb## - replaced with 2nd person or 3rd person form
return regreplace(message, "##Infinitive::[a-z]+##",
(isSecondPerson ? #'verbSecondPerson : #'verbThirdPerson), 1);
}
/////////////////////////////////////////////////////////////////////////////
public nomask varargs string parseTargetInfo(string message, string typeOfTarget,
object target, int isSecondPerson)
{
string ret = message;
// ##<typeOfTarget>Name## - target's name
// ##<typeOfTarget>Possessive[::Name]## - target's possessive / your
// ##<typeOfTarget>Objective## - target's objective / you
// ##<typeOfTarget>Subjective## - target's subjective/pronoun / you
if(isValidAttacker(target))
{
// What this lacks in elegance, it more than makes up for in laziness
ret = regreplace(ret, sprintf("##%sName3rd##", typeOfTarget),
getName(target, 0), 1);
ret = regreplace(ret, sprintf("##%sName::capitalize##", typeOfTarget),
capitalize(getName(target, isSecondPerson)), 1);
ret = regreplace(ret, sprintf("##%sName##", typeOfTarget),
getName(target, isSecondPerson), 1);
ret = regreplace(ret, sprintf("##%sObjective##", typeOfTarget),
getObjective(target, isSecondPerson), 1);
ret = regreplace(ret, sprintf("##%sSubjective##", typeOfTarget),
getSubjective(target, isSecondPerson), 1);
ret = regreplace(ret, sprintf("##%sPossessive::Name##", typeOfTarget),
getPossessiveName(target, isSecondPerson), 1);
ret = regreplace(ret, sprintf("##%sPossessive##", typeOfTarget),
getPossessive(target, isSecondPerson), 1);
ret = regreplace(ret, sprintf("##%sReflexive##", typeOfTarget),
getReflexive(target, isSecondPerson), 1);
ret = regreplace(ret, "##BodyPart##", getBodyPart(target), 1);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string parseTargetWeapon(string message, string typeOfTarget,
object weapon)
{
string ret = message;
if (objectp(weapon))
{
string weaponType = weapon->query("weapon type") ? weapon->query("weapon type") : "";
ret = regreplace(ret, sprintf("##%sWeapon##", typeOfTarget),
weaponType, 1);
}
// TODO
// ##AttackerWeapon## - type of attacker's weapon (longsword, short sword,
// claymore...)
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string capitalizeSentences(string message)
{
string ret = regreplace(message, "^[a-z]", #'upper_case);
ret = regreplace(ret, "[.!?] [a-z]", #'upper_case, 1);
ret = regreplace(ret, " ", " ", 1);
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string parseEfun(string match)
{
string ret = "ERROR";
// This is of the form efun::type::(path|name|this)[::function]
// type can be any of: filename, target, this
// path is a file path, name is the name of target, this is a "don't care"
// function is a method on the called object. The method MUST return a
// string.
string *arguments = explode(match, "::");
if(sizeof(arguments) > 2)
{
switch(arguments[0])
{
case "##call_other":
{
switch(arguments[1])
{
case "filename":
case "file":
{
if(file_size(arguments[2]) > 0)
{
object objFromFile = load_object(arguments[2]);
if(objFromFile && arguments[3] &&
function_exists(arguments[3], objFromFile))
{
ret = call_other(objFromFile, arguments[3]);
}
}
break;
}
case "target":
{
if(arguments[2] && this_player() &&
environment(this_player()) &&
present(arguments[2], environment(this_player())))
{
ret = call_other(present(arguments[2],
environment(this_player())), arguments[3]);
}
break;
}
case "this":
{
if(arguments[3])
{
ret = call_other(this_object(), arguments[3]);
}
break;
}
default:
{
break;
}
}
break;
}
case "##read_file":
{
if((arguments[1] == "filename") &&file_size(arguments[2]) > 0)
{
ret = read_file(arguments[2]);
}
break;
}
default:
{
break;
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string parseEfunCall(string message)
{
return regreplace(message,
"##([^:]+)::(file|target|this)::([^:]+)::([a-zA-Z0-9_])+",
#'parseEfun,1);
}
/////////////////////////////////////////////////////////////////////////////
private nomask int isValidLiving(object livingCheck)
{
return (livingCheck && objectp(livingCheck) &&
(member(inherit_list(livingCheck), MaterialAttributes) > -1));
}
/////////////////////////////////////////////////////////////////////////////
public 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
// ##InitiatorReflexive## - Initiator's reflexive pronoun / yourself
// ##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 = parseSimileDictionary(message, initiator);
message = parseVerbDictionary(message,
"HitDictionary", initiator);
message = parseVerbs(message, isSecondPerson);
}
if (isValidLiving(initiator))
{
message = parseTargetInfo(message, "Initiator",
initiator, isSecondPerson);
}
if (isValidLiving(target))
{
isSecondPerson = (perspective == "target");
message = parseTargetInfo(message, "Target",
target, isSecondPerson);
}
message = capitalizeSentences(message);
return message;
}
/////////////////////////////////////////////////////////////////////////////
public 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))
{
string parsedMessage;
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, format(parsedMessage, 78));
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
public nomask void displayMessageToSelf(string message, object initiator)
{
if (initiator && objectp(initiator))
{
string parsedMessage;
parsedMessage = parseTemplate(message, "initiator", initiator,
initiator);
tell_object(initiator, format(parsedMessage, 78));
}
}