kpsewhich now expects "-var-value" instead of "-var".
[ibg.git] / chapters / 03.rst
1 ==============================
2  Heidi: our first Inform game
3 ==============================
4
5 .. epigraph::
6
7    | |CENTER| *E was an esquire, with pride on his brow;*
8    | |CENTER| *F was a farmer, and followed the plough.*
9
10 .. only:: html
11
12   .. image:: /images/picE.png
13      :align: left
14
15 |E|\ach of the three games in this guide is created step by step; you'll
16 get most benefit (especially to begin with) if you take an active part,
17 typing in the source code on your computer.  Our first game, described in
18 this chapter and the two which follow, tells this sentimental little story:
19
20     "Heidi lives in a tiny cottage deep in the forest.  One sunny day,
21     standing before the cottage, she hears the frenzied tweeting of baby
22     bird; its nest has fallen from the tall tree in the clearing!  Heidi
23     puts the bird into the nest, and then climbs the tree to place the nest
24     back on its branch."
25
26 It's a very simple tale, but even so we'll cover quite a lot of ground
27 before we have a finished Inform game.  We'll get there in stages, first
28 making a very rough approximation of the story, and then successively
29 refining the details until it's good enough for an initial attempt (there's
30 time later for more advanced stuff).
31
32 Creating a basic source file
33 ============================
34
35 The first task is to create an Inform source file template.  Every game
36 that we design will start out like this.  Follow these steps:
37
38 #. Create an ``Inform\Games\Heidi`` folder (maybe by copying
39    ``Inform\Games\MyGame1``).
40
41    .. note::
42
43       In this guide, we use the PC convention of placing a backslash
44       between folder names.  On a Macintosh, use a regular slash:
45       ``Inform/Games/Heidi``.
46
47 #. In that folder, use your text editor to create this source file
48    ``Heidi.inf``:
49
50    .. include:: /config/typethis.rst
51
52    .. code-block:: inform
53
54       !% -SD
55       !============================================================================
56       Constant Story "Heidi";
57       Constant Headline
58                   "^A simple Inform example
59                    ^by Roger Firth and Sonja Kesserich.^";
60
61       Include "Parser";
62       Include "VerbLib";
63
64       !============================================================================
65       ! The game objects
66
67       !============================================================================
68       ! Entry point routines
69
70       [ Initialise; ];
71
72       !============================================================================
73       ! Standard and extended grammar
74
75       Include "Grammar";
76
77       !============================================================================
78
79    Soon, we'll explain what this means.  For now, just type it all in,
80    paying particular attention to those seven semicolons, and ensuring that
81    the double quotes "..." always come in pairs.  The first line beginning
82    with "``!%``" is special, and we'll talk about it in a moment; the
83    remaining exclamation mark lines, on the other hand, are purely
84    decorative; they just make the file's structure a little easier to
85    understand.
86
87    Ensure the file is named ``Heidi.inf``, rather than ``Heidi.txt`` or
88    ``Heidi.inf.txt``.
89
90    Remember that, throughout this guide, we place the "``TYPE``" symbol
91    alongside pieces of code that we recommend you to type into your own
92    game files as you read through the examples (which, conversely, means
93    that you *don't* need to type the unmarked pieces of code).  You'll 
94    learn Inform more quickly by trying it for yourself, rather than just 
95    taking our word for how things work.
96
97 #. In the same folder, use your text editor to create the compilation
98    support file ``Heidi.bat`` (on a PC):
99
100    .. include:: /config/typethis.rst
101
102    ::
103
104        ..\..\Lib\Base\Inform Heidi
105                    +include_path=.\,..\..\Lib\Base,..\..\Lib\Contrib | more
106
107        pause "at end of compilation"
108
109    or ``Heidi.command`` (on a Macintosh):
110
111    .. include:: /config/typethis.rst
112
113    ::
114
115        cd ~/Inform/Games/Heidi/
116
117        ../../Lib/Base/inform30_macosx Heidi
118                        +include_path=./,../../Lib/Base,../../Lib/Contrib
119
120    Remember that there's just one space between "``Heidi``" and
121    "``+include_path``".
122
123    Type in the file from scratch, or copy and edit ``MyGame1.bat`` (or
124    ``MyGame1.command``).  At this point, you should have a ``Heidi`` folder
125    containing two files: ``Heidi.inf`` and either ``Heidi.bat`` or
126    ``Heidi.command``.
127
128 #. Compile the source file ``Heidi.inf``; refer back to
129    :ref:`inform-windows` or :ref:`inform-apple` for guidance.  If the
130    compilation works, a story file ``Heidi.z5`` appears in the folder.  If
131    the compilation *doesn't* work, you've probably made a typing mistake;
132    check everything until you find it.
133
134 #. You can run the story file in your Inform interpreter; you should see
135    this (except that the Serial number will be different -- it's based on
136    the date):
137
138    .. code-block:: transcript
139
140       Heidi
141       A simple Inform example
142       by Roger Firth and Sonja Kesserich.
143       Release 1 / Serial number 040804 / Inform v6.30 Library 6/11 SD
144
145       Darkness
146       It is pitch dark, and you can't see a thing.
147
148       >
149
150 When you get that far, your template source file is correct.  Let's explain
151 what it contains.
152
153 Understanding the source file
154 =============================
155
156 Although we've got a certain amount of freedom of expression, source files
157 tend to conform to a standard overall structure: these lines at the start,
158 that material next, those pieces coming at the end, and so on.  What we're
159 doing here is mapping out a structure that suits us, giving ourselves a
160 clear framework onto which the elements of the game can be fitted.  Having
161 a clear (albeit sparse) map at the start will help us to keep things
162 organised as the game evolves.  We can infer several Inform rules just by
163 looking at the source file.
164
165 .. Generated by autoindex
166 .. index::
167    single: Strict mode
168
169 * If the *very first line* (or lines) of the source file begin with the
170   characters "``!%``", then the compiler treats what follows on those lines
171   as control instructions to itself rather than as part of the game's
172   source.  The instructions most commonly placed here are compiler
173   switches, a way of controlling detailed aspects of how the compiler
174   operates.  These particular switches, two of many, are turning on
175   :term:`Strict mode`, which makes the game less likely to misbehave when
176   being played, and :term:`Debug mode`, which provides some extra commands
177   which can be helpful when tracking down problems.
178
179   .. note::
180
181      Actually, the :option:`-S` is redundant, since Strict mode is already
182      on by default.  We include it here as a reminder that (a) to turn
183      Strict mode *off*, you change this setting to :option:`-~S`, and (b)
184      alphabetic case matters here: :option:`-s` causes a display of
185      compiler statistics (and :option:`-~s` does nothing at all).
186
187 * Otherwise, when the compiler comes across an exclamation mark, it ignores
188   the rest of the line.  If the ``!`` is at the start of a line, the whole
189   line is ignored; if the ``!`` is halfway along a line, the compiler takes
190   note of the first half, and then ignores the exclamation mark and
191   everything after it on that line.  We call material following an
192   exclamation mark, not seen by anybody else, a :term:`comment`; it's often
193   a remark that we write to remind ourselves of how something works or why
194   we tackled a problem in a particular way.  There's nothing special about
195   those equals signs: they just produce clear lines across the page.
196
197   It's always a good idea to comment code as you write it, for later it
198   will help you to understand what was going on at a particular spot.
199   Although it all seems clear in your head when you first write it, in a
200   few months you may suspect that a totally alien mind must have produced
201   that senseless gibberish.
202
203   By the way, the compiler *doesn't* give special treatment to exclamation
204   marks in quoted text: ``!`` within quotes "..." is treated as a normal
205   character.  On this line, the first ``!`` is part of the sequence (or
206   :term:`string`) of characters to be displayed:
207
208   .. code-block:: inform
209
210      print "Hello world!";         ! <- is the start of this comment
211
212 * The compiler ignores blank lines, and treats lots of space like a single
213   space (except when the spaces are part of a character string).  So, these
214   two rules tell us that we *could* have typed the source file like this:
215
216   .. code-block:: inform
217
218      Constant Story "Heidi";
219      Constant Headline
220      "^A simple Inform example^by Roger Firth and Sonja Kesserich.^";
221      Include "Parser";Include "VerbLib";
222      [ Initialise; ];
223      Include "Grammar";
224
225   We didn't type it that way because, though shorter, it's much harder to
226   read.  When designing a game, you'll spend a lot of time studying what
227   you've typed, so it's worthwhile taking a bit of care to make it as
228   readable as possible.
229
230 * Every game should have the :term:`constant` definitions for ``Story``
231   (the game's name) and ``Headline`` (typically, information on the game's
232   theme, copyright, authorship and so on).  These two :term:`string`
233   values, along with a release number and date, and details of the
234   compiler, compose the :term:`banner` which is displayed at the start of
235   each game.
236
237 * Every game needs the three lines which ``Include`` the standard library
238   files -- that is, they merge those files' contents into your source file:
239
240   .. code-block:: inform
241
242      Include "Parser";
243      Include "VerbLib";
244      ...
245      Include "Grammar";
246
247   They always have to be in this order, with ``Parser`` and ``VerbLib``
248   near the start of the file, and ``Grammar`` near the end.
249
250 * Every game needs to define an ``Initialise`` routine (note the British
251   spelling):
252
253   .. code-block:: inform
254
255      [ Initialise; ];
256
257   The :term:`routine` that we've defined here doesn't do anything useful,
258   but it still needs to be present.  Later, we'll come back to
259   ``Initialise`` and explain what a routine is and why we need this one.
260
261 * You'll notice that each of the items mentioned in the previous three
262   rules ends with a semicolon.  Inform is very fussy about its punctuation,
263   and gets really upset if you forget a terminating semicolon.  In fact,
264   the compiler just keeps reading your source file until it finds one;
265   that's why we were able to take three lines to define the ``Headline``
266   constant
267
268   .. code-block:: inform
269
270      Constant Headline
271            "^A simple Inform example
272             ^by Roger Firth and Sonja Kesserich.^";
273
274 Just to repeat what we said earlier: every game that you design will start
275 out from a basic source file like this (in fact, it might be sensible to
276 keep a copy of this template file in a safe place, as a starting point for
277 future games).  Think of this stuff as the basic preparation which you'll
278 quickly come to take for granted, much as a landscape artist always begins
279 by sizing the canvas before starting to paint.  So, now that we've taken a
280 quick tour of Inform's general needs, we can start thinking about what this
281 particular game requires.
282
283 Defining the game's locations
284 =============================
285
286 A good starting point in any game is to think about the locations which are
287 involved: this sketch map shows the four that we'll use:
288
289 .. image:: /images/heidi1.*
290    :align: center
291
292 In IF, we talk about each of these locations as a :term:`room`, even though
293 in this example none of them has four walls.  So let's use Inform to define
294 those rooms.  Here's a first attempt:
295
296 .. code-block:: inform
297
298    Object   "In front of a cottage"
299      with   description
300                 "You stand outside a cottage. The forest stretches east.",
301       has   light;
302
303    Object   "Deep in the forest"
304      with   description
305                "Through the dense foliage, you glimpse a building to the west.
306                 A track heads to the northeast.",
307       has   light;
308
309    Object   "A forest clearing"
310      with   description
311                "A tall sycamore stands in the middle of this clearing.
312                 The path winds southwest through the trees.",
313       has   light;
314
315    Object   "At the top of the tree"
316      with   description "You cling precariously to the trunk.",
317       has   light;
318
319 Again, we can infer some general principles from these four examples:
320
321 * A room definition starts with the word ``Object`` and ends, about four
322   lines later, with a semicolon.  Each of the components that appears in
323   your game -- not only the rooms, but also the people, the things that you
324   see and touch, intangibles like a sound, a smell, a gust of wind -- is
325   defined in this way; think of an "object" simply as the general term for
326   the myriad thingies which together comprise the model world which your
327   game inhabits.
328
329 * The phrase in double quotes following the word ``Object`` is the name
330   that the interpreter uses to provide the player character with a list of
331   the objects around her: where she is, what she can see, what she's
332   holding, and so on.
333
334   .. note::
335
336      We're using the word "player" to mean both the person who is playing
337      the game, and the principal protagonist (often known as the player
338      character) within the game itself.  Since the latter -- Heidi -- is
339      female, we'll refer to the player as "she" while discussing this game.
340
341 * A keyword ``with`` follows, which simply tells the compiler what to
342   expect next.
343
344 .. Generated by autoindex
345 .. index::
346    pair: description; library property
347
348 * The word :prop:`description`, introducing another piece of text which
349   gives more detail about the object: in the case of a room, it's the
350   appearance of the surrounding environment when the player character is in
351   that room.  The textual description is given in double quotes, and is
352   followed by a comma.
353
354 * Near the end, the keyword ``has`` appears, which again tells the compiler
355   to expect a certain kind of information.
356
357 .. Generated by autoindex
358 .. index::
359    pair: light; library attribute
360
361 * The word :attr:`light` says that this object is a source of illumination,
362   and that therefore the player character can see what's happening here.
363   There has to be at least one light source in every room (unless you want
364   the player to be told that "It's pitch dark and you can't see a thing");
365   most commonly, that light source is the room itself.
366
367 A smidgeon of background may help set this into context (there's more in
368 the next chapter).  An object can have both :term:`properties` (introduced
369 by the keyword ``with``) and :term:`attributes` (written after the word
370 ``has``).  A property has both a name (like :prop:`description`) and a
371 value (like the character string "``You stand outside a cottage.  The
372 forest stretches east.``"); an attribute has merely a name.
373
374 In a little while, when you play this game, you'll observe that it starts
375 like this:
376
377 .. code-block:: transcript
378
379    In front of a cottage
380    You stand outside a cottage. The forest stretches east.
381
382 And here you can see how the room's name (``In front of a cottage``) and
383 description (``You stand outside a cottage.  The forest stretches east.``)
384 are used.
385
386 Joining up the rooms
387 ====================
388
389 We said that this was a first attempt at defining the rooms; it's fine as
390 far as it goes, but a few bits of information are missing.  If you look at
391 the game's sketch map, you can see how the rooms are intended to be
392 connected; from "Deep in the forest", for example, the player character
393 should be able to move west towards the cottage, or northeast to the
394 clearing.  Now, although our descriptions mention or imply these available
395 routes, we also need to explicitly add them to the room definitions in a
396 form that the game itself can make sense of.  Like this:
397
398 .. code-block:: inform
399
400    Object   before_cottage "In front of a cottage"
401      with   description
402                 "You stand outside a cottage. The forest stretches east.",
403             e_to forest,
404      has    light;
405
406    Object   forest "Deep in the forest"
407      with   description
408                 "Through the dense foliage, you glimpse a building to the west.
409                  A track heads to the northeast.",
410             w_to before_cottage,
411             ne_to clearing,
412      has    light;
413
414    Object   clearing "A forest clearing"
415      with   description
416                 "A tall sycamore stands in the middle of this clearing.
417                  The path winds southwest through the trees.",
418             sw_to forest,
419             u_to top_of_tree,
420      has    light;
421
422    Object   top_of_tree "At the top of the tree"
423      with   description "You cling precariously to the trunk.",
424             d_to clearing,
425      has    light;
426
427 We've made two changes to the room objects.
428
429 * First, between the word ``Object`` and the object's name in double
430   quotes, we've inserted a different type of name: a private, internal
431   identification, never seen by the player; one that we can use *within*
432   the source file when one object needs to refer to another object.  For
433   example, the first room is identified as ``before_cottage``, and the
434   second as ``forest``.
435
436   Unlike the external name contained in double quotes, the internal
437   identifier has to be a single word -- that is, without spaces.  To aid
438   readability, we often use an underscore character to act as sort of
439   pseudo-space: ``before_cottage`` is a bit clearer than ``beforecottage``.
440
441 * Second, we've added lines after the object descriptions which use those
442   internal identifiers to show how the rooms are connected; one line for
443   each connection.  The ``before_cottage`` object has this additional
444   line::
445
446      e_to forest,
447
448   This means that a player standing in front of the cottage can type GO
449   EAST (or EAST, or just E), and the game will transport her to the room
450   whose internal identification is ``forest``.  If she tries to move in any
451   other direction from this room, she'll be told "You can't go that way".
452
453   What we've just defined is a *one-way* easterly connection:
454   ``before_cottage`` â†’ ``forest``.  The forest object has two additional
455   lines::
456
457      w_to before_cottage,
458      ne_to clearing,
459
460   The first line defines a westerly connection ``forest`` â†’
461   ``before_cottage`` (thus enabling the player character to return to the
462   cottage), and the second defines a connection ``forest`` â†’ ``clearing``
463   which heads off to the northeast.
464
465   .. Generated by autoindex
466   .. index::
467      pair: d_to; library property
468      pair: e_to; library property
469      pair: in_to; library property
470      pair: n_to; library property
471      pair: ne_to; library property
472      pair: nw_to; library property
473      pair: out_to; library property
474      pair: s_to; library property
475      pair: se_to; library property
476      pair: sw_to; library property
477      pair: u_to; library property
478      pair: w_to; library property
479
480   Inform provides for eight "horizontal" connections (:prop:`n_to`,
481   :prop:`ne_to`, :prop:`e_to`, :prop:`se_to`, :prop:`s_to`, :prop:`sw_to`,
482   :prop:`w_to`, :prop:`nw_to`) two "vertical" ones (:prop:`u_to`,
483   :prop:`d_to`) and two specials :prop:`in_to`, and :prop:`out_to`.  You'll
484   see some of these used for the remaining inter-room connections.
485
486 There's one last detail to attend to before we can test what we've done.
487 You'll recollect that our story begins with Heidi standing in front of her
488 cottage.  We need to tell the interpreter that ``before_cottage`` is the
489 room where the game starts, and we do this in the ``Initialise`` routine::
490
491     [ Initialise; location = before_cottage; ];
492
493 :var:`location` is a :term:`variable`, part of the library, which tells the
494 interpreter in which room the player character currently is.  Here, we're
495 saying that, at the start of the game, the player character is in the
496 ``before_cottage`` room.
497
498 Now we can add what we've done to the ``Heidi.inf`` source file template.
499 At this stage, you should study the four room definitions, comparing them
500 with the sketch map until you're comfortable that you understand how to
501 create simple rooms and define the connections between them.
502
503 .. include:: /config/typethis.rst
504
505 .. code-block:: inform
506
507    !============================================================================
508    Constant Story "Heidi";
509    Constant Headline
510                "^A simple Inform example
511                 ^by Roger Firth and Sonja Kesserich.^";
512
513    Include "Parser";
514    Include "VerbLib";
515
516    !============================================================================
517    ! The game objects
518
519    Object   before_cottage "In front of a cottage"
520      with   description
521                 "You stand outside a cottage. The forest stretches east.",
522             e_to forest,
523      has    light;
524
525    Object   forest "Deep in the forest"
526      with   description
527                 "Through the dense foliage, you glimpse a building to the west.
528                  A track heads to the northeast.",
529             w_to before_cottage,
530             ne_to clearing,
531      has    light;
532
533    Object   clearing "A forest clearing"
534      with   description
535                 "A tall sycamore stands in the middle of this clearing.
536                  The path winds southwest through the trees.",
537             sw_to forest,
538             u_to top_of_tree,
539      has    light;
540
541    Object   top_of_tree "At the top of the tree"
542      with   description "You cling precariously to the trunk.",
543             d_to clearing,
544      has    light;
545
546    !============================================================================
547    ! Entry point routines
548
549    [ Initialise; location = before_cottage; ];
550
551    !============================================================================
552    ! Standard and extended grammar
553
554    Include "Grammar";
555
556    !============================================================================
557
558 Type this in, as always taking great care with the punctuation -- watch
559 those commas and semicolons.  Compile it, and fix any mistakes which the
560 compiler reports.  You can then play the game in its current state.
561 Admittedly, you can't do very much, but you should be able to move freely
562 among the four rooms that you've defined.
563
564 .. note::
565
566    In order to minimise the amount of typing that you have to do, the
567    descriptive text in this game has been kept as short as possible.  In a
568    real game, you would typically provide more interesting descriptions
569    than these.
570
571 Adding the bird and the nest
572 ============================
573
574 Given what we said earlier, you won't be surprised to hear that both the
575 bird and its nest are Inform objects.  We'll start their definitions like
576 this:
577
578 .. code-block:: inform
579
580    Object  bird "baby bird"
581      with  description "Too young to fly, the nestling tweets helplessly.",
582       has  ;
583
584    Object  nest "bird's nest"
585      with  description "The nest is carefully woven of twigs and moss.",
586       has  ;
587
588 You can see that these definitions have exactly the same format as the
589 rooms we defined previously: a one-word internal identifier (``bird``,
590 ``nest``), and a word or phrase naming the object for the player's benefit
591 (``baby bird``, ``bird's nest``).  They both have some descriptive detail:
592 for a room this is printed when the player first enters, or when she types
593 LOOK; for other objects it's printed when she EXAMINEs that object.  What
594 they *don't* have are connections (:prop:`e_to`, :prop:`w_to`, etc.  apply
595 only to rooms) or :attr:`light` (it's not necessary -- the rooms ensure
596 that light is available).
597
598 When the game is running, the player will want to refer to these two
599 objects, saying for instance EXAMINE THE BABY BIRD or PICK UP THE NEST.
600 For this to work reliably, we need to specify the word (or words) which
601 relate to each object.  Our aim here is flexibility: providing a choice of
602 relevant vocabulary so that the player can use whatever term seems
603 appropriate to her, with a good chance of it being understood.  We add a
604 line to each definition:
605
606 .. include:: /config/typethis.rst
607
608 .. code-block:: inform
609
610    Object  bird "baby bird"
611      with  description "Too young to fly, the nestling tweets helplessly.",
612            name 'baby' 'bird' 'nestling',
613       has  ;
614
615    Object  nest "bird's nest"
616      with  description "The nest is carefully woven of twigs and moss.",
617            name 'bird^s' 'nest' 'twigs' 'moss',
618       has  ;
619
620 The :prop:`name` introduces a list in single quotes '...'.  We call each of
621 those quoted things a :term:`dictionary word`, and we do mean "word", not
622 "phrase" (``'baby'``\ ``'bird'`` rather than ``'baby bird'``); you can't
623 uses spaces, commas or periods *in* dictionary words, though there's a
624 space *between* each one, and the whole list ends with a comma.  The idea
625 is that the interpreter decides which object a player is talking about by
626 matching what she types against the full set of all dictionary words.  If
627 the player mentions BIRD, or BABY BIRD, or NESTLING, it's the ``baby bird``
628 that she means; if she mentions NEST, BIRD'S NEST or MOSS, it's the
629 ``bird's nest``. And if she types NEST BABY or BIRD TWIGS, the interpreter
630 will politely say that it doesn't understand what on earth she's talking
631 about.
632
633 .. index::
634    single: apostrophes
635
636 .. note::
637
638    You'll notice the use of ``'bird^s'`` to define the dictionary word
639    BIRD'S; this oddity is necessary because the compiler expects the single
640    quotes in the list always to come in pairs -- one at the start of the
641    dictionary word, and one at the end.  If we had typed ``'bird's'`` then
642    the compiler would find the opening quote, the four letters ``b``,
643    ``i``, ``r`` and ``d``, and what looks like the closing quote.  So far
644    so good; it's read the word BIRD and now expects a space before the next
645    opening quote... but instead finds ``s'`` which makes no sense.  In
646    cases like this we must use the circumflex ``^`` to *represent* the
647    apostrophe, and the compiler then treats ``bird's`` as a dictionary
648    word.
649
650 You may be wondering why we need a list of :prop:`name` words for the bird
651 and its nest, yet we didn't when we defined the rooms?  It's because the
652 player can't interact with a room in the same way as with other objects;
653 for example, she doesn't need to say EXAMINE THE FOREST -- just being there
654 and typing LOOK is sufficient.
655
656 .. Generated by autoindex
657 .. index::
658    pair: container; library attribute
659    pair: open; library attribute
660
661 The bird's definition is complete, but there's an additional complexity
662 with the nest: we need to be able to put the bird into it.  We do this by
663 labelling the nest as a :attr:`container` -- able to hold other objects --
664 so that the player can type PUT (or INSERT) BIRD IN (or INTO) NEST.
665 Furthermore, we label it as :attr:`open`; this prevents the interpreter
666 from asking us to open it before putting in the bird.
667
668 .. code-block:: inform
669
670    Object   nest "bird's nest"
671      with   description "The nest is carefully woven of twigs and moss.",
672             name 'bird^s' 'nest' 'twigs' 'moss',
673      has    container open;
674
675 Both objects are now defined, and we can incorporate them into the game.
676 To do this, we need to choose the locations where the player will find
677 them.  Let's say that the bird is found in the forest, while the nest is in
678 the clearing.  This is how we set this up:
679
680 .. code-block:: inform
681
682    Object   bird "baby bird" forest
683      with   description "Too young to fly, the nestling tweets helplessly.",
684             name 'baby' 'bird' 'nestling',
685      has    ;
686
687    Object   nest "bird's nest" clearing
688      with   description "The nest is carefully woven of twigs and moss.",
689             name 'bird^s' 'nest' 'twigs' 'moss',
690      has    container open;
691
692 Read that first line as: "Here's the definition of an object which is
693 identified within this file as ``bird``, which is known to the player as
694 ``baby bird``, and which is initially located inside the object identified
695 within this file as ``forest``."
696
697 Where in the source file do these new objects fit?  Well, anywhere really,
698 but you'll find it convenient to insert them following the rooms where
699 they're found.  This means adding the bird just after the forest, and the
700 nest just after the clearing.  Here's the middle piece of the source file:
701
702 .. code-block:: inform
703
704    !============================================================================
705    ! The game objects
706
707    Object  before_cottage "In front of a cottage"
708      with  description
709                 "You stand outside a cottage. The forest stretches east.",
710            e_to forest,
711       has  light;
712
713    Object  forest "Deep in the forest"
714      with  description
715                 "Through the dense foliage, you glimpse a building to the west.
716                  A track heads to the northeast.",
717            w_to before_cottage,
718            ne_to clearing,
719       has  light;
720
721    Object  bird "baby bird" forest
722      with  description "Too young to fly, the nestling tweets helplessly.",
723            name 'baby' 'bird' 'nestling',
724       has  ;
725
726    Object  clearing "A forest clearing"
727      with  description
728                 "A tall sycamore stands in the middle of this clearing.
729                  The path winds southwest through the trees.",
730            sw_to forest,
731            u_to top_of_tree,
732       has  light;
733
734    Object  nest "bird's nest" clearing
735      with  description "The nest is carefully woven of twigs and moss.",
736            name 'bird^s' 'nest' 'twigs' 'moss',
737       has  container open;
738
739    Object  top_of_tree "At the top of the tree"
740      with  description "You cling precariously to the trunk.",
741            d_to clearing,
742       has  light;
743
744    !============================================================================
745
746 Make those changes, recompile the game, play it and you'll see this:
747
748 .. code-block:: transcript
749
750    Deep in the forest
751    Through the dense foliage, you glimpse a building to the west. A track heads
752    to the northeast.
753
754    You can see a baby bird here.
755
756    >
757
758 Adding the tree and the branch
759 ==============================
760
761 The description of the clearing mentions a tall sycamore tree, up which the
762 player character supposedly "climbs".  We'd better define it:
763
764 .. include:: /config/typethis.rst
765
766 .. code-block:: inform
767
768    Object   tree "tall sycamore tree" clearing
769      with   description
770                 "Standing proud in the middle of the clearing,
771                  the stout tree looks easy to climb.",
772             name 'tall' 'sycamore' 'tree' 'stout' 'proud',
773      has    scenery;
774
775 Everything there should be familiar, apart from that :attr:`scenery` at the
776 end. We've already mentioned the tree in the description of the forest
777 clearing, so we don't want the interpreter adding "You can see a tall
778 sycamore tree here" afterwards, as it does for the bird and the nest.  By
779 labelling the tree as :attr:`scenery` we suppress that, and also prevent it
780 from being picked up by the player character.  One final object: the branch
781 at the top of the tree.  Again, not many surprises in this definition:
782
783 .. include:: /config/typethis.rst
784
785 .. code-block:: inform
786
787    Object   branch "wide firm bough" top_of_tree
788      with   description "It's flat enough to support a small object.",
789             name 'wide' 'firm' 'flat' 'bough' 'branch',
790      has    static supporter;
791
792 The only new things are those two labels.  :attr:`static` is similar to
793 :attr:`scenery`: it prevents the branch from being picked up by the player
794 character, but *doesn't* suppress mention of it when describing the
795 setting.  And :attr:`supporter` is rather like the :attr:`container` that
796 we used for the nest, except that this time the player character can put
797 other objects *onto* the branch.  (In passing, we'll mention that an object
798 can't normally be both a :attr:`container` *and* a :attr:`supporter`.)  And
799 so here are our objects again:
800
801 .. code-block:: inform
802
803    !============================================================================
804    ! The game objects
805
806    Object   before_cottage "In front of a cottage"
807      with   description
808                 "You stand outside a cottage. The forest stretches east.",
809             e_to forest,
810      has    light;
811
812    Object   forest "Deep in the forest"
813      with   description
814                 "Through the dense foliage, you glimpse a building to the west.
815                  A track heads to the northeast.",
816             w_to before_cottage,
817             ne_to clearing,
818      has    light;
819
820    Object   bird "baby bird" forest
821      with   description "Too young to fly, the nestling tweets helplessly.",
822             name 'baby' 'bird' 'nestling',
823      has    ;
824
825    Object   clearing "A forest clearing"
826      with   description
827                 "A tall sycamore stands in the middle of this clearing.
828                  The path winds southwest through the trees.",
829             sw_to forest,
830             u_to top_of_tree,
831      has    light;
832
833    Object   nest "bird's nest" clearing
834      with   description "The nest is carefully woven of twigs and moss.",
835             name 'bird^s' 'nest' 'twigs' 'moss',
836       has   container open;
837
838    Object   tree "tall sycamore tree" clearing
839      with   description
840                 "Standing proud in the middle of the clearing,
841                  the stout tree looks easy to climb.",
842             name 'tall' 'sycamore' 'tree' 'stout' 'proud',
843       has   scenery;
844
845    Object   top_of_tree "At the top of the tree"
846      with   description "You cling precariously to the trunk.",
847             d_to clearing,
848       has   light;
849
850    Object   branch "wide firm bough" top_of_tree
851      with   description "It's flat enough to support a small object.",
852             name 'wide' 'firm' 'flat' 'bough' 'branch',
853       has   static supporter;
854
855    !============================================================================
856
857 Once again, make the changes, recompile, and investigate what you can do in
858 your model world.
859
860 Finishing touches
861 =================
862
863 Our first pass at the game is nearly done; just two more changes to
864 describe.  The first is easy: Heidi wouldn't be able to climb the tree
865 carrying the bird and the nest separately: we want the player character to
866 put the bird into the nest first.  One easy way to enforce this is by
867 adding a line near the top of the file:
868
869 .. include:: /config/typethis.rst
870
871 .. code-block:: inform
872
873    !============================================================================
874    Constant Story "Heidi";
875    Constant Headline
876                "^A simple Inform example
877                 ^by Roger Firth and Sonja Kesserich.^";
878
879    Constant MAX_CARRIED 1;
880
881 The value of ``MAX_CARRIED`` limits the number of objects that the player
882 character can be holding at any one time; by setting it to 1, we're saying
883 that she can carry the bird or the nest, but not both.  However, the limit
884 ignores the contents of :attr:`container` or :attr:`supporter` objects, so
885 the nest with the bird inside it is still counted as one object.
886
887 The other change is slightly more complex and more important: there's
888 currently no way to "win" the game!  The goal is for the player character
889 to put the bird in the nest, take the nest to the top of the tree, and
890 place it on the branch; when that happens, the game should be over.  This
891 is one way of making it happen:
892
893 .. include:: /config/typethis.rst
894
895 .. code-block:: inform
896
897    Object  branch "wide firm bough" top_of_tree
898      with  description "It's flat enough to support a small object.",
899            name 'wide' 'firm' 'flat' 'bough' 'branch',
900            each_turn [; if (nest in branch) deadflag = 2; ],
901       has  static supporter;
902
903 .. note::
904
905    Here's an explanation of what's going on.  If you find this difficult to
906    grasp, don't worry.  It's the hardest bit so far, and it introduces
907    several new concepts all at once.  Later in the guide, we'll explain
908    those concepts more clearly, so you can just skip this bit if you want.
909
910    .. Generated by autoindex
911    .. index::
912       pair: deadflag; library variable
913
914    The variable :var:`deadflag`, part of the library, is normally 0.  If
915    you set its value to 2, the interpreter notices and ends the game with
916    "You have won".  The statement::
917
918          if (nest in branch) deadflag = 2;
919
920    should be read as: "Test whether the ``nest`` is currently in the
921    ``branch`` (if the branch is a :attr:`container`) or on it (if the
922    ``branch`` is a supporter); if it is, set the value of :var:`deadflag`
923    to 2; if it isn't, do nothing."  The surrounding part::
924
925          each_turn [; ... ],
926
927    should be read as: "At the end of each turn (when the player is in the
928    same room as the branch), do whatever is written inside the square
929    brackets".  So, putting that all together:
930
931    * At the end of each turn (after the player has typed something and
932      pressed the Enter key, and the interpreter has done whatever was
933      requested) the interpreter checks whether the player and the
934      ``branch`` are in the same room.  If not, nothing happens.  If they're
935      together, it looks to see where the nest is.  Initially it's in the
936      ``clearing``, so nothing happens.
937
938    * Also at the end of each turn, the interpreter checks the value of
939      :var:`deadflag`.  Usually it's 0, so nothing happens.
940
941    * Finally the player character puts the ``nest`` on the ``branch``.
942      "Aha!"  says the interpreter (to itself, of course), and sets the
943      value of :var:`deadflag` to 2.
944
945    * Immediately afterwards, (another part of) the interpreter checks and
946      finds that the value of :var:`deadflag` has changed to 2, which means
947      that the game is successfully completed; so, it says to the player,
948      "you've won!"
949
950 That's as far as we'll take this example for now.  Make those final
951 changes, recompile, and test what you've achieved.  You'll probably find a
952 few things that could be done better -- even on a simple game like this
953 there's considerable scope for improvement -- so we'll revisit Heidi in her
954 forest shortly.  First, though, we'll recap what we've learnt so far.