-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy paththing.t
10643 lines (8752 loc) · 341 KB
/
thing.t
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
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#charset "us-ascii"
#include "advlite.h"
property subLocation;
property lookAroundShowExits;
property stanceToward;
property setStanceToward;
/*
* Mentionable is the base class for objects that the player can refer to
* in command input. In order for the parser to recognize an object, the
* object must have vocabulary words in the dictionary. This class's
* main function, then, is to set up the dictionary for an object, so
* that its words are recognizable to the parser.
*
* This class is based on LMentionable, which is defined in the language
* module. LMentionable provides implementations for certain methods
* that we rely upon for functionality that varies by language.
*/
class Mentionable: LMentionable
/*
* 'vocab' is a string that we use to initialize the object's short
* name, dictionary words, grammatical gender, and grammatical number
* (singular/plural). This is designed to make it as convenient as
* possible to describe the object's name and grammatical behavior
* for input and output purposes.
*
* The syntax is language-specific - see initVocab() for details.
*/
vocab = nil
/*
* The object's short name, for display in lists and announcements.
* By default, this is automatically derived from 'vocab', so you
* usually don't have to set it directly. If you define a non-nil
* 'name' value manually, though, it takes precedence (i.e., the
* library won't replace it with the name implied by 'vocab').
*/
name = nil
/*
* My room title. This is displayed as the start of the room
* headline, which is the first line of the room description when
* 'self' is the outermost visible container of the point-of-view
* actor. The headline is also conventionally shown on the status
* line whenever the player is in the location.
*
* The room title essentially serves as a label for the room on the
* player's mental and/or paper map of the game's geography. It's
* usually something short and pithy that sums up the room's essence,
* function, or appearance, and is usually written in title case: Ice
* Cave, Bank Lobby, Front of House, Transporter Room 3, etc.
*
* The room headline sometimes adds more status information to the
* title, such as the point-of-view actor's direct container when the
* actor is inside an intermediate container within the room, such as
* a chair.
*/
roomTitle = nil
/*
* The object's disambiguation name. This is a more detailed version
* of the name, for situations where the short name is ambiguous.
* For example, the parser displays this name in "Which do you mean"
* questions when it would help tell two of the listed objects apart.
*
* By default, this is the same as the short name. It's uncommon to
* override this, since short names are typically already detailed
* enough for most purposes. Every so often, though, you'll want to
* keep the short name very terse, so you'll leave out some
* distinguishing detail that it *could* have had. In such cases,
* you can add the distinguishing detail here, so that it's displayed
* only when it's really needed.
*/
disambigName = (name)
/*
* Disambiguation prompt grouping. When the parser generates a
* disambiguation question ("Which did you mean, the red book, or the blue
* book?"), it'll group the objects in the list by common disambigGroup
* values. The group is just an arbitrary value that keeps like objects
* together in the list. You can use a string, a class, or whatever you
* like for this, as long as grouped objects have the same value in
* common. We give this property a default value of 0 so that the
* disambigOrder will work by default without the need to specify a
* disambigGroup.
*/
disambigGroup = 0
/*
* Disambiguation prompt sorting. This gives the display order of
* this item within its disambiguation group, if it has one. The
* parser sorts objects within each group in ascending order of this
* value when generating the object list for a disambiguation
* question. This is simply an integer; the default is 1 for every
* object, which makes the ordering arbitrary.
*
* This property is useful for grouped objects with a natural
* ordering, such as items with sequential labels of some sort
* (numbers, letters, etc). You can use this property to ensure that
* lists of these items will be displayed in the natural order:
* "button 1, button 2, or button 3?" or "the door on the left, the
* door in the middle, or the door on the right?"
*/
disambigOrder = (listOrder)
/*
* Is this object's short name a proper name? A proper name is the
* name of a person, place, or other unique entity with its own name.
*
* This property controls how the library shows the object's name in
* generated messages. In English, for example, articles (a, the)
* aren't used with a proper name.
*
* The English library tries to infer whether the object has a proper
* name based on the 'vocab' string: if all the words in the short
* name are capitalized, we'll consider it a proper name. This rule
* of thumb doesn't always apply, though, so you can override it:
* simply set 'proper' explicitly in an individual object, and the
* setting will take precedence over whatever the name's
* capitalization would otherwise imply. (Other languages might have
* different rules for inferring 'proper', and some might not be able
* to infer it at all.)
*/
proper = nil
/*
* The object's name is "qualified" grammatically, meaning that it
* can't be combined with articles (a, the) or possessives. Proper
* names are considered to be qualified, but it's possible for a name
* to be qualified but not proper, such as a name that incorporates a
* possessive.
*/
qualified = (proper)
/*
* The grammatical person of narration relative to this object. Use
* 1 for first person ("I am in a cave"), 2 for second person ("You
* are in a cave"), and 3 for third person ("Bob is in a cave").
*
* Usually, every object in the game will be in the third person -
* *except* the player character object, which is usually in the
* second person. The library doesn't care which person you use for
* the player character, though - you're free to use first or third
* person if you prefer. First-person and third-person IF are
* relatively uncommon, but not unheard of.
*
* This property is used for verb agreement when library messages are
* generated. This ensures that library messages adapt to the
* correct narrative person of the story automatically. To write a
* first-person game, you don't have to replace all of the default
* messages, but simply set person=1 in your PC object.
*/
person = 3
/*
* The object's grammatical gender(s). This information is used to
* determine which pronouns can match the object as an antecedent,
* which pronouns should represent it in output, and (for some
* languages) which articles and other gender-agreement words should
* be used in conjunction with the object name in output.
*
* The default is neuter. Use isHim and isHer to give an object
* masculine and/or feminine gender. Use isIt to explicitly give an
* object neuter gender. By default, we infer isIt from isHim and
* isHer: we assume the object is neuter if it's not masculine or
* feminine.
*
* Languages that have grammatical gender will almost certainly want
* to parse articles in the 'vocab' property to make it more
* convenient to encode each object's gender. For example, a French
* implementation could parse 'le' or 'la' at the start of a vocab
* string and set isHim and isHer accordingly.
*
* The English library sets isHim and isHer if 'him' and 'her'
* (respectively) are found in the object's pronoun list. (This is
* the most convenient way to represent gender via the vocab string
* in English, since English doesn't have gendered articles.)
*
* Note that we define the genders as three separate properties, so
* the genders are NOT mutually exclusive - an object can be a "him",
* a "her", and an "it" at the same time. This is because a single
* object can have multiple grammatical genders in some languages.
* In English, for example, an animal can be referred to as gendered
* (matching its physical gender) or neuter; and a few inanimate
* objects have a sort of optional, idiomatic gender, such as
* referring to a ship or country as "her".
*
* Some languages might need additional genders. When needed,
* LMentionable should simply define suitable additional properties.
*
* In most gendered languages, the grammatical gender is an attribute
* of the noun, not of the object. In particular, if an object has
* two nouns in its vocabulary, the two nouns might be of different
* genders. The language module might therefore limit the use of
* isIt et al to the gender of the object's name string as it appears
* in output (e.g., for selecting an article when showing the name in
* a list, or selecting a pronoun to represent the object), and use a
* completely different scheme to tag the gender of individual
* vocabulary words. One approach would be to use separate mNoun and
* fNoun token properties (and more if needed) to distinguish the
* gender of individual nouns in the dictionary.
*/
isIt = (!(isHim || isHer))
isHim = nil
isHer = nil
/*
* The object's name's grammatical number. This specifies singular
* or plural usage for the object's name when it appears in generated
* messages. By default, an object has singular usage, so it'll
* appear as (for example) "an orange". Some objects have names with
* plural usage, either because they're words that always appear in
* the plural (such as "scissors"), or because the game object
* represents a group of items that are too numerous and unimportant
* to the game to actually implement as separate Thing objects. For
* example, the books in a library might be implemented collectively
* as a single "books" object.
*
* The English library sets this to true if 'them' is listed as a
* pronoun for the object in the 'vocab' property.
*/
plural = nil
/*
* Some objects, such as a pair of shoes or a flight of stairs could be
* regarded as either singular or plural and referred to as either 'it' or
* 'them'. Set ambiguouslyPlural to true for such objects.
*/
ambiguouslyPlural = nil
/*
* The object's name is a mass noun. A mass noun is a word that has
* singular form but represents an indeterminate quantity or group of
* something, such as "sand" or "furniture".
*
* In English, mass nouns use "some" as the indefinite article rather
* than "a" (some sand, not a sand). Their plural usage tends to
* differ from regular nouns, in that they already carry a sense of
* plurality; if you have two distinct piles of sand, the two
* together are usually still just "sand", not "two sands".
*
* When a mass noun is awkward as an object's name, you can often
* make it into a regular noun by naming its overall form. "Sand" is
* a mass noun, but recasting it as "pile of sand" makes it an
* ordinary noun. (The generic way to do this for a homogeneous
* substance is to add "quantity of".)
*/
massNoun = nil
/*
* My nominal contents is the special contents item we can use in
* naming the object. This is useful for containers whose identities
* come primarily from their contents, such as a vessel for liquids
* or a box of loose files. Returns an object that qualifies the
* name: a "water" object for BOX OF WATER, a "files" object for BOX
* OF FILES. Nil means that the object isn't named by its contents.
*
* Note that this is always a single object (or nil), not the whole
* list of contents. We can only be named by one content object.
* (So you can't have a "box of books and papers" by having separate
* nominal contents objects for the books and the papers; although
* you could fake it by creating a "books and papers" object.)
*/
nominalContents = nil
/*
* Can I be distinguished in parser messages by my contents? If so,
* we can be distinguished (in parser messages) from similar objects
* by our contents, or lack thereof: "bucket of water" vs "empty
* bucket". If this is true, our nominalContents property determines
* the contents we display for this.
*/
distinguishByContents = nil
/*
* Match the object to a noun phrase in the player's input. If the given
* token list is a valid name for this object, we return a combination of
* MatchXxx flag values describing the match. If the token list isn't a
* valid name for this object, we return 0.
*
* By default, we call simpleMatchName(), which matches the name if all of
* the words in the token list are in the object's vocabulary list,
* regardless of word order.
*
* In most cases, an unordered word match works just fine. The obvious
* drawback with this approach is that it's far too generous at matching
* nonsense phrases to object names - DUSTY OLD SPELL BOOK and BOOK DUSTY
* SPELL OLD are treated the same. In most cases, users won't enter
* nonsense phrases like that anyway, so they'll probably never notice
* that we accept them. If they enter something like that intentionally,
* we can plead Garbage In/Garbage Out: a user who willfully types a
* nonsense command has only himself to blame for a nonsense reply.
*
* Occasionally, though, there are reasons to be pickier. When these come
* up, you can override matchName() to be as picky as you like.
*
* The most common situation where pickiness is called for is when two
* objects happen to share some of the same vocabulary words, but certain
* words orderings clearly refer to only one or the other. With the
* unordered approach, this can be a nuisance for the player because it
* can trigger disambiguation questions that seem unnecessary. Overriding
* matchName() to be picky about word order for those specific objects can
* often fix this. In this implementation the matchPhrases property can be
* used for this purpose.
*
* Another example is ensuring the user knows the correct full name of an
* object as part of a puzzle: you can override matchName() to make sure
* the user doesn't accidentally stumble on the object by using one of its
* vocabulary words to refer to something else nearby. Another example is
* matching words that aren't in the vocabulary list, such as a game
* object that represents a group of apparent objects that have a whole
* range of labels ("post office box 123", say).
*/
matchName(tokens)
{
return matchNameCommon(tokens, matchPhrases, matchPhrasesExclude);
}
/*
* Match a name against a list of tokens entered by the player. phrases is
* the list of match phrases defined on the object (either for initial
* matching or for disambiguation) and excludes should be true or nil
* depending on whether failure to match phrases should exclude a match
* overall.
*/
matchNameCommon(tokens, phrases, excludes)
{
/*
* If an item is hidden, the player character either shouldn't know of
* its existence, or at least shouldn't be able to interact with it,
* so it shouldn't match any vocab.
*/
if(isHidden && !gCommand.action.unhides)
return 0;
/*
* First try the phrase-match matcher; if this fails return 0 to
* indicate that we don't match. If it succeeds in matching a phrase
* of more than one word, return MatchPhrase (we found a match).
*/
local phraseMatch = 0;
/*
* We only need to test for phrase matching if there are any phrases
* to match,
*/
if(phrases != nil)
{
/* See if our list of tokens matches any of our phrases. */
phraseMatch = phraseMatchName(phrases, tokens);
/*
* If there's a mismatch between our phrases and our tokens,
* return 0 to indicate we don't have a match overall. A mismatch
* only occurs if one or more of our tokens appears in one or more
* of our phrases but there's no match between a phrase and the
* succession of tokens.
*/
if(phraseMatch is in (0, nil) && excludes)
return 0;
/*
* A return type of true means there was no overlap between the
* tokens and the matchPhrases, so the matchPhrases should have no
* effect on the match.
*/
if(dataType(phraseMatch) != TypeInt)
phraseMatch = 0;
}
/*
* Now compute our simple match score (based on our individual tokens
* without regard to their ordering or to their matching any phrases).
*/
local simpleMatch = simpleMatchName(tokens);
/*
* If the simpleMatchName routine fails to match anything, consider
* the match a failure
*/
if(simpleMatch == 0)
return 0;
/*
* Otherwise boost the simpleMatch score with the result of the phrase
* match.
*/
return phraseMatch | simpleMatch;
}
/*
* Match the object to a noun phrase in *disambiguation* input. This
* checks words in the player's reply to a "Which one did you
* mean...?" question from the parser. When the player replies to
* this kind of question, they usually don't respond with the full
* name, but with just an adjective or two.
*
* Now, you might think we should handle these replies by just
* appending them to the original noun phrase in the input. But we
* can't just do that: matchName() *could* care about the order of
* the words in the noun phrase, so we can't just assume that we can
* stick them in somewhere and still have a valid name for the
* object. So, instead of doing that, we call this routine with the
* phrase from the player's answer to the "Which one" question.
* Since this routine knows that the new words aren't part of the
* original phrase, it can deal with them as it sees fit with respect
* to word order.
*
* The default here, of course, is to do the same thing as the
* default matchName(): we simply call simpleMatchName() to match the
* input to the object vocabulary, ignoring word order. This will
* usually work even when matchName() is overridden to care about
* word order, since the added words here are just serving to
* distinguish one object from another.
*/
matchNameDisambig(tokens)
{
/*
* If disambigMatchPhrases is defined then we must match it
* exclusively; i.e. a fail to match any relevant phrase must result
* in a failure overall; otherwise we'll just keep getting the same
* disambiguation question over and over.
*/
return matchNameCommon(tokens, disambigMatchPhrases, true);
}
/*
* Simple implementation of matchName(), which simply checks to see
* if all of the tokens are associated with the object. The "simple"
* aspect is that we don't pay any attention to the order of the
* words - we simply check that they're all in the object's
* vocabulary list, in any order.
*/
simpleMatchName(tokens)
{
/* if the token list is empty, it's no match */
if (tokens.length() == 0)
return 0;
/* we haven't found any strength demerits yet */
local strength = MatchNoTrunc | MatchNoApprox;
/* we haven't found any part-of-speech matches yet */
local partOfSpeech = 0;
/* remember the vocabulary word list and the string comparator */
local vw = vocabWords, cmp = Mentionable.dictComp;
/*
* if we're distinguishable by contents, add either the
* vocabulary for our contents object, if we have one, or the
* special 'empty' vocabulary words
*/
if (distinguishByContents)
{
vw += nominalContents != nil
? nominalContents.vocabWords : emptyVocabWords;
}
/* note the number of states we have */
local stateCnt = states.length();
/* scan the token list */
for (local i = 1, local len = tokens.length() ; i <= len ; ++i)
{
/* get the word */
local tok = tokens[i];
/* match this word */
local match = matchToken(tok, vw, cmp);
/*
* if we didn't match it from our own vocabulary, try
* matching it against any states we have
*/
if (match == 0)
{
/* try each state until we find a match or run out of states */
for (local j = 1 ; j <= stateCnt ; ++j)
{
/* try this state - stop searching if it matches */
local state = states[j];
if ((match = state.matchName(
tok, self.(state.stateProp), cmp)) != 0)
break;
}
}
/*
* if we didn't find a match for this token, the whole phrase
* fails (even if we've already matched other tokens)
*/
if (match == 0)
return 0;
/*
* We found a match for this token, so combine it into the
* running totals for the overall phrase. The overall phrase
* strength is the WEAKEST of the individual token strengths.
* The overall phrase part-of-speech mix is the union of the
* individual token part-of-speech matches.
*/
strength = min(strength, match & MatchStrengthMask);
partOfSpeech |= match;
}
/*
* Omit prepositions from the results. We don't want to reject
* prepositions that are in our vocabulary, which is why we've
* kept them in the results thus far, but we also don't want to
* match the whole phrase on the strength of just a preposition -
* "of" just isn't sufficiently specific to match "pile of
* paper". Also mask out the match-strength bits we've
* accumulated, so that we can tell specifically which parts of
* speech we've matched.
*/
partOfSpeech &= MatchPartMask & ~MatchPrep;
/*
* we need at least one actual part-of-speech match - if we
* didn't find any, we must have had a string of nothing but
* prepositions
*/
if (partOfSpeech == 0)
return 0;
/* return the overall results, combined into a single bit vector */
return strength | partOfSpeech;
}
/*
* If we have any phraseMatches defined, check whether we fail to match
* any of them. This will be the case if we find a phraseMatch containing
* one of our tokens but not the rest in the right order.
*/
phraseMatchName(phrases, tokens)
{
/* Start by assuming we won't find a mismatch */
local ok = true;
/* Note the number of tokens to check */
local tokLen = tokens.length;
/* Note the string comparator to use. */
local cmp = Mentionable.dictComp;
/*
* Go through each phraseMatch in turn to see if the tokens either
* fail to match it or succeed in matching it.
*/
foreach(local pm in valToList(phrases))
{
/* Split the phraseMatch into a list of words */
local pmList = pm.split(' ');
/*
* If the list of words from the phraseMatch contains no words in
* common with the token list, there's nothing to test; but if it
* does, we need to test it.
*/
if(pmList.overlapsWith(tokens))
{
/*
* See if we can find a list equivalent to the phraseMatch
* list as a sublist of the tokens list.
*/
local pmLength = pmList.length();
for(local i in 1 .. tokLen - pmLength + 1)
{
/*
* If we can we've succeeded in finding a phrase match, so
* we can return true straight away.
*/
if(tokens.sublist(i, pmLength).strComp(pmList, cmp))
{
return pmLength > 1 ? MatchPhrase : MatchAdj;
}
}
/*
* If we don't find a phrase match, note the failure, but
* there may be other phrases to try matching, so carry on
* looking.
*/
ok = 0;
}
}
/* Return the result */
return ok;
}
/*
* A single-quoted string, or a list of single-quoted strings containing
* exact phrases (i.e. sequences of words) that must be matched by the
* player input if any of the words in the phrase matches appear in the
* player input. Note that words defined here should also be defined in
* the vocab property; the purpose of the matchPhrases property is to
* limit matches. Note also that object will be matched if any of the
* phrases in the list is matched.
*/
matchPhrases = nil
/*
* Do we want to test for phrase matches when disambiguating? We'll assume
* that by default we do since the same reasons for wanting the phrase
* match are likely to apply when disambiguating, and that we'll use the
* same set of phrases. This can be overridden to supply a different set
* of phrases or none.
*/
disambigMatchPhrases = matchPhrases
/*
* If failing to match any of the match phrases (when the player's input
* includes at least one word used in any of them) excludes a match, then
* return nil
*/
matchPhrasesExclude = true
/*
* On dynamically creating a new object, do the automatic vocabulary
* and short name initialization.
*/
construct()
{
/* do the vocabulary initialization */
initVocab();
/* build the list of applicable states */
foreach (local s in State.all)
{
if (s.appliesTo(self))
states += s;
}
}
/*
* Vocabulary word list. This is a vector of VocabWord objects that
* we build in initVocab(), giving the individual words that this
* object uses for its noun phrase vocabulary.
*/
vocabWords = []
/* the State objects applying to this object */
states = []
/*
* The filterResolveList method allows this object to remove itself or
* other objects from the list of resolved objects.
*
* np is the noun phrase, so np.matches gives the current list of matches,
* and np.matches[i].obj gives the ith object match. To change the list of
* matches, manipulate the np.matches list.
*
* cmd is the command object, so that cmd.action gives the action about to
* be executed.
*
* mode is the match mode.
*
* By default we do nothing here.
*/
filterResolveList(np, cmd, mode) { }
/*
* Our original vocab string, if we've defined an altVocab that might replace our original
* vocab. Tbis is should normally be left to the library to set at preinit.
*/
originalVocab = nil
/*
* An alternative vocab string to be used when useAltVocabWhen is true, or list of altenative
* strings to be used under various conditions.
*/
altVocab = nil
/*
* A condition that must be true for us to change (or maintain) our vocab to our altVocab. If
* it returns nil we revert back to our original vocab. If we return -1 the change to altVocab
* becomes permanent and our updateVocab methdd won't be executed any more.
*
* But if altViocab is defined as a list, we return nil to return to our originalVocab, 0 to
* return to our original vocab and keep it for the rest of the game, n (where n > 0) to
* change our vocab to the nth item in our altVocab list or -n to change our vocab to the nth
* item in our altVocab list and then keep it there for the remainder of the game (i.e. stop
* checking or vocab updates).
*/
useAltVocabWhen = nil
/*
* A condition that when true means that the library will stop checking for switching vocab to
* and from the altVocab (or between different vocabs). This could, for example, be set to
* useAltVocabWhen when we only want to change vocab once, say when the player gets to learn
* the name of an NPC or the true nature of an object is first revealed.
*/
finalizeVocabWhen = nil
/* Initialize our alternative vocab */
initAltVocab()
{
/*
* Add ourselves to the list of Things whose vocab might change so we can be checked each
* turn.
*/
libGlobal.altVocabLst += self;
/* Store a copy of our original vocab string so we can revert to it. */
originalVocab = vocab;
}
/*
* This is called every turn on every Thing listed in libGlobal.altVocabLst. By default it
* carries out alternation between our original vocab and our altVocab according to the value
* of useAltVocabWhen. Game code can override this methed to do something different, but must
* give altVocab a non-nil value for this method to be invoked each turn, or each turn when
* this Thing is in scope.
*
*/
updateVocab()
{
if(altVocab)
{
/* Stash the current value of useAltVocabWhen so we don't have to recalculate it. */
local uavw = useAltVocabWhen;
/*
* If the condition for using our altVocab is false and we're not already using our
* original vocab, replace our current vocab with our original vocab.
*/
if(uavw == nil && vocab != originalVocab)
replaceVocab(originalVocab);
/*
* if altVocab is defined as a list we change vocab to the appropriate item in the
* list.
*/
if(dataType(altVocab) == TypeList)
{
/*
* A return value of less that 1 means we want to change the vocab to the -uavw
* item in the list and keep it there for the rest of the game.
*/
if((uavw != nil && uavw < 1) || finalizeVocabWhen)
{
libGlobal.altVocabLst -= self;
uavw = -uavw;
}
/*
* If uavw is in range and is different from the previous value, then we need to
* change the vocab to entry uavw in our altVocab list. A value of 0 means that we
* want to change the vocab to its original value and leave it there for the rest
* of the game.
*/
if(uavw != uavwNum && (uavw == nil || uavw <= altVocab.length))
{
uavwNum = uavw;
local newVocab = (uavw && uavw > 0) ? altVocab[uavw] : originalVocab;
replaceVocab(newVocab);
}
}
else
{
/*
* If the condition for using our altVocab is true and we're not already using it,
* then replace our vocab with our altVocab.
*/
if(uavw && vocab != altVocab)
replaceVocab(altVocab);
/*
* If our useAltVocabWhen property evaluates to the special value of -1, then we
* want the change to our altVocab to be permanent, so remove us from the list of
* Things whose updateVocab() property is regularly called.
*/
if(uavw == -1 || finalizeVocabWhen)
libGlobal.altVocabLst -= self;
}
}
}
/* The previous return value from useAltVocabWhen - for internal library use only. */
uavwNum = nil
/*
* Method designed to be called from the action() method of a dobjFor(XXX) block to display a
* message safely for an action that might be executed implicitly. If the action is implicit,
* the message won't be displayed until immediately after the implicit action reports. If the
* action isn't an implicit one, the message will be displayed straight away. The optional
* second msg2 parameter is a variant message for display immediately after the implicit
* action reports; otherwise msg will be used.
*/
actionReport(msg, msg2?)
{
if(gAction.isImplicit)
reportPostImplicit(msg2 ?? msg);
else
say(msg);
}
/*
* The text (or method to display the text) to display in response to a THINK ABOUT command
* directed to this Mentionable. Note that this thinkDesc will only be used if the THINK ABOUT
* command is not handled by a Thought object.
*/
// thinkDesc = nil
;
/* ------------------------------------------------------------------------ */
/*
* Match a token from the player's input against a given vocabulary list.
* Returns a set of MatchXxx flags for a match, or 0 if there's no match.
*
* 'tok' is the token string to match. 'words' is the list of words to
* match, as VocabWords objects. 'cmp' is the StringComparator object
* that we use to compare the strings.
*/
matchToken(tok, words, cmp)
{
/* we don't have a match for this token yet */
local strength = 0, partOfSpeech = 0;
/* try matching this token against our vocabulary list */
for (local len = words.length(), local i = 1 ; i <= len ; ++i)
{
/* get this vocabulary word entry */
local entry = words[i];
/* check this token against the dictionary word */
local match = cmp.matchValues(tok, entry.wordStr);
/* if there's no match, keep looking */
if (match == 0)
continue;
/*
* Figure the result flags for this match. Note that any
* bits in the String Comparator match value above 0x80
* are character approximation flags.
*/
local result =
(match & StrCompTrunc ? 0 : MatchNoTrunc)
| (match & ~0xFF ? 0 : MatchNoApprox);
/*
* Check the required match-strength flags to see if this
* match is allowed. If a MatchNoXxx flag is set in the
* required flags in the dictionary entry, it means that
* the match itself MUST have that flag. So, if a flag
* is set in the dictionary, and it's not set in the
* result, reject this match.
*/
if (entry.strengthFlags & ~result)
continue;
/*
* Okay, it's a match. There are three possibilities for how
* it relates to other matches we've already found:
*
* - It's stronger, meaning that it's not truncated while the
* earlier match was, or not approximated while the earlier
* match was. We only want to keep the strongest matche(es),
* so if we have a prior match, forget it.
*
* - It's equally strong. We might have found another
* part-of-speech usage for the word at the same match
* strength. Combine the part-of-speech flags into the
* running total.
*
* - It's weaker. We only want to keep the best matches, so
* reject this one.
*/
if (result > strength)
{
/* it's stronger - replace any past match with this one */
strength = result;
partOfSpeech = entry.posFlags;
}
else if (result == strength)
{
/* equally strong - add this entry's part of speech */
partOfSpeech |= entry.posFlags;
}
}
/* return the combined MatchXxx flags */
return strength | partOfSpeech;
}
/* ------------------------------------------------------------------------ */
/*
* A VocabWord is an entry in a Mentionable object's list of noun phrase
* words.
*/
class VocabWord: object
construct(w, f)
{
/* remember the word string */
wordStr = w;
/* separate out the part-of-speech and match-strength flags */
posFlags = f & MatchPartMask;
strengthFlags = f & MatchStrengthMask;
}
/* the word string (the text of this vocabulary word) */
wordStr = nil
/* the part-of-speech flags (MatchNoun, etc) */
posFlags = 0
/* the required match strength flags (MatchNoTrunc, MatchNoApprox) */
strengthFlags = 0
;
/* ------------------------------------------------------------------------ */
/*
* A State represents a changeable condition of a Mentionable that can be
* used as part of the object's name in command input. For example, a
* state could be used to represent whether a match is lit or unlit: the
* words 'lit' and 'unlit' could then be used to describe the object,
* according to its current condition.
*
* The actual current condition of a given object is given by a property
* of the Mentionable, which we define as part of the State object. So
* testing whether an object is lit or unlit is just a matter of checking
* the corresponding property of the object.
*
* The parser considers an object to have the state, for parsing
* purposes, if the object defines any value for the state property.
*
* Most of the State object's definition is its vocabulary, which is
* obviously language-specific. We therefore leave it to the language
* modules to define the individual State instances. Games can also add
* new states as needed, of course.
*/
class State: LState
/*
* The Mentionable property that indicates the current condition of
* an object that has this State. The range of values that this
* property takes on in the Mentionable is up to the State to define.
* For some states, this will be a simple boolean: Lit/Unlit,
* Open/Closed, On/Off, etc. For others, this might be an integer
* range or a set of string values.
*/
stateProp = nil
/*
* Does this state apply to the given object? By default, we
* consider any object that defines the state property to exhibit the
* state.
*/
appliesTo(obj) { return obj.propDefined(stateProp); }
/*
* Match a token from the object name for the given state value.
* Mentionable.matchName() calls this to see if a token applies
* because of the object's current conditdion. 'tok' is the token
* string; 'state' is the object's value for the state property; and
* 'cmp' is the string comparator to use for the string comparisons.
* Returns a combination of MatchXxx flags, or zero if the token
* doesn't match the current condition.
*
* For example, a Lit/Unlit state would return MatchAdj for 'lit' if
* 'state' is true, 0 otherwise.
*/
matchName(tok, state, cmp)
{
/* get the vocabulary for the state; if none, there's no match */
local v = getVocab(state);
if (v == nil)
return 0;
/* compare the token against the list for the current state */
return matchToken(tok, v, cmp);
}
/*
* Get the vocabulary words that apply to the given state. For
* example, a Lit/Unlit object might return 'lit' if state is true
* and 'unlit' if state is nil.
*/
getVocab(state)
{