-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhtpataic12.html
1007 lines (988 loc) · 36.7 KB
/
htpataic12.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
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
<!DOCTYPE html>
<html>
<head>
<title>12. Open and close</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><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><b>12. Open and close</b></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>12. Open and close</h2>
<p class="intro">
In the previous chapter,
we used ‘condition’ functions to make objects disappear.
But of course, there is a much simpler way to achieve the same:
just clear the object’s <i>location</i> attribute!
</p>
<p>
The cave entrance was a typical example
where <b>condition functions</b> work particularly well.
This is because the entrance is affected by attributes in <i>other</i> objects
(the guard and the silver coin);
the functions make it possible to keep all the logic together.
</p>
<p>
Let’s take a more straightforward example.
Suppose the cave has a door leading to a backroom.
Just a simple doorway, which the player can open and close.
Just like in the previous chapter, we will use two objects for the passage;
one to represent the open door, and another for when it is closed.
</p>
<table><tr>
<td class="snippet">- backroom
description "a backroom"
tags "backroom"
details "The room is dusty and messy.\n"
- openDoorToBackroom
description "an open door to the south"
tags "south", "door", "doorway"<span class="red">
destination backroom</span>
details "The door is open.\n"
textGo "You walk through the door into the backroom.\n"
- closedDoorToBackroom
description "a closed door to the south"
tags "south", "door", "doorway"<span class="red">
location cave
prospect backroom</span>
details "The door is closed.\n"
textGo "The door is closed.\n"
</td>
</tr></table>
<p>
Naturally, the door should be accessible from the other side as well.
</p>
<table><tr>
<td class="snippet">- openDoorToCave
description "an open door to the north"
tags "north", "door", "doorway"<span class="red">
destination cave</span>
details "The door is open.\n"
textGo "You walk through the door into the cave.\n"
- closedDoorToCave
description "a closed door to the north"
tags "north", "door", "doorway"<span class="red">
location backroom
prospect cave</span>
details "The door is closed.\n"
textGo "The door is closed.\n"
</td>
</tr></table>
<p>
Notice I only gave the closed doorways a location; the open ones have none.
So initially, the door is closed
(hence the dashed arrows between cave and backroom
in the generated map you see on the right).
To open the door, all we have to do is swap the locations.
</p>
<img class="genmap" src="code12/map.png" />
<table><tr>
<td class="snippet">openDoorToBackroom->location = cave;
closedDoorToBackroom->location = NULL;
openDoorToCave->location = backroom;
closedDoorToCave->location = NULL;
</td>
</tr></table>
<p>
Let’s create a helper function to accommodate this.
</p>
<table><tr>
<td class="snippet">void swapLocations(OBJECT *obj1, OBJECT *obj2)
{
OBJECT *tmp = obj1->location;
obj1->location = obj2->location;
obj2->location = tmp;
}
</td>
</tr></table>
<p>
Now the following statements can be used to open the door;
and once it is open, the same statements will close it again.
</p>
<table><tr>
<td class="snippet">swapLocations(openDoorToBackroom, closedDoorToBackroom);
swapLocations(openDoorToCave, closedDoorToCave);
</td>
</tr></table>
<p>
The helper function is particularly convenient
when the object in question is <i>movable</i>.
For example, a <b>box</b> can be opened and closed,
but it is also an item that can be picked up and moved elsewhere.
In other words, its location is not fixed.
Function <i>swapLocations</i> does not rely on a fixed location,
since it passes the current location back and forth between two objects.
</p>
<p>
Of course, a box is not a passage;
the player is always on the outside, so a single pair of objects will suffice,
and so will a single call to <i>swapLocations</i>.
</p>
<table><tr>
<td class="snippet">swapLocations(openBox, closedBox);
</td>
</tr></table>
<p>
This is more or less all we need
to implement some new commands <i>open</i> and <i>close</i>.
Below is a simple implementation of <i>open</i>;
the implementation of <i>close</i> is similar.
</p>
<table><tr>
<td class="snippet">OBJECT *obj = parseObject(noun);
if (obj == closedDoorToBackRoom || obj == closedDoorToCave)
{<span class="red">
swapLocations(openDoorToBackroom, closedDoorToBackroom);
swapLocations(openDoorToCave, closedDoorToCave);</span>
printf("OK.\n");
}
else if (obj == closedBox)
{<span class="red">
swapLocations(openBox, closedBox);</span>
printf("OK.\n");
}
else if (obj == openDoorToBackRoom || obj == openDoorToCave || obj == openBox)
{
printf("That is already open.\n");
}
else
{
printf("That cannot be opened.\n");
}
</td>
</tr></table>
<p>
To make things slightly more complicated,
we can put a <b>lock</b> on the door or on the box.
This requires (at least) <i>three</i> mutually exclusive objects;
one for each of the possible states:
<i>open</i>, <i>closed</i> and <i>locked</i>.
But we can still use the same function to swap locations of the objects.
For example, here’s how to unlock a locked box; and vice versa.
</p>
<table><tr>
<td class="snippet">swapLocations(closedBox, lockedBox);
</td>
</tr></table>
<p>
There is some overhead involved in the other commands.
Our implementation of command <i>open</i> must be expanded
to handle the new object <i>lockedBox</i>:
</p>
<table><tr>
<td class="snippet">...
else if (obj == lockedBox)
{
printf("You can't, it is locked.\n");
}
...
</td>
</tr></table>
<p>
It may be clear that the number of lines of code
is proportional to the number of doors in the game
(and boxes and other objects that can be opened).
So if your game has more than just a handful of doors,
then it is a good idea to go for a more generic solution.
By the way, this is something that goes for every command:
when it concerns a good many objects, try to write generic code;
but when you are dealing with one or two special cases,
just stick to straightforward, specialized code.
</p>
<p>
Generic code typically comes with a data-driven approach.
In other words,
we need to add one or more attributes to our object structure.
In this particular case, we will be adding a function pointer
for each of the commands we wish to support:
<i>open</i>, <i>close</i>, <i>lock</i> and <i>unlock</i>.
</p>
<table class="code"><tr>
<th>object.txt</th>
</tr><tr>
<td>
<ol>
<li class="old">#include <stdbool.h></li>
<li class="old">#include <stdio.h></li>
<li class="old">#include "object.h"</li>
<li class="new">#include "toggle.h"</li>
<li class="old"></li>
<li class="old">typedef struct object {</li>
<li class="old"> bool (*condition)(void);</li>
<li class="old"> const char *description;</li>
<li class="old"> const char **tags;</li>
<li class="old"> struct object *location;</li>
<li class="old"> struct object *destination;</li>
<li class="old"> struct object *prospect;</li>
<li class="old"> const char *details;</li>
<li class="old"> const char *contents;</li>
<li class="old"> const char *textGo;</li>
<li class="old"> int weight;</li>
<li class="old"> int capacity;</li>
<li class="old"> int health;</li>
<li class="new"><span class="old"> </span>void (*open)(void);</li>
<li class="new"> void (*close)(void);</li>
<li class="new"> void (*lock)(void);</li>
<li class="new"> void (*unlock)(void);</li>
<li class="old">} OBJECT;</li>
<li class="old"></li>
<li class="old">extern OBJECT objs[];</li>
<li class="old"></li>
<li class="old">- field</li>
<li class="old"> description "an open field"</li>
<li class="old"> tags "field"</li>
<li class="old"> details "The field is a nice and quiet place under a clear blue sky."</li>
<li class="old"> capacity 9999</li>
<li class="old"></li>
<li class="old">- cave</li>
<li class="old"> description "a little cave"</li>
<li class="old"> tags "cave"</li>
<li class="old"> details "The cave is just a cold, damp, rocky chamber."</li>
<li class="old"> capacity 9999</li>
<li class="old"></li>
<li class="old">- silver</li>
<li class="old"> description "a silver coin"</li>
<li class="old"> tags "silver", "coin", "silver coin"</li>
<li class="old"> location field</li>
<li class="old"> details "The coin has an eagle on the obverse."</li>
<li class="old"> weight 1</li>
<li class="old"></li>
<li class="old">- gold</li>
<li class="old"> description "a gold coin"</li>
<li class="old"> tags "gold", "coin", "gold coin"</li>
<li class="new"><span class="old"> location </span>openBox</li>
<li class="old"> details "The shiny coin seems to be a rare and priceless artefact."</li>
<li class="old"> weight 1</li>
<li class="old"></li>
<li class="old">- guard</li>
<li class="old"> description "a burly guard"</li>
<li class="old"> tags "guard", "burly guard"</li>
<li class="old"> location field</li>
<li class="old"> details "The guard is a really big fellow."</li>
<li class="old"> contents "He has"</li>
<li class="old"> health 100</li>
<li class="old"> capacity 20</li>
<li class="old"></li>
<li class="old">- player</li>
<li class="old"> description "yourself"</li>
<li class="old"> tags "yourself"</li>
<li class="old"> location field</li>
<li class="old"> details "You would need a mirror to look at yourself."</li>
<li class="old"> contents "You have"</li>
<li class="old"> health 100</li>
<li class="old"> capacity 20</li>
<li class="old"></li>
<li class="old">- intoCave</li>
<li class="old"> condition { return guard->health == 0 || silver->location == guard; }</li>
<li class="old"> description "a cave entrance to the east"</li>
<li class="old"> tags "east", "entrance"</li>
<li class="old"> location field</li>
<li class="old"> destination cave</li>
<li class="old"> details "The entrance is just a narrow opening in a small outcrop."</li>
<li class="old"> textGo "You walk into the cave."</li>
<li class="new"><span class="old"> </span>open isAlreadyOpen</li>
<li class="old"></li>
<li class="old">- intoCaveBlocked</li>
<li class="old"> condition { return guard->health > 0 && silver->location != guard; }</li>
<li class="old"> description "a cave entrance to the east"</li>
<li class="old"> tags "east", "entrance"</li>
<li class="old"> location field</li>
<li class="old"> prospect cave</li>
<li class="old"> details "The entrance is just a narrow opening in a small outcrop."</li>
<li class="old"> textGo "The guard stops you from walking into the cave."</li>
<li class="new"><span class="old"> </span>open isAlreadyOpen</li>
<li class="old"></li>
<li class="old">- exitCave</li>
<li class="old"> description "an exit to the west"</li>
<li class="old"> tags "west", "exit"</li>
<li class="old"> location cave</li>
<li class="old"> destination field</li>
<li class="old"> details "Sunlight pours in through an opening in the cave's wall."</li>
<li class="old"> textGo "You walk out of the cave."</li>
<li class="new"><span class="old"> </span>open isAlreadyOpen</li>
<li class="old"></li>
<li class="old">- wallField</li>
<li class="old"> description "dense forest all around"</li>
<li class="old"> tags "west", "north", "south", "forest"</li>
<li class="old"> location field</li>
<li class="old"> details "The field is surrounded by trees and undergrowth."</li>
<li class="old"> textGo "Dense forest is blocking the way."</li>
<li class="old"></li>
<li class="old">- wallCave</li>
<li class="old"> description "solid rock all around"</li>
<li class="old"> tags "east", "north", "rock"</li>
<li class="old"> location cave</li>
<li class="old"> details "Carved in stone is a secret password 'abccb'."</li>
<li class="old"> textGo "Solid rock is blocking the way."</li>
<li class="old"></li>
<li class="new">- backroom</li>
<li class="new"> description "a backroom"</li>
<li class="new"> tags "backroom"</li>
<li class="new"> details "The room is dusty and messy."</li>
<li class="new"> capacity 9999</li>
<li class="new"></li>
<li class="new">- wallBackroom</li>
<li class="new"> description "solid rock all around"</li>
<li class="new"> tags "east", "west", "south", "rock"</li>
<li class="new"> location backroom</li>
<li class="new"> details "Trendy wallpaper covers the rock walls."</li>
<li class="new"> textGo "Solid rock is blocking the way."</li>
<li class="new"></li>
<li class="new">- openDoorToBackroom</li>
<li class="new"> description "an open door to the south"</li>
<li class="new"> tags "south", "door", "doorway"</li>
<li class="new"> destination backroom</li>
<li class="new"> details "The door is open."</li>
<li class="new"> textGo "You walk through the door into a backroom."</li>
<li class="new"> open isAlreadyOpen</li>
<li class="new"> close toggleDoorToBackroom</li>
<li class="new"></li>
<li class="new">- closedDoorToBackroom</li>
<li class="new"> description "a closed door to the south"</li>
<li class="new"> tags "south", "door", "doorway"</li>
<li class="new"> location cave</li>
<li class="new"> prospect backroom</li>
<li class="new"> details "The door is closed."</li>
<li class="new"> textGo "The door is closed."</li>
<li class="new"> open toggleDoorToBackroom</li>
<li class="new"> close isAlreadyClosed</li>
<li class="new"></li>
<li class="new">- openDoorToCave</li>
<li class="new"> description "an open door to the north"</li>
<li class="new"> tags "north", "door", "doorway"</li>
<li class="new"> destination cave</li>
<li class="new"> details "The door is open."</li>
<li class="new"> textGo "You walk through the door into the cave."</li>
<li class="new"> open isAlreadyOpen</li>
<li class="new"> close toggleDoorToCave</li>
<li class="new"></li>
<li class="new">- closedDoorToCave</li>
<li class="new"> description "a closed door to the north"</li>
<li class="new"> tags "north", "door", "doorway"</li>
<li class="new"> location backroom</li>
<li class="new"> prospect cave</li>
<li class="new"> details "The door is closed."</li>
<li class="new"> textGo "The door is closed."</li>
<li class="new"> open toggleDoorToCave</li>
<li class="new"> close isAlreadyClosed</li>
<li class="new"></li>
<li class="new">- openBox</li>
<li class="new"> description "a wooden box"</li>
<li class="new"> tags "box", "wooden box"</li>
<li class="new"> details "The box is open."</li>
<li class="new"> weight 5</li>
<li class="new"> capacity 10</li>
<li class="new"> open isAlreadyOpen</li>
<li class="new"> close toggleBox</li>
<li class="new"> lock isStillOpen</li>
<li class="new"> unlock isAlreadyOpen</li>
<li class="new"></li>
<li class="new">- closedBox</li>
<li class="new"> description "a wooden box"</li>
<li class="new"> tags "box", "wooden box"</li>
<li class="new"> details "The box is closed."</li>
<li class="new"> weight 5</li>
<li class="new"> open toggleBox</li>
<li class="new"> close isAlreadyClosed</li>
<li class="new"> lock toggleBoxLock</li>
<li class="new"> unlock isAlreadyUnlocked</li>
<li class="new"></li>
<li class="new">- lockedBox</li>
<li class="new"> description "a wooden box"</li>
<li class="new"> tags "box", "wooden box"</li>
<li class="new"> location backroom</li>
<li class="new"> details "The box is closed."</li>
<li class="new"> weight 5</li>
<li class="new"> open isStillLocked</li>
<li class="new"> close isAlreadyClosed</li>
<li class="new"> lock isAlreadyLocked</li>
<li class="new"> unlock toggleBoxLock</li>
<li class="new"></li>
<li class="new">- keyForBox</li>
<li class="new"> description "a tiny key"</li>
<li class="new"> tags "key", "tiny key"</li>
<li class="new"> location cave</li>
<li class="new"> details "The key is really small and shiny."</li>
<li class="new"> weight 1</li>
</ol>
</td>
</tr></table>
<div class="explanation">
<p>
Explanation:
</p>
<ul>
<li>Line 89:
at first sight, <i>isAlreadyOpen</i> may seem inappropriate here;
technically, <i>intoCaveBlocked</i> is a closed passage.
But storywise, it is still an opening.
</li>
<li>Line 169, 180, 191:
If you prefer a heavy treasure chest instead of a box,
then all you have to do is increase the weight
(and adjust the relevant text and tags accordingly).
</li>
</ul>
</div>
<p>
To avoid
<a href="http://en.wikipedia.org/wiki/Duplicate_code">duplicate code</a>,
I deliberately did <i>not</i> use anonymous functions this time.
Instead, we will implement the necessary logic in a separate module.
Function <i>swapLocations</i> is in there too,
but in a slightly extended version,
which will also output feedback to the user.
</p>
<table class="code"><tr>
<th>toggle.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern void cannotBeOpened(void);</li>
<li class="new">extern void cannotBeClosed(void);</li>
<li class="new">extern void cannotBeLocked(void);</li>
<li class="new">extern void cannotBeUnlocked(void);</li>
<li class="new"></li>
<li class="new">extern void isAlreadyOpen(void);</li>
<li class="new">extern void isAlreadyClosed(void);</li>
<li class="new">extern void isAlreadyLocked(void);</li>
<li class="new">extern void isAlreadyUnlocked(void);</li>
<li class="new"></li>
<li class="new">extern void isStillOpen(void);</li>
<li class="new">extern void isStillLocked(void);</li>
<li class="new"></li>
<li class="new">extern void toggleDoorToBackroom(void);</li>
<li class="new">extern void toggleDoorToCave(void);</li>
<li class="new">extern void toggleBox(void);</li>
<li class="new">extern void toggleBoxLock(void);</li>
</ol>
</td>
</tr><tr>
<th>toggle.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <stdbool.h></li>
<li class="new">#include <stdio.h></li>
<li class="new">#include "object.h"</li>
<li class="new"></li>
<li class="new">static void swapLocations(const char *verb1, OBJECT *obj1,</li>
<li class="new"> const char *verb2, OBJECT *obj2)</li>
<li class="new">{</li>
<li class="new"> OBJECT *tmp = obj1->location;</li>
<li class="new"> OBJECT *obj = tmp != NULL ? obj1 : obj2;</li>
<li class="new"> const char *verb = tmp != NULL ? verb1 : verb2;</li>
<li class="new"> obj1->location = obj2->location;</li>
<li class="new"> obj2->location = tmp;</li>
<li class="new"> if (verb != NULL) printf("You %s %s.\n", verb, obj->description);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void cannotBeOpened(void) { printf("That cannot be opened.\n"); }</li>
<li class="new">void cannotBeClosed(void) { printf("That cannot be closed.\n"); }</li>
<li class="new">void cannotBeLocked(void) { printf("That cannot be locked.\n"); }</li>
<li class="new">void cannotBeUnlocked(void) { printf("That cannot be unlocked.\n"); }</li>
<li class="new"></li>
<li class="new">void isAlreadyOpen(void) { printf("That is already open.\n"); }</li>
<li class="new">void isAlreadyClosed(void) { printf("That is already closed.\n"); }</li>
<li class="new">void isAlreadyLocked(void) { printf("That is already locked.\n"); }</li>
<li class="new">void isAlreadyUnlocked(void) { printf("That is already unlocked.\n"); }</li>
<li class="new"></li>
<li class="new">void isStillOpen(void) { printf("That is still open.\n"); }</li>
<li class="new">void isStillLocked(void) { printf("That is locked.\n"); }</li>
<li class="new"></li>
<li class="new">void toggleDoorToBackroom(void)</li>
<li class="new">{</li>
<li class="new"> swapLocations(NULL, openDoorToCave, NULL, closedDoorToCave);</li>
<li class="new"> swapLocations("close", openDoorToBackroom, "open", closedDoorToBackroom);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void toggleDoorToCave(void)</li>
<li class="new">{</li>
<li class="new"> swapLocations(NULL, openDoorToBackroom, NULL, closedDoorToBackroom);</li>
<li class="new"> swapLocations("close", openDoorToCave, "open", closedDoorToCave);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void toggleBox(void)</li>
<li class="new">{</li>
<li class="new"> swapLocations("close", openBox, "open", closedBox);</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void toggleBoxLock(void)</li>
<li class="new">{</li>
<li class="new"> if (keyForBox->location == player)</li>
<li class="new"> {</li>
<li class="new"> swapLocations("lock", closedBox, "unlock", lockedBox);</li>
<li class="new"> }</li>
<li class="new"> else</li>
<li class="new"> {</li>
<li class="new"> printf("You don't have a key.\n");</li>
<li class="new"> }</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<p>
As announced earlier, the implementations of the four commands
<i>open</i>, <i>close</i>, <i>lock</i> and <i>unlock</i>
are totally generic.
</p>
<table class="code"><tr>
<th>openclose.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern void executeOpen(const char *noun);</li>
<li class="new">extern void executeClose(const char *noun);</li>
<li class="new">extern void executeLock(const char *noun);</li>
<li class="new">extern void executeUnlock(const char *noun);</li>
</ol>
</td>
</tr><tr>
<th>openclose.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <stdbool.h></li>
<li class="new">#include <stdio.h></li>
<li class="new">#include "object.h"</li>
<li class="new">#include "reach.h"</li>
<li class="new"></li>
<li class="new">void executeOpen(const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = reachableObject("what you want to open", noun);</li>
<li class="new"> if (obj != NULL) (*obj->open)();</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeClose(const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = reachableObject("what you want to close", noun);</li>
<li class="new"> if (obj != NULL) (*obj->close)();</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeLock(const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = reachableObject("what you want to lock", noun);</li>
<li class="new"> if (obj != NULL) (*obj->lock)();</li>
<li class="new">}</li>
<li class="new"></li>
<li class="new">void executeUnlock(const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = reachableObject("what you want to unlock", noun);</li>
<li class="new"> if (obj != NULL) (*obj->unlock)();</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<p>
Above, I used a generic function <i>reachableObject</i>
to handle objects that are not here;
see below for its implementation.
This way, we don’t have to write the same code four times
(once for every <i>execute</i> function).
More commands will be added in chapter 15;
these will benefit from the same function.
</p>
<table class="code"><tr>
<th>reach.h</th>
</tr><tr>
<td>
<ol>
<li class="new">extern OBJECT *reachableObject(const char *intention, const char *noun);</li>
</ol>
</td>
</tr><tr>
<th>reach.c</th>
</tr><tr>
<td>
<ol>
<li class="new">#include <stdbool.h></li>
<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"></li>
<li class="new">OBJECT *reachableObject(const char *intention, const char *noun)</li>
<li class="new">{</li>
<li class="new"> OBJECT *obj = getVisible(intention, noun);</li>
<li class="new"> switch (getDistance(player, obj))</li>
<li class="new"> {</li>
<li class="new"> case distSelf:</li>
<li class="new"> printf("You should not be doing that to yourself.\n");</li>
<li class="new"> break;</li>
<li class="new"> case distHeldContained:</li>
<li class="new"> case distHereContained:</li>
<li class="new"> printf("You would have to get it from %s first.\n",</li>
<li class="new"> obj->location->description);</li>
<li class="new"> break;</li>
<li class="new"> case distOverthere:</li>
<li class="new"> printf("Too far away, move closer please.\n");</li>
<li class="new"> break;</li>
<li class="new"> case distNotHere:</li>
<li class="new"> case distUnknownObject:</li>
<li class="new"> // already handled by getVisible</li>
<li class="new"> break;</li>
<li class="new"> default:</li>
<li class="new"> return obj;</li>
<li class="new"> }</li>
<li class="new"> return NULL;</li>
<li class="new">}</li>
</ol>
</td>
</tr></table>
<p>
The necessary modifications to <i>object.awk</i> are very straightforward:
</p>
<table class="code"><tr>
<th>object.awk</th>
</tr><tr>
<td>
<ol>
<li class="old">BEGIN {</li>
<li class="old"> count = 0;</li>
<li class="old"> obj = "";</li>
<li class="old"> if (pass == "c2") {</li>
<li class="old"> print "\nstatic bool alwaysTrue(void) { return true; }";</li>
<li class="old"> print "\nOBJECT objs[] = {";</li>
<li class="old"> }</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">/^- / {</li>
<li class="old"> outputRecord(",");</li>
<li class="old"> obj = $2;</li>
<li class="old"> prop["condition"] = "alwaysTrue";</li>
<li class="old"> prop["description"] = "NULL";</li>
<li class="old"> prop["tags"] = "";</li>
<li class="old"> prop["location"] = "NULL";</li>
<li class="old"> prop["destination"] = "NULL";</li>
<li class="old"> prop["prospect"] = "";</li>
<li class="old"> prop["details"] = "\"You see nothing special.\"";</li>
<li class="old"> prop["contents"] = "\"You see\"";</li>
<li class="old"> prop["textGo"] = "\"You can't get much closer than this.\"";</li>
<li class="old"> prop["weight"] = "99";</li>
<li class="old"> prop["capacity"] = "0";</li>
<li class="old"> prop["health"] = "0";</li>
<li class="new"><span class="old"> </span>prop["open"] = "cannotBeOpened";</li>
<li class="new"> prop["close"] = "cannotBeClosed";</li>
<li class="new"> prop["lock"] = "cannotBeLocked";</li>
<li class="new"> prop["unlock"] = "cannotBeUnlocked";</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">obj && /^[ \t]+[a-z]/ {</li>
<li class="old"> name = $1;</li>
<li class="old"> $1 = "";</li>
<li class="old"> if (name in prop) {</li>
<li class="old"> prop[name] = $0;</li>
<li class="old"> if (/^[ \t]*\{/) {</li>
<li class="old"> prop[name] = name count;</li>
<li class="old"> if (pass == "c1") print "static bool " prop[name] "(void) " $0;</li>
<li class="old"> }</li>
<li class="old"> }</li>
<li class="old"> else if (pass == "c2") {</li>
<li class="old"> print "#error \"" FILENAME " line " NR ": unknown attribute '" name "'\"";</li>
<li class="old"> }</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">!obj && pass == (/^#include/ ? "c1" : "h") {</li>
<li class="old"> print;</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">END {</li>
<li class="old"> outputRecord("\n};");</li>
<li class="old"> if (pass == "h") {</li>
<li class="old"> print "\n#define endOfObjs\t(objs + " count ")";</li>
<li class="old"> print "\n#define validObject(obj)\t" \</li>
<li class="old"> "((obj) != NULL && (*(obj)->condition)())";</li>
<li class="old"> }</li>
<li class="old">}</li>
<li class="old"></li>
<li class="old">function outputRecord(separator)</li>
<li class="old">{</li>
<li class="old"> if (obj) {</li>
<li class="old"> if (pass == "h") {</li>
<li class="old"> print "#define " obj "\t(objs + " count ")";</li>
<li class="old"> }</li>
<li class="old"> else if (pass == "c1") {</li>
<li class="old"> print "static const char *tags" count "[] = {" prop["tags"] ", NULL};";</li>
<li class="old"> }</li>
<li class="old"> else if (pass == "c2") {</li>
<li class="old"> print "\t{\t/* " count " = " obj " */";</li>
<li class="old"> print "\t\t" prop["condition"] ",";</li>
<li class="old"> print "\t\t" prop["description"] ",";</li>
<li class="old"> print "\t\ttags" count ",";</li>
<li class="old"> print "\t\t" prop["location"] ",";</li>
<li class="old"> print "\t\t" prop["destination"] ",";</li>
<li class="old"> print "\t\t" prop[prop["prospect"] ? "prospect" : "destination"] ",";</li>
<li class="old"> print "\t\t" prop["details"] ",";</li>
<li class="old"> print "\t\t" prop["contents"] ",";</li>
<li class="old"> print "\t\t" prop["textGo"] ",";</li>
<li class="old"> print "\t\t" prop["weight"] ",";</li>
<li class="old"> print "\t\t" prop["capacity"] ",";</li>
<li class="new"><span class="old"> print "\t\t" </span>prop["health"] ",";</li>
<li class="new"> print "\t\t" prop["open"] ",";</li>
<li class="new"> print "\t\t" prop["close"] ",";</li>
<li class="new"> print "\t\t" prop["lock"] ",";</li>
<li class="new"> print "\t\t" prop["unlock"];</li>
<li class="old"> print "\t}" separator;</li>
<li class="old"> delete prop;</li>
<li class="old"> }</li>
<li class="old"> count++;</li>
<li class="old"> }</li>
<li class="old">}</li>
</ol>
</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 />
a cave entrance to the east<br />
dense forest all around<br />
<br />
--> get coin<br />
You pick up a silver coin.<br />
<br />
--> give coin<br />
You give a silver coin to a burly guard.<br />
<br />
--> go cave<br />
You walk into the cave.<br />
<br />
You are in a little cave.<br />
You see:<br />
an exit to the west<br />
solid rock all around<br />
a closed door to the south<br />
a tiny key<br />
<br />
--> get key<br />
You pick up a tiny key.<br />
<br />
--> go south<br />
The door is closed.<br />
<br />
--> open door<br />
You open a closed door to the south.<br />
<br />
--> go south<br />
You walk through the door into a backroom.<br />
<br />
You are in a backroom.<br />
You see:<br />
solid rock all around<br />
an open door to the north<br />
a wooden box<br />
<br />
--> unlock box<br />
You unlock a wooden box.<br />
<br />
--> open box<br />
You open a wooden box.<br />
<br />
--> look box<br />
The box is open.<br />
You see:<br />
a gold coin<br />
<br />
--> get gold<br />
You get a gold coin from a wooden box.<br />
<br />
--> quit<br />
<br />
Bye!<br />
</td></tr>
</table>
<p>
The additions to <i>parsexec.c</i> are equally straightforward.
</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="old">#include "inventory.h"</li>
<li class="new">#include "openclose.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="old"> }</li>
<li class="old"> else if (strcmp(verb, "get") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeGet(noun);</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "drop") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeDrop(noun);</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "give") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeGive(noun);</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "ask") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeAsk(noun);</li>
<li class="old"> }</li>
<li class="old"> else if (strcmp(verb, "inventory") == 0)</li>
<li class="old"> {</li>
<li class="old"> executeInventory();</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "open") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeOpen(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "close") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeClose(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "lock") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeLock(noun);</li>
<li class="new"> }</li>
<li class="new"> else if (strcmp(verb, "unlock") == 0)</li>
<li class="new"> {</li>
<li class="new"> executeUnlock(noun);</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>
<p>
Notes:
<ul>
<li>You may have noticed that <i>object.txt</i> has almost doubled in size
in this chapter.
I can already promise you, this is only the beginning.
<i>object.txt</i> is our main source of game data;
once we seriously start adding locations, items and actors,
the number of lines could easily grow to many thousands.
</li>
<li>Function <i>swapLocation</i>
can be used for many other things than doors and locks.
In chapter 15, it will be used again, this time to turn the light on and off.
</li>
<li>As you can see in the sample output,
the player can get the gold from the box,
but he is unable to put it back in again!
Our parser cannot handle ‘complex’ commands like
<i>put coin in box</i>.
So in the next chapter, we will write a whole new parser;
the current two-line implementation badly needs replacement!
</li>
</ul>
</p>
<hr />
<table class="download"><tr><td>
<a class="button" href="code12/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="htpataic13.html">13. The parser</a>
</p>
</body>
</html>