-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcommand.t
1465 lines (1245 loc) · 52.3 KB
/
command.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"
/* ------------------------------------------------------------------------ */
/*
* A Command describes the results of parsing one player predicate - that
* is, a single verb phrase, with all its parts. This includes the
* action to be performed and the objects to perform it on. It also
* includes information on the text of the player's input, and how it
* maps onto the grammar structures defined by the language module.
*
* The Command object is built in several steps, so its contents aren't
* complete until all of the steps are completed.
*/
class Command: object
/*
* Create the command object. There are several ways to create a
* command:
*
* new Command(parseTree) - create from a parsed command syntax tree.
*
* new Command(action, dobjProd...) - create from a given Action and
* a set of parsed syntax trees for the noun phrases. The first noun
* phrase is the direct object, the second is the indirect object,
* and the third is the accessory.
*
* new Command(action, dobjs...) - create from a given Action and a
* set of objects or object lists for the noun slots. The first
* argument after the Action, dobjs, can be a single Mentionable
* object to use as the resolved direct object, or a list or vector
* of Mentionables to use as the multiple direct objects. The next
* argument is in the same format and is used for the indirect
* object. The third is the accessory.
*
* new Command(actor, action, dobjs...) - create from a given actor
* (as a Mentionable object), an Action object, and the object list.
*
* new Command() - create a blank Command, for setting up externally
* or in a subclass.
*/
construct([args])
{
/* presume the command will be implicitly addressed to the PC */
actor = gPlayerChar; //World.playerChar;
/* check the various argument list formats */
if (args.matchProto([Production]))
{
/* build the command from the parse tree */
args[1].build(self, nil);
/* save the parse tree */
parseTree = args[1];
}
else if (args.matchProto([Action, '...'])
|| args.matchProto([Mentionable, Action, '...']))
{
/* retrieve and skip the actor, if present */
local i = 1;
if (!args[i].ofKind(Action))
actor = args[i++];
/* retrieve the action */
action = args[i++];
/* the additional arguments are for the noun phrase slots */
local roles = NounRole.all, rlen = roles.length();
local alen = args.length();
for (local r = 1 ; r <= rlen && i <= alen ; ++i, ++r)
{
/* get this noun phrase match tree or object list */
local np = args[i];
/*
* Check the type of this argument: if it's a Production,
* it's a parse tree to build into a NounPhrase list for
* the slot. If it's a list/vector, it's a list of
* resolved objects for the slot.
*/
if (np.ofKind(Production))
{
/* parse tree - assign the role for this noun phrase */
np.nounPhraseRole = roles[r];
/* build it */
np.build(self, addNounListItem(roles[r], np));
}
else if (np.ofKind(List))
{
/* it's an object list - save a copy */
self.(roles[r].objListProp) = np;
}
else if (np.ofKind(Vector))
{
/* it's an object vector - save a list copy */
self.(roles[r].objListProp) = np.toList();
}
else if (np.ofKind(Mentionable))
{
/* single object - make it into a single-element list */
self.(roles[r].objListProp) = [np];
/* also set it as the current object */
self.(roles[r].objProp) = np;
/* synthesize an NPMatch object for it */
local m = new NPMatch(nil, np, 0);
m.flags = SelProg;
self.(roles[r].objMatchProp) = m;
}
else
throw new ArgumentMismatchError();
}
}
else if (args.matchProto([]))
{
/* no arguments - they just want a basic empty command */
}
else
throw new ArgumentMismatchError();
/* set up the reflexive antecedent table (if we didn't already) */
if (reflexiveAnte == nil)
reflexiveAnte = new LookupTable(16, 32);
}
/* clone - create a new Command based on this Command */
clone()
{
/* create a new object with my same property values */
local cl = createClone();
/*
* make a copy of the antecedent table, so that changes made in
* the clone don't affect the original, and vice versa
*/
cl.reflexiveAnte = new LookupTable(16, 32);
reflexiveAnte.forEachAssoc({ key, val: cl.reflexiveAnte[key] = val });
/* likewise the object list vectors, if any */
foreach (local role in npList)
{
local v = cl.(role.objListProp);
if (v.ofKind(Vector))
cl.(role.objListProp) = new Vector(v.length(), v);
}
/* return the clone */
return cl;
}
/* clone a noun phrase that's part of this command */
cloneNP(np)
{
/* create a clone of the noun phrase */
local cl = np.clone();
/* find and replace the original copy with the clone */
foreach (local role in npList)
{
/* look for 'np' in this role's list of noun phrases */
local idx;
if ((idx = self.(role.npListProp).indexOf(np)) != nil)
{
/* found it - replace it with the clone, and we're done */
self.(role.npListProp)[idx] = cl;
break;
}
}
/* return the clone */
return cl;
}
/*
* Execute the action. This carries out the entire command
* processing sequence for the action. If the action involves a list
* of objects (as in TAKE ALL or DROP BOOK AND CANDLE), we iterate
* over the listed objects, executing the action on each object in
* turn.
*/
exec()
{
try
{
action.reset();
gAction = action;
originalAction = action;
lastAction = nil;
gCommand = self;
actions = [];
if(verbProd != nil)
local lastCommandStr = buildCommandString();
if(gameMain.autoSwitchAgain && action != Again)
{
gameMain.againRepeatsParse = action.againRepeatsParse;
}
if(action.isRepeatable)
{
libGlobal.lastCommand = self.createClone();
libGlobal.lastCommandForAgain = lastCommandStr;
}
if(action.includeInUndo && verbProd != nil)
{
libGlobal.lastCommandForUndo = lastCommandStr;
savepoint();
}
/*
* First, carry out the group action. This gives the verb a
* chance to perform the action collectively on all of the objects
* at once.
*/
action.execGroup(self);
/*
* Get the list of predicate noun roles. We only iterate over the
* noun roles that are verb arguments.
*/
local predRoles = npList.subset({ r: r.isPredicate });
/*
* If we have any noun phrases, iterate over each combination of
* objects. Otherwise, this is an intransitive verb, so just
* perform a single execution of the Action, with no objects.
*/
if (predRoles.length() == 0)
{
/* it's intransitive - just execute once */
execIter([action]);
}
else
{
/*
* It's transitive, so execute iteratively over the objects.
* First, generate the distinguished object names for each
* list.
*/
foreach (local role in predRoles)
{
/*
* If we're dealing with the direct object and our action wants to combine
* duplicate objects remove any duplicate direct objects from our list for
* direcc objects that allow duplicates to be removed.
*/
if(role == DirectObject && action.combineDuplicateObjects)
{
local lst1 = self.(role.objListProp).toList();
local lst2 = lst1.subset({x:x.obj.combineDuplicateObjects});
local lst3 = lst1.subset({x:x.obj.combineDuplicateObjects == nil});
local lst4 = lst2.mapAll({x:x.obj}).getUnique();
lst1 = [];
foreach(local o in lst4)
{
lst1 += lst2.valWhich({x:x.obj==o});
}
lst1 += valToList(lst3);
self.(role.objListProp) = new Vector(lst1);
}
/* get the NPMatch list for this role */
local matches = self.(role.objListProp);
/* get the list of names for this role's list of objects */
local names = Distinguisher.getNames(
matches.mapAll({ m: m.obj }), nil);
/*
* Assign each NPMatch object its name from the name list.
* The name list is of the form [name, [objects]], so for
* each object, we need to find the element n such that
* n[2] (the object list) contains the object in question,
* then retrieve the name string from n[1].
*/
matches.forEach(
{ m: m.name = names.valWhich(
{ n: n[2].indexOf(m.obj) != nil })[1] });
}
/*
* Sort the preRoles into canonical order (typically
* DirectObject, IndirectObject, AccessoryObject)
*/
predRoles = predRoles.sort(SortAsc, {a, b: a.order - b.order});
/*
* execute for each combination of objects, starting with the
* objects in the first role list
*/
execCombos(predRoles, 1, [action]);
}
/*
* Let every action that has been involved in this Command
* summarize or report on what it has just done.
*/
foreach(local a in actions)
{
action = a;
action.reportAction();
}
/*
* Reset the action to the original one, if it has been executed,
* or else to the first one actually executed.
*/
action = (actions.indexOf(originalAction) || actions.length == 0)
? originalAction : actions[1];
/* List our own sequence of afterReports, if we have any. */
afterReport();
/* Carry out the after action handling for the current action */
action.afterAction();
/*
* Carry out the turn sequence handling (daemons and turn count
* increment) for the current action.
*/
action.turnSequence();
/* Advance the game clock time for the current action. */
action.advanceTime();
}
catch(AbortActionSignal aas)
{
/*
* An AbortActionSignal skips the rest of the command including
* the post-action processing such as daemons and advancing the
* turn counter; the idea is that an abort command (macro)
* effectively cancels the entire command, or at least the rest of
* the command from the point it's issued.
*/
}
}
/*
* Rebuild the original command string from the tokens. We call this out
* as a separate method so language-specific code can override it.
*/
buildCommandString()
{
return valToList(verbProd.tokenList).mapAll({x:
getTokVal(x)}).join(' ');
}
/* A list of actions executed directly by this command or via a Doer */
actions = []
/*
* The originalAction this Command started out with, which may be changed
* by a Doer (or some other mechanism)
*/
originalAction = nil
/*
* A list of strings containing reports to be displayed at the end of the
* command execution cycle for this command.
*/
afterReports = []
/*
* A list of strings containing reports to be immediately displayed after any implicit action
* reports
*/
postImplicitReports = []
/*
* Run through our list of afterReports displaying each in turn. We do
* this on the Command rather than on any of the Actions since actions may
* invoke other actions (implicit, remapped, nested or replaced), while
* the afterReports pertain to the command as a whole.
*/
afterReport()
{
foreach(local cur in afterReports)
{
"<.p>";
say(cur);
}
}
/*
* A list of reports of previous implicit actions performed in the course
* of executing this command which can be used if we need to collate a
* report of a stack of implicit actions.
*/
implicitActionReports = []
/*
* Execute the command for each combination of objects for noun role index
* 'n' and above. 'lst' is a list containing a partial object combination
* for roles at lower indices. We iterate over each combination of the
* remaining objects. predRoles is a list containing predicate roles (such
* DirectObject, IndirectObject, AccessoryObject) relating to this action.
* Callers are responsible for sorting predRoles into the correct order
* before calling this method.
*/
execCombos(predRoles, n, lst)
{
/* get this slot's role */
local role = predRoles[n];
/* iterate over the objects in the slot at this index */
foreach (local obj in self.(role.objListProp))
{
/* set the current object and selection flags for this role */
self.(role.objProp) = obj.obj;
self.(role.objMatchProp) = obj;
/*
* create a new list that includes the new object at the
* appropriate place.
*/
local nlst = lst;
/* add the object to the list */
nlst += obj.obj;
/*
* if there are more noun roles, recursively iterate over
* combinations of the remaining roles
*/
if (n < predRoles.length())
{
/* we have more roles - iterate over them recursively */
execCombos(predRoles, n+1, nlst);
}
else
{
/*
* this is the last role - we have a complete combination
* of current objects now, so execute the action with the
* current set
*/
execIter(nlst);
}
}
}
/*
* Execute one iteration of the command for a particular combination of objects. 'lst' is the
* object combination about to be executed: the object combination to execute: this is an
* [action, dobj, iobj, ...] list.
*/
execIter(lst)
{
try
{
/*
* Allow the special verb manager to veto this action if we've been executing a
* SpecialVerb.
*/
specialVerbMgr.checkSV(lst);
/*
* Give our actor's preAction method the chance to veto this action (or maybe do
* something else) before it's passed to a Doer.
*/
gActor.preAction(lst);
/* carry out the default action processing */
execDoer(lst);
}
catch (ExitSignal ex)
{
}
finally
{
/*
* If the action isn't one this Command has executed before while
* iterating over its list of objects, note that we've now
* executed it
*/
if(actions.indexOf(action) == nil)
actions += action;
/* Restore the original action */
action = originalAction;
}
}
/*
* Execute the command via the Doers that match the command's action
* and objects. 'lst' is the object combination to execute: [action,
* dobj, iobj, ...].
*/
execDoer(lst)
{
/*
* In case the author tries to use gDobj and the like to match a
* command, assign values to them
*/
if(dobj)
gDobj = dobj;
if(iobj)
gIobj = iobj;
if(acc)
gAobj = aobj;
/* find the list of matching Doers */
local dlst = DoerCmd.findDoers(lst);
IfDebug(doers, oSay('''[Executing Doer; cmd = '<<dlst[1].cmd>>']\n'''));
dlst[1].exec(self);
}
/* Change the action to a new action with a new set of objects */
changeAction(newAct, newDo, newIo, newAo)
{
action = newAct;
/*
* If we haven't used this action before while executing this command,
* reset it.
*/
if(actions.indexOf(action) == nil)
action.reset();
dobj = newDo;
iobj = newIo;
acc = newAo;
action.curDobj = newDo;
action.curIobj = newIo;
action.curAobj = newAo;
gAction = action;
gAction.redirectParent = originalAction;
}
/*
* Invoke a callback for each object in the current command
* iteration. This invokes the callback on the direct object,
* indirect object, accessory, and any other custom roles added by
* the game.
*/
forEachObj(func)
{
try
{
/* loop over the occupied roles */
foreach (local role in npListSorted)
{
/* if this role is occupied, invoke the callback */
local obj = self.(role.objProp);
if (obj != nil)
func(role, obj);
}
}
catch (BreakLoopSignal sig)
{
/* we've broken out of the loop, so 'sig' is now handled */
}
}
/*
* Are terse messages OK for this command? A terse message is a
* simple acknowledgment of a standard command, such as "Taken",
* "Dropped", "Done", etc. The action is so ordinary that the result
* of a successful attempt should be obvious to the player; so the
* only reply needed is an acknowledgment, not an explanation.
*
* Terse replies only apply to simple actions, and only when the
* actor is the player character, AND there's no disambiguation
* involved. If the actor isn't the PC, an acknowledgment isn't
* sufficient; we should instead describe the NPC carrying out the
* action, since it's something we observe, not something we do. If
* any objects were disambiguated, we also want to describe the
* action fully, because the ambiguity calls for a description of
* precisely which objects were chosen. Disambiguation guesses are
* sometimes wrong, so when they're involved, it's not safe to assume
* that the player and parser must both be thinking the same thing.
* Showing a full description of the action will make it obvious to
* the player when we guessed wrong, because the description won't
* accord with what they had in mind. A terse acknowledgment would
* hide this difference, allowing the player to wrongly assume that
* the parser did what they thought it was going to do and
* potentially leading to confusion down the road.
*/
terseOK()
{
/* use full messages for NPC-directed commands */
if (actor != gPlayerChar)
return nil;
/*
* use full message for commands where ALL was used, since
* the player might not otherwise know what ALL referred to.
*/
if(matchedAll || matchedMulti)
return nil;
/* check all noun roles for Disambig flags */
foreach (local role in npList)
{
/*
* if this is a predicate role, and there's an object in this
* slot with the Disambig flag, don't allow terse messages
*/
if (role.isPredicate
&& self.(role.objProp) != nil
&& (self.(role.objMatchProp).flags & SelDisambig) != 0)
return nil;
}
/*
* If we're reporting on fewer objects than the player requested then
* we'd better be specific about which ones we mean.
*/
if(dobjs.length > action.reportList.length)
return nil;
/* we have no objection to terse messages */
return true;
}
/*
* Add a noun production, building it out as though it had been part
* of the original parse tree. This can be used to add a noun phrase
* after the initial parsing, such as when the player supplies a
* missing object.
*/
addNounProd(role, prod)
{
/* create a noun list item for the production */
local np = addNounListItem(role, prod);
if(npListSorted.length != npList.length)
npListSorted = npList;
/* build the tree */
prod.nounPhraseRole = role;
prod.build(self, np);
/* let the verb production know about the change */
verbProd.answerMissing(self, np);
}
/* add a noun phrase to the given role (a NounRole) */
addNounListItem(role, prod)
{
/* create the new noun phrase object of the appropriate type */
local np = prod.npClass.createInstance(nil, prod);
/* remember the role in the noun phrase */
np.role = role;
/* add it to the given list */
self.(role.npListProp) += np;
/*
* If this role isn't already in our list or roles, and it has a
* match property, add it. Roles without match properties aren't
* predicate noun roles, so they don't go in our predicate object
* list.
*/
if (npList.indexOf(role) == nil)
{
npList += role;
/*
* if the action is a TIAction then make sure the Direct and
* Indirect Objects are dealt with in the right order as specified
* by the action's resolveIobjFirst property. We do this on a copy
* of the list (npSorted) so we don't break anything that needs
* the original order (such as matching a Doer).
*/
npListSorted = npList;
if(action != nil && action.ofKind(TIAction))
{
local doIdx = npListSorted.indexOf(DirectObject);
local ioIdx = npListSorted.indexOf(IndirectObject);
if(doIdx != nil && ioIdx != nil)
{
if((action.resolveIobjFirst && ioIdx > doIdx)
|| (!action.resolveIobjFirst && doIdx > ioIdx))
{
npListSorted[ioIdx] = DirectObject;
npListSorted[doIdx] = IndirectObject;
}
}
}
}
/* return the new noun phrase */
return np;
}
/*
* Start processing a new disambiguation reply. This adds a reply to
* a disambiguation question.
*/
startDisambigReply(parent, prod)
{
/* create the first NounPhrase for this reply */
local np = new NounPhrase(parent, prod);
/* add a new NounPhrase list to the reply list */
disambig = disambig.append([np]);
/* return the new noun phrase */
return np;
}
/*
* Add a disambiguation list item. This adds a NounPhrase item to
* the current reply list.
*/
addDisambigNP(prod)
{
/* get the current reply list */
local idx = disambig.length(), lst = disambig[idx];
/* create the new noun phrase */
local np = new NounPhrase(lst[1].parent, prod);
/* add it to the current disambiguation reply list */
disambig[idx] = lst + np;
/* return it */
return np;
}
/*
* Fetch a disambiguation reply. If we have more replies available,
* this returns the next reply's noun phrase list, otherwise nil.
*/
fetchDisambigReply()
{
return (disambigIdx <= disambig.length()
? disambig[disambigIdx++]
: nil);
}
/* mark a noun phrase role as empty */
emptyNounRole(role)
{
/* if this role isn't in our list yet, add it */
if (npList.indexOf(role) == nil)
npList += role;
/* count the missing phrase */
++missingNouns;
/* clear out the role list */
self.(role.npListProp) = [];
}
/* resolve the noun phrases */
resolveNouns()
{
/*
* Note that we're the current command, in case anything wants to know
*/
gCommand = self;
/* we don't have an error for this resolution pass yet */
cmdErr = nil;
/* we haven't started pulling disambiguation replies yet */
disambigIdx = 1;
/* Note the orginal lists of nps */
local npListOld = npList;
local npListSortedOld = npListSorted;
/*
* If the player has just supplied a missing object, we only want to
* resolve the object for that role.
*/
if(npToResolve != nil)
{
npList = [npToResolve];
npListSorted = [npToResolve];
}
/*
* Start by getting the basic vocabulary matches for each noun
* phrase. Run through each noun phrase list.
*/
forEachNP({ np: np.matchVocab(self) });
/*
* Before we do the object selection, build the tentative lists
* of resolved objects. This can be handy during disambiguation
* to help decide the resolution of one slot based on the
* possible values for other slots. For example, for PUT COIN IN
* JAR, it might help us choose a coin to know that the iobj is
* JAR.
*/
buildObjLists();
/* determine the actor */
if (actorNPs != [])
{
/*
* We have an explicit addressee. If we have more than one
* object for the actor phrase, disambiguate to a single
* object. Disambiguate in the context of TALK TO.
*/
local anp = actorNPs[1];
if (anp.matches.length() > 1)
anp.disambiguate(self, 1, TalkTo);
if (anp.matches.length() == 0)
throw new UnmatchedActorError(anp);
/* pull out the match as the actor object */
actor = anp.matches[1].obj;
}
/*
* select the objects from the available matches according to the
* grammatical mode (definite, indefinite, plural)
*/
forEachNP({ np: np.selectObjects(self) });
/*
* Go back and re-resolve ALL lists. For two-object commands,
* resolving ALL in one slot sometimes depends on resolving the
* object in the other slot first.
*/
forEachNP({ np: np.resolveAll(self) });
/*
* Set up the second-person reflexive pronoun antecedent. For a
* command addressed in the imperative form to an NPC (e.g., BOB,
* EXAMINE YOURSELF), YOU binds to the addressee. For anything
* else (e.g., EXAMINE YOURSELF, or TELL BOB TO EXAMINE
* YOURSELF), YOU binds to the player character.
*/
if (reflexiveAnte != nil)
{
if (actorNPs != [] && actorPerson == 2)
{
/* imperative addressed to an actor: YOU is the actor */
reflexiveAnte[You] = [actor];
}
else
{
/* for anything else, YOU is the PC */
reflexiveAnte[You] = [gPlayerChar]; //[World.playerChar];
}
}
/*
* Resolve reflexive pronouns (e.g., ASK BOB ABOUT HIMSELF). We
* have to do this as a separate step because reflexives refer
* back to other noun phrases in the same command. We can't do
* this until after we resolve everything else.
*/
forEachNP({ np: np.resolveReflexives(self) });
/* check for empty roles */
foreach (local role in npList)
{
if (self.(role.npListProp).length() == 0)
throw new EmptyNounError(self, role);
}
/*
* Clear out the old object lists, then build them anew. The old
* object lists were tentative, before disambiguation; we want to
* replace them now with the final lists.
*/
buildObjLists();
/* Restore the original lists of nps */
npList = npListOld;
npListSorted = npListSortedOld;
}
/* carry out a callback for each noun phrase in each list */
forEachNP(func)
{
/* run through each noun phrase list in the command */
foreach (local role in npListSorted)
{
/* run through each NounPhrase in this slot's list */
foreach (local np in self.(role.npListProp))
{
/* invoke the callback on this item */
func(np);
}
}
}
/*
* If the parser has just asked the player to supply a missing object via
* the askMissingObject() function, we don't want to resolve the nouns for
* every object role, but only for the role with which askMissingObject()
* is currently concerned; askMissingObject() stores that role here so
* that our resolvedNouns() method knows to resolve only the noun for this
* role rather than for all the roles in the command. If npToResolve is
* nil (as it normally will be) then it will be ignored, and all noun
* roles will be resolved.
*/
npToResolve = nil
/*
* Build the object lists. This runs through each NounPhrase in the
* command to build its 'objs' list, then builds the corresponding
* master list in the Command object.
*/
buildObjLists()
{
/*
* Note that we're the current command object, so that other object
* methods can refer to our object lists.
*/
gCommand = self;
/* run through each active noun phrase list */
foreach (local role in npList)
{
/* set up a vector to hold this list's nouns */
self.(role.objListProp) = new Vector(10);
/* build the object list for each NounPhrase */
foreach (local np in self.(role.npListProp))
{
/* build the list */
np.buildObjList();
/* append it to the master match list for this slot */
self.(role.objListProp).appendAll(np.matches);
}
}
}
/*
* Save a potential antecedent for a reflexive pronoun coming up
* later in the command. Each time we visit a noun phrase during the
* reflexive pronoun phase, we'll note its resolved objects here.
* Since we visit the noun phrases in their order of appearance in
* the command, we'll naturally always have the latest one mentioned
* when we come to a reflexive pronoun. This gives us the correct
* resolution, which is the nearest preceding noun. Note that the
* noun phrase shouldn't call this routine to note reflexive
* pronouns, since they don't bind to earlier reflexive pronouns -
* they only bind to regular noun phrases.
*/
saveReflexiveAnte(obj)
{
/* if we don't have a reflexive antecedent table, skip this */
if (reflexiveAnte == nil)
return;
/* if the object isn't already a list, wrap it in a list */
local lst = obj;
if (!lst.ofKind(Collection))
lst = [obj];
/*
* Run through the regular pronoun list, and save this object
* with the pronouns that apply to this object. Note that a
* given object might match multiple pronouns, so we might save
* the object for several different pronouns.
*/
foreach (local p in Pronoun.all)