| 1 |
#charset "us-ascii" |
| 2 |
|
| 3 |
/* |
| 4 |
* Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved. |
| 5 |
* |
| 6 |
* TADS 3 Library: events |
| 7 |
* |
| 8 |
* This module defines the event framework. An event is a programmed |
| 9 |
* operation that occurs at a particular point in the game; an event can |
| 10 |
* be turn-based, in which case it occurs after a given number of turns |
| 11 |
* has elapsed, or it can occur in real time, which means that it occurs |
| 12 |
* after a particular interval of time has elapsed. |
| 13 |
*/ |
| 14 |
|
| 15 |
#include "adv3.h" |
| 16 |
#include <dict.h> |
| 17 |
#include <gramprod.h> |
| 18 |
|
| 19 |
|
| 20 |
/* ------------------------------------------------------------------------ */ |
| 21 |
/* |
| 22 |
* Run the main scheduling loop. This continues until we encounter an |
| 23 |
* end-of-file error reading from the console, or a QuitException is |
| 24 |
* thrown to terminate the game. |
| 25 |
*/ |
| 26 |
runScheduler() |
| 27 |
{ |
| 28 |
/* keep going until we quit the game */ |
| 29 |
for (;;) |
| 30 |
{ |
| 31 |
/* catch the exceptions that terminate the game */ |
| 32 |
try |
| 33 |
{ |
| 34 |
/* start with an empty list of schedulable items */ |
| 35 |
local vec = new Vector(10); |
| 36 |
|
| 37 |
/* find the lowest time at which something is ready to run */ |
| 38 |
local minTime = nil; |
| 39 |
foreach (local cur in Schedulable.allSchedulables) |
| 40 |
{ |
| 41 |
/* get this item's next eligible run time */ |
| 42 |
local curTime = cur.getNextRunTime(); |
| 43 |
|
| 44 |
/* |
| 45 |
* if it's not nil, and it's equal to or below the |
| 46 |
* lowest we've seen so far, note it |
| 47 |
*/ |
| 48 |
if (curTime != nil && (minTime == nil || curTime <= minTime)) |
| 49 |
{ |
| 50 |
/* |
| 51 |
* if this is different from the current minimum |
| 52 |
* schedulable time, clear out the list of |
| 53 |
* schedulables, because the list keeps track of the |
| 54 |
* items at the lowest time only |
| 55 |
*/ |
| 56 |
if (minTime != nil && curTime < minTime) |
| 57 |
vec.removeRange(1, vec.length()); |
| 58 |
|
| 59 |
/* add this item to the list */ |
| 60 |
vec.append(cur); |
| 61 |
|
| 62 |
/* note the new lowest schedulable time */ |
| 63 |
minTime = curTime; |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/* |
| 68 |
* if nothing's ready to run, the game is over by default, |
| 69 |
* since we cannot escape this state - we can't ourselves |
| 70 |
* change anything's run time, so if nothing's ready to run |
| 71 |
* now, we won't be able to change that, and so nothing will |
| 72 |
* ever be ready to run |
| 73 |
*/ |
| 74 |
if (minTime == nil) |
| 75 |
{ |
| 76 |
"\b[Error: nothing is available for scheduling - |
| 77 |
terminating]\b"; |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
/* |
| 82 |
* Advance the global turn counter by the amount of game |
| 83 |
* clock time we're consuming now. |
| 84 |
*/ |
| 85 |
libGlobal.totalTurns += minTime - Schedulable.gameClockTime; |
| 86 |
|
| 87 |
/* |
| 88 |
* advance the game clock to the minimum run time - nothing |
| 89 |
* interesting happens in game time until then, so we can |
| 90 |
* skip straight ahead to this time |
| 91 |
*/ |
| 92 |
Schedulable.gameClockTime = minTime; |
| 93 |
|
| 94 |
/* calculate the schedule order for each item */ |
| 95 |
vec.forEach({x: x.calcScheduleOrder()}); |
| 96 |
|
| 97 |
/* |
| 98 |
* We have a list of everything schedulable at the current |
| 99 |
* game clock time. Sort the list in ascending scheduling |
| 100 |
* order, so that the higher priority items come first in |
| 101 |
* the list. |
| 102 |
*/ |
| 103 |
vec = vec.sort( |
| 104 |
SortAsc, {a, b: a.scheduleOrder - b.scheduleOrder}); |
| 105 |
|
| 106 |
/* |
| 107 |
* Run through the list and run each item. Keep running |
| 108 |
* each item as long as it's ready to run - that is, as long |
| 109 |
* as its schedulable time equals the game clock time. |
| 110 |
*/ |
| 111 |
vecLoop: |
| 112 |
foreach (local cur in vec) |
| 113 |
{ |
| 114 |
/* run this item for as long as it's ready to run */ |
| 115 |
while (cur.getNextRunTime() == minTime) |
| 116 |
{ |
| 117 |
try |
| 118 |
{ |
| 119 |
/* |
| 120 |
* execute this item - if it doesn't want to be |
| 121 |
* called again without considering other |
| 122 |
* objects, stop looping and refigure the |
| 123 |
* scheduling order from scratch |
| 124 |
*/ |
| 125 |
if (!cur.executeTurn()) |
| 126 |
break vecLoop; |
| 127 |
} |
| 128 |
catch (Exception exc) |
| 129 |
{ |
| 130 |
/* |
| 131 |
* The scheduled operation threw an exception. |
| 132 |
* If the schedulable's next run time didn't get |
| 133 |
* updated, then the same schedulable will be |
| 134 |
* considered ready to run again immediately on |
| 135 |
* the next time through the loop. It's quite |
| 136 |
* possible in this case that we'll simply repeat |
| 137 |
* the operation that threw the exception and get |
| 138 |
* right back here again. If this happens, it |
| 139 |
* will effectively starve all of the other |
| 140 |
* schedulables. To ensure that other |
| 141 |
* schedulables get a chance to run before we try |
| 142 |
* this erroneous operation again, advance its |
| 143 |
* next run time by one unit if it hasn't already |
| 144 |
* been advanced. |
| 145 |
*/ |
| 146 |
if (cur.getNextRunTime() == minTime) |
| 147 |
cur.incNextRunTime(1); |
| 148 |
|
| 149 |
/* re-throw the exception */ |
| 150 |
throw exc; |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
catch (EndOfFileException eofExc) |
| 156 |
{ |
| 157 |
/* end of file reading command input - we're done */ |
| 158 |
return; |
| 159 |
} |
| 160 |
catch (QuittingException quitExc) |
| 161 |
{ |
| 162 |
/* explicitly quitting - we're done */ |
| 163 |
return; |
| 164 |
} |
| 165 |
catch (RestartSignal rsSig) |
| 166 |
{ |
| 167 |
/* |
| 168 |
* Restarting - re-throw the signal for handling in the |
| 169 |
* system startup code. Note that we explicitly catch this |
| 170 |
* signal, only to rethrow it, because we'd otherwise flag it |
| 171 |
* as an unhandled error in the catch-all Exception handler. |
| 172 |
*/ |
| 173 |
throw rsSig; |
| 174 |
} |
| 175 |
catch (RuntimeError rtErr) |
| 176 |
{ |
| 177 |
/* if this is a debugger error of some kind, re-throw it */ |
| 178 |
if (rtErr.isDebuggerSignal) |
| 179 |
throw rtErr; |
| 180 |
|
| 181 |
/* display the error, but keep going */ |
| 182 |
"\b[<<rtErr.displayException()>>]\b"; |
| 183 |
} |
| 184 |
catch (TerminateCommandException tce) |
| 185 |
{ |
| 186 |
/* |
| 187 |
* Aborted command - ignore it. This is most like to occur |
| 188 |
* when a fuse, daemon, or the like tries to terminate itself |
| 189 |
* with this exception, thinking it's operating in a normal |
| 190 |
* command execution environment. As a convenience, simply |
| 191 |
* ignore these exceptions so that any code can use them to |
| 192 |
* abort everything and return to the main scheduling loop. |
| 193 |
*/ |
| 194 |
} |
| 195 |
catch (ExitSignal es) |
| 196 |
{ |
| 197 |
/* ignore this, just as we ignore TerminateCommandException */ |
| 198 |
} |
| 199 |
catch (ExitActionSignal eas) |
| 200 |
{ |
| 201 |
/* ignore this, just as we ignore TerminateCommandException */ |
| 202 |
} |
| 203 |
catch (Exception exc) |
| 204 |
{ |
| 205 |
/* some other unhandled exception - display it and keep going */ |
| 206 |
"\b[Unhandled exception: <<exc.displayException()>>]\b"; |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
/* ------------------------------------------------------------------------ */ |
| 212 |
/* |
| 213 |
* An item that can be scheduled for time-based notifications. The main |
| 214 |
* scheduler loop in runScheduler() operates on objects of this class. |
| 215 |
* |
| 216 |
* Note that we build a list of all Schedulable instances during |
| 217 |
* pre-initialization. If any Schedulable objects are dynamically |
| 218 |
* created, they must be added to the list explicitly after creation in |
| 219 |
* order for the event manager to schedule them for execution. The |
| 220 |
* default constructor does this automatically, so subclasses can simply |
| 221 |
* inherit our constructor to be added to the master list. |
| 222 |
*/ |
| 223 |
class Schedulable: object |
| 224 |
/* construction - add myself to the Schedulable list */ |
| 225 |
construct() |
| 226 |
{ |
| 227 |
/* |
| 228 |
* Add myself to the master list of Schedulable instances. Note |
| 229 |
* that we must update the list in the Schedulable class itself. |
| 230 |
*/ |
| 231 |
Schedulable.allSchedulables += self; |
| 232 |
} |
| 233 |
|
| 234 |
/* |
| 235 |
* Get the next time (on the game clock) at which I'm eligible for |
| 236 |
* execution. We won't receive any scheduling notifications until |
| 237 |
* this time. If this object doesn't want any scheduling |
| 238 |
* notifications, return nil. |
| 239 |
*/ |
| 240 |
getNextRunTime() { return nextRunTime; } |
| 241 |
|
| 242 |
/* advance my next run time by the given number of clock units */ |
| 243 |
incNextRunTime(amt) |
| 244 |
{ |
| 245 |
if (nextRunTime != nil) |
| 246 |
nextRunTime += amt; |
| 247 |
} |
| 248 |
|
| 249 |
/* |
| 250 |
* Notify this object that its scheduled run time has arrived. This |
| 251 |
* should perform the scheduled task. If the scheduled task takes |
| 252 |
* any game time, the object's internal next run time should be |
| 253 |
* updated accordingly. |
| 254 |
* |
| 255 |
* The scheduler will invoke this method of the same object |
| 256 |
* repeatedly for as long as its nextRunTime remains unchanged AND |
| 257 |
* this method returns true. If the object's scheduling priority |
| 258 |
* changes relative to other schedulable objects, it should return |
| 259 |
* nil here to tell the scheduler to recalculate scheduling |
| 260 |
* priorities. |
| 261 |
*/ |
| 262 |
executeTurn() { return true; } |
| 263 |
|
| 264 |
/* |
| 265 |
* Scheduling order. This determines which item goes first when |
| 266 |
* multiple items are schedulable at the same time (i.e., they all |
| 267 |
* have the same getNextRunTime() values). The item with the lowest |
| 268 |
* number here goes first. |
| 269 |
* |
| 270 |
* This should never be evaluated except immediately after a call to |
| 271 |
* calcScheduleOrder. |
| 272 |
*/ |
| 273 |
scheduleOrder = 100 |
| 274 |
|
| 275 |
/* |
| 276 |
* Calculate the scheduling order, returning the order value and |
| 277 |
* storing it in our property scheduleOrder. This is used to |
| 278 |
* calculate and cache the value prior to sorting a list of |
| 279 |
* schedulable items. We use this two-step approach (first |
| 280 |
* calculate, then sort) so that we avoid repeatedly evaluating a |
| 281 |
* complex calculation, if indeed there is a complex calculation to |
| 282 |
* perform. |
| 283 |
* |
| 284 |
* By default, we assume that the schedule order is static, so we |
| 285 |
* simply leave our scheduleOrder property unchanged and return its |
| 286 |
* present value. |
| 287 |
*/ |
| 288 |
calcScheduleOrder() { return scheduleOrder; } |
| 289 |
|
| 290 |
/* my next running time, in game clock time */ |
| 291 |
nextRunTime = nil |
| 292 |
|
| 293 |
/* |
| 294 |
* A class variable giving the current game clock time. This is a |
| 295 |
* class variable because there's only one global game clock. The |
| 296 |
* game clock starts at zero and increments in game time units; a |
| 297 |
* game time unit is the arbitrary quantum of time for our event |
| 298 |
* scheduling system. |
| 299 |
*/ |
| 300 |
gameClockTime = 0 |
| 301 |
|
| 302 |
/* |
| 303 |
* A list of all of the Schedulable objects in the game. We set this |
| 304 |
* up during pre-initialization; if any Schedulable instances are |
| 305 |
* created dynamically, they must be explicitly added to this list |
| 306 |
* after creation. |
| 307 |
*/ |
| 308 |
allSchedulables = nil |
| 309 |
; |
| 310 |
|
| 311 |
/* |
| 312 |
* Pre-initializer: build the master list of Schedulable instances |
| 313 |
*/ |
| 314 |
PreinitObject |
| 315 |
/* |
| 316 |
* Execute preinitialization. Build a list of all of the schedulable |
| 317 |
* objects in the game, so that we can scan this list quickly during |
| 318 |
* play. |
| 319 |
*/ |
| 320 |
execute() |
| 321 |
{ |
| 322 |
local vec; |
| 323 |
|
| 324 |
/* set up an empty vector to hold the schedulable objects */ |
| 325 |
vec = new Vector(32); |
| 326 |
|
| 327 |
/* add all of the Schedulable instances to the vector */ |
| 328 |
forEachInstance(Schedulable, {s: vec.append(s)}); |
| 329 |
|
| 330 |
/* save the list of Schedulable instances as an ordinary list */ |
| 331 |
Schedulable.allSchedulables = vec.toList(); |
| 332 |
} |
| 333 |
; |
| 334 |
|
| 335 |
/* ------------------------------------------------------------------------ */ |
| 336 |
/* |
| 337 |
* Basic Event Manager. This is a common base class for the game-time |
| 338 |
* and real-time event managers. This class handles the details of |
| 339 |
* managing the event queue; the subclasses must define the specifics of |
| 340 |
* event timing. |
| 341 |
*/ |
| 342 |
class BasicEventManager: object |
| 343 |
/* add an event */ |
| 344 |
addEvent(event) |
| 345 |
{ |
| 346 |
/* append the event to our list */ |
| 347 |
events_.append(event); |
| 348 |
} |
| 349 |
|
| 350 |
/* remove an event */ |
| 351 |
removeEvent(event) |
| 352 |
{ |
| 353 |
/* remove the event from our list */ |
| 354 |
events_.removeElement(event); |
| 355 |
} |
| 356 |
|
| 357 |
/* |
| 358 |
* Remove events matching the given object and property combination. |
| 359 |
* We remove all events that match both the object and property |
| 360 |
* (events matching only the object or only the property are not |
| 361 |
* affected). |
| 362 |
* |
| 363 |
* This is provided mostly as a convenience for cases where an event |
| 364 |
* is known to be uniquely identifiable by its object and property |
| 365 |
* values; this saves the caller the trouble of keeping track of the |
| 366 |
* Event object created when the event was first registered. |
| 367 |
* |
| 368 |
* When a particular object/property combination might be used in |
| 369 |
* several different events, it's better to keep a reference to the |
| 370 |
* Event object representing each event, and use removeEvent() to |
| 371 |
* remove the specific Event object of interest. |
| 372 |
* |
| 373 |
* Returns true if we find any matching events, nil if not. |
| 374 |
*/ |
| 375 |
removeMatchingEvents(obj, prop) |
| 376 |
{ |
| 377 |
local found; |
| 378 |
|
| 379 |
/* |
| 380 |
* Scan our list, and remove each event matching the parameters. |
| 381 |
* Note that it's safe to remove things from a vector that we're |
| 382 |
* iterating with foreach(), since foreach() makes a safe copy |
| 383 |
* of the vector for the iteration. |
| 384 |
*/ |
| 385 |
found = nil; |
| 386 |
foreach (local cur in events_) |
| 387 |
{ |
| 388 |
/* if this one matches, remove it */ |
| 389 |
if (cur.eventMatches(obj, prop)) |
| 390 |
{ |
| 391 |
/* remove the event */ |
| 392 |
removeEvent(cur); |
| 393 |
|
| 394 |
/* note that we found a match */ |
| 395 |
found = true; |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
/* return our 'found' indication */ |
| 400 |
return found; |
| 401 |
} |
| 402 |
|
| 403 |
/* |
| 404 |
* Remove the current event - this is provided for convenience so |
| 405 |
* that an event can cancel itself in the course of its execution. |
| 406 |
* |
| 407 |
* Note that this has no effect on the current event execution - |
| 408 |
* this simply prevents the event from receiving additional |
| 409 |
* notifications in the future. |
| 410 |
*/ |
| 411 |
removeCurrentEvent() |
| 412 |
{ |
| 413 |
/* remove the currently active event from our list */ |
| 414 |
removeEvent(curEvent_); |
| 415 |
} |
| 416 |
|
| 417 |
/* event list - each instance must initialize this to a vector */ |
| 418 |
// events_ = nil |
| 419 |
; |
| 420 |
|
| 421 |
/* |
| 422 |
* Event Manager. This is a schedulable object that keeps track of |
| 423 |
* fuses and daemons, and schedules their execution. |
| 424 |
*/ |
| 425 |
eventManager: BasicEventManager, Schedulable |
| 426 |
/* |
| 427 |
* Use a scheduling order of 1000 to ensure we go after all actors. |
| 428 |
* By default, actors use scheduling orders in the range 100 to 400, |
| 429 |
* so our order of 1000 ensures that fuses and daemons run after all |
| 430 |
* characters on a given turn. |
| 431 |
*/ |
| 432 |
scheduleOrder = 1000 |
| 433 |
|
| 434 |
/* |
| 435 |
* Get the next run time. We'll find the lowest run time of our |
| 436 |
* fuses and daemons and return that. |
| 437 |
*/ |
| 438 |
getNextRunTime() |
| 439 |
{ |
| 440 |
local minTime; |
| 441 |
|
| 442 |
/* |
| 443 |
* run through our list of events, and find the event that is |
| 444 |
* scheduled to run at the lowest game clock time |
| 445 |
*/ |
| 446 |
minTime = nil; |
| 447 |
foreach (local cur in events_) |
| 448 |
{ |
| 449 |
local curTime; |
| 450 |
|
| 451 |
/* get this item's scheduled run time */ |
| 452 |
curTime = cur.getNextRunTime(); |
| 453 |
|
| 454 |
/* if it's not nil and it's the lowest so far, remember it */ |
| 455 |
if (curTime != nil && (minTime == nil || curTime < minTime)) |
| 456 |
minTime = curTime; |
| 457 |
} |
| 458 |
|
| 459 |
/* return the minimum time we found */ |
| 460 |
return minTime; |
| 461 |
} |
| 462 |
|
| 463 |
/* |
| 464 |
* Execute a turn. We'll execute each fuse and each daemon that is |
| 465 |
* currently schedulable. |
| 466 |
*/ |
| 467 |
executeTurn() |
| 468 |
{ |
| 469 |
local lst; |
| 470 |
|
| 471 |
/* |
| 472 |
* build a list of all of our events with the current game clock |
| 473 |
* time - these are the events that are currently schedulable |
| 474 |
*/ |
| 475 |
lst = events_.subset({x: x.getNextRunTime() |
| 476 |
== Schedulable.gameClockTime}); |
| 477 |
|
| 478 |
/* execute the items in this list */ |
| 479 |
executeList(lst); |
| 480 |
|
| 481 |
/* no change in scheduling priorities */ |
| 482 |
return true; |
| 483 |
} |
| 484 |
|
| 485 |
/* |
| 486 |
* Execute a command prompt turn. We'll execute each |
| 487 |
* per-command-prompt daemon. |
| 488 |
*/ |
| 489 |
executePrompt() |
| 490 |
{ |
| 491 |
/* execute all of the per-command-prompt daemons */ |
| 492 |
executeList(events_.subset({x: x.isPromptDaemon})); |
| 493 |
} |
| 494 |
|
| 495 |
/* |
| 496 |
* internal service routine - execute the fuses and daemons in the |
| 497 |
* given list, in eventOrder priority order |
| 498 |
*/ |
| 499 |
executeList(lst) |
| 500 |
{ |
| 501 |
/* sort the list in ascending event order */ |
| 502 |
lst = lst.toList() |
| 503 |
.sort(SortAsc, {a, b: a.eventOrder - b.eventOrder}); |
| 504 |
|
| 505 |
/* run through the list and execute each item ready to run */ |
| 506 |
foreach (local cur in lst) |
| 507 |
{ |
| 508 |
/* remember our old active event, then establish the new one */ |
| 509 |
local oldEvent = curEvent_; |
| 510 |
curEvent_ = cur; |
| 511 |
|
| 512 |
/* make sure we restore things on the way out */ |
| 513 |
try |
| 514 |
{ |
| 515 |
local pc; |
| 516 |
|
| 517 |
/* have the player character note the pre-event conditions */ |
| 518 |
pc = gPlayerChar; |
| 519 |
pc.noteConditionsBefore(); |
| 520 |
|
| 521 |
/* cancel any sense caching currently in effect */ |
| 522 |
libGlobal.disableSenseCache(); |
| 523 |
|
| 524 |
/* execute the event */ |
| 525 |
cur.executeEvent(); |
| 526 |
|
| 527 |
/* |
| 528 |
* if the player character is the same as it was, ask |
| 529 |
* the player character to note any change in conditions |
| 530 |
*/ |
| 531 |
if (gPlayerChar == pc) |
| 532 |
pc.noteConditionsAfter(); |
| 533 |
} |
| 534 |
catch (Exception exc) |
| 535 |
{ |
| 536 |
/* |
| 537 |
* If an event throws an exception out of its handler, |
| 538 |
* remove the event from the active list. If we were to |
| 539 |
* leave it active, we'd go back and execute the same |
| 540 |
* event again the next time we look for something to |
| 541 |
* schedule, and that would in turn probably just |
| 542 |
* encounter the same exception - so we'd be stuck in an |
| 543 |
* infinite loop executing this erroneous code. To |
| 544 |
* ensure that we don't get stuck, remove the event. |
| 545 |
*/ |
| 546 |
removeCurrentEvent(); |
| 547 |
|
| 548 |
/* re-throw the exception */ |
| 549 |
throw exc; |
| 550 |
} |
| 551 |
finally |
| 552 |
{ |
| 553 |
/* restore the enclosing current event */ |
| 554 |
curEvent_ = oldEvent; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
/* our list of fuses and daemons */ |
| 560 |
events_ = static new Vector(20) |
| 561 |
|
| 562 |
/* the event currently being executed */ |
| 563 |
curEvent_ = nil |
| 564 |
; |
| 565 |
|
| 566 |
/* |
| 567 |
* Pseudo-action subclass to represent the action environment while |
| 568 |
* processing a daemon, fuse, or other event. |
| 569 |
*/ |
| 570 |
class EventAction: Action |
| 571 |
/* |
| 572 |
* event actions are internal system actions; they don't consume |
| 573 |
* additional turns themselves, since they run between player turns |
| 574 |
*/ |
| 575 |
actionTime = 0; |
| 576 |
; |
| 577 |
|
| 578 |
/* |
| 579 |
* A basic event, for game-time and real-time events. |
| 580 |
*/ |
| 581 |
class BasicEvent: object |
| 582 |
/* construction */ |
| 583 |
construct(obj, prop) |
| 584 |
{ |
| 585 |
/* remember the object and property to call at execution */ |
| 586 |
obj_ = obj; |
| 587 |
prop_ = prop; |
| 588 |
} |
| 589 |
|
| 590 |
/* |
| 591 |
* Execute the event. This must be overridden by the subclass to |
| 592 |
* perform the appropriate operation when executed. In particular, |
| 593 |
* the subclass must reschedule or unschedule the event, as |
| 594 |
* appropriate. |
| 595 |
*/ |
| 596 |
executeEvent() { } |
| 597 |
|
| 598 |
/* does this event match the given object/property combination? */ |
| 599 |
eventMatches(obj, prop) { return obj == obj_ && prop == prop_; } |
| 600 |
|
| 601 |
/* |
| 602 |
* Call our underlying method. This is an internal routine intended |
| 603 |
* for use by the executeEvent() implementations. |
| 604 |
*/ |
| 605 |
callMethod() |
| 606 |
{ |
| 607 |
/* |
| 608 |
* invoke the method in our sensory context, and in a simulated |
| 609 |
* action environment |
| 610 |
*/ |
| 611 |
withActionEnv(EventAction, gPlayerChar, |
| 612 |
{: callWithSenseContext(source_, sense_, |
| 613 |
{: obj_.(self.prop_)() }) }); |
| 614 |
} |
| 615 |
|
| 616 |
/* the object and property we invoke */ |
| 617 |
obj_ = nil |
| 618 |
prop_ = nil |
| 619 |
|
| 620 |
/* |
| 621 |
* The sensory context of the event. When the event fires, we'll |
| 622 |
* execute its method in this sensory context, so that any messages |
| 623 |
* generated will be displayed only if the player character can |
| 624 |
* sense the source object in the given sense. |
| 625 |
* |
| 626 |
* By default, these are nil, which means that the event's messages |
| 627 |
* will be displayed (or, at least, they won't be suppressed because |
| 628 |
* of the sensory context). |
| 629 |
*/ |
| 630 |
source_ = nil |
| 631 |
sense_ = nil |
| 632 |
; |
| 633 |
|
| 634 |
/* |
| 635 |
* Base class for fuses and daemons |
| 636 |
*/ |
| 637 |
class Event: BasicEvent |
| 638 |
/* our next run time, in game clock time */ |
| 639 |
getNextRunTime() { return nextRunTime; } |
| 640 |
|
| 641 |
/* delay our scheduled run time by the given number of turns */ |
| 642 |
delayEvent(turns) { nextRunTime += turns; } |
| 643 |
|
| 644 |
/* remove this event from the event manager */ |
| 645 |
removeEvent() { eventManager.removeEvent(self); } |
| 646 |
|
| 647 |
/* |
| 648 |
* Event order - this establishes the order we run relative to other |
| 649 |
* events scheduled to run at the same game clock time. Lowest |
| 650 |
* number goes first. By default, we provide an event order of 100, |
| 651 |
* which should leave plenty of room for custom events before and |
| 652 |
* after default events. |
| 653 |
*/ |
| 654 |
eventOrder = 100 |
| 655 |
|
| 656 |
/* creation */ |
| 657 |
construct(obj, prop) |
| 658 |
{ |
| 659 |
/* inherit default handling */ |
| 660 |
inherited(obj, prop); |
| 661 |
|
| 662 |
/* add myself to the event manager's active event list */ |
| 663 |
eventManager.addEvent(self); |
| 664 |
} |
| 665 |
|
| 666 |
/* |
| 667 |
* our next execution time, expressed in game clock time; by |
| 668 |
* default, we'll set this to nil, which means that we are not |
| 669 |
* scheduled to execute at all |
| 670 |
*/ |
| 671 |
nextRunTime = nil |
| 672 |
|
| 673 |
/* by default, we're not a per-command-prompt daemon */ |
| 674 |
isPromptDaemon = nil |
| 675 |
; |
| 676 |
|
| 677 |
/* |
| 678 |
* Fuse. A fuse is an event that fires once at a given time in the |
| 679 |
* future. Once a fuse is executed, it is removed from further |
| 680 |
* scheduling. |
| 681 |
*/ |
| 682 |
class Fuse: Event |
| 683 |
/* |
| 684 |
* Creation. 'turns' is the number of turns in the future at which |
| 685 |
* the fuse is executed; if turns is 0, the fuse will be executed on |
| 686 |
* the current turn. |
| 687 |
*/ |
| 688 |
construct(obj, prop, turns) |
| 689 |
{ |
| 690 |
/* inherit the base class constructor */ |
| 691 |
inherited(obj, prop); |
| 692 |
|
| 693 |
/* |
| 694 |
* set my scheduled time to the current game clock time plus the |
| 695 |
* number of turns into the future |
| 696 |
*/ |
| 697 |
nextRunTime = Schedulable.gameClockTime + turns; |
| 698 |
} |
| 699 |
|
| 700 |
/* execute the fuse */ |
| 701 |
executeEvent() |
| 702 |
{ |
| 703 |
/* call my method */ |
| 704 |
callMethod(); |
| 705 |
|
| 706 |
/* a fuse fires only once, so remove myself from further scheduling */ |
| 707 |
eventManager.removeEvent(self); |
| 708 |
} |
| 709 |
; |
| 710 |
|
| 711 |
/* |
| 712 |
* Sensory-context-sensitive fuse - this is a fuse with an explicit |
| 713 |
* sensory context. We'll run the fuse in its sense context, so any |
| 714 |
* messages generated will be visible only if the given source object is |
| 715 |
* reachable by the player character in the given sense. |
| 716 |
* |
| 717 |
* Conceptually, the source object is considered the source of any |
| 718 |
* messages that the fuse generates, and the messages pertain to the |
| 719 |
* given sense; so if the player character cannot sense the source |
| 720 |
* object in the given sense, the messages should not be displayed. For |
| 721 |
* example, if the fuse will describe the noise made by an alarm clock |
| 722 |
* when the alarm goes off, the source object would be the alarm clock |
| 723 |
* and the sense would be sound; this way, if the player character isn't |
| 724 |
* in hearing range of the alarm clock when the alarm goes off, we won't |
| 725 |
* display messages about the alarm noise. |
| 726 |
*/ |
| 727 |
class SenseFuse: Fuse |
| 728 |
construct(obj, prop, turns, source, sense) |
| 729 |
{ |
| 730 |
/* inherit the base constructor */ |
| 731 |
inherited(obj, prop, turns); |
| 732 |
|
| 733 |
/* remember our sensory context */ |
| 734 |
source_ = source; |
| 735 |
sense_ = sense; |
| 736 |
} |
| 737 |
; |
| 738 |
|
| 739 |
/* |
| 740 |
* Daemon. A daemon is an event that fires repeatedly at given |
| 741 |
* intervals. When a daemon is executed, it is scheduled again for |
| 742 |
* execution after its interval elapses again. |
| 743 |
*/ |
| 744 |
class Daemon: Event |
| 745 |
/* |
| 746 |
* Creation. 'interval' is the number of turns between invocations |
| 747 |
* of the daemon; this should be at least 1, which causes the daemon |
| 748 |
* to be invoked on each turn. The first execution will be |
| 749 |
* (interval-1) turns in the future - so if interval is 1, the |
| 750 |
* daemon will first be executed on the current turn, and if |
| 751 |
* interval is 2, the daemon will be executed on the next turn. |
| 752 |
*/ |
| 753 |
construct(obj, prop, interval) |
| 754 |
{ |
| 755 |
/* inherit the base class constructor */ |
| 756 |
inherited(obj, prop); |
| 757 |
|
| 758 |
/* |
| 759 |
* an interval of less than 1 is meaningless, so make sure it's |
| 760 |
* at least 1 |
| 761 |
*/ |
| 762 |
if (interval < 1) |
| 763 |
interval = 1; |
| 764 |
|
| 765 |
/* remember my interval */ |
| 766 |
interval_ = interval; |
| 767 |
|
| 768 |
/* |
| 769 |
* set my initial execution time, in game clock time - add one |
| 770 |
* less than the interval to the current game clock time, so |
| 771 |
* that we count the current turn as yet to elapse for the |
| 772 |
* purposes of the interval before the daemon's first execution |
| 773 |
*/ |
| 774 |
nextRunTime = Schedulable.gameClockTime + interval - 1; |
| 775 |
} |
| 776 |
|
| 777 |
/* execute the daemon */ |
| 778 |
executeEvent() |
| 779 |
{ |
| 780 |
/* call my method */ |
| 781 |
callMethod(); |
| 782 |
|
| 783 |
/* advance our next run time by our interval */ |
| 784 |
nextRunTime += interval_; |
| 785 |
} |
| 786 |
|
| 787 |
/* our execution interval, in turns */ |
| 788 |
interval_ = 1 |
| 789 |
; |
| 790 |
|
| 791 |
/* |
| 792 |
* Sensory-context-sensitive daemon - this is a daemon with an explicit |
| 793 |
* sensory context. This is the daemon counterpart of SenseFuse. |
| 794 |
*/ |
| 795 |
class SenseDaemon: Daemon |
| 796 |
construct(obj, prop, interval, source, sense) |
| 797 |
{ |
| 798 |
/* inherit the base constructor */ |
| 799 |
inherited(obj, prop, interval); |
| 800 |
|
| 801 |
/* remember our sensory context */ |
| 802 |
source_ = source; |
| 803 |
sense_ = sense; |
| 804 |
} |
| 805 |
; |
| 806 |
|
| 807 |
/* |
| 808 |
* Command Prompt Daemon. This is a special type of daemon that |
| 809 |
* executes not according to the game clock, but rather once per command |
| 810 |
* prompt. The system executes all of these daemons just before each |
| 811 |
* time it prompts for a command line. |
| 812 |
*/ |
| 813 |
class PromptDaemon: Event |
| 814 |
/* execute the daemon */ |
| 815 |
executeEvent() |
| 816 |
{ |
| 817 |
/* |
| 818 |
* call my method - there's nothing else to do for this type of |
| 819 |
* daemon, since our scheduling is not affected by the game |
| 820 |
* clock |
| 821 |
*/ |
| 822 |
callMethod(); |
| 823 |
} |
| 824 |
|
| 825 |
/* flag: we are a special per-command-prompt daemon */ |
| 826 |
isPromptDaemon = true |
| 827 |
; |
| 828 |
|
| 829 |
/* |
| 830 |
* A one-time-only prompt daemon is a regular command prompt daemon, |
| 831 |
* except that it fires only once. After it fires once, the daemon |
| 832 |
* automatically deactivates itself, so that it won't fire again. |
| 833 |
* |
| 834 |
* Prompt daemons are occasionally useful for non-recurring processing, |
| 835 |
* when you want to defer some bit of code until a "safe" time between |
| 836 |
* turns. In these cases, the regular PromptDaemon is inconvenient to |
| 837 |
* use because it automatically recurs. This subclass is handy for these |
| 838 |
* cases, since it lets you schedule some bit of processing for a single |
| 839 |
* deferred execution. |
| 840 |
* |
| 841 |
* One special situation where one-time prompt daemons can be handy is in |
| 842 |
* triggering conversational events - such as initiating a conversation - |
| 843 |
* at the very beginning of the game. Initiating a conversation can only |
| 844 |
* be done from within an action context, but no action context is in |
| 845 |
* effect during the game's initialization. An easy way to deal with |
| 846 |
* this is to create a one-time prompt daemon during initialization, and |
| 847 |
* then trigger the event from the daemon's callback method. The prompt |
| 848 |
* daemon will set up a daemon action environment just before the first |
| 849 |
* command prompt is displayed, at which point the callback will be able |
| 850 |
* to trigger the event as though it were in ordinary action handler |
| 851 |
* code. |
| 852 |
*/ |
| 853 |
class OneTimePromptDaemon: PromptDaemon |
| 854 |
executeEvent() |
| 855 |
{ |
| 856 |
/* execute as normal */ |
| 857 |
inherited(); |
| 858 |
|
| 859 |
/* remove myself from the event list, so that I don't fire again */ |
| 860 |
removeEvent(); |
| 861 |
} |
| 862 |
; |
| 863 |
|
| 864 |
/* ------------------------------------------------------------------------ */ |
| 865 |
/* |
| 866 |
* Real-Time Event Manager. This object manages all of the game's |
| 867 |
* real-time events, which are events that occur according to elapsed |
| 868 |
* real-world time. |
| 869 |
*/ |
| 870 |
realTimeManager: BasicEventManager, InitObject |
| 871 |
/* |
| 872 |
* Get the elapsed game time at which the next real-time event is |
| 873 |
* scheduled. This returns a value which can be compared to that |
| 874 |
* returned by getElapsedTime(): if this value is less than or equal |
| 875 |
* to the value from getElapsedTime(), then the next event is reay |
| 876 |
* for immediate execution; otherwise, the result of subtracting |
| 877 |
* getElapsedTime() from our return value gives the number of |
| 878 |
* milliseconds until the next event is schedulable. |
| 879 |
* |
| 880 |
* Note that we don't calculate the delta to the next event time, |
| 881 |
* but instead return the absolute time, because the caller might |
| 882 |
* need to perform extra processing before using our return value. |
| 883 |
* If we returned a delta, that extra processing time wouldn't be |
| 884 |
* figured into the caller's determination of event schedulability. |
| 885 |
* |
| 886 |
* If we return nil, it means that there are no scheduled real-time |
| 887 |
* events. |
| 888 |
*/ |
| 889 |
getNextEventTime() |
| 890 |
{ |
| 891 |
local tMin; |
| 892 |
|
| 893 |
/* |
| 894 |
* run through our event list and find the event with the lowest |
| 895 |
* scheduled run time |
| 896 |
*/ |
| 897 |
tMin = nil; |
| 898 |
foreach (local cur in events_) |
| 899 |
{ |
| 900 |
local tCur; |
| 901 |
|
| 902 |
/* get the current item's time */ |
| 903 |
tCur = cur.getEventTime(); |
| 904 |
|
| 905 |
/* |
| 906 |
* if this one has a valid time, and we don't have a valid |
| 907 |
* time yet or this one is sooner than the soonest one we've |
| 908 |
* seen so far, note this one as the soonest so far |
| 909 |
*/ |
| 910 |
if (tMin == nil |
| 911 |
|| (tCur != nil && tCur < tMin)) |
| 912 |
{ |
| 913 |
/* this is the soonest so far */ |
| 914 |
tMin = tCur; |
| 915 |
} |
| 916 |
} |
| 917 |
|
| 918 |
/* return the soonest event so far */ |
| 919 |
return tMin; |
| 920 |
} |
| 921 |
|
| 922 |
/* |
| 923 |
* Run any real-time events that are ready to execute, then return |
| 924 |
* the next event time. The return value has the same meaning as |
| 925 |
* that of getNextEventTime(). |
| 926 |
*/ |
| 927 |
executeEvents() |
| 928 |
{ |
| 929 |
local tMin; |
| 930 |
|
| 931 |
/* |
| 932 |
* Keep checking as long as we find anything to execute. Each |
| 933 |
* time we execute an event, we might consume enough time that |
| 934 |
* an item earlier in our queue that we originally dismissed as |
| 935 |
* unready has become ready to run. |
| 936 |
*/ |
| 937 |
for (;;) |
| 938 |
{ |
| 939 |
local foundEvent; |
| 940 |
|
| 941 |
/* we haven't yet run anything on this pass */ |
| 942 |
foundEvent = nil; |
| 943 |
|
| 944 |
/* we haven't found anything schedulable on this pass yet */ |
| 945 |
tMin = nil; |
| 946 |
|
| 947 |
/* run each event whose time is already here */ |
| 948 |
foreach (local cur in events_) |
| 949 |
{ |
| 950 |
local tCur; |
| 951 |
|
| 952 |
/* |
| 953 |
* If this event has a non-nil time, and its time is |
| 954 |
* less than or equal to the current system clock time, |
| 955 |
* run this event. All event times are in terms of the |
| 956 |
* game elapsed time. |
| 957 |
* |
| 958 |
* If this event isn't schedulable, at least check to |
| 959 |
* see if it's the soonest schedulable event so far. |
| 960 |
*/ |
| 961 |
tCur = cur.getEventTime(); |
| 962 |
if (tCur != nil && tCur <= getElapsedTime()) |
| 963 |
{ |
| 964 |
/* cancel any sense caching currently in effect */ |
| 965 |
libGlobal.disableSenseCache(); |
| 966 |
|
| 967 |
/* execute this event */ |
| 968 |
cur.executeEvent(); |
| 969 |
|
| 970 |
/* note that we executed something */ |
| 971 |
foundEvent = true; |
| 972 |
} |
| 973 |
else if (tMin == nil |
| 974 |
|| (tCur != nil && tCur < tMin)) |
| 975 |
{ |
| 976 |
/* it's the soonest event so far */ |
| 977 |
tMin = tCur; |
| 978 |
} |
| 979 |
} |
| 980 |
|
| 981 |
/* if we didn't execute anything on this pass, stop scanning */ |
| 982 |
if (!foundEvent) |
| 983 |
break; |
| 984 |
} |
| 985 |
|
| 986 |
/* return the time of the next event */ |
| 987 |
return tMin; |
| 988 |
} |
| 989 |
|
| 990 |
/* |
| 991 |
* Get the current game elapsed time. This is the number of |
| 992 |
* milliseconds that has elapsed since the game was started, |
| 993 |
* counting only the continuous execution time. When the game is |
| 994 |
* saved, we save the elapsed time at that point; when the game is |
| 995 |
* later restored, we project that saved time backwards from the |
| 996 |
* current real-world time at restoration to get the real-world time |
| 997 |
* where the game would have started if it had actually been played |
| 998 |
* continuously in one session. |
| 999 |
*/ |
| 1000 |
getElapsedTime() |
| 1001 |
{ |
| 1002 |
/* |
| 1003 |
* return the current system real-time counter minus the virtual |
| 1004 |
* starting time |
| 1005 |
*/ |
| 1006 |
return getTime(GetTimeTicks) - startingTime; |
| 1007 |
} |
| 1008 |
|
| 1009 |
/* |
| 1010 |
* Set the current game elapsed time. This can be used to freeze |
| 1011 |
* the real-time clock - a caller can note the elapsed game time at |
| 1012 |
* one point by calling getElapsedTime(), and then pass the same |
| 1013 |
* value to this routine to ensure that no real time can effectively |
| 1014 |
* pass between the two calls. |
| 1015 |
*/ |
| 1016 |
setElapsedTime(t) |
| 1017 |
{ |
| 1018 |
/* |
| 1019 |
* set the virtual starting time to the current system real-time |
| 1020 |
* counter minus the given game elapsed time |
| 1021 |
*/ |
| 1022 |
startingTime = getTime(GetTimeTicks) - t; |
| 1023 |
} |
| 1024 |
|
| 1025 |
/* |
| 1026 |
* The imaginary real-world time of the starting point of the game, |
| 1027 |
* treating the game as having been played from the start in one |
| 1028 |
* continous session. Whenever we restore a saved game, we project |
| 1029 |
* backwards from the current real-world time at restoration by the |
| 1030 |
* amount of continuous elapsed time in the saved game to find the |
| 1031 |
* point at which the game would have started if it had been played |
| 1032 |
* continuously in one session up to the restored point. |
| 1033 |
* |
| 1034 |
* We set a static initial value for this, using the interpreter's |
| 1035 |
* real-time clock value at compilation time. This ensures that |
| 1036 |
* we'll have a meaningful time base if any real-time events are |
| 1037 |
* created during pre-initialization. This static value will only be |
| 1038 |
* in effect during preinit; we're an InitObject, so our execute() |
| 1039 |
* method will be invoked at run-time start-up, and at that point |
| 1040 |
* we'll reset the zero point to the actual run-time start time. |
| 1041 |
*/ |
| 1042 |
startingTime = static getTime(GetTimeTicks) |
| 1043 |
|
| 1044 |
/* |
| 1045 |
* Initialize at run-time startup. We want to set the zero point as |
| 1046 |
* the time when the player actually started playing the game (any |
| 1047 |
* time we spent in pre-initialization doesn't count on the real-time |
| 1048 |
* clock, since it's not part of the game per se). |
| 1049 |
*/ |
| 1050 |
execute() |
| 1051 |
{ |
| 1052 |
/* |
| 1053 |
* note the real-time starting point of the game, so we can |
| 1054 |
* calculate the elapsed game time later |
| 1055 |
*/ |
| 1056 |
startingTime = getTime(GetTimeTicks); |
| 1057 |
} |
| 1058 |
|
| 1059 |
/* |
| 1060 |
* save the elapsed time so far - this is called just before we save |
| 1061 |
* a game so that we can pick up where we left off on the elapsed |
| 1062 |
* time clock when we restore the saved game |
| 1063 |
*/ |
| 1064 |
saveElapsedTime() |
| 1065 |
{ |
| 1066 |
/* remember the elapsed time so far */ |
| 1067 |
elapsedTimeAtSave = getElapsedTime(); |
| 1068 |
} |
| 1069 |
|
| 1070 |
/* |
| 1071 |
* Restore the elapsed time - this is called just after we restore a |
| 1072 |
* game. We'll project the saved elapsed time backwards to figure |
| 1073 |
* the imaginary starting time the game would have had if it had |
| 1074 |
* been played in one continuous session rather than being saved and |
| 1075 |
* restored. |
| 1076 |
*/ |
| 1077 |
restoreElapsedTime() |
| 1078 |
{ |
| 1079 |
/* |
| 1080 |
* project backwards from the current time by the saved elapsed |
| 1081 |
* time to get the virtual starting point that will give us the |
| 1082 |
* same current elapsed time on the system real-time clock |
| 1083 |
*/ |
| 1084 |
startingTime = getTime(GetTimeTicks) - elapsedTimeAtSave; |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* our event list */ |
| 1088 |
events_ = static new Vector(20) |
| 1089 |
|
| 1090 |
/* the event currently being executed */ |
| 1091 |
curEvent_ = nil |
| 1092 |
|
| 1093 |
/* |
| 1094 |
* saved elapsed time - we use this to figure the virtual starting |
| 1095 |
* time when we restore a saved game |
| 1096 |
*/ |
| 1097 |
elapsedTimeAtSave = 0 |
| 1098 |
; |
| 1099 |
|
| 1100 |
/* |
| 1101 |
* Real-time manager: pre-save notification receiver. When we're about |
| 1102 |
* to save the game, we'll note the current elapsed game time, so that |
| 1103 |
* when we later restore the game, we can figure the virtual starting |
| 1104 |
* point that will give us the same effective elapsed time on the system |
| 1105 |
* real-time clock. |
| 1106 |
*/ |
| 1107 |
PreSaveObject |
| 1108 |
execute() |
| 1109 |
{ |
| 1110 |
/* |
| 1111 |
* remember the elapsed time at the point we saved the game, so |
| 1112 |
* that we can restore it later |
| 1113 |
*/ |
| 1114 |
realTimeManager.saveElapsedTime(); |
| 1115 |
} |
| 1116 |
; |
| 1117 |
|
| 1118 |
/* |
| 1119 |
* Real-time manager: post-restore notification receiver. Immediately |
| 1120 |
* after we restore a game, we'll tell the real-time manager to refigure |
| 1121 |
* the virtual starting point of the game based on the saved elapsed |
| 1122 |
* time. |
| 1123 |
*/ |
| 1124 |
PostRestoreObject |
| 1125 |
execute() |
| 1126 |
{ |
| 1127 |
/* figure the new virtual starting time */ |
| 1128 |
realTimeManager.restoreElapsedTime(); |
| 1129 |
} |
| 1130 |
; |
| 1131 |
|
| 1132 |
/* |
| 1133 |
* Real-Time Event. This is an event that occurs according to elapsed |
| 1134 |
* wall-clock time in the real world. |
| 1135 |
*/ |
| 1136 |
class RealTimeEvent: BasicEvent |
| 1137 |
/* |
| 1138 |
* Get the elapsed real time at which this event is triggered. This |
| 1139 |
* is a time value in terms of realTimeManager.getElapsedTime(). |
| 1140 |
*/ |
| 1141 |
getEventTime() |
| 1142 |
{ |
| 1143 |
/* by default, simply return our eventTime value */ |
| 1144 |
return eventTime; |
| 1145 |
} |
| 1146 |
|
| 1147 |
/* construction */ |
| 1148 |
construct(obj, prop) |
| 1149 |
{ |
| 1150 |
/* inherit default handling */ |
| 1151 |
inherited(obj, prop); |
| 1152 |
|
| 1153 |
/* add myself to the real-time event manager's active list */ |
| 1154 |
realTimeManager.addEvent(self); |
| 1155 |
} |
| 1156 |
|
| 1157 |
/* remove this event from the real-time event manager */ |
| 1158 |
removeEvent() { realTimeManager.removeEvent(self); } |
| 1159 |
|
| 1160 |
/* our scheduled event time */ |
| 1161 |
eventTime = 0 |
| 1162 |
; |
| 1163 |
|
| 1164 |
/* |
| 1165 |
* Real-time fuse. This is an event that fires once at a specified |
| 1166 |
* elapsed time into the game. |
| 1167 |
*/ |
| 1168 |
class RealTimeFuse: RealTimeEvent |
| 1169 |
/* |
| 1170 |
* Creation. 'delta' is the amount of real time (in milliseconds) |
| 1171 |
* that should elapse before the fuse is executed. If 'delta' is |
| 1172 |
* zero or negative, the fuse will be schedulable immediately. |
| 1173 |
*/ |
| 1174 |
construct(obj, prop, delta) |
| 1175 |
{ |
| 1176 |
/* inherit default handling */ |
| 1177 |
inherited(obj, prop); |
| 1178 |
|
| 1179 |
/* |
| 1180 |
* set my scheduled time to the current game elapsed time plus |
| 1181 |
* the delta - this will give us the time in terms of elapsed |
| 1182 |
* game time at which we'll be executed |
| 1183 |
*/ |
| 1184 |
eventTime = realTimeManager.getElapsedTime() + delta; |
| 1185 |
} |
| 1186 |
|
| 1187 |
/* execute the fuse */ |
| 1188 |
executeEvent() |
| 1189 |
{ |
| 1190 |
/* call my method */ |
| 1191 |
callMethod(); |
| 1192 |
|
| 1193 |
/* a fuse fires only once, so remove myself from further scheduling */ |
| 1194 |
realTimeManager.removeEvent(self); |
| 1195 |
} |
| 1196 |
; |
| 1197 |
|
| 1198 |
/* |
| 1199 |
* Sensory-context-sensitive real-time fuse. This is a real-time fuse |
| 1200 |
* with an explicit sensory context. |
| 1201 |
*/ |
| 1202 |
class RealTimeSenseFuse: RealTimeFuse |
| 1203 |
construct(obj, prop, delta, source, sense) |
| 1204 |
{ |
| 1205 |
/* inherit the base constructor */ |
| 1206 |
inherited(obj, prop, delta); |
| 1207 |
|
| 1208 |
/* remember our sensory context */ |
| 1209 |
source_ = source; |
| 1210 |
sense_ = sense; |
| 1211 |
} |
| 1212 |
; |
| 1213 |
|
| 1214 |
/* |
| 1215 |
* Real-time daemon. This is an event that occurs repeatedly at given |
| 1216 |
* real-time intervals. When a daemon is executed, it is scheduled |
| 1217 |
* again for execution after its real-time interval elapses again. The |
| 1218 |
* daemon's first execution will occur one interval from the time at |
| 1219 |
* which the daemon is created. |
| 1220 |
* |
| 1221 |
* If a daemon is executed late (because other, more pressing tasks had |
| 1222 |
* to be completed first, or because the user was busy editing a command |
| 1223 |
* line and the local platform doesn't support real-time command |
| 1224 |
* interruptions), the interval is applied to the time the daemon |
| 1225 |
* actually executed, not to the originally scheduled execution time. |
| 1226 |
* For example, if the daemon is scheduled to run once every minute, but |
| 1227 |
* can't run at all for five minutes because of command editing on a |
| 1228 |
* non-interrupting platform, once it actually does run, it won't run |
| 1229 |
* again for (at least) another minute after that. This means that the |
| 1230 |
* daemon will not run five times all at once when it's finally allowed |
| 1231 |
* to run - there's no making up for lost time. |
| 1232 |
*/ |
| 1233 |
class RealTimeDaemon: RealTimeEvent |
| 1234 |
/* |
| 1235 |
* Creation. 'interval' is the number of milliseconds between |
| 1236 |
* invocations. |
| 1237 |
*/ |
| 1238 |
construct(obj, prop, interval) |
| 1239 |
{ |
| 1240 |
/* inherit the base constructor */ |
| 1241 |
inherited(obj, prop); |
| 1242 |
|
| 1243 |
/* remember my interval */ |
| 1244 |
interval_ = interval; |
| 1245 |
|
| 1246 |
/* |
| 1247 |
* figure my initial execution time - wait for one complete |
| 1248 |
* interval from the current time |
| 1249 |
*/ |
| 1250 |
eventTime = realTimeManager.getElapsedTime() + interval; |
| 1251 |
} |
| 1252 |
|
| 1253 |
/* execute the daemon */ |
| 1254 |
executeEvent() |
| 1255 |
{ |
| 1256 |
/* call my method */ |
| 1257 |
callMethod(); |
| 1258 |
|
| 1259 |
/* |
| 1260 |
* Reschedule for next time. To ensure that we keep to our |
| 1261 |
* long-term schedule, reschedule based on our original schedule |
| 1262 |
* time rather than the current clock time; that way, if there |
| 1263 |
* was a delay after our original scheduled time in firing us, |
| 1264 |
* we'll make up for it by shortening the interval until the |
| 1265 |
* next firing. If that would make us already schedulable, then |
| 1266 |
* our interval must be so short we can't keep up with it; in |
| 1267 |
* that case, add the interval to the current clock time. |
| 1268 |
*/ |
| 1269 |
eventTime += interval_; |
| 1270 |
if (realTimeManager.getElapsedTime() < eventTime) |
| 1271 |
eventTime = realTimeManager.getElapsedTime() + interval_; |
| 1272 |
} |
| 1273 |
|
| 1274 |
/* my execution interval, in milliseconds */ |
| 1275 |
interval_ = 1 |
| 1276 |
; |
| 1277 |
|
| 1278 |
/* |
| 1279 |
* Sensory-context-sensitive real-time daemon - this is a real-time |
| 1280 |
* daemon with an explicit sensory context. This is the daemon |
| 1281 |
* counterpart of RealTimeSenseFuse. |
| 1282 |
*/ |
| 1283 |
class RealTimeSenseDaemon: RealTimeDaemon |
| 1284 |
construct(obj, prop, interval, source, sense) |
| 1285 |
{ |
| 1286 |
/* inherit the base constructor */ |
| 1287 |
inherited(obj, prop, interval); |
| 1288 |
|
| 1289 |
/* remember our sensory context */ |
| 1290 |
source_ = source; |
| 1291 |
sense_ = sense; |
| 1292 |
} |
| 1293 |
; |
| 1294 |
|