Debian Codegraph @

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

raw
1 /* Copyright (c) 2006 by Michael J. Roberts. All Rights Reserved. */
2 /*
3 * It's often handy for the player to be able to refer to objects by
4 * their current open/closed states ("go through the open door" or "look
5 * in the closed jar"). This is a small extension that adds "open" and
6 * "closed" vocabulary automatically to Openable objects, according to
7 * their current states. It also lets the disambiguation system
8 * distinguish objects according to their open/closed state, so that the
9 * parser can ask for help accordingly ("Which do you mean, the open jar,
10 * or one of the closed jars?").
11 *
12 * The author hereby grants permission to anyone to use this software for
13 * any purpose and without fee, under the condition that anyone using
14 * this software assumes all liability for his or her use of the
15 * software. NO WARRANTY: The author specifically disclaims all
16 * warranties with respect to this software, including all implied
17 * warranties of merchantability and fitness.
18 */
19
20 #include <adv3.h>
21 #include <en_us/en_us.h>
22
23 /*
24 * State objects for 'open' and 'closed' states
25 */
26 openState: ThingState
27 stateTokens = ['open']
28 ;
29 closedState: ThingState
30 stateTokens = ['closed']
31 ;
32
33 /*
34 * A disambiguation distinguisher for open/closed state. This can tell
35 * apart two objects if they're both openable, and their open/closed
36 * states differ.
37 *
38 * This allows the parser to ask disambiguation questions based on the
39 * open/closed state when it's the only thing that distinguishes the
40 * relevant objects. "Which do you mean, the open jar, or one of the
41 * closed jars?"
42 */
43 openClosedDistinguisher: Distinguisher
44 canDistinguish(a, b)
45 {
46 return a.ofKind(Openable) && b.ofKind(Openable)
47 && a.isOpen != b.isOpen;
48 }
49
50 name(obj) { return obj.nameOpenClosed; }
51 aName(obj) { return obj.aNameOpenClosed; }
52 theName(obj) { return obj.theNameOpenClosed; }
53 countName(obj, cnt) { return obj.pluralNameOpenClosed; }
54 ;
55
56 /*
57 * In the basic openable class, add handling for state-dependent 'open'
58 * and 'closed' adjectives, and use the current state as a basis for
59 * disambiguation (via a Distinguisher).
60 *
61 * Note that we take advantage of the ThingState mechanism to help with
62 * the name matching, but we don't use the mechanism fully. In
63 * particular, we never report openState or closedState in getState() or
64 * allStates(). The reason we don't use the full mechanism is that adv3
65 * currently only allows an object to have a single active state, and we
66 * don't want to rule out other states by using up the whole single
67 * active state slot for the open/closed status. Hopefully this
68 * restriction will be lifted in the future, in which case we'll be able
69 * to simplify this by simply adding openState or closedState to the
70 * current state list, and then fall back on the standard
71 * matchNameCommon() handling.
72 */
73 modify BasicOpenable
74 /* add the adjectives */
75 adjective = 'open' 'closed'
76
77 /* ...but only accept the one that applies to our current state */
78 matchNameCommon(origTokens, adjustedTokens)
79 {
80 /*
81 * check with our appropriate state object - if that fails, the
82 * whole match fails
83 */
84 local st = (isOpen ? openState : closedState);
85 if (st.matchName(self, origTokens, adjustedTokens,
86 [openState, closedState]) == nil)
87 return nil;
88
89 /* inherit the base handling */
90 return inherited(origTokens, adjustedTokens);
91 }
92
93 /* use the open/closed state as a basis for disambiguation */
94 distinguishers = (nilToList(inherited) + openClosedDistinguisher)
95
96 /* open/closed names */
97 nameOpenClosed = ((isOpen ? 'open ' : 'closed ') + name)
98 theNameOpenClosed = ((isOpen ? 'the open ' : 'the closed ') + name)
99 pluralNameOpenClosed = ((isOpen ? 'open ' : 'closed ') + name)
100 aNameOpenClosed()
101 {
102 if (isPlural || isMassNoun)
103 return (isOpen ? 'open ' : 'closed ') + name;
104 else
105 return (isOpen ? 'an open ' : 'a closed ') + name;
106 }
107 ;
108