| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library: command reports |
| 7 |
* |
| 8 |
* This module defines the "command report" classes, which the command |
| 9 |
* execution engine uses to keep track of the status of a command. |
| 10 |
*/ |
| 11 |
|
| 12 |
#include "adv3.h" |
| 13 |
|
| 14 |
|
| 15 |
/* ------------------------------------------------------------------------ */ |
| 16 |
/* |
| 17 |
* Command report objects. The library uses these to control how the |
| 18 |
* text from a command is displayed. Game code can also use report |
| 19 |
* objects to show and control command results, but this isn't usually |
| 20 |
* necessary; game code can usually simply display messages directly. |
| 21 |
* |
| 22 |
* Reports are divided into two broad classes: "default" and "full" |
| 23 |
* reports. |
| 24 |
* |
| 25 |
* A "default" report is one that simply confirms that an action was |
| 26 |
* performed, and provides little additional information. The library |
| 27 |
* uses default reports for simple commands whose full implications |
| 28 |
* should normally be obvious to a player typing such commands: take, |
| 29 |
* drop, put in, and the like. The library's default reports are |
| 30 |
* usually quite terse: "Taken", "Dropped", "Done". |
| 31 |
* |
| 32 |
* A "full" report is one that gives the player more information than a |
| 33 |
* simple confirmation. These reports typically describe either the |
| 34 |
* changes to the game state caused by a command or surprising side |
| 35 |
* effects of a command. For example, if the command is "push button," |
| 36 |
* and pushing the button opens the door next to the button, a full |
| 37 |
* report would describe the door opening. |
| 38 |
* |
| 39 |
* Note that a full report is warranted any time a command describes |
| 40 |
* anything beyond a simple confirmation. In our door-opening button |
| 41 |
* example, opening the door by pushing the button always warrants a |
| 42 |
* full report, even if the player has already seen the effects of the |
| 43 |
* button a hundred times before, and even if the button is labeled |
| 44 |
* "push to open door." It doesn't matter whether or not the |
| 45 |
* consequences of the command ought to be obvious to the player; what |
| 46 |
* matters is that the command warrants a report beyond a simple |
| 47 |
* confirmation. Any time a report is more than a simple confirmation, |
| 48 |
* it is a full report, no matter how obvious to the player the effects |
| 49 |
* of the action. |
| 50 |
* |
| 51 |
* Full reports are further divided into three subcategories by time |
| 52 |
* ordering: "main," "before," and "after." "Before" and "after" |
| 53 |
* reports are ordered before and after (respectively) a main report. |
| 54 |
*/ |
| 55 |
class CommandReport: object |
| 56 |
construct() |
| 57 |
{ |
| 58 |
/* |
| 59 |
* remember the action with which we're associated, unless a |
| 60 |
* subclass already specifically set the action |
| 61 |
*/ |
| 62 |
if (action_ == nil) |
| 63 |
action_ = gAction; |
| 64 |
} |
| 65 |
|
| 66 |
/* get/set my action */ |
| 67 |
getAction() { return action_; } |
| 68 |
setAction(action) { action_ = action; } |
| 69 |
|
| 70 |
/* check to see if my action is implicit */ |
| 71 |
isActionImplicit() { return action_ != nil && action_.isImplicit; } |
| 72 |
|
| 73 |
/* check to see if my action is nested in the other report's action */ |
| 74 |
isActionNestedIn(other) |
| 75 |
{ |
| 76 |
return (action_ != nil |
| 77 |
&& other.getAction() != nil |
| 78 |
&& action_.isNestedIn(other.getAction())); |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Flag: if this property is true, this report indicates a failure. |
| 83 |
* By default, a report does not indicate failure. |
| 84 |
*/ |
| 85 |
isFailure = nil |
| 86 |
|
| 87 |
/* |
| 88 |
* Flag: if this property is true, this report indicates an |
| 89 |
* interruption for interactive input. |
| 90 |
*/ |
| 91 |
isQuestion = nil |
| 92 |
|
| 93 |
/* iteration number current when we were added to the transcript */ |
| 94 |
iter_ = nil |
| 95 |
|
| 96 |
/* the action I'm associated with */ |
| 97 |
action_ = nil |
| 98 |
|
| 99 |
/* |
| 100 |
* Am I part of the same action as the given report? Returns true if |
| 101 |
* this action is part of the same iteration and part of the same |
| 102 |
* action as the other report. |
| 103 |
*/ |
| 104 |
isPartOf(report) |
| 105 |
{ |
| 106 |
/* |
| 107 |
* if I don't have an action, or the other report doesn't have an |
| 108 |
* action, we're not related |
| 109 |
*/ |
| 110 |
if (action_ == nil || report.action_ == nil) |
| 111 |
return nil; |
| 112 |
|
| 113 |
/* if our iterations don't match, we're not related */ |
| 114 |
if (iter_ != report.iter_) |
| 115 |
return nil; |
| 116 |
|
| 117 |
/* check if I'm part of the other report's action */ |
| 118 |
return action_.isPartOf(report.action_); |
| 119 |
} |
| 120 |
; |
| 121 |
|
| 122 |
/* |
| 123 |
* Group separator. This simply displays separation between groups of |
| 124 |
* messages - that is, between one set of messages associated with a |
| 125 |
* single action and a set of messages associated with a different |
| 126 |
* action. |
| 127 |
*/ |
| 128 |
class GroupSeparatorMessage: CommandReport |
| 129 |
construct(report) |
| 130 |
{ |
| 131 |
/* use the same action and iteration as the given report */ |
| 132 |
action_ = report.getAction(); |
| 133 |
iter_ = report.iter_; |
| 134 |
} |
| 135 |
|
| 136 |
/* show the normal command results separator */ |
| 137 |
showMessage() { say(gLibMessages.complexResultsSeparator); } |
| 138 |
; |
| 139 |
|
| 140 |
/* |
| 141 |
* Internal separator. This displays separation within a group of |
| 142 |
* messages for a command, to visually separate the results from an |
| 143 |
* implied command from the results for the enclosing command. |
| 144 |
*/ |
| 145 |
class InternalSeparatorMessage: CommandReport |
| 146 |
construct(report) |
| 147 |
{ |
| 148 |
/* use the same action and iteration as the given report */ |
| 149 |
action_ = report.getAction(); |
| 150 |
iter_ = report.iter_; |
| 151 |
} |
| 152 |
|
| 153 |
/* show the normal command results separator */ |
| 154 |
showMessage() { say(gLibMessages.internalResultsSeparator); } |
| 155 |
; |
| 156 |
|
| 157 |
/* |
| 158 |
* Report boundary marker. This is a pseudo-report that doesn't display |
| 159 |
* anything; its purpose is to allow a caller to identify a block of |
| 160 |
* reports (the reports between two markers) for later removal or |
| 161 |
* reordering. |
| 162 |
*/ |
| 163 |
class MarkerReport: CommandReport |
| 164 |
showMessage() { } |
| 165 |
; |
| 166 |
|
| 167 |
/* |
| 168 |
* End-of-description marker. This serves as a marker in the transcript |
| 169 |
* stream to let us know where the descriptive reports for a given |
| 170 |
* action end. |
| 171 |
*/ |
| 172 |
class EndOfDescReport: MarkerReport |
| 173 |
; |
| 174 |
|
| 175 |
/* |
| 176 |
* Simple MessageResult-based command report |
| 177 |
*/ |
| 178 |
class CommandReportMessage: CommandReport, MessageResult |
| 179 |
construct([params]) |
| 180 |
{ |
| 181 |
/* invoke our base class constructors */ |
| 182 |
inherited CommandReport(); |
| 183 |
inherited MessageResult(params...); |
| 184 |
} |
| 185 |
; |
| 186 |
|
| 187 |
/* |
| 188 |
* default report |
| 189 |
*/ |
| 190 |
class DefaultCommandReport: CommandReportMessage |
| 191 |
; |
| 192 |
|
| 193 |
/* |
| 194 |
* extra information report |
| 195 |
*/ |
| 196 |
class ExtraCommandReport: CommandReportMessage |
| 197 |
; |
| 198 |
|
| 199 |
/* |
| 200 |
* default descriptive report |
| 201 |
*/ |
| 202 |
class DefaultDescCommandReport: CommandReportMessage |
| 203 |
; |
| 204 |
|
| 205 |
/* |
| 206 |
* cosmetic spacing report |
| 207 |
*/ |
| 208 |
class CosmeticSpacingCommandReport: CommandReportMessage |
| 209 |
; |
| 210 |
|
| 211 |
/* |
| 212 |
* base class for all "full" reports |
| 213 |
*/ |
| 214 |
class FullCommandReport: CommandReportMessage |
| 215 |
/* |
| 216 |
* a full report has a sequence number that tells us where the |
| 217 |
* report goes relative to the others - the higher this number, the |
| 218 |
* later the report goes |
| 219 |
*/ |
| 220 |
seqNum = nil |
| 221 |
; |
| 222 |
|
| 223 |
/* |
| 224 |
* "before" report - these come before the main report |
| 225 |
*/ |
| 226 |
class BeforeCommandReport: FullCommandReport |
| 227 |
seqNum = 1 |
| 228 |
; |
| 229 |
|
| 230 |
/* |
| 231 |
* main report |
| 232 |
*/ |
| 233 |
class MainCommandReport: FullCommandReport |
| 234 |
seqNum = 2 |
| 235 |
; |
| 236 |
|
| 237 |
/* |
| 238 |
* failure report |
| 239 |
*/ |
| 240 |
class FailCommandReport: FullCommandReport |
| 241 |
seqNum = 2 |
| 242 |
isFailure = true |
| 243 |
; |
| 244 |
|
| 245 |
/* |
| 246 |
* failure marker - this is a silent report that marks an action as |
| 247 |
* having failed without actually generating any message text |
| 248 |
*/ |
| 249 |
class FailCommandMarker: MarkerReport |
| 250 |
isFailure = true |
| 251 |
; |
| 252 |
|
| 253 |
/* |
| 254 |
* "after" report - these come after the main report |
| 255 |
*/ |
| 256 |
class AfterCommandReport: FullCommandReport |
| 257 |
seqNum = 3 |
| 258 |
; |
| 259 |
|
| 260 |
/* |
| 261 |
* An interruption for interactive input. This is used to report a |
| 262 |
* prompt for more information that's needed before the command can |
| 263 |
* proceed, such as a prompt for a missing object, or a disambiguation |
| 264 |
* prompt. |
| 265 |
*/ |
| 266 |
class QuestionCommandReport: MainCommandReport |
| 267 |
isQuestion = true; |
| 268 |
; |
| 269 |
|
| 270 |
/* |
| 271 |
* A conversation begin/end report. This is a special marker we insert |
| 272 |
* into the transcript to flag the boundaries of an NPC's conversational |
| 273 |
* message. |
| 274 |
*/ |
| 275 |
class ConvBoundaryReport: CommandReport |
| 276 |
construct(id) { actorID = id; } |
| 277 |
|
| 278 |
/* the actor's ID number, as assigned by the ConversationManager */ |
| 279 |
actorID = nil |
| 280 |
; |
| 281 |
class ConvBeginReport: ConvBoundaryReport |
| 282 |
showMessage() { say('<.convbegin ' + actorID + '>'); } |
| 283 |
; |
| 284 |
class ConvEndReport: ConvBoundaryReport |
| 285 |
construct(id, node) |
| 286 |
{ |
| 287 |
inherited(id); |
| 288 |
defConvNode = node; |
| 289 |
} |
| 290 |
|
| 291 |
showMessage() |
| 292 |
{ |
| 293 |
if (actorID != nil) |
| 294 |
say('<.convend ' + actorID + ' ' |
| 295 |
+ (defConvNode == nil ? '' : defConvNode) + '>'); |
| 296 |
} |
| 297 |
|
| 298 |
/* the default new ConvNode for the actor */ |
| 299 |
defConvNode = nil |
| 300 |
; |
| 301 |
|
| 302 |
/* ------------------------------------------------------------------------ */ |
| 303 |
/* |
| 304 |
* Announcements. We use these to track announcements to be made as |
| 305 |
* part of an action's results. |
| 306 |
*/ |
| 307 |
class CommandAnnouncement: CommandReport |
| 308 |
construct([params]) |
| 309 |
{ |
| 310 |
/* inherit default handling */ |
| 311 |
inherited(); |
| 312 |
|
| 313 |
/* remember our text */ |
| 314 |
messageText_ = getMessageText(params...); |
| 315 |
} |
| 316 |
|
| 317 |
/* |
| 318 |
* Get our message text. By default, we simply get the gLibMessages |
| 319 |
* message given by the property. |
| 320 |
*/ |
| 321 |
getMessageText([params]) |
| 322 |
{ |
| 323 |
/* get the library message */ |
| 324 |
return gLibMessages.(messageProp_)(params...); |
| 325 |
} |
| 326 |
|
| 327 |
/* |
| 328 |
* Show our message. Our default implementation shows the library |
| 329 |
* message given by our messageProp_ property, using the parameters |
| 330 |
* we stored in our constructor. |
| 331 |
*/ |
| 332 |
showMessage() |
| 333 |
{ |
| 334 |
/* call gLibMessages to show our message */ |
| 335 |
say(messageText_); |
| 336 |
} |
| 337 |
|
| 338 |
/* our gLibMessages property */ |
| 339 |
messageProp_ = nil |
| 340 |
|
| 341 |
/* our message text */ |
| 342 |
messageText_ = '' |
| 343 |
; |
| 344 |
|
| 345 |
/* |
| 346 |
* Multiple-object announcement. When the player applies a single |
| 347 |
* command to a series of objects (as in "take the book and the folder" |
| 348 |
* or "take all"), we'll show one of these announcements for each object, |
| 349 |
* just before we execute the command for that object. This announcement |
| 350 |
* usually just shows the object's name plus suitable punctuation (in |
| 351 |
* English, a colon), and helps the player see which results go with |
| 352 |
* which objects. |
| 353 |
*/ |
| 354 |
class MultiObjectAnnouncement: CommandAnnouncement |
| 355 |
construct(preCalcMsg, obj, whichObj, action) |
| 356 |
{ |
| 357 |
/* do the inherited work */ |
| 358 |
inherited(obj, whichObj, action); |
| 359 |
|
| 360 |
/* |
| 361 |
* if we have a pre-calculated message, use it instead of the |
| 362 |
* message we just generated - this lets the caller explicitly |
| 363 |
* set the message as desired |
| 364 |
*/ |
| 365 |
if (preCalcMsg != nil) |
| 366 |
messageText_ = preCalcMsg; |
| 367 |
} |
| 368 |
|
| 369 |
/* show the announceMultiActionObject message */ |
| 370 |
messageProp_ = &announceMultiActionObject |
| 371 |
; |
| 372 |
|
| 373 |
/* |
| 374 |
* Default object announcement. We display this announcement whenever |
| 375 |
* the player leaves out a required object from a command, but the parser |
| 376 |
* is able to infer which object they must have meant. The parser infers |
| 377 |
* that an object was intended when a verb requires an object that the |
| 378 |
* player didn't specify, and there's only one logical choice for the |
| 379 |
* missing object. We announce our assumption to put it out in the open, |
| 380 |
* to ensure that the player is immediately alerted if they had something |
| 381 |
* else in mind. |
| 382 |
* |
| 383 |
* In English, this type of announcement conventionally consists of |
| 384 |
* simply the name of the assumed object, in parenthesis and on a line by |
| 385 |
* itself. In cases where the object role involves a prepositional |
| 386 |
* phrase in the verb structure, we generally show the preposition before |
| 387 |
* the object name. This format usually reads intuitively, by combining |
| 388 |
* with the text just above of the player's own command: |
| 389 |
* |
| 390 |
*. >open |
| 391 |
*. (the door> |
| 392 |
*. You try opening the door, but it seems to be locked. |
| 393 |
*. |
| 394 |
*. >unlock the door |
| 395 |
* (with the key) |
| 396 |
*/ |
| 397 |
class DefaultObjectAnnouncement: CommandAnnouncement |
| 398 |
construct(obj, whichObj, action, allResolved) |
| 399 |
{ |
| 400 |
/* remember our object */ |
| 401 |
obj_ = obj; |
| 402 |
|
| 403 |
/* remember the message parameters */ |
| 404 |
whichObj_ = whichObj; |
| 405 |
allResolved_ = allResolved; |
| 406 |
|
| 407 |
/* remember my action */ |
| 408 |
action_ = action; |
| 409 |
|
| 410 |
/* inherit default handling */ |
| 411 |
inherited(); |
| 412 |
} |
| 413 |
|
| 414 |
/* get our message text */ |
| 415 |
getMessageText() |
| 416 |
{ |
| 417 |
/* get the announcement message from our object */ |
| 418 |
return obj_.announceDefaultObject(whichObj_, action_, allResolved_); |
| 419 |
} |
| 420 |
|
| 421 |
/* our defaulted object */ |
| 422 |
obj_ = nil |
| 423 |
|
| 424 |
/* our message parameters */ |
| 425 |
whichObj_ = nil |
| 426 |
allResolved_ = nil |
| 427 |
; |
| 428 |
|
| 429 |
/* |
| 430 |
* Ambiguous object announcement. We display this when the parser |
| 431 |
* manages to resolve a noun phrase to an object (or objects) from an |
| 432 |
* ambiguous set of possibilities, without having to ask the player for |
| 433 |
* help but also without absolute certainty that the objects selected are |
| 434 |
* the ones the player meant. This happens when more than enough objects |
| 435 |
* are logical possibilities for selection, but some objects are more |
| 436 |
* logical choices than others. The parser picks the most logical of the |
| 437 |
* available options, but since other logical choices are present, the |
| 438 |
* parser can't be certain that it chose the ones the player actually |
| 439 |
* meant. Because of this uncertainty, we generate one of these |
| 440 |
* announcements each time this happens. This report lets the player |
| 441 |
* know exactly which object we chose, which will immediately alert the |
| 442 |
* player when our selection is different from what they had in mind. |
| 443 |
* |
| 444 |
* In form, this type of announcement usually looks just like a default |
| 445 |
* object announcement. |
| 446 |
*/ |
| 447 |
class AmbigObjectAnnouncement: CommandAnnouncement |
| 448 |
/* show the announceAmbigObject announcement */ |
| 449 |
messageProp_ = &announceAmbigActionObject |
| 450 |
; |
| 451 |
|
| 452 |
/* |
| 453 |
* Remapped action announcement. This is used when we need to mention a |
| 454 |
* defaulted or disambiguated object, but the player's original input was |
| 455 |
* remapped to a different action that rearranges the object roles. In |
| 456 |
* these cases, rather than just announcing the defaulted object name, we |
| 457 |
* announce the entire remapped action; we show the full action |
| 458 |
* description because rearrangement of the object roles usually makes |
| 459 |
* the standard object-only announcement confusing to read, since it |
| 460 |
* doesn't naturally fit in as a continuation of what the user typed. |
| 461 |
* |
| 462 |
* In English, this message is usually shown with the entire verb phrase, |
| 463 |
* in present participle form ("opening the door"), enclosed in |
| 464 |
* parentheses and on a line by itself. |
| 465 |
*/ |
| 466 |
class RemappedActionAnnouncement: CommandAnnouncement |
| 467 |
construct() |
| 468 |
{ |
| 469 |
/* use the action as the message parameter */ |
| 470 |
inherited(gAction); |
| 471 |
} |
| 472 |
|
| 473 |
messageProp_ = &announceRemappedAction |
| 474 |
; |
| 475 |
|
| 476 |
/* |
| 477 |
* Each language module must define a class called |
| 478 |
* ImplicitAnnouncementContext, and three instances of the class, for use |
| 479 |
* by the generic library. The language module can define other |
| 480 |
* instances of the context class as needed. We minimally need the |
| 481 |
* following instances to be defined by the language module: |
| 482 |
* |
| 483 |
* standardImpCtx: this is the standard context, which indicates that we |
| 484 |
* want the default format for the implicit action announcement. |
| 485 |
* |
| 486 |
* tryingImpCtx: this is the "trying" context, which indicates that we |
| 487 |
* want the announcement to phrase the action to indicate that we're only |
| 488 |
* trying the action, not actually performing it. We use this when the |
| 489 |
* implicit action has failed, in which case we want our announcement to |
| 490 |
* say that we're merely attempting the action; the announcement |
| 491 |
* shouldn't imply that the action has actually been performed. |
| 492 |
* |
| 493 |
* askingImpCtx: this is the "asking" context, which indicates that the |
| 494 |
* action was interrupted with an interactive question, such as a prompt |
| 495 |
* for a missing direct object. |
| 496 |
* |
| 497 |
* We leave it up to the language module to define the class and these |
| 498 |
* two instances. This lets the language module represent the context |
| 499 |
* types any way it likes. |
| 500 |
*/ |
| 501 |
|
| 502 |
/* |
| 503 |
* Implicit action announcement. This is displayed when we perform a |
| 504 |
* command implicitly, which we usually do to fulfill a precondition of |
| 505 |
* an action. |
| 506 |
* |
| 507 |
* In English, we usually show an implied action as the verb participle |
| 508 |
* phrase ("opening the door"), prefixed with "first", and enclosed in |
| 509 |
* parentheses on a line by itself (hence, "(first opening the door)"). |
| 510 |
*/ |
| 511 |
class ImplicitActionAnnouncement: CommandAnnouncement |
| 512 |
construct(action, msg) |
| 513 |
{ |
| 514 |
/* use the given message property */ |
| 515 |
messageProp_ = msg; |
| 516 |
|
| 517 |
/* |
| 518 |
* Inherit default. The first message parameter is the action; |
| 519 |
* the second is our standard implicit action context object, |
| 520 |
* indicating that we want the normal context. |
| 521 |
*/ |
| 522 |
inherited(action, standardImpCtx); |
| 523 |
} |
| 524 |
|
| 525 |
/* |
| 526 |
* Make this announcement silent. This eliminates any announcement |
| 527 |
* for this action, but makes it otherwise behave like a normal |
| 528 |
* implied action. |
| 529 |
*/ |
| 530 |
makeSilent() |
| 531 |
{ |
| 532 |
/* clear my message text */ |
| 533 |
messageText_ = ''; |
| 534 |
|
| 535 |
/* |
| 536 |
* use the silent announcement message if we have to regenerate |
| 537 |
* our text for another context |
| 538 |
*/ |
| 539 |
messageProp_ = &silentImplicitAction; |
| 540 |
} |
| 541 |
|
| 542 |
/* |
| 543 |
* Note that the action we're attempting is merely an attempt that |
| 544 |
* failed. This will change our report to indicate that we're only |
| 545 |
* trying the action, rather than suggesting that we actually carried |
| 546 |
* it out. |
| 547 |
*/ |
| 548 |
noteJustTrying() |
| 549 |
{ |
| 550 |
/* note that we're just trying the action */ |
| 551 |
justTrying = true; |
| 552 |
|
| 553 |
/* change our message to the "trying" form */ |
| 554 |
messageText_ = getMessageText(getAction(), tryingImpCtx); |
| 555 |
} |
| 556 |
|
| 557 |
/* |
| 558 |
* Note that the action we're attempting is incomplete, as it was |
| 559 |
* interupted for interactive input (such as asking for a missing |
| 560 |
* object). |
| 561 |
*/ |
| 562 |
noteQuestion() |
| 563 |
{ |
| 564 |
/* note that the action was interrupted with a question */ |
| 565 |
justAsking = true; |
| 566 |
|
| 567 |
/* change our message to the "asking" form */ |
| 568 |
messageText_ = getMessageText(getAction(), askingImpCtx); |
| 569 |
} |
| 570 |
|
| 571 |
/* |
| 572 |
* Flag: we're just attempting the action; this is set when we |
| 573 |
* determine that the implicit action has failed, in which case we |
| 574 |
* want an announcement indicating that we're merely attempting the |
| 575 |
* action, not actually performing it. Presume that we're actually |
| 576 |
* going to perform the action; the action can change this if |
| 577 |
* necessary. |
| 578 |
*/ |
| 579 |
justTrying = nil |
| 580 |
|
| 581 |
/* flag: the action was interrupted with an interactive question */ |
| 582 |
justAsking = nil |
| 583 |
; |
| 584 |
|
| 585 |
class CommandSepAnnouncement: CommandAnnouncement |
| 586 |
construct() |
| 587 |
{ |
| 588 |
/* we're not associated with an iteration or action */ |
| 589 |
action_ = nil; |
| 590 |
iter_ = 0; |
| 591 |
} |
| 592 |
|
| 593 |
showMessage() |
| 594 |
{ |
| 595 |
/* show a command separator */ |
| 596 |
"<.commandsep>"; |
| 597 |
} |
| 598 |
; |
| 599 |
|
| 600 |
/* ------------------------------------------------------------------------ */ |
| 601 |
/* |
| 602 |
* Command Transcript. This is a "semantic transcript" of the results of |
| 603 |
* a command. This provides a list of CommandReport objects describing |
| 604 |
* the results of the command. |
| 605 |
*/ |
| 606 |
class CommandTranscript: OutputFilter |
| 607 |
construct() |
| 608 |
{ |
| 609 |
/* set up a vector to hold the reports */ |
| 610 |
reports_ = new Vector(5); |
| 611 |
} |
| 612 |
|
| 613 |
/* |
| 614 |
* flag: the command has failed (i.e., at least one failure report |
| 615 |
* has been generated) |
| 616 |
*/ |
| 617 |
isFailure = nil |
| 618 |
|
| 619 |
/* |
| 620 |
* Note that the current action has failed. This is equivalent to |
| 621 |
* adding a reportFailure() message to the transcript. |
| 622 |
*/ |
| 623 |
noteFailure() |
| 624 |
{ |
| 625 |
/* add an empty reportFailure message */ |
| 626 |
reportFailure(''); |
| 627 |
} |
| 628 |
|
| 629 |
/* |
| 630 |
* Did the given action fail? This scans the transcript to determine |
| 631 |
* if there are any failure messages associated with the given |
| 632 |
* action. |
| 633 |
*/ |
| 634 |
actionFailed(action) |
| 635 |
{ |
| 636 |
/* |
| 637 |
* scan the transcript for failure messages that are associated |
| 638 |
* with the given action |
| 639 |
*/ |
| 640 |
return reports_.indexWhich( |
| 641 |
{x: x.isPartOf(action) && x.isFailure}) != nil; |
| 642 |
} |
| 643 |
|
| 644 |
/* |
| 645 |
* flag: I'm active; when this is nil, we'll pass text through our |
| 646 |
* filter routine unchanged |
| 647 |
*/ |
| 648 |
isActive = true |
| 649 |
|
| 650 |
/* |
| 651 |
* Summarize the current action's reports. This allows a caller to |
| 652 |
* turn a series of iterated reports into a single report for the |
| 653 |
* entire action. For example, we could change something like this: |
| 654 |
* |
| 655 |
* gold coin: Bob accepts the gold coin. |
| 656 |
*. gold coin: Bob accepts the gold coin. |
| 657 |
*. gold coin: Bob accepts the gold coin. |
| 658 |
* |
| 659 |
* into this: |
| 660 |
* |
| 661 |
* Bob accepts the three gold coins. |
| 662 |
* |
| 663 |
* This function runs through the reports for the current action, |
| 664 |
* submitting each one to the 'cond' callback to see if it's of |
| 665 |
* interest to the summary. For each consecutive run of two or more |
| 666 |
* reports that can be summarized, we'll remove the reports that |
| 667 |
* 'cond' accepted, and we'll remove the multiple-object announcement |
| 668 |
* reports associated with them, and we'll insert a new report with |
| 669 |
* the message returned by the 'report' callback. |
| 670 |
* |
| 671 |
* 'cond' is called as cond(x), where 'x' is a report object. This |
| 672 |
* callback returns true if the report can be summarized for the |
| 673 |
* caller's purposes, nil if not. |
| 674 |
* |
| 675 |
* 'report' is called as report(vec), where 'vec' is a Vector |
| 676 |
* consisting of all of the consecutive report objects that we're now |
| 677 |
* summarizing. This function returns a string giving the message to |
| 678 |
* use in place of the reports we're removing. This should be a |
| 679 |
* summary message, standing in for the set of individual reports |
| 680 |
* we're removing. |
| 681 |
* |
| 682 |
* There's an important subtlety to note. If the messages you're |
| 683 |
* summarizing are conversational (that is, if they're generated by |
| 684 |
* TopicEntry responses), you should take care to generate the full |
| 685 |
* replacement text in the 'report' part, rather than doing so in |
| 686 |
* separate code that you run after summarizeAction() returns. This |
| 687 |
* is important because it ensures that the Conversation Manager |
| 688 |
* knows that your replacement message is part of the same |
| 689 |
* conversation. If you wait until after summarizeAction() returns |
| 690 |
* to generate more response text, the conversation manager won't |
| 691 |
* realize that the additional text is part of the same conversation. |
| 692 |
*/ |
| 693 |
summarizeAction(cond, report) |
| 694 |
{ |
| 695 |
local vec = new Vector(8); |
| 696 |
local rpt = reports_; |
| 697 |
local cnt = rpt.length(); |
| 698 |
local i; |
| 699 |
|
| 700 |
/* find the first report for the current action */ |
| 701 |
for (i = 1 ; i <= cnt && rpt[i].getAction() != gAction ; ++i) ; |
| 702 |
|
| 703 |
/* iterate over the transcript for the current action */ |
| 704 |
for ( ; ; ++i) |
| 705 |
{ |
| 706 |
local ok; |
| 707 |
|
| 708 |
/* presume we won't find a summarizable item */ |
| 709 |
ok = nil; |
| 710 |
|
| 711 |
/* if we're still in range, check what we have */ |
| 712 |
if (i <= cnt) |
| 713 |
{ |
| 714 |
/* get the current item */ |
| 715 |
local cur = rpt[i]; |
| 716 |
|
| 717 |
/* if this one is of interest, note it in the vector */ |
| 718 |
if (cond(cur)) |
| 719 |
{ |
| 720 |
/* add it to our vector of summarizable reports */ |
| 721 |
vec.append(cur); |
| 722 |
|
| 723 |
/* note that we're okay on this round */ |
| 724 |
ok = true; |
| 725 |
} |
| 726 |
else if (cur.ofKind(ImplicitActionAnnouncement) |
| 727 |
|| cur.ofKind(MultiObjectAnnouncement) |
| 728 |
|| cur.ofKind(DefaultCommandReport) |
| 729 |
|| cur.ofKind(ConvBoundaryReport)) |
| 730 |
{ |
| 731 |
/* we can keep these in summaries */ |
| 732 |
ok = true; |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
/* |
| 737 |
* If this item isn't summarizable, or we've reached the last |
| 738 |
* item, generate a summary of any we have so far. (We need |
| 739 |
* to generate the summary on reaching the last item because |
| 740 |
* there are no further items that could go in the summary.) |
| 741 |
*/ |
| 742 |
if (!ok || i == cnt) |
| 743 |
{ |
| 744 |
/* if we have two or more items, generate a summary */ |
| 745 |
if (vec.length() > 1) |
| 746 |
{ |
| 747 |
local insIdx; |
| 748 |
local txt; |
| 749 |
|
| 750 |
/* remove each item in the vector */ |
| 751 |
foreach (local cur in vec) |
| 752 |
{ |
| 753 |
local idx; |
| 754 |
|
| 755 |
/* get the index of the current item */ |
| 756 |
idx = rpt.indexOf(cur); |
| 757 |
|
| 758 |
/* |
| 759 |
* we're summarizing this item, so remove the |
| 760 |
* individual item - subsume it into the summary |
| 761 |
*/ |
| 762 |
rpt.removeElementAt(idx); |
| 763 |
--i; |
| 764 |
--cnt; |
| 765 |
|
| 766 |
/* insert the summary here */ |
| 767 |
insIdx = idx; |
| 768 |
|
| 769 |
/* |
| 770 |
* skip any implicit action announcements, |
| 771 |
* default command announcements, and |
| 772 |
* conversational boundary markers |
| 773 |
*/ |
| 774 |
for (--idx ; |
| 775 |
idx > 0 |
| 776 |
&& (rpt[idx].ofKind(ImplicitActionAnnouncement) |
| 777 |
|| rpt[idx].ofKind(DefaultCommandReport) |
| 778 |
|| rpt[idx].ofKind(ConvBoundaryReport)) ; |
| 779 |
--idx) ; |
| 780 |
|
| 781 |
/* |
| 782 |
* if the preceding element is a multi-object |
| 783 |
* announcement, remove it - let the summary |
| 784 |
* mention the individual objects if it wants to |
| 785 |
*/ |
| 786 |
if (idx > 0 |
| 787 |
&& rpt[idx].ofKind(MultiObjectAnnouncement)) |
| 788 |
{ |
| 789 |
/* remove this element and adjust the counters */ |
| 790 |
rpt.removeElementAt(idx); |
| 791 |
--i; |
| 792 |
--cnt; |
| 793 |
--insIdx; |
| 794 |
|
| 795 |
/* |
| 796 |
* If this leaves us with a <.convbegin> |
| 797 |
* preceded directly by a <.convend> for the |
| 798 |
* same actor, the two cancel each other out. |
| 799 |
* Simply remove both of them. |
| 800 |
*/ |
| 801 |
if (idx <= rpt.length() |
| 802 |
&& idx > 1 |
| 803 |
&& rpt[idx].ofKind(ConvBeginReport) |
| 804 |
&& rpt[idx-1].ofKind(ConvEndReport) |
| 805 |
&& rpt[idx].actorID == rpt[idx-1].actorID) |
| 806 |
{ |
| 807 |
/* |
| 808 |
* we have a canceling <.convend> + |
| 809 |
* <.convbegin> pair - simply remove them |
| 810 |
* both and adjust the counters |
| 811 |
* accordingly |
| 812 |
*/ |
| 813 |
rpt.removeRange(idx - 1, idx); |
| 814 |
i -= 2; |
| 815 |
cnt -= 2; |
| 816 |
insIdx -= 2; |
| 817 |
} |
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
/* ask the caller for the summary */ |
| 822 |
txt = report(vec); |
| 823 |
|
| 824 |
/* insert it */ |
| 825 |
rpt.insertAt(insIdx, new MainCommandReport(txt)); |
| 826 |
++cnt; |
| 827 |
++i; |
| 828 |
} |
| 829 |
|
| 830 |
/* clear the vector */ |
| 831 |
if (vec.length() > 0) |
| 832 |
vec.removeRange(1, vec.length()); |
| 833 |
} |
| 834 |
|
| 835 |
/* if we've reached the end of the list, we're done */ |
| 836 |
if (i > cnt) |
| 837 |
break; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
/* activate - set up to capture output */ |
| 842 |
activate() |
| 843 |
{ |
| 844 |
/* make myself active */ |
| 845 |
isActive = true; |
| 846 |
} |
| 847 |
|
| 848 |
/* deactivate - stop capturing output */ |
| 849 |
deactivate() |
| 850 |
{ |
| 851 |
/* make myself inactive */ |
| 852 |
isActive = nil; |
| 853 |
} |
| 854 |
|
| 855 |
/* |
| 856 |
* Count an iteration. An Action should call this once per iteration |
| 857 |
* if it's a top-level (non-nested) command. |
| 858 |
*/ |
| 859 |
newIter() { ++iter_; } |
| 860 |
|
| 861 |
/* |
| 862 |
* Flush the transcript in preparation for reading input. This shows |
| 863 |
* all pending reports, clears the backlog of reports (so that we |
| 864 |
* don't show them again in the future), and deactivates the |
| 865 |
* transcript's capture feature so that subsequent output goes |
| 866 |
* directly to the output stream. |
| 867 |
* |
| 868 |
* We return the former activation status - that is, we return true |
| 869 |
* if the transcript was activated before the call, nil if not. |
| 870 |
*/ |
| 871 |
flushForInput() |
| 872 |
{ |
| 873 |
/* show our reports, and deactivate output capture */ |
| 874 |
local wasActive = showReports(true); |
| 875 |
|
| 876 |
/* clear the reports, since we've now shown them all */ |
| 877 |
clearReports(); |
| 878 |
|
| 879 |
/* return the previous activation status */ |
| 880 |
return wasActive; |
| 881 |
} |
| 882 |
|
| 883 |
/* |
| 884 |
* Show our reports. Returns true if the transcript was previously |
| 885 |
* active, nil if not. |
| 886 |
*/ |
| 887 |
showReports(deact) |
| 888 |
{ |
| 889 |
local wasActive; |
| 890 |
|
| 891 |
/* |
| 892 |
* remember whether we were active or not originally, then |
| 893 |
* deactivate (maybe just temporarily) so that we can write out |
| 894 |
* our reports without recursively intercepting them |
| 895 |
*/ |
| 896 |
wasActive = isActive; |
| 897 |
deactivate(); |
| 898 |
|
| 899 |
/* first, apply all defined transformations to our transcript */ |
| 900 |
applyTransforms(); |
| 901 |
|
| 902 |
/* |
| 903 |
* Temporarily cancel any sense context message blocking. We |
| 904 |
* have already taken into account for each report whether or |
| 905 |
* not the report was visible when it was generated, so we can |
| 906 |
* display each report that made it past that check without any |
| 907 |
* further conditions. |
| 908 |
*/ |
| 909 |
callWithSenseContext(nil, nil, function() |
| 910 |
{ |
| 911 |
/* show the reports */ |
| 912 |
foreach (local cur in reports_) |
| 913 |
{ |
| 914 |
/* if we're allowed to show this report, show it */ |
| 915 |
if (canShowReport(cur)) |
| 916 |
cur.showMessage(); |
| 917 |
} |
| 918 |
}); |
| 919 |
|
| 920 |
/* |
| 921 |
* if we were active and we're not being asked to deactivate, |
| 922 |
* re-activate now that we're finished showing our reports |
| 923 |
*/ |
| 924 |
if (wasActive && !deact) |
| 925 |
activate(); |
| 926 |
|
| 927 |
/* return the former activation status */ |
| 928 |
return wasActive; |
| 929 |
} |
| 930 |
|
| 931 |
/* |
| 932 |
* Add a report. |
| 933 |
*/ |
| 934 |
addReport(report) |
| 935 |
{ |
| 936 |
/* check for a failure report */ |
| 937 |
if (report.isFailure) |
| 938 |
{ |
| 939 |
/* set the failure flag for the entire command */ |
| 940 |
isFailure = true; |
| 941 |
|
| 942 |
/* |
| 943 |
* If this is an implied command, and the actor is in "NPC |
| 944 |
* mode", suppress the report. When an implied action fails |
| 945 |
* in NPC mode, we act as though we never attempted the |
| 946 |
* action. |
| 947 |
*/ |
| 948 |
if (gAction.isImplicit && gActor.impliedCommandMode() == ModeNPC) |
| 949 |
{ |
| 950 |
/* add a failure marker, not the message report */ |
| 951 |
reports_.append(new FailCommandMarker()); |
| 952 |
|
| 953 |
/* that's all we need to add */ |
| 954 |
return; |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
/* |
| 959 |
* Do not queue reports made while the sense context is blocking |
| 960 |
* output because the player character cannot sense the locus of |
| 961 |
* the action. Note that this check comes before we queue the |
| 962 |
* report, but after we've noted any effect on the status of the |
| 963 |
* overall action; even if we're not going to show the report, |
| 964 |
* its status effects are still valid. |
| 965 |
*/ |
| 966 |
if (senseContext.isBlocking) |
| 967 |
return; |
| 968 |
|
| 969 |
/* |
| 970 |
* If the new report's iteration ID hasn't been set already, note |
| 971 |
* the current iteration in the report. Some types of reports |
| 972 |
* will have already set a specific iteration before we get here, |
| 973 |
* so set the iteration ID only if the report hasn't done so |
| 974 |
* already. |
| 975 |
*/ |
| 976 |
if (report.iter_ == nil) |
| 977 |
report.iter_ = iter_; |
| 978 |
|
| 979 |
/* append the report */ |
| 980 |
reports_.append(report); |
| 981 |
} |
| 982 |
|
| 983 |
/* get the last report added */ |
| 984 |
getLastReport() |
| 985 |
{ |
| 986 |
local cnt = reports_.length(); |
| 987 |
return (cnt == 0 ? nil : reports_[cnt]); |
| 988 |
} |
| 989 |
|
| 990 |
/* delete the last report added */ |
| 991 |
deleteLastReport() |
| 992 |
{ |
| 993 |
local cnt = reports_.length(); |
| 994 |
if (cnt != 0) |
| 995 |
reports_.removeElementAt(cnt); |
| 996 |
} |
| 997 |
|
| 998 |
/* |
| 999 |
* Add a marker report. This adds a marker to the report stream, |
| 1000 |
* and returns the marker object. The marker doesn't show any |
| 1001 |
* message in the final display, but callers can use a pair of |
| 1002 |
* markers to identify a range of reports for later reordering or |
| 1003 |
* removal. |
| 1004 |
*/ |
| 1005 |
addMarker() |
| 1006 |
{ |
| 1007 |
/* create the new report */ |
| 1008 |
local marker = new MarkerReport(); |
| 1009 |
|
| 1010 |
/* add it to the stream */ |
| 1011 |
addReport(marker); |
| 1012 |
|
| 1013 |
/* return the new report */ |
| 1014 |
return marker; |
| 1015 |
} |
| 1016 |
|
| 1017 |
/* delete the reports between two markers */ |
| 1018 |
deleteRange(marker1, marker2) |
| 1019 |
{ |
| 1020 |
local idx1, idx2; |
| 1021 |
|
| 1022 |
/* find the indices of the two markers */ |
| 1023 |
idx1 = reports_.indexOf(marker1); |
| 1024 |
idx2 = reports_.indexOf(marker2); |
| 1025 |
|
| 1026 |
/* if we found both, delete the range */ |
| 1027 |
if (idx1 != nil && idx2 != nil) |
| 1028 |
reports_.removeRange(idx1, idx2); |
| 1029 |
} |
| 1030 |
|
| 1031 |
/* |
| 1032 |
* Pull out the reports between two markers, and reinsert them at |
| 1033 |
* the end of the transcript. |
| 1034 |
*/ |
| 1035 |
moveRangeAppend(marker1, marker2) |
| 1036 |
{ |
| 1037 |
local idx1, idx2; |
| 1038 |
|
| 1039 |
/* find the indices of the two markers */ |
| 1040 |
idx1 = reports_.indexOf(marker1); |
| 1041 |
idx2 = reports_.indexOf(marker2); |
| 1042 |
|
| 1043 |
/* if we didn't find both, ignore the request */ |
| 1044 |
if (idx1 == nil || idx2 == nil) |
| 1045 |
return; |
| 1046 |
|
| 1047 |
/* append each item in the range to the end of the report list */ |
| 1048 |
for (local i = idx1 ; i <= idx2 ; ++i) |
| 1049 |
reports_.append(reports_[i]); |
| 1050 |
|
| 1051 |
/* delete the original copies */ |
| 1052 |
reports_.removeRange(idx1, idx2); |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* |
| 1056 |
* Perform a callback on all of the reports in the transcript. |
| 1057 |
* We'll invoke the given callback function func(rpt) once for each |
| 1058 |
* report, with the report object as the parameter. |
| 1059 |
*/ |
| 1060 |
forEachReport(func) { reports_.forEach(func); } |
| 1061 |
|
| 1062 |
/* |
| 1063 |
* End the description section of the report. This adds a marker |
| 1064 |
* report that indicates that anything following (and part of the |
| 1065 |
* same action) is no longer part of the description; this can be |
| 1066 |
* important when we apply the default description suppression |
| 1067 |
* transformation, because it tells us not to consider the |
| 1068 |
* non-descriptive messages following this marker when, for example, |
| 1069 |
* suppressing default descriptive messages. |
| 1070 |
*/ |
| 1071 |
endDescription() |
| 1072 |
{ |
| 1073 |
/* add an end-of-description report */ |
| 1074 |
addReport(new EndOfDescReport()); |
| 1075 |
} |
| 1076 |
|
| 1077 |
/* |
| 1078 |
* Announce that the action is implicit |
| 1079 |
*/ |
| 1080 |
announceImplicit(action, msgProp) |
| 1081 |
{ |
| 1082 |
/* |
| 1083 |
* If the actor performing the command is not in "player" mode, |
| 1084 |
* save an implicit action announcement; for NPC mode, we treat |
| 1085 |
* implicit command results like any other results, so we don't |
| 1086 |
* want a separate announcement. |
| 1087 |
*/ |
| 1088 |
if (gActor.impliedCommandMode() == ModePlayer) |
| 1089 |
{ |
| 1090 |
/* create the new report */ |
| 1091 |
local report = new ImplicitActionAnnouncement(action, msgProp); |
| 1092 |
|
| 1093 |
/* add it to the transcript */ |
| 1094 |
addReport(report); |
| 1095 |
|
| 1096 |
/* return it */ |
| 1097 |
return report; |
| 1098 |
} |
| 1099 |
else |
| 1100 |
{ |
| 1101 |
/* no need for a report */ |
| 1102 |
return nil; |
| 1103 |
} |
| 1104 |
} |
| 1105 |
|
| 1106 |
/* |
| 1107 |
* Announce a remapped action |
| 1108 |
*/ |
| 1109 |
announceRemappedAction() |
| 1110 |
{ |
| 1111 |
/* save a remapped-action announcement */ |
| 1112 |
addReport(new RemappedActionAnnouncement()); |
| 1113 |
} |
| 1114 |
|
| 1115 |
/* |
| 1116 |
* Announce one of a set of objects to a multi-object action. We'll |
| 1117 |
* record this announcement for display with our report list. |
| 1118 |
*/ |
| 1119 |
announceMultiActionObject(preCalcMsg, obj, whichObj) |
| 1120 |
{ |
| 1121 |
/* save a multi-action object announcement */ |
| 1122 |
addReport(new MultiObjectAnnouncement( |
| 1123 |
preCalcMsg, obj, whichObj, gAction)); |
| 1124 |
} |
| 1125 |
|
| 1126 |
/* |
| 1127 |
* Announce an object that was resolved with slight ambiguity. |
| 1128 |
*/ |
| 1129 |
announceAmbigActionObject(obj, whichObj) |
| 1130 |
{ |
| 1131 |
/* save an ambiguous object announcement */ |
| 1132 |
addReport(new AmbigObjectAnnouncement(obj, whichObj, gAction)); |
| 1133 |
} |
| 1134 |
|
| 1135 |
/* |
| 1136 |
* Announce a default object. |
| 1137 |
*/ |
| 1138 |
announceDefaultObject(obj, whichObj, action, allResolved) |
| 1139 |
{ |
| 1140 |
/* save the default object announcement */ |
| 1141 |
addReport(new DefaultObjectAnnouncement( |
| 1142 |
obj, whichObj, action, allResolved)); |
| 1143 |
} |
| 1144 |
|
| 1145 |
/* |
| 1146 |
* Add a command separator. |
| 1147 |
*/ |
| 1148 |
addCommandSep() |
| 1149 |
{ |
| 1150 |
/* add a command separator announcement */ |
| 1151 |
addReport(new CommandSepAnnouncement()); |
| 1152 |
} |
| 1153 |
|
| 1154 |
/* |
| 1155 |
* clear our reports |
| 1156 |
*/ |
| 1157 |
clearReports() |
| 1158 |
{ |
| 1159 |
/* forget all of the reports in the main list */ |
| 1160 |
if (reports_.length() != 0) |
| 1161 |
reports_.removeRange(1, reports_.length()); |
| 1162 |
} |
| 1163 |
|
| 1164 |
/* |
| 1165 |
* Can we show a given report? By default, we always return true, |
| 1166 |
* but subclasses might want to override this to suppress certain |
| 1167 |
* types of reports. |
| 1168 |
*/ |
| 1169 |
canShowReport(report) { return true; } |
| 1170 |
|
| 1171 |
/* |
| 1172 |
* Filter text. If we're active, we'll turn the text into a command |
| 1173 |
* report and add it to our report list, blocking the text from |
| 1174 |
* reaching the underlying stream; otherwise, we'll pass it through |
| 1175 |
* unchanged. |
| 1176 |
*/ |
| 1177 |
filterText(ostr, txt) |
| 1178 |
{ |
| 1179 |
/* if we're inactive, pass text through unchanged */ |
| 1180 |
if (!isActive) |
| 1181 |
return txt; |
| 1182 |
|
| 1183 |
/* |
| 1184 |
* If the current sense context doesn't allow any messages to be |
| 1185 |
* generated, block the generated text entirely. We want to |
| 1186 |
* block text or not according to the sense context in effect |
| 1187 |
* now; so we must note it now rather than wait until we |
| 1188 |
* actually display the report, since the context could be |
| 1189 |
* different by then. |
| 1190 |
*/ |
| 1191 |
if (senseContext.isBlocking) |
| 1192 |
return nil; |
| 1193 |
|
| 1194 |
/* add a main report to our list if the text is non-empty */ |
| 1195 |
if (txt != '') |
| 1196 |
addReport(new MainCommandReport(txt)); |
| 1197 |
|
| 1198 |
/* capture the text - send nothing to the underlying stream */ |
| 1199 |
return nil; |
| 1200 |
} |
| 1201 |
|
| 1202 |
/* apply transformations */ |
| 1203 |
applyTransforms() |
| 1204 |
{ |
| 1205 |
/* apply each defined transformation */ |
| 1206 |
foreach (local cur in transforms_) |
| 1207 |
cur.applyTransform(self, reports_); |
| 1208 |
} |
| 1209 |
|
| 1210 |
/* |
| 1211 |
* check to see if the current action has a report matching the given |
| 1212 |
* criteria |
| 1213 |
*/ |
| 1214 |
currentActionHasReport(func) |
| 1215 |
{ |
| 1216 |
/* check to see if we can find a matching report */ |
| 1217 |
return (findCurrentActionReport(func) != nil); |
| 1218 |
} |
| 1219 |
|
| 1220 |
/* find a report in the current action that matches the given criteria */ |
| 1221 |
findCurrentActionReport(func) |
| 1222 |
{ |
| 1223 |
/* |
| 1224 |
* Find an action that's part of the current iteration and which |
| 1225 |
* matches the given function's criteria. Return the first match |
| 1226 |
* we find. |
| 1227 |
*/ |
| 1228 |
return reports_.valWhich({x: x.iter_ == iter_ && (func)(x)}); |
| 1229 |
} |
| 1230 |
|
| 1231 |
/* |
| 1232 |
* iteration number - for an iterated top-level command, this helps |
| 1233 |
* us keep the results for a particular iteration grouped together |
| 1234 |
*/ |
| 1235 |
iter_ = 1 |
| 1236 |
|
| 1237 |
/* our vector of reports */ |
| 1238 |
reports_ = nil |
| 1239 |
|
| 1240 |
/* our list of transformations */ |
| 1241 |
transforms_ = [defaultReportTransform, implicitGroupTransform, |
| 1242 |
reportOrderTransform, complexMultiTransform] |
| 1243 |
; |
| 1244 |
|
| 1245 |
/* ------------------------------------------------------------------------ */ |
| 1246 |
/* |
| 1247 |
* Transcript Transform. |
| 1248 |
*/ |
| 1249 |
class TranscriptTransform: object |
| 1250 |
/* |
| 1251 |
* Apply our transform to the transcript vector. By default, we do |
| 1252 |
* nothing; each subclass must override this to manipulate the vector |
| 1253 |
* to make the change it wants to make. |
| 1254 |
*/ |
| 1255 |
applyTransform(trans, vec) { } |
| 1256 |
; |
| 1257 |
|
| 1258 |
/* ------------------------------------------------------------------------ */ |
| 1259 |
/* |
| 1260 |
* Transcript Transform: set before/main/after report order. We'll look |
| 1261 |
* for any before/after reports that are out of order with respect to |
| 1262 |
* their main reports, and move them into the appropriate positions. |
| 1263 |
*/ |
| 1264 |
reportOrderTransform: TranscriptTransform |
| 1265 |
applyTransform(trans, vec) |
| 1266 |
{ |
| 1267 |
/* scan for before/after reports */ |
| 1268 |
for (local i = 1, local len = vec.length() ; i <= len ; ++i) |
| 1269 |
{ |
| 1270 |
/* get this item */ |
| 1271 |
local cur = vec[i]; |
| 1272 |
|
| 1273 |
/* if this is a before/after report, consider moving it */ |
| 1274 |
if (cur.ofKind(FullCommandReport) && cur.seqNum != nil) |
| 1275 |
{ |
| 1276 |
local idx; |
| 1277 |
|
| 1278 |
/* |
| 1279 |
* This item cares about its sequencing, so it could be |
| 1280 |
* out of order with respect to other items from the same |
| 1281 |
* sequence. Find the first item with a higher sequence |
| 1282 |
* number from the same group, and make sure this item is |
| 1283 |
* before the first such item. |
| 1284 |
*/ |
| 1285 |
for (idx = 1 ; idx < i ; ++idx) |
| 1286 |
{ |
| 1287 |
local x; |
| 1288 |
|
| 1289 |
/* get this item */ |
| 1290 |
x = vec[idx]; |
| 1291 |
|
| 1292 |
/* if x should come after cur, we need to move cur */ |
| 1293 |
if (x.ofKind(FullCommandReport) |
| 1294 |
&& x.seqNum > cur.seqNum |
| 1295 |
&& x.isPartOf(cur)) |
| 1296 |
{ |
| 1297 |
/* remove cur and reinsert it before x */ |
| 1298 |
vec.removeElementAt(i); |
| 1299 |
vec.insertAt(idx, cur); |
| 1300 |
|
| 1301 |
/* adjust our scan index for the removal */ |
| 1302 |
--i; |
| 1303 |
|
| 1304 |
/* no need to look any further */ |
| 1305 |
break; |
| 1306 |
} |
| 1307 |
} |
| 1308 |
} |
| 1309 |
} |
| 1310 |
} |
| 1311 |
; |
| 1312 |
|
| 1313 |
/* ------------------------------------------------------------------------ */ |
| 1314 |
/* |
| 1315 |
* Transcript Transform: remove unnecessary default reports. We'll scan |
| 1316 |
* the transcript for default reports for actions which also have |
| 1317 |
* implicit announcements or non-default reports, and remove those |
| 1318 |
* default reports. We'll also remove default descriptive reports which |
| 1319 |
* also have non-default reports in the same action. |
| 1320 |
*/ |
| 1321 |
defaultReportTransform: TranscriptTransform |
| 1322 |
applyTransform(trans, vec) |
| 1323 |
{ |
| 1324 |
/* scan for default reports */ |
| 1325 |
for (local i = 1, local len = vec.length() ; i <= len ; ++i) |
| 1326 |
{ |
| 1327 |
local cur; |
| 1328 |
|
| 1329 |
/* get this item */ |
| 1330 |
cur = vec[i]; |
| 1331 |
|
| 1332 |
/* |
| 1333 |
* if this is a default report, check to see if we want to |
| 1334 |
* keep it |
| 1335 |
*/ |
| 1336 |
if (cur.ofKind(DefaultCommandReport)) |
| 1337 |
{ |
| 1338 |
/* |
| 1339 |
* check for a main report or an implicit announcement |
| 1340 |
* associated with the same action; if we find anything, |
| 1341 |
* we don't need to keep the default report |
| 1342 |
*/ |
| 1343 |
if (vec.indexWhich( |
| 1344 |
{x: (x != cur |
| 1345 |
&& cur.isPartOf(x) |
| 1346 |
&& (x.ofKind(FullCommandReport) |
| 1347 |
|| x.ofKind(ImplicitActionAnnouncement))) |
| 1348 |
}) != nil) |
| 1349 |
{ |
| 1350 |
/* we don't need this default report */ |
| 1351 |
vec.removeElementAt(i); |
| 1352 |
|
| 1353 |
/* adjust our scan index for the removal */ |
| 1354 |
--i; |
| 1355 |
--len; |
| 1356 |
} |
| 1357 |
} |
| 1358 |
|
| 1359 |
/* |
| 1360 |
* if this is a default descriptive report, check to see if |
| 1361 |
* we want to keep it |
| 1362 |
*/ |
| 1363 |
if (cur.ofKind(DefaultDescCommandReport)) |
| 1364 |
{ |
| 1365 |
local fullIdx; |
| 1366 |
|
| 1367 |
/* |
| 1368 |
* check for a main report associated with the same |
| 1369 |
* action |
| 1370 |
*/ |
| 1371 |
fullIdx = vec.indexWhich( |
| 1372 |
{x: (x != cur |
| 1373 |
&& cur.isPartOf(x) |
| 1374 |
&& x.ofKind(FullCommandReport))}); |
| 1375 |
|
| 1376 |
/* |
| 1377 |
* if we found another report, check to see if it comes |
| 1378 |
* before or after any 'end of description' for the same |
| 1379 |
* action |
| 1380 |
*/ |
| 1381 |
if (fullIdx != nil) |
| 1382 |
{ |
| 1383 |
local endIdx; |
| 1384 |
|
| 1385 |
/* find the 'end of description' report, if any */ |
| 1386 |
endIdx = vec.indexWhich( |
| 1387 |
{x: (x != cur |
| 1388 |
&& cur.isPartOf(x) |
| 1389 |
&& x.ofKind(EndOfDescReport))}); |
| 1390 |
|
| 1391 |
/* |
| 1392 |
* if we found a full report before the |
| 1393 |
* end-of-description report, then the full report is |
| 1394 |
* part of the description and thus should suppress |
| 1395 |
* the default report; otherwise, the description |
| 1396 |
* portion includes only the default report and the |
| 1397 |
* default report should thus remain |
| 1398 |
*/ |
| 1399 |
if (endIdx == nil || fullIdx < endIdx) |
| 1400 |
{ |
| 1401 |
/* don't keep the default descriptive report */ |
| 1402 |
vec.removeElementAt(i); |
| 1403 |
|
| 1404 |
/* adjust our indices for the removal */ |
| 1405 |
--i; |
| 1406 |
--len; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
} |
| 1410 |
} |
| 1411 |
} |
| 1412 |
; |
| 1413 |
|
| 1414 |
/* ------------------------------------------------------------------------ */ |
| 1415 |
/* |
| 1416 |
* Transcript Transform: group implicit announcements. We'll find any |
| 1417 |
* runs of consecutive implicit command announcements, and group each run |
| 1418 |
* into a single announcement listing all of the implied actions. For |
| 1419 |
* example, we'll turn this: |
| 1420 |
* |
| 1421 |
*. >go south |
| 1422 |
*. (first opening the door) |
| 1423 |
*. (first unlocking the door) |
| 1424 |
* |
| 1425 |
* this into: |
| 1426 |
* |
| 1427 |
*. >go south |
| 1428 |
*. (first opening the door and unlocking the door) |
| 1429 |
* |
| 1430 |
* In addition, if we find an implicit announcement in the middle of a |
| 1431 |
* set of regular command reports, and it's for an action nested within |
| 1432 |
* the action generating the regular reports, we'll start a new paragraph |
| 1433 |
* before the implicit announcement. |
| 1434 |
*/ |
| 1435 |
implicitGroupTransform: TranscriptTransform |
| 1436 |
applyTransform(trans, vec) |
| 1437 |
{ |
| 1438 |
/* |
| 1439 |
* Scan for implicit announcements whose actions failed, and mark |
| 1440 |
* the implicit actions as such. This allows us to phrase the |
| 1441 |
* implicit announcements as attempts rather than as actual |
| 1442 |
* actions, which sounds a little better because it doesn't clash |
| 1443 |
* with the failure report that immediately follows. |
| 1444 |
*/ |
| 1445 |
for (local i = 1, local len = vec.length() ; i <= len ; ++i) |
| 1446 |
{ |
| 1447 |
local sub; |
| 1448 |
|
| 1449 |
/* get this item */ |
| 1450 |
local cur = vec[i]; |
| 1451 |
|
| 1452 |
/* |
| 1453 |
* If this is an implicit action announcement, and its |
| 1454 |
* corresponding action (or any nested action) failed, mark |
| 1455 |
* the implicit announcement as a mere attempt. Likewise, if |
| 1456 |
* we're interrupting the action for interactive input, it's |
| 1457 |
* likewise just an incomplete attempt. |
| 1458 |
*/ |
| 1459 |
if (cur.ofKind(ImplicitActionAnnouncement) |
| 1460 |
&& (sub = vec.valWhich( |
| 1461 |
{x: ((x.isFailure || x.isQuestion) |
| 1462 |
&& (x.isPartOf(cur) |
| 1463 |
|| x.isActionNestedIn(cur)))})) != nil) |
| 1464 |
{ |
| 1465 |
/* |
| 1466 |
* it's either a failed attempt or an interruption for a |
| 1467 |
* question - note which one |
| 1468 |
*/ |
| 1469 |
if (sub.isFailure) |
| 1470 |
cur.noteJustTrying(); |
| 1471 |
else |
| 1472 |
cur.noteQuestion(); |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
/* |
| 1477 |
* Scan for implicit announcement groups. Since we're only |
| 1478 |
* scanning for runs of two or more announcements, we can stop |
| 1479 |
* scanning one short of the end of the list - there's no need to |
| 1480 |
* check the last item because it can't possibly be followed by |
| 1481 |
* another item. Thus, scan while i < len. |
| 1482 |
*/ |
| 1483 |
for (local i = 1, local len = vec.length() ; i < len ; ++i) |
| 1484 |
{ |
| 1485 |
local origI = i; |
| 1486 |
|
| 1487 |
/* get this item */ |
| 1488 |
local cur = vec[i]; |
| 1489 |
|
| 1490 |
/* |
| 1491 |
* If it's an implied action announcement, and the next one |
| 1492 |
* qualifies for group inclusion, build a group. Note that |
| 1493 |
* because we only loop until we reach the second-to-last |
| 1494 |
* item, we know for sure there is indeed a next item to |
| 1495 |
* index here. |
| 1496 |
*/ |
| 1497 |
if (cur.ofKind(ImplicitActionAnnouncement) |
| 1498 |
&& canGroupWith(cur, vec[i+1])) |
| 1499 |
{ |
| 1500 |
local j; |
| 1501 |
local groupVec; |
| 1502 |
|
| 1503 |
/* create a vector to hold the re-sorted group listing */ |
| 1504 |
groupVec = new Vector(16); |
| 1505 |
|
| 1506 |
/* |
| 1507 |
* Scan items for grouping. This time, we want to scan |
| 1508 |
* to the last (not second-to-last) item in the main |
| 1509 |
* list, since we could conceivably group everything |
| 1510 |
* remaining. |
| 1511 |
*/ |
| 1512 |
for (j = i ; j <= len ; ) |
| 1513 |
{ |
| 1514 |
/* get this item */ |
| 1515 |
cur = vec[j]; |
| 1516 |
|
| 1517 |
/* unstack any recursive grouping */ |
| 1518 |
j = unstackRecursiveGroup(groupVec, vec, j); |
| 1519 |
|
| 1520 |
/* |
| 1521 |
* if we've used now everything in the list, or the |
| 1522 |
* next item can't be grouped with the current item, |
| 1523 |
* we're done |
| 1524 |
*/ |
| 1525 |
if (j > len || !canGroupWith(cur, vec[j])) |
| 1526 |
break; |
| 1527 |
} |
| 1528 |
|
| 1529 |
/* process default object announcements */ |
| 1530 |
processDefaultAnnouncements(groupVec); |
| 1531 |
|
| 1532 |
/* build the composite message for the entire group */ |
| 1533 |
vec[i].messageText_ = implicitAnnouncementGrouper |
| 1534 |
.compositeMessage(groupVec); |
| 1535 |
|
| 1536 |
/* |
| 1537 |
* Clear the messages in the second through last grouped |
| 1538 |
* announcements. Leave the report objects themselves |
| 1539 |
* intact, so that our internal structural record of the |
| 1540 |
* transcript remains as it was, but make them silent in |
| 1541 |
* the displayed text, since these messages are now |
| 1542 |
* subsumed into the combined first message. |
| 1543 |
*/ |
| 1544 |
for (++i ; i < j ; ++i) |
| 1545 |
vec[i].messageText_ = ''; |
| 1546 |
|
| 1547 |
/* |
| 1548 |
* continue the main loop from the next element after the |
| 1549 |
* last one we included in the group |
| 1550 |
*/ |
| 1551 |
i = j - 1; |
| 1552 |
} |
| 1553 |
|
| 1554 |
/* |
| 1555 |
* If this is an implied action or default object |
| 1556 |
* announcement that interrupts a set of regular command |
| 1557 |
* reports, and it's for an action nested within the action |
| 1558 |
* generating the reports, add a paragraph spacer before the |
| 1559 |
* implicit announcement. |
| 1560 |
*/ |
| 1561 |
if ((cur.ofKind(ImplicitActionAnnouncement) |
| 1562 |
|| cur.ofKind(DefaultObjectAnnouncement)) |
| 1563 |
&& cur.messageText_ != '') |
| 1564 |
{ |
| 1565 |
local j; |
| 1566 |
|
| 1567 |
/* scan back for the nearest announcement with text */ |
| 1568 |
for (j = origI - 1 ; j >= 1 && vec[j].messageText_ == '' ; |
| 1569 |
--j) ; |
| 1570 |
|
| 1571 |
/* |
| 1572 |
* if it's a regular command report, and our implied or |
| 1573 |
* default announcement is nested within it, add a |
| 1574 |
* paragraph spacer |
| 1575 |
*/ |
| 1576 |
if (j >= 1 |
| 1577 |
&& vec[j].ofKind(FullCommandReport) |
| 1578 |
&& cur.isActionNestedIn(vec[j])) |
| 1579 |
{ |
| 1580 |
/* |
| 1581 |
* insert a paragraph spacer before the announcement |
| 1582 |
* - this will make the implied action and its |
| 1583 |
* results stand out as separate actions, rather than |
| 1584 |
* running everything together without spacing |
| 1585 |
*/ |
| 1586 |
vec.insertAt(origI, new GroupSeparatorMessage(cur)); |
| 1587 |
|
| 1588 |
/* adjust our indices for the insertion */ |
| 1589 |
++i; |
| 1590 |
++len; |
| 1591 |
} |
| 1592 |
} |
| 1593 |
} |
| 1594 |
} |
| 1595 |
|
| 1596 |
/* |
| 1597 |
* "Unstack" a recursive group of nested announcements. Adds the |
| 1598 |
* recursive group to the output group vector in chronological order, |
| 1599 |
* and returns the index of the next item after the recursive group. |
| 1600 |
* |
| 1601 |
* A recursive group is a set of nested implicit commands, where one |
| 1602 |
* implicit command triggered another, which triggered another, and |
| 1603 |
* so on. The innermost of the nested set is the one that's actually |
| 1604 |
* executed first chronologically, since an implied command must be |
| 1605 |
* carried out before its enclosing command can proceed. For |
| 1606 |
* example: |
| 1607 |
* |
| 1608 |
*. >go south |
| 1609 |
*. (first opening the door) |
| 1610 |
*. (first unlocking the door) |
| 1611 |
*. (first taking the key out of the bag) |
| 1612 |
* |
| 1613 |
* Going south implies opening the door, but before we can open the |
| 1614 |
* door, we must unlock it, and before we can unlock it we must be |
| 1615 |
* holding the key. In report order, the innermost command is listed |
| 1616 |
* last, since it's nested within the enclosing commands. |
| 1617 |
* Chronologically, though, the innermost command is actually |
| 1618 |
* executed first. The purpose of this routine is to unstack these |
| 1619 |
* nested sets, rearranging them into chronological order. |
| 1620 |
*/ |
| 1621 |
unstackRecursiveGroup(groupVec, vec, idx) |
| 1622 |
{ |
| 1623 |
local cur; |
| 1624 |
|
| 1625 |
/* remember the item we're tasked to work on */ |
| 1626 |
cur = vec[idx]; |
| 1627 |
|
| 1628 |
/* skip the current item */ |
| 1629 |
++idx; |
| 1630 |
|
| 1631 |
/* |
| 1632 |
* Scan for items nested within vec[idx]. Process each child |
| 1633 |
* item first. An item is nested within us if can be grouped |
| 1634 |
* with us, and its action is a child of our action. |
| 1635 |
*/ |
| 1636 |
for (local len = vec.length() ; idx <= len ; ) |
| 1637 |
{ |
| 1638 |
/* if the next item is nested within 'cur', process it */ |
| 1639 |
if (canGroupWith(cur, vec[idx]) |
| 1640 |
&& vec[idx].getAction().isNestedIn(cur.getAction())) |
| 1641 |
{ |
| 1642 |
/* |
| 1643 |
* It's nested with us - process it recursively. Since |
| 1644 |
* our goal is to unstack these reports into |
| 1645 |
* chronological order, we must process our children |
| 1646 |
* first, so that they get added to the group vector |
| 1647 |
* first, since children chronologically predede their |
| 1648 |
* parents. |
| 1649 |
*/ |
| 1650 |
idx = unstackRecursiveGroup(groupVec, vec, idx); |
| 1651 |
} |
| 1652 |
else |
| 1653 |
{ |
| 1654 |
/* it's not nested within us, so we're done */ |
| 1655 |
break; |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
/* add our item to the result vector */ |
| 1660 |
groupVec.append(cur); |
| 1661 |
|
| 1662 |
/* |
| 1663 |
* return the index of the next item; this is simply the current |
| 1664 |
* 'idx' value, since we've advanced it past each item we've |
| 1665 |
* processed |
| 1666 |
*/ |
| 1667 |
return idx; |
| 1668 |
} |
| 1669 |
|
| 1670 |
/* |
| 1671 |
* Process default object announcements in a grouped message vector. |
| 1672 |
* |
| 1673 |
* Default object announcements come in two flavors: with and without |
| 1674 |
* message text. Those without message text are present purely to |
| 1675 |
* retain a structural record of the default object in the internal |
| 1676 |
* transcript; we can simply remove these, since the actions that |
| 1677 |
* created them didn't even want default messages. For those that do |
| 1678 |
* include message text, remove them as well, but also use their |
| 1679 |
* actions to replace the corresponding parent actions, so that the |
| 1680 |
* parent actions reflect what actually happened with the final |
| 1681 |
* defaulted objects. |
| 1682 |
*/ |
| 1683 |
processDefaultAnnouncements(vec) |
| 1684 |
{ |
| 1685 |
/* scan the vector for default announcements */ |
| 1686 |
for (local i = 1, local len = vec.length() ; i <= len ; ++i) |
| 1687 |
{ |
| 1688 |
local cur = vec[i]; |
| 1689 |
|
| 1690 |
/* if this is a default announcement, process it */ |
| 1691 |
if (cur.ofKind(DefaultObjectAnnouncement)) |
| 1692 |
{ |
| 1693 |
/* |
| 1694 |
* If it has a message, use its action to replace the |
| 1695 |
* parent action. The only way an implied command can |
| 1696 |
* have a defaulted object is for the implied command to |
| 1697 |
* have been stated with too few objects, so that an |
| 1698 |
* askForIobj (for example) occurred. In such cases, the |
| 1699 |
* default announcement will be a child action of the |
| 1700 |
* original underspecified action, so we can simply find |
| 1701 |
* the original action and replace it with the defaulted |
| 1702 |
* action. |
| 1703 |
*/ |
| 1704 |
if (cur.messageText_ != '') |
| 1705 |
{ |
| 1706 |
/* |
| 1707 |
* Scan for the parent announcement. |
| 1708 |
* |
| 1709 |
* Note that the implicit announcement containing the |
| 1710 |
* parent action will follow the default announcement |
| 1711 |
* in the result list, since the default announcement |
| 1712 |
* is a child of the parent. |
| 1713 |
*/ |
| 1714 |
for (local j = i + 1 ; j <= len ; ++j) |
| 1715 |
{ |
| 1716 |
/* if this is the parent action, replace it */ |
| 1717 |
if (vec[j].getAction() |
| 1718 |
== cur.getAction().parentAction) |
| 1719 |
{ |
| 1720 |
/* this is it - replace the action */ |
| 1721 |
vec[j].setAction(cur.getAction()); |
| 1722 |
|
| 1723 |
/* no need to look any further */ |
| 1724 |
break; |
| 1725 |
} |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
/* remove the default announcement from the list */ |
| 1730 |
vec.removeElementAt(i); |
| 1731 |
|
| 1732 |
/* adjust our list index and length for the deletion */ |
| 1733 |
--i; |
| 1734 |
--len; |
| 1735 |
} |
| 1736 |
} |
| 1737 |
} |
| 1738 |
|
| 1739 |
/* |
| 1740 |
* Can we group the second item with the first? Returns true if the |
| 1741 |
* second item is also an implicit action announcement, or it's a |
| 1742 |
* default object announcement whose parent action is the first |
| 1743 |
* item's action. |
| 1744 |
*/ |
| 1745 |
canGroupWith(a, b) |
| 1746 |
{ |
| 1747 |
/* if 'b' is also an implicit announcement, we can include it */ |
| 1748 |
if (b.ofKind(ImplicitActionAnnouncement)) |
| 1749 |
return true; |
| 1750 |
|
| 1751 |
/* |
| 1752 |
* if 'b' is a default object announcement, and has the same |
| 1753 |
* parent action as 'a', then we can group it; otherwise we can't |
| 1754 |
*/ |
| 1755 |
return (b.ofKind(DefaultObjectAnnouncement) |
| 1756 |
&& b.getAction().parentAction == a.getAction()); |
| 1757 |
} |
| 1758 |
; |
| 1759 |
|
| 1760 |
/* ------------------------------------------------------------------------ */ |
| 1761 |
/* |
| 1762 |
* Transcript Transform: Complex Multi-object Separation. If we have an |
| 1763 |
* action that's being applied to one of a bunch of iterated objects, and |
| 1764 |
* the action has any implied command announcements associated with it, |
| 1765 |
* we'll set off the result for this command from its preceding and |
| 1766 |
* following commands by a paragraph separator. |
| 1767 |
*/ |
| 1768 |
complexMultiTransform: TranscriptTransform |
| 1769 |
applyTransform(trans, vec) |
| 1770 |
{ |
| 1771 |
/* scan the list for multi-object announcements */ |
| 1772 |
foreach (local cur in vec) |
| 1773 |
{ |
| 1774 |
/* if it's a multi-object announcement, check it */ |
| 1775 |
if (cur.ofKind(MultiObjectAnnouncement)) |
| 1776 |
{ |
| 1777 |
local idx; |
| 1778 |
local cnt; |
| 1779 |
local sep; |
| 1780 |
|
| 1781 |
/* |
| 1782 |
* We have a multi-object announcement. If we find only |
| 1783 |
* one other report within the group, and the report's |
| 1784 |
* text is short, let this report run together with its |
| 1785 |
* neighbors without any additional visual separation. |
| 1786 |
* Otherwise, set this group apart from its neighbors by |
| 1787 |
* adding a paragraph break before and after the group; |
| 1788 |
* this will make the results easier to read by visually |
| 1789 |
* separating each longish response as a separate |
| 1790 |
* paragraph. |
| 1791 |
* |
| 1792 |
* First, find the current item's index. |
| 1793 |
*/ |
| 1794 |
idx = vec.indexWhich({x: x == cur}); |
| 1795 |
|
| 1796 |
/* |
| 1797 |
* now scan subsequent items in the same command |
| 1798 |
* iteration, and check to see if (1) we have more than |
| 1799 |
* one item, or (2) the item has a longish message |
| 1800 |
*/ |
| 1801 |
for (cnt = 0, ++idx, sep = nil ; |
| 1802 |
idx <= vec.length() && cnt < 2 ; ++idx, ++cnt) |
| 1803 |
{ |
| 1804 |
local sub = vec[idx]; |
| 1805 |
|
| 1806 |
/* if we've reached the end of the group, stop scanning */ |
| 1807 |
if (sub.iter_ != cur.iter_) |
| 1808 |
break; |
| 1809 |
|
| 1810 |
/* |
| 1811 |
* If it has long text, add visual separation. Note |
| 1812 |
* that "long" is just a heuristic, because we can't |
| 1813 |
* tell whether the text will wrap in any given |
| 1814 |
* interpreter - that depends on the width of the |
| 1815 |
* interpreter window and the font size, among other |
| 1816 |
* things, and we have no way of knowing any of this |
| 1817 |
* here. |
| 1818 |
*/ |
| 1819 |
if (sub.ofKind(CommandReportMessage) |
| 1820 |
&& sub.messageText_ != nil |
| 1821 |
&& sub.messageText_.length() > 60) |
| 1822 |
{ |
| 1823 |
/* it's long - add separation */ |
| 1824 |
sep = true; |
| 1825 |
break; |
| 1826 |
} |
| 1827 |
} |
| 1828 |
|
| 1829 |
/* if we need separation, add it now */ |
| 1830 |
if (sep || cnt > 1) |
| 1831 |
{ |
| 1832 |
/* |
| 1833 |
* This is indeed a complex iterated item. Set it |
| 1834 |
* off by paragraph breaks before and after the |
| 1835 |
* iteration. |
| 1836 |
* |
| 1837 |
* First, find the first item in this iteration. If |
| 1838 |
* it's not the first item in the whole transcript, |
| 1839 |
* insert a separator before it. |
| 1840 |
*/ |
| 1841 |
idx = vec.indexWhich({x: x.iter_ == cur.iter_}); |
| 1842 |
if (idx != 1) |
| 1843 |
vec.insertAt(idx, new GroupSeparatorMessage(cur)); |
| 1844 |
|
| 1845 |
/* |
| 1846 |
* Next, find the last item in this iteration. If |
| 1847 |
* it's no the last item in the entire transcript, |
| 1848 |
* add a separator after it. |
| 1849 |
*/ |
| 1850 |
idx = vec.lastIndexWhich({x: x.iter_ == cur.iter_}); |
| 1851 |
if (idx != vec.length()) |
| 1852 |
vec.insertAt(idx + 1, new GroupSeparatorMessage(cur)); |
| 1853 |
} |
| 1854 |
} |
| 1855 |
|
| 1856 |
/* |
| 1857 |
* if it's a command result from an implied command, and we |
| 1858 |
* have another command result following from the enclosing |
| 1859 |
* command, add a separator between this result and the next |
| 1860 |
* result |
| 1861 |
*/ |
| 1862 |
if (cur.ofKind(CommandReportMessage) && cur.isActionImplicit) |
| 1863 |
{ |
| 1864 |
local idx; |
| 1865 |
|
| 1866 |
/* get the index of this element */ |
| 1867 |
idx = vec.indexOf(cur); |
| 1868 |
|
| 1869 |
/* |
| 1870 |
* if there's another element following, check to see if |
| 1871 |
* it's a command report for an enclosing action (i.e., |
| 1872 |
* an action that initiated this implied action) |
| 1873 |
*/ |
| 1874 |
if (idx < vec.length()) |
| 1875 |
{ |
| 1876 |
local nxt; |
| 1877 |
|
| 1878 |
/* get the next element */ |
| 1879 |
nxt = vec[idx + 1]; |
| 1880 |
|
| 1881 |
/* |
| 1882 |
* if it's a command report for an action that |
| 1883 |
* encloses this action, or it's another implicit |
| 1884 |
* announcement, then put a separator before it |
| 1885 |
*/ |
| 1886 |
if ((nxt.ofKind(CommandReportMessage) |
| 1887 |
&& nxt.getAction() != cur.getAction() |
| 1888 |
&& cur.isActionNestedIn(nxt)) |
| 1889 |
|| nxt.ofKind(ImplicitActionAnnouncement)) |
| 1890 |
|
| 1891 |
{ |
| 1892 |
/* add a separator */ |
| 1893 |
vec.insertAt(idx + 1, |
| 1894 |
new InternalSeparatorMessage(cur)); |
| 1895 |
} |
| 1896 |
} |
| 1897 |
} |
| 1898 |
} |
| 1899 |
} |
| 1900 |
; |
| 1901 |
|
| 1902 |
/* ------------------------------------------------------------------------ */ |
| 1903 |
/* |
| 1904 |
* Invoke a callback function using a transcript of the given class. |
| 1905 |
* Returns the return value of the callback function. |
| 1906 |
*/ |
| 1907 |
withCommandTranscript(transcriptClass, func) |
| 1908 |
{ |
| 1909 |
local transcript; |
| 1910 |
local oldTranscript; |
| 1911 |
|
| 1912 |
/* |
| 1913 |
* if we already have an active transcript, just invoke the |
| 1914 |
* function, running everything through the existing active |
| 1915 |
* transcript |
| 1916 |
*/ |
| 1917 |
if (gTranscript != nil && gTranscript.isActive) |
| 1918 |
{ |
| 1919 |
/* invoke the callback and return the result */ |
| 1920 |
return (func)(); |
| 1921 |
} |
| 1922 |
|
| 1923 |
/* |
| 1924 |
* Create a transcript of the given class. Make the transcript |
| 1925 |
* transient, since it's effectively part of the output stream state |
| 1926 |
* and thus shouldn't be saved or undone. |
| 1927 |
*/ |
| 1928 |
transcript = transcriptClass.createTransientInstance(); |
| 1929 |
|
| 1930 |
/* make this transcript the current global transcript */ |
| 1931 |
oldTranscript = gTranscript; |
| 1932 |
gTranscript = transcript; |
| 1933 |
|
| 1934 |
/* install the transcript as a filter on the main output stream */ |
| 1935 |
mainOutputStream.addOutputFilter(transcript); |
| 1936 |
|
| 1937 |
/* make sure we undo our global changes before we leave */ |
| 1938 |
try |
| 1939 |
{ |
| 1940 |
/* invoke the callback and return the result */ |
| 1941 |
return (func)(); |
| 1942 |
} |
| 1943 |
finally |
| 1944 |
{ |
| 1945 |
/* uninstall the transcript output filter */ |
| 1946 |
mainOutputStream.removeOutputFilter(transcript); |
| 1947 |
|
| 1948 |
/* restore the previous global transcript */ |
| 1949 |
gTranscript = oldTranscript; |
| 1950 |
|
| 1951 |
/* show the transcript results */ |
| 1952 |
transcript.showReports(true); |
| 1953 |
} |
| 1954 |
} |
| 1955 |
|