Debian Codegraph @

terps/tads/tads3/lib/extensions/newNames.t

raw
1 /*
2 * Copyright (c) 2000, 2007 by Michael J. Roberts. All Rights Reserved.
3 *
4 * NOTE: THIS FILE IS OBSOLETE! As of TADS 3.0.19, everything in this
5 * extension is incorporated directly into the standard adv3 library, so
6 * there's no need to add it separately to your project. You should
7 * remove it from your project's list of source files. This file is now
8 * included in the distribution for historical reference only.
9 *
10 *
11 * TADS 3 Library Extension - New Names
12 *
13 * This is an experimental extension that enhances the new
14 * detailed-naming feature that was first introduced in 3.0.10. This
15 * feature has been turned off by default, because it's somewhat
16 * incomplete and quirky as presently implemented. This extension is
17 * essentially a second version of the feature that attempts to reduce
18 * the quirks and improve the feature's performance.
19 *
20 * The point of the detailed-naming feature is to generate differentiated
21 * names in object announcements: default object messages, which tell you
22 * which object the parser has selected by default when you leave a
23 * needed object out of a command ("UNLOCK DOOR / (with the gold key)");
24 * multi-object prefixes, which tell you which of the several objects in
25 * your command the parser is working on now ("TAKE GOLD KEY AND SILVER
26 * KEY / gold key: Taken / silver key: Taken"); and ambiguous selection
27 * messages, which ("TAKE COIN / (the silver coin)"). By
28 * "differentiated" names, we mean names that distinguish the object in
29 * question from other similarly named objects that are part of the same
30 * command or are also in scope. For example, if there are several
31 * objects present that are all called "gold coin," the detailed naming
32 * system might call one of them "your gold coin" and the other "the gold
33 * coin on the floor," so that you can tell specifically which one the
34 * parser is talking about at any given time.
35 *
36 * The original scheme basically accomplished this, but it had some
37 * problems: it was a little *too* specific at times, and it wasn't as
38 * consistent as one would like. The problem with being too specific is
39 * that it's unnatural; in natural speech, people are really good at
40 * using just enough specificity for the context. When science fiction
41 * writers want to emphasize the alienness of an android or a Vulcan,
42 * they have the character refer to everyday objects in comically
43 * detailed, technical language; the original detailed-naming scheme had
44 * this sort of effect. So, the main improvement this extension tries to
45 * make is to find a better balance, to use enough specificity to
46 * distinguish nearby objects, but not so much that the parser sounds
47 * like an android caricature.
48 *
49 * The specific changes:
50 *
51 * 1. For multi-object announcements, the naming now only attempts to
52 * differentiate the listed objects from *each other*, NOT from
53 * everything else in scope.
54 *
55 * 2. For multi-object announcements, the basis for the differentiation
56 * (location, ownership, etc) is determined *in advance* for all of the
57 * objects, before the command is executed on any of the objects. In the
58 * original implementation, the differentiation was figured for each
59 * object on the fly, so if the command being executed affected the
60 * object state, the objects involved could become distinguishable during
61 * the command even though they weren't initially. From the player's
62 * perspective, they're entering the command with respect to the state of
63 * the world at the start of the command, so it's more consistent with
64 * the player's expectations to determine the naming at that point in
65 * time. This approach also results in more consistent naming overall,
66 * since the game-state basis for the naming is the same for the whole
67 * list.
68 *
69 * 3. When objects are distinguishable by their base names, the base
70 * names are used. The original implementation favored the disambigName
71 * even if the base names were distinguishable. This was undesirable
72 * because the disambigName is essentially designed to globally
73 * distinguish an object, so it tends to be very specific; when we only
74 * need to distinguish an object from other things in scope, we usually
75 * don't need such a specific name.
76 */
77
78 #include <adv3.h>
79 #include <en_us.h>
80
81 modify ResolveInfo
82 /*
83 * The pre-calculated multi-object announcement text for this object.
84 * When we iterate over the object list in a command with multiple
85 * direct or indirect objects (TAKE THE BOOK, BELL, AND CANDLE), we
86 * calculate the little announcement messages ("book:") for the
87 * objects BEFORE we execute the actual commands. We then use the
88 * pre-calculated announcements during our iteration. This ensures
89 * consistency in the basis for choosing the names, which is
90 * important in cases where the names include state-dependent
91 * information for the purposes of distinguishing one object from
92 * another. The relevant state can change over the course of
93 * executing the command on the objects in the iteration, so if we
94 * calculated the names on the fly we could end up with inconsistent
95 * naming. The user thinks of the objects in terms of their state at
96 * the start of the command, so the pre-calculation approach is not
97 * only more internally consistent, but is also more consistent with
98 * the user's perspective.
99 */
100 multiAnnounce = nil
101 ;
102
103 modify Thing
104 /*
105 * Get the distinguisher to use for printing this object's name in an
106 * action announcement (such as a multi-object, default object, or
107 * vague-match announcement). We check the global option setting to
108 * see if we should actually use distinguishers for this; if so, we
109 * call getInScopeDistinguisher() to find the correct distinguisher,
110 * otherwise we use the "null" distinguisher, which simply lists
111 * objects by their base names.
112 *
113 * 'lst' is the list of other objects from which we're trying to
114 * differentiate 'self'. The reason 'lst' is given is that it lets
115 * us choose the simplest name for each object that usefully
116 * distinguishes it; to do this, we need to know exactly what we're
117 * distinguishing it from.
118 */
119 replace getAnnouncementDistinguisher(lst)
120 {
121 return (gameMain.useDistinguishersInAnnouncements
122 ? getBestDistinguisher(lst)
123 : nullDistinguisher);
124 }
125
126 /*
127 * Get a distinguisher that differentiates me from all of the other
128 * objects in scope, if possible, or at least from some of the other
129 * objects in scope.
130 */
131 replace getInScopeDistinguisher()
132 {
133 /* return the best distinguisher for the objects in scope */
134 return getBestDistinguisher(gActor.scopeList());
135 }
136
137 /*
138 * Get a distinguisher that differentiates me from all of the other
139 * objects in the given list, if possible, or from as many of the
140 * other objects as possible.
141 */
142 getBestDistinguisher(lst)
143 {
144 local bestDist, bestCnt;
145
146 /* remove 'self' from the list */
147 lst -= self;
148
149 /*
150 * Try the "null" distinguisher, which distinguishes objects
151 * simply by their base names - if that can tell us apart from
152 * the other objects, there's no need for anything more
153 * elaborate. So, calculate the list of indistinguishable
154 * objects using the null distinguisher, and if it's empty, we
155 * can just use the null distinguisher as our result.
156 */
157 if (lst.subset({obj: !nullDistinguisher.canDistinguish(self, obj)})
158 .length() == 0)
159 return nullDistinguisher;
160
161 /*
162 * Reduce the list to the set of objects that are basic
163 * equivalents of mine - these are the only ones we care about
164 * telling apart from us, since all of the other objects can be
165 * distinguished by their disambig name.
166 */
167 lst = lst.subset(
168 {obj: !basicDistinguisher.canDistinguish(self, obj)});
169
170 /* if the basic distinguisher can tell us apart, just use it */
171 if (lst.length() == 0)
172 return basicDistinguisher;
173
174 /* as a last resort, fall back on the basic distinguisher */
175 bestDist = basicDistinguisher;
176 bestCnt = lst.countWhich({obj: bestDist.canDistinguish(self, obj)});
177
178 /*
179 * run through my distinguisher list looking for the
180 * distinguisher that can tell me apart from the largest number
181 * of my vocab equivalents
182 */
183 foreach (local dist in distinguishers)
184 {
185 /* if this is the default, skip it */
186 if (dist == bestDist)
187 continue;
188
189 /* check to see how many objects 'dist' can distinguish me from */
190 local cnt = lst.countWhich({obj: dist.canDistinguish(self, obj)});
191
192 /*
193 * if it can distinguish me from every other object, use this
194 * one - it uniquely identifies me
195 */
196 if (cnt == lst.length())
197 return dist;
198
199 /*
200 * that can't distinguish us from everything else here, but
201 * if it's the best so far, remember it; we'll fall back on
202 * the best that we find if we fail to find a perfect
203 * distinguisher
204 */
205 if (cnt > bestCnt)
206 {
207 bestDist = dist;
208 bestCnt = cnt;
209 }
210 }
211
212 /*
213 * We didn't find any distinguishers that can tell me apart from
214 * every other object, so choose the one that can tell me apart
215 * from the most other objects.
216 */
217 return bestDist;
218 }
219 ;
220
221 modify Action
222 /* announce a multi-action object, if appropriate */
223 maybeAnnounceMultiObject(info, numberInList, whichObj)
224 {
225 /*
226 * announce if we have more than one object, or we're set to
227 * announce this object in any case
228 */
229 if (numberInList > 1 || (info.flags_ & AlwaysAnnounce) != 0)
230 {
231 /* show the current object of a multi-object action */
232 gTranscript.announceMultiActionObject(
233 info.multiAnnounce, info.obj_, whichObj);
234
235 /* tell the caller we made an announcement */
236 return true;
237 }
238
239 /* tell the caller we didn't make an announcement */
240 return nil;
241 }
242
243 /*
244 * Pre-calculate the multi-object announcement text for each object.
245 * This is important because these announcements might choose a form
246 * for the name that distinguishes it from the other objects in the
247 * iteration, and the basis for distinction might be state-dependent
248 * (such as the object's current owner or location), and the relevant
249 * state might change as we iterate over the objects. From the
250 * user's perspective, they're referring to the objects based on the
251 * state at the start of the command, so the user will expect to see
252 * names based on the that state.
253 */
254 cacheMultiObjectAnnouncements(lst, whichObj)
255 {
256 /* run through the list and cache each object's announcement */
257 foreach (local cur in lst)
258 {
259 /* calculate and cache this object's multi-object announcement */
260 cur.multiAnnounce = libMessages.announceMultiActionObject(
261 cur.obj_, whichObj, self);
262 }
263 }
264
265 /* get the list of resolved objects in the given role */
266 getResolvedObjList(which)
267 {
268 /*
269 * the base action doesn't have any objects in any roles, so just
270 * return nil; subclasses need to override this
271 */
272 return nil;
273 }
274 ;
275
276 modify TAction
277 /* get the list of resolved objects in the given role */
278 getResolvedObjList(which)
279 {
280 return (which == DirectObject ? getResolvedDobjList()
281 : inherited(which));
282 }
283
284 /*
285 * Execute the action. We'll run through the execution sequence
286 * once for each resolved direct object.
287 */
288 replace doActionMain()
289 {
290 /*
291 * Set the direct object list as the antecedent, using the
292 * language-specific pronoun setter. Don't set pronouns for a
293 * nested command, because the player didn't necessarily refer to
294 * the objects in a nested command.
295 */
296 if (parentAction == nil)
297 gActor.setPronoun(dobjList_);
298
299 /* we haven't yet canceled the iteration */
300 iterationCanceled = nil;
301
302 /* pre-calculate the multi-object announcement text for each dobj */
303 cacheMultiObjectAnnouncements(dobjList_, DirectObject);
304
305 /* run through the sequence once for each direct object */
306 for (local i = 1, local len = dobjList_.length() ;
307 i <= len && !iterationCanceled ; ++i)
308 {
309 /* make this object our current direct object */
310 dobjCur_ = dobjList_[i].obj_;
311 dobjInfoCur_ = dobjList_[i];
312
313 /* announce the object if appropriate */
314 announceActionObject(dobjList_[i], len, whichMessageObject);
315
316 /* run the execution sequence for the current direct object */
317 doActionOnce();
318
319 /* if we're top-level, count the iteration in the transcript */
320 if (parentAction == nil)
321 gTranscript.newIter();
322 }
323 }
324
325 /* announce a default object used with this action */
326 announceDefaultObject(obj, whichObj, resolvedAllObjects)
327 {
328 local prep;
329 local nm;
330
331 /*
332 * Get the name to display. Since we're selecting an object
333 * automatically from everything in scope, we need to
334 * differentiate the object from the other objects in scope.
335 */
336 nm = obj.getAnnouncementDistinguisher(gActor.scopeList())
337 .theName(obj);
338
339 /*
340 * get any direct object preposition - this is the part inside
341 * the "(what)" specifier parens, excluding the last word
342 */
343 rexSearch('<lparen>(.*<space>+)?<alpha>+<rparen>', verbPhrase);
344 prep = (rexGroup(1) == nil ? '' : rexGroup(1)[3]);
345
346 /* do any verb-specific adjustment of the preposition */
347 if (prep != nil)
348 prep = adjustDefaultObjectPrep(prep, obj);
349
350 /* show the preposition (if any) and the object */
351 return (prep == '' ? nm : prep + nm);
352 }
353 ;
354
355 modify TIAction
356 /* get the list of resolved objects in the given role */
357 getResolvedObjList(which)
358 {
359 return (which == IndirectObject ? getResolvedIobjList()
360 : inherited(which));
361 }
362
363 /*
364 * Execute the action. We'll run through the execution sequence
365 * once for each resolved object in our direct or indirect object
366 * list, depending on which one is the list and which one is the
367 * singleton.
368 */
369 doActionMain()
370 {
371 local lst;
372 local preAnnouncedDobj;
373 local preAnnouncedIobj;
374
375 /*
376 * Get the list of resolved objects for the multiple object. If
377 * neither has multiple objects, it doesn't matter which is
378 * iterated, since we'll just do the command once anyway.
379 */
380 lst = (iobjList_.length() > 1 ? iobjList_ : dobjList_);
381
382 /*
383 * Set the pronoun antecedents, using the game-specific pronoun
384 * setter. Don't set an antecedent for a nested command.
385 */
386 if (parentAction == nil)
387 {
388 /*
389 * Set both direct and indirect objects as potential
390 * antecedents. Rather than trying to figure out right now
391 * which one we might want to refer to in the future, remember
392 * both - we'll decide which one is the logical antecedent
393 * when we find a pronoun to resolve in a future command.
394 */
395 gActor.setPronounMulti(dobjList_, iobjList_);
396
397 /*
398 * If one or the other object phrase was specified in the
399 * input as a pronoun, keep the meaning of that pronoun the
400 * same, overriding whatever we just did. Note that the
401 * order we use here doesn't matter: if a given pronoun
402 * appears in only one of the two lists, then the list where
403 * it's not set has no effect on the pronoun, hence it
404 * doesn't matter which comes first; if a pronoun appears in
405 * both lists, it will have the same value in both lists, so
406 * we'll just do the same thing twice, so, again, order
407 * doesn't matter.
408 */
409 setPronounByInput(dobjList_);
410 setPronounByInput(iobjList_);
411 }
412
413 /*
414 * pre-announce the non-list object if appropriate - this will
415 * provide a common pre-announcement if we iterate through
416 * several announcements of the main list objects
417 */
418 if (lst == dobjList_)
419 {
420 /* pre-announce the single indirect object if needed */
421 preAnnouncedIobj = preAnnounceActionObject(
422 iobjList_[1], dobjList_, IndirectObject);
423
424 /* we haven't announced the direct object yet */
425 preAnnouncedDobj = nil;
426
427 /* pre-calculate the multi-object announcements */
428 cacheMultiObjectAnnouncements(dobjList_, DirectObject);
429 }
430 else
431 {
432 /* pre-announce the single direct object if needed */
433 preAnnouncedDobj = preAnnounceActionObject(
434 dobjList_[1], iobjList_, DirectObject);
435
436 /* we haven't announced the indirect object yet */
437 preAnnouncedIobj = nil;
438
439 /* pre-calculate the multi-object announcements */
440 cacheMultiObjectAnnouncements(iobjList_, IndirectObject);
441 }
442
443 /* we haven't yet canceled the iteration */
444 iterationCanceled = nil;
445
446 /* iterate over the resolved list for the multiple object */
447 for (local i = 1, local len = lst.length() ;
448 i <= len && !iterationCanceled ; ++i)
449 {
450 local dobjInfo;
451 local iobjInfo;
452
453 /*
454 * make the current list item the direct or indirect object,
455 * as appropriate
456 */
457 if (lst == dobjList_)
458 {
459 /* the direct object is the multiple object */
460 dobjInfo = dobjInfoCur_ = lst[i];
461 iobjInfo = iobjInfoCur_ = iobjList_[1];
462 }
463 else
464 {
465 /* the indirect object is the multiple object */
466 dobjInfo = dobjInfoCur_ = dobjList_[1];
467 iobjInfo = iobjInfoCur_ = lst[i];
468 }
469
470 /* get the current dobj and iobj from the resolve info */
471 dobjCur_ = dobjInfo.obj_;
472 iobjCur_ = iobjInfo.obj_;
473
474 /*
475 * if the action was remapped, and we need to announce
476 * anything, announce the entire action
477 */
478 if (isRemapped())
479 {
480 /*
481 * We were remapped. The entire phrasing of the new
482 * action might have changed from what the player typed,
483 * so it might be nonsensical to show the objects as we
484 * usually would, as sentence fragments that are meant
485 * to combine with what the player actually typed. So,
486 * instead of showing the usual sentence fragments, show
487 * the entire phrasing of the command.
488 *
489 * Only show the announcement if we have a reason to: we
490 * have unclear disambiguation in one of the objects, or
491 * one of the objects is defaulted.
492 *
493 * If we don't want to announce the remapped action,
494 * still consider showing a multi-object announcement,
495 * if we would normally need to do so.
496 */
497 if (needRemappedAnnouncement(dobjInfo)
498 || needRemappedAnnouncement(iobjInfo))
499 {
500 /* show the remapped announcement */
501 gTranscript.announceRemappedAction();
502 }
503 else
504 {
505 /* announce the multiple dobj if necessary */
506 if (!preAnnouncedDobj)
507 maybeAnnounceMultiObject(
508 dobjInfo, dobjList_.length(), DirectObject);
509
510 /* announce the multiple iobj if necessary */
511 if (!preAnnouncedIobj)
512 maybeAnnounceMultiObject(
513 iobjInfo, iobjList_.length(), IndirectObject);
514 }
515 }
516 else
517 {
518 /* announce the direct object if appropriate */
519 if (!preAnnouncedDobj)
520 announceActionObject(dobjInfo, dobjList_.length(),
521 DirectObject);
522
523 /* announce the indirect object if appropriate */
524 if (!preAnnouncedIobj)
525 announceActionObject(iobjInfo, iobjList_.length(),
526 IndirectObject);
527 }
528
529 /* run the execution sequence for the current direct object */
530 doActionOnce();
531
532 /* if we're top-level, count the iteration in the transcript */
533 if (parentAction == nil)
534 gTranscript.newIter();
535 }
536 }
537
538 /* announce a default object used with this action */
539 announceDefaultObject(obj, whichObj, resolvedAllObjects)
540 {
541 local verb;
542 local prep;
543 local nm;
544
545 /* presume we won't have a verb or preposition */
546 verb = '';
547 prep = '';
548
549 /*
550 * Check the full phrasing - if we're showing the direct object,
551 * but an indirect object was supplied, use the verb's
552 * participle form ("asking bob") in the default string, since
553 * we must clarify that we're not tagging the default string on
554 * to the command line. Don't include the participle form if we
555 * don't know all the objects yet, since in this case we are in
556 * fact tagging the default string onto the command so far, as
557 * there's nothing else in the command to get in the way.
558 */
559 if (whichObj == DirectObject && resolvedAllObjects)
560 {
561 /*
562 * extract the verb's participle form (including any
563 * complementizer phrase)
564 */
565 rexSearch('/(<^lparen>+) <lparen>', verbPhrase);
566 verb = rexGroup(1)[3] + ' ';
567 }
568
569 /* get the preposition to use, if any */
570 switch(whichObj)
571 {
572 case DirectObject:
573 /* use the preposition in the first "(what)" phrase */
574 rexSearch('<lparen>(.*?)<space>*<alpha>+<rparen>', verbPhrase);
575 prep = rexGroup(1)[3];
576 break;
577
578 case IndirectObject:
579 /* use the preposition in the second "(what)" phrase */
580 rexSearch('<rparen>.*<lparen>(.*?)<space>*<alpha>+<rparen>',
581 verbPhrase);
582 prep = rexGroup(1)[3];
583 break;
584 }
585
586 /*
587 * get the name to display - since we selected this object
588 * automatically from among the objects in scope, we need to
589 * differentiate it from the other objects in scope
590 */
591 nm = obj.getAnnouncementDistinguisher(gActor.scopeList())
592 .theName(obj);
593
594 /* build and return the complete phrase */
595 return spSuffix(verb) + spSuffix(prep) + nm;
596 }
597 ;
598
599 modify MultiObjectAnnouncement
600 construct(preCalcMsg, obj, whichObj, action)
601 {
602 /* do the inherited work */
603 inherited(obj, whichObj, action);
604
605 /*
606 * if we have a pre-calculated message, use it instead of the
607 * message we just generated - this lets the caller explicitly
608 * set the message as desired
609 */
610 if (preCalcMsg != nil)
611 messageText_ = preCalcMsg;
612 }
613 ;
614
615 modify CommandTranscript
616 /*
617 * Announce one of a set of objects to a multi-object action. We'll
618 * record this announcement for display with our report list.
619 */
620 replace announceMultiActionObject(preCalcMsg, obj, whichObj)
621 {
622 /* save a multi-action object announcement */
623 addReport(new MultiObjectAnnouncement(
624 preCalcMsg, obj, whichObj, gAction));
625 }
626 ;
627
628 modify libMessages
629 /*
630 * Announce the current object of a set of multiple objects on which
631 * we're performing an action. This is used to tell the player
632 * which object we're acting upon when we're iterating through a set
633 * of objects specified in a command targeting multiple objects.
634 */
635 announceMultiActionObject(obj, whichObj, action)
636 {
637 /*
638 * get the display name - we only need to differentiate this
639 * object from the other objects in the iteration
640 */
641 local nm = obj.getAnnouncementDistinguisher(
642 action.getResolvedObjList(whichObj)).name(obj);
643
644 /* build the announcement */
645 return '<./p0>\n<.announceObj>' + nm + ':<./announceObj> <.p0>';
646 }
647
648 /*
649 * Announce a singleton object that we selected from a set of
650 * ambiguous objects. This is used when we disambiguate a command
651 * and choose an object over other objects that are also logical but
652 * are less likely. In such cases, it's courteous to tell the
653 * player what we chose, because it's possible that the user meant
654 * one of the other logical objects - announcing this type of choice
655 * helps reduce confusion by making it immediately plain to the
656 * player when we make a choice other than what they were thinking.
657 */
658 announceAmbigActionObject(obj, whichObj, action)
659 {
660 /*
661 * get the display name - distinguish the object from everything
662 * else in scope, since we chose from a set of ambiguous options
663 */
664 local nm = obj.getAnnouncementDistinguisher(gActor.scopeList())
665 .theName(obj);
666
667 /* announce the object in "assume" style, ending with a newline */
668 return '<.assume>' + nm + '<./assume>\n';
669 }
670 ;
671