-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtpataic05.html
711 lines (696 loc) · 22 KB
/
htpataic05.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
<!DOCTYPE html>
<html>
<head>
<title>5. Inventory</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><b>5. Inventory</b></div>
<div><a href="htpataic06.html">6. Passages</a></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>5. Inventory</h2>
<p class="intro">
We will now make it possible for actors to ‘hold’ items.
</p>
<p>
In the previous chapter, we made an array to store all objects, including the player himself.
By considering the player as an object,
we make the player an integrated part of the game,
instead of a spectator watching the virtual world from a safe distance.
</p>
<p>
The advantage of this approach becomes most obvious in a game with multiple player characters
(either <a href="http://en.wikipedia.org/wiki/MUD">MUD</a>-style
or a single-player game where you can switch between different characters),
but it is equally useful in a solo mission.
Player attributes no longer have to be stored in separate variables;
we can use the same data structure as used for any other object.
So the player, being an object:
</p>
<ul>
<li><b>has a location</b> (“where am I?”)</li>
<li><b>is a location</b> for any items the player may be holding.</li>
</ul>
<p>
This makes certain common actions very easy to implement:
</p>
<table cellpadding="8"><tr>
<th>Action</th>
<th>Typical<br />command</th>
<th>Example</th>
</tr><tr>
<td class="comment">Player moves from location to location</td>
<td class="comment">go</td>
<td class="snippet"><span class="red">player</span>->location = cave;</td>
</tr><tr>
<td class="comment">List items and actors present at a location</td>
<td class="comment">look</td>
<td class="snippet">listObjectsAtLocation(<span class="red">cave</span>);</td>
</tr><tr>
<td class="comment">Player gets an item</td>
<td class="comment">get</td>
<td class="snippet">silver->location = <span class="red">player</span>;</td>
</tr><tr>
<td class="comment">Player drops an item</td>
<td class="comment">drop</td>
<td class="snippet">silver->location = <span class="red">player</span>->location;</td>
</tr><tr>
<td class="comment">List the player’s inventory</td>
<td class="comment">inventory</td>
<td class="snippet">listObjectsAtLocation(<span class="red">player</span>);</td>
</tr><tr>
<td class="comment">Player gives an item to an actor</td>
<td class="comment">give</td>
<td class="snippet">silver->location = <span class="red">guard</span>;</td>
</tr><tr>
<td class="comment">Player receives an item from an actor</td>
<td class="comment">ask</td>
<td class="snippet">silver->location = <span class="red">player</span>;</td>
</tr><tr>
<td class="comment">List another actor’s inventory</td>
<td class="comment">examine</td>
<td class="snippet">listObjectsAtLocation(<span class="red">guard</span>);</td>
</tr></table>
<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 />
<br />
--> get silver<br />
You pick up a silver coin.<br />
<br />
--> inventory<br />
You see:<br />
a silver coin<br />
<br />
--> look around<br />
You are in an open field.<br />
You see:<br />
a burly guard<br />
<br />
--> give silver<br />
You give a silver coin to a burly guard.<br />
<br />
--> inventory<br />
You are empty-handed.<br />
<br />
--> ask silver<br />
You get a silver coin from a burly guard.<br />
<br />
--> inventory<br />
You see:<br />
a silver coin<br />
<br />
--> go cave<br />
OK.<br />
You are in a little cave.<br />
You see:<br />
a gold coin<br />
<br />
--> give silver<br />
There is nobody here to give that to.<br />
<br />
--> drop silver<br />
You drop a silver coin.<br />
<br />
--> look around<br />
You are in a little cave.<br />
You see:<br />
a silver coin<br />
a gold coin<br />
<br />
--> inventory<br />
You are empty-handed.<br />
<br />
--> quit<br />
<br />
Bye!<br />
</td></tr>
</table>
<p>
Commands <i>go</i> and <i>look</i> (the first two examples above)
were already implemented in the previous chapter.
Now we will introduce some typical <b>inventory</b> actions
for both the player and the non-player characters (commands
<i>get</i>, <i>drop</i>, <i>give</i>, <i>ask</i> and <i>inventory</i>).
</p>
<table class="code"><tr>
<th>parsexec.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 "location.h"</li>
<li class="new">#include "inventory.h"</li>
<li class="old"></li>
<li class="old">bool parseAndExecute(char *input)</li>
<li class="old">{</li>
<li class="old"> char *verb = strtok(input, " \n");</li>
<li class="old"> char *noun = strtok(NULL, " \n");</li>
<li class="old"> if (verb != NULL)</li>
<li class="old"> {</li>
<li class="old"> if (strcmp(verb, "quit") == 0)</li>
<li class="old"> {</li>
<li class="old"> return false;</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "look") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeLook(noun);</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "go") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeGo(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "get") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeGet(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "drop") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeDrop(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "give") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeGive(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "ask") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeAsk(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "inventory") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeInventory();</li>
<li class="old"> }</li>
<li class="old"> else</li>
<li class="old"> {</li>
<li class="old"> printf("I don't know how to '%s'.\n", verb);</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="old"> return true;</li>
<li class="old">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 5:
another new module is included.
</li>
<li>Line 25-44:
adding these 20 lines makes the game recognize five more commands.
</li>
</ul>
</div>
<p>
The new commands are implemented by the following module.
</p>
<table class="code"><tr>
<th>inventory.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern void executeGet(const char *noun);</li>
<li class="new">extern void executeDrop(const char *noun);</li>
<li class="new">extern void executeAsk(const char *noun);</li>
<li class="new">extern void executeGive(const char *noun);</li>
<li class="new">extern void executeInventory(void);</li>
</ol>
</td>
</tr><tr>
<th>inventory.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <stdio.h></li>
<li class="new">#include "object.h"</li>
<li class="new">#include "misc.h"</li>
<li class="new">#include "noun.h"</li>
<li class="new">#include "move.h"</li>
<li class="new"></li>
<li class="new">void executeGet(const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = getVisible("what you want to get", noun);</li>
<li class="new"> if (obj == NULL)</li>
<li class="new"> {</li>
<li class="new"> // already handled by getVisible</li>
<li class="new"> }</li>
<li class="new"> else if (obj == player)</li>
<li class="new"> {</li>
<li class="new"> printf("You should not be doing that to yourself.\n");</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location == player)</li>
<li class="new"> {</li>
<li class="new"> printf("You already have %s.\n", obj->description);</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location == guard)</li>
<li class="new"> {</li>
<li class="new"> printf("You should ask %s nicely.\n", obj->location->description);</li>
<li class="new"> }</li>
<li class="new"> else</li>
<li class="new"> {</li>
<li class="new"> moveObject(obj, player);</li>
<li class="new"> }</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeDrop(const char *noun)</li>
<li class="new">{</li>
<li class="new"> moveObject(getPossession(player, "drop", noun), player->location);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeAsk(const char *noun)</li>
<li class="new">{</li>
<li class="new"> moveObject(getPossession(actorHere(), "ask", noun), player);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeGive(const char *noun)</li>
<li class="new">{</li>
<li class="new"> moveObject(getPossession(player, "give", noun), actorHere());</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeInventory(void)</li>
<li class="new">{</li>
<li class="new"> if (listObjectsAtLocation(player) == 0)</li>
<li class="new"> {</li>
<li class="new"> printf("You are empty-handed.\n");</li>
<li class="new"> }</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 9, 34, 39, 44:
functions <i>getVisible</i> and <i>getPossession</i>
are used to determine which object belongs to a given noun;
see <i>noun.c</i> below.
</li>
<li>Line 28, 34, 39, 44:
function <i>moveObject</i>
is used to transport the object to its new location;
see <i>move.c</i> below.
</li>
<li>Line 39, 44:
due to the simplicity of our verb-noun parser,
commands <i>ask</i> and <i>give</i> have only one argument: the item.
The actor involved is implied by using function <i>actorHere</i>
(defined below).
</li>
<li>Line 49:
function <i>listObjectsAtLocation</i>
was already defined in the previous chapter (<i>misc.c</i>).
Its return value tells us how many objects were found.
</li>
<li>Line 51:
the user expects some response, even when the list of objects is empty.
</li>
</ul>
</div>
<p>
Essentially, commands <i>get</i>, <i>drop</i>, <i>give</i> and <i>ask</i>
do little more than move an item from one place to another.
A single function <i>moveObject</i>
can perform that action for all four commands.
</p>
<table class="code"><tr>
<th>move.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern void moveObject(OBJECT *obj, OBJECT *to);</li>
</ol>
</td>
</tr><tr>
<th>move.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <stdio.h></li>
<li class="new">#include "object.h"</li>
<li class="new"></li>
<li class="new">static void describeMove(OBJECT *obj, OBJECT *to)</li>
<li class="new">{</li>
<li class="new"> if (to == player->location)</li>
<li class="new"> {</li>
<li class="new"> printf("You drop %s.\n", obj->description);</li>
<li class="new"> }</li>
<li class="new"> else if (to != player)</li>
<li class="new"> {</li>
<li class="new"> printf(to == guard ? "You give %s to %s.\n" : "You put %s in %s.\n",</li>
<li class="new"> obj->description, to->description);</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location == player->location)</li>
<li class="new"> {</li>
<li class="new"> printf("You pick up %s.\n", obj->description);</li>
<li class="new"> }</li>
<li class="new"> else</li>
<li class="new"> {</li>
<li class="new"> printf("You get %s from %s.\n",</li>
<li class="new"> obj->description, obj->location->description);</li>
<li class="new"> }</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void moveObject(OBJECT *obj, OBJECT *to)</li>
<li class="new">{</li>
<li class="new"> if (obj == NULL)</li>
<li class="new"> {</li>
<li class="new"> // already handled by getVisible or getPossession</li>
<li class="new"> }</li>
<li class="new"> else if (to == NULL)</li>
<li class="new"> {</li>
<li class="new"> printf("There is nobody here to give that to.\n");</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location == NULL)</li>
<li class="new"> {</li>
<li class="new"> printf("That is way too heavy.\n");</li>
<li class="new"> }</li>
<li class="new"> else</li>
<li class="new"> {</li>
<li class="new"> describeMove(obj, to);</li>
<li class="new"> obj->location = to;</li>
<li class="new"> }</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 6-23:
various messages to confirm that the move was successful.
Alternatively, we could have given a simple ‘OK’.
</li>
<li>Line 28-39:
various conditions under which the move will not take place.
</li>
<li>Line 32-35:
there is only one possibility for the item’s destination to be NULL:
when trying to ‘give’ something
at a location where the player is alone.
</li>
<li>Line 36-39:
some objects are not meant to be picked up;
for example <i>field</i> and <i>cave</i>.
The condition to recognize those objects, is still rather crude here.
For example, it is currently possible to pick up the guard.
This will be improved in chapter 10.
</li>
<li>Line 43:
the core of the function; here the object is actually moved.
</li>
</ul>
</div>
<p>
Command ‘get’ uses function <i>getVisible</i>
to translate a noun to an object,
just like command ‘go’ does; see the previous chapter.
But for commands that operate on objects the player (or some other actor)
is already holding (<i>drop</i>, <i>ask</i>, <i>give</i>),
we need something slightly different.
We will add a function <i>getPossession</i> to <i>noun.c</i>.
</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="new">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="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="old"> obj->location == NULL ||</li>
<li class="old"> obj->location->location == player ||</li>
<li class="old"> obj->location->location == 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="new"></li>
<li class="new">OBJECT *getPossession(OBJECT *from, const char *verb, const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = NULL;</li>
<li class="new"> if (from == NULL)</li>
<li class="new"> {</li>
<li class="new"> printf("I don't understand who you want to %s.\n", verb);</li>
<li class="new"> }</li>
<li class="new"> else if ((obj = getObject(noun)) == NULL)</li>
<li class="new"> {</li>
<li class="new"> printf("I don't understand what you want to %s.\n", verb);</li>
<li class="new"> }</li>
<li class="new"> else if (obj == from)</li>
<li class="new"> {</li>
<li class="new"> printf("You should not be doing that to %s.\n", obj->description);</li>
<li class="new"> obj = NULL;</li>
<li class="new"> }</li>
<li class="new"> else if (obj->location != from)</li>
<li class="new"> {</li>
<li class="new"> if (from == player)</li>
<li class="new"> {</li>
<li class="new"> printf("You are not holding any %s.\n", noun);</li>
<li class="new"> }</li>
<li class="new"> else</li>
<li class="new"> {</li>
<li class="new"> printf("There appears to be no %s you can get from %s.\n",</li>
<li class="new"> noun, from->description);</li>
<li class="new"> }</li>
<li class="new"> obj = NULL;</li>
<li class="new"> }</li>
<li class="new"> return obj;</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 45-75:
Just like function <i>getVisible</i>,
the new function <i>getPossession</i> is a wrapper around <i>getObject</i>
(see line 52)
that either returns a matching object,
or NULL if no appropriate object matches the noun.
</li>
</ul>
</p>
</div>
<p>
Function <i>actorHere</i> is used in commands <i>give</i> and <i>ask</i>,
but it could be useful for other commands as well.
So we define it in <i>misc.c</i>.
</p>
<table class="code"><tr>
<th>misc.h</th>
</tr><tr>
<td>
<ol>
<li class="new">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 *actorHere(void)</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 == player->location && obj == guard)</li>
<li class="new"> {</li>
<li class="new"> return obj;</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">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>Line 4-15:
function <i>actorHere</i> returns a pointer to the actor
present at the same location as the player,
or NULL if the player is alone.
In line 9 there is an exhaustive, hard-coded list of non-player characters
(up until now, just one: the <i>guard</i>).
In chapter 10, we will start using an attribute as a more elegant way
to distinguish actors from items and other non-actor objects.
</li>
</ul>
</div>
<p>
The other modules (<i>main.c</i>, <i>location.c</i>, <i>object.c</i>)
remain unchanged,
you can see them in the previous chapters.
</p>
<p>
In chapter 12, you will see us add more commands.
But first, we will be making some improvements to command <i>go</i>.
</p>
<hr />
<table class="download"><tr><td>
<a class="button" href="code05/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="htpataic06.html">6. Passages</a>
</p>
</body>
</html>