Debian Codegraph @

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

raw
1 #charset "us-ascii"
2
3 #include <adv3.h>
4 #include <en_us.h>
5
6 /*
7 * smartAccompany.t
8 *
9 * by Eric Eve
10 *
11 * A small extension that improves the ordering and grouping of reports
12 * from an NPC in an AccompanyingState following the player character.
13 * For a full explanation of what this extension does, and how it works,
14 * see Example 3 in the Tech Man article on Manipulating the Transcript.
15 *
16 * Version 1.3; 27-May-09
17 *
18 * Version 1.3 fixes a bug which causes the name of the following actor
19 * (and nothing else) to be displayed in a separate sentence when the
20 * actor follows the player character from a dark room.
21 *
22 * version 1.2; 04-May-09
23 *
24 * Version 1.2 fixes a bug that can cause a run-time error when a report
25 * in the transcript doesn't have a messageText_ property.
26 *
27 * Version 1.1 fixes a bug that could cause a run-time error when travel
28 * between rooms is disallowed.
29 *
30 *
31 */
32
33 ModuleID
34 name = 'smartAccompany'
35 byline = 'by Eric Eve'
36 htmlByline = 'by <a href="mailto:[email protected]">Eric Eve</a>'
37 version = '1.3'
38 listingOrder = 75
39 ;
40
41 /*
42 * Combine reports of the NPC's actions in the old location into a single
43 * sentence and move it to just before the new room description.
44 */
45
46 modify AccompanyingState
47 beforeTravel(traveler, connector)
48 {
49 gAction.callAfterActionMain(self);
50 inherited(traveler, connector);
51 }
52
53 afterActionMain()
54 {
55 /*
56 * Find the insertion point, which is just before the description
57 * of the new room.
58 */
59
60 local idx = gTranscript.reports_.indexWhich({ x: x.messageText_ ==
61 '<.roomname>' });
62
63 /*
64 * If there is no insertion point, travel failed for some reason,
65 * in which case we don't want to change the transcript at all.
66 */
67
68 if(idx == nil)
69 return;
70
71 local actor = getActor;
72 local str = '';
73 local vec = new Vector(4);
74 local pat = new RexPattern('<NoCase>^' + actor.theName + '<Space>+');
75
76 /*
77 * Look through all the reports in the transcript for those whose
78 * message text begins with the name of our actor, followed by at
79 * least one space (so that if the actor's name is Rob we don't
80 * pick up any messages relating to Roberta, for example),
81 * regardless of case (so we pick up messages about 'the tall man'
82 * and 'The tall man').
83 *
84 * Once we get to the description of a new room, stop looking (we
85 * don't want to include the description of the actor arriving in
86 * the new location, just the actor departing the old one).
87 *
88 * Store the relevant message strings (those before the new room
89 * description, but not any after it) in the vector vec; at the
90 * same time remove these reports from the transcript (since we'll
91 * be replacing them with a single report below).
92 */
93
94 while((idx = gTranscript.reports_.indexWhich({ x: x.messageText_ != nil
95 && rexMatch(pat, x.messageText_) })) != nil)
96 {
97 if(idx > gTranscript.reports_.indexWhich({ x: x.messageText_ ==
98 '<.roomname>' }))
99 break;
100
101
102 vec.append(gTranscript.reports_[idx].messageText_);
103 gTranscript.reports_.removeElementAt(idx);
104 }
105
106 local len = actor.theName.length() + 1;
107
108 /*
109 * Now go through each of the strings in vec in turn, stripping
110 * off the actor's name at the start and the period/full-stop at
111 * the end (so that, for example 'Bob stands up. ' becomes 'stands
112 * up'. In searching for the position of the terminating full
113 * stop (period) we start beyond the end of the actor's name in
114 * case the actor's name contains a full stop (e.g. Prof. Smith).
115 */
116
117 vec.applyAll( { cur: cur.substr(len, cur.find('.', len) - len)});
118
119 /*
120 * Concatanate these truncated action reports into a single string
121 * listing the actor's actions (e.g. 'stands up and comes with
122 * you'), separating the final pair of actions with 'and' and any
123 * previous actions with a comma. We can use stringLister (defined
124 * in the previous example) to do this for us.
125 */
126
127 str = stringLister.makeSimpleList(vec.toList);
128
129 /*
130 * If we're left with an empty string, there's nothing left to do,
131 * so we should stop here rather than go on to display a
132 * fragmentary sentence.
133 */
134
135 if(str == '')
136 return;
137
138 /*
139 * Put the actor's name back at the start of the string, and
140 * conclude the string with a full-stop and a paragraph break.
141 */
142 str = '\^' + actor.theName + ' ' + str + '.<.p>';
143
144 /*
145 * Find the insertion point, which is just before the description
146 * of the new room. This may have changed as a result of our
147 * manipulations above.
148 */
149
150 idx = gTranscript.reports_.indexWhich({ x: x.messageText_ ==
151 '<.roomname>' });
152
153 /*
154 * Insert the message we just created as a new report at the
155 * appropriate place, i.e. just before the new room description.
156 */
157 gTranscript.reports_.insertAt(idx, new MessageResult(str));
158
159 }
160 ;
161
162 /*
163 * The modifications on AccompanyingState probably aren't so suitable for a
164 * GuidedTourState, so we make afterActionMain() do nothing by default. This
165 * can be overridden by game authors if desired.
166 */
167
168 modify GuidedTourState
169 afterActionMain() { }
170 ;
171
172 /*
173 * Ensure that the default sayDeparting() message from
174 * AccompanyingInTravelState is added to the transcript in a single report,
175 * starting with the NPC's name.
176 */
177
178 modify AccompanyingInTravelState
179 sayDeparting(conn)
180 {
181 local msg = mainOutputStream.captureOutput( {: inherited(conn) });;
182 if(msg.startsWith('\^'))
183 msg = msg.substr(2);
184 mainReport(msg);
185 }
186 ;
187