Debian Codegraph @

terps/taylor/player.c

raw symbols SCIP
1 //
2 // player.c
3 // Part of TaylorMade, an interpreter for Adventure Soft UK games
4 //
5 // Created by Petter Sjölund on 2022-04-05.
6 //
7
8 #include <ctype.h>
9 #include <stdarg.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <strings.h>
14 #include <time.h>
15 #include <unistd.h>
16
17 #include "glk.h"
18 #ifdef SPATTERLIGHT
19 #include "glkimp.h"
20 #endif
21 #include "c64decrunch.h"
22 #include "decompressz80.h"
23 #include "decrypttotloader.h"
24 #include "extracommands.h"
25 #include "extracttape.h"
26 #include "glkstart.h"
27 #include "parseinput.h"
28 #include "restorestate.h"
29 #include "sagadraw.h"
30 #include "utility.h"
31
32 #include "taylor.h"
33
34 uint8_t Flag[128];
35 uint8_t ObjectLoc[256];
36
37 uint8_t *FileImage = NULL;
38 uint8_t *EndOfData = NULL;
39 size_t FileImageLen = 0;
40 size_t VerbBase;
41 static size_t TokenBase;
42 static size_t MessageBase;
43 static size_t Message2Base;
44 static size_t RoomBase;
45 static size_t ObjectBase;
46 static size_t ExitBase;
47 static size_t ObjLocBase;
48 static size_t StatusBase;
49 static size_t ActionBase;
50 static size_t FlagBase;
51 size_t AnimationData = 0;
52
53 int NumLowObjects;
54
55 static int ActionsExecuted;
56 int PrintedOK;
57 int Redraw = 0;
58
59 int FirstAfterInput = 0;
60
61 int StopTime = 0;
62 int JustStarted = 1;
63 int ShouldRestart = 0;
64
65 int NoGraphics = 0;
66
67 long FileBaselineOffset = 0;
68
69 char DelimiterChar = '_';
70
71 static char *Filename;
72 static uint8_t *CompanionFile;
73
74 char LastChar = 0;
75 static int Upper = 0;
76 int PendSpace = 0;
77
78 static int LastNoun = 0;
79 int LastVerb = 0;
80 static int GoVerb = 0;
81
82 static int FoundVerb = 0;
83 static int FoundNoun = 0;
84
85 struct GameInfo *Game = NULL;
86 extern struct GameInfo games[];
87
88 strid_t room_description_stream = NULL;
89
90 extern int AnimationRunning;
91
92 static int DeferredGoto = 0;
93 int InKaylethPreview = 0;
94
95 #ifdef DEBUG
96
97 /*
98 * Debugging
99 */
100 static unsigned char WordMap[256][5];
101
102 static char *Condition[] = {
103 "<ERROR>",
104 "AT",
105 "NOTAT",
106 "ATGT",
107 "ATLT",
108 "PRESENT",
109 "HERE",
110 "ABSENT",
111 "NOTHERE",
112 "CARRIED",
113 "NOTCARRIED",
114 "WORN",
115 "NOTWORN",
116 "NODESTROYED",
117 "DESTROYED",
118 "ZERO",
119 "NOTZERO",
120 "WORD1",
121 "WORD2",
122 "WORD3",
123 "CHANCE",
124 "LT",
125 "GT",
126 "EQ",
127 "NE",
128 "OBJECTAT",
129 "COND26",
130 "COND27",
131 "COND28",
132 "COND29",
133 "COND30",
134 "COND31",
135 };
136
137 static char *Action[] = {
138 "<ERROR>",
139 "LOAD?",
140 "QUIT",
141 "INVENTORY",
142 "ANYKEY",
143 "SAVE",
144 "DROPALL",
145 "LOOK",
146 "OK", /* Guess */
147 "GET",
148 "DROP",
149 "GOTO",
150 "GOBY",
151 "SET",
152 "CLEAR",
153 "MESSAGE",
154 "CREATE",
155 "DESTROY",
156 "PRINT",
157 "DELAY",
158 "WEAR",
159 "REMOVE",
160 "LET",
161 "ADD",
162 "SUB",
163 "PUT", /* ?? */
164 "SWAP",
165 "SWAPF",
166 "MEANS",
167 "PUTWITH",
168 "BEEP", /* Rebel Planet at least */
169 "REFRESH?",
170 "RAMSAVE",
171 "RAMLOAD",
172 "CLSLOW?",
173 "OOPS",
174 "DIAGNOSE",
175 "SWITCHINVENTORY",
176 "SWITCHCHARACTER",
177 "CONTINUE",
178 "IMAGE",
179 "ACT41",
180 "ACT42",
181 "ACT43",
182 "ACT44",
183 "ACT45",
184 "ACT46",
185 "ACT47",
186 "ACT48",
187 "ACT49",
188 "ACT50",
189 };
190
191 static void LoadWordTable(void)
192 {
193 unsigned char *p = FileImage + VerbBase;
194
195 while (1) {
196 if (p[4] == 255)
197 break;
198 if (WordMap[p[4]][0] == 0)
199 memcpy(WordMap[p[4]], p, 4);
200 p += 5;
201 }
202 }
203
204 static void PrintWord(unsigned char word)
205 {
206 if (word == 126)
207 fprintf(stderr, "* ");
208 else if (word == 0 || WordMap[word][0] == 0)
209 fprintf(stderr, "%-4d ", word);
210 else {
211 fprintf(stderr, "%c%c%c%c ",
212 WordMap[word][0],
213 WordMap[word][1],
214 WordMap[word][2],
215 WordMap[word][3]);
216 }
217 }
218
219 #endif
220
221 static char Q3Condition[] = {
222 CONDITIONERROR,
223 AT,
224 NOTAT,
225 ATGT,
226 ATLT,
227 PRESENT,
228 ABSENT,
229 CARRIED,
230 NOTCARRIED,
231 NODESTROYED,
232 DESTROYED,
233 ZERO,
234 NOTZERO,
235 WORD1,
236 WORD2,
237 CHANCE,
238 LT,
239 GT,
240 EQ,
241 OBJECTAT,
242 0,
243 0,
244 0,
245 0,
246 0,
247 0,
248 0,
249 0,
250 0,
251 0,
252 0,
253 0,
254 };
255
256 static char Q3Action[] = {
257 ACTIONERROR,
258 SWITCHINVENTORY, /* Swap inventory and dark flag */
259 DIAGNOSE, /* Print Reed Richards' watch status message */
260 LOADPROMPT,
261 QUIT,
262 SHOWINVENTORY,
263 ANYKEY,
264 SAVE,
265 DONE, /* Set "condition failed" flag, Flag[118], to 1 */
266 GET,
267 DROP,
268 GOTO,
269 SWITCHCHARACTER, /* Go to the location of the other guy */
270 SET,
271 CLEAR,
272 MESSAGE,
273 CREATE,
274 DESTROY,
275 LET,
276 ADD,
277 SUB,
278 PUT,
279 SWAP,
280 IMAGE, /* Draw image on top of room image */
281 0,
282 0,
283 0,
284 0,
285 0,
286 0,
287 0,
288 0,
289 0,
290 0,
291 0,
292 0,
293 0,
294 0,
295 0,
296 0,
297 0,
298 0,
299 0,
300 0,
301 0,
302 0,
303 0,
304 0,
305 0,
306 0,
307 0
308 };
309
310 size_t FindCode(const char *x, size_t base, size_t len)
311 {
312 unsigned char *p = FileImage + base;
313 while (p < FileImage + FileImageLen - len) {
314 if (memcmp(p, x, len) == 0)
315 return p - FileImage;
316 p++;
317 }
318 return (size_t)-1;
319 }
320
321 static size_t FindFlags(void)
322 {
323 size_t pos;
324
325 /* Questprobe */
326 pos = FindCode("\xE7\x97\x51\x95\x5B\x7E\x5D\x7E\x76\x93", 0, 10);
327 if (pos == -1) {
328 /* Look for the flag initial block copy */
329 pos = FindCode("\x01\x06\x00\xED\xB0\xC9\x00\xFD", 0, 8);
330 if (pos == -1) {
331 if (Game)
332 return Game->start_of_flags + FileBaselineOffset;
333 else {
334 fprintf(stderr, "Cannot find initial flag data.\n");
335 glk_exit();
336 }
337 } else
338 return pos + 5;
339 }
340 return pos + 11;
341 }
342
343 static int LooksLikeTokens(size_t pos)
344 {
345 if (pos > FileImageLen)
346 return 0;
347 unsigned char *p = FileImage + pos;
348 int n = 0;
349 int t = 0;
350 while (n < 512) {
351 unsigned char c = p[n] & 0x7F;
352 if (c >= 'a' && c <= 'z')
353 t++;
354 n++;
355 }
356 if (t > 300)
357 return 1;
358 return 0;
359 }
360
361 static void TokenClassify(size_t pos)
362 {
363 unsigned char *p = FileImage + pos;
364 int n = 0;
365 while (n++ < 256) {
366 do {
367 if (*p == 0x5E || *p == 0x7E)
368 Version = 0;
369 } while (!(*p++ & 0x80));
370 }
371 }
372
373 static size_t FindTokens(void)
374 {
375 size_t addr;
376 size_t pos = 0;
377
378 if (Game)
379 switch (CurrentGame) {
380 case TOT_TEXT_ONLY_64:
381 case TOT_HYBRID_64:
382 if ((pos = FindCode("\x80\x59\x6f\x75\x20\x61\x72\x65\x20\x69", 0, 10)) != -1)
383 return pos;
384 break;
385 case TEMPLE_OF_TERROR_64:
386 if ((pos = FindCode("\x80\x20\x54\x68\x65\x72\x65\x20\x69\x73", 0, 10)) != -1)
387 return pos;
388 break;
389 case REBEL_PLANET_64:
390 if ((pos = FindCode("\xa7\x2e\xfe\x20\xfe\x2c\xfe\x21\xfe\x3f", 0, 10)) != -1)
391 return pos;
392 break;
393 case QUESTPROBE3_64:
394 if ((pos = FindCode("\x61\xa0\x64\xa0\x65\xa0\x67\xa0\x69\xa0", 0, 10)) != -1)
395 return pos;
396 break;
397 case HEMAN_64:
398 case KAYLETH_64:
399 if ((pos = FindCode("\x80\x59\x6f\x75\x20\x61\x72\x65\x20\x69", 0, 10)) != -1)
400 return pos;
401 break;
402 default:
403 break;
404 }
405
406 do {
407 pos = FindCode("\x47\xB7\x28\x0B\x2B\x23\xCB\x7E", pos + 1, 8);
408 if (pos == -1) {
409 /* Questprobe */
410 pos = FindCode("\x58\x58\x58\x58\xFF", 0, 5);
411 if (pos == -1) {
412 /* Last resort */
413 addr = FindCode("You are in ", 0, 11) - 1;
414 if (addr == -1) {
415 if (Game)
416 return Game->start_of_tokens + FileBaselineOffset;
417 fprintf(stderr, "Unable to find token table.\n");
418 return 0;
419 }
420 return addr;
421 } else
422 return pos + 6;
423 }
424 addr = (FileImage[pos - 1] << 8 | FileImage[pos - 2]) - 0x4000 + FileBaselineOffset;
425 } while (LooksLikeTokens(addr) == 0);
426 TokenClassify(addr);
427 return addr;
428 }
429
430 void WriteToRoomDescriptionStream(const char *fmt, ...)
431 {
432 if (room_description_stream == NULL)
433 return;
434 va_list ap;
435 char msg[2048];
436
437 va_start(ap, fmt);
438 vsnprintf(msg, sizeof msg, fmt, ap);
439 va_end(ap);
440
441 glk_put_string_stream(room_description_stream, msg);
442 }
443
444 static void OutWrite(char c)
445 {
446 if (isalpha(c) && Upper) {
447 c = toupper(c);
448 Upper = 0;
449 }
450 PrintCharacter(c);
451 }
452
453 void OutFlush(void)
454 {
455 if (LastChar)
456 OutWrite(LastChar);
457 if (PendSpace && LastChar != '\n' && !FirstAfterInput)
458 OutWrite(' ');
459 LastChar = 0;
460 PendSpace = 0;
461 }
462
463 static void OutReset(void)
464 {
465 OutFlush();
466 }
467
468 static int Q3Upper = 0;
469
470 void OutCaps(void)
471 {
472 if (LastChar) {
473 OutWrite(LastChar);
474 LastChar = 0;
475 }
476 Upper = 1;
477 Q3Upper = 1;
478 }
479
480 static int periods = 0;
481 int JustWrotePeriod = 0;
482
483 void OutChar(char c)
484 {
485 if (c == ']')
486 c = '\n';
487
488 if (c == '.') {
489 periods++;
490 if (LastChar == '?' || FirstAfterInput || isspace(LastChar) || LastChar == '.') {
491 c = ' ';
492 if (LastChar == ' ')
493 LastChar = 0;
494 }
495 PendSpace = 0;
496 } else {
497 if (periods && !JustWrotePeriod && c != ',' && c != '?' && c != '!') {
498 if (periods == 2)
499 periods = 1;
500 for (int i = 0; i < periods; i++) {
501 JustWrotePeriod = 0;
502 PrintCharacter('.');
503 }
504 JustWrotePeriod = 1;
505 LastChar = 0;
506 PendSpace = 0;
507 Upper = 0;
508 }
509 periods = 0;
510 }
511
512 if (c == ' ') {
513 PendSpace = 1;
514 return;
515 }
516 if (FirstAfterInput) {
517 if (isspace(LastChar)) {
518 LastChar = 0;
519 Upper = 1;
520 } else if (!isspace(c)) {
521 FirstAfterInput = 0;
522 }
523 PendSpace = 0;
524 }
525 if (LastChar) {
526 if (isspace(LastChar))
527 PendSpace = 0;
528 if (LastChar == '.' && JustWrotePeriod) {
529 LastChar = 0;
530 } else {
531 OutWrite(LastChar);
532 }
533 }
534 if (PendSpace) {
535 if (JustWrotePeriod && BaseGame != KAYLETH)
536 Upper = 1;
537 OutWrite(' ');
538 PendSpace = 0;
539 }
540 LastChar = c;
541 if (LastChar == 10 || LastChar == 13) {
542 Upper = 1;
543 Q3Upper = 1;
544 }
545 }
546
547 static void OutReplace(char c)
548 {
549 LastChar = c;
550 }
551
552 static void OutKillSpace()
553 {
554 PendSpace = 0;
555 }
556
557 void OutString(char *p)
558 {
559 while (*p)
560 OutChar(*p++);
561 }
562
563 static unsigned char *TokenText(unsigned char n)
564 {
565 unsigned char *p = FileImage + TokenBase;
566 if (Version == QUESTPROBE3_TYPE)
567 n -= 0x7b;
568
569 while (n > 0 && p < EndOfData) {
570 while ((*p & 0x80) == 0)
571 p++;
572 n--;
573 p++;
574 }
575 return p;
576 }
577
578 void Q3PrintChar(uint8_t c)
579 { // Print character
580 if (c < 0x20 && c != 0x0a)
581 return;
582
583 if (FirstAfterInput)
584 Q3Upper = 1;
585
586 if (Q3Upper && c >= 'a') {
587 c -= 0x20; // token is made uppercase
588 }
589 OutChar(c);
590 if (c > '!') {
591 Q3Upper = 0;
592 Upper = 0;
593 }
594 if (c == '!' || c == '?' || c == ':' || c == '.') {
595 Q3Upper = 1;
596 }
597 }
598
599 static void PrintToken(unsigned char n)
600 {
601 if (CurrentGame == BLIZZARD_PASS && MyLoc == 49 && n == 0x2d) {
602 OutChar(0x2d);
603 return;
604 }
605 unsigned char *p = TokenText(n);
606 unsigned char c;
607 do {
608 c = *p++;
609 if (Version == QUESTPROBE3_TYPE)
610 Q3PrintChar(c & 0x7F);
611 else
612 OutChar(c & 0x7F);
613 } while (p < EndOfData && !(c & 0x80));
614 }
615
616 static void Q3PrintText(unsigned char *p, int n)
617 {
618 while (n > 0) {
619 while (*p != 0x1f && *p != 0x18) {
620 p++;
621 }
622 n--;
623 p++;
624 }
625 do {
626 if (*p == 0x18)
627 return;
628 if (*p >= 0x7b) // if c is >= 0x7b it is a token
629 PrintToken(*p);
630 else
631 Q3PrintChar(*p);
632 } while (*p++ != 0x1f);
633 }
634
635 static void PrintText1(unsigned char *p, int n)
636 {
637 while (n > 0) {
638 while (p < EndOfData && *p != 0x7E && *p != 0x5E)
639 p++;
640 n--;
641 p++;
642 }
643 while (p < EndOfData && *p != 0x7E && *p != 0x5E)
644 PrintToken(*p++);
645 if (*p == 0x5E) {
646 PendSpace = 1;
647 }
648 }
649
650 /*
651 * Version 0 is different
652 */
653
654 static int InventoryLower = 0;
655
656 static void PrintText0(unsigned char *p, int n)
657 {
658 if (p > EndOfData)
659 return;
660 unsigned char *t = NULL;
661 unsigned char c;
662 while (1) {
663 if (t == NULL)
664 t = TokenText(*p++);
665 c = *t & 0x7F;
666 if (c == 0x5E || c == 0x7E) {
667 if (n == 0) {
668 if (c == 0x5E) {
669 PendSpace = 1;
670 }
671 return;
672 }
673 n--;
674 } else if (n == 0) {
675 if (InventoryLower) {
676 c = tolower(c);
677 InventoryLower = 0;
678 }
679 OutChar(c);
680 }
681 if (t >= EndOfData || (*t++ & 0x80))
682 t = NULL;
683 }
684 }
685
686 static void PrintText(unsigned char *p, int n)
687 {
688 if (Version == REBEL_PLANET_TYPE) { /* In stream end markers */
689 PrintText0(p, n);
690 } else if (Version == QUESTPROBE3_TYPE) {
691 Q3PrintText(p, n);
692 } else { /* Out of stream end markers (faster) */
693 PrintText1(p, n);
694 }
695 }
696
697 static void Message(unsigned char m)
698 {
699 unsigned char *p = FileImage + MessageBase;
700 PrintText(p, m);
701 if (CurrentGame != BLIZZARD_PASS)
702 OutChar(' ');
703 if (BaseGame == REBEL_PLANET && m == 156)
704 InventoryLower = 1;
705 else
706 InventoryLower = 0;
707 }
708
709 static void Message2(unsigned int m)
710 {
711 unsigned char *p = FileImage + Message2Base;
712 PrintText(p, m);
713 OutChar(' ');
714 }
715
716 void SysMessage(unsigned char m)
717 {
718 if (Version != QUESTPROBE3_TYPE) {
719 Message(m);
720 return;
721 }
722 if (m == EXITS)
723 m = 217;
724 else
725 m = 210 + m;
726 Message(m);
727 }
728
729 static void PrintObject(unsigned char obj)
730 {
731 if (BaseGame == TEMPLE_OF_TERROR && obj == 41) {
732 OutString("door.");
733 return;
734 }
735 unsigned char *p = FileImage + ObjectBase;
736 if (Version == QUESTPROBE3_TYPE)
737 p--;
738 PrintText(p, obj);
739 }
740
741 static void PrintRoom(unsigned char room)
742 {
743 unsigned char *p = FileImage + RoomBase;
744 if (CurrentGame == BLIZZARD_PASS && room < 102)
745 p = FileImage + 0x18000 + FileBaselineOffset;
746 PrintText(p, room);
747 }
748
749 static void PrintNumber(unsigned char n)
750 {
751 char buf[4];
752 char *p = buf;
753 if (n == 0)
754 snprintf(buf, sizeof buf, "00");
755 else
756 snprintf(buf, sizeof buf, "%d", (int)n);
757
758 while (*p)
759 OutChar(*p++);
760 }
761
762 static unsigned char Destroyed()
763 {
764 return 252;
765 }
766
767 static unsigned char Carried()
768 {
769 return Flag[2];
770 }
771
772 static unsigned char Worn()
773 {
774 if (Version == QUESTPROBE3_TYPE)
775 return 0;
776 return Flag[3];
777 }
778
779 static unsigned char NumObjects()
780 {
781 if (Version == QUESTPROBE3_TYPE)
782 return 49;
783
784 /* This removes one weird empty "You notice." in Kayleth */
785 if (BaseGame == KAYLETH)
786 return 120;
787 return Flag[6];
788 }
789
790 static int WaitFlag()
791 {
792 if (Version == QUESTPROBE3_TYPE)
793 return 5;
794 else if (BaseGame != REBEL_PLANET && BaseGame != KAYLETH)
795 return -1;
796 else
797 return 7;
798 }
799
800 static int CarryItem(void)
801 {
802 if (Version == QUESTPROBE3_TYPE)
803 return 1;
804 /* Flag 5: items carried, flag 4: max carried */
805 if (ItemsCarried == MaxCarried && CurrentGame != BLIZZARD_PASS)
806 return 0;
807 if (ItemsCarried < 255)
808 ItemsCarried++;
809 return 1;
810 }
811
812 static int DarkFlag(void)
813 {
814 if (Version == QUESTPROBE3_TYPE)
815 return 43;
816 return 1;
817 }
818
819 static void DropItem(void)
820 {
821 if (Version != QUESTPROBE3_TYPE && ItemsCarried > 0)
822 ItemsCarried--;
823 }
824
825 static void Put(unsigned char obj, unsigned char loc)
826 {
827 /* Will need refresh logics somewhere, maybe here ? */
828 if (ObjectLoc[obj] == MyLoc || loc == MyLoc)
829 Redraw = 1;
830 ObjectLoc[obj] = loc;
831 }
832
833 static int Present(unsigned char obj)
834 {
835 if (obj >= NumObjects())
836 return 0;
837 unsigned char v = ObjectLoc[obj];
838 if (v == MyLoc || v == Worn() || v == Carried() || (Version == QUESTPROBE3_TYPE && v == OtherGuyInv && OtherGuyLoc == MyLoc))
839 return 1;
840 return 0;
841 }
842
843 static int Chance(int n)
844 {
845 unsigned long v = rand() >> 12;
846 v %= 100;
847 if (v > n)
848 return 0;
849 return 1;
850 }
851
852 void Look(void);
853
854 static void NewGame(void)
855 {
856 Redraw = 1;
857 memset(Flag, 0, 128);
858 memcpy(Flag, FileImage + FlagBase, 7);
859 if (Version == QUESTPROBE3_TYPE) {
860 for (int i = 0; i < 124; i++) {
861 Flag[4 + i] = 0;
862 }
863 Flag[42] = 0;
864 Flag[43] = 0;
865 Flag[2] = 254;
866 Flag[3] = 253;
867 }
868 MyLoc = 0;
869 memcpy(ObjectLoc, FileImage + ObjLocBase, NumObjects());
870 if (WaitFlag() != -1)
871 Flag[WaitFlag()] = 0;
872 Look();
873 PrintedOK = 1;
874 }
875
876 static int GetFileLength(strid_t stream)
877 {
878 glk_stream_set_position(stream, 0, seekmode_End);
879 return glk_stream_get_position(stream);
880 }
881
882 int YesOrNo(void)
883 {
884 while (1) {
885 uint8_t c = WaitCharacter();
886 if (c == 250)
887 c = 0;
888 OutChar(c);
889 OutChar('\n');
890 OutFlush();
891 if (c == 'n' || c == 'N')
892 return 0;
893 if (c == 'y' || c == 'Y')
894 return 1;
895 OutString("Please answer Y or N.\n");
896 OutFlush();
897 }
898 }
899
900 int LoadGame(void)
901 {
902 frefid_t fileref = glk_fileref_create_by_prompt(fileusage_SavedGame,
903 filemode_Read, 0);
904 if (!fileref) {
905 OutFlush();
906 return 0;
907 }
908
909 /*
910 * Reject the file reference if we're expecting to read from it, and the
911 * referenced file doesn't exist.
912 */
913 if (!glk_fileref_does_file_exist(fileref)) {
914 OutString("Unable to open file.\n");
915 glk_fileref_destroy(fileref);
916 OutFlush();
917 return 0;
918 }
919
920 strid_t stream = glk_stream_open_file(fileref, filemode_Read, 0);
921 if (!stream) {
922 OutString("Unable to open file.\n");
923 glk_fileref_destroy(fileref);
924 OutFlush();
925 return 0;
926 }
927
928 struct SavedState *state = SaveCurrentState();
929
930 /* Restore saved game data. */
931
932 if (glk_get_buffer_stream(stream, (char *)Flag, 128) != 128 || glk_get_buffer_stream(stream, (char *)ObjectLoc, 256) != 256 || GetFileLength(stream) != 384) {
933 RecoverFromBadRestore(state);
934 } else {
935 glk_window_clear(Bottom);
936 Look();
937 free(state);
938 InKaylethPreview = 0;
939 }
940 glk_stream_close(stream, NULL);
941 glk_fileref_destroy(fileref);
942 return 1;
943 }
944
945 static int LoadPrompt(void)
946 {
947 OutCaps();
948 glk_window_clear(Bottom);
949 SysMessage(RESUME_A_SAVED_GAME);
950 OutFlush();
951
952 if (!YesOrNo()) {
953 glk_window_clear(Bottom);
954 if (DeferredGoto == 1) {
955 MyLoc = 1;
956 DeferredGoto = 0;
957 }
958 return 0;
959 } else {
960 return LoadGame();
961 }
962 }
963
964 static int RecursionGuard = 0;
965
966 static void QuitGame(void)
967 {
968 if (LastChar == '\n')
969 OutReplace(' ');
970 OutFlush();
971 Look();
972 OutCaps();
973 SysMessage(PLAY_AGAIN);
974 OutFlush();
975 if (YesOrNo()) {
976 ShouldRestart = 1;
977 StopTime = 2;
978 return;
979 } else {
980 glk_exit();
981 }
982 }
983
984 static void Inventory(void)
985 {
986 int i;
987 int f = 0;
988 if (BaseGame != REBEL_PLANET)
989 OutCaps();
990 SysMessage(INVENTORY);
991 for (i = 0; i < NumObjects(); i++) {
992 if (ObjectLoc[i] == Carried() || ObjectLoc[i] == Worn()) {
993 f = 1;
994 PrintObject(i);
995 if (ObjectLoc[i] == Worn()) {
996 OutReplace(0);
997 SysMessage(NOWWORN);
998 if (CurrentGame == REBEL_PLANET) {
999 OutKillSpace();
1000 OutFlush();
1001 OutChar(',');
1002 }
1003 }
1004 }
1005 }
1006 if (f == 0)
1007 SysMessage(NOTHING);
1008 else {
1009 OutReplace('.');
1010 OutChar(' ');
1011 OutCaps();
1012 }
1013 }
1014
1015 static void AnyKey(void)
1016 {
1017 SysMessage(HIT_ENTER);
1018 OutFlush();
1019 WaitCharacter();
1020 }
1021
1022 static void Okay(void)
1023 {
1024 SysMessage(OKAY);
1025 OutChar(' ');
1026 OutCaps();
1027 PrintedOK = 1;
1028 }
1029
1030 void SaveGame(void)
1031 {
1032
1033 strid_t file;
1034 frefid_t ref;
1035
1036 ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame,
1037 filemode_Write, 0);
1038 if (ref == NULL) {
1039 OutString("Save failed.\n");
1040 OutFlush();
1041 return;
1042 }
1043
1044 file = glk_stream_open_file(ref, filemode_Write, 0);
1045 glk_fileref_destroy(ref);
1046 if (file == NULL) {
1047 OutString("Save failed.\n");
1048 OutFlush();
1049 return;
1050 }
1051
1052 /* Write game state. */
1053 glk_put_buffer_stream(file, (char *)Flag, 128);
1054 glk_put_buffer_stream(file, (char *)ObjectLoc, 256);
1055 glk_stream_close(file, NULL);
1056 OutString("Saved.\n");
1057 OutFlush();
1058 }
1059
1060 static void TakeAll(int start)
1061 {
1062 if (Flag[DarkFlag()]) {
1063 SysMessage(TOO_DARK_TO_SEE);
1064 return;
1065 }
1066 int found = 0;
1067 for (int i = start; i < NumObjects(); i++) {
1068 if (ObjectLoc[i] == MyLoc) {
1069 if (found)
1070 OutChar('\n');
1071 found = 1;
1072 PrintObject(i);
1073 OutReplace(0);
1074 OutString("......");
1075 if (CarryItem() == 0) {
1076 SysMessage(YOURE_CARRYING_TOO_MUCH);
1077 return;
1078 }
1079 OutKillSpace();
1080 OutString("Taken");
1081 OutFlush();
1082 Put(i, Carried());
1083 }
1084 }
1085 if (!found) {
1086 Message(31);
1087 }
1088 }
1089
1090 static void DropAll(int loud)
1091 {
1092 int i;
1093 int found = 0;
1094 for (i = 0; i < NumObjects(); i++) {
1095 if (ObjectLoc[i] == Carried() && ObjectLoc[i] != Worn()) {
1096 if (loud) {
1097 if (found)
1098 OutChar('\n');
1099 found = 1;
1100 PrintObject(i);
1101 OutReplace(0);
1102 OutString("......");
1103 OutKillSpace();
1104 OutString("Dropped");
1105 OutFlush();
1106 }
1107 Put(i, MyLoc);
1108 }
1109 }
1110 if (loud & !found) {
1111 OutString("You have nothing to drop. ");
1112 }
1113 ItemsCarried = 0;
1114 }
1115
1116 static int GetObject(unsigned char obj)
1117 {
1118 if (ObjectLoc[obj] == Carried() || ObjectLoc[obj] == Worn()) {
1119 SysMessage(YOU_HAVE_IT);
1120 return 0;
1121 }
1122 if (!(Version == QUESTPROBE3_TYPE && Flag[1] == MyLoc && ObjectLoc[obj] == Flag[3])) {
1123 if (ObjectLoc[obj] != MyLoc) {
1124 SysMessage(YOU_DONT_SEE_IT);
1125 return 0;
1126 }
1127 }
1128 if (CarryItem() == 0) {
1129 SysMessage(YOURE_CARRYING_TOO_MUCH);
1130 return 0;
1131 }
1132
1133 Put(obj, Carried());
1134 return 1;
1135 }
1136
1137 static int DropObject(unsigned char obj)
1138 {
1139 /* FIXME: check if this is how the real game behaves */
1140 if (ObjectLoc[obj] == Worn()) {
1141 SysMessage(YOU_ARE_WEARING_IT);
1142 return 0;
1143 }
1144 if (ObjectLoc[obj] != Carried()) {
1145 SysMessage(YOU_HAVENT_GOT_IT);
1146 return 0;
1147 }
1148
1149 DropItem();
1150 Put(obj, MyLoc);
1151 return 1;
1152 }
1153
1154 static void ListExits(int caps)
1155 {
1156 unsigned char locw = 0x80 | MyLoc;
1157 unsigned char *p;
1158 int f = 0;
1159 p = FileImage + ExitBase;
1160
1161 while (*p != locw)
1162 p++;
1163 p++;
1164 while (*p < 0x80) {
1165 if (f == 0) {
1166 if (CurrentGame == BLIZZARD_PASS && LastChar == ',')
1167 LastChar = 0;
1168 OutCaps();
1169 SysMessage(EXITS);
1170 }
1171 f = 1;
1172 if (caps)
1173 OutCaps();
1174 SysMessage(*p);
1175 p += 2;
1176 }
1177 if (f == 1) {
1178 OutReplace('.');
1179 OutChar('\n');
1180 }
1181 }
1182
1183 static void RunStatusTable(void);
1184 static void QP3DrawExtraImages(void);
1185
1186 void Look(void)
1187 {
1188 if (MyLoc == 0 || (BaseGame == KAYLETH && MyLoc == 91) || NoGraphics)
1189 CloseGraphicsWindow();
1190 else
1191 OpenGraphicsWindow();
1192 int i;
1193 int f = 0;
1194
1195 PendSpace = 0;
1196 if (!(CurrentWindow == Bottom && LineEvent))
1197 OutReset();
1198 TopWindow();
1199
1200 Redraw = 0;
1201 if (Transcript)
1202 glk_put_char_stream(Transcript, '\n');
1203
1204 OutCaps();
1205
1206 if (Flag[DarkFlag()]) {
1207 SysMessage(TOO_DARK_TO_SEE);
1208 OutString("\n\n");
1209 DrawBlack();
1210 BottomWindow();
1211 return;
1212 }
1213 if (BaseGame == REBEL_PLANET && MyLoc > 0)
1214 OutString("You are ");
1215 PrintRoom(MyLoc);
1216
1217 for (i = 0; i < NumLowObjects; i++) {
1218 if (ObjectLoc[i] == MyLoc) {
1219 if (f == 0) {
1220 if (Version == QUESTPROBE3_TYPE) {
1221 OutReplace(0);
1222 SysMessage(0);
1223 } else if (BaseGame == HEMAN || BaseGame == REBEL_PLANET) {
1224 OutChar(' ');
1225 }
1226 }
1227 f = 1;
1228 PendSpace = 1;
1229 PrintObject(i);
1230 }
1231 }
1232 if (f == 1 && !isalpha(LastChar))
1233 OutReplace('.');
1234
1235 if (Version == QUESTPROBE3_TYPE) {
1236 ListExits(1);
1237 } else {
1238 f = 0;
1239 for (; i < NumObjects(); i++) {
1240 if (ObjectLoc[i] == MyLoc) {
1241 if (f == 0) {
1242 /* Only the text-only and hybrid games */
1243 if (BaseGame == TEMPLE_OF_TERROR
1244 && CurrentGame != TEMPLE_OF_TERROR
1245 && CurrentGame != TEMPLE_OF_TERROR_64) {
1246 OutChar(' ');
1247 }
1248 SysMessage(YOU_SEE);
1249 if (CurrentGame == BLIZZARD_PASS) {
1250 PendSpace = 0;
1251 OutString(":- ");
1252 }
1253 if (Version == REBEL_PLANET_TYPE)
1254 OutReplace(0);
1255 }
1256 f = 1;
1257 PrintObject(i);
1258 }
1259 }
1260 if (f == 1)
1261 OutReplace('.');
1262 else
1263 OutChar('.');
1264 ListExits(BaseGame != TEMPLE_OF_TERROR && BaseGame != HEMAN);
1265 }
1266
1267 if (LastChar != '\n')
1268 OutChar('\n');
1269
1270 if ((Options & FORCE_INVENTORY) && !(MyLoc == 0 || (BaseGame == KAYLETH && MyLoc == 91))) {
1271 OutChar('\n');
1272 Inventory();
1273 OutChar('\n');
1274 }
1275
1276 OutChar('\n');
1277
1278 BottomWindow();
1279
1280 if (MyLoc != 0 && !NoGraphics && TAYLOR_GRAPHICS_ENABLED && Graphics) {
1281 if (Resizing) {
1282 DrawSagaPictureFromBuffer();
1283 return;
1284 }
1285 if (Version == QUESTPROBE3_TYPE) {
1286 int tempstop = StopTime;
1287 StopTime = 0;
1288 DrawImages = 255;
1289 RunStatusTable();
1290 QP3DrawExtraImages();
1291 StopTime = tempstop;
1292 } else {
1293 glk_window_clear(Graphics);
1294 DrawRoomImage();
1295 }
1296 }
1297 }
1298
1299 static void Goto(unsigned char loc)
1300 {
1301 if (BaseGame == QUESTPROBE3 && !PrintedOK)
1302 Okay();
1303 if (BaseGame == HEMAN && MyLoc == 0 && loc == 1) {
1304 DeferredGoto = 1;
1305 } else {
1306 MyLoc = loc;
1307 Redraw = 1;
1308 }
1309 }
1310
1311 static void Delay(unsigned char seconds)
1312 {
1313 OutChar(' ');
1314 OutFlush();
1315
1316 if (Options & NO_DELAYS)
1317 return;
1318
1319 glk_request_char_event(Bottom);
1320 glk_cancel_char_event(Bottom);
1321
1322 glk_request_timer_events(1000 * seconds);
1323
1324 event_t ev;
1325
1326 do {
1327 glk_select(&ev);
1328 Updates(ev);
1329 } while (ev.type != evtype_Timer);
1330
1331 glk_request_timer_events(AnimationRunning);
1332 }
1333
1334 static void Wear(unsigned char obj)
1335 {
1336 if (ObjectLoc[obj] == Worn()) {
1337 SysMessage(YOU_ARE_WEARING_IT);
1338 return;
1339 }
1340 if (ObjectLoc[obj] != Carried()) {
1341 SysMessage(YOU_HAVENT_GOT_IT);
1342 return;
1343 }
1344 DropItem();
1345 Put(obj, Worn());
1346 }
1347
1348 static void Remove(unsigned char obj)
1349 {
1350 if (ObjectLoc[obj] != Worn()) {
1351 SysMessage(YOU_ARE_NOT_WEARING_IT);
1352 return;
1353 }
1354 if (CarryItem() == 0) {
1355 SysMessage(YOURE_CARRYING_TOO_MUCH);
1356 return;
1357 }
1358 Put(obj, Carried());
1359 }
1360
1361 static void Means(unsigned char vb, unsigned char no)
1362 {
1363 Word[0] = vb;
1364 Word[1] = no;
1365 }
1366
1367 static void Q3SwitchInvFlags(unsigned char a, unsigned char b)
1368 {
1369 if (Flag[2] == a) {
1370 Flag[2] = b;
1371 Flag[3] = a;
1372 }
1373 }
1374
1375 static void Q3UpdateFlags(void)
1376 {
1377 if (ObjectLoc[7] == 253)
1378 ObjectLoc[7] = 254;
1379 if (IsThing) {
1380 if (ObjectLoc[2] == 0xfc) {
1381 /* If the "holding HUMAN TORCH by the hands" object is destroyed (i.e. not held) */
1382 /* the "location of the other guy" flag is set to the location of the Human Torch object */
1383 OtherGuyLoc = ObjectLoc[18];
1384 } else {
1385 OtherGuyLoc = MyLoc;
1386 }
1387 Q3SwitchInvFlags(253, 254);
1388 } else { /* I'm the HUMAN TORCH */
1389 if (ObjectLoc[1] == 0xfc) {
1390 /* If the "holding THING by the hands" object is destroyed (i.e. not held) */
1391 /* The "location of the other guy" flag is set to the location of the Thing object */
1392 OtherGuyLoc = ObjectLoc[17];
1393 } else {
1394 OtherGuyLoc = MyLoc;
1395 }
1396 Q3SwitchInvFlags(254, 253);
1397 }
1398
1399 /* Reset flag 39 when Xandu is knocked out */
1400 if (ObjectLoc[33] != 22)
1401 Flag[39] = 0;
1402 /* And set it when he is present */
1403 else if (Present(33))
1404 Flag[39] = 1;
1405
1406 /* Make sure that:
1407 - The watch isn't carried in the "intro"
1408 - That Thing has it when the game starts, no
1409 matter who we begin as
1410 */
1411 if (!Q3SwitchedWatch) {
1412 if (MyLoc == 6 || MyLoc == 0) {
1413 ObjectLoc[37] = 0xfc;
1414 } else {
1415 ObjectLoc[37] = 254;
1416 Q3SwitchedWatch = 1;
1417 }
1418 }
1419
1420 if (DrawImages)
1421 return;
1422
1423 TurnsLow++; /* Turns played % 100 */
1424 if (TurnsLow == 100) {
1425 TurnsHigh++; /* Turns divided by 100 */
1426 TurnsLow = 0;
1427 }
1428
1429 ThingAsphyx++; // Turns since Thing started holding breath
1430 if (ThingAsphyx == 0)
1431 ThingAsphyx = 0xff;
1432 TorchAsphyx++; // Turns since Torch started holding breath
1433 if (TorchAsphyx == 0)
1434 TorchAsphyx = 0xff;
1435 }
1436
1437 /* Questprobe 3 numbers the flags differently, so we have to offset them by 4 */
1438 static void Q3AdjustConditions(unsigned char op, unsigned char *arg1)
1439 {
1440 switch (op) {
1441 case ZERO:
1442 case NOTZERO:
1443 case LT:
1444 case GT:
1445 case EQ:
1446 case NE:
1447 *arg1 += 4;
1448 break;
1449 default:
1450 break;
1451 }
1452 }
1453
1454 static void Q3AdjustActions(unsigned char op, unsigned char *arg1, unsigned char *arg2)
1455 {
1456 switch (op) {
1457 case SET:
1458 case CLEAR:
1459 case LET:
1460 case ADD:
1461 case SUB:
1462 if (arg1 != NULL)
1463 *arg1 += 4;
1464 break;
1465 case SWAPF:
1466 if (arg1 != NULL)
1467 *arg1 += 4;
1468 if (arg2 != NULL)
1469 *arg2 += 4;
1470 break;
1471 default:
1472 break;
1473 }
1474 }
1475
1476 static int TwoConditionParameters()
1477 {
1478 if (Version == QUESTPROBE3_TYPE)
1479 return 16;
1480 else
1481 return 21;
1482 }
1483
1484 static int TwoActionParameters()
1485 {
1486 if (Version == QUESTPROBE3_TYPE)
1487 return 18;
1488 else
1489 return 22;
1490 }
1491
1492 static void ExecuteLineCode(unsigned char *p, int *done)
1493 {
1494 unsigned char arg1 = 0, arg2 = 0;
1495 int n;
1496 do {
1497 unsigned char op = *p;
1498
1499 if (op & 0x80)
1500 break;
1501 p++;
1502 arg1 = *p++;
1503
1504 #ifdef DEBUG
1505 if (Version == QUESTPROBE3_TYPE) {
1506 unsigned char debugarg1 = arg1;
1507 Q3AdjustConditions(Q3Condition[op], &debugarg1);
1508 fprintf(stderr, "%s %d ", Condition[Q3Condition[op]], debugarg1);
1509 } else {
1510 fprintf(stderr, "%s %d ", Condition[op], arg1);
1511 }
1512 #endif
1513 if (op >= TwoConditionParameters()) {
1514 arg2 = *p++;
1515 #ifdef DEBUG
1516 fprintf(stderr, "%d ", arg2);
1517 #endif
1518 }
1519
1520 if (Version == QUESTPROBE3_TYPE) {
1521 op = Q3Condition[op];
1522 Q3AdjustConditions(op, &arg1);
1523 }
1524
1525 switch (op) {
1526 case AT:
1527 if (MyLoc == arg1)
1528 continue;
1529 break;
1530 case NOTAT:
1531 if (MyLoc != arg1)
1532 continue;
1533 break;
1534 case ATGT:
1535 if (MyLoc > arg1)
1536 continue;
1537 break;
1538 case ATLT:
1539 if (MyLoc < arg1)
1540 continue;
1541 break;
1542 case PRESENT:
1543 if (Present(arg1))
1544 continue;
1545 break;
1546 case HERE:
1547 if (ObjectLoc[arg1] == MyLoc)
1548 continue;
1549 break;
1550 case ABSENT:
1551 if (!Present(arg1))
1552 continue;
1553 break;
1554 case NOTHERE:
1555 if (ObjectLoc[arg1] != MyLoc)
1556 continue;
1557 break;
1558 case CARRIED:
1559 if (ObjectLoc[arg1] == Carried() || ObjectLoc[arg1] == Worn())
1560 continue;
1561 break;
1562 case NOTCARRIED:
1563 if (ObjectLoc[arg1] != Carried() && ObjectLoc[arg1] != Worn())
1564 continue;
1565 break;
1566 case WORN:
1567 if (ObjectLoc[arg1] == Worn())
1568 continue;
1569 break;
1570 case NOTWORN:
1571 if (ObjectLoc[arg1] != Worn())
1572 continue;
1573 break;
1574 case NODESTROYED:
1575 if (ObjectLoc[arg1] != Destroyed())
1576 continue;
1577 break;
1578 case DESTROYED:
1579 if (ObjectLoc[arg1] == Destroyed())
1580 continue;
1581 break;
1582 case ZERO:
1583 if (BaseGame == TEMPLE_OF_TERROR) {
1584 /* Unless we have kicked sand in the eyes of the guard, tracked by flag 63, make sure he kills us if we try to pass, by setting flag 28 to zero */
1585 if (arg1 == 28 && Flag[63] == 0 && Word[0] == 20 && Word[1] == 162)
1586 Flag[28] = 0;
1587 }
1588 if (Flag[arg1] == 0)
1589 continue;
1590 break;
1591 case NOTZERO:
1592 if (Flag[arg1] != 0)
1593 continue;
1594 break;
1595 case WORD1:
1596 if (Word[2] == arg1)
1597 continue;
1598 break;
1599 case WORD2:
1600 if (Word[3] == arg1)
1601 continue;
1602 break;
1603 case WORD3:
1604 if (Word[4] == arg1)
1605 continue;
1606 break;
1607 case CHANCE:
1608 if (Chance(arg1))
1609 continue;
1610 break;
1611 case LT:
1612 if (Flag[arg1] < arg2)
1613 continue;
1614 break;
1615 case GT:
1616 if (Flag[arg1] > arg2)
1617 continue;
1618 break;
1619 case EQ:
1620 /* Fix final puzzle (Flag 12 conflict) */
1621 if (BaseGame == TEMPLE_OF_TERROR) {
1622 if (arg1 == 12 && arg2 == 4)
1623 arg1 = 60;
1624 }
1625 if (Flag[arg1] == arg2) {
1626 continue;
1627 }
1628 break;
1629 case NE:
1630 if (Flag[arg1] != arg2)
1631 continue;
1632 break;
1633 case OBJECTAT:
1634 if (ObjectLoc[arg1] == arg2)
1635 continue;
1636 break;
1637 default:
1638 fprintf(stderr, "Unknown condition %d.\n",
1639 op);
1640 break;
1641 }
1642 #ifdef DEBUG
1643 fprintf(stderr, "\n");
1644 #endif
1645 return;
1646 } while (1);
1647
1648 ActionsExecuted = 1;
1649
1650 do {
1651 unsigned char op = *p;
1652 if (!(op & 0x80))
1653 break;
1654
1655 #ifdef DEBUG
1656 if (op & 0x40)
1657 fprintf(stderr, "DONE:");
1658 if (Version == QUESTPROBE3_TYPE)
1659 fprintf(stderr, "%s(%d) ", Action[Q3Action[op & 0x3F]], op & 0x3F);
1660 else
1661 fprintf(stderr, "%s(%d) ", Action[op & 0x3F], op & 0x3F);
1662 #endif
1663
1664 p++;
1665 if (op & 0x40)
1666 *done = 1;
1667 op &= 0x3F;
1668
1669 if (op > 8) {
1670 arg1 = *p++;
1671 #ifdef DEBUG
1672 unsigned char debugarg1 = arg1;
1673 if (Version == QUESTPROBE3_TYPE)
1674 Q3AdjustActions(Q3Action[op], &debugarg1, NULL);
1675 fprintf(stderr, "%d ", debugarg1);
1676 #endif
1677 }
1678 if (op >= TwoActionParameters()) {
1679 arg2 = *p++;
1680 #ifdef DEBUG
1681 unsigned char debugarg2 = arg2;
1682 if (Version == QUESTPROBE3_TYPE)
1683 Q3AdjustActions(Q3Action[op], NULL, &debugarg2);
1684 fprintf(stderr, "%d ", debugarg2);
1685 #endif
1686 }
1687
1688 if (Version == QUESTPROBE3_TYPE) {
1689 op = Q3Action[op];
1690 Q3AdjustActions(op, &arg1, &arg2);
1691
1692 if (!PrintedOK)
1693 Okay();
1694 }
1695
1696 int WasDark = Flag[DarkFlag()];
1697
1698 switch (op) {
1699 case LOADPROMPT:
1700 if (LoadPrompt()) {
1701 *done = 1;
1702 return;
1703 }
1704 break;
1705 case QUIT:
1706 if (!RecursionGuard) {
1707 RecursionGuard = 1;
1708 QuitGame();
1709 }
1710 *done = 1;
1711 return;
1712 case SHOWINVENTORY:
1713 Inventory();
1714 break;
1715 case ANYKEY:
1716 AnyKey();
1717 break;
1718 case SAVE:
1719 StopTime = 1;
1720 SaveGame();
1721 break;
1722 case DROPALL:
1723 if ((BaseGame == REBEL_PLANET && (Word[0] != 20 || Word[1] != 141)) || (BaseGame == KAYLETH && (Word[0] != 20 || Word[1] != 254)))
1724 DropAll(0);
1725 else
1726 DropAll(1);
1727 break;
1728 case LOOK:
1729 Look();
1730 break;
1731 case PRINTOK:
1732 /* Guess */
1733 Okay();
1734 break;
1735 case GET:
1736 if (GetObject(arg1) == 0 && Version == QUESTPROBE3_TYPE)
1737 *done = 1;
1738 break;
1739 case DROP:
1740 if (DropObject(arg1) == 0 && BaseGame == REBEL_PLANET) {
1741 *done = 1;
1742 return;
1743 }
1744 break;
1745 case GOTO:
1746 /*
1747 He-Man moves the the player to a special "By the power of Grayskull" room
1748 and then issues an undo to return to the previous room.
1749 */
1750 if (BaseGame == HEMAN && arg1 == 83)
1751 SaveUndo();
1752 Goto(arg1);
1753 break;
1754 case GOBY:
1755 /* Blizzard pass era */
1756 if (Version == BLIZZARD_PASS_TYPE)
1757 Goto(ObjectLoc[arg1]);
1758 else
1759 Message2(arg1);
1760 break;
1761 case SET:
1762 Flag[arg1] = 255;
1763 break;
1764 case CLEAR:
1765 Flag[arg1] = 0;
1766 break;
1767 case MESSAGE:
1768 /* Prevent repeated "Blob returns to his post" messages */
1769 if (CurrentGame == QUESTPROBE3_64 && arg1 == 44)
1770 Flag[59] = 0;
1771 Message(arg1);
1772 if (CurrentGame == BLIZZARD_PASS && arg1 != 160)
1773 OutChar('\n');
1774 break;
1775 case CREATE:
1776 Put(arg1, MyLoc);
1777 break;
1778 case DESTROY:
1779 Put(arg1, Destroyed());
1780 break;
1781 case PRINT:
1782 PrintNumber(Flag[arg1]);
1783 break;
1784 case DELAY:
1785 Delay(arg1);
1786 break;
1787 case WEAR:
1788 Wear(arg1);
1789 break;
1790 case REMOVE:
1791 Remove(arg1);
1792 break;
1793 case LET:
1794 if (BaseGame == TEMPLE_OF_TERROR) {
1795 if (arg1 == 28 && arg2 == 2) {
1796 /* If the serpent guard is present, we have just kicked sand in his eyes. Set flag 63 to track this */
1797 Flag[63] = (ObjectLoc[48] == MyLoc);
1798 }
1799 }
1800 Flag[arg1] = arg2;
1801 break;
1802 case ADD:
1803 /* Fix final puzzle (Flag 12 conflict) */
1804 if (BaseGame == TEMPLE_OF_TERROR) {
1805 if (arg1 == 12 && arg2 == 1)
1806 arg1 = 60;
1807 }
1808 n = Flag[arg1] + arg2;
1809 if (n > 255)
1810 n = 255;
1811 Flag[arg1] = n;
1812 break;
1813 case SUB:
1814 n = Flag[arg1] - arg2;
1815 if (n < 0)
1816 n = 0;
1817 Flag[arg1] = n;
1818 break;
1819 case PUT:
1820 Put(arg1, arg2);
1821 break;
1822 case SWAP:
1823 n = ObjectLoc[arg1];
1824 Put(arg1, ObjectLoc[arg2]);
1825 Put(arg2, n);
1826 break;
1827 case SWAPF:
1828 n = Flag[arg1];
1829 Flag[arg1] = Flag[arg2];
1830 Flag[arg2] = n;
1831 break;
1832 case MEANS:
1833 Means(arg1, arg2);
1834 break;
1835 case PUTWITH:
1836 Put(arg1, ObjectLoc[arg2]);
1837 break;
1838 case BEEP:
1839 #if defined(GLK_MODULE_GARGLKBLEEP)
1840 garglk_zbleep(1 + (arg1 == 250));
1841 #elif defined(SPATTERLIGHT)
1842 fprintf(stderr, "BEEP: arg1: %d arg2: %d\n", arg1, arg2);
1843 win_beep(1 + (arg1 == 250));
1844 #else
1845 putchar('\007');
1846 fflush(stdout);
1847 #endif
1848 break;
1849 case REFRESH:
1850 if (BaseGame == KAYLETH)
1851 TakeAll(78);
1852 if (BaseGame == HEMAN)
1853 TakeAll(45);
1854 Redraw = 1;
1855 break;
1856 case RAMSAVE:
1857 RamSave(1);
1858 break;
1859 case RAMLOAD:
1860 RamLoad();
1861 break;
1862 case CLSLOW:
1863 OutFlush();
1864 glk_window_clear(Bottom);
1865 break;
1866 case OOPS:
1867 RestoreUndo(0);
1868 Redraw = 1;
1869 break;
1870 case DIAGNOSE:
1871 Message(223);
1872 char buf[5];
1873 char *q = buf;
1874 /* TurnsLow = turns % 100, TurnsHigh == turns / 100 */
1875 snprintf(buf, sizeof buf, "%04d", TurnsLow + TurnsHigh * 100);
1876 while (*q)
1877 OutChar(*q++);
1878 SysMessage(14);
1879 if (IsThing)
1880 /* THING is always 100 percent rested */
1881 OutString("100");
1882 else {
1883 /* Calculate "restedness" percentage */
1884 /* Flag[7] == 80 means 100 percent rested */
1885 q = buf;
1886 snprintf(buf, sizeof buf, "%d", (Flag[7] >> 2) + Flag[7]);
1887 while (*q)
1888 OutChar(*q++);
1889 }
1890 SysMessage(15);
1891 break;
1892 case SWITCHINVENTORY: {
1893 uint8_t temp = Flag[2]; /* Switch inventory */
1894 Flag[2] = OtherGuyInv;
1895 OtherGuyInv = temp;
1896 temp = Flag[42]; /* Switch dark flag */
1897 Flag[42] = Flag[43];
1898 Flag[43] = temp;
1899 Redraw = 1;
1900 break;
1901 }
1902 case SWITCHCHARACTER:
1903 /* Go to the location of the other guy */
1904 MyLoc = ObjectLoc[arg1];
1905 /* Pick him up, so that you don't see yourself */
1906 GetObject(arg1);
1907 Redraw = 1;
1908 break;
1909 case DONE:
1910 *done = 1;
1911 break;
1912 case IMAGE:
1913 if (!TAYLOR_GRAPHICS_ENABLED)
1914 break;
1915 if (MyLoc == 3 || Flag[DarkFlag()]) {
1916 DrawBlack();
1917 break;
1918 }
1919 if (arg1 == 0) {
1920 ClearGraphMem();
1921 DrawSagaPictureNumber(MyLoc - 1);
1922 } else if (arg1 == 45 && ObjectLoc[48] != MyLoc) {
1923 break;
1924 } else {
1925 DrawSagaPictureNumber(arg1 - 1);
1926 }
1927 DrawSagaPictureFromBuffer();
1928 break;
1929 default:
1930 fprintf(stderr, "Unknown command %d.\n", op);
1931 break;
1932 }
1933 if (WasDark != Flag[DarkFlag()])
1934 Redraw = 1;
1935 } while (1);
1936 #ifdef DEBUG
1937 fprintf(stderr, "\n");
1938 #endif
1939 }
1940
1941 static unsigned char *NextLine(unsigned char *p)
1942 {
1943 unsigned char op;
1944 while (!((op = *p) & 0x80)) {
1945 p += 2;
1946 if (op >= TwoConditionParameters())
1947 p++;
1948 }
1949 while (((op = *p) & 0x80)) {
1950 op &= 0x3F;
1951 p++;
1952 if (op > 8)
1953 p++;
1954 if (op >= TwoActionParameters())
1955 p++;
1956 }
1957 return p;
1958 }
1959
1960 /* Draw two images that are unused in the original game */
1961 static void QP3DrawExtraImages(void)
1962 {
1963 if (!TAYLOR_GRAPHICS_ENABLED)
1964 return;
1965 if (MyLoc == 34 && ObjectLoc[29] == 34) {
1966 PatchAndDrawQP3Cannon();
1967 } else if (MyLoc == 2 && ObjectLoc[17] == 2 && Flag[26] > 16 && Flag[26] < 20) {
1968 /* Draw close-up of Thing */
1969 DrawSagaPictureNumber(53);
1970 DrawSagaPictureFromBuffer();
1971 }
1972 }
1973
1974 static void RunStatusTable(void)
1975 {
1976 if (StopTime) {
1977 StopTime--;
1978 return;
1979 }
1980 unsigned char *p = FileImage + StatusBase;
1981
1982 int done = 0;
1983 ActionsExecuted = 0;
1984
1985 if (Version == QUESTPROBE3_TYPE) {
1986 Q3UpdateFlags();
1987 }
1988
1989 while (*p != 0x7F) {
1990 while (Version == QUESTPROBE3_TYPE && *p == 0x7e) {
1991 p++;
1992 }
1993 ExecuteLineCode(p, &done);
1994 if (done) {
1995 return;
1996 }
1997 p = NextLine(p);
1998 }
1999 if (Version == QUESTPROBE3_TYPE)
2000 DrawImages = 0;
2001 }
2002
2003 static void RunCommandTable(void)
2004 {
2005 unsigned char *p = FileImage + ActionBase;
2006
2007 int done = 0;
2008 ActionsExecuted = 0;
2009 FoundVerb = 0;
2010 FoundNoun = 0;
2011
2012 while (*p != 0x7F) {
2013
2014 if (p[0] == Word[0] || p[0] == Word[1])
2015 FoundVerb = 1;
2016 if (p[1] == Word[0] || p[1] == Word[1])
2017 FoundNoun = 1;
2018
2019 /* Match input to table entry as VERB NOUN or NOUN VERB */
2020 /* 126 is wildcard that matches any word */
2021 if (((*p == 126 || *p == Word[0]) && (p[1] == 126 || p[1] == Word[1])) ||
2022 ((*p == 126 || *p == Word[1]) && (p[1] == 126 || p[1] == Word[0]))) {
2023 #ifdef DEBUG
2024 PrintWord(p[0]);
2025 PrintWord(p[1]);
2026 #endif
2027 /* Work around a Questprobe 3 bug */
2028 if (Version == QUESTPROBE3_TYPE) {
2029 /* In great room, Xandu present */
2030 if (Present(33)) {
2031 Flag[39] = 1;
2032 Message(24);
2033 Goto(26);
2034 ActionsExecuted = 1;
2035 return;
2036 }
2037 }
2038 ExecuteLineCode(p + 2, &done);
2039 if (done)
2040 return;
2041 }
2042 p = NextLine(p + 2);
2043 }
2044 }
2045
2046 static int AutoExit(unsigned char v)
2047 {
2048 unsigned char *p = FileImage + ExitBase;
2049 unsigned char want = MyLoc | 0x80;
2050 while (*p != want) {
2051 if (*p == 0xFE)
2052 return 0;
2053 p++;
2054 }
2055 p++;
2056 while (*p < 0x80) {
2057 if (*p == v) {
2058 Goto(p[1]);
2059 return 1;
2060 }
2061 p += 2;
2062 }
2063 return 0;
2064 }
2065
2066 static int IsDir(unsigned char word)
2067 {
2068 if (word == 0)
2069 return 0;
2070 if (Version == QUESTPROBE3_TYPE) {
2071 return (word <= 4 || word == 57 || word == 60);
2072 } else
2073 return (word <= 10);
2074 }
2075
2076 extern int FoundExtraCommand;
2077
2078 static void RunOneInput(void)
2079 {
2080 PrintedOK = 0;
2081
2082 if (FoundExtraCommand) {
2083 if (TryExtraCommand()) {
2084 if (Redraw)
2085 Look();
2086 return;
2087 }
2088 }
2089 if (Word[0] == 0 && Word[1] == 0) {
2090 if (TryExtraCommand() == 0) {
2091 OutCaps();
2092 SysMessage(I_DONT_UNDERSTAND);
2093 StopTime = 2;
2094 } else {
2095 if (Redraw)
2096 Look();
2097 }
2098 return;
2099 }
2100 if (IsDir(Word[0]) || (Word[0] == GoVerb && IsDir(Word[1]))) {
2101 if (AutoExit(Word[0]) || AutoExit(Word[1])) {
2102 StopTime = 0;
2103 RunStatusTable();
2104 if (Redraw)
2105 Look();
2106 return;
2107 }
2108 }
2109
2110 /* Handle IT */
2111 if (Word[1] == 128)
2112 Word[1] = LastNoun;
2113 if (Word[1] != 0)
2114 LastNoun = Word[1];
2115
2116 OutCaps();
2117 RunCommandTable();
2118
2119 if (ActionsExecuted == 0) {
2120 int OriginalVerb = Word[0];
2121 if (TryExtraCommand() == 0) {
2122 if (LastVerb) {
2123 Word[4] = Word[3];
2124 Word[3] = Word[2];
2125 Word[2] = Word[1];
2126 Word[1] = Word[0];
2127 Word[0] = LastVerb;
2128 RunCommandTable();
2129 }
2130 if (ActionsExecuted == 0) {
2131 if (IsDir(OriginalVerb) || (Word[0] == GoVerb && IsDir(Word[1]))) {
2132 SysMessage(YOU_CANT_GO_THAT_WAY);
2133 } else if (FoundVerb) {
2134 SysMessage(THATS_BEYOND_MY_POWER);
2135 } else if (FoundNoun == 1) {
2136 SysMessage(I_DONT_UNDERSTAND_THAT_VERB);
2137 } else {
2138 SysMessage(I_DONT_UNDERSTAND);
2139 }
2140 OutFlush();
2141 StopTime = 1;
2142 return;
2143 }
2144 } else {
2145 if (Redraw)
2146 Look();
2147 return;
2148 }
2149 }
2150
2151 if (Word[0] != 0)
2152 LastVerb = Word[0];
2153
2154 if (Redraw && !((BaseGame == REBEL_PLANET && MyLoc == 250) || (BaseGame == KAYLETH && MyLoc == 15))) {
2155 Look();
2156 }
2157
2158 Redraw = 0;
2159
2160 if (WaitFlag() != -1 && Flag[WaitFlag()] > 1)
2161 Flag[WaitFlag()]++;
2162
2163 do {
2164 if (WaitFlag() != -1 && Flag[WaitFlag()]) {
2165 Flag[WaitFlag()]--;
2166 if (LastChar != '\n')
2167 OutChar('\n');
2168 }
2169
2170 if (Version == QUESTPROBE3_TYPE) {
2171 DrawImages = 0;
2172 RunStatusTable();
2173 DrawImages = 255;
2174 int tempstop = StopTime;
2175 StopTime = 0;
2176 RunStatusTable();
2177 QP3DrawExtraImages();
2178 StopTime = tempstop;
2179 } else {
2180 RunStatusTable();
2181 }
2182
2183 if (Redraw) {
2184 Look();
2185 }
2186 Redraw = 0;
2187
2188 } while (WaitFlag() != -1 && Flag[WaitFlag()] > 0);
2189 if (AnimationRunning)
2190 glk_request_timer_events(AnimationRunning);
2191 if (WaitFlag() != -1)
2192 Flag[WaitFlag()] = 0;
2193 }
2194
2195 void PrintFirstTenBytes(size_t offset)
2196 {
2197 fprintf(stderr, "\nFirst 10 bytes at 0x%04zx: ", offset);
2198 for (int i = 0; i < 10; i++)
2199 fprintf(stderr, "0x%02x ", FileImage[offset + i]);
2200 fprintf(stderr, "\n");
2201 }
2202
2203 static void FindTables(void)
2204 {
2205 TokenBase = FindTokens();
2206 RoomBase = Game->start_of_room_descriptions + FileBaselineOffset;
2207 ObjectBase = Game->start_of_item_descriptions + FileBaselineOffset;
2208 StatusBase = Game->start_of_automatics + FileBaselineOffset;
2209 ActionBase = Game->start_of_actions + FileBaselineOffset;
2210 ExitBase = Game->start_of_room_connections + FileBaselineOffset;
2211 FlagBase = FindFlags();
2212 ObjLocBase = Game->start_of_item_locations + FileBaselineOffset;
2213 MessageBase = Game->start_of_messages + FileBaselineOffset;
2214 Message2Base = Game->start_of_messages_2 + FileBaselineOffset;
2215
2216 if (BaseGame == KAYLETH) {
2217 AnimationData = FindCode("\xff\x00\x00\x00\x0f\x00\x5d\x0f\x00\x61", 0, 10);
2218 if (AnimationData == -1)
2219 AnimationData = FindCode("\xff\x00\x00\x15\x0f\x00\x5d\x0f\x00\x61", 0, 10);
2220 }
2221 }
2222
2223 /*
2224 * Version 0 is different
2225 */
2226
2227 static int GuessLowObjectEnd0(void)
2228 {
2229 unsigned char *p = FileImage + ObjectBase;
2230 unsigned char *t = NULL;
2231 unsigned char c = 0, lc;
2232 int n = 0;
2233
2234 while (p < EndOfData) {
2235 if (t == NULL)
2236 t = TokenText(*p++);
2237 lc = c;
2238 c = *t & 0x7F;
2239 if (c == 0x5E || c == 0x7E) {
2240 if (lc == ',' && n > 20)
2241 return n;
2242 n++;
2243 }
2244 if (*t++ & 0x80)
2245 t = NULL;
2246 }
2247 return -1;
2248 }
2249
2250 static int GuessLowObjectEnd(void)
2251 {
2252 unsigned char *p = FileImage + ObjectBase;
2253 unsigned char *x;
2254 int n = 0;
2255
2256 /* Can't automatically guess in this case */
2257 if (CurrentGame == BLIZZARD_PASS)
2258 return 70;
2259 else if (Version == QUESTPROBE3_TYPE)
2260 return 49;
2261
2262 if (Version == REBEL_PLANET_TYPE)
2263 return GuessLowObjectEnd0();
2264
2265 while (n < NumObjects()) {
2266 while (*p != 0x7E && *p != 0x5E) {
2267 p++;
2268 }
2269 x = TokenText(p[-1]);
2270 while (!(*x & 0x80)) {
2271 x++;
2272 }
2273 if ((*x & 0x7F) == ',')
2274 return n;
2275 n++;
2276 p++;
2277 }
2278 fprintf(stderr, "Unable to guess the last description object.\n");
2279 return 0;
2280 }
2281
2282 static void RestartGame(void)
2283 {
2284 RecursionGuard = 0;
2285 RestoreState(InitialState);
2286 JustStarted = 0;
2287 StopTime = 0;
2288 OutFlush();
2289 glk_window_clear(Bottom);
2290 Look();
2291 RunStatusTable();
2292 ShouldRestart = 0;
2293 Look();
2294 }
2295
2296 int glkunix_startup_code(glkunix_startup_t *data)
2297 {
2298 int argc = data->argc;
2299 char **argv = data->argv;
2300
2301 if (argc < 1)
2302 return 0;
2303
2304 if (argc > 1)
2305 while (argv[1]) {
2306 if (*argv[1] != '-')
2307 break;
2308 switch (argv[1][1]) {
2309 case 'n':
2310 Options |= NO_DELAYS;
2311 break;
2312 }
2313 argv++;
2314 argc--;
2315 }
2316
2317 if (argv[1] == NULL) {
2318 fprintf(stderr, "%s: <file>.\n", argv[0]);
2319 glk_exit();
2320 }
2321
2322 size_t namelen = strlen(argv[1]);
2323 Filename = MemAlloc(namelen + 1);
2324 strncpy(Filename, argv[1], namelen);
2325 Filename[namelen] = '\0';
2326
2327 FILE *f = fopen(Filename, "r");
2328 if (f == NULL) {
2329 perror(Filename);
2330 glk_exit();
2331 }
2332
2333 fseek(f, 0, SEEK_END);
2334 FileImageLen = ftell(f);
2335 if (FileImageLen == -1) {
2336 fclose(f);
2337 glk_exit();
2338 }
2339
2340 FileImage = MemAlloc(FileImageLen);
2341
2342 fseek(f, 0, SEEK_SET);
2343 if (fread(FileImage, 1, FileImageLen, f) != FileImageLen) {
2344 fprintf(stderr, "File read error!\n");
2345 }
2346
2347 FileImage = ProcessFile(FileImage, &FileImageLen);
2348
2349 EndOfData = FileImage + FileImageLen;
2350
2351 fclose(f);
2352
2353 #ifdef GARGLK
2354 garglk_set_program_name("TaylorMade 0.4");
2355 garglk_set_program_info("TaylorMade 0.4 by Alan Cox\n"
2356 "Glk port and graphics support by Petter Sjölund\n");
2357 const char *s;
2358 if ((s = strrchr(Filename, '/')) != NULL || (s = strrchr(Filename, '\\')) != NULL) {
2359 garglk_set_story_name(s + 1);
2360 } else {
2361 garglk_set_story_name(Filename);
2362 }
2363 #endif
2364
2365 return 1;
2366 }
2367
2368 #ifdef DEBUG
2369
2370 void PrintConditionAddresses(void)
2371 {
2372 fprintf(stderr, "Memory adresses of conditions\n\n");
2373 uint16_t conditionsOffsets = 0x56A8 + FileBaselineOffset;
2374 uint8_t *conditions;
2375 conditions = &FileImage[conditionsOffsets];
2376 for (int i = 1; i < 20; i++) {
2377 uint16_t address = *conditions++;
2378 address += *conditions * 256;
2379 conditions++;
2380 fprintf(stderr, "Condition %02d: 0x%04x (%s)\n", i, address, Condition[Q3Condition[i]]);
2381 }
2382 fprintf(stderr, "\n");
2383 }
2384
2385 void PrintActionAddresses(void)
2386 {
2387 fprintf(stderr, "Memory adresses of actions\n\n");
2388 uint16_t actionOffsets = 0x591C + FileBaselineOffset;
2389 uint8_t *actions;
2390 actions = &FileImage[actionOffsets];
2391 for (int i = 1; i < 24; i++) {
2392 uint16_t address = *actions++;
2393 address += *actions * 256;
2394 actions++;
2395 fprintf(stderr, " Action %02d: 0x%04x (%s)\n", i, address, Action[Q3Action[i]]);
2396 }
2397 fprintf(stderr, "\n");
2398 }
2399
2400 #endif
2401
2402 struct GameInfo *DetectGame(size_t LocalVerbBase)
2403 {
2404 struct GameInfo *LocalGame;
2405
2406 for (int i = 0; i < NUMGAMES; i++) {
2407 LocalGame = &games[i];
2408 FileBaselineOffset = (long)LocalVerbBase - (long)LocalGame->start_of_dictionary;
2409 long diff = FindTokens() - LocalVerbBase;
2410 if ((LocalGame->start_of_tokens - LocalGame->start_of_dictionary) == diff) {
2411 #ifdef DEBUG
2412 fprintf(stderr, "This is %s\n", LocalGame->Title);
2413 #endif
2414 return LocalGame;
2415 } else {
2416 #ifdef DEBUG
2417 fprintf(stderr, "Diff for game %s: %d. Looking for %ld\n", LocalGame->Title, LocalGame->start_of_tokens - LocalGame->start_of_dictionary, diff);
2418 #endif
2419 }
2420 }
2421 return NULL;
2422 }
2423
2424 static void UnparkFileImage(uint8_t *ParkedFile, size_t ParkedLength, long ParkedOffset, int FreeCompanion)
2425 {
2426 FileImage = ParkedFile;
2427 FileImageLen = ParkedLength;
2428 FileBaselineOffset = ParkedOffset;
2429 if (FreeCompanion)
2430 free(CompanionFile);
2431 }
2432
2433 static void LookForSecondTOTGame(void)
2434 {
2435 size_t namelen = strlen(Filename);
2436 char *secondfile = MemAlloc(namelen + 1);
2437 strncpy(secondfile, Filename, namelen);
2438 secondfile[namelen] = '\0';
2439
2440 char *period = strrchr(secondfile, '.');
2441 if (period == NULL)
2442 period = &secondfile[namelen - 1];
2443 else
2444 period--;
2445
2446 if (CurrentGame == TEMPLE_OF_TERROR)
2447 *period = 'b';
2448 else
2449 *period = 'a';
2450
2451 FILE *f = fopen(secondfile, "r");
2452 if (f == NULL)
2453 return;
2454
2455 fseek(f, 0, SEEK_END);
2456 size_t filelength = ftell(f);
2457 if (filelength == -1) {
2458 return;
2459 }
2460
2461 CompanionFile = MemAlloc(filelength);
2462
2463 fseek(f, 0, SEEK_SET);
2464 if (fread(CompanionFile, 1, filelength, f) != filelength) {
2465 fprintf(stderr, "File read error!\n");
2466 }
2467
2468 fclose(f);
2469
2470 uint8_t *ParkedFile = FileImage;
2471 size_t ParkedLength = FileImageLen;
2472 size_t ParkedOffset = FileBaselineOffset;
2473
2474 CompanionFile = ProcessFile(CompanionFile, &filelength);
2475
2476 FileImage = CompanionFile;
2477 FileImageLen = filelength;
2478
2479 size_t AltVerbBase = FindCode("NORT\001N", 0, 6);
2480
2481 if (AltVerbBase == -1) {
2482 UnparkFileImage(ParkedFile, ParkedLength, ParkedOffset, 1);
2483 return;
2484 }
2485
2486 struct GameInfo *AltGame = DetectGame(AltVerbBase);
2487
2488 if ((CurrentGame == TOT_TEXT_ONLY && AltGame->gameID != TEMPLE_OF_TERROR) || (CurrentGame == TEMPLE_OF_TERROR && AltGame->gameID != TOT_TEXT_ONLY)) {
2489 UnparkFileImage(ParkedFile, ParkedLength, ParkedOffset, 1);
2490 return;
2491 }
2492
2493 Display(Bottom, "Found files for both the text-only version and the graphics version of Temple of Terror.\n"
2494 "Would you like to use the longer texts from the text-only version along with the graphics from the other file? (Y/N) ");
2495 if (!YesOrNo()) {
2496 UnparkFileImage(ParkedFile, ParkedLength, ParkedOffset, 1);
2497 return;
2498 }
2499
2500 int index = 0;
2501
2502 if (CurrentGame == TOT_TEXT_ONLY) {
2503 while (Game->gameID != TOT_HYBRID) {
2504 Game = &games[index++];
2505 }
2506 SagaSetup();
2507 UnparkFileImage(ParkedFile, ParkedLength, ParkedOffset, 0);
2508 } else {
2509 UnparkFileImage(ParkedFile, ParkedLength, ParkedOffset, 0);
2510 SagaSetup();
2511 while (Game->gameID != TOT_HYBRID) {
2512 Game = &games[index++];
2513 }
2514 FileImage = CompanionFile;
2515 FileImageLen = filelength;
2516 VerbBase = AltVerbBase;
2517 }
2518
2519 EndOfData = FileImage + FileImageLen;
2520 }
2521
2522 void LoadKaylethAnimationData(void);
2523
2524 void glk_main(void)
2525 {
2526 if (DetectC64(&FileImage, &FileImageLen) != UNKNOWN_GAME) {
2527 EndOfData = FileImage + FileImageLen;
2528 } else {
2529 fprintf(stderr, "DetectC64 did not recognize the game\n");
2530 }
2531
2532 #ifdef DEBUG
2533 fprintf(stderr, "Loaded %zu bytes.\n", FileImageLen);
2534 #endif
2535
2536 VerbBase = FindCode("NORT\001N", 0, 6);
2537 if (VerbBase == -1) {
2538 fprintf(stderr, "No verb table!\n");
2539 glk_exit();
2540 }
2541
2542 if (!Game)
2543 Game = DetectGame(VerbBase);
2544 if (Game == NULL) {
2545 fprintf(stderr, "Did not recognize game!\n");
2546 glk_exit();
2547 } else {
2548 FileBaselineOffset = VerbBase - Game->start_of_dictionary;
2549 }
2550 #ifdef DEBUG
2551 fprintf(stderr, "FileBaselineOffset: %ld\n", FileBaselineOffset);
2552 #endif
2553
2554 #ifdef SPATTERLIGHT
2555 if (gli_determinism) {
2556 srand(1234);
2557 } else
2558 #endif
2559 srand((unsigned int)time(NULL));
2560
2561 DisplayInit();
2562
2563 if (CurrentGame == TEMPLE_OF_TERROR || CurrentGame == TOT_TEXT_ONLY) {
2564 LookForSecondTOTGame();
2565 }
2566
2567 GoVerb = ParseWord("GO");
2568
2569 SagaSetup();
2570
2571 if (CurrentGame == QUESTPROBE3 || CurrentGame == REBEL_PLANET)
2572 DelimiterChar = '=';
2573
2574 FindTables();
2575 #ifdef DEBUG
2576 if (Version != BLIZZARD_PASS_TYPE && Version != QUESTPROBE3_TYPE)
2577 Action[12] = "MESSAGE2";
2578 LoadWordTable();
2579 #endif
2580 NewGame();
2581 NumLowObjects = GuessLowObjectEnd();
2582 InitialState = SaveCurrentState();
2583
2584 RunStatusTable();
2585 if (Redraw) {
2586 OutFlush();
2587 Look();
2588 }
2589
2590 if (BaseGame == KAYLETH) {
2591 LoadKaylethAnimationData();
2592 InKaylethPreview = 1;
2593 }
2594
2595 while (1) {
2596 if (ShouldRestart) {
2597 RestartGame();
2598 if (BaseGame != KAYLETH)
2599 SaveUndo();
2600 } else if (!StopTime && !InKaylethPreview)
2601 SaveUndo();
2602 Parser();
2603 FirstAfterInput = 1;
2604 RunOneInput();
2605 if (StopTime) {
2606 StopTime--;
2607 } else if (!InKaylethPreview) {
2608 JustStarted = 0;
2609 }
2610 if (MyLoc == 1) {
2611 InKaylethPreview = 0;
2612 }
2613 }
2614 }