Debian Codegraph @

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

raw
1 #charset "us-ascii"
2
3 #include <adv3.h>
4 #include <en_us.h>
5
6 /*
7 * Combine Reports
8 *
9 * by Eric Eve
10 *
11 * Version 1.04 (27-Feb-11)
12 *
13 * This extension is a further development of Example 2 in the Technical
14 * Manual article on Manipulating the Transcript. If this extension is
15 * included in your game, reports of taking, dropping, or putting
16 * (in/on/under/behind) a series of reports will be combined into a
17 * single report listing the objects taken, dropped, or put. Reports of
18 * actions that failed will appear in the normal way, but will be moved
19 * to the end so as not to interrupt the combined report of successful
20 * actions.
21 *
22 * With this extension installed, output like:
23 *
24 *. blue ball: Taken
25 *. red pen: Taken
26 *. gold coin: Taken
27 *. gold coin: Taken
28 *
29 * Is grouped into a single sentence like:
30 *
31 *. You take the blue ball, the red pen, and the two gold coins.
32 *
33 * For PUT actions the extension also groups any implicit take action
34 * reports in the same way. e.g. instead of:
35 *
36 * (first taking the red book, then taking the green ball, then taking
37 * the pen, then taking the gold coin, then taking the gold coin)
38 *
39 * We get:
40 *
41 * (first taking the red book, the green ball, the pen and two gold coins)
42 *
43 * Version History:
44 *
45 * Version 1.03 tidies up the output from a multiple TAKE FROM command
46 *
47 * Version 1.02 makes use of the new standard Library class SimpleLister
48 *
49 * Version 1.01 makes a couple of things more customizable, and corrects
50 * a typo in the report produced for dropping multiple objects.
51 *
52 */
53
54
55
56 ModuleID
57 name = 'combineReports'
58 byline = 'by Eric Eve'
59 htmlByline = 'by <a href="mailto:[email protected]">Eric Eve</a>'
60 version = '1.03'
61 listingOrder = 75
62 ;
63
64 /* first define a useful lister */
65
66 definiteObjectLister: SimpleLister
67
68 /*
69 * Use the definite article form of the object ('the ball') unless its
70 * one of several identical objects, in which case the indefinite form
71 * ('a ball') is more appropriate.
72 */
73 showListItem(obj, options, pov, infoTab)
74 {
75 say(obj.isEquivalent ? obj.aName : obj.theName);
76 }
77 ;
78
79
80
81 /* modify CommandReport to keep a note of the direct object it refers to */
82
83 modify CommandReport
84 construct()
85 {
86 inherited();
87 dobj_ = gDobj;
88 }
89 dobj_ = nil
90 ;
91
92
93
94 /* Make TAKE, DROP and PUT combine reports by using the actionReportManager */
95
96 modify TakeAction
97 afterActionMain()
98 {
99 inherited;
100 if(parentAction == nil)
101 actionReportManager.afterActionMain();
102 }
103 vCorrect(str) { return '{take[s]|took}'; }
104 ;
105
106 modify TakeFromAction
107 afterActionMain()
108 {
109 inherited;
110 if(parentAction == nil)
111 actionReportManager.afterActionMain();
112 }
113 vCorrect(str) { return '{take[s]|took}'; }
114 ;
115
116 modify DropAction
117 afterActionMain()
118 {
119 inherited;
120 if(parentAction == nil)
121 actionReportManager.afterActionMain();
122 }
123 vCorrect(str) { return 'drop{s/ped}'; }
124 ;
125
126
127 modify PutOnAction
128 afterActionMain()
129 {
130 inherited;
131 if(parentAction == nil)
132 actionReportManager.afterActionMain();
133 }
134 vCorrect(str) { return '{put[s]|put}'; }
135 ;
136
137 modify PutInAction
138 afterActionMain()
139 {
140 inherited;
141 if(parentAction == nil)
142 actionReportManager.afterActionMain();
143 }
144 vCorrect(str) { return '{put[s]|put}'; }
145 ;
146
147 modify PutUnderAction
148 afterActionMain()
149 {
150 inherited;
151 if(parentAction == nil)
152 actionReportManager.afterActionMain();
153 }
154 vCorrect(str) { return '{put[s]|put}'; }
155 ;
156
157 modify PutBehindAction
158 afterActionMain()
159 {
160 inherited;
161 if(parentAction == nil)
162 actionReportManager.afterActionMain();
163 }
164 vCorrect(str) { return '{put[s]|put}'; }
165 ;
166
167 /*
168 * The actionReport Manager does most of the work. It is hopefully
169 * sufficiently general that it could be made to work with other actions
170 * besides TAKE, DROP and PUT IN/ON/UNDER/BEHIND. Experiment at your own
171 * risk!
172 */
173
174 actionReportManager: object
175
176 /*
177 * The minimum number of objects that must be in a list before we
178 * attempt to summarize it. Normally this will be 2, but it may be
179 * that some games will want to change it to 1, for example, so that
180 * the same reporting style is used for singleton objects as for lists.
181 */
182
183 minLengthToSummarize = 2
184
185 afterActionMain()
186 {
187 /*
188 * If the action isn't iterating over at least
189 * minLengthToSummarize direct objects we have nothing to do, so
190 * we'll stop before doing any messing with the transcript
191 */
192
193 if(gAction.dobjList_.length() < minLengthToSummarize)
194 return;
195
196
197 /*
198 * First move any reports of failed attempts to the end, so they
199 * can be fully reported after the summary of the actions that
200 * succeeded.
201 */
202
203
204 local len = gTranscript.reports_.length;
205
206 for(local i = gTranscript.reports_.indexWhich({x: x.isFailure});
207 i != nil && i <= len;
208 i = gTranscript.reports_.indexWhich({x: x.isFailure}))
209 {
210
211 /*
212 * first find the MultiObjectAnnouncement relating to this
213 * failure
214 */
215
216 local idx1 = i;
217 while(idx1 > 1
218 && !gTranscript.reports_[idx1].ofKind(MultiObjectAnnouncement))
219 idx1--;
220
221 /* then find the next MultiObjectAnnouncement */
222 local idx2 = i;
223 while(idx2 <= len
224 && !gTranscript.reports_[idx2].ofKind(MultiObjectAnnouncement))
225 idx2++;
226
227 /* Extract a list of all reports between these markers */
228 local objVec = new Vector(20).copyFrom(gTranscript.reports_,
229 idx1, 1, idx2 - idx1);
230
231 /*
232 * Ensure that all reports about this object are marked as
233 * failures
234 */
235
236 objVec.forEach({x: x.isFailure = true });
237
238 /* Move this list to the end of the transcript */
239 gTranscript.reports_.removeRange(idx1, idx2-1);
240
241 gTranscript.reports_.appendAll(objVec);
242
243 /*
244 * We don't want to check these reports again, so reduce len by
245 * the number of reports we just moved.
246 */
247 len -= objVec.length();
248
249 }
250
251 /*
252 * Give the game author the opportunity of further processing the
253 * failure reports, if desired. We also check the summarizeFailures
254 * flag (nil by default) so that we don't carry out any pointless
255 * processing if we don't need it here. It also leaves open the
256 * possibility of some future version of this extension defining
257 * its own version of processFailures(), which game authors can
258 * then opt in to using.
259 */
260 if(summarizeFailures && gTranscript.reports_.length > len)
261 {
262 local failVec
263 = processFailures(new Vector(20).copyFrom(gTranscript.reports_,
264 len + 1, 1, gTranscript.reports_.length() - len));
265
266 gTranscript.reports_ = gTranscript.reports_.setLength(len) +
267 failVec;
268 }
269 /*
270 * Define this function separately as we'll use it more than once;
271 * the function identifies implicit action reports relating to
272 * taking things.
273 */
274
275 local impFunc = {x: x.ofKind(ImplicitActionAnnouncement)
276 && x.action_.ofKind(TakeAction) && !x.isFailure };
277
278
279 /*
280 * Count how many implicit action reports there are relating to
281 * taking things.
282 */
283 local impActions = gTranscript.reports_.countWhich(impFunc);
284
285 /* We only need to do anything if there's more than one. */
286 if(impActions > 1)
287 {
288 /* Note the location of the first relevant implicit action report */
289 local firstImp = gTranscript.reports_.indexWhich(impFunc);
290
291 /* Store a copy of this report */
292 local rep = gTranscript.reports_[firstImp];
293
294 /* Get a list of all the implicit take reports */
295 local impVec = gTranscript.reports_.subset(impFunc);
296
297 local impTxt = definiteObjectLister.makeSimpleList(
298 impVec.mapAll({x: x.dobj_}).getUnique()
299 );
300
301 /*
302 * Change the text of this implicit action report to account
303 * for all the objects implicitly taken
304 */
305
306 local otherIdx = gTranscript.reports_.indexWhich
307 ({x: x.ofKind(ImplicitActionAnnouncement)
308 && !x.action_.ofKind(TakeAction) && !x.isFailure });
309
310 if(otherIdx)
311 {
312 /*
313 * if we're going to show some other implicit reports, it's
314 * probably best to do so first
315 */
316 firstImp = otherIdx + 1;
317 rep.messageText_ = 'taking ' + impTxt;
318 }
319 else
320 rep.messageText_ = '<./p0>\n<.assume>first taking ' +
321 impTxt + '<./assume>\n';
322
323 /*
324 * Prevent the implicitAnnouncementGrouper from overwriting the
325 * text we've just stored.
326 */
327 rep.messageProp_ = nil;
328
329 /*
330 * Remove all the individual implicit take action
331 * reports from the transcript.
332 */
333 gTranscript.reports_ = gTranscript.reports_.subset({x: !impFunc(x) });
334
335 /*
336 * Add back our summary implicit action report at the location
337 * of the first individual report we removed.
338 */
339 gTranscript.reports_.insertAt(firstImp, rep);
340
341 /*
342 * Remove all the CommandReports relating to taking things,
343 * since they would otherwise show up now we've removed
344 * most of the implicit action reports.
345 */
346 gTranscript.reports_ = gTranscript.reports_.subset({
347 x: !((x.ofKind(DefaultCommandReport) ||
348 x.ofKind(MainCommandReport))
349 && x.action_.ofKind(TakeAction)) } );
350 }
351
352 /*
353 * After all the preliminary work we finally summarize the
354 * successful default reports relating to the main action into a
355 * single report. There's a complication with TAKE FROM since a
356 * TAKE FROM action is eventually turned into a TAKE action, so we
357 * need to handle this as a special case.
358 */
359
360
361 gTranscript.summarizeAction(
362 { x: ((x.action_ == gAction) || (gActionIs(TakeFrom) &&
363 x.action_.ofKind (TakeAction)))
364 && !x.isFailure },
365 new function (vec)
366 {
367 /*
368 * Construct a string reporting the objects we did take,
369 * ensuring that each one is counted only once.
370 */
371
372 local dobjText = definiteObjectLister.makeSimpleList
373 ( vec.applyAll({x: x.dobj_}).getUnique());
374
375
376 /* Then use this to construct a description of the action */
377 return gAction.getActionDescWith(dobjText);
378
379 });
380
381 }
382
383 /*
384 * The processFailures method is provided as a hook for game authors
385 * who want to process the failure messages (e.g, to summarize them
386 * some way) further than this extension does (it just moves them all
387 * to the end).
388 *
389 * The vec parameter contains the vector of failure messages for
390 * further processing. Note that this method won't be called at all
391 * unless there are some failure messages in vec to process.
392 *
393 * This method also won't be called unless summarizeFailures is true
394 * (by default, it's nil).
395 *
396 * This method should return a vector of CommandReports resulting from
397 * whatever we wanted to do to the failure reports generated by the
398 * library. By default we just return the vector that's passed to us.
399 *
400 * The caller will automatically append the vector returned by this
401 * method to the vector of successul reports.
402 */
403
404 processFailures(vec) { return vec; }
405
406 /*
407 * By default we don't run the processFailures routine at all. This
408 * avoids pointless work when processFailures doesn't do anything, and
409 * also allows authors to opt in to any future version of
410 * processFailures that does do something.
411 */
412 summarizeFailures = nil
413
414 ;
415
416 /*
417 * This modification enables us to prevent the implicitAnnouncementGrouper
418 * from overwriting a customized messageText_
419 */
420
421 modify CommandAnnouncement
422 getMessageText([params])
423 {
424 if(messageProp_)
425 return gLibMessages.(messageProp_)(params...);
426 else
427 return messageText_;
428 }
429 ;
430
431
432 /*
433 * Service routines for describing an action given a string (dobjText)
434 * containing a list of direct objects.
435 *
436 * The complication is that the verb getVerbPhrase uses the verb in the
437 * present imperative form, e.g. 'TAKE'. This is wrong if the game is in the
438 * past tense or the actor is third person singular. We therefore need to
439 * correct for that.
440 */
441
442 modify TAction
443 getActionDescWith(dobjText)
444 {
445 return '{You/he} ' + vCheck(gAction.getVerbPhrase1(true,
446 gAction.verbPhrase, dobjText, nil)) + '.<.p>';
447 }
448
449 verbName()
450 {
451 rexMatch(pat, verbPhrase);
452 return rexGroup(1)[3];
453 }
454
455 vCheck(str)
456 {
457 local vName = verbName();
458 local correctedVName = vCorrect(vName);
459 if(vName != correctedVName)
460 str = rexReplace('%<'+vName+'%>', str, correctedVName, ReplaceAll);
461
462 return str;
463 }
464
465 /*
466 * Put the verb into the correct tense and ensure that it agrees with
467 * its subject. Subclasses may need to override depending on the verb,
468 * to add the appropriate verbEndingXXX property.
469 */
470
471 vCorrect(str) { return gActor.conjugateRegularVerb(str); }
472
473 pat = static new RexPattern('(.*)(?=/)')
474 ;
475
476 modify TIAction
477 getActionDescWith(dobjText)
478 {
479 return '{You/he} '+ vCheck(gAction.getVerbPhrase2(true,
480 gAction.verbPhrase, dobjText, nil, gIobj.theName)) + '.<.p>';
481 }
482 ;