Debian Codegraph @

terps/alan2/main.c

raw symbols SCIP
1 /*----------------------------------------------------------------------*\
2
3 MAIN.C
4
5 Main module of interpreter for ALAN Adventure Language
6
7 \*----------------------------------------------------------------------*/
8
9 #define V27COMPATIBLE
10
11 #include "sysdep.h"
12
13 #include "types.h"
14 #include "main.h"
15
16 #include <time.h>
17 #ifdef USE_READLINE
18 #include "readline.h"
19 #endif
20
21 #ifdef HAVE_SHORT_FILENAMES
22 #include "av.h"
23 #else
24 #include "alan.version.h"
25 #endif
26
27 #include "args.h"
28 #include "parse.h"
29 #include "inter.h"
30 #include "rules.h"
31 #ifdef REVERSED
32 #include "reverse.h"
33 #endif
34 #include "debug.h"
35 #include "stack.h"
36 #include "exe.h"
37 #include "term.h"
38
39 #ifdef GLK
40 #include "glk.h"
41 #include "glkio.h"
42 #endif
43
44 /* PUBLIC DATA */
45
46 winid_t glkMainWin;
47 winid_t glkStatusWin;
48
49 /* The Amachine memory */
50 Aword *memory;
51 static AcdHdr dummyHeader; /* Dummy to use until memory allocated */
52 AcdHdr *header = &dummyHeader;
53
54 int memTop = 0; /* Top of load memory */
55
56 int conjWord; /* First conjunction in dictonary, for ',' */
57
58
59 /* Amachine variables */
60 CurVars cur;
61
62 /* Amachine structures */
63 WrdElem *dict; /* Dictionary pointer */
64 ActElem *acts; /* Actor table pointer */
65 LocElem *locs; /* Location table pointer */
66 VrbElem *vrbs; /* Verb table pointer */
67 StxElem *stxs; /* Syntax table pointer */
68 ObjElem *objs; /* Object table pointer */
69 CntElem *cnts; /* Container table pointer */
70 RulElem *ruls; /* Rule table pointer */
71 EvtElem *evts; /* Event table pointer */
72 MsgElem *msgs; /* Message table pointer */
73 Aword *scores; /* Score table pointer */
74 Aword *freq; /* Cumulative character frequencies */
75
76 int dictsize;
77
78 Boolean verbose = FALSE;
79 Boolean errflg = TRUE;
80 Boolean trcflg = FALSE;
81 Boolean dbgflg = FALSE;
82 Boolean stpflg = FALSE;
83 Boolean logflg = FALSE;
84 Boolean statusflg = TRUE;
85 Boolean fail = FALSE;
86 Boolean anyOutput = FALSE;
87
88
89 /* The files and filenames */
90 char *advnam;
91 FILE *txtfil;
92 FILE *logfil;
93
94
95 /* Screen formatting info */
96 int col, lin;
97 int paglen, pagwidth;
98
99 Boolean needsp = FALSE;
100 Boolean skipsp = FALSE;
101
102 /* Restart jump buffer */
103 jmp_buf restart_label;
104
105
106 /* PRIVATE DATA */
107 static jmp_buf jmpbuf; /* Error return long jump buffer */
108
109
110
111 /*======================================================================
112
113 terminate()
114
115 Terminate the execution of the adventure, e.g. close windows,
116 return buffers...
117
118 */
119 #ifdef _PROTOTYPES_
120 void terminate(int code)
121 #else
122 void terminate(code)
123 int code;
124 #endif
125 {
126 #ifdef __amiga__
127 #ifdef AZTEC_C
128 #include <fcntl.h>
129 extern struct _dev *_devtab;
130 char buf[85];
131
132 if (con) { /* Running from WB, created a console so kill it */
133 /* Running from WB, so we created a console and
134 hacked the Aztec C device table to use it for all I/O
135 so now we need to make it close it (once!) */
136 _devtab[1].fd = _devtab[2].fd = 0;
137 } else
138 #else
139 /* Geek Gadgets GCC */
140 #include <workbench/startup.h>
141 #include <clib/dos_protos.h>
142 #include <clib/intuition_protos.h>
143
144 if (_WBenchMsg != NULL) {
145 Close(window);
146 if (_WBenchMsg->sm_ArgList != NULL)
147 UnLock(CurrentDir(cd));
148 } else
149 #endif
150 #endif
151 newline();
152 free(memory);
153 if (logflg)
154 fclose(logfil);
155
156 #ifdef __MWERKS__
157 printf("Command-Q to close window.");
158 #endif
159
160 #ifdef GLK
161 glk_exit();
162 #else
163 exit(code);
164 #endif
165 }
166
167 /*======================================================================
168
169 usage()
170
171 */
172 #ifdef _PROTOTYPES_
173 void usage(void)
174 #else
175 void usage()
176 #endif
177 {
178 printf("Usage:\n\n");
179 printf(" %s [<switches>] <adventure>\n\n", PROGNAME);
180 printf("where the possible optional switches are:\n");
181 #ifdef GLK
182 glk_set_style(style_Preformatted);
183 #endif
184 printf(" -v verbose mode\n");
185 printf(" -l log player commands and game output to a file\n");
186 printf(" -i ignore version and checksum errors\n");
187 printf(" -n no Status Line\n");
188 printf(" -d enter debug mode\n");
189 printf(" -t trace game execution\n");
190 printf(" -s single instruction trace\n");
191 #ifdef GLK
192 glk_set_style(style_Normal);
193 #endif
194 }
195
196
197 /*======================================================================
198
199 syserr()
200
201 Print a little text blaming the user for the system error.
202
203 */
204 #ifdef _PROTOTYPES_
205 void syserr(char *str)
206 #else
207 void syserr(str)
208 char *str;
209 #endif
210 {
211 output("$n$nAs you enter the twilight zone of Adventures, you stumble \
212 and fall to your knees. In front of you, you can vaguely see the outlines \
213 of an Adventure that never was.$n$nSYSTEM ERROR: ");
214 output(str);
215 output("$n$n");
216
217 if (logflg)
218 fclose(logfil);
219 newline();
220
221 #ifdef __amiga__
222 #ifdef AZTEC_C
223 {
224 char buf[80];
225
226 if (con) { /* Running from WB, wait for user ack. */
227 printf("press RETURN to quit");
228 gets(buf);
229 }
230 }
231 #endif
232 #endif
233
234 terminate(0);
235 }
236
237
238 /*======================================================================
239
240 error()
241
242 Print an error message, force new player input and abort.
243
244 */
245 #ifdef _PROTOTYPES_
246 void error(MsgKind msgno) /* IN - The error message number */
247 #else
248 void error(msgno)
249 MsgKind msgno; /* IN - The error message number */
250 #endif
251 {
252 if (msgno != MSGMAX)
253 prmsg(msgno);
254 wrds[wrdidx] = EOF; /* Force new player input */
255 dscrstkp = 0; /* Reset describe stack */
256 longjmp(jmpbuf,TRUE);
257 }
258
259
260 /*======================================================================
261
262 statusline()
263
264 Print the the status line on the top of the screen.
265
266 */
267 void statusline(void)
268 {
269 #ifdef GLK
270 glui32 glkWidth;
271 char line[100];
272 int pcol = col;
273 int i;
274
275 if (NULL == glkStatusWin)
276 return;
277
278 glk_set_window(glkStatusWin);
279 glk_window_clear(glkStatusWin);
280 glk_window_get_size(glkStatusWin, &glkWidth, NULL);
281
282 glk_set_style(style_User1);
283 for (i = 0; i < glkWidth; i++)
284 glk_put_char(' ');
285
286 col = 1;
287 glk_window_move_cursor(glkStatusWin, 1, 0);
288 needsp = FALSE;
289 say(where(HERO));
290 if (header->maxscore > 0)
291 sprintf(line, "Score %d(%d)/%d moves", cur.score, (int)header->maxscore, cur.tick);
292 else
293 sprintf(line, "%d moves", cur.tick);
294 glk_window_move_cursor(glkStatusWin, glkWidth - col - strlen(line), 0);
295 printf(line);
296 needsp = FALSE;
297
298 col = pcol;
299
300 glk_set_window(glkMainWin);
301 #else
302 #ifdef HAVE_ANSI
303 char line[100];
304 int i;
305 int pcol = col;
306
307 if (!statusflg) return;
308 /* ansi_position(1,1); ansi_bold_on(); */
309 printf("\x1b[1;1H");
310 printf("\x1b[7m");
311 col = 1;
312 say(where(HERO));
313 if (header->maxscore > 0)
314 sprintf(line, "Score %ld(%ld)/%ld moves", cur.score, (int)header->maxscore, cur.tick);
315 else
316 sprintf(line, "%ld moves", cur.tick);
317 for (i=0; i < pagwidth - col - strlen(line); i++) putchar(' ');
318 printf(line);
319 printf("\x1b[m");
320 printf("\x1b[%d;1H", paglen);
321 needsp = FALSE;
322
323 col = pcol;
324 #endif
325 #endif
326 }
327
328
329 /*======================================================================
330
331 logprint()
332
333 Print some text and log it if logging is on.
334
335 */
336 void logprint(char str[])
337 {
338 printf(str);
339 if (logflg)
340 fprintf(logfil, "%s", str);
341 }
342
343
344
345 /*======================================================================
346
347 newline()
348
349 Make a newline, but check for screen full.
350
351 */
352 #ifdef _PROTOTYPES_
353 void newline(void)
354 #else
355 void newline()
356 #endif
357 {
358 #ifdef GLK
359 glk_put_char('\n');
360 #else
361 char buf[256];
362
363 col = 1;
364 if (lin >= paglen - 1) {
365 logprint("\n");
366 needsp = FALSE;
367 prmsg(M_MORE);
368 #ifdef USE_READLINE
369 (void) readline(buf);
370 #else
371 fgets(buf, 256, stdin);
372 #endif
373 getPageSize();
374 lin = 0;
375 } else
376 logprint("\n");
377
378 lin++;
379 needsp = FALSE;
380 #endif
381 }
382
383
384 /*======================================================================
385
386 para()
387
388 Make a new paragraph, i.e one empty line (one or two newlines).
389
390 */
391 #ifdef _PROTOTYPES_
392 void para(void)
393 #else
394 void para()
395 #endif
396 {
397 if (col != 1)
398 newline();
399 newline();
400 }
401
402
403 /*======================================================================
404
405 clear()
406
407 Clear the screen.
408
409 */
410 #ifdef _PROTOTYPES_
411 void clear(void)
412 #else
413 void clear()
414 #endif
415 {
416 #ifdef GLK
417 glk_window_clear(glkMainWin);
418 #else
419 #ifdef HAVE_ANSI
420 if (!statusflg) return;
421 printf("\x1b[2J");
422 printf("\x1b[%d;1H", paglen);
423 #endif
424 #endif
425 }
426
427
428 /*======================================================================
429
430 allocate()
431
432 Safely allocate new memory.
433
434 */
435 #ifdef _PROTOTYPES_
436 void *allocate(unsigned long len) /* IN - Length to allocate */
437 #else
438 void *allocate(len)
439 unsigned long len; /* IN - Length to allocate */
440 #endif
441 {
442 void *p = (void *)malloc((size_t)len);
443
444 if (p == NULL)
445 syserr("Out of memory.");
446
447 return p;
448 }
449
450
451 /*----------------------------------------------------------------------
452
453 just()
454
455 Justify a string so that it wraps at end of screen.
456
457 */
458 #ifdef _PROTOTYPES_
459 static void just(char str[])
460 #else
461 static void just(str)
462 char str[];
463 #endif
464 {
465 #ifdef GLK
466 logprint(str);
467 #else
468 int i;
469 char ch;
470
471 if (col >= pagwidth && !skipsp)
472 newline();
473
474 while (strlen(str) > pagwidth - col) {
475 i = pagwidth - col - 1;
476 while (!isSpace(str[i]) && i > 0) /* First find wrap point */
477 i--;
478 if (i == 0 && col == 1) /* If it doesn't fit at all */
479 /* Wrap immediately after this word */
480 while (!isSpace(str[i]) && str[i] != '\0')
481 i++;
482 if (i > 0) { /* If it fits ... */
483 ch = str[i]; /* Save space or NULL */
484 str[i] = '\0'; /* Terminate string */
485 logprint(str); /* and print it */
486 skipsp = FALSE; /* If skipping, now we're done */
487 str[i] = ch; /* Restore character */
488 /* Skip white after printed portion */
489 for (str = &str[i]; isSpace(str[0]) && str[0] != '\0'; str++);
490 }
491 newline(); /* Then start a new line */
492 }
493 logprint(str); /* Print tail */
494 col = col + strlen(str); /* Update column */
495 #endif
496 }
497
498
499 /*----------------------------------------------------------------------
500
501 space()
502
503 Output a space if needed.
504
505 */
506 #ifdef _PROTOTYPES_
507 static void space(void)
508 #else
509 static void space()
510 #endif
511 {
512 if (skipsp)
513 skipsp = FALSE;
514 else {
515 if (needsp) {
516 logprint(" ");
517 col++;
518 }
519 }
520 needsp = FALSE;
521 }
522
523
524
525 /*----------------------------------------------------------------------
526
527 sayparam()
528
529 A parameter needs to be said, check for words the player used and use
530 them if possible.
531
532 */
533 #ifdef _PROTOTYPES_
534 static void sayparam(int p)
535 #else
536 static void sayparam(p)
537 int p;
538 #endif
539 {
540 int i;
541
542 for (i = 0; i <= p; i++)
543 if (params[i].code == EOF)
544 syserr("Nonexistent parameter referenced.");
545
546 if (params[p].firstWord == EOF) /* Any words he used? */
547 say(params[p].code);
548 else /* Yes, so use them... */
549 for (i = params[p].firstWord; i <= params[p].lastWord; i++) {
550 just((char *)addrTo(dict[wrds[i]].wrd));
551 if (i < params[p].lastWord)
552 just(" ");
553 }
554 }
555
556
557 /*----------------------------------------------------------------------
558
559 prsym()
560
561 Print an expanded symbolic reference.
562
563 N = newline
564 I = indent on a new line
565 P = new paragraph
566 L = current location name
567 O = current object -> first parameter!
568 V = current verb
569 A = current actor
570 T = tabulation
571 $ = no space needed after this
572 */
573 #ifdef _PROTOTYPES_
574 static void prsym(
575 char *str /* IN - The string starting with '$' */
576 )
577 #else
578 static void prsym(str)
579 char *str; /* IN - The string starting with '$' */
580 #endif
581 {
582 switch (toLower(str[1])) {
583 case 'n':
584 newline();
585 needsp = FALSE;
586 break;
587 case 'i':
588 newline();
589 logprint(" ");
590 col = 5;
591 needsp = FALSE;
592 break;
593 case 'o':
594 sayparam(0);
595 needsp = TRUE; /* We did print something non-white */
596 break;
597 case '1':
598 case '2':
599 case '3':
600 case '4':
601 case '5':
602 case '6':
603 case '7':
604 case '8':
605 case '9':
606 sayparam(str[1]-'1');
607 needsp = TRUE; /* We did print something non-white */
608 break;
609 case 'l':
610 say(cur.loc);
611 needsp = TRUE; /* We did print something non-white */
612 break;
613 case 'a':
614 say(cur.act);
615 needsp = TRUE; /* We did print something non-white */
616 break;
617 case 'v':
618 just((char *)addrTo(dict[vrbwrd].wrd));
619 needsp = TRUE; /* We did print something non-white */
620 break;
621 case 'p':
622 para();
623 needsp = FALSE;
624 break;
625 case 't': {
626 int i;
627 int spaces = 4-(col-1)%4;
628
629 for (i = 0; i<spaces; i++) logprint(" ");
630 col = col + spaces;
631 needsp = FALSE;
632 break;
633 }
634 case '$':
635 skipsp = TRUE;
636 break;
637 default:
638 logprint("$");
639 break;
640 }
641 }
642
643
644
645 /*======================================================================
646
647 output()
648
649 Output a string to suit the screen. Any symbolic inserts ('$') are
650 recogniced and performed.
651
652 */
653 #ifdef _PROTOTYPES_
654 void output(char original[])
655 #else
656 void output(original)
657 char original[];
658 #endif
659 {
660 char ch;
661 char *str, *copy;
662 char *symptr;
663
664 copy = strdup(original);
665 str = copy;
666
667 if (str[0] != '$' || str[1] != '$')
668 space(); /* Output space if needed (& not inhibited) */
669
670 while ((symptr = strchr(str, '$')) != (char *) NULL) {
671 ch = *symptr; /* Terminate before symbol */
672 *symptr = '\0';
673 if (strlen(str) > 0) {
674 just(str); /* Output part before '$' */
675 if (str[strlen(str)-1] == ' ')
676 needsp = FALSE;
677 }
678 *symptr = ch; /* restore '$' */
679 prsym(symptr); /* Print the symbolic reference */
680 str = &symptr[2]; /* Advance to after symbol and continue */
681 }
682 if (str[0] != 0) {
683 just(str); /* Output trailing part */
684 skipsp = FALSE;
685 if (str[strlen(str)-1] != ' ')
686 needsp = TRUE;
687 }
688 anyOutput = TRUE;
689 free(copy);
690 }
691
692
693 /*======================================================================
694
695 prmsg()
696
697 Print a message from the message table.
698
699 */
700 #ifdef _PROTOTYPES_
701 void prmsg(MsgKind msg) /* IN - message number */
702 #else
703 void prmsg(msg)
704 MsgKind msg; /* IN - message number */
705 #endif
706 {
707 interpret(msgs[msg].stms);
708 }
709
710
711 /*----------------------------------------------------------------------*\
712
713 Various check functions
714
715 endOfTable()
716 isObj, isLoc, isAct, IsCnt & isNum
717
718 \*----------------------------------------------------------------------*/
719
720 /* How to know we are at end of a table */
721 #ifdef _PROTOTYPES_
722 Boolean eot(Aword *adr)
723 #else
724 Boolean eot(adr)
725 Aword *adr;
726 #endif
727 {
728 return *adr == EOF;
729 }
730
731
732 #ifdef _PROTOTYPES_
733 Boolean isObj(Aword x)
734 #else
735 Boolean isObj(x)
736 Aword x;
737 #endif
738 {
739 return x >= OBJMIN && x <= OBJMAX;
740 }
741
742 #ifdef _PROTOTYPES_
743 Boolean isCnt(Aword x)
744 #else
745 Boolean isCnt(x)
746 Aword x;
747 #endif
748 {
749 return (x >= CNTMIN && x <= CNTMAX) ||
750 (isObj(x) && objs[x-OBJMIN].cont != 0) ||
751 (isAct(x) && acts[x-ACTMIN].cont != 0);
752 }
753
754 #ifdef _PROTOTYPES_
755 Boolean isAct(Aword x)
756 #else
757 Boolean isAct(x)
758 Aword x;
759 #endif
760 {
761 return x >= ACTMIN && x <= ACTMAX;
762 }
763
764 #ifdef _PROTOTYPES_
765 Boolean isLoc(Aword x)
766 #else
767 Boolean isLoc(x)
768 Aword x;
769 #endif
770 {
771 return x >= LOCMIN && x <= LOCMAX;
772 }
773
774 #ifdef _PROTOTYPES_
775 Boolean isNum(Aword x)
776 #else
777 Boolean isNum(x)
778 Aword x;
779 #endif
780 {
781 return x >= LITMIN && x <= LITMAX && litValues[x-LITMIN].type == TYPNUM;
782 }
783
784 #ifdef _PROTOTYPES_
785 Boolean isStr(Aword x)
786 #else
787 Boolean isStr(x)
788 Aword x;
789 #endif
790 {
791 return x >= LITMIN && x <= LITMAX && litValues[x-LITMIN].type == TYPSTR;
792 }
793
794 #ifdef _PROTOTYPES_
795 Boolean isLit(Aword x)
796 #else
797 Boolean isLit(x)
798 Aword x;
799 #endif
800 {
801 return x >= LITMIN && x <= LITMAX;
802 }
803
804
805
806 /*======================================================================
807
808 exitto()
809
810 Is there an exit from one location to another?
811
812 */
813 #ifdef _PROTOTYPES_
814 Boolean exitto(int to, int from)
815 #else
816 Boolean exitto(to, from)
817 int to, from;
818 #endif
819 {
820 ExtElem *ext;
821
822 if (locs[from-LOCMIN].exts == 0)
823 return(FALSE); /* No exits */
824
825 for (ext = (ExtElem *) addrTo(locs[from-LOCMIN].exts); !endOfTable(ext); ext++)
826 if (ext->next == to)
827 return(TRUE);
828
829 return(FALSE);
830 }
831
832
833
834 #ifdef CHECKOBJ
835 /*======================================================================
836
837 checkobj()
838
839 Check that the object given is valid, else print an error message
840 or find out what he wanted.
841
842 This routine is not used any longer, kept for sentimental reasons ;-)
843
844 */
845 void checkobj(obj)
846 Aword *obj;
847 {
848 Aword oldobj;
849
850 if (*obj != EOF)
851 return;
852
853 oldobj = EOF;
854 for (cur.obj = OBJMIN; cur.obj <= OBJMAX; cur.obj++) {
855 /* If an object is present and it is possible to perform his action */
856 if (isHere(cur.obj) && possible())
857 if (oldobj == EOF)
858 oldobj = cur.obj;
859 else
860 error(WANT); /* And we didn't find multiple objects */
861 }
862
863 if (oldobj == EOF)
864 error(WANT); /* But we found ONE */
865
866 *obj = cur.obj = oldobj;
867 output("($o)"); /* Then he surely meant this object */
868 }
869 #endif
870
871
872
873
874 /*----------------------------------------------------------------------
875 count()
876
877 Count the number of items in a container.
878
879 */
880 #ifdef _PROTOTYPES_
881 static int count(int cnt) /* IN - the container to count */
882 #else
883 static int count(cnt)
884 int cnt; /* IN - the container to count */
885 #endif
886 {
887 int i, j = 0;
888
889 for (i = OBJMIN; i <= OBJMAX; i++)
890 if (in(i, cnt))
891 /* Then it's in this container also */
892 j++;
893 return(j);
894 }
895
896
897
898 /*----------------------------------------------------------------------
899 sumatr()
900
901 Sum the values of one attribute in a container. Recursively.
902
903 */
904 #ifdef _PROTOTYPES_
905 static int sumatr(
906 Aword atr, /* IN - the attribute to sum over */
907 Aword cnt /* IN - the container to sum */
908 )
909 #else
910 static int sumatr(atr, cnt)
911 Aword atr; /* IN - the attribute to sum over */
912 Aword cnt; /* IN - the container to sum */
913 #endif
914 {
915 int i;
916 int sum = 0;
917
918 for (i = OBJMIN; i <= OBJMAX; i++)
919 if (objs[i-OBJMIN].loc == cnt) { /* Then it's in this container */
920 if (objs[i-OBJMIN].cont != 0) /* This is also a container! */
921 sum = sum + sumatr(atr, i);
922 sum = sum + attribute(i, atr);
923 }
924 return(sum);
925 }
926
927
928
929
930 /*======================================================================
931 checklim()
932
933 Checks if a limit for a container is exceeded.
934
935 */
936 #ifdef _PROTOTYPES_
937 Boolean checklim(
938 Aword cnt, /* IN - Container code */
939 Aword obj /* IN - The object to add */
940 )
941 #else
942 Boolean checklim(cnt, obj)
943 Aword cnt; /* IN - Container code */
944 Aword obj; /* IN - The object to add */
945 #endif
946 {
947 LimElem *lim;
948 Aword props;
949
950 fail = TRUE;
951 if (!isCnt(cnt))
952 syserr("Checking limits for a non-container.");
953
954 /* Find the container properties */
955 if (isObj(cnt))
956 props = objs[cnt-OBJMIN].cont;
957 else if (isAct(cnt))
958 props = acts[cnt-ACTMIN].cont;
959 else
960 props = cnt;
961
962 if (cnts[props-CNTMIN].lims != 0) { /* Any limits at all? */
963 for (lim = (LimElem *) addrTo(cnts[props-CNTMIN].lims); !endOfTable(lim); lim++)
964 if (lim->atr == 0) {
965 if (count(cnt) >= lim->val) {
966 interpret(lim->stms);
967 return(TRUE); /* Limit check failed */
968 }
969 } else {
970 if (sumatr(lim->atr, cnt) + attribute(obj, lim->atr) > lim->val) {
971 interpret(lim->stms);
972 return(TRUE);
973 }
974 }
975 }
976 fail = FALSE;
977 return(FALSE);
978 }
979
980
981
982
983
984 /*----------------------------------------------------------------------*\
985
986 Action routines
987
988 \*----------------------------------------------------------------------*/
989
990
991
992 /*----------------------------------------------------------------------
993 trycheck()
994
995 Tries a check, returns TRUE if it passed, FALSE else.
996
997 */
998 #ifdef _PROTOTYPES_
999 static Boolean trycheck(
1000 Aaddr adr, /* IN - ACODE address to check table */
1001 Boolean act /* IN - Act if it fails ? */
1002 )
1003 #else
1004 static Boolean trycheck(adr, act)
1005 Aaddr adr; /* IN - ACODE address to check table */
1006 Boolean act; /* IN - Act if it fails ? */
1007 #endif
1008 {
1009 ChkElem *chk;
1010
1011 chk = (ChkElem *) addrTo(adr);
1012 if (chk->exp == 0) {
1013 interpret(chk->stms);
1014 return(FALSE);
1015 } else {
1016 while (!endOfTable(chk)) {
1017 interpret(chk->exp);
1018 if (!(Abool)pop()) {
1019 if (act)
1020 interpret(chk->stms);
1021 return(FALSE);
1022 }
1023 chk++;
1024 }
1025 return(TRUE);
1026 }
1027 }
1028
1029
1030 /*======================================================================
1031 go()
1032
1033 Move hero in a direction.
1034
1035 */
1036 #ifdef _PROTOTYPES_
1037 void go(int dir)
1038 #else
1039 void go(dir)
1040 int dir;
1041 #endif
1042 {
1043 ExtElem *ext;
1044 Boolean ok;
1045 Aword oldloc;
1046
1047 ext = (ExtElem *) addrTo(locs[cur.loc-LOCMIN].exts);
1048 if (locs[cur.loc-LOCMIN].exts != 0)
1049 while (!endOfTable(ext)) {
1050 if (ext->code == dir) {
1051 ok = TRUE;
1052 if (ext->checks != 0) {
1053 if (trcflg) {
1054 printf("\n<EXIT %d (%s) from %d (", dir,
1055 (char *)addrTo(dict[wrds[wrdidx-1]].wrd), cur.loc);
1056 debugsay(cur.loc);
1057 printf("), Checking:>\n");
1058 }
1059 ok = trycheck(ext->checks, TRUE);
1060 }
1061 if (ok) {
1062 oldloc = cur.loc;
1063 if (ext->action != 0) {
1064 if (trcflg) {
1065 printf("\n<EXIT %d (%s) from %d (", dir,
1066 (char *)addrTo(dict[wrds[wrdidx-1]].wrd), cur.loc);
1067 debugsay(cur.loc);
1068 printf("), Executing:>\n");
1069 }
1070 interpret(ext->action);
1071 }
1072 /* Still at the same place? */
1073 if (where(HERO) == oldloc) {
1074 if (trcflg) {
1075 printf("\n<EXIT %d (%s) from %d (", dir,
1076 (char *)addrTo(dict[wrds[wrdidx-1]].wrd), cur.loc);
1077 debugsay(cur.loc);
1078 printf("), Moving:>\n");
1079 }
1080 locate(HERO, ext->next);
1081 }
1082 }
1083 return;
1084 }
1085 ext++;
1086 }
1087 error(M_NO_WAY);
1088 }
1089
1090
1091
1092 /*----------------------------------------------------------------------
1093
1094 findalt()
1095
1096 Find the verb alternative wanted in a verb list and return
1097 the address to it.
1098
1099 */
1100 #ifdef _PROTOTYPES_
1101 static AltElem *findalt(
1102 Aword vrbsadr, /* IN - Address to start of list */
1103 Aword param /* IN - Which parameter to match */
1104 )
1105 #else
1106 static AltElem *findalt(vrbsadr, param)
1107 Aword vrbsadr; /* IN - Address to start of list */
1108 Aword param; /* IN - Which parameter to match */
1109 #endif
1110 {
1111 VrbElem *vrb;
1112 AltElem *alt;
1113
1114 if (vrbsadr == 0)
1115 return(NULL);
1116
1117 for (vrb = (VrbElem *) addrTo(vrbsadr); !endOfTable(vrb); vrb++)
1118 if (vrb->code == cur.vrb) {
1119 for (alt = (AltElem *) addrTo(vrb->alts); !endOfTable(alt); alt++)
1120 if (alt->param == param || alt->param == 0)
1121 return alt;
1122 return NULL;
1123 }
1124 return NULL;
1125 }
1126
1127
1128
1129
1130 /*======================================================================
1131
1132 possible()
1133
1134 Check if current action is possible according to the CHECKs.
1135
1136 */
1137 #ifdef _PROTOTYPES_
1138 Boolean possible(void)
1139 #else
1140 Boolean possible()
1141 #endif
1142 {
1143 AltElem *alt[MAXPARAMS+2]; /* List of alt-pointers, one for each param */
1144 int i; /* Parameter index */
1145
1146 fail = FALSE;
1147 alt[0] = findalt(header->vrbs, 0);
1148 /* Perform global checks */
1149 if (alt[0] != 0 && alt[0]->checks != 0) {
1150 if (!trycheck(alt[0]->checks, FALSE)) return FALSE;
1151 if (fail) return FALSE;
1152 }
1153
1154 /* Now CHECKs in this location */
1155 alt[1] = findalt(locs[cur.loc-LOCMIN].vrbs, 0);
1156 if (alt[1] != 0 && alt[1]->checks != 0)
1157 if (!trycheck(alt[1]->checks, FALSE))
1158 return FALSE;
1159
1160 for (i = 0; params[i].code != EOF; i++) {
1161 alt[i+2] = findalt(objs[params[i].code-OBJMIN].vrbs, i+1);
1162 /* CHECKs in a possible parameter */
1163 if (alt[i+2] != 0 && alt[i+2]->checks != 0)
1164 if (!trycheck(alt[i+2]->checks, FALSE))
1165 return FALSE;
1166 }
1167
1168 for (i = 0; i < 2 || params[i-2].code != EOF; i++)
1169 if (alt[i] != 0 && alt[i]->action != 0)
1170 break;
1171 if (i >= 2 && params[i-2].code == EOF)
1172 /* Didn't find any code for this verb/object combination */
1173 return FALSE;
1174 else
1175 return TRUE;
1176 }
1177
1178
1179
1180 /*----------------------------------------------------------------------
1181
1182 do_it()
1183
1184 Execute the action commanded by hero.
1185
1186 */
1187 #ifdef _PROTOTYPES_
1188 static void do_it(void)
1189 #else
1190 static void do_it()
1191 #endif
1192 {
1193 AltElem *alt[MAXPARAMS+2]; /* List of alt-pointers, one for each param */
1194 Boolean done[MAXPARAMS+2]; /* Is it done */
1195 int i; /* Parameter index */
1196 char trace[80]; /* Trace string buffer */
1197
1198 fail = FALSE;
1199 alt[0] = findalt(header->vrbs, 0);
1200 /* Perform global checks */
1201 if (alt[0] != 0 && alt[0]->checks != 0) {
1202 if (trcflg)
1203 printf("\n<VERB %d, CHECK, GLOBAL:>\n", cur.vrb);
1204 if (!trycheck(alt[0]->checks, TRUE)) return;
1205 if (fail) return;
1206 }
1207
1208 /* Now CHECKs in this location */
1209 alt[1] = findalt(locs[cur.loc-LOCMIN].vrbs, 0);
1210 if (alt[1] != 0 && alt[1]->checks != 0) {
1211 if (trcflg)
1212 printf("\n<VERB %d, CHECK, in LOCATION:>\n", cur.vrb);
1213 if (!trycheck(alt[1]->checks, TRUE)) return;
1214 if (fail) return;
1215 }
1216
1217 for (i = 0; params[i].code != EOF; i++) {
1218 if (isLit(params[i].code))
1219 alt[i+2] = 0;
1220 else {
1221 if (isObj(params[i].code))
1222 alt[i+2] = findalt(objs[params[i].code-OBJMIN].vrbs, i+1);
1223 else if (isAct(params[i].code))
1224 alt[i+2] = findalt(acts[params[i].code-ACTMIN].vrbs, i+1);
1225 else
1226 syserr("Illegal parameter type.");
1227 /* CHECKs in the parameters */
1228 if (alt[i+2] != 0 && alt[i+2]->checks != 0) {
1229 if (trcflg)
1230 printf("\n<VERB %d, CHECK, in Parameter #%d:>\n", cur.vrb, i);
1231 if (!trycheck(alt[i+2]->checks, TRUE)) return;
1232 if (fail) return;
1233 }
1234 }
1235 }
1236
1237 /* Check for anything to execute... */
1238 for (i = 0; i < 2 || params[i-2].code != EOF; i++)
1239 if (alt[i] != 0 && alt[i]->action != 0)
1240 break;
1241 if (i >= 2 && params[i-2].code == EOF)
1242 /* Didn't find any code for this verb/object combination */
1243 error(M_CANT0);
1244
1245 /* Perform actions! */
1246
1247 /* First try any BEFORE or ONLY from outside in */
1248 done[0] = FALSE;
1249 done[1] = FALSE;
1250 for (i = 2; params[i-2].code != EOF; i++)
1251 done[i] = FALSE;
1252 i--;
1253 while (i >= 0) {
1254 if (alt[i] != 0)
1255 if (alt[i]->qual == (Aword)Q_BEFORE || alt[i]->qual == (Aword)Q_ONLY) {
1256 if (alt[i]->action != 0) {
1257 if (trcflg) {
1258 if (i == 0)
1259 strcpy(trace, "GLOBAL");
1260 else if (i == 1)
1261 strcpy(trace, "in LOCATION");
1262 else
1263 sprintf(trace, "in PARAMETER %d", i-1);
1264 if (alt[i]->qual == (Aword)Q_BEFORE)
1265 printf("\n<VERB %d, %s (BEFORE), Body:>\n", cur.vrb, trace);
1266 else
1267 printf("\n<VERB %d, %s (ONLY), Body:>\n", cur.vrb, trace);
1268 }
1269 interpret(alt[i]->action);
1270 if (fail) return;
1271 if (alt[i]->qual == (Aword)Q_ONLY) return;
1272 }
1273 done[i] = TRUE;
1274 }
1275 i--;
1276 }
1277
1278 /* Then execute any not declared as AFTER, i.e. the default */
1279 for (i = 0; i < 2 || params[i-2].code != EOF; i++) {
1280 if (alt[i] != 0)
1281 if (alt[i]->qual != (Aword)Q_AFTER) {
1282 if (!done[i] && alt[i]->action != 0) {
1283 if (trcflg) {
1284 if (i == 0)
1285 strcpy(trace, "GLOBAL");
1286 else if (i == 1)
1287 strcpy(trace, "in LOCATION");
1288 else
1289 sprintf(trace, "in PARAMETER %d", i-1);
1290 printf("\n<VERB %d, %s, Body:>\n", cur.vrb, trace);
1291 }
1292 interpret(alt[i]->action);
1293 if (fail) return;
1294 }
1295 done[i] = TRUE;
1296 }
1297 }
1298
1299 /* Finally, the ones declared as after */
1300 i--;
1301 while (i >= 0) {
1302 if (alt[i] != 0)
1303 if (!done[i] && alt[i]->action != 0) {
1304 if (trcflg) {
1305 if (i == 0)
1306 strcpy(trace, "GLOBAL");
1307 else if (i == 1)
1308 strcpy(trace, "in LOCATION");
1309 else
1310 sprintf(trace, "in PARAMETER %d", i-1);
1311 printf("\n<VERB %d, %s (AFTER), Body:>\n", cur.vrb, trace);
1312 }
1313 interpret(alt[i]->action);
1314 if (fail) return;
1315 }
1316 i--;
1317 }
1318 }
1319
1320
1321
1322 /*======================================================================
1323
1324 action()
1325
1326 Execute all activities commanded. Handles possible multiple actions
1327 such as THEM or lists of objects.
1328
1329 */
1330 #ifdef _PROTOTYPES_
1331 void action(
1332 ParamElem plst[] /* IN - Plural parameter list */
1333 )
1334 #else
1335 void action(plst)
1336 ParamElem plst[];
1337 #endif
1338 {
1339 int i, mpos;
1340 char marker[10];
1341
1342 if (plural) {
1343 /*
1344 The code == 0 means this is a multiple position. We must loop
1345 over this position (and replace it by each present in the plst)
1346 */
1347 for (mpos = 0; params[mpos].code != 0; mpos++); /* Find multiple position */
1348 sprintf(marker, "($%d)", mpos+1); /* Prepare a printout with $1/2/3 */
1349 for (i = 0; plst[i].code != EOF; i++) {
1350 params[mpos] = plst[i];
1351 output(marker);
1352 do_it();
1353 if (plst[i+1].code != EOF)
1354 para();
1355 }
1356 params[mpos].code = 0;
1357 } else
1358 do_it();
1359 }
1360
1361
1362
1363 /*----------------------------------------------------------------------*\
1364
1365 Event Handling
1366
1367 eventchk()
1368
1369 \*----------------------------------------------------------------------*/
1370
1371
1372 /*----------------------------------------------------------------------
1373 eventchk()
1374
1375 Check if any events are pending. If so execute them.
1376 */
1377 #ifdef _PROTOTYPES_
1378 static void eventchk(void)
1379 #else
1380 static void eventchk()
1381 #endif
1382 {
1383 while (etop != 0 && eventq[etop-1].time == cur.tick) {
1384 etop--;
1385 if (isLoc(eventq[etop].where))
1386 cur.loc = eventq[etop].where;
1387 else
1388 cur.loc = where(eventq[etop].where);
1389 if (trcflg) {
1390 printf("\n<EVENT %d (at ", eventq[etop].event);
1391 debugsay(cur.loc);
1392 printf("):>\n");
1393 }
1394 interpret(evts[eventq[etop].event-EVTMIN].code);
1395 }
1396 }
1397
1398
1399
1400
1401
1402 /*----------------------------------------------------------------------*\
1403
1404 Main program and initialisation
1405
1406 codfil
1407 filenames
1408
1409 checkvers()
1410 load()
1411 checkdebug()
1412 initheader()
1413 initstrings()
1414 start()
1415 init()
1416 main()
1417
1418 \*----------------------------------------------------------------------*/
1419
1420
1421 static FILE *codfil;
1422 static char codfnm[256] = "";
1423 static char txtfnm[256] = "";
1424 static char logfnm[256] = "";
1425
1426
1427 /*----------------------------------------------------------------------
1428
1429 checkvers()
1430
1431 */
1432 #ifdef _PROTOTYPES_
1433 static void checkvers(AcdHdr *header)
1434 #else
1435 static void checkvers(header)
1436 AcdHdr *header;
1437 #endif
1438 {
1439 char vers[4];
1440 char state[2];
1441
1442 /* Construct our own version */
1443 vers[0] = alan.version.version;
1444 vers[1] = alan.version.revision;
1445
1446 /* Check version of .ACD file */
1447 if (dbgflg) {
1448 state[0] = header->vers[3];
1449 state[1] = '\0';
1450 printf("<Version of '%s' is %d.%d(%d)%s>",
1451 advnam,
1452 (int)(header->vers[0]),
1453 (int)(header->vers[1]),
1454 (int)(header->vers[2]),
1455 (header->vers[3])==0? "": state);
1456 newline();
1457 }
1458
1459 /* Compatible if version and revision match... */
1460 if (strncmp(header->vers, vers, 2) != 0) {
1461 #ifdef V25COMPATIBLE
1462 if (header->vers[0] == 2 && header->vers[1] == 5) /* Check for 2.5 version */
1463 /* This we can convert later if needed... */;
1464 else
1465 #endif
1466 #ifdef V27COMPATIBLE
1467 if (header->vers[0] == 2 && header->vers[1] == 7) /* Check for 2.7 version */
1468 /* This we can convert later if needed... */;
1469 else
1470 #endif
1471 if (errflg) {
1472 char str[80];
1473 sprintf(str, "Incompatible version of ACODE program. Game is %ld.%ld, interpreter %ld.%ld.",
1474 (long) (header->vers[0]),
1475 (long) (header->vers[1]),
1476 (long) alan.version.version,
1477 (long) alan.version.revision);
1478 syserr(str);
1479 } else
1480 output("<WARNING! Incompatible version of ACODE program.>\n");
1481 }
1482 }
1483
1484
1485 /*----------------------------------------------------------------------
1486
1487 load()
1488
1489 */
1490 #ifdef _PROTOTYPES_
1491 static void load(void)
1492 #else
1493 static void load()
1494 #endif
1495 {
1496 AcdHdr tmphdr;
1497 Aword crc = 0;
1498 int i,tmp;
1499 char err[100];
1500
1501 rewind(codfil);
1502 tmp = fread(&tmphdr, sizeof(tmphdr), 1, codfil);
1503 rewind(codfil);
1504 checkvers(&tmphdr);
1505
1506 /* Allocate and load memory */
1507
1508 #ifdef REVERSED
1509 reverseHdr(&tmphdr);
1510 #endif
1511
1512 /* No memory allocated yet? */
1513 if (memory == NULL) {
1514 #ifdef V25COMPATIBLE
1515 if (tmphdr.vers[0] == 2 && tmphdr.vers[1] == 5)
1516 /* We need some more memory to expand 2.5 format*/
1517 memory = allocate((tmphdr.size+tmphdr.objmax-tmphdr.objmin+1+2)*sizeof(Aword));
1518 else
1519 #endif
1520 memory = allocate(tmphdr.size*sizeof(Aword));
1521 }
1522 header = (AcdHdr *) addrTo(0);
1523
1524 memTop = fread(addrTo(0), sizeof(Aword), tmphdr.size, codfil);
1525 if (memTop != tmphdr.size)
1526 syserr("Could not read all ACD code.");
1527
1528 /* Calculate checksum */
1529 for (i = sizeof(tmphdr)/sizeof(Aword); i < memTop; i++) {
1530 crc += memory[i]&0xff;
1531 crc += (memory[i]>>8)&0xff;
1532 crc += (memory[i]>>16)&0xff;
1533 crc += (memory[i]>>24)&0xff;
1534 #ifdef CRCLOG
1535 printf("%6x\t%6lx\t%6lx\n", i, crc, memory[i]);
1536 #endif
1537 }
1538 if (crc != tmphdr.acdcrc) {
1539 sprintf(err, "Checksum error in .ACD file (0x%lx instead of 0x%lx).",
1540 (unsigned long) crc, (unsigned long) tmphdr.acdcrc);
1541 if (errflg)
1542 syserr(err);
1543 else {
1544 output("<WARNING! $$");
1545 output(err);
1546 output("$$ Ignored, proceed at your own risk.>$n");
1547 }
1548 }
1549
1550 #ifdef REVERSED
1551 if (dbgflg||trcflg||stpflg)
1552 output("<Hmm, this is a little-endian machine, fixing byte ordering....");
1553 reverseACD(tmphdr.vers[0] == 2 && tmphdr.vers[1] == 5); /* Reverse all words in the ACD file */
1554 if (dbgflg||trcflg||stpflg)
1555 output("OK.>$n");
1556 #endif
1557
1558 #ifdef V25COMPATIBLE
1559 /* Check for 2.5 version */
1560 if (tmphdr.vers[0] == 2 && tmphdr.vers[1] == 5) {
1561 if (dbgflg||trcflg||stpflg)
1562 output("<Hmm, this is a v2.5 game, please wait while I convert it...");
1563 c25to26ACD();
1564 if (dbgflg||trcflg||stpflg)
1565 output("OK.>$n");
1566 }
1567 #endif
1568
1569 }
1570
1571
1572 /*----------------------------------------------------------------------
1573
1574 checkdebug()
1575
1576 */
1577 #ifdef _PROTOTYPES_
1578 static void checkdebug(void)
1579 #else
1580 static void checkdebug()
1581 #endif
1582 {
1583 /* Make sure he can't debug if not allowed! */
1584 if (!header->debug) {
1585 if (dbgflg|trcflg|stpflg)
1586 printf("<Sorry, '%s' is not compiled for debug!>\n", advnam);
1587 para();
1588 dbgflg = FALSE;
1589 trcflg = FALSE;
1590 stpflg = FALSE;
1591 }
1592
1593 if (dbgflg) /* If debugging */
1594 srand(0); /* use no randomization */
1595 else
1596 srand(time(0)); /* seed random generator */
1597 }
1598
1599
1600 /*----------------------------------------------------------------------
1601
1602 initheader()
1603
1604 */
1605 #ifdef _PROTOTYPES_
1606 static void initheader(void)
1607 #else
1608 static void initheader()
1609 #endif
1610 {
1611 dict = (WrdElem *) addrTo(header->dict);
1612 /* Find out number of entries in dictionary */
1613 for (dictsize = 0; !endOfTable(&dict[dictsize]); dictsize++);
1614 vrbs = (VrbElem *) addrTo(header->vrbs);
1615 stxs = (StxElem *) addrTo(header->stxs);
1616 locs = (LocElem *) addrTo(header->locs);
1617 acts = (ActElem *) addrTo(header->acts);
1618 objs = (ObjElem *) addrTo(header->objs);
1619 evts = (EvtElem *) addrTo(header->evts);
1620 cnts = (CntElem *) addrTo(header->cnts);
1621 ruls = (RulElem *) addrTo(header->ruls);
1622 msgs = (MsgElem *) addrTo(header->msgs);
1623 scores = (Aword *) addrTo(header->scores);
1624
1625 if (header->pack)
1626 freq = (Aword *) addrTo(header->freq);
1627 }
1628
1629
1630 /*----------------------------------------------------------------------
1631
1632 initstrings()
1633
1634 */
1635 #ifdef _PROTOTYPES_
1636 static void initstrings(void)
1637 #else
1638 static void initstrings()
1639 #endif
1640 {
1641 IniElem *init;
1642
1643 for (init = (IniElem *) addrTo(header->init); !endOfTable(init); init++) {
1644 getstr(init->fpos, init->len);
1645 memory[init->adr] = pop();
1646 }
1647 }
1648
1649
1650 /*----------------------------------------------------------------------
1651
1652 start()
1653
1654 */
1655 #ifdef _PROTOTYPES_
1656 static void start(void)
1657 #else
1658 static void start()
1659 #endif
1660 {
1661 int startloc;
1662
1663 cur.tick = -1;
1664 cur.loc = startloc = where(HERO);
1665 cur.act = HERO;
1666 cur.score = 0;
1667 if (trcflg)
1668 printf("\n<START:>\n");
1669 interpret(header->start);
1670 para();
1671
1672 acts[HERO-ACTMIN].loc = 0;
1673 locate(HERO, startloc);
1674 }
1675
1676
1677
1678 /*----------------------------------------------------------------------
1679 init()
1680
1681 Initialization, program load etc.
1682
1683 */
1684 #ifdef _PROTOTYPES_
1685 static void init(void)
1686 #else
1687 static void init()
1688 #endif
1689 {
1690 int i;
1691
1692 /* Initialise some status */
1693 etop = 0; /* No pending events */
1694 looking = FALSE; /* Not looking now */
1695 dscrstkp = 0; /* No describe in progress */
1696
1697 load();
1698
1699 initheader();
1700 checkdebug();
1701
1702 /* Initialise string attributes */
1703 initstrings();
1704
1705 getPageSize();
1706
1707 /* Find first conjunction and use that for ',' handling */
1708 for (i = 0; i < dictsize; i++)
1709 if (isConj(i)) {
1710 conjWord = i;
1711 break;
1712 }
1713
1714 /* Start the adventure */
1715 clear();
1716 start();
1717 }
1718
1719
1720
1721 /*----------------------------------------------------------------------
1722 movactor()
1723
1724 Let the current actor move. If player, ask him.
1725
1726 */
1727 #ifdef _PROTOTYPES_
1728 static void movactor(void)
1729 #else
1730 static void movactor()
1731 #endif
1732 {
1733 ScrElem *scr;
1734 StepElem *step;
1735 ActElem *act = (ActElem *) &acts[cur.act-ACTMIN];
1736
1737 cur.loc = where(cur.act);
1738 if (cur.act == HERO) {
1739 parse();
1740 fail = FALSE; /* fail only aborts one actor */
1741 rules();
1742 } else if (act->script != 0) {
1743 for (scr = (ScrElem *) addrTo(act->scradr); !endOfTable(scr); scr++)
1744 if (scr->code == act->script) {
1745 /* Find correct step in the list by indexing */
1746 step = (StepElem *) addrTo(scr->steps);
1747 step = (StepElem *) &step[act->step];
1748 /* Now execute it, maybe. First check wait count */
1749 if (step->after > act->count) {
1750 /* Wait some more */
1751 if (trcflg) {
1752 printf("\n<ACTOR %d, ", cur.act);
1753 debugsay(cur.act);
1754 printf(" (at ");
1755 debugsay(cur.loc);
1756 printf("), SCRIPT %ld, STEP %ld, Waiting %ld more>\n",
1757 act->script, act->step+1, step->after-act->count);
1758 }
1759 act->count++;
1760 rules();
1761 return;
1762 } else
1763 act->count = 0;
1764 /* Then check possible expression */
1765 if (step->exp != 0) {
1766 if (trcflg) {
1767 printf("\n<ACTOR %d, ", cur.act);
1768 debugsay(cur.act);
1769 printf(" (at ");
1770 debugsay(cur.loc);
1771 printf("), SCRIPT %ld, STEP %ld, Evaluating:>\n",
1772 act->script, act->step+1);
1773 }
1774 interpret(step->exp);
1775 if (!(Abool)pop()) {
1776 rules();
1777 return; /* Hadn't happened yet */
1778 }
1779 }
1780 /* OK, so finally let him do his thing */
1781 act->step++; /* Increment step number before executing... */
1782 if (trcflg) {
1783 printf("\n<ACTOR %d, ", cur.act);
1784 debugsay(cur.act);
1785 printf(" (at ");
1786 debugsay(cur.loc);
1787 printf("), SCRIPT %ld, STEP %ld, Executing:>\n",
1788 act->script, act->step);
1789 }
1790 interpret(step->stm);
1791 step++;
1792 /* ... so that we can see if he is USEing another script now */
1793 if (act->step != 0 && endOfTable(step))
1794 /* No more steps in this script, so stop him */
1795 act->script = 0;
1796 fail = FALSE; /* fail only aborts one actor */
1797 rules();
1798 return;
1799 }
1800 syserr("Unknown actor script.");
1801 } else if (trcflg) {
1802 printf("\n<ACTOR %d, ", cur.act);
1803 debugsay(cur.act);
1804 printf(" (at ");
1805 debugsay(cur.loc);
1806 printf("), Idle>\n");
1807 rules();
1808 return;
1809 }
1810 }
1811
1812 /*----------------------------------------------------------------------
1813
1814 openFiles()
1815
1816 Open the necessary files.
1817
1818 */
1819 #ifdef _PROTOTYPES_
1820 static void openFiles(void)
1821 #else
1822 static void openFiles()
1823 #endif
1824 {
1825 char str[256];
1826 char *usr = "";
1827 time_t tick;
1828
1829 /* Open Acode file */
1830 strcpy(codfnm, advnam);
1831 strcat(codfnm, ".acd");
1832 if ((codfil = fopen(codfnm, READ_MODE)) == NULL) {
1833 strcpy(str, "Can't open adventure code file '");
1834 strcat(str, codfnm);
1835 strcat(str, "'.");
1836 syserr(str);
1837 }
1838
1839 #ifdef GARGLK
1840 {
1841 char *s = strrchr(codfnm, '\\');
1842 if (!s) s = strrchr(codfnm, '/');
1843 garglk_set_story_name(s ? s + 1 : codfnm);
1844 }
1845 #endif
1846
1847 /* Open Text file */
1848 strcpy(txtfnm, advnam);
1849 strcat(txtfnm, ".dat");
1850 if ((txtfil = fopen(txtfnm, READ_MODE)) == NULL) {
1851 strcpy(str, "Can't open adventure text data file '");
1852 strcat(str, txtfnm);
1853 strcat(str, "'.");
1854 syserr(str);
1855 }
1856
1857 /* If logging open log file */
1858 if (logflg) {
1859 char *namstart;
1860
1861 if((namstart = strrchr(advnam, ']')) == NULL
1862 && (namstart = strrchr(advnam, '>')) == NULL
1863 && (namstart = strrchr(advnam, '/')) == NULL
1864 && (namstart = strrchr(advnam, '\\')) == NULL
1865 && (namstart = strrchr(advnam, ':')) == NULL)
1866 namstart = &advnam[0];
1867 else
1868 namstart++;
1869
1870 time(&tick);
1871 sprintf(logfnm, "%s%d%s.log", namstart, (int)tick, usr);
1872 if ((logfil = fopen(logfnm, "w")) == NULL)
1873 logflg = FALSE;
1874 }
1875 }
1876
1877
1878 /*======================================================================
1879
1880 run()
1881
1882 Run the adventure
1883
1884 */
1885 void run(void)
1886 {
1887 openFiles();
1888
1889 setjmp(restart_label); /* Return here if he wanted to restart */
1890
1891 init(); /* Load, initialise and start the adventure */
1892
1893 while (TRUE) {
1894 #ifdef MALLOC
1895 if (malloc_verify() == 0) syserr("Error in heap.");
1896 #endif
1897 if (dbgflg)
1898 debug();
1899
1900 eventchk();
1901 cur.tick++;
1902 (void) setjmp(jmpbuf);
1903
1904 /* Move all characters */
1905 for (cur.act = ACTMIN; cur.act <= ACTMAX; cur.act++)
1906 movactor();
1907 }
1908 }
1909