Use blockdiag to create the Heidi box diagrams.
[ibg.git] / chapters / 04.rst
1 ======================
2  Reviewing the basics
3 ======================
4
5 .. epigraph::
6
7    | *G was a gamester, who had but ill-luck;*
8    | *H was a hunter, and hunted a buck.*
9
10 .. only:: html
11
12   .. image:: /images/picG.png
13      :align: left
14
15 .. raw:: latex
16
17     \dropcap{g}
18
19 oing through the design of our first game in the previous chapter has
20 introduced all sorts of Inform concepts, often without giving you much
21 detail about what's been happening.  So let's review some of what we've
22 learnt so far, in a slightly more organised fashion.  We'll talk about
23 "Constants and variables" on page 49, "Object definitions" on page 50,
24 "Object relationships -- the object tree" on page 52, "Things in quotes" on
25 page 55, and "Routines and statements" on page 56.
26
27 Constants and variables
28 =======================
29
30 Superficially similar, constants and variables are actually very different
31 beasts.
32
33 .. rubric:: Constants
34
35 A **constant** is a name to which a value is given once and once only; you
36 can't later use that name to stand for a different value.  Think of it as a
37 stone tablet on which you carve a number: a carving can't be undone, so
38 that you see the same number every time you look at the stone.
39
40 So far, we've seen a ``Constant`` being set up with its value as a string
41 of characters::
42
43     Constant Story "Heidi";
44
45 and as a number::
46
47     Constant MAX_CARRIED 1;
48
49 Those two examples represent the most common ways in which constants are
50 used in Inform.
51
52 .. rubric:: Variables
53
54 A **variable** is a name to which a value is given, but that value can be
55 changed to a different one at any time.  Think of it as a blackboard on
56 which you mark a number in chalk: whenever you need to, just wipe the board
57 and write up a new number.
58
59 We haven't set up any variables of our own yet, though we've used a couple
60 which the library created like this::
61
62     Global location;
63     Global deadflag;
64
65 The value of a **global variable** created in this way is initially 0, but
66 you can change it at any time.  For example, we used the statement::
67
68      location = before_cottage;
69
70 to reset the value of the ``location`` variable to the 
71 ``before_cottage`` object, and we wrote::
72
73      if (nest in branch) deadflag = 2;
74
75 to reset the value of the ``deadflag`` variable to 2.
76
77 Later, we'll talk about the **local variable** (see "Routines" on page 179)
78 and about using object properties as variables (see "Objects" on page 177).
79
80 Object definitions
81 ==================
82
83 The most important information you should have gleaned from the previous
84 chapter is that your entire game is defined as a series of objects.  Each
85 room is an object, each item that the player sees and touches is an object;
86 indeed the player herself is also an object (one that's automatically
87 defined by the library).
88
89 The general model of an **object** definition looks like this::
90
91         Object      obj_id   "external_name"   parent_obj_id
92            with     property    value ,
93                     property    value ,
94                     ...
95                     property    value ,
96            has      attribute    attribute   ... attribute
97            ;
98
99 The definition starts with the word ``Object`` and ends with a semicolon;
100 in between are three major blocks of information:
101
102 * immediately after the word ``Object`` is the header information;
103 * the word ``with`` introduces the object's **properties**;
104 * the word ``has`` introduces the object's **attributes**.
105
106 .. rubric:: Object headers
107
108 An object header comprises up to three items, all optional:
109
110 * An internal ``obj_id`` by which other objects refer to this object.  It's
111   a single word (though it can contain digits and underscores) of up to
112   thirty-two characters, and it must be unique within the game.  You can
113   omit the ``obj_id`` if this object isn't referred to by any other
114   objects.
115
116   For example: ``bird``, ``tree``, ``top_of_tree``.
117
118 * An ``external_name``, in double quotes, which is what the interpreter
119   uses when referring to the object.  It can be one or more words, and need
120   not be unique (for instance, you might have several ``"Somewhere in the
121   desert"`` rooms).  Although not mandatory, it's best to give *every*
122   object an ``external_name``.  For example: ``"baby bird"``, ``"tall
123   sycamore tree"``, ``"At the top of the tree"``.
124
125 * The internal ``obj_id`` of another object which is the initial location
126   of this object (its "parent" -- see the next section) at the start of the
127   game.  This is omitted from objects which have no initial parent; it's
128   *always* omitted from a room.
129
130   For example: the definition of the ``bird`` starts like this, specifying
131   that at the start of the game, it can be found in the ``forest`` room
132   (though later the player character will pick it up and move it around)::
133
134       Object   bird "baby bird" forest
135       ...
136
137   The ``tree`` starts like this; the only real difference is that, because
138   the player character can't move a ``scenery`` object, it's always going
139   to be in the ``clearing``::
140
141       Object   tree "tall sycamore tree" clearing
142       ...
143
144   .. note::
145
146      There's an alternative method for defining an object's initial
147      location, using "arrows" rather than the parent's internal ``obj_id``.
148      For example, the definition of the bird could have started like this::
149
150          Object   -> bird "baby bird"
151          ...
152
153      We don't use the arrows method in this guide, though we do describe
154      how it works in "Setting up the object tree" on page 185.
155
156 .. rubric:: Object properties
157
158 An object's property definitions are introduced by the ``with`` keyword.
159 An object can have any number of properties, and they can be defined in any
160 order.  Each definition has two parts: a name, and a value; there's a space
161 between the two parts, and a comma at the end.
162
163 Think of each property as a variable which is specifically associated with
164 that object.  The variable's initial setting is the supplied value; if
165 necessary, it can be reset to other values during play (though in fact most
166 property values don't change in this way).
167
168 Here are examples of the properties that we've come across so far::
169
170     description "The nest is carefully woven of twigs and moss.",
171     e_to forest,
172     name 'baby' 'bird' 'nestling',
173     each_turn [; if (nest in branch) deadflag = 2; ],
174
175 By happy coincidence, those examples also demonstrate most of the different
176 types of value which can be assigned to a property.  The value associated
177 with the ``description`` property in this particular example is a string of
178 characters in double quotes; the value associated with this ``e_to``
179 property is the internal identity of an object; the ``name`` property is a
180 bit unusual -- its value is a list of dictionary words, each in single
181 quotes; the ``each_turn`` property has a value which is an **embedded
182 routine** (see "Embedded routines" on page 58).  The only other type of
183 value which is commonly found is a simple number; for example::
184
185      capacity 10,
186
187 In all, the library defines around forty-eight standard properties -- like
188 ``name`` and ``each_turn`` -- which you can associate with your objects;
189 there's a complete list in "Object properties" on page 266.  And in
190 "William Tell: in his prime" on page 91 we show you how to invent your own
191 property variables.
192
193 .. rubric:: Object attributes
194
195 An object's attribute list is introduced by the ``has`` keyword.  An object
196 can have any number of attributes, and they can be listed in any order,
197 with a space between each.
198
199 As with properties, you can think of each attribute as a variable which is
200 specifically associated with that object.  However, an attribute is a much
201 more limited form of variable, since it can have only two possible states:
202 present, and absent (also known as set/clear, on/off, or true/false;
203 incidentally, a two-state variable like this is often called a **flag**).
204 Initially, an attribute is either present (if you mention its name in the
205 list) or absent (otherwise); if necessary, its state can change during play
206 (and this is relatively common).  We often say that a certain object
207 currently *has* a certain attribute, or that conversely it *hasn't* got it.
208
209 The attributes that we've come across so far are::
210
211      container light open scenery static supporter
212
213 Each of those answers a question: Is this object a container?  Does it
214 provide light?  and so on.  If the attribute is present then the answer is
215 Yes; if the attribute isn't present, the answer is No.
216
217 The library defines around thirty standard attributes, listed in "Object
218 attributes" on page 269.  Although you *can* devise additional attributes
219 -- see "Common properties and attributes" on page 185 -- in practice you
220 seldom need to.
221
222 Object relationships -- the object tree
223 =======================================
224
225 Not only is your game composed entirely of objects, but also Inform takes
226 great care to keep track of the relationships between those objects.  By
227 "relationship" we don't mean that Walter is Wilhelm's son, while Helga and
228 Wilhelm are just good friends; it's a much more comprehensive exercise in
229 recording exactly where each object is located, relative to the other
230 objects in the game.
231
232 Despite what we just said, Inform relationships *are* managed in terms of
233 **parent** and **child** objects, though in a much broader sense than
234 Wilhelm and Walter.  When the player character is in a particular room --
235 for example the forest -- we can say that:
236
237 * the forest object is *the* parent of the player object, or alternatively
238 * the player object is *a* child of the forest object.
239
240 Also, if the player is carrying an object -- for example the nest -- we say
241 that:
242
243 * the player object is *the* parent of the nest object, or that
244 * the nest object is *a* child of the player object.
245
246 Note the emphasis there: an object has exactly *one* parent (or no parent
247 at all), but can have *any number* of child objects (including none).
248
249 For an example of an object having more than one child, think about the way
250 we defined the nest and tree objects::
251
252     Object   nest "bird's nest" clearing
253     ...
254
255     Object   tree "tall sycamore tree" clearing
256     ...
257
258 We used the third of the header items to say that the clearing was the
259 parent of the nest, and also that the clearing was the parent of the tree;
260 that is, both nest and tree are child objects of the clearing.
261
262 .. note::
263
264    A "room" isn't anything magical; it's just an object which *never* has a
265    parent, and which *may* from time to time have the player object as a
266    child.
267
268 When we defined the bird, we placed it in the forest, like so::
269
270     Object   bird "baby bird" forest
271     ...
272
273 We didn't place any other objects in that room, so at the start of the game
274 the forest was the parent of the bird (and the bird was the only child of
275 the forest).  But what happens when the player character, initially in the
276 ``before_cottage`` room, goes EAST to the forest?  Answer: the player's
277 parent is now the forest, and the forest has two children -- the bird *and*
278 the player.  This is a key principle of the way Inform manages its objects:
279 the parent--child relationships between objects change continuously, often
280 dramatically, as the game progresses.
281
282 Another example of this: suppose the player character picks up the bird.
283 This causes another change in the relationships.  The bird is now a child
284 of the player (and *not* of the forest), and the player is both a parent
285 (of the bird) and a child (of the forest).
286
287 Here we show how the object relationships change during the course of the
288 game.  The straight lines represent parent--child relationships, with the
289 parent object at the top of the line, and the child object at the bottom.
290
291 1. At the start of the game:
292
293    .. blockdiag:: /figures/heidiobj1.diag
294       :align: center
295       :scale: 80%
296
297 2. The player types: ``GO EAST``
298
299    .. blockdiag:: /figures/heidiobj2.diag
300       :align: center
301       :scale: 80%
302
303 3. The player types: ``TAKE THE BIRD``
304
305    .. blockdiag:: /figures/heidiobj3.diag
306       :align: center
307       :scale: 80%
308
309 4. The player types: ``GO NORTHEAST``
310
311    .. blockdiag:: /figures/heidiobj4.diag
312       :align: center
313       :scale: 80%
314
315 5. The player types: ``PUT BIRD IN NEST``
316
317    .. blockdiag:: /figures/heidiobj5.diag
318       :align: center
319       :scale: 80%
320
321 6. The player types: ``TAKE NEST``
322
323    .. blockdiag:: /figures/heidiobj6.diag
324       :align: center
325       :scale: 80%
326
327 7. The player types: ``UP``
328
329    .. blockdiag:: /figures/heidiobj7.diag
330       :align: center
331       :scale: 80%
332
333 8. The player types: ``PUT NEST ON BRANCH``
334
335    .. blockdiag:: /figures/heidiobj8.diag
336       :align: center
337       :scale: 80%
338
339 In this short example, we've taken a lot of time and space to spell out
340 exactly how the objects relationship patterns -- generally known as the
341 **object tree** -- appear at each stage.  Normally you wouldn't bother with
342 this much detail (a) because the interpreter does most of the work for you,
343 and (b) because in a real game there are usually too many objects for you
344 to keep track of.  What's important is that you understand the basic
345 principles: at any moment in time an object either has no parent (which
346 probably means either that it's a room, or that it's floating in hyperspace
347 and not currently part of the game) or exactly one parent -- the object
348 that it's "in" or "on" or "a part of".  However, there's no restriction on
349 the number of children that an object can have.
350
351 There's a practical use for these relationships, covered in detail further
352 on.  As a designer, you can refer to the current parent or children of any
353 given object with the ``parent``, ``child`` and ``children`` routines, and
354 this is one feature that you will be using frequently.  There are also
355 other routines associated with the object tree, to help you keep track of
356 the objects or move them around.  We'll see them one by one in the next
357 chapters.  For a quick summary, see "Objects" on page 177.
358
359 Things in quotes
360 ================
361
362 Inform makes careful distinction between double and single quotes.
363
364 .. rubric:: Double quotes
365
366 Double quotes ``"..."`` surround a **string** -- a letter, a word, a 
367 paragraph, or almost any number of characters -- which you want the 
368 interpreter to display while the game is being played.  You can use the 
369 tilde ``~`` to represent a double quote inside the string, and the 
370 circumflex ``^`` to represent a newline (line break) character.  
371 Upper-case and lower-case letters are treated as different.
372
373 A long string can be split over several lines; Inform transforms each 
374 line break (and any spaces around it) into a single space (extra spaces 
375 *not* at a line break are preserved, though).  These two strings are 
376 equivalent::
377
378     "This is a      string of characters."
379
380     "This
381       is
382             a    string
383                        of characters."
384
385 When the interpreter displays a long character string -- for example, while
386 describing a feature-packed room -- it employs automatic word-wrapping to
387 fit the text to the player's screen.  This is where you might insert ``^``
388 characters to force line breaks to appear, thus presenting the text as a
389 series of paragraphs.  So far, we've seen strings used as the value of a
390 ``Constant``::
391
392     Constant Headline
393           "^A simple Inform example
394            ^by Roger Firth and Sonja Kesserich.^";
395
396 which could equally have been defined thus::
397
398     Constant Headline
399           "^A simple Inform example^by Roger Firth and Sonja Kesserich.^";
400
401 and as the value of an object ``description`` property::
402
403     description "Too young to fly, the nestling tweets helplessly.",
404
405 Later, you'll find that they're also very common in ``print`` statements.
406
407 .. rubric:: Single quotes
408
409 Single quotes ``'...'`` surround a **dictionary word**.  This has to be 
410 a single word -- no spaces -- and generally contains only letters (and 
411 occasionally numbers and hyphens), though you can use ``^`` to represent 
412 an apostrophe inside the word.  Upper-case and lower-case letters are 
413 treated as identical; also, the interpreter normally looks only at the 
414 first nine characters of each word that the player types.
415
416 When the player types a command, the interpreter divides what was typed
417 into individual words, which it then looks up in the dictionary.  If it
418 finds all the words, and they seem to represent a sensible course of
419 action, that's what happens next.
420
421 So far, we've seen dictionary words used as the values of an object
422 ``name`` property::
423
424      name 'bird^s' 'nest' 'twigs' 'moss',
425
426 and indeed that's just about the only place where they commonly occur.
427 You'll save yourself a lot of confusion by remembering the distinction:
428 Double quotes for Output, Single quotes for Input (DOSI).
429
430 Routines and statements
431 =======================
432
433 A routine is a collection of statements, which are performed (or we often
434 say "are executed") at run-time by the interpreter.  There are two types of
435 routine, and about two dozen types of statement (there's a complete list in
436 "Statements" on page 174; see also "Inform language" on page 257).
437
438 .. rubric:: Statements
439
440 A **statement** is an instruction telling the interpreter to perform a
441 particular task -- to "do something" -- while the game is being played.  A
442 real game usually has lots and lots of statements, but so far we've
443 encountered only a few.  We saw::
444
445      location = before_cottage;
446
447 which is an example of an **assignment** statement, so-called because the
448 equals sign ``=`` assigns a new value (the internal ID of our
449 ``before_cottage`` room) to a variable (the global variable ``location``
450 which is part of the library).  Later we saw::
451
452      if (nest in branch) deadflag = 2;
453
454 which is actually *two* statements: an assignment, preceded by an ``if``
455 statement::
456
457      if (nest in branch) ...
458
459 The ``if`` statement tests a particular condition; if the condition is
460 true, the interpreter executes whatever statement comes next; if it isn't
461 true, the interpreter ignores the next statement.  In this example, the
462 interpreter is testing whether the ``nest`` object is "in" or "on" (which
463 we now know means "is a child of") the ``branch`` object.  For most of the
464 game, that condition is not true, and so the interpreter ignores the
465 following statement.  Eventually, when the condition becomes true, the
466 interpreter executes that statement: it performs an assignment::
467
468     deadflag = 2;
469
470 which changes the value of the library variable ``deadflag`` from its 
471 current value to 2.  Incidentally, ``if`` statements are often written 
472 on two lines, with the "controlled" statement indented.  This makes it 
473 easier to read, but doesn't change the way that it works::
474
475     if (nest in branch)
476         deadflag = 2;
477
478 The thing that's being controlled by the ``if`` statement doesn't have to
479 be an assignment; it can be any kind of statement.  In fact, you can have
480 lots of statements, not just one, controlled by an ``if`` statement.  We'll
481 talk about these other possibilities later.  For now, just remember that
482 the only place where you'll find statements are within standalone routines
483 and embedded routines.
484
485 .. rubric:: Standalone routines
486
487 A **standalone routine** is a series of statements, collected together and
488 given a name.  When the routine is "called" -- by its given name -- those
489 statements are executed.  Here's the one that we've defined::
490
491     [ Initialise; location = before_cottage; ];
492
493 Because it's such a tiny routine, we placed it all on a single line.  Let's
494 rewrite it to use several lines (as with the ``if`` statement, this improves
495 the readability, but doesn't affect how it works)::
496
497     [ Initialise;
498         location = before_cottage;
499     ];
500
501 The ``[ Initialise;`` is the start of the routine, and defines the name by
502 which it can be "called".  The ``];`` is the end of the routine.  In
503 between are the statements -- sometimes known as the body of the routine --
504 which are executed when the routine is called.  And how is that done?  By a
505 statement like this::
506
507     Initialise();
508
509 That single statement, the routine's name followed by opening and closing
510 parentheses, is all that it takes to call a routine.  When it comes across
511 a line like this, the interpreter executes the statements -- in this
512 example there's only one, but there may be ten, twenty, even a hundred of
513 them -- in the body of the routine.  Having done that, the interpreter
514 resumes what it was doing, on the line following the ``Initialise();``
515 call.
516
517 .. note::
518
519    You may have noticed that, although we've defined a routine named
520    ``Initialise``, we've never actually called it.  Don't worry -- the
521    routine *is* called, by the Inform library, right at the start of a 
522    game.
523
524 .. rubric:: Embedded routines
525
526 An **embedded routine** is much like a standalone routine, though it
527 doesn't have a name and doesn't end in a semicolon.  This is the one that
528 we defined::
529
530      [; if (nest in branch) deadflag = 2; ]
531
532 except that we didn't write it in isolation like that: instead, we defined
533 it to be the value of an object property::
534
535      each_turn [; if (nest in branch) deadflag = 2; ],
536
537 which would have worked just the same if we'd written it like this::
538
539      each_turn [;
540          if (nest in branch)
541              deadflag = 2;
542      ],
543
544 All embedded routines are defined in this manner: as the value of an object
545 property.  That's where they're embedded -- inside an object.  The
546 introductory characters ``[;`` maybe look a little odd, but it's really
547 only the same syntax as for a standalone routine, only without a name
548 between the ``[`` and ``;``.
549
550 For calling an embedded routine, thus causing the statements it contains to
551 be executed, the method that we described for a standalone routine won't
552 work.  An embedded routine has no name, and needs none; it's
553 *automatically* called by the library at appropriate moments, which are
554 determined by the role of the property for which it is the value.  In our
555 example, that's at the end of every turn in which the player character is
556 in the same room as the branch.  Later, we'll see other examples of
557 embedded routines, each designed to perform a task which is appropriate for
558 the property whose value it is; we'll also see that it is possible to call
559 an embedded routine yourself, using an ``obj_id.property()`` syntax -- in
560 this example, we could call the routine by writing ``branch.each_turn()``.
561 There's more about these topics in "Routines and arguments" on page 67, "A
562 diversion: working with routines" on page 104 and in "Routines" on
563 page 179.
564
565 That ends our review of the ground covered in our first game.  We'll have
566 more to say about most of this later, but we're trying not to overload you
567 with facts at this early stage.  What we'd like you to do is to look back
568 at the source of the game, and ensure that you can recognise all the
569 elements which this chapter has described.  Then, we'll move on to fix a
570 few of the game's more important defects.