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