| 1 |
#charset "us-ascii" |
| 2 |
#include <adv3.h> |
| 3 |
#include <en_us.h> |
| 4 |
|
| 5 |
ModuleID |
| 6 |
{ |
| 7 |
name = 'TCommandTopic Library Extension' |
| 8 |
byline = 'by Eric Eve' |
| 9 |
htmlByline = 'by <a href="mailto:[email protected]">Eric Eve</a>' |
| 10 |
version = '3.2' |
| 11 |
listingOrder = 70 |
| 12 |
} |
| 13 |
|
| 14 |
/* |
| 15 |
* VERSION HISTORY |
| 16 |
* |
| 17 |
* v3.2 21-Jul-09 - adds cmdDir to CommandHelper so that the direction of a |
| 18 |
* TravelAction can be inspected. |
| 19 |
* |
| 20 |
* v3.1 01-Mar-08 - Added handling on Actor to trap System Actions issued |
| 21 |
* as commands to NPCs. |
| 22 |
* |
| 23 |
* v3.0 08-Jul-07 - Added handling to ActorState to allow optional blocking |
| 24 |
* of SystemActions and TopicActions directed at the Actor without needing |
| 25 |
* to provide custom CommandTopics to trap them. |
| 26 |
* Also enhanced CommandHelper.proper() to deal more intelligently with |
| 27 |
* possessives. |
| 28 |
* |
| 29 |
* v2.2 11-Dec-05 - Added handling to optionally give or restore initial |
| 30 |
* capitals to proper names used in cmdPhrase. |
| 31 |
* |
| 32 |
* v2.1 10-Dec-05 - Added handling to CommandHelper to prevent run-time |
| 33 |
* error when constructing cmdPhrase in the case of a TopicAction type |
| 34 |
* command (e.g. fred, ask me about john). |
| 35 |
* |
| 36 |
* v2.0 27-Mar-04 - Substantial changes: |
| 37 |
*.- Move the matchTopic handling into CommandTopic |
| 38 |
*.- Allow CommandTopic to match the indirect object as well as the direct |
| 39 |
* object of commands. |
| 40 |
*.- Define a new CommandHelper |
| 41 |
* mix-in class to make it more convenient to get at the current command |
| 42 |
* action and its objects |
| 43 |
*.- Define two new methods on CommandTopic, |
| 44 |
* handleAction(fromActor, action) and actionResponse(fromActor, action) |
| 45 |
* for convenience of command handling. |
| 46 |
*.- Define new AltCommandTopic and |
| 47 |
* AltTCommandTopic classes |
| 48 |
* |
| 49 |
* v1.1 13-Mar-04 |
| 50 |
*.- Move handleTopic, actionPhrase and currentAction into |
| 51 |
* CommandTopic |
| 52 |
*.- Add an obeyCommand property to CommandTopic & |
| 53 |
* DefaultCommandTopic |
| 54 |
*.- Add a currentIObj property to TCommandTopic |
| 55 |
*.- Add the ExecCommandTopic class |
| 56 |
*/ |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
/* modify DefaultCommandTopic to make two new properties available */ |
| 61 |
|
| 62 |
modify DefaultCommandTopic |
| 63 |
handleTopic(fromActor, action) |
| 64 |
{ |
| 65 |
getActor.lastCommandTopic_ = self; |
| 66 |
|
| 67 |
handleAction(fromActor, action); |
| 68 |
|
| 69 |
inherited(fromActor, action); |
| 70 |
|
| 71 |
actionResponse(fromActor, action); |
| 72 |
} |
| 73 |
|
| 74 |
/* This method performs any handling we want before |
| 75 |
* topicResponse; it's mainly here |
| 76 |
* to be overridden by CommandHelper, but can be used |
| 77 |
* for any purposes authors want. |
| 78 |
*/ |
| 79 |
|
| 80 |
handleAction(fromActor, action) |
| 81 |
{ |
| 82 |
|
| 83 |
} |
| 84 |
|
| 85 |
/* This method can be overridden to provide responses |
| 86 |
* that need to know the issuing actor or the action |
| 87 |
* commanded. It is performed after topicResponse. |
| 88 |
*/ |
| 89 |
|
| 90 |
actionResponse(fromActor, action) |
| 91 |
{ |
| 92 |
/* by default - do nothing */ |
| 93 |
} |
| 94 |
|
| 95 |
matchScore = 3 |
| 96 |
obeyCommand = nil |
| 97 |
; |
| 98 |
|
| 99 |
modify CommandTopic |
| 100 |
/* |
| 101 |
* The direct object, or a list of direct objects, that will be matched |
| 102 |
* by this topic. If this is left at nil, any direct object or none |
| 103 |
* will be matched. |
| 104 |
*/ |
| 105 |
matchDobj = nil |
| 106 |
|
| 107 |
/* |
| 108 |
* The indirect object, or a list of indirect objects, that will be matched |
| 109 |
* by this topic. If this is left at nil, any indirect object or none |
| 110 |
* will be matched. |
| 111 |
*/ |
| 112 |
matchIobj = nil |
| 113 |
|
| 114 |
matchTopic(fromActor, action) |
| 115 |
{ |
| 116 |
/* First check whether we match the action of the command */ |
| 117 |
if(!inherited(fromActor, action)) |
| 118 |
return nil; |
| 119 |
|
| 120 |
/* Then check whether we match the direct object of the command action*/ |
| 121 |
|
| 122 |
if(matchDobj != nil && !matchObjs(action.getDobj, matchDobj)) |
| 123 |
return nil; |
| 124 |
|
| 125 |
/* Finally, check whether we match its indirect object */ |
| 126 |
|
| 127 |
if(matchIobj != nil && !matchObjs(action.getIobj, matchIobj)) |
| 128 |
return nil; |
| 129 |
|
| 130 |
|
| 131 |
/* We've matched everything we should, so return matchScore */ |
| 132 |
return matchScore; |
| 133 |
} |
| 134 |
|
| 135 |
matchObjs(obj, objToMatch) |
| 136 |
{ |
| 137 |
if(objToMatch.ofKind(Collection)) |
| 138 |
{ |
| 139 |
return objToMatch.indexWhich({x: obj.ofKind(x)}); |
| 140 |
} |
| 141 |
else |
| 142 |
{ |
| 143 |
return obj.ofKind(objToMatch); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
handleTopic(fromActor, action) |
| 149 |
{ |
| 150 |
/* Tell our actor that this is the commandTopic that has |
| 151 |
* been matched and is responding. |
| 152 |
*/ |
| 153 |
getActor.lastCommandTopic_ = self; |
| 154 |
|
| 155 |
handleAction(fromActor, action); |
| 156 |
|
| 157 |
inherited(fromActor, action); |
| 158 |
|
| 159 |
actionResponse(fromActor, action); |
| 160 |
} |
| 161 |
|
| 162 |
/* This method performs any handling we want before |
| 163 |
* topicResponse; it's mainly here |
| 164 |
* to be overridden by CommandHelper, but can be used |
| 165 |
* for any purposes authors want. |
| 166 |
*/ |
| 167 |
handleAction(fromActor, action) |
| 168 |
{ |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
/* |
| 174 |
* An additional method for providing customised responses |
| 175 |
* that provides ready access to the fromActor and action parameters |
| 176 |
*/ |
| 177 |
|
| 178 |
actionResponse(fromActor, action) |
| 179 |
{ |
| 180 |
/* by default - do nothing */ |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/* Set this to true if you want the actor to obey the command just as |
| 185 |
* it was issued */ |
| 186 |
obeyCommand = nil |
| 187 |
|
| 188 |
; |
| 189 |
|
| 190 |
/* Mix-in Class for use with CommandTopic and DefaultCommandTopic |
| 191 |
* Principally designed to cache the currently commanded action |
| 192 |
* and its most commonly-useful properties. This is mainly for |
| 193 |
* convenience, and allows the additional properties to be accessed |
| 194 |
* from topicResponse. |
| 195 |
*/ |
| 196 |
CommandHelper : object |
| 197 |
handleAction(fromActor, action) |
| 198 |
{ |
| 199 |
cmdDobj = action.getDobj; |
| 200 |
cmdIobj = action.getIobj; |
| 201 |
cmdTopic = action.getTopic; |
| 202 |
cmdAction = action; |
| 203 |
cmdDir = action.getDirection; |
| 204 |
cmdPhrase = proper(action.ofKind(TopicActionBase) ? topicCmdPhrase : |
| 205 |
youToMe(action.getInfPhrase)); |
| 206 |
} |
| 207 |
cmdDobj = nil |
| 208 |
cmdIobj = nil |
| 209 |
cmdAction = nil |
| 210 |
cmdPhrase = nil |
| 211 |
cmdTopic = nil |
| 212 |
cmdDir = nil |
| 213 |
topicCmdPhrase |
| 214 |
{ |
| 215 |
/* |
| 216 |
* This is a hack to get round the fact that |
| 217 |
* TopicTAction.getVerbPhrase() references gTopicText, which in turn |
| 218 |
* references gAction (which is the wrong action at this point), so |
| 219 |
* we temporarily need gAction to refer to the action commanded, not |
| 220 |
* the EventAction which is the real gAction at this point. |
| 221 |
*/ |
| 222 |
|
| 223 |
local oldAction; |
| 224 |
try |
| 225 |
{ |
| 226 |
oldAction = gAction; |
| 227 |
gAction = cmdAction; |
| 228 |
return youToMe(cmdAction.getInfPhrase); |
| 229 |
} |
| 230 |
finally |
| 231 |
{ |
| 232 |
gAction = oldAction; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
/* Takes a string and replaces any occurrence of a word that exists in |
| 237 |
* the list of properNames with the same word with an initial capital. |
| 238 |
*/ |
| 239 |
|
| 240 |
proper(str) |
| 241 |
{ |
| 242 |
foreach(local name in nilToList(properNames)) |
| 243 |
{ |
| 244 |
local name1 = ' ' + name; |
| 245 |
local name2 = ' ' + name.substr(1,1).toUpper + name.substr(2); |
| 246 |
str = str.findReplace(name1, name2, ReplaceAll); |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* If the current npc is mentioned in the possessive, change this |
| 251 |
* to 'your'; also replace the npc's name with 'yourself'. |
| 252 |
*/ |
| 253 |
|
| 254 |
str = langMessageBuilder.generateMessage(str); |
| 255 |
str = rexReplace('%<' + getActor.theName + '<`|squote>s%>', str, 'your', |
| 256 |
ReplaceAll); |
| 257 |
str = rexReplace('%<' + getActor.theName + '%>', str, 'yourself', |
| 258 |
ReplaceAll); |
| 259 |
|
| 260 |
return str; |
| 261 |
} |
| 262 |
|
| 263 |
/* |
| 264 |
* Modify this to contain a list of proper names that you want to |
| 265 |
* appear with an initial capital if they appear in a cmdPhrase |
| 266 |
*/ |
| 267 |
properNames = [] |
| 268 |
; |
| 269 |
|
| 270 |
function youToMe(str) |
| 271 |
{ |
| 272 |
if(str.ofKind(String)) |
| 273 |
{ |
| 274 |
str = str.findReplace(' you ', ' me ', ReplaceAll); |
| 275 |
if(str.endsWith(' you')) |
| 276 |
str = str.findReplace(' you', ' me', ReplaceOnce, |
| 277 |
str.length-5); |
| 278 |
} |
| 279 |
return str.toLower(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
/* |
| 284 |
* TCommandTopic is a CommandTopic that can match a particular direct |
| 285 |
* object or set of direct objects as well as a particular action or |
| 286 |
* set of actions. This allows the TCommandTopic's topicResponse() |
| 287 |
* method either to display a message specific to a particular action |
| 288 |
* and direct object, but also, optionally, to define the actions |
| 289 |
* the target actor takes in response. |
| 290 |
* |
| 291 |
* For example, to define a TCommandTopic that matches the command |
| 292 |
* >BOB, TAKE THE GOLD COIN |
| 293 |
* or |
| 294 |
* >BOB, TAKE THE DIAMOND |
| 295 |
* |
| 296 |
* and then displays a suitable conversational interchange and |
| 297 |
* had Bob take the gold coin you might define: |
| 298 |
* |
| 299 |
* + TCommandTopic @TakeAction |
| 300 |
* matchDobj = [goldCoin, diamond] |
| 301 |
* topicResponse() |
| 302 |
* { |
| 303 |
* "<q>Bob, <<cmdPhrase>>, would you?</q>, you ask.\b |
| 304 |
* <q>Sure,</q> he agrees readily. "; |
| 305 |
* nestedActorAction(getActor, Take, currentDobj); |
| 306 |
* } |
| 307 |
* ; |
| 308 |
*/ |
| 309 |
|
| 310 |
class TCommandTopic : CommandHelper, CommandTopic |
| 311 |
; |
| 312 |
|
| 313 |
class DefaultTCommandTopic : CommandHelper, DefaultCommandTopic |
| 314 |
; |
| 315 |
|
| 316 |
|
| 317 |
modify Actor |
| 318 |
/* Keep track of the last CommandTopic triggered on this actor */ |
| 319 |
lastCommandTopic_ = nil |
| 320 |
|
| 321 |
/* By default, trap system commands targeted to NPCs */ |
| 322 |
obeyCommand(issuingActor, action) |
| 323 |
{ |
| 324 |
if(action.ofKind(SystemAction) && obeySystemCommands == nil) |
| 325 |
{ |
| 326 |
systemActionToNPC(); |
| 327 |
return nil; |
| 328 |
} |
| 329 |
|
| 330 |
return inherited(issuingActor, action); |
| 331 |
} |
| 332 |
|
| 333 |
systemActionToNPC() { libMessages.systemActionToNPC(); } |
| 334 |
obeySystemCommands = nil |
| 335 |
; |
| 336 |
|
| 337 |
/* |
| 338 |
* Modification to ActorState.obeyCommand() to make it work with |
| 339 |
* TCommandTopic. This modification affects only actions that |
| 340 |
* have one or more direct objects, and does the following: |
| 341 |
* |
| 342 |
* 1) If the action has more than one direct object, it is |
| 343 |
* split into a series of actions each having only one |
| 344 |
* direct object, taking each direct object in turn. |
| 345 |
* This allows the game author to assume that any |
| 346 |
* TCommandTopic will only have one direct object to |
| 347 |
* deal with at a time, facilitating the handling when |
| 348 |
* the author may wish to have the target actor make |
| 349 |
* different responses where the direct objects are different. |
| 350 |
* |
| 351 |
* 2) For each action with a direct object passed to handleConversation |
| 352 |
* (and then to a CommandTopic, TCommandTopic or DefaultCommandTopic) |
| 353 |
* the current direct object is marked as such on the action object |
| 354 |
* passed; this allows CommandTopic, TCommandTopic and DefaultCommandTopic |
| 355 |
* to use methods such as getDobj and getInfPhrase that assume that |
| 356 |
* the direct object is defined. |
| 357 |
*/ |
| 358 |
|
| 359 |
modify ActorState |
| 360 |
obeyCommand(issuingActor, action) |
| 361 |
{ |
| 362 |
/* |
| 363 |
* Reset the lastCommandTopic to nil in case the later |
| 364 |
* handling bypasses setting it, and we're left with |
| 365 |
* the spurious residue of a previous command. |
| 366 |
*/ |
| 367 |
|
| 368 |
getActor.lastCommandTopic_ = nil; |
| 369 |
|
| 370 |
|
| 371 |
/* |
| 372 |
* If we direct a SystemAction or a TopicAction at the actor, it |
| 373 |
* should normally be blocked. Commands like BOB, SAVE or BOB, ASK |
| 374 |
* SALLY ABOUT FRED are unlikely to have satisfactory handling, |
| 375 |
* and will typically produce suboptimal responses in |
| 376 |
* DefaultCommandTopics, so it's easiest to block them globally |
| 377 |
* here. We also provide a couple of flags to allow this to be |
| 378 |
* easily overridden on a per-state basis if desired. |
| 379 |
*/ |
| 380 |
|
| 381 |
|
| 382 |
if(autoBlockSystemCommands && action.ofKind(SystemAction)) |
| 383 |
{ |
| 384 |
explainBlockSystemCommand(); |
| 385 |
return nil; |
| 386 |
} |
| 387 |
|
| 388 |
if(autoBlockTopicCommands && action.ofKind(TopicActionBase)) |
| 389 |
{ |
| 390 |
explainBlockTopicCommand(); |
| 391 |
return nil; |
| 392 |
} |
| 393 |
/* |
| 394 |
* If the action takes a direct object (and so could have a list |
| 395 |
* of direct objects), split it into a series of actions |
| 396 |
* with a single direct object |
| 397 |
*/ |
| 398 |
|
| 399 |
|
| 400 |
local dobjLst = action.getResolvedDobjList; |
| 401 |
|
| 402 |
if(dobjLst != nil) |
| 403 |
{ |
| 404 |
local singleAction = action; |
| 405 |
|
| 406 |
local iobjLst = action.getResolvedIobjList; |
| 407 |
if(iobjLst != nil) |
| 408 |
singleAction.iobjCur_ = iobjLst[1]; |
| 409 |
|
| 410 |
foreach(local obj in dobjLst) |
| 411 |
{ |
| 412 |
singleAction.dobjCur_ = obj; |
| 413 |
handleConversation(issuingActor, singleAction, commandConvType); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
/* Otherwise, just treat it as a single command */ |
| 418 |
|
| 419 |
else |
| 420 |
handleConversation(issuingActor, action, commandConvType); |
| 421 |
|
| 422 |
/* |
| 423 |
* check whether to accept or refuse the command, based on |
| 424 |
* the decision of the CommandTopic matched |
| 425 |
*/ |
| 426 |
return getActor.lastCommandTopic_ != nil |
| 427 |
&& getActor.lastCommandTopic_.obeyCommand; |
| 428 |
} |
| 429 |
|
| 430 |
autoBlockSystemCommands = true |
| 431 |
explainBlockSystemCommand() { playerMessages.systemActionToNPC(); } |
| 432 |
|
| 433 |
autoBlockTopicCommands = true |
| 434 |
explainBlockTopicCommand() |
| 435 |
{ |
| 436 |
"It d{oes|id}n't make much sense to ask <<getActor.itObj>> to do that. "; |
| 437 |
} |
| 438 |
; |
| 439 |
|
| 440 |
|
| 441 |
/* AltCommandTopic is an AltTopic with the a special handleTopic |
| 442 |
* method to facilitate the handling of commands. |
| 443 |
*/ |
| 444 |
|
| 445 |
AltCommandTopic : AltTopic |
| 446 |
handleTopic(fromActor, action) |
| 447 |
{ |
| 448 |
|
| 449 |
getActor.lastCommandTopic_ = self; |
| 450 |
|
| 451 |
handleAction(fromActor, action); |
| 452 |
|
| 453 |
inherited(fromActor, action); |
| 454 |
|
| 455 |
actionResponse(fromActor, action); |
| 456 |
} |
| 457 |
|
| 458 |
handleAction(fromActor, action) |
| 459 |
{ |
| 460 |
|
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
/* |
| 465 |
* An additional method for providing customised responses |
| 466 |
* that provides ready access to the fromActor and action parameters |
| 467 |
*/ |
| 468 |
|
| 469 |
actionResponse(fromActor, action) |
| 470 |
{ |
| 471 |
/* by default - do nothing */ |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
/* Set this to true if you want the actor to obey the command just as |
| 476 |
* it was issued */ |
| 477 |
obeyCommand = nil |
| 478 |
|
| 479 |
; |
| 480 |
|
| 481 |
AltTCommandTopic : CommandHelper, AltCommandTopic |
| 482 |
; |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
class SuggestedCommandTopic : SuggestedTopic |
| 487 |
suggestionGroup = [suggestionCommandGroup] |
| 488 |
fullName = ('tell {it targetActor/him} to ' + name) |
| 489 |
; |
| 490 |
|
| 491 |
suggestionCommandGroup : SuggestionListGroup |
| 492 |
groupPrefix = "tell {it targetActor/him} to " |
| 493 |
; |
| 494 |
|