forked from realms-mud/core-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prerequisites.c
701 lines (670 loc) · 25 KB
/
prerequisites.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
//*****************************************************************************
// Class: prerequisites
// File Name: prerequisites.c
//
// Copyright (c) 2018 - Allen Cummings, RealmsMUD, All rights reserved. See
// the accompanying LICENSE file for details.
//
// Description: This class is used to handle prerequisites. It is primarily
// of interest for the lib's add-on modules, though it can
// certainly be of use to any class that needs to limit its use,
// entry, and so on based on an interactive object's data/state.
//
//*****************************************************************************
// This shows the blueprint for how prerequisites must be built.
protected mapping prerequisites = ([
// <key>: ([
// "type": one of the items in the isValidPrerequisiteType array,
// "value": optional value
// ]),
// Examples:
// "level": ([
// "type": "level",
// "guild": "mage", // This is an optional parameter.
// "value": 10
// ]),
// "elemental fire": ([
// "type": "skill",
// "value": 15
// ]),
// "fetch a spatula": ([
// "type": "quest",
// ]),
// "fire spells": ([
// "type": "research"
// ]),
// "race": ([
// "type": "race",
// "value": ({ "elf", "half elf" })
// ]),
// "guild": ([
// "type": "guild",
// "value": ({ "fighter" })
// ]),
// "best kill": ([
// "type": "combat statistic",
// "value": 45
// ]),
// "elf": ([
// "type": "combat statistic",
// "value": 100 // number of kills of creatures of this race
// ]),
// "trait": ([
// "type": "trait",
// "value": ({ "boorish", "loutish" })
// ])
]);
//-----------------------------------------------------------------------------
// Method: isValidPrerequisiteType
// Description: This method will return true if the supplied prerequisite type
// is valid.
//
// Parameters: type - the prerequisite type to check
//
// Returns: true if the prerequisite type is valid.
//-----------------------------------------------------------------------------
private nomask int isValidPrerequisiteType(string type)
{
return (member(({ "research", "attribute", "skill", "quest", "guild",
"race", "faction", "trait", "background", "combat statistic", "level",
"opinion", "state" }), type) > -1);
}
//-----------------------------------------------------------------------------
// Method: validPrerequisite
// Description: This method will return true if the supplied prerequisite
// mapping is in the proper format and is of a valid type.
//
// Parameters: prerequisite - the prerequisite to check
//
// Returns: true if the prerequisite is of valid format.
//-----------------------------------------------------------------------------
protected nomask int validPrerequisite(mapping prerequisite)
{
return (prerequisite && mappingp(prerequisite) &&
member(prerequisite, "type") &&
isValidPrerequisiteType(prerequisite["type"]));
}
//-----------------------------------------------------------------------------
// Method: getPrerequisite
// Description: This method will return the details mapping for the requested
// prerequisite if it exists.
//
// Parameters: prerequisite - the prerequisite to retrieve
//
// Returns: the details mapping for the specified prerequisite.
//-----------------------------------------------------------------------------
private nomask mapping getPrerequisite(string prerequisite, string grouping)
{
mapping ret = 0;
if (grouping && member(prerequisites, grouping) &&
member(prerequisites[grouping], prerequisite) &&
validPrerequisite(prerequisites[grouping][prerequisite]))
{
ret = prerequisites[grouping][prerequisite] + ([]);
}
else if(member(prerequisites, prerequisite) &&
validPrerequisite(prerequisites[prerequisite]))
{
ret = prerequisites[prerequisite] + ([ ]);
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: getPrerequisites
// Description: This method will return all of the prerequisites for this
// object.
//
// Returns: the prerequisites mapping
//-----------------------------------------------------------------------------
public nomask mapping getPrerequisites()
{
return prerequisites + ([ ]);
}
//-----------------------------------------------------------------------------
// Method: addPrerequisite
// Description: This method will add the supplied prerequisite details for the
// given key provided that it is in the valid prerequisite
// format and the supplied key does not already exist. This method
// can only be accessed by objects that inherit it.
//
// Parameters: key - the key/name of the prerequisite
// prerequisite - the prerequisite details mapping
//
// Returns: true if the prerequisite has been added
//-----------------------------------------------------------------------------
protected nomask varargs int addPrerequisite(string key, mapping prerequisite, string grouping)
{
int ret = 0;
if (grouping && stringp(grouping) && key && stringp(key) &&
validPrerequisite(prerequisite) && (!member(prerequisites, grouping) ||
(member(prerequisites, grouping) && !member(prerequisites[grouping], key))))
{
if (!member(prerequisites, grouping))
{
prerequisites[grouping] = ([]);
}
prerequisites[grouping] += ([key:prerequisite]);
ret = 1;
}
else if(!grouping && key && stringp(key) && validPrerequisite(prerequisite) &&
!member(prerequisites, key))
{
prerequisites[key] = prerequisite + ([ ]);
ret = 1;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: validResearcher
// Description: This method will return true if the object passed inherits
// the research module.
//
// Parameters: researcher - the object to check
//
// Returns: true if the researcher object is of type 'research'
//-----------------------------------------------------------------------------
private nomask int validResearcher(object researcher)
{
return (researcher && objectp(researcher) && researcher->has("research"));
}
//-----------------------------------------------------------------------------
// Method: checkResearch
// Description: This method will check whether or not the passed researcher
// has researched the item identified by the passed 'research'
// key.
//
// Parameters: researcher - the object to check
// research - the research item to verify knowledge of
//
// Returns: true if the researcher object 'knows' the passed research topic
//-----------------------------------------------------------------------------
private nomask int checkResearch(object researcher, string research)
{
return (validResearcher(researcher) && researcher->has("research") &&
researcher->isResearched(research));
}
//-----------------------------------------------------------------------------
// Method: checkAttribute
// Description: This method will check whether or not the passed researcher
// has an 'attribute' attribute of at least the passed value.
//
// Parameters: researcher - the object to check
// attribute - the attribute to check
// value - the minimum value of the attribute
//
// Returns: true if the researcher object has attribute of at least the
// specified value.
//-----------------------------------------------------------------------------
private nomask int checkAttribute(object researcher, string attribute,
int value)
{
return (validResearcher(researcher) && researcher->has("attributes") &&
(researcher->attributeValue(attribute) >= value));
}
//-----------------------------------------------------------------------------
// Method: checkSkill
// Description: This method will check whether or not the passed researcher
// has a 'skill' skill of at least the passed value.
//
// Parameters: researcher - the object to check
// skill - the skill to check
// value - the minimum value of the skill
//
// Returns: true if the researcher object has skill of at least the
// specified value.
//-----------------------------------------------------------------------------
private nomask int checkSkill(object researcher, string skill, int value)
{
return (validResearcher(researcher) && researcher->has("skills") &&
(researcher->getSkill(skill) >= value));
}
//-----------------------------------------------------------------------------
// Method: checkLevel
// Description: This method will check whether or not the passed researcher
// is of at least the specified level. If the optional guild
// argument is passed, it will check the level of that guild.
// Otherwise, the passed in object's effective level is checked
//
// Parameters: researcher - the object to check
// level - the minimum level of the researcher
// guild - Optional. The guild to check the researcher's level in
//
// Returns: true if the researcher object is of at least the specified level
//-----------------------------------------------------------------------------
private nomask varargs int checkLevel(object researcher, int level, string guild)
{
int ret = validResearcher(researcher) && researcher->has("guilds");
if(ret)
{
if(guild && stringp(guild) && researcher->memberOfGuild(guild))
{
ret &&= researcher->guildLevel(guild) >= level;
}
else
{
ret &&= researcher->effectiveLevel() >= level;
}
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkQuest
// Description: This method will check whether or not the passed researcher
// has completed the passed quest.
//
// Parameters: researcher - the object to check
// quest - the quest to check if completed
//
// Returns: true if the researcher object has completed the passed quest
//-----------------------------------------------------------------------------
private nomask int checkQuest(object researcher, string quest)
{
return (validResearcher(researcher) && researcher->has("quests") &&
researcher->questIsCompleted(quest));
}
//-----------------------------------------------------------------------------
// Method: checkGuilds
// Description: This method will check whether or not the passed researcher
// is a member of one of the passed guilds
//
// Parameters: researcher - the object to check
// guilds - an array of valid guilds to check membership of
//
// Returns: true if the researcher object is a member of at least one of the
// specified guilds.
//-----------------------------------------------------------------------------
private nomask int checkGuilds(object researcher, string *guilds)
{
int ret = 0;
if(validResearcher(researcher) && researcher->has("guilds") &&
pointerp(guilds))
{
ret = 1;
int inGuild = 0;
foreach(string guild in guilds)
{
inGuild ||= researcher->memberOfGuild(guild);
}
ret &&= inGuild;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkRaces
// Description: This method will check whether or not the passed researcher
// is a member of one of the passed races
//
// Parameters: researcher - the object to check
// guilds - an array of valid races to check membership of
//
// Returns: true if the researcher object is a member of one of the specified
// races.
//-----------------------------------------------------------------------------
private nomask int checkRaces(object researcher, string *races)
{
int ret = 0;
if(validResearcher(researcher) && researcher->has("races") &&
pointerp(races))
{
ret = 1;
int ofRace = 0;
foreach(string race in races)
{
ofRace ||= researcher->Race() == race;
}
ret &&= ofRace;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkFactions
// Description: This method will check whether or not the passed researcher
// is a member of one of the passed factions
//
// Parameters: researcher - the object to check
// factions - an array of valid factions to check membership of
//
// Returns: true if the researcher object is a member of at least one of the
// specified factions.
//-----------------------------------------------------------------------------
private nomask int checkFactions(object researcher, string *factions)
{
int ret = 0;
if(validResearcher(researcher) && researcher->has("factions") &&
pointerp(factions))
{
ret = 1;
int ofFaction = 0;
foreach(string faction in factions)
{
ofFaction ||= researcher->memberOfFaction(faction);
}
ret &&= ofFaction;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkTraits
// Description: This method will check whether or not the passed researcher
// has at least one of the passed traits
//
// Parameters: researcher - the object to check
// traits - an array of valid traits
//
// Returns: true if the researcher object has at least one of the specified
// traits.
//-----------------------------------------------------------------------------
private nomask int checkTraits(object researcher, string *traits)
{
int ret = 0;
if(validResearcher(researcher) && researcher->has("traits") &&
pointerp(traits))
{
ret = 1;
int hasTrait = 0;
foreach(string trait in traits)
{
hasTrait ||= researcher->isTraitOf(trait);
}
ret &&= hasTrait;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkBackground
// Description: This method will check whether or not the passed researcher
// has at least one of the passed backgrounds
//
// Parameters: researcher - the object to check
// backgrounds - an array of valid backgrounds to check
//
// Returns: true if the researcher object has at least one of the specified
// backgrounds.
//-----------------------------------------------------------------------------
private nomask int checkBackground(object researcher, string *backgrounds)
{
int ret = 0;
if(validResearcher(researcher) && researcher->has("background") &&
pointerp(backgrounds))
{
ret = 1;
int hasBackground = 0;
foreach(string background in backgrounds)
{
hasBackground ||= researcher->isBackgroundTraitOf(background);
}
ret &&= hasBackground;
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkCombatStats
// Description: This method will check whether or not the passed researcher
// has a 'type' combat statistic of at least the passed value.
// Only "best kill" and "race" are valid.
//
// Parameters: researcher - the object to check
// type - the combat statistic to check - "best kill" or "race"
// value - the minimum value of the statistic
//
// Returns: true if the researcher object has combat statistics of the given
// type of at least the specified value.
//-----------------------------------------------------------------------------
private nomask int checkCombatStats(object researcher, string type, int value)
{
// Only two types are supported: "best kill" which is the level of the best
// kill or "race". If the prereq is NOT "best kill" it will be interpreted
// as the number of kills of a certain race.
int ret = 0;
if(validResearcher(researcher) && researcher->has("combat"))
{
if(type && stringp(type))
{
switch(type)
{
case "best kill":
{
ret = researcher->bestKillMeetsLevel(value);
break;
}
default:
{
ret = researcher->racialKillsMeetCount(type, value);
break;
}
}
}
}
return ret;
}
//-----------------------------------------------------------------------------
// Method: checkPrerequisites
// Description: This method will check whether or not the passed researcher
// has passed all of the requirements and will return true if this
// is the case.
//
// Parameters: researcher - the object to check
//
// Returns: true if the researcher object has passed all of the prerequisites.
//-----------------------------------------------------------------------------
public nomask varargs int checkPrerequisites(object researcher, string grouping, object owner)
{
int ret = 1;
string *prerequisiteList = (grouping && member(prerequisites, grouping)) ?
m_indices(prerequisites[grouping]) : m_indices(prerequisites);
if(sizeof(prerequisiteList))
{
foreach(string prerequisite in prerequisiteList)
{
mapping prerequisiteData = getPrerequisite(prerequisite, grouping);
if (prerequisiteData)
{
switch (prerequisiteData["type"])
{
case "research":
{
// This should NOT be used to handle prerequisites
// that would exist within a single research tree
// structure.
ret &&= checkResearch(researcher, prerequisite);
break;
}
case "attribute":
{
ret &&= checkAttribute(researcher, prerequisite,
prerequisiteData["value"]);
break;
}
case "skill":
{
ret &&= checkSkill(researcher, prerequisite,
prerequisiteData["value"]);
break;
}
case "quest":
{
ret &&= checkQuest(researcher, prerequisite);
break;
}
case "guild":
{
ret &&= checkGuilds(researcher, prerequisiteData["value"]);
break;
}
case "race":
{
ret &&= checkRaces(researcher, prerequisiteData["value"]);
break;
}
case "faction":
{
ret &&= checkFactions(researcher, prerequisiteData["value"]);
break;
}
case "trait":
{
ret &&= checkTraits(researcher, prerequisiteData["value"]);
break;
}
case "background":
{
ret &&= checkBackground(researcher, prerequisiteData["value"]);
break;
}
case "level":
{
ret &&= (member(prerequisiteData, "guild") ?
checkLevel(researcher, prerequisiteData["value"],
prerequisiteData["guild"]) :
checkLevel(researcher, prerequisiteData["value"]));
break;
}
case "combat statistic":
{
ret &&= checkCombatStats(researcher, prerequisite,
prerequisiteData["value"]);
break;
}
case "opinion":
{
ret &&= owner && (owner->opinionOf(researcher) >=
prerequisiteData["value"]);
break;
}
case "state":
{
ret &&= researcher && member(prerequisiteData, "state key") &&
(researcher->stateFor(load_object(prerequisiteData["state key"])) ==
prerequisiteData["value"]);
break;
}
default:
{
ret = 0;
break;
}
}
}
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string displayResearchPrerequsite(string item)
{
string ret = "";
object dictionary = load_object("/lib/dictionaries/researchDictionary.c");
if (dictionary)
{
object research = dictionary->researchObject(item);
if (research)
{
ret = capitalize(research->query("name"));
}
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string displayQuestPrerequsite(string item)
{
string ret = "";
object dictionary = load_object("/lib/dictionaries/questsDictionary.c");
if (dictionary)
{
ret = dictionary->questSummary(item);
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
private nomask string displayTraitPrerequsite(string *traits)
{
string ret = "";
object dictionary = load_object("/lib/dictionaries/traitsDictionary.c");
if (dictionary && sizeof(traits))
{
foreach(string trait in traits)
{
object traitObj = dictionary->traitObject(trait);
if (traitObj)
{
ret += capitalize(traitObj->query("name")) + " or ";
}
}
ret = ret[..(sizeof(ret) - 5)];
}
return ret;
}
/////////////////////////////////////////////////////////////////////////////
public nomask string displayPrerequisites(string colorConfiguration, object configuration)
{
string ret = "";
if (sizeof(prerequisites) && objectp(configuration))
{
ret = configuration->decorate("Prerequisites:\n",
"field header", "research", colorConfiguration);
string *prereqKeys = sort_array(m_indices(prerequisites), (: $1 > $2 :));
foreach(string key in prereqKeys)
{
string prereq = "";
switch (prerequisites[key]["type"])
{
case "quest":
{
prereq = displayQuestPrerequsite(key);
break;
}
case "research":
{
prereq = displayResearchPrerequsite(key);
break;
}
case "trait":
{
prereq = displayTraitPrerequsite(prerequisites[key]["value"]);
break;
}
case "attribute":
case "level":
case "opinion":
case "skill":
{
prereq = sprintf("%s of %d", capitalize(key),
prerequisites[key]["value"]);
if (member(prerequisites[key], "guild"))
{
prereq += " in " + capitalize(prerequisites[key]["guild"]);
}
break;
}
case "combat statistic":
{
prereq = sprintf("%s %s %d", capitalize(key),
(key == "best kill") ? "level is" : "kill count of",
prerequisites[key]["value"]);
break;
}
case "guild":
case "race":
case "faction":
{
if (pointerp(prerequisites[key]["value"]) &&
sizeof(prerequisites[key]["value"]))
{
foreach(string value in prerequisites[key]["value"])
{
prereq += capitalize(value) + " or ";
}
prereq = prereq[..(sizeof(prereq) - 5)];
}
break;
}
}
ret += configuration->decorate(sprintf("%15s: ",
capitalize(prerequisites[key]["type"])),
"field header", "research", colorConfiguration) +
configuration->decorate(prereq + "\n",
"prerequisite", "research", colorConfiguration);
}
}
return ret;
}