Fix remaining naked uses of \dropcap.
[ibg.git] / chapters / 16.rst
1 =====================
2  Debugging your game
3 =====================
4
5 .. only:: html
6
7   .. image:: /images/picN.png
8      :align: left
9
10 |N|\obody understands the phrase *errare humanum est* quite in the same
11 way as a programmer does. Computers are highly efficient machines 
12 capable of wondrous calculations, but they lack imagination and insist 
13 that every single item thrown at them must be presented according to 
14 certain rules previously defined. You can't negotiate with a computer; 
15 you either bow in submission or bite the dust.
16
17 Inform behaves no differently. If you make a typing or syntax mistake, 
18 the compiler will send you back to revise your work. "It was just a 
19 lousy comma!" you cry in disgust. The compiler remains silent. It has 
20 nothing to gain by argument, because it’s always right. So you go and 
21 change the lousy comma. No harm done except perhaps to your pride.
22
23 Errors that are found during compilation may be tedious to correct, but 
24 are usually easy to find; after all, the compiler tries politely to 
25 point out what and where the mistake was. Trouble begins after you've 
26 managed to satisfy all of the compiler's complaints. You are rewarded by 
27 a clean screen, devoid of a list of errors, and you are offered -- a 
28 gift!
29
30 A new file has appeared in your folder. A story file. Yes, *the* game. 
31 You quickly open your favourite interpreter and begin to play -- only to 
32 discover the dark side of errors, the bugs. Bugs come in all shapes, 
33 colours and sizes: big, small, stupid, absurd, minor, disturbing, 
34 nerve-wracking and catastrophic. They are often unpredictable: they 
35 regale our eyes with surprising, unexpected behaviour. They defy logic: 
36 I can TAKE the key, and the game even says "Taken", but the key remains 
37 in the same place and won't appear in my inventory. Or: opening the door 
38 while wearing the fur coat causes a programming error and a cryptic 
39 message "tried to find the attribute of nothing". And many, many others.
40
41 When designing a game you try to take into consideration the states that 
42 your objects will find themselves in, but any medium-sized game has such 
43 a number of objects and actions that it's almost impossible to think of 
44 all the possible variations, permutations and possibilities.
45
46 Debugging consists in finding run-time errors, and then correcting them. 
47 Pretty easy, you might think, but no. Detection of such errors is not 
48 straightforward, since they tend to manifest themselves only under 
49 precise circumstances. Then you have to investigate your code to find 
50 out what is causing them. And then, if you discover the offending lines, 
51 you must make the appropriate changes. (There is also the case when you 
52 can't find the mistake. Don't worry, it's there somewhere. Persistence 
53 always pays off in the end.)
54
55 To help you out in this daunting task, Inform has a stock of special
56 actions: the debugging verbs. They become available at run-time when the
57 source file is compiled in :term:`Debug mode` (:option:`-D` switch). When
58 you are ready to release your game, you’ll have to recompile, switching off
59 Debug to avoid allowing the players to benefit from the debugging verbs.
60 We'll cover briefly a few of these actions, and tell you what they do.
61
62 Command lists
63 =============
64
65 The only way to test a game is to play it. As you make progress writing 
66 code, the game grows complicated, and it becomes really tiresome to 
67 repeat all the commands every time you play. Not unusually, when you fix 
68 the behaviour of some object, you are also affecting the behaviour of 
69 other objects or actions, so it's a good idea to test everything now and 
70 then; you have to make sure that your recent changes and fixes didn't 
71 spoil something that previously worked fine.
72
73 The RECORDING command (RECORDING ON and RECORDING OFF) saves the 
74 commands that you type as you play into a text file (you'll probably be 
75 prompted for a file name). When you add a new section to the game, you 
76 can play to that point, type RECORDING ON to capture (in another file) 
77 the commands which exercise that section, and then later use your editor 
78 to append those new commands to the existing list.
79
80 The REPLAY command runs the text file created by RECORDING, playing all 
81 the stored commands in one go. This way you can very quickly check 
82 whether everything is working as it should.
83
84 You can open the file of commands with any text editor program and 
85 modify the contents as need arises: for instance, if you want to delete 
86 some commands no longer necessary because of a change to the game, or if 
87 you forgot to test some particular object and you need to add new 
88 commands.
89
90 This technique (the use of recorded lists of commands) is, and we can't 
91 emphasise it too strongly, one of the most useful testing features for a 
92 game designer.
93
94
95 Spill them guts
96 ===============
97
98 Some debugging verbs offer information about the current state of things.
99
100 TREE
101
102   This action lists all the objects in the game and how they contain 
103   each other. You can discover the possessions of just one object by 
104   typing TREE *object*. All the objects that you have defined in the 
105   source file are turned into numbers by Inform when it compiles the 
106   story file; this command also lists those internal
107   :samp:`{obj_id}` numbers.
108
109 SHOWOBJ *object*
110
111   Displays information about the *object*, the attributes it currently 
112   has and the value of its properties. The *object* can be anywhere, 
113   not necessarily in scope. For instance, in "Heidi":
114
115   .. code-block:: transcript
116
117     >SHOWOBJ NEST
118     Object "bird's nest" (29) in "yourself"
119       has container moved open workflag
120       with name 'bird's' 'nest' 'twigs' 'moss',
121            description "The nest is carefully woven of twigs and moss." (19230),
122
123 SHOWVERB *verb*
124
125   Displays the grammar of the *verb*, just like a standard ``Verb``
126   definition. This comes in handy when you have tampered with ``Extend`` 
127   and are not sure about the final results of your machinations. An 
128   example from "William Tell":
129
130   .. code-block:: transcript
131
132     >SHOWVERB GIVE
133     Verb 'feed' 'give' 'offer' 'pay'
134         * held 'to' creature -> Give
135         * creature held -> Give reverse
136         * 'over' held 'to' creature -> Give
137         * 'homage' 'to' noun -> Salute
138
139   The first lines reproduce the verb definition as it's written in the 
140   library. The last line, however, is the direct consequence of our 
141   tailored ``Extend``:
142
143   .. code-block:: inform
144
145     Extend 'give'
146         * 'homage' 'to' noun        -> Salute;
147
148 SCOPE
149
150   Lists all of the objects currently in scope (in general terms, visible 
151   to the player character). More powerfully, you can type SCOPE *object* 
152   to discover which objects are in scope for the named *object*. This 
153   feature becomes useful when you have NPCs capable of tampering with 
154   their surroundings.
155
156
157 What on earth is going on?
158 ==========================
159
160 There comes the time when some actions don't produce the desired effects 
161 and you don't know why. The following debugging verbs offer information 
162 about what the interpreter is up to, which might enable you to identify 
163 the moment when things started to go awry.
164
165 ACTIONS (or ACTIONS ON ) and ACTIONS OFF
166
167   Gives information about all the actions going on. Some actions get 
168   redirected to others, and this becomes at times a source of mischief 
169   and mystery; here you get a clue what's happening. For example, take 
170   this transcript from "William Tell":
171
172   .. code-block:: transcript
173
174     Further along the street
175     People are still pushing and shoving their way from the southern gate towards
176     the town square, just a little further north. You recognise the owner of a fruit
177     and vegetable stall.
178
179     Helga pauses from sorting potatoes to give you a cheery wave.
180
181     >SEARCH STALL
182     [ Action Search with noun 35 (fruit and vegetable stall) ]
183     [ Action Examine with noun 35 (fruit and vegetable stall) (from < > statement) ]
184     It's really only a small table, with a big heap of potatoes, some carrots and
185     turnips, and a few apples.
186     ...
187
188 CHANGES (or CHANGES ON ) and CHANGES OFF
189
190   Tracks object movements, and changes to properties and attributes:
191
192   .. code-block:: transcript
193
194     Middle of the square
195     There is less of a crush in the middle of the square; most people prefer to
196     keep as far away as possible from the pole which towers here, topped with that
197     absurd ceremonial hat. A group of soldiers stands nearby, watching everyone who
198     passes.
199
200     >GO NORTH
201     [Setting Middle of the square.warnings_count to 1]
202     A soldier bars your way.
203
204     "Oi, you, lofty; forgot yer manners, didn't you? How's about a nice salute for
205     the vogt's hat?"
206
207     >AGAIN
208     [Setting Middle of the square.warnings_count to 2]
209
210     "I know you, Tell, yer a troublemaker, ain't you? Well, we don't want no bovver
211     here, so just be a good boy and salute the friggin' hat. Do it now: I ain't
212     gonna ask you again..."
213
214     >SALUTE HAT
215     [Setting hat on a pole.has_been_saluted to 1]
216     You salute the hat on the pole.
217
218     "Why, thank you, sir," sneers the soldier.
219
220     >GO SOUTH
221     [Setting Middle of the square.warnings_count to 0]
222     [Setting hat on a pole.has_been_saluted to 0]
223     [Moving yourself to South side of the square]
224     ...
225
226 TIMERS (or TIMERS ON ) and TIMERS OFF
227
228   This verb shows you the state of all active timers and daemons at the 
229   end of each turn. We haven't mentioned timers -- similar to daemons -- 
230   in this guide; you might perhaps use one to explode a bomb ten turns 
231   after lighting its fuse.
232
233 TRACE (or TRACE ON ), TRACE *number* and TRACE OFF
234
235   If you turn on this powerful verb, you'll be able to follow the activity
236   of the :term:`parser` -- that part of the library which tries to make
237   sense of what the player types -- and this will indeed be a wonderful
238   moment of gratitude that someone else took the trouble of writing
239   it. Since the parser does so many things, you can decide the level of
240   detail about the displayed information with the *number* parameter, which
241   can go from 1 (minimum info) to 5 (maximum info). By default, TRACE ON
242   and TRACE with no number sets level 1. Trace level 1 shows the grammar
243   line that the parser is thinking about, while level 2 shows each
244   individual token on each grammar line that it tries. The information
245   displayed with higher levels may become quite hacky, and you are advised
246   to use this feature only if nothing else helps.
247
248 Super-powers
249 ============
250
251 GONEAR *object*
252
253   This action lets you teleport to the room where the *object* is. This 
254   is useful when, for example, certain parts of the map are closed 
255   until the player character solves some puzzle, or if the game map is 
256   divided in different areas. If the room you want to visit has no 
257   objects, you can use...
258
259 GOTO *number*
260
261   Teleports you to the room with that internal *number*. Since rooms 
262   usually have no name, you'll have to discover the internal number of 
263   the room object (with the command TREE, for instance).
264
265 PURLOIN *object*
266
267   .. Generated by autoindex
268   .. index::
269      pair: scenery; library attribute
270      pair: static; library attribute
271
272   PURLOIN works exactly as TAKE , with the nice addition that it doesn't 
273   matter where the object is: in another room, inside a locked 
274   container, in the claws of the bloodthirsty dragon. More dangerously, 
275   it doesn't matter if the object is takeable, so you may purloin 
276   :attr:`static` or :attr:`scenery` objects. PURLOIN is useful in a variety of 
277   situations, basically when you want to test a particular feature of 
278   the game that requires the player character to have some objects 
279   handy. Instead of tediously collecting them, you may simply PURLOIN 
280   them. Be careful: it's unwise to PURLOIN objects not meant to be 
281   taken, as the game's behaviour may become unpredictable.
282
283 ABSTRACT *object* TO *object*
284
285   .. Generated by autoindex
286   .. index::
287      pair: animate; library attribute
288      pair: container; library attribute
289      pair: supporter; library attribute
290
291   This verb enables you to move the first *object* to the second 
292   *object*. As with PURLOIN , both objects can be anywhere in the game. 
293   Bear in mind that the second object should logically be a 
294   :attr:`container`, a :attr:`supporter` , or something :attr:`animate`.
295
296
297 Infix: the harlot's prerogative
298 ===============================
299
300 The basic debugging verbs are fairly versatile, easy to use, and don't
301 consume a lot of memory. Occasionally though, you'll meet a bug which you
302 simply can't catch using regular techniques, and that’s when you might want
303 to investigate the Infix debugger. You'll need to compile using the
304 :option:`-X` switch, and you'll then be able to monitor and modify almost
305 all of your game’s data and objects. For instance, you can use ";" to
306 inspect -- and change -- a variable:
307
308 .. code-block:: transcript
309
310   Inside Benny's cafe
311   Benny's offers the FINEST selection of pastries and sandwiches. Customers clog
312   the counter, where Benny himself manages to serve, cook and charge without
313   missing a step. At the north side of the cafe you can see a red door connecting
314   with the toilet.
315
316   >; deadflag
317   ; == 0
318
319   >; deadflag = 4
320   ; == 4
321
322       *** You have been SHAMEFULLY defeated ***
323
324   In that game you scored 0 out of a possible 2, in 2 turns.
325
326 It's often quite maddening to realise that some variable is still 
327 :const:`false` because the Chalk puzzle didn't work properly, and that you 
328 can't test the Cheese puzzle until the variable becomes :const:`true`. Rather 
329 than quit, fix the Chalk, recompile, play back to the current position 
330 and only *then* tackle the Cheese, how much easier to just change the 
331 variable in mid-stream, and carry right on.
332
333 You can use ``;WATCH`` to monitor an object; you'll see it receive 
334 messages and you'll be told when its property and attribute values 
335 change:
336
337 .. code-block:: transcript
338
339   >;WATCH MID_SQUARE
340   ; Watching object "Middle of the square" (43).
341
342   >NORTH
343   [Moving yourself to Middle of the square]
344   [Moving local people to Middle of the square]
345   [Moving Gessler's soldiers to Middle of the square]
346   [Moving your son to Middle of the square]
347
348   Middle of the square
349   There is less of a crush in the middle of the square; most people prefer to
350   keep as far away as possible from the pole which towers here, topped with that
351   absurd ceremonial hat. A group of soldiers stands nearby, watching everyone who
352   passes.
353   [Giving Middle of the square visited]
354
355   >NORTH
356   [ "Middle of the square".before() ]
357   [ mid_square.before() ]
358   [Setting Middle of the square.warnings_count to 1]
359   A soldier bars your way.
360
361   "Oi, you, lofty; forgot yer manners, didn't you? How's about a nice salute for
362   the vogt's hat?"
363
364   >NORTH
365   [ "Middle of the square".before() ]
366   [ mid_square.before() ]
367   [Setting Middle of the square.warnings_count to 2]
368
369   "I know you, Tell, yer a troublemaker, ain't you? Well, we don't want no bovver
370   here, so just be a good boy and salute the friggin' hat. Do it now: I ain't
371   gonna ask you again..."
372
373   >NORTH
374   [ "Middle of the square".before() ]
375   [ mid_square.before() ]
376   [Setting Middle of the square.warnings_count to 3]
377
378   "OK, Herr Tell, now you're in real trouble.
379   ...
380
381 .. todo::
382
383    "Herr" above is italicized.  Was that a mistake in the original text?
384
385    Update: I don't think so.  In 08.rst, lines 465 and 516, "Herr" is
386    explicitly underlined (which probably appears italicized on output).
387
388 Infix is quite complex -- there are more commands than those we have 
389 shown you -- so while it's good to have available, it's not really a 
390 tool for novices. If you do use it, be careful: you get a lot of runtime 
391 power, and may easily screw up the state of the game. Remember, however, 
392 that the changes affect only the current story file while it’s running; 
393 to make permanent amendments, you still need to edit the source file.
394
395 You won't need it often, but Infix can sometimes provide quick answers 
396 to tricky problems.
397
398 No matter what
399 ==============
400
401 Your game will still have some undetected bugs despite all your efforts 
402 to clean it up. This is normal, even for experienced designers; don't 
403 feel discouraged or demoralised. You might find it reassuring to know 
404 that our own example games in this guide -- which certainly don't 
405 qualify as "complex programming" -- were far from perfect at the First 
406 Edition. We blush at the following report from an extremely diligent 
407 play-tester:
408
409   I found these things when playing “Captain Fate”:
410
411   * player is able to wear clothes over the costume,
412
413   * player can change into costume in the dark unlocked bathroom without
414     being interrupted,
415
416   * player can drop clothes in the dark unlocked bathroom. Try REMOVE
417     CLOTHES. X SELF. REMOVE COSTUME. INV -- X SELF says that you
418     are wearing the costume, but the inventory does not reflect this.
419
420 The Second Edition fixed those problems, and quite a few more besides. 
421 "That's it;" we thought, "after all this time, our example games are 
422 sure to be squeaky clean." In our dreams... Another diligent play-tester 
423 then wrote:
424
425   While reading I took notes of some mistakes and inconsistencies:
426
427   * BENNY, GIVE KEY TO CUSTOMERS and BENNY, GIVE KEY will
428     make Benny give the key to the player. The same goes for coffee.
429
430   * Benny will force the player back into the cafe even when the key is
431     dropped in the café, or put on the counter (in Benny's plain sight!).
432
433 Of course, the code we've offered you in *this* edition takes care of 
434 those embarrassing issues, but it might very well happen that a few more 
435 undetected absurdities pop up from now on.
436
437 .. Generated by autoindex
438 .. index::
439    single: RAIF
440
441 The final stage of debugging must happen elsewhere, at the hands of some 
442 wilful, headstrong and determined beta-testers; these are the people 
443 who, if you’re lucky, will methodically tear your game to shreds and 
444 make extensive reports of things that don't work reliably, things that 
445 don't work as smoothly as they might, things that ought to work but 
446 don't, things that never even crossed your mind (like, uh, dropping the 
447 costume in the dark). Once you think your game is finished -- in that it 
448 does all that you think it should, and you've run out of ideas on how 
449 else to test it -- look for a few beta-testers; three or four is good. 
450 The IF community offers some beta-testing resources, or you can always 
451 ask in RAIF for kind souls willing to have a go at your game. Remember 
452 the golden rules:
453
454 * **Expect no mercy**. Although it hurts, a merciless approach is what you
455   need at this time; much better to discover your errors and oversights
456   now, before you release the game more widely. And don't forget to
457   acknowledge your testers' assistance somewhere within the game.
458
459 * **Never say never**.  If your testers suggest that the game should
460   respond better to an attempted action, don't automatically respond with
461   "No one's going to try that!" They already have, and will again -- be
462   grateful for your testers' devious minds and twisted psyches.  Although a
463   normal player won't try *all* of those oddball things, every player is
464   bound to try at least *one*, and their enjoyment will be greater, the
465   reality enhanced, if the game "understands".
466
467 * **Ask for more**. Don't treat your testers simply as validators of your
468   programming skills, but rather as reviewers of your storytelling
469   abilities. Encourage them to comment on how well the pieces fit together,
470   and to make suggestions -- small or radical -- for improvement; don't
471   necessarily reject good ideas just because implementing them "will take
472   too long". For example: "the scene in the Tower of London doesn't somehow
473   seem to belong in an Arabian Nights game", or "having to solve three
474   puzzles in a row just to discover the plate of sheep's eyes is a little
475   over the top", or "this five-room trek across the desert really is a bit
476   dull; perhaps you could add a quicksand or something to liven it up?", or
477   "the character of the eunuch in the harem seems to be lacking in
478   something". That is, view the testers collectively not as simple
479   spell-checkers, but rather as collaborative editors on your latest novel.