-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.c
1919 lines (1715 loc) · 62.5 KB
/
world.c
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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
/* tab:8
*
* world.c - world content source file for the ECE391 MP2 F11 adventure game
*
* "Copyright (c) 2011 by Steven S. Lumetta."
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose, without fee, and without written agreement is
* hereby granted, provided that the above copyright notice and the following
* two paragraphs appear in all copies of this software.
*
* IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO
* ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
* DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
* EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
* THE UNIVERSITY OF ILLINOIS HAS ANY OBLIGATION TO PROVIDE MAINTENANCE,
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
*
* Author: Steve Lumetta
* Version: 1
* Creation Date: Wed Sep 14 02:19:41 2011
* Filename: world.c
* History:
* SL 1 Wed Sep 14 02:19:41 2011
* Completed initial implementation.
* SL 2 Thu Sep 15 00:31:40 2011
* Cleaned up code for distribution.
*/
#include <string.h>
#include <strings.h>
#include "assert.h"
#include "photo.h"
#include "world.h"
/* parameters defined for this file */
/* room identifiers */
enum {
R_NONE = -1,
/* Area 0: The Backpack */
R_INVENTORY,
/* Area 1: Everitt and Green Street */
R_IN_391LAB, /* inside the 391 lab */
R_BY_391LAB, /* outside of the 391 lab */
R_IN_IEEE, /* inside the IEEE/HKN office */
R_BY_IEEE, /* outside of the IEEE/HKN office */
R_IN_395LAB, /* inside the 395 lab */
R_BY_395LAB, /* outside of the 395 lab */
R_EVT_STAIR, /* Everitt Lab's eastern stairwell */
R_IN_CLEANR, /* inside the cleanroom */
R_BY_CLEANR, /* outside of the cleanroom */
R_EVRT_VEND, /* near the Everitt vending machine */
R_ALMAMATER, /* near the Alma Mater statue */
R_IN_COCOMR, /* inside of Cocomero */
R_BY_COCOMR, /* just outside of Cocomero */
R_BY_ZAS, /* across from the ruins of Za's */
R_EAST_EVRT, /* East entrance of Everitt Lab */
R_EVRT_BSMT, /* entrance to Everitt Lab basement */
/* Area 2: Bardeen Quad and Environs */
R_WEST_BONE, /* looking West along the Boneyard */
R_CIRCLE_N, /* Boneyard Bridge looking North */
R_CIRCLE_SW, /* Boneyard Bridge looking Southwest */
R_EAST_BONE, /* looking East along the Boneyard */
R_BARDEEN, /* Bardeen Quad */
R_LIB_BACK, /* rear of Grainger library */
R_RESERVE, /* Grainger reserve desk */
R_TALBOT_NW, /* looking Northwest at Talbot */
R_TALBOT_SW, /* looking Southwest at Talbot */
R_TALBOT, /* inside Talbot Laboratory */
R_SPRINGFLD, /* looking West along Springfield */
R_CARIBOU, /* the Caribou coffee shop */
R_KENNEY, /* Kenney Gym */
R_DCL, /* Digital Computer Laboratory */
R_LIB_FRONT, /* front of Grainger library */
/* Area 3: CSL and Environs */
R_KENNEY_E, /* East of Kenney Gym */
R_NEWMARK, /* Newmark Laboratory */
R_MNTL_NW, /* looking Northwest at MNTL */
R_MNTL_SW, /* looking Southwest at MNTL */
R_MNTLLOBBY, /* the lobby of MNTL */
R_MNTL_LAB1, /* a laboratory within MNTL (#1) */
R_MNTL_LAB2, /* a laboratory within MNTL (#2) */
R_MNTL_LAB3, /* a laboratory within MNTL (#3) */
R_CSL_VIEW, /* Coordinated Science Laboratory */
R_CSL_DOOR, /* the CSL main entrance */
R_CSL_LOBBY, /* in the CSL lobby */
R_CSL_UPPER, /* on an upper floor of CSL */
R_CSLLOUNGE, /* in the new CSL lounge area */
R_BECK_LOT, /* the Beckman Circle parking lot */
R_BECKMAN, /* the Beckman Institute */
R_BECK_DOOR, /* Beckman main entrance */
R_BECKLOBBY, /* in the lobby of Beckman */
R_BECK_MRI, /* an MRI machine ... somewhere */
/* Area 4: The Rest of the World, Featuring the Remote Sensing Lab */
R_GARAGE, /* the campus parking structure */
R_CAR_SITE, /* the (fictitious) ECE391 car */
R_ALLERTON, /* the Allerton mansion */
R_FU_DOGS, /* the Fu dogs at Allerton */
R_STATUE, /* a statue near the Fu dogs */
R_SUNSINGER, /* the Allerton Sun Singer statue */
R_WILLARD, /* Willard Airport fountain view */
R_WILL_SIDE, /* side view of Willard and tower */
R_REM_PLANE, /* a sensor-laden plane */
R_COCKPIT, /* cockpit of remote sensing plane */
R_OVER_WILL, /* flying above Willard Airport */
R_AIR_RIO, /* view of Rio de Janeiro from air */
R_REM_ICE, /* the ice fields near rem. sen. lab */
R_REM_LAB, /* part of a remote sensing lab */
N_ROOMS
};
/* object identifiers */
enum {
O_NONE = -1,
O_BOARD, /* a motorized mountain board */
O_JETPACK, /* Buzz Lightyear: to Infinity ... */
O_TUX, /* Tux: our mascot */
O_MP2, /* the MP2 specification (covers mode X) */
O_BOOK_C, /* the C programming language */
O_BOOK_WODE, /* stories by P.G. Wodehouse */
O_GPS_BAD, /* a malfunctioning GPS device */
O_GPS_GOOD, /* a working GPS device */
O_GPS_SPEC, /* GPS chip data sheet (specifications) */
O_BUNNYSUIT, /* a pink bunny suit */
O_BATT_EMPTY, /* an uncharged car battery */
O_BATT_FULL, /* a fully charged car battery */
O_BATT_CAR, /* battery as it appears in the car */
O_MTN_DEW, /* a bottle of dew */
O_FISH, /* a fish to lure penguins */
O_ICARD, /* an I-card */
O_CAR_KEY, /* the keys to a car */
O_ROBOT_DEAD, /* a buggy lockpicking robot */
O_ROBOT_LIVE, /* lockpicking robot with new control program */
O_MIMO_CARD, /* a MIMO card for planes */
N_OBJECTS
};
/* flag identifiers for recording the player's accomplishments */
enum {
FLAG_HAS_EATEN, /* player has eaten something */
FLAG_WEARING_SUIT, /* player is wearing a bunny suit */
FLAG_CAR_OPEN, /* player has managed to open the car */
FLAG_CAR_FIXED, /* player has fixed the car */
FLAG_LURED_TUX, /* player has lured Tux to their side */
NUM_FLAGS
};
/* identifiers for rooms with photo swapping */
enum {
SWAP_CIRCLE, /* Boneyard Creek Bridge photo swap */
SWAP_CAR, /* open/closed hood */
N_SWAPS
};
/* types local to this file (declared in types.h) */
/*
* The structure representing a room in the world. The backpack/inventory
* is also a 'room' (#0, R_INVENTORY).
*/
struct room_t {
const char* name; /* name of room */
photo_t* view; /* photo currently shown for room */
object_t* contents; /* linked list of objects in room */
room_t* left; /* room to the "left" */
room_t* enter; /* doors, etc. */
room_t* right; /* room to the "right" */
};
/*
* The structure representing an object in the world. Objects are
* unique, which prevents players from drinking too much Dew (they're
* all the same bottle!). Sorry.
*/
struct object_t {
const char* name; /* name of object */
object_t* next; /* linked list of room contents */
room_t* loc; /* in what 'room'? */
uint16_t x, y; /* location within room photo */
image_t* img; /* image for use in room */
};
/*
* This local structure is used to specify room connectivity and data
* in a reasonably manageable way. The array entries in the database
* specify the room id, name, filename, and connections to other
* rooms by symbolic name. Since there are many rooms, trying to
* keep the order between the enumeration of symbolic names and the
* array entries can be tricky. Instead, we associate array entries
* with specific symbolic names using the id field. Thus, you can
* reorder the entries in the room_data array without affecting their
* meaning.
*/
typedef struct room_data_t room_data_t;
struct room_data_t {
int32_t id; /* id of room being described */
const char* const name; /* name of room */
const char* const filename; /* file name for room photo */
int32_t left; /* id of room to 'left' */
int32_t enter; /* id of room reached by 'enter' */
int32_t right; /* id of room to 'right' */
};
/* the room connectivity data */
static const room_data_t room_data[N_ROOMS] = {
/* Area 0: The Backpack */
{R_INVENTORY, "Inventory", "images/backpack.photo",
R_NONE, R_NONE, R_NONE},
/* Area 1: Everitt and Green Street */
{R_IN_391LAB, "391 Lab", "images/391lab.photo",
R_NONE, R_BY_391LAB, R_NONE},
{R_BY_391LAB, "Outside of 391", "images/outside391.photo",
R_BY_ZAS, R_IN_391LAB, R_BY_IEEE},
{ R_IN_IEEE, "IEEE Office", "images/ieee.photo",
R_NONE, R_BY_IEEE, R_NONE},
{ R_BY_IEEE, "Outside IEEE", "images/byieee.photo",
R_BY_391LAB, R_IN_IEEE, R_BY_395LAB},
{R_IN_395LAB, "395 Lab", "images/395lab.photo",
R_NONE, R_BY_395LAB, R_NONE},
{R_BY_395LAB, "Outside of 395", "images/outside395.photo",
R_BY_IEEE, R_NONE, R_EVT_STAIR},
{R_EVT_STAIR, "Everitt Stairs", "images/evtstair.photo",
R_BY_395LAB, R_EAST_EVRT, R_BY_CLEANR},
{R_IN_CLEANR, "In Cleanroom", "images/cleanr.photo",
R_NONE, R_BY_CLEANR, R_NONE},
{R_BY_CLEANR, "By the Cleanroom", "images/outclean.photo",
R_EVT_STAIR, R_NONE, R_EVRT_VEND},
{R_EVRT_VEND, "Vending Machine", "images/vend.photo",
R_BY_CLEANR, R_EVRT_BSMT, R_NONE},
{R_ALMAMATER, "Alma Mater", "images/almamater.photo",
R_EAST_EVRT, R_EAST_EVRT, R_BY_COCOMR},
{R_IN_COCOMR, "Cocomero", "images/incoco.photo",
R_NONE, R_BY_COCOMR, R_NONE},
{R_BY_COCOMR, "Near Cocomero", "images/bycoco.photo",
R_ALMAMATER, R_IN_COCOMR, R_BY_ZAS},
{R_BY_ZAS, "The Ruins", "images/ruins.photo",
R_BY_COCOMR, R_NONE, R_NONE},
{R_EAST_EVRT, "East of Everitt", "images/eeast.photo",
R_ALMAMATER, R_EVT_STAIR, R_EVRT_BSMT},
{R_EVRT_BSMT, "Basement Entry", "images/basement.photo",
R_EAST_EVRT, R_EVRT_VEND, R_CIRCLE_SW},
/* Area 2: Bardeen Quad and Environs */
{R_WEST_BONE, "Boneyard Creek", "images/bonew.photo",
R_CIRCLE_SW, R_NONE, R_CIRCLE_N},
{ R_CIRCLE_N, "Boneyard Bridge", "images/circlen1.photo",
R_WEST_BONE, R_TALBOT_NW, R_EAST_BONE},
{R_CIRCLE_SW, "Boneyard Bridge", "images/circlesw.photo",
R_EAST_BONE, R_EVRT_BSMT, R_CIRCLE_N},
{R_EAST_BONE, "Boneyard Creek", "images/bonee.photo",
R_CIRCLE_N, R_NONE, R_CIRCLE_SW},
{ R_BARDEEN, "Bardeen Quad", "images/bardeen.photo",
R_LIB_BACK, R_EAST_BONE, R_TALBOT_SW},
{ R_LIB_BACK, "Grainger Library", "images/graingerback.photo",
R_DCL, R_RESERVE, R_BARDEEN},
{ R_RESERVE, "Grainger Reserves", "images/reserve.photo",
R_NONE, R_LIB_BACK, R_LIB_FRONT},
{R_TALBOT_NW, "Talbot Lab", "images/talbotnw.photo",
R_CIRCLE_SW, R_TALBOT, R_TALBOT_SW},
{R_TALBOT_SW, "Talbot Lab", "images/talbotsw.photo",
R_TALBOT_NW, R_TALBOT, R_SPRINGFLD},
{ R_TALBOT, "Talbot Lab", "images/talbot.photo",
R_NONE, R_TALBOT_NW, R_NONE},
{R_SPRINGFLD, "Springfield Avenue", "images/springfield.photo",
R_TALBOT_SW, R_CARIBOU, R_KENNEY},
{ R_CARIBOU, "Caribou", "images/caribou.photo",
R_NONE, R_SPRINGFLD, R_NONE},
{ R_KENNEY, "Kenney Gym", "images/kenney.photo",
R_SPRINGFLD, R_NONE, R_DCL},
{ R_DCL, "DCL", "images/dcl.photo",
R_KENNEY, R_KENNEY_E, R_LIB_FRONT},
{R_LIB_FRONT, "Grainger Library", "images/graingerfront.photo",
R_DCL, R_RESERVE, R_TALBOT_SW},
/* Area 3: CSL and Environs */
{ R_KENNEY_E, "East of Kenney", "images/kenneye.photo",
R_DCL, R_DCL, R_NEWMARK},
{ R_NEWMARK, "Newmark Lab", "images/newmark.photo",
R_MNTL_NW, R_NONE, R_KENNEY_E},
{ R_MNTL_NW, "MNTL", "images/mntlnw.photo",
R_NEWMARK, R_MNTLLOBBY, R_CSL_VIEW},
{ R_MNTL_SW, "MNTL", "images/mntlsw.photo",
R_MNTL_NW, R_MNTLLOBBY, R_BECKMAN},
{R_MNTLLOBBY, "Lobby of MNTL", "images/mntllobby.photo",
R_MNTL_LAB1, R_MNTL_SW, R_MNTL_LAB2},
{R_MNTL_LAB1, "Kevin's Lab in MNTL", "images/mntllab1.photo",
R_NONE, R_NONE, R_MNTLLOBBY},
{R_MNTL_LAB2, "MNTL Laser Lab", "images/mntllab2.photo",
R_MNTLLOBBY, R_MNTL_LAB3, R_NONE},
{R_MNTL_LAB3, "MNTL Laser Lab", "images/mntllab3.photo",
R_NONE, R_MNTL_LAB2, R_NONE},
{ R_CSL_VIEW, "CSL", "images/csl.photo",
R_BECK_LOT, R_CSL_DOOR, R_MNTL_NW},
{ R_CSL_DOOR, "CSL Main Entrance", "images/csldoor.photo",
R_BECK_LOT, R_NONE, R_MNTL_NW},
{R_CSL_LOBBY, "CSL Lobby", "images/csllobby.photo",
R_CSL_UPPER, R_CSL_DOOR, R_NONE},
{R_CSL_UPPER, "Upper Floor of CSL", "images/cslupper.photo",
R_NONE, R_CSLLOUNGE, R_CSL_LOBBY},
{R_CSLLOUNGE, "CSL Lounge", "images/csllounge.photo",
R_NONE, R_CSL_UPPER, R_NONE},
{ R_BECK_LOT, "Beckman Circle Lot", "images/becklot.photo",
R_BECKMAN, R_GARAGE, R_CSL_VIEW},
{ R_BECKMAN, "Beckman Institute", "images/beckman.photo",
R_MNTL_SW, R_BECK_DOOR, R_BECK_LOT},
{R_BECK_DOOR, "Beckman Institute", "images/beckdoor.photo",
R_MNTL_SW, R_NONE, R_BECK_LOT},
{R_BECKLOBBY, "Beckman Lobby", "images/becklobby.photo",
R_NONE, R_BECK_MRI, R_BECK_DOOR},
{ R_BECK_MRI, "An MRI Lab", "images/beckmri.photo",
R_NONE, R_BECKLOBBY, R_NONE},
/* Area 4: The Rest of the World, Featuring the Remote Sensing Lab */
{ R_GARAGE, "Campus Parking", "images/garage.photo",
R_BECK_LOT, R_CAR_SITE, R_NONE},
{ R_CAR_SITE, "Use Someone's Car?", "images/carclosed.photo",
R_NONE, R_GARAGE, R_NONE},
{ R_ALLERTON, "Allerton Mansion", "images/allerton.photo",
R_FU_DOGS, R_NONE, R_SUNSINGER},
{ R_FU_DOGS, "Fu Dog Statues", "images/fudogs.photo",
R_NONE, R_STATUE, R_ALLERTON},
{ R_STATUE, "A Tall Statue", "images/statue.photo",
R_NONE, R_FU_DOGS, R_NONE},
{R_SUNSINGER, "The Sun Singer", "images/sunsinger.photo",
R_ALLERTON, R_NONE, R_NONE},
{ R_WILLARD, "Willard Airport", "images/willard.photo",
R_NONE, R_WILL_SIDE, R_NONE},
{R_WILL_SIDE, "Willard Tower", "images/willardside.photo",
R_REM_PLANE, R_NONE, R_WILLARD},
{R_REM_PLANE, "Sensor-Laden Plane", "images/rsenseplane.photo",
R_COCKPIT, R_NONE, R_WILL_SIDE},
{ R_COCKPIT, "Plane Cockpit", "images/cockpit.photo",
R_NONE, R_NONE, R_REM_PLANE},
{R_OVER_WILL, "Flying over Willard", "images/overwillard.photo",
R_NONE, R_COCKPIT, R_AIR_RIO},
{ R_AIR_RIO, "Rio de Janeiro", "images/riofromair.photo",
R_OVER_WILL, R_NONE, R_REM_ICE},
{ R_REM_ICE, "Ice Fields", "images/rsenseice.photo",
R_AIR_RIO, R_REM_LAB, R_NONE},
{ R_REM_LAB, "Remote Sensing Lab", "images/rsenselab.photo",
R_NONE, R_REM_ICE, R_NONE}
};
/*
* This local structure is used to specify object information in a
* reasonably manageable way. The array entries in the database specify
* the object id, name, filename, starting room (if any), and starting
* location * within that room (if not random). We use this approach for
* the same * reason as with the rooms: to avoid the need to match the
* order between the enumeration of symbolic names and the array entries.
*/
typedef struct obj_data_t obj_data_t;
struct obj_data_t {
int32_t id; /* object identifier */
const char* const name; /* object keyword */
const char* const filename; /* object image file name */
int32_t room; /* starting room or R_NONE */
int32_t x; /* starting x position (-1 for random) */
int32_t y; /* starting y position */
};
/* the object positioning data */
static const obj_data_t obj_data[N_OBJECTS] = {
{ O_BOARD, "board", "images/board.obj", R_IN_IEEE, -1, -1},
{ O_JETPACK, "jetpack", "images/jetpack.obj", R_TALBOT, -1, -1},
{ O_TUX, "tux", "images/tux.obj", R_REM_LAB, 250, 100},
{ O_MP2, "mp2", "images/mp2.obj", R_CSLLOUNGE, -1, -1},
{ O_BOOK_C, "book", "images/book.obj", R_NONE, -1, -1},
{ O_BOOK_WODE, "book", "images/book2.obj", R_NONE, -1, -1},
{ O_GPS_BAD, "gps", "images/gpsbad.obj", R_TALBOT, -1, -1},
{ O_GPS_GOOD, "gps", "images/gpsgood.obj", R_NONE, -1, -1},
{ O_GPS_SPEC, "spec", "images/gpsspec.obj", R_CSL_UPPER, -1, -1},
{ O_BUNNYSUIT, "bunnysuit", "images/bunnysuit.obj", R_ALMAMATER, 230, 250},
{O_BATT_EMPTY, "battery", "images/battery.obj", R_NONE, -1, -1},
{ O_BATT_FULL, "battery", "images/battery.obj", R_NONE, -1, -1},
{ O_BATT_CAR, "battery", "images/batteryincar.obj", R_NONE, -1, -1},
{ O_MTN_DEW, "dew", "images/dew.obj", R_NONE, -1, -1},
{ O_FISH, "fish", "images/fish.obj", R_EAST_BONE, 80, 260},
{ O_ICARD, "Icard", "images/icard.obj", R_BARDEEN, -1, -1},
{ O_CAR_KEY, "key", "images/key.obj", R_CARIBOU, -1, -1},
{O_ROBOT_DEAD, "robot", "images/robot.obj", R_MNTL_LAB3, -1, -1},
{O_ROBOT_LIVE, "robot", "images/robot.obj", R_NONE, -1, -1},
{ O_MIMO_CARD, "mimo", "images/mimo.obj", R_STATUE, -1, -1}
};
/*
* Some rooms alternate between two photos. For these rooms, we load the
* image data for both photos once, but need an extra pointer in order to
* keep track of the photo currently swapped out. We use these swap data
* to name and describe these extra photos.
*/
typedef struct swap_data_t swap_data_t;
struct swap_data_t {
int32_t id;
const char* const filename;
};
/* the swap photo descriptions */
static const swap_data_t swap_data[N_SWAPS] = {
{SWAP_CIRCLE, "images/circlen2.photo"}, /* alternate for Boneyard */
{SWAP_CAR, "images/caropen.photo"} /* open/closed car photos */
};
/* functions local to this file--see function headers for details */
static void do_photo_swap (room_t* r, int32_t which);
static object_t* find_in_room (const room_t* r, const char* arg);
static void insert_object_at (object_t* o, room_t* r, int32_t x, int32_t y);
static void insert_object (object_t* o, room_t* r);
static void move_object_to_inventory (object_t* obj);
static object_t* obj_special_get (room_t* r, const char* arg);
static int32_t player_flag_is_set (int32_t fnum);
static void player_set_flag (int32_t fnum);
static void remove_object (object_t* o);
/* file-scope variables */
/*
* Flags are coded as bit vectors using an array of 32-bit words. It's
* overkill for this game, but it's nice not to worry about the number of
* flags...
*/
static room_t room[N_ROOMS]; /* rooms */
static object_t object[N_OBJECTS]; /* objects */
static uint32_t player_flags[(NUM_FLAGS + 31) / 32]; /* accomplishment flags */
static photo_t* swap_photo[N_SWAPS]; /* swapping photos */
/*
* do_photo_swap
* DESCRIPTION: Swap a room photo with another stored image.
* INPUTS: r -- the room into which the photo is swapped
* which -- index into array of stored photos
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: none
*/
static void
do_photo_swap (room_t* r, int32_t which)
{
photo_t* tmp; /* temporary variable to help with swap */
/* Swap the photos. */
tmp = r->view;
r->view = swap_photo[which];
swap_photo[which] = tmp;
}
/*
* find_in_room
* DESCRIPTION: Find an object by name in a room. The name must match
* exactly, although the match is not sensitive to case.
* INPUTS: r -- the room in which to look
* arg -- the name of the object (a string)
* OUTPUTS: none
* RETURN VALUE: a pointer to a matching object, or NULL if none is found
* SIDE EFFECTS: none
*/
static object_t*
find_in_room (const room_t* r, const char* arg)
{
object_t* obj; /* index over room contents */
/* Loop over objects in room. */
for (obj = r->contents; NULL != obj; obj = obj->next) {
/* If we find a matching object, return it. */
if (0 == strcasecmp (arg, obj->name)) {
return obj;
}
}
/* No object found: return NULL. */
return NULL;
}
/*
* insert_object_at
* DESCRIPTION: Place an object at a specific (x,y) location in a room.
* The location refers to the placement of the object in
* the room's photo.
* INPUTS: o -- the object being placed
* r -- the room
* x -- the x position for the object
* y -- the y position for the object
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: takes the object out of its current location
*/
static void
insert_object_at (object_t* o, room_t* r, int32_t x, int32_t y)
{
/* Remove object from its current room, if any. */
remove_object (o);
/* Position the object within the new room. */
o->x = x;
o->y = y;
/* Now add the object to the new room's contents. */
o->loc = r;
o->next = r->contents;
r->contents = o;
}
/*
* insert_object
* DESCRIPTION: Insert object at a random position within a room.
* INPUTS: o -- the object being placed
* r -- the room
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: takes the object out of its current location; sets
* the position of the object within the new room
* randomly
*/
static void
insert_object (object_t* o, room_t* r)
{
int32_t space; /* room photo height in pixels */
int32_t range; /* number of pixels in placement interval */
int32_t xpos; /* x position selected */
int32_t ypos; /* y position selected */
int32_t img_ht; /* object image height in pixels */
/* Choose a random x location. */
range = photo_width (r->view) - image_width (o->img);
xpos = (0 >= range ? 0 : (rand () % range));
/* Place in the lowest quarter of the roo photo if the object fits... */
space = photo_height (r->view);
img_ht = image_height (o->img);
range = space / 4 - img_ht;
if (0 >= range) {
/* Doesn't fit: try not to let the object fall off the bottom. */
range = space - img_ht;
ypos = (0 >= range ? 0 : (rand () % range));
} else {
ypos = (0 >= range ? 0 : (rand () % range) + (3 * space) / 4);
}
/* Now put the object into the room at the chosen location. */
insert_object_at (o, r, xpos, ypos);
}
/*
* move_object_to_inventory
* DESCRIPTION: Move an object into the player's inventory. Try to
* place objects on a 3x3 grid for clarity, but place
* randomly if necessary.
* INPUTS: obj -- the object
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: takes the object out of its current location
*/
static void
move_object_to_inventory (object_t* obj)
{
object_t* conf; /* loop index over possible conflicts for a space */
int32_t x; /* loop index for 3x3 grid x positions */
int32_t y; /* loop index for 3x3 grid y positions */
/*
* This approach is asymptotically slow (N^2), but there shouldn't be
* much in inventory, so it doesn't matter.
*/
for (y = 10; 160 >= y; y += 50) {
for (x = 10; 210 >= x; x += 100) {
for (conf = room[R_INVENTORY].contents; NULL != conf;
conf = conf->next) {
if (x == conf->x && y == conf->y) {
break;
}
}
if (NULL == conf) {
insert_object_at (obj, &room[R_INVENTORY], x, y);
return;
}
}
}
/* Give up: place randomly in bottom quarter like a room. */
insert_object (obj, &room[R_INVENTORY]);
}
/*
* obj_special_get
* DESCRIPTION: Handle special effects "get" commands, in which a player
* gets an object that is not represented as an object_t in
* the room's contents.
* INPUTS: r -- the room in which the "get" is performed
* arg -- the name of the object sought
* OUTPUTS: none
* RETURN VALUE: an object to be gotten by the player, or NULL for nothing
* SIDE EFFECTS: may move objects or show status messages
*/
static object_t*
obj_special_get (room_t* r, const char* arg)
{
/* Get a book from the Grainger reference desk... */
if (&room[R_RESERVE] == r && 0 == strcasecmp ("book", arg)) {
/* can only get it once... */
if (player_flag_is_set (FLAG_HAS_EATEN)) {
if (NULL == object[O_BOOK_C].loc) {
show_status ("You check out the C book.");
return &object[O_BOOK_C];
}
} else {
if (NULL == object[O_BOOK_WODE].loc) {
show_status ("Here's a nice Wodehouse collection.");
return &object[O_BOOK_WODE];
}
}
}
/* Pick up the car battery... */
if (&room[R_CAR_SITE] == r && object[O_BATT_CAR].loc == r) {
remove_object (&object[O_BATT_CAR]);
return &object[O_BATT_EMPTY];
}
/* That's all, folks! */
return NULL;
}
/*
* player_flag_is_set
* DESCRIPTION: Checks whether the player has accomplished a specified task.
* INPUTS: fnum -- the accomplishment identifier (a FLAG_*)
* OUTPUTS: none
* RETURN VALUE: 1 if the player has accomplished the task, 0 if not
* SIDE EFFECTS: none
*/
static int32_t
player_flag_is_set (int32_t fnum)
{
return (0 != (player_flags[fnum / 32] & (1UL << (fnum % 32))));
}
/*
* player_set_flag
* DESCRIPTION: Sets the flag indicating that the player has accomplished
* a specified task.
* INPUTS: fnum -- the accomplishment identifier (a FLAG_*)
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: none
*/
static void
player_set_flag (int32_t fnum)
{
player_flags[fnum / 32] |= (1UL << (fnum % 32));
}
/*
* remove_object
* DESCRIPTION: Take an object out of its current location, leaving it
* in limbo (NULL location).
* INPUTS: o -- the object
* OUTPUTS: none
* RETURN VALUE: none
* SIDE EFFECTS: none
*/
static void
remove_object (object_t* o)
{
object_t** find; /* loop index over pointers to objects in room */
/* Is object already in limbo? */
if (NULL != o->loc) {
/* Remove from previous room (with safety check)... */
for (find = &o->loc->contents; NULL != *find; find = &(*find)->next) {
if (o == *find) {
/* We found the predecessor! Unlink the object. */
*find = o->next;
break;
}
}
/* Mark the object's location as NULL. */
o->loc = NULL;
}
}
/*
* obj_get_x
* DESCRIPTION: Get x position of object within containing room.
* INPUTS: obj -- pointer to the object
* OUTPUTS: none
* RETURN VALUE: the object obj's x position
* SIDE EFFECTS: none
*/
uint16_t
obj_get_x (const object_t* obj)
{
return obj->x;
}
/*
* obj_get_y
* DESCRIPTION: Get y position of object within containing room.
* INPUTS: obj -- pointer to the object
* OUTPUTS: none
* RETURN VALUE: the object obj's y position
* SIDE EFFECTS: none
*/
uint16_t
obj_get_y (const object_t* obj)
{
return obj->y;
}
/*
* obj_image
* DESCRIPTION: Get image pointer for object.
* INPUTS: obj -- pointer to the object
* OUTPUTS: none
* RETURN VALUE: the object obj's image pointer
* SIDE EFFECTS: none
*/
image_t*
obj_image (const object_t* obj)
{
return obj->img;
}
/*
* obj_next
* DESCRIPTION: Get pointer to next object in object's room. Use with
* room_contents_iterate to iterate over all objects in
* a room.
* INPUTS: obj -- pointer to the object
* OUTPUTS: none
* RETURN VALUE: the object obj's next pointer (NULL if obj is last)
* SIDE EFFECTS: none
*/
object_t*
obj_next (const object_t* obj)
{
return obj->next;
}
/*
* room_contents_iterate
* DESCRIPTION: Get pointer to the first object in a room. Use with
* obj_next to iterate over all objects in a room.
* INPUTS: r -- pointer to the room
* OUTPUTS: none
* RETURN VALUE: a pointer to room r's first object (NULL when empty)
* SIDE EFFECTS: none
*/
object_t*
room_contents_iterate (const room_t* r)
{
return r->contents;
}
/*
* room_name
* DESCRIPTION: Get name for a room.
* INPUTS: r -- pointer to the room
* OUTPUTS: none
* RETURN VALUE: the name of room r (a string)
* SIDE EFFECTS: none
*/
const char*
room_name (const room_t* r)
{
return r->name;
}
/*
* room_photo
* DESCRIPTION: Get room photo for a room.
* INPUTS: r -- pointer to the room
* OUTPUTS: none
* RETURN VALUE: a pointer to room r's photo
* SIDE EFFECTS: none
*/
photo_t*
room_photo (const room_t* r)
{
return r->view;
}
/*
* room_photo_height
* DESCRIPTION: Get height of room photo in pixels for a room.
* INPUTS: r -- pointer to the room
* OUTPUTS: none
* RETURN VALUE: height of room r's photo in pixels
* SIDE EFFECTS: none
*/
uint32_t
room_photo_height (const room_t* r)
{
return photo_height (r->view);
}
/*
* room_photo_width
* DESCRIPTION: Get width of room photo in pixels for a room.
* INPUTS: r -- pointer to the room
* OUTPUTS: none
* RETURN VALUE: width of room r's photo in pixels
* SIDE EFFECTS: none
*/
uint32_t
room_photo_width (const room_t* r)
{
return photo_width (r->view);
}
/*
* build_world
* DESCRIPTION: Builds and connects the rooms, creates objects, and
* reads in all image data (could be done lazily with
* caching instead).
* INPUTS: none
* OUTPUTS: none
* RETURN VALUE: 1 on success, or 0 on failure
* SIDE EFFECTS: prints error messages to stderr on failure
*/
int32_t
build_world ()
{
int32_t idx; /* index over data arrays */
int32_t which; /* id for current data item */
/* Clear all accomplishment flags. */
(void)memset (player_flags, 0, sizeof (player_flags));
/* Clear room data to enable sanity check for duplication. */
(void)memset (room, 0, sizeof (room));
/* Loop over room data. */
for (idx = 0; N_ROOMS > idx; idx++) {
/* Set the room id. */
which = room_data[idx].id;
/* Check for bad and duplicate ids. */
if (0 > which || N_ROOMS <= which) {
fputs ("Bad index in room data.\n", stderr);
return 0;
}
if (NULL != room[which].name) {
fprintf (stderr, "Duplicate index %d in room data.\n", which);
return 0;
}
/* Set up the room. */
room[which].name = room_data[idx].name;
room[which].view = read_photo (room_data[idx].filename);
if (NULL == room[which].view) {
fprintf (stderr, "Can't read room photo %s.\n",
room_data[idx].filename);
return 0;
}
room[which].contents = NULL;
room[which].left = (R_NONE == room_data[idx].left ? NULL :
&room[room_data[idx].left]);
room[which].enter = (R_NONE == room_data[idx].enter ? NULL :
&room[room_data[idx].enter]);
room[which].right = (R_NONE == room_data[idx].right ? NULL :
&room[room_data[idx].right]);
}
/* Clear object data to enable sanity check for duplication. */
(void)memset (object, 0, sizeof (object));
/* Loop over object data. */
for (idx = 0; N_OBJECTS > idx; idx++) {
/* Set the object id. */
which = obj_data[idx].id;
/* Check for bad and duplicate ids. */
if (0 > which || N_OBJECTS <= which) {
fputs ("Bad index in object data.\n", stderr);
return 0;
}
if (NULL != object[which].name) {
fprintf (stderr, "Duplicate index %d in object data.\n", which);
return 0;
}
/* Set up the object. */
object[which].name = obj_data[idx].name;
object[which].img = read_obj_image (obj_data[idx].filename);
if (NULL == object[which].img) {
fprintf (stderr, "Can't read object photo %s.\n",
obj_data[idx].filename);
return 0;
}
object[which].next = NULL;
object[which].loc = NULL;
object[which].x = 0;
object[which].y = 0;
/* Insert it into a room if necessary. */
if (R_NONE != obj_data[idx].room) {
if (-1 != obj_data[idx].x) {
insert_object_at (&object[which], &room[obj_data[idx].room],
obj_data[idx].x, obj_data[idx].y);
} else {
insert_object (&object[which], &room[obj_data[idx].room]);
}
}
}
/* Clear swap photo data to enable sanity check for duplication. */
(void)memset (swap_photo, 0, sizeof (swap_photo));
/* Loop over swap photo data. */
for (idx = 0; N_SWAPS > idx; idx++) {
/* Set the swap photo id. */
which = swap_data[idx].id;
/* Check for bad and duplicate ids. */
if (0 > which || N_SWAPS <= which) {
fputs ("Bad index in swap data.\n", stderr);
return 0;
}
if (NULL != swap_photo[which]) {
fprintf (stderr, "Duplicate index %d in swap data.\n", which);
return 0;
}
/* Read in the swap photo. */
swap_photo[which] = read_photo (swap_data[idx].filename);
if (NULL == swap_photo[which]) {
fprintf (stderr, "Can't read room photo %s.\n",
swap_data[idx].filename);
return 0;
}
}
/* Everything worked! */
return 1;
}
/*
* start_in_room
* DESCRIPTION: Get a pointer to the room in which the player begins
* the game.
* INPUTS: none
* OUTPUTS: none
* RETURN VALUE: pointer to the starting room
* SIDE EFFECTS: none
*/
room_t*
start_in_room ()
{