| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library: command execution |
| 7 |
* |
| 8 |
* This module defines functions that perform command execution. |
| 9 |
*/ |
| 10 |
|
| 11 |
#include "adv3.h" |
| 12 |
|
| 13 |
|
| 14 |
/* ------------------------------------------------------------------------ */ |
| 15 |
/* |
| 16 |
* Execute a command line, as issued by the given actor and as given as a |
| 17 |
* list of tokens. |
| 18 |
* |
| 19 |
* If 'firstInSentence' is true, we're at the start of a "sentence." The |
| 20 |
* meaning and effect of this may vary by language. In English, a |
| 21 |
* sentence ends with certain punctuation marks (a period, semicolon, |
| 22 |
* exclamation mark, or question mark), so anything after one of these |
| 23 |
* punctuation marks is the start of a new sentence. Also in English, we |
| 24 |
* can address a command to an explicit target actor using the "actor," |
| 25 |
* prefix syntax, which we can't use except at the start of a sentence. |
| 26 |
* |
| 27 |
* If the command line consists of multiple commands, we will only |
| 28 |
* actually execute the first command before returning. We'll schedule |
| 29 |
* any additional commands for later execution by putting them into the |
| 30 |
* target actor's pending command queue before we return, but we won't |
| 31 |
* actually execute them. |
| 32 |
*/ |
| 33 |
executeCommand(targetActor, issuingActor, toks, firstInSentence) |
| 34 |
{ |
| 35 |
local actorPhrase; |
| 36 |
local actorSpecified; |
| 37 |
|
| 38 |
/* |
| 39 |
* Turn on sense caching while we're working, until execution |
| 40 |
* begins. The parsing and resolution phases of command processing |
| 41 |
* don't involve any changes to game state, so we can safely cache |
| 42 |
* sense information; caching sense information during these phases |
| 43 |
* is desirable because these steps (noun resolution in particular) |
| 44 |
* involve repeated inspection of the current sensory environment, |
| 45 |
* which can require expensive calculations. |
| 46 |
*/ |
| 47 |
libGlobal.enableSenseCache(); |
| 48 |
|
| 49 |
/* we don't have an explicit actor phrase yet */ |
| 50 |
actorPhrase = nil; |
| 51 |
|
| 52 |
/* presume an actor will not be specified */ |
| 53 |
actorSpecified = nil; |
| 54 |
|
| 55 |
/* |
| 56 |
* If this is the start of a new sentence, and the issuing actor |
| 57 |
* wants to cancel any target actor designation at the end of each |
| 58 |
* sentence, change the target actor back to the issuing actor. |
| 59 |
*/ |
| 60 |
if (firstInSentence |
| 61 |
&& issuingActor != targetActor |
| 62 |
&& issuingActor.revertTargetActorAtEndOfSentence) |
| 63 |
{ |
| 64 |
/* switch to the target actor */ |
| 65 |
targetActor = issuingActor; |
| 66 |
|
| 67 |
/* switch to the issuer's sense context */ |
| 68 |
senseContext.setSenseContext(issuingActor, sight); |
| 69 |
} |
| 70 |
|
| 71 |
/* |
| 72 |
* Keep going until we've processed the command. This might take |
| 73 |
* several iterations, because we might have replacement commands to |
| 74 |
* execute. |
| 75 |
*/ |
| 76 |
parseTokenLoop: |
| 77 |
for (;;) |
| 78 |
{ |
| 79 |
local lst; |
| 80 |
local action; |
| 81 |
local match; |
| 82 |
local nextIdx; |
| 83 |
local nextCommandTokens; |
| 84 |
local extraIdx; |
| 85 |
local extraTokens; |
| 86 |
|
| 87 |
/* |
| 88 |
* Catch any errors that occur while executing the command, |
| 89 |
* since some of them are signals to us that we should reparse |
| 90 |
* some new text read or generated deep down inside the command |
| 91 |
* processing. |
| 92 |
*/ |
| 93 |
try |
| 94 |
{ |
| 95 |
local rankings; |
| 96 |
|
| 97 |
/* we have no extra tokens yet */ |
| 98 |
extraTokens = []; |
| 99 |
|
| 100 |
/* |
| 101 |
* Parse the token list. If this is the first command on |
| 102 |
* the command line, allow an actor prefix. Otherwise, just |
| 103 |
* look for a command. |
| 104 |
*/ |
| 105 |
lst = (firstInSentence ? firstCommandPhrase : commandPhrase) |
| 106 |
.parseTokens(toks, cmdDict); |
| 107 |
|
| 108 |
/* |
| 109 |
* As a first cut at reducing the list of possible matches |
| 110 |
* to those that make sense, eliminate from this list any |
| 111 |
* matches which do not have valid actions. In grammars for |
| 112 |
* "scrambling" languages (i.e., languages with flexible |
| 113 |
* word ordering), it is possible to construct commands that |
| 114 |
* fit the grammatical rules of sentence construction but |
| 115 |
* which make no sense because of the specific constraints |
| 116 |
* of the verbs involved; we can filter out such nonsense |
| 117 |
* interpretations immediately by keeping only those |
| 118 |
* structural interpretations that can resolve to valid |
| 119 |
* actions. |
| 120 |
*/ |
| 121 |
lst = lst.subset( |
| 122 |
{x: x.resolveFirstAction(issuingActor, targetActor) != nil}); |
| 123 |
|
| 124 |
/* if we have no matches, the command isn't understood */ |
| 125 |
if (lst.length() == 0) |
| 126 |
{ |
| 127 |
/* |
| 128 |
* If this is a first-in-sentence phrase, try looking for |
| 129 |
* a target actor phrase. If we can find one, we can |
| 130 |
* direct the 'oops' to that actor, to allow the game to |
| 131 |
* customize messages more specifically. |
| 132 |
*/ |
| 133 |
if (firstInSentence) |
| 134 |
{ |
| 135 |
local i; |
| 136 |
|
| 137 |
/* try parsing an "actor, <unknown>" phrase */ |
| 138 |
lst = actorBadCommandPhrase.parseTokens(toks, cmdDict); |
| 139 |
|
| 140 |
/* if we got any matches, try to resolve actors */ |
| 141 |
lst = lst.mapAll({x: x.resolveNouns( |
| 142 |
issuingActor, issuingActor, |
| 143 |
new TryAsActorResolveResults())}); |
| 144 |
|
| 145 |
/* drop any that didn't yield any results */ |
| 146 |
lst = lst.subset({x: x != nil && x.length() != 0}); |
| 147 |
|
| 148 |
/* |
| 149 |
* |
| 150 |
* if anything's left, and one of the entries |
| 151 |
* resolves to an actor, arbitrarily pick the |
| 152 |
* first such entry use the resolved actor as the |
| 153 |
* new target actor |
| 154 |
*/ |
| 155 |
if (lst.length() != 0 |
| 156 |
&& (i = lst.indexWhich( |
| 157 |
{x: x[1].obj_.ofKind(Actor)})) != nil) |
| 158 |
targetActor = lst[i][1].obj_; |
| 159 |
} |
| 160 |
|
| 161 |
/* |
| 162 |
* We don't understand the command. Check for unknown |
| 163 |
* words - if we have any, give them a chance to use |
| 164 |
* OOPS to correct a typo. |
| 165 |
*/ |
| 166 |
tryOops(toks, issuingActor, targetActor, |
| 167 |
1, toks, rmcCommand); |
| 168 |
|
| 169 |
/* |
| 170 |
* try running it by the SpecialTopic history to see if |
| 171 |
* they're trying to use a special topic in the wrong |
| 172 |
* context - if so, explain that they can't use the |
| 173 |
* command right now, rather than claiming that the |
| 174 |
* command is completely invalid |
| 175 |
*/ |
| 176 |
if (specialTopicHistory.checkHistory(toks)) |
| 177 |
{ |
| 178 |
/* the special command is not currently available */ |
| 179 |
targetActor.notifyParseFailure( |
| 180 |
issuingActor, &specialTopicInactive, []); |
| 181 |
} |
| 182 |
else |
| 183 |
{ |
| 184 |
/* tell the issuer we didn't understand the command */ |
| 185 |
targetActor.notifyParseFailure( |
| 186 |
issuingActor, &commandNotUnderstood, []); |
| 187 |
} |
| 188 |
|
| 189 |
/* |
| 190 |
* we're done with this command, and we want to abort |
| 191 |
* any subsequent commands on the command line |
| 192 |
*/ |
| 193 |
return; |
| 194 |
} |
| 195 |
|
| 196 |
/* show the matches if we're in debug mode */ |
| 197 |
dbgShowGrammarList(lst); |
| 198 |
|
| 199 |
/* |
| 200 |
* Perform a tentative resolution on each alternative |
| 201 |
* structural interpretation of the command, and rank the |
| 202 |
* interpretations in order of "goodness" as determined by |
| 203 |
* our ranking criteria encoded in CommandRanking. |
| 204 |
* |
| 205 |
* Note that we perform the tentative resolution and ranking |
| 206 |
* even if we have only one interpretation, because the |
| 207 |
* tentative resolution is often useful for the final |
| 208 |
* resolution pass. |
| 209 |
*/ |
| 210 |
rankings = CommandRanking |
| 211 |
.sortByRanking(lst, issuingActor, targetActor); |
| 212 |
|
| 213 |
/* |
| 214 |
* Take the interpretation that came up best in the rankings |
| 215 |
* (or, if we have multiple at the same ranking, arbitrarily |
| 216 |
* pick the one that happened to come up first in the list) |
| 217 |
* - they're ranked in descending order of goodness, so take |
| 218 |
* the first one. |
| 219 |
*/ |
| 220 |
match = rankings[1].match; |
| 221 |
|
| 222 |
/* if we're in debug mode, show the winner */ |
| 223 |
dbgShowGrammarWithCaption('Winner', match); |
| 224 |
|
| 225 |
/* |
| 226 |
* Get the token list for the rest of the command after what |
| 227 |
* we've parsed so far. |
| 228 |
* |
| 229 |
* Note that we'll start over and parse these tokens anew, |
| 230 |
* even though we might have parsed them already into a |
| 231 |
* subcommand on the previous iteration. Even if we already |
| 232 |
* parsed these tokens, we want to parse them again, because |
| 233 |
* we did not have a suitable context for evaluating the |
| 234 |
* semantic strengths of the possible structural |
| 235 |
* interpretations this command on the previous iteration; |
| 236 |
* for example, it was not possible to resolve noun phrases |
| 237 |
* in this command because we could not guess what the scope |
| 238 |
* would be when we got to this point. So, we'll simply |
| 239 |
* discard the previous match tree, and start over, treating |
| 240 |
* the new tokens as a brand new command. |
| 241 |
*/ |
| 242 |
nextIdx = match.getNextCommandIndex(); |
| 243 |
nextCommandTokens = toks.sublist(nextIdx); |
| 244 |
|
| 245 |
/* if the pending command list is empty, make it nil */ |
| 246 |
if (nextCommandTokens.length() == 0) |
| 247 |
nextCommandTokens = nil; |
| 248 |
|
| 249 |
/* |
| 250 |
* Get the part of the token list that the match doesn't use |
| 251 |
* at all. These are the tokens following the tokens we |
| 252 |
* matched. |
| 253 |
*/ |
| 254 |
extraIdx = match.tokenList.length() + 1; |
| 255 |
extraTokens = toks.sublist(extraIdx); |
| 256 |
|
| 257 |
/* |
| 258 |
* We now have the best match for the command tree. |
| 259 |
* |
| 260 |
* If the command has an actor clause, resolve the actor, so |
| 261 |
* that we can direct the command to that actor. If the |
| 262 |
* command has no actor clause, the command is to the actor |
| 263 |
* who issued the command. |
| 264 |
* |
| 265 |
* Do NOT process the actor clause if we've already done so. |
| 266 |
* If we edit the token list and retry the command after |
| 267 |
* this point, there is no need to resolve the actor part |
| 268 |
* again. Doing so could be bad - if resolving the actor |
| 269 |
* required player interaction, such as asking for help with |
| 270 |
* an ambiguous noun phrase, we do not want to go through |
| 271 |
* the same interaction again just because we have to edit |
| 272 |
* and retry a later part of the command. |
| 273 |
*/ |
| 274 |
if (match.hasTargetActor()) |
| 275 |
{ |
| 276 |
local actorResults; |
| 277 |
|
| 278 |
/* |
| 279 |
* If we haven't yet explicitly specified a target |
| 280 |
* actor, and the default target actor is different from |
| 281 |
* the issuing actor, then this is really a new command |
| 282 |
* from the issuing actor. |
| 283 |
* |
| 284 |
* First, an actor change mid-command is allowed only if |
| 285 |
* the issuing actor waits for NPC commands to be |
| 286 |
* carried out; if not, then we can't change the actor |
| 287 |
* mid-command. |
| 288 |
* |
| 289 |
* Second, since the command comes from the issuing |
| 290 |
* actor, not from the default target actor, we want to |
| 291 |
* issue the orders on the issuing actor's turn, so put |
| 292 |
* the command into the queue for the issuer, and let |
| 293 |
* the issuer re-parse the command on the issuer's next |
| 294 |
* turn. |
| 295 |
*/ |
| 296 |
if (!actorSpecified && issuingActor != targetActor) |
| 297 |
{ |
| 298 |
/* |
| 299 |
* don't allow the command if the issuing actor |
| 300 |
* doesn't wait for orders to be completed |
| 301 |
*/ |
| 302 |
if (!issuingActor.issueCommandsSynchronously) |
| 303 |
{ |
| 304 |
/* turn off any sense capturing */ |
| 305 |
senseContext.setSenseContext(nil, sight); |
| 306 |
|
| 307 |
/* show the error */ |
| 308 |
issuingActor.getParserMessageObj() |
| 309 |
.cannotChangeActor(); |
| 310 |
|
| 311 |
/* done */ |
| 312 |
return; |
| 313 |
} |
| 314 |
|
| 315 |
/* put the command into the issuer's queue */ |
| 316 |
issuingActor.addFirstPendingCommand( |
| 317 |
firstInSentence, issuingActor, toks); |
| 318 |
|
| 319 |
/* done */ |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
/* create an actor-specialized results object */ |
| 324 |
actorResults = new ActorResolveResults(); |
| 325 |
|
| 326 |
/* set up the actors in the results object */ |
| 327 |
actorResults.setActors(targetActor, issuingActor); |
| 328 |
|
| 329 |
/* resolve the actor object */ |
| 330 |
match.resolveNouns(issuingActor, targetActor, actorResults); |
| 331 |
|
| 332 |
/* get the target actor from the command */ |
| 333 |
targetActor = match.getTargetActor(); |
| 334 |
|
| 335 |
/* pull out the phrase specifying the actor */ |
| 336 |
actorPhrase = match.getActorPhrase(); |
| 337 |
|
| 338 |
/* |
| 339 |
* Copy antecedents from the issuing actor to the target |
| 340 |
* actor. Since the issuer is giving us a new command |
| 341 |
* here, pronouns will be given from the issuer's |
| 342 |
* perspective. |
| 343 |
*/ |
| 344 |
targetActor.copyPronounAntecedentsFrom(issuingActor); |
| 345 |
|
| 346 |
/* let the actor phrase know we're actually using it */ |
| 347 |
match.execActorPhrase(issuingActor); |
| 348 |
|
| 349 |
/* |
| 350 |
* Ask the target actor if it's interested in the |
| 351 |
* command at all. This only applies when the actor was |
| 352 |
* actually specified - if an actor wasn't specified, |
| 353 |
* the command is either directed to the issuer itself, |
| 354 |
* in which case the command will always be accepted; or |
| 355 |
* the command is a continuation of a command line |
| 356 |
* previously accepted. |
| 357 |
*/ |
| 358 |
if (!targetActor.acceptCommand(issuingActor)) |
| 359 |
{ |
| 360 |
/* |
| 361 |
* the command was immediately rejected - abandon |
| 362 |
* the command and any subsequent commands on the |
| 363 |
* same line |
| 364 |
*/ |
| 365 |
return; |
| 366 |
} |
| 367 |
|
| 368 |
/* note that an actor was specified */ |
| 369 |
actorSpecified = true; |
| 370 |
|
| 371 |
/* |
| 372 |
* Pull out the rest of the command (other than the |
| 373 |
* target actor specification) and start over with a |
| 374 |
* fresh parse of the whole thing. We must do this |
| 375 |
* because our tentative resolution pass that we used to |
| 376 |
* pick the best structural interpretation of the |
| 377 |
* command couldn't go far enough - since we didn't know |
| 378 |
* the actor involved, we weren't able to resolve nouns |
| 379 |
* in the rest of the command. Now that we know the |
| 380 |
* actor, we can start over and resolve everything in |
| 381 |
* the rest of the command, and thus choose the right |
| 382 |
* structural match for the command. |
| 383 |
*/ |
| 384 |
toks = match.getCommandTokens(); |
| 385 |
|
| 386 |
/* what follows obviously isn't first in the sentence */ |
| 387 |
firstInSentence = nil; |
| 388 |
|
| 389 |
/* go back to parse the rest */ |
| 390 |
continue parseTokenLoop; |
| 391 |
} |
| 392 |
|
| 393 |
/* pull out the first action from the command */ |
| 394 |
action = match.resolveFirstAction(issuingActor, targetActor); |
| 395 |
|
| 396 |
/* |
| 397 |
* If the winning interpretation had any unknown words, run |
| 398 |
* a resolution pass to resolve those interactively, if |
| 399 |
* possible. We want to do this before doing any other |
| 400 |
* interactive resolution because OOPS has the unique |
| 401 |
* property of forcing us to reparse the command; if we |
| 402 |
* allowed any other interactive resolution to happen before |
| 403 |
* processing an OOPS, we'd likely have to repeat the other |
| 404 |
* resolution on the reparse, which would confuse and |
| 405 |
* irritate users by asking the same question more than once |
| 406 |
* for what is apparently the same command. |
| 407 |
*/ |
| 408 |
if (rankings[1].unknownWordCount != 0) |
| 409 |
{ |
| 410 |
/* |
| 411 |
* resolve using the OOPS results gatherer - this runs |
| 412 |
* essentially the same preliminary resolution process |
| 413 |
* as the ranking results gatherer, but does perform |
| 414 |
* interactive resolution of unknown words via OOPS |
| 415 |
*/ |
| 416 |
match.resolveNouns( |
| 417 |
issuingActor, targetActor, |
| 418 |
new OopsResults(issuingActor, targetActor)); |
| 419 |
} |
| 420 |
|
| 421 |
/* |
| 422 |
* If the command is directed to a different actor than the |
| 423 |
* issuer, change to the target actor's sense context. |
| 424 |
* |
| 425 |
* On the other hand, if this is a conversational command |
| 426 |
* (e.g., BOB, YES or BOB, GOODBYE), execute it within the |
| 427 |
* sense context of the issuer, even when a target is |
| 428 |
* specified. Target actors in conversational commands |
| 429 |
* designate the interlocutor rather than the performing |
| 430 |
* actor: BOB, YES doesn't ask Bob to say "yes", but rather |
| 431 |
* means that the player character (the issuer) is saying |
| 432 |
* "yes" to Bob. |
| 433 |
*/ |
| 434 |
if (action != nil && action.isConversational(issuingActor)) |
| 435 |
senseContext.setSenseContext(issuingActor, sight); |
| 436 |
else if (actorSpecified && targetActor != issuingActor) |
| 437 |
senseContext.setSenseContext(targetActor, sight); |
| 438 |
|
| 439 |
/* set up a transcript to receive the command results */ |
| 440 |
withCommandTranscript(CommandTranscript, function() |
| 441 |
{ |
| 442 |
/* |
| 443 |
* Execute the action. |
| 444 |
* |
| 445 |
* If a target actor was specified, and it's not the same |
| 446 |
* as the issuing actor, this counts as a turn for the |
| 447 |
* issuing actor. |
| 448 |
*/ |
| 449 |
executeAction(targetActor, actorPhrase, issuingActor, |
| 450 |
actorSpecified && issuingActor != targetActor, |
| 451 |
action); |
| 452 |
}); |
| 453 |
|
| 454 |
/* |
| 455 |
* If we have anything remaining on the command line, insert |
| 456 |
* the remaining commands at the start of the target actor's |
| 457 |
* command queue, since we want the target actor to continue |
| 458 |
* with the rest of this command line as its next operation. |
| 459 |
* |
| 460 |
* Since the remainder of the line represents part of the |
| 461 |
* work the actor pulled out of the queue in order to call |
| 462 |
* us, put the remainder back in at the START of the actor's |
| 463 |
* queue - it was before anything else that might be in the |
| 464 |
* actor's queue before, so it should stay ahead of anything |
| 465 |
* else now. |
| 466 |
*/ |
| 467 |
if (nextCommandTokens != nil) |
| 468 |
{ |
| 469 |
/* |
| 470 |
* Prepend the remaining commands to the actor's queue. |
| 471 |
* The next command is the start of a new sentence if |
| 472 |
* the command we just executed ends the sentence. |
| 473 |
*/ |
| 474 |
targetActor.addFirstPendingCommand( |
| 475 |
match.isEndOfSentence(), issuingActor, nextCommandTokens); |
| 476 |
} |
| 477 |
|
| 478 |
/* |
| 479 |
* If the command was directed from the issuer to a |
| 480 |
* different target actor, and the issuer wants to wait for |
| 481 |
* the full set of issued commands to complete before |
| 482 |
* getting another turn, tell the issuer to begin waiting. |
| 483 |
*/ |
| 484 |
if (actorSpecified && issuingActor != targetActor) |
| 485 |
issuingActor.waitForIssuedCommand(targetActor); |
| 486 |
|
| 487 |
/* we're done */ |
| 488 |
return; |
| 489 |
} |
| 490 |
catch (ParseFailureException rfExc) |
| 491 |
{ |
| 492 |
/* |
| 493 |
* Parsing failed in such a way that we cannot proceed. |
| 494 |
* Tell the target actor to notify the issuing actor. |
| 495 |
*/ |
| 496 |
rfExc.notifyActor(targetActor, issuingActor); |
| 497 |
|
| 498 |
/* |
| 499 |
* the command cannot proceed, so abandon any remaining |
| 500 |
* tokens on the command line |
| 501 |
*/ |
| 502 |
return; |
| 503 |
} |
| 504 |
catch (CancelCommandLineException cclExc) |
| 505 |
{ |
| 506 |
/* |
| 507 |
* if there are any tokens remaining, the game might want to |
| 508 |
* show an explanation |
| 509 |
*/ |
| 510 |
if (nextCommandTokens != nil) |
| 511 |
targetActor.getParserMessageObj().explainCancelCommandLine(); |
| 512 |
|
| 513 |
/* stop now, abandoning the rest of the command line */ |
| 514 |
return; |
| 515 |
} |
| 516 |
catch (TerminateCommandException tcExc) |
| 517 |
{ |
| 518 |
/* |
| 519 |
* the command cannot proceed - we can't do any more |
| 520 |
* processing of this command, so simply return, abandoning |
| 521 |
* any additional tokens we have |
| 522 |
*/ |
| 523 |
return; |
| 524 |
} |
| 525 |
catch (RetryCommandTokensException rctExc) |
| 526 |
{ |
| 527 |
/* |
| 528 |
* We want to replace the current command's token list with |
| 529 |
* the new token list - get the new list, and then go back |
| 530 |
* and start over processing it. |
| 531 |
* |
| 532 |
* Note that we must retain any tokens beyond those that the |
| 533 |
* match tree used. The exception only edits the current |
| 534 |
* match tree's matched tokens, since it doesn't have access |
| 535 |
* to any of the original tokens beyond those, so we must |
| 536 |
* now add back in any tokens beyond the originals. |
| 537 |
*/ |
| 538 |
toks = rctExc.newTokens_ + extraTokens; |
| 539 |
|
| 540 |
/* go back and process the command again with the new tokens */ |
| 541 |
continue parseTokenLoop; |
| 542 |
} |
| 543 |
catch (ReplacementCommandStringException rcsExc) |
| 544 |
{ |
| 545 |
local str; |
| 546 |
|
| 547 |
/* retrieve the new command string from the exception */ |
| 548 |
str = rcsExc.newCommand_; |
| 549 |
|
| 550 |
/* |
| 551 |
* if the command string is nil, it means that the command |
| 552 |
* has been fully handled already, so we simply return |
| 553 |
* without any further work |
| 554 |
*/ |
| 555 |
if (str == nil) |
| 556 |
return; |
| 557 |
|
| 558 |
/* |
| 559 |
* Replace the entire command string with the one from the |
| 560 |
* exception - this cancels any previous command that we |
| 561 |
* had. |
| 562 |
*/ |
| 563 |
toks = cmdTokenizer.tokenize(str); |
| 564 |
|
| 565 |
/* |
| 566 |
* we have a brand new command line, so we're starting a |
| 567 |
* brand new sentence |
| 568 |
*/ |
| 569 |
firstInSentence = true; |
| 570 |
|
| 571 |
/* set the issuing and target actor according to the exception */ |
| 572 |
issuingActor = rcsExc.issuingActor_; |
| 573 |
targetActor = rcsExc.targetActor_; |
| 574 |
|
| 575 |
/* |
| 576 |
* Put this work into the target actor's work queue, so that |
| 577 |
* the issuer will carry out the command at the next |
| 578 |
* opportunity. This is a brand new command line, so it |
| 579 |
* starts a new sentence. |
| 580 |
*/ |
| 581 |
targetActor.addPendingCommand(true, issuingActor, toks); |
| 582 |
|
| 583 |
/* we're done processing this command */ |
| 584 |
return; |
| 585 |
} |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
/* ------------------------------------------------------------------------ */ |
| 590 |
/* |
| 591 |
* GlobalRemapping makes it possible to transform one action into another |
| 592 |
* globally - as opposed to the remapTo mechanism, which lets an object |
| 593 |
* involved in the command perform a remapping. The key difference |
| 594 |
* between global remappings and remapTo is that the latter can't happen |
| 595 |
* until after the objects are resolved (for fairly obvious reasons: each |
| 596 |
* remapTo mapping is associated with an object, so you can't know which |
| 597 |
* mapping to apply until you know which object is involved). In |
| 598 |
* contrast, global remappings are performed *before* object resolution - |
| 599 |
* this is possible because the mappings don't depend on the objects |
| 600 |
* involved in the action. |
| 601 |
* |
| 602 |
* Whenever an action is about to be executed, the parser runs through |
| 603 |
* all of the defined global remappings, and gives each one a chance to |
| 604 |
* remap the command. If any remapping succeeds, we replace the original |
| 605 |
* command with the remapped version, then repeat the scan of the global |
| 606 |
* remapping list from the start - we do another complete scan of the |
| 607 |
* list in case there's another global mapping that applies to the |
| 608 |
* remapped version of the command. We repeat this process until we make |
| 609 |
* it through the whole list without finding a remapping. |
| 610 |
* |
| 611 |
* GlobalRemapping instances are added to the master list of mappings |
| 612 |
* automatically at pre-init time, and any time you construct one |
| 613 |
* dynamically with 'new'. |
| 614 |
*/ |
| 615 |
class GlobalRemapping: PreinitObject |
| 616 |
/* |
| 617 |
* Check for and apply a remapping. This method must be implemented |
| 618 |
* in each GlobalRemapping instance to perform the actual remapping |
| 619 |
* work. |
| 620 |
* |
| 621 |
* This routine should first check to see if the command is relevant |
| 622 |
* to this remapping. In most cases, this means checking that the |
| 623 |
* command matches some template, such as having a particular action |
| 624 |
* (verb) and combination of potential objects. Note that the |
| 625 |
* objects aren't fully resolved during global remapping - the whole |
| 626 |
* point of global remapping is to catch certain phrasings before we |
| 627 |
* get to the noun resolution phase - but the *phrases* involved will |
| 628 |
* be known, so the range of potential matches is knowable. |
| 629 |
* |
| 630 |
* If the routine decides that the action isn't relevant to this |
| 631 |
* remapping, it should simply return nil. |
| 632 |
* |
| 633 |
* If the action decides to remap the action, it must create a new |
| 634 |
* Action object representing the replacement version of the command. |
| 635 |
* Then, return a list, [targetActor, action], giving the new target |
| 636 |
* actor and the new action. You don't have to change the target |
| 637 |
* actor, of course, but it's included in the result so that you can |
| 638 |
* change it if you want to. For example, you could use this to |
| 639 |
* remap a command of the form "X, GIVE ME Y" to "ME, ASK X FOR Y" - |
| 640 |
* note that the target actor changes from X to ME. |
| 641 |
*/ |
| 642 |
getRemapping(issuingActor, targetActor, action) |
| 643 |
{ |
| 644 |
/* |
| 645 |
* this must be overridden to perform the actual remapping; by |
| 646 |
* default, simply return nil to indicate that we don't want to |
| 647 |
* remap this action |
| 648 |
*/ |
| 649 |
return nil; |
| 650 |
} |
| 651 |
|
| 652 |
/* |
| 653 |
* Remapping order - the parser applies global remappings in |
| 654 |
* ascending order of this value. In most cases, the order shouldn't |
| 655 |
* matter, since most remappings should be narrow enough that a given |
| 656 |
* command will only be subject to one remapping rule. However, in |
| 657 |
* some cases you might need to define rules that overlap, so the |
| 658 |
* ordering lets you specify which one goes first. In most cases |
| 659 |
* you'll want to apply the more specific rule first. |
| 660 |
*/ |
| 661 |
remappingOrder = 100 |
| 662 |
|
| 663 |
/* |
| 664 |
* Static class method: look for a remapping. This runs through the |
| 665 |
* master list of mappings, looking for a mapping that applies to the |
| 666 |
* given command. If we find one, we'll replace the command with the |
| 667 |
* remapped version, then start over with a fresh scan of the entire |
| 668 |
* list to see if there's a remapping for the *new* version of the |
| 669 |
* command. We repeat this until we get through the whole list |
| 670 |
* without finding a remapping. |
| 671 |
* |
| 672 |
* The return value is a list, [targetActor, action], giving the |
| 673 |
* resulting target actor and new action object. If we don't find |
| 674 |
* any remapping, this will simply be the original values passed in |
| 675 |
* as our arguments; if we do find a remapping, this will be the new |
| 676 |
* version of the command. |
| 677 |
*/ |
| 678 |
findGlobalRemapping(issuingActor, targetActor, action) |
| 679 |
{ |
| 680 |
/* get the global remapping list */ |
| 681 |
local v = GlobalRemapping.allGlobalRemappings; |
| 682 |
local cnt = v.length(); |
| 683 |
|
| 684 |
/* if necessary, sort the list */ |
| 685 |
if (GlobalRemapping.listNeedsSorting) |
| 686 |
{ |
| 687 |
/* sort it by ascending remappingOrder value */ |
| 688 |
v.sort(SortAsc, {a, b: a.remappingOrder - b.remappingOrder}); |
| 689 |
|
| 690 |
/* note that it's now sorted */ |
| 691 |
GlobalRemapping.listNeedsSorting = nil; |
| 692 |
} |
| 693 |
|
| 694 |
/* |
| 695 |
* iterate through the list repeatedly, until we make it all the |
| 696 |
* way through without finding a mapping |
| 697 |
*/ |
| 698 |
for (local done = nil ; !done ; ) |
| 699 |
{ |
| 700 |
/* presume we won't find a remapping on this iteration */ |
| 701 |
done = true; |
| 702 |
|
| 703 |
/* run through the list, looking for a remapping */ |
| 704 |
for (local i = 1 ; i <= cnt ; ++i) |
| 705 |
{ |
| 706 |
local rm; |
| 707 |
|
| 708 |
/* check for a remapping */ |
| 709 |
rm = v[i].getRemapping(issuingActor, targetActor, action); |
| 710 |
if (rm != nil) |
| 711 |
{ |
| 712 |
/* found a remapping - apply it */ |
| 713 |
targetActor = rm[1]; |
| 714 |
action = rm[2]; |
| 715 |
|
| 716 |
/* |
| 717 |
* we found a remapping, so we have to repeat the |
| 718 |
* scan of the whole list - note that we're not done, |
| 719 |
* and break out of the current scan so that we start |
| 720 |
* over with a fresh scan |
| 721 |
*/ |
| 722 |
done = nil; |
| 723 |
break; |
| 724 |
} |
| 725 |
} |
| 726 |
} |
| 727 |
|
| 728 |
/* return the final version of the command */ |
| 729 |
return [targetActor, action]; |
| 730 |
} |
| 731 |
|
| 732 |
/* pre-initialization: add each instance to the master list */ |
| 733 |
execute() |
| 734 |
{ |
| 735 |
/* add me to the master list */ |
| 736 |
registerGlobalRemapping(); |
| 737 |
} |
| 738 |
|
| 739 |
/* construction: add myself to the master list */ |
| 740 |
construct() |
| 741 |
{ |
| 742 |
/* add me to the master list */ |
| 743 |
registerGlobalRemapping(); |
| 744 |
} |
| 745 |
|
| 746 |
/* register myself with the global list, making this an active mapping */ |
| 747 |
registerGlobalRemapping() |
| 748 |
{ |
| 749 |
/* add myself to the global list */ |
| 750 |
GlobalRemapping.allGlobalRemappings.append(self); |
| 751 |
|
| 752 |
/* note that a sort is required the next time we run */ |
| 753 |
GlobalRemapping.listNeedsSorting = true; |
| 754 |
} |
| 755 |
|
| 756 |
/* |
| 757 |
* unregister - this removes me from the global list, making this |
| 758 |
* mapping inactive: after being unregistered, the parser won't apply |
| 759 |
* this mapping to new commands |
| 760 |
*/ |
| 761 |
unregisterGlobalRemapping() |
| 762 |
{ |
| 763 |
GlobalRemapping.allGlobalRemappings.removeElement(self); |
| 764 |
} |
| 765 |
|
| 766 |
/* |
| 767 |
* Static class property: the master list of remappings. We build |
| 768 |
* this automatically at preinit time, and manipulate it via our |
| 769 |
* constructor. |
| 770 |
*/ |
| 771 |
allGlobalRemappings = static new Vector(10) |
| 772 |
|
| 773 |
/* |
| 774 |
* static class property: the master list needs to be sorted; this is |
| 775 |
* set to true each time we update the list, so that the list scanner |
| 776 |
* knows to sort it before doing its scan |
| 777 |
*/ |
| 778 |
listNeedsSorting = nil |
| 779 |
; |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
/* ------------------------------------------------------------------------ */ |
| 784 |
/* |
| 785 |
* Execute an action, as specified by an Action object. We'll resolve |
| 786 |
* the nouns in the action, then perform the action. |
| 787 |
*/ |
| 788 |
executeAction(targetActor, targetActorPhrase, |
| 789 |
issuingActor, countsAsIssuerTurn, action) |
| 790 |
{ |
| 791 |
local rm, results; |
| 792 |
|
| 793 |
startOver: |
| 794 |
/* check for a global remapping */ |
| 795 |
rm = GlobalRemapping.findGlobalRemapping( |
| 796 |
issuingActor, targetActor, action); |
| 797 |
targetActor = rm[1]; |
| 798 |
action = rm[2]; |
| 799 |
|
| 800 |
/* create a basic results object to handle the resolution */ |
| 801 |
results = new BasicResolveResults(); |
| 802 |
|
| 803 |
/* set up the actors in the results object */ |
| 804 |
results.setActors(targetActor, issuingActor); |
| 805 |
|
| 806 |
/* catch any "remap" signals while resolving noun phrases */ |
| 807 |
try |
| 808 |
{ |
| 809 |
/* resolve noun phrases */ |
| 810 |
action.resolveNouns(issuingActor, targetActor, results); |
| 811 |
} |
| 812 |
catch (RemapActionSignal sig) |
| 813 |
{ |
| 814 |
/* mark the new action as remapped */ |
| 815 |
sig.action_.setRemapped(action); |
| 816 |
|
| 817 |
/* get the new action from the signal */ |
| 818 |
action = sig.action_; |
| 819 |
|
| 820 |
/* start over with the new action */ |
| 821 |
goto startOver; |
| 822 |
} |
| 823 |
|
| 824 |
/* |
| 825 |
* Check to see if we should create an undo savepoint for the |
| 826 |
* command. If the action is not marked for inclusion in the undo |
| 827 |
* log, there is no need to log a savepoint for it. |
| 828 |
* |
| 829 |
* Don't save undo for nested commands. A nested command is part of |
| 830 |
* a main command, and we only want to save undo for the main |
| 831 |
* command, not for its individual sub-commands. |
| 832 |
*/ |
| 833 |
if (action.includeInUndo |
| 834 |
&& action.parentAction == nil |
| 835 |
&& (targetActor.isPlayerChar() |
| 836 |
|| (issuingActor.isPlayerChar() && countsAsIssuerTurn))) |
| 837 |
{ |
| 838 |
/* |
| 839 |
* Remember the command we're about to perform, so that if we |
| 840 |
* undo to here we'll be able to report what we undid. Note that |
| 841 |
* we do this *before* setting the savepoint, because we want |
| 842 |
* after the undo to know the command we were about to issue at |
| 843 |
* the savepoint. |
| 844 |
*/ |
| 845 |
libGlobal.lastCommandForUndo = action.getOrigText(); |
| 846 |
libGlobal.lastActorForUndo = |
| 847 |
(targetActorPhrase == nil |
| 848 |
? nil |
| 849 |
: targetActorPhrase.getOrigText()); |
| 850 |
|
| 851 |
/* |
| 852 |
* set a savepoint here, so that we on 'undo' we'll restore |
| 853 |
* conditions to what they were just before we executed this |
| 854 |
* command |
| 855 |
*/ |
| 856 |
savepoint(); |
| 857 |
} |
| 858 |
|
| 859 |
/* |
| 860 |
* If this counts as a turn for the issuer, adjust the issuer's busy |
| 861 |
* time. |
| 862 |
* |
| 863 |
* However, this doesn't apply if the command is conversational (that |
| 864 |
* is, it's something like "BOB, HELLO"). A conversational command |
| 865 |
* is conceptually carried out by the issuer, not the target actor, |
| 866 |
* since the action consists of the issuer actually saying something |
| 867 |
* to the target actor. The normal turn accounting in Action will |
| 868 |
* count a conversational command this way, so we don't have to do |
| 869 |
* the extra bookkeeping for such a command here. |
| 870 |
*/ |
| 871 |
if (countsAsIssuerTurn && !action.isConversational(issuingActor)) |
| 872 |
{ |
| 873 |
/* |
| 874 |
* note in the issuer that the target is the most recent |
| 875 |
* conversational partner |
| 876 |
*/ |
| 877 |
issuingActor.lastInterlocutor = targetActor; |
| 878 |
|
| 879 |
/* make the issuer busy for the order-giving interval */ |
| 880 |
issuingActor.addBusyTime(nil, |
| 881 |
issuingActor.orderingTime(targetActor)); |
| 882 |
|
| 883 |
/* notify the target that this will be a non-idle turn */ |
| 884 |
targetActor.nonIdleTurn(); |
| 885 |
} |
| 886 |
|
| 887 |
/* |
| 888 |
* If the issuer is directing the command to a different actor, and |
| 889 |
* it's not a conversational command, check with the target actor to |
| 890 |
* see if it wants to accept the command. Don't check |
| 891 |
* conversational commands, since these aren't of the nature of |
| 892 |
* orders to be obeyed. |
| 893 |
*/ |
| 894 |
if (issuingActor != targetActor |
| 895 |
&& !action.isConversational(issuingActor) |
| 896 |
&& !targetActor.obeyCommand(issuingActor, action)) |
| 897 |
{ |
| 898 |
/* |
| 899 |
* if the issuing actor's "ordering time" is zero, make this take |
| 900 |
* up a turn anyway, just for the refusal |
| 901 |
*/ |
| 902 |
if (issuingActor.orderingTime(targetActor) == 0) |
| 903 |
issuingActor.addBusyTime(nil, 1); |
| 904 |
|
| 905 |
/* |
| 906 |
* Since we're aborting the command, we won't get into the normal |
| 907 |
* execution for it. However, we might still want to save it for |
| 908 |
* an attempted re-issue with AGAIN, so do so explicitly now. |
| 909 |
*/ |
| 910 |
action.saveActionForAgain(issuingActor, countsAsIssuerTurn, |
| 911 |
targetActor, targetActorPhrase); |
| 912 |
|
| 913 |
/* |
| 914 |
* This command was rejected, so don't process it any further, |
| 915 |
* and give up on processing any remaining commands on the same |
| 916 |
* command line. |
| 917 |
*/ |
| 918 |
throw new TerminateCommandException(); |
| 919 |
} |
| 920 |
|
| 921 |
/* execute the action */ |
| 922 |
action.doAction(issuingActor, targetActor, targetActorPhrase, |
| 923 |
countsAsIssuerTurn); |
| 924 |
} |
| 925 |
|
| 926 |
/* ------------------------------------------------------------------------ */ |
| 927 |
/* |
| 928 |
* Try an implicit action. |
| 929 |
* |
| 930 |
* Returns true if the action was attempted, whether or not it |
| 931 |
* succeeded, nil if the command was not even attempted. We will not |
| 932 |
* attempt an implied command that verifies as "dangerous," since this |
| 933 |
* means that it should be obvious to the player character that such a |
| 934 |
* command should not be performed lightly. |
| 935 |
*/ |
| 936 |
_tryImplicitAction(issuingActor, targetActor, msgProp, actionClass, [objs]) |
| 937 |
{ |
| 938 |
local action; |
| 939 |
|
| 940 |
/* create an instance of the desired action class */ |
| 941 |
action = actionClass.createActionInstance(); |
| 942 |
|
| 943 |
/* mark the action as implicit */ |
| 944 |
action.setImplicit(msgProp); |
| 945 |
|
| 946 |
/* install the resolved objects in the action */ |
| 947 |
action.setResolvedObjects(objs...); |
| 948 |
|
| 949 |
/* |
| 950 |
* For an implicit action, we must check the objects involved to make |
| 951 |
* sure they're in scope. If any of the objects aren't in scope, |
| 952 |
* there is no way the actor would know to perform the command, so |
| 953 |
* the command would not be implied in the first place. Simply fail |
| 954 |
* without trying the command. |
| 955 |
*/ |
| 956 |
if (!action.resolvedObjectsInScope()) |
| 957 |
return nil; |
| 958 |
|
| 959 |
/* |
| 960 |
* catch the abort-implicit signal, so we can turn it into a result |
| 961 |
* code for our caller instead of an exception |
| 962 |
*/ |
| 963 |
try |
| 964 |
{ |
| 965 |
/* in NPC mode, add a command separator before each implied action */ |
| 966 |
if (targetActor.impliedCommandMode() == ModeNPC) |
| 967 |
gTranscript.addCommandSep(); |
| 968 |
|
| 969 |
/* execute the action */ |
| 970 |
action.doAction(issuingActor, targetActor, nil, nil); |
| 971 |
|
| 972 |
/* |
| 973 |
* if the actor is in "NPC" mode for implied commands, do some |
| 974 |
* extra work |
| 975 |
*/ |
| 976 |
if (targetActor.impliedCommandMode() == ModeNPC) |
| 977 |
{ |
| 978 |
/* |
| 979 |
* we're in NPC mode, so if the implied action failed, then |
| 980 |
* act as though the command had never been attempted |
| 981 |
*/ |
| 982 |
if (gTranscript.actionFailed(action)) |
| 983 |
{ |
| 984 |
/* the implied command failed - act like we didn't even try */ |
| 985 |
return nil; |
| 986 |
} |
| 987 |
|
| 988 |
/* |
| 989 |
* In "NPC" mode, we display the results from implied |
| 990 |
* commands as though they had been explicitly entered as |
| 991 |
* separate actions. So, add visual separation after the |
| 992 |
* results from the implied command. |
| 993 |
*/ |
| 994 |
gTranscript.addCommandSep(); |
| 995 |
} |
| 996 |
|
| 997 |
/* tell the caller we at least tried to execute the command */ |
| 998 |
return true; |
| 999 |
} |
| 1000 |
catch (AbortImplicitSignal sig) |
| 1001 |
{ |
| 1002 |
/* tell the caller we didn't execute the command at all */ |
| 1003 |
return nil; |
| 1004 |
} |
| 1005 |
catch (ParseFailureException exc) |
| 1006 |
{ |
| 1007 |
/* |
| 1008 |
* Parse failure. If the actor is in NPC mode, we can't have |
| 1009 |
* asked for a new command, so this must be some failure in |
| 1010 |
* processing the implied command itself; most likely, we tried |
| 1011 |
* to resolve a missing object or the like and found that we |
| 1012 |
* couldn't perform interactive resolution (because of the NPC |
| 1013 |
* mode). In this case, simply treat this as a failure of the |
| 1014 |
* implied command itself, and act as though we didn't even try |
| 1015 |
* the implied command. |
| 1016 |
* |
| 1017 |
* If the actor is in player mode, then we *can* perform |
| 1018 |
* interactive resolution, so we won't have thrown a parser |
| 1019 |
* failure before trying to solve the problem interactively. The |
| 1020 |
* failure must therefore be in an interactive response. In this |
| 1021 |
* case, simply re-throw the failure so that it reaches the main |
| 1022 |
* parser. |
| 1023 |
*/ |
| 1024 |
if (targetActor.impliedCommandMode() == ModeNPC) |
| 1025 |
{ |
| 1026 |
/* NPC mode - the implied command itself failed */ |
| 1027 |
return nil; |
| 1028 |
} |
| 1029 |
else |
| 1030 |
{ |
| 1031 |
/* player mode - interactive resolution failed */ |
| 1032 |
throw exc; |
| 1033 |
} |
| 1034 |
} |
| 1035 |
} |
| 1036 |
|
| 1037 |
/* ------------------------------------------------------------------------ */ |
| 1038 |
/* |
| 1039 |
* Run a replacement action. |
| 1040 |
*/ |
| 1041 |
_replaceAction(actor, actionClass, [objs]) |
| 1042 |
{ |
| 1043 |
/* run the replacement action as a nested action */ |
| 1044 |
_nestedAction(true, actor, actionClass, objs...); |
| 1045 |
|
| 1046 |
/* the invoking command is done */ |
| 1047 |
exit; |
| 1048 |
} |
| 1049 |
|
| 1050 |
/* ------------------------------------------------------------------------ */ |
| 1051 |
/* |
| 1052 |
* Resolve and execute a replacement action. This differs from the |
| 1053 |
* normal replacement action execution in that the action we execute |
| 1054 |
* requires resolution before execution. |
| 1055 |
*/ |
| 1056 |
resolveAndReplaceAction(newAction) |
| 1057 |
{ |
| 1058 |
/* prepare the replacement action */ |
| 1059 |
prepareNestedAction(true, nil, newAction); |
| 1060 |
|
| 1061 |
/* |
| 1062 |
* resolve and execute the new action, using the same target and |
| 1063 |
* issuing actors as the original action |
| 1064 |
*/ |
| 1065 |
executeAction(gActor, nil, gIssuingActor, nil, newAction); |
| 1066 |
|
| 1067 |
/* the invoking command has been replaced, so it's done */ |
| 1068 |
exit; |
| 1069 |
} |
| 1070 |
|
| 1071 |
/* ------------------------------------------------------------------------ */ |
| 1072 |
/* |
| 1073 |
* Run an action as a new turn. Returns the CommandTranscript describing |
| 1074 |
* the action's results. |
| 1075 |
*/ |
| 1076 |
_newAction(transcriptClass, issuingActor, targetActor, actionClass, [objs]) |
| 1077 |
{ |
| 1078 |
local action; |
| 1079 |
|
| 1080 |
/* create an instance of the desired action class */ |
| 1081 |
action = actionClass.createActionInstance(); |
| 1082 |
|
| 1083 |
/* execute the command with the action instance */ |
| 1084 |
return newActionObj(transcriptClass, issuingActor, targetActor, |
| 1085 |
action, objs...); |
| 1086 |
} |
| 1087 |
|
| 1088 |
/* |
| 1089 |
* Run an action as a new turn. This is almost the same as _newAction, |
| 1090 |
* but should be used when the caller has already explicitly created an |
| 1091 |
* instance of the Action to be performed. |
| 1092 |
* |
| 1093 |
* If issuingActor is nil, we'll use the current global issuing actor; if |
| 1094 |
* that's also nil, we'll use the target actor. |
| 1095 |
* |
| 1096 |
* Returns a CommandTranscript object describing the result of the |
| 1097 |
* action. |
| 1098 |
*/ |
| 1099 |
newActionObj(transcriptClass, issuingActor, targetActor, actionObj, [objs]) |
| 1100 |
{ |
| 1101 |
/* create the results object and install it as the global transcript */ |
| 1102 |
return withCommandTranscript(transcriptClass, function() |
| 1103 |
{ |
| 1104 |
/* install the resolved objects in the action */ |
| 1105 |
actionObj.setResolvedObjects(objs...); |
| 1106 |
|
| 1107 |
/* |
| 1108 |
* if the issuing actor isn't specified, use the current global |
| 1109 |
* issuing actor; if that's also not set, use the target actor |
| 1110 |
*/ |
| 1111 |
if (issuingActor == nil) |
| 1112 |
issuingActor = gIssuingActor; |
| 1113 |
if (issuingActor == nil) |
| 1114 |
issuingActor = targetActor; |
| 1115 |
|
| 1116 |
/* |
| 1117 |
* Execute the given action. Because this is a new action, |
| 1118 |
* execute the action in a new sense context for the given actor. |
| 1119 |
*/ |
| 1120 |
callWithSenseContext(targetActor.isPlayerChar() |
| 1121 |
? nil : targetActor, sight, |
| 1122 |
{: actionObj.doAction(issuingActor, targetActor, nil, nil)}); |
| 1123 |
|
| 1124 |
/* return the current global transcript object */ |
| 1125 |
return gTranscript; |
| 1126 |
}); |
| 1127 |
} |
| 1128 |
|
| 1129 |
/* ------------------------------------------------------------------------ */ |
| 1130 |
/* |
| 1131 |
* Run a nested action. 'isReplacement' has the same meaning as in |
| 1132 |
* execNestedAction(). |
| 1133 |
*/ |
| 1134 |
_nestedAction(isReplacement, actor, actionClass, [objs]) |
| 1135 |
{ |
| 1136 |
local action; |
| 1137 |
|
| 1138 |
/* create an instance of the desired action class */ |
| 1139 |
action = actionClass.createActionInstance(); |
| 1140 |
|
| 1141 |
/* install the resolved objects in the action */ |
| 1142 |
action.setResolvedObjects(objs...); |
| 1143 |
|
| 1144 |
/* execute the new action */ |
| 1145 |
execNestedAction(isReplacement, nil, actor, action); |
| 1146 |
} |
| 1147 |
|
| 1148 |
/* |
| 1149 |
* Execute a fully-constructed nested action. |
| 1150 |
* |
| 1151 |
* 'isReplacement' indicates whether the action is a full replacement or |
| 1152 |
* an ordinary nested action. If it's a replacement, then we use the |
| 1153 |
* game time taken by the replacement, and set the enclosing action |
| 1154 |
* (i.e., the current gAction) to take zero time. If it's an ordinary |
| 1155 |
* nested action, then we consider the nested action to take zero time, |
| 1156 |
* using the current action's time as the overall command time. |
| 1157 |
* |
| 1158 |
* 'isRemapping' indicates whether or not this is a remapped action. If |
| 1159 |
* we're remapping from one action to another, this will be true; for |
| 1160 |
* any other kind of nested or replacement action, this should be nil. |
| 1161 |
*/ |
| 1162 |
execNestedAction(isReplacement, isRemapping, actor, action) |
| 1163 |
{ |
| 1164 |
/* prepare the nested action */ |
| 1165 |
prepareNestedAction(isReplacement, isRemapping, action); |
| 1166 |
|
| 1167 |
/* execute the new action in the actor's sense context */ |
| 1168 |
callWithSenseContext( |
| 1169 |
actor.isPlayerChar() ? nil : actor, sight, |
| 1170 |
{: action.doAction(gIssuingActor, actor, nil, nil) }); |
| 1171 |
} |
| 1172 |
|
| 1173 |
/* |
| 1174 |
* Prepare a nested or replacement action for execution. |
| 1175 |
*/ |
| 1176 |
prepareNestedAction(isReplacement, isRemapping, action) |
| 1177 |
{ |
| 1178 |
/* |
| 1179 |
* if the original action is an implicit command, make the new |
| 1180 |
* command implicit as well |
| 1181 |
*/ |
| 1182 |
if (gAction.isImplicit) |
| 1183 |
{ |
| 1184 |
/* |
| 1185 |
* make the new action implicit, but don't describe it as a |
| 1186 |
* separate implicit command - it's effectively part of the |
| 1187 |
* original implicit command |
| 1188 |
*/ |
| 1189 |
action.setImplicit(nil); |
| 1190 |
} |
| 1191 |
|
| 1192 |
/* mark the new action as nested */ |
| 1193 |
action.setNested(); |
| 1194 |
|
| 1195 |
/* a nested action is part of the enclosing action */ |
| 1196 |
action.setOriginalAction(gAction); |
| 1197 |
|
| 1198 |
/* if this is a remapping, mark it as such */ |
| 1199 |
if (isRemapping) |
| 1200 |
action.setRemapped(gAction); |
| 1201 |
|
| 1202 |
/* |
| 1203 |
* Set either the nested action's time or the enclosing (current) |
| 1204 |
* action's time to zero - we want to count only the time of one |
| 1205 |
* command or the other. |
| 1206 |
* |
| 1207 |
* If we're running an ordinary nested command, set the nested |
| 1208 |
* command's time to zero, since we want to consider it just a part |
| 1209 |
* of the enclosing command and thus to take no time of its own. |
| 1210 |
* |
| 1211 |
* If we're running a full replacement command, and we're replacing |
| 1212 |
* something other than an implied command, don't consider the |
| 1213 |
* enclosing command to take any time, since the enclosing command is |
| 1214 |
* carrying out its entire function via the replacement and thus |
| 1215 |
* requires no time of its own. If we're replacing an implied |
| 1216 |
* command, this doesn't apply, since the implied command defers to |
| 1217 |
* its enclosing command for timing. If we're replacing a command |
| 1218 |
* that already has zero action time, this also doesn't apply, since |
| 1219 |
* we're presumably replacing a command that's itself nested. |
| 1220 |
*/ |
| 1221 |
if (isReplacement && !gAction.isImplicit && gAction.actionTime != 0) |
| 1222 |
gAction.zeroActionTime(); |
| 1223 |
else |
| 1224 |
action.actionTime = 0; |
| 1225 |
} |
| 1226 |
|
| 1227 |
/* ------------------------------------------------------------------------ */ |
| 1228 |
/* |
| 1229 |
* Run a previously-executed command as a nested action, re-resolving |
| 1230 |
* all of its objects to ensure they are still valid. |
| 1231 |
*/ |
| 1232 |
nestedActionAgain(action) |
| 1233 |
{ |
| 1234 |
/* reset the any cached information for the new command context */ |
| 1235 |
action.resetAction(); |
| 1236 |
|
| 1237 |
/* mark the action as nested */ |
| 1238 |
action.setNested(); |
| 1239 |
action.setOriginalAction(gAction); |
| 1240 |
|
| 1241 |
/* |
| 1242 |
* do not count any time for the nested action, since it's merely |
| 1243 |
* part of the main turn and doesn't count as a separate turn of its |
| 1244 |
* own |
| 1245 |
*/ |
| 1246 |
action.actionTime = 0; |
| 1247 |
|
| 1248 |
/* execute the command */ |
| 1249 |
executeAction(gActor, nil, gIssuingActor, nil, action); |
| 1250 |
} |
| 1251 |
|
| 1252 |
|
| 1253 |
/* ------------------------------------------------------------------------ */ |
| 1254 |
/* |
| 1255 |
* Run some code in a simulated Action environment. We'll create a dummy |
| 1256 |
* instance of the given Action class, and set up a command transcript, |
| 1257 |
* then invoke the function. This is useful for writing daemon code that |
| 1258 |
* needs to invoke other code that's set up to expect a normal action |
| 1259 |
* processing environment. |
| 1260 |
*/ |
| 1261 |
withActionEnv(actionClass, actor, func) |
| 1262 |
{ |
| 1263 |
local oldAction, oldActor; |
| 1264 |
|
| 1265 |
/* remember the old globals */ |
| 1266 |
oldAction = gAction; |
| 1267 |
oldActor = gActor; |
| 1268 |
|
| 1269 |
try |
| 1270 |
{ |
| 1271 |
/* set up a dummy action */ |
| 1272 |
gAction = actionClass.createInstance(); |
| 1273 |
|
| 1274 |
/* use the player character as the actor */ |
| 1275 |
gActor = actor; |
| 1276 |
|
| 1277 |
/* |
| 1278 |
* execute the function with a command transcript active; obtain |
| 1279 |
* and return the return value of the function |
| 1280 |
*/ |
| 1281 |
return withCommandTranscript(CommandTranscript, func); |
| 1282 |
} |
| 1283 |
finally |
| 1284 |
{ |
| 1285 |
/* restore globals on the way out */ |
| 1286 |
gAction = oldAction; |
| 1287 |
gActor = oldActor; |
| 1288 |
} |
| 1289 |
} |
| 1290 |
|
| 1291 |
|
| 1292 |
/* ------------------------------------------------------------------------ */ |
| 1293 |
/* |
| 1294 |
* Exit signal. This signal indicates that we're finished with the |
| 1295 |
* entire command execution sequence for an action; the remainder of the |
| 1296 |
* command execution sequence is to be skipped for the action. Throw |
| 1297 |
* this from within the command execution sequence in order to skip |
| 1298 |
* directly to the end-of-turn processing. This skips everything |
| 1299 |
* remaining in the action, including after-action notification and the |
| 1300 |
* like. This signal skips directly past the 'afterAction' phase of the |
| 1301 |
* command. |
| 1302 |
* |
| 1303 |
* Note that this doesn't prevent further processing of the same command |
| 1304 |
* if there are multiple objects involved, and it doesn't affect |
| 1305 |
* processing of additional commands on the same command line. If you |
| 1306 |
* want to cancel further iteration of the same command for additional |
| 1307 |
* objects, call gAction.cancelIteration(). |
| 1308 |
*/ |
| 1309 |
class ExitSignal: Exception |
| 1310 |
; |
| 1311 |
|
| 1312 |
/* |
| 1313 |
* Exit Action signal. This signal indicates that we're finished with |
| 1314 |
* the execAction portion of processing the command, but we still want |
| 1315 |
* to proceed with the rest of the command as normal. This can be used |
| 1316 |
* when a step in the action processing wants to preempt any of the more |
| 1317 |
* default processing that would normally follow. This skips directly |
| 1318 |
* to the 'afterAction' phase of the command. |
| 1319 |
* |
| 1320 |
* Note that this doesn't prevent further processing of the same command |
| 1321 |
* if there are multiple objects involved, and it doesn't affect |
| 1322 |
* processing of additional commands on the same command line. If you |
| 1323 |
* want to cancel further iteration of the same command for additional |
| 1324 |
* objects, call gAction.cancelIteration(). |
| 1325 |
*/ |
| 1326 |
class ExitActionSignal: Exception |
| 1327 |
; |
| 1328 |
|
| 1329 |
/* |
| 1330 |
* Abort implicit command signal. This exception indicates that we are |
| 1331 |
* aborting an implicit command without having tried to execute the |
| 1332 |
* command at all. This is thrown when an implied command is to be |
| 1333 |
* aborted before it's even attempted, such as when verification shows |
| 1334 |
* the command is obviously dangerous and thus should never be attempted |
| 1335 |
* without the player having explicitly requesting it. |
| 1336 |
*/ |
| 1337 |
class AbortImplicitSignal: Exception |
| 1338 |
; |
| 1339 |
|
| 1340 |
/* ------------------------------------------------------------------------ */ |
| 1341 |
/* |
| 1342 |
* Action Remap signal. This signal can be thrown only during the noun |
| 1343 |
* phrase resolution phase of execution, and indicates that we want to |
| 1344 |
* remap the action to a different action, specified in the signal. |
| 1345 |
* |
| 1346 |
* This is useful when an object is always used in a special way, so |
| 1347 |
* that a generic verb used with the object must be mapped to a more |
| 1348 |
* specific verb on the object. For example, a game with a generic USE |
| 1349 |
* verb might convert USE PAINTBRUSH ON WALL to PAINT WALL WITH |
| 1350 |
* PAINTBRUSH by remapping the UseWith action to a PaintWith action |
| 1351 |
* instead. |
| 1352 |
*/ |
| 1353 |
class RemapActionSignal: Exception |
| 1354 |
construct(action) |
| 1355 |
{ |
| 1356 |
/* remember the new action */ |
| 1357 |
action_ = action; |
| 1358 |
} |
| 1359 |
|
| 1360 |
/* the new action that should replace the original action */ |
| 1361 |
action_ = nil |
| 1362 |
; |
| 1363 |
|
| 1364 |
/* |
| 1365 |
* Remap a 'verify' method for a remapped action. This is normally |
| 1366 |
* invoked through the remapTo() macro. |
| 1367 |
*/ |
| 1368 |
remapVerify(oldRole, resultSoFar, remapInfo) |
| 1369 |
{ |
| 1370 |
local newAction; |
| 1371 |
local objs; |
| 1372 |
local idx; |
| 1373 |
local newRole; |
| 1374 |
|
| 1375 |
/* extract new action's object list from the remapping info list */ |
| 1376 |
objs = remapInfo.sublist(2); |
| 1377 |
|
| 1378 |
/* |
| 1379 |
* Create a new action object. We only perform verification |
| 1380 |
* remapping during the resolution phase of the command processing, |
| 1381 |
* because once we've finished resolving, we'll actually replace the |
| 1382 |
* action with the remapped action and thus won't have to remap |
| 1383 |
* verification (or anything else) at that point. So, pass true for |
| 1384 |
* the in-resolve flag to the action creation routine. |
| 1385 |
*/ |
| 1386 |
newAction = remapActionCreate(true, oldRole, remapInfo); |
| 1387 |
|
| 1388 |
/* |
| 1389 |
* Find the object that's given as a resolved object, rather than as |
| 1390 |
* a DirectObject (etc) identifier - the one given as a specific |
| 1391 |
* object is the one that corresponds to the original object. |
| 1392 |
*/ |
| 1393 |
idx = objs.indexWhich({x: dataType(x) == TypeObject}); |
| 1394 |
|
| 1395 |
/* get the role identifier (DirectObject, etc) for the slot position */ |
| 1396 |
newRole = newAction.getRoleFromIndex(idx); |
| 1397 |
|
| 1398 |
/* if we don't yet have a result list object, create one */ |
| 1399 |
if (resultSoFar == nil) |
| 1400 |
resultSoFar = new VerifyResultList(); |
| 1401 |
|
| 1402 |
/* if we found a remapping, verify it */ |
| 1403 |
if (idx != nil) |
| 1404 |
{ |
| 1405 |
/* |
| 1406 |
* Remember the remapped object in the result list. Note that we |
| 1407 |
* do this first, before calling the remapped verification |
| 1408 |
* property, so that our call to the remapped verification |
| 1409 |
* property will overwrite this setting if it does further |
| 1410 |
* remapping. We want the ultimate target object represented |
| 1411 |
* here, after all remappings are finished. |
| 1412 |
*/ |
| 1413 |
resultSoFar.remapAction_ = newAction; |
| 1414 |
resultSoFar.remapTarget_ = objs[idx]; |
| 1415 |
resultSoFar.remapRole_ = newRole; |
| 1416 |
|
| 1417 |
/* install the new action as the current action while verifying */ |
| 1418 |
local oldAction = gAction; |
| 1419 |
gAction = newAction; |
| 1420 |
|
| 1421 |
try |
| 1422 |
{ |
| 1423 |
/* call verification on the new object in the new role */ |
| 1424 |
return newAction.callVerifyProp( |
| 1425 |
objs[idx], |
| 1426 |
newAction.getVerifyPropForRole(newRole), |
| 1427 |
newAction.getPreCondPropForRole(newRole), |
| 1428 |
newAction.getRemapPropForRole(newRole), |
| 1429 |
resultSoFar, newRole); |
| 1430 |
} |
| 1431 |
finally |
| 1432 |
{ |
| 1433 |
/* restore the old gAction on the way out */ |
| 1434 |
gAction = oldAction; |
| 1435 |
} |
| 1436 |
} |
| 1437 |
else |
| 1438 |
{ |
| 1439 |
/* there's no remapping, so there's nothing to verify */ |
| 1440 |
return resultSoFar; |
| 1441 |
} |
| 1442 |
} |
| 1443 |
|
| 1444 |
/* |
| 1445 |
* Perform a remapping to a new action. This is normally invoked |
| 1446 |
* through the remapTo() macro. |
| 1447 |
*/ |
| 1448 |
remapAction(inResolve, oldRole, remapInfo) |
| 1449 |
{ |
| 1450 |
local newAction; |
| 1451 |
|
| 1452 |
/* get the new action */ |
| 1453 |
newAction = remapActionCreate(inResolve, oldRole, remapInfo); |
| 1454 |
|
| 1455 |
/* |
| 1456 |
* replace the current action, using the appropriate mechanism |
| 1457 |
* depending on the current processing phase |
| 1458 |
*/ |
| 1459 |
if (inResolve) |
| 1460 |
{ |
| 1461 |
/* |
| 1462 |
* we're still resolving the objects, so we must use a signal to |
| 1463 |
* start the resolution process over for the new action |
| 1464 |
*/ |
| 1465 |
throw new RemapActionSignal(newAction); |
| 1466 |
} |
| 1467 |
else |
| 1468 |
{ |
| 1469 |
/* |
| 1470 |
* We've finished resolving everything, so we can simply use the |
| 1471 |
* new action as a replacement action. |
| 1472 |
*/ |
| 1473 |
execNestedAction(true, true, gActor, newAction); |
| 1474 |
|
| 1475 |
/* |
| 1476 |
* the remapped action replaces the original action, so |
| 1477 |
* terminate the original action |
| 1478 |
*/ |
| 1479 |
exit; |
| 1480 |
} |
| 1481 |
} |
| 1482 |
|
| 1483 |
/* |
| 1484 |
* Create a new action object for the given remapped action. |
| 1485 |
*/ |
| 1486 |
remapActionCreate(inResolve, oldRole, remapInfo) |
| 1487 |
{ |
| 1488 |
local newAction; |
| 1489 |
local newObjs; |
| 1490 |
local newActionClass; |
| 1491 |
local objs; |
| 1492 |
|
| 1493 |
/* get the new action class and object list from the remap info */ |
| 1494 |
newActionClass = remapInfo[1]; |
| 1495 |
objs = remapInfo.sublist(2); |
| 1496 |
|
| 1497 |
/* |
| 1498 |
* create a new instance of the replacement action, carrying forward |
| 1499 |
* the properties of the original (current) action |
| 1500 |
*/ |
| 1501 |
newAction = newActionClass.createActionFrom(gAction); |
| 1502 |
|
| 1503 |
/* remember the original action we're remapping */ |
| 1504 |
newAction.setOriginalAction(gAction); |
| 1505 |
|
| 1506 |
/* set up an empty vector for the match trees for the new action */ |
| 1507 |
newObjs = new Vector(objs.length()); |
| 1508 |
|
| 1509 |
/* remap according to the phase of the execution */ |
| 1510 |
if (inResolve) |
| 1511 |
{ |
| 1512 |
/* translate the object mappings */ |
| 1513 |
foreach (local cur in objs) |
| 1514 |
{ |
| 1515 |
/* check what we have to translate */ |
| 1516 |
if (dataType(cur) == TypeEnum) |
| 1517 |
{ |
| 1518 |
/* |
| 1519 |
* it's an object role - if it's the special OtherObject |
| 1520 |
* designator, get the other role of a two-object |
| 1521 |
* command |
| 1522 |
*/ |
| 1523 |
if (cur == OtherObject) |
| 1524 |
cur = gAction.getOtherObjectRole(oldRole); |
| 1525 |
|
| 1526 |
/* |
| 1527 |
* get the match tree for this role from the old action |
| 1528 |
* and add it to our list |
| 1529 |
*/ |
| 1530 |
newObjs.append(gAction.getMatchForRole(cur)); |
| 1531 |
} |
| 1532 |
else |
| 1533 |
{ |
| 1534 |
/* append the new ResolveInfo to the new object list */ |
| 1535 |
newObjs.append(gAction.getResolveInfo(cur, oldRole)); |
| 1536 |
} |
| 1537 |
} |
| 1538 |
|
| 1539 |
/* set the object matches in the new action */ |
| 1540 |
newAction.setObjectMatches(newObjs.toList()...); |
| 1541 |
} |
| 1542 |
else |
| 1543 |
{ |
| 1544 |
/* translate the object mappings */ |
| 1545 |
foreach (local cur in objs) |
| 1546 |
{ |
| 1547 |
/* check what we have to translate */ |
| 1548 |
if (dataType(cur) != TypeEnum) |
| 1549 |
{ |
| 1550 |
/* it's an explicit object - use it directly */ |
| 1551 |
newObjs.append(cur); |
| 1552 |
} |
| 1553 |
else |
| 1554 |
{ |
| 1555 |
/* it's a role - translate OtherObject if needed */ |
| 1556 |
if (cur == OtherObject) |
| 1557 |
cur = gAction.getOtherObjectRole(oldRole); |
| 1558 |
|
| 1559 |
/* get the resolved object for this role */ |
| 1560 |
newObjs.append(gAction.getObjectForRole(cur)); |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* set the resolved objects in the new action */ |
| 1565 |
newAction.setResolvedObjects(newObjs.toList()...); |
| 1566 |
} |
| 1567 |
|
| 1568 |
/* return the new action */ |
| 1569 |
return newAction; |
| 1570 |
} |
| 1571 |
|
| 1572 |
/* ------------------------------------------------------------------------ */ |
| 1573 |
/* |
| 1574 |
* Result message object. This is used for verification results and |
| 1575 |
* main command reports, which must keep track of messages to display. |
| 1576 |
*/ |
| 1577 |
class MessageResult: object |
| 1578 |
/* |
| 1579 |
* Construct given literal message text, or alternatively a property |
| 1580 |
* of the current actor's verb messages object. In either case, |
| 1581 |
* we'll expand the message immediately to allow the message to be |
| 1582 |
* displayed later with any parameters fixed at the time the message |
| 1583 |
* is constructed. |
| 1584 |
*/ |
| 1585 |
construct(msg, [params]) |
| 1586 |
{ |
| 1587 |
/* if we're based on an existing object, copy its characteristics */ |
| 1588 |
if (dataType(msg) == TypeObject && msg.ofKind(MessageResult)) |
| 1589 |
{ |
| 1590 |
/* base it on the existing object */ |
| 1591 |
messageText_ = msg.messageText_; |
| 1592 |
messageProp_ = msg.messageProp_; |
| 1593 |
return; |
| 1594 |
} |
| 1595 |
|
| 1596 |
/* |
| 1597 |
* if the message was given as a property, remember the property |
| 1598 |
* for identification purposes |
| 1599 |
*/ |
| 1600 |
if (dataType(msg) == TypeProp) |
| 1601 |
messageProp_ = msg; |
| 1602 |
|
| 1603 |
/* |
| 1604 |
* Resolve the message and store the text. Use the action's |
| 1605 |
* objects (the direct object, indirect object, etc) as the |
| 1606 |
* sources for message overrides - this makes it easy to override |
| 1607 |
* messages on a per-object basis without having to rewrite the |
| 1608 |
* whole verify/check/action routines. |
| 1609 |
*/ |
| 1610 |
messageText_ = resolveMessageText(gAction.getCurrentObjects(), |
| 1611 |
msg, params); |
| 1612 |
} |
| 1613 |
|
| 1614 |
/* |
| 1615 |
* Static method: resolve a message. If the message is given as a |
| 1616 |
* property, we'll look up the message in the given source objects |
| 1617 |
* and in the actor's "action messages" object. We'll return the |
| 1618 |
* resolved message string. |
| 1619 |
*/ |
| 1620 |
resolveMessageText(sources, msg, params) |
| 1621 |
{ |
| 1622 |
/* |
| 1623 |
* If we have more than one source object, it means that the |
| 1624 |
* command has more than one object slot (such as a TIAction, |
| 1625 |
* which has direct and indirect objects). Rearrange the list so |
| 1626 |
* that the nearest caller is the first object in the list. If |
| 1627 |
* one of these source objects provides an override, we generally |
| 1628 |
* want to get the message from the immediate caller rather than |
| 1629 |
* the other object. Note that we only care about the *first* |
| 1630 |
* source object we find in the stack trace, because we only care |
| 1631 |
* about the actual message generator call; enclosing calls |
| 1632 |
* aren't relevant to the message priority because they don't |
| 1633 |
* necessarily have anything to do with the messaging. |
| 1634 |
*/ |
| 1635 |
if (sources.length() > 1) |
| 1636 |
{ |
| 1637 |
/* look through the stack trace for a 'self' in the source list */ |
| 1638 |
local tr = t3GetStackTrace(); |
| 1639 |
for (local i = 1, local trCnt = tr.length() ; i <= trCnt ; ++i) |
| 1640 |
{ |
| 1641 |
/* check this 'self' */ |
| 1642 |
local s = tr[i].self_; |
| 1643 |
local sIdx = sources.indexOf(s); |
| 1644 |
if (sIdx != nil) |
| 1645 |
{ |
| 1646 |
/* |
| 1647 |
* it's a match - move this object to the head of the |
| 1648 |
* list so that we give its message bindings priority |
| 1649 |
*/ |
| 1650 |
if (sIdx != 1) |
| 1651 |
sources = [s] + (sources - s); |
| 1652 |
|
| 1653 |
/* no need to look any further */ |
| 1654 |
break; |
| 1655 |
} |
| 1656 |
} |
| 1657 |
} |
| 1658 |
|
| 1659 |
/* |
| 1660 |
* The message can be given either as a string or as a property |
| 1661 |
* of the actor's verb message object. If it's the latter, look |
| 1662 |
* up the text of the property from the appropriate object. |
| 1663 |
*/ |
| 1664 |
findTextSource: |
| 1665 |
if (dataType(msg) == TypeProp) |
| 1666 |
{ |
| 1667 |
local msgObj; |
| 1668 |
|
| 1669 |
/* |
| 1670 |
* Presume that we'll read the message from the current |
| 1671 |
* actor's "action message object." This is typically |
| 1672 |
* playerActionMessages or npcActionMessages, but it's up to |
| 1673 |
* the actor to specify which object we get our messages |
| 1674 |
* from. |
| 1675 |
*/ |
| 1676 |
msgObj = gActor.getActionMessageObj(); |
| 1677 |
|
| 1678 |
/* |
| 1679 |
* First, look up the message property in the action's |
| 1680 |
* objects (the direct object, indirect object, etc). This |
| 1681 |
* makes it easy to override messages on a per-object basis |
| 1682 |
* without having to rewrite the whole verify/check/action |
| 1683 |
* routine. |
| 1684 |
*/ |
| 1685 |
foreach (local cur in sources) |
| 1686 |
{ |
| 1687 |
/* check to see if this object defines the message property */ |
| 1688 |
if (cur != nil && cur.propDefined(msg)) |
| 1689 |
{ |
| 1690 |
local res; |
| 1691 |
|
| 1692 |
/* |
| 1693 |
* This object defines the property, so check what |
| 1694 |
* we have. |
| 1695 |
*/ |
| 1696 |
switch (cur.propType(msg)) |
| 1697 |
{ |
| 1698 |
case TypeProp: |
| 1699 |
/* |
| 1700 |
* It's another property, so we're being |
| 1701 |
* directed back to the player action message |
| 1702 |
* object. The object does override the |
| 1703 |
* message, but the override points to another |
| 1704 |
* message property in the action object message |
| 1705 |
* set. Simply redirect 'msg' to point to the |
| 1706 |
* new property, and use the same action message |
| 1707 |
* object we already assumed we'd use. |
| 1708 |
*/ |
| 1709 |
msg = cur.(msg); |
| 1710 |
break; |
| 1711 |
|
| 1712 |
case TypeSString: |
| 1713 |
/* it's a simple string - retrieve it */ |
| 1714 |
msg = cur.(msg); |
| 1715 |
|
| 1716 |
/* |
| 1717 |
* since it's just a string, we're done finding |
| 1718 |
* the message text - there's no need to do any |
| 1719 |
* further property lookup, since we've obviously |
| 1720 |
* reached the end of that particular line |
| 1721 |
*/ |
| 1722 |
break findTextSource; |
| 1723 |
|
| 1724 |
case TypeCode: |
| 1725 |
/* |
| 1726 |
* Check the parameter count - we'll allow this |
| 1727 |
* method to take the full set of parameters, or |
| 1728 |
* no parameters at all. We allow the no-param |
| 1729 |
* case for convenience in cases where the method |
| 1730 |
* simply wants to return a string or property ID |
| 1731 |
* from a short method that doesn't need to know |
| 1732 |
* the parameters; in these cases, it's |
| 1733 |
* syntactically a lot nicer looking to write it |
| 1734 |
* as a "prop = (expresion)" than to write the |
| 1735 |
* full method-with-params syntax. |
| 1736 |
*/ |
| 1737 |
if (cur.getPropParams(msg) == [0, 0, nil]) |
| 1738 |
res = cur.(msg); |
| 1739 |
else |
| 1740 |
res = cur.(msg)(params...); |
| 1741 |
|
| 1742 |
/* |
| 1743 |
* If that returned nil, ignore it entirely and |
| 1744 |
* keep scanning the remaining source objects. |
| 1745 |
* The object must have decided it didn't want to |
| 1746 |
* provide the message override in this case |
| 1747 |
* after all. |
| 1748 |
*/ |
| 1749 |
if (res == nil) |
| 1750 |
continue; |
| 1751 |
|
| 1752 |
/* we didn't get nil, so use the result */ |
| 1753 |
msg = res; |
| 1754 |
|
| 1755 |
/* |
| 1756 |
* if we got a string, we've fully resolved the |
| 1757 |
* message text, so we can stop searching for it |
| 1758 |
*/ |
| 1759 |
if (dataType(msg) == TypeSString) |
| 1760 |
break findTextSource; |
| 1761 |
|
| 1762 |
/* |
| 1763 |
* It's not nil and it's not a string, so it must |
| 1764 |
* be a property ID. In this case, the property |
| 1765 |
* ID is a property to evaluate in the normal |
| 1766 |
* action message object. Simply proceed to |
| 1767 |
* evaluate the new message property as normal. |
| 1768 |
*/ |
| 1769 |
break; |
| 1770 |
|
| 1771 |
case TypeNil: |
| 1772 |
/* |
| 1773 |
* it's explicitly nil, which simply means to |
| 1774 |
* ignore this definition; keep scanning other |
| 1775 |
* source objects |
| 1776 |
*/ |
| 1777 |
continue; |
| 1778 |
|
| 1779 |
default: |
| 1780 |
/* |
| 1781 |
* In any other case, this must simply be the |
| 1782 |
* message we're to use. For this case, the |
| 1783 |
* source of the message is this object, so |
| 1784 |
* forget about the normal action message object |
| 1785 |
* and instead use the current object. Then |
| 1786 |
* proceed to evaluate the message property as |
| 1787 |
* normal, which will fetch it from the current |
| 1788 |
* object. |
| 1789 |
*/ |
| 1790 |
msgObj = cur; |
| 1791 |
break; |
| 1792 |
} |
| 1793 |
|
| 1794 |
/* |
| 1795 |
* we found a definition, so we don't need to look |
| 1796 |
* at any of the other objects involved in the |
| 1797 |
* action - we just use the first override we find |
| 1798 |
*/ |
| 1799 |
break; |
| 1800 |
} |
| 1801 |
} |
| 1802 |
|
| 1803 |
/* look up the message in the actor's message generator */ |
| 1804 |
msg = msgObj.(msg)(params...); |
| 1805 |
} |
| 1806 |
|
| 1807 |
/* |
| 1808 |
* format the string and remember the result - do the formatting |
| 1809 |
* immediately, because we want to make sure we expand any |
| 1810 |
* substitution parameters in the context of the current |
| 1811 |
* command, since the parameters might change (and thus alter |
| 1812 |
* the meaning of the message) by the time it's displayed |
| 1813 |
*/ |
| 1814 |
msg = langMessageBuilder.generateMessage(msg); |
| 1815 |
|
| 1816 |
/* |
| 1817 |
* "quote" the message text - it's fully expanded now, so |
| 1818 |
* there's no need to further expand anything that might by |
| 1819 |
* coincidence look like substitution parameters in its text |
| 1820 |
*/ |
| 1821 |
msg = langMessageBuilder.quoteMessage(msg); |
| 1822 |
|
| 1823 |
/* return the resolved message string */ |
| 1824 |
return msg; |
| 1825 |
} |
| 1826 |
|
| 1827 |
/* |
| 1828 |
* set a new message, given the same type of information as we'd use |
| 1829 |
* to construct the object |
| 1830 |
*/ |
| 1831 |
setMessage(msg, [params]) |
| 1832 |
{ |
| 1833 |
/* simply invoke the constructor to re-fill the message data */ |
| 1834 |
construct(msg, params...); |
| 1835 |
} |
| 1836 |
|
| 1837 |
/* |
| 1838 |
* Display a message describing why the command isn't allowed. |
| 1839 |
*/ |
| 1840 |
showMessage() |
| 1841 |
{ |
| 1842 |
/* show our message string */ |
| 1843 |
say(messageText_); |
| 1844 |
} |
| 1845 |
|
| 1846 |
/* the text of our result message */ |
| 1847 |
messageText_ = nil |
| 1848 |
|
| 1849 |
/* the message property, if we have one */ |
| 1850 |
messageProp_ = nil |
| 1851 |
; |
| 1852 |
|