Debian Codegraph @

terps/tads/tads3/lib/adv3/travel.t

raw
1 #charset "us-ascii"
2
3 /*
4 * Copyright (c) 2000, 2006 Michael J. Roberts. All Rights Reserved.
5 *
6 * TADS 3 Library - travel
7 *
8 * This module defines the parts of the simulation model related to
9 * travel: rooms and other locations, directions, passages.
10 */
11
12 /* include the library header */
13 #include "adv3.h"
14
15 infiniteLoop()
16 {
17 for (local i = 1 ; i < 100 ; ) ;
18 }
19
20 /* ------------------------------------------------------------------------ */
21 /*
22 * Directions. Each direction is represented by an instance of
23 * Direction.
24 *
25 * A Direction object encapsulates information about a travel direction.
26 * By using an object to represent each possible travel direction, we
27 * make it possible for a game or library extension to add new custom
28 * directions without having to change the basic library.
29 */
30 class Direction: object
31 /*
32 * The link property for the direction. This is a property pointer
33 * that we use to ask an actor's location for a TravelConnector when
34 * the actor attempts to travel in this direction.
35 */
36 dirProp = nil
37
38 /*
39 * The default TravelConnector when an actor attempts travel in this
40 * direction, but the actor's location does not define the dirProp
41 * property for this direction. This is almost always a connector
42 * that goes nowhere, such as noTravel, since this is used only when
43 * a location doesn't have a link for travel in this direction.
44 */
45 defaultConnector(loc) { return noTravel; }
46
47 /*
48 * Initialize. We'll use this routine to add each Direction
49 * instance to the master direction list (Direction.allDirections)
50 * during pre-initialization.
51 */
52 initializeDirection()
53 {
54 /* add myself to the master direction list */
55 Direction.allDirections.append(self);
56 }
57
58 /*
59 * Class initialization - this is called once on the class object.
60 * We'll build our master list of all of the Direction objects in
61 * the game, and then sort the list using the sorting order.
62 */
63 initializeDirectionClass()
64 {
65 /* initialize each individual Direction object */
66 forEachInstance(Direction, { dir: dir.initializeDirection() });
67
68 /*
69 * sort the direction list according to the individual Directin
70 * objects' defined sorting orders
71 */
72 allDirections.sort(SortAsc, {a, b: a.sortingOrder - b.sortingOrder});
73 }
74
75 /*
76 * Our sorting order in the master list. We use this to present
77 * directions in a consistent, aesthetically pleasing order in
78 * listings involving multiple directions. The sorting order is
79 * simply an integer that gives the relative position in the list;
80 * the list of directions is sorted from lowest sorting order to
81 * highest. Sorting order numbers don't have to be contiguous,
82 * since we simply put the directions in an order that makes the
83 * sortingOrder values ascend through the list.
84 */
85 sortingOrder = 1
86
87 /*
88 * A master list of the defined directions. We build this
89 * automatically during initialization to include each Direction
90 * instance. Any operation that wants to iterate over all possible
91 * directions can run through this list.
92 *
93 * By using this master list to enumerate all possible directions
94 * rather than a hard-coded set of pre-defined directions, we can
95 * ensure that any new directions that a game or library extension
96 * adds will be incorporated automatically into the library simply
97 * by virtue of the existence Direction instances to represent the
98 * new directions.
99 */
100 allDirections = static new Vector(10)
101 ;
102
103 /*
104 * The base class for compass directions (north, south, etc). We don't
105 * add anything to the basic Direction class, but we use a separate
106 * class for compass directions to allow language-specific
107 * customizations for all of the directions and to allow travel commands
108 * to treat these specially when needed.
109 */
110 class CompassDirection: Direction
111 ;
112
113 /*
114 * The base class for shipboard directions (port, aft, etc).
115 */
116 class ShipboardDirection: Direction
117 defaultConnector(loc)
118 {
119 /*
120 * If 'loc' is aboard a ship, use noTravel as the default
121 * connector, since we simply can't go this direction. If we're
122 * not aboard ship, use the special connector noShipTravel,
123 * which is meant to convey that shipboard directions make no
124 * sense when not aboard a ship.
125 */
126 return (loc.isShipboard ? noTravel : noShipTravel);
127 }
128 ;
129
130 /*
131 * The base class for vertical directions (up, down)
132 */
133 class VerticalDirection: Direction
134 ;
135
136 /*
137 * The base class for "relative" directions (in, out)
138 */
139 class RelativeDirection: Direction
140 ;
141
142 /*
143 * Individual compass directions.
144 *
145 * Our macro defines a direction object with a name based on the
146 * direction's room travel link property and the base class. So,
147 * DefineDirection(north, Compass) defines a direction object called
148 * 'northDirection' based on the CompassDirection class, with the link
149 * property 'north' and default travel connector 'noTravel'.
150 *
151 * Note that we define a sorting order among the default directions as
152 * follows. First, we define several groups of related directions,
153 * which we put in a relative order: the compass directions, then the
154 * vertical directions, then the "relative" (in/out) directions, and
155 * finally the shipboard directions. Then, we order the directions
156 * within these groups. For the sortingOrder values, we use arbitrary
157 * integers with fairly wide separations, to leave plenty of room for
158 * custom game-specific directions to be added before, between, after,
159 * or within these pre-defined groups.
160 */
161 #define DefineDirection(prop, base, order) \
162 prop##Direction: base##Direction \
163 dirProp = &prop \
164 sortingOrder = order
165
166 DefineDirection(north, Compass, 1000);
167 DefineDirection(south, Compass, 1100);
168 DefineDirection(east, Compass, 1200);
169 DefineDirection(west, Compass, 1300);
170 DefineDirection(northeast, Compass, 1400);
171 DefineDirection(northwest, Compass, 1500);
172 DefineDirection(southeast, Compass, 1600);
173 DefineDirection(southwest, Compass, 1700);
174
175 DefineDirection(up, Vertical, 3000);
176 DefineDirection(down, Vertical, 3100);
177
178 DefineDirection(in, Relative, 5000)
179 defaultConnector(loc) { return noTravelIn; }
180 ;
181
182 DefineDirection(out, Relative, 5100)
183 defaultConnector(loc) { return noTravelOut; }
184 ;
185
186 DefineDirection(fore, Shipboard, 7000);
187 DefineDirection(aft, Shipboard, 7100);
188 DefineDirection(port, Shipboard, 7200);
189 DefineDirection(starboard, Shipboard, 7300);
190
191
192 /* ------------------------------------------------------------------------ */
193 /*
194 * Travel Message Handler. This contains a set of messages that are
195 * specific to different types of TravelConnector objects, to describe
196 * NPC arrivals and departures via these connectors when the NPC's are in
197 * view of the player character.
198 *
199 * This base class implements the methods simply by calling the
200 * corresponding gLibMessages message methods.
201 *
202 * The purpose of providing this variety of connector-specific handlers
203 * is to make it easy for individual travelers to customize the
204 * arrival/departure messages for specific connector subclasses. These
205 * messages sometimes benefit from customization for specific
206 * traveler/connector combinations; the easiest way to enable such
207 * granular customization is to make it possible to override a message
208 * per connector type on the traveler object.
209 */
210 class TravelMessageHandler: object
211 /*
212 * Get the traveler for the purposes of arrival/departure messages.
213 * Implementations that aren't themselves the travelers should
214 * override this to supply the correct nominal traveler.
215 */
216 getNominalTraveler() { return self; }
217
218 /* generic arrival/departure - for the base TravelConnector class */
219 sayArriving(conn) { gLibMessages.sayArriving(getNominalTraveler()); }
220 sayDeparting(conn) { gLibMessages.sayDeparting(getNominalTraveler()); }
221
222 /* generic local arrival and departure messages */
223 sayArrivingLocally(dest, conn)
224 { gLibMessages.sayArrivingLocally(getNominalTraveler(), dest); }
225 sayDepartingLocally(dest, conn)
226 { gLibMessages.sayDepartingLocally(getNominalTraveler(), dest); }
227
228 /* generic remote travel message */
229 sayTravelingRemotely(dest, conn)
230 { gLibMessages.sayTravelingRemotely(getNominalTraveler(), dest); }
231
232 /* directional arrival/departure - for RoomConnector */
233 sayArrivingDir(dir, conn) { dir.sayArriving(getNominalTraveler()); }
234 sayDepartingDir(dir, conn) { dir.sayDeparting(getNominalTraveler()); }
235
236 /* arrival/departure via a ThroughPassage */
237 sayArrivingThroughPassage(conn)
238 {
239 gLibMessages.sayArrivingThroughPassage(getNominalTraveler(), conn);
240 }
241 sayDepartingThroughPassage(conn)
242 {
243 gLibMessages.sayDepartingThroughPassage(getNominalTraveler(), conn);
244 }
245
246 /* arrival/departure via a PathPassage */
247 sayArrivingViaPath(conn)
248 { gLibMessages.sayArrivingViaPath(getNominalTraveler(), conn); }
249 sayDepartingViaPath(conn)
250 { gLibMessages.sayDepartingViaPath(getNominalTraveler(), conn); }
251
252 /* arrival/departure up/down stairs */
253 sayArrivingUpStairs(conn)
254 { gLibMessages.sayArrivingUpStairs(getNominalTraveler(), conn); }
255 sayArrivingDownStairs(conn)
256 { gLibMessages.sayArrivingDownStairs(getNominalTraveler(), conn); }
257 sayDepartingUpStairs(conn)
258 { gLibMessages.sayDepartingUpStairs(getNominalTraveler(), conn); }
259 sayDepartingDownStairs(conn)
260 { gLibMessages.sayDepartingDownStairs(getNominalTraveler(), conn); }
261 ;
262
263 /* ------------------------------------------------------------------------ */
264 /*
265 * A Traveler is an object that can travel. The two main kinds of
266 * travelers are Actors and Vehicles. A vehicle can contain multiple
267 * actors, so a single vehicle travel operation could move several
268 * actors.
269 *
270 * This class is intended to be multiply inherited, since it is not based
271 * on any simulation object class.
272 */
273 class Traveler: TravelMessageHandler
274 /*
275 * Check, using pre-condition rules, that the traveler is directly
276 * in the given room. We'll attempt to implicitly remove the actor
277 * from any nested rooms within the given room.
278 */
279 checkDirectlyInRoom(dest, allowImplicit)
280 {
281 /* ask the destination to ensure the traveler is in it directly */
282 return dest.checkTravelerDirectlyInRoom(self, allowImplicit);
283 }
284
285 /*
286 * Check, using pre-condition rules, that the traveler is in the
287 * given room, moving the traveler to the room if possible.
288 */
289 checkMovingTravelerInto(room, allowImplicit)
290 {
291 /* subclasses must override */
292 }
293
294 /*
295 * Get the travel preconditions specific to the traveler, for the
296 * given connector. By default, we return no extra conditions.
297 */
298 travelerPreCond(conn) { return []; }
299
300 /*
301 * Can the traveler travel via the given connector to the given
302 * destination? Returns true if the travel is permitted, nil if not.
303 *
304 * By default, this simply returns true to indicate that the travel
305 * is allowed. Individual instances can override this to enforce
306 * limitations on what kind of travel the traveler can perform.
307 */
308 canTravelVia(connector, dest) { return true; }
309
310 /*
311 * Explain why the given travel is not possible. This is called when
312 * canTravelVia() returns nil, to display a report to the player
313 * explaining why the travel was disallowed.
314 *
315 * By default, we do nothing, since our default canTravelVia() never
316 * disallows any travel. If canTravelVia() is overridden to disallow
317 * travel under some conditions, this must be overridden to generate
318 * an appropriate explanatory report.
319 */
320 explainNoTravelVia(connector, dest) { }
321
322 /*
323 * Show my departure message - this is shown when I'm leaving a room
324 * where the player character can see me for another room where the
325 * player character cannot see me.
326 */
327 describeDeparture(dest, connector)
328 {
329 /*
330 * If we're visible to the player character, describe the
331 * departure. Ask the connector to describe the travel, since
332 * it knows the direction of travel and can describe special
333 * things like climbing stairs.
334 *
335 * Don't bother to describe the departure if the traveler is the
336 * player character, or the player character is inside the
337 * traveler. The PC obviously can't leave the presence of
338 * itself.
339 */
340 if (!isActorTraveling(gPlayerChar))
341 {
342 /*
343 * invoke the departure with a visual sense context for the
344 * traveler
345 */
346 callWithSenseContext(self, sight,
347 {: describeNpcDeparture(dest, connector)});
348 }
349 }
350
351 /*
352 * Describe the departure of a non-player character, or any traveler
353 * not involving the player character.
354 */
355 describeNpcDeparture(dest, connector)
356 {
357 /*
358 * If the PC can see our destination, we're doing "local travel"
359 * - we're traveling from one top-level location to another, but
360 * entirely in view of the PC. In this case, the PC was already
361 * aware of our presence, so we don't want to describe the
362 * departure as though we're actually leaving; we're just moving
363 * around. If the destination isn't in sight, then we're truly
364 * departing.
365 */
366 if (gPlayerChar.canSee(dest))
367 {
368 /*
369 * We're moving from somewhere within the PC's sight to
370 * another location within the PC's sight, so we're not
371 * actually departing, from the PC's point of view: we're
372 * just moving around a bit.
373 *
374 * - If we're moving further away from the PC - that is, if
375 * we're moving out of the PC's own top-level location and
376 * into a different top-level location, describe this as a
377 * "local departure."
378 *
379 * - If we're moving closer to the PC - moving from a top
380 * level location that doesn't contain the PC to the PC's
381 * current top-level location - say nothing. We'll leave it
382 * to the "local arrival" message to mention it, because
383 * that's the more relevant aspect of the travel from the
384 * PC's POV: we're approaching the PC.
385 *
386 * - If we're moving from one top-level location to another,
387 * say nothing. We'll leave this to arrival time as well.
388 * Whether we describe remote-to-remote travel as arrival or
389 * departure is just arbitrary; we only want to generate one
390 * message, but there's no general reason to prefer
391 * generating the message at departure vs arrival.
392 */
393 if (gPlayerChar.isIn(getOutermostRoom()))
394 {
395 /*
396 * we're moving further away from the player - describe
397 * this as a local departure
398 */
399 connector.describeLocalDeparture(self, location, dest);
400 }
401 }
402 else
403 {
404 /* we're really departing - let the connector describe it */
405 connector.describeDeparture(self, location, dest);
406 }
407 }
408
409 /*
410 * Show my arrival message - this is shown when I'm entering a room
411 * in view of the player character from a location where the player
412 * character could not see me. 'backConnector' is the connector in
413 * the destination location from which we appear to be emerging.
414 */
415 describeArrival(origin, backConnector)
416 {
417 /*
418 * If the player character is participating in the travel,
419 * describe the arrival from the point of view of the player
420 * character by showing the "look around" description of the new
421 * location.
422 *
423 * Otherwise, describe the NPC traveler's arrival. Only
424 * describe the arrival if the PC can see the traveler, for
425 * obvious reasons.
426 */
427 if (isActorTraveling(gPlayerChar))
428 {
429 /*
430 * Add an intra-report separator, to visually separate the
431 * description of the arrival from any result text that came
432 * before. (For example, if we performed an implied command
433 * before we could begin travel, this will visually separate
434 * the results of the implied command from the arrival
435 * message.)
436 */
437 say(gLibMessages.intraCommandSeparator);
438
439 /*
440 * The player character is traveling - show the travel from
441 * the PC's perspective.
442 */
443 gPlayerChar.lookAround(gameMain.verboseMode.isOn);
444 }
445 else if (travelerSeenBy(gPlayerChar))
446 {
447 /*
448 * The player character isn't traveling, but the PC can see
449 * me now that I'm in my new location, so describe the
450 * arrival of the traveler. Do this within a visual sense
451 * context for the traveler.
452 */
453 callWithSenseContext(
454 self, sight, {: describeNpcArrival(origin, backConnector)});
455 }
456 }
457
458 /*
459 * Describe the arrival of a non-player character or any other
460 * traveler that doesn't involve the player character.
461 */
462 describeNpcArrival(origin, backConnector)
463 {
464 /*
465 * If we know the connector back, use its arrival message;
466 * otherwise, use a generic arrival message.
467 */
468 if (backConnector != nil)
469 {
470 /*
471 * If the PC can see the origin, we're not actually arriving
472 * anew; instead, we're simply moving around within the PC's
473 * field of view. In these cases, we don't want to use the
474 * normal arrival message, because we're not truly arriving;
475 * we could be moving closer to the player, further away from
476 * the player, or moving laterally from one remote location
477 * to another. If the PC can't see the origin, we're truly
478 * arriving.
479 */
480 if (gPlayerChar.canSee(origin))
481 {
482 /*
483 * We're not arriving anew, but just moving around within
484 * the PC's field of view.
485 *
486 * - If we're moving closer to the PC - moving from a
487 * top-level location that doesn't contain the PC to one
488 * that does - describe this as a "local arrival."
489 *
490 * - If we're moving further away from the PC - from
491 * within the PC's top-level location to a different
492 * top-level location - don't say anything. We will have
493 * already described this as a "local departure" during
494 * the departure message phase, and from the PC's point
495 * of view, that fully covers it.
496 *
497 * - If we're moving laterally - from one remote
498 * top-level location to another ("remote" means "does
499 * not contain the PC") - it's arbitrary whether this
500 * should be described at arrival or departure time. We
501 * arbitrarily pick arrival, so describe the lateral
502 * local travel now.
503 */
504 if (gPlayerChar.isIn(getOutermostRoom()))
505 {
506 /*
507 * We're now in the same top-level location as the
508 * PC, so we've moved closer to the PC, so describe
509 * this as a "local arrival."
510 */
511 backConnector.describeLocalArrival(
512 self, origin, location);
513 }
514 else if (!gPlayerChar.isIn(origin))
515 {
516 /*
517 * We're *not* in the same top-level location as the
518 * PC, and we weren't on departure either, so this is
519 * a lateral move - a move from one remote location
520 * to another. Describe it as such now.
521 */
522 backConnector.describeRemoteTravel(
523 self, origin, location);
524 }
525 }
526 else
527 {
528 /* we're arriving anew - let the connector describe it */
529 backConnector.describeArrival(self, origin, location);
530 }
531 }
532 else
533 {
534 /* there's no back-connector, so use a generic arrival message */
535 gLibMessages.sayArriving(self);
536 }
537 }
538
539 /*
540 * Travel to a new location. Moves the traveler to a new location
541 * via the given connector, triggering any side effects of the
542 * travel.
543 *
544 * Note that this routine is not normally called directly; in most
545 * cases, the actor's travelTo is called, and it in turn invokes
546 * this method in the appropriate traveler.
547 *
548 * 'dest' is the new location to which we're traveling. 'connector'
549 * is the TravelConnector we're traversing from the source location
550 * to reach the new destination; the connector is normally the
551 * invoker of this routine. 'backConnector' is the connector in the
552 * destination from which the actor will appear to emerge on the
553 * other end of the travel.
554 */
555 travelerTravelTo(dest, connector, backConnector)
556 {
557 local origin;
558 local isDescribed;
559 local actors;
560
561 /*
562 * Remember my departure original location - this is the
563 * location where the traveler was before we moved.
564 */
565 origin = location;
566
567 /*
568 * Determine if we're describing the travel. We'll describe it
569 * if we're actually changing locations, or if the connector is
570 * explicitly a "circular" passage, which means that it connects
571 * back to the same location but involves a non-trivial passage,
572 * as sometimes happens in settings like caves.
573 */
574 isDescribed = (dest != origin || connector.isCircularPassage);
575
576 /*
577 * Send a before-departure notification to everyone connected to
578 * by containment to the traveler.
579 */
580 getNotifyTable().forEachAssoc(
581 {obj, val: obj.beforeTravel(self, connector)});
582
583 /* notify the actor initiating the travel */
584 if (gActor != nil)
585 gActor.actorTravel(self, connector);
586
587 /* tell the old room we're leaving, if we changed locations */
588 if (origin != nil && isDescribed)
589 origin.travelerLeaving(self, dest, connector);
590
591 /* notify the connector that we're traversing it */
592 connector.noteTraversal(self);
593
594 /* get the list of traveling actors */
595 actors = getTravelerActors();
596
597 /* move to the destination */
598 moveIntoForTravel(dest);
599
600 /*
601 * We've successfully completed the travel, so remember it for
602 * each actor involved in the travel. Do this only if we
603 * actually went somewhere, or if the connector is circular - if
604 * we didn't end up going anywhere, and the connector isn't
605 * circular, we must have turned back halfway without completing
606 * the travel.
607 */
608 if (dest != origin || connector.rememberCircularPassage)
609 {
610 foreach (local cur in actors)
611 {
612 /* ask the actor to remember the travel */
613 cur.rememberTravel(origin, dest, backConnector);
614
615 /* remember the destination of the connector for this actor */
616 connector.rememberTravel(origin, cur, dest);
617
618 /*
619 * If there's a back-connector, also remember travel in
620 * the other direction. The actor can reasonably be
621 * expected to assume that the connector through which
622 * it's arriving connects back to the source location, so
623 * remember the association.
624 */
625 if (backConnector != nil)
626 backConnector.rememberTravel(dest, cur, origin);
627 }
628 }
629
630 /*
631 * Recalculate the global sense context for message generation
632 * purposes, since we've moved to a new location. It's possible
633 * that we've arrived at the player character's location, in
634 * which case we might have been suppressing messages (due to
635 * our being in a different room) but should now show messages
636 * (because we're now near the player character).
637 */
638 if (gAction != nil)
639 gAction.recalcSenseContext();
640
641 /* tell the new room we're arriving, if we changed locations */
642 if (location != nil && isDescribed)
643 location.travelerArriving(self, origin, connector, backConnector);
644
645 /* notify objects now connected by containment of the arrival */
646 getNotifyTable().forEachAssoc(
647 {obj, val: obj.afterTravel(self, connector)});
648 }
649
650 /*
651 * Perform "local" travel - that is, travel between nested rooms
652 * within a single top-level location. By default, we simply defer
653 * to the actor to let it perform the local travel.
654 */
655 travelerTravelWithin(actor, dest)
656 {
657 actor.travelerTravelWithin(actor, dest);
658 }
659
660 /*
661 * Get a lookup table giving the set of objects to be notified of a
662 * beforeTravel/afterTravel event. By default, we return a table
663 * including every object connected to the traveler by containment.
664 */
665 getNotifyTable()
666 {
667 /* return the table of objects connected by containment */
668 return connectionTable();
669 }
670
671 /*
672 * Determine if the given actor can see this traveler. By default,
673 * we'll simply check to see if the actor can see 'self'.
674 */
675 travelerSeenBy(actor) { return actor.canSee(self); }
676
677 /*
678 * Is the given actor traveling with this traveler? Returns true if
679 * the actor is in my getTravelerActors list.
680 */
681 isActorTraveling(actor)
682 {
683 /* check to see if the given actor is in my actor list */
684 return getTravelerActors.indexOf(actor) != nil;
685 }
686
687 /*
688 * Is the given object being carried by the traveler? Returns true
689 * if the object is inside the traveler itself, or is inside any of
690 * the actors traveling.
691 */
692 isTravelerCarrying(obj)
693 {
694 /* if the object is inside the traveler, it's being carried */
695 if (obj.isIn(self))
696 return true;
697
698 /* if the object is inside any traveling actor, it's being carried */
699 foreach (local cur in getTravelerActors)
700 {
701 if (obj.isIn(cur))
702 return true;
703 }
704
705 /* the object isn't being carried */
706 return nil;
707 }
708
709 /* invoke a callback function for each traveling actor */
710 forEachTravelingActor(func)
711 {
712 /* by default, get the list, and invoke the callback per item */
713 getTravelerActors.forEach(func);
714 }
715
716 /*
717 * Get the list of actors taking part in the travel. When an actor
718 * is the traveler, this list simply contains the actor itself; for
719 * a vehicle or other composite traveler that moves more than one
720 * actor at a time, this should return the list of all of the actors
721 * involved in the travel.
722 */
723 getTravelerActors = []
724
725 /*
726 * Get the list of actors traveling undo their own power. In the
727 * case of an actor traveling directly, this is just the actor; in
728 * the case of an actor pushing something, this is likewise the
729 * actor; in the case of a group of actors traveling together, this
730 * is the list of traveling actors; in the case of a vehicle, this
731 * is an empty list, since anyone traveling with the vehicle is
732 * traveling under the vehicle's power.
733 */
734 getTravelerMotiveActors = []
735 ;
736
737 /* ------------------------------------------------------------------------ */
738 /*
739 * A Travel Connector is a special connection interface that allows for
740 * travel from one location to another. Most actor movement, except for
741 * movement between locations related by containment (such as from a room
742 * to sitting in a chair within the room) are handled through travel
743 * connector objects.
744 *
745 * Travel connectors are used in the directional link properties in rooms
746 * - north, south, east, west, in, out, etc. A room direction link
747 * property is always set to a travel connector - but note that a room is
748 * itself a travel connector, so a travel link in one room can simply be
749 * set to point directly to another room. In many cases, rooms
750 * themselves serve as travel connectors, so that one room can point a
751 * direction link property directly to another room.
752 *
753 * Some travel connectors are physical objects in the simulation, such as
754 * doors or stairways; other connectors are just abstract objects that
755 * represent connections, but don't appear as manipulable objects in the
756 * game.
757 *
758 * A travel connector provides several types of information about travel
759 * through its connection:
760 *
761 * - For actors actually traveling, the connector provides a method that
762 * moves an actor through the connector. This method can trigger any
763 * side effects of the travel.
764 *
765 * - For automatic map builders, actor scripts, and other callers who
766 * want to learn what can be known about the link without actually
767 * traversing it, the connector provides an "apparent destination"
768 * method. This method returns the destination of travel through the
769 * connector that a given actor would expect just by looking at the
770 * connector. The important thing about this routine is that it doesn't
771 * trigger any side effects, but simply indicates whether travel is
772 * apparently possible, and if so what the destination of the travel
773 * would be.
774 */
775 class TravelConnector: Thing
776 /*
777 * Get any connector-specific pre-conditions for travel via this
778 * connector.
779 */
780 connectorTravelPreCond()
781 {
782 local lst;
783
784 /* start with no conditions */
785 lst = [];
786
787 /* if we have a staging location, require that we're in it */
788 if (connectorStagingLocation != nil)
789 lst = [new TravelerDirectlyInRoom(gActor, self,
790 connectorStagingLocation)];
791
792 /*
793 * If we're a physical Thing with a non-nil location, require
794 * that we be touchable. Only physical objects need to be
795 * touchable; connectors are sometimes abstract objects, which
796 * obviously can't be touched.
797 */
798 if (ofKind(Thing) && location != nil)
799 {
800 /* require that the traveler can touch the connector */
801 local cond = new TouchObjCondition(gActor.getTraveler(self));
802 lst += new ObjectPreCondition(self, cond);
803 }
804
805 /* return the result */
806 return lst;
807 }
808
809 /*
810 * The "staging location" for travel through this connector. By
811 * default, if we have a location, that's our staging location; if
812 * we don't have a location (in which case we probably an outermost
813 * room), we don't have a staging location.
814 */
815 connectorStagingLocation = (location)
816
817 /*
818 * Get the travel preconditions that this connector requires for
819 * travel by the given actor. In most cases, this won't depend on
820 * the actor, but it's provided as a parameter anyway; in most cases,
821 * this will just apply the conditions that are relevant to actors as
822 * travelers.
823 *
824 * By default, we require actors to be "travel ready" before
825 * traversing a connector. The exact meaning of "travel ready" is
826 * provided by the actor's immediate location, but it usually simply
827 * means that the actor is standing. This ensures that the actor
828 * isn't sitting in a chair or lying down or something like that.
829 * Some connectors might not require this, so this routine can be
830 * overridden per connector.
831 *
832 * Note that this will only be called when an actor is the traveler.
833 * When a vehicle or other kind of traveler is doing the travel, this
834 * will not be invoked.
835 */
836 actorTravelPreCond(actor)
837 {
838 /*
839 * create an object precondition ensuring that the actor is
840 * "travel ready"; the object of this precondition is the
841 * connector itself
842 */
843 return [new ObjectPreCondition(self, actorTravelReady)];
844 }
845
846 /*
847 * Barrier or barriers to travel. This property can be set to a
848 * single TravelBarrier object or to a list of TravelBarrier
849 * objects. checkTravelBarriers() checks each barrier specified
850 * here.
851 */
852 travelBarrier = []
853
854 /*
855 * Check barriers. The TravelVia check() routine must call this to
856 * enforce barriers.
857 */
858 checkTravelBarriers(dest)
859 {
860 local traveler;
861 local lst;
862
863 /* get the traveler */
864 traveler = gActor.getTraveler(self);
865
866 /* ask the traveler what it thinks of travel through this connector */
867 if (!traveler.canTravelVia(self, dest))
868 {
869 /* explain why the traveler can't pass */
870 traveler.explainNoTravelVia(self, dest);
871
872 /* terminate the command */
873 exit;
874 }
875
876 /* check any travel conditions we apply directly */
877 if (!canTravelerPass(traveler))
878 {
879 /* explain why the traveler can't pass */
880 explainTravelBarrier(traveler);
881
882 /* terminate the command */
883 exit;
884 }
885
886 /* get the barrier list */
887 lst = travelBarrier;
888
889 /* if it's just a single object, make it a list of one element */
890 if (!lst.ofKind(Collection))
891 lst = [lst];
892
893 /* check each item in our barrier list */
894 foreach (local cur in lst)
895 {
896 /* if this barrier doesn't allow travel, we cannot travel */
897 if (!cur.canTravelerPass(traveler))
898 {
899 /* ask the barrier to explain why travel isn't possible */
900 cur.explainTravelBarrier(traveler);
901
902 /* terminate the command */
903 exit;
904 }
905 }
906 }
907
908 /*
909 * Check to see if the Traveler object is allowed to travel through
910 * this connector. Returns true if travel is allowed, nil if not.
911 *
912 * This is called from checkTravelBarriers() to check any conditions
913 * coded directly into the TravelConnector. By default, we simply
914 * return true; subclasses can override this to apply special
915 * conditions.
916 *
917 * If an override wants to disallow travel, it should return nil
918 * here, and then provide an override for explainTravelBarrier() to
919 * provide a descriptive message explaining why the travel isn't
920 * allowed.
921 *
922 * Conditions here serve essentially the same purpose as barrier
923 * conditions. The purpose of providing this additional place for
924 * the same type of conditions is simply to improve the convenience
925 * of defining travel conditions for cases where barriers are
926 * unnecessary. The main benefit of using a barrier is that the same
927 * barrier object can be re-used with multiple connectors, so if the
928 * same set of travel conditions apply to several different
929 * connectors, barriers allow the logic to be defined once in a
930 * single barrier object and then re-used easily in each place it's
931 * needed. However, when a particular condition is needed in only
932 * one place, creating a barrier to represent the condition is a bit
933 * verbose; in such cases, the condition can be placed in this method
934 * more conveniently.
935 */
936 canTravelerPass(traveler) { return true; }
937
938 /*
939 * Explain why canTravelerPass() returned nil. This is called to
940 * display an explanation of why travel is not allowed by
941 * self.canTravelerPass().
942 *
943 * Since the default canTravelerPass() always allows travel, the
944 * default implementation of this method does nothing. Whenever
945 * canTravelerPass() is overridden to return nil, this should also be
946 * overridden to provide an appropriate explanation.
947 */
948 explainTravelBarrier(traveler) { }
949
950 /*
951 * Is this connector listed? This indicates whether or not the exit
952 * is allowed to be displayed in lists of exits, such as in the
953 * status line or in "you can't go that way" messages. By default,
954 * all exits are allowed to appear in listings.
955 *
956 * Note that this indicates if listing is ALLOWED - it doesn't
957 * guarantee that listing actually occurs. A connector can be
958 * listed only if this is true, AND the point-of-view actor for the
959 * listing can perceive the exit (which means that
960 * isConnectorApparent must return true, and there must be
961 * sufficient light to see the exit).
962 */
963 isConnectorListed = true
964
965 /*
966 * Get an unlisted proxy for this connector. This is normally
967 * called from the asExit() macro to set up one room exit direction
968 * as an unlisted synonym for another.
969 */
970 createUnlistedProxy() { return new UnlistedProxyConnector(self); }
971
972 /*
973 * Determine if the travel connection is apparent - as a travel
974 * connector - to the actor in the given origin location. This
975 * doesn't indicate whether or not travel is possible, or where
976 * travel goes, or that the actor can tell where the passage goes;
977 * this merely indicates whether or not the actor should realize
978 * that the passage exists at all.
979 *
980 * A closed door, for example, would return true, because even a
981 * closed door makes it clear that travel is possible in the
982 * direction, even if it's not possible currently. A secret door,
983 * on the other hand, would return nil while closed, because it
984 * would not be apparent to the actor that the object is a door at
985 * all.
986 */
987 isConnectorApparent(origin, actor)
988 {
989 /* by default, passages are apparent */
990 return true;
991 }
992
993 /*
994 * Determine if the travel connection is passable by the given
995 * traveler in the current state. For example, a door would return
996 * true when open, nil when closed.
997 *
998 * This information is intended to help game code probing the
999 * structure of the map. This information is NOT used in actor
1000 * travel; for actor travel, we rely on custom checks in the
1001 * connector's TravelVia handler to enforce the conditions of travel.
1002 * Actor travel uses TravelVia customizations rather than this method
1003 * because that allows better specificity in reporting failures.
1004 * This method lets game code get at the same information, but in a
1005 * more coarse-grained fashion.
1006 */
1007 isConnectorPassable(origin, traveler)
1008 {
1009 /* by default, we're passable */
1010 return true;
1011 }
1012
1013 /*
1014 * Get the apparent destination of travel by the actor to the given
1015 * origin. This returns the location to which the connector
1016 * travels, AS FAR AS THE ACTOR KNOWS. If the actor does not know
1017 * and cannot tell where the connector leads, this should return nil.
1018 *
1019 * Note that this method does NOT necessarily return the actual
1020 * destination, because we obviously can't know the destination for
1021 * certain until we traverse the connection. Rather, the point of
1022 * this routine is to return as much information as the actor is
1023 * supposed to have. This can be used for purposes like
1024 * auto-mapping, where we'd want to show what the player character
1025 * knows of the map, and NPC goal-seeking, where an NPC tries to
1026 * figure out how to get from one point to another based on the
1027 * NPC's knowledge of the map. In these sorts of applications, it's
1028 * important to use only knowledge that the actor is supposed to
1029 * have within the parameters of the simulation.
1030 *
1031 * Callers should always test isConnectorApparent() before calling
1032 * this routine. This routine does not check to ensure that the
1033 * connector is apparent, so it could return misleading information
1034 * if used independently of isConnectorApparent(); for example, if
1035 * the connector *formerly* worked but has now disappeared, and the
1036 * actor has a memory of the former destination, we'll return the
1037 * remembered destination.
1038 *
1039 * The actor can know the destination by a number of means:
1040 *
1041 * 1. The location is familiar to the character. For example, if
1042 * the setting is the character's own house, the character would
1043 * obviously know the house well, so would know where you'd end up
1044 * going east from the living room or south from the kitchen. We
1045 * use the origin method actorKnowsDestination() to determine this.
1046 *
1047 * 2. The destination is readily visible from the origin location,
1048 * or is clearly marked. For example, in an outdoor setting, it
1049 * might be clear that going east from the field takes you to the
1050 * hilltop. In an indoor setting, an open passage might make it
1051 * clear that going east from the living room takes you to the
1052 * dining room. We use the origin method actorKnowsDestination() to
1053 * determine this.
1054 *
1055 * 3. The actor has been through the connector already in the course
1056 * of the game, and so remembers the connection by virtue of recent
1057 * experience. If our travelMemory class property is set to a
1058 * non-nil lookup table object, then we'll automatically use the
1059 * lookup table to remember the destination each time an actor
1060 * travels via a connector, and use this information by default to
1061 * provide apparent destination information.
1062 */
1063 getApparentDestination(origin, actor)
1064 {
1065 local dest;
1066
1067 /*
1068 * Ask the origin if the actor knows the destination for the
1069 * given connector. If so, and we can determine our
1070 * destination, then return the destination.
1071 */
1072 if (origin.actorKnowsDestination(actor, self)
1073 && (dest = getDestination(origin,
1074 actor.getTraveler(self))) != nil)
1075 return dest;
1076
1077 /*
1078 * If we have a travelMemory table, look to see if the traversal
1079 * of this actor via this connector from this origin is recorded
1080 * in the table, and if so, assume that the destination is the
1081 * same as it was last time.
1082 *
1083 * Note that we ignore our memory of travel if we never saw the
1084 * destination of the travel (which would be the case if the
1085 * destination was dark every time we've been there, so we've
1086 * never seen any details about the location).
1087 */
1088 if (travelMemory != nil
1089 && (dest = travelMemory[[actor, origin, self]]) != nil
1090 && actor.hasSeen(dest))
1091 {
1092 /* we know the destination from past experience */
1093 return dest;
1094 }
1095
1096 /* we don't know the destination */
1097 return nil;
1098 }
1099
1100 /*
1101 * Get our destination, given the traveler and the origin location.
1102 *
1103 * This method is required to return the current destination for the
1104 * travel. If the connector doesn't go anywhere, this should return
1105 * nil. The results of this method must be stable for the extent of
1106 * a turn, up until the time travel actually occurs; in other words,
1107 * it must be possible to call this routine simply for information
1108 * purposes, to determine where the travel will end up.
1109 *
1110 * This method should not trigger any side effects, since it's
1111 * necessary to be able to call this method more than once in the
1112 * course of a given travel command. If it's necessary to trigger
1113 * side effects when the connector is actually traversed, apply the
1114 * side effects in noteTraversal().
1115 *
1116 * For auto-mapping and the like, note that getApparentDestination()
1117 * is a better choice, since this method has internal information
1118 * that might not be apparent to the characters in the game and thus
1119 * shouldn't be revealed through something like an auto-map. This
1120 * method is intended for internal use in the course of processing a
1121 * travel action, since it knows the true destination of the travel.
1122 */
1123 getDestination(origin, traveler) { return nil; }
1124
1125 /*
1126 * Get the travel connector leading to the given destination from the
1127 * given origin and for the given travel. Return nil if we don't
1128 * know a connector leading there.
1129 *
1130 * By default, we simply return 'self' if our destination is the
1131 * given destination, or nil if not.
1132 *
1133 * Some subclasses might encapsulate one or more "secondary"
1134 * connectors - that is, the main connector might choose among
1135 * multiple other connectors. In these cases, the secondary
1136 * connectors typically won't be linked to directions on their own,
1137 * so the room can't see them directly - it can only find them
1138 * through us, since we're effectively a wrapper for the secondary
1139 * connectors. In these cases, we won't have any single destination
1140 * ourself, so getDestination() will have to return nil. But we
1141 * *can* work backwards: given a destination, we can find the
1142 * secondary connector that points to that destination. That's what
1143 * this routine is for.
1144 */
1145 connectorGetConnectorTo(origin, traveler, dest)
1146 {
1147 /* if we go there, return 'self', else return nil */
1148 return (getDestination(origin, traveler) == dest ? self : nil);
1149 }
1150
1151 /*
1152 * Note that the connector is being traversed. This is invoked just
1153 * before the traveler is moved; this notification is fired after the
1154 * other travel-related notifications (beforeTravel, actorTravel,
1155 * travelerLeaving). This is a good place to display any special
1156 * messages describing what happens during the travel, because any
1157 * messages displayed here will come after any messages related to
1158 * reactions from other objects.
1159 */
1160 noteTraversal(traveler)
1161 {
1162 /* do nothing by default */
1163 }
1164
1165 /*
1166 * Service routine: add a memory of a successful traversal of a
1167 * travel connector. If we have a travel memory table, we'll add
1168 * the traversal to the table, so that we can find it later.
1169 *
1170 * This is called from Traveler.travelerTravelTo() on successful
1171 * travel. We're called for each actor participating in the travel.
1172 */
1173 rememberTravel(origin, actor, dest)
1174 {
1175 /*
1176 * If we have a travelMemory table, add this traversal. Store
1177 * the destination, keyed by the combination of the actor,
1178 * origin, and connector object (i.e., self) - this will allow
1179 * us to remember the destination we reached last time if we
1180 * want to know where the same route goes in the future.
1181 */
1182 if (TravelConnector.travelMemory != nil)
1183 TravelConnector.travelMemory[[actor, origin, self]] = dest;
1184 }
1185
1186 /*
1187 * Our "travel memory" table. If this contains a non-nil lookup
1188 * table object, we'll store a record of each successful traversal
1189 * of a travel connector here - we'll record the destination keyed
1190 * by the combination of actor, origin, and connector, so that we
1191 * can later check to see if the actor has any memory of where a
1192 * given connector goes from a given origin.
1193 *
1194 * We keep this information by default, which is why we statically
1195 * create the table here. Keeping this information does involve
1196 * some overhead, so some authors might want to get rid of this
1197 * table (by setting the property to nil) if the game doesn't make
1198 * any use of the information. Note that this table is stored just
1199 * once, in the TravelConnector class itself - there's not a
1200 * separate table per connector.
1201 */
1202 travelMemory = static new LookupTable(256, 512)
1203
1204 /*
1205 * Is this a "circular" passage? A circular passage is one that
1206 * explicitly connects back to its origin, so that traveling through
1207 * the connector leaves us where we started. When a passage is
1208 * marked as circular, we'll describe travel through the passage
1209 * exactly as though we had actually gone somewhere. By default, if
1210 * traveling through a passage leaves us where we started, we assume
1211 * that nothing happened, so we don't describe any travel.
1212 *
1213 * Circular passages don't often occur in ordinary settings; these
1214 * are mostly useful in disorienting environments, such as twisty
1215 * cave networks, where a passage between locations can change
1216 * direction and even loop back on itself.
1217 */
1218 isCircularPassage = nil
1219
1220 /*
1221 * Should we remember a circular trip through this passage? By
1222 * default, we remember the destination of a passage that takes us
1223 * back to our origin only if we're explicitly marked as a circular
1224 * passage; in other cases, we assume that the travel was blocked
1225 * somehow instead.
1226 */
1227 rememberCircularPassage = (isCircularPassage)
1228
1229 /*
1230 * Describe an actor's departure through the connector from the
1231 * given origin to the given destination. This description is from
1232 * the point of view of another actor in the origin location.
1233 */
1234 describeDeparture(traveler, origin, dest)
1235 {
1236 local dir;
1237
1238 /*
1239 * See if we can find a direction linked to this connector from
1240 * the origin location. If so, describe the departure using the
1241 * direction; otherwise, describe it using the generic departure
1242 * message.
1243 *
1244 * Find the connector from the player character's perspective,
1245 * because the description we're generating is for the player's
1246 * benefit and thus should be from the PC's perspective.
1247 */
1248 if ((dir = origin.directionForConnector(self, gPlayerChar)) != nil)
1249 {
1250 /*
1251 * We found a direction linked to the connector, so this
1252 * must have been the way they traveled. Describe the
1253 * departure as being in that direction.
1254 *
1255 * Note that it's possible that more than one direction is
1256 * linked to the same connector. In such cases, we'll just
1257 * take the first link we find, because it's equally
1258 * accurate to say that the actor went in any of the
1259 * directions linked to the same connector.
1260 */
1261 traveler.sayDepartingDir(dir, self);
1262 }
1263 else
1264 {
1265 /*
1266 * We didn't find any direction out of the origin linking to
1267 * this connector, so we don't know how what direction they
1268 * went. Show the generic departure message.
1269 */
1270 traveler.sayDeparting(self);
1271 }
1272 }
1273
1274 /*
1275 * Describe an actor's arrival through the connector from the given
1276 * origin into the given destination. This description is from the
1277 * point of view of another actor in the destination.
1278 *
1279 * Note that this is called on the connector that reverses the
1280 * travel, NOT on the connector the actor is actually traversing -
1281 * that is, 'self' is the backwards connector, leading from the
1282 * destination back to the origin location. So, if we have two
1283 * sides to a door, and the actor traverses the first side, this
1284 * will be called on the second side - the one that links the
1285 * destination back to the origin.
1286 */
1287 describeArrival(traveler, origin, dest)
1288 {
1289 local dir;
1290
1291 /*
1292 * See if we can find a direction linked to this connector in
1293 * the destination location. If so, describe the arrival using
1294 * the direction; otherwise, describe it using a generic arrival
1295 * message.
1296 *
1297 * Find the connector from the player character's perspective,
1298 * because the description we're generating is for the player's
1299 * benefit and thus should be from the PC's perspective.
1300 */
1301 if ((dir = dest.directionForConnector(self, gPlayerChar)) != nil)
1302 {
1303 /*
1304 * we found a direction linked to this back connector, so
1305 * describe the arrival as coming from the direction we
1306 * found
1307 */
1308 traveler.sayArrivingDir(dir, self);
1309 }
1310 else
1311 {
1312 /*
1313 * we didn't find any direction links, so use a generic
1314 * arrival message
1315 */
1316 traveler.sayArriving(self);
1317 }
1318 }
1319
1320 /*
1321 * Describe a "local departure" via this connector. This is called
1322 * when a traveler moves around entirely within the field of view of
1323 * the player character, and move *further away* from the PC - that
1324 * is, the traveler's destination is visible to the PC when we're
1325 * leaving our origin, AND the origin's top-level location contains
1326 * the PC. We'll describe the travel not in terms of truly
1327 * departing, but simply in terms of moving away.
1328 */
1329 describeLocalDeparture(traveler, origin, dest)
1330 {
1331 /* say that we're departing locally */
1332 traveler.sayDepartingLocally(dest, self);
1333 }
1334
1335 /*
1336 * Describe a "local arrival" via this connector. This is called
1337 * when the traveler moves around entirely within the field of view
1338 * of the player character, and comes *closer* to the PC - that is,
1339 * the traveler's origin is visible to the player character when we
1340 * arrive in our destination, AND the destination's top-level
1341 * location contains the PC. We'll describe the travel not in terms
1342 * of truly arriving, since the traveler was already here to start
1343 * with, but rather as entering the destination, but just in terms of
1344 * moving closer.
1345 */
1346 describeLocalArrival(traveler, origin, dest)
1347 {
1348 /* say that we're arriving locally */
1349 traveler.sayArrivingLocally(dest, self);
1350 }
1351
1352 /*
1353 * Describe "remote travel" via this connector. This is called when
1354 * the traveler moves around entirely within the field of view of the
1355 * PC, but between two "remote" top-level locations - "remote" means
1356 * "does not contain the PC." In this case, the traveler isn't
1357 * arriving or departing, exactly; it's just moving laterally from
1358 * one top-level location to another.
1359 */
1360 describeRemoteTravel(traveler, origin, dest)
1361 {
1362 /* say that we're traveling laterally */
1363 traveler.sayTravelingRemotely(dest, self);
1364 }
1365
1366 /*
1367 * Find a connector in the destination location that connects back as
1368 * the source of travel from the given connector when traversed from
1369 * the source location. Returns nil if there is no such connector.
1370 * This must be called while the traveler is still in the source
1371 * location; we'll attempt to find the connector back to the
1372 * traveler's current location.
1373 *
1374 * The purpose of this routine is to identify the connector by which
1375 * the traveler arrives in the new location. This can be used, for
1376 * example, to generate a connector-specific message describing the
1377 * traveler's emergence from the connector (so we can say one thing
1378 * if the traveler arrives via a door, and another if the traveler
1379 * arrives by climing up a ladder).
1380 *
1381 * By default, we'll try to find a travel link in the destination
1382 * that links us back to this same connector, in which case we'll
1383 * return 'self' as the connector from which the traveler emerges in
1384 * the new location. Failing that, we'll look for a travel link
1385 * whose apparent source is the origin location.
1386 *
1387 * This should be overridden for any connector with an explicit
1388 * complementary connector. For example, it is common to implement a
1389 * door using a pair of objects, one representing each side of the
1390 * door; in such cases, each door object would simply return its twin
1391 * here. Note that a complementary connector doesn't actually have
1392 * to go anywhere, since it's still useful to have a connector back
1393 * simply for describing travelers arriving on the connector.
1394 *
1395 * This *must* be overridden when the destination location doesn't
1396 * have a simple connector whose apparent source is this connector,
1397 * because in such cases we won't be able to find the reverse
1398 * connector with our direction search.
1399 */
1400 connectorBack(traveler, dest)
1401 {
1402 local origin;
1403
1404 /* if there's no destination, there's obviously no connector */
1405 if (dest == nil)
1406 return nil;
1407
1408 /*
1409 * get the origin location - this the traveler's current
1410 * immediate container
1411 */
1412 origin = traveler.location;
1413
1414 /*
1415 * First, try to find a link back to this same connector - this
1416 * will handle simple symmetrical links with the same connector
1417 * object shared between two rooms.
1418 *
1419 * We try to find the actual connector before looking for a
1420 * connector back to the origin - it's possible that there are
1421 * several ways back to the starting point, and we want to make
1422 * sure we pick the one that was actually traversed if possible.
1423 */
1424 foreach (local dir in Direction.allDirections)
1425 {
1426 /*
1427 * If this direction link from the destination is linked back
1428 * to this same connector, we have a symmetrical connection,
1429 * so we're the connector back. Note that we're interested
1430 * only in map structure here, so we don't pass an actor; the
1431 * actor isn't actually in the new location, so what the
1432 * actor can see is irrelevant to us here.
1433 */
1434 if (dest.getTravelConnector(dir, nil) == self)
1435 {
1436 /* the same connector goes in both directions */
1437 return self;
1438 }
1439 }
1440
1441 /*
1442 * we didn't find a link back to the same connector, so try to
1443 * find a link from the destination whose apparent source is the
1444 * origin
1445 */
1446 foreach (local dir in Direction.allDirections)
1447 {
1448 local conn;
1449
1450 /*
1451 * if this link from the destination has an apparent source
1452 * of our origin, the traveler appears to be arriving from
1453 * this link
1454 */
1455 if ((conn = dest.getTravelConnector(dir, nil)) != nil
1456 && conn.fixedSource(dest, traveler) == origin)
1457 {
1458 /*
1459 * this direction has an apparent source of the origin
1460 * room - it's not necessarily the same link they
1461 * traversed, but at least it appears to come from the
1462 * same place they came from, so it'll have to do
1463 */
1464 return conn;
1465 }
1466 }
1467
1468 /* we couldn't find any link back to the origin */
1469 return nil;
1470 }
1471
1472 /*
1473 * Get the "fixed" source for travelers emerging from this connector,
1474 * if possible. This can return nil if the connector does not have a
1475 * fixed relationship with another connector.
1476 *
1477 * The purpose of this routine is to find complementary connectors
1478 * for simple static map connections. This is especially useful for
1479 * direct room-to-room connections.
1480 *
1481 * When a connector relationship other than a simple static mapping
1482 * exists, the connectors must generally override connectorBack(), in
1483 * which case this routine will not be needed (at least, this routine
1484 * won't be needed as long as the overridden connectorBack() doesn't
1485 * call it). Whenever it is not clear how to implement this routine,
1486 * don't - implement connectorBack() instead.
1487 */
1488 fixedSource(dest, traveler)
1489 {
1490 /* by default, return nothing */
1491 return nil;
1492 }
1493
1494 /*
1495 * Can the given actor see this connector in the dark, looking from
1496 * the given origin? Returns true if so, nil if not.
1497 *
1498 * This is used to determine if the actor can travel from the given
1499 * origin via this connector when the actor (in the origin location)
1500 * is in darkness.
1501 *
1502 * By default, we implement the usual convention, which is that
1503 * travel from a dark room is possible only when the destination is
1504 * lit. If we can't determine our destination, we will assume that
1505 * the connector is not visible.
1506 */
1507 isConnectorVisibleInDark(origin, actor)
1508 {
1509 local dest;
1510
1511 /*
1512 * Get my destination - if we can't determine our destination,
1513 * then assume we're not visible.
1514 */
1515 if ((dest = getDestination(origin, actor.getTraveler(self))) == nil)
1516 return nil;
1517
1518 /*
1519 * Check the ambient illumination level in the destination. If
1520 * it's 2 or higher, then it's lit; otherwise, it's dark. If
1521 * the destination is lit, consider the connector to be visible,
1522 * on the theory that the connector lets a little bit of the
1523 * light from the destination leak into the origin room - just
1524 * enough to make the connection itself visible without making
1525 * anything else in the origin room visible.
1526 */
1527 return (dest.wouldBeLitFor(actor));
1528 }
1529
1530 /*
1531 * Handle travel in the dark. Specifically, this is called when an
1532 * actor attempts travel from one dark location to another dark
1533 * location. (We don't invoke this in any other case:
1534 * light-to-light, light-to-dark, and dark-to-light travel are all
1535 * allowed without any special checks.)
1536 *
1537 * By default, we will prohibit dark-to-dark travel by calling the
1538 * location's darkTravel handler. Individual connectors can
1539 * override this to allow such travel or apply different handling.
1540 */
1541 darkTravel(actor, dest)
1542 {
1543 /*
1544 * by default, simply call the actor's location's darkTravel
1545 * handler
1546 */
1547 actor.location.roomDarkTravel(actor);
1548 }
1549
1550 /*
1551 * Action handler for the internal "TravelVia" action. This is not a
1552 * real action, but is instead a pseudo-action that we implement
1553 * generically for travel via the connector. Subclasses that want to
1554 * handle real actions by traveling via the connector can use
1555 * remapTo(TravelVia) to implement the real action handlers. Note
1556 * that remapTo should be used (rather than, say, asDobjFor), since
1557 * this will ensure that every type of travel through the connector
1558 * actually looks like a TravelVia action, which is useful for
1559 * intercepting travel actions generically in other code.
1560 */
1561 dobjFor(TravelVia)
1562 {
1563 preCond()
1564 {
1565 /*
1566 * For our preconditions, use the traveler's preconditions,
1567 * plus the location's preconditions, plus any special
1568 * connector-specific preconditions we supply.
1569 */
1570 return gActor.getTraveler(self).travelerPreCond(self)
1571 + gActor.location.roomTravelPreCond()
1572 + connectorTravelPreCond();
1573 }
1574 verify()
1575 {
1576 /*
1577 * Verify travel for the current command's actor through this
1578 * connector. This performs normal action verify processing.
1579 *
1580 * The main purpose of this routine is to allow the connector
1581 * to flag obviously dangerous travel to allow a caller to
1582 * avoid such travel as an implicit or scripted action. In
1583 * most cases, there's no need to make travel illogical
1584 * because there's generally no potential ambiguity involved
1585 * in analyzing a travel verb.
1586 *
1587 * Note that this routine must check with the actor to
1588 * determine if the actor or a vehicle will actually be
1589 * performing the travel, by calling gActor.getTraveler(), if
1590 * the routine cares about the difference.
1591 */
1592 }
1593 check()
1594 {
1595 local t = gActor.getTraveler(self);
1596 local dest;
1597
1598 /*
1599 * Check the travel.
1600 *
1601 * This routine should take into account the light levels at
1602 * the source and destination locations, if travel between
1603 * dark rooms is to be disallowed.
1604 */
1605
1606 /* get my destination */
1607 dest = getDestination(t.location, t);
1608
1609 /* check dark-to-dark travel */
1610 gActor.checkDarkTravel(dest, self);
1611
1612 /* enforce barriers */
1613 checkTravelBarriers(dest);
1614 }
1615
1616 action()
1617 {
1618 local t = gActor.getTraveler(self);
1619 local dest;
1620
1621 /*
1622 * Execute the travel, moving the command's actor through the
1623 * travel connection: we carry out any side effects of the
1624 * travel and deliver the actor (if appropriate) to the
1625 * destination of the connector.
1626 *
1627 * Note that this routine must check with the actor to
1628 * determine if the actor or a vehicle will actually be
1629 * performing the travel, by calling gActor.getTraveler(), if
1630 * the routine cares about the difference. In most cases,
1631 * the routine won't care: most implementations of this
1632 * routine will (if they effect any travel at all) eventually
1633 * call gActor.travelTo() to carry out the travel, and that
1634 * routine will always route the actual movement to the
1635 * vehicle if necessary.
1636 */
1637
1638 /* get my destination */
1639 dest = getDestination(t.location, t);
1640
1641 /* travel to my destination */
1642 gActor.travelTo(dest, self, connectorBack(t, dest));
1643 }
1644 }
1645 ;
1646
1647 /*
1648 * A "probe" object, for testing light levels in rooms. This is a dummy
1649 * object that we use for what-if testing - it's not actually part of
1650 * the simulation.
1651 */
1652 lightProbe: Thing;
1653
1654 /*
1655 * A TravelBarrier can be attached to a TravelConnector, via the
1656 * travelBarrier property, to form a conditional barrier to travel.
1657 */
1658 class TravelBarrier: object
1659 /*
1660 * Determine if this barrier blocks the given traveler. By default,
1661 * we don't block anyone. This doesn't make us much of a barrier, so
1662 * subclasses should override this with a more specific condition.
1663 */
1664 canTravelerPass(traveler) { return true; }
1665
1666 /*
1667 * Explain why travel isn't allowed. This should generate an
1668 * appropriate failure report explaining the problem. This is
1669 * invoked when travel is attempted and canTravelerPass returns nil.
1670 * Subclasses must override this.
1671 */
1672 explainTravelBarrier(traveler) { }
1673 ;
1674
1675 /*
1676 * An "unlisted proxy" connector acts as a proxy for another connector.
1677 * We act exactly like the underlying connector, except that we suppress
1678 * the connector from automatic exit lists. This can be used for cases
1679 * where an otherwise normal connector is needed but the connector is
1680 * not to appear in automatic exit lists (such as the status line).
1681 *
1682 * The most common situation where this kind of connector is useful is
1683 * where multiple directions in a given room all go to the same
1684 * destination. In these cases, it's often desirable for some of the
1685 * directions to be unlisted alternatives. The asExit() macro can be
1686 * used for convenience to set up these direction synonyms.
1687 */
1688 class UnlistedProxyConnector: object
1689 construct(pri)
1690 {
1691 /* remember my underlying primary connector */
1692 primaryConn = pri;
1693 }
1694
1695 /*
1696 * Our underlying connector. Start out with a default TadsObject
1697 * rather than nil in case anyone wants to call a property or test
1698 * inheritance before we're finished with our constructor - this will
1699 * produce reasonable default behavior without having to test for nil
1700 * everywhere.
1701 */
1702 primaryConn = TadsObject
1703
1704 /* we're not listed */
1705 isConnectorListed = nil
1706
1707 /* map any TravelVia action to our underlying connector */
1708 dobjFor(TravelVia) remapTo(TravelVia, primaryConn)
1709
1710 /* redirect everything we don't handle to the underlying connector */
1711 propNotDefined(prop, [args]) { return primaryConn.(prop)(args...); }
1712
1713 /*
1714 * As a proxy, we don't want to disguise the fact that we're a proxy,
1715 * if someone specifically asks, so admist to being of our own true
1716 * kind; but we also act mostly like our underlying connector, so if
1717 * someone wants to know if we're one of those, say yes to that as
1718 * well. So, return true if the inherited version returns true, and
1719 * also return true if our primary connector would return true.
1720 */
1721 ofKind(cls) { return inherited(cls) || primaryConn.ofKind(cls); }
1722 ;
1723
1724
1725 /*
1726 * A travel connector that doesn't allow any travel - if travel is
1727 * attempted, we simply use the origin's cannotTravel method to display
1728 * an appropriate message.
1729 */
1730 noTravel: TravelConnector
1731 /* it is obvious that is no passage this way */
1732 isConnectorApparent(origin, actor) { return nil; }
1733
1734 /* this is not a passable connector */
1735 isConnectorPassable(origin, traveler) { return nil; }
1736
1737 dobjFor(TravelVia)
1738 {
1739 /*
1740 * we know that no travel will occur, so we don't need to satisfy
1741 * any preconditions
1742 */
1743 preCond = []
1744
1745 action()
1746 {
1747 /* we can't go this way - use the origin's default message */
1748 gActor.location.cannotTravel();
1749 }
1750 }
1751 ;
1752
1753 /*
1754 * An "ask which" travel connector. Rather than just traversing a
1755 * connector, we ask for a direct object for a specified travel verb; if
1756 * the player supplies the missing indirect object (or if the parser can
1757 * automatically choose a default), we'll perform the travel verb using
1758 * that direct object.
1759 *
1760 * This type of connector has two uses.
1761 *
1762 * First, the library various instances, with appropriate specified
1763 * travel verbs, as the default connector for certain directions that
1764 * frequently end up mapping to in-scenario objects. Specifically,
1765 * noTravelIn, noTravelDown, and noTravelOut are used as the default in,
1766 * down, and out connectors. If the player types DOWN, for example, and
1767 * there's no override for 'down' in a given room, then we'll invoke
1768 * noTravelDown; this will in turn ask for a missing direct object for
1769 * the GetOffOf action, since DOWN can mean getting off of a platform or
1770 * other nested room when on such a thing. When there's an obvious
1771 * thing to get down from, the parser will provide the default
1772 * automatically, which will make DOWN into a simple synonym for GET OFF
1773 * OF <whatever>.
1774 *
1775 * Second, games can use this kind of connector for a given direction
1776 * when the direction is ambiguous. For example, you can use this as
1777 * the 'north' connector when there are two doors leading north from the
1778 * location. When the player types NORTH, the parser will ask which
1779 * door the player wants to go through.
1780 */
1781 class AskConnector: TravelConnector, ResolveAsker
1782 /*
1783 * The specific travel action to attempt. This must be a TAction -
1784 * an action that takes a direct object (and only a direct object).
1785 * The default is TravelVia, but this should usually be customized
1786 * in each instance to the type of travel appropriate for the
1787 * possible connectors.
1788 */
1789 travelAction = TravelViaAction
1790
1791 /*
1792 * The list of possible direct objects for the travel action. If
1793 * this is nil, we'll simply treat the direct object of the
1794 * travelAction as completely missing, forcing the parser to either
1795 * find a default or ask the player for the missing object. If the
1796 * travel is limited to a specific set of objects (for example, if
1797 * there are two doors leading north, and we want to ask which one
1798 * to use), this should be set to the list of possible objects; the
1799 * parser will then use the ambiguous noun phrase rules instead of
1800 * the missing noun phrase rules to ask the player for more
1801 * information.
1802 */
1803 travelObjs = nil
1804
1805 /*
1806 * The phrase to use in the disambiguation question to ask which of
1807 * the travelObjs entries is to be used. The language-specific
1808 * module provides a suitable default, but this should usually be
1809 * overridden if travelObjs is overridden.
1810 */
1811 travelObjsPhrase = nil
1812
1813 /*
1814 * An extra prompt message to show before the normal parser prompt
1815 * for a missing or ambiguous object. We'll show this just before
1816 * the normal parser message, if it's specified.
1817 *
1818 * If you want to customize the messages more completely, you can
1819 * override askDisambig() or askMissingObject(). The parser will
1820 * invoke these to generate the prompt, so you can customize the
1821 * entire messages by overriding these.
1822 */
1823 promptMessage = nil
1824
1825 /*
1826 * For each of the ResolveAsker methods that might be invoked, add
1827 * the promptMessage text before the normal parser question.
1828 */
1829 askDisambig(targetActor, promptTxt, curMatchList, fullMatchList,
1830 requiredNum, askingAgain, dist)
1831 {
1832 promptMessage;
1833 inherited(targetActor, promptTxt, curMatchList, fullMatchList,
1834 requiredNum, askingAgain, dist);
1835 }
1836
1837 askMissingObject(targetActor, action, which)
1838 {
1839 promptMessage;
1840 inherited(targetActor, action, which);
1841 }
1842
1843 /* handle travel via this connector */
1844 dobjFor(TravelVia)
1845 {
1846 /*
1847 * No preconditions or checks are necessary, since we don't
1848 * actually perform any travel on our own; we simply recast the
1849 * command as a new action, hence we want to delegate the
1850 * preconditions and check() handling to the replacement action.
1851 * Note that this means that you can't put a travel barrier
1852 * directly on an AskConnector - you have to put any barriers on
1853 * the underlying real connectors instead.
1854 */
1855 preCond = []
1856 check() { }
1857
1858 /*
1859 * Recast the travel into our specified action, asking for the
1860 * direct object we need for that action.
1861 */
1862 action()
1863 {
1864 /*
1865 * if we have a set of possible direct objects, retry this
1866 * with the ambiguous object set; otherwise, retry with a
1867 * completely missing direct object
1868 */
1869 if (travelObjs != nil)
1870 travelAction.retryWithAmbiguousDobj(
1871 gAction, travelObjs, self, travelObjsPhrase);
1872 else
1873 travelAction.retryWithMissingDobj(gAction, self);
1874 }
1875 }
1876
1877 /*
1878 * Get a connector leading to the given destination. We'll scan our
1879 * travel objects; for each one that's a TravelConnector, we'll ask
1880 * it to find the connector, and return the result if we get one.
1881 */
1882 connectorGetConnectorTo(origin, traveler, dest)
1883 {
1884 /* if we have no travel objects, there's nothing to check */
1885 if (travelObjs == nil)
1886 return nil;
1887
1888 /* scan our secondary connectors */
1889 foreach (local cur in travelObjs)
1890 {
1891 /* if this is a travel connector, ask it what it thinks */
1892 if (cur.ofKind(TravelConnector))
1893 {
1894 local conn;
1895
1896 /*
1897 * if this secondary connector can give us a connector to
1898 * the destination, use that connector
1899 */
1900 conn = cur.connectorGetConnectorTo(origin, traveler, dest);
1901 if (conn != nil)
1902 return conn;
1903 }
1904 }
1905
1906 /* didn't find a match */
1907 return nil;
1908 }
1909 ;
1910
1911 /*
1912 * A "default ask connector" is an AskConnector that we use for certain
1913 * directions (down, in, out) as the library default connector for the
1914 * directions.
1915 */
1916 class DefaultAskConnector: AskConnector
1917 /*
1918 * since this is a default connector for all locations, indicate that
1919 * no travel is apparently possible in this direction
1920 */
1921 isConnectorApparent(origin, actor) { return nil; }
1922
1923 /* this is not a passable connector */
1924 isConnectorPassable(origin, traveler) { return nil; }
1925 ;
1926
1927 /*
1928 * A default travel connector for going in. When travel in the relative
1929 * direction "in" isn't allowed, we'll try recasting the command as an
1930 * "enter" command with an unknown direct object.
1931 */
1932 noTravelIn: DefaultAskConnector
1933 /* when we go 'in', we'll try to ENTER something */
1934 travelAction = EnterAction
1935 ;
1936
1937 /*
1938 * A default travel connector for going out. When travel in the
1939 * relative direction "out" isn't allowed, we'll try recasting the
1940 * command as an "get out of" command with an unknown direct object.
1941 */
1942 noTravelOut: DefaultAskConnector
1943 /* when we go 'out', we'll try to GET OUT OF something */
1944 travelAction = GetOutOfAction
1945 ;
1946
1947 /*
1948 * A default travel connector for going out from a nested room. This
1949 * works the same way as noTravelOut, except that we'll show OUT as a
1950 * listed exit.
1951 */
1952 nestedRoomOut: noTravelOut
1953 isConnectorApparent(origin, actor) { return true; }
1954 ;
1955
1956 /*
1957 * A special travel connector for 'down' that recasts the command as a
1958 * "get off of" command. This can be used for platforms and the like,
1959 * where a 'down' command should usually be taken to mean "get off
1960 * platform" rather than "down from enclosing room".
1961 */
1962 noTravelDown: DefaultAskConnector
1963 /* when we go 'down', we'll try to GET OFF OF something */
1964 travelAction = GetOffOfAction
1965 ;
1966
1967 /*
1968 * A travel connector for going in that explicitly redirects the command
1969 * to "enter" and asks for the missing direct object. This behaves the
1970 * same way as noTravelIn, but explicitly makes the inward travel
1971 * apparent; this can be used to override the noTravelIn default for
1972 * locations where travel in is explicitly allowed.
1973 */
1974 askTravelIn: AskConnector
1975 travelAction = EnterAction
1976 ;
1977
1978 /* explicitly redirect travel out to "get out of" */
1979 askTravelOut: AskConnector
1980 travelAction = GetOutOfAction
1981 ;
1982
1983 /* explicitly redirect travel down to "get off of" */
1984 askTravelDown: AskConnector
1985 travelAction = GetOffOfAction
1986 ;
1987
1988 /*
1989 * "No Shipboard Directions" travel connector. This is used as the
1990 * default connector for the shipboard directions for the base Room
1991 * class. This connector displays a special message indicating that the
1992 * room is not a boat hence the shipboard directions don't work here.
1993 */
1994 noShipTravel: noTravel
1995 dobjFor(TravelVia)
1996 {
1997 action()
1998 {
1999 /* simply indicate that this direction isn't applicable here */
2000 gLibMessages.notOnboardShip();
2001 }
2002 }
2003 ;
2004
2005 /* ------------------------------------------------------------------------ */
2006 /*
2007 * A mix-in class that can be added to objects that also inherit from
2008 * TravelConnector to add a message as the connector is traversed.
2009 *
2010 * Note that this isn't itself a travel connector; it's just a class
2011 * that should be combined with TravelConnector or one of its
2012 * subclasses. This class should be in the superclass list before the
2013 * TravelConnector-derived superclass.
2014 */
2015 class TravelWithMessage: object
2016 /*
2017 * My message to display when the player character traverses the
2018 * connector. This should be overridden with the custom message for
2019 * the connector. By default, if we're a Script, we'll invoke the
2020 * script to show the next message.
2021 */
2022 travelDesc()
2023 {
2024 if (ofKind(Script))
2025 doScript();
2026 }
2027
2028 /*
2029 * My message to display when any non-player character traverses the
2030 * connector. If this is not overridden, no message will be
2031 * displayed when an NPC travels through the connector.
2032 */
2033 npcTravelDesc = ""
2034
2035 /*
2036 * Display my message. By default, we show one message for the
2037 * player character and another message for NPC's.
2038 */
2039 showTravelDesc()
2040 {
2041 if (gActor.isPlayerChar())
2042 travelDesc;
2043 else
2044 npcTravelDesc;
2045 }
2046
2047 /* on traversing the connector, show our message */
2048 noteTraversal(traveler)
2049 {
2050 /* display my message */
2051 showTravelDesc();
2052
2053 /* inherit any other superclass handling as well */
2054 inherited(traveler);
2055 }
2056 ;
2057
2058 /*
2059 * A simple connector that displays a message when the connector is
2060 * traversed.
2061 */
2062 class TravelMessage: TravelWithMessage, TravelConnector
2063 /* my destination location */
2064 destination = nil
2065
2066 /* get my destination */
2067 getDestination(origin, traveler) { return destination; }
2068
2069 /* our source is the same as our destination */
2070 fixedSource(dest, traveler) { return destination; }
2071 ;
2072
2073 /*
2074 * A travel connector that can't be traversed, and which shows a custom
2075 * failure message when traversal is attempted. Instances should define
2076 * travelDesc to the message to display when travel is attempted.
2077 *
2078 * Travel is not apparently possible in this direction, so this type of
2079 * connector will not show up in automatic exit lists or maps. This
2080 * class is designed for connections that are essentially the same as no
2081 * connection at all, but where it's desirable to use a special message
2082 * to describe why travel can't be accomplished.
2083 */
2084 class NoTravelMessage: TravelMessage
2085 dobjFor(TravelVia)
2086 {
2087 /* as in noTravel, we need no preconditions or checks */
2088 preCond = []
2089 action()
2090 {
2091 /* simply show my message - that's all we do */
2092 showTravelDesc();
2093 }
2094 }
2095
2096 /*
2097 * Because no travel is possible, we want a non-empty message for
2098 * NPC's as well as for the PC; by default, use the same message for
2099 * all actors by using travelDesc for NPC's.
2100 */
2101 npcTravelDesc = (travelDesc)
2102
2103 /* travel is not apparently possible in this direction */
2104 isConnectorApparent(origin, actor) { return nil; }
2105 isConnectorPassable(origin, traveler) { return nil; }
2106 ;
2107
2108 /*
2109 * A "fake" connector. This is a connector that doesn't actually allow
2110 * travel, but acts like it *could*. We simply show a special message
2111 * when travel is attempted, but we don't move the actor.
2112 *
2113 * This is a subclass of NoTravelMessage, so instances should customize
2114 * travelDes with the special failure message when travel is attempted.
2115 *
2116 * Note that this type of connector is by default *apparently* an exit,
2117 * even though it doesn't actually go anywhere. This is useful for "soft
2118 * boundaries," where the game's map is meant to appear to the player to
2119 * continue but beyond which no more locations actually exist in the map.
2120 * This is an oft-used device meant to create an illusion that the game's
2121 * map exists in a larger world even though the larger world is not
2122 * modeled. The message we display should in such cases attribute the
2123 * actor's inability to traverse the connector to a suitable constraint
2124 * within the context of the game world; for example, the actor could be
2125 * said to be unwilling to go beyond this point because the actor knows
2126 * or suspects there's nothing for a long way in this direction, or
2127 * because the actor's goals require staying within the modeled map, or
2128 * because the actor is afraid of what lies beyond.
2129 *
2130 * Note that FakeConnector should only be used when the reason for
2131 * blocking the travel is apparent before we even try travel. In
2132 * particular, FakeConnector is good for motivational reasons not to
2133 * travel, where the actor decides for reasons of its own not to even
2134 * attempt the travel. It's especially important not to use
2135 * FakeConnector in cases where the travel is described as attempted but
2136 * aborted halfway in - things like encountering a blocked tunnel. This
2137 * is important because FakeConnector aborts the travel immediately,
2138 * before sending out any of the notifications that would accompany
2139 * ordinary travel, which means that physical barriers (trolls blocking
2140 * the way, being tied to a chair) that would otherwise block ordinary
2141 * travel will be bypassed. For cases where travel is attempted, but
2142 * something turns you back halfway in, use DeadEndConnector.
2143 */
2144 class FakeConnector: NoTravelMessage
2145 /*
2146 * travel is *apparently* possible in this direction (even though
2147 * it's not *actually* possible)
2148 */
2149 isConnectorApparent(origin, actor) { return true; }
2150 isConnectorPassable(origin, traveler) { return nil; }
2151 ;
2152
2153 /* ------------------------------------------------------------------------ */
2154 /*
2155 * A Dead End Connector is a connector that appears to lead somewhere,
2156 * but which turns out to be impassable for reasons that aren't apparent
2157 * until we get some distance into the passage.
2158 *
2159 * The Dead End Connector might look a lot like the Fake Connector, but
2160 * there's an important difference. A Fake Connector is a connector that
2161 * can't even be physically attempted: the reason not to take a fake
2162 * connector is something that shows up before we even start moving,
2163 * usually a motivational reason ("You really can't leave town until you
2164 * find your missing brother"). A Dead End Connector, on the other hand,
2165 * is meant to model a physical attempt to travel that's blocked by some
2166 * problem halfway along, such as travel down a tunnel that turns out to
2167 * have caved in.
2168 */
2169 class DeadEndConnector: TravelMessage
2170 /*
2171 * The apparent destination name. If the actor is meant to know the
2172 * apparent destination from the outset, or if traversing the
2173 * connector gives the actor an idea of where the connector
2174 * supposedly goes, this can be used to give the name of that
2175 * destination. This name will show up in exit listings, for
2176 * example, once the PC knows where the connector supposedly goes.
2177 *
2178 * Note that this isn't the actual destination of the connector,
2179 * since the actual destination is simply back to the origin (that's
2180 * the whole point of the dead end, after all). This is simply where
2181 * the connector *appears* to go. If an attempted traversal doesn't
2182 * even reveal that much, then you should just leave this nil, since
2183 * the destination will never become apparent to the PC.
2184 */
2185 apparentDestName = nil
2186
2187 /*
2188 * Our apparent destination. By default, we create a FakeDestination
2189 * object to represent our apparent destination if we have a non-nil
2190 * name for the apparent destination.
2191 *
2192 * If the supposed-but-unreachable destination of the connector is in
2193 * fact a real location in the game, you can override this to point
2194 * directly to that actual location. This default is for the typical
2195 * case where the supposed destination doesn't actually exist on the
2196 * game map as a real room.
2197 */
2198 apparentDest()
2199 {
2200 /*
2201 * if we have an apparent destination name, create a
2202 * FakeDestination to represent the apparent destination, and
2203 * plug that in as our apparent destination for future reference
2204 */
2205 if (apparentDestName != nil)
2206 {
2207 local fake;
2208
2209 /* create a fake destination */
2210 fake = new FakeDestination(self);
2211
2212 /* plug it in as our new apparentDest for future calls */
2213 apparentDest = fake;
2214
2215 /* return the new object as the result */
2216 return fake;
2217 }
2218
2219 /* our apparent destination is unknown */
2220 return nil;
2221 }
2222
2223 /* get our apparent destination */
2224 getApparentDestination(origin, actor)
2225 {
2226 /*
2227 * If the actor knows the destination for the given connector (as
2228 * determined by the origin room), or we have a memory of this
2229 * traversal, return our fake destination object to represent the
2230 * destination. We can only return our fake destination if we
2231 * have an explicit apparent destination, since it's only in this
2232 * case that our apparent destination ever actually becomes
2233 * known, even after an attempted traversal.
2234 *
2235 * Our actual destination is always just the origin, but we
2236 * *appear* to have some other destination. Even though we can
2237 * never actually reach that other, apparent destination, we at
2238 * least want to give the appearance of going there, which we
2239 * provide through our fake destination object.
2240 */
2241 if (apparentDest != nil
2242 && (origin.actorKnowsDestination(actor, self)
2243 || travelMemory[[actor, origin, self]] != nil))
2244 return apparentDest;
2245
2246 /* our unreachable destination is not apparent */
2247 return nil;
2248 }
2249
2250 /* there's no corresponding connector back for a dead end */
2251 connectorBack(traveler, dest) { return nil; }
2252
2253 /* our actual destination is always our origin */
2254 getDestination(origin, traveler) { return origin; }
2255
2256 /* do remember circular trips, since that's the only kind we make */
2257 rememberCircularPassage = true
2258 ;
2259
2260 /*
2261 * A fake apparent destination, for dead-end connectors. The dead-end
2262 * connector will create an object of this class to represent its
2263 * apparent but actually unreachable destination, if it has an apparent
2264 * destination name.
2265 */
2266 class FakeDestination: object
2267 /* construct - remember our associated connector */
2268 construct(conn) { connector = conn; }
2269
2270 /* get our destination name - this is the name from our connector */
2271 getDestName(actor, origin) { return connector.apparentDestName; }
2272
2273 /* our underlying connector (usually a DeadEndConnector) */
2274 connector = nil
2275 ;
2276
2277 /* ------------------------------------------------------------------------ */
2278 /*
2279 * A direct room-to-room connector. In most cases, it is not necessary
2280 * to create one of these objects, because rooms can serve as their own
2281 * connectors. These objects are needed in certain cases, though, such
2282 * as when a room-to-room connection requires a travel barrier.
2283 */
2284 class RoomConnector: TravelConnector
2285 /* the two rooms we link */
2286 room1 = nil
2287 room2 = nil
2288
2289 /*
2290 * get the destination, given the origin: this is the one of the two
2291 * rooms we link that we're not in now
2292 */
2293 getDestination(origin, traveler)
2294 {
2295 if (origin == room1)
2296 return room2;
2297 else if (origin == room2)
2298 return room1;
2299 else
2300 return nil;
2301 }
2302
2303 fixedSource(origin, traveler)
2304 {
2305 /*
2306 * we're a symmetrical two-way connector, so our source from the
2307 * perspective of one room is the same as the destination from
2308 * that same perspective
2309 */
2310 return getDestination(origin, traveler);
2311 }
2312
2313 /*
2314 * Get the precondition for this connector. The normal
2315 * TravelConnector rule that the traveler must be in the outbound
2316 * connector's location is not meaningful for an abstract room
2317 * connector, because this type of connection itself isn't
2318 * represented as a physical game object with a location; it's just
2319 * an abstract data structure. This means that we must have a
2320 * directional property in the *source* location that points directly
2321 * to the destination (i.e., self).
2322 *
2323 * So, the appropriate starting point for room connectors is the
2324 * object that contains the connection. In other words, we must
2325 * search for the nearest object enclosing the *traveler* that has a
2326 * direction property directly linked to 'self'; that enclosing
2327 * container is the required starting location for the travel.
2328 */
2329 connectorTravelPreCond()
2330 {
2331 /*
2332 * Scan upwards from the traveler's location, looking for an
2333 * object that has a directional property linked to self. Only
2334 * scan into locations that the actor can see.
2335 */
2336 for (local loc = gActor.getTraveler(self).location ;
2337 loc != nil && gActor.canSee(loc) ; loc = loc.location)
2338 {
2339 /* look for a directional connector directly from 'loc' to us */
2340 if (loc.localDirectionLinkForConnector(self) != nil)
2341 {
2342 /*
2343 * we're linked from this enclosing location, so this is
2344 * where we have to be before travel
2345 */
2346 return [new TravelerDirectlyInRoom(gActor, self, loc)];
2347 }
2348 }
2349
2350 /* we couldn't find a link, so apply no condition */
2351 return [];
2352 }
2353 ;
2354
2355 /*
2356 * A one-way room connector. This works like an ordinary room
2357 * connector, but connects only in one direction. To use this class,
2358 * simply define the 'destination' property to point to the room we
2359 * connect to.
2360 */
2361 class OneWayRoomConnector: RoomConnector
2362 /* my destination - instances must define this */
2363 destination = nil
2364
2365 /* we always have a fixed destination */
2366 getDestination(origin, traveler) { return destination; }
2367 ;
2368
2369 /*
2370 * A room "auto-connector". This is a special subclass of RoomConnector
2371 * that can be mixed in to any BasicLocation subclass to make the room
2372 * usable as the direct target of a directional property in another room
2373 * - so you could say "east = myRoom", for example, without creating any
2374 * intermediate connector.
2375 */
2376 class RoomAutoConnector: RoomConnector
2377 /*
2378 * Suppose that roomA.north = roomB. This means that if an actor is
2379 * in roomA, and executes a "north" command, we'll execute a
2380 * TravelVia action on room B, because the "connector" will be
2381 * roomB. In these cases, the destination of the travel and the
2382 * travel connector are one and the same. So, when the connector is
2383 * roomB, the destination of travel is also simply roomB.
2384 */
2385 getDestination(origin, traveler)
2386 {
2387 /* we are our own destination */
2388 return self;
2389 }
2390 ;
2391
2392 /* ------------------------------------------------------------------------ */
2393 /*
2394 * Base class for passages between rooms. This can be used for a
2395 * passage that not only connects the rooms but also exists as an object
2396 * in its own right within the rooms it connects.
2397 *
2398 * In most cases, two passage objects will exist - one on each side of
2399 * the passage, so one object in each room connected. One of the
2400 * objects should be designated as the "master"; the other is the
2401 * "slave." The master object is the one which should implement all
2402 * special behavior involving state changes (such as opening or closing
2403 * a door).
2404 *
2405 * This basic passage is not designed to be opened and closed; use Door
2406 * for a passage that can be opened and closed.
2407 */
2408 class Passage: Linkable, Fixture, TravelConnector
2409 /*
2410 * Our destination - this is where the actor ends up when traveling
2411 * through the passage (assuming the passage is open). By default,
2412 * we return the "room location" of our other side's container; in
2413 * cases where our other side is not directly in our destination
2414 * room (for example, the other side is part of some larger object
2415 * structure), this property should be overridden to specify the
2416 * actual destination.
2417 *
2418 * If our otherSide is nil, the passage doesn't go anywhere. This
2419 * can be useful to create things that look and act like passages,
2420 * but which don't actually go anywhere - in other words,
2421 * passage-like decorations.
2422 */
2423 destination = (otherSide != nil ? otherSide.location.roomLocation : nil)
2424
2425 /* get my destination - just return my 'destination' property */
2426 getDestination(origin, traveler) { return destination; }
2427
2428 /* get our open/closed status */
2429 isOpen()
2430 {
2431 /*
2432 * if we have a separate master object, defer to it; otherwise,
2433 * use our own status
2434 */
2435 return (masterObject == self ? isOpen_ : masterObject.isOpen);
2436 }
2437
2438 /* internal open/closed status - open by default */
2439 isOpen_ = true
2440
2441 /*
2442 * We're not visible in the dark if we're closed. If we're open,
2443 * the normal rules apply.
2444 *
2445 * Normally, a passage is visible in the dark if there's light in
2446 * the adjoining location: our model is that enough light is leaking
2447 * in through the passage to make the passage itself visible, but
2448 * not enough to light anything else in the current room. In the
2449 * case of a closed passage, though, we assume that it completely
2450 * blocks any light from the other room, eliminating any indication
2451 * of a passage.
2452 *
2453 * If you do want an openable passage to be visible in the dark even
2454 * when it's closed, it's probably better to make the passage
2455 * self-illuminating (i.e., with brightness 1), because this will
2456 * put the passage in scope and thus allow it to be manipulated.
2457 */
2458 isConnectorVisibleInDark(origin, actor)
2459 { return isOpen() && inherited(origin, actor); }
2460
2461 /* a passage is passable when it's open */
2462 isConnectorPassable(origin, traveler) { return isOpen(); }
2463
2464 /*
2465 * Initialize. If we're a slave, we'll set up the otherSide
2466 * relationship between this passage and our master passage.
2467 */
2468 initializeThing()
2469 {
2470 /* inherit default handling */
2471 inherited();
2472
2473 /*
2474 * if we have a master side, initialize our relationship with
2475 * the master side
2476 */
2477 if (masterObject != self)
2478 {
2479 /* set our otherSide to point to the master */
2480 otherSide = masterObject;
2481
2482 /* set the master's otherSide to point to us */
2483 masterObject.initMasterObject(self);
2484 }
2485 }
2486
2487 /*
2488 * Initialize the master object. The other side of a two-sided door
2489 * will call this on the master object to let the master object know
2490 * about the other side. 'other' is the other-side object. By
2491 * default, we'll simply remember the other object in our own
2492 * 'otherSide' property.
2493 */
2494 initMasterObject(other) { otherSide = other; }
2495
2496 /*
2497 * Our corresponding passage object on the other side of the
2498 * passage. This will be set automatically during initialization
2499 * based on the masterObject property of the slave - it is not
2500 * generally necessary to set this manually.
2501 */
2502 otherSide = nil
2503
2504 /* our other side is the other facet of the passage */
2505 getFacets() { return otherSide != nil ? [otherSide] : inherited(); }
2506
2507 /*
2508 * our source is always our destination, since we have a one-to-one
2509 * relationship with our comlementary passage in the destination
2510 * location (we point to it, it points to us)
2511 */
2512 fixedSource(origin, traveler) { return destination; }
2513
2514 /* the connector back is our complementary side, if we have one */
2515 connectorBack(traveler, dest)
2516 {
2517 /* if we have a complementary side, it's the connector back */
2518 if (otherSide != nil)
2519 return otherSide;
2520
2521 /* we don't have a complementary side, so use the default handling */
2522 return inherited(traveler, dest);
2523 }
2524
2525 /* can the given actor travel through this passage? */
2526 canActorTravel(actor)
2527 {
2528 /*
2529 * by default, the actor can travel through the passage if and
2530 * only if the passage is open
2531 */
2532 return isOpen;
2533 }
2534
2535 /*
2536 * Display our message when we don't allow the actor to travel
2537 * through the passage because the passage is closed. By default,
2538 * we'll simply display the default cannotTravel message for the
2539 * actor's location, but this can be overridden to provide a more
2540 * specific report of the problem.
2541 */
2542 cannotTravel()
2543 {
2544 /* use the actor's location's cannotTravel handling */
2545 gActor.location.cannotTravel();
2546 }
2547
2548 /* carry out travel via this connector */
2549 dobjFor(TravelVia)
2550 {
2551 /* check travel */
2552 check()
2553 {
2554 /*
2555 * Move the actor only if we're open; if we're not, use the
2556 * standard no-travel handling for the actor's location.
2557 * Note that we don't try to implicitly open the passage,
2558 * because the basic passage is not openable; if we're
2559 * closed, it means we're impassable for some reason that
2560 * presumably cannot be remedied by a simple "open" command.
2561 */
2562 if (!canActorTravel(gActor))
2563 {
2564 /* we're closed, so use our no-travel handling */
2565 cannotTravel();
2566 exit;
2567 }
2568 else
2569 {
2570 /* inherit the default checks */
2571 inherited();
2572 }
2573 }
2574 }
2575
2576 dobjFor(LookThrough)
2577 {
2578 action()
2579 {
2580 /*
2581 * if we're open, we simply "can't see much from here";
2582 * otherwise, we can't see through it at all
2583 */
2584 if (isOpen)
2585 mainReport(&nothingThroughPassageMsg);
2586 else
2587 inherited();
2588 }
2589 }
2590 ;
2591
2592 /*
2593 * A passage that an actor can travel through (with a "go through" or
2594 * "enter" command). A "go through" command applied to the passage
2595 * simply makes the actor travel through the passage as though using the
2596 * appropriate directional command.
2597 *
2598 * We describe actors arriving or departing via the passage using
2599 * "through" descriptions ("Bob arrives through the door," etc).
2600 */
2601 class ThroughPassage: Passage
2602 describeDeparture(traveler, origin, dest)
2603 {
2604 /* describe the traveler departing through this passage */
2605 traveler.sayDepartingThroughPassage(self);
2606 }
2607
2608 describeArrival(traveler, origin, dest)
2609 {
2610 /* describe the traveler arriving through this passage */
2611 traveler.sayArrivingThroughPassage(self);
2612 }
2613
2614 /* treat "go through self" as travel through the passage */
2615 dobjFor(GoThrough) remapTo(TravelVia, self)
2616
2617 /* "enter" is the same as "go through" for this type of passage */
2618 dobjFor(Enter) asDobjFor(GoThrough)
2619
2620 /*
2621 * Explicitly map the indirect object 'verify' handlers for the
2622 * push-travel commands corresponding to the regular travel commands
2623 * we provide. This isn't strictly necessary except when we're mixed
2624 * in to something like a Decoration, in which case explicitly
2625 * defining the mapping here is important because it will give the
2626 * mapping higher precedence than a catch-all handlers overriding the
2627 * same mapping we inherit from Thing. We use the same mapping that
2628 * Thing provides by default.
2629 */
2630 mapPushTravelIobj(PushTravelThrough, TravelVia)
2631 mapPushTravelIobj(PushTravelEnter, TravelVia)
2632 ;
2633
2634 /*
2635 * A Path Passage is a specialization of through passage that's more
2636 * suitable for outdoor locations. This type of passage is good for
2637 * things like outdoor walkways, paths, and streets, where travelers walk
2638 * along the connector but aren't enclosed by it. The main practical
2639 * difference is how we describe departures and arrivals; in English, we
2640 * describe these as being "via" the path rather than "through" the path,
2641 * since there's no enclosure involved in these connections.
2642 */
2643 class PathPassage: ThroughPassage
2644 describeDeparture(traveler, origin, dest)
2645 {
2646 traveler.sayDepartingViaPath(self);
2647 }
2648
2649 describeArrival(traveler, origin, dest)
2650 {
2651 /* describe the traveler arriving through this passage */
2652 traveler.sayArrivingViaPath(self);
2653 }
2654
2655 /* putting something on a path is the same as dropping it */
2656 iobjFor(PutOn) remapTo(Drop, DirectObject)
2657
2658 /* use a special message for standing on a path */
2659 cannotStandOnMsg = &cannotStandOnPathMsg
2660
2661 /* FOLLOW PATH -> travel via the path */
2662 dobjFor(Follow) remapTo(TravelVia, DirectObject)
2663 ;
2664
2665
2666 /* ------------------------------------------------------------------------ */
2667 /*
2668 * The exit portal of a one-way passage. This isn't a fully functional
2669 * passage, but rather an object that acts as the receiving end of a
2670 * passage that can only be traversed in one direction.
2671 *
2672 * This can be used for various purposes: the underside of a trap door,
2673 * the bottom of a chute, the exit of a wormhole.
2674 */
2675 class ExitOnlyPassage: ThroughPassage
2676 dobjFor(TravelVia)
2677 {
2678 action()
2679 {
2680 /*
2681 * Show our default failure message. This can be overridden
2682 * to provide a more customized description of why the
2683 * passage cannot be entered from this side.
2684 */
2685 reportFailure(&cannotEnterExitOnlyMsg, self);
2686 }
2687 }
2688 ;
2689
2690 /* ------------------------------------------------------------------------ */
2691 /*
2692 * Stairway - this is a special kind of passage that is used for
2693 * vertical connections, such as stairways and ladders. This type of
2694 * passage doesn't allow "enter" or "go through," but does allow "climb".
2695 *
2696 * The basic Stairway should generally not be used, as it doesn't know
2697 * where it is relative to connected stairs. Instead, the more specific
2698 * StairwayUp and StairwayDown should be used in most cases.
2699 *
2700 * Note that at midpoints along long stairways (landings, for example),
2701 * separate stairway objects should normally be used: one going up and
2702 * one going down.
2703 */
2704 class Stairway: Passage
2705 /*
2706 * Treat "climb self" as travel through the passage
2707 */
2708 dobjFor(Climb) remapTo(TravelVia, self)
2709 ;
2710
2711 /*
2712 * A stairway going up from here.
2713 */
2714 class StairwayUp: Stairway
2715 describeArrival(traveler, origin, dest)
2716 {
2717 /*
2718 * describe the actor arriving by coming down these stairs (they
2719 * leave up, so they arrive down)
2720 */
2721 traveler.sayArrivingDownStairs(self);
2722 }
2723
2724 describeDeparture(traveler, origin, dest)
2725 {
2726 /* describe the actor leaving up these stairs */
2727 traveler.sayDepartingUpStairs(self);
2728 }
2729
2730 /* "climb up" is the specific direction for "climb" here */
2731 dobjFor(ClimbUp) asDobjFor(Climb)
2732
2733 /* cannot climb down from here */
2734 dobjFor(ClimbDown)
2735 {
2736 verify() { illogical(&stairwayNotDownMsg); }
2737 }
2738 ;
2739
2740 /*
2741 * A stairway going down from here.
2742 */
2743 class StairwayDown: Stairway
2744 /* "climb down" is the specific direction for "climb" here */
2745 dobjFor(ClimbDown) asDobjFor(Climb)
2746
2747 describeArrival(traveler, origin, dest)
2748 {
2749 /*
2750 * describe the actor arriving by coming up these stairs (they
2751 * leave down, so they arrive up)
2752 */
2753 traveler.sayArrivingUpStairs(self);
2754 }
2755
2756 describeDeparture(traveler, origin, dest)
2757 {
2758 /* describe the actor traveling down these stairs */
2759 traveler.sayDepartingDownStairs(self);
2760 }
2761
2762 /* cannot climb up from here */
2763 dobjFor(ClimbUp)
2764 {
2765 verify() { illogical(&stairwayNotUpMsg); }
2766 }
2767 ;
2768
2769 /* ------------------------------------------------------------------------ */
2770 /*
2771 * Basic Door class. This is the base class for door-like objects: a
2772 * travel connector with two sides, and which can be opened and closed.
2773 * Each side of the door is linked to the other, so that opening or
2774 * closing one side makes the same change to the other side.
2775 *
2776 * A basic door has the internal functionality of a door, but doesn't
2777 * provide handling for player commands that manipulate the open/closed
2778 * status.
2779 */
2780 class BasicDoor: BasicOpenable, ThroughPassage
2781 /*
2782 * Open/close the door. If we have a complementary door object
2783 * representing the other side, we'll remark in the sensory context
2784 * of its location that it's also opening or closing.
2785 */
2786 makeOpen(stat)
2787 {
2788 /* inherit the default behavior */
2789 inherited(stat);
2790
2791 /*
2792 * if our new status is in effect, notify the other side so that
2793 * it can generate a message in its location
2794 */
2795 if (isOpen == stat && otherSide != nil)
2796 otherSide.noteRemoteOpen(stat);
2797 }
2798
2799 /*
2800 * Note a "remote" change to our open/close status. This is an
2801 * open/close operation performed on our complementary object
2802 * representing the other side of the door. We'll remark on the
2803 * change in the sensory context of this side, but only if we're
2804 * suppressing output in the current context - if we're not, then
2805 * the player will see the message generated by the side that we
2806 * directly acted upon, so we don't need a separate report for the
2807 * other side.
2808 */
2809 noteRemoteOpen(stat)
2810 {
2811 /*
2812 * If I'm not visible to the player character in the current
2813 * sense context, where the action is actually taking place,
2814 * switch to my own sensory context and display a report. This
2815 * way, if the player can see this door but not the other side,
2816 * and the action is taking place on the other side, we'll still
2817 * see a note about the change. We only need to do this if
2818 * we're not already visible to the player, because if we are,
2819 * we'll generate an ordinary report of the door opening in the
2820 * action's context.
2821 */
2822 if (senseContext.isBlocking)
2823 {
2824 /* show a message in my own sensory context */
2825 callWithSenseContext(self, sight, {: describeRemoteOpen(stat) });
2826 }
2827 }
2828
2829 /*
2830 * Describe the door being opened remotely (that is, by someone on
2831 * the other side). This is called from noteRemoteOpen to actually
2832 * display the message.
2833 */
2834 describeRemoteOpen(stat)
2835 {
2836 /* show the default library message for opening the door remotely */
2837 gLibMessages.sayOpenDoorRemotely(self, stat);
2838 }
2839
2840 /* carry out travel through the door */
2841 dobjFor(TravelVia)
2842 {
2843 action()
2844 {
2845 /* move the actor to our destination */
2846 inherited();
2847
2848 /* remember that this is the last door the actor traversed */
2849 gActor.rememberLastDoor(self);
2850 }
2851 }
2852
2853 /*
2854 * Boost the likelihood that a command is referring to us if we just
2855 * traveled through the door; this is meant to be called from
2856 * verify() routines. For a few commands (close, lock), the most
2857 * likely door being referred to is the door just traversed.
2858 */
2859 boostLikelihoodOnTravel()
2860 {
2861 /*
2862 * if this (or our other side) is the last door the actor
2863 * traversed, boost the likelihood that they're referring to us
2864 */
2865 if (gActor.lastDoorTraversed == self
2866 || (otherSide != nil && gActor.lastDoorTraversed == otherSide))
2867 logicalRank(120, 'last door traversed');
2868 }
2869 ;
2870
2871 /* ------------------------------------------------------------------------ */
2872 /*
2873 * Door. This is a travel connector that can be opened and closed with
2874 * player commands. This is a simple subclass of BasicDoor that adds
2875 * support for player commands to manipulate the door.
2876 */
2877 class Door: Openable, BasicDoor
2878 /* make us initially closed */
2879 initiallyOpen = nil
2880
2881 /* if we can't travel because the door is closed, say so */
2882 cannotTravel()
2883 {
2884 if (gActor.canSee(self) && !isOpen)
2885 reportFailure(&cannotGoThroughClosedDoorMsg, self);
2886 else
2887 inherited();
2888 }
2889
2890 /*
2891 * get the 'door open' precondition - by default, we create a
2892 * standard doorOpen precondition for this object, but this can be
2893 * overridden if desired to create custom doorOpen variations
2894 */
2895 getDoorOpenPreCond() { return new ObjectPreCondition(self, doorOpen); }
2896
2897 /* the door must be open before we can travel this way */
2898 connectorTravelPreCond()
2899 {
2900 local ret;
2901 local doorCond;
2902
2903 /* start with the inherited conditions */
2904 ret = inherited();
2905
2906 /* if there's a door-open condition, add it as well */
2907 if ((doorCond = getDoorOpenPreCond()) != nil)
2908 ret += doorCond;
2909
2910 /* return the result */
2911 return ret;
2912 }
2913
2914 dobjFor(Close)
2915 {
2916 verify()
2917 {
2918 /* inherit default handling */
2919 inherited();
2920
2921 /* boost the likelihood if they just traveled through us */
2922 boostLikelihoodOnTravel();
2923 }
2924 }
2925
2926 dobjFor(Lock)
2927 {
2928 verify()
2929 {
2930 /* inherit default handling */
2931 inherited();
2932
2933 /* boost the likelihood if they just traveled through us */
2934 boostLikelihoodOnTravel();
2935 }
2936 }
2937
2938 /*
2939 * looking behind a door implies opening it to see what's on the
2940 * other side
2941 */
2942 dobjFor(LookBehind)
2943 {
2944 preCond = (inherited + objOpen)
2945 action()
2946 {
2947 /*
2948 * If the door is open, AND we implicitly opened the door to
2949 * carry out this action, use a special report that takes
2950 * into account that we were specifically looking to see what
2951 * was on the other side of the door; otherwise, use the
2952 * default behavior
2953 */
2954 if (isOpen
2955 && gTranscript.currentActionHasReport(
2956 {x: (x.action_.ofKind(OpenAction)
2957 && x.isActionImplicit())}))
2958 mainReport(&nothingBeyondDoorMsg);
2959 else
2960 inherited();
2961 }
2962 }
2963
2964 /* looking through a door requires it to be open */
2965 dobjFor(LookThrough)
2966 {
2967 preCond = (nilToList(inherited) + objOpen)
2968 }
2969
2970 ;
2971
2972 /* ------------------------------------------------------------------------ */
2973 /*
2974 * Secret Door. This is a special kind of door that gives no hint of
2975 * its being a door when it's closed. This can be used for objects that
2976 * serve as a secret passage but are otherwise normal objects, such as a
2977 * bookcase that conceals a passage.
2978 */
2979 class SecretDoor: BasicDoor
2980 /* a secret passage usually starts off secret (i.e., closed) */
2981 initiallyOpen = nil
2982
2983 isConnectorApparent(origin, actor)
2984 {
2985 /*
2986 * A secret passage is not apparent as a pasage unless it's
2987 * open.
2988 */
2989 if (isOpen)
2990 {
2991 /* the passage is open - use the inherited handling */
2992 return inherited(origin, actor);
2993 }
2994 else
2995 {
2996 /*
2997 * the passage is closed, so it's not apparently a passage
2998 * at all
2999 */
3000 return nil;
3001 }
3002 }
3003 ;
3004
3005 /*
3006 * Hidden Door. This is a secret door that is invisible when closed.
3007 * This can be used when the passage isn't even visible when it's
3008 * closed, such as a door that seamlessly melds into the wall.
3009 */
3010 class HiddenDoor: SecretDoor
3011 /*
3012 * When we're closed, we're completely invisible, so we have no
3013 * sight presence. When we're open, we have our normal visual
3014 * presence.
3015 */
3016 sightPresence = (isOpen)
3017 ;
3018
3019 /* ------------------------------------------------------------------------ */
3020 /*
3021 * Automatically closing door. This type of door closes behind an actor
3022 * as the actor traverses the connector.
3023 */
3024 class AutoClosingDoor: Door
3025 dobjFor(TravelVia)
3026 {
3027 action()
3028 {
3029 /* inherit the default handling */
3030 inherited();
3031
3032 /*
3033 * Only close the door if the actor performing the travel
3034 * isn't accompanying another actor. If the actor is
3035 * accompanying someone, we essentially want them to hold the
3036 * door for the other actor - at the very least, we don't
3037 * want to slam it in the other actor's face!
3038 */
3039 if (!gActor.curState.ofKind(AccompanyingInTravelState))
3040 {
3041 /* close the door */
3042 makeOpen(nil);
3043
3044 /* mention that the automatic closing */
3045 reportAutoClose();
3046 }
3047 }
3048 }
3049
3050 /*
3051 * Report the automatic closure. The TravelVia action() calls this
3052 * after closing the door to generate a message mentioning that the
3053 * door was closed. By default, we just show the standard
3054 * doorClosesBehindMsg library message.
3055 */
3056 reportAutoClose() { mainReport(&doorClosesBehindMsg, self); }
3057 ;
3058
3059 /* ------------------------------------------------------------------------ */
3060 /*
3061 * The base class for Enterables and Exitables. These are physical
3062 * objects associated with travel connectors. For example, the object
3063 * representing the exterior of a building in the location containing
3064 * the building could be an Enterable, so that typing ENTER BUILDING
3065 * takes us into the building via the travel connector that leads inside.
3066 *
3067 * Enterables and Exitables are physical covers for travel connectors.
3068 * These objects aren't travel connectors themselves, and they don't
3069 * specify the destination; instead, these just point to travel
3070 * connectors.
3071 */
3072 class TravelConnectorLink: object
3073 /* the underlying travel connector */
3074 connector = nil
3075
3076 /*
3077 * The internal "TravelVia" action just maps to travel via the
3078 * underlying connector. However, we want to apply our own
3079 * preconditions, so we don't directly remap to the underlying
3080 * connector. Instead, we provide our own full TravelVia
3081 * implementation, and then we perform the travel on the underlying
3082 * connector via a replacement action in our own action() handler.
3083 */
3084 dobjFor(TravelVia)
3085 {
3086 /* the physical link object has to be touchable */
3087 preCond = [touchObj]
3088
3089 verify() { }
3090 action()
3091 {
3092 /* carry out the action by traveling via our connector */
3093 replaceAction(TravelVia, connector);
3094 }
3095 }
3096
3097 /*
3098 * These objects are generally things like buildings (exterior or
3099 * interior), which tend to be large enough that their details can be
3100 * seen at a distance.
3101 */
3102 sightSize = large
3103 ;
3104
3105 /*
3106 * An Enterable is an object that exists in one location, and which can
3107 * be entered to take an actor to another location. Enterables are used
3108 * for things such as the outsides of buildings, so that the building
3109 * can have a presence on its outside and can be entered via a command
3110 * like "go into building".
3111 *
3112 * An Enterable isn't a connector, but points to a connector. This type
3113 * of object is most useful when there's already a connector that exists
3114 * as a separate object, such as the door to a house: the house object
3115 * can be made an Enterable that simply points to the door object.
3116 */
3117 class Enterable: TravelConnectorLink, Fixture
3118 /*
3119 * "Enter" action this simply causes the actor to travel via the
3120 * connector.
3121 */
3122 dobjFor(Enter) remapTo(TravelVia, self)
3123
3124 /* explicitly define the push-travel indirect object mapping */
3125 mapPushTravelIobj(PushTravelEnter, TravelVia)
3126 ;
3127
3128 /*
3129 * An Exitable is like an Enterable, except that you exit it rather than
3130 * enter it. This can be used for objects representing the current
3131 * location as an enclosure (a jail cell), or an exit door.
3132 */
3133 class Exitable: TravelConnectorLink, Fixture
3134 /* Get Out Of/Exit action - this simply maps to travel via the connector */
3135 dobjFor(GetOutOf) remapTo(TravelVia, self)
3136
3137 /* explicitly define the push-travel indirect object mapping */
3138 mapPushTravelIobj(PushTravelGetOutOf, TravelVia)
3139 ;
3140
3141 /*
3142 * An EntryPortal is just like an Enterable, except that "go through"
3143 * also works on it. Likewise, an ExitPortal is just like an Exitable
3144 * but accepts "go through" as well.
3145 */
3146 class EntryPortal: Enterable
3147 dobjFor(GoThrough) remapTo(TravelVia, self)
3148 ;
3149 class ExitPortal: Exitable
3150 dobjFor(GoThrough) remapTo(TravelVia, self)
3151 ;
3152
3153 /* ------------------------------------------------------------------------ */
3154 /*
3155 * A "TravelPushable" is an object that can't be taken, but can be moved
3156 * from one location to another via commands of the form "push obj dir,"
3157 * "push obj through passage," and the like.
3158 *
3159 * TravelPushables tend to be rather rare, and we expect that instances
3160 * will almost always be special cases that require additional
3161 * specialized code. This is therefore only a general framework for
3162 * pushability.
3163 */
3164 class TravelPushable: Immovable
3165 cannotTakeMsg = &cannotTakePushableMsg
3166 cannotMoveMsg = &cannotMovePushableMsg
3167 cannotPutMsg = &cannotPutPushableMsg
3168
3169 /* can we be pushed via the given travel connector? */
3170 canPushTravelVia(connector, dest) { return true; }
3171
3172 /* explain why canPushTravelVia said we can't be pushed this way */
3173 explainNoPushTravelVia(connector, dest) { }
3174
3175 /*
3176 * Receive notification that we're about to be pushed somewhere.
3177 * This is called just before the underlying traveler performs the
3178 * actual travel. (By default, we don't even define this method, to
3179 * ensure that if we're combined with another base class that
3180 * overrides the method, the overrider will be called.)
3181 */
3182 // beforeMovePushable(traveler, connector, dest) { }
3183
3184 /*
3185 * Move the object to a new location as part of a push-travel
3186 * operation. By default, this simply uses moveInto to move the
3187 * object, but subclasses can override this to apply conditions to
3188 * the pushing, put the object somewhere else in some cases, display
3189 * extra messages, or do anything else that's necessary.
3190 *
3191 * Our special PushTraveler calls this routine after the underlying
3192 * real traveler has finished its travel to the new location, so the
3193 * traveler's location will indicate the destination of the travel.
3194 * Note that this routine is never called if the traveler ends up in
3195 * its original location after the travel, so this routine isn't
3196 * called when travel isn't allowed for the underlying traveler.
3197 */
3198 movePushable(traveler, connector)
3199 {
3200 /* move me to the traveler's new location */
3201 moveIntoForTravel(traveler.location);
3202
3203 /* describe what we're doing */
3204 describeMovePushable(traveler, connector);
3205 }
3206
3207 /*
3208 * Describe the actor pushing the object into the new location.
3209 * This is called from movePushable; we pull this out as a separate
3210 * routine so that the description of the pushing can be overridden
3211 * without having to override all of movePushable.
3212 */
3213 describeMovePushable(traveler, connector)
3214 {
3215 /*
3216 * If the actor is the player character, mention that we're
3217 * pushing the object with us. For an NPC, show nothing,
3218 * because the normal travel message will mention that the NPC
3219 * is pushing the object as part of the normal travel report
3220 * (thanks to PushTraveler's name override).
3221 */
3222 if (gActor.isPlayerChar)
3223 mainReport(&okayPushTravelMsg, self);
3224 }
3225
3226 dobjFor(PushTravel)
3227 {
3228 verify() { }
3229 action()
3230 {
3231 local newTrav;
3232 local oldTrav;
3233 local wasExcluded;
3234
3235 /*
3236 * Create a push traveler to coordinate the travel. The
3237 * push traveler performs the normal travel, and also moves
3238 * the object being pushed.
3239 */
3240 newTrav = pushTravelerClass.createInstance(
3241 self, gActor.getPushTraveler(self));
3242
3243 /* set the actor's special traveler to our push traveler */
3244 oldTrav = gActor.setSpecialTraveler(newTrav);
3245
3246 /*
3247 * Add myself to the actor's look-around exclusion list -
3248 * this ensures that we won't be listed among the objects in
3249 * the new location when we show the description on arrival.
3250 * This is desirable because of how we describe the travel:
3251 * we first want to show the new location's description, then
3252 * describe pushing this object into the new location. But
3253 * we actually have to move the object first, to make sure
3254 * any side effects of its presence in the new location are
3255 * taken into account when we describe the location - because
3256 * by the time we're in the room enough to have a look
3257 * around, the pushable will be in the room with us. Even
3258 * so, we don't actually want to describe the pushable as in
3259 * the room at this stage, because from the player's
3260 * perspective we're really still in the process of pushing
3261 * it; describing it as already settled into the new location
3262 * sounds weird because it makes it seem like the object was
3263 * already in place when we arrived. To avoid this
3264 * weirdness, suppress it from the new location's initial
3265 * description on arrival.
3266 */
3267 wasExcluded = gActor.excludeFromLookAround(self);
3268
3269 /* make sure we undo our global changes when we're done */
3270 try
3271 {
3272 /*
3273 * Now that we've activated our special push traveler
3274 * for the actor, simply perform the ordinary travel
3275 * command. The travel command we perform depends on
3276 * the kind of push-travel command we attempted, so
3277 * simply let the current action carry out the action.
3278 */
3279 gAction.performTravel();
3280 }
3281 finally
3282 {
3283 /* restore the actor's old special traveler on the way out */
3284 gActor.setSpecialTraveler(oldTrav);
3285
3286 /*
3287 * if we weren't in the actor's 'look around' exclusion
3288 * list before, remove us from the list
3289 */
3290 if (!wasExcluded)
3291 gActor.unexcludeFromLookAround(self);
3292 }
3293 }
3294 }
3295
3296 /*
3297 * The class we create for our special push traveler - by default,
3298 * this is PushTraveler, but we parameterize this via this property
3299 * to allow special PushTraveler subclasses to be created; this
3300 * could be useful, for example, to customize the traveler name
3301 * messages.
3302 */
3303 pushTravelerClass = PushTraveler
3304 ;
3305
3306 /*
3307 * A special Traveler class for travel involving pushing an object from
3308 * one room to another. This class encapsulates the object being pushed
3309 * and the actual Traveler performing the travel.
3310 *
3311 * For the most part, we refer Traveler methods to the underlying
3312 * Traveler. We override a few methods to provide special handling.
3313 */
3314 class PushTraveler: object
3315 construct(obj, traveler)
3316 {
3317 /* remember the object being pushed and the real traveler */
3318 obj_ = obj;
3319 traveler_ = traveler;
3320 }
3321
3322 /* the object being pushed */
3323 obj_ = nil
3324
3325 /*
3326 * the underlying Traveler - this is the real Traveler that will
3327 * move to a new location
3328 */
3329 traveler_ = nil
3330
3331 /*
3332 * Travel to a new location. We'll run the normal travel routine
3333 * for the underlying real traveler; then, if we ended up in a new
3334 * location, we'll move the object being pushed to the traveler's
3335 * new location.
3336 */
3337 travelerTravelTo(dest, connector, backConnector)
3338 {
3339 local oldLoc;
3340 local origin;
3341
3342 /* remember the traveler's origin, so we can tell if we moved */
3343 origin = traveler_.location;
3344
3345 /* let the object being pushed describe the departure */
3346 obj_.beforeMovePushable(traveler_, connector, dest);
3347
3348 /*
3349 * *Tentatively* move the pushable to its new location. Just do
3350 * the basic move-into for this, since it's only tentative - this
3351 * is just so that we get any side effects of its presence in the
3352 * new location for the purposes of describing the new location.
3353 */
3354 oldLoc = location;
3355 obj_.baseMoveInto(dest);
3356
3357 /*
3358 * Call the traveler's normal travel routine, to perform the
3359 * actual movement of the underlying traveler.
3360 */
3361 traveler_.travelerTravelTo(dest, connector, backConnector);
3362
3363 /* undo the tentative move of the pushable */
3364 obj_.baseMoveInto(oldLoc);
3365
3366 /*
3367 * If we moved to a new location, we can now actually move the
3368 * pushable object to the new location.
3369 */
3370 if (traveler_.location != origin)
3371 obj_.movePushable(traveler_, connector);
3372 }
3373
3374 /*
3375 * Perform local travel, between nested rooms within a top-level
3376 * location. By default, we simply don't allow pushing objects
3377 * between nested rooms.
3378 *
3379 * To allow pushing an object between nested rooms, override this in
3380 * parallel with travelerTravelTo(). Note that you'll have to call
3381 * travelerTravelWithin() on the underlying traveler (which will
3382 * generally be the actor), and you'll probably want to set up a new
3383 * set of notifiers parallel to beforeMovePushable() and
3384 * movePushable(). You'll probably particularly need to customize
3385 * the report in your parallel for movePushable() - the default ("you
3386 * push x into the area") isn't very good when nested rooms are
3387 * involved, and you'll probably want something more specific.
3388 */
3389 travelerTravelWithin(actor, dest)
3390 {
3391 reportFailure(&cannotPushObjectNestedMsg, obj_);
3392 exit;
3393 }
3394
3395 /*
3396 * Can we travel via the given connector? We'll ask our underlying
3397 * traveler first, and if that succeeds, we'll ask the object we're
3398 * pushing.
3399 */
3400 canTravelVia(connector, dest)
3401 {
3402 /* ask the underlying traveler first, then our pushed object */
3403 return (traveler_.canTravelVia(connector, dest)
3404 && obj_.canPushTravelVia(connector, dest));
3405 }
3406
3407 /*
3408 * Explain why the given travel is not possible. If our underlying
3409 * traveler raised the objection, let it explain; otherwise, let our
3410 * pushed object explain.
3411 */
3412 explainNoTravelVia(connector, dest)
3413 {
3414 if (!traveler_.canTravelVia(connector, dest))
3415 traveler_.explainNoTravelVia(connector, dest);
3416 else
3417 obj_.explainNoPushTravelVia(connector, dest);
3418 }
3419
3420 /* by default, send everything to the underlying Traveler */
3421 propNotDefined(prop, [args]) { return traveler_.(prop)(args...); }
3422 ;
3423
3424 /*
3425 * A PushTravelBarrier is a TravelConnector that allows regular travel,
3426 * but not travel that involves pushing something. By default, we block
3427 * all push travel, but subclasses can customize this so that we block
3428 * only specific objects.
3429 */
3430 class PushTravelBarrier: TravelBarrier
3431 /*
3432 * Determine if the given pushed object is allowed to pass. Returns
3433 * true if so, nil if not. By default, we'll return nil for every
3434 * object; subclasses can override this to allow some objects to be
3435 * pushed through the barrier but not others.
3436 */
3437 canPushedObjectPass(obj) { return nil; }
3438
3439 /* explain why an object can't pass */
3440 explainTravelBarrier(traveler)
3441 {
3442 reportFailure(&cannotPushObjectThatWayMsg, traveler.obj_);
3443 }
3444
3445 /*
3446 * Determine if the given traveler can pass through this connector.
3447 * If the traveler isn't a push traveler, we'll allow the travel;
3448 * otherwise, we'll block the travel if our canPushedObjectPass
3449 * routine says the object being pushed can pass.
3450 */
3451 canTravelerPass(traveler)
3452 {
3453 /* if it's not a push traveler, it can pass */
3454 if (!traveler.ofKind(PushTraveler))
3455 return true;
3456
3457 /* it can pass if we can pass the object being pushed */
3458 return canPushedObjectPass(traveler.obj_);
3459 }
3460 ;
3461
3462
3463 /* ------------------------------------------------------------------------ */
3464 /*
3465 * A basic location - this is the base class for locations that can
3466 * contain actors.
3467 */
3468 class BasicLocation: Thing
3469 /*
3470 * Get the nested room list grouper for an actor in the given
3471 * posture directly in this room. This is used when we're listing
3472 * the actors within the nested room as
3473 *
3474 * By default, we maintain a lookup table, and store one nested
3475 * actor grouper object for each posture. This makes it so that we
3476 * show one group per posture in this room; for example, if we
3477 * contain two sitting actors and three standing actors, we'll say
3478 * something like "bill and bob are sitting on the stage, and jill,
3479 * jane, and jack are standing on the stage." This can be
3480 * overridden if a different arrangement of groups is desired; for
3481 * example, an override could simply return a single grouper to list
3482 * everyone in the room together, regardless of posture.
3483 */
3484 listWithActorIn(posture)
3485 {
3486 /* if we don't have a lookup table for this yet, create one */
3487 if (listWithActorInTable == nil)
3488 listWithActorInTable = new LookupTable(5, 5);
3489
3490 /* if this posture isn't in the table yet, create a grouper for it */
3491 if (listWithActorInTable[posture] == nil)
3492 listWithActorInTable[posture] =
3493 new RoomActorGrouper(self, posture);
3494
3495 /* return the grouper for this posture */
3496 return listWithActorInTable[posture];
3497 }
3498
3499 /*
3500 * our listWithActorIn table - this gets initialized to a
3501 * LookupTable as soon as we need one (in listWithActorIn)
3502 */
3503 listWithActorInTable = nil
3504
3505 /*
3506 * Check the ambient illumination level in the room for the given
3507 * actor's senses to determine if the actor would be able to see if
3508 * the actor were in this room without any additional light sources.
3509 * Returns true if the room is lit for the purposes of the actor's
3510 * visual senses, nil if not.
3511 *
3512 * Note that if the actor is already in this location, there's no
3513 * need to run this test, since we could get the information from
3514 * the actor directly. The point of this test is to determine the
3515 * light level in this location without the actor having to be
3516 * present.
3517 */
3518 wouldBeLitFor(actor)
3519 {
3520 /*
3521 * Check for a simple, common case before running the more
3522 * expensive full what-if test. Most rooms provide their own
3523 * illumination in the actor's 'sight' sense; if this room does
3524 * provide its own interior 'brightness' of at least 2, and the
3525 * actor has 'sight' among its visual senses, then the room is
3526 * lit. Note that we must use transSensingOut() to determine
3527 * how we transmit our own brightness to our interior, since
3528 * that gives the transparency for looking from within this room
3529 * at objects outside the room; our own intrinsic brightness is
3530 * defined as the brightness of our exterior surface.
3531 */
3532 if (actor.sightlikeSenses.indexOf(sight) != nil
3533 && brightness > 1
3534 && transSensingOut(sight) == transparent)
3535 return true;
3536
3537 /*
3538 * We can't determine for sure that the location is lit with a
3539 * simple test, so run a full what-if test, checking what
3540 * ambient light level our "probe" object would see if it were
3541 * moved to this location. Return true if the ambient light
3542 * level is higher than the "self-lit" value of 1, nil if not.
3543 */
3544 return (lightProbe.whatIf(
3545 {: senseAmbientMax(actor.sightlikeSenses) },
3546 &moveInto, self) > 1);
3547 }
3548
3549 /*
3550 * Show our room description: this is the interior description of
3551 * the room, for use when the room is viewed by an actor within the
3552 * room. By default, we show our ordinary 'desc'.
3553 */
3554 roomDesc { desc; }
3555
3556 /* as part of a room description, mention an actor in this room */
3557 roomActorHereDesc(actor) { gLibMessages.actorInRoom(actor, self); }
3558
3559 /*
3560 * Provide a default description for an actor in this location, as
3561 * seen from a remote location (i.e., from a separate top-level room
3562 * that's linked to our top-level room by a sense connector of some
3563 * kind). By default, we'll describe the actor as being in this
3564 * nested room.
3565 */
3566 roomActorThereDesc(actor)
3567 {
3568 local pov = getPOV();
3569 local outer;
3570
3571 /* get the outermost visible enclosing room */
3572 outer = getOutermostVisibleRoom(pov);
3573
3574 /*
3575 * If we found a room, and it's not us (i.e., we found something
3576 * outside this room that can be seen from the current point of
3577 * view), use the three-part description: actor in nested room
3578 * (self) in outer room. If we didn't find a room, or it's the
3579 * same as us, all we can say is that the actor is in the nested
3580 * room.
3581 */
3582 if (outer not in (nil, self))
3583 {
3584 /* use the three-part description: actor in self in outer */
3585 gLibMessages.actorInRemoteNestedRoom(actor, self, outer, pov);
3586 }
3587 else
3588 {
3589 /* we're as far out as we can see: just say actor is in self */
3590 gLibMessages.actorInRemoteRoom(actor, self, pov);
3591 }
3592 }
3593
3594 /* show the status addendum for an actor in this location */
3595 roomActorStatus(actor) { gLibMessages.actorInRoomStatus(actor, self); }
3596
3597 /* describe the actor's posture while in this location */
3598 roomActorPostureDesc(actor)
3599 { gLibMessages.actorInRoomPosture(actor, self); }
3600
3601 /* acknowledge a posture change while in this location */
3602 roomOkayPostureChange(actor)
3603 { defaultReport(&roomOkayPostureChangeMsg, actor.posture, self); }
3604
3605 /*
3606 * describe the actor's posture as part of the EXAMINE description of
3607 * the nested room
3608 */
3609 roomListActorPosture(actor)
3610 { gLibMessages.actorInRoom(actor, self); }
3611
3612 /*
3613 * Prefix and suffix messages for listing a group of actors
3614 * nominally in this location. 'posture' is the posture of the
3615 * actors. 'remote' is the outermost visible room containing the
3616 * actors, but only if that room is remote from the point-of-view
3617 * actor; if everything is local, this will be nil. 'lst' is the
3618 * list of actors being listed. By default, we'll just show the
3619 * standard library messages.
3620 */
3621 actorInGroupPrefix(pov, posture, remote, lst)
3622 {
3623 if (remote == nil)
3624 gLibMessages.actorInGroupPrefix(posture, self, lst);
3625 else
3626 gLibMessages.actorInRemoteGroupPrefix(
3627 pov, posture, self, remote, lst);
3628 }
3629 actorInGroupSuffix(pov, posture, remote, lst)
3630 {
3631 if (remote == nil)
3632 gLibMessages.actorInGroupSuffix(posture, self, lst);
3633 else
3634 gLibMessages.actorInRemoteGroupSuffix(
3635 pov, posture, self, remote, lst);
3636 }
3637
3638 /*
3639 * Show a list of exits from this room as part of failed travel
3640 * ("you can't go that way").
3641 */
3642 cannotGoShowExits(actor)
3643 {
3644 /* if we have an exit lister, ask it to show exits */
3645 if (gExitLister != nil)
3646 gExitLister.cannotGoShowExits(actor, self);
3647 }
3648
3649 /* show the exit list in the status line */
3650 showStatuslineExits()
3651 {
3652 /* if we have a global exit lister, ask it to show the exits */
3653 if (gExitLister != nil)
3654 gExitLister.showStatuslineExits();
3655 }
3656
3657 /*
3658 * Get the estimated height, in lines of text, of the exits display's
3659 * contribution to the status line. This is used to calculate the
3660 * extra height we need in the status line, if any, to display the
3661 * exit list. If we're not configured to display exits in the status
3662 * line, this should return zero.
3663 */
3664 getStatuslineExitsHeight()
3665 {
3666 if (gExitLister != nil)
3667 return gExitLister.getStatuslineExitsHeight();
3668 else
3669 return 0;
3670 }
3671
3672 /*
3673 * Make the actor stand up from this location. By default, we'll
3674 * simply change the actor's posture to "standing," and show a
3675 * default success report.
3676 *
3677 * Subclasses might need to override this. For example, a chair
3678 * will set the actor's location to the room containing the chair
3679 * when the actor stands up from the chair.
3680 */
3681 makeStandingUp()
3682 {
3683 /* simply set the actor's new posture */
3684 gActor.makePosture(standing);
3685
3686 /* issue a default report of the change */
3687 defaultReport(&okayPostureChangeMsg, standing);
3688 }
3689
3690 /*
3691 * Default posture for an actor in the location. This is the
3692 * posture assumed by an actor when moving out of a nested room
3693 * within this location.
3694 */
3695 defaultPosture = standing
3696
3697 /* failure report we issue when we can't return to default posture */
3698 mustDefaultPostureProp = &mustBeStandingMsg
3699
3700 /* run the appropriate implied command to achieve our default posture */
3701 tryMakingDefaultPosture()
3702 {
3703 return defaultPosture.tryMakingPosture(self);
3704 }
3705
3706 /*
3707 * Check this object as a staging location. We're a valid location,
3708 * so we allow this.
3709 */
3710 checkStagingLocation(dest)
3711 {
3712 /* we've valid, so we don't need to do anything */
3713 }
3714
3715 /*
3716 * Try moving the actor into this location.
3717 */
3718 checkMovingActorInto(allowImplicit)
3719 {
3720 /*
3721 * If the actor isn't somewhere within us, we can't move the
3722 * actor here implicitly - we have no generic way of causing
3723 * implicit travel between top-level locations. Note that some
3724 * rooms might want to override this when travel between
3725 * adjacent locations makes sense as an implicit action; we
3726 * expect that such cases will be rare, so we don't attempt to
3727 * generalize this possibility.
3728 */
3729 if (!gActor.isIn(self))
3730 {
3731 reportFailure(&cannotDoFromHereMsg);
3732 exit;
3733 }
3734
3735 /*
3736 * if the actor is already directly in me, simply check to make
3737 * sure the actor is in the default posture for the room - if
3738 * not, try running an appropriate implied command to change the
3739 * posture
3740 */
3741 if (gActor.isDirectlyIn(self))
3742 {
3743 /* if the actor's already in the default posture, we're okay */
3744 if (gActor.posture == defaultPosture)
3745 return nil;
3746
3747 /* run the implied command to stand up (or whatever) */
3748 if (allowImplicit && tryMakingDefaultPosture())
3749 {
3750 /* make sure we're in the proper posture now */
3751 if (gActor.posture != defaultPosture)
3752 exit;
3753
3754 /* note that we ran an implied command */
3755 return true;
3756 }
3757
3758 /* we couldn't get into the default posture - give up */
3759 reportFailure(mustDefaultPostureProp);
3760 exit;
3761 }
3762
3763 /*
3764 * The actor is within a nested room within me. Find our
3765 * immediate child containing the actor, and remove the actor
3766 * from the child.
3767 */
3768 foreach (local cur in contents)
3769 {
3770 /* if this is the one containing the actor, remove the actor */
3771 if (gActor.isIn(cur))
3772 return cur.checkActorOutOfNested(allowImplicit);
3773 }
3774
3775 /* we didn't find the nested room with the actor, so give up */
3776 reportFailure(&cannotDoFromHereMsg);
3777 exit;
3778 }
3779
3780 /*
3781 * Check, using pre-condition rules, that the actor is ready to
3782 * enter this room as a nested location. By default, we do nothing,
3783 * since we're not designed as a nested location.
3784 */
3785 checkActorReadyToEnterNestedRoom(allowImplicit)
3786 {
3787 return nil;
3788 }
3789
3790 /*
3791 * Check that the traveler is directly in the given room, using
3792 * pre-condition rules. 'nested' is the nested location immediately
3793 * within this room that contains the actor (directly or
3794 * indirectly).
3795 */
3796 checkTravelerDirectlyInRoom(traveler, allowImplicit)
3797 {
3798 /* if the actor is already directly in this room, we're done */
3799 if (traveler.isDirectlyIn(self))
3800 return nil;
3801
3802 /* try moving the actor here */
3803 return traveler.checkMovingTravelerInto(self, allowImplicit);
3804 }
3805
3806 /*
3807 * Check, using pre-condition rules, that the actor is removed from
3808 * this nested location and moved to its exit destination. By
3809 * default, we're not a nested location, so there's nothing for us
3810 * to do.
3811 */
3812 checkActorOutOfNested(allowImplicit)
3813 {
3814 /* we're not a nested location, so there's nothing for us to do */
3815 return nil;
3816 }
3817
3818 /*
3819 * Determine if the current gActor, who is directly in this location,
3820 * is "travel ready." This means that the actor is ready, as far as
3821 * this location is concerned, to traverse the given connector. By
3822 * default, we consider an actor to be travel-ready if the actor is
3823 * standing; this takes care of most nested room situations, such as
3824 * chairs and beds, automatically.
3825 */
3826 isActorTravelReady(conn) { return gActor.posture == standing; }
3827
3828 /*
3829 * Run an implicit action, if possible, to make the current actor
3830 * "travel ready." This will be called if the actor is directly in
3831 * this location and isActorTravelReady() returns nil. By default,
3832 * we try to make the actor stand up. This should always be paired
3833 * with isActorTravelReady - the condition that routine tests should
3834 * be the condition this routine tries to bring into effect. If no
3835 * implicit action is possible, simply return nil.
3836 */
3837 tryMakingTravelReady(conn) { return tryImplicitAction(Stand); }
3838
3839 /* the message explaining what we must do to be travel-ready */
3840 notTravelReadyMsg = &mustBeStandingMsg
3841
3842 /*
3843 * An actor is attempting to disembark this location. By default,
3844 * we'll simply turn this into an "exit" command.
3845 */
3846 disembarkRoom()
3847 {
3848 /* treat this as an 'exit' command */
3849 replaceAction(Out);
3850 }
3851
3852 /*
3853 * The destination for objects explicitly dropped by an actor within
3854 * this room. By default, we'll return self, because items dropped
3855 * should simply go in the room itself. Some types of rooms will
3856 * want to override this; for example, a room that represents the
3857 * middle of a tightrope would probably want to set the drop
3858 * destination to the location below the tightrope. Likewise,
3859 * objects like chairs will usually prefer to have dropped items go
3860 * into the enclosing room.
3861 */
3862 getDropDestination(objToDrop, path)
3863 {
3864 /* by default, objects dropped in this room end up in this room */
3865 return self;
3866 }
3867
3868 /*
3869 * The nominal drop destination - this is the location where objects
3870 * are *reported* to go when dropped by an actor in this location.
3871 * By default, we simply return 'self'.
3872 *
3873 * The difference between the actual drop location and the nominal
3874 * drop location is that the nominal drop location is used only for
3875 * reporting messages, while the actual drop location is the
3876 * location where objects are moved on 'drop' or equivalent actions.
3877 * Rooms, for example, want to report that a dropped object lands on
3878 * the floor (or the ground, or whatever), even though the room
3879 * itself is the location where the object actually ends up. We
3880 * distinguish between the nominal and actual drop location to allow
3881 * these distinctions in reported messages.
3882 */
3883 getNominalDropDestination() { return self; }
3884
3885 /*
3886 * The "nominal actor container" - this is the container which we'll
3887 * say actors are in when we describe actors who are actually in
3888 * this location. By default, this simply returns self, but it's
3889 * sometimes useful to describe actors as being in some object other
3890 * than self. The most common case is that normal top-level rooms
3891 * usually want to describe actors as being "on the floor" or
3892 * similar.
3893 */
3894 getNominalActorContainer(posture) { return self; }
3895
3896 /*
3897 * Get any extra items in scope for an actor in this location.
3898 * These are items that are to be in scope even if they're not
3899 * reachable through any of the normal sense paths (so they'll be in
3900 * scope even in the dark, for example).
3901 *
3902 * By default, this returns nothing. Subclasses can override as
3903 * necessary to include additional items in scope. For example, a
3904 * chair would probably want to include itself in scope, since the
3905 * actor presumably knows he or she is sitting in a chair even if
3906 * it's too dark to see the chair.
3907 */
3908 getExtraScopeItems(actor) { return []; }
3909
3910 /*
3911 * Receive notification that we're about to perform a command within
3912 * this location. This is called on the outermost room first, then
3913 * on the nested rooms, from the outside in, until reaching the room
3914 * directly containing the actor performing the command.
3915 */
3916 roomBeforeAction()
3917 {
3918 }
3919
3920 /*
3921 * Receive notification that we've just finished a command within
3922 * this location. This is called on the room immediately containing
3923 * the actor performing the command, then on the room containing
3924 * that room, and so on to the outermost room.
3925 */
3926 roomAfterAction()
3927 {
3928 }
3929
3930 /*
3931 * Get my notification list - this is a list of objects on which we
3932 * must call beforeAction and afterAction when an action is
3933 * performed within this room.
3934 *
3935 * We'll also include any registered notification items for all of
3936 * our containing rooms up to the outermost container.
3937 *
3938 * The general notification mechanism always includes in the
3939 * notification list all of the objects connected by containment to
3940 * the actor, so objects that are in this room need not register for
3941 * explicit notification.
3942 */
3943 getRoomNotifyList()
3944 {
3945 local lst;
3946
3947 /* start with our explicitly registered list */
3948 lst = roomNotifyList;
3949
3950 /* add notification items for our immediate locations */
3951 forEachContainer(
3952 {cont: lst = lst.appendUnique(cont.getRoomNotifyList())});
3953
3954 /* return the result */
3955 return lst;
3956 }
3957
3958 /*
3959 * Add an item to our registered notification list for actions in
3960 * the room.
3961 *
3962 * Items can be added here if they must be notified of actions
3963 * performed by within the room even when the items aren't in the
3964 * room at the time of the action. All items connected by
3965 * containment with the actor performing an action are automatically
3966 * notified of the action; only items that must receive notification
3967 * even when not connected by containment need to be registered
3968 * here.
3969 */
3970 addRoomNotifyItem(obj)
3971 {
3972 roomNotifyList += obj;
3973 }
3974
3975 /* remove an item from the registered notification list */
3976 removeRoomNotifyItem(obj)
3977 {
3978 roomNotifyList -= obj;
3979 }
3980
3981 /* our list of registered notification items */
3982 roomNotifyList = []
3983
3984 /*
3985 * Get the room location. Since we're capable of holding actors, we
3986 * are our own room location.
3987 */
3988 roomLocation = (self)
3989
3990 /*
3991 * Receive notification that a traveler is arriving. This is a
3992 * convenience method that rooms can override to carry out side
3993 * effects of arrival. This is called just before the room's
3994 * arrival message (usually the location description) is displayed,
3995 * so the method can make any adjustments to the room's status or
3996 * contents needed for the arrival. By default, we do nothing.
3997 */
3998 enteringRoom(traveler) { }
3999
4000 /*
4001 * Receive notification that a traveler is leaving. This is a
4002 * convenience method that rooms can override to carry out side
4003 * effects of departure. This is called just after any departure
4004 * message is displayed. By default, we do nothing.
4005 */
4006 leavingRoom(traveler) { }
4007
4008 /*
4009 * Receive notification that a traveler is about to leave the room.
4010 * 'traveler' is the object actually traveling. In most cases this
4011 * is simply the actor; but when the actor is in a vehicle, this is
4012 * the vehicle instead.
4013 *
4014 * By default, we describe the traveler's departure if the traveler's
4015 * destination is different from its present location.
4016 */
4017 travelerLeaving(traveler, dest, connector)
4018 {
4019 /* describe the departure */
4020 if (dest != traveler.location)
4021 traveler.describeDeparture(dest, connector);
4022
4023 /* run the departure notification */
4024 leavingRoom(traveler);
4025 }
4026
4027 /*
4028 * Receive notification that a traveler is arriving in the room.
4029 * 'traveler' is the object actually traveling. In most cases this
4030 * is simply the actor; but when the actor is in a vehicle, this is
4031 * the vehicle instead.
4032 *
4033 * By default, we set each of the "motive" actors to its default
4034 * posture, then describe the arrival.
4035 */
4036 travelerArriving(traveler, origin, connector, backConnector)
4037 {
4038 /*
4039 * Set the self-motive actors into the proper default posture
4040 * for the location. We only do this for actors moving under
4041 * their own power, since actors in vehicles will presumably
4042 * just stay in the posture that's appropriate for the vehicle.
4043 */
4044 foreach (local actor in traveler.getTravelerMotiveActors)
4045 {
4046 /*
4047 * If the actor isn't in this posture already, set the actor
4048 * to the new posture. Note that we set the new posture
4049 * directly, rather than via a nested command; we don't want
4050 * the travel to consist of a NORTH plus a SIT, but simply of
4051 * a NORTH. Note that this could bypass side effects
4052 * normally associated with the SIT (or whatever), but we
4053 * assume that when a room with a specific posture is linked
4054 * directly from a separate location, the travel connector
4055 * linking up to the new room will take care of the necessary
4056 * side effects.
4057 */
4058 if (actor.posture != defaultPosture)
4059 actor.makePosture(defaultPosture);
4060 }
4061
4062 /* run the arrival notification */
4063 enteringRoom(traveler);
4064
4065 /* describe the arrival */
4066 traveler.describeArrival(origin, backConnector);
4067 }
4068
4069 /*
4070 * Receive notification of travel among nested rooms. When an actor
4071 * moves between two locations related directly by containing (such
4072 * as from a chair to the room containing the chair, or vice versa),
4073 * we first call this routine on the origin of the travel, then we
4074 * move the actor, then we call this same routine on the destination
4075 * of the travel.
4076 *
4077 * This routine is used any time an actor is moved with
4078 * travelWithin(). This is not used when an actor travels between
4079 * locations related by a TravelConnector object rather than by
4080 * direct containment.
4081 *
4082 * We do nothing by default. Locations can override this if they
4083 * wish to perform any special handling during this type of travel.
4084 */
4085 actorTravelingWithin(origin, dest)
4086 {
4087 }
4088
4089 /*
4090 * Determine if the given actor has "intrinsic" knowledge of the
4091 * destination of the given travel connector leading away from this
4092 * location. This knowledge is independent of any memory the actor
4093 * has of actual travel through the connector in the course of the
4094 * game, which we track separately via the TravelConnector's travel
4095 * memory mechanism.
4096 *
4097 * There are two main reasons an actor would have intrinsic
4098 * knowledge of a connector's destination:
4099 *
4100 * 1. The actor is supposed to be familiar with the location and its
4101 * surroundings, within the context of the game. For example, if
4102 * part of the game is the player character's own house, the PC
4103 * would probably know where all of the connections among rooms go.
4104 *
4105 * 2. The destination location is plainly visible from this location
4106 * or is clearly marked (such as with a sign). For example, if the
4107 * current location is an open field, a nearby hilltop to the east
4108 * might be visible from here, so we could see from here where we'll
4109 * end up by going east. Alternatively, if we're in a lobby, and
4110 * the passage to the west is marked with a sign reading "electrical
4111 * room," an actor would have good reason to think an electrical
4112 * room lies to the west.
4113 *
4114 * We handle case (1) automatically through our actorIsFamiliar()
4115 * method: if the actor is familiar with the location, we assume by
4116 * default that the actor knows where all of the connectors from
4117 * here go. We don't have any default handling for case (2), so
4118 * individual rooms (or subclasses) must override this method if
4119 * they want to specify intrinsic knowledge for any of their
4120 * outgoing connectors.
4121 */
4122 actorKnowsDestination(actor, conn)
4123 {
4124 /*
4125 * if the actor is familiar with this location, then the actor
4126 * by default knows where all of the outgoing connections go
4127 */
4128 if (actorIsFamiliar(actor))
4129 return true;
4130
4131 /* there's no other way the actor would know the destination */
4132 return nil;
4133 }
4134
4135 /*
4136 * Is the actor familiar with this location? In other words, is the
4137 * actor supposed to know the location well at the start of the game?
4138 *
4139 * This should return true if the actor is familiar with this
4140 * location, nil if not. By default, we return nil, since actors
4141 * are not by default familiar with any locations.
4142 *
4143 * The purpose of this routine is to determine if the actor is meant
4144 * to know the location well, within the context of the game, even
4145 * before the game starts. For example, if an area in the game is
4146 * an actor's own house, the actor would naturally be familiar,
4147 * within the context of the game, with the locations making up the
4148 * house.
4149 *
4150 * Note that this routine doesn't need to "learn" based on the
4151 * events of the game. The familiarity here is meant only to model
4152 * the actor's knowledge as of the start of the game.
4153 */
4154 actorIsFamiliar(actor) { return nil; }
4155
4156 /*
4157 * The default "you can't go that way" message for travel within this
4158 * location in directions that don't allow travel. This is shown
4159 * whenever an actor tries to travel in one of the directions we have
4160 * set to point to noTravel. A room can override this to produce a
4161 * different, customized message for unset travel directions - this
4162 * is an easy way to change the cannot-travel message for several
4163 * directions at once.
4164 *
4165 * The handling depends on whether or not it's dark. If it's dark,
4166 * we don't want to reveal whether or not it's actually possible to
4167 * perform the travel, since there's no light to see where the exits
4168 * are.
4169 */
4170 cannotTravel()
4171 {
4172 /* check for darkness */
4173 if (!gActor.isLocationLit())
4174 {
4175 /* the actor is in the dark - use our dark travel message */
4176 cannotGoThatWayInDark();
4177 }
4178 else
4179 {
4180 /* use the standard "can't go that way" routine */
4181 cannotGoThatWay();
4182 }
4183 }
4184
4185 /*
4186 * Receive notification of travel from one dark location to another.
4187 * This is called before the actor is moved from the source
4188 * location, and can cancel the travel if desired by using 'exit' to
4189 * terminate the command.
4190 *
4191 * By default, we'll simply display the same handler we do when the
4192 * player attempts travel in a direction with no travel possible in
4193 * the dark (cannotGoThatWayInDark), and then use 'exit' to cancel
4194 * the command. This default behavior provides the player with no
4195 * mapping information in the dark, since the same message is
4196 * generated whether or not travel would be possible in a given
4197 * direction were light present.
4198 */
4199 roomDarkTravel(actor)
4200 {
4201 /*
4202 * show the same message we would show if we attempted travel in
4203 * the dark in a direction with no exit
4204 */
4205 cannotGoThatWayInDark();
4206
4207 /* terminate the command */
4208 exit;
4209 }
4210
4211 /*
4212 * Show the default "you can't go that way" message for this
4213 * location. By default, we show a generic message, but individual
4214 * rooms might want to override this to provide a more specific
4215 * description of why travel isn't allowed.
4216 */
4217 cannotGoThatWay()
4218 {
4219 /* "you can't go that way" */
4220 reportFailure(cannotGoThatWayMsg);
4221
4222 /* show a list of exits, if appropriate */
4223 cannotGoShowExits(gActor);
4224 }
4225
4226 /*
4227 * The message to display when it's not possible to travel in a given
4228 * direction from this room; this is either a single-quoted string or
4229 * an actor action messages property (by default, it's the latter,
4230 * giving a default library message).
4231 */
4232 cannotGoThatWayMsg = &cannotGoThatWayMsg
4233
4234 /*
4235 * Show a version of the "you can't go that way" message for travel
4236 * while in the dark. This is called when the actor is in the dark
4237 * (i.e., there's no ambient light at the actor) and attempts to
4238 * travel in a direction that doesn't allow travel. By default, we
4239 * show a generic "you can't see where you're going in the dark"
4240 * message.
4241 *
4242 * This routine is essentially a replacement for the
4243 * cannotGoThatWay() routine that we use when the actor is in the
4244 * dark.
4245 */
4246 cannotGoThatWayInDark()
4247 {
4248 /* "it's too dark; you can't see where you're going */
4249 reportFailure(&cannotGoThatWayInDarkMsg);
4250 }
4251
4252 /*
4253 * Get preconditions for travel for an actor in this location. These
4254 * preconditions should be applied by any command that will involve
4255 * travel from this location. By default, we impose no additional
4256 * requirements.
4257 */
4258 roomTravelPreCond = []
4259
4260 /*
4261 * Get the effective location of an actor directly within me, for
4262 * the purposes of a "follow" command. To follow someone, we must
4263 * have the same effective follow location that the target had when
4264 * we last observed the target leaving.
4265 *
4266 * For most rooms, this is simply the room itself.
4267 */
4268 effectiveFollowLocation = (self)
4269
4270 /*
4271 * Dispatch the room daemon. This is a daemon routine invoked once
4272 * per turn; we in turn invoke roomDaemon on the current player
4273 * character's current location.
4274 */
4275 dispatchRoomDaemon()
4276 {
4277 /* call roomDaemon on the player character's location */
4278 if (gPlayerChar.location != nil)
4279 gPlayerChar.location.roomDaemon();
4280 }
4281 ;
4282
4283 /* ------------------------------------------------------------------------ */
4284 /*
4285 * Room: the basic class for top-level game locations (that is, game
4286 * locations that aren't inside any other simulation objects, but are at
4287 * the top level of the containment hierarchy). This is the smallest
4288 * unit of movement; we do not distinguish among locations within a
4289 * room, even if a Room represents a physically large location. If it
4290 * is necessary to distinguish among different locations in a large
4291 * physical room, simply divide the physical room into sections and
4292 * represent each section with a separate Room object.
4293 *
4294 * A Room is not necessarily indoors; it is simply a location where an
4295 * actor can be located. This peculiar usage of "room" to denote any
4296 * atomic location, even outdoors, was adopted by the authors of the
4297 * earliest adventure games, and has been the convention ever since.
4298 *
4299 * A room's contents are the objects contained directly within the room.
4300 * These include fixed features of the room as well as loose items in
4301 * the room, which are effectively "on the floor" in the room.
4302 *
4303 * The Room class implements the Travel Connector interface in such a
4304 * way that travel from one room to another can be established simply by
4305 * setting a direction property (north, south, etc) in the origin room
4306 * to point to the destination room. This type of travel link has no
4307 * side effects and is unconditional.
4308 *
4309 * A room is by default an indoor location; this means that it contains
4310 * walls, floor, and ceiling. An outdoor location should be based on
4311 * OutdoorRoom rather than Room.
4312 */
4313 class Room: Fixture, BasicLocation, RoomAutoConnector
4314 /*
4315 * Initialize
4316 */
4317 initializeThing()
4318 {
4319 /* inherit default handling */
4320 inherited();
4321
4322 /*
4323 * Add my room parts to my contents list. Only include room
4324 * parts that don't have explicit locations.
4325 */
4326 contents += roomParts.subset({x: x.location == nil});
4327 }
4328
4329 /*
4330 * we're a "top-level" location: we don't have any other object
4331 * containing us, but we're nonetheless part of the game world, so
4332 * we're at the top level of the containment tree
4333 */
4334 isTopLevel = true
4335
4336 /*
4337 * we generally do not want rooms to be included when a command
4338 * refers to 'all'
4339 */
4340 hideFromAll(action) { return true; }
4341
4342 /* don't consider myself a default for STAND ON, SIT ON, or LIE ON */
4343 hideFromDefault(action)
4344 {
4345 /* don't hide from STAND ON, SIT ON, LIE ON */
4346 if (action.ofKind(StandOnAction)
4347 || action.ofKind(SitOnAction)
4348 || action.ofKind(LieOnAction))
4349 return nil;
4350
4351 /* don't hide from defaults for other actions, though */
4352 return inherited(action);
4353 }
4354
4355
4356 /*
4357 * Most rooms provide their own implicit lighting. We'll use
4358 * 'medium' lighting (level 3) by default, which provides enough
4359 * light for all activities, but is reduced to dim light (level 2)
4360 * when it goes through obscuring media or over distance.
4361 */
4362 brightness = 3
4363
4364 /*
4365 * Get my "destination name," as seen by the given actor from the
4366 * given origin location. This gives the name we can use to
4367 * describe this location from the perspective of an actor in an
4368 * adjoining location looking at a travel connector from that
4369 * location to here.
4370 *
4371 * By default, we simply return our destName property. This default
4372 * behavior can be overridden if it's necessary for a location to
4373 * have different destination names in different adjoining
4374 * locations, or when seen by different actors.
4375 *
4376 * If this location's name cannot or should not be described from an
4377 * adjoining location, this should simply return nil.
4378 */
4379 getDestName(actor, origin) { return destName; }
4380
4381 /*
4382 * Our destination name, if we have one. By default, we make this
4383 * nil, which means the room cannot be described as a destination of
4384 * connectors from adjoining locations.
4385 */
4386 destName = nil
4387
4388 /*
4389 * My "atmosphere" list. This can be set to an EventList object to
4390 * provide atmosphere messages while the player character is within
4391 * this room. The default roomDaemon will show one message from this
4392 * EventList (by calling the EventList's doScript() method) on each
4393 * turn the player character is in this location.
4394 */
4395 atmosphereList = nil
4396
4397 /*
4398 * Room daemon - this is invoked on the player character's immediate
4399 * location once per turn in a daemon.
4400 */
4401 roomDaemon()
4402 {
4403 /*
4404 * if we have an atmosphere message list, display the next
4405 * message
4406 */
4407 if (atmosphereList != nil)
4408 {
4409 /* show visual separation, then the current atmosphere message */
4410 "<.commandsep>";
4411 atmosphereList.doScript();
4412 }
4413 }
4414
4415 /*
4416 * The nominal drop destination - this is the location where we
4417 * describe objects as being when they're actually directly within
4418 * the room.
4419 *
4420 * By default, we return the object representing the room's floor.
4421 * If there's no floor, we simply return 'self'.
4422 */
4423 getNominalDropDestination()
4424 {
4425 local floor;
4426
4427 /*
4428 * if there's a floor, it's the nominal drop destination;
4429 * otherwise, just indicate that our contents are in 'self'
4430 */
4431 return ((floor = roomFloor) != nil ? floor : self);
4432 }
4433
4434 /*
4435 * Since we could be our own nominal drop destination, we need a
4436 * message to describe things being put here.
4437 */
4438 putDestMessage = &putDestRoom
4439
4440 /*
4441 * The nominal actor container. By default, this is the room's
4442 * nominal drop destination, which is usually the floor or
4443 * equivalent.
4444 */
4445 getNominalActorContainer(posture) { return getNominalDropDestination(); }
4446
4447 /*
4448 * move something into a room is accomplished by putting the object
4449 * on the floor
4450 */
4451 tryMovingObjInto(obj)
4452 {
4453 local floor;
4454
4455 /* if we have a floor, put the object there */
4456 if ((floor = roomFloor) != nil)
4457 return tryImplicitAction(PutOn, obj, floor);
4458 else
4459 return nil;
4460 }
4461
4462 /* explain that something must be in the room first */
4463 mustMoveObjInto(obj)
4464 {
4465 local floor;
4466
4467 /* if we have a floor, say that the object has to go there */
4468 if ((floor = roomFloor) != nil)
4469 reportFailure(&mustBeInMsg, obj, floor);
4470 else
4471 inherited(obj);
4472 }
4473
4474 /*
4475 * Get the apparent location of one of our room parts.
4476 *
4477 * In most cases, we use generic objects (defaultFloor,
4478 * defaultNorthWall, etc.) for the room parts. There's only one
4479 * instance of each of these generic objects in the whole game -
4480 * there's only one floor, one north wall, and so on - so these
4481 * instances can't have specific locations the way normal objects do.
4482 * Thus the need for this method: this tells us the *apparent*
4483 * location of one of the room part objects as perceived from this
4484 * room.
4485 *
4486 * If the part isn't in this location, we'll return nil.
4487 */
4488 getRoomPartLocation(part)
4489 {
4490 local loc;
4491
4492 /*
4493 * if this part is in our part list, then its apparent location
4494 * is 'self'
4495 */
4496 if (roomParts.indexOf(part) != nil)
4497 return self;
4498
4499 /*
4500 * if the room part has an explicit location itself, and that
4501 * location is either 'self' or is in 'self', return the
4502 * location
4503 */
4504 if (part != nil
4505 && (loc = part.location) != nil
4506 && (loc == self || loc.isIn(self)))
4507 return loc;
4508
4509 /* we don't have the part */
4510 return nil;
4511 }
4512
4513 /*
4514 * Get the list of extra parts for the room. An indoor location has
4515 * walls, floor, and ceiling; these are all generic objects that are
4516 * included for completeness only.
4517 *
4518 * A room with special walls, floor, or ceiling should override this
4519 * to eliminate the default objects from appearing in the room.
4520 * Note that if the room has a floor, it should always be
4521 * represented by an object of class Floor, and should always be
4522 * part of this list.
4523 */
4524 roomParts = [defaultFloor, defaultCeiling,
4525 defaultNorthWall, defaultSouthWall,
4526 defaultEastWall, defaultWestWall]
4527
4528 /*
4529 * Get the room's floor. This looks for an object of class Floor in
4530 * the roomParts list; if there is no such object, we'll return nil,
4531 * indicating that the room has no floor at all.
4532 */
4533 roomFloor = (roomParts.valWhich({x: x.ofKind(Floor)}))
4534
4535 /*
4536 * Get any extra items in scope in this location. These are items
4537 * that are to be in scope even if they're not reachable through any
4538 * of the normal sense paths (so they'll be in scope even in the
4539 * dark, for example).
4540 *
4541 * By default, if we have a floor, and the actor is directly in this
4542 * room, we return our floor: this is because an actor is presumed to
4543 * be in physical contact with the floor whenever directly in the
4544 * room, and thus the actor would be aware that there's a floor
4545 * there, even if the actor can't see the floor. In some rooms, it's
4546 * desirable to have certain objects in scope because they're
4547 * essential features of the room; for example, a location that is
4548 * part of a stairway would probably have the stairs in scope by
4549 * virtue of the actor standing on them.
4550 */
4551 getExtraScopeItems(actor)
4552 {
4553 local floor;
4554
4555 /*
4556 * if we have a floor, and the actor is in this room, explicitly
4557 * make the floor in scope
4558 */
4559 if ((floor = roomFloor) != nil && actor.isDirectlyIn(self))
4560 return [floor];
4561 else
4562 return [];
4563 }
4564
4565 /*
4566 * When we're in the room, treat EXAMINE <ROOM> the same as LOOK
4567 * AROUND. (This will only work if the room is given vocabulary
4568 * like a normal object.)
4569 */
4570 dobjFor(Examine)
4571 {
4572 verify()
4573 {
4574 /*
4575 * When we're in the room, downgrade the likelihood a bit, as
4576 * we'd rather inspect an object within the room if there's
4577 * something with the same name. When we're *not* in the
4578 * room - meaning this is a remote room that's visible from
4579 * the player's location - downgrade the likelihood even
4580 * more, since we're more likely to want to examine the room
4581 * we're actually in than a remote room with the same name.
4582 */
4583 if (gActor.isIn(self))
4584 logicalRank(80, 'x room');
4585 else
4586 logicalRank(70, 'x room');
4587 }
4588
4589 action()
4590 {
4591 /*
4592 * if the looker is within the room, replace EXAMINE <self>
4593 * with LOOK AROUND; otherwise, use the normal description
4594 */
4595 if (gActor.isIn(self) && gActor.canSee(self))
4596 gActor.lookAround(LookRoomDesc | LookListSpecials
4597 | LookListPortables);
4598 else
4599 inherited();
4600 }
4601 }
4602
4603 /* treat LOOK IN <room> as EXAMINE <room> */
4604 dobjFor(LookIn) remapTo(Examine, self)
4605
4606 /* LOOK UNDER and BEHIND are illogical */
4607 dobjFor(LookUnder) { verify { illogical(&cannotLookUnderMsg); } }
4608 dobjFor(LookBehind) { verify { illogical(&cannotLookBehindMsg); } }
4609
4610 /* treat SMELL/LISTEN TO <room> as just SMELL/LISTEN */
4611 dobjFor(Smell) remapTo(SmellImplicit)
4612 dobjFor(ListenTo) remapTo(ListenImplicit)
4613
4614 /* map STAND/SIT/LIE ON <room> to my default floor */
4615 dobjFor(StandOn) maybeRemapTo(roomFloor != nil, StandOn, roomFloor)
4616 dobjFor(SitOn) maybeRemapTo(roomFloor != nil, SitOn, roomFloor)
4617 dobjFor(LieOn) maybeRemapTo(roomFloor != nil, LieOn, roomFloor)
4618
4619 /*
4620 * treat an explicit GET OUT OF <ROOM> as OUT if there's an apparent
4621 * destination for OUT; otherwise treat it as "vague travel," which
4622 * simply tells the player that they need to specify a direction
4623 */
4624 dobjFor(GetOutOf)
4625 {
4626 remap()
4627 {
4628 /* remap only if this isn't an implied action */
4629 if (gAction.parentAction == nil)
4630 {
4631 /*
4632 * if we have an apparent Out connection, go there;
4633 * otherwise it's not obvious where we're meant to go
4634 */
4635 if (out != nil && out.isConnectorApparent(self, gActor))
4636 return [OutAction];
4637 else
4638 return [VagueTravelAction];
4639 }
4640
4641 /* don't remap here - use the standard handling */
4642 return inherited();
4643 }
4644 }
4645
4646 /*
4647 * for BOARD and ENTER, there are three possibilities:
4648 *
4649 * - we're already directly in this room, in which case it's
4650 * illogical to travel here again
4651 *
4652 * - we're in a nested room within this room, in which case ENTER
4653 * <self> is the same as GET OUT OF <outermost nested room within
4654 * self>
4655 *
4656 * - we're in a separate top-level room that's connected by a sense
4657 * connector, in which case ENTER <self> should be handled as TRAVEL
4658 * VIA <connector from actor's current location to self>
4659 */
4660 dobjFor(Board) asDobjFor(Enter)
4661 dobjFor(Enter)
4662 {
4663 verify()
4664 {
4665 /*
4666 * if we're already here, entering the same location is
4667 * redundant; if we're not in a nested room, we need a travel
4668 * connector to get there from here
4669 */
4670 if (gActor.isDirectlyIn(self))
4671 illogicalAlready(&alreadyInLocMsg);
4672 else if (!gActor.isIn(self)
4673 && gActor.location.getConnectorTo(gActor, self) == nil)
4674 illogicalNow(&whereToGoMsg);
4675 }
4676 preCond()
4677 {
4678 /*
4679 * if we're in a different top-level room, and there's a
4680 * travel connector, we'll simply travel via the connector,
4681 * so we don't need to impose any pre-conditions of our own
4682 */
4683 if (!gActor.isIn(self)
4684 && gActor.location.getConnectorTo(gActor, self) != nil)
4685 return [];
4686
4687 /*
4688 * if we're in a nested room within this object, we'll
4689 * replace the action with "get out of <outermost nested
4690 * room>", so there's no need for any extra preconditions
4691 * here
4692 */
4693 if (gActor.isIn(self) && !gActor.isDirectlyIn(self))
4694 return [];
4695
4696 /* otherwise, use the default conditions */
4697 return inherited();
4698 }
4699 action()
4700 {
4701 /*
4702 * if we're in a nested room, get out; otherwise travel via a
4703 * suitable travel connector
4704 */
4705 if (gActor.isIn(self) && !gActor.isDirectlyIn(self))
4706 {
4707 /*
4708 * get out of the *outermost* nested room - that is, our
4709 * direct child that contains the actor
4710 */
4711 local chi = contents.valWhich({x: gActor.isIn(x)});
4712
4713 /* if we found it, get out of it */
4714 if (chi != nil)
4715 replaceAction(GetOutOf, chi);
4716 }
4717 else
4718 {
4719 /* get the connector from here to there */
4720 local conn = gActor.location.getConnectorTo(gActor, self);
4721
4722 /* if we found it, go that way */
4723 if (conn != nil)
4724 replaceAction(TravelVia, conn);
4725 }
4726
4727 /*
4728 * if we didn't replace the action yet, we can't figure out
4729 * how to get here from the actor's current location
4730 */
4731 reportFailure(&whereToGoMsg);
4732 }
4733 }
4734 ;
4735
4736 /*
4737 * A dark room, which provides no light of its own
4738 */
4739 class DarkRoom: Room
4740 /*
4741 * turn off the lights
4742 */
4743 brightness = 0
4744 ;
4745
4746 /*
4747 * An outdoor location. This differs from an indoor location in that it
4748 * has ground and sky rather than floor and ceiling, and has no walls.
4749 */
4750 class OutdoorRoom: Room
4751 /* an outdoor room has ground and sky, but no walls */
4752 roomParts = [defaultGround, defaultSky]
4753 ;
4754
4755 /*
4756 * A shipboard room. This is a simple mix-in class: it can be used
4757 * along with any type of Room to indicate that this room is aboard a
4758 * ship. When a room is aboard a ship, the shipboard travel directions
4759 * (port, starboard, fore, aft) are allowed; these directions normally
4760 * make no sense.
4761 *
4762 * This is a mix-in class rather than a Room subclass to allow it to be
4763 * used in conjunction with any other Room subclass. To make a room
4764 * shipboard, simply declare your room like this:
4765 *
4766 * mainDeck: Shipboard, Room // etc
4767 */
4768 class Shipboard: object
4769 /* mark the location as being aboard ship */
4770 isShipboard = true
4771 ;
4772
4773 /*
4774 * For convenience, we define ShipboardRoom as a shipboard version of the
4775 * basic Room type.
4776 */
4777 class ShipboardRoom: Shipboard, Room
4778 ;
4779
4780 /* ------------------------------------------------------------------------ */
4781 /*
4782 * Make a room "floorless." This is a mix-in class that you can include
4783 * in a superclass list ahead of Room or any of its subclasses to create
4784 * a room where support is provided by some means other than standing on
4785 * a surface, or where there's simply no support. Examples: hanging on a
4786 * rope over a chasm; climbing a ladder; in free-fall after jumping out
4787 * of a plane; levitating in mid-air.
4788 *
4789 * There are two main special features of a floorless room. First, and
4790 * most obviously, there's no "floor" or "ground" object among the room
4791 * parts. We accomplish this by simply subtracting out any object of
4792 * class Floor from the room parts list inherited from the combined base
4793 * room class.
4794 *
4795 * Second, there's no place to put anything down, so objects dropped here
4796 * either disappear from the game or are transported to another location
4797 * (the room at the bottom of the chasm, for example).
4798 */
4799 class Floorless: object
4800 /*
4801 * Omit the default floor/ground objects from the room parts list.
4802 * Room classes generally have static room parts lists, so calculate
4803 * this once per instance and store the results.
4804 *
4805 * NOTE - if you combine Floorless with a base Room class that has a
4806 * dynamic room parts list, you'll need to override this to calculate
4807 * the subset dynamically on each invocation.
4808 */
4809 roomParts = perInstance(inherited().subset({x: !x.ofKind(Floor)}))
4810
4811 /*
4812 * The room below, if any - this is where objects dropped here will
4813 * actually end up. By default, this is nil, which means that
4814 * objects dropped here simply disappear from the game. If there's a
4815 * "bottom of chasm" location where dropped objects should land,
4816 * provide it here.
4817 */
4818 bottomRoom = nil
4819
4820 /* receive a dropped object */
4821 receiveDrop(obj, desc)
4822 {
4823 /*
4824 * move the dropped object to the room at the bottom of whatever
4825 * it is we're suspended over; if there is no bottom room, we'll
4826 * simply remove the dropped object from the game
4827 */
4828 obj.moveInto(bottomRoom);
4829
4830 /*
4831 * Say that the object drops out of sight below. Build this by
4832 * combining the generic report prefix from the drop descriptor
4833 * with our generic suffix, which describes the object as
4834 * vanishing below.
4835 */
4836 mainReport(desc.getReportPrefix(obj, self)
4837 + gActor.getActionMessageObj().floorlessDropMsg(obj));
4838 }
4839 ;
4840
4841 /*
4842 * For convenience, provide a combination of Floorless with the ordinary
4843 * Room.
4844 */
4845 class FloorlessRoom: Floorless, Room;
4846
4847 /* ------------------------------------------------------------------------ */
4848 /*
4849 * Room Part - base class for "parts" of rooms, such as floors and walls.
4850 * Room parts are unusual in a couple of ways.
4851 *
4852 * First, room parts are frequently re-used widely throughout a game. We
4853 * define a single instance of each of several parts that are found in
4854 * typical rooms, and then re-use those instances in all rooms with those
4855 * parts. For example, we define one "default floor" object, and then
4856 * use that object in most or all rooms that have floors. We do this for
4857 * efficiency, to avoid creating hundreds of essentially identical copies
4858 * of the common parts.
4859 *
4860 * Second, because room parts are designed to be re-used, things that are
4861 * in or on the parts are actually represented in the containment model
4862 * as being directly in their containing rooms. For example, an object
4863 * that is said to be "on the floor" actually has its 'location' property
4864 * set to its immediate room container, and the 'contents' list that
4865 * contains the object is that of the room, not of the floor object. We
4866 * must therefore override some of the normal handling for object
4867 * locations within room parts, in order to make it appear (for the
4868 * purposes of command input and descriptive messages) that the things
4869 * are in/on their room parts, even though they're not really represented
4870 * that way in the containment model.
4871 */
4872 class RoomPart: Fixture
4873 /*
4874 * When we explicitly examine a RoomPart, list any object that's
4875 * nominally contained in the room part, as long it doesn't have a
4876 * special description for the purposes of the room part. (If it
4877 * does have a special description, then examining the room part will
4878 * automatically display that special desc, so we don't want to
4879 * include the object in a separate list of miscellaneous contents of
4880 * the room part.)
4881 */
4882 isObjListedInRoomPart(obj)
4883 {
4884 /*
4885 * list the object *unless* it has a special description for the
4886 * purposes of examining this room part
4887 */
4888 return !obj.useSpecialDescInRoomPart(self);
4889 }
4890
4891 /*
4892 * Add this room part to the given room.
4893 *
4894 * Room parts don't have normal "location" properties. Instead, a
4895 * room part explicitly appears in the "roomParts" list of each room
4896 * that contains it. For the most part, room parts are static -
4897 * they're initialized in the room definitions and never changed.
4898 * However, if you need to dynamically add a room part to a room
4899 * during the game, you can do so using this method.
4900 */
4901 moveIntoAdd(room)
4902 {
4903 /* add me to the room's 'roomParts' and 'contents' lists */
4904 room.roomParts += self;
4905 room.contents += self;
4906 }
4907
4908 /*
4909 * Remove this room part from the given room. This can be used if
4910 * it's necessary to remove the room part dynamically from a room.
4911 */
4912 moveOutOf(room)
4913 {
4914 /* remove me from the room's 'roomParts' and 'contents' lists */
4915 room.roomParts -= self;
4916 room.contents -= self;
4917 }
4918
4919 /*
4920 * Don't include room parts in 'all'. Room parts are so ubiquitous
4921 * that we never want to assume that they're involved in a command
4922 * except when it is specifically so stated.
4923 */
4924 hideFromAll(action) { return true; }
4925
4926 /* do allow use as a default, though */
4927 hideFromDefault(action) { return nil; }
4928
4929 /*
4930 * When multiple room parts show up in a resolve list, and some of
4931 * the parts are local to the actor's immediate location and others
4932 * aren't, keep only the local ones. This helps avoid pointless
4933 * ambiguity in cases where two (or more) top-level locations are
4934 * linked with a sense connector, and one or the other location has
4935 * custom room part objects.
4936 */
4937 filterResolveList(lst, action, whichObj, np, requiredNum)
4938 {
4939 /* if a definite number of objects is required, check ambiguity */
4940 if (requiredNum != nil)
4941 {
4942 /* get the subset that's just RoomParts */
4943 local partLst = lst.subset({x: x.obj_.ofKind(RoomPart)});
4944
4945 /*
4946 * get the *remote* subset - this is the subset that's not in
4947 * the outermost room of the target actor
4948 */
4949 local outer = action.actor_.getOutermostRoom();
4950 local remoteLst = partLst.subset({x: !x.obj_.isIn(outer)});
4951
4952 /*
4953 * If all of the objects are remote, or all of them are
4954 * local, we can't narrow things down on this basis; but if
4955 * we found some remote and some local, eliminate the remote
4956 * items, since we want to favor the local ones
4957 */
4958 if (remoteLst.length() not in (0, partLst.length()))
4959 lst -= remoteLst;
4960 }
4961
4962 /* now do any inherited work, and return the result */
4963 return inherited(lst, action, whichObj, np, requiredNum);
4964 }
4965
4966 /*
4967 * Since room parts are generally things like walls and floors that
4968 * enclose the entire room, they're typically visually large, and
4969 * tend to have fairly large-scale details (such as doors and
4970 * windows). So, by default we set the sightSize to 'large' so that
4971 * the details are visible at a distance.
4972 */
4973 sightSize = large
4974
4975 /*
4976 * as with decorations, downgrade the likelihood for Examine, as the
4977 * standard walls, floors, etc. are pretty much background noise
4978 * that are just here in case someone wants to refer to them
4979 * explicitly
4980 */
4981 dobjFor(Examine)
4982 {
4983 verify()
4984 {
4985 inherited();
4986 logicalRank(70, 'x decoration');
4987 }
4988 }
4989
4990 /* describe the status - shows the things that are in/on the part */
4991 examineStatus()
4992 {
4993 /* show the contents of the room part */
4994 examinePartContents(&descContentsLister);
4995 }
4996
4997 /* show our contents */
4998 examinePartContents(listerProp)
4999 {
5000 /*
5001 * Get my location, as perceived by the actor - this is the room
5002 * that contains this part. If I don't have a location as
5003 * perceived by the actor, then we can't show any contents.
5004 */
5005 local loc = gActor.location.getRoomPartLocation(self);
5006 if (loc == nil)
5007 return;
5008
5009 /*
5010 * create a copy of the lister customized for this part, if we
5011 * haven't already done so
5012 */
5013 if (self.(listerProp).part_ == nil)
5014 {
5015 self.(listerProp) = self.(listerProp).createClone();
5016 self.(listerProp).part_ = self;
5017 }
5018
5019 /* show the contents of the containing location */
5020 self.(listerProp).showList(gActor, self, loc.contents, 0, 0,
5021 gActor.visibleInfoTable(), nil);
5022 }
5023
5024 /*
5025 * show our special contents - this shows objects with special
5026 * descriptions that are specifically in this room part
5027 */
5028 examineSpecialContents()
5029 {
5030 local infoTab;
5031 local lst;
5032
5033 /* get the actor's list of visible items */
5034 infoTab = gActor.visibleInfoTable();
5035
5036 /*
5037 * get the list of special description items, using only the
5038 * subset that uses special descriptions in this room part
5039 */
5040 lst = specialDescList(infoTab,
5041 {obj: obj.useSpecialDescInRoomPart(self)});
5042
5043 /* show the list */
5044 specialContentsLister.showList(gActor, nil, lst, 0, 0, infoTab, nil);
5045 }
5046
5047 /*
5048 * Get the destination for a thrown object that hits me. Since we
5049 * don't have a real location, we must ask the actor for our room
5050 * part location, and then use its hit-and-fall destination.
5051 */
5052 getHitFallDestination(thrownObj, path)
5053 {
5054 local loc;
5055 local dest;
5056
5057 /*
5058 * if we have an explicit location, start with it; otherwise, ask
5059 * the actor's location to find us; if we can't even find
5060 * ourselves there, just use the actor's current location
5061 */
5062 if ((loc = location) == nil
5063 && (loc = gActor.location.getRoomPartLocation(self)) == nil)
5064 loc = gActor.location;
5065
5066 /* use the location's drop destination for thrown objects */
5067 dest = loc.getDropDestination(thrownObj, path);
5068
5069 /* give the destination a chance to make adjustments */
5070 return dest.adjustThrowDestination(thrownObj, path);
5071 }
5072
5073 /* consider me to be in any room of which I'm a part */
5074 isIn(loc)
5075 {
5076 local rpl;
5077
5078 /*
5079 * get the room-part location of this room part, from the
5080 * perspective of the prospective location we're asking about
5081 */
5082 if (loc != nil && (rpl = loc.getRoomPartLocation(self)) != nil)
5083 {
5084 /*
5085 * We indeed have a room part location in the given
5086 * location, so we're a part of the overall location. We
5087 * might be directly in the location (i.e., 'rpl' could
5088 * equal 'loc'), or we might be somewhere relative to 'loc'
5089 * (for example, we could be within a nested room within
5090 * 'loc', in which case 'rpl' is a nested room unequal to
5091 * 'loc' but is within 'loc'). So, if 'rpl' equals 'loc' or
5092 * is contained in 'loc', I'm within 'loc', because I'm
5093 * within 'rpl'.
5094 */
5095 if (rpl == loc || rpl.isIn(loc))
5096 return true;
5097 }
5098
5099 /*
5100 * we don't appear to be in 'loc' on the basis of our special
5101 * room-part location; fall back on the inherited handling
5102 */
5103 return inherited(loc);
5104 }
5105
5106 /* our contents listers */
5107 contentsLister = roomPartContentsLister
5108 descContentsLister = roomPartDescContentsLister
5109 lookInLister = roomPartLookInLister
5110 specialContentsLister = specialDescLister
5111
5112 /* look in/on: show our contents */
5113 dobjFor(LookIn)
5114 {
5115 verify() { }
5116 action()
5117 {
5118 /* show my contents */
5119 examinePartContents(&lookInLister);
5120
5121 /* show my special contents */
5122 examineSpecialContents();
5123 }
5124 }
5125
5126 /* we can't look behind/through/under a room part by default */
5127 nothingUnderMsg = &cannotLookUnderMsg
5128 nothingBehindMsg = &cannotLookBehindMsg
5129 nothingThroughMsg = &cannotLookThroughMsg
5130
5131 /*
5132 * initialization - add myself to my location's roomPart list if I
5133 * have an explicit location
5134 */
5135 initializeThing()
5136 {
5137 /* do the normal work first */
5138 inherited();
5139
5140 /*
5141 * if I have an explicit location, and I'm not in my location's
5142 * roomPart list, add myself to the list
5143 */
5144 if (location != nil && location.roomParts.indexOf(self) == nil)
5145 location.roomParts += self;
5146 }
5147 ;
5148
5149
5150 /*
5151 * A floor for a nested room. This should be placed directly within a
5152 * nested room object if the nested room is to be described as having a
5153 * floor separate from the nested room itself. We simply remap any
5154 * commands relating to using the floor as a surface (Put On, Throw At,
5155 * Sit On, Lie On, Stand On) to the enclosing nested room.
5156 */
5157 class NestedRoomFloor: Fixture
5158 iobjFor(PutOn) remapTo(PutOn, DirectObject, location)
5159 iobjFor(ThrowAt) remapTo(ThrowAt, DirectObject, location)
5160 dobjFor(SitOn) remapTo(SitOn, location)
5161 dobjFor(LieOn) remapTo(LieOn, location)
5162 dobjFor(StandOn) remapTo(StandOn, location)
5163 ;
5164
5165 /*
5166 * Base class for the default floor and the default ground of a top-level
5167 * room. The floor and ground are where things usually go when dropped,
5168 * and they're the locations where actors within a room are normally
5169 * standing.
5170 */
5171 class Floor: RoomPart
5172 /* specifically allow me as a default for STAND ON, SIT ON, and LIE ON */
5173 hideFromDefault(action)
5174 {
5175 /* don't hide from STAND ON, SIT ON, LIE ON */
5176 if (action.ofKind(StandOnAction)
5177 || action.ofKind(SitOnAction)
5178 || action.ofKind(LieOnAction))
5179 return nil;
5180
5181 /* for other actions, use the standard handling */
5182 return inherited(action);
5183 }
5184
5185 /*
5186 * When explicitly examining a Floor object, list any objects that
5187 * are listed in the normal room description (as in LOOK AROUND). By
5188 * default, the floor is the nominal container for anything directly
5189 * in the room, so we'll normally want LOOK AROUND and LOOK AT FLOOR
5190 * to produce the same list of objects.
5191 */
5192 isObjListedInRoomPart(obj)
5193 {
5194 /* list the object if it's listed in a normal LOOK AROUND */
5195 return obj.isListed;
5196 }
5197
5198 /*
5199 * 'put x on floor' equals 'drop x'. Add a precondition that the
5200 * drop destination is the main room, since otherwise we could have
5201 * strange results if we dropped something inside a nested room.
5202 */
5203 iobjFor(PutOn)
5204 {
5205 preCond()
5206 {
5207 /*
5208 * require that this floor object itself is reachable, and
5209 * that the drop destination for the direct object is an
5210 * outermost room
5211 */
5212 return [touchObj, new ObjectPreCondition(
5213 gDobj, dropDestinationIsOuterRoom)];
5214 }
5215 verify() { }
5216 action() { replaceAction(Drop, gDobj); }
5217 }
5218
5219 /*
5220 * The message we use to describe this object prepositionally, as the
5221 * destination of a throw or drop. This should be a gLibMessages
5222 * property with the appropriate prepositional phrase. We use a
5223 * custom message specific to floor-like objects.
5224 */
5225 putDestMessage = &putDestFloor
5226
5227 /* 'throw x at floor' */
5228 iobjFor(ThrowAt)
5229 {
5230 check()
5231 {
5232 /*
5233 * If I'm reachable, suggest just putting it down instead.
5234 * We only make the suggestion, rather than automatically
5235 * treating the command as DROP, because a player explicitly
5236 * typing THROW <obj> AT FLOOR is probably attempting to
5237 * express something more violent than merely putting the
5238 * object down; the player probably is thinking in terms of
5239 * breaking the object (or the floor).
5240 */
5241 if (canBeTouchedBy(gActor))
5242 {
5243 mainReport(&shouldNotThrowAtFloorMsg);
5244 exit;
5245 }
5246 }
5247 }
5248
5249 /* is the given actor already on the floor? */
5250 isActorOnFloor(actor)
5251 {
5252 /*
5253 * the actor is on the floor if the actor is directly in the
5254 * floor's room-part-location for the actor's location
5255 */
5256 return actor.isDirectlyIn(actor.location.getRoomPartLocation(self));
5257 }
5258
5259 /* verify sitting/standing/lying on the floor */
5260 verifyEntry(newPosture, alreadyMsg)
5261 {
5262 /*
5263 * If we're already in my location, and we're in the desired
5264 * posture, this command is illogical because we're already
5265 * where they want us to end up. Otherwise, it's logical to
5266 * stand/sit/lie on the floor, but rank it low since we don't
5267 * want the floor to interfere with selecting a default if
5268 * there's anything around that's actually like a chair.
5269 *
5270 * If it's logical, note that we've verified okay for the
5271 * action. On a future pass, we might have enforced a
5272 * precondition that moved us here, at which point our work will
5273 * be done - but we don't want to complain in that case, since
5274 * it was logical from the player's perspective to carry out the
5275 * command even though we have nothing left to do.
5276 */
5277 if (gActor.posture == newPosture
5278 && isActorOnFloor(gActor)
5279 && gAction.verifiedOkay.indexOf(self) == nil)
5280 {
5281 /* we're already on the floor in the desired posture */
5282 illogicalNow(alreadyMsg);
5283 }
5284 else
5285 {
5286 /*
5287 * it's logical, but rank it low in case there's something
5288 * more special we can stand/sit/lie on
5289 */
5290 logicalRank(50, 'on floor');
5291
5292 /*
5293 * note that we've verified okay, so we don't complain on a
5294 * future pass if we discover that a precondition has
5295 * brought us into compliance with the request prematurely
5296 */
5297 gAction.verifiedOkay += self;
5298 }
5299 }
5300
5301 /* perform sitting/standing/lying on the floor */
5302 performEntry(newPosture)
5303 {
5304 /*
5305 * bring the new posture into effect; there's no need for
5306 * actually moving the actor, since the preconditions will have
5307 * moved us to our main enclosing room already
5308 */
5309 gActor.makePosture(newPosture);
5310
5311 /* report success */
5312 defaultReport(&roomOkayPostureChangeMsg, newPosture, self);
5313 }
5314
5315 /* 'stand on floor' causes actor to stand in the containing room */
5316 dobjFor(StandOn)
5317 {
5318 preCond = [touchObj,
5319 new ObjectPreCondition(gActor.location.getOutermostRoom(),
5320 actorDirectlyInRoom)]
5321 verify() { verifyEntry(standing, &alreadyStandingOnMsg); }
5322 action() { performEntry(standing); }
5323 }
5324
5325 /* 'sit on floor' causes the actor to sit in the containing room */
5326 dobjFor(SitOn)
5327 {
5328 preCond = [touchObj,
5329 new ObjectPreCondition(gActor.location.getOutermostRoom(),
5330 actorDirectlyInRoom)]
5331 verify() { verifyEntry(sitting, &alreadySittingOnMsg); }
5332 action() { performEntry(sitting); }
5333 }
5334
5335 /* 'lie on floor' causes the actor to lie down in the room */
5336 dobjFor(LieOn)
5337 {
5338 preCond = [touchObj,
5339 new ObjectPreCondition(gActor.location.getOutermostRoom(),
5340 actorDirectlyInRoom)]
5341 verify() { verifyEntry(lying, &alreadyLyingOnMsg); }
5342 action() { performEntry(lying); }
5343 }
5344
5345 /*
5346 * Mention that an actor is here, as part of a room description.
5347 * When the actor is standing, just say that the actor is here, since
5348 * it's overstating the obvious to say that the actor is standing on
5349 * the floor. For other postures, do mention the floor.
5350 */
5351 roomActorHereDesc(actor)
5352 {
5353 /*
5354 * if we're standing, just say that the actor is "here";
5355 * otherwise, say that the actor is sitting/lying/etc on self
5356 */
5357 if (actor.posture == standing)
5358 gLibMessages.roomActorHereDesc(actor);
5359 else
5360 gLibMessages.actorInRoom(actor, self);
5361 }
5362
5363 /*
5364 * Mention that an actor is here, as part of a room description.
5365 * Since a floor is a trivial part of its enclosing room, there's no
5366 * point in mentioning that we're on the floor, as that's stating the
5367 * obvious; instead, simply describe the actor as being in the
5368 * actor's actual enclosing room.
5369 */
5370 roomActorThereDesc(actor) { actor.location.roomActorThereDesc(actor); }
5371
5372 /*
5373 * Show our room name status for an actor on the floor. Since
5374 * standing on the floor is the trivial default for any room, we
5375 * won't bother mentioning it. Other postures we'll mention the same
5376 * way we would for any nested room.
5377 */
5378 roomActorStatus(actor)
5379 {
5380 if (actor.posture != standing)
5381 gLibMessages.actorInRoomStatus(actor, self);
5382 }
5383
5384 /*
5385 * Show the actor's posture here. When we're standing on the floor,
5386 * don't mention the posture, as this is too trivial a condition to
5387 * state. Otherwise, mention it as normal for a nested room.
5388 */
5389 roomActorPostureDesc(actor)
5390 {
5391 if (actor.posture != standing)
5392 gLibMessages.actorInRoomPosture(actor, self);
5393 }
5394
5395 /*
5396 * Generate an acknowledgment for a posture change here. If the
5397 * actor is standing, just say "okay, you're now standing" without
5398 * mentioning the floor, since standing on the floor is the trivial
5399 * default. For other postures, say that we're sitting/lying/etc on
5400 * the floor.
5401 */
5402 roomOkayPostureChange(actor)
5403 {
5404 if (actor.posture == standing)
5405 defaultReport(&okayPostureChangeMsg, standing);
5406 else
5407 defaultReport(&roomOkayPostureChangeMsg, actor.posture, self);
5408 }
5409
5410 /*
5411 * mention the actor as part of the EXAMINE description of a nested
5412 * room containing the actor
5413 */
5414 roomListActorPosture(actor)
5415 {
5416 /*
5417 * Since standing is the default posture for an actor, and since
5418 * the floor (or equivalent) is the default place to be standing,
5419 * don't bother mentioning actors standing on a floor.
5420 * Otherwise, mention that the actor is sitting/lying/etc here.
5421 */
5422 if (actor.posture != standing)
5423 gLibMessages.actorInRoom(actor, self);
5424 }
5425
5426 /*
5427 * Prefix and suffix messages for listing a group of actors
5428 * nominally on the this floor. Actors are said to be on the floor
5429 * when they're really in the location containing the floor.
5430 *
5431 * If we're talking about a remote location, simply describe it as
5432 * the location rather than mentioning the floor, since the floor is
5433 * a trivial part of the remote location not worth mentioning.
5434 *
5435 * If we're local, and we're standing, we'll simply say that we're
5436 * "standing here"; again, saying that we're standing on the floor
5437 * is stating the obvious. If we're not standing, we will mention
5438 * that we're on the floor.
5439 */
5440 actorInGroupPrefix(pov, posture, remote, lst)
5441 {
5442 if (remote != nil)
5443 gLibMessages.actorThereGroupPrefix(pov, posture, remote, lst);
5444 else if (posture == standing)
5445 gLibMessages.actorHereGroupPrefix(posture, lst);
5446 else
5447 gLibMessages.actorInGroupPrefix(posture, self, lst);
5448 }
5449 actorInGroupSuffix(pov, posture, remote, lst)
5450 {
5451 if (remote != nil)
5452 gLibMessages.actorThereGroupSuffix(pov, posture, remote, lst);
5453 else if (posture == standing)
5454 gLibMessages.actorHereGroupSuffix(posture, lst);
5455 else
5456 gLibMessages.actorInGroupSuffix(posture, self, lst);
5457 }
5458 ;
5459
5460 /* ------------------------------------------------------------------------ */
5461 /*
5462 * Define the default room parts.
5463 */
5464
5465 /*
5466 * the default floor, for indoor locations
5467 */
5468 defaultFloor: Floor
5469 ;
5470
5471 /*
5472 * the default ceiling, for indoor locations
5473 */
5474 defaultCeiling: RoomPart
5475 ;
5476
5477 /*
5478 * The default walls, for indoor locations. We provide a north, south,
5479 * east, and west wall in each indoor location by default.
5480 */
5481 class DefaultWall: RoomPart
5482 ;
5483
5484 defaultNorthWall: DefaultWall
5485 ;
5486
5487 defaultSouthWall: DefaultWall
5488 ;
5489
5490 defaultEastWall: DefaultWall
5491 ;
5492
5493 defaultWestWall: DefaultWall
5494 ;
5495
5496 /*
5497 * the default sky, for outdoor locations
5498 */
5499 defaultSky: Distant, RoomPart
5500 ;
5501
5502 /*
5503 * The default ground, for outdoor locations.
5504 */
5505 defaultGround: Floor
5506 ;
5507
5508 /* ------------------------------------------------------------------------ */
5509 /*
5510 * A "room part item" is an object that's specially described as being
5511 * part of, or attached to, a RoomPart (a wall, ceiling, floor, or the
5512 * like). This is a mix-in class that can be combined with any ordinary
5513 * object class (but usually with something non-portable, such as a
5514 * Fixture or Immovable). The effect of adding RoomPartItem to an
5515 * object's superclasses is that a command like EXAMINE EAST WALL (or
5516 * whichever room part the object is associated with) will display the
5517 * object's specialDesc, but a simple LOOK will not. This class is
5518 * sometimes useful for things like doors, windows, ceiling fans, and
5519 * other things attached to the room.
5520 *
5521 * Note that this is a mix-in class, so you should always combine it with
5522 * a regular Thing-based class.
5523 *
5524 * When using this class, you should define two properties in the object:
5525 * specialNominalRoomPartLocation, which you should set to the RoomPart
5526 * (such as a wall) where the object should be described; and
5527 * specialDesc, which is the description to show when the room part is
5528 * examined. Alternatively (or in addition), you can define
5529 * initNominalRoomPartLocation and initSpecialDesc - these work the same
5530 * way, but will only be in effect until the object is moved.
5531 */
5532 class RoomPartItem: object
5533 /*
5534 * show our special description when examining our associated room
5535 * part, as long as we actually define a special description
5536 */
5537 useSpecialDescInRoomPart(part)
5538 {
5539 /* only show the special description in our associated room part */
5540 if (!isNominallyInRoomPart(part))
5541 return nil;
5542
5543 /*
5544 * if we define an initial special description, and this is our
5545 * nominal room part for that description, use it
5546 */
5547 if (isInInitState
5548 && propType(&initSpecialDesc) != TypeNil
5549 && initNominalRoomPartLocation == part)
5550 return true;
5551
5552 /* likewise for our specialDesc */
5553 if (propType(&specialDesc) != TypeNil
5554 && specialNominalRoomPartLocation == part)
5555 return true;
5556
5557 /* otherwise, don't use the special description */
5558 return nil;
5559 }
5560
5561 /*
5562 * don't use the special description in room descriptions, or in
5563 * examining any other container
5564 */
5565 useSpecialDescInRoom(room) { return nil; }
5566 useSpecialDescInContents(cont) { return nil; }
5567 ;
5568
5569 /* ------------------------------------------------------------------------ */
5570 /*
5571 * A Nested Room is any object that isn't a room but which can contain
5572 * an actor: chairs, beds, platforms, vehicles, and the like.
5573 *
5574 * An important property of nested rooms is that they're not
5575 * full-fledged rooms for the purposes of actor arrivals and departures.
5576 * Specifically, an actor moving from a room to a nested room within the
5577 * room does not trigger an actor.travelTo invocation, but simply moves
5578 * the actor from the containing room to the nested room. Moving from
5579 * the nested room to the containing room likewise triggers no
5580 * actor.travelTo invocation. The travelTo method is not applicable for
5581 * intra-room travel because no TravelConnector objects are traversed in
5582 * such travel; we simply move in and out of contained objects. To
5583 * mitigate this loss of notification, we instead call
5584 * actor.travelWithin() when moving among nested locations.
5585 *
5586 * By default, an actor attempting to travel from a nested location via
5587 * a directional command will simply attempt the travel as though the
5588 * actor were in the enclosing location.
5589 */
5590 class NestedRoom: BasicLocation
5591 /*
5592 * Our interior room name. This is the status line name we display
5593 * when an actor is within this object and can't see out to the
5594 * enclosing room. Since we can't rely on the enclosing room's
5595 * status line name if we can't see the enclosing room, we must
5596 * provide one of our own.
5597 *
5598 * By default, we'll use our regular name.
5599 */
5600 roomName = (name)
5601
5602 /*
5603 * Show our interior room description. We use this to generate the
5604 * long "look" description for the room when an actor is within the
5605 * room and cannot see the enclosing room.
5606 *
5607 * Note that this is used ONLY when the actor cannot see the
5608 * enclosing room - when the enclosing room is visible (because the
5609 * nested room is something like a chair that doesn't enclose the
5610 * actor, or can enclose the actor but is open or transparent), then
5611 * we'll simply use the description of the enclosing room instead,
5612 * adding a note to the short name shown at the start of the room
5613 * description indicating that the actor is in the nested room.
5614 *
5615 * By default, we'll show the appropriate "actor here" description
5616 * for the posture, so we'll say something like "You are sitting on
5617 * the red chair" or "You are in the phone booth." Instances can
5618 * override this to customize the description with something more
5619 * detailed, if desired.
5620 */
5621 roomDesc
5622 {
5623 local pov = getPOVActorDefault(gActor);
5624 pov.listActorPosture(pov);
5625 }
5626
5627 /*
5628 * The maximum bulk the room can hold. We'll define this to a large
5629 * number by default so that bulk isn't a concern.
5630 *
5631 * Lower numbers here can be used, for example, to limit the seating
5632 * capacity of a chair.
5633 */
5634 bulkCapacity = 10000
5635
5636 /*
5637 * Check for ownership. For a nested room, an actor can be taken to
5638 * own the nested room by virtue of being inside the room - the
5639 * chair Bob is sitting in can be called "bob's chair".
5640 *
5641 * If we don't have an explicit owner, and the potential owner 'obj'
5642 * is in me and can own me, we'll report that 'obj' does in fact own
5643 * me. Otherwise, we'll defer to the inherited implementation.
5644 */
5645 isOwnedBy(obj)
5646 {
5647 /*
5648 * if we're not explicitly owned, and 'obj' can own me, and
5649 * 'obj' is inside me, consider us owned by 'obj'
5650 */
5651 if (owner == nil && obj.isIn(self) && obj.canOwn(self))
5652 return true;
5653
5654 /* defer to the inherited definition of ownership */
5655 return inherited(obj);
5656 }
5657
5658 /*
5659 * Get the extra scope items within this room. Normally, the
5660 * immediately enclosing nested room is in scope for an actor in the
5661 * room. So, if the actor is directly in 'self', return 'self'.
5662 */
5663 getExtraScopeItems(actor)
5664 {
5665 /* if the actor is directly in the nested room, return it */
5666 if (actor.isDirectlyIn(self))
5667 return [self];
5668 else
5669 return [];
5670 }
5671
5672 /*
5673 * By default, 'out' within a nested room should take us out of the
5674 * nested room itself. The easy way to accomplish this is to set up
5675 * a 'nestedRoomOut' connector for the nested room, which will
5676 * automatically try a GET OUT command. If we didn't do this, we'd
5677 * *usually* pick up the noTravelOut from our enclosing room, but
5678 * only when the enclosing room didn't override 'out' to point
5679 * somewhere else. Explicitly setting up a 'noTravelOut' here
5680 * ensures that we'll consistently GET OUT of the nested room even if
5681 * the enclosing room has its own 'out' destination.
5682 *
5683 * Note that nestedRoomOut shows as a listed exit in exit listings
5684 * (for the EXITS command and in the status line). If you don't want
5685 * OUT to be listed as an available exit for the nested room, you
5686 * should override this to use noTravelOut instead.
5687 */
5688 out = nestedRoomOut
5689
5690 /*
5691 * An actor is attempting to "get out" while in this location. By
5692 * default, we'll treat this as getting out of this object. This
5693 * can be overridden if "get out" should do something different.
5694 */
5695 disembarkRoom()
5696 {
5697 /* run the appropriate command to get out of this nested room */
5698 removeFromNested();
5699 }
5700
5701 /*
5702 * Make the actor stand up from this location. By default, we'll
5703 * cause the actor to travel (using travelWithin) to our container,
5704 * and assume the appropriate posture for the container.
5705 */
5706 makeStandingUp()
5707 {
5708 /* remember the old posture, in case the travel fails */
5709 local oldPosture = gActor.posture;
5710
5711 /* get the exit destination; if there isn't one, we can't proceed */
5712 local dest = exitDestination;
5713 if (dest == nil)
5714 {
5715 reportFailure(&cannotDoFromHereMsg);
5716 exit;
5717 }
5718
5719 /*
5720 * Set the actor's posture to the default for the destination.
5721 * Do this before effecting the actual travel, so that the
5722 * destination can change this default if it wants.
5723 */
5724 gActor.makePosture(dest.defaultPosture);
5725
5726 /* protect against 'exit' and the like during the travel attempt */
5727 try
5728 {
5729 /*
5730 * move the actor to our exit destination, traveling entirely
5731 * within nested locations
5732 */
5733 gActor.travelWithin(dest);
5734 }
5735 finally
5736 {
5737 /* if we didn't end up traveling, restore the old posture */
5738 if (gActor.isIn(self))
5739 gActor.makePosture(oldPosture);
5740 }
5741
5742 /* generate the appropriate default for the new location */
5743 gActor.okayPostureChange();
5744 }
5745
5746 /*
5747 * Try an implied command to move the actor from outside of this
5748 * nested room into this nested room. This must be overridden in
5749 * subclasses to carry out the appropriate implied command. Returns
5750 * the result of tryImplicitAction().
5751 *
5752 * This is called when we need to move an actor into this location
5753 * as part of an implied command. We use an overridable method
5754 * because different kinds of nested rooms have different commands
5755 * for entering: SIT ON CHAIR, LIE ON BED, GET IN CAR, RIDE BIKE,
5756 * and so on. This should be normally be overridden imply by
5757 * calling tryImplicitAction() with the appropriate command for the
5758 * specific type of nested room, and returning the result.
5759 */
5760 tryMovingIntoNested()
5761 {
5762 /* do nothing by default - subclasses must override */
5763 return nil;
5764 }
5765
5766 /*
5767 * message property to use for reportFailure when
5768 * tryMovingIntoNested fails
5769 */
5770 mustMoveIntoProp = nil
5771
5772 /*
5773 * Try an implied command to remove an actor from this location and
5774 * place the actor in my immediate containing location. This must
5775 * be overridden in subclasses to carry out the appropriate implied
5776 * command. Returns the result of tryImplicitAction().
5777 *
5778 * This is essentially the reverse of tryMovingIntoNested(), and
5779 * should in most cases be implemented by calling
5780 * tryImplicitAction() with the appropriate command to get out of
5781 * the room, and returning the result.
5782 */
5783 tryRemovingFromNested()
5784 {
5785 /* do nothing by default - subclasses must override */
5786 return nil;
5787 }
5788
5789 /*
5790 * Replace the current action with one that removes the actor from
5791 * this nested room. This is used to implement the GET OUT command
5792 * when the actor is directly in this nested room. In most cases,
5793 * this should simply be implemented with a call to replaceAction()
5794 * with the appropriate command.
5795 */
5796 removeFromNested()
5797 {
5798 /* subclasses must override */
5799 }
5800
5801 /*
5802 * Try moving the actor into this location. This is used to move
5803 * the actor into this location as part of meeting preconditions,
5804 * and we use the normal precondition check protocol: we return nil
5805 * if the condition (actor is in this room) is already met; we
5806 * return true if we successfully execute an implied command to meet
5807 * the condition; and we report a failure message and terminate the
5808 * command with 'exit' if we don't know how to meet the condition or
5809 * the implied command we try to execute fails or fails to satisfy
5810 * the condition.
5811 *
5812 * This does not normally need to be overridden in subclasses.
5813 */
5814 checkMovingActorInto(allowImplicit)
5815 {
5816 /* if the actor is within me, use default handling */
5817 if (gActor.isIn(self))
5818 return inherited(allowImplicit);
5819
5820 /* try an implied command to move the actor into this nested room */
5821 if (allowImplicit && tryMovingIntoNested())
5822 {
5823 /* if we didn't succeed, terminate the command */
5824 if (!gActor.isDirectlyIn(self))
5825 exit;
5826
5827 /* tell the caller we executed an implied command */
5828 return true;
5829 }
5830
5831 /*
5832 * if we can be seen, report that the actor must travel here
5833 * first; if we can't be seen, simply say that this can't be done
5834 * from here
5835 */
5836 if (gActor.canSee(self))
5837 {
5838 /* report that we have to move into 'self' first */
5839 reportFailure(mustMoveIntoProp, self);
5840 }
5841 else
5842 {
5843 /*
5844 * we can't be seen; simply say this we can't do this command
5845 * from the current location
5846 */
5847 reportFailure(&cannotDoFromHereMsg);
5848 }
5849
5850 /* terminate the action */
5851 exit;
5852 }
5853
5854 /*
5855 * Check, using pre-condition rules, that the actor is removed from
5856 * this nested location and moved to my immediate location. This is
5857 * used to enforce a precondition that the actor is in the enclosing
5858 * location.
5859 *
5860 * This isn't normally overridden in subclasses.
5861 */
5862 checkActorOutOfNested(allowImplicit)
5863 {
5864 /* try removing the actor from this nested location */
5865 if (allowImplicit && tryRemovingFromNested())
5866 {
5867 /*
5868 * make sure we managed to move the actor to our exit
5869 * destination
5870 */
5871 if (!gActor.isDirectlyIn(exitDestination))
5872 exit;
5873
5874 /* indicate that we carried out an implied command */
5875 return true;
5876 }
5877
5878 /* we can't carry out our implied departure plan - fail */
5879 reportFailure(&cannotDoFromMsg, self);
5880 exit;
5881 }
5882
5883 /*
5884 * Check, using pre-condition rules, that the actor is ready to
5885 * enter this room as a nested location.
5886 *
5887 * This isn't normally overridden in subclasses.
5888 */
5889 checkActorReadyToEnterNestedRoom(allowImplicit)
5890 {
5891 /*
5892 * If the actor is directly in this room, we obviously need do
5893 * nothing, as the actor is already in this nested room.
5894 */
5895 if (gActor.isDirectlyIn(self))
5896 return nil;
5897
5898 /*
5899 * If the actor isn't within us (directly or indirectly), we
5900 * must move the actor to a valid "staging location," so that
5901 * the actor can move from the staging location into us. (A
5902 * staging location is simply any location from which we can
5903 * move directly into this nested room without any intervening
5904 * travel in or out of other nested rooms.)
5905 */
5906 if (!gActor.isIn(self))
5907 return checkActorInStagingLocation(allowImplicit);
5908
5909 /*
5910 * The actor is within us, but isn't directly within us, so
5911 * handle this with the normal routine to move the actor into
5912 * this room.
5913 */
5914 return checkMovingActorInto(allowImplicit);
5915 }
5916
5917 /*
5918 * Check, using precondition rules, that the actor is in a valid
5919 * "staging location" for entering this nested room. We'll ensure
5920 * that the actor is directly in one of the locations in our
5921 * stagingLocations list, running an appropriate implicit command to
5922 * move the actor to the first item in that list if the actor isn't
5923 * in any of them.
5924 *
5925 * This isn't normally overridden in subclasses.
5926 */
5927 checkActorInStagingLocation(allowImplicit)
5928 {
5929 local lst;
5930 local target;
5931
5932 /* get the list of staging locations */
5933 lst = stagingLocations;
5934
5935 /* if there are no valid staging locations, we can't move here */
5936 if (lst.length() == 0)
5937 {
5938 cannotMoveActorToStagingLocation();
5939 exit;
5940 }
5941
5942 /*
5943 * Try each of the locations in our staging list, to see if the
5944 * actor is directly in any of them.
5945 */
5946 foreach (local cur in lst)
5947 {
5948 /*
5949 * if the actor is directly in this staging location, then
5950 * the actor can reach the destination with no additional
5951 * intervening travel - simply return nil in this case to
5952 * indicate that no implicit commands are needed before the
5953 * proposed nested room entry
5954 */
5955 if (gActor.isDirectlyIn(cur))
5956 return nil;
5957 }
5958
5959 /*
5960 * The actor isn't directly in any staging location, so we must
5961 * move the actor to an appropriate staging location before we
5962 * can proceed. Choose a staging location based on the actor's
5963 * current location.
5964 */
5965 if ((target = chooseStagingLocation()) != nil)
5966 {
5967 /*
5968 * We've chosen a target staging location. First, check to
5969 * make sure the location we've chosen is valid - we might
5970 * have chosen a default (such as the nested room's
5971 * immediate container) that isn't usable as a staging
5972 * location, so we need to check with it first to make sure
5973 * it's willing to allow this.
5974 */
5975 target.checkStagingLocation(self);
5976
5977 /*
5978 * The check routine didn't abort the command, so try an
5979 * appropriate implicit command to move the actor into the
5980 * chosen staging location.
5981 */
5982 return target.checkMovingActorInto(allowImplicit);
5983 }
5984
5985 /*
5986 * There's no apparent intermediate staging location given the
5987 * actor's current location. We thus cannot proceed with the
5988 * command; simply report that we can't get there from here.
5989 */
5990 cannotMoveActorToStagingLocation();
5991 exit;
5992 }
5993
5994 /*
5995 * Choose an intermediate staging location, given the actor's
5996 * current location. This routine is called when the actor is
5997 * attempting to move into 'self', but isn't in any of the allowed
5998 * staging locations for 'self'; this routine's purpose is to choose
5999 * the staging location that the actor should implicitly try to
6000 * reach on the way to 'self'.
6001 *
6002 * By default, we'll attempt to find the first of our staging
6003 * locations that indirectly contains the actor. (We know none of
6004 * the staging locations directly contains the actor, because if one
6005 * did, we wouldn't be called in the first place - we're only called
6006 * when the actor isn't already directly in one of our staging
6007 * locations.) This approach is appropriate when nested rooms are
6008 * related purely by containment: if an actor is in a nested room
6009 * within one of our staging locations, we can reach that staging
6010 * location by having the actor get out of the more deeply nested
6011 * room.
6012 *
6013 * However, this default approach is not appropriate when nested
6014 * rooms are related in some way other than simple containment. We
6015 * don't have any general framework for other types of nested room
6016 * relationships, so this routine must be overridden in such a case
6017 * with special-purpose code defining the special relationship.
6018 *
6019 * If we fail to find any staging location indirectly containing the
6020 * actor, we'll return the result of defaultStagingLocation().
6021 */
6022 chooseStagingLocation()
6023 {
6024 /* look for a staging location indirectly containing the actor */
6025 foreach (local cur in stagingLocations)
6026 {
6027 /*
6028 * if the actor is indirectly in this staging location,
6029 * choose it as the target intermediate staging location
6030 */
6031 if (gActor.isIn(cur))
6032 return cur;
6033 }
6034
6035 /*
6036 * We didn't find any locations in the staging list that
6037 * indirectly contain the actor, so use the default staging
6038 * location.
6039 */
6040 return defaultStagingLocation();
6041 }
6042
6043 /*
6044 * The default staging location for this nested room. This is the
6045 * staging location we'll attempt to reach implicitly if the actor
6046 * isn't in any of the rooms in the stagingLocations list already.
6047 * We'll return the first element of our stagingLocations list for
6048 * which isStagingLocationKnown returns true.
6049 */
6050 defaultStagingLocation()
6051 {
6052 local lst;
6053
6054 /* get the list of valid staging locations */
6055 lst = stagingLocations;
6056
6057 /* find the first element which is known to the actor */
6058 foreach (local cur in lst)
6059 {
6060 /* if this staging location is known, take it as the default */
6061 if (isStagingLocationKnown(cur))
6062 return cur;
6063 }
6064
6065 /* we didn't find any known staging locations - there's no default */
6066 return nil;
6067 }
6068
6069 /*
6070 * Report that we are unable to move an actor to any staging
6071 * location for this nested room. By default, we'll generate the
6072 * message "you can't do that from here," but this can overridden to
6073 * provide a more specific if desired.
6074 */
6075 cannotMoveActorToStagingLocation()
6076 {
6077 /* report the standard "you can't do that from here" message */
6078 reportFailure(&cannotDoFromHereMsg);
6079 }
6080
6081 /*
6082 * Report that we are unable to move an actor out of this nested
6083 * room, because there's no valid 'exit destination'. This is
6084 * called when we attempt to GET OUT OF the nested room, and the
6085 * 'exitDestination' property is nil.
6086 */
6087 cannotMoveActorOutOf()
6088 {
6089 /* report the standard "you can't do that from here" message */
6090 reportFailure(&cannotDoFromHereMsg);
6091 }
6092
6093
6094 /*
6095 * The valid "staging locations" for this nested room. This is a
6096 * list of the rooms from which an actor can DIRECTLY reach this
6097 * nested room; in other words, the actor will be allowed to enter
6098 * 'self', with no intervening travel, if the actor is directly in
6099 * any of these locations.
6100 *
6101 * If the list is empty, there are no valid staging locations.
6102 *
6103 * The point of listing staging locations is to make certain that
6104 * the actor has to go through one of these locations in order to
6105 * get into this nested room. This ensures that we enforce any
6106 * conditions or trigger any side effects of moving through the
6107 * staging locations, so that a player can't bypass a puzzle by
6108 * trying to move directly from one location to another without
6109 * going through the required intermediate steps. Since we always
6110 * require that an actor go through one of our staging locations in
6111 * order to enter this nested room, and since we carry out the
6112 * travel to the staging location using implied commands (which are
6113 * just ordinary commands, entered and executed automatically by the
6114 * parser), we can avoid having to code any checks redudantly in
6115 * both the staging locations and any other nearby locations.
6116 *
6117 * By default, an actor can only enter a nested room from the room's
6118 * direct container. For example, if a chair is on a stage, an
6119 * actor must be standing on the stage before the actor can sit on
6120 * the chair.
6121 */
6122 stagingLocations = [location]
6123
6124 /*
6125 * Our exit destination. This is where an actor ends up when the
6126 * actor is immediately inside this nested room and uses a "get out
6127 * of" or equivalent command to exit the nested room.
6128 *
6129 * By default, we'll use the default staging location as the exit
6130 * destination.
6131 */
6132 exitDestination = (defaultStagingLocation())
6133
6134 /*
6135 * Is the given staging location "known"? This returns true if the
6136 * staging location is usable as a default, nil if not. If this
6137 * returns true, then the location can be used in an implied command
6138 * to move the actor to the staging location in order to move the
6139 * actor into self.
6140 *
6141 * If this returns nil, no implied command will be attempted for
6142 * this possible staging location. This doesn't mean that an actor
6143 * gets a free pass through the staging location; on the contrary,
6144 * it simply means that we won't try any automatic command to move
6145 * an actor to the staging location, hence travel from a non-staging
6146 * location to this nested room will simply fail. This can be used
6147 * when part of the puzzle is to figure out that moving to the
6148 * staging location is required in the first place: if we allowed an
6149 * implied command in such cases, we'd give away the puzzle by
6150 * solving it automatically.
6151 *
6152 * By default, we'll treat all of our staging locations as known.
6153 */
6154 isStagingLocationKnown(loc) { return true; }
6155
6156 /*
6157 * Get the travel preconditions for an actor in this location. By
6158 * default, if we have a container, and the actor can see the
6159 * container, we'll return its travel preconditions; otherwise, we'll
6160 * use our inherited preconditions.
6161 */
6162 roomTravelPreCond()
6163 {
6164 local ret;
6165
6166 /*
6167 * If we can see out to our location, use the location's
6168 * conditions, since by default we'll try traveling from the
6169 * location; if we can't see out to our location, we won't be
6170 * attempting travel through our location's connectors, so use
6171 * our own preconditions instead.
6172 */
6173 if (location != nil && gActor.canSee(location))
6174 ret = location.roomTravelPreCond();
6175 else
6176 ret = inherited();
6177
6178 /* return the results */
6179 return ret;
6180 }
6181
6182 /*
6183 * We cannot take a nested room that the actor is occupying
6184 */
6185 dobjFor(Take)
6186 {
6187 verify()
6188 {
6189 /* it's illogical to take something that contains the actor */
6190 if (gActor.isIn(self))
6191 illogicalNow(&cannotTakeLocationMsg);
6192
6193 /* inherit the default handling */
6194 inherited();
6195 }
6196 }
6197
6198 /*
6199 * "get out of" action - exit the nested room
6200 */
6201 dobjFor(GetOutOf)
6202 {
6203 preCond()
6204 {
6205 return [new ObjectPreCondition(self, actorDirectlyInRoom)];
6206 }
6207 verify()
6208 {
6209 /*
6210 * the actor must be located on the platform; but allow the
6211 * actor to be indirectly on the platform, since we'll use a
6212 * precondition to move the actor out of any more nested
6213 * rooms within us
6214 */
6215 if (!gActor.isIn(self))
6216 illogicalNow(&notOnPlatformMsg);
6217 }
6218 check()
6219 {
6220 /*
6221 * If we have no 'exit destination' - that is, we have
6222 * nowhere to go when we GET OUT OF the nested room - then
6223 * prohibit the operation.
6224 */
6225 if (exitDestination == nil)
6226 {
6227 /* explain the problem and terminate the command */
6228 cannotMoveActorOutOf();
6229 exit;
6230 }
6231 }
6232 action()
6233 {
6234 /* travel to our get-out-of destination */
6235 gActor.travelWithin(exitDestination);
6236
6237 /*
6238 * set the actor's posture to the default posture for the
6239 * new location
6240 */
6241 gActor.makePosture(gActor.location.defaultPosture);
6242
6243 /* issue a default report of the change */
6244 defaultReport(&okayNotStandingOnMsg);
6245 }
6246 }
6247
6248 /* explicitly define the push-travel indirect object mappings */
6249 mapPushTravelIobj(PushTravelGetOutOf, TravelVia)
6250 ;
6251
6252
6253 /* ------------------------------------------------------------------------ */
6254 /*
6255 * A "high nested room" is a nested room that is elevated above the rest
6256 * of the room. This specializes the staging location handling so that
6257 * it generates more specific messages.
6258 */
6259 class HighNestedRoom: NestedRoom
6260 /* report that we're unable to move to a staging location */
6261 cannotMoveActorToStagingLocation()
6262 {
6263 reportFailure(&nestedRoomTooHighMsg, self);
6264 }
6265
6266 /* if we can't get out, report that it's because we're too high up */
6267 cannotMoveActorOutOf()
6268 {
6269 reportFailure(&nestedRoomTooHighToExitMsg, self);
6270 }
6271
6272 /*
6273 * Staging locations. By default, we'll return an empty list,
6274 * because a high location is not usually reachable directly from its
6275 * containing location.
6276 *
6277 * Note that puzzles involving moving platforms will have to manage
6278 * this list dynamically, which could be done either by writing a
6279 * method here that returns a list of currently valid staging
6280 * locations, or by adding objects to this list as they become valid
6281 * staging locations and removing them when they cease to be. For
6282 * example, if we have an air vent in the ceiling that we can only
6283 * reach when a chair is placed under the vent, this property could
6284 * be implemented as a method that returns a list containing the
6285 * chair only when the chair is in the under-the-vent state.
6286 *
6287 * Note that this empty default setting will also give us no exit
6288 * destination, since the default exit location is the default
6289 * staging location.
6290 */
6291 stagingLocations = []
6292 ;
6293
6294
6295 /* ------------------------------------------------------------------------ */
6296 /*
6297 * A chair is an item that an actor can sit on. When an actor is sitting
6298 * on a chair, the chair contains the actor. In addition to sitting,
6299 * chairs can optionally allow standing as well.
6300 *
6301 * We define the "BasicChair" as something that an actor can sit on, and
6302 * then subclass this with the standard "Chair", which adds surface
6303 * capabilities.
6304 */
6305 class BasicChair: NestedRoom
6306 /*
6307 * A list of the allowed postures for this object. By default, we
6308 * can sit and stand on a chair, since most ordinary chairs are
6309 * suitable for both.
6310 */
6311 allowedPostures = [sitting, standing]
6312
6313 /*
6314 * A list of the obvious postures for this object. The only obvious,
6315 * default thing you do with most ordinary chairs is sit on them,
6316 * even they allow other postures. Something like a large sofa might
6317 * want to allow both sitting and lying.
6318 *
6319 * This list differs from the allowed postures list because some
6320 * postures might be possible but not probable. For most ordinary
6321 * chairs, standing is possible, but it's not the first thing you'd
6322 * think of doing with the chair.
6323 */
6324 obviousPostures = [sitting]
6325
6326 /*
6327 * A chair's effective follow location is usually its location's
6328 * effective follow location, because we don't usually want to treat
6329 * a chair as a separate location for the purposes of "follow."
6330 * That is, if A and B are in the same room, and A sits down on a
6331 * chair in the room, we don't want to count this as a move that B
6332 * could follow.
6333 */
6334 effectiveFollowLocation = (location.effectiveFollowLocation)
6335
6336 /*
6337 * Try an implied command to move the actor from outside of this
6338 * nested room into this nested room. By default, we'll call upon
6339 * our default posture object to activate its command to move the
6340 * actor into this object in the default posture. For a chair, the
6341 * default posture is typically sitting, so the 'sitting' posture
6342 * will perform a SIT ON <self> command.
6343 */
6344 tryMovingIntoNested()
6345 {
6346 /*
6347 * ask our default posture object to carry out the appropriate
6348 * command to move the actor into 'self' in that posture
6349 */
6350 return defaultPosture.tryMakingPosture(self);
6351 }
6352
6353 /* tryMovingIntoNested failure message is "must sit on chair" */
6354 mustMoveIntoProp = &mustSitOnMsg
6355
6356 /* default posture in this nested room is sitting */
6357 defaultPosture = sitting
6358
6359 /*
6360 * by default, objects dropped while sitting in a chair go into the
6361 * enclosing location's drop destination
6362 */
6363 getDropDestination(obj, path)
6364 {
6365 return location != nil
6366 ? location.getDropDestination(obj, path)
6367 : self;
6368 }
6369
6370 /*
6371 * Remove an actor from the chair. By default, we'll simply stand
6372 * up, since this is the normal way out of a chair.
6373 */
6374 tryRemovingFromNested()
6375 {
6376 /* try standing up */
6377 if (gActor.posture == sitting)
6378 return tryImplicitAction(Stand);
6379 else
6380 return tryImplicitAction(GetOffOf, self);
6381 }
6382
6383 /*
6384 * Run the appropriate command to remove us from this nested
6385 * container, as a replacement command.
6386 */
6387 removeFromNested()
6388 {
6389 /* to get out of a chair, we simply stand up */
6390 if (gActor.posture == sitting)
6391 replaceAction(Stand);
6392 else
6393 replaceAction(GetOutOf, self);
6394 }
6395
6396 /*
6397 * "sit on" action
6398 */
6399 dobjFor(SitOn)
6400 {
6401 preCond = (preCondForEntry(sitting))
6402 verify()
6403 {
6404 /* verify entering the chair in a 'sitting' posture */
6405 if (verifyEntry(sitting, &alreadySittingOnMsg, &noRoomToSitMsg))
6406 inherited();
6407 }
6408 action()
6409 {
6410 /* enter the chair in the 'sitting' posture */
6411 performEntry(sitting);
6412 }
6413 }
6414
6415 /*
6416 * "stand on" action
6417 */
6418 dobjFor(StandOn)
6419 {
6420 /* if it's allowed, use the same preconditions as 'sit on' */
6421 preCond = (preCondForEntry(standing))
6422
6423 verify()
6424 {
6425 /* verify entering in a 'standing' posture */
6426 if (verifyEntry(standing, &alreadyStandingOnMsg,
6427 &noRoomToStandMsg))
6428 inherited();
6429 }
6430 action()
6431 {
6432 /* enter the chair in the 'standing' posture */
6433 performEntry(standing);
6434 }
6435 }
6436
6437 /*
6438 * "lie on" action
6439 */
6440 dobjFor(LieOn)
6441 {
6442 preCond = (preCondForEntry(lying))
6443 verify()
6444 {
6445 /* verify entering in a 'lying' posture */
6446 if (verifyEntry(lying, &alreadyLyingOnMsg, &noRoomToLieMsg))
6447 inherited();
6448 }
6449 action()
6450 {
6451 /* enter in the 'lying' posture */
6452 performEntry(lying);
6453 }
6454 }
6455
6456 /*
6457 * For "get on/in" / "board", let our default posture object handle
6458 * it, by running the appropriate nested action that moves the actor
6459 * into self in the default posture.
6460 */
6461 dobjFor(Board)
6462 {
6463 verify() { }
6464 action() { defaultPosture.setActorToPosture(gActor, self); }
6465 }
6466
6467 /* "get off of" is the same as "get out of" */
6468 dobjFor(GetOffOf) asDobjFor(GetOutOf)
6469
6470 /* standard preconditions for sitting/lying/standing on the chair */
6471 preCondForEntry(posture)
6472 {
6473 /*
6474 * if this is not among my allowed postures, we don't need any
6475 * special preconditions, since we'll fail in the verify
6476 */
6477 if (allowedPostures.indexOf(posture) == nil)
6478 return inherited();
6479
6480 /*
6481 * in order to enter the chair, we have to be able to touch it
6482 * and we have to be able to enter it as a nested room
6483 */
6484 return [touchObj,
6485 new ObjectPreCondition(self, actorReadyToEnterNestedRoom)];
6486 }
6487
6488 /*
6489 * Verify that we can enter the chair in the given posture. This
6490 * performs verification work common to SIT ON, LIE ON, and STAND ON.
6491 * If this returns true, the caller should inherit the base class
6492 * default handling, otherwise it shouldn't.
6493 */
6494 verifyEntry(posture, alreadyMsg, noRoomMsg)
6495 {
6496 /*
6497 * if the given posture isn't allowed, tell the caller to use the
6498 * inherited default handling
6499 */
6500 if (allowedPostures.indexOf(posture) == nil)
6501 return true;
6502
6503 /* this posture is allowed, but it might not be obvious */
6504 if (obviousPostures.indexOf(posture) == nil)
6505 nonObvious;
6506
6507 /*
6508 * If the actor is already on this chair in the given posture,
6509 * this action is redundant. If we already verified okay on this
6510 * point for this same action, ignore the repeated command - it
6511 * must mean that we applied a precondition that did all of our
6512 * work for us (such as moving us out of a nested room
6513 * immediately within us).
6514 */
6515 if (gActor.posture == posture && gActor.isDirectlyIn(self)
6516 && gAction.verifiedOkay.indexOf(self) == nil)
6517 illogicalNow(alreadyMsg);
6518 else
6519 gAction.verifiedOkay += self;
6520
6521 /*
6522 * If there's not room for the actor's added bulk, don't allow
6523 * the actor to sit/lie/stand on the chair. If the actor is
6524 * already within the chair, there's no need to add the actor's
6525 * bulk for this change, since it's already counted as being
6526 * within us.
6527 */
6528 if (!gActor.isIn(self)
6529 && getBulkWithin() + gActor.getBulk() > bulkCapacity)
6530 illogicalNow(noRoomMsg);
6531
6532 /* we can't sit/stand/lie on something the actor is holding */
6533 if (isIn(gActor))
6534 illogicalNow(&cannotEnterHeldMsg);
6535
6536 /*
6537 * if the actor is already in me, but in a different posture,
6538 * boost the likelihood slightly
6539 */
6540 if (gActor.isDirectlyIn(self) && gActor.posture != posture)
6541 logicalRank(120, 'already in');
6542
6543 /* tell the caller we don't want to inherit the base class handling */
6544 return nil;
6545 }
6546
6547 /*
6548 * Perform entry in the given posture. This carries out the common
6549 * actions for SIT ON, LIE ON, and STAND ON.
6550 */
6551 performEntry(posture)
6552 {
6553 /*
6554 * Move the actor into me - this counts as interior travel within
6555 * the enclosing room. Note that we move the actor before
6556 * changing the actor's posture in case the travel fails.
6557 */
6558 gActor.travelWithin(self);
6559
6560 /* set the actor to the desired posture */
6561 gActor.makePosture(posture);
6562
6563 /* report success */
6564 defaultReport(&roomOkayPostureChangeMsg, posture, self);
6565 }
6566 ;
6567
6568 /*
6569 * A Chair is a basic chair with the addition of being a Surface.
6570 */
6571 class Chair: BasicChair, Surface
6572 /*
6573 * By default, a chair has a seating capacity of one person, so use
6574 * a maximum bulk that only allows one actor to occupy the chair at
6575 * a time.
6576 */
6577 bulkCapacity = 10
6578 ;
6579
6580 /*
6581 * Bed. This is an extension of Chair that allows actors to lie on it
6582 * as well as sit on it. As with chairs, we have a basic bed, plus a
6583 * regular bed that serves as a surface as well.
6584 */
6585 class BasicBed: BasicChair
6586 /*
6587 * we can sit, lie, and stand on a typical bed, but only sitting and
6588 * lying are obvious default actions
6589 */
6590 allowedPostures = [sitting, lying, standing]
6591 obviousPostures = [sitting, lying]
6592
6593 /* tryMovingIntoNested failure message is "must sit on chair" */
6594 mustMoveIntoProp = &mustLieOnMsg
6595
6596 /* default posture in this nested room is sitting */
6597 defaultPosture = lying
6598 ;
6599
6600 /*
6601 * A Bed is a basic bed with the addition of Surface capabilities.
6602 */
6603 class Bed: BasicBed, Surface
6604 ;
6605
6606 /*
6607 * A Platform is a nested room upon which an actor can stand. In
6608 * general, when you can stand on something, you can also sit and lie on
6609 * it as well (it might not be comfortable, but it is usually at least
6610 * possible), so we make this a subclass of Bed.
6611 *
6612 * The main difference between a platform and a chair that allows
6613 * standing is that a platform is more of a mini-room. In particular,
6614 * items an actor drops while standing on a platform land on the platform
6615 * itself, whereas items dropped while sitting (or standing) on a chair
6616 * land in the enclosing room. In addition, the obvious default action
6617 * for a chair is to sit on it, while the obvious default action for a
6618 * platform is to stand on it.
6619 */
6620 class BasicPlatform: BasicBed
6621 /*
6622 * we can sit, lie, and stand on a typical platform, and all of
6623 * these could be reasonably expected to be done
6624 */
6625 allowedPostures = [sitting, lying, standing]
6626 obviousPostures = [sitting, lying, standing]
6627
6628 /* an actor can follow another actor onto or off of a platform */
6629 effectiveFollowLocation = (self)
6630
6631 /* tryMovingIntoNested failure message is "must get on platform" */
6632 mustMoveIntoProp = &mustGetOnMsg
6633
6634 /* default posture in this nested room is sitting */
6635 defaultPosture = standing
6636
6637 /* by default, objects dropped on a platform go onto the platform */
6638 getDropDestination(obj, path)
6639 {
6640 return self;
6641 }
6642
6643 /*
6644 * Remove an actor from the platform. "Get off" is the normal
6645 * command to leave a platform.
6646 */
6647 tryRemovingFromNested()
6648 {
6649 /* try getting off of the platform */
6650 return tryImplicitAction(GetOffOf, self);
6651 }
6652
6653 /*
6654 * Replace the current action with one that removes the actor from
6655 * this nested room.
6656 */
6657 removeFromNested()
6658 {
6659 /* get off of the platform */
6660 replaceAction(GetOffOf, self);
6661 }
6662
6663 /*
6664 * Make the actor stand up. On a platform, standing is normally
6665 * allowed, so STAND doesn't usually imply "get off platform" as it
6666 * does in the base class.
6667 */
6668 makeStandingUp()
6669 {
6670 /*
6671 * If standing isn't among my allowed postures, inherit the
6672 * default behavior, which is to get out of the nested room.
6673 */
6674 if (allowedPostures.indexOf(standing) == nil)
6675 {
6676 /* we can't stand on the platform, so use the default handling */
6677 inherited();
6678 }
6679 else
6680 {
6681 /* we can stand on the platform, so make the actor stand */
6682 gActor.makePosture(standing);
6683
6684 /* issue a default report of the change */
6685 defaultReport(&roomOkayPostureChangeMsg, standing, self);
6686 }
6687 }
6688
6689 /*
6690 * Traveling 'down' from a platform should generally be taken to
6691 * mean 'get off platform'.
6692 */
6693 down = noTravelDown
6694 ;
6695
6696 /*
6697 * A Platform is a basic platform with the addition of Surface behavior.
6698 */
6699 class Platform: BasicPlatform, Surface
6700 ;
6701
6702 /*
6703 * A "nominal platform" is a named place where NPC's can stand. This
6704 * class makes it easy to arrange for an NPC to be described as standing
6705 * in a particular location in the room: for example, we could have an
6706 * actor "standing in a doorway", or "leaning against the streetlamp."
6707 *
6708 * In most cases, a nominal platform is a "secret" object, in that it
6709 * won't be listed in a room's contents and it won't have any vocabulary
6710 * words. So, the player will never be able to refer to the object in a
6711 * command.
6712 *
6713 * To use this class, instantiate it with an object located in the room
6714 * containing the pseudo-platform. Don't give the object any vocabulary
6715 * words. Locate actors within the pseudo-platform to give them the
6716 * special description.
6717 *
6718 * For simple platform-like "standing on" descriptions, just define the
6719 * name. For descriptions like "standing in", "standing under", or
6720 * "standing near", where only the preposition needs to be customized,
6721 * define the name and define actorInPrep. For more elaborate
6722 * customizations, such as "leaning against the streetlamp", you'll need
6723 * to override roomActorHereDesc, roomActorStatus, roomActorPostureDesc,
6724 * roomListActorPosture, and actorInGroupPrefix/Suffix,
6725 */
6726 class NominalPlatform: Fixture, Platform
6727 /* don't let anyone stand/sit/lie here via a command */
6728 dobjFor(StandOn) { verify() { illogical(&cannotStandOnMsg); } }
6729 dobjFor(SitOn) { verify() { illogical(&cannotSitOnMsg); } }
6730 dobjFor(LieOn) { verify() { illogical(&cannotLieOnMsg); } }
6731
6732 /* ignore me for 'all' and object defaulting */
6733 hideFromAll(action) { return true; }
6734 hideFromDefault(action) { return true; }
6735
6736 /*
6737 * nominal platforms are internal objects only, not part of the
6738 * visible game world structure, so treat them as equivalent to their
6739 * location for FOLLOW purposes
6740 */
6741 effectiveFollowLocation = (location.effectiveFollowLocation)
6742 ;
6743
6744 /*
6745 * A booth is a nested room that serves as a small enclosure within a
6746 * larger room. Booths can serve as regular containers as well as
6747 * nested rooms, and can be made openable by addition of the Openable
6748 * mix-in class. Note that booths don't have to be fully enclosed, nor
6749 * do they actually have to be closable.
6750 *
6751 * Examples of booths: a cardboard box large enough for an actor can
6752 * stand in; a closet; a shallow pit.
6753 */
6754 class Booth: BasicPlatform, Container
6755 /*
6756 * Try an implied command to move the actor from outside of this
6757 * nested room into this nested room.
6758 */
6759 tryMovingIntoNested()
6760 {
6761 /* try getting in me */
6762 return tryImplicitAction(Board, self);
6763 }
6764
6765 /*
6766 * Remove an actor from the booth. "Get out" is the normal command
6767 * to leave this type of room.
6768 */
6769 tryRemovingFromNested()
6770 {
6771 /* try getting out of the object */
6772 return tryImplicitAction(GetOutOf, self);
6773 }
6774
6775 /*
6776 * Replace the current action with one that removes the actor from
6777 * this nested room.
6778 */
6779 removeFromNested()
6780 {
6781 /* get out of the object */
6782 replaceAction(GetOutOf, self);
6783 }
6784
6785 /*
6786 * "Enter" is equivalent to "get in" (or "board") for a booth
6787 */
6788 dobjFor(Enter) asDobjFor(Board)
6789
6790 /* explicitly define the push-travel indirect object mapping */
6791 mapPushTravelIobj(PushTravelEnter, Board)
6792 ;
6793
6794 /* ------------------------------------------------------------------------ */
6795 /*
6796 * A Vehicle is a special type of nested room that moves instead of the
6797 * actor in response to travel commands. When an actor in a vehicle
6798 * types, for example, "go north," the vehicle moves north, not the
6799 * actor.
6800 *
6801 * In most cases, a Vehicle should multiply inherit from one of the
6802 * other nested room subclasses to make it more specialized. For
6803 * example, a bicycle might inherit from Chair, so that actors can sit
6804 * on the bike.
6805 *
6806 * Note that because Vehicle inherits from NestedRoom, the OUT direction
6807 * in the vehicle by default means what it does in NestedRoom -
6808 * specifically, getting out of the vehicle. This is appropriate for
6809 * vehicles where we'd describe passengers as being inside the vehicle,
6810 * such as a car or a boat. However, if the vehicle is something you
6811 * ride on, like a horse or a bike, it's probably more appropriate for
6812 * OUT to mean "ride the vehicle out of the enclosing room." To get
6813 * this effect, simply override the "out" property and set it to nil;
6814 * this will prevent the NestedRoom definition from being inherited,
6815 * which will make us look for the OUT location of the enclosing room as
6816 * the travel destination.
6817 */
6818 class Vehicle: NestedRoom, Traveler
6819 /*
6820 * When a traveler is in a vehicle, and the traveler performs a
6821 * travel command, the vehicle is what changes location; the
6822 * contained traveler simply stays put while the vehicle moves.
6823 */
6824 getLocTraveler(trav, conn)
6825 {
6826 local stage;
6827
6828 /*
6829 * If the connector is contained within the vehicle, take it as
6830 * leading out of the vehicle - this means we want to move
6831 * traveler within the vehicle rather than the vehicle.
6832 * Consider the connector be inside the vehicle if it's a
6833 * physical object (a Thing) that's inside the vehicle, or one
6834 * of the vehicle's own directional links points directly to the
6835 * connector.
6836 *
6837 * Likewise, if the connector is marked with the vehicle or any
6838 * object inside the vehicle as its staging location, the
6839 * vehicle obviously isn't involved in the travel.
6840 */
6841 if ((conn != nil && conn.ofKind(Thing) && conn.isIn(self))
6842 || Direction.allDirections.indexWhich(
6843 {dir: self.(dir.dirProp) == conn}) != nil
6844 || ((stage = conn.connectorStagingLocation) != nil
6845 && (stage == self || stage.isIn(self))))
6846 {
6847 /*
6848 * this connector leads out from within the vehicle - move
6849 * the inner traveler rather than the vehicle itself
6850 */
6851 return trav;
6852 }
6853
6854 /*
6855 * If we have a location, ask it who travels when the VEHICLE is
6856 * the traveler within it; otherwise, the vehicle is the
6857 * traveler.
6858 *
6859 * We ask the location, because the location might itself be a
6860 * vehicle, in which case it might want us to be driving around
6861 * the enclosing vehicle. However, we pass ourselves (i.e.,
6862 * this vehicle) as the inner traveler, rather than the traveler
6863 * we were passed, because a traveler within a vehicle moves the
6864 * vehicle when traveling.
6865 */
6866 return (location != nil ? location.getLocTraveler(self, conn) : self);
6867 }
6868
6869 /*
6870 * An OUT command while within a vehicle could mean one of two
6871 * things: either to GET OUT of the vehicle, or to ride/drive the
6872 * vehicle out of its enclosing location.
6873 *
6874 * There's no good way of guessing which meaning the player intends,
6875 * so we have to choose one or the other. We choose the ride/drive
6876 * interpretation as the default, for two reasons. First, it seems
6877 * to be what most players expect. Second, the other interpretation
6878 * leaves no standard way of expressing the ride/drive meaning. We
6879 * return nil here to indicate to the OUT action that we want the
6880 * enclosing location's 'out' connector to be used while an actor is
6881 * in the vehicle.
6882 *
6883 * For some vehicles, it might be more appropriate for OUT to mean
6884 * GET OUT. In these cases, simply override this so that it returns
6885 * nestedRoomOut.
6886 */
6887 out = nil
6888
6889 /*
6890 * Get the "location push traveler" - this is the traveler when a
6891 * push-travel command is performed by a traveler within this
6892 * location. If the object we're trying to push is within me, use
6893 * the contained traveler, since the contained traveler must be
6894 * trying to push the object around directly. If the object isn't
6895 * inside me, then we're presumably trying to use the vehicle to push
6896 * around the object, so the traveler is the vehicle or something
6897 * containing the vehicle.
6898 */
6899 getLocPushTraveler(trav, obj)
6900 {
6901 /*
6902 * If the object is inside me, use the nested traveler;
6903 * otherwise, we're presumably trying to use the vehicle to move
6904 * the object.
6905 */
6906 if (obj.isIn(self))
6907 {
6908 /*
6909 * we're moving something around inside me; use the
6910 * contained traveler
6911 */
6912 return trav;
6913 }
6914 else if (location != nil)
6915 {
6916 /*
6917 * we're pushing something around outside me, so we're
6918 * probably trying to use the vehicle to do so; we have a
6919 * location, so ask it what it thinks, passing myself as the
6920 * new suggested traveler
6921 */
6922 return location.getLocPushTraveler(self, obj);
6923 }
6924 else
6925 {
6926 /*
6927 * we're pushing something around outside me, and I have no
6928 * location, so I must be the traveler
6929 */
6930 return self;
6931 }
6932 }
6933
6934 /*
6935 * Determine if an actor is traveling with me. The normal base
6936 * class implementation works, but it's more efficient just to check
6937 * to see if the actor is inside this object than to construct the
6938 * entire nested contents list just to check to see if the actor's
6939 * in that list.
6940 */
6941 isActorTraveling(actor) { return actor.isIn(self); }
6942
6943 /* invoke a callback for each actor traveling with us */
6944 forEachTravelingActor(func)
6945 {
6946 /* invoke the callback on each actor in our contents */
6947 allContents().forEach(function(obj) {
6948 if (obj.isActor)
6949 (func)(obj);
6950 });
6951 }
6952
6953 /*
6954 * Get the actors involved in the travel. This is a list consisting
6955 * of all of the actors contained within the vehicle.
6956 */
6957 getTravelerActors = (allContents().subset({x: x.isActor}))
6958
6959 /*
6960 * there are no self-motive actors in a vehicle - the vehicle is
6961 * doing the travel, and the actors within are just moving along
6962 * with it as cargo
6963 */
6964 getTravelerMotiveActors = []
6965
6966 /*
6967 * Traveler preconditions for the vehicle. By default, we add no
6968 * preconditions of our own, but specific vehicles might want to
6969 * override this. For example, a car might want to require that the
6970 * doors are closed, the engine is running, and the seatbelts are
6971 * fastened before it can travel.
6972 */
6973 travelerPreCond(conn) { return []; }
6974
6975 /*
6976 * Check, using pre-condition rules, that the traveler is in the
6977 * given room, moving the traveler to the room if possible.
6978 */
6979 checkMovingTravelerInto(room, allowImplicit)
6980 {
6981 /* if we're in the desired location, we're set */
6982 if (isDirectlyIn(room))
6983 return nil;
6984
6985 /*
6986 * By default, we can't move a vehicle into a room implicitly.
6987 * Individual vehicles can override this when there's an obvious
6988 * way of moving the vehicle in and out of nested rooms.
6989 */
6990 reportFailure(&vehicleCannotDoFromMsg, self);
6991 exit;
6992 }
6993
6994 /*
6995 * the lister object we use to display the list of actors aboard, in
6996 * arrival and departure messages for the vehicle
6997 */
6998 aboardVehicleListerObj = aboardVehicleLister
6999 ;
7000
7001 /*
7002 * A VehicleBarrier is a TravelConnector that allows actors to travel,
7003 * but blocks vehicles. By default, we block all vehicles, but
7004 * subclasses can customize this so that we block only specific
7005 * vehicles.
7006 */
7007 class VehicleBarrier: TravelBarrier
7008 /*
7009 * Determine if the given traveler can pass through this connector.
7010 * By default, we'll return nil for a Vehicle, true for anything
7011 * else. This can be overridden to allow specific vehicles to pass,
7012 * or to filter on any other criteria.
7013 */
7014 canTravelerPass(traveler) { return !traveler.ofKind(Vehicle); }
7015
7016 /* explain why we can't pass */
7017 explainTravelBarrier(traveler)
7018 {
7019 reportFailure(&cannotGoThatWayInVehicleMsg, traveler);
7020 }
7021 ;
7022