forked from realms-mud/core-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
specification.c
531 lines (510 loc) · 18.6 KB
/
specification.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//*****************************************************************************
// Class: specification
// File Name: specification.c
//
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//*****************************************************************************
protected mapping researchData = ([
// "name": <name of the research>
// "description": <research's description>
// "type": active|passive|sustained|ritual|knowledge
// "scope": self|targeted|area|environmental|region|global
// "effect": beneficial|combat|enchantment|summoning|strategic
// "research type": points|timed|tree root
// "research cost": <value> - points to purchase or time delay
// "limited by": ([
// "opponent race": <valid race>
// "opponent guild": <valid guild>
// "environment": <valid environment type>
// "intoxicated": <level>
// "drugged": <level>
// "near death": <percentage of hit points left (0-100)>
// "stamina drained": <percentage of stamina left (0-100)>
// "spell points drained": <percentage of spell points left (0-100)>
// ])
// Other data is specified in specific type-inherited methods
]);
/////////////////////////////////////////////////////////////////////////////
protected nomask object getDictionary(string service)
{
object ret = 0;
if (service && stringp(service))
{
string dictionary = sprintf("/lib/dictionaries/%sDictionary.c",
service);
if (file_size(dictionary) > -1)
{
ret = load_object(dictionary);
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
protected nomask int validLimitor(mapping limitor)
{
int ret = 0;
if (limitor && mappingp(limitor))
{
string *limitorKeys = m_indices(limitor);
if (sizeof(limitorKeys))
{
ret = 1;
}
foreach(string key in limitorKeys)
{
if (key && stringp(key))
{
switch (key)
{
case "opponent race":
{
object racialDictionary = getDictionary("racial");
if (racialDictionary)
{
ret &&= racialDictionary->isValidRace(
limitor[key]) || racialDictionary->isCreatureRace(limitor[key]);
}
break;
}
case "opponent guild":
{
object guildDictionary = getDictionary("guilds");
if (guildDictionary)
{
ret &&= guildDictionary->isValidGuild(
limitor[key]);
}
break;
}
case "opponent faction":
{
object factionDictionary = getDictionary("factions");
if (factionDictionary)
{
ret &&= factionDictionary->isValidFaction(
limitor[key]);
}
break;
}
case "crafting type":
{
object craftingDictionary = getDictionary("crafting");
if (craftingDictionary)
{
ret &&= craftingDictionary->isValidType(
limitor[key]);
}
break;
}
case "environment":
{
object environmentDictionary =
getDictionary("environment");
if (environmentDictionary)
{
ret &&= environmentDictionary->isValidType(
limitor[key]);
}
break;
}
case "equipment":
{
object materialsDictionary = getDictionary("materials");
if (materialsDictionary)
{
if (pointerp(limitor[key]) && sizeof(limitor[key]))
{
int isValid = 0;
string *list = limitor[key];
foreach(string equipment in list)
{
isValid ||= materialsDictionary->isValidArmorType(equipment) ||
materialsDictionary->isValidArmorBlueprint(equipment) ||
materialsDictionary->isValidWeaponType(equipment) ||
materialsDictionary->isValidWeaponBlueprint(equipment);
}
ret &&= isValid;
}
else
{
ret &&= materialsDictionary->isValidArmorType(limitor[key]) ||
materialsDictionary->isValidArmorBlueprint(limitor[key]) ||
materialsDictionary->isValidWeaponType(limitor[key]) ||
materialsDictionary->isValidWeaponBlueprint(limitor[key]);
}
}
break;
}
case "intoxicated":
case "drugged":
{
ret &&= intp(limitor[key]);
break;
}
case "near death":
case "stamina drained":
case "spell points drained":
{
ret &&= intp(limitor[key]) && (limitor[key] >= 0)
&& (limitor[key] <= 100);
break;
}
default:
{
ret = 0;
}
}
}
else
{
ret = 0;
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
protected int addSpecification(string type, mixed value)
{
int ret = 0;
if (type && stringp(type))
{
switch (type)
{
case "name":
case "description":
{
if (value && stringp(value))
{
ret = 1;
researchData[type] = value;
}
else
{
raise_error(sprintf("ERROR - specification: The '%s' value "
"must be a string.\n", type));
}
break;
}
case "limited by":
{
if (validLimitor(value))
{
ret = 1;
researchData["limited by"] = value;
}
else
{
raise_error("ERROR - specification: The value of 'limited "
"by' must be a valid limiting mapping. See the "
"specification.validLimitor method for details.\n");
}
break;
}
default:
{
raise_error("ERROR - specification: The provided specification "
"type is unknown. It must be one of: name, description, "
"or limited by.\n");
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask int EffectIsLimited()
{
return member(researchData, "limited by");
}
/////////////////////////////////////////////////////////////////////////////
protected int blockSkillApplication(string skill, object owner, object target)
{
// Overwrite this method for special processing beyond what is offered
// via "limited by".
return 0;
}
/////////////////////////////////////////////////////////////////////////////
public nomask varargs int canApplySkill(string skill, object owner, object target, int verbose)
{
int ret = 1;
notify_fail("");
if (member(researchData, "limited by") && owner && objectp(owner))
{
if (member(researchData["limited by"], "opponent race"))
{
ret &&= target && objectp(target) &&
function_exists("Race", target) && (target->Race() ==
researchData["limited by"]["opponent race"]);
if (!ret && verbose)
{
printf("Your opponent is not of the %s race.\n",
researchData["limited by"]["opponent race"]);
}
}
if (member(researchData["limited by"], "opponent guild"))
{
ret &&= target && objectp(target) &&
function_exists("memberOfGuild", target) &&
target->memberOfGuild(
researchData["limited by"]["opponent guild"]);
if (!ret && verbose)
{
printf("Your opponent is not of the %s guild.\n",
researchData["limited by"]["opponent guild"]);
}
}
if (member(researchData["limited by"], "opponent faction"))
{
ret &&= target && objectp(target) &&
function_exists("memberOfFaction", target) &&
target->memberOfFaction(
researchData["limited by"]["opponent faction"]);
if (!ret && verbose)
{
object faction = getDictionary("factions")->factionObject(
researchData["limited by"]["opponent faction"]);
if (faction)
{
printf("Your opponent is not of the %s faction.\n",
faction->name());
}
}
}
if (member(researchData["limited by"], "crafting type"))
{
ret &&= target && (target->query("crafting type") ==
researchData["limited by"]["crafting type"]);
if (!ret && verbose)
{
printf("The item is of the wrong type to be affected by this research.\n");
}
}
if (member(researchData["limited by"], "environment"))
{
object environmentDictionary =
getDictionary("environment");
if (environmentDictionary)
{
ret &&= environment(owner) &&
environmentDictionary->isEnvironmentOfType(environment(owner),
researchData["limited by"]["environment"]);
}
if (!ret && verbose)
{
printf("You are not in the correct environment (%s) to do that.\n",
researchData["limited by"]["environment"]);
}
}
if (member(researchData["limited by"], "intoxicated"))
{
ret &&= function_exists("Intoxicated", owner) &&
(owner->Intoxicated() >=
researchData["limited by"]["intoxicated"]);
if (!ret && verbose)
{
printf("You are not intoxicated enough to do that.\n");
}
}
if (member(researchData["limited by"], "drugged"))
{
ret &&= function_exists("Drugged", owner) &&
(owner->Drugged() >= researchData["limited by"]["drugged"]);
if (!ret && verbose)
{
printf("You are not drugged enough to do that.\n");
}
}
if (member(researchData["limited by"], "near death"))
{
ret &&= function_exists("hitPoints", owner) &&
(owner->hitPoints() <= researchData["limited by"]["near death"]);
if (!ret && verbose)
{
printf("You are not injured enough to do that.\n");
}
}
if (member(researchData["limited by"], "stamina drained"))
{
ret &&= function_exists("staminaPoints", owner) &&
(owner->staminaPoints() <= researchData["limited by"]["stamina drained"]);
if (!ret && verbose)
{
printf("You are not weary enough to do that.\n");
}
}
if (member(researchData["limited by"], "spell points drained"))
{
ret &&= function_exists("spellPoints", owner) &&
(owner->spellPoints() <= researchData["limited by"]["spell points drained"]);
if (!ret && verbose)
{
printf("You are not drained enough to do that.\n");
}
}
if (member(researchData["limited by"], "equipment"))
{
if (pointerp(researchData["limited by"]["equipment"]) &&
sizeof(researchData["limited by"]["equipment"]))
{
int hasEquipment = 0;
string *list = researchData["limited by"]["equipment"];
foreach(string equipment in list)
{
hasEquipment ||= owner->usingEquipmentOfType(equipment);
}
ret &&= hasEquipment;
}
else
{
ret &&= function_exists("usingEquipmentOfType", owner) &&
owner->usingEquipmentOfType(researchData["limited by"]["equipment"]);
}
if (!ret && verbose)
{
printf("You must be using the proper equipment for that (%s).\n",
stringp(researchData["limited by"]["equipment"]) ?
researchData["limited by"]["equipment"] :
implode(researchData["limited by"]["equipment"], ", "));
}
}
}
return ret && !blockSkillApplication(skill, owner, target);
}
/////////////////////////////////////////////////////////////////////////////
public mixed query(string element)
{
mixed ret = 0;
if (member(researchData, element))
{
ret = researchData[element];
if (pointerp(ret))
{
ret += ({});
}
if (mappingp(ret))
{
ret += ([]);
}
}
else
{
switch (element)
{
case "bonuses":
{
ret = sort_array(filter(m_indices(researchData),
(: return (sizeof(regexp(({ $1 }), "bonus")) > 0) &&
(researchData[$1] > 0); :)), (: $1 > $2 :));
break;
}
case "penalties":
{
ret = sort_array(filter(m_indices(researchData),
(: (sizeof(regexp(({ $1 }), "penalty to")) ||
(sizeof(regexp(({ $1 }), "bonus")) > 0) &&
(researchData[$1] < 0)) :)),
(: $1 > $2 :));
break;
}
case "apply to":
{
ret = sort_array(filter(m_indices(researchData),
(: (sizeof(regexp(({ $1 }), "apply")) > 0) :)),
(: $1 > $2 :));
break;
}
case "raw bonuses":
{
ret = sort_array(filter(m_indices(researchData),
(: (sizeof(regexp(({ $1 }), "bonus")) > 0) :)),
(: $1 > $2 :));
break;
}
case "enchantments":
{
ret = ([]);
string *enchantments = filter(m_indices(researchData),
(: ((sizeof(regexp(({ $1 }), "bonus .* enchantment")) > 0) &&
(researchData[$1] > 0)) :));
if (sizeof(enchantments))
{
foreach(string enchantment in enchantments)
{
ret[regreplace(enchantment,
"bonus (.*) enchantment", "\\1")] =
researchData[enchantment];
}
}
break;
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string displayLimiters(string colorConfiguration, object configuration)
{
string ret = "";
if (member(researchData, "limited by") &&
sizeof(researchData["limited by"]))
{
string limiter = "This is only applied when %s %s %s.";
string *prereqKeys = sort_array(
m_indices(researchData["limited by"]), (: $1 > $2 :));
foreach(string key in prereqKeys)
{
switch (key)
{
case "opponent race":
case "opponent guild":
case "crafting type":
case "environment":
{
ret += sprintf(limiter, key, "is", researchData["limited by"][key]);
break;
}
case "opponent faction":
{
object faction = getDictionary("factions")->factionObject(
researchData["limited by"][key]);
if (faction)
{
ret += sprintf(limiter, key, "is", faction->name());
}
break;
}
case "intoxicated":
case "drugged":
case "near death":
case "stamina drained":
case "spell points drained":
{
ret += sprintf(limiter, "you", "are", key);
break;
}
case "equipment":
{
string equipment = "";
if (pointerp(researchData["limited by"][key]) &&
sizeof(researchData["limited by"][key]))
{
equipment = implode(researchData["limited by"][key], " or ");
}
else
{
equipment = researchData["limited by"][key];
}
ret += sprintf(limiter, "you're", "using:", equipment);
break;
}
}
}
}
if (ret != "")
{
ret = configuration->decorate(format(ret, 78),
"field header", "research", colorConfiguration);
}
return ret;
}