Debian Codegraph @

terps/tads/tads3/lib/adv3/resolver.t

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved.
5 *
6 * TADS 3 Library: Resolvers.
7 *
8 * This module defines the Resolver classes. A Resolver is an abstract
9 * object that the parser uses to control the resolution of noun phrases
10 * to game objects. Specialized Resolver subclasses allow noun phrases
11 * to be resolved differently according to their grammatical function in
12 * a command.
13 */
14
15 #include "adv3.h"
16
17
18 /* ------------------------------------------------------------------------ */
19 /*
20 * Basic object resolver. An Action object creates an object resolver
21 * to mediate the process of resolving noun phrases to objects.
22 *
23 * A resolver encapsulates a set of object resolution rules. In most
24 * cases, an action that takes only a direct object can be its own
25 * resolver, because it needs only one set of resolution rules; for this
26 * reason, this basic Resolver implementation is designed to work with
27 * the direct object. Actions with multiple objects will need separate
28 * resolvers for each object, since they might want to use different
29 * rules for the different objects.
30 */
31 class Resolver: object
32 construct(action, issuingActor, targetActor)
33 {
34 /* remember my action and actor objects */
35 action_ = action;
36 issuer_ = issuingActor;
37 actor_ = targetActor;
38
39 /* cache the scope list */
40 cacheScopeList();
41 }
42
43 /*
44 * Are we a sub-phrase resolver? This should return true if we're
45 * being used to resolve a sub-phrase of the main phrase.
46 */
47 isSubResolver = nil
48
49 /*
50 * Reset the resolver - this can be called if we are to re-use the
51 * same resolver to resolve a list of noun phrases again.
52 */
53 resetResolver()
54 {
55 /* forget the equivalents we've resolved so far */
56 equivs_ = nil;
57 }
58
59 /* get the action we're resolving */
60 getAction() { return action_; }
61
62 /* get the target actor */
63 getTargetActor() { return actor_; }
64
65 /*
66 * Match an object's name. By default, we'll call the object's own
67 * matchName method with the given original and adjusted token
68 * lists. Subclasses can override this to call different match
69 * methods (such as matchNameDisambig).
70 */
71 matchName(obj, origTokens, adjustedTokens)
72 {
73 return obj.matchName(origTokens, adjustedTokens);
74 }
75
76 /*
77 * Get the resolver for qualifier phrases. By default, this simply
78 * returns myself, since the resolver for qualifiers is in most
79 * contexts the same as the main resolver.
80 *
81 * This can be overridden in contexts where the qualifier resolver
82 * is different from the main resolver. In general, when a
83 * sub-resolver narrows the scope for resolving a phrase, such as an
84 * exclusion list or a disambiguation response, we will want to
85 * resolve qualifiers in the context of the main resolution scope
86 * rather than the narrowed scope.
87 */
88 getQualifierResolver() { return self; }
89
90 /*
91 * Get the resolver for possessive phrases. By default, we return a
92 * standard possessive resolver. This can be overridden in contexts
93 * wher ethe possesive resolution context is special.
94 */
95 getPossessiveResolver() { return new PossessiveResolver(self); }
96
97 /*
98 * Cache the scope list for this object. By default, we cache the
99 * standard physical scope list for our target actor.
100 *
101 * Note that if a subclass uses completely different rules for
102 * determining scope, it need not store a scope_ list at all. The
103 * scope_ list is purely an implementation detail of the base
104 * Resolver class. A subclass can use whatever internal
105 * implementation it wants, as long as it overrides objInScope() and
106 * getScopeList() to return consistent results.
107 */
108 cacheScopeList()
109 {
110 /* cache our actor's default scope list */
111 scope_ = actor_.scopeList();
112 }
113
114 /*
115 * Determine if an object is in scope for the purposes of object
116 * resolution. By default, we'll return true if the object is in our
117 * cached scope list - this ensures that we produce results that are
118 * consistent with getScopeList().
119 *
120 * Some subclasses might want to override this method to decide on
121 * scope without reference to a cached scope list, for efficiency
122 * reasons. For example, if a command's scope is the set of all
123 * objects, caching the full list would take a lot of memory; to save
124 * the memory, you could override cacheScopeList() to do nothing at
125 * all, and then override objInScope() to return true - this will
126 * report that every object is in scope without bothering to store a
127 * list of every object.
128 *
129 * Be aware that if you override objInScope(), you should ensure that
130 * getScopeList() yields consistent results. In particular,
131 * objInScope() should return true for every object in the list
132 * returned by getScopeList() (although getScopeList() doesn't
133 * necessarily have to return every object for which objInScope() is
134 * true).
135 */
136 objInScope(obj) { return scope_.indexOf(obj) != nil; }
137
138 /*
139 * Get the full list of objects in scope. By default, this simply
140 * returns our cached scope list.
141 *
142 * For every object in the list that getScopeList() returns,
143 * objInScope() must return true. However, getScopeList() need not
144 * return *all* objects that are in scope as far as objInScope() is
145 * concerned - it can, but a subset of in-scope objects is
146 * sufficient.
147 *
148 * The default implementation returns the complete set of in-scope
149 * objects by simply returning the cached scope list. This is the
150 * same scope list that the default objInScope() checks, which
151 * ensures that the two methods produce consistent results.
152 *
153 * The reason that it's okay for this method to return a subset of
154 * in-scope objects is that the result is only used to resolve
155 * "wildcard" phrases in input, and such phrases don't have to expand
156 * to every possible object. Examples of wildcard phrases include
157 * ALL, missing phrases that need default objects, and locational
158 * phrases ("the vase on the table" - which isn't superficially a
159 * wildcard, but implicitly contains one in the form of "considering
160 * only everything on the table"). It's perfectly reasonable for the
161 * parser to expand a wildcard based on what's actually in sight, in
162 * mind, or whatever's appropriate. So, in cases where you define an
163 * especially expansive objInScope() - for example, a universal scope
164 * like the one TopicResolver uses - it's usually fine to use the
165 * default definition of getScopeList(), which returns only the
166 * objects that are in the smaller physical scope.
167 */
168 getScopeList() { return scope_; }
169
170 /*
171 * Is this a "global" scope? By default, the scope is local: it's
172 * limited to what the actor can see, hear, etc. In some cases, the
173 * scope is broader, and extends beyond the senses; we call those
174 * cases global scope.
175 *
176 * This is an advisory status only. The caller musn't take this to
177 * mean that everything is in scope; objInScope() and getScopeList()
178 * must still be used to make the exact determination of what objects
179 * are in scope. However, some noun phrase productions might wish to
180 * know generally whether we're in a local or global sort of scope,
181 * so that they can adjust their zeal at reducing ambiguity. In
182 * cases of global scope, we generally want to be more inclusive of
183 * possible matches than in local scopes, because we have much less
184 * of a basis to guess about what the player might mean.
185 */
186 isGlobalScope = nil
187
188 /*
189 * Get the binding for a reflexive third-person pronoun (himself,
190 * herself, itself, themselves). By default, the reflexive binding
191 * is the anaphoric binding from the action - that is, it refers
192 * back to the preceding noun phrase in a verb phrase with multiple
193 * noun slots (as in ASK BOB ABOUT HIMSELF: 'himself' refers back to
194 * 'bob', the previous noun phrase).
195 */
196 getReflexiveBinding(typ) { return getAction().getAnaphoricBinding(typ); }
197
198 /*
199 * Resolve a pronoun antecedent, given a pronoun selector. This
200 * returns a list of ResolveInfo objects, for use in object
201 * resolution. 'poss' is true if this is a possessive pronoun (his,
202 * her, its, etc), nil if it's an ordinary, non-possessive pronoun
203 * (him, her, it, etc).
204 */
205 resolvePronounAntecedent(typ, np, results, poss)
206 {
207 local lst;
208 local scopeLst;
209
210 /* check the Action for a special override for the pronoun */
211 lst = getAction().getPronounOverride(typ);
212
213 /* if there's no override, get the standard raw antecedent list */
214 if (lst == nil)
215 lst = getRawPronounAntecedent(typ);
216
217 /* if there is no antecedent, return an empty list */
218 if (lst != nil && lst != [])
219 {
220 local cur;
221
222 /* if it's a single object, turn it into a list */
223 if (dataType(lst) == TypeObject)
224 lst = [lst];
225
226 /* add any extra objects for the pronoun binding */
227 foreach (cur in lst)
228 lst = cur.expandPronounList(typ, lst);
229
230 /* filter the list to keep only in-scope objects */
231 scopeLst = new Vector(lst.length());
232 foreach (cur in lst)
233 {
234 local facets;
235
236 /* get the object's facets */
237 facets = cur.getFacets();
238
239 /*
240 * If it has any, pick the best one that's in scope. If
241 * not, keep the object only if it's in scope.
242 */
243 if (facets.length() != 0)
244 {
245 local best;
246
247 /*
248 * This object has other facets, so we want to
249 * consider the other in-scope facets in case any are
250 * more suitable than the original one. For example,
251 * we might have just referred to a door, and then
252 * traveled through the door to an adjoining room.
253 * We now want the antecedent to be the side (facet)
254 * of the door that's in the new location.
255 */
256
257 /* get the in-scope subset of the facets */
258 facets = (facets + cur).subset({x: objInScope(x)});
259
260 /* keep the best facet from the list */
261 best = findBestFacet(actor_, facets);
262
263 /*
264 * If we found a winner, use it instead of the
265 * original.
266 */
267 if (best != nil)
268 cur = best;
269 }
270
271 /* if the object is in scope, include it in the results */
272 if (objInScope(cur))
273 scopeLst.append(cur);
274 }
275
276 /* create a list of ResolveInfo objects from the antecedents */
277 lst = scopeLst.toList().mapAll({x: new ResolveInfo(x, 0, np)});
278 }
279
280 /*
281 * If there's nothing matching in scope, try to find a default.
282 * Look to see if there's a unique default object matching the
283 * pronoun, and select it if so.
284 */
285 if (lst == nil || lst == [])
286 lst = getPronounDefault(typ, np);
287
288 /* run the normal resolution list filtering on the list */
289 lst = action_.finishResolveList(lst, whichObject, np, nil);
290
291 /* return the result */
292 return lst;
293 }
294
295 /*
296 * Get the "raw" pronoun antecedent list for a given pronoun
297 * selector. This returns a list of objects matching the pronoun.
298 * The list is raw in that it is given as a list of game objects
299 * (not ResolveInfo objects), and it isn't filtered for scope.
300 */
301 getRawPronounAntecedent(typ)
302 {
303 /* check for pronouns that are relative to the issuer or target */
304 switch(typ)
305 {
306 case PronounMe:
307 /*
308 * It's a first-person construction. If the issuing actor is
309 * the player character, and we don't treat you/me as
310 * interchangeable, this refers to the player character only
311 * if the game refers to the player character in the second
312 * person (so, if the game calls the PC "you", the player
313 * calls the PC "me"). If the issuing actor isn't the player
314 * character, then a first-person pronoun refers to the
315 * command's issuer. If we allow you/me mixing, then "me"
316 * always means the PC in input, no matter how the game
317 * refers to the PC in output.
318 */
319 if (issuer_.isPlayerChar
320 && issuer_.referralPerson != SecondPerson
321 && !gameMain.allowYouMeMixing)
322 {
323 /*
324 * the issuer is the player, but the game doesn't call
325 * the PC "you", so "me" has no meaning
326 */
327 return [];
328 }
329 else
330 {
331 /* "me" refers to the command's issuer */
332 return [issuer_];
333 }
334
335 case PronounYou:
336 /*
337 * It's a second-person construction. If the target actor is
338 * the player character, and we don't treat you/me as
339 * interchangeable, this refers to the player character only
340 * if the game refers to the player character in the first
341 * person (so, if the game calls the PC "me", then the player
342 * calls the PC "you"). If we allow you/me mixing, "you" is
343 * always the PC in input, no matter how the game refers to
344 * the PC in output.
345 *
346 * If the target actor isn't the player character, then a
347 * second-person pronoun refers to either the target actor or
348 * to the player character, depending on the referral person
349 * of the current command that's targeting the actor. If the
350 * command is in the second person, then a second-person
351 * pronoun refers to the actor ("bob, hit you" means for Bob
352 * to hit himself). If the command is in the third person,
353 * then a second-person pronoun is a bit weird, but probably
354 * refers to the player character ("tell bob to hit you"
355 * means for Bob to hit the PC).
356 */
357 if (actor_.isPlayerChar
358 && actor_.referralPerson != FirstPerson
359 && !gameMain.allowYouMeMixing)
360 {
361 /*
362 * the target is the player character, but the game
363 * doesn't call the PC "me", so "you" has no meaning in
364 * this command
365 */
366 return [];
367 }
368 else if (actor_.commandReferralPerson == ThirdPerson)
369 {
370 /*
371 * we're addressing the actor in the third person, so YOU
372 * probably doesn't refer to the target actor; the only
373 * other real possibility is that it refers to the player
374 * character
375 */
376 return [gPlayerChar];
377 }
378 else
379 {
380 /* in other cases, "you" refers to the command's target */
381 return [actor_];
382 }
383
384 default:
385 /*
386 * it's not a relative pronoun, so ask the target actor for
387 * the antecedent based on recent commands
388 */
389 return actor_.getPronounAntecedent(typ);
390 }
391 }
392
393 /*
394 * Determine if "all" is allowed for the noun phrase we're resolving.
395 * By default, we'll just ask the action.
396 */
397 allowAll()
398 {
399 /* ask the action to determine whether or not "all" is allowed */
400 return action_.actionAllowsAll;
401 }
402
403 /*
404 * Get the "all" list - this is the list of objects that we should
405 * use when the object of the command is the special word "all".
406 * We'll ask the action to resolve 'all' for the direct object,
407 * since we are by default a direct object resolver.
408 */
409 getAll(np)
410 {
411 /*
412 * ask the action to resolve 'all' for the direct object, and
413 * then filter the list and return the result
414 */
415 return filterAll(action_.getAllDobj(actor_, getScopeList()),
416 DirectObject, np);
417 }
418
419 /*
420 * Filter an 'all' list to remove things that don't belong. We
421 * always remove the actor executing the command, as well as any
422 * objects explicitly marked as hidden from 'all' lists.
423 *
424 * Returns a ResolveInfo list, with each entry marked with the
425 * MatchedAll flag.
426 */
427 filterAll(lst, whichObj, np)
428 {
429 local result;
430
431 /* set up a vector to hold the result */
432 result = new Vector(lst.length());
433
434 /*
435 * run through the list and include elements that we don't want
436 * to exclude
437 */
438 foreach (local cur in lst)
439 {
440 /*
441 * if this item isn't the actor, and isn't marked for
442 * exclusion from 'all' lists in general, include it
443 */
444 if (cur != actor_ && !cur.hideFromAll(getAction()))
445 result.append(cur);
446 }
447
448 /*
449 * create a ResolveInfo for each object, with the 'MatchedAll'
450 * flag set for each object
451 */
452 result.applyAll({x: new ResolveInfo(x, MatchedAll, np)});
453
454 /* run through the list and apply each object's own filtering */
455 result = getAction().finishResolveList(result, whichObject, np, nil);
456
457 /* return the result as a list */
458 return result.toList();
459 }
460
461 /*
462 * Get the list of potential default objects. This is simply the
463 * basic 'all' list, not filtered for exclusion with hideFromAll.
464 */
465 getAllDefaults()
466 {
467 /* ask the action to resolve 'all' for the direct object */
468 local lst = action_.getAllDobj(actor_, getScopeList());
469
470 /* return the results as ResolveInfo objects */
471 return lst.mapAll({x: new ResolveInfo(x, 0, nil)});
472 }
473
474 /*
475 * Filter an ambiguous list of objects ('lst') resolving to a noun
476 * phrase. If the objects in the list vary in the degree of
477 * suitability for the command, returns a list consisting only of the
478 * most suitable objects. If the objects are all equally suitable -
479 * or equally unsuitable - the whole list should be returned
480 * unchanged.
481 *
482 * 'requiredNum' is the number of objects required in the final list
483 * by the caller; if the result list is larger than this, the caller
484 * will consider the results ambiguous.
485 *
486 * 'np' is the noun phrase production that we're resolving. This is
487 * usually a subclass of NounPhraseProd.
488 *
489 * This routine does NOT perform any interactive disambiguation, but
490 * is merely a first attempt at reducing the number of matching
491 * objects by removing the obviously unsuitable ones.
492 *
493 * For example, for an "open" command, if the list consists of one
494 * object that's open and one object that's currently closed, the
495 * result list should include only the closed one, since it is
496 * obvious that the one that's already open does not need to be
497 * opened again. On the other hand, if the list consists only of
498 * open objects, they should all be returned, since they're all
499 * equally unsuitable.
500 *
501 * It is not necessary to reduce the list to a single entry; it is
502 * adequate merely to reduce the ambiguity by removing any items that
503 * are clearly less suitable than the survivors.
504 */
505 filterAmbiguousNounPhrase(lst, requiredNum, np)
506 {
507 return withGlobals(
508 {:action_.filterAmbiguousDobj(lst, requiredNum, np)});
509 }
510
511 /*
512 * Filter an ambiguous noun phrase list using the strength of
513 * possessive qualification, if any. If we have subsets at
514 * different possessive strengths, choose the strongest subset that
515 * has at least the required number of objects.
516 */
517 filterPossRank(lst, num)
518 {
519 local sub1 = lst.subset({x: x.possRank_ >= 1});
520 local sub2 = lst.subset({x: x.possRank_ >= 2});
521
522 /*
523 * sub2 is the subset with rank 2; if this meets our needs,
524 * return it. If sub2 doesn't meet our needs, then check to see
525 * if sub1 does; sub1 is the subset with rank 1 or higher. If
526 * neither subset meets our needs, use the original list.
527 */
528 if (sub2.length() >= num)
529 return sub2;
530 else if (sub1.length() >= num)
531 return sub1;
532 else
533 return lst;
534 }
535
536 /*
537 * Filter a list of ambiguous matches ('lst') for a noun phrase, to
538 * reduce each set of equivalent items to a single such item, if
539 * desired. If no equivalent reduction is desired for this type of
540 * resolver, this can simply return the original list.
541 *
542 * 'np' is the noun phrase production that we're resolving. This is
543 * usually a subclass of NounPhraseProd.
544 */
545 filterAmbiguousEquivalents(lst, np)
546 {
547 /* if we have only one item, there's obviously nothing redundant */
548 if (lst.length() == 1)
549 return lst;
550
551 /* scan the list, looking for equivalents */
552 for (local i = 1, local len = lst.length() ; i <= len ; ++i)
553 {
554 /*
555 * if this item is marked as equivalent, check for others
556 * like it
557 */
558 if (lst[i].obj_.isEquivalent)
559 {
560 /*
561 * If this object is in our list of previously-used
562 * equivalents, and we have more equivalents to this
563 * object in our list, then omit this one, so that we
564 * keep a different equivalent this time. This way, if
565 * we have a noun list such as "take coin and coin",
566 * we'll return different equivalent items for each
567 * equivalent noun phrase.
568 */
569 if (equivs_ != nil
570 && equivs_.indexOf(lst[i].obj_) != nil
571 && lst.lastIndexWhich(
572 {x: x.obj_.isVocabEquivalent(lst[i].obj_)}) > i)
573 {
574 /*
575 * we've already returned this one, and we have
576 * another equivalent later in the list that we can
577 * use instead this time - remove this one from the
578 * list
579 */
580 lst = lst.removeElementAt(i);
581
582 /* adjust the our counters for the removal */
583 --len;
584 --i;
585 }
586 else
587 {
588 /*
589 * We've decided to keep this element, either
590 * because we haven't already returned it as a match
591 * for this noun phrase, or because it's the last
592 * one of its kind. Add it to the list of
593 * equivalents we've previously returned.
594 */
595 if (equivs_ == nil)
596 equivs_ = new Vector(10);
597 equivs_.append(lst[i].obj_);
598
599 /*
600 * check each object at a higher index to see if
601 * it's equivalent to this one
602 */
603 for (local j = i + 1 ; j <= len ; ++j)
604 {
605 /* check this object */
606 if (lst[i].obj_.isVocabEquivalent(lst[j].obj_))
607 {
608 /* they match - remove the other one */
609 lst = lst.removeElementAt(j);
610
611 /* reduce the list length accordingly */
612 --len;
613
614 /* back up our scanning index as well */
615 --j;
616 }
617 }
618 }
619 }
620 }
621
622 /* return the updated list */
623 return lst;
624 }
625
626 /*
627 * Filter a plural phrase to reduce the set to the logical subset, if
628 * possible. If there is no logical subset, simply return the
629 * original set.
630 *
631 * 'np' is the noun phrase we're resolving; this is usually a
632 * subclass of PluralProd.
633 */
634 filterPluralPhrase(lst, np)
635 {
636 return withGlobals({:action_.filterPluralDobj(lst, np)});
637 }
638
639 /*
640 * Select a resolution for an indefinite noun phrase ("a coin"),
641 * given a list of possible matches. The matches will be given to
642 * us sorted from most likely to least likely, as done by
643 * filterAmbiguousNounPhrase().
644 *
645 * By default, we simply select the first 'n' items from the list
646 * (which are the most likely matches), because in most contexts, an
647 * indefinite noun phrase means that we should arbitrarily select
648 * any matching object. This can be overridden for contexts in
649 * which indefinite noun phrases must be handled differently.
650 */
651 selectIndefinite(results, lst, requiredNumber)
652 {
653 /*
654 * arbitrarily choose the first 'requiredNumber' item(s) from
655 * the list
656 */
657 return lst.sublist(1, requiredNumber);
658 }
659
660 /*
661 * Get the default object or objects for this phrase. Returns a list
662 * of ResolveInfo objects if a default is available, or nil if no
663 * default is available. This routine does not interact with the
664 * user; it should merely determine if the command implies a default
665 * strongly enough to assume it without asking the user.
666 *
667 * By default, we ask the action for a default direct object.
668 * Resolver subclasses should override this as appropriate for the
669 * specific objects they're used to resolve.
670 */
671 getDefaultObject(np)
672 {
673 /* ask the action to provide a default direct object */
674 return withGlobals({:action_.getDefaultDobj(np, self)});
675 }
676
677 /*
678 * Resolve a noun phrase involving unknown words, if possible. If
679 * it is not possible to resolve such a phrase, return nil;
680 * otherwise, return a list of resolved objects. This routine does
681 * not interact with the user - "oops" prompting is handled
682 * separately.
683 *
684 * 'tokList' is the token list for the phrase, in the canonical
685 * format as returned from the tokenizer. Each element of 'tokList'
686 * is a sublist representing one token.
687 *
688 * Note that this routine allows for specialized unknown word
689 * resolution separately from the more general matchName mechanism.
690 * The purpose of this method is to allow the specific type of
691 * resolver to deal with unknown words specially, rather than using
692 * the matchName mechanism. This routine is called as a last
693 * resort, only after the matchName mechanism fails to find any
694 * matches.
695 */
696 resolveUnknownNounPhrase(tokList)
697 {
698 /* by default, we can't resolve an unknown noun phrase */
699 return nil;
700 }
701
702 /*
703 * Execute a callback function in the global context of our actor
704 * and action - we'll set gActor and gAction to our own stored actor
705 * and action values, then call the callback, then restore the old
706 * globals.
707 */
708 withGlobals(func)
709 {
710 /* invoke the function with our action and actor in the globals */
711 return withParserGlobals(issuer_, actor_, action_, func);
712 }
713
714 /* the role played by this object, if any */
715 whichObject = DirectObject
716
717 /*
718 * Get an indication of which object we're resolving, for message
719 * generation purposes. By default, we'll indicate direct object;
720 * this should be overridden for resolvers of indirect and other
721 * types of objects.
722 */
723 whichMessageObject = DirectObject
724
725 /*
726 * The cached scope list, if we have one. Note that this is an
727 * internal implementation detail of the base class; subclasses can
728 * dispense with the cached scope list if they define their own
729 * objInScope() and getScopeList() overrides.
730 *
731 * Note that any subclasses (including Actions) that make changes to
732 * this list MUST ensure that the result only contains unique
733 * entries. The library assumes in several places that there are no
734 * duplicate entries in the list; subtle problems can occur if the
735 * list contains any duplicates.
736 */
737 scope_ = []
738
739 /* my action */
740 action_ = nil
741
742 /* the issuing actor */
743 issuer_ = nil
744
745 /* the target actor object */
746 actor_ = nil
747
748 /*
749 * List of equivalent objects we've resolved so far. We use this to
750 * try to return different equivalent objects when multiple noun
751 * phrases refer to the same set of equivalents.
752 */
753 equivs_ = nil
754 ;
755
756 /* ------------------------------------------------------------------------ */
757 /*
758 * Proxy Resolver - this is used to create resolvers that refer methods
759 * not otherwise overridden back to an underlying resolver
760 */
761 class ProxyResolver: object
762 construct(origResolver)
763 {
764 /* remember my underlying resolver */
765 self.origResolver = origResolver;
766 }
767
768 /* delegate methods we don't override to the underlying resolver */
769 propNotDefined(prop, [args])
770 {
771 /* delegate the call to the original resolver */
772 return origResolver.(prop)(args...);
773 }
774
775 /* base our possessive resolver on the proxy */
776 getPossessiveResolver() { return new PossessiveResolver(self); }
777 ;
778
779 /* ------------------------------------------------------------------------ */
780 /*
781 * Basic resolver for indirect objects
782 */
783 class IobjResolver: Resolver
784 /*
785 * we resolve indirect objects for message generation purposes
786 */
787 whichObject = IndirectObject
788 whichMessageObject = IndirectObject
789
790 /* resolve 'all' for the indirect object */
791 getAll(np)
792 {
793 /*
794 * ask the action to resolve 'all' for the indirect object, and
795 * then filter the list and return the result
796 */
797 return filterAll(action_.getAllIobj(actor_, getScopeList()),
798 IndirectObject, np);
799 }
800
801 /* get all possible default objects */
802 getAllDefaults()
803 {
804 /* ask the action to resolve 'all' for the indirect object */
805 local lst = action_.getAllIobj(actor_, getScopeList());
806
807 /* return the results as ResolveInfo objects */
808 return lst.mapAll({x: new ResolveInfo(x, 0, nil)});
809 }
810
811 /* filter an ambiguous noun phrase */
812 filterAmbiguousNounPhrase(lst, requiredNum, np)
813 {
814 return withGlobals(
815 {:action_.filterAmbiguousIobj(lst, requiredNum, np)});
816 }
817
818 /*
819 * Filter a plural phrase to reduce the set to the logical subset,
820 * if possible. If there is no logical subset, simply return the
821 * original set.
822 */
823 filterPluralPhrase(lst, np)
824 {
825 return withGlobals({:action_.filterPluralIobj(lst, np)});
826 }
827
828 /*
829 * Get the default object or objects for this phrase. Since we
830 * resolve indirect objects, we'll ask the action for a default
831 * indirect object.
832 */
833 getDefaultObject(np)
834 {
835 /* ask the action to provide a default indirect object */
836 return withGlobals({:action_.getDefaultIobj(np, self)});
837 }
838 ;
839
840 /* ------------------------------------------------------------------------ */
841 /*
842 * Basic topic qualifier resolver. This can be used to resolve qualifier
843 * phrases (such as possessives or locationals) within topic phrases.
844 */
845 class TopicQualifierResolver: Resolver
846 getAll(np)
847 {
848 /* 'all' doesn't make sense as a qualifier; return an empty list */
849 return [];
850 }
851
852 getAllDefaults()
853 {
854 /* we don't need defaults for a qualifier */
855 return [];
856 }
857
858 filterAmbiguousNounPhrase(lst, requiredNum, np)
859 {
860 /* we have no basis for any filtering; return the list unchanged */
861 return lst;
862 }
863
864 filterPluralPhrase(lst, np)
865 {
866 /* we have no basis for any filtering */
867 return lst;
868 }
869
870 getDefaultObject(np)
871 {
872 /* have have no way to pick a default */
873 return nil;
874 }
875 ;
876
877 /* ------------------------------------------------------------------------ */
878 /*
879 * Actor Resolver. We use this to resolve the actor to whom a command
880 * is directed: the actor must be in scope for the player character.
881 */
882 class ActorResolver: Resolver
883 construct(issuingActor)
884 {
885 /* remember the issuing actor */
886 actor_ = issuingActor;
887
888 /*
889 * Use our pseudo-action for "command actor" - this represents
890 * the intermediate step where the issuing actor is doing
891 * whatever physical activity is needed (such as talking) to give
892 * the command to the target actor. This isn't a real action;
893 * it's just an implied intermediate step in the overall action.
894 * We need this mostly because there are assumptions elsewhere in
895 * the resolution process that there's a valid Action object
896 * available.
897 */
898 action_ = CommandActorAction;
899
900 /* ...and the action needs an actor */
901 action_.actor_ = actor_;
902
903 /* cache the scope list for the actor who issued the command */
904 cacheScopeList();
905 }
906
907 /*
908 * Get the "all" list - this is the list of objects that we should
909 * use when the object of the command is the special word "all". By
910 * default, we'll return everything in scope.
911 */
912 getAll(np)
913 {
914 /* we can't address 'all' */
915 throw new ParseFailureException(&cannotAddressMultiple);
916 }
917
918 /* get the default object list */
919 getAllDefaults()
920 {
921 /* there are no default actors */
922 return [];
923 }
924
925 /*
926 * Filter an ambiguous list of objects. We will filter according to
927 * which objects are most logical as targets of commands.
928 */
929 filterAmbiguousNounPhrase(lst, requiredNum, np)
930 {
931 local likelyCnt;
932
933 /* give each object in the list a chance to filter the list */
934 lst = getAction().finishResolveList(lst, ActorObject,
935 np, requiredNum);
936
937 /*
938 * Run through the list and see how many objects are likely
939 * command targets.
940 */
941 likelyCnt = 0;
942 foreach (local cur in lst)
943 {
944 /* if it's a likely command target, count it */
945 if (cur.obj_.isLikelyCommandTarget)
946 ++likelyCnt;
947 }
948
949 /*
950 * If some of the targets are likely and others aren't, and we
951 * have at least the required number of likely targets, keep
952 * only the likely ones. If they're all likely or all unlikely,
953 * it doesn't help us because we still have no basis for
954 * choosing some over others; if removing unlikely ones would
955 * not give us enough to meet the minimum number required it
956 * also doesn't help, because we don't have a basis for
957 * selecting as many as are needed.
958 */
959 if (likelyCnt != 0 && likelyCnt != lst.length()
960 && likelyCnt >= requiredNum)
961 {
962 /*
963 * we have a useful subset of likely ones - filter the list
964 * down to the likely subset
965 */
966 lst = lst.subset({cur: cur.obj_.isLikelyCommandTarget});
967 }
968
969 /* return the result */
970 return lst;
971 }
972
973 /*
974 * Filter a plural list
975 */
976 filterPluralPhrase(lst, np)
977 {
978 /*
979 * Use the same filtering that we use for ambiguous nouns. This
980 * simply reduces the set to the likely command targets if any
981 * are likely command targets.
982 */
983 return filterAmbiguousNounPhrase(lst, 1, np);
984 }
985
986 /* get a default object */
987 getDefaultObject(np)
988 {
989 /* there is never a default for the target actor */
990 return nil;
991 }
992
993 /* resolve a noun phrase involving unknown words */
994 resolveUnknownNounPhrase(tokList)
995 {
996 /* we can't resolve an unknown noun phrase used as an actor target */
997 return nil;
998 }
999
1000 /*
1001 * Get a raw pronoun antecedent list. Since we are resolving the
1002 * target actor, pronouns are relative to the issuing actor.
1003 */
1004 getRawPronounAntecedent(typ)
1005 {
1006 /* check for pronouns that are relative to the issuer */
1007 switch(typ)
1008 {
1009 case PronounMe:
1010 /*
1011 * It's a first-person construction. If the issuing actor
1012 * is the player character, and the PC is in the second
1013 * person, this refers to the player character (the game
1014 * calls the PC "you", so the player calls the PC "me"). If
1015 * the issuing actor is an NPC, this is unconditionally the
1016 * PC.
1017 */
1018 if (actor_.isPlayerChar && actor_.referralPerson != SecondPerson)
1019 return [];
1020 else
1021 return [actor_];
1022
1023 case PronounYou:
1024 /*
1025 * It's a second-person construction. If the issuer is the
1026 * player character, and the player character is in the
1027 * first person, this refers to the player character (the
1028 * game calls the PC "me", so the player calls the PC
1029 * "you"). If the issuer isn't the player character, "you"
1030 * has no meaning.
1031 */
1032 if (!actor_.isPlayerChar || actor_.referralPerson != FirstPerson)
1033 return [];
1034 else
1035 return [actor_];
1036
1037 default:
1038 /*
1039 * it's not a relative pronoun, so ask the issuing actor for
1040 * the antecedent based on recent commands
1041 */
1042 return actor_.getPronounAntecedent(typ);
1043 }
1044 }
1045
1046 /* we resolve target actors */
1047 whichObject = ActorObject
1048 whichMessageObject = ActorObject
1049 ;
1050
1051 /*
1052 * A pseudo-action for "command actor." This represents the act of one
1053 * actor (usually the PC) giving a command to another, as in "BOB, GO
1054 * NORTH". This isn't a real action that the player can type; it's just
1055 * an internal construct that we use to represent the partially resolved
1056 * action, when we know that we're addressing another actor but we're
1057 * still working on figuring out what we're saying.
1058 */
1059 class CommandActorAction: Action
1060 ;
1061
1062 /* ------------------------------------------------------------------------ */
1063 /*
1064 * A possessive resolver is a proxy to a main resolver that considers an
1065 * object in scope if (a) it's in scope in the base resolver, or (b) the
1066 * object is known to the actor.
1067 */
1068 class PossessiveResolver: ProxyResolver
1069 objInScope(obj)
1070 {
1071 /*
1072 * An object is in scope for the purposes of a possessive phrase
1073 * if it's in scope in the base resolver, or it's known to the
1074 * actor. An object is only in scope for a possessive qualifier
1075 * phrase if its canResolvePossessive property is true.
1076 */
1077 return (obj.canResolvePossessive
1078 && (origResolver.objInScope(obj)
1079 || origResolver.getTargetActor().knowsAbout(obj)));
1080 }
1081
1082 /* this is a sub-resolver */
1083 isSubResolver = true
1084 ;
1085