873d6a0f4819a044892af313bc81a47f66e6fe97
[ibg.git] / chapters / 05.rst
1 =================
2  Heidi revisited
3 =================
4
5 .. epigraph::
6
7    | |CENTER| *I was an innkeeper, who loved to carouse;*
8    | |CENTER| *J was a joiner, and built up a house.*
9
10 .. only:: html
11
12   .. image:: /images/picI.png
13      :align: left
14
15 |I|\n even the simplest story, there's bound to be scope for the player to
16 attempt activities that you hadn't anticipated.  Sometimes there may be
17 alternative ways of approaching a problem: if you can't be sure which
18 approach the player will take, you really ought to allow for all
19 possibilities.  Sometimes the objects you create and the descriptions you
20 provide may suggest to the player that doing such-and-such should be
21 possible, and, within reason, you ought to allow for that also.  The basic
22 game design is easy: what takes the time, and makes a game large and
23 complex, is taking care of all the *other* things that the player may think
24 of trying.
25
26 Here, we try to illustrate what this means by addressing a few of the more
27 glaring deficiencies in our first game.
28
29 Listening to the bird
30 =====================
31
32 Here's a fragment of the game being played:
33
34 .. code-block:: transcript
35
36    Deep in the forest
37    Through the dense foliage, you glimpse a building to the west. A track heads
38    to the northeast.
39
40    You can see a baby bird here.
41
42    >EXAMINE THE BIRD
43    Too young to fly, the nestling tweets helplessly.
44
45    >LISTEN TO BIRD
46    You hear nothing unexpected.
47
48    >
49
50 That's not too smart, is it?  Our description specifically calls the
51 player's attention to the sound of the bird -- and then she finds out that
52 we've got nothing special to say about its helpless tweeting.
53
54 The library has a stock of actions and responses for each of the game's
55 defined verbs, so it can handle most of the player's input with a default,
56 standard behaviour instead of remaining impertinently silent or saying that
57 it doesn't understand what the player intends.  "You hear nothing
58 unexpected" is the library's standard LISTEN response, good enough after
59 LISTEN TO NEST or LISTEN TO TREE, but fairly inappropriate here; we really
60 need to substitute a more relevant response after LISTEN TO BIRD.  Here's
61 how we do it:
62
63 .. code-block:: inform
64
65    Object   bird "baby bird" forest
66      with   description "Too young to fly, the nestling tweets helplessly.",
67             name 'baby' 'bird' 'nestling',
68             before [;
69                   Listen:
70                     print "It sounds scared and in need of assistance.^";
71                     return true;
72             ],
73       has   ;
74
75 We'll go through this a step at a time:
76
77 #. We've added a new ``before`` property to our bird object.  The
78    interpreter looks at the property *before* attempting to perform any
79    action which is directed specifically at this object::
80
81       before [; ... ],
82
83 #. The value of the property is an embedded routine, containing a label and
84    two statements::
85
86        Listen:
87          print "It sounds scared and in need of assistance.^";
88          return true;
89
90 #. The label is the name of an action, in this case ``Listen``.  What we're
91    telling the interpreter is: if the action that you're about to perform
92    on the bird is a ``Listen``, execute these statements first; if it's any
93    other action, carry on as normal.  So, if the player types EXAMINE BIRD,
94    PICK UP BIRD, PUT BIRD IN NEST, HIT BIRD or FONDLE BIRD, then she'll get
95    the standard response.  If she types LISTEN TO BIRD, then our two
96    statements get executed before anything else happens.  We call this
97    "trapping" or "intercepting" the action of Listening to the bird.
98
99 #. The two statements that we execute are, first::
100
101        print "It sounds scared and in need of assistance.^";
102
103    which causes the interpreter to display the string given in double
104    quotes; remember that a ``^`` character in a string appears as a
105    newline.  Second, we execute::
106
107        return true;
108
109    which tells the interpreter that it doesn't need to do anything else,
110    because we've handled the ``Listen`` action ourselves.  And the game now
111    behaves like this -- perfect:
112
113    .. code-block:: transcript
114
115       >LISTEN TO BIRD
116       It sounds scared and in need of assistance.
117
118       >
119
120 The use of the ``return true`` statement probably needs a bit more
121 explanation.  An object's ``before`` property traps an action aimed at that
122 object right at the start, before the interpreter has started to do
123 anything.  That's the point at which the statements in the embedded routine
124 are executed.  If the last of those statements is ``return true`` then the
125 interpreter assumes that the action has been dealt with by those
126 statements, and so there's nothing left to do: no action, no message;
127 nothing.  On the other hand, if the last of the statements is ``return
128 false`` then the interpreter carries on to perform the default action as
129 though it hadn't been intercepted.  Sometimes that's what you want it to
130 do, but not here: if instead we'd written this:
131
132 .. code-block:: inform
133
134    Object    bird "baby bird" forest
135      with    description "Too young to fly, the nestling tweets helplessly.",
136              name 'baby' 'bird' 'nestling',
137              before [;
138                 Listen:
139                   print "It sounds scared and in need of assistance.^";
140                   return false;
141              ],
142        has   ;
143
144 then the interpreter would first have displayed our string, and then
145 carried on with its normal response, which is to display the standard
146 message:
147
148 .. code-block:: transcript
149
150    >LISTEN TO BIRD
151    It sounds scared and in need of assistance.
152    You hear nothing unexpected.
153
154    >
155
156 This technique -- intercepting an action aimed at a particular object in
157 order to do something appropriate for that object -- is one that we'll use
158 again and again.
159
160 Entering the cottage
161 ====================
162
163 At the start of the game the player character stands "outside a cottage", which
164 might lead her to believe that she can go inside:
165
166 .. code-block:: transcript
167
168    In front of a cottage
169    You stand outside a cottage. The forest stretches east.
170
171    >IN
172    You can't go that way.
173
174    >
175
176 Again, that isn't perhaps the most appropriate response, but it's easy to
177 change:
178
179 .. code-block:: inform
180
181    Object    before_cottage "In front of a cottage"
182      with    description
183                  "You stand outside a cottage. The forest stretches east.",
184              e_to forest,
185              in_to "It's such a lovely day -- much too nice to go inside.",
186              cant_go "The only path lies to the east.",
187        has   light;
188
189 The ``in_to`` property would normally link to another room, in the same way
190 as the ``e_to`` property contain the internal ID of the ``forest`` object.
191 However, if instead you set its value to be a string, the interpreter
192 displays that string when the player tries the IN direction.  Other --
193 unspecified -- directions like NORTH and UP still elicit the standard "You
194 can't go that way" response, but we can change that too, by supplying a
195 ``cant_go`` property whose value is a suitable string.  We then get this
196 friendlier behaviour:
197
198 .. code-block:: transcript
199
200    In front of a cottage
201    You stand outside a cottage. The forest stretches east.
202
203    >IN
204    It's such a lovely day -- much too nice to go inside.
205
206    >NORTH
207    The only path lies to the east.
208
209    >EAST
210
211    Deep in the forest
212    ...
213
214 There's another issue here; since we haven't actually implemented an object
215 to represent the cottage, a perfectly reasonable EXAMINE COTTAGE command
216 receives the obviously nonsensical reply "You can't see any such thing".
217 That's easy to fix; we can add a new ``cottage`` object, making it a piece
218 of ``scenery`` just like the ``tree``:
219
220 .. code-block:: inform
221
222    Object   cottage "tiny cottage" before_cottage
223      with   description "It's small and simple, but you're very happy here.",
224             name 'tiny' 'cottage' 'home' 'house' 'hut' 'shed' 'hovel',
225       has   scenery;
226
227 This solves the problem, but promptly gives us another unreasonable
228 response:
229
230 .. code-block:: transcript
231
232    In front of a cottage
233    You stand outside a cottage. The forest stretches east.
234
235    >ENTER COTTAGE
236    That's not something you can enter.
237
238    >
239
240 The situation here is similar to our LISTEN TO BIRD problem, and the
241 solution we adopt is similar as well:
242
243 .. code-block:: inform
244
245    Object   cottage "tiny cottage" before_cottage
246      with   description "It's small and simple, but you're very happy here.",
247             name 'tiny' 'cottage' 'home' 'house' 'hut' 'shed' 'hovel',
248             before [;
249                Enter:
250                  print_ret "It's such a lovely day -- much too nice to go inside.";
251             ],
252       has   scenery;
253
254 We use a ``before`` property to intercept the ``Enter`` action applied to
255 the cottage object, so that we can display a more appropriate message.
256 This time, however, we've done it using one statement rather than two.  It
257 turns out that the sequence "``print`` a string which ends with a newline
258 character, and then ``return true``" is so frequently needed that there's a
259 special statement which does it all.  That is, this single statement (where
260 you'll note that the string *doesn't* need to end in ``^``)::
261
262      print_ret "It's such a lovely day -- much too nice to go inside.";
263
264 works exactly the same as this pair of statements::
265
266      print "It's such a lovely day -- much too nice to go inside.^";
267      return true;
268
269 We could have used the shorter form when handling LISTEN TO BIRD, and we
270 *will* use it from now on.
271
272 Climbing the tree
273 =================
274
275 In the clearing, holding the nest and looking at the tree, the player is
276 meant to type UP.  Just as likely, though, she'll try CLIMB TREE (which
277 currently gives the completely misleading response "I don't think much is
278 to be achieved by that").  Yet another opportunity to use a ``before``
279 property, but now with a difference.
280
281 .. code-block:: inform
282
283    Object   tree "tall sycamore tree" clearing
284      with   description
285                  "Standing proud in the middle of the clearing,
286                   the stout tree looks easy to climb.",
287             name 'tall' 'sycamore' 'tree' 'stout' 'proud',
288             before [;
289                Climb:
290                  PlayerTo(top_of_tree);
291                  return true;
292             ],
293      has    scenery;
294
295 This time, when we intercept the ``Climb`` action applied to the ``tree``
296 object, it's not in order to display a better message; it's because we want
297 to move the player character to another room, just as if she'd typed UP.
298 Relocating the player character is actually quite a complex business, but
299 fortunately all of that complexity is hidden: there's a standard
300 :term:`library routine` to do the job, not one that we've written, but one
301 that's provided as part of the Inform system.
302
303 You'll remember that, when we first mentioned routines (see
304 :ref:`standalone-routines`), we used the example of ``Initialise()`` and
305 said that "the routine's name followed by opening and closing parentheses
306 is all that it takes to call a routine".  That was true for
307 ``Initialise()``, but not quite the whole story.  To move the player
308 character, we've got to specify where we want her to go, and we do that by
309 supplying the internal ID of the destination room within the opening and
310 closing parentheses.  That is, instead of just ``PlayerTo()`` we call
311 ``PlayerTo(top_of_tree)``, and we describe ``top_of_tree`` as the routine's
312 :term:`argument`.
313
314 Although we've moved the player character to another room, we're still in
315 the middle of the intercepted ``Climb`` action.  As previously, we need to
316 tell the interpreter that we've dealt with the action, and so we don't want
317 the standard rejection message to be displayed.  The ``return true``
318 statement does that, as usual.
319
320 Dropping objects from the tree
321 ==============================
322
323 In a normal room like the ``forest`` or the ``clearing``, the player can
324 DROP something she's carrying and it'll effectively fall to the ground at
325 her feet.  Simple, convenient, predictable -- except when the player is at
326 the top of the tree.  Should she DROP something from up there, having it
327 land nearby might seem a bit improbable; much more likely that it would
328 fall to the clearing below.
329
330 It looks like we might want to intercept the ``Drop`` action, but not quite
331 in the way we've been doing up until now.  For one thing, we don't want to
332 complicate the definitions of the ``bird`` and the ``nest`` and any other
333 objects we may introduce: much better to find a general solution that will
334 work for all objects.  And second, we need to recognise that not all
335 objects are droppable; the player can't, for example, DROP THE BRANCH.
336
337 The best approach to the second problem is to intercept the ``Drop`` action
338 *after* it has occurred, rather than beforehand.  That way, we let the
339 library take care of objects which aren't being held or which can't be
340 dropped, and only become involved once a ``Drop`` has been successful.  And
341 the best approach to the first problem is to do this particular
342 interception not on an object-by-object basis, as we have been doing so
343 far, but instead for every ``Drop`` which takes place in our troublesome
344 ``top_of_tree`` room.  This is what we have to write:
345
346 .. code-block:: inform
347
348    Object   top_of_tree "At the top of the tree"
349      with   description "You cling precariously to the trunk.",
350             d_to clearing,
351             after [;
352                Drop:
353                  move noun to clearing;
354                  return false;
355             ],
356       has   light;
357
358 Let's again take it a step at a time:
359
360 #. We've added a new ``after`` property to our ``top_of_tree`` object.  The
361    interpreter looks at the property *subsequent to* performing any action in
362    this room::
363
364        after [; ... ],
365
366 #. The value of the property is an embedded routine, containing a label and
367    two statements::
368
369        Drop:
370          move noun to clearing;
371          return false;
372
373 #. The label is the name of an action, in this case ``Drop``.  What we're
374    telling the interpreter is: if the action that you've just performed
375    here is a ``Drop``, execute these statements before telling the player
376    what you've done; if it's any other action, carry on as normal.
377
378 #. The two statements that we execute are first::
379
380        move noun to clearing;
381
382    which takes the object which has just been moved from the ``player``
383    object to the ``top_of_tree`` object (by the successful ``Drop`` action)
384    and moves it again so that its parent becomes the ``clearing`` object.
385    That ``noun`` is a library variable that always contains the internal ID
386    of the object which is the target of the current action.  If the player
387    types DROP NEST, ``noun`` contains the internal ID of the ``nest``
388    object; if she types DROP NESTLING then ``noun`` contains the internal
389    ID of the ``bird`` object.  Second, we execute::
390
391        return false;
392
393    which tells the interpreter that it should now let the player know
394    what's happened.  Here's the result of all this:
395
396    .. code-block:: transcript
397
398       At the top of the tree
399       You cling precariously to the trunk.
400
401       You can see a wide firm bough here.
402
403       >DROP NEST
404       Dropped.
405
406       >LOOK
407
408       At the top of the tree
409       You cling precariously to the trunk.
410
411       You can see a wide firm bough here.
412
413       >DOWN
414
415       A forest clearing
416       A tall sycamore stands in the middle of this clearing. The path winds
417       southwest through the trees.
418
419       You can see a bird's nest (in which is a baby bird) here.
420
421       >
422
423 Of course, you might think that the standard message "Dropped" is slightly
424 unhelpful in these non-standard circumstances.  If you prefer to hint at
425 what's just happened, you could use this alternative solution:
426
427 .. code-block:: inform
428
429    Object   top_of_tree "At the top of the tree"
430      with   description "You cling precariously to the trunk.",
431             d_to clearing,
432             after [;
433                Drop:
434                  move noun to clearing;
435                  print_ret "Dropped... to the ground far below.";
436             ],
437      has    light;
438
439 The ``print_ret`` statement does two things for us: displays a more
440 informative message, and returns ``true`` to tell the interpreter that
441 there's no need to let the player know what's happened -- we've handled
442 that ourselves.
443
444 Is the bird in the nest?
445 ========================
446
447 The game ends when the player character puts the nest onto the branch.  Our
448 assumption here is that the bird is inside the nest, but this might not be
449 so; the player may have first taken up the bird and then gone back for the
450 nest, or vice versa.  It would be better not to end the game until we'd
451 checked for the bird actually being in the nest; fortunately, that's easy
452 to do:
453
454 .. code-block:: inform
455
456    Object   branch "wide firm bough" top_of_tree
457      with   description "It's flat enough to support a small object.",
458             name 'wide' 'firm' 'flat' 'bough' 'branch',
459             each_turn [; if (bird in nest && nest in branch) deadflag = 2; ],
460       has   static supporter;
461
462 The extended ``if`` statement::
463
464     if (bird in nest && nest in branch) deadflag = 2;
465
466 should now be read as: "Test whether the ``bird`` is currently in (or on)
467 the ``nest``, *and* whether the ``nest`` is currently on (or in) the
468 ``branch``; if both parts are ``true``, set the value of ``deadflag`` to 2;
469 otherwise, do nothing".
470
471 Summing up
472 ==========
473
474 You should by now have some appreciation of the need not only to handle the
475 obvious actions which were at the forefront of your mind when designing the
476 game, but also as many as you can of the other possible ways that a player
477 may choose to interact with the objects presented to her.  Some of those
478 ways will be highly intelligent, some downright dumb; in either case you
479 should try to ensure that the game's response is at least sensible, even
480 when you're telling the player "sorry, you can't do that".
481
482 The new topics that we've encountered here include these:
483
484 Object properties
485 -----------------
486
487 Objects can have a ``before`` property -- if there is one, the interpreter
488 looks at it *before* performing an action which in some way involves that
489 object.  Similarly, you can provide an ``after`` property, which the
490 interpreter looks at *after* performing an action but before telling the
491 player what's happened.  Both ``before`` and ``after`` properties can be
492 used not only with tangible objects like the ``bird``, ``cottage`` and
493 ``tree`` (when they intercept actions aimed at that particular object) but
494 also with rooms (when they intercept actions aimed at any object in that
495 room).
496
497 The value of each ``before`` and ``after`` property is an embedded routine.
498 If such a routine ends with ``return false``, the interpreter then carries
499 on with the next stage of the action which has been intercepted; if it ends
500 with ``return true``, the interpreter does nothing further for that action.
501 By combining these possibilities, you can supplement the work done by a
502 standard action with statements of your own, or you can replace a standard
503 action completely.
504
505 Previously, we've seen connection properties used with the internal ID of
506 the room to which they lead.  In this chapter, we showed that the value
507 could also be a string (explaining why movement in that direction isn't
508 possible).  Here are examples of both, and also of the ``cant_go`` property
509 which provides just such an explanation for *all* connections that aren't
510 explicitly listed::
511
512     e_to forest,
513     in_to "It's such a lovely day -- much too nice to go inside.",
514     cant_go "The only path lies to the east.",
515
516 .. _routines-args:
517
518 Routines and arguments
519 ----------------------
520
521 The library includes a number of useful routines, available to perform
522 certain common tasks if you require them; there's a list in
523 :ref:`library-routines`.  We used the ``PlayerTo`` routine, which moves the
524 player character from her current room to another one -- not necessarily
525 adjacent to the first room.
526
527 When calling ``PlayerTo``, we had to tell the library which room is the
528 destination.  We did this by supplying that room's internal ID within
529 parentheses, thus::
530
531     PlayerTo(clearing);
532
533 A value given in parentheses like that is called an :term:`argument` of the
534 routine.  In fact, a routine can have more than one argument; if so,
535 they're separated by commas.  For example, to move the player character to
536 a room *without* displaying that room's description, we could have supplied
537 a second argument::
538
539     PlayerTo(clearing,1);
540
541 In this example, the effect of the ``1`` is to prevent the description
542 being displayed.
543
544 Statements
545 ----------
546
547 We encountered several new statements:
548
549 ``return true;``
550
551 ``return false;``
552     We used these at the end of embedded routines to control what the
553     interpreter did next.
554
555 ``print "string";``
556
557 ``print_ret "string";``
558     The ``print`` statement simply displays the string of characters
559     represented here by *string*.  The ``print_ret`` statement also does
560     that, then outputs a newline character, and finally executes a ``return
561     true;``
562
563 ``if (condition && condition ) ...``
564     We extended the simple ``if`` statement that we met before.  The ``&&``
565     (to be read as "and") is an operator commonly used when testing for
566     more than one condition at the same time.  It means "if this condition
567     is true *and* this condition is also true *and* ..."  There's also a
568     ``||`` operator, to be read as "or", and a "not" operator ``~~``, which
569     turns true into false and vice versa.
570
571     .. note::
572
573        In addition, there are ``&``, ``|`` and ``~`` operators, but they do
574        a rather different job and are much less common.  Take care not to
575        get them confused.
576
577 ``move obj_id to parent_obj_id;``
578      The ``move`` statement rearranges the object tree, by making the first
579      ``obj_id`` a child of the ``parent_obj_id``.
580
581 .. rubric:: Actions
582
583 We've talked a lot about intercepting actions like ``Listen``, ``Enter``,
584 ``Climb`` and ``Drop``.  An action is a generalised representation of
585 something to be done, determined by the verb which the player types.  For
586 example, the verbs HEAR and LISTEN are ways of saying much the same thing,
587 and so both result in the same action: ``Listen``.  Similarly, verbs like
588 ENTER, GET INTO, SIT ON and WALK INSIDE all lead to an action of ``Enter``,
589 CLIMB and SCALE lead to Climb, and DISCARD, DROP, PUT DOWN and THROW all
590 lead to ``Drop``.  This makes life much easier for the designer; although
591 Inform defines quite a lot of actions, there are many fewer than there are
592 ways of expressing those same actions using English verbs.
593
594 Each action is represented internally by a number, and the value of the
595 current action is stored in a library variable called, erm, ``action``.
596 Two more variables are also useful here: ``noun`` holds the internal ID of
597 the object which is the focus of the action, and ``second`` holds the
598 internal ID of the secondary object (if there is one).  Here are some
599 examples of these:
600
601 ===============================    ======     =======   =======
602 Player types                       action     noun      second
603 -------------------------------    ------     -------   -------
604 LISTEN                             Listen     nothing   nothing
605 LISTEN TO THE BIRD                 Listen     bird      nothing
606 PICK UP THE BIRD                   Take       bird      nothing
607 PUT BIRD IN NEST                   Insert     bird      nest
608 DROP THE NEST                      Drop       nest      nothing
609 PUT NEST ON BRANCH                 PutOn      nest      branch
610 ===============================    ======     =======   =======
611
612 The value ``nothing`` is a built-in constant (like ``true`` and ``false``)
613 which means, well, there isn't any object to refer to.  There's a list of
614 standard library actions in :ref:`group-1-actions`, :ref:`group-2-actions`
615 and :ref:`group-3-actions`.
616
617 We've now reached the end of our first game.  In these three chapters we've
618 shown you the basic principles on which almost all games are based, and
619 introduced you to many of the components that you'll need when creating
620 more interesting IF.  We suggest that you take one last look at the source
621 code (see :doc:`/appendices/b`), and then move on to the next stage.