| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library - miscellaneous definitions |
| 7 |
* |
| 8 |
* This module contains miscellaneous definitions that don't have a |
| 9 |
* natural grouping with any larger modules, and which aren't complex |
| 10 |
* enough to justify modules of their own. |
| 11 |
*/ |
| 12 |
|
| 13 |
/* include the library header */ |
| 14 |
#include "adv3.h" |
| 15 |
|
| 16 |
|
| 17 |
/* ------------------------------------------------------------------------ */ |
| 18 |
/* |
| 19 |
* When a call is made to a property not defined or inherited by the |
| 20 |
* target object, the system will automatically invoke this method. The |
| 21 |
* method will be invoked with a property pointer as its first argument, |
| 22 |
* and the original arguments as the remaining arguments. The first |
| 23 |
* argument gives the property that was invoked and not defined by the |
| 24 |
* object. A typical definition in an object would look like this: |
| 25 |
* |
| 26 |
* propNotDefined(prop, [args]) { ... } |
| 27 |
* |
| 28 |
* If this method is not defined by the object, the system simply |
| 29 |
* returns nil as the value of the undefined property evaluation or |
| 30 |
* method invocation. |
| 31 |
*/ |
| 32 |
property propNotDefined; |
| 33 |
export propNotDefined; |
| 34 |
|
| 35 |
|
| 36 |
/* ------------------------------------------------------------------------ */ |
| 37 |
/* |
| 38 |
* We refer to some properties defined primarily in score.t - that's an |
| 39 |
* optional module, though, so make sure the compiler has heard of these. |
| 40 |
*/ |
| 41 |
property calcMaxScore, runScoreNotifier; |
| 42 |
|
| 43 |
|
| 44 |
/* ------------------------------------------------------------------------ */ |
| 45 |
/* |
| 46 |
* The library base class for the gameMain object. |
| 47 |
* |
| 48 |
* Each game MUST define an object called 'gameMain' to define how the |
| 49 |
* game starts up. You can use GameMainDef as the base class of your |
| 50 |
* 'gameMain' object, in which case the only thing you're required to |
| 51 |
* specify in your object is the 'initialPlayerChar' property - you can |
| 52 |
* inherit everything else from the GameMainDef class if you don't |
| 53 |
* require any further customizations. |
| 54 |
*/ |
| 55 |
class GameMainDef: object |
| 56 |
/* |
| 57 |
* The initial player character. Each game's 'gameMain' object MUST |
| 58 |
* define this to refer to the Actor object that serves as the |
| 59 |
* initial player character. |
| 60 |
*/ |
| 61 |
initialPlayerChar = nil |
| 62 |
|
| 63 |
/* |
| 64 |
* Show the game's introduction. This routine is called by the |
| 65 |
* default newGame() just before entering the main command loop. The |
| 66 |
* command loop starts off by showing the initial room description, |
| 67 |
* so there's no need to do that here. |
| 68 |
* |
| 69 |
* Most games will want to override this, to show a prologue message |
| 70 |
* setting up the game's initial situation for the player. We don't |
| 71 |
* show anything by default. |
| 72 |
*/ |
| 73 |
showIntro() { } |
| 74 |
|
| 75 |
/* |
| 76 |
* Show the "goodbye" message. This is called after the main command |
| 77 |
* loop terminates. |
| 78 |
* |
| 79 |
* We don't show anything by default. If you want to show a "thanks |
| 80 |
* for playing" type of message as the game exits, override this |
| 81 |
* routine with the desired text. |
| 82 |
*/ |
| 83 |
showGoodbye() { } |
| 84 |
|
| 85 |
/* |
| 86 |
* Begin a new game. This default implementation shows the |
| 87 |
* introductory message, calls the main command loop, and finally |
| 88 |
* shows the goodbye message. |
| 89 |
* |
| 90 |
* You can override this routine if you want to customize the startup |
| 91 |
* protocol. For example, if you want to create a pre-game options |
| 92 |
* menu, you could override this routine to show the list of options |
| 93 |
* and process the user's input. If you need only to customize the |
| 94 |
* introduction and goodbye messages, you can simply override |
| 95 |
* showIntro() and showGoodbye() instead. |
| 96 |
*/ |
| 97 |
newGame() |
| 98 |
{ |
| 99 |
/* |
| 100 |
* Show the statusline before we display our introductory. This |
| 101 |
* will help minimize redrawing - if we waited until after |
| 102 |
* displaying some text, we might have to redraw some of the |
| 103 |
* screen to rearrange things for the new screen area taken up by |
| 104 |
* the status line, which could be visible to the user. By |
| 105 |
* setting up the status line first, we'll probably have less to |
| 106 |
* redraw because we won't have anything on the screen yet when |
| 107 |
* figuring the layout. |
| 108 |
*/ |
| 109 |
statusLine.showStatusLine(); |
| 110 |
|
| 111 |
/* show the introduction */ |
| 112 |
showIntro(); |
| 113 |
|
| 114 |
/* run the game, showing the initial location's full description */ |
| 115 |
runGame(true); |
| 116 |
|
| 117 |
/* show the end-of-game message */ |
| 118 |
showGoodbye(); |
| 119 |
} |
| 120 |
|
| 121 |
/* |
| 122 |
* Restore a game and start it running. This is invoked when the |
| 123 |
* user launches the interpreter using a saved game file; for |
| 124 |
* example, on a Macintosh, this happens when the user double-clicks |
| 125 |
* on a saved game file on the desktop. |
| 126 |
* |
| 127 |
* This default implementation bypasses any normal introduction |
| 128 |
* messages: we simply restore the game file if possible, and |
| 129 |
* immediately start the game's main command loop. Most games won't |
| 130 |
* need to override this, but you can if you need some special effect |
| 131 |
* in the restore-at-startup case. |
| 132 |
*/ |
| 133 |
restoreAndRunGame(filename) |
| 134 |
{ |
| 135 |
local succ; |
| 136 |
|
| 137 |
/* mention that we're about to restore the saved position */ |
| 138 |
gLibMessages.noteMainRestore(); |
| 139 |
|
| 140 |
/* try restoring it */ |
| 141 |
succ = RestoreAction.startupRestore(filename); |
| 142 |
|
| 143 |
/* show a blank line after the restore result message */ |
| 144 |
"<.p>"; |
| 145 |
|
| 146 |
/* if we were successful, run the game */ |
| 147 |
if (succ) |
| 148 |
{ |
| 149 |
/* |
| 150 |
* Run the command loop. There's no need to show the room |
| 151 |
* description, since the RESTORE action will have already |
| 152 |
* done so. |
| 153 |
*/ |
| 154 |
runGame(nil); |
| 155 |
|
| 156 |
/* show the end-of-game message */ |
| 157 |
showGoodbye(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
* Set the interpreter window title, if applicable to the local |
| 163 |
* platform. This simply displays a <TITLE> tag to set the title to |
| 164 |
* the string found in the versionInfo object. |
| 165 |
*/ |
| 166 |
setGameTitle() |
| 167 |
{ |
| 168 |
/* write the <TITLE> tag with the game's name */ |
| 169 |
"<title><<versionInfo.name>></title>"; |
| 170 |
} |
| 171 |
|
| 172 |
/* |
| 173 |
* Set up the HTML-mode about-box. By default, this does nothing. |
| 174 |
* Games can use this routine to show an <ABOUTBOX> tag, if desired, |
| 175 |
* to set up the contents of an about-box for HTML TADS platforms. |
| 176 |
* |
| 177 |
* Note that an <ABOUTBOX> tag must be re-initialized each time the |
| 178 |
* main game window is cleared, so this routine should be called |
| 179 |
* again after any call to clearScreen(). |
| 180 |
*/ |
| 181 |
setAboutBox() |
| 182 |
{ |
| 183 |
/* we don't show any about-box by default */ |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
* Build a saved game metadata table. This returns a LookupTable |
| 188 |
* containing string key/value pairs that are stored in saved game |
| 189 |
* files, providing descriptive information that can be displayed to |
| 190 |
* the user when browsing a collection of save files. This is called |
| 191 |
* each time we execute a SAVE command, so that we store the current |
| 192 |
* context of the game. |
| 193 |
* |
| 194 |
* Some interpreters display information from this table when |
| 195 |
* presenting the user with a list of files for RESTORE. The |
| 196 |
* contents of the table are intentionally open-ended to allow for |
| 197 |
* future extensions, but at the moment, the following keys are |
| 198 |
* specifically defined (note that capitalization must be exact): |
| 199 |
* |
| 200 |
* UserDesc - descriptive text entered by the user (this should |
| 201 |
* simply be the contents of the 'userDesc' parameter). This is |
| 202 |
* treated as ordinary plain text (i.e., no HTML or other markups are |
| 203 |
* interpreted in this text). |
| 204 |
* |
| 205 |
* AutoDesc - descriptive text generated by the game to describe the |
| 206 |
* saved position. This text can contain the simple HTML markups |
| 207 |
* <b>..</b>, <i>..</i>, and <br> for formatting. |
| 208 |
* |
| 209 |
* Return nil if you don't want to save any metadata information. |
| 210 |
* |
| 211 |
* 'userDesc' is an optional string entered by the user via the Save |
| 212 |
* Game dialog. Some interpreters let the user enter a description |
| 213 |
* for a saved game via the file selector dialog; the descriptive |
| 214 |
* text is separate from the filename, and is intended to let the |
| 215 |
* user enter a more free-form description than would be allowed in a |
| 216 |
* filename. This text, if any, is passed to use via the 'userDesc' |
| 217 |
* parameter. |
| 218 |
*/ |
| 219 |
getSaveDesc(userDesc) |
| 220 |
{ |
| 221 |
/* create the lookup table */ |
| 222 |
local t = new LookupTable(); |
| 223 |
|
| 224 |
/* store the user description, if provided */ |
| 225 |
if (userDesc != nil) |
| 226 |
t['UserDesc'] = userDesc; |
| 227 |
|
| 228 |
/* start our auto description with the current room name */ |
| 229 |
desc = gPlayerChar.getLookAroundName() + '; '; |
| 230 |
|
| 231 |
/* if we're keeping score, include the score */ |
| 232 |
if (libGlobal.scoreObj != nil) |
| 233 |
desc += toString(libGlobal.scoreObj.totalScore) + ' points in '; |
| 234 |
|
| 235 |
/* add the number of turns so far */ |
| 236 |
desc += toString(libGlobal.totalTurns) + ' moves'; |
| 237 |
|
| 238 |
/* add the auto description */ |
| 239 |
t['AutoDesc'] = desc; |
| 240 |
|
| 241 |
/* return the table */ |
| 242 |
return t; |
| 243 |
} |
| 244 |
|
| 245 |
/* |
| 246 |
* The gameMain object also specifies some settings that control |
| 247 |
* optional library behavior. If you want the standard library |
| 248 |
* behavior, you can just inherit the default settings from this |
| 249 |
* class. Some games might want to select non-default variations, |
| 250 |
* though. |
| 251 |
*/ |
| 252 |
|
| 253 |
/* |
| 254 |
* The maximum number of points possible in the game. If the game |
| 255 |
* includes the scoring module at all, and this is non-nil, the SCORE |
| 256 |
* and FULL SCORE commands will display this value to the player as a |
| 257 |
* rough indication of how much farther there is to go in the game. |
| 258 |
* |
| 259 |
* By default, we initialize this on demand, by calculating the sum |
| 260 |
* of the point values of the Achievement objects in the game. The |
| 261 |
* game can override this if needed to specify a specific maximum |
| 262 |
* possible score, rather than relying on the automatic calculation. |
| 263 |
*/ |
| 264 |
maxScore() |
| 265 |
{ |
| 266 |
local m; |
| 267 |
|
| 268 |
/* ask the score module (if any) to compute the maximum score */ |
| 269 |
m = (libGlobal.scoreObj != nil |
| 270 |
? libGlobal.scoreObj.calcMaxScore : nil); |
| 271 |
|
| 272 |
/* supersede this initializer with the calculated value */ |
| 273 |
maxScore = m; |
| 274 |
|
| 275 |
/* return the result */ |
| 276 |
return m; |
| 277 |
} |
| 278 |
|
| 279 |
/* |
| 280 |
* The score ranking list - this provides a list of names for |
| 281 |
* various score levels. If the game provides a non-nil list here, |
| 282 |
* the SCORE and FULL SCORE commands will show the rank along with |
| 283 |
* the score ("This makes you a Master Adventurer"). |
| 284 |
* |
| 285 |
* This is a list of score entries. Each score entry is itself a |
| 286 |
* list of two elements: the first element is the minimum score for |
| 287 |
* the rank, and the second is a string describing the rank. The |
| 288 |
* ranks should be given in ascending order, since we simply search |
| 289 |
* the list for the first item whose minimum score is greater than |
| 290 |
* our score, and use the preceding item. The first entry in the |
| 291 |
* list would normally have a minimum of zero points, since it |
| 292 |
* should give the initial, lowest rank. |
| 293 |
* |
| 294 |
* If this is set to nil, which it is by default, we'll simply skip |
| 295 |
* score ranks entirely. |
| 296 |
*/ |
| 297 |
scoreRankTable = nil |
| 298 |
|
| 299 |
/* |
| 300 |
* Verbose mode. If this is on, the full room description is |
| 301 |
* displayed each time the player enters a room, regardless of |
| 302 |
* whether or not the player has seen the room before; if this is |
| 303 |
* nil, the full description is only displayed on the player's first |
| 304 |
* entry to a room, and only the short description on re-entry. Note |
| 305 |
* that the library provides VERBOSE and TERSE commands that let the |
| 306 |
* player change this setting dynamically. |
| 307 |
* |
| 308 |
* We use a BinarySettingsItem to store the current mode, so that |
| 309 |
* this setting's default will be taken from the user's global |
| 310 |
* cross-game preferences. |
| 311 |
*/ |
| 312 |
verboseMode = verboseModeSettingsItem |
| 313 |
|
| 314 |
/* |
| 315 |
* Option flag: allow the player to use "you" and "me" |
| 316 |
* interchangeably in referring to the player character. We set this |
| 317 |
* true by default, so that the player can refer to the player |
| 318 |
* character in either the first or second person, regardless of how |
| 319 |
* the game refers to the PC. |
| 320 |
* |
| 321 |
* If desired, the game can set this flag to nil to force the player |
| 322 |
* to use the correct pronoun to refer to the player character. We |
| 323 |
* define "correct" in the case of first or second person as the |
| 324 |
* complement of what the game uses: if the game calls the PC "me", |
| 325 |
* the player must say "you", and vice versa. In a third-person |
| 326 |
* game, the player must also refer to the PC in the third person. |
| 327 |
* |
| 328 |
* We set the default to allow using "you" and "me" interchangeably |
| 329 |
* because (a) this will create no confusion in most games, and (b) |
| 330 |
* many players would be annoyed otherwise. For one thing, most |
| 331 |
* experienced IF players will be rather set in their ways; they'll |
| 332 |
* be accustomed to using either "me" or "you" (but usually "me") to |
| 333 |
* refer to the PC, and will tend out of habit to do so even in games |
| 334 |
* that don't use the traditional second-person narration format. |
| 335 |
* For another thing, different players have different ideas about |
| 336 |
* whether the PC is "you" or "me" in input, even in a conventional |
| 337 |
* second-person game. Some players think in terms of a conversation |
| 338 |
* with the narrator, in which case the narrator's "you" is the |
| 339 |
* player's "me", and vice versa; other players are rather more |
| 340 |
* literal-minded, assuming that if the game talks about "you" then |
| 341 |
* so should the player. |
| 342 |
* |
| 343 |
* Even in games that use first-person or third-person narration, it |
| 344 |
* seems unlikely that there will be a separate second-person element |
| 345 |
* to the narration, and as long as that's true, it should cause no |
| 346 |
* confusion for the game to accept "you" and "me" as equivalent in |
| 347 |
* commands. However, the library provides this option in case such |
| 348 |
* as situation does arise. |
| 349 |
*/ |
| 350 |
allowYouMeMixing = true |
| 351 |
|
| 352 |
/* |
| 353 |
* Option flag: filter plural phrase matches exclude the most obvious |
| 354 |
* illogicalities, such as trying to TAKE an object that's already |
| 355 |
* being held, or trying to OPEN an object that's already open. |
| 356 |
* |
| 357 |
* This is set to true by default, which means that we exclude an |
| 358 |
* object from matching a plural phrase when the object's "verify" |
| 359 |
* routine for the verb has an "illogical-already" or an |
| 360 |
* "illogical-self" result. |
| 361 |
* |
| 362 |
* If you would prefer that plural words are simply matched to |
| 363 |
* everything present that matches the vocabulary, without any |
| 364 |
* filtering at all, override this and set it to nil. |
| 365 |
*/ |
| 366 |
filterPluralMatches = true |
| 367 |
|
| 368 |
/* |
| 369 |
* Option flag: allow ALL to be used for every verb. This is true by |
| 370 |
* default, which means that players will be allowed to use ALL with |
| 371 |
* any command - OPEN ALL, EXAMINE ALL, etc. |
| 372 |
* |
| 373 |
* Some authors don't like to allow players to use ALL with so many |
| 374 |
* verbs, because they think it's a sort of "cheating" when players |
| 375 |
* try things like OPEN ALL. This option lets you disable ALL for |
| 376 |
* most verbs; if you set this to nil, only the basic inventory |
| 377 |
* management verbs (TAKE, TAKE FROM, DROP, PUT IN, PUT ON) will |
| 378 |
* allow ALL, and other verbs will simply respond with an error |
| 379 |
* ("'All' isn't allowed with that verb"). |
| 380 |
* |
| 381 |
* If you're writing an especially puzzle-oriented game, you might |
| 382 |
* want to set this to nil. It's a trade-off though, as some people |
| 383 |
* will think your game is less player-friendly if you disable ALL. |
| 384 |
*/ |
| 385 |
allVerbsAllowAll = true |
| 386 |
|
| 387 |
/* |
| 388 |
* When a command fails, should we continue processing any remaining |
| 389 |
* commands on the same command line, or simply ignore them? The |
| 390 |
* reason we might want to ignore additional commands is that they |
| 391 |
* might not do what the player was expecting if an earlier command |
| 392 |
* failed; this can sometimes create confusing situations, because |
| 393 |
* the player expected one effect but got something quite different. |
| 394 |
* On the other hand, *not* executing all the commands on the command |
| 395 |
* line could be confusing in its own way, since the game's |
| 396 |
* assessment of what constitutes "failure" might not be clear to the |
| 397 |
* player; from the player's perspective, the game might appear to be |
| 398 |
* inexplicably skipping commands. |
| 399 |
* |
| 400 |
* There's no perfect solution. As always, the ideal is to |
| 401 |
* understand the player's intentions and act accordingly. But when |
| 402 |
* a command fails, it's usually because the player's idea of what's |
| 403 |
* going on is out of sync with the game's - in other words, if we're |
| 404 |
* in this situation to start with, it's probably because our best |
| 405 |
* effort to understand the player's intentions has already failed. |
| 406 |
* This isn't always the case; sometimes we understand the player's |
| 407 |
* intentions perfectly well, but the command fails anyway because of |
| 408 |
* some surprising new development. In these cases, aborting the |
| 409 |
* rest of the command is arguably the right approach, because the |
| 410 |
* player will need a chance to reconsider the pre-typed commands in |
| 411 |
* light of the new information. In other cases, though, it's not so |
| 412 |
* clear. For many players, the prime virtue for the parser is to be |
| 413 |
* predictable, and the most predictable thing to do is to simply |
| 414 |
* plow through the rest of the command line in all cases. |
| 415 |
* |
| 416 |
* Our traditional approach (from the early adv3 versions, and even |
| 417 |
* in tads 2) has been the simple-minded approach - just keep going |
| 418 |
* in all cases. So, we make this the default. You can abort |
| 419 |
* remaining commands on a command failure by setting this to true. |
| 420 |
*/ |
| 421 |
cancelCmdLineOnFailure = nil |
| 422 |
|
| 423 |
/* |
| 424 |
* Should we use distinguishers when generating action object |
| 425 |
* announcement messages? If this is set, announcement messages that |
| 426 |
* list objects by name will add distinguishing details to indicate |
| 427 |
* specifically which objects are being referred to. This applies to |
| 428 |
* messages announcing default objects, vaguely matched objects, and |
| 429 |
* multiple objects. |
| 430 |
* |
| 431 |
* A distinguisher is a parser object that tells two objects apart by |
| 432 |
* some feature that's different in the two objects. The key thing |
| 433 |
* is that the difference has some natural language phrasing |
| 434 |
* associated with it, both on the input side and the output side. |
| 435 |
* For example, the locational distinguisher can tell two objects |
| 436 |
* apart if they have different containers, since it can describe the |
| 437 |
* objects by adding containment phrases like "in the box" or "on the |
| 438 |
* table". |
| 439 |
* |
| 440 |
* When this flag is turned on, the parser will try to pick a |
| 441 |
* distinguisher that can tell apart the specific objects mentioned |
| 442 |
* in the announcement, from one another or from other objects in |
| 443 |
* scope, depending on the context. The point is to help make it |
| 444 |
* clearer to the player the exact objects being referred to. |
| 445 |
* |
| 446 |
* Even when this flag is set, the parser tries to minimize the use |
| 447 |
* of additional distinguishing detail. The goal is to be natural in |
| 448 |
* the usage, adding detail only when a human speaker would, which is |
| 449 |
* when the phrasing would otherwise be ambiguous. |
| 450 |
* |
| 451 |
* Setting this flag to nil makes the parser simply use the basic |
| 452 |
* name of each object in an announcement. You can use this setting |
| 453 |
* if you find that the distinguisher mode generates too much fussy |
| 454 |
* detail for your taste. |
| 455 |
*/ |
| 456 |
useDistinguishersInAnnouncements = true |
| 457 |
|
| 458 |
/* |
| 459 |
* How should we handle object announcements when an object is |
| 460 |
* automatically disambiguated? This controls how an action is |
| 461 |
* described when the parser uses the logicalness rules to narrow |
| 462 |
* down the object for a noun phrase when the noun phrase could refer |
| 463 |
* to multiple in-scope objects. There are three options: |
| 464 |
* |
| 465 |
* AnnounceUnclear - Make a parenthetical announcement only when the |
| 466 |
* choice is *not* clear (as described below). This is the original |
| 467 |
* library behavior, from before this option was added. |
| 468 |
* |
| 469 |
* AnnounceClear - Make a parenthetical announcement (for example, |
| 470 |
* "(the red door)") for all disambiguated objects, whether clear or |
| 471 |
* unclear. We don't make an announcement when there's only one |
| 472 |
* in-scope object matching the noun phrase - the announcement is |
| 473 |
* only when multiple objects match the words. |
| 474 |
* |
| 475 |
* DescribeClear - For *unclear* disambiguation, make a parenthetical |
| 476 |
* announcement, to emphasize that the parser had to make a choice. |
| 477 |
* For *clear* disambiguation, skip the announcement, but *do* use a |
| 478 |
* verbose version of the library message in place of one of the |
| 479 |
* terse default replies. For example, for >TAKE BOX, instead of |
| 480 |
* "Taken", we would reply "You take the green box." The longer |
| 481 |
* reply in these cases always mentions the involved object by name, |
| 482 |
* to make it clear exactly which object we chose to use. |
| 483 |
* |
| 484 |
* The default setting is DescribeClear. |
| 485 |
* |
| 486 |
* This only applies when the disambiguation choice is clear - that |
| 487 |
* is, when there's exactly one in-scope object that passes the |
| 488 |
* logicalness tests. For example, if the current location contains |
| 489 |
* a red door that's open and a green door that's closed, CLOSE DOOR |
| 490 |
* clearly refers to the red door because the other one is already |
| 491 |
* closed - it's not logical. There are other cases where the |
| 492 |
* disambiguation is a best guess rather than a clear choice, such as |
| 493 |
* when there are multiple logical objects but there's one that's |
| 494 |
* more likely than the others due to the logicalRank results. In |
| 495 |
* those best-guess situations, the parser always announces its |
| 496 |
* decision, because it's entirely plausible that the player meant |
| 497 |
* one of the other logical, but less likely, choices. |
| 498 |
*/ |
| 499 |
ambigAnnounceMode = DescribeClear |
| 500 |
|
| 501 |
/* |
| 502 |
* Should the "before" notifications (beforeAction, roomBeforeAction, |
| 503 |
* and actorAction) run before or after the "check" phase? |
| 504 |
* |
| 505 |
* The library traditionally ran the "before" notifiers first, so |
| 506 |
* this is the default. However, in many ways it's more logical and |
| 507 |
* useful to run "check" first. That way, you can consider the |
| 508 |
* action to be more or less committed by the time the "before" |
| 509 |
* notifiers are invoked. Of course, a command is never *truly* |
| 510 |
* committed until it's actually been executed, since a "before" |
| 511 |
* handler could always cancel it. But this is relatively rare - |
| 512 |
* "before" handlers usually carry out side effects, so it's very |
| 513 |
* useful to be able to know that the command has already passed all |
| 514 |
* of its own internal checks by the time "before" is invoked - that |
| 515 |
* way, you can invoke side effects without worrying that the command |
| 516 |
* will subsequently fail. |
| 517 |
*/ |
| 518 |
beforeRunsBeforeCheck = true |
| 519 |
; |
| 520 |
|
| 521 |
/* |
| 522 |
* The VERBOSE mode settings item. |
| 523 |
*/ |
| 524 |
verboseModeSettingsItem: BinarySettingsItem |
| 525 |
/* VERBOSE mode is on by default */ |
| 526 |
isOn = true |
| 527 |
|
| 528 |
/* our configuration file variable ID */ |
| 529 |
settingID = 'adv3.verbose' |
| 530 |
|
| 531 |
/* show our description */ |
| 532 |
settingDesc = (gLibMessages.shortVerboseStatus(isOn)) |
| 533 |
; |
| 534 |
|
| 535 |
/* ------------------------------------------------------------------------ */ |
| 536 |
/* |
| 537 |
* Clear the main game window. In most cases, you should call this |
| 538 |
* rather than calling the low-level clearScreen() function directly, |
| 539 |
* since this routine takes care of a couple of chores that should |
| 540 |
* usually be done at the same time. |
| 541 |
* |
| 542 |
* First, we flush the transcript to ensure that no left-over reports |
| 543 |
* that were displayed before we cleared the screen will show up on the |
| 544 |
* new screen. Second, we call the low-level clearScreen() function to |
| 545 |
* actually clear the display window. Finally, we re-display any |
| 546 |
* <ABOUTBOX> tag, to ensure that the about-box will still be around; |
| 547 |
* this is necessary because any existing <ABOUTBOX> tag is lost after |
| 548 |
* the screen is cleared. |
| 549 |
*/ |
| 550 |
cls() |
| 551 |
{ |
| 552 |
/* flush any captured transcript output */ |
| 553 |
if (gTranscript != nil) |
| 554 |
gTranscript.flushForInput(); |
| 555 |
|
| 556 |
/* clear the screen */ |
| 557 |
aioClearScreen(); |
| 558 |
} |
| 559 |
|
| 560 |
/* ------------------------------------------------------------------------ */ |
| 561 |
/* |
| 562 |
* Run the game. We start by showing the description of the initial |
| 563 |
* location, if desired, and then we read and interpret commands until |
| 564 |
* the game ends (via a "quit" command, winning, death of the player |
| 565 |
* character, or any other way of terminating the game). |
| 566 |
* |
| 567 |
* This routine doesn't return until the game ends. |
| 568 |
* |
| 569 |
* Before calling this routine, the caller should already have set the |
| 570 |
* global variable gPlayerChar to the player character actor. |
| 571 |
* |
| 572 |
* 'look' is a flag indicating whether or not to look around; if this is |
| 573 |
* true, we'll show a full description of the player character's initial |
| 574 |
* location, as though the player were to type "look around" as the first |
| 575 |
* command. |
| 576 |
*/ |
| 577 |
runGame(look) |
| 578 |
{ |
| 579 |
/* show the starting location */ |
| 580 |
if (look) |
| 581 |
{ |
| 582 |
/* run the initial "look around" in a dummy command context */ |
| 583 |
withActionEnv(EventAction, gPlayerChar, |
| 584 |
{: gPlayerChar.lookAround(true) }); |
| 585 |
} |
| 586 |
|
| 587 |
/* run the scheduling loop until the game ends */ |
| 588 |
runScheduler(); |
| 589 |
} |
| 590 |
|
| 591 |
/* ------------------------------------------------------------------------ */ |
| 592 |
/* |
| 593 |
* Main program entrypoint. The core run-time start-up code calls this |
| 594 |
* after running pre-initialization and load-time initialization. This |
| 595 |
* entrypoint is called when we're starting the game normally; when the |
| 596 |
* game is launched through a saved-position file, mainRestore() will be |
| 597 |
* invoked instead. |
| 598 |
*/ |
| 599 |
main(args) |
| 600 |
{ |
| 601 |
libGlobal.commandLineArgs = args; |
| 602 |
mainCommon(&newGame); |
| 603 |
} |
| 604 |
|
| 605 |
/* |
| 606 |
* Main program entrypoint for restoring a saved-position file. This is |
| 607 |
* invoked from the core run-time start-up code when the game is launched |
| 608 |
* from the operating system via a saved-position file. For example, on |
| 609 |
* Windows, double-clicking on a saved-position file on the Windows |
| 610 |
* desktop launches the interpreter, which looks in the save file to find |
| 611 |
* the game executable to run, then starts the game and invokes this |
| 612 |
* entrypoint. |
| 613 |
*/ |
| 614 |
mainRestore(args, restoreFile) |
| 615 |
{ |
| 616 |
libGlobal.commandLineArgs = args; |
| 617 |
mainCommon(&restoreAndRunGame, restoreFile); |
| 618 |
} |
| 619 |
|
| 620 |
/* |
| 621 |
* Common main entrypoint - this handles starting a new game or restoring |
| 622 |
* an existing saved state. |
| 623 |
*/ |
| 624 |
mainCommon(prop, [args]) |
| 625 |
{ |
| 626 |
try |
| 627 |
{ |
| 628 |
/* restore the global default settings */ |
| 629 |
settingsManager.restoreSettings(); |
| 630 |
} |
| 631 |
catch (Exception exc) |
| 632 |
{ |
| 633 |
/* |
| 634 |
* ignore any errors restoring defaults - it's not critical that |
| 635 |
* we restore this file automatically |
| 636 |
*/ |
| 637 |
} |
| 638 |
|
| 639 |
try |
| 640 |
{ |
| 641 |
/* at the start of the session, set up the UI subsystem */ |
| 642 |
if (mainGlobal.restartID == 0) |
| 643 |
{ |
| 644 |
/* initialize the UI */ |
| 645 |
initUI(); |
| 646 |
|
| 647 |
/* |
| 648 |
* tell the system library to call our UI shutdown function |
| 649 |
* at program exit |
| 650 |
*/ |
| 651 |
mainAtExit.addHandler(terminateUI); |
| 652 |
} |
| 653 |
|
| 654 |
/* initialize the display */ |
| 655 |
initDisplay(); |
| 656 |
|
| 657 |
/* call the appropriate gameMain method */ |
| 658 |
gameMain.(prop)(args...); |
| 659 |
} |
| 660 |
catch (QuittingException q) |
| 661 |
{ |
| 662 |
/* |
| 663 |
* This exception is a signal to quit the game, which we will now |
| 664 |
* proceed to do by returning from this function, which exits the |
| 665 |
* program. |
| 666 |
*/ |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
/* ------------------------------------------------------------------------ */ |
| 671 |
/* |
| 672 |
* Determine if the given object overrides the definition of the given |
| 673 |
* property inherited from the given base class. Returns true if the |
| 674 |
* object derives from the given base class, and the object's definition |
| 675 |
* of the property comes from a different place than the base class's |
| 676 |
* definition of the property. |
| 677 |
*/ |
| 678 |
overrides(obj, base, prop) |
| 679 |
{ |
| 680 |
return (obj.ofKind(base) |
| 681 |
&& (obj.propDefined(prop, PropDefGetClass) |
| 682 |
!= base.propDefined(prop, PropDefGetClass))); |
| 683 |
} |
| 684 |
|
| 685 |
/* ------------------------------------------------------------------------ */ |
| 686 |
/* |
| 687 |
* Library Pre-Initializer. This object performs the following |
| 688 |
* initialization operations immediately after compilation is completed: |
| 689 |
* |
| 690 |
* - adds each defined Thing to its container's contents list |
| 691 |
* |
| 692 |
* - adds each defined Sense to the global sense list |
| 693 |
* |
| 694 |
* This object is named so that other libraries and/or user code can |
| 695 |
* create initialization order dependencies upon it. |
| 696 |
*/ |
| 697 |
adv3LibPreinit: PreinitObject |
| 698 |
execute() |
| 699 |
{ |
| 700 |
/* save each SettingsItem's factory default settings */ |
| 701 |
forEachInstance(SettingsItem, |
| 702 |
{i: i.factoryDefault = i.settingToText()}); |
| 703 |
|
| 704 |
/* set the initial player character, as specified in gameMain */ |
| 705 |
gPlayerChar = gameMain.initialPlayerChar; |
| 706 |
|
| 707 |
/* |
| 708 |
* visit every VocabObject, and run its vocabulary initializer |
| 709 |
* (this routine will be defined in the language-specific part |
| 710 |
* of the library to enter each object's vocabulary words into |
| 711 |
* the dictionary) |
| 712 |
*/ |
| 713 |
forEachInstance(VocabObject, { obj: obj.initializeVocab() }); |
| 714 |
|
| 715 |
/* visit every Thing, and run its general initializer */ |
| 716 |
forEachInstance(Thing, { obj: obj.initializeThing() }); |
| 717 |
|
| 718 |
/* initialize SpecialTopic objects */ |
| 719 |
forEachInstance(SpecialTopic, { obj: obj.initializeSpecialTopic() }); |
| 720 |
|
| 721 |
/* |
| 722 |
* Initialize each MultiInstance object. Do this after |
| 723 |
* initializing the Thing objects, because we'll be dynamically |
| 724 |
* constructing new Thing objects for the instances. Those new |
| 725 |
* Things will be initialized by their constructors, so we don't |
| 726 |
* want to initialize them redundantly with explicit |
| 727 |
* initializeThing calls. |
| 728 |
*/ |
| 729 |
forEachInstance(MultiInstance, { obj: obj.initializeLocation() }); |
| 730 |
|
| 731 |
/* add every Sense to the global sense list */ |
| 732 |
forEachInstance(Sense, { obj: libGlobal.allSenses += obj }); |
| 733 |
|
| 734 |
/* |
| 735 |
* initialize each ActorState - do this before initializing |
| 736 |
* actors, since we want each actor's initial state to plug |
| 737 |
* itself into its actor before we initialize the actors |
| 738 |
*/ |
| 739 |
forEachInstance(ActorState, { obj: obj.initializeActorState() }); |
| 740 |
|
| 741 |
/* initialize each Actor */ |
| 742 |
forEachInstance(Actor, { obj: obj.initializeActor() }); |
| 743 |
|
| 744 |
/* |
| 745 |
* initialize the AltTopics first, to set up their parents' lists |
| 746 |
* of their AltTopic children |
| 747 |
*/ |
| 748 |
forEachInstance(AltTopic, { obj: obj.initializeAltTopic() }); |
| 749 |
|
| 750 |
/* initialize the topic database entries */ |
| 751 |
forEachInstance(TopicEntry, { obj: obj.initializeTopicEntry() }); |
| 752 |
|
| 753 |
/* initialize the suggested topics */ |
| 754 |
forEachInstance(SuggestedTopic, |
| 755 |
{ obj: obj.initializeSuggestedTopic() }); |
| 756 |
|
| 757 |
/* initialize the master direction list */ |
| 758 |
Direction.initializeDirectionClass(); |
| 759 |
|
| 760 |
/* initialize the noise/odor notification daemon */ |
| 761 |
local d = new Daemon(SensoryEmanation, ¬eSenseChanges, 1); |
| 762 |
|
| 763 |
/* |
| 764 |
* give it a later-than-default event order, so that it runs |
| 765 |
* after most other daemons and fuses |
| 766 |
*/ |
| 767 |
d.eventOrder = 500; |
| 768 |
|
| 769 |
/* set up a daemon for the current location */ |
| 770 |
new Daemon(BasicLocation, &dispatchRoomDaemon, 1); |
| 771 |
|
| 772 |
/* |
| 773 |
* Initialize the status line daemon. Set this daemon's event |
| 774 |
* order to a high value so that it runs last, after all other |
| 775 |
* daemons - we want this to be the last prompt daemon so that |
| 776 |
* the status line is updated after any other daemons have done |
| 777 |
* their jobs already, in case any of them move the player |
| 778 |
* character to a new location or affect the score, or make any |
| 779 |
* other changes that should be reflected on the status line. |
| 780 |
*/ |
| 781 |
local sld = new PromptDaemon(statusLine, &showStatusLineDaemon); |
| 782 |
sld.eventOrder = 1000; |
| 783 |
|
| 784 |
/* |
| 785 |
* Attach the command sequencer output filter, the |
| 786 |
* language-specific message parameter substitution filter, the |
| 787 |
* style tag formatter filter, and the paragraph filter to the |
| 788 |
* main output stream. Stack them so that the paragraph manager |
| 789 |
* is at the bottom, since the library tag filter can produce |
| 790 |
* paragraph tags and thus needs to sit atop the paragraph |
| 791 |
* filter. Put the command sequencer above those, since it |
| 792 |
* might need to write style tags. Finally, put the sense |
| 793 |
* context filter on top of those. |
| 794 |
*/ |
| 795 |
mainOutputStream.addOutputFilter(typographicalOutputFilter); |
| 796 |
mainOutputStream.addOutputFilter(mainParagraphManager); |
| 797 |
mainOutputStream.addOutputFilter(styleTagFilter); |
| 798 |
mainOutputStream.addOutputFilter(langMessageBuilder); |
| 799 |
mainOutputStream.addOutputFilter(commandSequencer); |
| 800 |
mainOutputStream.addOutputFilter(conversationManager); |
| 801 |
mainOutputStream.addOutputFilter(senseContext); |
| 802 |
|
| 803 |
/* |
| 804 |
* Attach our message parameter filter and style tag filter to |
| 805 |
* the status line streams. We don't need most of the main |
| 806 |
* window's filters in the status line. |
| 807 |
*/ |
| 808 |
statusTagOutputStream.addOutputFilter(styleTagFilter); |
| 809 |
statusTagOutputStream.addOutputFilter(langMessageBuilder); |
| 810 |
|
| 811 |
statusLeftOutputStream.addOutputFilter(styleTagFilter); |
| 812 |
statusLeftOutputStream.addOutputFilter(langMessageBuilder); |
| 813 |
|
| 814 |
statusRightOutputStream.addOutputFilter(styleTagFilter); |
| 815 |
statusRightOutputStream.addOutputFilter(langMessageBuilder); |
| 816 |
} |
| 817 |
|
| 818 |
/* |
| 819 |
* Make sure the output streams we depend on are initialized before |
| 820 |
* me (so that they set up properly internally). Also, make sure |
| 821 |
* that the message builder object (langMessageBuilder) is set up |
| 822 |
* first, so that we can add entries to its parameter substitution |
| 823 |
* table. |
| 824 |
*/ |
| 825 |
execBeforeMe = [mainOutputStream, statusTagOutputStream, |
| 826 |
statusLeftOutputStream, statusRightOutputStream, |
| 827 |
langMessageBuilder] |
| 828 |
; |
| 829 |
|
| 830 |
/* ------------------------------------------------------------------------ */ |
| 831 |
/* |
| 832 |
* Library Initializer. This object performs the following |
| 833 |
* initialization operations each time the game is started: |
| 834 |
* |
| 835 |
* - sets up the library's default output function |
| 836 |
*/ |
| 837 |
adv3LibInit: InitObject |
| 838 |
execute() |
| 839 |
{ |
| 840 |
/* |
| 841 |
* Set up our default output function. Note that we must do |
| 842 |
* this during run-time initialization each time we start the |
| 843 |
* game, rather than during pre-initialization, because the |
| 844 |
* default output function state is not part of the load-image |
| 845 |
* configuration. |
| 846 |
*/ |
| 847 |
t3SetSay(say); |
| 848 |
} |
| 849 |
; |
| 850 |
|
| 851 |
|
| 852 |
/* ------------------------------------------------------------------------ */ |
| 853 |
/* |
| 854 |
* Generic script object. This class can be used to implement a simple |
| 855 |
* state machine. |
| 856 |
*/ |
| 857 |
class Script: object |
| 858 |
/* |
| 859 |
* Get the current state. This returns a value that gives the |
| 860 |
* current state of the script, which is usually simply an integer. |
| 861 |
*/ |
| 862 |
getScriptState() |
| 863 |
{ |
| 864 |
/* by default, return our state property */ |
| 865 |
return curScriptState; |
| 866 |
} |
| 867 |
|
| 868 |
/* |
| 869 |
* Process the next step of the script. This routine must be |
| 870 |
* overridden to perform the action of the script. This routine's |
| 871 |
* action should call getScriptState() to get our current state, and |
| 872 |
* should update the internal state appropriately to take us to the |
| 873 |
* next step after the current one. |
| 874 |
* |
| 875 |
* By default, we don't do anything at all. |
| 876 |
*/ |
| 877 |
doScript() |
| 878 |
{ |
| 879 |
/* override to carry out the script */ |
| 880 |
} |
| 881 |
|
| 882 |
/* |
| 883 |
* Property giving our current state. This should never be used |
| 884 |
* directly; instead, getScriptState() should always be used, since |
| 885 |
* getScriptState() can be overridden so that the state depends on |
| 886 |
* something other than this internal state property. The meaning of |
| 887 |
* the state identifier is specific to each subclass. |
| 888 |
*/ |
| 889 |
curScriptState = 0 |
| 890 |
; |
| 891 |
|
| 892 |
/* |
| 893 |
* Random-Firing script add-in. This is a mix-in class that you can add |
| 894 |
* to the superclass list of any Script subclass to make the script |
| 895 |
* execute only a given percentage of the time it's invoked. Each time |
| 896 |
* doScript() is invoked on the script, we'll look at the probability |
| 897 |
* settings (see the properties below) to determine whether we really |
| 898 |
* want to execute the script this time; if so, we'll proceed with the |
| 899 |
* scripted event, otherwise we'll just return immediately, doing |
| 900 |
* nothing. |
| 901 |
* |
| 902 |
* Note that this must be used in the superclass list *before* the Script |
| 903 |
* subclass: |
| 904 |
* |
| 905 |
* myScript: RandomFiringScript, EventList |
| 906 |
*. // ...my definitions... |
| 907 |
*. ; |
| 908 |
* |
| 909 |
* This class is especially useful for random atmospheric events, because |
| 910 |
* it allows you to make the timing of scripted events random. Rather |
| 911 |
* than making a scripted event happen on every single turn, you can use |
| 912 |
* this to make events happen only sporadically. It can often feel too |
| 913 |
* predictable and repetitious when a random background event happens on |
| 914 |
* every single turn; firing events less frequently often makes them feel |
| 915 |
* more realistic. |
| 916 |
*/ |
| 917 |
class RandomFiringScript: object |
| 918 |
/* |
| 919 |
* Percentage of the time an event occurs. By default, we execute an |
| 920 |
* event 100% of the time - meaning every time that doScript() is |
| 921 |
* invoked. If you set this to a lower percentage, then each time |
| 922 |
* doScript() is invoked, we'll randomly decide whether or not to |
| 923 |
* execute an event based on this percentage. For example, if you |
| 924 |
* want an event to execute on average about a third of the time, set |
| 925 |
* this to 33. |
| 926 |
* |
| 927 |
* Note that this is a probabilistic frequency. Setting this to 33 |
| 928 |
* does *not* mean that we'll execute exactly every third time. |
| 929 |
* Rather, it means that we'll randomly execute or not on each |
| 930 |
* invocation, and averaged over a large number of invocations, we'll |
| 931 |
* execute about a third of the time. |
| 932 |
*/ |
| 933 |
eventPercent = 100 |
| 934 |
|
| 935 |
/* |
| 936 |
* Random atmospheric events can get repetitive after a while, so we |
| 937 |
* provide an easy way to reduce the frequency of our events after a |
| 938 |
* while. This way, we'll generate the events more frequently at |
| 939 |
* first, but once the player has seen them enough to get the idea, |
| 940 |
* we'll cut back. Sometimes, the player will spend a lot of time in |
| 941 |
* one place trying to solve a puzzle, so the same set of random |
| 942 |
* events can get stale. Set eventReduceAfter to the number of times |
| 943 |
* you want the events to be generated at full frequency; after we've |
| 944 |
* fired events that many times, we'll change eventPercent to |
| 945 |
* eventReduceTo. If eventReduceAfter is nil, we won't ever change |
| 946 |
* eventPercent. |
| 947 |
*/ |
| 948 |
eventReduceAfter = nil |
| 949 |
eventReduceTo = nil |
| 950 |
|
| 951 |
/* |
| 952 |
* When doScript() is invoked, check the event probabilities before |
| 953 |
* proceeding. |
| 954 |
*/ |
| 955 |
doScript() |
| 956 |
{ |
| 957 |
/* process the script step only if the event odds allow it */ |
| 958 |
if (checkEventOdds()) |
| 959 |
inherited(); |
| 960 |
} |
| 961 |
|
| 962 |
/* |
| 963 |
* Check the event odds to see if we want to fire an event at all on |
| 964 |
* this invocation. |
| 965 |
*/ |
| 966 |
checkEventOdds() |
| 967 |
{ |
| 968 |
/* |
| 969 |
* check the event odds to see if we fire an event this time; if |
| 970 |
* not, we're done with the script invocation |
| 971 |
*/ |
| 972 |
if (rand(100) >= eventPercent) |
| 973 |
return nil; |
| 974 |
|
| 975 |
/* |
| 976 |
* we're firing an event this time, so count this against the |
| 977 |
* reduction limit, if there is one |
| 978 |
*/ |
| 979 |
if (eventReduceAfter != nil) |
| 980 |
{ |
| 981 |
/* decrement the limit counter */ |
| 982 |
--eventReduceAfter; |
| 983 |
|
| 984 |
/* if it has reached zero, apply the reduced frequency */ |
| 985 |
if (eventReduceAfter == 0) |
| 986 |
{ |
| 987 |
/* apply the reduced frequency */ |
| 988 |
eventPercent = eventReduceTo; |
| 989 |
|
| 990 |
/* we no longer have a limit to look for */ |
| 991 |
eventReduceAfter = nil; |
| 992 |
} |
| 993 |
} |
| 994 |
|
| 995 |
/* indicate that we do want to fire an event */ |
| 996 |
return true; |
| 997 |
} |
| 998 |
; |
| 999 |
|
| 1000 |
/* ------------------------------------------------------------------------ */ |
| 1001 |
/* |
| 1002 |
* An "event list." This is a general-purpose type of script that lets |
| 1003 |
* you define the scripted events separately from the Script object. |
| 1004 |
* |
| 1005 |
* The script is driven by a list of values; each value represents one |
| 1006 |
* step of the script. Each value can be a single-quoted string, in |
| 1007 |
* which case the string is simply displayed; a function pointer, in |
| 1008 |
* which case the function is invoked without arguments; another Script |
| 1009 |
* object, in which case the object's doScript() method is invoked; a |
| 1010 |
* property pointer, in which case the property of 'self' (the EventList |
| 1011 |
* object) is invoked with no arguments; or nil, in which case nothing |
| 1012 |
* happens. |
| 1013 |
* |
| 1014 |
* This base type of event list runs through the list once, in order, and |
| 1015 |
* then simply stops doing anything once we pass the last event. |
| 1016 |
*/ |
| 1017 |
class EventList: Script |
| 1018 |
construct(lst) { eventList = lst; } |
| 1019 |
|
| 1020 |
/* the list of events */ |
| 1021 |
eventList = [] |
| 1022 |
|
| 1023 |
/* cached length of the event list */ |
| 1024 |
eventListLen = (eventList.length()) |
| 1025 |
|
| 1026 |
/* advance to the next state */ |
| 1027 |
advanceState() |
| 1028 |
{ |
| 1029 |
/* increment our state index */ |
| 1030 |
++curScriptState; |
| 1031 |
} |
| 1032 |
|
| 1033 |
/* by default, start at the first list element */ |
| 1034 |
curScriptState = 1 |
| 1035 |
|
| 1036 |
/* process the next step of the script */ |
| 1037 |
doScript() |
| 1038 |
{ |
| 1039 |
/* get our current event state */ |
| 1040 |
local idx = getScriptState(); |
| 1041 |
|
| 1042 |
/* get the list (evaluate it once to avoid repeated side effects) */ |
| 1043 |
local lst = eventList; |
| 1044 |
|
| 1045 |
/* cache the length */ |
| 1046 |
eventListLen = lst.length(); |
| 1047 |
|
| 1048 |
/* if it's a valid index in our list, fire the event */ |
| 1049 |
if (idx >= 1 && idx <= eventListLen) |
| 1050 |
{ |
| 1051 |
/* carry out the event */ |
| 1052 |
doScriptEvent(lst[idx]); |
| 1053 |
} |
| 1054 |
|
| 1055 |
/* perform any end-of-script processing */ |
| 1056 |
scriptDone(); |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* carry out one script event */ |
| 1060 |
doScriptEvent(evt) |
| 1061 |
{ |
| 1062 |
/* check what kind of event we have */ |
| 1063 |
switch (dataTypeXlat(evt)) |
| 1064 |
{ |
| 1065 |
case TypeSString: |
| 1066 |
/* it's a string - display it */ |
| 1067 |
say(evt); |
| 1068 |
break; |
| 1069 |
|
| 1070 |
case TypeObject: |
| 1071 |
/* it must be a Script object - invoke its doScript() method */ |
| 1072 |
evt.doScript(); |
| 1073 |
break; |
| 1074 |
|
| 1075 |
case TypeFuncPtr: |
| 1076 |
/* it's a function pointer - invoke it */ |
| 1077 |
(evt)(); |
| 1078 |
break; |
| 1079 |
|
| 1080 |
case TypeProp: |
| 1081 |
/* it's a property of self - invoke it */ |
| 1082 |
self.(evt)(); |
| 1083 |
break; |
| 1084 |
|
| 1085 |
default: |
| 1086 |
/* do nothing in other cases */ |
| 1087 |
break; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* |
| 1092 |
* Perform any end-of-script processing. By default, we advance the |
| 1093 |
* script to the next state. |
| 1094 |
* |
| 1095 |
* Some scripts might want to override this. For example, a script |
| 1096 |
* could be driven entirely by some external timing; the state of a |
| 1097 |
* script could vary once per turn, for example, or could change each |
| 1098 |
* time an actor pushes a button. In these cases, invoking the |
| 1099 |
* script wouldn't affect the state of the event list, so the |
| 1100 |
* subclass would override scriptDone() so that it does nothing at |
| 1101 |
* all. |
| 1102 |
*/ |
| 1103 |
scriptDone() |
| 1104 |
{ |
| 1105 |
/* advance to the next state */ |
| 1106 |
advanceState(); |
| 1107 |
} |
| 1108 |
; |
| 1109 |
|
| 1110 |
/* |
| 1111 |
* An "external" event list is one whose state is driven externally to |
| 1112 |
* the script. Specifically, the state is *not* advanced by invoking the |
| 1113 |
* script; the state is advanced exclusively by some external process |
| 1114 |
* (for example, by a daemon that invokes the event list's advanceState() |
| 1115 |
* method). |
| 1116 |
*/ |
| 1117 |
class ExternalEventList: EventList |
| 1118 |
scriptDone() { } |
| 1119 |
; |
| 1120 |
|
| 1121 |
/* |
| 1122 |
* A cyclical event list - this runs through the event list in order, |
| 1123 |
* returning to the first element when we pass the last element. |
| 1124 |
*/ |
| 1125 |
class CyclicEventList: EventList |
| 1126 |
advanceState() |
| 1127 |
{ |
| 1128 |
/* go to the next state */ |
| 1129 |
++curScriptState; |
| 1130 |
|
| 1131 |
/* if we've passed the end of the list, loop back to the start */ |
| 1132 |
if (curScriptState > eventListLen) |
| 1133 |
curScriptState = 1; |
| 1134 |
} |
| 1135 |
; |
| 1136 |
|
| 1137 |
/* |
| 1138 |
* A stopping event list - this runs through the event list in order, |
| 1139 |
* then stops at the last item and repeats it each time the script is |
| 1140 |
* subsequently invoked. |
| 1141 |
* |
| 1142 |
* This is often useful for things like ASK ABOUT topics, where we reveal |
| 1143 |
* more information when asked repeatedly about a topic, but eventually |
| 1144 |
* reach a point where we've said everything: |
| 1145 |
* |
| 1146 |
*. >ask bob about black book |
| 1147 |
*. "What makes you think I know anything about it?" he says, his |
| 1148 |
* voice shaking. |
| 1149 |
* |
| 1150 |
* >again |
| 1151 |
*. "No! You can't make me tell you!" |
| 1152 |
* |
| 1153 |
* >again |
| 1154 |
*. "All right, I'll tell you what you want to know! But I warn you, |
| 1155 |
* these are things mortal men were never meant to know. Your life, your |
| 1156 |
* very soul will be in danger from the moment you hear these dark secrets!" |
| 1157 |
* |
| 1158 |
* >again |
| 1159 |
*. [scene missing] |
| 1160 |
* |
| 1161 |
* >again |
| 1162 |
*. "I've already told you all I know." |
| 1163 |
* |
| 1164 |
* >again |
| 1165 |
*. "I've already told you all I know." |
| 1166 |
*/ |
| 1167 |
class StopEventList: EventList |
| 1168 |
advanceState() |
| 1169 |
{ |
| 1170 |
/* if we haven't yet reached the last state, go to the next one */ |
| 1171 |
if (curScriptState < eventListLen) |
| 1172 |
++curScriptState; |
| 1173 |
} |
| 1174 |
; |
| 1175 |
|
| 1176 |
/* |
| 1177 |
* A synchronized event list. This is an event list that takes its |
| 1178 |
* actions from a separate event list object. We get our current state |
| 1179 |
* from the other list, and advancing our state advances the other list's |
| 1180 |
* state in lock step. Set 'masterObject' to refer to the master list |
| 1181 |
* whose state we synchronize with. |
| 1182 |
* |
| 1183 |
* This can be useful, for example, when we have messages that reflect |
| 1184 |
* two different points of view on the same events: the messages for each |
| 1185 |
* point of view can be kept in a separate list, but the one list can be |
| 1186 |
* a slave of the other to ensure that the two lists are based on a |
| 1187 |
* common state. |
| 1188 |
*/ |
| 1189 |
class SyncEventList: EventList |
| 1190 |
/* my master event list object */ |
| 1191 |
masterObject = nil |
| 1192 |
|
| 1193 |
/* my state is simply the master list's state */ |
| 1194 |
getScriptState() { return masterObject.getScriptState(); } |
| 1195 |
|
| 1196 |
/* to advance my state, advance the master list's state */ |
| 1197 |
advanceState() { masterObject.advanceState(); } |
| 1198 |
|
| 1199 |
/* let the master list take care of finishing a script step */ |
| 1200 |
scriptDone() { masterObject.scriptDone(); } |
| 1201 |
; |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* Randomized event list. This is similar to a regular event list, but |
| 1205 |
* chooses an event at random each time it's invoked. |
| 1206 |
*/ |
| 1207 |
class RandomEventList: RandomFiringScript, EventList |
| 1208 |
/* process the next step of the script */ |
| 1209 |
doScript() |
| 1210 |
{ |
| 1211 |
/* check the odds to see if we want to fire an event at all */ |
| 1212 |
if (!checkEventOdds()) |
| 1213 |
return; |
| 1214 |
|
| 1215 |
/* get our next random number */ |
| 1216 |
local idx = getNextRandom(); |
| 1217 |
|
| 1218 |
/* cache the list and its length, to avoid repeated side effects */ |
| 1219 |
local lst = eventList; |
| 1220 |
eventListLen = lst.length(); |
| 1221 |
|
| 1222 |
/* run the event, if the index is valid */ |
| 1223 |
if (idx >= 1 && idx <= eventListLen) |
| 1224 |
doScriptEvent(lst[idx]); |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* |
| 1228 |
* Get the next random state. By default, we simply return a number |
| 1229 |
* from 1 to the number of entries in our event list. This is a |
| 1230 |
* separate method to allow subclasses to customize the way the |
| 1231 |
* random number is selected. |
| 1232 |
*/ |
| 1233 |
getNextRandom() |
| 1234 |
{ |
| 1235 |
/* |
| 1236 |
* Note that rand(n) returns a number from 0 to n-1 inclusive; |
| 1237 |
* since list indices run from 1 to list.length, add one to the |
| 1238 |
* result of rand(list.length) to get a value in the proper range |
| 1239 |
* for a list index. |
| 1240 |
*/ |
| 1241 |
return rand(eventListLen) + 1; |
| 1242 |
} |
| 1243 |
; |
| 1244 |
|
| 1245 |
/* |
| 1246 |
* Shuffled event list. This is similar to a random event list, except |
| 1247 |
* that we fire our events in a "shuffled" order rather than an |
| 1248 |
* independently random order. "Shuffled order" means that we fire the |
| 1249 |
* events in random order, but we don't re-fire an event until we've run |
| 1250 |
* through all of the other events. The effect is as though we were |
| 1251 |
* dealing from a deck of cards. |
| 1252 |
* |
| 1253 |
* For the first time through the main list, we normally shuffle the |
| 1254 |
* strings immediately at startup, but this is optional. If shuffleFirst |
| 1255 |
* is set to nil, we will NOT shuffle the list the first time through - |
| 1256 |
* we'll run through it once in the given order, then shuffle for the |
| 1257 |
* next time through, then shuffle again for the next, and so on. So, if |
| 1258 |
* you want a specific order for the first time through, just define the |
| 1259 |
* list in the desired order and set shuffleFirst to nil. |
| 1260 |
* |
| 1261 |
* You can optionally specify a separate list of one-time-only sequential |
| 1262 |
* strings in the property firstEvents. We'll run through these strings |
| 1263 |
* once. When we've exhausted them, we'll switch to the main eventList |
| 1264 |
* list, showing it one time through in its given order, then shuffling |
| 1265 |
* it and running through it again, and so on. The firstEvents list is |
| 1266 |
* never shuffled - it's always shown in exactly the order given. |
| 1267 |
*/ |
| 1268 |
class ShuffledEventList: RandomFiringScript, EventList |
| 1269 |
/* |
| 1270 |
* a list of events to go through sequentially, in the exact order |
| 1271 |
* specified, before firing any events from the main list |
| 1272 |
*/ |
| 1273 |
firstEvents = [] |
| 1274 |
|
| 1275 |
/* |
| 1276 |
* Flag: shuffle the eventList list before we show it for the first |
| 1277 |
* time. By default, this is set to true, so that the behavior is |
| 1278 |
* random on each independent run of the game. However, it might be |
| 1279 |
* desirable in some cases to always use the original ordering of the |
| 1280 |
* eventList list the first time through the list. If this is set to |
| 1281 |
* nil, we won't shuffle the list the first time through. |
| 1282 |
*/ |
| 1283 |
shuffleFirst = true |
| 1284 |
|
| 1285 |
/* |
| 1286 |
* Flag: suppress repeats in the shuffle. If this is true, it |
| 1287 |
* prevents a given event from showing up twice in a row, which could |
| 1288 |
* otherwise happen right after a shuffle. This is ignored for lists |
| 1289 |
* with one or two events: it's impossible to prevent repeats in a |
| 1290 |
* one-element list, and doing so in a two-element list would produce |
| 1291 |
* a predictable A-B-A-B... pattern. |
| 1292 |
* |
| 1293 |
* You might want to set this to nil for lists of three or four |
| 1294 |
* elements, since such short lists can result in fairly |
| 1295 |
* un-random-looking sequences when repeats are suppressed, because |
| 1296 |
* the available number of permutations drops significantly. |
| 1297 |
*/ |
| 1298 |
suppressRepeats = true |
| 1299 |
|
| 1300 |
/* process the next step of the script */ |
| 1301 |
doScript() |
| 1302 |
{ |
| 1303 |
/* cache the lists to avoid repeated side effects */ |
| 1304 |
local firstLst = firstEvents; |
| 1305 |
local firstLen = firstLst.length(); |
| 1306 |
local lst = eventList; |
| 1307 |
eventListLen = lst.length(); |
| 1308 |
|
| 1309 |
/* process the script step only if the event odds allow it */ |
| 1310 |
if (!checkEventOdds()) |
| 1311 |
return; |
| 1312 |
|
| 1313 |
/* |
| 1314 |
* States 1..N, where N is the number of elements in the |
| 1315 |
* firstEvents list, simply show the firstEvents elements in |
| 1316 |
* order. |
| 1317 |
* |
| 1318 |
* If we're set to shuffle the main eventList list initially, all |
| 1319 |
* states above N simply show elements from the eventList list in |
| 1320 |
* shuffled order. |
| 1321 |
* |
| 1322 |
* If we're NOT set to shuffle the main eventList list initially, |
| 1323 |
* the following apply: |
| 1324 |
* |
| 1325 |
* States N+1..N+M, where M is the number of elements in the |
| 1326 |
* eventList list, show the eventList elements in order. |
| 1327 |
* |
| 1328 |
* States above N+M show elements from the eventList list in |
| 1329 |
* shuffled order. |
| 1330 |
*/ |
| 1331 |
local evt; |
| 1332 |
if (curScriptState <= firstLen) |
| 1333 |
{ |
| 1334 |
/* simply fetch the next string from firstEvents */ |
| 1335 |
evt = firstEvents[curScriptState++]; |
| 1336 |
} |
| 1337 |
else if (!shuffleFirst && curScriptState <= firstLen + eventListLen) |
| 1338 |
{ |
| 1339 |
/* fetch the next string from eventList */ |
| 1340 |
evt = lst[curScriptState++ - firstLen]; |
| 1341 |
} |
| 1342 |
else |
| 1343 |
{ |
| 1344 |
/* we're showing shuffled strings from the eventList list */ |
| 1345 |
evt = lst[getNextRandom()]; |
| 1346 |
} |
| 1347 |
|
| 1348 |
/* execute the event */ |
| 1349 |
doScriptEvent(evt); |
| 1350 |
} |
| 1351 |
|
| 1352 |
|
| 1353 |
/* |
| 1354 |
* Get the next random event. We'll pick an event from our list of |
| 1355 |
* events using a ShuffledIntegerList to ensure we pick each value |
| 1356 |
* once before re-using any values. |
| 1357 |
*/ |
| 1358 |
getNextRandom() |
| 1359 |
{ |
| 1360 |
/* if we haven't created our shuffled list yet, do so now */ |
| 1361 |
if (shuffledList_ == nil) |
| 1362 |
{ |
| 1363 |
/* |
| 1364 |
* create a shuffled integer list - we'll use these shuffled |
| 1365 |
* integers as indices into our event list |
| 1366 |
*/ |
| 1367 |
shuffledList_ = new ShuffledIntegerList(1, eventListLen); |
| 1368 |
|
| 1369 |
/* apply our suppressRepeats option to the shuffled list */ |
| 1370 |
shuffledList_.suppressRepeats = suppressRepeats; |
| 1371 |
} |
| 1372 |
|
| 1373 |
/* ask the shuffled list to pick an element */ |
| 1374 |
return shuffledList_.getNextValue(); |
| 1375 |
} |
| 1376 |
|
| 1377 |
/* our ShuffledList - we'll initialize this on demand */ |
| 1378 |
shuffledList_ = nil |
| 1379 |
; |
| 1380 |
|
| 1381 |
/* ------------------------------------------------------------------------ */ |
| 1382 |
/* |
| 1383 |
* Shuffled List - this class keeps a list of values that can be returned |
| 1384 |
* in random order, but with the constraint that we never repeat a value |
| 1385 |
* until we've handed out every value. Think of a shuffled deck of |
| 1386 |
* cards: the order of the cards handed out is random, but once a card is |
| 1387 |
* dealt, it can't be dealt again until we put everything back into the |
| 1388 |
* deck and reshuffle. |
| 1389 |
*/ |
| 1390 |
class ShuffledList: object |
| 1391 |
/* |
| 1392 |
* the list of values we want to shuffle - initialize this in each |
| 1393 |
* instance to the set of values we want to return in random order |
| 1394 |
*/ |
| 1395 |
valueList = [] |
| 1396 |
|
| 1397 |
/* |
| 1398 |
* Flag: suppress repeated values. We mostly suppress repeats by our |
| 1399 |
* very design, since we run through the entire list before repeating |
| 1400 |
* anything in the list. However, there's one situation (in a list |
| 1401 |
* with more than one element) where a repeat can occur: immediately |
| 1402 |
* after a shuffle, we could select the last element from the |
| 1403 |
* previous shuffle as the first element of the new shuffle. If this |
| 1404 |
* flag is set, we'll suppress this type of repeat by choosing again |
| 1405 |
* any time we're about to choose a repeat. |
| 1406 |
* |
| 1407 |
* Note that we ignore this for a list of one element, since it's |
| 1408 |
* obviously impossible to avoid repeats in this case. We also |
| 1409 |
* ignore it for a two-element list, since this would produce the |
| 1410 |
* predictable pattern A-B-A-B..., defeating the purpose of the |
| 1411 |
* shuffle. |
| 1412 |
*/ |
| 1413 |
suppressRepeats = nil |
| 1414 |
|
| 1415 |
/* create from a given list */ |
| 1416 |
construct(lst) |
| 1417 |
{ |
| 1418 |
/* remember our list of values */ |
| 1419 |
valueList = lst; |
| 1420 |
} |
| 1421 |
|
| 1422 |
/* |
| 1423 |
* Get a random value. This will return a randomly-selected element |
| 1424 |
* from 'valueList', but we'll return every element of 'valueList' |
| 1425 |
* once before repeating any element. |
| 1426 |
* |
| 1427 |
* If we've returned every value on the current round, we'll |
| 1428 |
* automatically shuffle the values and start a new round. |
| 1429 |
*/ |
| 1430 |
getNextValue() |
| 1431 |
{ |
| 1432 |
local i; |
| 1433 |
local ret; |
| 1434 |
local justReshuffled = nil; |
| 1435 |
|
| 1436 |
/* if we haven't initialized our vector, do so now */ |
| 1437 |
if (valuesVec == nil) |
| 1438 |
{ |
| 1439 |
/* create the vector */ |
| 1440 |
valuesVec = new Vector(valueList.length(), valueList); |
| 1441 |
|
| 1442 |
/* all values are initially available */ |
| 1443 |
valuesAvail = valuesVec.length(); |
| 1444 |
} |
| 1445 |
|
| 1446 |
/* if we've exhausted our values on this round, start over */ |
| 1447 |
if (valuesAvail == 0) |
| 1448 |
{ |
| 1449 |
/* shuffle the elements */ |
| 1450 |
reshuffle(); |
| 1451 |
|
| 1452 |
/* note that we just did a shuffle */ |
| 1453 |
justReshuffled = true; |
| 1454 |
} |
| 1455 |
|
| 1456 |
/* pick a random element from the 'available' partition */ |
| 1457 |
i = rand(valuesAvail) + 1; |
| 1458 |
|
| 1459 |
/* |
| 1460 |
* If we just reshuffled, and we're configured to suppress a |
| 1461 |
* repeat immediately after a reshuffle, and we chose the first |
| 1462 |
* element of the vector, and we have at least three elements, |
| 1463 |
* choose a different element. The first element in the vector is |
| 1464 |
* always the last element we return from each run-through, since |
| 1465 |
* the 'available' partition is at the start of the list and thus |
| 1466 |
* shrinks down until it contains only the first element. |
| 1467 |
* |
| 1468 |
* If we have one element, there's obviously no point in trying to |
| 1469 |
* suppress repeats. If we have two elements, we *still* don't |
| 1470 |
* want to suppress repeats, because in this case we'd generate a |
| 1471 |
* predicatable A-B-A-B pattern (because we could never have two |
| 1472 |
* A's or two B's in a row). |
| 1473 |
*/ |
| 1474 |
if (justReshuffled && suppressRepeats && valuesAvail > 2) |
| 1475 |
{ |
| 1476 |
/* |
| 1477 |
* we don't want repeats, so choose anything besides the |
| 1478 |
* first element; keep choosing until we get another element |
| 1479 |
*/ |
| 1480 |
while (i == 1) |
| 1481 |
i = rand(valuesAvail) + 1; |
| 1482 |
} |
| 1483 |
|
| 1484 |
/* remember the element we're returning */ |
| 1485 |
ret = valuesVec[i]; |
| 1486 |
|
| 1487 |
/* |
| 1488 |
* Move the value at the top of the 'available' partition down |
| 1489 |
* into the hole we're creating at 'i', since we're about to |
| 1490 |
* reduce the size of the 'available' partition to reflect the |
| 1491 |
* use of one more value; that would leave the element at the top |
| 1492 |
* of the partition homeless, so we need somewhere to put it. |
| 1493 |
* Luckily, we also need to delete element 'i', since we're using |
| 1494 |
* this element. Solve both problems at once by moving element |
| 1495 |
* we're rendering homeless into the hole we're creating. |
| 1496 |
*/ |
| 1497 |
valuesVec[i] = valuesVec[valuesAvail]; |
| 1498 |
|
| 1499 |
/* move the value we're returning into the top slot */ |
| 1500 |
valuesVec[valuesAvail] = ret; |
| 1501 |
|
| 1502 |
/* reduce the 'available' partition by one */ |
| 1503 |
--valuesAvail; |
| 1504 |
|
| 1505 |
/* return the result */ |
| 1506 |
return ret; |
| 1507 |
} |
| 1508 |
|
| 1509 |
/* |
| 1510 |
* Shuffle the values. This puts all of the values back into the |
| 1511 |
* deck (as it were) for a new round. It's never required to call |
| 1512 |
* this, because getNextValue() automatically shuffles the deck and |
| 1513 |
* starts over each time it runs through the entire deck. This is |
| 1514 |
* provided in case the caller has a reason to want to put all the |
| 1515 |
* values back into play immediately, before every value has been |
| 1516 |
* dealt on the current round. |
| 1517 |
*/ |
| 1518 |
reshuffle() |
| 1519 |
{ |
| 1520 |
/* |
| 1521 |
* Simply reset the counter of available values. Go with the |
| 1522 |
* original source list's length, in case we haven't initialized |
| 1523 |
* our internal vector yet. |
| 1524 |
*/ |
| 1525 |
valuesAvail = valueList.length(); |
| 1526 |
} |
| 1527 |
|
| 1528 |
/* |
| 1529 |
* Internal vector of available/used values. Elements from 1 to |
| 1530 |
* 'valuesAvail', inclusive, are still available for use on this |
| 1531 |
* round. Elements above 'valuesAvail' have already been used. |
| 1532 |
*/ |
| 1533 |
valuesVec = nil |
| 1534 |
|
| 1535 |
/* number of values still available on this round */ |
| 1536 |
valuesAvail = 0 |
| 1537 |
; |
| 1538 |
|
| 1539 |
/* |
| 1540 |
* A Shuffled Integer List is a special kind of Shuffled List that |
| 1541 |
* returns integers in a given range. Like an ordinary Shuffled List, |
| 1542 |
* we'll return integers in the given range in random order, but we'll |
| 1543 |
* only return each integer once during a given round; when we exhaust |
| 1544 |
* the supply, we'll reshuffle the set of integers and start over. |
| 1545 |
*/ |
| 1546 |
class ShuffledIntegerList: ShuffledList |
| 1547 |
/* |
| 1548 |
* The minimum and maximum values for our range. Instances should |
| 1549 |
* define these to the range desired. |
| 1550 |
*/ |
| 1551 |
rangeMin = 1 |
| 1552 |
rangeMax = 10 |
| 1553 |
|
| 1554 |
/* initialize the value list on demand */ |
| 1555 |
valueList = nil |
| 1556 |
|
| 1557 |
/* construct with the given range */ |
| 1558 |
construct(rmin, rmax) |
| 1559 |
{ |
| 1560 |
rangeMin = rmin; |
| 1561 |
rangeMax = rmax; |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* get the next value */ |
| 1565 |
getNextValue() |
| 1566 |
{ |
| 1567 |
/* |
| 1568 |
* If we haven't set up our value list yet, do so now. This is |
| 1569 |
* simply a list of integers from rangeMin to rangeMax. |
| 1570 |
*/ |
| 1571 |
if (valueList == nil) |
| 1572 |
{ |
| 1573 |
local ele = rangeMin; |
| 1574 |
valueList = List.generate({i: ele++}, rangeMax - rangeMin + 1); |
| 1575 |
} |
| 1576 |
|
| 1577 |
/* use the inherited handling to select from our value list */ |
| 1578 |
return inherited(); |
| 1579 |
} |
| 1580 |
; |
| 1581 |
|
| 1582 |
|
| 1583 |
/* ------------------------------------------------------------------------ */ |
| 1584 |
/* |
| 1585 |
* Library global variables |
| 1586 |
*/ |
| 1587 |
libGlobal: object |
| 1588 |
/* |
| 1589 |
* The current library messages object. This is the source object |
| 1590 |
* for messages that don't logically relate to the actor carrying out |
| 1591 |
* the comamand. It's mostly used for meta-command replies, and for |
| 1592 |
* text fragments that are used to construct descriptions. |
| 1593 |
* |
| 1594 |
* This message object isn't generally used for parser messages or |
| 1595 |
* action replies - most of those come from the objects given by the |
| 1596 |
* current actor's getParserMessageObj() or getActionMessageObj(), |
| 1597 |
* respectively. |
| 1598 |
* |
| 1599 |
* By default, this is set to libMessages. The library never changes |
| 1600 |
* this itself, but a game can change this if it wants to switch to a |
| 1601 |
* new set of messages during a game. (If you don't need to change |
| 1602 |
* messages during a game, but simply want to customize some of the |
| 1603 |
* default messages, you don't need to set this variable - you can |
| 1604 |
* simply use 'modify libMessages' instead. This variable is |
| 1605 |
* designed for cases where you want to *dynamically* change the |
| 1606 |
* standard messages during the game.) |
| 1607 |
*/ |
| 1608 |
libMessageObj = libMessages |
| 1609 |
|
| 1610 |
/* |
| 1611 |
* Sense cache - we keep SenseInfo lists here, keyed by [pov,sense]; |
| 1612 |
* we normally discard the cached information at the start of each |
| 1613 |
* turn, and disable caching entirely at the start of the "action" |
| 1614 |
* phase of each turn. We leave caching disabled during each turn's |
| 1615 |
* action phase because this is the phase where simulation state |
| 1616 |
* changes are typically made, and hence it would be difficult to |
| 1617 |
* keep the cache coherent during this phase. |
| 1618 |
* |
| 1619 |
* When this is nil, it indicates that caching is disabled. We only |
| 1620 |
* allow caching during certain phases of execution, when game state |
| 1621 |
* is not conventionally altered, so that we don't have to do a lot |
| 1622 |
* of work to keep the cache up to date. |
| 1623 |
*/ |
| 1624 |
senseCache = nil |
| 1625 |
|
| 1626 |
/* |
| 1627 |
* Can-Touch cache - we keep CanTouchInfo entries here, keyed by |
| 1628 |
* [from,to]. This cache is the touch-path equivalent of the sense |
| 1629 |
* cache, and is enabled and disabled |
| 1630 |
*/ |
| 1631 |
canTouchCache = nil |
| 1632 |
|
| 1633 |
/* |
| 1634 |
* Connection list cache - this is a cache of all of the objects |
| 1635 |
* connected by containment to a given object. |
| 1636 |
*/ |
| 1637 |
connectionCache = nil |
| 1638 |
|
| 1639 |
/* |
| 1640 |
* Actor visual ambient cache - this keeps track of the ambient light |
| 1641 |
* level at the given actor. |
| 1642 |
*/ |
| 1643 |
actorVisualAmbientCache = nil |
| 1644 |
|
| 1645 |
/* enable the cache, clearing any old cached information */ |
| 1646 |
enableSenseCache() |
| 1647 |
{ |
| 1648 |
/* create a new, empty lookup table for the sense cache */ |
| 1649 |
senseCache = new LookupTable(32, 64); |
| 1650 |
|
| 1651 |
/* create the can-touch cache */ |
| 1652 |
canTouchCache = new LookupTable(32, 64); |
| 1653 |
|
| 1654 |
/* create the actor visual ambient cache */ |
| 1655 |
actorVisualAmbientCache = new LookupTable(32, 64); |
| 1656 |
|
| 1657 |
/* create a connection list cache */ |
| 1658 |
connectionCache = new LookupTable(32, 64); |
| 1659 |
} |
| 1660 |
|
| 1661 |
/* disable the cache */ |
| 1662 |
disableSenseCache() |
| 1663 |
{ |
| 1664 |
/* forget the cache tables */ |
| 1665 |
senseCache = nil; |
| 1666 |
canTouchCache = nil; |
| 1667 |
actorVisualAmbientCache = nil; |
| 1668 |
connectionCache = nil; |
| 1669 |
} |
| 1670 |
|
| 1671 |
/* |
| 1672 |
* Invalidate the sense cache. This can be called if something |
| 1673 |
* happens during noun resolution or verification that causes any |
| 1674 |
* cached sense information to become out of date. For example, if |
| 1675 |
* you have to create a new game-world object during noun-phrase |
| 1676 |
* resolution, this should be called to ensure that the new object's |
| 1677 |
* visibility is properly calculated and incorporated into the cached |
| 1678 |
* information. |
| 1679 |
*/ |
| 1680 |
invalSenseCache() |
| 1681 |
{ |
| 1682 |
/* remember whether or not caching is currently enabled */ |
| 1683 |
local wasEnabled = (senseCache != nil); |
| 1684 |
|
| 1685 |
/* clear the cache by disabling it */ |
| 1686 |
disableSenseCache(); |
| 1687 |
|
| 1688 |
/* if the cache was previously enabled, re-enable it */ |
| 1689 |
if (wasEnabled) |
| 1690 |
enableSenseCache(); |
| 1691 |
} |
| 1692 |
|
| 1693 |
/* |
| 1694 |
* List of all of the senses. The library pre-initializer will load |
| 1695 |
* this list with a reference to each instance of class Sense. |
| 1696 |
*/ |
| 1697 |
allSenses = [] |
| 1698 |
|
| 1699 |
/* |
| 1700 |
* The current player character |
| 1701 |
*/ |
| 1702 |
playerChar = nil |
| 1703 |
|
| 1704 |
/* |
| 1705 |
* The current perspective actor. This is the actor who's performing |
| 1706 |
* the action (LOOK AROUND, EXAMINE, SMELL, etc) that's generating |
| 1707 |
* the current description. |
| 1708 |
*/ |
| 1709 |
pointOfViewActor = nil |
| 1710 |
|
| 1711 |
/* |
| 1712 |
* The current perspective object. This is *usually* the actor |
| 1713 |
* performing the current command, but can be a different object when |
| 1714 |
* the actor is viewing the location being described via an |
| 1715 |
* intermediary, such as through a closed-circuit TV camera. |
| 1716 |
*/ |
| 1717 |
pointOfView = nil |
| 1718 |
|
| 1719 |
/* |
| 1720 |
* The stack of point of view objects. The last element of the |
| 1721 |
* vector is the most recent point of view after the current point |
| 1722 |
* of view. |
| 1723 |
*/ |
| 1724 |
povStack = static new Vector(32) |
| 1725 |
|
| 1726 |
/* |
| 1727 |
* The global score object. We use a global for this, rather than |
| 1728 |
* referencing libScore directly, to allow the score module to be |
| 1729 |
* left out entirely if the game doesn't make use of scoring. The |
| 1730 |
* score module should set this during pre-initialization. |
| 1731 |
*/ |
| 1732 |
scoreObj = nil |
| 1733 |
|
| 1734 |
/* |
| 1735 |
* The global Footnote class object. We use a global for this, |
| 1736 |
* rather than referencing Footnote directly, to allow the footnote |
| 1737 |
* module to be left out entirely if the game doesn't make use of |
| 1738 |
* footnotes. The footnote class should set this during |
| 1739 |
* pre-initialization. |
| 1740 |
*/ |
| 1741 |
footnoteClass = nil |
| 1742 |
|
| 1743 |
/* the total number of turns so far */ |
| 1744 |
totalTurns = 0 |
| 1745 |
|
| 1746 |
/* |
| 1747 |
* flag: the parser is in 'debug' mode, in which it displays the |
| 1748 |
* parse tree for each command entered |
| 1749 |
*/ |
| 1750 |
parserDebugMode = nil |
| 1751 |
|
| 1752 |
/* |
| 1753 |
* Most recent command, for 'undo' purposes. This is the last |
| 1754 |
* command the player character performed, or the last initial |
| 1755 |
* command a player directed to an NPC. |
| 1756 |
* |
| 1757 |
* Note that if the player directed a series of commands to an NPC |
| 1758 |
* with a single command line, only the first command on such a |
| 1759 |
* command line is retained here, because it is only the first such |
| 1760 |
* command that counts as a player's turn in terms of the game |
| 1761 |
* clock. Subsequent commands are executed by the NPC's on the |
| 1762 |
* NPC's own time, and do not count against the PC's game clock |
| 1763 |
* time. The first command counts against the PC's clock because of |
| 1764 |
* the time it takes the PC to give the command to the NPC. |
| 1765 |
*/ |
| 1766 |
lastCommandForUndo = '' |
| 1767 |
|
| 1768 |
/* |
| 1769 |
* Most recent target actor phrase; this goes with |
| 1770 |
* lastCommandForUndo. This is nil if the last command did not |
| 1771 |
* specify an actor (i.e., was implicitly for the player character), |
| 1772 |
* otherwise is the string the player typed specifying a target |
| 1773 |
* actor. |
| 1774 |
*/ |
| 1775 |
lastActorForUndo = '' |
| 1776 |
|
| 1777 |
/* |
| 1778 |
* Current command information. We keep track of the current |
| 1779 |
* command's actor and action here, as well as the verification |
| 1780 |
* result list and the command report list. |
| 1781 |
*/ |
| 1782 |
curActor = nil |
| 1783 |
curIssuingActor = nil |
| 1784 |
curAction = nil |
| 1785 |
curVerifyResults = nil |
| 1786 |
|
| 1787 |
/* the exitLister object, if included in the build */ |
| 1788 |
exitListerObj = nil |
| 1789 |
|
| 1790 |
/* the hint manager, if included in the build */ |
| 1791 |
hintManagerObj = nil |
| 1792 |
|
| 1793 |
/* |
| 1794 |
* The game's IFID, as defined in the game's main module ID object. |
| 1795 |
* If the game has multiple IFIDs in the module list, this will store |
| 1796 |
* only the first IFID in the list. NOTE: the library initializes |
| 1797 |
* this automatically during preinit; don't set this manually. |
| 1798 |
*/ |
| 1799 |
IFID = nil |
| 1800 |
|
| 1801 |
/* |
| 1802 |
* Command line arguments. The library sets this to a list of |
| 1803 |
* strings containing the arguments passed to the program on the |
| 1804 |
* command line. This list contains the command line arguments |
| 1805 |
* parsed according to the local conventions for the operating system |
| 1806 |
* and C++ library. The standard parsing procedure used by most |
| 1807 |
* systems is to break the line into tokens delimited by space |
| 1808 |
* characters. Many systems also allow space characters to be |
| 1809 |
* embedded in tokens by quoting the tokens. The first argument is |
| 1810 |
* always the name of the .t3 file currently executing. |
| 1811 |
*/ |
| 1812 |
commandLineArgs = [] |
| 1813 |
|
| 1814 |
/* |
| 1815 |
* Retrieve a "switch" from the command line. Switches are options |
| 1816 |
* specifies with the conventional Unix "-xxx" notation. This |
| 1817 |
* searches for a command option that equals the given string or |
| 1818 |
* starts with the given substring. If we find it, we return the |
| 1819 |
* part of the option after the given substring - this is |
| 1820 |
* conventionally the value of the switch. For example, the command |
| 1821 |
* line might look like this: |
| 1822 |
* |
| 1823 |
*. t3run mygame.t3 -name=MyGame -user=Bob |
| 1824 |
* |
| 1825 |
* Searching for '-name=' would return 'MyGame', and searching for |
| 1826 |
* '-user=' would return' Bob'. |
| 1827 |
* |
| 1828 |
* If the switch is found but has no value attached, the return value |
| 1829 |
* is an empty string. If the switch isn't found at all, the return |
| 1830 |
* value is nil. |
| 1831 |
*/ |
| 1832 |
getCommandSwitch(s) |
| 1833 |
{ |
| 1834 |
/* search from argument 2 to the last switch argument */ |
| 1835 |
local args = commandLineArgs; |
| 1836 |
for (local i in 2..args.length()) |
| 1837 |
{ |
| 1838 |
/* |
| 1839 |
* if this isn't a switch, or is the special "-" last switch |
| 1840 |
* marker, we're done |
| 1841 |
*/ |
| 1842 |
local a = args[i]; |
| 1843 |
if (!a.startsWith('-') || a == '-') |
| 1844 |
return nil; |
| 1845 |
|
| 1846 |
/* check for a match */ |
| 1847 |
if (a.startsWith(s)) |
| 1848 |
return a.substr(s.length() + 1); |
| 1849 |
} |
| 1850 |
|
| 1851 |
/* didn't find it */ |
| 1852 |
return nil; |
| 1853 |
} |
| 1854 |
; |
| 1855 |
|
| 1856 |
/* ------------------------------------------------------------------------ */ |
| 1857 |
/* |
| 1858 |
* FinishType objects are used in finishGameMsg() to indicate what kind |
| 1859 |
* of game-over message to display. We provide a couple of standard |
| 1860 |
* objects for the most common cases. |
| 1861 |
*/ |
| 1862 |
class FinishType: object |
| 1863 |
/* the finishing message, as a string or library message property */ |
| 1864 |
finishMsg = nil |
| 1865 |
; |
| 1866 |
|
| 1867 |
/* 'death' - the game has ended due to the player character's demise */ |
| 1868 |
ftDeath: FinishType finishMsg = &finishDeathMsg; |
| 1869 |
|
| 1870 |
/* 'victory' - the player has won the game */ |
| 1871 |
ftVictory: FinishType finishMsg = &finishVictoryMsg; |
| 1872 |
|
| 1873 |
/* 'failure' - the game has ended in failure (but not necessarily death) */ |
| 1874 |
ftFailure: FinishType finishMsg = &finishFailureMsg; |
| 1875 |
|
| 1876 |
/* 'game over' - the game has simply ended */ |
| 1877 |
ftGameOver: FinishType finishMsg = &finishGameOverMsg; |
| 1878 |
|
| 1879 |
/* |
| 1880 |
* Finish the game, showing a message explaining why the game has ended. |
| 1881 |
* This can be called when an event occurs that ends the game, such as |
| 1882 |
* the player character's death, winning, or any other endpoint in the |
| 1883 |
* story. |
| 1884 |
* |
| 1885 |
* We'll show a message defined by 'msg', using a standard format. The |
| 1886 |
* format depends on the language, but in English, it's usually the |
| 1887 |
* message surrounded by asterisks: "*** You have won! ***". 'msg' can |
| 1888 |
* be: |
| 1889 |
* |
| 1890 |
*. - nil, in which case we display nothing |
| 1891 |
*. - a string, which we'll display as the message |
| 1892 |
*. - a FinishType object, from which we'll get the message |
| 1893 |
* |
| 1894 |
* After showing the message (if any), we'll prompt the user with |
| 1895 |
* options for how to proceed. We'll always show the QUIT, RESTART, and |
| 1896 |
* RESTORE options; other options can be offered by listing one or more |
| 1897 |
* FinishOption objects in the 'extra' parameter, which is given as a |
| 1898 |
* list of FinishOption objects. The library defines a few non-default |
| 1899 |
* finish options, such as finishOptionUndo and finishOptionCredits; in |
| 1900 |
* addition, the game can subclass FinishOption to create its own custom |
| 1901 |
* options, as desired. |
| 1902 |
*/ |
| 1903 |
finishGameMsg(msg, extra) |
| 1904 |
{ |
| 1905 |
local lst; |
| 1906 |
|
| 1907 |
/* |
| 1908 |
* Adjust the turn counter to take into account the action currently |
| 1909 |
* in progress, if any, and to reflect any turns that the player |
| 1910 |
* character has already completed and which aren't yet reflected in |
| 1911 |
* the turn counter. If we're processing a daemon, the PC's next |
| 1912 |
* schedulable run time will already reflect the last turn the PC |
| 1913 |
* completed, but the global turn counter won't be there yet, since |
| 1914 |
* we're still scheduling daemons that were ready to run on the same |
| 1915 |
* turn as the player's last action. |
| 1916 |
*/ |
| 1917 |
libGlobal.totalTurns = gPlayerChar.nextRunTime + gAction.actionTime; |
| 1918 |
|
| 1919 |
/* |
| 1920 |
* Explicitly run any final score notification now. This will ensure |
| 1921 |
* that any points awarded in the course of the final command that |
| 1922 |
* brought us to this point will generate the usual notification, and |
| 1923 |
* that the notification will appear at a reasonable place, just |
| 1924 |
* before the termination message. |
| 1925 |
*/ |
| 1926 |
if (libGlobal.scoreObj != nil) |
| 1927 |
libGlobal.scoreObj.runScoreNotifier(); |
| 1928 |
|
| 1929 |
/* translate the message, if specified */ |
| 1930 |
if (dataType(msg) == TypeObject) |
| 1931 |
{ |
| 1932 |
/* it's a FinishType object - get its message property or string */ |
| 1933 |
msg = msg.finishMsg; |
| 1934 |
|
| 1935 |
/* if it's a library message property, look it up */ |
| 1936 |
if (dataType(msg) == TypeProp) |
| 1937 |
msg = gLibMessages.(msg); |
| 1938 |
} |
| 1939 |
|
| 1940 |
/* if we have a message, display it */ |
| 1941 |
if (msg != nil) |
| 1942 |
gLibMessages.showFinishMsg(msg); |
| 1943 |
|
| 1944 |
/* if the extra options include a scoring option, show the score */ |
| 1945 |
if (extra != nil && extra.indexWhich({x: x.showScoreInFinish}) != nil) |
| 1946 |
{ |
| 1947 |
"<.p>"; |
| 1948 |
libGlobal.scoreObj.showScore(); |
| 1949 |
"<.p>"; |
| 1950 |
} |
| 1951 |
|
| 1952 |
/* |
| 1953 |
* Since we need to interact directly with the player, any sense |
| 1954 |
* context currently in effect is now irrelevant. Reset the sense |
| 1955 |
* context by setting the 'source' object to nil to indicate that we |
| 1956 |
* don't need any sense blocking at all. We can just set the context |
| 1957 |
* directly, since this routine will never return into the |
| 1958 |
* surrounding command processing - we always either terminate the |
| 1959 |
* program or proceed to a different game context (via undo, restore, |
| 1960 |
* restart, etc). By the same token, the actor we're talking to now |
| 1961 |
* is the player character. |
| 1962 |
*/ |
| 1963 |
senseContext.setSenseContext(nil, sight); |
| 1964 |
gActor = gPlayerChar; |
| 1965 |
|
| 1966 |
/* start with the standard options */ |
| 1967 |
lst = [finishOptionRestore, finishOptionRestart]; |
| 1968 |
|
| 1969 |
/* add any additional options in the 'extra' parameter */ |
| 1970 |
if (extra != nil) |
| 1971 |
lst += extra; |
| 1972 |
|
| 1973 |
/* always add 'quit' as the last option */ |
| 1974 |
lst += finishOptionQuit; |
| 1975 |
|
| 1976 |
/* process the options */ |
| 1977 |
processOptions(lst); |
| 1978 |
} |
| 1979 |
|
| 1980 |
/* finish the game, offering the given extra options but no message */ |
| 1981 |
finishGame(extra) |
| 1982 |
{ |
| 1983 |
finishGameMsg(nil, extra); |
| 1984 |
} |
| 1985 |
|
| 1986 |
/* |
| 1987 |
* Show failed startup restore options. If a restore operation fails at |
| 1988 |
* startup, we won't just proceed with the game, but ask the user what |
| 1989 |
* they want to do; we'll offer the options of restoring another game, |
| 1990 |
* quitting, or starting the game from the beginning. |
| 1991 |
*/ |
| 1992 |
failedRestoreOptions() |
| 1993 |
{ |
| 1994 |
/* process our set of options */ |
| 1995 |
processOptions([restoreOptionRestoreAnother, restoreOptionStartOver, |
| 1996 |
finishOptionQuit]); |
| 1997 |
} |
| 1998 |
|
| 1999 |
/* |
| 2000 |
* Process a list of finishing options. We'll loop, showing prompts and |
| 2001 |
* reading responses, until we get a response that terminates the loop. |
| 2002 |
*/ |
| 2003 |
processOptions(lst) |
| 2004 |
{ |
| 2005 |
/* keep going until we get a valid response */ |
| 2006 |
promptLoop: |
| 2007 |
for (;;) |
| 2008 |
{ |
| 2009 |
local resp; |
| 2010 |
|
| 2011 |
/* show the options */ |
| 2012 |
finishOptionsLister.showListAll(lst, 0, 0); |
| 2013 |
|
| 2014 |
/* switch to before-command mode for reading the interactive input */ |
| 2015 |
"<.commandbefore>"; |
| 2016 |
|
| 2017 |
/* |
| 2018 |
* update the status line, in case the score or turn counter has |
| 2019 |
* changed (this is especially likely when we first enter this |
| 2020 |
* loop, since we might have just finished the game with our |
| 2021 |
* previous action, and that action might well have awarded us |
| 2022 |
* some points) |
| 2023 |
*/ |
| 2024 |
statusLine.showStatusLine(); |
| 2025 |
|
| 2026 |
/* read a response */ |
| 2027 |
resp = inputManager.getInputLine(nil, nil); |
| 2028 |
|
| 2029 |
/* switch to command-after mode */ |
| 2030 |
"<.commandafter>"; |
| 2031 |
|
| 2032 |
/* check for a match to each of the options in our list */ |
| 2033 |
foreach (local cur in lst) |
| 2034 |
{ |
| 2035 |
/* if this one matches, process the option */ |
| 2036 |
if (cur.responseMatches(resp)) |
| 2037 |
{ |
| 2038 |
/* it matches - carry out the option */ |
| 2039 |
if (cur.doOption()) |
| 2040 |
{ |
| 2041 |
/* |
| 2042 |
* they returned true - they want to continue asking |
| 2043 |
* for more options |
| 2044 |
*/ |
| 2045 |
continue promptLoop; |
| 2046 |
} |
| 2047 |
else |
| 2048 |
{ |
| 2049 |
/* |
| 2050 |
* they returned nil - they want us to stop asking |
| 2051 |
* for options and return to our caller |
| 2052 |
*/ |
| 2053 |
return; |
| 2054 |
} |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
/* |
| 2059 |
* If we got this far, it means that we didn't get a valid |
| 2060 |
* option. Display our "invalid option" message, and continue |
| 2061 |
* looping so that we show the prompt again and read a new |
| 2062 |
* option. |
| 2063 |
*/ |
| 2064 |
gLibMessages.invalidFinishOption(resp); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
|
| 2068 |
/* |
| 2069 |
* Finish Option class. This is the base class for the abstract objects |
| 2070 |
* representing options offered by finishGame. |
| 2071 |
*/ |
| 2072 |
class FinishOption: object |
| 2073 |
/* |
| 2074 |
* The description, as displayed in the list of options. For the |
| 2075 |
* default English messages, this is expected to be a verb phrase in |
| 2076 |
* infinitive form, and should show the keyword accepted as a |
| 2077 |
* response in all capitals: "RESTART", "see some AMUSING things to |
| 2078 |
* do", "show CREDITS". |
| 2079 |
*/ |
| 2080 |
desc = "" |
| 2081 |
|
| 2082 |
/* |
| 2083 |
* By default, the item is listed. If you want to create an |
| 2084 |
* invisible option that's accepted but which isn't listed in the |
| 2085 |
* prompt, just set this to nil. Invisible options are sometimes |
| 2086 |
* useful when the output of one option mentions another option; for |
| 2087 |
* example, the CREDITS message might mention a LICENSE command for |
| 2088 |
* displaying the license, so you want to make that command available |
| 2089 |
* without cluttering the prompt with it. |
| 2090 |
*/ |
| 2091 |
isListed = true |
| 2092 |
|
| 2093 |
/* our response keyword */ |
| 2094 |
responseKeyword = '' |
| 2095 |
|
| 2096 |
/* |
| 2097 |
* a single character we accept as an alternative to our full |
| 2098 |
* response keyword, or nil if we don't accept a single-character |
| 2099 |
* response |
| 2100 |
*/ |
| 2101 |
responseChar = nil |
| 2102 |
|
| 2103 |
/* |
| 2104 |
* Match a response string to this option. Returns true if the |
| 2105 |
* string matches our response, nil otherwise. By default, we'll |
| 2106 |
* return true if the string exactly matches responseKeyword or |
| 2107 |
* exactly matches our responseChar (if that's non-nil), but this |
| 2108 |
* can be overridden to match other strings if desired. By default, |
| 2109 |
* we'll match the response without regard to case. |
| 2110 |
*/ |
| 2111 |
responseMatches(response) |
| 2112 |
{ |
| 2113 |
/* do all of our work in lower-case */ |
| 2114 |
response = response.toLower(); |
| 2115 |
|
| 2116 |
/* |
| 2117 |
* check for a match the full response keyword or to the single |
| 2118 |
* response character |
| 2119 |
*/ |
| 2120 |
return (response == responseKeyword.toLower() |
| 2121 |
|| (responseChar != nil |
| 2122 |
&& response == responseChar.toLower())); |
| 2123 |
} |
| 2124 |
|
| 2125 |
/* |
| 2126 |
* Carry out the option. This is called when the player enters a |
| 2127 |
* response that matches this option. This routine must perform the |
| 2128 |
* action of the option, then return true to indicate that we should |
| 2129 |
* ask for another option, or nil to indicate that the finishGame() |
| 2130 |
* routine should simply return. |
| 2131 |
*/ |
| 2132 |
doOption() |
| 2133 |
{ |
| 2134 |
/* tell finishGame() to ask for another option */ |
| 2135 |
return true; |
| 2136 |
} |
| 2137 |
|
| 2138 |
/* |
| 2139 |
* Flag: show the score with the end-of-game announcement. If any |
| 2140 |
* option in the list of finishing options has this flag set, we'll |
| 2141 |
* show the score using the same message that the SCORE command |
| 2142 |
* uses. |
| 2143 |
*/ |
| 2144 |
showScoreInFinish = nil |
| 2145 |
; |
| 2146 |
|
| 2147 |
/* |
| 2148 |
* QUIT option for finishGame. The language-specific code should modify |
| 2149 |
* this to specify the description and response keywords. |
| 2150 |
*/ |
| 2151 |
finishOptionQuit: FinishOption |
| 2152 |
doOption() |
| 2153 |
{ |
| 2154 |
/* |
| 2155 |
* carry out the Quit action - this will signal a |
| 2156 |
* QuittingException, so this call will never return |
| 2157 |
*/ |
| 2158 |
QuitAction.terminateGame(); |
| 2159 |
} |
| 2160 |
; |
| 2161 |
|
| 2162 |
/* |
| 2163 |
* RESTORE option for finishGame. |
| 2164 |
*/ |
| 2165 |
finishOptionRestore: FinishOption |
| 2166 |
doOption() |
| 2167 |
{ |
| 2168 |
/* |
| 2169 |
* Try restoring. If this succeeds (i.e., it returns true), tell |
| 2170 |
* the caller to stop looping and to proceed with the game by |
| 2171 |
* returning nil. If this fails, tell the caller to keep looping |
| 2172 |
* by returning true. |
| 2173 |
*/ |
| 2174 |
if (RestoreAction.askAndRestore()) |
| 2175 |
{ |
| 2176 |
/* |
| 2177 |
* we succeeded, so we're now restored to some prior game |
| 2178 |
* state - terminate any remaining processing in the command |
| 2179 |
* that triggered the end-of-game options |
| 2180 |
*/ |
| 2181 |
throw new TerminateCommandException(); |
| 2182 |
} |
| 2183 |
else |
| 2184 |
{ |
| 2185 |
/* it failed - tell the caller to keep looping */ |
| 2186 |
return true; |
| 2187 |
} |
| 2188 |
} |
| 2189 |
; |
| 2190 |
|
| 2191 |
/* |
| 2192 |
* RESTART option for finishGame |
| 2193 |
*/ |
| 2194 |
finishOptionRestart: FinishOption |
| 2195 |
doOption() |
| 2196 |
{ |
| 2197 |
/* |
| 2198 |
* carry out the restart - this will not return, since we'll |
| 2199 |
* reset the game state and re-enter the game at the restart |
| 2200 |
* entrypoint |
| 2201 |
*/ |
| 2202 |
RestartAction.doRestartGame(); |
| 2203 |
} |
| 2204 |
; |
| 2205 |
|
| 2206 |
/* |
| 2207 |
* START FROM BEGINNING option for failed startup restore. This is just |
| 2208 |
* like finishOptionRestart, but shows a different option name. |
| 2209 |
*/ |
| 2210 |
restoreOptionStartOver: finishOptionRestart |
| 2211 |
; |
| 2212 |
|
| 2213 |
/* |
| 2214 |
* RESTORE ANOTHER GAME option for failed startup restore. This is just |
| 2215 |
* like finishOptionRestore, but shows a different option name. |
| 2216 |
*/ |
| 2217 |
restoreOptionRestoreAnother: finishOptionRestore |
| 2218 |
; |
| 2219 |
|
| 2220 |
/* |
| 2221 |
* UNDO option for finishGame |
| 2222 |
*/ |
| 2223 |
finishOptionUndo: FinishOption |
| 2224 |
doOption() |
| 2225 |
{ |
| 2226 |
/* try performing the undo */ |
| 2227 |
if (UndoAction.performUndo(nil)) |
| 2228 |
{ |
| 2229 |
/* act as though UNDO were the last actual command, for AGAIN */ |
| 2230 |
AgainAction.saveForAgain(gPlayerChar, gPlayerChar, |
| 2231 |
nil, UndoAction); |
| 2232 |
|
| 2233 |
/* |
| 2234 |
* Success - terminate the current command with no further |
| 2235 |
* processing. |
| 2236 |
*/ |
| 2237 |
throw new TerminateCommandException(); |
| 2238 |
} |
| 2239 |
else |
| 2240 |
{ |
| 2241 |
/* |
| 2242 |
* failure - show a blank line and tell the caller to ask |
| 2243 |
* for another option, since we couldn't carry out this |
| 2244 |
* option |
| 2245 |
*/ |
| 2246 |
"<.p>"; |
| 2247 |
return true; |
| 2248 |
} |
| 2249 |
} |
| 2250 |
; |
| 2251 |
|
| 2252 |
/* |
| 2253 |
* FULL SCORE option for finishGame |
| 2254 |
*/ |
| 2255 |
finishOptionFullScore: FinishOption |
| 2256 |
doOption() |
| 2257 |
{ |
| 2258 |
/* show a blank line before the score display */ |
| 2259 |
"\b"; |
| 2260 |
|
| 2261 |
/* run the Full Score action */ |
| 2262 |
FullScoreAction.showFullScore(); |
| 2263 |
|
| 2264 |
/* show a paragraph break after the score display */ |
| 2265 |
"<.p>"; |
| 2266 |
|
| 2267 |
/* |
| 2268 |
* this option has now had its full effect, so tell the caller |
| 2269 |
* to go back and ask for a new option |
| 2270 |
*/ |
| 2271 |
return true; |
| 2272 |
} |
| 2273 |
|
| 2274 |
/* |
| 2275 |
* by default, show the score with the end-of-game announcement when |
| 2276 |
* this option is included |
| 2277 |
*/ |
| 2278 |
showScoreInFinish = true |
| 2279 |
; |
| 2280 |
|
| 2281 |
/* |
| 2282 |
* Option to show the score in finishGame. This doesn't create a listed |
| 2283 |
* option in the set of offered options, but rather is simply a flag to |
| 2284 |
* finishGame() that the score should be announced along with the |
| 2285 |
* end-of-game announcement message. |
| 2286 |
*/ |
| 2287 |
finishOptionScore: FinishOption |
| 2288 |
/* show the score in the end-of-game announcement */ |
| 2289 |
showScoreInFinish = true |
| 2290 |
|
| 2291 |
/* this is not a listed option */ |
| 2292 |
isListed = nil |
| 2293 |
|
| 2294 |
/* this option isn't selectable, so it has no effect */ |
| 2295 |
doOption() { } |
| 2296 |
; |
| 2297 |
|
| 2298 |
/* |
| 2299 |
* CREDITS option for finishGame |
| 2300 |
*/ |
| 2301 |
finishOptionCredits: FinishOption |
| 2302 |
doOption() |
| 2303 |
{ |
| 2304 |
/* show a blank line before the credits */ |
| 2305 |
"\b"; |
| 2306 |
|
| 2307 |
/* run the Credits action */ |
| 2308 |
CreditsAction.execSystemAction(); |
| 2309 |
|
| 2310 |
/* show a paragraph break after the credits */ |
| 2311 |
"<.p>"; |
| 2312 |
|
| 2313 |
/* |
| 2314 |
* this option has now had its full effect, so tell the caller |
| 2315 |
* to go back and ask for a new option |
| 2316 |
*/ |
| 2317 |
return true; |
| 2318 |
} |
| 2319 |
; |
| 2320 |
|
| 2321 |
/* |
| 2322 |
* AMUSING option for finishGame |
| 2323 |
*/ |
| 2324 |
finishOptionAmusing: FinishOption |
| 2325 |
/* |
| 2326 |
* The game must modify this object to define a doOption method. We |
| 2327 |
* have no built-in way to show a list of amusing things to try, so |
| 2328 |
* if a game wants to offer this option, it must provide a suitable |
| 2329 |
* definition here. (We never offer this option by default, so a |
| 2330 |
* game need not provide a definition if the game doesn't explicitly |
| 2331 |
* offer this option via the 'extra' argument to finishGame()). |
| 2332 |
*/ |
| 2333 |
; |
| 2334 |
|
| 2335 |
/* ------------------------------------------------------------------------ */ |
| 2336 |
/* |
| 2337 |
* The settings user interface. This is a subclass of the Settings |
| 2338 |
* Manager that adds a command-line user interface, particularly to allow |
| 2339 |
* the user to view, save, and load the default settings. |
| 2340 |
* |
| 2341 |
* Our user interface consists mainly of a pair of special commands: SAVE |
| 2342 |
* DEFAULTS and RESTORE DEFAULTS. The SAVE DEFAULTS command tells the |
| 2343 |
* library to write out all of the current settings (at least, all of |
| 2344 |
* those that participate in this framework) to a file. RESTORE DEFAULTS |
| 2345 |
* explicitly reads that same file and puts the stored settings into |
| 2346 |
* effect. Finally, we'll also read the file and activate its stored |
| 2347 |
* settings when we start (or RESTART) the game. |
| 2348 |
* |
| 2349 |
*/ |
| 2350 |
settingsUI: settingsManager |
| 2351 |
/* display all of the current settings */ |
| 2352 |
showAll() |
| 2353 |
{ |
| 2354 |
local first = true; |
| 2355 |
|
| 2356 |
/* loop over all SettingsItem instances */ |
| 2357 |
forEachInstance(SettingsItem, function(item) |
| 2358 |
{ |
| 2359 |
/* include only items that want to participate in the listing */ |
| 2360 |
if (item.includeInListing) |
| 2361 |
{ |
| 2362 |
/* add a separator if this isn't the first one */ |
| 2363 |
if (!first) |
| 2364 |
gLibMessages.settingsItemSeparator; |
| 2365 |
|
| 2366 |
/* show this item's description */ |
| 2367 |
item.settingDesc; |
| 2368 |
|
| 2369 |
/* it's no longer the first */ |
| 2370 |
first = nil; |
| 2371 |
} |
| 2372 |
}); |
| 2373 |
} |
| 2374 |
|
| 2375 |
/* |
| 2376 |
* Save settings, and display an acknowledgment message (or an error |
| 2377 |
* message, if necessary) for the user's edification. |
| 2378 |
*/ |
| 2379 |
saveSettingsMsg() |
| 2380 |
{ |
| 2381 |
/* catch any errors */ |
| 2382 |
try |
| 2383 |
{ |
| 2384 |
/* save the settings */ |
| 2385 |
saveSettings(); |
| 2386 |
|
| 2387 |
/* if we got this far, declare success */ |
| 2388 |
gLibMessages.savedDefaults(); |
| 2389 |
} |
| 2390 |
catch (Exception exc) |
| 2391 |
{ |
| 2392 |
/* we couldn't open the file */ |
| 2393 |
gLibMessages.defaultsFileWriteError; |
| 2394 |
} |
| 2395 |
} |
| 2396 |
|
| 2397 |
/* |
| 2398 |
* Restore settings, and display an acknowledgment or error message, |
| 2399 |
* as appropriate. |
| 2400 |
*/ |
| 2401 |
restoreSettingsMsg() |
| 2402 |
{ |
| 2403 |
/* catch any errors */ |
| 2404 |
try |
| 2405 |
{ |
| 2406 |
/* restore the settings */ |
| 2407 |
restoreSettings(); |
| 2408 |
|
| 2409 |
/* if we got this far, declare success */ |
| 2410 |
gLibMessages.restoredDefaults(); |
| 2411 |
} |
| 2412 |
catch (SettingsNotSupportedException sns) |
| 2413 |
{ |
| 2414 |
/* this interpreter doesn't support the settings file */ |
| 2415 |
gLibMessages.defaultsFileNotSupported; |
| 2416 |
} |
| 2417 |
catch (Exception exc) |
| 2418 |
{ |
| 2419 |
/* display other errors */ |
| 2420 |
gLibMessages.defaultsFileReadError(exc); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
; |
| 2424 |
|
| 2425 |
/* ------------------------------------------------------------------------ */ |
| 2426 |
/* |
| 2427 |
* Utility functions |
| 2428 |
*/ |
| 2429 |
|
| 2430 |
/* |
| 2431 |
* nilToList - convert a 'nil' value to an empty list. This can be |
| 2432 |
* useful for mix-in classes that will be used in different inheritance |
| 2433 |
* contexts, since the classes might or might not inherit a base class |
| 2434 |
* definition for list-valued methods such as preconditions. This |
| 2435 |
* provides a usable default for list-valued methods that return nothing |
| 2436 |
* from superclasses. |
| 2437 |
*/ |
| 2438 |
nilToList(val) |
| 2439 |
{ |
| 2440 |
return (val != nil ? val : []); |
| 2441 |
} |
| 2442 |
|
| 2443 |
/* |
| 2444 |
* partitionList - partition a list into a pair of two lists, the first |
| 2445 |
* containing items that match the predicate 'fn', the second containing |
| 2446 |
* items that don't match 'fn'. 'fn' is a function pointer (usually an |
| 2447 |
* anonymous function) that takes a single argument - a list element - |
| 2448 |
* and returns true or nil. |
| 2449 |
* |
| 2450 |
* The return value is a list with two elements. The first element is a |
| 2451 |
* list giving the elements of the original list for which 'fn' returns |
| 2452 |
* true, the second element is a list giving the elements for which 'fn' |
| 2453 |
* returns nil. |
| 2454 |
* |
| 2455 |
* (Contributed by Tommy Nordgren.) |
| 2456 |
*/ |
| 2457 |
partitionList(lst, fn) |
| 2458 |
{ |
| 2459 |
local lst1 = lst.subset(fn); |
| 2460 |
local lst2 = lst.subset({x : !fn(x)}); |
| 2461 |
|
| 2462 |
return [lst1, lst2]; |
| 2463 |
} |
| 2464 |
|
| 2465 |
/* |
| 2466 |
* Determine if list a is a subset of list b. a is a subset of b if |
| 2467 |
* every element of a is in b. |
| 2468 |
*/ |
| 2469 |
isListSubset(a, b) |
| 2470 |
{ |
| 2471 |
/* a can't be a subset if it has more elements than b */ |
| 2472 |
if (a.length() > b.length()) |
| 2473 |
return nil; |
| 2474 |
|
| 2475 |
/* check each element of a to see if it's also in b */ |
| 2476 |
foreach (local cur in a) |
| 2477 |
{ |
| 2478 |
/* if this element of a is not in b, a is not a subset of b */ |
| 2479 |
if (b.indexOf(cur) == nil) |
| 2480 |
return nil; |
| 2481 |
} |
| 2482 |
|
| 2483 |
/* |
| 2484 |
* we didn't find any elements of a that are not also in b, so a is a |
| 2485 |
* subset of b |
| 2486 |
*/ |
| 2487 |
return true; |
| 2488 |
} |
| 2489 |
|