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