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