Debian Codegraph @

terps/level9/level9.c

raw symbols SCIP
1 /***********************************************************************\
2 *
3 * Level 9 interpreter
4 * Version 5.2
5 * Copyright (c) 1996-2025 Glen Summers and contributors.
6 * Contributions from David Kinder, Alan Staniforth, Simon Baldwin,
7 * Dieter Baron and Andreas Scherrer.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
22 *
23 * The input routine will repond to the following 'hash' commands
24 * #save saves position file directly (bypasses any
25 * disk change prompts)
26 * #restore restores position file directly (bypasses any
27 * protection code)
28 * #quit terminates current game, RunGame() will return FALSE
29 * #cheat tries to bypass restore protection on v3,4 games
30 * (can be slow)
31 * #dictionary lists game dictionary (press a key to interrupt)
32 * #picture <n> show picture <n>
33 * #seed <n> set the random number seed to the value <n>
34 * #play plays back a file as the input to the game
35 *
36 \***********************************************************************/
37
38 #include <ctype.h>
39 #include <math.h>
40 #include <stdarg.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <time.h>
45
46 #include "level9.h"
47
48 /* #define L9DEBUG */
49 /* #define CODEFOLLOW */
50 /* #define FULLSCAN */
51
52 /* "L901" */
53 #define L9_ID 0x4c393031
54
55 #define IBUFFSIZE 500
56 #define RAMSAVESLOTS 10
57 #define GFXSTACKSIZE 100
58 #define FIRSTLINESIZE 96
59
60 /* Typedefs */
61 typedef struct
62 {
63 L9UINT16 vartable[256];
64 L9BYTE listarea[LISTAREASIZE];
65 } SaveStruct;
66
67 /* Enumerations */
68 enum L9GameTypes { L9_V1, L9_V2, L9_V3, L9_V4 };
69 enum L9MsgTypes { MSGT_V1, MSGT_V2 };
70 /*
71 Graphics type Resolution Scale stack reset
72 -------------------------------------------------
73 GFX_V2 160 x 128 yes
74 GFX_V3A 160 x 96 yes
75 GFX_V3B 160 x 96 no
76 GFX_V3C 320 x 96 no
77 */
78 enum L9GfxTypes { GFX_V2, GFX_V3A, GFX_V3B, GFX_V3C };
79
80 /* Global Variables */
81 L9BYTE* startfile=NULL,*pictureaddress=NULL,*picturedata=NULL;
82 L9BYTE* startdata;
83 L9UINT32 FileSize,picturesize;
84
85 L9BYTE *L9Pointers[12];
86 L9BYTE *absdatablock,*list2ptr,*list3ptr,*list9startptr,*acodeptr;
87 L9BYTE *startmd,*endmd,*endwdp5,*wordtable,*dictdata,*defdict;
88 L9UINT16 dictdatalen;
89 L9BYTE *startmdV2;
90
91 int wordcase;
92 int unpackcount;
93 char unpackbuf[8];
94 L9BYTE* dictptr;
95 char threechars[34];
96 int L9GameType;
97 int L9MsgType;
98 char LastGame[MAX_PATH];
99 char FirstLine[FIRSTLINESIZE];
100 int FirstLinePos=0;
101 int FirstPicture=-1;
102
103 #if defined(AMIGA) && defined(_DCC)
104 __far SaveStruct ramsavearea[RAMSAVESLOTS];
105 #else
106 SaveStruct ramsavearea[RAMSAVESLOTS];
107 #endif
108
109 GameState workspace;
110
111 L9UINT16 randomseed;
112 L9UINT16 constseed=0;
113 L9BOOL Running;
114
115 char ibuff[IBUFFSIZE];
116 L9BYTE* ibuffptr;
117 char obuff[34];
118 FILE* scriptfile=NULL;
119
120 L9BOOL Cheating=FALSE;
121 int CheatWord;
122 GameState CheatWorkspace;
123
124 int reflectflag,scale,gintcolour,option;
125 int l9textmode=0,drawx=0,drawy=0,screencalled=0,showtitle=1;
126 L9BYTE *gfxa5=NULL;
127 Bitmap* bitmap=NULL;
128 int gfx_mode=GFX_V2;
129
130 L9BYTE* GfxA5Stack[GFXSTACKSIZE];
131 int GfxA5StackPos=0;
132 int GfxScaleStack[GFXSTACKSIZE];
133 int GfxScaleStackPos=0;
134
135 char lastchar='.';
136 char lastactualchar=0;
137 int d5;
138
139 L9BYTE* codeptr; /* instruction codes */
140 L9BYTE code;
141
142 L9BYTE* list9ptr;
143
144 int unpackd3;
145
146 L9BYTE exitreversaltable[20]= {0x00,0x04,0x06,0x07,0x01,0x08,0x02,0x03,0x05,0x0a,0x09,0x0c,0x0b,0xff,0xff,0x0f,0xff,0xff,0xff,0xff};
147
148 L9UINT16 gnostack[128];
149 L9BYTE gnoscratch[32];
150 int object,gnosp,numobjectfound,searchdepth,inithisearchpos;
151
152
153 struct L9V1GameInfo
154 {
155 L9BYTE dictVal1, dictVal2;
156 int dictStart, L9Ptrs[5], absData, msgStart, msgLen;
157 };
158 struct L9V1GameInfo L9V1Games[] =
159 {
160 0x1a,0x24,301, 0x0000,-0x004b, 0x0080,-0x002b, 0x00d0,0x03b0, 0x0f80,0x4857, /* Colossal Adventure */
161 0x20,0x3b,283,-0x0583, 0x0000,-0x0508,-0x04e0, 0x0000,0x0800, 0x1000,0x39d1, /* Adventure Quest */
162 0x14,0xff,153,-0x00d6, 0x0000, 0x0000, 0x0000, 0x0000,0x0a20, 0x16bf,0x420d, /* Dungeon Adventure */
163 0x15,0x5d,252,-0x3e70, 0x0000,-0x3d30,-0x3ca0, 0x0100,0x4120,-0x3b9d,0x3988, /* Lords of Time */
164 0x15,0x6c,284,-0x00f0, 0x0000,-0x0050,-0x0050,-0x0050,0x0300, 0x1930,0x3c17, /* Snowball */
165 };
166 int L9V1Game = -1;
167
168
169 /* Prototypes */
170 L9BOOL LoadGame2(char *filename,char *picname);
171 int getlongcode(void);
172 L9BOOL GetWordV2(char *buff,int Word);
173 L9BOOL GetWordV3(char *buff,int Word);
174 void show_picture(int pic);
175
176
177 #ifdef CODEFOLLOW
178 #define CODEFOLLOWFILE "c:\\temp\\level9.txt"
179 FILE *f;
180 L9UINT16 *cfvar,*cfvar2;
181 char *codes[]=
182 {
183 "Goto",
184 "intgosub",
185 "intreturn",
186 "printnumber",
187 "messagev",
188 "messagec",
189 "function",
190 "input",
191 "varcon",
192 "varvar",
193 "_add",
194 "_sub",
195 "ilins",
196 "ilins",
197 "jump",
198 "Exit",
199 "ifeqvt",
200 "ifnevt",
201 "ifltvt",
202 "ifgtvt",
203 "screen",
204 "cleartg",
205 "picture",
206 "getnextobject",
207 "ifeqct",
208 "ifnect",
209 "ifltct",
210 "ifgtct",
211 "printinput",
212 "ilins",
213 "ilins",
214 "ilins",
215 };
216 char *functions[]=
217 {
218 "calldriver",
219 "L9Random",
220 "save",
221 "restore",
222 "clearworkspace",
223 "clearstack"
224 };
225 char *drivercalls[]=
226 {
227 "init",
228 "drivercalcchecksum",
229 "driveroswrch",
230 "driverosrdch",
231 "driverinputline",
232 "driversavefile",
233 "driverloadfile",
234 "settext",
235 "resettask",
236 "returntogem",
237 "10 *",
238 "loadgamedatafile",
239 "randomnumber",
240 "13 *",
241 "driver14",
242 "15 *",
243 "driverclg",
244 "line",
245 "fill",
246 "driverchgcol",
247 "20 *",
248 "21 *",
249 "ramsave",
250 "ramload",
251 "24 *",
252 "lensdisplay",
253 "26 *",
254 "27 *",
255 "28 *",
256 "29 *",
257 "allocspace",
258 "31 *",
259 "showbitmap",
260 "33 *",
261 "checkfordisc"
262 };
263 #endif
264
265 void initdict(L9BYTE *ptr)
266 {
267 dictptr=ptr;
268 unpackcount=8;
269 }
270
271 char getdictionarycode(void)
272 {
273 if (unpackcount!=8) return unpackbuf[unpackcount++];
274 else
275 {
276 /* unpackbytes */
277 L9BYTE d1=*dictptr++,d2;
278 unpackbuf[0]=d1>>3;
279 d2=*dictptr++;
280 unpackbuf[1]=((d2>>6) + (d1<<2)) & 0x1f;
281 d1=*dictptr++;
282 unpackbuf[2]=(d2>>1) & 0x1f;
283 unpackbuf[3]=((d1>>4) + (d2<<4)) & 0x1f;
284 d2=*dictptr++;
285 unpackbuf[4]=((d1<<1) + (d2>>7)) & 0x1f;
286 d1=*dictptr++;
287 unpackbuf[5]=(d2>>2) & 0x1f;
288 unpackbuf[6]=((d2<<3) + (d1>>5)) & 0x1f;
289 unpackbuf[7]=d1 & 0x1f;
290 unpackcount=1;
291 return unpackbuf[0];
292 }
293 }
294
295 int getdictionary(int d0)
296 {
297 if (d0>=0x1a) return getlongcode();
298 else return d0+0x61;
299 }
300
301 int getlongcode(void)
302 {
303 int d0,d1;
304 d0=getdictionarycode();
305 if (d0==0x10)
306 {
307 wordcase=1;
308 d0=getdictionarycode();
309 return getdictionary(d0); /* reentrant? */
310 }
311 d1=getdictionarycode();
312 return 0x80 | ((d0<<5) & 0xe0) | (d1 & 0x1f);
313 }
314
315 void printchar(char c)
316 {
317 if (Cheating) return;
318
319 if (c&128)
320 lastchar=(c&=0x7f);
321 else if (c!=0x20 && c!=0x0d && (c<'\"' || c>='.'))
322 {
323 if (lastchar=='!' || lastchar=='?' || lastchar=='.') c=toupper(c);
324 lastchar=c;
325 }
326 /* eat multiple CRs */
327 if (c!=0x0d || lastactualchar!=0x0d)
328 {
329 os_printchar(c);
330 if (FirstLinePos < FIRSTLINESIZE-1)
331 FirstLine[FirstLinePos++]=tolower(c);
332 }
333 lastactualchar=c;
334 }
335
336 void printstring(char*buf)
337 {
338 int i;
339 for (i=0;i< (int) strlen(buf);i++) printchar(buf[i]);
340 }
341
342 void printdecimald0(int d0)
343 {
344 char temp[12];
345 sprintf(temp,"%d",d0);
346 printstring(temp);
347 }
348
349 void error(char *fmt,...)
350 {
351 char buf[256];
352 int i;
353 va_list ap;
354 va_start(ap,fmt);
355 vsprintf(buf,fmt,ap);
356 va_end(ap);
357 for (i=0;i< (int) strlen(buf);i++)
358 os_printchar(buf[i]);
359 }
360
361 void printautocase(int d0)
362 {
363 if (d0 & 128) printchar((char) d0);
364 else
365 {
366 if (wordcase) printchar((char) toupper(d0));
367 else if (d5<6) printchar((char) d0);
368 else
369 {
370 wordcase=0;
371 printchar((char) toupper(d0));
372 }
373 }
374 }
375
376 void displaywordref(L9UINT16 Off)
377 {
378 static int mdtmode=0;
379
380 wordcase=0;
381 d5=(Off>>12)&7;
382 Off&=0xfff;
383 if (Off<0xf80)
384 {
385 /* dwr01 */
386 L9BYTE *a0,*oPtr,*a3;
387 int d0,d2,i;
388
389 if (mdtmode==1) printchar(0x20);
390 mdtmode=1;
391
392 /* setindex */
393 a0=dictdata;
394 d2=dictdatalen;
395
396 /* dwr02 */
397 oPtr=a0;
398 while (d2 && Off >= L9WORD(a0+2))
399 {
400 a0+=4;
401 d2--;
402 }
403 /* dwr04 */
404 if (a0==oPtr)
405 {
406 a0=defdict;
407 }
408 else
409 {
410 a0-=4;
411 Off-=L9WORD(a0+2);
412 a0=startdata+L9WORD(a0);
413 }
414 /* dwr04b */
415 Off++;
416 initdict(a0);
417 a3=(L9BYTE*) threechars; /* a3 not set in original, prevent possible spam */
418
419 /* dwr05 */
420 while (TRUE)
421 {
422 d0=getdictionarycode();
423 if (d0<0x1c)
424 {
425 /* dwr06 */
426 if (d0>=0x1a) d0=getlongcode();
427 else d0+=0x61;
428 *a3++=d0;
429 }
430 else
431 {
432 d0&=3;
433 a3=(L9BYTE*) threechars+d0;
434 if (--Off==0) break;
435 }
436 }
437 for (i=0;i<d0;i++) printautocase(threechars[i]);
438
439 /* dwr10 */
440 while (TRUE)
441 {
442 d0=getdictionarycode();
443 if (d0>=0x1b) return;
444 printautocase(getdictionary(d0));
445 }
446 }
447
448 else
449 {
450 if (d5&2) printchar(0x20); /* prespace */
451 mdtmode=2;
452 Off&=0x7f;
453 if (Off!=0x7e) printchar((char)Off);
454 if (d5&1) printchar(0x20); /* postspace */
455 }
456 }
457
458 int getmdlength(L9BYTE **Ptr)
459 {
460 int tot=0,len;
461 do
462 {
463 len=(*(*Ptr)++ -1) & 0x3f;
464 tot+=len;
465 } while (len==0x3f);
466 return tot;
467 }
468
469 void printmessage(int Msg)
470 {
471 L9BYTE* Msgptr=startmd;
472 L9BYTE Data;
473
474 int len;
475 L9UINT16 Off;
476
477 while (Msg>0 && Msgptr-endmd<=0)
478 {
479 Data=*Msgptr;
480 if (Data&128)
481 {
482 Msgptr++;
483 Msg-=Data&0x7f;
484 }
485 else {
486 len=getmdlength(&Msgptr);
487 Msgptr+=len;
488 }
489 Msg--;
490 }
491 if (Msg<0 || *Msgptr & 128) return;
492
493 len=getmdlength(&Msgptr);
494 if (len==0) return;
495
496 while (len)
497 {
498 Data=*Msgptr++;
499 len--;
500 if (Data&128)
501 {
502 /* long form (reverse word) */
503 Off=(Data<<8) + *Msgptr++;
504 len--;
505 }
506 else
507 {
508 Off=(wordtable[Data*2]<<8) + wordtable[Data*2+1];
509 }
510 if (Off==0x8f80) break;
511 displaywordref(Off);
512 }
513 }
514
515 /* v2 message stuff */
516
517 int msglenV2(L9BYTE **ptr)
518 {
519 int i=0;
520 L9BYTE a;
521
522 /* catch berzerking code */
523 if (*ptr >= startdata+FileSize) return 0;
524
525 while ((a=**ptr)==0)
526 {
527 (*ptr)++;
528
529 if (*ptr >= startdata+FileSize) return 0;
530
531 i+=255;
532 }
533 i+=a;
534 return i;
535 }
536
537 void printcharV2(char c)
538 {
539 if (c==0x25) c=0xd;
540 else if (c==0x5f) c=0x20;
541 printautocase(c);
542 }
543
544 void displaywordV2(L9BYTE *ptr,int msg)
545 {
546 int n;
547 L9BYTE a;
548 if (msg==0) return;
549 while (--msg)
550 {
551 ptr+=msglenV2(&ptr);
552 }
553 n=msglenV2(&ptr);
554
555 while (--n>0)
556 {
557 a=*++ptr;
558 if (a<3) return;
559
560 if (a>=0x5e) displaywordV2(startmdV2-1,a-0x5d);
561 else printcharV2((char)(a+0x1d));
562 }
563 }
564
565 int msglenV1(L9BYTE **ptr)
566 {
567 L9BYTE *ptr2=*ptr;
568 while (ptr2<startdata+FileSize && *ptr2++!=1);
569 return ptr2-*ptr;
570 }
571
572 void displaywordV1(L9BYTE *ptr,int msg)
573 {
574 int n;
575 L9BYTE a;
576 while (msg--)
577 {
578 ptr+=msglenV1(&ptr);
579 }
580 n=msglenV1(&ptr);
581
582 while (--n>0)
583 {
584 a=*ptr++;
585 if (a<3) return;
586
587 if (a>=0x5e) displaywordV1(startmdV2,a-0x5e);
588 else printcharV2((char)(a+0x1d));
589 }
590 }
591
592 L9BOOL amessageV2(L9BYTE *ptr,int msg,long *w,long *c)
593 {
594 int n;
595 L9BYTE a;
596 static int depth=0;
597 if (msg==0) return FALSE;
598 while (--msg)
599 {
600 ptr+=msglenV2(&ptr);
601 }
602 if (ptr >= startdata+FileSize) return FALSE;
603 n=msglenV2(&ptr);
604
605 while (--n>0)
606 {
607 a=*++ptr;
608 if (a<3) return TRUE;
609
610 if (a>=0x5e)
611 {
612 if (++depth>10 || !amessageV2(startmdV2-1,a-0x5d,w,c))
613 {
614 depth--;
615 return FALSE;
616 }
617 depth--;
618 }
619 else
620 {
621 char ch=a+0x1d;
622 if (ch==0x5f || ch==' ') (*w)++;
623 else (*c)++;
624 }
625 }
626 return TRUE;
627 }
628
629 L9BOOL amessageV1(L9BYTE *ptr,int msg,long *w,long *c)
630 {
631 int n;
632 L9BYTE a;
633 static int depth=0;
634
635 while (msg--)
636 {
637 ptr+=msglenV1(&ptr);
638 }
639 if (ptr >= startdata+FileSize) return FALSE;
640 n=msglenV1(&ptr);
641
642 while (--n>0)
643 {
644 a=*ptr++;
645 if (a<3) return TRUE;
646
647 if (a>=0x5e)
648 {
649 if (++depth>10 || !amessageV1(startmdV2,a-0x5e,w,c))
650 {
651 depth--;
652 return FALSE;
653 }
654 depth--;
655 }
656 else
657 {
658 char ch=a+0x1d;
659 if (ch==0x5f || ch==' ') (*w)++;
660 else (*c)++;
661 }
662 }
663 return TRUE;
664 }
665
666 L9BOOL analyseV2(double *wl)
667 {
668 long words=0,chars=0;
669 int i;
670 for (i=1;i<256;i++)
671 {
672 long w=0,c=0;
673 if (amessageV2(startmd,i,&w,&c))
674 {
675 words+=w;
676 chars+=c;
677 }
678 else return FALSE;
679 }
680 *wl=words ? (double) chars/words : 0.0;
681 return TRUE;
682 }
683
684 L9BOOL analyseV1(double *wl)
685 {
686 long words=0,chars=0;
687 int i;
688 for (i=0;i<256;i++)
689 {
690 long w=0,c=0;
691 if (amessageV1(startmd,i,&w,&c))
692 {
693 words+=w;
694 chars+=c;
695 }
696 else return FALSE;
697 }
698
699 *wl=words ? (double) chars/words : 0.0;
700 return TRUE;
701 }
702
703 void printmessageV2(int Msg)
704 {
705 if (L9MsgType==MSGT_V2) displaywordV2(startmd,Msg);
706 else displaywordV1(startmd,Msg);
707 }
708
709 L9UINT32 filelength(FILE *f)
710 {
711 L9UINT32 pos,FileSize;
712
713 pos=ftell(f);
714 fseek(f,0,SEEK_END);
715 FileSize=ftell(f);
716 fseek(f,pos,SEEK_SET);
717 return FileSize;
718 }
719
720 void L9Allocate(L9BYTE **ptr,L9UINT32 Size)
721 {
722 if (*ptr) free(*ptr);
723 *ptr=malloc(Size);
724 if (*ptr==NULL)
725 {
726 fprintf(stderr,"Unable to allocate memory for the game! Exiting...\n");
727 exit(0);
728 }
729 }
730
731 void FreeMemory(void)
732 {
733 if (startfile)
734 {
735 free(startfile);
736 startfile=NULL;
737 }
738 if (pictureaddress)
739 {
740 free(pictureaddress);
741 pictureaddress=NULL;
742 }
743 if (bitmap)
744 {
745 free(bitmap);
746 bitmap=NULL;
747 }
748 if (scriptfile)
749 {
750 fclose(scriptfile);
751 scriptfile=NULL;
752 }
753 picturedata=NULL;
754 picturesize=0;
755 gfxa5=NULL;
756 }
757
758 L9BOOL load(char *filename)
759 {
760 FILE *f=fopen(filename,"rb");
761 if (!f) return FALSE;
762
763 if ((FileSize=filelength(f)) < 256)
764 {
765 fclose(f);
766 error("\rFile is too small to contain a Level 9 game\r");
767 return FALSE;
768 }
769
770 L9Allocate(&startfile,FileSize);
771 if (fread(startfile,1,FileSize,f)!=FileSize)
772 {
773 fclose(f);
774 return FALSE;
775 }
776 fclose(f);
777 return TRUE;
778 }
779
780 L9UINT16 scanmovewa5d0(L9BYTE* Base,L9UINT32 *Pos)
781 {
782 L9UINT16 ret=L9WORD(Base+*Pos);
783 (*Pos)+=2;
784 return ret;
785 }
786
787 L9UINT32 scangetaddr(int Code,L9BYTE *Base,L9UINT32 *Pos,L9UINT32 acode,int *Mask)
788 {
789 (*Mask)|=0x20;
790 if (Code&0x20)
791 {
792 /* getaddrshort */
793 signed char diff=Base[*Pos];
794 (*Pos)++;
795 return (*Pos)+diff-1;
796 }
797 else
798 {
799 return acode+scanmovewa5d0(Base,Pos);
800 }
801 }
802
803 void scangetcon(int Code,L9UINT32 *Pos,int *Mask)
804 {
805 (*Pos)++;
806 if (!(Code & 64)) (*Pos)++;
807 (*Mask)|=0x40;
808 }
809
810 L9BOOL CheckCallDriverV4(L9BYTE* Base,L9UINT32 Pos)
811 {
812 int i,j;
813
814 /* Look back for an assignment from a variable
815 * to list9[0], which is used to specify the
816 * driver call.
817 */
818 for (i = 0; i < 2; i++)
819 {
820 int x = Pos - ((i+1)*3);
821 if ((Base[x] == 0x89) && (Base[x+1] == 0x00))
822 {
823 /* Get the variable being copied to list9[0] */
824 int var = Base[x+2];
825
826 /* Look back for an assignment to the variable. */
827 for (j = 0; j < 2; j++)
828 {
829 int y = x - ((j+1)*3);
830 if ((Base[y] == 0x48) && (Base[y+2] == var))
831 {
832 /* If this a V4 driver call? */
833 switch (Base[y+1])
834 {
835 case 0x0E:
836 case 0x20:
837 case 0x22:
838 return TRUE;
839 }
840 return FALSE;
841 }
842 }
843 }
844 }
845 return FALSE;
846 }
847
848 L9BOOL ValidateSequence(L9BYTE* Base,L9BYTE* Image,L9UINT32 iPos,L9UINT32 acode,L9UINT32 *Size,L9UINT32 FileSize,L9UINT32 *Min,L9UINT32 *Max,L9BOOL Rts,L9BOOL *JumpKill, L9BOOL *DriverV4)
849 {
850 L9UINT32 Pos;
851 L9BOOL Finished=FALSE,Valid;
852 L9UINT32 Strange=0;
853 int ScanCodeMask;
854 int Code;
855 *JumpKill=FALSE;
856
857 if (iPos>=FileSize)
858 return FALSE;
859 Pos=iPos;
860 if (Pos<*Min) *Min=Pos;
861
862 if (Image[Pos]) return TRUE; /* hit valid code */
863
864 do
865 {
866 Code=Base[Pos];
867 Valid=TRUE;
868 if (Image[Pos]) break; /* converged to found code */
869 Image[Pos++]=2;
870 if (Pos>*Max) *Max=Pos;
871
872 ScanCodeMask=0x9f;
873 if (Code&0x80)
874 {
875 ScanCodeMask=0xff;
876 if ((Code&0x1f)>0xa)
877 Valid=FALSE;
878 Pos+=2;
879 }
880 else switch (Code & 0x1f)
881 {
882 case 0: /* goto */
883 {
884 L9UINT32 Val=scangetaddr(Code,Base,&Pos,acode,&ScanCodeMask);
885 Valid=ValidateSequence(Base,Image,Val,acode,Size,FileSize,Min,Max,TRUE/*Rts*/,JumpKill,DriverV4);
886 Finished=TRUE;
887 break;
888 }
889 case 1: /* intgosub */
890 {
891 L9UINT32 Val=scangetaddr(Code,Base,&Pos,acode,&ScanCodeMask);
892 Valid=ValidateSequence(Base,Image,Val,acode,Size,FileSize,Min,Max,TRUE,JumpKill,DriverV4);
893 break;
894 }
895 case 2: /* intreturn */
896 Valid=Rts;
897 Finished=TRUE;
898 break;
899 case 3: /* printnumber */
900 Pos++;
901 break;
902 case 4: /* messagev */
903 Pos++;
904 break;
905 case 5: /* messagec */
906 scangetcon(Code,&Pos,&ScanCodeMask);
907 break;
908 case 6: /* function */
909 switch (Base[Pos++])
910 {
911 case 2:/* random */
912 Pos++;
913 break;
914 case 1:/* calldriver */
915 if (DriverV4)
916 {
917 if (CheckCallDriverV4(Base,Pos-2))
918 *DriverV4 = TRUE;
919 }
920 break;
921 case 3:/* save */
922 case 4:/* restore */
923 case 5:/* clearworkspace */
924 case 6:/* clear stack */
925 break;
926 case 250: /* printstr */
927 while (Base[Pos++]);
928 break;
929
930 default:
931 #ifdef L9DEBUG
932 /* printf("scan: illegal function call: %d",Base[Pos-1]); */
933 #endif
934 Valid=FALSE;
935 break;
936 }
937 break;
938 case 7: /* input */
939 Pos+=4;
940 break;
941 case 8: /* varcon */
942 scangetcon(Code,&Pos,&ScanCodeMask);
943 Pos++;
944 break;
945 case 9: /* varvar */
946 Pos+=2;
947 break;
948 case 10: /* _add */
949 Pos+=2;
950 break;
951 case 11: /* _sub */
952 Pos+=2;
953 break;
954 case 14: /* jump */
955 #ifdef L9DEBUG
956 /* printf("jmp at codestart: %ld",acode); */
957 #endif
958 *JumpKill=TRUE;
959 Finished=TRUE;
960 break;
961 case 15: /* exit */
962 Pos+=4;
963 break;
964 case 16: /* ifeqvt */
965 case 17: /* ifnevt */
966 case 18: /* ifltvt */
967 case 19: /* ifgtvt */
968 {
969 L9UINT32 Val;
970 Pos+=2;
971 Val=scangetaddr(Code,Base,&Pos,acode,&ScanCodeMask);
972 Valid=ValidateSequence(Base,Image,Val,acode,Size,FileSize,Min,Max,Rts,JumpKill,DriverV4);
973 break;
974 }
975 case 20: /* screen */
976 if (Base[Pos++]) Pos++;
977 break;
978 case 21: /* cleartg */
979 Pos++;
980 break;
981 case 22: /* picture */
982 Pos++;
983 break;
984 case 23: /* getnextobject */
985 Pos+=6;
986 break;
987 case 24: /* ifeqct */
988 case 25: /* ifnect */
989 case 26: /* ifltct */
990 case 27: /* ifgtct */
991 {
992 L9UINT32 Val;
993 Pos++;
994 scangetcon(Code,&Pos,&ScanCodeMask);
995 Val=scangetaddr(Code,Base,&Pos,acode,&ScanCodeMask);
996 Valid=ValidateSequence(Base,Image,Val,acode,Size,FileSize,Min,Max,Rts,JumpKill,DriverV4);
997 break;
998 }
999 case 28: /* printinput */
1000 break;
1001 case 12: /* ilins */
1002 case 13: /* ilins */
1003 case 29: /* ilins */
1004 case 30: /* ilins */
1005 case 31: /* ilins */
1006 #ifdef L9DEBUG
1007 /* printf("scan: illegal instruction"); */
1008 #endif
1009 Valid=FALSE;
1010 break;
1011 }
1012 if (Valid && (Code & ~ScanCodeMask))
1013 Strange++;
1014 } while (Valid && !Finished && Pos<FileSize); /* && Strange==0); */
1015 (*Size)+=Pos-iPos;
1016 return Valid; /* && Strange==0; */
1017 }
1018
1019 L9BYTE calcchecksum(L9BYTE* ptr,L9UINT32 num)
1020 {
1021 L9BYTE d1=0;
1022 while (num--!=0) d1+=*ptr++;
1023 return d1;
1024 }
1025
1026 /*
1027 L9BOOL Check(L9BYTE* StartFile,L9UINT32 FileSize,L9UINT32 Offset)
1028 {
1029 L9UINT16 d0,num;
1030 int i;
1031 L9BYTE* Image;
1032 L9UINT32 Size=0,Min,Max;
1033 L9BOOL ret,JumpKill;
1034
1035 for (i=0;i<12;i++)
1036 {
1037 d0=L9WORD (StartFile+Offset+0x12 + i*2);
1038 if (d0>=0x8000+LISTAREASIZE) return FALSE;
1039 }
1040
1041 num=L9WORD(StartFile+Offset)+1;
1042 if (Offset+num>FileSize) return FALSE;
1043 if (calcchecksum(StartFile+Offset,num)) return FALSE;
1044
1045 Image=calloc(FileSize,1);
1046
1047 Min=Max=Offset+d0;
1048 ret=ValidateSequence(StartFile,Image,Offset+d0,Offset+d0,&Size,FileSize,&Min,&Max,FALSE,&JumpKill,NULL);
1049 free(Image);
1050 return ret;
1051 }
1052 */
1053
1054 long Scan(L9BYTE* StartFile,L9UINT32 FileSize)
1055 {
1056 L9BYTE *Chk=malloc(FileSize+1);
1057 L9BYTE *Image=calloc(FileSize,1);
1058 L9UINT32 i,num,Size,MaxSize=0;
1059 int j;
1060 L9UINT16 d0=0,l9,md,ml,dd,dl;
1061 L9UINT32 Min,Max;
1062 long Offset=-1;
1063 L9BOOL JumpKill, DriverV4;
1064
1065 if ((Chk==NULL)||(Image==NULL))
1066 {
1067 fprintf(stderr,"Unable to allocate memory for game scan! Exiting...\n");
1068 exit(0);
1069 }
1070
1071 Chk[0]=0;
1072 for (i=1;i<=FileSize;i++)
1073 Chk[i]=Chk[i-1]+StartFile[i-1];
1074
1075 for (i=0;i<FileSize-33;i++)
1076 {
1077 num=L9WORD(StartFile+i)+1;
1078 /*
1079 Chk[i] = 0 +...+ i-1
1080 Chk[i+n] = 0 +...+ i+n-1
1081 Chk[i+n] - Chk[i] = i + ... + i+n
1082 */
1083 if (num>0x2000 && i+num<=FileSize && Chk[i+num]==Chk[i])
1084 {
1085 md=L9WORD(StartFile+i+0x2);
1086 ml=L9WORD(StartFile+i+0x4);
1087 dd=L9WORD(StartFile+i+0xa);
1088 dl=L9WORD(StartFile+i+0xc);
1089
1090 if (ml>0 && md>0 && i+md+ml<=FileSize && dd>0 && dl>0 && i+dd+dl*4<=FileSize)
1091 {
1092 /* v4 files may have acodeptr in 8000-9000, need to fix */
1093 for (j=0;j<12;j++)
1094 {
1095 d0=L9WORD (StartFile+i+0x12 + j*2);
1096 if (j!=11 && d0>=0x8000 && d0<0x9000)
1097 {
1098 if (d0>=0x8000+LISTAREASIZE) break;
1099 }
1100 else if (i+d0>FileSize) break;
1101 }
1102 /* list9 ptr must be in listarea, acode ptr in data */
1103 if (j<12 /*|| (d0>=0x8000 && d0<0x9000)*/) continue;
1104
1105 l9=L9WORD(StartFile+i+0x12 + 10*2);
1106 if (l9<0x8000 || l9>=0x8000+LISTAREASIZE) continue;
1107
1108 Size=0;
1109 Min=Max=i+d0;
1110 DriverV4=0;
1111 if (ValidateSequence(StartFile,Image,i+d0,i+d0,&Size,FileSize,&Min,&Max,FALSE,&JumpKill,&DriverV4))
1112 {
1113 #ifdef L9DEBUG
1114 printf("Found valid header at %ld, code size %ld",i,Size);
1115 #endif
1116 if (Size>MaxSize && Size>100)
1117 {
1118 Offset=i;
1119 MaxSize=Size;
1120 L9GameType=DriverV4?L9_V4:L9_V3;
1121 }
1122 }
1123 }
1124 }
1125 }
1126 free(Chk);
1127 free(Image);
1128 return Offset;
1129 }
1130
1131 long ScanV2(L9BYTE* StartFile,L9UINT32 FileSize)
1132 {
1133 L9BYTE *Chk=malloc(FileSize+1);
1134 L9BYTE *Image=calloc(FileSize,1);
1135 L9UINT32 i,Size,MaxSize=0,num;
1136 int j;
1137 L9UINT16 d0=0,l9;
1138 L9UINT32 Min,Max;
1139 long Offset=-1;
1140 L9BOOL JumpKill;
1141
1142 if ((Chk==NULL)||(Image==NULL))
1143 {
1144 fprintf(stderr,"Unable to allocate memory for game scan! Exiting...\n");
1145 exit(0);
1146 }
1147
1148 Chk[0]=0;
1149 for (i=1;i<=FileSize;i++)
1150 Chk[i]=Chk[i-1]+StartFile[i-1];
1151
1152 for (i=0;i<FileSize-28;i++)
1153 {
1154 num=L9WORD(StartFile+i+28)+1;
1155 if (i+num<=FileSize && ((Chk[i+num]-Chk[i+32])&0xff)==StartFile[i+0x1e])
1156 {
1157 for (j=0;j<14;j++)
1158 {
1159 d0=L9WORD (StartFile+i+ j*2);
1160 if (j!=13 && d0>=0x8000 && d0<0x9000)
1161 {
1162 if (d0>=0x8000+LISTAREASIZE) break;
1163 }
1164 else if (i+d0>FileSize) break;
1165 }
1166 /* list9 ptr must be in listarea, acode ptr in data */
1167 if (j<14 /*|| (d0>=0x8000 && d0<0x9000)*/) continue;
1168
1169 l9=L9WORD(StartFile+i+6 + 9*2);
1170 if (l9<0x8000 || l9>=0x8000+LISTAREASIZE) continue;
1171
1172 Size=0;
1173 Min=Max=i+d0;
1174 if (ValidateSequence(StartFile,Image,i+d0,i+d0,&Size,FileSize,&Min,&Max,FALSE,&JumpKill,NULL))
1175 {
1176 #ifdef L9DEBUG
1177 printf("Found valid V2 header at %ld, code size %ld",i,Size);
1178 #endif
1179 if (Size>MaxSize && Size>100)
1180 {
1181 Offset=i;
1182 MaxSize=Size;
1183 }
1184 }
1185 }
1186 }
1187 free(Chk);
1188 free(Image);
1189 return Offset;
1190 }
1191
1192 long ScanV1(L9BYTE* StartFile,L9UINT32 FileSize)
1193 {
1194 L9BYTE *Image=calloc(FileSize,1);
1195 L9UINT32 i,Size;
1196 int Replace;
1197 L9BYTE* ImagePtr;
1198 long MaxPos=-1;
1199 L9UINT32 MaxCount=0;
1200 L9UINT32 Min,Max,MaxMin,MaxMax;
1201 L9BOOL JumpKill,MaxJK;
1202
1203 int dictOff1, dictOff2;
1204 L9BYTE dictVal1 = 0xff, dictVal2 = 0xff;
1205
1206 if (Image==NULL)
1207 {
1208 fprintf(stderr,"Unable to allocate memory for game scan! Exiting...\n");
1209 exit(0);
1210 }
1211
1212 for (i=0;i<FileSize;i++)
1213 {
1214 if ((StartFile[i]==0 && StartFile[i+1]==6) || (StartFile[i]==32 && StartFile[i+1]==4))
1215 {
1216 Size=0;
1217 Min=Max=i;
1218 Replace=0;
1219 if (ValidateSequence(StartFile,Image,i,i,&Size,FileSize,&Min,&Max,FALSE,&JumpKill,NULL))
1220 {
1221 if (Size>MaxCount && Size>100 && Size<10000)
1222 {
1223 MaxCount=Size;
1224 MaxMin=Min;
1225 MaxMax=Max;
1226
1227 MaxPos=i;
1228 MaxJK=JumpKill;
1229 }
1230 Replace=0;
1231 }
1232 for (ImagePtr=Image+Min;ImagePtr<=Image+Max;ImagePtr++)
1233 {
1234 if (*ImagePtr==2)
1235 *ImagePtr=Replace;
1236 }
1237 }
1238 }
1239 #ifdef L9DEBUG
1240 printf("V1scan found code at %ld size %ld",MaxPos,MaxCount);
1241 #endif
1242
1243 /* V1 dictionary detection from L9Cut by Paul David Doherty */
1244 for (i=0;i<FileSize-20;i++)
1245 {
1246 if (StartFile[i]=='A')
1247 {
1248 if (StartFile[i+1]=='T' && StartFile[i+2]=='T' && StartFile[i+3]=='A' && StartFile[i+4]=='C' && StartFile[i+5]==0xcb)
1249 {
1250 dictOff1 = i;
1251 dictVal1 = StartFile[dictOff1+6];
1252 break;
1253 }
1254 }
1255 }
1256 for (i=dictOff1;i<FileSize-20;i++)
1257 {
1258 if (StartFile[i]=='B')
1259 {
1260 if (StartFile[i+1]=='U' && StartFile[i+2]=='N' && StartFile[i+3]=='C' && StartFile[i+4] == 0xc8)
1261 {
1262 dictOff2 = i;
1263 dictVal2 = StartFile[dictOff2+5];
1264 break;
1265 }
1266 }
1267 }
1268 L9V1Game = -1;
1269 if (dictVal1 != 0xff || dictVal2 != 0xff)
1270 {
1271 for (i = 0; i < sizeof L9V1Games / sizeof L9V1Games[0]; i++)
1272 {
1273 if ((L9V1Games[i].dictVal1 == dictVal1) && (L9V1Games[i].dictVal2 == dictVal2))
1274 {
1275 L9V1Game = i;
1276 dictdata = StartFile+dictOff1-L9V1Games[i].dictStart;
1277 }
1278 }
1279 }
1280
1281 #ifdef L9DEBUG
1282 if (L9V1Game >= 0)
1283 printf("V1scan found known dictionary: %d",L9V1Game);
1284 #endif
1285
1286 free(Image);
1287
1288 if (MaxPos>0)
1289 {
1290 acodeptr=StartFile+MaxPos;
1291 return 0;
1292 }
1293 return -1;
1294 }
1295
1296 #ifdef FULLSCAN
1297 void FullScan(L9BYTE* StartFile,L9UINT32 FileSize)
1298 {
1299 L9BYTE *Image=calloc(FileSize,1);
1300 L9UINT32 i,Size;
1301 int Replace;
1302 L9BYTE* ImagePtr;
1303 L9UINT32 MaxPos=0;
1304 L9UINT32 MaxCount=0;
1305 L9UINT32 Min,Max,MaxMin,MaxMax;
1306 int Offset;
1307 L9BOOL JumpKill,MaxJK;
1308 for (i=0;i<FileSize;i++)
1309 {
1310 Size=0;
1311 Min=Max=i;
1312 Replace=0;
1313 if (ValidateSequence(StartFile,Image,i,i,&Size,FileSize,&Min,&Max,FALSE,&JumpKill,NULL))
1314 {
1315 if (Size>MaxCount)
1316 {
1317 MaxCount=Size;
1318 MaxMin=Min;
1319 MaxMax=Max;
1320
1321 MaxPos=i;
1322 MaxJK=JumpKill;
1323 }
1324 Replace=0;
1325 }
1326 for (ImagePtr=Image+Min;ImagePtr<=Image+Max;ImagePtr++)
1327 {
1328 if (*ImagePtr==2)
1329 *ImagePtr=Replace;
1330 }
1331 }
1332 printf("%ld %ld %ld %ld %s",MaxPos,MaxCount,MaxMin,MaxMax,MaxJK ? "jmp killed" : "");
1333 /* search for reference to MaxPos */
1334 Offset=0x12 + 11*2;
1335 for (i=0;i<FileSize-Offset-1;i++)
1336 {
1337 if ((L9WORD(StartFile+i+Offset)) +i==MaxPos)
1338 {
1339 printf("possible v3,4 Code reference at : %ld",i);
1340 /* startdata=StartFile+i; */
1341 }
1342 }
1343 Offset=13*2;
1344 for (i=0;i<FileSize-Offset-1;i++)
1345 {
1346 if ((L9WORD(StartFile+i+Offset)) +i==MaxPos)
1347 printf("possible v2 Code reference at : %ld",i);
1348 }
1349 free(Image);
1350 }
1351 #endif
1352
1353 L9BOOL findsubs(L9BYTE* testptr, L9UINT32 testsize, L9BYTE** picdata, L9UINT32 *picsize)
1354 {
1355 int i, j, length, count;
1356 L9BYTE *picptr, *startptr, *tmpptr;
1357
1358 if (testsize < 16) return FALSE;
1359
1360 /*
1361 Try to traverse the graphics subroutines.
1362
1363 Each subroutine starts with a header: nn | nl | ll
1364 nnn : the subroutine number ( 0x000 - 0x7ff )
1365 lll : the subroutine length ( 0x004 - 0x3ff )
1366
1367 The first subroutine usually has the number 0x000.
1368 Each subroutine ends with 0xff.
1369
1370 findsubs() searches for the header of the second subroutine
1371 (pattern: 0xff | nn | nl | ll) and then tries to find the
1372 first and next subroutines by evaluating the length fields
1373 of the subroutine headers.
1374 */
1375 for (i = 4; i < (int)(testsize - 4); i++)
1376 {
1377 picptr = testptr + i;
1378 if (*(picptr - 1) != 0xff || (*picptr & 0x80) || (*(picptr + 1) & 0x0c) || (*(picptr + 2) < 4))
1379 continue;
1380
1381 count = 0;
1382 startptr = picptr;
1383
1384 while (TRUE)
1385 {
1386 length = ((*(picptr + 1) & 0x0f) << 8) + *(picptr + 2);
1387 if (length > 0x3ff || picptr + length + 4 > testptr + testsize)
1388 break;
1389
1390 picptr += length;
1391 if (*(picptr - 1) != 0xff)
1392 {
1393 picptr -= length;
1394 break;
1395 }
1396 if ((*picptr & 0x80) || (*(picptr + 1) & 0x0c) || (*(picptr + 2) < 4))
1397 break;
1398
1399 count++;
1400 }
1401
1402 if (count > 10)
1403 {
1404 /* Search for the start of the first subroutine */
1405 for (j = 4; j < 0x3ff; j++)
1406 {
1407 tmpptr = startptr - j;
1408 if (*tmpptr == 0xff || tmpptr < testptr)
1409 break;
1410
1411 length = ((*(tmpptr + 1) & 0x0f) << 8) + *(tmpptr + 2);
1412 if (tmpptr + length == startptr)
1413 {
1414 startptr = tmpptr;
1415 break;
1416 }
1417 }
1418
1419 if (*tmpptr != 0xff)
1420 {
1421 *picdata = startptr;
1422 *picsize = picptr - startptr;
1423 return TRUE;
1424 }
1425 }
1426 }
1427 return FALSE;
1428 }
1429
1430 L9BOOL intinitialise(char*filename,char*picname)
1431 {
1432 /* init */
1433 /* driverclg */
1434
1435 int i;
1436 int hdoffset;
1437 long Offset;
1438 FILE *f;
1439
1440 if (pictureaddress)
1441 {
1442 free(pictureaddress);
1443 pictureaddress=NULL;
1444 }
1445 picturedata=NULL;
1446 picturesize=0;
1447 gfxa5=NULL;
1448
1449 if (!load(filename))
1450 {
1451 error("\rUnable to load: %s\r",filename);
1452 return FALSE;
1453 }
1454
1455 /* try to load graphics */
1456 if (picname)
1457 {
1458 f=fopen(picname,"rb");
1459 if (f)
1460 {
1461 picturesize=filelength(f);
1462 L9Allocate(&pictureaddress,picturesize);
1463 if (fread(pictureaddress,1,picturesize,f)!=picturesize)
1464 {
1465 free(pictureaddress);
1466 pictureaddress=NULL;
1467 picturesize=0;
1468 }
1469 fclose(f);
1470 }
1471 }
1472 screencalled=0;
1473 l9textmode=0;
1474
1475 #ifdef FULLSCAN
1476 FullScan(startfile,FileSize);
1477 #endif
1478
1479 Offset=Scan(startfile,FileSize);
1480 if (Offset<0)
1481 {
1482 Offset=ScanV2(startfile,FileSize);
1483 L9GameType=L9_V2;
1484 if (Offset<0)
1485 {
1486 Offset=ScanV1(startfile,FileSize);
1487 L9GameType=L9_V1;
1488 if (Offset<0)
1489 {
1490 error("\rUnable to locate valid Level 9 game in file: %s\r",filename);
1491 return FALSE;
1492 }
1493 }
1494 }
1495
1496 startdata=startfile+Offset;
1497 FileSize-=Offset;
1498
1499 /* setup pointers */
1500 if (L9GameType==L9_V1)
1501 {
1502 if (L9V1Game < 0)
1503 {
1504 error("\rWhat appears to be V1 game data was found, but the game was not recognised.\rEither this is an unknown V1 game file or, more likely, it is corrupted.\r");
1505 return FALSE;
1506 }
1507 for (i=0;i<5;i++)
1508 {
1509 int off=L9V1Games[L9V1Game].L9Ptrs[i];
1510 if (off<0)
1511 L9Pointers[i+2]=acodeptr+off;
1512 else
1513 L9Pointers[i+2]=workspace.listarea+off;
1514 }
1515 absdatablock=acodeptr-L9V1Games[L9V1Game].absData;
1516 }
1517 else
1518 {
1519 /* V2,V3,V4 */
1520 hdoffset=L9GameType==L9_V2 ? 4 : 0x12;
1521 for (i=0;i<12;i++)
1522 {
1523 L9UINT16 d0=L9WORD(startdata+hdoffset+i*2);
1524 L9Pointers[i]= (i!=11 && d0>=0x8000 && d0<=0x9000) ? workspace.listarea+d0-0x8000 : startdata+d0;
1525 }
1526 absdatablock=L9Pointers[0];
1527 dictdata=L9Pointers[1];
1528 list2ptr=L9Pointers[3];
1529 list3ptr=L9Pointers[4];
1530 /*list9startptr */
1531 list9startptr=L9Pointers[10];
1532 acodeptr=L9Pointers[11];
1533 }
1534
1535 switch (L9GameType)
1536 {
1537 case L9_V1:
1538 {
1539 double a1;
1540 startmd=acodeptr+L9V1Games[L9V1Game].msgStart;
1541 startmdV2=startmd+L9V1Games[L9V1Game].msgLen;
1542
1543 if (analyseV1(&a1) && a1>2 && a1<10)
1544 {
1545 L9MsgType=MSGT_V1;
1546 #ifdef L9DEBUG
1547 printf("V1 msg table: wordlen=%.2lf",a1);
1548 #endif
1549 }
1550 else
1551 {
1552 error("\rUnable to identify V1 message table in file: %s\r",filename);
1553 return FALSE;
1554 }
1555 break;
1556 }
1557 case L9_V2:
1558 {
1559 double a2,a1;
1560 startmd=startdata + L9WORD(startdata+0x0);
1561 startmdV2=startdata + L9WORD(startdata+0x2);
1562
1563 /* determine message type */
1564 if (analyseV2(&a2) && a2>2 && a2<10)
1565 {
1566 L9MsgType=MSGT_V2;
1567 #ifdef L9DEBUG
1568 printf("V2 msg table: wordlen=%.2lf",a2);
1569 #endif
1570 }
1571 else if (analyseV1(&a1) && a1>2 && a1<10)
1572 {
1573 L9MsgType=MSGT_V1;
1574 #ifdef L9DEBUG
1575 printf("V1 msg table: wordlen=%.2lf",a1);
1576 #endif
1577 }
1578 else
1579 {
1580 error("\rUnable to identify V2 message table in file: %s\r",filename);
1581 return FALSE;
1582 }
1583 break;
1584 }
1585 case L9_V3:
1586 case L9_V4:
1587 startmd=startdata + L9WORD(startdata+0x2);
1588 endmd=startmd + L9WORD(startdata+0x4);
1589 defdict=startdata + L9WORD(startdata+6);
1590 endwdp5=defdict + 5 + L9WORD(startdata+0x8);
1591 dictdata=startdata + L9WORD(startdata+0x0a);
1592 dictdatalen=L9WORD(startdata+0x0c);
1593 wordtable=startdata + L9WORD(startdata+0xe);
1594 break;
1595 }
1596
1597 #ifndef NO_SCAN_GRAPHICS
1598 /* If there was no graphics file, look in the game data */
1599 if (pictureaddress)
1600 {
1601 if (!findsubs(pictureaddress, picturesize, &picturedata, &picturesize))
1602 {
1603 picturedata = NULL;
1604 picturesize = 0;
1605 }
1606 }
1607 else
1608 {
1609 if (!findsubs(startdata, FileSize, &picturedata, &picturesize)
1610 && !findsubs(startfile, startdata - startfile, &picturedata, &picturesize))
1611 {
1612 picturedata = NULL;
1613 picturesize = 0;
1614 }
1615 }
1616 #endif
1617
1618 memset(FirstLine,0,FIRSTLINESIZE);
1619 FirstLinePos=0;
1620
1621 return TRUE;
1622 }
1623
1624 L9BOOL checksumgamedata(void)
1625 {
1626 return calcchecksum(startdata,L9WORD(startdata)+1)==0;
1627 }
1628
1629 L9UINT16 movewa5d0(void)
1630 {
1631 L9UINT16 ret=L9WORD(codeptr);
1632 codeptr+=2;
1633 return ret;
1634 }
1635
1636 L9UINT16 getcon(void)
1637 {
1638 if (code & 64)
1639 {
1640 /* getconsmall */
1641 return *codeptr++;
1642 }
1643 else return movewa5d0();
1644 }
1645
1646 L9BYTE* getaddr(void)
1647 {
1648 if (code&0x20)
1649 {
1650 /* getaddrshort */
1651 signed char diff=*codeptr++;
1652 return codeptr+ diff-1;
1653 }
1654 else
1655 {
1656 return acodeptr+movewa5d0();
1657 }
1658 }
1659
1660 L9UINT16 *getvar(void)
1661 {
1662 #ifndef CODEFOLLOW
1663 return workspace.vartable + *codeptr++;
1664 #else
1665 cfvar2=cfvar;
1666 return cfvar=workspace.vartable + *codeptr++;
1667 #endif
1668 }
1669
1670 void Goto(void)
1671 {
1672 L9BYTE* target = getaddr();
1673 if (target == codeptr - 2)
1674 Running = FALSE; /* Endless loop! */
1675 else
1676 codeptr = target;
1677 }
1678
1679 void intgosub(void)
1680 {
1681 L9BYTE* newcodeptr=getaddr();
1682 if (workspace.stackptr==STACKSIZE)
1683 {
1684 error("\rStack overflow error\r");
1685 Running=FALSE;
1686 return;
1687 }
1688 workspace.stack[workspace.stackptr++]=(L9UINT16) (codeptr-acodeptr);
1689 codeptr=newcodeptr;
1690 }
1691
1692 void intreturn(void)
1693 {
1694 if (workspace.stackptr==0)
1695 {
1696 error("\rStack underflow error\r");
1697 Running=FALSE;
1698 return;
1699 }
1700 codeptr=acodeptr+workspace.stack[--workspace.stackptr];
1701 }
1702
1703 void printnumber(void)
1704 {
1705 printdecimald0(*getvar());
1706 }
1707
1708 void messagec(void)
1709 {
1710 if (L9GameType<=L9_V2)
1711 printmessageV2(getcon());
1712 else
1713 printmessage(getcon());
1714 }
1715
1716 void messagev(void)
1717 {
1718 if (L9GameType<=L9_V2)
1719 printmessageV2(*getvar());
1720 else
1721 printmessage(*getvar());
1722 }
1723
1724 void init(L9BYTE* a6)
1725 {
1726 #ifdef L9DEBUG
1727 printf("driver - init");
1728 #endif
1729 }
1730
1731 void randomnumber(L9BYTE* a6)
1732 {
1733 #ifdef L9DEBUG
1734 printf("driver - randomnumber");
1735 #endif
1736 L9SETWORD(a6,rand());
1737 }
1738
1739 void driverclg(L9BYTE* a6)
1740 {
1741 #ifdef L9DEBUG
1742 printf("driver - driverclg");
1743 #endif
1744 }
1745
1746 void _line(L9BYTE* a6)
1747 {
1748 #ifdef L9DEBUG
1749 printf("driver - line");
1750 #endif
1751 }
1752
1753 void fill(L9BYTE* a6)
1754 {
1755 #ifdef L9DEBUG
1756 printf("driver - fill");
1757 #endif
1758 }
1759
1760 void driverchgcol(L9BYTE* a6)
1761 {
1762 #ifdef L9DEBUG
1763 printf("driver - driverchgcol");
1764 #endif
1765 }
1766
1767 void drivercalcchecksum(L9BYTE* a6)
1768 {
1769 #ifdef L9DEBUG
1770 printf("driver - calcchecksum");
1771 #endif
1772 }
1773
1774 void driveroswrch(L9BYTE* a6)
1775 {
1776 #ifdef L9DEBUG
1777 printf("driver - driveroswrch");
1778 #endif
1779 }
1780
1781 void driverosrdch(L9BYTE* a6)
1782 {
1783 #ifdef L9DEBUG
1784 printf("driver - driverosrdch");
1785 #endif
1786
1787 os_flush();
1788 if (Cheating) {
1789 *a6 = '\r';
1790 } else {
1791 /* max delay of 1/50 sec */
1792 *a6=os_readchar(20);
1793 }
1794 }
1795
1796 void driversavefile(L9BYTE* a6)
1797 {
1798 #ifdef L9DEBUG
1799 printf("driver - driversavefile");
1800 #endif
1801 }
1802
1803 void driverloadfile(L9BYTE* a6)
1804 {
1805 #ifdef L9DEBUG
1806 printf("driver - driverloadfile");
1807 #endif
1808 }
1809
1810 void settext(L9BYTE* a6)
1811 {
1812 #ifdef L9DEBUG
1813 printf("driver - settext");
1814 #endif
1815 }
1816
1817 void resettask(L9BYTE* a6)
1818 {
1819 #ifdef L9DEBUG
1820 printf("driver - resettask");
1821 #endif
1822 }
1823
1824 void driverinputline(L9BYTE* a6)
1825 {
1826 #ifdef L9DEBUG
1827 printf("driver - driverinputline");
1828 #endif
1829 }
1830
1831 void returntogem(L9BYTE* a6)
1832 {
1833 #ifdef L9DEBUG
1834 printf("driver - returntogem");
1835 #endif
1836 }
1837
1838 void lensdisplay(L9BYTE* a6)
1839 {
1840 #ifdef L9DEBUG
1841 printf("driver - lensdisplay");
1842 #endif
1843
1844 printstring("\rLenslok code is ");
1845 printchar(*a6);
1846 printchar(*(a6+1));
1847 printchar('\r');
1848 }
1849
1850 void allocspace(L9BYTE* a6)
1851 {
1852 #ifdef L9DEBUG
1853 printf("driver - allocspace");
1854 #endif
1855 }
1856
1857 void driver14(L9BYTE *a6)
1858 {
1859 #ifdef L9DEBUG
1860 printf("driver - call 14");
1861 #endif
1862
1863 *a6 = 0;
1864 }
1865
1866 void showbitmap(L9BYTE *a6)
1867 {
1868 #ifdef L9DEBUG
1869 printf("driver - showbitmap");
1870 #endif
1871
1872 os_show_bitmap(a6[1],a6[3],a6[5]);
1873 }
1874
1875 void checkfordisc(L9BYTE *a6)
1876 {
1877 #ifdef L9DEBUG
1878 printf("driver - checkfordisc");
1879 #endif
1880
1881 *a6 = 0;
1882 list9startptr[2] = 0;
1883 }
1884
1885 void driver(int d0,L9BYTE* a6)
1886 {
1887 switch (d0)
1888 {
1889 case 0: init(a6); break;
1890 case 0x0c: randomnumber(a6); break;
1891 case 0x10: driverclg(a6); break;
1892 case 0x11: _line(a6); break;
1893 case 0x12: fill(a6); break;
1894 case 0x13: driverchgcol(a6); break;
1895 case 0x01: drivercalcchecksum(a6); break;
1896 case 0x02: driveroswrch(a6); break;
1897 case 0x03: driverosrdch(a6); break;
1898 case 0x05: driversavefile(a6); break;
1899 case 0x06: driverloadfile(a6); break;
1900 case 0x07: settext(a6); break;
1901 case 0x08: resettask(a6); break;
1902 case 0x04: driverinputline(a6); break;
1903 case 0x09: returntogem(a6); break;
1904 /*
1905 case 0x16: ramsave(a6); break;
1906 case 0x17: ramload(a6); break;
1907 */
1908 case 0x19: lensdisplay(a6); break;
1909 case 0x1e: allocspace(a6); break;
1910 /* v4 */
1911 case 0x0e: driver14(a6); break;
1912 case 0x20: showbitmap(a6); break;
1913 case 0x22: checkfordisc(a6); break;
1914 }
1915 }
1916
1917 void ramsave(int i)
1918 {
1919 #ifdef L9DEBUG
1920 printf("driver - ramsave %d",i);
1921 #endif
1922
1923 memmove(ramsavearea+i,workspace.vartable,sizeof(SaveStruct));
1924 }
1925
1926 void ramload(int i)
1927 {
1928 #ifdef L9DEBUG
1929 printf("driver - ramload %d",i);
1930 #endif
1931
1932 memmove(workspace.vartable,ramsavearea+i,sizeof(SaveStruct));
1933 }
1934
1935 void calldriver(void)
1936 {
1937 L9BYTE* a6=list9startptr;
1938 int d0=*a6++;
1939 #ifdef CODEFOLLOW
1940 fprintf(f," %s",drivercalls[d0]);
1941 #endif
1942
1943 if (d0==0x16 || d0==0x17)
1944 {
1945 int d1=*a6;
1946 if (d1>0xfa) *a6=1;
1947 else if (d1+1>=RAMSAVESLOTS) *a6=0xff;
1948 else
1949 {
1950 *a6=0;
1951 if (d0==0x16) ramsave(d1+1); else ramload(d1+1);
1952 }
1953 *list9startptr=*a6;
1954 }
1955 else if (d0==0x0b)
1956 {
1957 char NewName[MAX_PATH];
1958 strcpy(NewName,LastGame);
1959 if (*a6==0)
1960 {
1961 printstring("\rSearching for next sub-game file.\r");
1962 if (!os_get_game_file(NewName,MAX_PATH))
1963 {
1964 printstring("\rFailed to load game.\r");
1965 return;
1966 }
1967 }
1968 else
1969 {
1970 os_set_filenumber(NewName,MAX_PATH,*a6);
1971 }
1972 LoadGame2(NewName,NULL);
1973 }
1974 else driver(d0,a6);
1975 }
1976
1977 void L9Random(void)
1978 {
1979 #ifdef CODEFOLLOW
1980 fprintf(f," %d",randomseed);
1981 #endif
1982 randomseed=(((randomseed<<8) + 0x0a - randomseed) <<2) + randomseed + 1;
1983 *getvar()=randomseed & 0xff;
1984 #ifdef CODEFOLLOW
1985 fprintf(f," %d",randomseed);
1986 #endif
1987 }
1988
1989 void save(void)
1990 {
1991 L9UINT16 checksum;
1992 int i;
1993 #ifdef L9DEBUG
1994 printf("function - save");
1995 #endif
1996 /* does a full save, workpace, stack, codeptr, stackptr, game name, checksum */
1997
1998 workspace.Id=L9_ID;
1999 workspace.codeptr=codeptr-acodeptr;
2000 workspace.listsize=LISTAREASIZE;
2001 workspace.stacksize=STACKSIZE;
2002 workspace.filenamesize=MAX_PATH;
2003 workspace.checksum=0;
2004 strcpy(workspace.filename,LastGame);
2005
2006 checksum=0;
2007 for (i=0;i<sizeof(GameState);i++) checksum+=((L9BYTE*) &workspace)[i];
2008 workspace.checksum=checksum;
2009
2010 if (os_save_file((L9BYTE*) &workspace,sizeof(workspace))) printstring("\rGame saved.\r");
2011 else printstring("\rUnable to save game.\r");
2012 }
2013
2014 int StrCompare(const char *s1, const char *s2)
2015 {
2016 int i = 0;
2017
2018 while (1)
2019 {
2020 i = toupper(*s1) - toupper(*s2);
2021 if ((i != 0) || (*s1 == '\0')) return i;
2022 ++s1; ++s2;
2023 }
2024 return i;
2025 }
2026
2027 int StrCompareN(const char *s1, const char *s2, int n)
2028 {
2029 int i = 0;
2030
2031 while (n-- > 0)
2032 {
2033 i = toupper(*s1) - toupper(*s2);
2034 if ((i != 0) || (*s1 == '\0')) return i;
2035 ++s1; ++s2;
2036 }
2037 return i;
2038 }
2039
2040 L9BOOL CheckFile(GameState *gs)
2041 {
2042 L9UINT16 checksum;
2043 int i;
2044 char c = 'Y';
2045
2046 if (gs->Id!=L9_ID) return FALSE;
2047 checksum=gs->checksum;
2048 gs->checksum=0;
2049 for (i=0;i<sizeof(GameState);i++) checksum-=*((L9BYTE*) gs+i);
2050 if (checksum) return FALSE;
2051 if (StrCompare(gs->filename,LastGame))
2052 {
2053 printstring("\rWarning: game path name does not match, you may be about to load this position file into the wrong story file.\r");
2054 printstring("Are you sure you want to restore? (Y/N)");
2055 os_flush();
2056
2057 c = '\0';
2058 while ((c != 'y') && (c != 'Y') && (c != 'n') && (c != 'N'))
2059 c = os_readchar(20);
2060 }
2061 if ((c == 'y') || (c == 'Y'))
2062 return TRUE;
2063 return FALSE;
2064 }
2065
2066 void NormalRestore(void)
2067 {
2068 GameState temp;
2069 int Bytes;
2070 #ifdef L9DEBUG
2071 printf("function - restore");
2072 #endif
2073 if (Cheating)
2074 {
2075 /* not really an error */
2076 Cheating=FALSE;
2077 error("\rWord is: %s\r",ibuff);
2078 }
2079
2080 if (os_load_file((L9BYTE*) &temp,&Bytes,sizeof(GameState)))
2081 {
2082 if (Bytes==V1FILESIZE)
2083 {
2084 printstring("\rGame restored.\r");
2085 memset(workspace.listarea,0,LISTAREASIZE);
2086 memmove(workspace.vartable,&temp,V1FILESIZE);
2087 }
2088 else if (CheckFile(&temp))
2089 {
2090 printstring("\rGame restored.\r");
2091 /* only copy in workspace */
2092 memmove(workspace.vartable,temp.vartable,sizeof(SaveStruct));
2093 }
2094 else
2095 {
2096 printstring("\rSorry, unrecognised format. Unable to restore\r");
2097 }
2098 }
2099 else printstring("\rUnable to restore game.\r");
2100 }
2101
2102 void restore(void)
2103 {
2104 int Bytes;
2105 GameState temp;
2106 if (os_load_file((L9BYTE*) &temp,&Bytes,sizeof(GameState)))
2107 {
2108 if (Bytes==V1FILESIZE)
2109 {
2110 printstring("\rGame restored.\r");
2111 /* only copy in workspace */
2112 memset(workspace.listarea,0,LISTAREASIZE);
2113 memmove(workspace.vartable,&temp,V1FILESIZE);
2114 }
2115 else if (CheckFile(&temp))
2116 {
2117 printstring("\rGame restored.\r");
2118 /* full restore */
2119 memmove(&workspace,&temp,sizeof(GameState));
2120 codeptr=acodeptr+workspace.codeptr;
2121 }
2122 else
2123 {
2124 printstring("\rSorry, unrecognised format. Unable to restore\r");
2125 }
2126 }
2127 else printstring("\rUnable to restore game.\r");
2128 }
2129
2130 void playback(void)
2131 {
2132 if (scriptfile) fclose(scriptfile);
2133 scriptfile = os_open_script_file();
2134 if (scriptfile)
2135 printstring("\rPlaying back input from script file.\r");
2136 else
2137 printstring("\rUnable to play back script file.\r");
2138 }
2139
2140 void l9_fgets(char* s, int n, FILE* f)
2141 {
2142 int c = '\0';
2143 int count = 0;
2144
2145 while ((c != '\n') && (c != '\r') && (c != EOF) && (count < n-1))
2146 {
2147 c = fgetc(f);
2148 *s++ = c;
2149 count++;
2150 }
2151 *s = '\0';
2152
2153 if (c == EOF)
2154 {
2155 s--;
2156 *s = '\n';
2157 }
2158 else if (c == '\r')
2159 {
2160 s--;
2161 *s = '\n';
2162
2163 c = fgetc(f);
2164 if ((c != '\r') && (c != EOF))
2165 fseek(f,-1,SEEK_CUR);
2166 }
2167 }
2168
2169 L9BOOL scriptinput(char* ibuff, int size)
2170 {
2171 while (scriptfile != NULL)
2172 {
2173 if (feof(scriptfile))
2174 {
2175 fclose(scriptfile);
2176 scriptfile=NULL;
2177 }
2178 else
2179 {
2180 char* p = ibuff;
2181 *p = '\0';
2182 l9_fgets(ibuff,size,scriptfile);
2183 while (*p != '\0')
2184 {
2185 switch (*p)
2186 {
2187 case '\n':
2188 case '\r':
2189 case '[':
2190 case ';':
2191 *p = '\0';
2192 break;
2193 case '#':
2194 if ((p==ibuff) && (StrCompareN(p,"#seed ",6)==0))
2195 p++;
2196 else
2197 *p = '\0';
2198 break;
2199 default:
2200 p++;
2201 break;
2202 }
2203 }
2204 if (*ibuff != '\0')
2205 {
2206 printstring(ibuff);
2207 lastchar=lastactualchar='.';
2208 return TRUE;
2209 }
2210 }
2211 }
2212 return FALSE;
2213 }
2214
2215 void clearworkspace(void)
2216 {
2217 memset(workspace.vartable,0,sizeof(workspace.vartable));
2218 }
2219
2220 void ilins(int d0)
2221 {
2222 error("\rIllegal instruction: %d\r",d0);
2223 Running=FALSE;
2224 }
2225
2226 void function(void)
2227 {
2228 int d0=*codeptr++;
2229 #ifdef CODEFOLLOW
2230 fprintf(f," %s",d0==250 ? "printstr" : functions[d0-1]);
2231 #endif
2232
2233 switch (d0)
2234 {
2235 case 1:
2236 if (L9GameType==L9_V1)
2237 StopGame();
2238 else
2239 calldriver();
2240 break;
2241 case 2: L9Random(); break;
2242 case 3: save(); break;
2243 case 4: NormalRestore(); break;
2244 case 5: clearworkspace(); break;
2245 case 6: workspace.stackptr=0; break;
2246 case 250:
2247 printstring((char*) codeptr);
2248 while (*codeptr++);
2249 break;
2250
2251 default: ilins(d0);
2252 }
2253 }
2254
2255 void findmsgequiv(int d7)
2256 {
2257 int d4=-1,d0;
2258 L9BYTE* a2=startmd;
2259
2260 do
2261 {
2262 d4++;
2263 if (a2>endmd) return;
2264 d0=*a2;
2265 if (d0&0x80)
2266 {
2267 a2++;
2268 d4+=d0&0x7f;
2269 }
2270 else if (d0&0x40)
2271 {
2272 int d6=getmdlength(&a2);
2273 do
2274 {
2275 int d1;
2276 if (d6==0) break;
2277
2278 d1=*a2++;
2279 d6--;
2280 if (d1 & 0x80)
2281 {
2282 if (d1<0x90)
2283 {
2284 a2++;
2285 d6--;
2286 }
2287 else
2288 {
2289 d0=(d1<<8) + *a2++;
2290 d6--;
2291 if (d7==(d0 & 0xfff))
2292 {
2293 d0=((d0<<1)&0xe000) | d4;
2294 list9ptr[1]=d0;
2295 list9ptr[0]=d0>>8;
2296 list9ptr+=2;
2297 if (list9ptr>=list9startptr+0x20) return;
2298 }
2299 }
2300 }
2301 } while (TRUE);
2302 }
2303 else {
2304 int len=getmdlength(&a2);
2305 a2+=len;
2306 }
2307 } while (TRUE);
2308 }
2309
2310 L9BOOL unpackword(void)
2311 {
2312 L9BYTE *a3;
2313
2314 if (unpackd3==0x1b) return TRUE;
2315
2316 a3=(L9BYTE*) threechars + (unpackd3&3);
2317
2318 /*uw01 */
2319 while (TRUE)
2320 {
2321 L9BYTE d0=getdictionarycode();
2322 if (dictptr>=endwdp5) return TRUE;
2323 if (d0>=0x1b)
2324 {
2325 *a3=0;
2326 unpackd3=d0;
2327 return FALSE;
2328 }
2329 *a3++=getdictionary(d0);
2330 }
2331 }
2332
2333 L9BOOL initunpack(L9BYTE* ptr)
2334 {
2335 initdict(ptr);
2336 unpackd3=0x1c;
2337 return unpackword();
2338 }
2339
2340 int partword(char c)
2341 {
2342 c=tolower(c);
2343
2344 if (c==0x27 || c==0x2d) return 0;
2345 if (c<0x30) return 1;
2346 if (c<0x3a) return 0;
2347 if (c<0x61) return 1;
2348 if (c<0x7b) return 0;
2349 return 1;
2350 }
2351
2352 L9UINT32 readdecimal(char *buff)
2353 {
2354 return atol(buff);
2355 }
2356
2357 void checknumber(void)
2358 {
2359 if (*obuff>=0x30 && *obuff<0x3a)
2360 {
2361 if (L9GameType==L9_V4)
2362 {
2363 *list9ptr=1;
2364 L9SETWORD(list9ptr+1,readdecimal(obuff));
2365 L9SETWORD(list9ptr+3,0);
2366 }
2367 else
2368 {
2369 L9SETDWORD(list9ptr,readdecimal(obuff));
2370 L9SETWORD(list9ptr+4,0);
2371 }
2372 }
2373 else
2374 {
2375 L9SETWORD(list9ptr,0x8000);
2376 L9SETWORD(list9ptr+2,0);
2377 }
2378 }
2379
2380 void NextCheat(void)
2381 {
2382 /* restore game status */
2383 memmove(&workspace,&CheatWorkspace,sizeof(GameState));
2384 codeptr=acodeptr+workspace.codeptr;
2385
2386 if (!((L9GameType<=L9_V2) ? GetWordV2(ibuff,CheatWord++) : GetWordV3(ibuff,CheatWord++)))
2387 {
2388 Cheating=FALSE;
2389 printstring("\rCheat failed.\r");
2390 *ibuff=0;
2391 }
2392 }
2393
2394 void StartCheat(void)
2395 {
2396 Cheating=TRUE;
2397 CheatWord=0;
2398
2399 /* save current game status */
2400 memmove(&CheatWorkspace,&workspace,sizeof(GameState));
2401 CheatWorkspace.codeptr=codeptr-acodeptr;
2402
2403 NextCheat();
2404 }
2405
2406 /* v3,4 input routine */
2407
2408 L9BOOL GetWordV3(char *buff,int Word)
2409 {
2410 int i;
2411 int subdict=0;
2412 /* 26*4-1=103 */
2413
2414 initunpack(startdata+L9WORD(dictdata));
2415 unpackword();
2416
2417 while (Word--)
2418 {
2419 if (unpackword())
2420 {
2421 if (++subdict==dictdatalen) return FALSE;
2422 initunpack(startdata+L9WORD(dictdata+(subdict<<2)));
2423 Word++; /* force unpack again */
2424 }
2425 }
2426 strcpy(buff,threechars);
2427 for (i=0;i<(int)strlen(buff);i++) buff[i]&=0x7f;
2428 return TRUE;
2429 }
2430
2431 L9BOOL CheckHash(void)
2432 {
2433 if (StrCompare(ibuff,"#cheat")==0) StartCheat();
2434 else if (StrCompare(ibuff,"#save")==0)
2435 {
2436 save();
2437 return TRUE;
2438 }
2439 else if (StrCompare(ibuff,"#restore")==0)
2440 {
2441 restore();
2442 return TRUE;
2443 }
2444 else if (StrCompare(ibuff,"#quit")==0)
2445 {
2446 StopGame();
2447 printstring("\rGame Terminated\r");
2448 return TRUE;
2449 }
2450 else if (StrCompare(ibuff,"#dictionary")==0)
2451 {
2452 CheatWord=0;
2453 printstring("\r");
2454 while ((L9GameType<=L9_V2) ? GetWordV2(ibuff,CheatWord++) : GetWordV3(ibuff,CheatWord++))
2455 {
2456 error("%s ",ibuff);
2457 if (os_stoplist() || !Running) break;
2458 }
2459 printstring("\r");
2460 return TRUE;
2461 }
2462 else if (StrCompareN(ibuff,"#picture ",9)==0)
2463 {
2464 int pic = 0;
2465 if (sscanf(ibuff+9,"%d",&pic) == 1)
2466 {
2467 if (L9GameType==L9_V4)
2468 os_show_bitmap(pic,0,0);
2469 else
2470 show_picture(pic);
2471 }
2472
2473 lastactualchar = 0;
2474 printchar('\r');
2475 return TRUE;
2476 }
2477 else if (StrCompareN(ibuff,"#seed ",6)==0)
2478 {
2479 int seed = 0;
2480 if (sscanf(ibuff+6,"%d",&seed) == 1)
2481 randomseed = constseed = seed;
2482 lastactualchar = 0;
2483 printchar('\r');
2484 return TRUE;
2485 }
2486 else if (StrCompare(ibuff,"#play")==0)
2487 {
2488 playback();
2489 return TRUE;
2490 }
2491 return FALSE;
2492 }
2493
2494 L9BOOL IsInputChar(char c)
2495 {
2496 if (c=='-' || c=='\'')
2497 return TRUE;
2498 if ((L9GameType>=L9_V3) && (c=='.' || c==','))
2499 return TRUE;
2500 return isalnum(c);
2501 }
2502
2503 L9BOOL corruptinginput(void)
2504 {
2505 L9BYTE *a0,*a2,*a6;
2506 int d0,d1,d2,keywordnumber,abrevword;
2507 char *iptr;
2508
2509 list9ptr=list9startptr;
2510
2511 if (ibuffptr==NULL)
2512 {
2513 if (Cheating) NextCheat();
2514 else
2515 {
2516 /* flush */
2517 os_flush();
2518 lastchar=lastactualchar='.';
2519 /* get input */
2520 if (!scriptinput(ibuff,IBUFFSIZE))
2521 {
2522 if (!os_input(ibuff,IBUFFSIZE))
2523 return FALSE; /* fall through */
2524 }
2525 if (CheckHash())
2526 return FALSE;
2527
2528 /* check for invalid chars */
2529 for (iptr=ibuff;*iptr!=0;iptr++)
2530 {
2531 if (!IsInputChar(*iptr))
2532 *iptr=' ';
2533 }
2534
2535 /* force CR but prevent others */
2536 os_printchar(lastactualchar='\r');
2537 }
2538 ibuffptr=(L9BYTE*) ibuff;
2539 }
2540
2541 a2=(L9BYTE*) obuff;
2542 a6=ibuffptr;
2543
2544 /*ip05 */
2545 while (TRUE)
2546 {
2547 d0=*a6++;
2548 if (d0==0)
2549 {
2550 ibuffptr=NULL;
2551 L9SETWORD(list9ptr,0);
2552 return TRUE;
2553 }
2554 if (partword((char)d0)==0) break;
2555 if (d0!=0x20)
2556 {
2557 ibuffptr=a6;
2558 L9SETWORD(list9ptr,0);
2559 L9SETWORD(list9ptr+2,0);
2560 list9ptr[1]=d0;
2561 *a2=0x20;
2562 keywordnumber=-1;
2563 return TRUE;
2564 }
2565 }
2566
2567 a6--;
2568 /*ip06loop */
2569 do
2570 {
2571 d0=*a6++;
2572 if (partword((char)d0)==1) break;
2573 d0=tolower(d0);
2574 *a2++=d0;
2575 } while (a2<(L9BYTE*) obuff+0x1f);
2576 /*ip06a */
2577 *a2=0x20;
2578 a6--;
2579 ibuffptr=a6;
2580 abrevword=-1;
2581 keywordnumber=-1;
2582 list9ptr=list9startptr;
2583 /* setindex */
2584 a0=dictdata;
2585 d2=dictdatalen;
2586 d0=*obuff-0x61;
2587 if (d0<0)
2588 {
2589 a6=defdict;
2590 d1=0;
2591 }
2592 else
2593 {
2594 /*ip10 */
2595 d1=0x67;
2596 if (d0<0x1a)
2597 {
2598 d1=d0<<2;
2599 d0=obuff[1];
2600 if (d0!=0x20) d1+=((d0-0x61)>>3)&3;
2601 }
2602 /*ip13 */
2603 if (d1>=d2)
2604 {
2605 checknumber();
2606 return TRUE;
2607 }
2608 a0+=d1<<2;
2609 a6=startdata+L9WORD(a0);
2610 d1=L9WORD(a0+2);
2611 }
2612 /*ip13gotwordnumber */
2613
2614 initunpack(a6);
2615 /*ip14 */
2616 d1--;
2617 do
2618 {
2619 d1++;
2620 if (unpackword())
2621 {/* ip21b */
2622 if (abrevword==-1) break; /* goto ip22 */
2623 else d0=abrevword; /* goto ip18b */
2624 }
2625 else
2626 {
2627 L9BYTE* a1=(L9BYTE*) threechars;
2628 int d6=-1;
2629
2630 a0=(L9BYTE*) obuff;
2631 /*ip15 */
2632 do
2633 {
2634 d6++;
2635 d0=tolower(*a1++ & 0x7f);
2636 d2=*a0++;
2637 } while (d0==d2);
2638
2639 if (d2!=0x20)
2640 {/* ip17 */
2641 if (abrevword==-1) continue;
2642 else d0=-1;
2643 }
2644 else if (d0==0) d0=d1;
2645 else if (abrevword!=-1) break;
2646 else if (d6>=4) d0=d1;
2647 else
2648 {
2649 abrevword=d1;
2650 continue;
2651 }
2652 }
2653 /*ip18b */
2654 findmsgequiv(d1);
2655
2656 abrevword=-1;
2657 if (list9ptr!=list9startptr)
2658 {
2659 L9SETWORD(list9ptr,0);
2660 return TRUE;
2661 }
2662 } while (TRUE);
2663 /* ip22 */
2664 checknumber();
2665 return TRUE;
2666 }
2667
2668 /* version 2 stuff hacked from bbc v2 files */
2669
2670 L9BOOL IsDictionaryChar(char c)
2671 {
2672 switch (c)
2673 {
2674 case '?': case '-': case '\'': case '/': return TRUE;
2675 case '!': case '.': case ',': return TRUE;
2676 }
2677 return isupper(c) || isdigit(c);
2678 }
2679
2680 L9BOOL GetWordV2(char *buff,int Word)
2681 {
2682 L9BYTE *ptr=dictdata,x;
2683
2684 while (Word--)
2685 {
2686 do
2687 {
2688 x=*ptr++;
2689 } while (x>0 && x<0x7f);
2690 if (x==0) return FALSE; /* no more words */
2691 ptr++;
2692 }
2693 do
2694 {
2695 x=*ptr++;
2696 if (!IsDictionaryChar(x&0x7f)) return FALSE;
2697 *buff++=x&0x7f;
2698 } while (x>0 && x<0x7f);
2699 *buff=0;
2700 return TRUE;
2701 }
2702
2703 L9BOOL inputV2(int *wordcount)
2704 {
2705 L9BYTE a,x;
2706 L9BYTE *ibuffptr,*obuffptr,*ptr,*list0ptr;
2707 char *iptr;
2708
2709 if (Cheating) NextCheat();
2710 else
2711 {
2712 os_flush();
2713 lastchar=lastactualchar='.';
2714 /* get input */
2715 if (!scriptinput(ibuff,IBUFFSIZE))
2716 {
2717 if (!os_input(ibuff,IBUFFSIZE))
2718 return FALSE; /* fall through */
2719 }
2720 if (CheckHash())
2721 return FALSE;
2722
2723 /* check for invalid chars */
2724 for (iptr=ibuff;*iptr!=0;iptr++)
2725 {
2726 if (!IsInputChar(*iptr))
2727 *iptr=' ';
2728 }
2729
2730 /* force CR but prevent others */
2731 os_printchar(lastactualchar='\r');
2732 }
2733 /* add space onto end */
2734 ibuffptr=(L9BYTE*) strchr(ibuff,0);
2735 *ibuffptr++=32;
2736 *ibuffptr=0;
2737
2738 *wordcount=0;
2739 ibuffptr=(L9BYTE*) ibuff;
2740 obuffptr=(L9BYTE*) obuff;
2741 /* ibuffptr=76,77 */
2742 /* obuffptr=84,85 */
2743 /* list0ptr=7c,7d */
2744 list0ptr=dictdata;
2745
2746 while (*ibuffptr==32) ++ibuffptr;
2747
2748 ptr=ibuffptr;
2749 do
2750 {
2751 while (*ptr==32) ++ptr;
2752 if (*ptr==0) break;
2753 (*wordcount)++;
2754 do
2755 {
2756 a=*++ptr;
2757 } while (a!=32 && a!=0);
2758 } while (*ptr>0);
2759
2760 while (TRUE)
2761 {
2762 ptr=ibuffptr; /* 7a,7b */
2763 while (*ibuffptr==32) ++ibuffptr;
2764
2765 while (TRUE)
2766 {
2767 a=*ibuffptr;
2768 x=*list0ptr++;
2769
2770 if (a==32) break;
2771 if (a==0)
2772 {
2773 *obuffptr++=0;
2774 return TRUE;
2775 }
2776
2777 ++ibuffptr;
2778 if (!IsDictionaryChar(x&0x7f)) x = 0;
2779 if (tolower(x&0x7f) != tolower(a))
2780 {
2781 while (x>0 && x<0x7f) x=*list0ptr++;
2782 if (x==0)
2783 {
2784 do
2785 {
2786 a=*ibuffptr++;
2787 if (a==0)
2788 {
2789 *obuffptr=0;
2790 return TRUE;
2791 }
2792 } while (a!=32);
2793 while (*ibuffptr==32) ++ibuffptr;
2794 list0ptr=dictdata;
2795 ptr=ibuffptr;
2796 }
2797 else
2798 {
2799 list0ptr++;
2800 ibuffptr=ptr;
2801 }
2802 }
2803 else if (x>=0x7f) break;
2804 }
2805
2806 a=*ibuffptr;
2807 if (a!=32)
2808 {
2809 ibuffptr=ptr;
2810 list0ptr+=2;
2811 continue;
2812 }
2813 --list0ptr;
2814 while (*list0ptr++<0x7e);
2815 *obuffptr++=*list0ptr;
2816 while (*ibuffptr==32) ++ibuffptr;
2817 list0ptr=dictdata;
2818 }
2819 }
2820
2821 void input(void)
2822 {
2823 if (L9GameType == L9_V3 && FirstPicture >= 0)
2824 {
2825 show_picture(FirstPicture);
2826 FirstPicture = -1;
2827 }
2828
2829 /* if corruptinginput() returns false then, input will be called again
2830 next time around instructionloop, this is used when save() and restore()
2831 are called out of line */
2832
2833 codeptr--;
2834 if (L9GameType<=L9_V2)
2835 {
2836 int wordcount;
2837 if (inputV2(&wordcount))
2838 {
2839 L9BYTE *obuffptr=(L9BYTE*) obuff;
2840 codeptr++;
2841 *getvar()=*obuffptr++;
2842 *getvar()=*obuffptr++;
2843 *getvar()=*obuffptr;
2844 *getvar()=wordcount;
2845 }
2846 }
2847 else
2848 if (corruptinginput()) codeptr+=5;
2849 }
2850
2851 void varcon(void)
2852 {
2853 L9UINT16 d6=getcon();
2854 *getvar()=d6;
2855
2856 #ifdef CODEFOLLOW
2857 fprintf(f," Var[%d]=%d)",cfvar-workspace.vartable,*cfvar);
2858 #endif
2859 }
2860
2861 void varvar(void)
2862 {
2863 L9UINT16 d6=*getvar();
2864 *getvar()=d6;
2865
2866 #ifdef CODEFOLLOW
2867 fprintf(f," Var[%d]=Var[%d] (=%d)",cfvar-workspace.vartable,cfvar2-workspace.vartable,d6);
2868 #endif
2869 }
2870
2871 void _add(void)
2872 {
2873 L9UINT16 d0=*getvar();
2874 *getvar()+=d0;
2875
2876 #ifdef CODEFOLLOW
2877 fprintf(f," Var[%d]+=Var[%d] (+=%d)",cfvar-workspace.vartable,cfvar2-workspace.vartable,d0);
2878 #endif
2879 }
2880
2881 void _sub(void)
2882 {
2883 L9UINT16 d0=*getvar();
2884 *getvar()-=d0;
2885
2886 #ifdef CODEFOLLOW
2887 fprintf(f," Var[%d]-=Var[%d] (-=%d)",cfvar-workspace.vartable,cfvar2-workspace.vartable,d0);
2888 #endif
2889 }
2890
2891 void jump(void)
2892 {
2893 L9UINT16 d0=L9WORD(codeptr);
2894 L9BYTE* a0;
2895 codeptr+=2;
2896
2897 a0=acodeptr+((d0+((*getvar())<<1))&0xffff);
2898 codeptr=acodeptr+L9WORD(a0);
2899 }
2900
2901 /* bug */
2902 void exit1(L9BYTE *d4,L9BYTE *d5,L9BYTE d6,L9BYTE d7)
2903 {
2904 L9BYTE* a0=absdatablock;
2905 L9BYTE d1=d7,d0;
2906 if (--d1)
2907 {
2908 do
2909 {
2910 d0=*a0;
2911 if (L9GameType==L9_V4)
2912 {
2913 if ((d0==0) && (*(a0+1)==0))
2914 goto notfn4;
2915 }
2916 a0+=2;
2917 }
2918 while ((d0&0x80)==0 || --d1);
2919 }
2920
2921 do
2922 {
2923 *d4=*a0++;
2924 if (((*d4)&0xf)==d6)
2925 {
2926 *d5=*a0;
2927 return;
2928 }
2929 a0++;
2930 }
2931 while (((*d4)&0x80)==0);
2932
2933 /* notfn4 */
2934 notfn4:
2935 d6=exitreversaltable[d6];
2936 a0=absdatablock;
2937 *d5=1;
2938
2939 do
2940 {
2941 *d4=*a0++;
2942 if (((*d4)&0x10)==0 || ((*d4)&0xf)!=d6) a0++;
2943 else if (*a0++==d7) return;
2944 /* exit6noinc */
2945 if ((*d4)&0x80) (*d5)++;
2946 } while (*d4);
2947 *d5=0;
2948 }
2949
2950 void Exit(void)
2951 {
2952 L9BYTE d4,d5;
2953 L9BYTE d7=(L9BYTE) *getvar();
2954 L9BYTE d6=(L9BYTE) *getvar();
2955 #ifdef CODEFOLLOW
2956 fprintf(f," d7=%d d6=%d",d7,d6);
2957 #endif
2958 exit1(&d4,&d5,d6,d7);
2959
2960 *getvar()=(d4&0x70)>>4;
2961 *getvar()=d5;
2962 #ifdef CODEFOLLOW
2963 fprintf(f," Var[%d]=%d(d4=%d) Var[%d]=%d",
2964 cfvar2-workspace.vartable,(d4&0x70)>>4,d4,cfvar-workspace.vartable,d5);
2965 #endif
2966 }
2967
2968 void ifeqvt(void)
2969 {
2970 L9UINT16 d0=*getvar();
2971 L9UINT16 d1=*getvar();
2972 L9BYTE* a0=getaddr();
2973 if (d0==d1) codeptr=a0;
2974
2975 #ifdef CODEFOLLOW
2976 fprintf(f," if Var[%d]=Var[%d] goto %d (%s)",cfvar2-workspace.vartable,cfvar-workspace.vartable,(L9UINT32) (a0-acodeptr),d0==d1 ? "Yes":"No");
2977 #endif
2978 }
2979
2980 void ifnevt(void)
2981 {
2982 L9UINT16 d0=*getvar();
2983 L9UINT16 d1=*getvar();
2984 L9BYTE* a0=getaddr();
2985 if (d0!=d1) codeptr=a0;
2986
2987 #ifdef CODEFOLLOW
2988 fprintf(f," if Var[%d]!=Var[%d] goto %d (%s)",cfvar2-workspace.vartable,cfvar-workspace.vartable,(L9UINT32) (a0-acodeptr),d0!=d1 ? "Yes":"No");
2989 #endif
2990 }
2991
2992 void ifltvt(void)
2993 {
2994 L9UINT16 d0=*getvar();
2995 L9UINT16 d1=*getvar();
2996 L9BYTE* a0=getaddr();
2997 if (d0<d1) codeptr=a0;
2998
2999 #ifdef CODEFOLLOW
3000 fprintf(f," if Var[%d]<Var[%d] goto %d (%s)",cfvar2-workspace.vartable,cfvar-workspace.vartable,(L9UINT32) (a0-acodeptr),d0<d1 ? "Yes":"No");
3001 #endif
3002 }
3003
3004 void ifgtvt(void)
3005 {
3006 L9UINT16 d0=*getvar();
3007 L9UINT16 d1=*getvar();
3008 L9BYTE* a0=getaddr();
3009 if (d0>d1) codeptr=a0;
3010
3011 #ifdef CODEFOLLOW
3012 fprintf(f," if Var[%d]>Var[%d] goto %d (%s)",cfvar2-workspace.vartable,cfvar-workspace.vartable,(L9UINT32) (a0-acodeptr),d0>d1 ? "Yes":"No");
3013 #endif
3014 }
3015
3016 int scalex(int x)
3017 {
3018 return (gfx_mode != GFX_V3C) ? (x>>6) : (x>>5);
3019 }
3020
3021 int scaley(int y)
3022 {
3023 return (gfx_mode == GFX_V2) ? 127 - (y>>7) : 95 - (((y>>5)+(y>>6))>>3);
3024 }
3025
3026 void detect_gfx_mode(void)
3027 {
3028 if (L9GameType == L9_V3)
3029 {
3030 /* These V3 games use graphics logic similar to the V2 games */
3031 if (strstr(FirstLine,"price of magik") != 0)
3032 gfx_mode = GFX_V3A;
3033 else if (strstr(FirstLine,"the archers") != 0)
3034 gfx_mode = GFX_V3A;
3035 else if (strstr(FirstLine,"secret diary of adrian mole") != 0)
3036 gfx_mode = GFX_V3A;
3037 else if ((strstr(FirstLine,"worm in paradise") != 0)
3038 && (strstr(FirstLine,"silicon dreams") == 0))
3039 gfx_mode = GFX_V3A;
3040 else if (strstr(FirstLine,"growing pains of adrian mole") != 0)
3041 gfx_mode = GFX_V3B;
3042 else if (strstr(FirstLine,"jewels of darkness") != 0 && picturesize < 11000)
3043 gfx_mode = GFX_V3B;
3044 else if (strstr(FirstLine,"silicon dreams") != 0)
3045 {
3046 if (picturesize > 11000
3047 || (startdata[0] == 0x14 && startdata[1] == 0x7d) /* Return to Eden /SD (PC) */
3048 || (startdata[0] == 0xd7 && startdata[1] == 0x7c)) /* Worm in Paradise /SD (PC) */
3049 gfx_mode = GFX_V3C;
3050 else
3051 gfx_mode = GFX_V3B;
3052 }
3053 else
3054 gfx_mode = GFX_V3C;
3055 }
3056 else
3057 gfx_mode = GFX_V2;
3058 }
3059
3060 void _screen(void)
3061 {
3062 int mode = 0;
3063
3064 if (L9GameType == L9_V3 && strlen(FirstLine) == 0)
3065 {
3066 if (*codeptr++)
3067 codeptr++;
3068 return;
3069 }
3070
3071 detect_gfx_mode();
3072 l9textmode = *codeptr++;
3073 if (l9textmode)
3074 {
3075 if (L9GameType==L9_V4)
3076 mode = 2;
3077 else if (picturedata)
3078 mode = 1;
3079 }
3080 os_graphics(mode);
3081
3082 screencalled = 1;
3083
3084 #ifdef L9DEBUG
3085 printf("screen %s",l9textmode ? "graphics" : "text");
3086 #endif
3087
3088 if (l9textmode)
3089 {
3090 codeptr++;
3091 /* clearg */
3092 /* gintclearg */
3093 os_cleargraphics();
3094
3095 /* title pic */
3096 if (showtitle==1 && mode==2)
3097 {
3098 showtitle = 0;
3099 os_show_bitmap(0,0,0);
3100 }
3101 }
3102 /* screent */
3103 }
3104
3105 void cleartg(void)
3106 {
3107 int d0 = *codeptr++;
3108 #ifdef L9DEBUG
3109 printf("cleartg %s",d0 ? "graphics" : "text");
3110 #endif
3111
3112 if (d0)
3113 {
3114 /* clearg */
3115 if (l9textmode)
3116 /* gintclearg */
3117 os_cleargraphics();
3118 }
3119 /* cleart */
3120 /* oswrch(0x0c) */
3121 }
3122
3123 L9BOOL validgfxptr(L9BYTE* a5)
3124 {
3125 return ((a5 >= picturedata) && (a5 < picturedata+picturesize));
3126 }
3127
3128 L9BOOL findsub(int d0,L9BYTE** a5)
3129 {
3130 int d1,d2,d3,d4;
3131
3132 d1=d0 << 4;
3133 d2=d1 >> 8;
3134 *a5=picturedata;
3135 /* findsubloop */
3136 while (TRUE)
3137 {
3138 d3=*(*a5)++;
3139 if (!validgfxptr(*a5))
3140 return FALSE;
3141 if (d3&0x80)
3142 return FALSE;
3143 if (d2==d3)
3144 {
3145 if ((d1&0xff)==(*(*a5) & 0xf0))
3146 {
3147 (*a5)+=2;
3148 return TRUE;
3149 }
3150 }
3151
3152 d3=*(*a5)++ & 0x0f;
3153 if (!validgfxptr(*a5))
3154 return FALSE;
3155
3156 d4=**a5;
3157 if ((d3|d4)==0)
3158 return FALSE;
3159
3160 (*a5)+=(d3<<8) + d4 - 2;
3161 if (!validgfxptr(*a5))
3162 return FALSE;
3163 }
3164 }
3165
3166 void gosubd0(int d0, L9BYTE** a5)
3167 {
3168 if (GfxA5StackPos < GFXSTACKSIZE)
3169 {
3170 GfxA5Stack[GfxA5StackPos] = *a5;
3171 GfxA5StackPos++;
3172 GfxScaleStack[GfxScaleStackPos] = scale;
3173 GfxScaleStackPos++;
3174
3175 if (findsub(d0,a5) == FALSE)
3176 {
3177 GfxA5StackPos--;
3178 *a5 = GfxA5Stack[GfxA5StackPos];
3179 GfxScaleStackPos--;
3180 scale = GfxScaleStack[GfxScaleStackPos];
3181 }
3182 }
3183 }
3184
3185 void newxy(int x, int y)
3186 {
3187 drawx += (x*scale)&~7;
3188 drawy += (y*scale)&~7;
3189 }
3190
3191 /* sdraw instruction plus arguments are stored in an 8 bit word.
3192 76543210
3193 iixxxyyy
3194 where i is instruction code
3195 x is x argument, high bit is sign
3196 y is y argument, high bit is sign
3197 */
3198 void sdraw(int d7)
3199 {
3200 int x,y,x1,y1;
3201
3202 /* getxy1 */
3203 x = (d7&0x18)>>3;
3204 if (d7&0x20)
3205 x = (x|0xfc) - 0x100;
3206 y = (d7&0x3)<<2;
3207 if (d7&0x4)
3208 y = (y|0xf0) - 0x100;
3209
3210 if (reflectflag&2)
3211 x = -x;
3212 if (reflectflag&1)
3213 y = -y;
3214
3215 /* gintline */
3216 x1 = drawx;
3217 y1 = drawy;
3218 newxy(x,y);
3219
3220 #ifdef L9DEBUG
3221 printf("gfx - sdraw (%d,%d) (%d,%d) colours %d,%d",
3222 x1,y1,drawx,drawy,gintcolour&3,option&3);
3223 #endif
3224
3225 os_drawline(scalex(x1),scaley(y1),scalex(drawx),scaley(drawy),
3226 gintcolour&3,option&3);
3227 }
3228
3229 /* smove instruction plus arguments are stored in an 8 bit word.
3230 76543210
3231 iixxxyyy
3232 where i is instruction code
3233 x is x argument, high bit is sign
3234 y is y argument, high bit is sign
3235 */
3236 void smove(int d7)
3237 {
3238 int x,y;
3239
3240 /* getxy1 */
3241 x = (d7&0x18)>>3;
3242 if (d7&0x20)
3243 x = (x|0xfc) - 0x100;
3244 y = (d7&0x3)<<2;
3245 if (d7&0x4)
3246 y = (y|0xf0) - 0x100;
3247
3248 if (reflectflag&2)
3249 x = -x;
3250 if (reflectflag&1)
3251 y = -y;
3252 newxy(x,y);
3253 }
3254
3255 void sgosub(int d7, L9BYTE** a5)
3256 {
3257 int d0 = d7&0x3f;
3258 #ifdef L9DEBUG
3259 printf("gfx - sgosub 0x%.2x",d0);
3260 #endif
3261 gosubd0(d0,a5);
3262 }
3263
3264 /* draw instruction plus arguments are stored in a 16 bit word.
3265 FEDCBA9876543210
3266 iiiiixxxxxxyyyyy
3267 where i is instruction code
3268 x is x argument, high bit is sign
3269 y is y argument, high bit is sign
3270 */
3271 void draw(int d7, L9BYTE** a5)
3272 {
3273 int xy,x,y,x1,y1;
3274
3275 /* getxy2 */
3276 xy = (d7<<8)+(*(*a5)++);
3277 x = (xy&0x3e0)>>5;
3278 if (xy&0x400)
3279 x = (x|0xe0) - 0x100;
3280 y = (xy&0xf)<<2;
3281 if (xy&0x10)
3282 y = (y|0xc0) - 0x100;
3283
3284 if (reflectflag&2)
3285 x = -x;
3286 if (reflectflag&1)
3287 y = -y;
3288
3289 /* gintline */
3290 x1 = drawx;
3291 y1 = drawy;
3292 newxy(x,y);
3293
3294 #ifdef L9DEBUG
3295 printf("gfx - draw (%d,%d) (%d,%d) colours %d,%d",
3296 x1,y1,drawx,drawy,gintcolour&3,option&3);
3297 #endif
3298
3299 os_drawline(scalex(x1),scaley(y1),scalex(drawx),scaley(drawy),
3300 gintcolour&3,option&3);
3301 }
3302
3303 /* move instruction plus arguments are stored in a 16 bit word.
3304 FEDCBA9876543210
3305 iiiiixxxxxxyyyyy
3306 where i is instruction code
3307 x is x argument, high bit is sign
3308 y is y argument, high bit is sign
3309 */
3310 void _move(int d7, L9BYTE** a5)
3311 {
3312 int xy,x,y;
3313
3314 /* getxy2 */
3315 xy = (d7<<8)+(*(*a5)++);
3316 x = (xy&0x3e0)>>5;
3317 if (xy&0x400)
3318 x = (x|0xe0) - 0x100;
3319 y = (xy&0xf)<<2;
3320 if (xy&0x10)
3321 y = (y|0xc0) - 0x100;
3322
3323 if (reflectflag&2)
3324 x = -x;
3325 if (reflectflag&1)
3326 y = -y;
3327 newxy(x,y);
3328 }
3329
3330 void icolour(int d7)
3331 {
3332 gintcolour = d7&3;
3333 #ifdef L9DEBUG
3334 printf("gfx - icolour 0x%.2x",gintcolour);
3335 #endif
3336 }
3337
3338 void size(int d7)
3339 {
3340 static int sizetable[7] = { 0x02,0x04,0x06,0x07,0x09,0x0c,0x10 };
3341
3342 d7 &= 7;
3343 if (d7)
3344 {
3345 int d0 = (scale*sizetable[d7-1])>>3;
3346 scale = (d0 < 0x100) ? d0 : 0xff;
3347 }
3348 else
3349 {
3350 /* sizereset */
3351 scale = 0x80;
3352 if (gfx_mode == GFX_V2 || gfx_mode == GFX_V3A)
3353 GfxScaleStackPos = 0;
3354 }
3355
3356 #ifdef L9DEBUG
3357 printf("gfx - size 0x%.2x",scale);
3358 #endif
3359 }
3360
3361 void gintfill(int d7)
3362 {
3363 if ((d7&7) == 0)
3364 /* filla */
3365 d7 = gintcolour;
3366 else
3367 d7 &= 3;
3368 /* fillb */
3369
3370 #ifdef L9DEBUG
3371 printf("gfx - gintfill (%d,%d) colours %d,%d",drawx,drawy,d7&3,option&3);
3372 #endif
3373
3374 os_fill(scalex(drawx),scaley(drawy),d7&3,option&3);
3375 }
3376
3377 void gosub(int d7, L9BYTE** a5)
3378 {
3379 int d0 = ((d7&7)<<8)+(*(*a5)++);
3380 #ifdef L9DEBUG
3381 printf("gfx - gosub 0x%.2x",d0);
3382 #endif
3383 gosubd0(d0,a5);
3384 }
3385
3386 void reflect(int d7)
3387 {
3388 #ifdef L9DEBUG
3389 printf("gfx - reflect 0x%.2x",d7);
3390 #endif
3391
3392 if (d7&4)
3393 {
3394 d7 &= 3;
3395 d7 ^= reflectflag;
3396 }
3397 /* reflect1 */
3398 reflectflag = d7;
3399 }
3400
3401 void notimp(void)
3402 {
3403 #ifdef L9DEBUG
3404 printf("gfx - notimp");
3405 #endif
3406 }
3407
3408 void gintchgcol(L9BYTE** a5)
3409 {
3410 int d0 = *(*a5)++;
3411
3412 #ifdef L9DEBUG
3413 printf("gfx - gintchgcol %d %d",(d0>>3)&3,d0&7);
3414 #endif
3415
3416 os_setcolour((d0>>3)&3,d0&7);
3417 }
3418
3419 void amove(L9BYTE** a5)
3420 {
3421 drawx = 0x40*(*(*a5)++);
3422 drawy = 0x40*(*(*a5)++);
3423 #ifdef L9DEBUG
3424 printf("gfx - amove (%d,%d)",drawx,drawy);
3425 #endif
3426 }
3427
3428 void opt(L9BYTE** a5)
3429 {
3430 int d0 = *(*a5)++;
3431 #ifdef L9DEBUG
3432 printf("gfx - opt 0x%.2x",d0);
3433 #endif
3434
3435 if (d0)
3436 d0 = (d0&3)|0x80;
3437 /* optend */
3438 option = d0;
3439 }
3440
3441 void restorescale(void)
3442 {
3443 #ifdef L9DEBUG
3444 printf("gfx - restorescale");
3445 #endif
3446 if (GfxScaleStackPos > 0)
3447 scale = GfxScaleStack[GfxScaleStackPos-1];
3448 }
3449
3450 L9BOOL rts(L9BYTE** a5)
3451 {
3452 if (GfxA5StackPos > 0)
3453 {
3454 GfxA5StackPos--;
3455 *a5 = GfxA5Stack[GfxA5StackPos];
3456 if (GfxScaleStackPos > 0)
3457 {
3458 GfxScaleStackPos--;
3459 scale = GfxScaleStack[GfxScaleStackPos];
3460 }
3461 return TRUE;
3462 }
3463 return FALSE;
3464 }
3465
3466 L9BOOL getinstruction(L9BYTE** a5)
3467 {
3468 int d7 = *(*a5)++;
3469 if ((d7&0xc0) != 0xc0)
3470 {
3471 switch ((d7>>6)&3)
3472 {
3473 case 0: sdraw(d7); break;
3474 case 1: smove(d7); break;
3475 case 2: sgosub(d7,a5); break;
3476 }
3477 }
3478 else if ((d7&0x38) != 0x38)
3479 {
3480 switch ((d7>>3)&7)
3481 {
3482 case 0: draw(d7,a5); break;
3483 case 1: _move(d7,a5); break;
3484 case 2: icolour(d7); break;
3485 case 3: size(d7); break;
3486 case 4: gintfill(d7); break;
3487 case 5: gosub(d7,a5); break;
3488 case 6: reflect(d7); break;
3489 }
3490 }
3491 else
3492 {
3493 switch (d7&7)
3494 {
3495 case 0: notimp(); break;
3496 case 1: gintchgcol(a5); break;
3497 case 2: notimp(); break;
3498 case 3: amove(a5); break;
3499 case 4: opt(a5); break;
3500 case 5: restorescale(); break;
3501 case 6: notimp(); break;
3502 case 7: return rts(a5);
3503 }
3504 }
3505 return TRUE;
3506 }
3507
3508 void absrunsub(int d0)
3509 {
3510 L9BYTE* a5;
3511 if (!findsub(d0,&a5))
3512 return;
3513 while (getinstruction(&a5));
3514 }
3515
3516 void show_picture(int pic)
3517 {
3518 if (L9GameType == L9_V3 && strlen(FirstLine) == 0)
3519 {
3520 FirstPicture = pic;
3521 return;
3522 }
3523
3524 if (picturedata)
3525 {
3526 /* Some games don't call the screen() opcode before drawing
3527 graphics, so here graphics are enabled if necessary. */
3528 if ((screencalled == 0) && (l9textmode == 0))
3529 {
3530 detect_gfx_mode();
3531 l9textmode = 1;
3532 os_graphics(1);
3533 }
3534
3535 #ifdef L9DEBUG
3536 printf("picture %d",pic);
3537 #endif
3538
3539 os_cleargraphics();
3540 /* gintinit */
3541 gintcolour = 3;
3542 option = 0x80;
3543 reflectflag = 0;
3544 drawx = 0x1400;
3545 drawy = 0x1400;
3546 /* sizereset */
3547 scale = 0x80;
3548
3549 GfxA5StackPos=0;
3550 GfxScaleStackPos=0;
3551 absrunsub(0);
3552 if (!findsub(pic,&gfxa5))
3553 gfxa5 = NULL;
3554 }
3555 }
3556
3557 void picture(void)
3558 {
3559 show_picture(*getvar());
3560 }
3561
3562 void GetPictureSize(int* width, int* height)
3563 {
3564 if (L9GameType == L9_V4)
3565 {
3566 if (width != NULL)
3567 *width = 0;
3568 if (height != NULL)
3569 *height = 0;
3570 }
3571 else
3572 {
3573 if (width != NULL)
3574 *width = (gfx_mode != GFX_V3C) ? 160 : 320;
3575 if (height != NULL)
3576 *height = (gfx_mode == GFX_V2) ? 128 : 96;
3577 }
3578 }
3579
3580 L9BOOL RunGraphics(void)
3581 {
3582 if (gfxa5)
3583 {
3584 if (!getinstruction(&gfxa5))
3585 gfxa5 = NULL;
3586 return TRUE;
3587 }
3588 return FALSE;
3589 }
3590
3591 void initgetobj(void)
3592 {
3593 int i;
3594 numobjectfound=0;
3595 object=0;
3596 for (i=0;i<32;i++) gnoscratch[i]=0;
3597 }
3598
3599 void getnextobject(void)
3600 {
3601 int d2,d3,d4;
3602 L9UINT16 *hisearchposvar,*searchposvar;
3603
3604 #ifdef L9DEBUG
3605 printf("getnextobject");
3606 #endif
3607
3608 d2=*getvar();
3609 hisearchposvar=getvar();
3610 searchposvar=getvar();
3611 d3=*hisearchposvar;
3612 d4=*searchposvar;
3613
3614 /* gnoabs */
3615 do
3616 {
3617 if ((d3 | d4)==0)
3618 {
3619 /* initgetobjsp */
3620 gnosp=128;
3621 searchdepth=0;
3622 initgetobj();
3623 break;
3624 }
3625
3626 if (numobjectfound==0) inithisearchpos=d3;
3627
3628 /* gnonext */
3629 do
3630 {
3631 if (d4==list2ptr[++object])
3632 {
3633 /* gnomaybefound */
3634 int d6=list3ptr[object]&0x1f;
3635 if (d6!=d3)
3636 {
3637 if (d6==0 || d3==0) continue;
3638 if (d3!=0x1f)
3639 {
3640 gnoscratch[d6]=d6;
3641 continue;
3642 }
3643 d3=d6;
3644 }
3645 /* gnofound */
3646 numobjectfound++;
3647 gnostack[--gnosp]=object;
3648 gnostack[--gnosp]=0x1f;
3649
3650 *hisearchposvar=d3;
3651 *searchposvar=d4;
3652 *getvar()=object;
3653 *getvar()=numobjectfound;
3654 *getvar()=searchdepth;
3655 return;
3656 }
3657 } while (object<=d2);
3658
3659 if (inithisearchpos==0x1f)
3660 {
3661 gnoscratch[d3]=0;
3662 d3=0;
3663
3664 /* gnoloop */
3665 do
3666 {
3667 if (gnoscratch[d3])
3668 {
3669 gnostack[--gnosp]=d4;
3670 gnostack[--gnosp]=d3;
3671 }
3672 } while (++d3<0x1f);
3673 }
3674 /* gnonewlevel */
3675 if (gnosp!=128)
3676 {
3677 d3=gnostack[gnosp++];
3678 d4=gnostack[gnosp++];
3679 }
3680 else d3=d4=0;
3681
3682 numobjectfound=0;
3683 if (d3==0x1f) searchdepth++;
3684
3685 initgetobj();
3686 } while (d4);
3687
3688 /* gnofinish */
3689 /* gnoreturnargs */
3690 *hisearchposvar=0;
3691 *searchposvar=0;
3692 *getvar()=object=0;
3693 *getvar()=numobjectfound;
3694 *getvar()=searchdepth;
3695 }
3696
3697 void ifeqct(void)
3698 {
3699 L9UINT16 d0=*getvar();
3700 L9UINT16 d1=getcon();
3701 L9BYTE* a0=getaddr();
3702 if (d0==d1) codeptr=a0;
3703 #ifdef CODEFOLLOW
3704 fprintf(f," if Var[%d]=%d goto %d (%s)",cfvar-workspace.vartable,d1,(L9UINT32) (a0-acodeptr),d0==d1 ? "Yes":"No");
3705 #endif
3706 }
3707
3708 void ifnect(void)
3709 {
3710 L9UINT16 d0=*getvar();
3711 L9UINT16 d1=getcon();
3712 L9BYTE* a0=getaddr();
3713 if (d0!=d1) codeptr=a0;
3714 #ifdef CODEFOLLOW
3715 fprintf(f," if Var[%d]!=%d goto %d (%s)",cfvar-workspace.vartable,d1,(L9UINT32) (a0-acodeptr),d0!=d1 ? "Yes":"No");
3716 #endif
3717 }
3718
3719 void ifltct(void)
3720 {
3721 L9UINT16 d0=*getvar();
3722 L9UINT16 d1=getcon();
3723 L9BYTE* a0=getaddr();
3724 if (d0<d1) codeptr=a0;
3725 #ifdef CODEFOLLOW
3726 fprintf(f," if Var[%d]<%d goto %d (%s)",cfvar-workspace.vartable,d1,(L9UINT32) (a0-acodeptr),d0<d1 ? "Yes":"No");
3727 #endif
3728 }
3729
3730 void ifgtct(void)
3731 {
3732 L9UINT16 d0=*getvar();
3733 L9UINT16 d1=getcon();
3734 L9BYTE* a0=getaddr();
3735 if (d0>d1) codeptr=a0;
3736 #ifdef CODEFOLLOW
3737 fprintf(f," if Var[%d]>%d goto %d (%s)",cfvar-workspace.vartable,d1,(L9UINT32) (a0-acodeptr),d0>d1 ? "Yes":"No");
3738 #endif
3739 }
3740
3741 void printinput(void)
3742 {
3743 L9BYTE* ptr=(L9BYTE*) obuff;
3744 char c;
3745 while ((c=*ptr++)!=' ') printchar(c);
3746
3747 #ifdef L9DEBUG
3748 printf("printinput");
3749 #endif
3750 }
3751
3752 void listhandler(void)
3753 {
3754 L9BYTE *a4,*MinAccess,*MaxAccess;
3755 L9UINT16 val;
3756 L9UINT16 *var;
3757 #ifdef CODEFOLLOW
3758 int offset;
3759 #endif
3760
3761 if ((code&0x1f)>0xa)
3762 {
3763 error("\rillegal list access %d\r",code&0x1f);
3764 Running=FALSE;
3765 return;
3766 }
3767 a4=L9Pointers[1+code&0x1f];
3768
3769 if (a4>=workspace.listarea && a4<workspace.listarea+LISTAREASIZE)
3770 {
3771 MinAccess=workspace.listarea;
3772 MaxAccess=workspace.listarea+LISTAREASIZE;
3773 }
3774 else
3775 {
3776 MinAccess=startdata;
3777 MaxAccess=startdata+FileSize;
3778 }
3779
3780 if (code>=0xe0)
3781 {
3782 /* listvv */
3783 #ifndef CODEFOLLOW
3784 a4+=*getvar();
3785 val=*getvar();
3786 #else
3787 offset=*getvar();
3788 a4+=offset;
3789 var=getvar();
3790 val=*var;
3791 fprintf(f," list %d [%d]=Var[%d] (=%d)",code&0x1f,offset,var-workspace.vartable,val);
3792 #endif
3793
3794 if (a4>=MinAccess && a4<MaxAccess) *a4=(L9BYTE) val;
3795 #ifdef L9DEBUG
3796 else printf("Out of range list access");
3797 #endif
3798 }
3799 else if (code>=0xc0)
3800 {
3801 /* listv1c */
3802 #ifndef CODEFOLLOW
3803 a4+=*codeptr++;
3804 var=getvar();
3805 #else
3806 offset=*codeptr++;
3807 a4+=offset;
3808 var=getvar();
3809 fprintf(f," Var[%d]= list %d [%d])",var-workspace.vartable,code&0x1f,offset);
3810 if (a4>=MinAccess && a4<MaxAccess) fprintf(f," (=%d)",*a4);
3811 #endif
3812
3813 if (a4>=MinAccess && a4<MaxAccess) *var=*a4;
3814 else
3815 {
3816 *var=0;
3817 #ifdef L9DEBUG
3818 printf("Out of range list access");
3819 #endif
3820 }
3821 }
3822 else if (code>=0xa0)
3823 {
3824 /* listv1v */
3825 #ifndef CODEFOLLOW
3826 a4+=*getvar();
3827 var=getvar();
3828 #else
3829 offset=*getvar();
3830 a4+=offset;
3831 var=getvar();
3832
3833 fprintf(f," Var[%d] =list %d [%d]",var-workspace.vartable,code&0x1f,offset);
3834 if (a4>=MinAccess && a4<MaxAccess) fprintf(f," (=%d)",*a4);
3835 #endif
3836
3837 if (a4>=MinAccess && a4<MaxAccess) *var=*a4;
3838 else
3839 {
3840 *var=0;
3841 #ifdef L9DEBUG
3842 printf("Out of range list access");
3843 #endif
3844 }
3845 }
3846 else
3847 {
3848 #ifndef CODEFOLLOW
3849 a4+=*codeptr++;
3850 val=*getvar();
3851 #else
3852 offset=*codeptr++;
3853 a4+=offset;
3854 var=getvar();
3855 val=*var;
3856 fprintf(f," list %d [%d]=Var[%d] (=%d)",code&0x1f,offset,var-workspace.vartable,val);
3857 #endif
3858
3859 if (a4>=MinAccess && a4<MaxAccess) *a4=(L9BYTE) val;
3860 #ifdef L9DEBUG
3861 else printf("Out of range list access");
3862 #endif
3863 }
3864 }
3865
3866 void executeinstruction(void)
3867 {
3868 #ifdef CODEFOLLOW
3869 f=fopen(CODEFOLLOWFILE,"a");
3870 fprintf(f,"%ld (s:%d) %x",(L9UINT32) (codeptr-acodeptr)-1,workspace.stackptr,code);
3871 if (!(code&0x80))
3872 fprintf(f," = %s",codes[code&0x1f]);
3873 #endif
3874
3875 if (code & 0x80)
3876 listhandler();
3877 else
3878 {
3879 switch (code & 0x1f)
3880 {
3881 case 0: Goto();break;
3882 case 1: intgosub();break;
3883 case 2: intreturn();break;
3884 case 3: printnumber();break;
3885 case 4: messagev();break;
3886 case 5: messagec();break;
3887 case 6: function();break;
3888 case 7: input();break;
3889 case 8: varcon();break;
3890 case 9: varvar();break;
3891 case 10: _add();break;
3892 case 11: _sub();break;
3893 case 12: ilins(code & 0x1f);break;
3894 case 13: ilins(code & 0x1f);break;
3895 case 14: jump();break;
3896 case 15: Exit();break;
3897 case 16: ifeqvt();break;
3898 case 17: ifnevt();break;
3899 case 18: ifltvt();break;
3900 case 19: ifgtvt();break;
3901 case 20: _screen();break;
3902 case 21: cleartg();break;
3903 case 22: picture();break;
3904 case 23: getnextobject();break;
3905 case 24: ifeqct();break;
3906 case 25: ifnect();break;
3907 case 26: ifltct();break;
3908 case 27: ifgtct();break;
3909 case 28: printinput();break;
3910 case 29: ilins(code & 0x1f);break;
3911 case 30: ilins(code & 0x1f);break;
3912 case 31: ilins(code & 0x1f);break;
3913 }
3914 }
3915 #ifdef CODEFOLLOW
3916 fprintf(f,"\n");
3917 fclose(f);
3918 #endif
3919 }
3920
3921 L9BOOL LoadGame2(char *filename,char *picname)
3922 {
3923 #ifdef CODEFOLLOW
3924 f=fopen(CODEFOLLOWFILE,"w");
3925 fprintf(f,"Code follow file...\n");
3926 fclose(f);
3927 #endif
3928
3929 /* may be already running a game, maybe in input routine */
3930 Running=FALSE;
3931 ibuffptr=NULL;
3932
3933 /* intstart */
3934 if (!intinitialise(filename,picname)) return FALSE;
3935 /* if (!checksumgamedata()) return FALSE; */
3936
3937 codeptr=acodeptr;
3938 if (constseed > 0)
3939 randomseed=constseed;
3940 else
3941 randomseed=(L9UINT16)time(NULL);
3942 strcpy(LastGame,filename);
3943 return Running=TRUE;
3944 }
3945
3946 L9BOOL LoadGame(char *filename,char *picname)
3947 {
3948 L9BOOL ret=LoadGame2(filename,picname);
3949 showtitle=1;
3950 clearworkspace();
3951 workspace.stackptr=0;
3952 /* need to clear listarea as well */
3953 memset((L9BYTE*) workspace.listarea,0,LISTAREASIZE);
3954 return ret;
3955 }
3956
3957 /* can be called from input to cause fall through for exit */
3958 void StopGame(void)
3959 {
3960 Running=FALSE;
3961 }
3962
3963 L9BOOL RunGame(void)
3964 {
3965 code=*codeptr++;
3966 /* printf("%d",code); */
3967 executeinstruction();
3968 return Running;
3969 }
3970
3971 void RestoreGame(char* filename)
3972 {
3973 int Bytes;
3974 GameState temp;
3975 FILE* f = NULL;
3976
3977 if ((f = fopen(filename, "rb")) != NULL)
3978 {
3979 Bytes = fread(&temp, 1, sizeof(GameState), f);
3980 if (Bytes==V1FILESIZE)
3981 {
3982 printstring("\rGame restored.\r");
3983 /* only copy in workspace */
3984 memset(workspace.listarea,0,LISTAREASIZE);
3985 memmove(workspace.vartable,&temp,V1FILESIZE);
3986 }
3987 else if (CheckFile(&temp))
3988 {
3989 printstring("\rGame restored.\r");
3990 /* full restore */
3991 memmove(&workspace,&temp,sizeof(GameState));
3992 codeptr=acodeptr+workspace.codeptr;
3993 }
3994 else
3995 printstring("\rSorry, unrecognised format. Unable to restore\r");
3996 }
3997 else
3998 printstring("\rUnable to restore game.\r");
3999 }
4000