forked from jbanes/haunted
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdl-haunted.c
1835 lines (1607 loc) · 53.6 KB
/
sdl-haunted.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
/*
HAUNTED HOUSE ADVENTURE
***********************
By Jenny Tyler and Les Howarth
Ported from Microsoft BASIC to ANSI C89 by John Elliott
Enhanced SDL edition for RetroFW by Jerason Banes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "SDL.h"
#include "renderer.h"
/* Counts of fixed-size arrays */
#define ROOM_COUNT 64
#define VERB_COUNT 27
#define WORD_COUNT 36
#define OBJECT_COUNT 18
/* The original BASIC has no option but to hardcode room and
* object numbers. In C we can at least use the preprocessor
* to make things a little more readable */
#define OBJ_NULL 0 /* Flag 0 => candle lit */
#define OBJ_PAINTING 1
#define OBJ_RING 2
#define OBJ_SPELLS 3
#define OBJ_GOBLET 4
#define OBJ_SCROLL 5
#define OBJ_COINS 6
#define OBJ_STATUE 7
#define OBJ_CANDLESTICK 8
#define OBJ_MATCHES 9
#define OBJ_VACUUM 10
#define OBJ_BATTERIES 11
#define OBJ_SHOVEL 12
#define OBJ_AXE 13
#define OBJ_ROPE 14
#define OBJ_BOAT 15
#define OBJ_AEROSOL 16
#define OBJ_CANDLE 17
#define OBJ_KEY 18
#define OBJ_NORTH 19
#define OBJ_SOUTH 20
#define OBJ_WEST 21
#define OBJ_EAST 22
#define OBJ_UP 23 /* Flag 23 => Front door open */
#define OBJ_DOWN 24 /* Flag 24 => Vacuum cleaner running */
#define OBJ_DOOR 25 /* Flag 25 => Wooden door open */
#define OBJ_BATS 26 /* Flag 26 => Bats active */
#define OBJ_GHOSTS 27 /* Flag 27 => Ghosts active */
#define OBJ_DRAWER 28
#define OBJ_DESK 29
#define OBJ_COAT 30
#define OBJ_RUBBISH 31
#define OBJ_COFFIN 32
#define OBJ_BOOKS 33
#define OBJ_XZANFAR 34 /* Flag 34 => magical barrier has been removed */
#define OBJ_WALL 35 /* Flag 35 => move was successful */
#define OBJ_SPELLS2 36
#define VERB_NULL 0
#define VERB_HELP 1
#define VERB_INVENTORY 2
#define VERB_GO 3
#define VERB_NORTH 4
#define VERB_SOUTH 5
#define VERB_WEST 6
#define VERB_EAST 7
#define VERB_UP 8
#define VERB_DOWN 9
#define VERB_GET 10
#define VERB_TAKE 11
#define VERB_OPEN 12
#define VERB_EXAMINE 13
#define VERB_READ 14
#define VERB_SAY 15
#define VERB_DIG 16
#define VERB_SWING 17
#define VERB_CLIMB 18
#define VERB_LIGHT 19
#define VERB_UNLIGHT 20
#define VERB_SPRAY 21
#define VERB_USE 22
#define VERB_UNLOCK 23
#define VERB_LEAVE 24
#define VERB_SCORE 25
#define VERB_SAVE 26
#define VERB_LOAD 27
#define ROOM_DCORNER 0
#define ROOM_OGARDEN 1
#define ROOM_WOODPILE 2
#define ROOM_YARD 3
#define ROOM_WEEDPATCH 4
#define ROOM_FOREST 5
#define ROOM_TFOREST 6
#define ROOM_TREE 7
#define ROOM_CORNER 8
#define ROOM_EKITCHEN 9
#define ROOM_KITCHEN 10
#define ROOM_SCULLERY 11
#define ROOM_DUSTY 12
#define ROOM_REARTURRET 13
#define ROOM_CLEARING 14
#define ROOM_PATH 15
#define ROOM_SIDE 16
#define ROOM_BHALLWAY 17
#define ROOM_DARKALCOVE 18
#define ROOM_SMALLDARK 19
#define ROOM_BSTAIRCASE 20
#define ROOM_WPASSAGE 21
#define ROOM_SLIPPERY 22
#define ROOM_CLIFFTOP 23
#define ROOM_CRUMBLING 24
#define ROOM_GLOOMY 25
#define ROOM_POOL_LIGHT 26
#define ROOM_VAULTED 27
#define ROOM_THICKDOOR 28
#define ROOM_TROPHY 29
#define ROOM_CELLAR 30
#define ROOM_CLIFFPATH1 31
#define ROOM_CUPBOARD 32
#define ROOM_FRONTHALL 33
#define ROOM_SITTING 34
#define ROOM_SECRET 35
#define ROOM_MSTAIRS 36
#define ROOM_DINING 37
#define ROOM_DEEPCELLAR 38
#define ROOM_CLIFFPATH2 39
#define ROOM_CLOSET 40
#define ROOM_FRONTLOBBY 41
#define ROOM_LIBRARY 42
#define ROOM_STUDY 43
#define ROOM_COBWEBBY 44
#define ROOM_VERYCOLD 45
#define ROOM_SPOOKY 46
#define ROOM_CLIFFPATH3 47
#define ROOM_VERANDAH 48
#define ROOM_FRONTPORCH 49
#define ROOM_FRONTTOWER 50
#define ROOM_SCORRIDOR 51
#define ROOM_GALLERY 52
#define ROOM_MARSHWALL 53
#define ROOM_MARSH 54
#define ROOM_SOGGYPATH 55
#define ROOM_TRAILING 56
#define ROOM_GATE 57
#define ROOM_BRAILING 58
#define ROOM_BTOWER 59
#define ROOM_DEBRIS 60
#define ROOM_BRICKWORK 61
#define ROOM_RSARCH 62
#define ROOM_CLIFFTOP2 63
/* Compass directions */
#define DIR_NORTH 1
#define DIR_SOUTH 2
#define DIR_WEST 3
#define DIR_EAST 4
#define DIR_UP 5
#define DIR_DOWN 6
/* Global variables corresponding to BASIC's use of variables: */
char *gl_msg; /* M$ -- parser message */
char gl_msgbuf[101]; /* Buffer for temporary message */
char gl_msgbuf2[202]; /* For use when the candle is waning or out */
char gl_command[81]; /* Q$ -- user command */
char gl_verb[81]; /* X$ -- first word of user command */
char gl_noun[81]; /* W$ -- second word of user command */
int gl_vb; /* VB -- verb number */
int gl_ob; /* OB -- noun number */
/* Variables for SDL conversion */
SDL_Surface* screen;
gfx_cursor cursor;
char gamePath[256];
int read_xzanfar = 0;
int examine_desk = 0;
int intro = 1;
/* Prototypes for what, in BASIC, would be line-numbered subroutines */
void inventory();
void move();
void get();
void vopen();
void examine();
void vread();
void say();
void dig();
void swing();
void spray();
void climb();
void light();
void unlight();
void use();
void unlock();
void leave();
void score();
void save();
void load();
char getkey();
int read_command(char *target);
void writechar(char c);
int action(char *target);
int subaction(char *target, int verb, int count, int location);
char* get_object(int index);
int object_count();
/* The game's vocabulary -- in the original BASIC, the V$() and O$() arrays */
struct vocab
{
char obj [WORD_COUNT + 1][14];
char verb [VERB_COUNT + 1][10];
} gl_vocab =
{
/* Nouns. The first OBJECT_COUNT of these correspond to portable objects */
{ "",
"PAINTING","RING","MAGIC SPELLS","GOBLET","SCROLL","COINS","STATUE","CANDLESTICK",
"MATCHES","VACUUM","BATTERIES","SHOVEL","AXE","ROPE","BOAT","AEROSOL","CANDLE","KEY",
"NORTH","SOUTH","WEST","EAST","UP","DOWN",
"DOOR","BATS","GHOSTS","DRAWER","DESK","COAT","RUBBISH",
"COFFIN","BOOKS","XZANFAR","WALL","SPELLS" },
/* Verbs */
{ "",
"HELP","CARRYING?","GO","N","S","W","E","UP","DOWN","GET","TAKE","OPEN","EXAMINE","READ","SAY",
"DIG","SWING","CLIMB","LIGHT","UNLIGHT","SPRAY","USE","UNLOCK","LEAVE","SCORE",
"SAVE","LOAD" }
};
/* The game's state. This is all stored in one structure to make it easy to do the
* save and restore. */
struct state
{
char magic[40]; /* Magic number (checked when state is loaded) */
char desc [ROOM_COUNT][40]; /* Location descriptions */
char route[ROOM_COUNT][5]; /* Connections between locations.
* The above two are in the file because they
* can change as the game progresses. */
short carried[WORD_COUNT + 1]; /* For each object, 1 if currently carried, else 0 */
short flag [WORD_COUNT + 1]; /* For portable objects, 1 if hidden, 0 if visible. For
* non-portable objects, used for other state */
short location[OBJECT_COUNT + 1]; /* The location of each object */
short rm; /* The location of the player */
short ll; /* The number of turns for which the candle will burn */
} gl_state, gl_initstate =
{
/* Initial state of the game */
"Haunted House v1.0 gamestate\032",
/* Descriptions */
{
"DARK CORNER","OVERGROWN GARDEN","BY LARGE WOODPILE","YARD BY RUBBISH",
"WEEDPATCH","FOREST","THICK FOREST","BLASTED TREE",
"CORNER OF HOUSE","ENTRANCE TO KITCHEN","KITCHEN & GRIMY COOKER","SCULLERY DOOR",
"ROOM WITH INCHES OF DUST","REAR TURRET ROOM","CLEARING BY HOUSE","PATH",
"SIDE OF HOUSE","BACK OF HALLWAY","DARK ALCOVE","SMALL DARK ROOM",
"BOTTOM OF SPIRAL STAIRCASE","WIDE PASSAGE","SLIPPERY STEPS","CLIFFTOP",
"NEAR CRUMBLING WALL","GLOOMY PASSAGE","POOL OF LIGHT","IMPRESSIVE VAULTED HALLWAY",
"HALL BY THICK WOODEN DOOR","TROPHY ROOM","CELLAR WITH BARRED WINDOW","CLIFF PATH",
"CUPBOARD WITH HANGING COAT","FRONT HALL","SITTING ROOM","SECRET ROOM",
"STEEP MARBLE STAIRS","DINING ROOM","DEEP CELLAR WITH COFFIN","CLIFF PATH",
"CLOSET","FRONT LOBBY","LIBRARY OF EVIL BOOKS","STUDY WITH DESK AND HOLE IN WALL",
"WEIRD COBWEBBY ROOM","VERY COLD CHAMBER","SPOOKY ROOM","CLIFF PATH BY MARSH",
"RUBBLE-STREWN VERANDAH","FRONT PORCH","FRONT TOWER","SLOPING CORRIDOR",
"UPPER GALLERY","MARSH BY WALL","MARSH","SOGGY PATH",
"BY TWISTED RAILING","PATH THROUGH IRON GATE","BY RAILINGS","BENEATH FRONT TOWER",
"DEBRIS FROM CRUMBLING FACADE","LARGE FALLEN BRICKWORK","ROTTING STONE ARCH","CRUMBLING CLIFFTOP",
},
/* Routes */
{ "SE","WE", "WE", "SWE","WE", "WE", "SWE", "SW",
"NS","SE", "WE", "NW", "SE", "W", "NE", "NSW",
"NS","NS", "SE", "WE", "NWUD","SE", "WSUD","NS",
"N", "NS", "NSE","WE", "WE", "NSW","NS", "NS",
"S", "NSE","NSW","S", "NSUD","N", "N", "NS",
"NE","NW", "NE", "W", "NSE", "WE", "W", "NS",
"SE","NSW","E", "WE", "NW", "S", "SW", "NW",
"NE","NWE","WE", "WE", "WE", "NWE","NWE", "W" },
/* Nothing carried */
{ 0 },
/* All objects visible (see initialise() below) */
{ 0 },
/* Initial locations of objects */
{ 0,ROOM_SPOOKY, /* Painting */
ROOM_DEEPCELLAR, /* Ring */
ROOM_SECRET, /* Magic spells */
ROOM_FRONTTOWER, /* Goblet */
ROOM_REARTURRET, /* Scroll */
ROOM_DARKALCOVE, /* Coins */
ROOM_THICKDOOR, /* Statue */
ROOM_LIBRARY, /* Candlestick */
ROOM_KITCHEN, /* Matches */
ROOM_GLOOMY, /* Vacuum cleaner */
ROOM_POOL_LIGHT, /* Batteries */
ROOM_WEEDPATCH, /* Shovel */
ROOM_WOODPILE, /* Axe */
ROOM_TREE, /* Rope */
ROOM_CLIFFPATH3, /* Boat */
ROOM_DEBRIS, /* Aerosol */
ROOM_STUDY, /* Candle */
ROOM_CUPBOARD /* Key */
}
};
char* gamePathInit(const char* path)
{
uintptr_t i, j;
for(i = 0, j = 0; path[i] != '\0'; i++)
{
if((path[i] == '\\') || (path[i] == '/')) j = i + 1;
}
strncpy(gamePath, path, j);
return gamePath;
}
void print_text(char *text)
{
int height = renderer_font_height();
renderer_font_print(&cursor, text);
}
void initialise()
{
memcpy(&gl_state, &gl_initstate, sizeof(gl_state));
/* Render a number of objects invisible */
gl_state.flag[OBJ_KEY ] =
gl_state.flag[OBJ_CANDLE] =
gl_state.flag[OBJ_RING ] =
gl_state.flag[OBJ_BATS ] =
gl_state.flag[OBJ_DRAWER] =
gl_state.flag[OBJ_UP ] = 1;
gl_state.ll = 60; /* Candle countdown = 60 */
gl_state.rm = ROOM_GATE; /* Start in room 57 */
gl_msg = "OK";
}
void print_options(char *left, char *right)
{
int width = renderer_font_width(right);
int height = renderer_font_height();
gfx_cursor left_cur = { 2, display_height - height - 2 };
gfx_cursor right_cur = { display_width - width - 2, display_height - height - 2 };
renderer_fill_rect(0, display_height - height - 4, display_width, height + 4, 0xAA, 0x37, 0x00);
renderer_font_print(&left_cur, left);
renderer_font_print(&right_cur, right);
}
void more()
{
print_options(" ", "More...");
getkey();
}
void clear()
{
int score = 0, i = 0;
int height = renderer_font_height();
gfx_cursor title = {160 , 2};
char text[256];
char *name = "HAUNTED HOUSE";
cursor.x = 0;
cursor.y = height + 8;
title.x -= renderer_font_width(name)/2;
renderer_clear(0, 0, 0);
renderer_fill_rect(0, 0, display_width, height + 4, 0x00, 0x00, 0x66);
renderer_font_print(&title, name);
// Compute score
for(score = 0, i = 1; i <= OBJECT_COUNT; i++)
{
if(gl_state.carried[i]) ++score;
}
if(score == 17 && gl_state.carried[OBJ_BOAT] == 0)
{
if(gl_state.rm == ROOM_GATE) score *= 2;
}
// Print score
sprintf(text, "Score: %d", score);
title.x = display_width - renderer_font_width(text) - 2;
renderer_font_print(&title, text);
// Print candle when lit
if(gl_state.flag[OBJ_NULL])
{
title.x = 2;
renderer_font_print(&title, "Candle:");
renderer_fill_rect(renderer_font_width("Candle:") + 4, 4, gl_state.ll, 6, 0xFF, 0xFF, 0xFF);
}
print_options("Exit (Select)", "Inventory (X) / Actions (A)");
}
int main(int argc, char **argv)
{
int i;
char *s;
char text[512];
gamePathInit(argv[0]);
// initialize SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Haunted House", "Haunted House");
renderer_init(&gamePath);
clear();
initialise();
while(1)
{
clear();
sprintf(text, "HAUNTED HOUSE\n-------------\nYOUR LOCATION\n%s\nEXITS:\n", gl_state.desc[gl_state.rm]);
print_text(text);
for (i = 0; gl_state.route[gl_state.rm][i]; i++)
{
/* To remove trailing comma: do it before each entry, thus. */
if (i > 0) writechar(',');
writechar(gl_state.route[gl_state.rm][i]);
/* writechar(','); */
}
writechar('\n');
/* List objects (if any) present */
for (i = 1; i <= OBJECT_COUNT; i++)
{
if (gl_state.location[i] == gl_state.rm && gl_state.flag[i] == 0)
{
sprintf(text, "YOU CAN SEE %s HERE\n", gl_vocab.obj[i]);
print_text(text);
}
}
sprintf(text, "=========================\n%s\n", gl_msg);
print_text(text);
print_text("WHAT WILL YOU DO NOW?");
/* Read player input */
if(!read_command(gl_command))
{
continue;
}
/* Remove trailing newline, if present */
s = strchr(gl_command, '\n');
if (s) *s = 0;
/* Parse into verb + noun. Find the first space. */
s = strchr(gl_command, ' ');
if (s)
{
*s = 0;
strcpy(gl_verb, gl_command);
/* Skip any additional spaces between verb and noun */
++s;
while (*s == ' ') ++s;
strcpy(gl_noun, s);
}
else /* No space. Treat the whole thing as the verb. */
{
gl_noun[0] = 0;
strcpy(gl_verb, gl_command);
}
/* Match verb and noun against vocab. Verb/noun numbers
* 0 are used to mean 'unknown', so the vocab tables are 1-based. */
gl_vb = VERB_NULL;
gl_ob = OBJ_NULL;
gl_msg = "WHAT";
for (i = 1; i <= VERB_COUNT; i++)
{
if (!strcmp(gl_verb, gl_vocab.verb[i]))
{
gl_vb = i;
break;
}
}
for (i = 1; i <= WORD_COUNT; i++)
{
if (!strcmp(gl_noun, gl_vocab.obj[i]))
{
gl_ob = i;
break;
}
}
if (gl_noun[0] != 0 && gl_ob == OBJ_NULL) /* Unrecognised noun string */
gl_msg = "THAT'S SILLY";
if (gl_noun[0] == 0) /* Verb only */
gl_msg = "I NEED TWO WORDS";
if (gl_vb == VERB_NULL)
{
if (gl_ob > OBJ_NULL)
{
sprintf(gl_msgbuf, "YOU CAN'T '%s %s'", gl_verb, gl_noun);
gl_msg = gl_msgbuf;
}
else gl_msg = "YOU DON'T MAKE SENSE";
}
/* Attempt to use an object that isn't currently held */
if (gl_vb > VERB_NULL && gl_ob > OBJ_NULL && gl_state.carried[gl_ob] == 0)
{
sprintf(gl_msgbuf, "YOU DON'T HAVE '%s'", gl_noun);
gl_msg = gl_msgbuf;
}
/* If bats are attacking the player, lock out all verbs (including
* meta verbs) except SPRAY. */
if (gl_state.flag[OBJ_BATS] == 1 && gl_state.rm == ROOM_REARTURRET
&& (rand() % 3) != 2 && gl_vb != VERB_SPRAY)
{
gl_msg = "BATS ATTACKING!";
/* Fortunately, a turn aborted due to bat attack does not cause
* the candle to expend a turn's wax. */
continue;
}
/* If in the cobwebby room and the vacuum cleaner isn't running, there's a 50% chance of
* activating the ghosts. */
if (gl_state.rm == ROOM_COBWEBBY && (rand() % 2) == 1 && gl_state.flag[OBJ_DOWN] != 1)
{
gl_state.flag[OBJ_GHOSTS] = 1;
}
/* If the candle is lit, decrease its timer. */
if (gl_state.flag[OBJ_NULL]) --gl_state.ll;
/* And when the timer reaches 0, the candle goes out. */
if (gl_state.ll < 1) gl_state.flag[OBJ_NULL] = 0;
/* Handle the appropriate verb. */
switch(gl_vb)
{
case VERB_INVENTORY: inventory(); break;
case VERB_GO:
case VERB_NORTH:
case VERB_SOUTH:
case VERB_WEST:
case VERB_EAST:
case VERB_UP:
case VERB_DOWN: move(); break;
case VERB_GET:
case VERB_TAKE: get(); break;
case VERB_OPEN: vopen(); break;
case VERB_EXAMINE: examine(); break;
case VERB_READ: vread(); break;
case VERB_SAY: say(); break;
case VERB_DIG: dig(); break;
case VERB_SWING: swing(); break;
case VERB_CLIMB: climb(); break;
case VERB_LIGHT: light(); break;
case VERB_UNLIGHT: unlight(); break;
case VERB_SPRAY: spray(); break;
case VERB_USE: use(); break;
case VERB_UNLOCK: unlock(); break;
case VERB_LEAVE: leave(); break;
case VERB_SCORE: score(); break;
case VERB_SAVE: save(); break;
case VERB_LOAD: load(); break;
}
/* If the candle is waning or out, append a warning.
* In the original BASIC, the warning message would
* replace the result of the verb subroutine,
* possibly overwriting valuable information.
*/
if (gl_state.ll == 10)
{
sprintf(gl_msgbuf2, "%s\nYOUR CANDLE IS WANING!", gl_msg);
gl_msg = gl_msgbuf2;
}
if (gl_state.ll == 1)
{
sprintf(gl_msgbuf2, "%s\nYOUR CANDLE IS OUT!", gl_msg);
gl_msg = gl_msgbuf2;
}
}
renderer_release();
SDL_Quit();
return 0;
}
int inventory_actions(int object, int offsetx, int offsety)
{
char command;
char text[1024];
int count = 2;
int options[2] = {
VERB_EXAMINE, VERB_LEAVE
};
int height = renderer_font_height() + 2;
int window_height = count * height;
int max_height = offsety + window_height + 10;
int index = 0;
int i;
if(max_height > display_height) offsety -= (max_height - display_height);
if(offsety - 6 < renderer_font_height()) offsety = renderer_font_height() + 6;
while(1)
{
renderer_fill_rect(offsetx - 6, offsety - 6, 86, window_height + 10, 0x66, 0x66, 0x66);
renderer_fill_rect(offsetx - 4, offsety - 4, 82, window_height + 6, 0x99, 0x99, 0x99);
cursor.x = offsetx;
cursor.y = offsety;
for(i = 0; i < count; i++)
{
if(i == index)
{
renderer_fill_rect(cursor.x - 1, cursor.y - 1, renderer_font_width(gl_vocab.verb[options[i]]) + 2, renderer_font_height() + 2, 0x00, 0x00, 0x00);
}
print_text(gl_vocab.verb[options[i]]);
cursor.x = offsetx;
cursor.y += height;
}
command = getkey();
if(command == 'N') index = (index - 1) % count;
if(command == 'S') index = (index + 1) % count;
if(command == 'B') return 0;
if(command == 'A')
{
if(options[index] == VERB_EXAMINE)
{
gl_vb = VERB_EXAMINE;
gl_ob = object;
clear();
examine();
sprintf(text, "\nExamine %s\n=========================\n%s\n", gl_vocab.obj[gl_ob], gl_msg);
print_text(text);
more();
return 1;
}
else if(options[index] == VERB_LEAVE)
{
gl_vb = VERB_LEAVE;
gl_ob = object;
clear();
leave();
sprintf(text, "\nYou leave the %s behind\n", gl_vocab.obj[gl_ob]);
print_text(text);
more();
return 1;
}
}
}
}
void inventory()
{
int count = 17;
int items[17] = {
OBJ_AEROSOL, OBJ_AXE, OBJ_BATTERIES,
OBJ_CANDLE, OBJ_CANDLESTICK, OBJ_COINS,
OBJ_GOBLET, OBJ_KEY, OBJ_SPELLS,
OBJ_MATCHES, OBJ_PAINTING, OBJ_RING,
OBJ_ROPE, OBJ_SCROLL, OBJ_SHOVEL,
OBJ_STATUE, OBJ_VACUUM
};
char command;
gfx_cursor inventory_cursor;
gfx_cursor selected;
int x = 0;
int y = 0;
int move_x = 0;
int move_y = 0;
int index;
int height = renderer_font_height();
int offset_x = (display_width - (20 * 2)) / 3;
int offset_y = (display_height - (height * 3) - (8 * 2) - 16) / 6;
int notempty = 0;
// Check if the menu has any items
for(int i=0; i<count; i++) notempty += gl_state.carried[items[i]];
if(!notempty) return;
// Find first item
for(int i=0; i<count; i++)
{
if(gl_state.carried[items[i]])
{
inventory_cursor.x = i%3;
inventory_cursor.y = i/3;
break;
}
}
while(1)
{
clear();
cursor.x = (display_width - renderer_font_width("INVENTORY")) / 2;
cursor.y = height + 16;
renderer_fill_rect(2, height + 8, display_width - 4, display_height - (height * 2) - 20, 0x66, 0x66, 0x66);
renderer_fill_rect(4, height + 10, display_width - 6, display_height - (height * 2) - 20, 0x99, 0x99, 0x99);
renderer_font_print(&cursor, "INVENTORY");
for(int i=0; i<count; i++)
{
cursor.x = 20 + (x * offset_x);
cursor.y = height + 16 + height + 16 + (y * offset_y);
if(gl_state.carried[items[i]])
{
if(i == (inventory_cursor.y * 3 + inventory_cursor.x))
{
renderer_fill_rect(cursor.x - 1, cursor.y - 1, renderer_font_width(gl_vocab.obj[items[i]]) + 2, height + 2, 0x00, 0x00, 0x00);
selected.x = x;
selected.y = y;
}
renderer_font_print(&cursor, gl_vocab.obj[items[i]]);
x++;
if(x >= 3)
{
y++;
x = 0;
}
}
}
command = getkey();
if(command == 'A')
{
index = (inventory_cursor.y * 3 + inventory_cursor.x);
x = 20 + (selected.x * offset_x);
y = height + 16 + height + 16 + (selected.y * offset_y);
if(inventory_actions(items[index], x + 25, y + 10)) return;
}
if(command == 'B') return;
if(command == 'N') move_y = -3;
if(command == 'E') move_x = 1;
if(command == 'S') move_y = 3;
if(command == 'W') move_x = -1;
if(move_y > 0)
{
move_y = 0;
index = (inventory_cursor.y * 3 + inventory_cursor.x);
while(move_y < 3)
{
index++;
if(index >= count) index -= count;
if(gl_state.carried[items[index]]) move_y++;
}
inventory_cursor.x = index%3;
inventory_cursor.y = index/3;
move_y = 0;
}
else if(move_y < 0)
{
move_y = 0;
index = (inventory_cursor.y * 3 + inventory_cursor.x);
while(move_y > -3)
{
index--;
if(index < 0) index += count;
if(gl_state.carried[items[index]]) move_y--;
}
inventory_cursor.x = index%3;
inventory_cursor.y = index/3;
move_y = 0;
}
if(move_x != 0 || move_y != 0)
{
index = (inventory_cursor.y * 3 + inventory_cursor.x);
do
{
index += move_x + move_y;
while(index < 0) index += count;
while(index >= count) index -= count;
inventory_cursor.x = index%3;
inventory_cursor.y = index/3;
}
while(!gl_state.carried[items[index]]);
move_x = 0;
move_y = 0;
}
x = 0;
y = 0;
}
}
void move()
{
int i;
int d = 0; /* direction */
char *route = gl_state.route[gl_state.rm];
if (gl_ob == OBJ_NULL)
{
d = gl_vb - (VERB_NORTH - 1);
}
else if (gl_ob >= OBJ_NORTH && gl_ob <= OBJ_DOWN)
{
d = gl_ob - (OBJ_NORTH - 1);
}
/* Convert up/down for the staircases to N/S/E/W */
switch (gl_state.rm)
{
case ROOM_BSTAIRCASE:
if (d == DIR_UP) d = DIR_NORTH;
if (d == DIR_DOWN) d = DIR_WEST;
break;
case ROOM_SLIPPERY:
if (d == DIR_UP) d = DIR_SOUTH;
if (d == DIR_DOWN) d = DIR_WEST;
break;
case ROOM_MSTAIRS:
if (d == DIR_UP) d = DIR_NORTH;
if (d == DIR_DOWN) d = DIR_SOUTH;
break;
}
if (gl_state.flag[OBJ_ROPE])
{
gl_msg = "CRASH! YOU FELL OUT OF THE TREE!";
gl_state.flag[OBJ_ROPE] = 0;
return;
}
if (gl_state.flag[OBJ_GHOSTS] && gl_state.rm == ROOM_GALLERY)
{
gl_msg = "GHOSTS WILL NOT LET YOU MOVE";
return;
}
if (gl_state.rm == ROOM_VERYCOLD && gl_state.carried[OBJ_PAINTING] && gl_state.flag[OBJ_XZANFAR] == 0)
{
gl_msg = "A MAGICAL BARRIER TO THE WEST";
return;
}
if (gl_state.rm == ROOM_POOL_LIGHT && gl_state.flag[OBJ_NULL] == 0 && (d == 1 || d == 4))
{
gl_msg = "YOU NEED A LIGHT";
return;
}
if (gl_state.rm == ROOM_MARSH && !gl_state.carried[OBJ_BOAT])
{
gl_msg = "YOU'RE STUCK!";
return;
}
if(gl_state.carried[OBJ_BOAT] &&
(gl_state.rm != ROOM_MARSHWALL &&
gl_state.rm != ROOM_MARSH &&
gl_state.rm != ROOM_SOGGYPATH &&
gl_state.rm != ROOM_CLIFFPATH3))
{
gl_msg = "YOU CAN'T CARRY A BOAT!";
return;
}
if (gl_state.rm > ROOM_POOL_LIGHT && gl_state.rm < ROOM_CELLAR && !gl_state.flag[OBJ_NULL])
{
gl_msg = "TOO DARK TO MOVE";
return;
}
gl_state.flag[OBJ_WALL] = 0;
for (i = 0; route[i]; i++)
{
if (route[i] == 'N' && d == DIR_NORTH)
{
gl_state.rm -= 8;
gl_state.flag[OBJ_WALL] = 1;
break;
}
if (route[i] == 'S' && d == DIR_SOUTH)
{
gl_state.rm += 8;
gl_state.flag[OBJ_WALL] = 1;
break;
}
if (route[i] == 'W' && d == DIR_WEST)
{
gl_state.rm--;
gl_state.flag[OBJ_WALL] = 1;
break;
}
if (route[i] == 'E' && d == DIR_EAST)
{
gl_state.rm++;
gl_state.flag[OBJ_WALL] = 1;
break;
}
}
gl_msg = (gl_state.flag[OBJ_WALL]) ? "OK" : "CAN'T GO THAT WAY!";
if (d < DIR_NORTH) gl_msg = "GO WHERE?";
if (gl_state.rm == ROOM_FRONTLOBBY && gl_state.flag[OBJ_UP] == 1)
{
strcpy(gl_state.route[ROOM_FRONTPORCH], "SW");
gl_msg = "THE DOOR SLAMS SHUT!";
gl_state.flag[OBJ_UP] = 0;
}
if(gl_state.rm == ROOM_GATE && gl_state.carried[OBJ_PAINTING])
{
score();
}
}
void get()
{
if (gl_ob > OBJECT_COUNT)
{
sprintf(gl_msgbuf, "I CAN'T GET %s", gl_noun);
gl_msg = gl_msgbuf;
return;
}
if (gl_state.location[gl_ob] != gl_state.rm)
{
gl_msg = "IT ISN'T HERE";
}
if (gl_state.flag[gl_ob])
{
sprintf(gl_msgbuf, "WHAT %s?", gl_noun);
gl_msg = gl_msgbuf;
}
if (gl_state.carried[gl_ob])
{
gl_msg = "YOU ALREADY HAVE IT";
}
if (gl_ob > OBJ_NULL && gl_state.location[gl_ob] == gl_state.rm && gl_state.flag[gl_ob] == 0)
{
gl_state.carried[gl_ob] = 1;
gl_state.location[gl_ob] = ROOM_COUNT + 1;
sprintf(gl_msgbuf, "YOU HAVE THE %s", gl_noun);
gl_msg = gl_msgbuf;
}
}
void vopen()
{
if (gl_state.rm == ROOM_STUDY && (gl_ob == OBJ_DRAWER || gl_ob == OBJ_DESK))
{
gl_state.flag[OBJ_CANDLE] = 0;
gl_msg = "DRAWER OPEN";
}
if (gl_state.rm == ROOM_THICKDOOR && gl_ob == OBJ_DOOR)
{
gl_msg = "IT'S LOCKED";
}
if (gl_state.rm == ROOM_DEEPCELLAR && gl_ob == OBJ_COFFIN)