Debian Codegraph @

terps/scott/scott.c

raw symbols SCIP
1 /*
2 * ScottFree Revision 1.14
3 *
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 *
11 * You must have an ANSI C compiler to build this program.
12 */
13
14 /*
15 * Parts of this source file (mainly the Glk parts) are copyright 2011
16 * Chris Spiegel.
17 *
18 * Some notes about the Glk version:
19 *
20 * o Room descriptions, exits, and visible items can, as in the
21 * original, be placed in a window at the top of the screen, or can be
22 * inline with user input in the main window. The former is default,
23 * and the latter can be selected with the -w flag.
24 *
25 * o Game saving and loading uses Glk, which means that providing a
26 * save game file on the command-line will no longer work. Instead,
27 * the verb "restore" has been special-cased to call GameLoad(), which
28 * now prompts for a filename via Glk.
29 *
30 * o The local character set is expected to be compatible with ASCII, at
31 * least in the printable character range. Newlines are specially
32 * handled, however, and converted to Glk's expected newline
33 * character.
34 */
35
36 #include <ctype.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <strings.h>
42 #include <time.h>
43
44 #include "glk.h"
45 #include "glkstart.h"
46 #include "saga.h"
47 #include "sagagraphics.h"
48 #include "titleimage.h"
49
50 #include "detectgame.h"
51 #include "layouttext.h"
52 #include "line_drawing.h"
53 #include "restorestate.h"
54 #include "sagadraw.h"
55
56 #include "parser.h"
57 #include "ti99_4a_terp.h"
58
59 #include "apple2draw.h"
60 #include "game_specific.h"
61 #include "gremlins.h"
62 #include "hulk.h"
63 #include "robinofsherwood.h"
64 #include "seasofblood.h"
65
66 #include "bsd.h"
67 #include "scott.h"
68
69 #ifdef SPATTERLIGHT
70 #include "glkimp.h"
71 #endif
72
73 const char *game_file = NULL;
74 char *DirPath = ".";
75
76 Header GameHeader;
77 Item *Items;
78 Room *Rooms;
79 const char **Verbs;
80 const char **Nouns;
81 const char **Messages;
82 Action *Actions;
83 int LightRefill;
84 int Counters[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
85 0, 0, 0, 0, 0, 0, 0, 0 }; /* Range unknown */
86 int CurrentCounter;
87 int SavedRoom;
88 int RoomSaved[16] = { 0, 0, 0, 0, 0, 0, 0, 0,
89 0, 0, 0, 0, 0, 0, 0, 0 }; /* Range unknown */
90
91 long BitFlags = 0; /* Might be >32 flags - I haven't seen >32 yet */
92
93 int AutoInventory = 0;
94 int Options; /* Option flags set */
95 glui32 TopWidth; /* Terminal width */
96 glui32 TopHeight; /* Height of top window */
97 int ImageWidth = 255;
98 int ImageHeight = 96;
99 int file_baseline_offset = 0;
100 const char *title_screen = NULL;
101
102 struct Command *CurrentCommand = NULL;
103 struct GameInfo *Game;
104 MachineType CurrentSys = SYS_UNKNOWN;
105
106 extern const char *sysdict[MAX_SYSMESS];
107 extern const char *sysdict_i_am[MAX_SYSMESS];
108
109 const char *sys[MAX_SYSMESS];
110 const char *system_messages[60];
111
112 const char *battle_messages[33];
113 uint8_t enemy_table[126];
114
115 uint8_t *entire_file;
116 size_t file_length;
117
118 int AnimationFlag = 0;
119
120 int showing_inventory = 0;
121 int showing_closeup = 0;
122 int last_image_index = -1;
123 int lastwasnewline = 0;
124
125 extern struct SavedState *InitialState;
126
127 /* JustStarted is only used for the error message "Can't undo on first move" */
128 int JustStarted = 1;
129 static int should_restart = 0;
130 int StopTime = 0;
131
132 int should_look_in_transcript = 0;
133 static int print_look_to_transcript = 0;
134 static int pause_next_room_description = 0;
135
136 int split_screen = 1;
137 winid_t Bottom, Top;
138 winid_t Graphics;
139
140 strid_t Transcript = NULL;
141
142 int WeAreBigEndian = 0;
143
144 #define TRS80_LINE \
145 "\n<------------------------------------------------------------>\n"
146
147 //#define DEBUG_ACTIONS
148
149 static void RestartGame(void);
150 static int YesOrNo(void);
151
152 static int PerformActions(int vb, int no);
153
154 void Display(winid_t w, const char *fmt, ...)
155 {
156 va_list ap;
157 char msg[2048];
158
159 int size = sizeof msg;
160
161 va_start(ap, fmt);
162 vsnprintf(msg, size, fmt, ap);
163 va_end(ap);
164
165 int oldlastwasnewline = lastwasnewline;
166 glui32 *unistring = ToUnicode(msg);
167 if (w != Bottom)
168 lastwasnewline = oldlastwasnewline;
169 glk_put_string_stream_uni(glk_window_get_stream(w), unistring);
170 free(unistring);
171 }
172
173 void UpdateSettings(void)
174 {
175 #ifdef SPATTERLIGHT
176 if (gli_sa_delays)
177 Options &= ~NO_DELAYS;
178 else
179 Options |= NO_DELAYS;
180
181 switch (gli_sa_inventory) {
182 case 0:
183 Options &= ~(FORCE_INVENTORY | FORCE_INVENTORY_OFF);
184 break;
185 case 1:
186 Options = (Options | FORCE_INVENTORY) & ~FORCE_INVENTORY_OFF;
187 break;
188 case 2:
189 Options = (Options | FORCE_INVENTORY_OFF) & ~FORCE_INVENTORY;
190 break;
191 }
192
193 switch (gli_sa_palette) {
194 case 0:
195 Options &= ~(FORCE_PALETTE_ZX | FORCE_PALETTE_C64);
196 break;
197 case 1:
198 Options = (Options | FORCE_PALETTE_ZX) & ~FORCE_PALETTE_C64;
199 break;
200 case 2:
201 Options = (Options | FORCE_PALETTE_C64) & ~FORCE_PALETTE_ZX;
202 break;
203 }
204 #endif
205
206 if (DrawingVector())
207 glk_request_timer_events(20);
208
209 palette_type previous_pal = palchosen;
210 if (Options & FORCE_PALETTE_ZX)
211 palchosen = ZXOPT;
212 else if (Options & FORCE_PALETTE_C64) {
213 if (Game->picture_format_version == 99)
214 palchosen = C64A;
215 else
216 palchosen = C64B;
217 } else
218 palchosen = Game->palette;
219 if (palchosen != previous_pal) {
220 DefinePalette();
221 if (VectorState != NO_VECTOR_IMAGE)
222 DrawSomeVectorPixels(1);
223 }
224 }
225
226 static void PrintWindowDelimiter(void)
227 {
228 glk_window_get_size(Top, &TopWidth, &TopHeight);
229 glk_window_move_cursor(Top, 0, TopHeight - 1);
230 glk_stream_set_current(glk_window_get_stream(Top));
231 if (Options & SPECTRUM_STYLE)
232 for (unsigned i = 0; i < TopWidth; i++)
233 glk_put_char('*');
234 else {
235 glk_put_char('<');
236 for (unsigned i = 0; i < TopWidth - 2; i++)
237 glk_put_char('-');
238 glk_put_char('>');
239 }
240 }
241
242 static strid_t room_description_stream = NULL;
243
244 static void FlushRoomDescription(char *buf)
245 {
246 glk_stream_close(room_description_stream, 0);
247
248 if (Transcript && print_look_to_transcript) {
249 glui32 *unistring = ToUnicode(buf);
250 glk_put_string_stream_uni(Transcript, unistring);
251 free(unistring);
252 }
253
254 int print_delimiter = (Options & (TRS80_STYLE | SPECTRUM_STYLE | TI994A_STYLE));
255
256 if (split_screen) {
257 glk_window_clear(Top);
258 glk_window_get_size(Top, &TopWidth, &TopHeight);
259 int rows, length;
260 char *text_with_breaks = LineBreakText(buf, TopWidth, &rows, &length);
261
262 glui32 bottomheight;
263 glk_window_get_size(Bottom, NULL, &bottomheight);
264 winid_t o2 = glk_window_get_parent(Top);
265 if (!(bottomheight < 3 && TopHeight < rows)) {
266 glk_window_get_size(Top, &TopWidth, &TopHeight);
267 glk_window_set_arrangement(o2, winmethod_Above | winmethod_Fixed, rows,
268 Top);
269 } else {
270 print_delimiter = 0;
271 }
272
273 int line = 0;
274 int index = 0;
275 int i;
276 char string[2048];
277 if (TopWidth > 2047)
278 TopWidth = 2047;
279 for (line = 0; line < rows && index < length; line++) {
280 for (i = 0; i < TopWidth; i++) {
281 string[i] = text_with_breaks[index++];
282 if (string[i] == 10 || string[i] == 13 || index >= length)
283 break;
284 }
285 if (i < TopWidth + 1) {
286 string[i++] = '\n';
287 }
288 string[i] = 0;
289 if (strlen(string) == 0)
290 break;
291 glk_window_move_cursor(Top, 0, line);
292 Display(Top, "%s", string);
293 }
294
295 if (line < rows - 1) {
296 glk_window_get_size(Top, &TopWidth, &TopHeight);
297 glk_window_set_arrangement(o2, winmethod_Above | winmethod_Fixed,
298 MIN(rows - 1, TopHeight - 1), Top);
299 }
300
301 free(text_with_breaks);
302 } else {
303 Display(Bottom, "%s", buf);
304 }
305
306 if (print_delimiter) {
307 PrintWindowDelimiter();
308 }
309
310 if (pause_next_room_description) {
311 Delay(0.8);
312 pause_next_room_description = 0;
313 }
314
315 if (buf != NULL) {
316 free(buf);
317 buf = NULL;
318 }
319 }
320
321 static void UpdateUSInventory(void)
322 {
323 char *buf = MemAlloc(1000);
324 buf = memset(buf, 0, 1000);
325 room_description_stream = glk_stream_open_memory(buf, 1000, filemode_Write, 0);
326 ListInventory(1);
327 FlushRoomDescription(buf);
328 InventoryUS();
329 }
330
331 void Updates(event_t ev)
332 {
333 if (ev.type == evtype_Arrange) {
334 UpdateSettings();
335
336 VectorState = NO_VECTOR_IMAGE;
337
338 CloseGraphicsWindow();
339 OpenGraphicsWindow();
340
341 if (split_screen) {
342 if (showing_inventory == 1) {
343 UpdateUSInventory();
344 } else {
345 int lastimg = last_image_index;
346 Look();
347 last_image_index = lastimg;
348 if (showing_closeup) {
349 if (Game->type == US_VARIANT)
350 DrawUSRoom(last_image_index);
351 else
352 DrawImage(last_image_index);
353 }
354 }
355 }
356 } else if (ev.type == evtype_Timer) {
357 switch (Game->type) {
358 case SHERWOOD_VARIANT:
359 UpdateRobinOfSherwoodAnimations();
360 break;
361 case GREMLINS_VARIANT:
362 UpdateGremlinsAnimations();
363 break;
364 case SECRET_MISSION_VARIANT:
365 UpdateSecretAnimations();
366 break;
367 default:
368 if (Game->picture_format_version == 99 && DrawingVector())
369 DrawSomeVectorPixels((VectorState == NO_VECTOR_IMAGE));
370 break;
371 }
372 }
373 }
374
375 void Delay(float seconds)
376 {
377 if (Options & NO_DELAYS)
378 return;
379
380 event_t ev;
381
382 if (!glk_gestalt(gestalt_Timer, 0))
383 return;
384
385 glk_request_char_event(Bottom);
386 glk_cancel_char_event(Bottom);
387
388 if (DrawingVector()) {
389 do {
390 glk_select(&ev);
391 Updates(ev);
392 } while (DrawingVector());
393 #ifdef SPATTERLIGHT
394 if (gli_slowdraw)
395 seconds = 0.5;
396 #endif
397 }
398
399 glk_request_timer_events(1000 * seconds);
400
401 do {
402 glk_select(&ev);
403 Updates(ev);
404 } while (ev.type != evtype_Timer);
405
406 glk_request_timer_events(0);
407 }
408
409 winid_t FindGlkWindowWithRock(glui32 rock)
410 {
411 winid_t win;
412 glui32 rockptr;
413 for (win = glk_window_iterate(NULL, &rockptr); win;
414 win = glk_window_iterate(win, &rockptr)) {
415 if (rockptr == rock)
416 return win;
417 }
418 return 0;
419 }
420
421 void OpenTopWindow(void)
422 {
423 Top = FindGlkWindowWithRock(GLK_STATUS_ROCK);
424 if (Top == NULL) {
425 if (split_screen) {
426 Top = glk_window_open(Bottom, winmethod_Above | winmethod_Fixed,
427 TopHeight, wintype_TextGrid, GLK_STATUS_ROCK);
428 if (Top == NULL) {
429 split_screen = 0;
430 Top = Bottom;
431 } else {
432 glk_window_get_size(Top, &TopWidth, NULL);
433 }
434 } else {
435 Top = Bottom;
436 }
437 }
438 }
439
440 glui32 OptimalPictureSize(glui32 *width, glui32 *height)
441 {
442 *width = ImageWidth;
443 *height = ImageHeight;
444 int multiplier = 1;
445 glui32 graphwidth, graphheight;
446 glk_window_get_size(Graphics, &graphwidth, &graphheight);
447 multiplier = graphheight / ImageHeight;
448 if (255 * multiplier > graphwidth)
449 multiplier = graphwidth / ImageWidth;
450
451 if (multiplier == 0)
452 multiplier = 1;
453
454 *width = ImageWidth * multiplier;
455 *height = ImageHeight * multiplier;
456
457 return multiplier;
458 }
459
460 void OpenGraphicsWindow(void)
461 {
462 #ifdef SPATTERLIGHT
463 if (!gli_enable_graphics)
464 return;
465 #endif
466 glui32 graphwidth, graphheight, optimal_width, optimal_height;
467
468 if (Top == NULL)
469 Top = FindGlkWindowWithRock(GLK_STATUS_ROCK);
470 if (Graphics == NULL)
471 Graphics = FindGlkWindowWithRock(GLK_GRAPHICS_ROCK);
472 if (Graphics == NULL && Top != NULL) {
473 glk_window_get_size(Top, &TopWidth, &TopHeight);
474 glk_window_close(Top, NULL);
475 Graphics = glk_window_open(Bottom, winmethod_Above | winmethod_Proportional,
476 60, wintype_Graphics, GLK_GRAPHICS_ROCK);
477 glk_window_get_size(Graphics, &graphwidth, &graphheight);
478 pixel_size = OptimalPictureSize(&optimal_width, &optimal_height);
479 x_offset = ((int)graphwidth - (int)optimal_width) / 2;
480
481 if (graphheight > optimal_height) {
482 winid_t parent = glk_window_get_parent(Graphics);
483 if (parent)
484 glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed,
485 optimal_height, NULL);
486 }
487
488 /* Set the graphics window background to match
489 * the main window background, best as we can,
490 * and clear the window.
491 */
492 glui32 background_color;
493 if (Bottom && glk_style_measure(Bottom, style_Normal, stylehint_BackColor, &background_color)) {
494 glk_window_set_background_color(Graphics, background_color);
495 glk_window_clear(Graphics);
496 }
497
498 Top = glk_window_open(Bottom, winmethod_Above | winmethod_Fixed, TopHeight,
499 wintype_TextGrid, GLK_STATUS_ROCK);
500 glk_window_get_size(Top, &TopWidth, &TopHeight);
501 } else {
502 if (!Graphics)
503 Graphics = glk_window_open(Bottom, winmethod_Above | winmethod_Proportional, 60,
504 wintype_Graphics, GLK_GRAPHICS_ROCK);
505 glk_window_get_size(Graphics, &graphwidth, &graphheight);
506 pixel_size = OptimalPictureSize(&optimal_width, &optimal_height);
507 x_offset = (graphwidth - optimal_width) / 2;
508 winid_t parent = glk_window_get_parent(Graphics);
509 if (parent)
510 glk_window_set_arrangement(parent, winmethod_Above | winmethod_Fixed,
511 optimal_height, NULL);
512 }
513 right_margin = optimal_width + x_offset;
514 }
515
516 void CloseGraphicsWindow(void)
517 {
518 if (Graphics == NULL)
519 Graphics = FindGlkWindowWithRock(GLK_GRAPHICS_ROCK);
520 if (Graphics) {
521 glk_window_close(Graphics, NULL);
522 Graphics = NULL;
523 glk_window_get_size(Top, &TopWidth, &TopHeight);
524 }
525 }
526
527 static void CleanupAndExit(void)
528 {
529 if (Transcript)
530 glk_stream_close(Transcript, NULL);
531 if (DrawingVector()) {
532 #ifdef SPATTERLIGHT
533 gli_slowdraw = 0;
534 #endif
535 DrawSomeVectorPixels(0);
536 }
537 glk_exit();
538 }
539
540 void Fatal(const char *x)
541 {
542 Display(Bottom, "%s\n", x);
543 CleanupAndExit();
544 }
545
546 static void ClearScreen(void)
547 {
548 glk_window_clear(Bottom);
549 }
550
551 void *MemAlloc(int size)
552 {
553 void *t = (void *)malloc(size);
554 if (t == NULL)
555 Fatal("Out of memory");
556 return (t);
557 }
558
559 int RandomPercent(int n)
560 {
561 uint64_t rv = (uint64_t)rand() << 6;
562 rv &= 0xffffffff;
563 rv %= 100;
564 if (rv < n)
565 return (1);
566 return (0);
567 }
568
569 int CountCarried(void)
570 {
571 int ct = 0;
572 int n = 0;
573 while (ct <= GameHeader.NumItems) {
574 if (Items[ct].Location == CARRIED)
575 n++;
576 ct++;
577 }
578 return (n);
579 }
580
581 const char *MapSynonym(int noun)
582 {
583 int n = 1;
584 const char *tp;
585 static char lastword[16]; /* Last non synonym */
586 while (n <= GameHeader.NumWords) {
587 tp = Nouns[n];
588 if (*tp == '*')
589 tp++;
590 else
591 strncpy(lastword, tp, GameHeader.WordLength + 1);
592 if (n == noun)
593 return (lastword);
594 n++;
595 }
596 return (NULL);
597 }
598
599 static int MatchUpItem(int noun, int loc)
600 {
601 const char *word = MapSynonym(noun);
602 int ct = 0;
603
604 if (word == NULL)
605 word = Nouns[noun];
606
607 while (ct <= GameHeader.NumItems) {
608 if (Items[ct].AutoGet && (loc == 0 || Items[ct].Location == loc) && xstrncasecmp(Items[ct].AutoGet, word, GameHeader.WordLength) == 0)
609 return (ct);
610 ct++;
611 }
612 return (-1);
613 }
614
615 static char *ReadString(FILE *f)
616 {
617 char tmp[1024];
618 char *t;
619 int c, nc;
620 int ct = 0;
621 do {
622 c = fgetc(f);
623 } while (c != EOF && isspace((unsigned char)c));
624 if (c != '"') {
625 Fatal("Initial quote expected");
626 }
627 do {
628 c = fgetc(f);
629 if (c == EOF)
630 Fatal("EOF in string");
631 if (c == '"') {
632 nc = fgetc(f);
633 if (nc != '"') {
634 ungetc(nc, f);
635 break;
636 }
637 }
638 if (c == '`')
639 c = '"'; /* pdd */
640
641 /* Ensure a valid Glk newline is sent. */
642 if (c == '\n')
643 tmp[ct++] = 10;
644 /* Special case: assume CR is part of CRLF in a
645 * DOS-formatted file, and ignore it.
646 */
647 else if (c == 13)
648 ;
649 /* Pass only ASCII to Glk; the other reasonable option
650 * would be to pass Latin-1, but it's probably safe to
651 * assume that Scott Adams games are ASCII only.
652 */
653 else if ((c >= 32 && c <= 126))
654 tmp[ct++] = c;
655 else
656 tmp[ct++] = '?';
657 } while (1);
658 tmp[ct] = 0;
659 t = MemAlloc(ct + 1);
660 memcpy(t, tmp, ct + 1);
661 return (t);
662 }
663
664 size_t GetFileLength(FILE *in)
665 {
666 if (fseek(in, 0, SEEK_END) == -1) {
667 return 0;
668 }
669 size_t length = ftell(in);
670 if (length == -1) {
671 return 0;
672 }
673 fseek(in, SEEK_SET, 0);
674 return length;
675 }
676
677 int header[24];
678
679 //static int SanityCheckScottFreeHeader(int ni, int na, int nw, int nr, int mc)
680 //{
681 // int16_t v = header[1]; // items
682 // if (v < 10 || v > 500)
683 // return 0;
684 // v = header[2]; // actions
685 // if (v < 100 || v > 500)
686 // return 0;
687 // v = header[3]; // word pairs
688 // if (v < 50 || v > 200)
689 // return 0;
690 // v = header[4]; // Number of rooms
691 // if (v < 10 || v > 100)
692 // return 0;
693 // v = header[5]; // Number of Messages
694 // if (v < 10 || v > 255)
695 // return 0;
696 //
697 // return 1;
698 //}
699
700 void FreeDatabase(void)
701 {
702 free(Items);
703 free(Actions);
704 free(Verbs);
705 free(Nouns);
706 free(Rooms);
707 free(Messages);
708 }
709
710 GameIDType LoadDatabase(FILE *f, int loud)
711 {
712 int ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm;
713 int ct;
714 short lo;
715 Action *ap;
716 Room *rp;
717 Item *ip;
718 /* Load the header */
719
720 loud = 1;
721
722 if (fscanf(f, "%*d %d %d %d %d %d %d %d %d %d %d %d", &ni, &na, &nw, &nr, &mc,
723 &pr, &tr, &wl, &lt, &mn, &trm)
724 < 10) {
725 if (loud)
726 debug_print("Invalid database(bad header)\n");
727 return UNKNOWN_GAME;
728 }
729 GameHeader.NumItems = ni;
730 Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1));
731 GameHeader.NumActions = na;
732 Actions = (Action *)MemAlloc(sizeof(Action) * (na + 1));
733 GameHeader.NumWords = nw;
734 GameHeader.WordLength = wl;
735 Verbs = MemAlloc(sizeof(char *) * (nw + 2));
736 Nouns = MemAlloc(sizeof(char *) * (nw + 2));
737 GameHeader.NumRooms = nr;
738 Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1));
739 GameHeader.MaxCarry = mc;
740 GameHeader.PlayerRoom = pr;
741 GameHeader.Treasures = tr;
742 GameHeader.LightTime = lt;
743 LightRefill = lt;
744 GameHeader.NumMessages = mn;
745 Messages = MemAlloc(sizeof(char *) * (mn + 1));
746 GameHeader.TreasureRoom = trm;
747
748 if (loud) {
749 debug_print("Number of items: %d\n", GameHeader.NumItems);
750 debug_print("Number of actions: %d\n", GameHeader.NumActions);
751 debug_print("Number of words: %d\n", GameHeader.NumWords);
752 debug_print("Word length: %d\n", GameHeader.WordLength);
753 debug_print("Number of rooms: %d\n", GameHeader.NumRooms);
754 debug_print("Number of messages: %d\n", GameHeader.NumMessages);
755 debug_print("Max carried: %d\n", GameHeader.MaxCarry);
756 debug_print("Starting location: %d\n", GameHeader.PlayerRoom);
757 debug_print("Light time: %d\n", GameHeader.LightTime);
758 debug_print("Number of treasures: %d\n", GameHeader.Treasures);
759 debug_print("Treasure room: %d\n", GameHeader.TreasureRoom);
760 }
761
762 /* Load the actions */
763
764 ct = 0;
765 ap = Actions;
766 if (loud)
767 debug_print("Reading %d actions.\n", na);
768 while (ct < na + 1) {
769 if (fscanf(f, "%hu %hu %hu %hu %hu %hu %hu %hu",
770 &ap->Vocab,
771 &ap->Condition[0],
772 &ap->Condition[1],
773 &ap->Condition[2],
774 &ap->Condition[3],
775 &ap->Condition[4],
776 &ap->Subcommand[0],
777 &ap->Subcommand[1])
778 != 8) {
779 fprintf(stderr, "Bad action line (%d)\n", ct);
780 FreeDatabase();
781 return UNKNOWN_GAME;
782 }
783
784 if (loud) {
785 debug_print("Action %d Vocab: %d (%d/%d)\n", ct, ap->Vocab,
786 ap->Vocab % 150, ap->Vocab / 150);
787 debug_print("Action %d Condition[0]: %d (%d/%d)\n", ct,
788 ap->Condition[0], ap->Condition[0] % 20, ap->Condition[0] / 20);
789 debug_print("Action %d Condition[1]: %d (%d/%d)\n", ct,
790 ap->Condition[1], ap->Condition[1] % 20, ap->Condition[1] / 20);
791 debug_print("Action %d Condition[2]: %d (%d/%d)\n", ct,
792 ap->Condition[2], ap->Condition[2] % 20, ap->Condition[2] / 20);
793 debug_print("Action %d Condition[0]: %d (%d/%d)\n", ct,
794 ap->Condition[3], ap->Condition[3] % 20, ap->Condition[3] / 20);
795 debug_print("Action %d Condition[0]: %d (%d/%d)\n", ct,
796 ap->Condition[4], ap->Condition[4] % 20, ap->Condition[4] / 20);
797 debug_print("Action %d Subcommand [0]]: %d (%d/%d)\n", ct, ap->Subcommand[0], ap->Subcommand[0] % 150, ap->Subcommand[0] / 150);
798 debug_print("Action %d Subcommand [1]]: %d (%d/%d)\n", ct, ap->Subcommand[1], ap->Subcommand[1] % 150, ap->Subcommand[1] / 150);
799 }
800 ap++;
801 ct++;
802 }
803
804 ct = 0;
805 if (loud)
806 debug_print("Reading %d word pairs.\n", nw);
807 while (ct < nw + 1) {
808 Verbs[ct] = ReadString(f);
809 debug_print("Verbs %d:%s.\n", ct, Verbs[ct]);
810 Nouns[ct] = ReadString(f);
811 debug_print("Nouns %d:%s.\n", ct, Nouns[ct]);
812 ct++;
813 }
814 ct = 0;
815 rp = Rooms;
816 if (loud)
817 debug_print("Reading %d rooms.\n", nr);
818 while (ct < nr + 1) {
819 if (fscanf(f, "%hd %hd %hd %hd %hd %hd", &rp->Exits[0], &rp->Exits[1],
820 &rp->Exits[2], &rp->Exits[3], &rp->Exits[4],
821 &rp->Exits[5])
822 != 6) {
823 debug_print("Bad room line (%d)\n", ct);
824 FreeDatabase();
825 return UNKNOWN_GAME;
826 }
827
828 rp->Text = ReadString(f);
829 if (loud)
830 debug_print("Room %d: \"%s\"\n", ct, rp->Text);
831 if (loud) {
832 debug_print("Room connections for room %d:\n", ct);
833 for (int i = 0; i < 6; i++)
834 debug_print("Exit %d: %d\n", i, rp->Exits[i]);
835 }
836 rp->Image = 255;
837 ct++;
838 rp++;
839 }
840
841 ct = 0;
842 if (loud)
843 debug_print("Reading %d messages.\n", mn);
844 while (ct < mn + 1) {
845 Messages[ct] = ReadString(f);
846 if (loud)
847 debug_print("Message %d: \"%s\"\n", ct, Messages[ct]);
848 ct++;
849 }
850 ct = 0;
851 if (loud)
852 debug_print("Reading %d items.\n", ni);
853 ip = Items;
854 while (ct < ni + 1) {
855 ip->Text = ReadString(f);
856 if (loud)
857 debug_print("Item %d: \"%s\"\n", ct, ip->Text);
858 ip->AutoGet = strchr(ip->Text, '/');
859 /* Some games use // to mean no auto get/drop word! */
860 if (ip->AutoGet && strcmp(ip->AutoGet, "//") && strcmp(ip->AutoGet, "/*")) {
861 char *t;
862 *ip->AutoGet++ = 0;
863 t = strchr(ip->AutoGet, '/');
864 if (t != NULL)
865 *t = 0;
866 }
867 if (fscanf(f, "%hd", &lo) != 1) {
868 debug_print("Bad item line (%d)\n", ct);
869 FreeDatabase();
870 return UNKNOWN_GAME;
871 }
872 ip->Location = (unsigned char)lo;
873 if (loud)
874 debug_print("Location of item %d: %d\n", ct, ip->Location);
875 ip->InitialLoc = ip->Location;
876 ip++;
877 ct++;
878 }
879 ct = 0;
880 /* Discard Comment Strings */
881 while (ct < na + 1) {
882 free(ReadString(f));
883 ct++;
884 }
885 if (fscanf(f, "%d", &ct) != 1) {
886 debug_print("Cannot read version\n");
887 FreeDatabase();
888 return UNKNOWN_GAME;
889 }
890 if (loud)
891 debug_print("Version %d.%02d of Adventure ", ct / 100, ct % 100);
892 if (fscanf(f, "%d", &ct) != 1) {
893 debug_print("Cannot read adventure number\n");
894 FreeDatabase();
895 return UNKNOWN_GAME;
896 }
897 if (loud)
898 debug_print("%d.\nLoad Complete.\n\n", ct);
899 /* Extra value in at least Hulk */
900 if (!fscanf(f, "%d", &ct)) {
901 debug_print("No extra value in file. This is not Hulk.\n");
902 }
903
904 fclose(f);
905 if (ct == 703 && LoadDOSImages()) {
906 CurrentSys = SYS_MSDOS;
907 return HULK_US;
908 }
909 return SCOTTFREE;
910 }
911
912 void Output(const char *a) { Display(Bottom, "%s", a); }
913
914 void OutputNumber(int a) { Display(Bottom, "%d", a); }
915
916 #if defined(__clang__)
917 #pragma mark Room description
918 #endif
919
920 static int ItIsDark(void)
921 {
922 return ((BitFlags & (1 << DARKBIT)) && Items[LIGHT_SOURCE].Location != CARRIED && Items[LIGHT_SOURCE].Location != MyLoc);
923 }
924
925 void DrawBlack(void)
926 {
927 glk_window_fill_rect(Graphics, 0, x_offset, 0, 32 * 8 * (glui32)pixel_size,
928 12 * 8 * (glui32)pixel_size);
929 }
930
931 void DrawImage(int image)
932 {
933 #ifdef SPATTERLIGHT
934 if (!gli_enable_graphics)
935 return;
936 #endif
937 OpenGraphicsWindow();
938 if (Graphics == NULL) {
939 debug_print("DrawImage: Graphic window NULL?\n");
940 return;
941 }
942 if (Game->picture_format_version == 99)
943 DrawVectorPicture(image);
944 else
945 DrawSagaPictureNumber(image);
946 }
947
948 void DrawRoomImage(void)
949 {
950 if (CurrentGame == ADVENTURELAND || CurrentGame == ADVENTURELAND_C64) {
951 AdventurelandDarkness();
952 }
953
954 int dark = ItIsDark();
955
956 if (dark && Graphics != NULL && (Rooms[MyLoc].Image != 255 || Game->type == US_VARIANT)) {
957 vector_image_shown = -1;
958 VectorState = NO_VECTOR_IMAGE;
959 glk_request_timer_events(0);
960 if (Game->type == US_VARIANT) {
961 glk_window_clear(Graphics);
962 DrawUSRoom(0);
963 if (CurrentSys == SYS_APPLE2)
964 DrawApple2ImageFromVideoMem();
965 } else
966 DrawBlack();
967 return;
968 }
969
970 if (Game->type == US_VARIANT) {
971 LookUS();
972 return;
973 }
974
975 switch (CurrentGame) {
976 case SEAS_OF_BLOOD:
977 case SEAS_OF_BLOOD_C64:
978 SeasOfBloodRoomImage();
979 return;
980 case ROBIN_OF_SHERWOOD:
981 case ROBIN_OF_SHERWOOD_C64:
982 RobinOfSherwoodLook();
983 return;
984 case HULK:
985 case HULK_C64:
986 HulkLook();
987 return;
988 default:
989 break;
990 }
991
992 if (Rooms[MyLoc].Image == 255) {
993 CloseGraphicsWindow();
994 return;
995 }
996
997 if (dark)
998 return;
999
1000 if (Game->picture_format_version == 99) {
1001 DrawImage(MyLoc - 1);
1002 return;
1003 }
1004
1005 if (Game->type == GREMLINS_VARIANT) {
1006 GremlinsLook();
1007 } else {
1008 DrawImage(Rooms[MyLoc].Image & 127);
1009 }
1010 for (int ct = 0; ct <= GameHeader.NumItems; ct++)
1011 if (Items[ct].Image && Items[ct].Location == MyLoc) {
1012 if ((Items[ct].Flag & 127) == MyLoc) {
1013 DrawImage(Items[ct].Image);
1014 /* Draw the correct image of the bear on the beach */
1015 } else if (Game->type == SAVAGE_ISLAND_VARIANT && ct == 20 && MyLoc == 8) {
1016 DrawImage(9);
1017 }
1018 }
1019 }
1020
1021 static void WriteToRoomDescriptionStream(const char *fmt, ...)
1022 #ifdef __GNUC__
1023 __attribute__((__format__(__printf__, 1, 2)))
1024 #endif
1025 ;
1026
1027 static void WriteToRoomDescriptionStream(const char *fmt, ...)
1028 {
1029 if (room_description_stream == NULL)
1030 return;
1031 va_list ap;
1032 char msg[2048];
1033
1034 va_start(ap, fmt);
1035 vsnprintf(msg, sizeof msg, fmt, ap);
1036 va_end(ap);
1037
1038 glk_put_string_stream(room_description_stream, msg);
1039 }
1040
1041 static void ListExitsSpectrumStyle(void)
1042 {
1043 int ct = 0;
1044 int f = 0;
1045
1046 while (ct < 6) {
1047 if ((&Rooms[MyLoc])->Exits[ct] != 0) {
1048 if (f == 0) {
1049 if (!(Options & PC_STYLE))
1050 WriteToRoomDescriptionStream("\n\n");
1051 WriteToRoomDescriptionStream("%s", sys[EXITS]);
1052 } else {
1053 WriteToRoomDescriptionStream("%s", sys[EXITS_DELIMITER]);
1054 }
1055 /* sys[] begins with the exit names */
1056 WriteToRoomDescriptionStream("%s", sys[ct]);
1057 f = 1;
1058 }
1059 ct++;
1060 }
1061 WriteToRoomDescriptionStream("\n");
1062 return;
1063 }
1064
1065 static void ListExits(void)
1066 {
1067 int ct = 0;
1068 int f = 0;
1069
1070 WriteToRoomDescriptionStream("\n\n%s", sys[EXITS]);
1071
1072 while (ct < 6) {
1073 if ((&Rooms[MyLoc])->Exits[ct] != 0) {
1074 if (f) {
1075 WriteToRoomDescriptionStream("%s", sys[EXITS_DELIMITER]);
1076 }
1077 /* sys[] begins with the exit names */
1078 WriteToRoomDescriptionStream("%s", sys[ct]);
1079 f = 1;
1080 }
1081 ct++;
1082 }
1083 if (f == 0)
1084 WriteToRoomDescriptionStream("%s", sys[NONE]);
1085 return;
1086 }
1087
1088 static int ItemEndsWithPeriod(int item)
1089 {
1090 if (item < 0 || item > GameHeader.NumItems)
1091 return 0;
1092 const char *desc = Items[item].Text;
1093 if (desc != NULL && desc[0] != 0) {
1094 const char lastchar = desc[strlen(desc) - 1];
1095 if (lastchar == '.' || lastchar == '!') {
1096 return 1;
1097 }
1098 }
1099 return 0;
1100 }
1101
1102 void Look(void)
1103 {
1104 showing_inventory = 0;
1105 DrawRoomImage();
1106
1107 if (split_screen && Top == NULL)
1108 return;
1109
1110 char *buf = MemAlloc(1000);
1111 buf = memset(buf, 0, 1000);
1112 room_description_stream = glk_stream_open_memory(buf, 1000, filemode_Write, 0);
1113
1114 Room *r;
1115 int ct, f;
1116
1117 if (!split_screen) {
1118 WriteToRoomDescriptionStream("\n");
1119 } else if (Transcript && print_look_to_transcript) {
1120 glk_put_char_stream_uni(Transcript, 10);
1121 }
1122
1123 if (ItIsDark()) {
1124 WriteToRoomDescriptionStream("%s", sys[TOO_DARK_TO_SEE]);
1125 FlushRoomDescription(buf);
1126 return;
1127 }
1128
1129 r = &Rooms[MyLoc];
1130
1131 if (!r->Text)
1132 return;
1133 /* An initial asterisk means the room description should not */
1134 /* start with "You are" or equivalent */
1135 if (*r->Text == '*') {
1136 WriteToRoomDescriptionStream("%s", r->Text + 1);
1137 } else {
1138 WriteToRoomDescriptionStream("%s%s", sys[YOU_ARE], r->Text);
1139 }
1140
1141 if (!(Options & SPECTRUM_STYLE)) {
1142 ListExits();
1143 WriteToRoomDescriptionStream(".\n");
1144 }
1145
1146 ct = 0;
1147 f = 0;
1148 while (ct <= GameHeader.NumItems) {
1149 if (Items[ct].Location == MyLoc) {
1150 if (Items[ct].Text[0] == 0) {
1151 fprintf(stderr, "Invisible item in room: %d\n", ct);
1152 ct++;
1153 continue;
1154 }
1155 if (f == 0) {
1156 WriteToRoomDescriptionStream("%s", sys[YOU_SEE]);
1157 f++;
1158 if (Options & SPECTRUM_STYLE && !(Options & PC_STYLE))
1159 WriteToRoomDescriptionStream("\n");
1160 } else if (!(Options & (TRS80_STYLE | SPECTRUM_STYLE))) {
1161 WriteToRoomDescriptionStream("%s", sys[ITEM_DELIMITER]);
1162 }
1163 WriteToRoomDescriptionStream("%s", Items[ct].Text);
1164 if (Options & (TRS80_STYLE | SPECTRUM_STYLE)) {
1165 WriteToRoomDescriptionStream("%s", sys[ITEM_DELIMITER]);
1166 }
1167 }
1168 ct++;
1169 }
1170
1171 if ((Options & TI994A_STYLE) && f) {
1172 WriteToRoomDescriptionStream(".");
1173 } else if (Options & PC_STYLE && !f)
1174 WriteToRoomDescriptionStream(". ");
1175
1176 if (Options & SPECTRUM_STYLE) {
1177 ListExitsSpectrumStyle();
1178 } else if (f) {
1179 WriteToRoomDescriptionStream("\n");
1180 }
1181
1182 if ((AutoInventory || (Options & FORCE_INVENTORY)) && !(Options & FORCE_INVENTORY_OFF)) {
1183 WriteToRoomDescriptionStream("\n");
1184 ListInventory(1);
1185 }
1186
1187 FlushRoomDescription(buf);
1188 }
1189
1190 void SaveGame(void)
1191 {
1192 strid_t file;
1193 frefid_t ref;
1194 int ct;
1195 char buf[128];
1196
1197 ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame,
1198 filemode_Write, 0);
1199 if (ref == NULL)
1200 return;
1201
1202 file = glk_stream_open_file(ref, filemode_Write, 0);
1203 glk_fileref_destroy(ref);
1204 if (file == NULL)
1205 return;
1206
1207 for (ct = 0; ct < 16; ct++) {
1208 snprintf(buf, sizeof buf, "%d %d\n", Counters[ct], RoomSaved[ct]);
1209 glk_put_string_stream(file, buf);
1210 }
1211 snprintf(buf, sizeof buf, "%ld %d %hd %d %d %hd %d\n", BitFlags,
1212 (BitFlags & (1 << DARKBIT)) ? 1 : 0, MyLoc, CurrentCounter,
1213 SavedRoom, GameHeader.LightTime, AutoInventory);
1214 glk_put_string_stream(file, buf);
1215 for (ct = 0; ct <= GameHeader.NumItems; ct++) {
1216 snprintf(buf, sizeof buf, "%hd\n", (short)Items[ct].Location);
1217 glk_put_string_stream(file, buf);
1218 }
1219
1220 glk_stream_close(file, NULL);
1221 Output(sys[SAVED]);
1222 }
1223
1224 static void LoadGame(void)
1225 {
1226 strid_t file;
1227 frefid_t ref;
1228 char buf[128];
1229 int ct = 0;
1230 short lo;
1231 short DarkFlag;
1232
1233 int PreviousAutoInventory = AutoInventory;
1234
1235 ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_SavedGame,
1236 filemode_Read, 0);
1237 if (ref == NULL)
1238 return;
1239
1240 file = glk_stream_open_file(ref, filemode_Read, 0);
1241 glk_fileref_destroy(ref);
1242 if (file == NULL)
1243 return;
1244
1245 struct SavedState *state = SaveCurrentState();
1246
1247 int result;
1248
1249 for (ct = 0; ct < 16; ct++) {
1250 glk_get_line_stream(file, buf, sizeof buf);
1251 result = sscanf(buf, "%d %d", &Counters[ct], &RoomSaved[ct]);
1252 if (result != 2 || RoomSaved[ct] > GameHeader.NumRooms) {
1253 RecoverFromBadRestore(state);
1254 return;
1255 }
1256 }
1257 glk_get_line_stream(file, buf, sizeof buf);
1258 result = sscanf(buf, "%ld %hd %hd %d %d %hd %d\n", &BitFlags, &DarkFlag,
1259 &MyLoc, &CurrentCounter, &SavedRoom, &GameHeader.LightTime,
1260 &AutoInventory);
1261 if (result == 6)
1262 AutoInventory = PreviousAutoInventory;
1263 if ((result != 7 && result != 6) || MyLoc > GameHeader.NumRooms || MyLoc < 1 || SavedRoom > GameHeader.NumRooms) {
1264 RecoverFromBadRestore(state);
1265 return;
1266 }
1267
1268 /* Backward compatibility */
1269 if (DarkFlag)
1270 SetBitFlag(15);
1271 for (ct = 0; ct <= GameHeader.NumItems; ct++) {
1272 glk_get_line_stream(file, buf, sizeof buf);
1273 result = sscanf(buf, "%hd\n", &lo);
1274 Items[ct].Location = (unsigned char)lo;
1275 if (result != 1 || (Items[ct].Location > GameHeader.NumRooms && Items[ct].Location != CARRIED)) {
1276 RecoverFromBadRestore(state);
1277 return;
1278 }
1279 }
1280
1281 glui32 position = glk_stream_get_position(file);
1282 glk_stream_set_position(file, 0, seekmode_End);
1283 glui32 end = glk_stream_get_position(file);
1284 if (end != position) {
1285 RecoverFromBadRestore(state);
1286 return;
1287 }
1288
1289 SaveUndo();
1290 JustStarted = 0;
1291 StopTime = 1;
1292 should_look_in_transcript = 1;
1293 }
1294
1295 static void RestartGame(void)
1296 {
1297 if (CurrentCommand)
1298 FreeCommands();
1299 RestoreState(InitialState);
1300 JustStarted = 0;
1301 StopTime = 0;
1302 Display(Bottom, "\n\n");
1303 glk_window_clear(Bottom);
1304 OpenTopWindow();
1305 should_restart = 0;
1306 }
1307
1308 static void TranscriptOn(void)
1309 {
1310 frefid_t ref;
1311
1312 if (Transcript) {
1313 Output(sys[TRANSCRIPT_ALREADY]);
1314 return;
1315 }
1316
1317 ref = glk_fileref_create_by_prompt(fileusage_TextMode | fileusage_Transcript,
1318 filemode_Write, 0);
1319 if (ref == NULL)
1320 return;
1321
1322 Transcript = glk_stream_open_file_uni(ref, filemode_Write, 0);
1323
1324 glk_fileref_destroy(ref);
1325
1326 if (Transcript == NULL) {
1327 Output(sys[FAILED_TRANSCRIPT]);
1328 return;
1329 }
1330
1331 glui32 *start_of_transcript = ToUnicode(sys[TRANSCRIPT_START]);
1332 glk_put_string_stream_uni(Transcript, start_of_transcript);
1333 free(start_of_transcript);
1334
1335 print_look_to_transcript = 1;
1336 Look();
1337
1338 glk_put_string_stream(glk_window_get_stream(Bottom),
1339 (char *)sys[TRANSCRIPT_ON]);
1340 glk_window_set_echo_stream(Bottom, Transcript);
1341 }
1342
1343 static void TranscriptOff(void)
1344 {
1345 if (Transcript == NULL) {
1346 Output(sys[NO_TRANSCRIPT]);
1347 return;
1348 }
1349
1350 glk_window_set_echo_stream(Bottom, NULL);
1351
1352 glui32 *end_of_transcript = ToUnicode(sys[TRANSCRIPT_END]);
1353 glk_put_string_stream_uni(Transcript, end_of_transcript);
1354 free(end_of_transcript);
1355
1356 glk_stream_close(Transcript, NULL);
1357 Transcript = NULL;
1358 Output(sys[TRANSCRIPT_OFF]);
1359 }
1360
1361 static void FlickerOn(void)
1362 {
1363 if (Options & FLICKER_ON) {
1364 Output("Flicker is already on");
1365 } else {
1366 Output("Flicker is now on");
1367 }
1368 if (Options & NO_DELAYS)
1369 Output(" (but delays are off, so it won't have any effect.)");
1370 else Output(".");
1371 Output("\n");
1372 Options |= FLICKER_ON;
1373 }
1374
1375 static void FlickerOff(void)
1376 {
1377 if (Options & FLICKER_ON) {
1378 Output("Flicker is now off.");
1379 } else {
1380 Output("Flicker is already off.");
1381 }
1382 Options &= ~FLICKER_ON;
1383 }
1384
1385 int PerformExtraCommand(int extra_stop_time)
1386 {
1387 struct Command command = *CurrentCommand;
1388 int verb = command.verb;
1389 if (verb > GameHeader.NumWords)
1390 verb -= GameHeader.NumWords;
1391 int noun = command.noun;
1392 if (noun > GameHeader.NumWords)
1393 noun -= GameHeader.NumWords;
1394 else if (noun) {
1395 const char *NounWord = CharWords[CurrentCommand->nounwordindex];
1396 int newnoun = WhichWord(NounWord, ExtraNouns, strlen(NounWord),
1397 NUMBER_OF_EXTRA_NOUNS);
1398 newnoun = ExtraNounsKey[newnoun];
1399 if (newnoun)
1400 noun = newnoun;
1401 }
1402
1403 StopTime = 1 + extra_stop_time;
1404
1405 switch (verb) {
1406 case RESTORE:
1407 if (noun == 0 || noun == GAME) {
1408 LoadGame();
1409 return 1;
1410 }
1411 break;
1412 case RESTART:
1413 if (noun == 0 || noun == GAME) {
1414 Output(sys[ARE_YOU_SURE]);
1415 if (YesOrNo()) {
1416 should_restart = 1;
1417 }
1418 return 1;
1419 }
1420 break;
1421 case SAVE:
1422 if (noun == 0 || noun == GAME) {
1423 SaveGame();
1424 return 1;
1425 }
1426 break;
1427 case UNDO:
1428 if (noun == 0 || noun == COMMAND) {
1429 RestoreUndo();
1430 return 1;
1431 }
1432 break;
1433 case RAM:
1434 if (noun == RAMLOAD) {
1435 RamRestore();
1436 return 1;
1437 } else if (noun == RAMSAVE) {
1438 RamSave();
1439 return 1;
1440 }
1441 break;
1442 case RAMSAVE:
1443 if (noun == 0) {
1444 RamSave();
1445 return 1;
1446 }
1447 break;
1448 case RAMLOAD:
1449 if (noun == 0) {
1450 RamRestore();
1451 return 1;
1452 }
1453 break;
1454 case SCRIPT:
1455 if (noun == ON || noun == 0) {
1456 TranscriptOn();
1457 return 1;
1458 } else if (noun == OFF) {
1459 TranscriptOff();
1460 return 1;
1461 }
1462 break;
1463 case EXCEPT:
1464 FreeCommands();
1465 case FLICKER:
1466 if (noun == ON || noun == 0) {
1467 FlickerOn();
1468 return 1;
1469 } else if (noun == OFF) {
1470 FlickerOff();
1471 return 1;
1472 }
1473 break;
1474 }
1475
1476 StopTime = 0;
1477 return 0;
1478 }
1479
1480 static int YesOrNo(void)
1481 {
1482 glk_request_char_event(Bottom);
1483
1484 event_t ev;
1485 int result = 0;
1486 const char y = tolower((unsigned char)sys[YES][0]);
1487 const char n = tolower((unsigned char)sys[NO][0]);
1488
1489 do {
1490 glk_select(&ev);
1491 if (ev.type == evtype_CharInput) {
1492 glk_put_char_stream(glk_window_get_stream(Bottom), ev.val1);
1493 const char reply = tolower((char)ev.val1);
1494 if (reply == y) {
1495 result = 1;
1496 } else if (reply == n) {
1497 result = 2;
1498 } else {
1499 Output(sys[ANSWER_YES_OR_NO]);
1500 glk_request_char_event(Bottom);
1501 }
1502 } else
1503 Updates(ev);
1504 } while (result == 0);
1505
1506 return (result == 1);
1507 }
1508
1509 void HitEnter(void)
1510 {
1511 glk_request_char_event(Bottom);
1512
1513 event_t ev;
1514 int result = 0;
1515 do {
1516 glk_select(&ev);
1517 if (ev.type == evtype_CharInput) {
1518 if (ev.val1 == keycode_Return) {
1519 result = 1;
1520 } else {
1521 glk_request_char_event(Bottom);
1522 }
1523 } else
1524 Updates(ev);
1525 } while (result == 0);
1526 showing_closeup = 0;
1527 return;
1528 }
1529
1530 static void WriteToLowerWindow(const char *fmt, ...)
1531 {
1532 va_list ap;
1533 char msg[2048];
1534
1535 int size = sizeof msg;
1536
1537 va_start(ap, fmt);
1538 vsnprintf(msg, size, fmt, ap);
1539 va_end(ap);
1540
1541 glui32 *unistring = ToUnicode(msg);
1542 glk_put_string_stream_uni(glk_window_get_stream(Bottom), unistring);
1543 free(unistring);
1544 }
1545
1546 void ListInventory(int upper)
1547 {
1548 void (*print_function)(const char *fmt, ...);
1549 if (upper) {
1550 print_function = WriteToRoomDescriptionStream;
1551 } else {
1552 print_function = WriteToLowerWindow;
1553 }
1554
1555 int i = 0;
1556 int lastitem = -1;
1557 print_function("%s", sys[INVENTORY]);
1558 while (i <= GameHeader.NumItems) {
1559 if (Items[i].Location == CARRIED) {
1560 if (Items[i].Text[0] == 0) {
1561 fprintf(stderr, "Invisible item in inventory: %d\n", i);
1562 i++;
1563 continue;
1564 }
1565 if (lastitem > -1 && (Options & (TRS80_STYLE | SPECTRUM_STYLE)) == 0) {
1566 print_function("%s", sys[ITEM_DELIMITER]);
1567 }
1568 lastitem = i;
1569 print_function("%s", Items[i].Text);
1570 if (Options & (TRS80_STYLE | SPECTRUM_STYLE)) {
1571 print_function("%s", sys[ITEM_DELIMITER]);
1572 }
1573 }
1574 i++;
1575 }
1576 if (lastitem == -1)
1577 print_function("%s", sys[NOTHING]);
1578 else if (Options & TI994A_STYLE) {
1579 if (!ItemEndsWithPeriod(lastitem))
1580 print_function(".");
1581 print_function(" ");
1582 }
1583 if (upper) {
1584 WriteToRoomDescriptionStream("\n");
1585 } else if (Transcript) {
1586 glk_put_char_stream_uni(Transcript, 10);
1587 }
1588 }
1589
1590 static void LookWithPause(void)
1591 {
1592 char fc = Rooms[MyLoc].Text[0];
1593 if (Rooms[MyLoc].Text == NULL || MyLoc == 0 || fc == 0 || fc == '.' || fc == ' ')
1594 return;
1595 should_look_in_transcript = 1;
1596 pause_next_room_description = 1;
1597 Look();
1598 }
1599
1600 void DoneIt(void)
1601 {
1602 if (split_screen && Top)
1603 Look();
1604 Output("\n\n");
1605 Output(sys[PLAY_AGAIN]);
1606 Output("\n");
1607 if (YesOrNo()) {
1608 should_restart = 1;
1609 } else {
1610 CleanupAndExit();
1611 }
1612 }
1613
1614 int PrintScore(void)
1615 {
1616 int i = 0;
1617 int n = 0;
1618 while (i <= GameHeader.NumItems) {
1619 if (Items[i].Location == GameHeader.TreasureRoom && *Items[i].Text == '*')
1620 n++;
1621 i++;
1622 }
1623 Display(Bottom, "%s %d %s%s %d.\n", sys[IVE_STORED], n, sys[TREASURES],
1624 sys[ON_A_SCALE_THAT_RATES], (n * 100) / GameHeader.Treasures);
1625 if (n == GameHeader.Treasures) {
1626 Output(sys[YOUVE_SOLVED_IT]);
1627 DoneIt();
1628 return 1;
1629 }
1630 return 0;
1631 }
1632
1633 void PrintNoun(void)
1634 {
1635 if (CurrentCommand)
1636 glk_put_string_stream_uni(glk_window_get_stream(Bottom),
1637 UnicodeWords[CurrentCommand->nounwordindex]);
1638 }
1639
1640 void MoveItemAToLocOfItemB(int itemA, int itemB)
1641 {
1642 Items[itemA].Location = Items[itemB].Location;
1643 if (Items[itemB].Location == MyLoc)
1644 should_look_in_transcript = 1;
1645 }
1646
1647 void GoTo(int loc)
1648 {
1649 #ifdef DEBUG_ACTIONS
1650 debug_print("player location is now room %d (%s).\n", loc,
1651 Rooms[loc].Text);
1652 #endif
1653 int oldloc = MyLoc;
1654 MyLoc = loc;
1655 should_look_in_transcript = 1;
1656 Look();
1657 if (oldloc != MyLoc && (Options & FLICKER_ON))
1658 Delay(0.2);
1659 }
1660
1661 void GoToStoredLoc(void)
1662 {
1663 #ifdef DEBUG_ACTIONS
1664 debug_print("switch location to stored location (%d) (%s).\n",
1665 SavedRoom, Rooms[SavedRoom].Text);
1666 #endif
1667 int t = MyLoc;
1668 MyLoc = SavedRoom;
1669 SavedRoom = t;
1670 should_look_in_transcript = 1;
1671 }
1672
1673 void SetBitFlag(int bit) {
1674 #ifdef DEBUG_ACTIONS
1675 debug_print("Bitflag %d is set\n", bit);
1676 #endif
1677 BitFlags |= 1 << bit;
1678 }
1679
1680 void ClearBitFlag(int bit) {
1681 #ifdef DEBUG_ACTIONS
1682 debug_print("Bitflag %d is cleared\n", bit);
1683 #endif
1684 BitFlags &= ~(1 << bit);
1685 }
1686
1687 static void ChangeDarkness(int dark) {
1688 int was_dark = ItIsDark();
1689 if (dark) {
1690 SetBitFlag(DARKBIT);
1691 should_look_in_transcript = (should_look_in_transcript == 1 || was_dark == 0);
1692 } else {
1693 ClearBitFlag(DARKBIT);
1694 should_look_in_transcript = (should_look_in_transcript == 1 || was_dark == 1);
1695 }
1696 }
1697
1698 void SetDark(void) {
1699 ChangeDarkness(1);
1700 }
1701
1702 void SetLight(void) {
1703 ChangeDarkness(0);
1704 }
1705
1706 void SwapLocAndRoomflag(int index)
1707 {
1708 #ifdef DEBUG_ACTIONS
1709 debug_print("swap location<->roomflag[%d]\n", index);
1710 #endif
1711 int temp = MyLoc;
1712 MyLoc = RoomSaved[index];
1713 RoomSaved[index] = temp;
1714 should_look_in_transcript = 1;
1715 Look();
1716 }
1717
1718 void SwapItemLocations(int itemA, int itemB)
1719 {
1720 int temp = Items[itemA].Location;
1721 Items[itemA].Location = Items[itemB].Location;
1722 Items[itemB].Location = temp;
1723 if (Items[itemA].Location == MyLoc || Items[itemB].Location == MyLoc)
1724 should_look_in_transcript = 1;
1725 }
1726
1727 void PutItemAInRoomB(int itemA, int roomB)
1728 {
1729 #ifdef DEBUG_ACTIONS
1730 debug_print("Item %d (%s) is put in room %d (%s). MyLoc: %d (%s)\n",
1731 itemA, Items[itemA].Text, roomB, Rooms[roomB].Text, MyLoc,
1732 Rooms[MyLoc].Text);
1733 #endif
1734 if (Items[itemA].Location == MyLoc)
1735 LookWithPause();
1736 Items[itemA].Location = roomB;
1737 }
1738
1739 void SwapCounters(int index)
1740 {
1741 #ifdef DEBUG_ACTIONS
1742 debug_print(
1743 "Select a counter. Current counter is swapped with backup "
1744 "counter %d\n",
1745 index);
1746 #endif
1747 if (index > 15) {
1748 debug_print("ERROR! parameter out of range. Max 15, got %d\n", index);
1749 index = 15;
1750 }
1751 int temp = CurrentCounter;
1752
1753 CurrentCounter = Counters[index];
1754 Counters[index] = temp;
1755 #ifdef DEBUG_ACTIONS
1756 debug_print("Value of new selected counter is %d\n",
1757 CurrentCounter);
1758 #endif
1759 }
1760
1761 void PrintMessage(int index)
1762 {
1763 #ifdef DEBUG_ACTIONS
1764 debug_print("Print message %d: \"%s\"\n", index,
1765 Messages[index]);
1766 #endif
1767 const char *message = Messages[index];
1768 if (message != NULL && message[0] != 0) {
1769 Output(message);
1770 const char lastchar = message[strlen(message) - 1];
1771 if (lastchar != 13 && lastchar != 10)
1772 Output(sys[MESSAGE_DELIMITER]);
1773 }
1774 }
1775
1776 void PlayerIsDead(void)
1777 {
1778 #ifdef DEBUG_ACTIONS
1779 debug_print("Player is dead\n");
1780 #endif
1781 Output(sys[IM_DEAD]);
1782 SetLight();
1783 MyLoc = GameHeader.NumRooms; /* It seems to be what the code says! */
1784 }
1785
1786 static ActionResultType PerformLine(int ct)
1787 {
1788 #ifdef DEBUG_ACTIONS
1789 debug_print("Performing line %d: ", ct);
1790 #endif
1791 int continuation = 0, dead = 0;
1792 int param[5], pptr = 0;
1793 int p;
1794 int act[4];
1795 int cc = 0;
1796 while (cc < 5) {
1797 int cv, dv;
1798 cv = Actions[ct].Condition[cc];
1799 dv = cv / 20;
1800 cv %= 20;
1801 #ifdef DEBUG_ACTIONS
1802 debug_print("Testing condition %d: ", cv);
1803 #endif
1804 switch (cv) {
1805 case 0:
1806 param[pptr++] = dv;
1807 break;
1808 case 1:
1809 #ifdef DEBUG_ACTIONS
1810 debug_print("Does the player carry %s?\n", Items[dv].Text);
1811 #endif
1812 if (Items[dv].Location != CARRIED)
1813 return ACT_FAILURE;
1814 break;
1815 case 2:
1816 #ifdef DEBUG_ACTIONS
1817 debug_print("Is %s in location?\n", Items[dv].Text);
1818 #endif
1819 if (Items[dv].Location != MyLoc)
1820 return ACT_FAILURE;
1821 break;
1822 case 3:
1823 #ifdef DEBUG_ACTIONS
1824 debug_print("Is %s held or in location?\n", Items[dv].Text);
1825 #endif
1826 if (Items[dv].Location != CARRIED && Items[dv].Location != MyLoc)
1827 return ACT_FAILURE;
1828 break;
1829 case 4:
1830 #ifdef DEBUG_ACTIONS
1831 debug_print("Is location %s?\n", Rooms[dv].Text);
1832 #endif
1833 if (MyLoc != dv)
1834 return ACT_FAILURE;
1835 break;
1836 case 5:
1837 #ifdef DEBUG_ACTIONS
1838 debug_print("Is %s NOT in location?\n", Items[dv].Text);
1839 #endif
1840 if (Items[dv].Location == MyLoc)
1841 return ACT_FAILURE;
1842 break;
1843 case 6:
1844 #ifdef DEBUG_ACTIONS
1845 debug_print("Does the player NOT carry %s?\n", Items[dv].Text);
1846 #endif
1847 if (Items[dv].Location == CARRIED)
1848 return ACT_FAILURE;
1849 break;
1850 case 7:
1851 #ifdef DEBUG_ACTIONS
1852 debug_print("Is location NOT %s?\n", Rooms[dv].Text);
1853 #endif
1854 if (MyLoc == dv)
1855 return ACT_FAILURE;
1856 break;
1857 case 8:
1858 #ifdef DEBUG_ACTIONS
1859 debug_print("Is bitflag %d set?\n", dv);
1860 #endif
1861 if ((BitFlags & (1 << dv)) == 0)
1862 return ACT_FAILURE;
1863 break;
1864 case 9:
1865 #ifdef DEBUG_ACTIONS
1866 debug_print("Is bitflag %d NOT set?\n", dv);
1867 #endif
1868 if (BitFlags & (1 << dv))
1869 return ACT_FAILURE;
1870 break;
1871 case 10:
1872 #ifdef DEBUG_ACTIONS
1873 debug_print("Does the player carry anything?\n");
1874 #endif
1875 if (CountCarried() == 0)
1876 return ACT_FAILURE;
1877 break;
1878 case 11:
1879 #ifdef DEBUG_ACTIONS
1880 debug_print("Does the player carry nothing?\n");
1881 #endif
1882 if (CountCarried())
1883 return ACT_FAILURE;
1884 break;
1885 case 12:
1886 #ifdef DEBUG_ACTIONS
1887 debug_print("Is %s neither carried nor in room?\n", Items[dv].Text);
1888 #endif
1889 if (Items[dv].Location == CARRIED || Items[dv].Location == MyLoc)
1890 return ACT_FAILURE;
1891 break;
1892 case 13:
1893 if (dv > GameHeader.NumItems + 1)
1894 Fatal("Broken database!");
1895 #ifdef DEBUG_ACTIONS
1896 debug_print("Is %s (%d) in play?\n", Items[dv].Text, dv);
1897 #endif
1898 if (Items[dv].Location == 0)
1899 return ACT_FAILURE;
1900 break;
1901 case 14:
1902 #ifdef DEBUG_ACTIONS
1903 debug_print("Is %s NOT in play?\n", Items[dv].Text);
1904 #endif
1905 if (Items[dv].Location)
1906 return ACT_FAILURE;
1907 break;
1908 case 15:
1909 #ifdef DEBUG_ACTIONS
1910 debug_print("Is CurrentCounter <= %d?\n", dv);
1911 #endif
1912 if (CurrentCounter > dv)
1913 return ACT_FAILURE;
1914 break;
1915 case 16:
1916 #ifdef DEBUG_ACTIONS
1917 debug_print("Is CurrentCounter > %d?\n", dv);
1918 #endif
1919 if (CurrentCounter <= dv)
1920 return ACT_FAILURE;
1921 break;
1922 case 17:
1923 #ifdef DEBUG_ACTIONS
1924 debug_print("Is %s still in initial room?\n", Items[dv].Text);
1925 #endif
1926 if (Items[dv].Location != Items[dv].InitialLoc)
1927 return ACT_FAILURE;
1928 break;
1929 case 18:
1930 #ifdef DEBUG_ACTIONS
1931 debug_print("Has %s been moved?\n", Items[dv].Text);
1932 #endif
1933 if (Items[dv].Location == Items[dv].InitialLoc)
1934 return ACT_FAILURE;
1935 break;
1936 case 19: /* Only seen in Brian Howarth games so far */
1937 #ifdef DEBUG_ACTIONS
1938 debug_print("Is current counter == %d?\n", dv);
1939 if (CurrentCounter != dv)
1940 debug_print("Nope, current counter is %d\n", CurrentCounter);
1941 #endif
1942 if (CurrentCounter != dv)
1943 return ACT_FAILURE;
1944 break;
1945 }
1946 #ifdef DEBUG_ACTIONS
1947 debug_print("YES\n");
1948 #endif
1949 cc++;
1950 }
1951 #if defined(__clang__)
1952 #pragma mark Subcommands
1953 #endif
1954
1955 /* Actions */
1956 act[0] = Actions[ct].Subcommand[0];
1957 act[2] = Actions[ct].Subcommand[1];
1958 act[1] = act[0] % 150;
1959 act[3] = act[2] % 150;
1960 act[0] /= 150;
1961 act[2] /= 150;
1962 cc = 0;
1963 pptr = 0;
1964 while (cc < 4) {
1965 #ifdef DEBUG_ACTIONS
1966 debug_print("Performing action %d: ", act[cc]);
1967 #endif
1968 if (act[cc] >= 1 && act[cc] < 52) {
1969 PrintMessage(act[cc]);
1970 } else if (act[cc] > 101) {
1971 PrintMessage(act[cc] - 50);
1972 } else
1973 switch (act[cc]) {
1974 case 0: /* NOP */
1975 break;
1976 case 52:
1977 if (CountCarried() >= GameHeader.MaxCarry) {
1978 Output(sys[YOURE_CARRYING_TOO_MUCH]);
1979 return ACT_SUCCESS;
1980 }
1981 Items[param[pptr++]].Location = CARRIED;
1982 break;
1983 case 53:
1984 #ifdef DEBUG_ACTIONS
1985 debug_print("item %d (\"%s\") is now in location.\n", param[pptr],
1986 Items[param[pptr]].Text);
1987 #endif
1988 Items[param[pptr++]].Location = MyLoc;
1989 should_look_in_transcript = 1;
1990 break;
1991 case 54:
1992 GoTo(param[pptr++]);
1993 break;
1994 case 55:
1995 #ifdef DEBUG_ACTIONS
1996 debug_print(
1997 "Item %d (%s) is removed from the game (put in room 0).\n",
1998 param[pptr], Items[param[pptr]].Text);
1999 #endif
2000 Items[param[pptr++]].Location = 0;
2001 break;
2002 case 56:
2003 SetDark();
2004 break;
2005 case 57:
2006 SetLight();
2007 break;
2008 case 58:
2009 SetBitFlag(param[pptr++]);
2010 break;
2011 case 59:
2012 #ifdef DEBUG_ACTIONS
2013 debug_print("Item %d (%s) is removed from play.\n", param[pptr],
2014 Items[param[pptr]].Text);
2015 #endif
2016 Items[param[pptr++]].Location = 0;
2017 break;
2018 case 60:
2019 ClearBitFlag(param[pptr++]);
2020 break;
2021 case 61:
2022 PlayerIsDead();
2023 break;
2024 case 62:
2025 p = param[pptr++];
2026 PutItemAInRoomB(p, param[pptr++]);
2027 break;
2028 case 63:
2029 #ifdef DEBUG_ACTIONS
2030 debug_print("Game over.\n");
2031 #endif
2032 DoneIt();
2033 dead = 1;
2034 break;
2035 case 64:
2036 break;
2037 case 65:
2038 dead = PrintScore();
2039 StopTime = 2;
2040 break;
2041 case 66:
2042 if (Game->type == SEAS_OF_BLOOD_VARIANT)
2043 AdventureSheet();
2044 else
2045 ListInventory(0);
2046 if (Game->type == US_VARIANT && has_graphics()) {
2047 UpdateUSInventory();
2048 }
2049 StopTime = 2;
2050 break;
2051 case 67:
2052 SetBitFlag(0);
2053 break;
2054 case 68:
2055 ClearBitFlag(0);
2056 break;
2057 case 69:
2058 GameHeader.LightTime = LightRefill;
2059 Items[LIGHT_SOURCE].Location = CARRIED;
2060 ClearBitFlag(LIGHTOUTBIT);
2061 break;
2062 case 70:
2063 ClearScreen(); /* pdd. */
2064 break;
2065 case 71:
2066 SaveGame();
2067 StopTime = 2;
2068 break;
2069 case 72:
2070 p = param[pptr++];
2071 SwapItemLocations(p, param[pptr++]);
2072 break;
2073 case 73:
2074 #ifdef DEBUG_ACTIONS
2075 debug_print("Continue with next line\n");
2076 #endif
2077 continuation = 1;
2078 break;
2079 case 74:
2080 Items[param[pptr++]].Location = CARRIED;
2081 break;
2082 case 75:
2083 p = param[pptr++];
2084 MoveItemAToLocOfItemB(p, param[pptr++]);
2085 break;
2086 case 76: /* Looking at adventure .. */
2087 #ifdef DEBUG_ACTIONS
2088 debug_print("LOOK\n");
2089 #endif
2090 print_look_to_transcript = 1;
2091 if (split_screen)
2092 Look();
2093 print_look_to_transcript =
2094 should_look_in_transcript = 0;
2095 break;
2096 case 77:
2097 if (CurrentCounter >= 1)
2098 CurrentCounter--;
2099 #ifdef DEBUG_ACTIONS
2100 debug_print(
2101 "decrementing current counter. Current counter is now %d.\n",
2102 CurrentCounter);
2103 #endif
2104 break;
2105 case 78:
2106 OutputNumber(CurrentCounter);
2107 Output(" ");
2108 break;
2109 case 79:
2110 #ifdef DEBUG_ACTIONS
2111 debug_print("CurrentCounter is set to %d.\n", param[pptr]);
2112 #endif
2113 CurrentCounter = param[pptr++];
2114 break;
2115 case 80:
2116 GoToStoredLoc();
2117 break;
2118 case 81:
2119 SwapCounters(param[pptr++]);
2120 break;
2121 case 82:
2122 CurrentCounter += param[pptr++];
2123 break;
2124 case 83:
2125 CurrentCounter -= param[pptr++];
2126 if (CurrentCounter < -1)
2127 CurrentCounter = -1;
2128 /* Note: This seems to be needed. I don't yet
2129 know if there is a maximum value to limit too */
2130 break;
2131 case 84:
2132 PrintNoun();
2133 break;
2134 case 85:
2135 PrintNoun();
2136 Output("\n");
2137 break;
2138 case 86:
2139 if (!(Options & SPECTRUM_STYLE))
2140 Output("\n");
2141 break;
2142 case 87:
2143 SwapLocAndRoomflag(param[pptr++]);
2144 break;
2145 case 88:
2146 #ifdef DEBUG_ACTIONS
2147 debug_print("Delay\n");
2148 #endif
2149 Delay(1);
2150 break;
2151 case 89:
2152 #ifdef DEBUG_ACTIONS
2153 debug_print("Action 89, parameter %d\n", param[pptr]);
2154 #endif
2155 p = param[pptr++];
2156 switch (CurrentGame) {
2157 case SPIDERMAN:
2158 case SPIDERMAN_C64:
2159 DrawBlack();
2160 break;
2161 case SECRET_MISSION:
2162 case SECRET_MISSION_C64:
2163 SecretAction(p);
2164 break;
2165 case ADVENTURELAND:
2166 case ADVENTURELAND_C64:
2167 AdventurelandAction(p);
2168 break;
2169 case SEAS_OF_BLOOD:
2170 case SEAS_OF_BLOOD_C64:
2171 BloodAction(p);
2172 break;
2173 case ROBIN_OF_SHERWOOD:
2174 case ROBIN_OF_SHERWOOD_C64:
2175 SherwoodAction(p);
2176 break;
2177 case GREMLINS:
2178 case GREMLINS_SPANISH:
2179 case GREMLINS_SPANISH_C64:
2180 case GREMLINS_GERMAN:
2181 case GREMLINS_GERMAN_C64:
2182 GremlinsAction();
2183 break;
2184 default:
2185 break;
2186 }
2187 break;
2188
2189 case 90:
2190 #ifdef DEBUG_ACTIONS
2191 debug_print("Draw Hulk image, parameter %d\n", param[pptr]);
2192 #endif
2193 if (CurrentGame != HULK && CurrentGame != HULK_C64 && CurrentGame != HULK_US) {
2194 pptr++;
2195 } else if (!ItIsDark())
2196 DrawHulkImage(param[pptr++]);
2197 break;
2198 default:
2199 debug_print("Unknown action %d [Param begins %d %d]\n", act[cc],
2200 param[pptr], param[pptr + 1]);
2201 break;
2202 }
2203 cc++;
2204 }
2205
2206 if (dead) {
2207 return ACT_GAMEOVER;
2208 } else if (continuation) {
2209 return ACT_CONTINUE;
2210 } else {
2211 return ACT_SUCCESS;
2212 }
2213 }
2214
2215 static void PrintTakenOrDropped(int index)
2216 {
2217 Output(sys[index]);
2218 int length = strlen(sys[index]);
2219 char last = sys[index][length - 1];
2220 if (last == 10 || last == 13)
2221 return;
2222 Output(" ");
2223 if ((!(CurrentCommand->allflag & LASTALL))
2224 || split_screen == 0) {
2225 Output("\n");
2226 }
2227 }
2228
2229 static ExplicitResultType PerformActions(int vb, int no)
2230 {
2231 int dark = ItIsDark();
2232 int ct = 0;
2233 ExplicitResultType flag;
2234 int doagain = 0;
2235 int found_match = 0;
2236 #if defined(__clang__)
2237 #pragma mark GO
2238 #endif
2239 if (vb == GO && no == -1) {
2240 Output(sys[DIRECTION]);
2241 return ER_SUCCESS;
2242 }
2243 if (vb == 1 && no >= 1 && no <= 6) {
2244 int nl;
2245 if (dark)
2246 Output(sys[DANGEROUS_TO_MOVE_IN_DARK]);
2247 nl = Rooms[MyLoc].Exits[no - 1];
2248 if (nl != 0) {
2249 /* Seas of Blood needs this to be able to flee back to the last room */
2250 if (Game->type == SEAS_OF_BLOOD_VARIANT)
2251 SavedRoom = MyLoc;
2252 if (Options & (SPECTRUM_STYLE | TI994A_STYLE))
2253 Output(sys[OK]);
2254 MyLoc = nl;
2255 should_look_in_transcript = 1;
2256 if (CurrentCommand && CurrentCommand->next) {
2257 LookWithPause();
2258 }
2259 return ER_SUCCESS;
2260 }
2261 if (dark) {
2262 SetLight();
2263 MyLoc = GameHeader.NumRooms; /* It seems to be what the code says! */
2264 Output(sys[YOU_FELL_AND_BROKE_YOUR_NECK]);
2265 return ER_SUCCESS;
2266 }
2267 Output(sys[YOU_CANT_GO_THAT_WAY]);
2268 return ER_SUCCESS;
2269 }
2270
2271 if (!dark) {
2272 if ((CurrentGame == HULK || CurrentGame == HULK_C64 || CurrentGame == HULK_US) && vb == 39) {
2273 HulkShowImageOnExamine(no);
2274 } else if (CurrentGame == COUNT_US && vb == 8) {
2275 CountShowImageOnExamineUS(no);
2276 } else if (CurrentGame == VOODOO_CASTLE_US && vb == 42) {
2277 VoodooShowImageOnExamineUS(no);
2278 }
2279 }
2280
2281 if (CurrentCommand && CurrentCommand->allflag && vb == CurrentCommand->verb && !(dark && vb == TAKE)) {
2282 if (!lastwasnewline)
2283 Output("\n");
2284 Output(Items[CurrentCommand->item].Text);
2285 Output("....");
2286 }
2287 flag = ER_RAN_ALL_LINES_NO_MATCH;
2288 if (CurrentGame != TI994A) {
2289 while (ct <= GameHeader.NumActions) {
2290 int verbvalue, nounvalue;
2291 verbvalue = Actions[ct].Vocab;
2292 /* Think this is now right. If a line we run has an action 73
2293 run all following lines with vocab of 0,0 */
2294 if (vb != 0 && (doagain && verbvalue != 0))
2295 break;
2296 /* Oops.. added this minor cockup fix 1.11 */
2297 if (vb != 0 && !doagain && flag == 0)
2298 break;
2299 nounvalue = verbvalue % 150;
2300 verbvalue /= 150;
2301 if ((verbvalue == vb) || (doagain && Actions[ct].Vocab == 0)) {
2302 if ((verbvalue == 0 && RandomPercent(nounvalue)) || doagain || (verbvalue != 0 && (nounvalue == no || nounvalue == 0))) {
2303 if (verbvalue == vb && vb != 0 && nounvalue == no)
2304 found_match = 1;
2305 ActionResultType flag2;
2306 if (flag == ER_RAN_ALL_LINES_NO_MATCH)
2307 flag = ER_RAN_ALL_LINES;
2308 if ((flag2 = PerformLine(ct)) != ACT_FAILURE) {
2309 /* ahah finally figured it out ! */
2310 flag = ER_SUCCESS;
2311 if (flag2 == ACT_CONTINUE)
2312 doagain = 1;
2313 else if (flag2 == ACT_GAMEOVER)
2314 return ER_SUCCESS;
2315 if (vb != 0 && doagain == 0)
2316 return ER_SUCCESS;
2317 }
2318 }
2319 }
2320
2321 ct++;
2322
2323 // clang-format off
2324 /* Previously this did not check ct against
2325 * GameHeader.NumActions and would read past the end of
2326 * Actions. I don't know what should happen on the last
2327 * action, but doing nothing is better than reading one
2328 * past the end.
2329 * --Chris
2330 */
2331 // clang-format on
2332
2333 if (ct <= GameHeader.NumActions && Actions[ct].Vocab != 0)
2334 doagain = 0;
2335 }
2336 } else {
2337 if (vb == 0) {
2338 RunImplicitTI99Actions();
2339 return ER_NO_RESULT;
2340 } else {
2341 flag = RunExplicitTI99Actions(vb, no);
2342 }
2343 }
2344
2345 if (found_match)
2346 return flag;
2347
2348 if (flag != ER_SUCCESS) {
2349 int item = 0;
2350 #if defined(__clang__)
2351 #pragma mark TAKE
2352 #endif
2353 if (vb == TAKE || vb == DROP) {
2354 if (CurrentCommand && CurrentCommand->allflag) {
2355 if (vb == TAKE && dark) {
2356 Output(sys[TOO_DARK_TO_SEE]);
2357 while (!(CurrentCommand->allflag & LASTALL)) {
2358 CurrentCommand = CurrentCommand->next;
2359 }
2360 return ER_SUCCESS;
2361 }
2362 item = CurrentCommand->item;
2363 int location = CARRIED;
2364 if (vb == TAKE)
2365 location = MyLoc;
2366 while (Items[item].Location != location && !(CurrentCommand->allflag & LASTALL)) {
2367 CurrentCommand = CurrentCommand->next;
2368 }
2369 if (Items[item].Location != location)
2370 return ER_SUCCESS;
2371 }
2372
2373 /* Yes they really _are_ hardcoded values */
2374 if (vb == TAKE) {
2375 if (no == -1) {
2376 Output(sys[WHAT]);
2377 return ER_SUCCESS;
2378 }
2379 if (CountCarried() >= GameHeader.MaxCarry) {
2380 Output(sys[YOURE_CARRYING_TOO_MUCH]);
2381 return ER_SUCCESS;
2382 }
2383 if (!item)
2384 item = MatchUpItem(no, MyLoc);
2385 if (item == -1) {
2386 item = MatchUpItem(no, CARRIED);
2387 if (item == -1) {
2388 item = MatchUpItem(no, 0);
2389 if (item == -1) {
2390 Output(sys[THATS_BEYOND_MY_POWER]);
2391 } else {
2392 Output(sys[YOU_DONT_SEE_IT]);
2393 }
2394 } else {
2395 Output(sys[YOU_HAVE_IT]);
2396 }
2397 return ER_SUCCESS;
2398 }
2399 Items[item].Location = CARRIED;
2400 PrintTakenOrDropped(TAKEN);
2401 return ER_SUCCESS;
2402 }
2403 #if defined(__clang__)
2404 #pragma mark DROP
2405 #endif
2406 if (vb == DROP) {
2407 if (no == -1) {
2408 Output(sys[WHAT]);
2409 return ER_SUCCESS;
2410 }
2411 if (!item)
2412 item = MatchUpItem(no, CARRIED);
2413 if (item == -1) {
2414 item = MatchUpItem(no, 0);
2415 if (item == -1) {
2416 Output(sys[THATS_BEYOND_MY_POWER]);
2417 } else {
2418 Output(sys[YOU_HAVENT_GOT_IT]);
2419 }
2420 return ER_SUCCESS;
2421 }
2422 Items[item].Location = MyLoc;
2423 PrintTakenOrDropped(DROPPED);
2424 return ER_SUCCESS;
2425 }
2426 }
2427 }
2428 return flag;
2429 }
2430
2431 glkunix_argumentlist_t glkunix_arguments[] = {
2432 { "-y", glkunix_arg_NoValue,
2433 "-y Generate 'You are', 'You are carrying' type messages for games "
2434 "that use these instead (eg Robin Of Sherwood)" },
2435 { "-i", glkunix_arg_NoValue,
2436 "-i Generate 'I am' type messages (default)" },
2437 { "-d", glkunix_arg_NoValue, "-d Debugging info on load " },
2438 { "-s", glkunix_arg_NoValue,
2439 "-s Generate authentic Scott Adams driver light messages rather "
2440 "than other driver style ones (Light goes out in n turns..)" },
2441 { "-t", glkunix_arg_NoValue,
2442 "-t Generate TRS80 style display (terminal width is 64 characters; "
2443 "a line <-----------------> is displayed after the top stuff; objects "
2444 "have periods after them instead of hyphens" },
2445 { "-p", glkunix_arg_NoValue,
2446 "-p Use for prehistoric databases which don't use bit 16" },
2447 { "-w", glkunix_arg_NoValue, "-w Disable upper window" },
2448 { "-n", glkunix_arg_NoValue, "-n No delays" },
2449 { "", glkunix_arg_ValueFollows, "filename file to load" },
2450
2451 { NULL, glkunix_arg_End, NULL }
2452 };
2453
2454 int glkunix_startup_code(glkunix_startup_t *data)
2455 {
2456 int argc = data->argc;
2457 char **argv = data->argv;
2458
2459 if (argc < 1)
2460 return 0;
2461
2462 if (argc > 1)
2463 while (argv[1]) {
2464 if (*argv[1] != '-')
2465 break;
2466 switch (argv[1][1]) {
2467 case 'y':
2468 Options |= YOUARE;
2469 break;
2470 case 'i':
2471 Options &= ~YOUARE;
2472 break;
2473 case 'd':
2474 Options |= DEBUGGING;
2475 break;
2476 case 's':
2477 Options |= SCOTTLIGHT;
2478 break;
2479 case 't':
2480 Options |= TRS80_STYLE;
2481 break;
2482 case 'p':
2483 Options |= PREHISTORIC_LAMP;
2484 break;
2485 case 'w':
2486 split_screen = 0;
2487 break;
2488 case 'n':
2489 Options |= NO_DELAYS;
2490 break;
2491 case 'f':
2492 Options |= FLICKER_ON;
2493 break;
2494 }
2495 argv++;
2496 argc--;
2497 }
2498
2499 #ifdef GARGLK
2500 garglk_set_program_name("ScottFree 1.14");
2501 garglk_set_program_info("ScottFree 1.14 by Alan Cox\n"
2502 "Glk port by Chris Spiegel\n");
2503 #endif
2504
2505 if (argc == 2) {
2506 game_file = argv[1];
2507
2508 #ifdef GARGLK
2509 const char *s;
2510 if ((s = strrchr(game_file, '/')) != NULL || (s = strrchr(game_file, '\\')) != NULL) {
2511 garglk_set_story_name(s + 1);
2512 } else {
2513 garglk_set_story_name(game_file);
2514 }
2515 #endif
2516
2517 if (game_file) {
2518 const char *n;
2519 int dirlen = 0;
2520 if ((n = strrchr(game_file, '/')) != NULL || (n = strrchr(game_file, '\\')) != NULL) {
2521 dirlen = (int)(n - game_file + 1);
2522 }
2523 if (dirlen) {
2524 DirPath = MemAlloc(dirlen + 1);
2525 memcpy(DirPath, game_file, dirlen);
2526 DirPath[dirlen] = 0;
2527 }
2528 }
2529 }
2530
2531 return 1;
2532 }
2533
2534 void glk_main(void)
2535 {
2536 int vb, no, n = 1;
2537
2538 if (*(char *)&n != 1) {
2539 WeAreBigEndian = 1;
2540 }
2541
2542 glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_Proportional, 0);
2543 glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_Indentation, 20);
2544 glk_stylehint_set(wintype_TextBuffer, style_User1, stylehint_ParaIndentation,
2545 20);
2546 glk_stylehint_set(wintype_TextBuffer, style_Preformatted, stylehint_Justification, stylehint_just_Centered);
2547
2548 Bottom = glk_window_open(0, 0, 0, wintype_TextBuffer, GLK_BUFFER_ROCK);
2549 if (Bottom == NULL)
2550 glk_exit();
2551 glk_set_window(Bottom);
2552
2553 if (game_file == NULL)
2554 Fatal("No game provided");
2555
2556 const char **dictpointer;
2557
2558 if (Options & YOUARE)
2559 dictpointer = sysdict;
2560 else
2561 dictpointer = sysdict_i_am;
2562
2563 for (int i = 0; i < MAX_SYSMESS; i++) {
2564 if (dictpointer[i] != NULL)
2565 sys[i] = dictpointer[i];
2566 else
2567 sys[i] = sysdict[i];
2568 }
2569
2570 GameIDType game_type = DetectGame(game_file);
2571
2572 if (game_type == UNKNOWN_GAME)
2573 Fatal("Unsupported game!");
2574
2575 if (game_type != SCOTTFREE && game_type != TI994A) {
2576 Options |= SPECTRUM_STYLE;
2577 split_screen = 1;
2578 } else {
2579 if (game_type != TI994A)
2580 Options |= TRS80_STYLE;
2581 split_screen = 1;
2582 }
2583
2584 if (title_screen != NULL) {
2585 if (split_screen)
2586 PrintTitleScreenGrid();
2587 else
2588 PrintTitleScreenBuffer();
2589 }
2590
2591 if (Options & TRS80_STYLE) {
2592 TopWidth = 64;
2593 TopHeight = 11;
2594 } else {
2595 TopWidth = 80;
2596 TopHeight = 10;
2597 }
2598
2599 if (game_type == TI994A) {
2600 Display(Bottom, "In this adventure, you may abbreviate any word \
2601 by typing its first %d letters, and directions by typing \
2602 one letter.\n\nDo you want to restore previously saved game?\n",
2603 GameHeader.WordLength);
2604 if (YesOrNo())
2605 LoadGame();
2606 ClearScreen();
2607 }
2608
2609 if (Game->type == US_VARIANT) {
2610 if (has_graphics()) {
2611 ImageWidth = 280;
2612 if (ImageHeight < 158)
2613 ImageHeight = 158;
2614 DrawTitleImage();
2615 OpenGraphicsWindow();
2616 sys[MESSAGE_DELIMITER] = " ";
2617 sys[ITEM_DELIMITER] = ". ";
2618 sys[EXITS_DELIMITER] = " ";
2619 sys[YOU_ARE] = "I am in a ";
2620 sys[YOU_SEE] = ". Visible: ";
2621 sys[INVENTORY] = "I've got: ";
2622 sys[NOTHING] = "Nothing at all. ";
2623 sys[EXITS] = "Some exits: ";
2624 if (CurrentSys == SYS_MSDOS)
2625 sys[WHAT_NOW] = "Command me? ";
2626 else
2627 sys[WHAT_NOW] = "What shall I do? ";
2628 sys[NORTH] = "NORTH";
2629 sys[EAST] = "EAST";
2630 sys[SOUTH] = "SOUTH";
2631 sys[WEST] = "WEST";
2632 sys[UP] = "UP";
2633 sys[DOWN] = "DOWN";
2634
2635 Options |= PC_STYLE;
2636 } else {
2637 CurrentGame = SCOTTFREE;
2638 Game->type = NO_TYPE;
2639 game_type = SCOTTFREE;
2640 }
2641 }
2642
2643 if (game_type == SCOTTFREE)
2644 Output("\
2645 Scott Free, A Scott Adams game driver in C.\n\
2646 Release 1.14, (c) 1993,1994,1995 Swansea University Computer Society.\n\
2647 Distributed under the GNU software license\n\n");
2648
2649 OpenTopWindow();
2650
2651 #ifdef SPATTERLIGHT
2652 UpdateSettings();
2653 if (gli_determinism)
2654 srand(1234);
2655 else
2656 #endif
2657 srand((unsigned int)time(NULL));
2658
2659 InitialState = SaveCurrentState();
2660
2661 while (1) {
2662 glk_tick();
2663
2664 if (should_restart)
2665 RestartGame();
2666
2667 if (!StopTime)
2668 PerformActions(0, 0);
2669 if (!(CurrentCommand && CurrentCommand->allflag && !(CurrentCommand->allflag & LASTALL))) {
2670 print_look_to_transcript = should_look_in_transcript;
2671 Look();
2672 print_look_to_transcript = should_look_in_transcript = 0;
2673 if (!StopTime && !should_restart)
2674 SaveUndo();
2675 }
2676
2677 if (should_restart)
2678 continue;
2679
2680 if (GetInput(&vb, &no) == 1)
2681 continue;
2682
2683 switch (PerformActions(vb, no)) {
2684 case ER_RAN_ALL_LINES_NO_MATCH:
2685 if (!RecheckForExtraCommand()) {
2686 Output(sys[I_DONT_UNDERSTAND]);
2687 FreeCommands();
2688 }
2689 break;
2690 case ER_RAN_ALL_LINES:
2691 Output(sys[YOU_CANT_DO_THAT_YET]);
2692 FreeCommands();
2693 break;
2694 default:
2695 JustStarted = 0;
2696 }
2697
2698 /* Brian Howarth games seem to use -1 for forever */
2699 if (Items[LIGHT_SOURCE].Location != DESTROYED && GameHeader.LightTime != -1 && !StopTime) {
2700 GameHeader.LightTime--;
2701 if (GameHeader.LightTime < 1) {
2702 SetBitFlag(LIGHTOUTBIT);
2703 if (Items[LIGHT_SOURCE].Location == CARRIED || Items[LIGHT_SOURCE].Location == MyLoc) {
2704 Output(sys[LIGHT_HAS_RUN_OUT]);
2705 }
2706 if ((Options & PREHISTORIC_LAMP) || (Game->subtype & MYSTERIOUS) || CurrentGame == TI994A)
2707 Items[LIGHT_SOURCE].Location = DESTROYED;
2708 } else if (GameHeader.LightTime < 25) {
2709 if (Items[LIGHT_SOURCE].Location == CARRIED || Items[LIGHT_SOURCE].Location == MyLoc) {
2710 if ((Options & SCOTTLIGHT) || (Game->subtype & MYSTERIOUS)) {
2711 Display(Bottom, "%s %d %s\n", sys[LIGHT_RUNS_OUT_IN], GameHeader.LightTime, sys[TURNS]);
2712 } else {
2713 if (GameHeader.LightTime % 5 == 0)
2714 Output(sys[LIGHT_GROWING_DIM]);
2715 }
2716 }
2717 }
2718 }
2719 if (StopTime)
2720 StopTime--;
2721 }
2722 }