619f16f00936ab4a1067143d9a3633b9b4e29d0c
[ibg.git] / chapters / 07.rst
1 ===============================
2  William Tell: the early years
3 ===============================
4
5 .. highlight:: inform
6
7 .. epigraph::
8
9    | |CENTER| *M was a miser, and hoarded up gold;*
10    | |CENTER| *N was a nobleman, gallant and bold.*
11
12 .. only:: html
13
14    .. image:: /images/picM.png
15       :align: left
16
17 |M|\oving along swiftly, we'll define the first two rooms and populate them
18 with assorted townspeople and street furniture, we'll equip Wilhelm with
19 his trusty bow and quiver of arrows, and we'll introduce Helga the friendly
20 stallholder.
21
22 Defining the street
23 ===================
24
25 This is the street room, the location where the game starts::
26
27    Room     street "A street in Altdorf"
28      with   description [;
29                  print "The narrow street runs north towards the town square.
30                      Local folk are pouring into the town through the gate to the
31                      south, shouting greetings, offering produce for sale,
32                      exchanging news, enquiring with exaggerated disbelief about
33                      the prices of the goods displayed by merchants whose stalls
34                      make progress even more difficult.^";
35                  if (self hasnt visited)
36                      print "^~Stay close to me, son,~ you say,
37                          ~or you'll get lost among all these people.~^";
38             ],
39             n_to below_square,
40             s_to
41                  "The crowd, pressing north towards the square,
42                   makes that impossible.";
43
44 We're using our new ``Room`` class, so there's no need for a ``light``
45 attribute.  The ``n_to`` and ``s_to`` properties, whose values are an
46 internal ID and a string respectively, are techniques we've used before.
47 The only innovation is that the ``description`` property has an embedded
48 routine as its value.
49
50 The first thing in that routine is a ``print`` statement, displaying
51 details of the street surroundings.  If that was all that we wanted to do,
52 we could have supplied those details by making the ``description`` value a
53 string; that is, these two examples behave identically::
54
55    description [;
56        print "The narrow street runs north towards the town square.
57            Local folk are pouring into the town through the gate to the
58            south, shouting greetings, offering produce for sale,
59            exchanging news, enquiring with exaggerated disbelief about
60            the prices of the goods displayed by merchants whose stalls
61            make progress even more difficult.^";
62    ],
63
64    description
65        "The narrow street runs north towards the town square.
66         Local folk are pouring into the town through the gate to the
67         south, shouting greetings, offering produce for sale,
68         exchanging news, enquiring with exaggerated disbelief about
69         the prices of the goods displayed by merchants whose stalls
70         make progress even more difficult.",
71
72 However, that *isn't* all that we want to do.  Having presented the basic
73 description, we're going to display that little line of dialogue, where
74 Wilhelm tells his son to be careful.  And we want to do that only once, the
75 very first time that the street's description is displayed.  If the player
76 types LOOK a few times, or moves north and then returns south to the
77 street, we're happy to see the surroundings described -- but we don't want
78 that dialogue again.  This is the pair of statements that makes it happen::
79
80    if (self hasnt visited)
81        print "^~Stay close to me, son,~ you say,
82            ~or you'll get lost among all these people.~^";
83
84 The line of dialogue is produced by the ``print`` statement, the ``print``
85 statement is controlled by the ``if`` statement, and the ``if`` statement
86 is performing the test ``self hasnt visited``.  In detail:
87
88 * ``visited`` is an attribute, but not one that you'd normally give to an
89   object yourself.  It's automatically applied to a room object by the
90   interpreter, but only after that room has been visited for the first
91   time by the player.
92
93 * ``hasnt`` (and ``has``) are available for testing whether a given
94   attribute is currently set for a given object.  :samp:`{X} has {Y}` is
95   true if object :samp:`{X}` currently has attribute :samp:`{Y}`, false if
96   it doesn't.  To make the test in reverse, :samp:`{X} hasnt {Y}` is true
97   if object :samp:`{X}` currently does not have attribute :samp:`{Y}`,
98   false if it does.
99
100 * ``self``, which we met in the previous chapter, is that useful variable
101   which, within an object, always refers to that object.  Since we're using
102   it in the middle of the ``street`` object, that's what it refers to.
103
104 So, putting it all together, ``self hasnt visited`` is true (and therefore
105 the ``print`` statement is executed) only while the ``street`` object has
106 *not* got a ``visited`` attribute.  Because the interpreter automatically
107 gives rooms a ``visited`` attribute as soon as the player has been there
108 once, this test will be true only for one turn.  Therefore, the line of
109 dialogue will be displayed only once: the first time the player visits the
110 street, at the very start of the game.
111
112 Although the primary importance of ``self`` is within class definitions, it
113 can also be convenient to use it simply within an object.  Why didn't we
114 just write this? ::
115
116    if (street hasnt visited)
117        print "^~Stay close to me, son,~ you say,
118            ~or you'll get lost among all these people.~^";
119
120 It's true that the effect is identical, but there are a couple of good
121 reasons for using ``self``.  One: it's an aid to understanding your code
122 days or weeks after writing it.
123
124 If you read the line ``if (street hasnt visited)``, you need to think for a
125 moment about which object is being tested; oh, it's this one.  When you
126 read ``if (self hasnt visited)``, you immediately *know* which object we're
127 talking about.
128
129 Another reason is auto-plagiarism.  Many times you'll find that a chunk of
130 code is useful in different situations (say, you want to repeat the
131 mechanics of the street description in another room).  Rather than writing
132 everything from scratch, you'll typically use copy-and-paste to repeat the
133 routine, and then all you have to do is compose the appropriate descriptive
134 strings for the new room.  If you've used ``self``, the line ``if (self
135 hasnt visited)`` is still good; if you've written instead ``if (street
136 hasnt visited)``, you'll have to change that as well.  Worse, if you
137 *forget* to change it, the game will still work -- but not in the way you'd
138 intended, and the resulting bug will be quite difficult to track down.
139
140 .. _adding-props:
141
142 Adding some props
143 =================
144
145 The street's description mentions various items -- the gate, the people,
146 etc. -- which ought to exist within the game (albeit only in minimal form)
147 to sustain the illusion of hustle and bustle.  Our ``Prop`` class is ideal
148 for this::
149
150    Prop     "south gate" street
151      with   name 'south' 'southern' 'wooden' 'gate',
152             description "The large wooden gate in the town walls is wide open.";
153
154    Prop     "assorted stalls"
155      with   name 'assorted' 'stalls',
156             description "Food, clothing, mountain gear; the usual stuff.",
157             found_in street below_square,
158      has    pluralname;
159
160    Prop     "produce"
161      with   name 'goods' 'produce' 'food' 'clothing' 'mountain' 'gear' 'stuff',
162             description "Nothing special catches your eye.",
163             found_in street below_square,
164      has    pluralname;
165
166    Prop     "merchants"
167      with   name 'merchant' 'merchants' 'trader' 'traders',
168             description
169                 "A few crooks, but mostly decent traders touting their wares
170                  with raucous overstatement.",
171             found_in street below_square,
172      has    animate pluralname;
173
174    Prop     "local people"
175      with   name 'people' 'folk' 'local' 'crowd',
176             description "Mountain folk, just like yourself.",
177             found_in [; return true; ],
178      has    animate pluralname;
179
180 .. note:: 
181
182    Because these objects are not referenced by other objects, we haven't
183    bothered to given them internal :samp:`{obj_ids}` (though we could have;
184    it wouldn't make any difference).  However, we *have* provided
185    :samp:`{external_names}`, because these are used by the ``Prop`` class's
186    ``print_ret ... (the) self`` statement.
187
188 You'll see a couple of new attributes: ``animate`` marks an object as being
189 "alive", while ``pluralname`` specifies that its external name is plural
190 rather than singular.  The interpreter uses these attributes to ensure that
191 messages about such objects are grammatical and appropriate (for example,
192 it will now refer to "some merchants" rather than "a merchants").  Because
193 the library handles so many situations automatically, it's hard to be sure
194 exactly what messages players may trigger; the best approach is to play
195 safe and always give an object the relevant set of attributes, even when,
196 as here, they probably won't be needed.
197
198 You'll also see a new ``found_in`` property, which specifies the rooms --
199 and only the rooms; ``found_in`` shouldn't be used to place objects inside
200 containers or supporters -- where this object is to appear.  The stalls,
201 for example, can be EXAMINEd both in the street and below the square, so we
202 *could* have created a ``Prop`` object in each room::
203
204    Prop       "assorted stalls" street
205      with     name 'assorted' 'stalls',
206               description "Food, clothing, mountain gear; the usual stuff.",
207       has     pluralname;
208
209    Prop       "assorted stalls" below_square
210      with     name 'assorted' 'stalls',
211               description "Food, clothing, mountain gear; the usual stuff.",
212       has     pluralname;
213
214 but ``found_in`` does the same job more neatly -- there's only one object,
215 but it appears in both the ``street`` and ``below_square`` rooms while the
216 player's there.  The local people are even more ubiquitous.  In this case
217 the ``found_in`` value is an embedded routine rather than a list of rooms;
218 such a routine would generally test the value of the current location and
219 ``return true`` if it wants to be present here, or ``false`` if not.  Since
220 we'd like the local people *always* to be present, in every room, we
221 ``return true`` without bothering to examine ``location``.  It's as though
222 we'd written any of these, but simpler and less error prone::
223
224    Prop       "local people"
225      with     name 'people' 'folk' 'local' 'crowd',
226               description "Mountain folk, just like yourself.",
227               found_in street below_square south_square mid_square north_square
228                   marketplace,
229       has     animate pluralname;
230
231    Prop       "local people"
232      with     name 'people' 'folk' 'local' 'crowd',
233               description "Mountain folk, just like yourself.",
234               found_in [;
235                   if (location == street       || location == below_square ||
236                       location == south_square || location == mid_square ||
237                       location == north_square || location == marketplace)
238                       return true;
239                   return false;
240               ],
241       has     animate pluralname;
242
243    Prop     "local people"
244      with   name 'people' 'folk' 'local' 'crowd',
245             description "Mountain folk, just like yourself.",
246             found_in [;
247                 if (location == street or below_square or south_square or
248                     mid_square or north_square or marketplace) return true;
249                 return false;
250             ],
251      has    animate pluralname;
252
253 In the second example, you'll see the ``||`` operator, to be read as "or",
254 which we mentioned near the end of "Heidi"; it combines the various
255 :samp:`location == {some_room}` comparisons so that the ``if`` statement is
256 true if *any* of those individual tests is true.  And in the third example
257 we introduce the ``or`` keyword, which is a more succinct way of achieving
258 exactly the same result.
259
260 .. _possessions:
261
262 The player's possessions
263 ========================
264
265 Since our ``Initialise`` routine has already mentioned them, we might as
266 well define Wilhelm's bow and arrows::
267
268    Object   bow "bow"
269      with   name 'bow',
270             description "Your trusty yew bow, strung with flax.",
271             before [;
272                Drop,Give,ThrowAt:
273                  print_ret "You're never without your trusty bow.";
274             ],
275      has    clothing;
276
277    Object   quiver "quiver"
278      with   name 'quiver',
279             description
280                  "Made of goatskin, it usually hangs over your left shoulder.",
281             before [;
282                Drop,Give,ThrowAt:
283                  print_ret "But it was a present from Hedwig, your wife.";
284             ],
285      has    container open clothing;
286
287 Both of these are straightforward objects, with the ``Drop``, ``Give`` and
288 ``ThrowAt`` actions being intercepted to ensure that Wilhelm is never
289 without them.  The ``clothing`` attribute makes its first appearance,
290 marking both the quiver and the bow as capable of being worn (as the result
291 of a WEAR BOW command, for instance); you'll remember that our
292 ``Initialise`` routine goes on to add a ``worn`` attribute to the quiver.
293
294 An empty quiver is pretty useless, so here's the class used to define
295 Wilhelm's stock of arrows.  This class has some unusual features::
296
297    Class    Arrow
298      with   name 'arrow' 'arrows//p',
299             article "an",
300             plural "arrows",
301             description "Just like all your other arrows -- sharp and true.",
302             before [;
303                Drop,Give,ThrowAt:
304                  print_ret "Your arrows are sharp, and you guard them carefully.";
305             ];
306
307 The classes we've created so far -- ``Room``, ``Prop`` and ``Furniture`` --
308 are intended for objects which behave the same but are otherwise clearly
309 separate.  For example, a table, a bed and a wardrobe would generally have
310 their own individual characteristics -- a name, a description, maybe some
311 specialised properties -- while still inheriting the general behaviour of
312 ``Furniture`` objects.  The arrows aren't like this: not only do they
313 behave the same, but also they are indistinguishable one from another.
314 We're trying for this effect:
315
316 .. code-block:: transcript
317
318    >INVENTORY
319    You are carrying:
320      a quiver (being worn)
321        three arrows
322      a bow
323
324 where the interpreter lumps together our stock of three arrows, rather than
325 listing them individually in this clumsy fashion:
326
327 .. code-block:: transcript
328
329    >INVENTORY
330    You are carrying:
331      a quiver (being worn)
332        an arrow
333        an arrow
334        an arrow
335      a bow
336
337 The interpreter will do this for us if our objects are "indistinguishable",
338 best achieved by making them members of a class which includes both
339 ``name`` and ``plural`` properties.  We define the actual arrows very
340 simply, like this::
341
342    Arrow "arrow" quiver;
343    Arrow "arrow" quiver;
344    Arrow "arrow" quiver;
345
346 and you can see that we provide only two pieces of information for each
347 ``Arrow`` object: an external name in double quotes ("arrow" in each case)
348 which the interpreter uses when referring to the object, and an initial
349 location (in the quiver).  That's all: no block of properties, no set of
350 attributes, and no internal identifier, because we never need to refer to
351 the individual ``Arrow`` objects within the game.
352
353 The name property of the class definition has an odd-looking dictionary
354 word::
355
356    name 'arrow' 'arrows//p',
357
358 The word ``'arrow'`` refers to a single arrow.  So also would the word
359 ``'arrows'``, unless we specifically tell the interpreter that it's a
360 plural reference.  That ``//p`` marks ``'arrows'`` as being a potential
361 reference to more than one object at once, thus enabling players to type
362 TAKE ARROWS and thereby pick up as many arrows as happened to be available
363 (without it, TAKE ARROWS would have picked up one at random).
364
365 There are two other properties not seen previously::
366
367    article "an",
368    plural "arrows",
369
370 The ``article`` property lets you define the object's indefinite article --
371 usually something like "a", "an" or "some" -- instead of letting the
372 library assign one automatically.  It's a belt-and-braces (OK,
373 belt-and-suspenders) precaution: because "arrow" starts with a vowel, we
374 need to display "an arrow" not "a arrow".  Most interpreters automatically
375 get this right, but just to be on the safe side, we explicitly define the
376 appropriate word.  And the ``plural`` property defines the word to be used
377 when lumping several of these objects together, as in the "three arrows"
378 inventory listing.  The interpreter can't just automatically slap an "s" on
379 the end; the plural of "slice of cake", for example, isn't "slice of
380 cakes".
381
382 Moving further along the street
383 ===============================
384
385 As Wilhelm moves north towards the square, he comes to this room::
386
387    Room     below_square "Further along the street"
388      with   description
389                 "People are still pushing and shoving their way from the southern
390                  gate towards the town square, just a little further north.
391                  You recognise the owner of a fruit and vegetable stall.",
392             n_to south_square,
393             s_to street;
394
395 No surprises there, nor in most of the supporting scenery objects. ::
396
397    Furniture   stall "fruit and vegetable stall" below_square
398      with name 'fruit' 'veg' 'vegetable' 'stall' 'table',
399            description
400                "It's really only a small table, with a big heap of potatoes,
401                 some carrots and turnips, and a few apples.",
402            before [; Search: <<Examine self>>; ],
403      has   scenery;
404
405    Prop     "potatoes" below_square
406      with   name 'potato' 'potatoes' 'spuds',
407             description
408                 "Must be a particularly early variety... by some 300 years!",
409      has    pluralname;
410
411    Prop     "fruit and vegetables" below_square
412      with   name 'carrot' 'carrots' 'turnip' 'turnips' 'apples' 'vegetables',
413             description "Fine locally grown produce.",
414      has    pluralname;
415
416 The only new thing here is the ``before`` property of the fruit'n'veg
417 stall.  The stall's description -- lots of items on a table -- may suggest
418 to players that they can SEARCH through the produce, maybe finding a lucky
419 beetroot or something else interesting.  No such luck -- and we might as
420 well trap the attempt.
421
422 Having intercepted a ``Search`` action, our plan is to respond with the
423 stall's description, as though the player has typed EXAMINE THE STALL.
424 There isn't an easy way for us to stealthily slide those literal words into
425 the interpreter, but we *can* simulate the effect which they'd cause: an
426 action of ``Examine`` applied to the object stall.  This rather cryptic
427 statement does the job::
428
429    <Examine stall>;
430
431 Having diverted the ``Search`` action into an ``Examine`` action, we must
432 tell the interpreter that it doesn't need to do anything else, because
433 we've handled the action ourselves.  We've done that before -- using
434 ``return true`` -- and so a first stab at the ``before`` action looks like
435 this::
436
437    before [; Search: <Examine stall>; return true; ],
438
439 The two-statement sequence ``<...>; return true`` is so common that there's
440 a single statement shortcut: ``<<...>>``.  Also, for exactly the same
441 reason as before, our code is clearer if we use ``self`` instead of
442 ``stall``.  So this is how the property finally stands::
443
444    before [; Search: <<Examine self>>; ],
445
446 A couple of final observations before we leave this topic.  The example
447 here is of an action (``Examine``) applied to an object (``self``, though
448 ``stall`` or ``noun`` would also work at this point).  You can also use the
449 ``<...>`` and ``<<...>>`` statements for actions which affect no objects::
450
451    <<Look>>;
452
453 (representing the command LOOK), or which affect two.  For example, the
454 command PUT THE BIRD IN THE NEST can be simulated with this statement::
455
456    <<Insert bird nest>>;
457
458 Introducing Helga
459 =================
460
461 One of the trickiest aspects of designing a good game is to provide
462 satisfying interaction with other characters.  It's hard enough to code
463 inanimate objects which provoke appropriate responses to whatever actions
464 the player character (PC) might attempt.  That all gets much worse once
465 those "other objects" are living creatures -- non-player characters (NPCs)
466 -- with, supposedly, minds of their own.  A good NPC might move around
467 independently, perform actions with a purpose, initiate conversations,
468 respond to what you say and do (and even to what you *don't* say or do); it
469 can be a real nightmare.
470
471 But not here: we've kept our three NPCs -- Helga, Walter and the vogt -- as
472 simple as possible.  Nevertheless, we can establish some fundamental
473 principles; here's the class upon which we base our NPCs::
474
475    Class    NPC
476      with   life [;
477                Answer,Ask,Order,Tell:
478                  print_ret "Just use T[ALK] [TO ", (the) self, "].";
479             ],
480      has    animate;
481
482 The most important thing here is the ``animate`` attribute -- that's what
483 defines an object as an NPC, and causes the interpreter to treat it a
484 little differently -- for example, TAKE HELGA results in "I don't suppose
485 Helga would care for that".
486
487 The ``animate`` attribute also brings into play nine extra actions which
488 can be applied only to animate objects: ``Answer``, ``Ask``, ``Order`` and
489 ``Tell`` are all associated with speech, and ``Attack``, ``Kiss``,
490 ``Show``, ``ThrowAt`` and ``WakeOther`` are associated with non-verbal
491 interaction.  Additionally, a new ``life`` property -- very similar to
492 ``before`` -- can be defined to intercept them.  Here we use it to trap
493 speech-related commands such as ASK HELGA ABOUT APPLE and TELL WALTER ABOUT
494 BABIES, telling players that in this game we've implemented only a simpler
495 TALK verb (which we describe in :ref:`verbs`).
496
497 Based on the NPC class we've created, here's Helga::
498
499    NPC      stallholder "Helga" below_square
500      with   name 'stallholder' 'greengrocer' 'monger' 'shopkeeper' 'merchant'
501                  'owner' 'Helga' 'dress' 'scarf' 'headscarf',
502             description
503                  "Helga is a plump, cheerful woman,
504                    concealed beneath a shapeless dress and a spotted headscarf.",
505             initial [;
506                  print "Helga pauses from sorting potatoes
507                       to give you a cheery wave.^";
508                  if (location hasnt visited) {
509                       move apple to player;
510                       print "^~Hello, Wilhelm, it's a fine day for trade! Is this
511                           young Walter? My, how he's grown. Here's an apple for him
512                           -- tell him to mind that scabby part, but the rest's good
513                           enough. How's Frau Tell? Give her my best wishes.~^";
514                  }
515             ],
516             times_spoken_to 0,         ! for counting the conversation topics
517             life [;
518                Talk:
519                  self.times_spoken_to = self.times_spoken_to + 1;
520                  switch (self.times_spoken_to) {
521                    1: score = score + 1;
522                       print_ret "You warmly thank Helga for the apple.";
523                    2: print_ret "~See you again soon.~";
524                    default:
525                       return false;
526                  }
527             ],
528      has    female proper;
529
530 The new attributes are ``female`` -- because we want the interpreter to
531 refer to Helga with the appropriate pronouns -- and ``proper``.  The latter
532 signifies that this object's external name is a proper noun, and so
533 references to it should not be preceded by "a" or "the": you wouldn't want
534 to display "You can see a Helga here" or "I don't suppose the Helga would
535 care for that".  You may notice the library variable ``score`` being
536 incremented.  This variable holds the number of points that the player has
537 scored; when it changes like this, the interpreter tells the player that
538 "Your score has just gone up by one point".
539
540 There are also ``life`` and ``times_spoken_to`` properties (which we'll
541 talk about in :doc:`09`) and an ``initial`` property.
542
543 ``initial`` is used when the interpreter is describing a room and listing
544 the objects initial you can see there.  If we *didn't* define it, you'd get
545 this:
546
547 .. code-block:: transcript
548
549    Further along the street
550    People are still pushing and shoving their way from the southern gate towards
551    the town square, just a little further north.  You recognise the owner of a fruit
552    and vegetable stall.
553
554    You can see Helga here.
555
556    >
557
558 but we want to introduce Helga in a more interactive manner, and that's
559 what the ``initial`` property is for: it replaces the standard "You can see
560 *object* here" with a tailored message of your own design.  The value of an
561 ``initial`` property can be either a string which is to be displayed or, as
562 here, an embedded routine.  This one is pretty similar to the
563 ``description`` property that we defined for the street: something that's
564 *always* printed (Helga pauses...) and something that's printed only on the
565 first occasion ("Hello, Wilhelm, it's a fine day... "):
566
567 .. code-block:: transcript
568
569    Further along the street
570    People are still pushing and shoving their way from the southern gate towards
571    the town square, just a little further north. You recognise the owner of a fruit
572    and vegetable stall.
573
574    Helga pauses from sorting potatoes to give you a cheery wave.
575
576    "Hello, Wilhelm, it's a fine day for trade! Is this young Walter? My, how he's
577    grown. Here's an apple for him -- tell him to mind that scabby part, but the
578    rest's good enough. How's Frau Tell? Give her my best wishes."
579
580    >
581
582 But it's not quite the same as the street's description routine.  First, we
583 need a slightly different ``if`` test: ``self hasnt visited`` works fine
584 for a room object, but this routine is part of an object *in* a room;
585 instead we could use either ``below_square hasnt visited`` or (better)
586 ``location hasnt visited`` -- since ``location`` is the library variable
587 that refers to the room where the player currently is.  And second, some
588 curly braces ``{...}`` have appeared: why?
589
590 On Wilhelm's first visit to this room, we need to do two things:
591
592 * ensure that Wilhelm is in possession of an apple, because that's
593   mentioned when we...
594
595 * display Helga's cheery greeting.
596
597 The ``move`` statement does the first of those, and the ``print`` statement
598 does the second.  And both statements need to be controlled by the ``if``
599 statement.  So far, we've used an ``if`` statement twice, in both cases to
600 control a single following statement. ::
601
602   if (nest in branch) deadflag = 2;
603
604   if (self hasnt visited)
605       print "^~Stay close to me, son,~ you say,
606           ~or you'll get lost among all these people.~^";
607
608 That's what an ``if`` does -- it controls whether the following statement
609 is executed or not.  So how can we control two statements at once?  Well,
610 we *could* write two ``if`` statements::
611
612   if (location hasnt visited)
613       move apple to player;
614   if (location hasnt visited)
615       print "^~Hello, Wilhelm, it's a fine day for trade! Is this
616           young Walter? My, how he's grown. Here's an apple for him
617           -- tell him to mind that scabby part, but the rest's good
618           enough. How's Frau Tell? Give her my best wishes.~^";
619
620 but that's unbearably clumsy; instead, we use the braces to group the
621 ``move`` and ``print`` statement into a :term:`statement block` (sometimes
622 known as a code block) which counts as a single statement for the purposes
623 of control by the ``if`` statement. ::
624
625   if (location hasnt visited) {
626       move apple to player;
627       print "^~Hello, Wilhelm, it's a fine day for trade! Is this
628           young Walter? My, how he's grown. Here's an apple for him
629           -- tell him to mind that scabby part, but the rest's good
630           enough. How's Frau Tell? Give her my best wishes.~^";
631   }
632
633 A statement block can contain one, two, ten, a hundred statements; it
634 doesn't matter -- they're all treated as one unit by ``if`` (and by
635 ``objectloop``, which we meet later, and by ``do``, ``for`` and ``while``,
636 all of them loop statements that we don't encounter in this guide).
637
638 .. note::
639
640    The exact positioning of the braces is a matter of personal choice.  We
641    use this style::
642
643       if (condition) {
644           statement;
645           statement;
646           ...
647       }
648
649    but other designers have their own preferences, including::
650
651       if (condition) {
652           statement;
653           statement;
654           ...
655           }
656
657       if (condition)
658       {   statement;
659           statement;
660           ...
661       }
662
663       if (condition)
664           {
665           statement;
666           statement;
667           ...
668           }
669
670 Although we've not yet needed to use it, now would probably be a good time
671 to mention the ``else`` extension to the ``if`` statement.  Sometimes we
672 want to execute one statement block if a certain condition is true, and a
673 different statement block if it's not true.  Again, we *could* write two
674 ``if`` statements::
675
676    if (location has visited) {
677        statement;
678        statement;
679        ...
680    }
681    if (location hasnt visited) {
682        statement;
683        statement;
684        ...
685    };
686
687 but that's hardly an elegant approach; an ``else`` clause does the job more
688 neatly::
689
690    if (location has visited) {
691        statement;
692        statement;
693        ...
694    }
695    else {
696        statement;
697        statement;
698        ...
699    };
700
701 We've done a lot of scene-setting, but the real action is still to come.
702 Next, it's time to define the town square, and create a confrontation
703 between Wilhelm and the vogt's soldiers.  (But first, see again
704 :ref:`compile-as-you-go` if you're typing in the game as you read through
705 the guide.)