-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtpataic06.html
557 lines (551 loc) · 18.5 KB
/
htpataic06.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<!DOCTYPE html>
<html>
<head>
<title>6. Passages</title>
<link rel="stylesheet" href="htpataic.css" type="text/css" />
</head>
<body>
<table class="contents"><tr><td>
<h4>Contents</h4>
<div><a href="htpataic01.html">1. Introduction</a></div>
<div><a href="htpataic02.html">2. The main loop</a></div>
<div><a href="htpataic03.html">3. Locations</a></div>
<div><a href="htpataic04.html">4. Objects</a></div>
<div><a href="htpataic05.html">5. Inventory</a></div>
<div><b>6. Passages</b></div>
<div><a href="htpataic07.html">7. Distance</a></div>
<div><a href="htpataic08.html">8. North, east, south, west</a></div>
<div><a href="htpataic09.html">9. Code generation</a></div>
<div><a href="htpataic10.html">10. More attributes</a></div>
<div><a href="htpataic11.html">11. Conditions</a></div>
<div><a href="htpataic12.html">12. Open and close</a></div>
<div><a href="htpataic13.html">13. The parser</a></div>
<div><a href="htpataic14.html">14. Multiple nouns</a></div>
<div><a href="htpataic15.html">15. Light and dark</a></div>
<div><a href="htpataic16.html">16. Savegame</a></div>
<div><a href="htpataic17.html">17. Test automation</a></div>
<div><a href="htpataic18.html">18. Abbreviations</a></div>
<div><a href="htpataic19.html">19. Conversations</a></div>
<div><a href="htpataic20.html">20. Combat</a></div>
<div><a href="htpataic21.html">21. Multi-player</a></div>
<div><a href="htpataic22.html">22. Client-server</a></div>
<div><a href="htpataic23.html">23. Database</a></div>
<div><a href="htpataic24.html">24. Speech</a></div>
<div><a href="htpataic25.html">25. JavaScript</a></div>
</td></tr></table>
<h1>How to program a text adventure in C</h1>
<p>
by Ruud Helderman
<<a href="mailto:r.helderman@hccnet.nl">r.helderman@hccnet.nl</a>>
</p>
<p>
Licensed under
<a href="https://github.com/helderman/htpataic/blob/master/LICENSE">MIT License</a>
</p>
<h2>6. Passages</h2>
<p class="intro">
It’s time to draw a map - and implement it!
</p>
<p>
The best tools for drawing a map will always be: a pencil and a piece of paper.
A basic map consists of <b>locations</b> (the rectangles),
connected by <b>passages</b> (the arrows).
We already created locations in chapter 3,
now we will start adding the passages.
</p>
<p>
<a href="mockmap06.gif" title="Click to enlarge">
<img class="mockmap" alt="Basic map with passages" src="mockmap06.gif" />
</a>
</p>
<p>
In the virtual world,
a ‘passage’ may be anything connecting two locations:
a road, a door, a stretch of sand in a desert.
Basically, a passage has the following properties:
<ul>
<li>A starting point (location).</li>
<li>A destination (location).</li>
<li>The narrative description, for example “a forest path”.</li>
<li>The tag by which the passage is referred to in the <i>go</i> command.</li>
</ul>
</p>
<p>
Considering these properties, it may not come as a surprise that
the <b>struct object</b> defined in chapter 4
is very suitable to store a passage.
In fact, a passage is not that different from an item or actor;
it is present at a certain location as a ‘visible exit’
(this location is the starting point).
It just behaves differently to certain commands.
In particular the command ‘go’:
applied to a passage, <i>go</i> will change the player’s location.
The target location (the ‘destination’)
can be stored in the <i>struct object</i> as a new attribute.
</p>
<table><tr>
<td class="old snippet">struct object {
const char *description;
const char *tag;
struct object *location;
<span class="new">struct object *destination;</span>
};
</td>
</tr></table>
<p>
Notes:
<ul>
<li>Obviously, <i>destination</i> is unused in most other objects
(items, actors), but I consider this to be a minor waste of space.
</li>
<li>A passage always runs in one direction;
to connect two locations bi-directionally,
we always have to create two separate passages.
This may seem clumsy at first, but it does give us great flexibility
in refining the behavior of command ‘go’;
see chapter 10 and 11.
</li>
<li>On a large map, you may find it tedious to create all passages by hand.
Then I strongly advise you to
<i>generate</i> the more repetitive parts of your map using custom tooling.
This will not be covered here,
but you may find some inspiration in chapter 9,
where we discuss code generation.
</li>
</ul>
</p>
<p>
So we expand the array of objects:
</p>
<table class="demo">
<tr><th>Sample output</th></tr>
<tr><td>
Welcome to Little Cave Adventure.<br />
You are in an open field.<br />
You see:<br />
a silver coin<br />
a burly guard<br />
a cave entrance<br />
<br />
--> go entrance<br />
OK.<br />
You are in a little cave.<br />
You see:<br />
a gold coin<br />
an exit<br />
<br />
--> go exit<br />
OK.<br />
You are in an open field.<br />
You see:<br />
a silver coin<br />
a burly guard<br />
a cave entrance<br />
<br />
--> go cave<br />
OK.<br />
You are in a little cave.<br />
You see:<br />
a gold coin<br />
an exit<br />
<br />
--> quit<br />
<br />
Bye!<br />
</td></tr>
</table>
<table class="code"><tr>
<th>object.h</th>
</tr><tr>
<td>
<ol>
<li class="old">typedef struct object {</li>
<li class="old"> const char *description;</li>
<li class="old"> const char *tag;</li>
<li class="old"> struct object *location;</li>
<li class="new"><span class="old"> </span>struct object *destination;</li>
<li class="old">} OBJECT;</li>
<li class="old"></li>
<li class="old">extern OBJECT objs[];</li>
<li class="old"></li>
<li class="old">#define field (objs + 0)</li>
<li class="old">#define cave (objs + 1)</li>
<li class="old">#define silver (objs + 2)</li>
<li class="old">#define gold (objs + 3)</li>
<li class="old">#define guard (objs + 4)</li>
<li class="old">#define player (objs + 5)</li>
<li class="new"><span class="old">#define </span>intoCave<span class="old"> (objs + 6)</span></li>
<li class="new">#define exitCave (objs + 7)</li>
<li class="new"></li>
<li class="new">#define endOfObjs (objs + 8)</li>
</ol>
</td>
</tr><tr>
<th>object.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="old"></li>
<li class="old">OBJECT objs[] = {</li>
<li class="new"><span class="old"> {"an open </span>field" ,<span class="old"> "field" , NULL </span>, NULL<span class="old"> },</span></li>
<li class="new"><span class="old"> {"a little </span>cave" ,<span class="old"> "cave" , NULL </span>, NULL<span class="old"> },</span></li>
<li class="new"><span class="old"> {"a silver </span>coin" ,<span class="old"> "silver" , </span>field, NULL<span class="old"> },</span></li>
<li class="new"><span class="old"> {"a gold coin" , "gold" , cave </span>, NULL<span class="old"> },</span></li>
<li class="new"><span class="old"> {"a burly </span>guard" ,<span class="old"> "guard" , </span>field, NULL<span class="old"> },</span></li>
<li class="new"><span class="old"> {"yourself" , "yourself", </span>field, NULL },</li>
<li class="new"> {"a cave entrance", "entrance", field, cave },</li>
<li class="new"> {"an exit" , "exit" , cave ,<span class="old"> field }</span></li>
<li class="old">};</li>
</ol>
</td>
</tr></table>
<p>
We will add a little helper function to <i>misc.c</i>
to determine whether a passage exists between two given locations.
</p>
<table class="code"><tr>
<th>misc.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern OBJECT *getPassage(OBJECT *from, OBJECT *to);</li>
<li class="old">extern OBJECT *actorHere(void);</li>
<li class="old">extern int listObjectsAtLocation(OBJECT *location);</li>
</ol>
</td>
</tr><tr>
<th>misc.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="old"></li>
<li class="new">OBJECT *getPassage(OBJECT *from, OBJECT *to)</li>
<li class="new">{</li>
<li class="new"> if (from != NULL && to != NULL)</li>
<li class="new"> {</li>
<li class="new"> OBJECT *obj;</li>
<li class="new"> for (obj = objs; obj < endOfObjs; obj++)</li>
<li class="new"> {</li>
<li class="new"> if (obj->location == from && obj->destination == to)</li>
<li class="new"> {</li>
<li class="new"> return obj;</li>
<li class="new"> }</li>
<li class="new"> }</li>
<li class="new"> }</li>
<li class="new"> return NULL;</li>
<li class="new">}</li>
<li class="new"></li>
<li class="old">OBJECT *actorHere(void)</li>
<li class="old">{</li>
<li class="old"> OBJECT *obj;</li>
<li class="old"> for (obj = objs; obj < endOfObjs; obj++)</li>
<li class="old"> {</li>
<li class="old"> if (obj->location == player->location && obj == guard)</li>
<li class="old"> {</li>
<li class="old"> return obj;</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="old"> return NULL;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">int listObjectsAtLocation(OBJECT *location)</li>
<li class="old">{</li>
<li class="old"> int count = 0;</li>
<li class="old"> OBJECT *obj;</li>
<li class="old"> for (obj = objs; obj < endOfObjs; obj++)</li>
<li class="old"> {</li>
<li class="old"> if (obj != player && obj->location == location)</li>
<li class="old"> {</li>
<li class="old"> if (count++ == 0)</li>
<li class="old"> {</li>
<li class="old"> printf("You see:\n");</li>
<li class="old"> }</li>
<li class="old"> printf("%s\n", obj->description);</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="old"> return count;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Lines 9 and 11:
we scan through all objects until we encounter a passage running
from location ‘from’ to location ‘to’.
</li>
<li>Line 13:
if a matching passage is found, we return a pointer to the passage object.
</li>
<li>Line 17:
if no such passage exists, we return NULL.
</li>
</ul>
</div>
<p>
We will use the new function <i>getPassage</i>
in the implementation of command ‘go’
to determine whether or not a passage exists
that can take the player to the desired location.
</p>
<table class="code"><tr>
<th>location.h</th>
</tr><tr>
<td>
<ol>
<li class="old">extern void executeLook(const char *noun);</li>
<li class="old">extern void executeGo(const char *noun);</li>
</ol>
</td>
</tr><tr>
<th>location.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdio.h></li>
<li class="old">#include <string.h></li>
<li class="old">#include "object.h"</li>
<li class="old">#include "misc.h"</li>
<li class="old">#include "noun.h"</li>
<li class="old"></li>
<li class="old">void executeLook(const char *noun)</li>
<li class="old">{</li>
<li class="old"> if (noun != NULL && strcmp(noun, "around") == 0)</li>
<li class="old"> {</li>
<li class="old"> printf("You are in %s.\n", player->location->description);</li>
<li class="old"> listObjectsAtLocation(player->location);</li>
<li class="old"> }</li>
<li class="old"> else</li>
<li class="old"> {</li>
<li class="old"> printf("I don't understand what you want to see.\n");</li>
<li class="old"> }</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">void executeGo(const char *noun)</li>
<li class="old">{</li>
<li class="old"> OBJECT *obj = getVisible("where you want to go", noun);</li>
<li class="old"> if (obj == NULL)</li>
<li class="old"> {</li>
<li class="old"> // already handled by getVisible</li>
<li class="old"> }</li>
<li class="new"> else if (getPassage(player->location, obj) != NULL)</li>
<li class="old"> {</li>
<li class="old"> printf("OK.\n");</li>
<li class="old"> player->location = obj;</li>
<li class="new"> executeLook("around");</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location != player->location)</li>
<li class="new"> {</li>
<li class="new"> printf("You don't see any %s here.\n", noun);</li>
<li class="new"> }</li>
<li class="new"> else if (obj->destination != NULL)</li>
<li class="new"> {</li>
<li class="new"> printf("OK.\n");</li>
<li class="new"> player->location = obj->destination;</li>
<li class="old"> executeLook("around");</li>
<li class="old"> }</li>
<li class="old"> else</li>
<li class="old"> {</li>
<li class="old"> printf("You can't get much closer than this.\n");</li>
<li class="old"> }</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 27:
as of now, command <i>go <location></i> will only be accepted
if a passage exists to take us there.
</li>
<li>Line 37-42:
besides <i>go <location></i> (lines 27-32),
we now offer an alternative way for the player to move:
<i>go <passage></i>.
For example, when in the field,
<i>go cave</i> and <i>go entrance</i> will have the same effect.
</li>
</ul>
</div>
<p>
We will also use the new function <i>getPassage</i> to determine whether
a certain location is visible from where the player is standing.
Locations that are not connected to the current location by a passage,
are not considered visible.
</p>
<table class="code"><tr>
<th>noun.h</th>
</tr><tr>
<td>
<ol>
<li class="old">extern OBJECT *getVisible(const char *intention, const char *noun);</li>
<li class="old">extern OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun);</li>
</ol>
</td>
</tr><tr>
<th>noun.c</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdbool.h></li>
<li class="old">#include <stdio.h></li>
<li class="old">#include <string.h></li>
<li class="old">#include "object.h"</li>
<li class="new">#include "misc.h"</li>
<li class="old"></li>
<li class="old">static bool objectHasTag(OBJECT *obj, const char *noun)</li>
<li class="old">{</li>
<li class="old"> return noun != NULL && *noun != '\0' && strcmp(noun, obj->tag) == 0;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">static OBJECT *getObject(const char *noun)</li>
<li class="old">{</li>
<li class="old"> OBJECT *obj, *res = NULL;</li>
<li class="old"> for (obj = objs; obj < endOfObjs; obj++)</li>
<li class="old"> {</li>
<li class="old"> if (objectHasTag(obj, noun))</li>
<li class="old"> {</li>
<li class="old"> res = obj;</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="old"> return res;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">OBJECT *getVisible(const char *intention, const char *noun)</li>
<li class="old">{</li>
<li class="old"> OBJECT *obj = getObject(noun);</li>
<li class="old"> if (obj == NULL)</li>
<li class="old"> {</li>
<li class="old"> printf("I don't understand %s.\n", intention);</li>
<li class="old"> }</li>
<li class="old"> else if (!(obj == player ||</li>
<li class="old"> obj == player->location ||</li>
<li class="old"> obj->location == player ||</li>
<li class="old"> obj->location == player->location ||</li>
<li class="new"><span class="old"> </span>getPassage(player->location, obj) !=<span class="old"> NULL ||</span></li>
<li class="new"><span class="old"> </span>(obj->location != NULL &&</li>
<li class="new"> (obj->location->location<span class="old"> == player ||</span></li>
<li class="new"><span class="old"> obj->location->location == </span>player->location))))</li>
<li class="old"> {</li>
<li class="old"> printf("You don't see any %s here.\n", noun);</li>
<li class="old"> obj = NULL;</li>
<li class="old"> }</li>
<li class="old"> return obj;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun)</li>
<li class="old">{</li>
<li class="old"> OBJECT *obj = NULL;</li>
<li class="old"> if (from == NULL)</li>
<li class="old"> {</li>
<li class="old"> printf("I don't understand who you want to %s.\n", verb);</li>
<li class="old"> }</li>
<li class="old"> else if ((obj = getObject(noun)) == NULL)</li>
<li class="old"> {</li>
<li class="old"> printf("I don't understand what you want to %s.\n", verb);</li>
<li class="old"> }</li>
<li class="old"> else if (obj == from)</li>
<li class="old"> {</li>
<li class="old"> printf("You should not be doing that to %s.\n", obj->description);</li>
<li class="old"> obj = NULL;</li>
<li class="old"> }</li>
<li class="old"> else if (obj->location != from)</li>
<li class="old"> {</li>
<li class="old"> if (from == player)</li>
<li class="old"> {</li>
<li class="old"> printf("You are not holding any %s.\n", noun);</li>
<li class="old"> }</li>
<li class="old"> else</li>
<li class="old"> {</li>
<li class="old"> printf("There appears to be no %s you can get from %s.\n",</li>
<li class="old"> noun, from->description);</li>
<li class="old"> }</li>
<li class="old"> obj = NULL;</li>
<li class="old"> }</li>
<li class="old"> return obj;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 36:
originally, every location was visible from every location.
We will now use function <i>getPassage</i>
to check whether or not two locations are adjacent.
This also prevents other objects from being mistaken for locations.
Originally, every object without a location, was considered to be a location.
This could have caused trouble in chapter 12,
where we will introduce some non-location objects
that (temporarily) do not have a location.
</li>
<li>Line 37:
the adjustment in line 36 does not relieve us from
a NULL check on <i>obj->location</i>.
Without this check, ‘go cave’ could cause
the last two rules (lines 38 and 39) to give a segmentation fault.
</li>
</ul>
</div>
<p>
The other modules
(<i>main.c</i>, <i>parsexec.c</i>, <i>inventory.c</i>, <i>move.c</i>)
remain unchanged, you can see them in the previous chapters.
</p>
<p>
Obviously, the map in this sample is trivial:
there are only two locations, and they are connected in both directions.
A third location will be added in chapter 12.
Homework assignment: draw a more elaborate map
and turn it into a list of objects (locations and passages).
</p>
<hr />
<table class="download"><tr><td>
<a class="button" href="code06/src.zip">⭳ Download source code</a>
<a class="button" href="https://repl.it/github/helderman/htpataic">🌀 Run on Repl.it</a>
</td></tr></table>
<p>
Next chapter: <a href="htpataic07.html">7. Distance</a>
</p>
</body>
</html>