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