Debian Codegraph @

terps/agility/agtread.c

raw symbols SCIP
1 /* agtread.c-- functions to read in AGT files and identify */
2 /* their version. */
3 /* Copyright (C) 1996-1999,2001 Robert Masenten */
4 /* This program may be redistributed under the terms of the
5 GNU General Public License, version 2; see agility.h for details. */
6 /* */
7 /* This is part of the source for AGiliTy: the (Mostly) Universal */
8 /* AGT Interpreter */
9
10
11 #include <ctype.h>
12 #include "agility.h"
13
14
15 static void read_da6(fc_type fc);
16 static void check_cmd_version(void);
17 static void build_cmd_table(void);
18 static void fixcmd(integer*,int);
19
20 /* This parses the block of integers of a command to standardize
21 the command numbers */
22 static short *cmd_table; /* Holds the command translation table used
23 by fixcmd */
24 int topcmd; /* The highest legal opcode in the current AGT version. */
25
26
27
28 static genfile fd_desc; /* File pointer for description file. */
29 static long desc_size; /* Size of description file. */
30
31 static int top_quest; /* Highest question actually referenced */
32 /* This is computed by fixcmd */
33
34 static int SL_NAME,SL_TEXT,SL_ROOM,SL_WORD;
35 static integer MAX_CMD_SIZE;
36
37 static rbool encrypt_desc=1; /* Are descriptions encrypted? */
38
39
40 /* This translates v1.8 status mode codes into ME statue mode codes */
41 const uchar agt18_statmode[]={0,4,3,1};
42
43
44 /*-------------------------------------------------------------------------*/
45 /* Utilities to convert strings, do ASCII translation, etc. and to add */
46 /* words to the dictionary (the actual dictionary routines are in */
47 /* agtdata.c; these routines here are just wrappers that prepare words */
48 /* to be added). */
49 /*-------------------------------------------------------------------------*/
50
51
52
53 static void fatals(char *msg,char *fname)
54 {
55 char *err;
56
57 err=rmalloc(strlen(msg)+strlen(fname)+1);
58 sprintf(err,msg,fname);
59 rfree(fname);
60 fatal(err);
61 rfree(err); /* This should never be executed */
62 }
63
64
65 static word add_dic1(uchar *buff, int n)
66 {
67 char nbuff[100];
68
69 int i;
70 if (n>100) n=100;
71 for(i=0;i<buff[0] && i<n;i++)
72 nbuff[i]=buff[i+1];
73 nbuff[i]=0;
74 return add_dict(nbuff);
75 }
76
77
78 static slist add_slist(uchar *buff)
79 {
80 int j,k;
81 slist start_ptr;
82 char nbuff[100];
83
84 k=0;
85 start_ptr=synptr;
86 if (buff[0]>80) fatal("Invalid game file format");
87 for(j=1;j<=buff[0];j++)
88 if (rspace(buff[j]) && k>0)
89 {
90 nbuff[k]=0;
91 addsyn(add_dict(nbuff));
92 k=0;
93 }
94 else nbuff[k++]=buff[j];
95 if (k>0) {
96 nbuff[k]=0;
97 addsyn(add_dict(nbuff));
98 }
99 addsyn(-1); /* End of list marker */
100 return start_ptr;
101 }
102
103
104
105
106 /*-------------------------------------------------------------------------*/
107 /* Description file manipulation routines: routines to open and close the */
108 /* description file and read in individual descriptions. */
109 /*-------------------------------------------------------------------------*/
110
111 /* The memory-based stuff is not done yet */
112
113
114 void convert_agt_descr(uchar *s)
115 /* Convert encrypted pascal string into plaintext C string */
116 {
117 int j,n;
118
119 n=s[0];
120 if (n>80) {
121 s[0]=0;
122 return;
123 }
124
125 if (encrypt_desc)
126 for(j=0;j<n;j++)
127 if(s[j+1]!=' ')
128 s[j]=fixchar[(s[j+1] - (j+1) + 0x100 )&0xFF];
129 else s[j]=' ';
130 else
131 for(j=0;j<n;j++)
132 s[j]=fixchar[s[j+1]];
133
134 s[n]=0;
135 }
136
137
138 void open_descr(fc_type fc)
139 {
140 char *errstr;
141 long i;
142 int alpha,cnt;
143 tline buff;
144
145 fd_desc=readopen(fc,fDSS,&errstr);
146 if (errstr!=NULL) fatal(errstr);
147 desc_size=binsize(fd_desc);
148 if (DIAG) {
149 char *s;
150 s=formal_name(fc,fDSS);
151 rprintf("Opened file %s (size:%ld)\n",s,desc_size);
152 rfree(s);
153 }
154
155 /* <Sigh> Now need to figure out if the input is encoded. Do this by
156 reading a few random lines and seeing if they "look" encoded */
157 alpha=cnt=0;
158 if (aver>AGT135 || aver==AGTCOS)
159 encrypt_desc=1;
160 else {
161 binread(fd_desc,buff,81,1,&errstr); /* Throw away first line */
162 while(cnt<300)
163 {
164 if (!binread(fd_desc,buff,81,1,&errstr)) /* EOF */
165 {
166 writeln("");
167 agtwarn("EOF while analyzing descriptions",0);
168 rprintf("......assuming type ");
169 break;
170 }
171 if (buff[0]>0 && buff[1]!=' ' && buff[1]>0) /* To avoid "banner"
172 lines */
173 {
174 for(i=1;i<=buff[0];i++)
175 {
176 if (buff[i]>='A' && buff[i]<='z') alpha++;
177 if (buff[i]!=' ') cnt++;
178 }
179 }
180 }
181 if (3*cnt<4*alpha) {
182 encrypt_desc=0;
183 if (aver==AGT135) aver=AGT12;
184 }
185 else encrypt_desc=1;
186 }
187 if (DIAG) {
188 if (encrypt_desc) rprintf(" [encrypted]\n");
189 else rprintf(" [plaintext: %d/%d]\n",alpha,cnt);}
190
191 mem_descr=NULL;
192 if (desc_size<=descr_maxmem) {
193 /* This is where we need to read the data in and convert it:
194 encrypted Pascal strings --> plaintext C strings */
195 binseek(fd_desc,0);
196 mem_descr=rmalloc(desc_size);
197 /* Read in the whole file */
198 binread(fd_desc,mem_descr,desc_size,1,&errstr);
199 if (errstr!=NULL) fatal(errstr);
200 for(i=0;i<desc_size;i+=sizeof(tline))
201 convert_agt_descr((uchar*)(mem_descr+i));
202 /* Decode and convert to C string */
203 }
204 }
205
206
207 void close_descr(void)
208 {
209 if (mem_descr!=NULL)
210 rfree(mem_descr);
211 else {
212 readclose(fd_desc);
213 fd_desc=NULL;
214 }
215 }
216
217
218 descr_line *agt_read_descr(long start,long len)
219 {
220 tline *d;
221 descr_line *lines;
222 long i;
223 char *errstr;
224
225 if (len==-1 || start==-1) return NULL;
226 lines=rmalloc(sizeof(descr_line)*(len+1));
227
228 if (mem_descr!=NULL) {
229 d=((tline*)mem_descr)+start;
230 for(i=0;i<len;i++)
231 lines[i]=(char*)(d+i);
232 } else {
233 d=rmalloc(sizeof(tline)*len);
234 binseek(fd_desc,start*sizeof(tline));
235 binread(fd_desc,d,sizeof(tline),len,&errstr);
236 if (errstr!=NULL) fatal(errstr);
237 for(i=0;i<len;i++) {
238 lines[i]=(char*)(d+i);
239 convert_agt_descr((uchar*)(d+i));
240 }
241 }
242 lines[len]=NULL; /* Mark end of array */
243 return lines;
244 }
245
246
247
248 /*-------------------------------------------------------------------------*/
249 /* Read DA2: The Room File. */
250 /*-------------------------------------------------------------------------*/
251
252
253 #define seti(a) (room[i].a=buff[bp] | (buff[bp+1]<<8),bp+=2)
254 #define set32(a) (room[i].a=buff[bp] | (buff[bp+1]<<8) | (buff[bp+2]<<16)|\
255 (buff[bp+3]<<24), bp+=4)
256 #define setb(a) (room[i].a=buff[bp],bp++)
257
258 #define setstr(leng) (bp+=(leng),new_str((char*)buff+bp-(leng),(leng),1))
259 #define setd(leng) (bp+=(leng),add_dic1(buff+bp-(leng),(leng)))
260 #define setsl() (bp+=sizeof(tline),add_slist(buff+bp-sizeof(tline)))
261 #define nonecheck(leng) (memcmp(buff+bp,nonestr,5)==0)
262
263
264 static void read_da2(fc_type fc)
265 {
266 int i, j, numroom;
267 uchar *buff; /* [FRS_ROOM];*/
268 long bp;
269
270 numroom=maxroom-first_room+1;
271 if (numroom<0) return;
272 room_name=rmalloc(numroom*sizeof(long));
273
274 buffopen(fc,fDA2,FRS_ROOM,"room",numroom);
275
276 bp=0;
277 for(i=0;i<numroom;i++)
278 {
279 buff=buffread(i);
280 bp=0;
281 if (nonecheck(SL_ROOM))
282 room[i].unused=1;
283 else room[i].unused=0;
284 room_name[i]=setstr(SL_ROOM);
285 room[i].replace_word=setd(SL_WORD);
286 room[i].replacing_word=setsl();
287 for(j=0;j<12;j++) seti(path[j]);
288
289 if (aver>=AGT15) set32(flag_noun_bits); /* Menu flags */
290 else room[i].flag_noun_bits=0;
291
292 if (aver>=AGTME10) set32(PIX_bits); /* PIX bits */
293 else room[i].PIX_bits=0;
294
295 seti(path[12]); /* Special */
296
297 /* There's a small possibility that the 1.5/Hotel flag_noun_bits
298 goes here, rather than above; 1.5/F is known to go above */
299
300 setb(seen);
301 seti(key);setb(locked_door);
302 if (room_inside!=NULL)
303 room_inside[i]=fixsign16(buff[bp],buff[bp+1]);
304 bp+=2; /* Skip # of nouns in this room */
305
306 seti(points);seti(light);setb(end);setb(win);
307 if (aver!=AGT10) setb(killplayer); /* I'm guessing here */
308 else room[i].killplayer=room[i].end;
309
310 if (aver>=AGTME10)
311 { seti(initdesc);seti(pict);}
312 else {room[i].initdesc=0;room[i].pict=0;}
313 if (aver>=AGTME15) room[i].autoverb=setd(SL_WORD);
314 else room[i].autoverb=0;
315 room[i].oclass=0;
316 room[i].seen=0;
317 }
318 if (DIAG)
319 rprintf(" Internal:%ld\n",bp);
320 buffclose();
321 }
322
323
324
325 /*-------------------------------------------------------------------------*/
326 /* Read DA3: The Noun File. */
327 /*-------------------------------------------------------------------------*/
328
329 #undef seti
330 #undef setb
331 #define seti(a) (noun[i].a=buff[bp] | (buff[bp+1]<<8),bp+=2)
332 #define setb(a) (noun[i].a=buff[bp],bp++)
333
334 static void read_da3(fc_type fc)
335 {
336 int i, numnoun;
337 long recsize;
338 uchar *buff; /* [FRS_NOUN];*/
339 long bp;
340
341 numnoun=maxnoun-first_noun+1;
342 if (numnoun<0) return;
343 noun_sdesc=rmalloc(numnoun*sizeof(long));
344 noun_pos=rmalloc(numnoun*sizeof(long));
345
346 recsize=buffopen(fc,fDA3,FRS_NOUN,"noun",numnoun);
347 if (aver==AGT15 && recsize>263) aver=AGT15F;
348
349 bp=0;
350 for(i=0;i<numnoun;i++)
351 {
352 buff=buffread(i);
353 bp=0;
354 if (nonecheck(SL_NAME)) {
355 bp+=SL_NAME;
356 noun[i].name=0;
357 noun[i].unused=1;
358 } else {
359 noun[i].name=setd(SL_NAME);
360 noun[i].unused=0;
361 }
362 noun_sdesc[i]=setstr(SL_TEXT);
363 noun[i].adj=setd(SL_NAME);
364
365 if (aver>=AGT15F) seti(initdesc);
366 else noun[i].initdesc=0;
367
368 setb(plural);
369 /* The following is a guess for ME games */
370 noun_pos[i]=setstr((ver==3)?SL_ROOM:SL_NAME);
371 setb(something_pos_near_noun); /* These may not be valid */
372 seti(nearby_noun); /* in masters ed. */
373
374 setb(has_syns);
375 noun[i].syns=setsl(); /*,SL_TEXT);*/
376 if (aver>=AGT15)
377 noun[i].related_name=setd(SL_NAME);
378 else
379 noun[i].related_name=0;
380 seti(location);seti(weight);seti(size);
381 seti(key);
382 /* All of following flags known to be valid except
383 pullable, on, and win */
384 setb(pushable);setb(pullable);setb(turnable);setb(playable);
385 setb(readable);setb(on);setb(closable);setb(open);setb(lockable);
386 setb(locked);setb(edible);setb(wearable);setb(drinkable);
387 setb(poisonous);setb(movable);setb(light);setb(shootable);
388 seti(num_shots);seti(points);
389 if (noun_inside!=NULL)
390 noun_inside[i]=fixsign16(buff[bp],buff[bp+1]);
391 bp+=2; /* Skip # of nouns contained in this one */
392 setb(win);
393 if (ver==3) seti(pict);
394 else noun[i].pict=0;
395 noun[i].oclass=0; /* AGT games don't support classes */
396 noun[i].isglobal=0;
397 noun[i].flagnum=0;
398 noun[i].seen=0;
399 noun[i].proper=0;
400 }
401 if (DIAG)
402 rprintf(" Internal:%ld\n",bp);
403 buffclose();
404 }
405
406
407 #undef seti
408 #undef setb
409 #define seti(a) (creature[i].a=buff[bp] | (buff[bp+1]<<8),bp+=2)
410 #define setb(a) (creature[i].a=buff[bp],bp++)
411
412
413
414 /*-------------------------------------------------------------------------*/
415 /* Read DA4: The Creature File. */
416 /*-------------------------------------------------------------------------*/
417
418 static void read_da4(fc_type fc)
419 {
420 int i, numcreat;
421 uchar *buff; /* [FRS_CREAT];*/
422 long bp;
423
424 numcreat=maxcreat-first_creat+1;
425 if (numcreat<=0) return;
426 creat_sdesc=rmalloc(numcreat*sizeof(long));
427
428 buffopen(fc,fDA4,FRS_CREAT,"creature",numcreat);
429
430 bp=0;
431 for(i=0;i<numcreat;i++)
432 {
433 buff=buffread(i);
434 bp=0;
435 if (nonecheck(SL_NAME)) {
436 bp+=SL_NAME;
437 creature[i].name=0;
438 creature[i].unused=1;
439 } else {
440 creature[i].name=setd(SL_NAME);
441 creature[i].unused=0;
442 }
443 creat_sdesc[i]=setstr(SL_TEXT);
444 creature[i].adj=setd(SL_NAME);
445 if (ver==3) seti(initdesc);
446 else creature[i].initdesc=0;
447 setb(has_syns);
448 creature[i].syns=setsl();
449 setb(groupmemb);
450 seti(location);seti(weapon);
451 setb(hostile);
452 seti(points);
453 if (creat_inside!=NULL)
454 creat_inside[i]=fixsign16(buff[bp],buff[bp+1]);
455 bp+=2; /* Skip # of nouns the creature is carrying */
456 seti(counter);seti(threshold);seti(timethresh);seti(timecounter);
457 setb(gender);
458 if (ver==3) seti(pict);
459 else creature[i].pict=0;
460 creature[i].oclass=0; /* AGT games don't support classes */
461 creature[i].isglobal=0;
462 creature[i].flagnum=0;
463 creature[i].seen=0;
464 creature[i].proper=0;
465 }
466 if (DIAG)
467 rprintf(" Internal:%ld\n",bp);
468 buffclose();
469 }
470
471 #undef seti
472 #undef setb
473
474
475
476
477 /*-------------------------------------------------------------------------*/
478 /* Read Commands (DA5 and DA6) and convert them to a uniform internal */
479 /* format. */
480 /*-------------------------------------------------------------------------*/
481
482
483 static int translate_vnum(int vnum)
484 /* actor is a numerical index occuring at the beginning of each command.
485 In general, it contains the verb number of the verb associated with
486 this command; because of AGiliTy's dictionary organization, we don't
487 really need this (the verb itself will be converted to a number anyhow),
488 but the field contains other useful information as well:
489 i)If this command header is really the object of a redirection command,
490 then the actor will have 1000 or 2000 added to it, depending on
491 AGT version.
492 ii)If this command is directed at an actor, then the creature number
493 will be in this field instead of the verb number.
494 Commands directed to ANYBODY have one plus the maximum verb number
495 in this field (a hassle since the maximum verb number depends on
496 AGT version: Classic:106, Master's:123); EVERYONE is the next
497 code after ANYBODY.
498 What this routine does is rationalize the differences between AGT versions.
499 --Verb values (ie. not referring to creatures) are converted to 1.
500 --Redirections are marked by multiplying by negative one and setting
501 cmdsize to 0.
502 --ANYBODY is set to 2
503 --EVERYBODY is set to 3
504 */
505 {
506 rbool redir; /* Is this command redirected? */
507 integer anycode;
508 int redir_val;
509
510 /* Earlier games use 1000 as redirect value, later games use 2000: */
511 /* We strip it off, but remember whether it was there or not so we
512 can restore this information later. */
513 redir_val=(aver<=AGT18MAX ? 1000 : 2000);
514 if (vnum>=redir_val) {
515 vnum=vnum%redir_val;
516 redir=1;
517 } else redir=0;
518
519 anycode=(aver<=AGT18MAX) ? 106 : 123;
520
521 /* Now to correct ANYBODY to something consistent and set verb
522 references to 1 since we don't need them and they just confuse things */
523 if (vnum<anycode) vnum=1; /* "player" */
524 else if (vnum==anycode) vnum=2; /* ANYBODY */
525 else if (vnum==anycode+1) vnum=3; /* EVERYBODY */
526
527 /* Finally restore redirection info. We now use the sign of vnum
528 to indicate this.*/
529 if (redir) vnum=-vnum;
530
531 return vnum;
532 }
533
534
535 #define CREC_SIZE (FRS_CMD)
536
537 static long badtokcnt;
538
539 static void read_da5(fc_type fc)
540 {
541 long i, j;
542 uchar *buff; /* [CREC_SIZE];*/
543 long bp;
544
545 if (!have_meta) return;
546 if (last_cmd<=0)
547 fatal("Bogus last_cmd");
548
549 buffopen(fc,fDA5,CREC_SIZE,"command",last_cmd);
550
551 if (aver>=AGT15F) cmd_ptr=rmalloc(sizeof(long)*last_cmd);
552 else cmd_ptr=NULL;
553
554 bp=0;
555 for(i=0;i<last_cmd;i++)
556 {
557 buff=buffread(i);
558 command[i].actor=translate_vnum(buff[0]+(buff[1]<<8));
559 bp=2;
560 command[i].verbcmd=setd(SL_WORD);
561 command[i].nouncmd=setd(SL_WORD);
562 if (aver>=AGTME10)
563 command[i].prep=setd(SL_WORD);
564 else command[i].prep=0;
565 command[i].objcmd=setd(SL_WORD);
566 command[i].noun_adj=command[i].obj_adj=0;
567 command[i].noun_obj=command[i].obj_obj=0;
568 if (aver<AGT15F) {
569 command[i].data=rmalloc(MAX_CMD_SIZE*sizeof(integer));
570 for(j=0;j<MAX_CMD_SIZE;j++)
571 command[i].data[j]=fixsign16(buff[bp+2*j],buff[bp+2*j+1]);
572 bp+=2*MAX_CMD_SIZE;
573 command[i].cmdsize=MAX_CMD_SIZE;
574 }
575 else {
576 cmd_ptr[i]=(long)buff[bp]+(((long)buff[bp+1])<<8);
577 bp+=2;
578 }
579 }
580 if (DIAG)
581 rprintf(" Internal:%ld\n",bp);
582 buffclose();
583
584 /* Now to read in DA6 for versions that have it */
585 if (aver>=AGT15F) read_da6(fc);
586 check_cmd_version(); /* This uses the opcodes to check gamefile
587 version information and change it if neccesary. */
588 build_cmd_table(); /* Create the command translation table for
589 this version of AGT. */
590
591 badtokcnt=0;
592 if (!RAW_CMD_OUT)
593 for(i=0;i<last_cmd;i++)
594 fixcmd(command[i].data,command[i].cmdsize);
595 rfree(cmd_table);
596 if (badtokcnt>MAX_BADTOK)
597 agtnwarn("Total number of bad opcodes:",badtokcnt,1);
598 }
599
600
601
602 static void read_da6(fc_type fc)
603 /* This will only be called for versions with a DA6 file--
604 i.e. Master's Edition and proto-ME games. */
605 {
606 genfile fda6;
607 char *fullname, *errstr;
608 long fsize; /* Size of the file */
609 long frame; /* The first element of the file that is in the buffer. */
610 uchar *cbuf; /* Buffer */
611 long cfile_size, cbuf_size; /* Number of tokens in file and in buffer */
612 long i, j;
613 long cmdstart, cmdend; /* Marks the start and end of the current command */
614 long ip; /* Points to instruction in cmd.data[] that we are writing to */
615 long bp; /* Pointer into buffer */
616 long endp; /* Used to indicate end of current read loop
617 (with an infinite buffer, this would always be an adjusted
618 cmdend) */
619 long adj_cbuf_size; /* Stores number of bytes actually read in to cbuf */
620
621 fda6=openbin(fc,fDA6,"Could not open code file '%s'.",1);
622 fsize=binsize(fda6);
623 fullname=formal_name(fc,fDA6);
624 if (DIAG) rprintf("Reading code file %s (size:%ld)\n",fullname,fsize);
625
626 if (aver==AGT15F && fsize==20000) aver=AGT16;
627 if (aver>=AGTME10) cfile_size=20000;
628 else if (aver==AGT16) cfile_size=10000;
629 else cfile_size=5000;
630
631 if (fsize!=2*cfile_size)
632 fatals("Code file %s is the wrong size.",fullname);
633
634 cbuf_size= (cfile_size<CBUF_SIZE) ? cfile_size: CBUF_SIZE;
635 cbuf=rmalloc(2*cbuf_size);
636 frame=cfile_size+1; /* Guarentee frame will be wrong */
637
638 for(i=0;i<last_cmd;i++)
639 if (cmd_ptr[i]>=2) {
640 for(j=i+1;j<last_cmd && cmd_ptr[j]<=cmd_ptr[i];j++);
641 if (j<last_cmd) cmdend=cmd_ptr[j];
642 else cmdend=cfile_size;
643 if (cmdend>cfile_size) fatals("Code file overrun(%s)",fullname);
644 --cmdend;
645 cmdstart=cmd_ptr[i]-1;
646 command[i].cmdsize=cmdend-cmdstart;
647 command[i].data=rmalloc(command[i].cmdsize*sizeof(integer));
648
649 ip=0;
650 bp=cmdstart-frame;
651 adj_cbuf_size=cbuf_size;
652
653 while(ip<command[i].cmdsize) {
654 if (bp<0 || bp>=adj_cbuf_size) { /* Read in new block */
655 frame=frame+bp;
656 binseek(fda6,frame*2);
657 if (frame+cbuf_size<=cfile_size)
658 adj_cbuf_size=cbuf_size;
659 else
660 adj_cbuf_size=cfile_size-frame;
661 if (adj_cbuf_size<=0) fatal("Unexpected end of file.");
662 if (!binread(fda6,cbuf,2,adj_cbuf_size,&errstr))
663 fatal(errstr);
664 bp=0;
665 }
666 endp=cmdend-frame;
667 if (endp>cbuf_size) endp=cbuf_size;
668 for(;bp<endp;ip++,bp++)
669 command[i].data[ip]=fixsign16(cbuf[bp*2L],cbuf[bp*2L+1]);
670 }
671 }
672 else {
673 command[i].data=NULL;
674 command[i].cmdsize=0;
675 }
676 rfree(cbuf);
677 readclose(fda6);
678 rfree(fullname);
679 }
680
681
682
683
684
685 static int check_endcmd(void)
686 /* What is the most common last byte for metacommands? Except
687 under very abnormal situations, this should be the EndCmd opcode */
688 {
689 int count[MAX_TOKEN_ID+1];
690 int i, tok, maxcnt, maxtok;
691 /* int nextcnt; */
692
693 for(i=0;i<=MAX_TOKEN_ID;i++) count[i]=0;
694 for(i=0;i<last_cmd;i++)
695 if (command[i].cmdsize>0) {
696 tok=command[i].data[command[i].cmdsize-1];
697 if (tok>=0 && tok<=MAX_TOKEN_ID) count[tok]++;
698 }
699 maxcnt=maxtok=0; /* nextcnt=0;*/
700 for(i=0;i<=MAX_TOKEN_ID;i++)
701 if (count[i]>=maxcnt) {
702 /* nextcnt=maxcnt; */
703 maxcnt=count[i];
704 maxtok=i;}
705 return maxtok;
706 }
707
708 static int compute_endcode(int ver)
709 /* Computes the correct endcode for a given gamefile version */
710 {
711 int i;
712
713 for(i=0;FIX_LIST[ver][i].tnew!=-1;i++);
714 return (FIX_LIST[ver][i].told-3); /* -3 to get to EndCmd */
715 }
716
717
718 static void check_cmd_version(void)
719 /* Run through the commands looking at the last legal byte. This is
720 normally the EndCmd token code, which can give us info on which
721 token encoding scheme is being used. */
722 {
723 int endcode;
724
725 endcode=check_endcmd();
726 if (DIAG) rprintf(" (EndCmd=%d)\n",endcode);
727 if (endcode<150) return; /* No metacommands, or something else is wrong. */
728 if (endcode==compute_endcode(aver)) return; /* We're okay */
729
730 /* Check for the special cases we know about */
731 if (aver==AGT135) {
732 if (endcode==compute_endcode(AGT182)) {aver=AGT182;return;}
733 else if (endcode==compute_endcode(AGT118)) {aver=AGT118; return;}
734 }
735 if (aver==AGTME10)
736 if (endcode==compute_endcode(AGTME10A)) {aver=AGTME10A;return;}
737 if (aver==AGTMAST)
738 if (endcode==compute_endcode(AGTME155)) {aver=AGTME155;return;}
739
740 /* If we still haven't fixed the problem, print out a warning and
741 pray. */
742 agtnwarn("Game has invalid EndCmd: ",endcode,1);
743 }
744
745
746
747 static void build_cmd_table(void)
748 {
749 int told,tnew,fp;
750 const cmd_fix_rec *fixtbl;
751
752 topcmd=compute_endcode(aver)+3;
753 cmd_table=rmalloc(sizeof(short)*topcmd);
754
755 fixtbl=FIX_LIST[aver];
756 fp=0; /* Pointer into fix table */
757 tnew=0; /* This shouldn't be neccessary */
758 for(told=0;told<topcmd;) {
759 if (told==fixtbl[fp].told) tnew=fixtbl[fp++].tnew;
760 cmd_table[told++]=tnew++;
761 }
762 }
763
764
765
766 static void badtokerr(char *s, int tok)
767 {
768 if (++badtokcnt<=MAX_BADTOK) agtnwarn(s,tok,1);
769 }
770
771 static void fixcmd(integer *clist,int cnt)
772 /* Okay, we need to go through the elements of clist (which is an array,
773 actually), figure out which ones are commands (as opposed to arguments)
774 and tweak them to hide version differences. */
775 {
776 long ip;
777
778 /* Now need to go through and adjust opcodes. */
779 for(ip=0;ip<cnt;ip++)
780 if (clist[ip]>=topcmd || clist[ip]<0)
781 badtokerr("Invalid token found: ",clist[ip]);
782 else {
783
784 clist[ip]=cmd_table[clist[ip]]; /* Translate */
785
786 /* Now increment ip by the length of the instruction */
787 /* Remember that we are already incrementing by one automatically */
788
789 if (clist[ip]>=END_ACT) break; /* CMD end marker */
790 if (clist[ip]<=MAX_COND)
791 ip+=cond_def[clist[ip]].argnum;
792 else if (clist[ip]<WIN_ACT) {
793 if (clist[ip]==1087 && ip+1<cnt) /* AskQuestion: Adjust top_quest */
794 if (top_quest<clist[ip+1]) top_quest=clist[ip+1];
795 ip+=act_def[clist[ip]-START_ACT].argnum;
796 }
797 /* else do nothing */
798 }
799 }
800
801
802
803
804
805 /*-------------------------------------------------------------------------*/
806 /* DA1 Reading Utilites: routines to read the various lines of the DA1 file */
807 /*-------------------------------------------------------------------------*/
808
809 static void chop_newline(char *s)
810 /* Remove trailing \r,\n, etc. characters */
811 {
812 char *t;
813
814 for(t=s;*t!=0;t++); /* Find the end of the string */
815 for(;t>=s && (*t==0 || *t=='\r'|| *t=='\n');t--);
816 *(t+1)=0;
817 }
818
819 static void fix_answer(char *s)
820 /* Put answer s into standard format: all lower case and with trailing/
821 following whitespace removed */
822 {
823 char *t, *p;
824
825 for(t=s;*t!=0;t++)
826 *t=tolower(*t);
827 /* Eliminate trailing space and newlines */
828 for(;t>=s && (*t==0 || rspace(*t)); t--);
829 *(t+1)=0;
830 /* Eliminate leading space and newlines */
831 for(t=s;rspace(*t);t++);
832 if (t!=s) {
833 for(p=s;*t!=0;)
834 *(p++)=*(t++);
835 *p=0;
836 }
837 }
838
839
840 static char linebuffer[81];
841 static int bhold;
842 static int linenum;
843 static rbool unexpected_eof;
844
845 static void read_line(genfile fd,char *typestr)
846 /* Read a line into buffer, unless bhold=1 in which case we want
847 to use the last line read */
848 {
849 if (bhold==0)
850 {
851 readln(fd,linebuffer,80);
852 if (linebuffer[0]==0 && texteof(fd)) {
853 unexpected_eof=1;
854 strcpy(linebuffer,">End Of File<");}
855 else chop_newline(linebuffer);
856 linenum++;
857 }
858 if (debug_da1 && typestr!=NULL)
859 {
860 rprintf("%s %4d:%s",typestr,linenum,linebuffer);
861 if (bhold) rprintf(" *");
862 writeln("");
863 }
864 bhold=0;
865 }
866
867
868 static void report(char *s,genfile fd)
869 {
870 if (DIAG) rprintf("REPORT:%s at %d\n",s,linenum);
871 }
872
873 static int isbool(genfile fd)
874 {
875 read_line(fd,NULL);
876 bhold=1;
877 return (strncasecmp(linebuffer,"TRUE",4)==0 ||
878 strncasecmp(linebuffer,"FALSE",5)==0);
879 }
880
881 static int isnum(genfile fd)
882 {
883 char *errstr;
884
885 read_line(fd,NULL);
886 bhold=1;
887 strtol(linebuffer,&errstr,10);
888 while (*errstr=='\n' || *errstr=='\r') errstr++;
889 if (debug_da1)
890 rprintf("NUMCHK: %s==>%c\n",linebuffer,*errstr);
891 return (*errstr=='\0');
892 }
893
894 static rbool readrbool(genfile fd)
895 {
896 read_line(fd,"BOOL");
897 return (strncasecmp(linebuffer,"TRUE",4)==0);
898 }
899
900
901 static long readnum(genfile fd)
902 {
903 read_line(fd,"NUM ");
904 return strtol(linebuffer,NULL,10);
905 }
906
907
908 static void readptr(genfile fd,descr_ptr *desc)
909 {
910 read_line(fd,"PTR ");
911 desc->start=strtol(linebuffer,NULL,10);
912 read_line(fd,"LEN");
913 desc->size=strtol(linebuffer,NULL,10);
914 }
915
916
917 static void readjunk(genfile fd)
918 {
919 read_line(fd,"JUNK");
920 }
921
922 static void readtext(genfile fd,tline s)
923 {
924 read_line(fd,"TEXT");
925 strncpy((char*)s,linebuffer,80);
926 s[80]=0;
927 }
928
929 static long readfname(genfile fd)
930 {
931 read_line(fd,"FILE");
932 return new_str(linebuffer,0,0);
933 /* Copy filename string to static string space and return index */
934 }
935
936 static word readdict(genfile fd)
937 {
938 read_line(fd,"DICT");
939 return add_dict(linebuffer);
940 }
941
942
943 static slist readslist(genfile fd) /* Read in synonym list line */
944 {
945 slist start_ptr;
946 char nbuff[50];
947 int j,k;
948
949 start_ptr=synptr;
950 read_line(fd,"SYN ");
951 /* Need to see if it is none or * terminated. */
952 for(j=0;linebuffer[j]!=0 && linebuffer[j]!='*';j++);
953 linebuffer[j]=0;
954 k=0;
955 for(j=0;linebuffer[j]!=0;j++)
956 if (rspace(linebuffer[j]) && k>0)
957 {
958 nbuff[k]=0;
959 addsyn(add_dict(nbuff));
960 k=0;
961 }
962 else if (!rspace(linebuffer[j]))
963 nbuff[k++]=linebuffer[j];
964 if (k>0) {
965 nbuff[k]=0;
966 addsyn(add_dict(nbuff));
967 }
968 addsyn(-1);
969 return start_ptr;
970 }
971
972
973
974 /*-------------------------------------------------------------------------*/
975 /* Version analysis: Utilities to analyse the file format version and */
976 /* deduce sizes. */
977 /*-------------------------------------------------------------------------*/
978
979 static int soggy_test(fc_type fc)
980 {
981 genfile fda3;
982 long fsize;
983
984 if (DIAG) {
985 char *s;
986 s=formal_name(fc,fDA3);
987 rprintf("Testing %s for abnormal noun organization....",s);
988 rfree(s);
989 }
990 fda3=openbin(fc,fDA3,"Could not find room file '%s'.",1);
991 fsize=binsize(fda3);
992 readclose(fda3);
993
994 if (fsize % (maxnoun-300+1)!=0) {
995 if (DIAG) rprintf("FOUND!\n");
996 return 1;
997 }
998 if (fsize / (maxnoun-300+1) > 300) {
999 if (DIAG) rprintf("FOUND!\n");
1000 return 1;
1001 }
1002 if (DIAG) rprintf("nope.\n");
1003 return 0;
1004 }
1005
1006
1007 static void deduce_sizes(fc_type fc, rbool diag)
1008 /* If diag is true, we will also allocate space for
1009 noun inside information; this is used by agtout */
1010 {
1011 if (ver==0) {
1012 ver=1;
1013 if (maxroom>=200) ver=2;
1014 else if (maxnoun!=0)
1015 if (maxnoun<300)
1016 if (maxcreat!=0)
1017 if (maxcreat>=500) ver=4; /* SOGGY */
1018 else ver=1; /* Small */
1019 else if (aver==AGTCOS) ver=4; /* SOGGY */
1020 else ver=1; /* Small */
1021 else if (aver!=AGTCOS) ver=2; /* Large */
1022 else if (soggy_test(fc)) ver=4;
1023 else ver=2;
1024 else if (maxcreat!=0)
1025 if (maxcreat>=500)
1026 if (aver!=AGTCOS) ver=2; /* Large */
1027 else if (soggy_test(fc)) ver=4; /* Either large or SOGGY */
1028 else ver=2;
1029 else ver=1; /* Small */
1030 else
1031 agtwarn("No nouns or creatures: unable to determine version."
1032 "\nAssuming AGT Small",0);
1033 }
1034
1035 if (aver<AGTME15)
1036 MaxQuestion=25;
1037 else
1038 MaxQuestion=100; /* This is a guess. */
1039 if (aver==AGTCOS)
1040 MaxQuestion=10;
1041 if (aver==AGT15 || aver==AGT15F)
1042 MaxQuestion=41;
1043 first_room=2;
1044 if (ver==1)
1045 {
1046 first_noun=200;
1047 first_creat=300;
1048 last_obj=399;
1049 last_message=250;
1050 }
1051 else /* ver 2 or 3 or 4 */
1052 {
1053 if (ver!=4)
1054 first_noun=300;
1055 else first_noun=200;
1056 first_creat=500;
1057 last_obj=699;
1058 if (aver<=AGT12) last_message=500;
1059 else if (aver<AGTME155) last_message=600;
1060 else last_message=800;
1061 }
1062 if (aver==AGTCOS) {
1063 if (ver==4) last_obj=610;
1064 else last_obj=599;
1065 if (ver==4) last_message=810; /* Soggy case */
1066 else last_message=700;
1067 }
1068
1069 if (aver>=AGT18 && aver<=AGT18MAX) {
1070 bold_mode=1;
1071 build_fixchar();
1072 fixchar['\\']=FORMAT_CODE;
1073 }
1074
1075 if (aver<AGTME10)
1076 { SL_TEXT=81;SL_NAME=SL_WORD=23;SL_ROOM=31;}
1077 else
1078 { SL_TEXT=81;SL_NAME=SL_WORD=16;SL_ROOM=31;}
1079 if (aver==AGT15 || aver==AGT15F) SL_NAME=SL_WORD=16;
1080
1081 if (aver>=AGTME10) {
1082 MAX_USTR=25;
1083 MAX_SUB=15;
1084 }
1085 else MAX_SUB=MAX_USTR=0;
1086
1087 if (aver>=AGT15)
1088 NUM_ERR=185; /* Number of standard error messages */
1089 else
1090 NUM_ERR=0;
1091
1092 DVERB=50;
1093 FLAG_NUM=255;
1094 CNT_NUM=VAR_NUM=50;
1095 exitmsg_base=1000;
1096
1097 num_rflags=num_nflags=num_cflags=0;
1098 num_rprops=num_nprops=num_cprops=0;
1099 objflag=NULL; objprop=NULL;
1100 attrtable=NULL; proptable=NULL;
1101 oflag_cnt=0; oprop_cnt=0;
1102 propstr=NULL; propstr_size=0;
1103 vartable=NULL; flagtable=NULL;
1104
1105
1106
1107 /* Now to allocate space for all of the 'immortal' data structures */
1108 /* We do this all at once to avoid fragmentation; all of the following
1109 will be around for the life of the program (unless we restart) and so
1110 should be allocated first */
1111
1112 synlist=rmalloc(sizeof(slist)*TOTAL_VERB);
1113 comblist=NULL; /* The original AGT didn't support multi-word verbs */
1114 num_comb=0;
1115 userprep=NULL; /* ... nor did it allow user-defined prepostions */
1116 num_prep=0;
1117
1118 if (numglobal>0)
1119 globalnoun=rmalloc(numglobal*sizeof(word));
1120
1121 if (aver<AGTME15 && aver!=AGT10) {
1122 question=rmalloc(MaxQuestion*sizeof(tline));
1123 answer=rmalloc(MaxQuestion*sizeof(tline));
1124 }
1125 else if (aver>=AGTME15) {
1126 quest_ptr=rmalloc(MaxQuestion*sizeof(descr_ptr));
1127 ans_ptr=rmalloc(MaxQuestion*sizeof(descr_ptr));}
1128 msg_ptr=rmalloc((last_message)*sizeof(descr_ptr));
1129
1130 if (maxroom>=first_room) {
1131 room=rmalloc((maxroom-first_room+1)*sizeof(room_rec));
1132 room_ptr=rmalloc((maxroom-first_room+1)*sizeof(descr_ptr));
1133 help_ptr=rmalloc((maxroom-first_room+1)*sizeof(descr_ptr));
1134 special_ptr=rmalloc((maxroom-first_room+1)*sizeof(descr_ptr));
1135 if (diag) room_inside=rmalloc((maxroom-first_room+1)*sizeof(integer));
1136 }
1137
1138 if (maxnoun>=first_noun) {
1139 noun=rmalloc((maxnoun-first_noun+1)*sizeof(noun_rec));
1140 noun_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1141 push_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1142 pull_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1143 text_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1144 turn_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1145 play_ptr=rmalloc((maxnoun-first_noun+1)*sizeof(descr_ptr));
1146 if (diag) noun_inside=rmalloc((maxnoun-first_noun+1)*sizeof(integer));
1147 }
1148
1149 if (maxcreat>=first_creat) {
1150 creature=rmalloc((maxcreat-first_creat+1)*sizeof(creat_rec));
1151 creat_ptr=rmalloc((maxcreat-first_creat+1)*sizeof(descr_ptr));
1152 ask_ptr=rmalloc((maxcreat-first_creat+1)*sizeof(descr_ptr));
1153 talk_ptr=rmalloc((maxcreat-first_creat+1)*sizeof(descr_ptr));
1154 if (diag) creat_inside=rmalloc((maxcreat-first_creat+1)*sizeof(integer));
1155 }
1156
1157 if (aver>=AGTME10) {
1158 userstr=rmalloc(MAX_USTR*sizeof(tline));
1159 sub_name=rmalloc(MAX_SUB*sizeof(word));
1160 }
1161 command=rmalloc(sizeof(cmd_rec)*last_cmd);
1162
1163 if (aver>=AGT15)
1164 err_ptr=rmalloc(NUM_ERR*sizeof(descr_ptr));
1165
1166
1167 reinit_dict(); /* The dictionary grows dynamically so we want to
1168 allocate it AFTER we have allocated all the permenent
1169 things */
1170 }
1171
1172
1173
1174
1175 /*-------------------------------------------------------------------------*/
1176 /* Read DA1: The Info file; this is a text file containing a miscellany of */
1177 /* game information that wouldn't fit elsewhere */
1178 /*-------------------------------------------------------------------------*/
1179
1180 static int try_read_da1(fc_type fc,genfile fda1,rbool diag)
1181 /* Returns new aver value to try, or 0 on success. */
1182 /* diag determines if noun inside info will be read */
1183 /* VER values: 1=Small
1184 2=Large
1185 3=Master's Edition
1186 4="Soggy Large", with a larger last_room
1187 */
1188 /* AVER values: see agility.h for the current values */
1189 /* NOTE: This routine is allowed to set *ver*, but is not allowed to
1190 change *aver*; should it be neccessary to change *aver*, then the routine
1191 should return the new *aver* value.
1192 (The only exception to this is in the very beginning-- and that may get
1193 changed)
1194 [This is done to allow the user to force a version number]
1195 */
1196 {
1197 int i;
1198
1199 MAX_CMD_SIZE=30;
1200 maxpict=maxpix=maxfont=maxsong=0;
1201 linenum=0;bhold=0;
1202 game_sig=0;
1203 unexpected_eof=0;
1204
1205 if (aver==0 && isbool(fda1)) aver=AGTMAST;
1206 /* From this point on can assume ME detected */
1207
1208 freeze_mode=0; /* The default values */
1209 if (aver>=AGTME10) { /* 2 rbool */
1210 debug_mode=readrbool(fda1); /* DEBUG */
1211 if (aver>=AGTME15) {
1212 if (!isbool(fda1)) aver=AGTME10;
1213 else freeze_mode=readrbool(fda1); } /* FREEZE */
1214 ver=3;
1215 }
1216
1217 start_room=readnum(fda1);
1218 treas_room=readnum(fda1);
1219 if (aver!=AGT10) resurrect_room=readnum(fda1);
1220 else resurrect_room=start_room;
1221 if (aver>=AGTME10) { /* 4 int */
1222 score_mode=readnum(fda1); /* Score option */
1223 statusmode=readnum(fda1); /* Status option */
1224 start_time=readnum(fda1); /* Starting time */
1225 delta_time=readnum(fda1); /* Delta_time */
1226 } else
1227 {
1228 score_mode=statusmode=0;
1229 start_time=delta_time=0;
1230 }
1231 max_lives=readnum(fda1);
1232 if (aver!=AGT10) max_score=readnum(fda1);
1233 else max_score=0;
1234 maxroom=readnum(fda1);
1235 maxnoun=readnum(fda1);
1236 maxcreat=readnum(fda1);
1237 if (aver>=AGTME10) numglobal=readnum(fda1);/* # of global nouns? */
1238 else numglobal=0;
1239 last_cmd=readnum(fda1);
1240 readjunk(fda1); /* Number of items being carried */
1241 readjunk(fda1); /* Number of items being worn */
1242 if (isbool(fda1)) return AGT10; /* AGT v1.0 */
1243 /* From this point on, can assume AGT v1.0 is detected. */
1244 readptr(fda1,&intro_ptr);
1245
1246 deduce_sizes(fc,diag);
1247
1248 if (aver>=AGTME10) {
1249
1250 (void)readdict(fda1); /* ?!?! Not sure what this is */
1251
1252 report("Reading global and flag nouns",fda1);
1253
1254 for(i=0;i<MAX_FLAG_NOUN;i++)
1255 flag_noun[i]=readdict(fda1); /* Read in flag nouns; may be NONE */
1256 for(;i<32;i++)
1257 readjunk(fda1);
1258
1259 for(i=0;i<numglobal;i++)
1260 globalnoun[i]=readdict(fda1); /* Global nouns */
1261 } else if (aver>=AGT15) {
1262
1263 (void)readdict(fda1);
1264
1265 report("Reading flag nouns",fda1);
1266 for(i=0;i<MAX_FLAG_NOUN;i++)
1267 flag_noun[i]=readdict(fda1);
1268 } else
1269 for(i=0;i<MAX_FLAG_NOUN;i++)
1270 flag_noun[i]=0;
1271
1272 report("Reading questions and junk",fda1);
1273
1274 if (aver<AGTME15 && aver!=AGT10) {
1275 for(i=0;i<MaxQuestion;i++)
1276 {
1277 readtext(fda1,question[i]); /* Question[i]== question #(i+1) */
1278 chop_newline(question[i]);
1279 readtext(fda1,answer[i]);
1280 fix_answer(answer[i]);
1281 }
1282 } else if (aver>=AGTME15) {
1283 /* There are 400 lines of description pointers, meaning
1284 200 descriptions. I'm guessing they're all questions and
1285 answers, which means that there are 100 questions here. */
1286 for(i=0;i<MaxQuestion;i++)
1287 {
1288 readptr(fda1,&quest_ptr[i]);
1289 readptr(fda1,&ans_ptr[i]);
1290 }
1291 }
1292
1293 if (!isbool(fda1)) { /* Something is wrong... */
1294 if (aver==AGTMAST)
1295 return AGTME10;
1296 else if (aver!=AGTCOS && aver!=AGT15 && aver!=AGT15F) return AGTCOS;
1297 else return AGT15;
1298 }
1299 report("Reading have_meta",fda1);
1300 have_meta=readrbool(fda1);
1301
1302 if (have_meta) {
1303 for(i=0;i<=last_obj;i++) { /* i.e. iterate over all objects */
1304 readjunk(fda1);
1305 readjunk(fda1);
1306 }
1307 }
1308
1309 /* The Master's Edition apparently _always_ sets have_meta,
1310 even if there are no metacommands. The only way to determine
1311 if there are really metacommands is to check last_cmd */
1312 if (aver>=AGTME10 && last_cmd==0) have_meta=0;
1313
1314 report("Reading synonyms",fda1);
1315
1316 for(i=0;i<TOTAL_VERB;i++)
1317 synlist[i]=synptr; /* Is this correct? */
1318 addsyn(-1); /* Put an end-of-list marker in place */
1319
1320 for(i=0;i<56;i++)
1321 synlist[i]=readslist(fda1); /* May read <none> */
1322
1323 if (aver>=AGTME10) { /* Unknown verbs */
1324 synlist[56]=readslist(fda1); /* VIEW */
1325 synlist[57]=synlist[14]; /* AFTER */
1326 synlist[14]=readslist(fda1); /* THROW */
1327 }
1328
1329 if (aver==AGT183) {
1330 /* Eliminate LIST_EXITS and add INSTRUCTIONS */
1331 synlist[58]=synlist[52];
1332 /* Move 'REMOVE'-- the last thing before INS in 1.83 verblist --
1333 up to INSTRUCTIONS where it belongs */
1334 for(i=52;i>42;i--) /* i:=Remove to Brief (above List Exits) */
1335 synlist[i]=synlist[i-1];
1336 synlist[41]=synptr; /* LIST_EXITS, which doesn't exist in 1.83 and so
1337 can't have synonyms */
1338 addsyn(-1);
1339 }
1340
1341 report("Starting dummy verbs",fda1);
1342 for(i=0;i<25;i++) {
1343 if (i!=0 || aver<AGTME10)
1344 synlist[i+BASE_VERB]=readslist(fda1);
1345 synlist[i+BASE_VERB+25]=readslist(fda1);
1346 }
1347 if (aver>=AGTME10) {
1348 synlist[BASE_VERB]=readslist(fda1);
1349 for(i=0;i<15;i++) /* Subroutines */
1350 synlist[i+BASE_VERB+50]=readslist(fda1);
1351 }
1352 report("Reading DESC ptrs",fda1);
1353 if (aver>=AGT15)
1354 for(i=0;i<NUM_ERR;i++)
1355 readptr(fda1,&err_ptr[i]); /* Read in "standard" error messages. */
1356 else /* Otherwise need to initialize them to nothing */
1357 for(i=0;i<NUM_ERR;i++)
1358 { err_ptr[i].start=0;err_ptr[i].size=-1;}
1359
1360 report("Reading messages",fda1);
1361 if (DIAG) rprintf(" MSGS:1..%ld [%ld]\n",last_message,last_message);
1362 for(i=0;i<last_message;i++)
1363 readptr(fda1,&msg_ptr[i]);
1364
1365 report("Reading room descs",fda1);
1366 for(i=0;i<=maxroom-first_room;i++) {
1367 readptr(fda1,&room_ptr[i]);
1368 readptr(fda1,&help_ptr[i]);
1369 readptr(fda1,&special_ptr[i]);
1370 }
1371
1372 report("Reading noun descs",fda1);
1373 for(i=0;i<=maxnoun-first_noun;i++) {
1374 readptr(fda1,&noun_ptr[i]);
1375 readptr(fda1,&push_ptr[i]);
1376 readptr(fda1,&pull_ptr[i]);
1377 readptr(fda1,&text_ptr[i]);
1378 readptr(fda1,&turn_ptr[i]);
1379 readptr(fda1,&play_ptr[i]);
1380 }
1381
1382 report("Reading creatures",fda1);
1383 if (maxcreat>=first_creat) {
1384 for(i=0;i<=maxcreat-first_creat;i++) {
1385 readptr(fda1,&creat_ptr[i]);
1386 if (aver!=0 && aver<=AGTCLASS) {
1387 ask_ptr[i].start=talk_ptr[i].start=0;
1388 ask_ptr[i].size=talk_ptr[i].size=-1;
1389 }
1390 else{
1391 readptr(fda1,&talk_ptr[i]);
1392 readptr(fda1,&ask_ptr[i]);
1393 if (aver==0 && (talk_ptr[i].size==0 || ask_ptr[i].size==0 ||
1394 unexpected_eof)) return AGT135;
1395 }
1396 }
1397 }
1398 if (aver==AGT135 && unexpected_eof) return AGT12;
1399
1400 if (aver>=AGTME10)
1401 {
1402 if (aver>=AGTME155 && !isnum(fda1)) return AGTME15;
1403
1404 maxpict=rangefix(readnum(fda1)); /* Number of pictures */
1405 maxpix=rangefix(readnum(fda1)); /* Numper of PIXs */
1406 maxsong=rangefix(readnum(fda1)); /* Number of sounds */
1407 maxfont=rangefix(readnum(fda1)); /* Number of fonts. */
1408
1409 if (maxpix>MAX_PIX) {
1410 rprintf("Invalid MAXPIX value?!?\n");
1411 maxpix=MAX_PIX;
1412 }
1413
1414 t_pictlist=rmalloc(sizeof(long)*maxpict);
1415 t_pixlist=rmalloc(sizeof(long)*maxpix);
1416 t_songlist=rmalloc(sizeof(long)*maxsong);
1417 t_fontlist=rmalloc(sizeof(long)*maxfont);
1418
1419 for(i=0;i<maxpict;i++)
1420 t_pictlist[i]=readfname(fda1); /* picture file names */
1421 for(i=0;i<maxpix;i++)
1422 pix_name[i]=readdict(fda1); /* PIX names */
1423 for(i=0;i<maxpix;i++)
1424 t_pixlist[i]=readfname(fda1); /* PIX filenames */
1425 for(i=0;i<maxsong;i++)
1426 t_songlist[i]=readfname(fda1); /* Sound filenames */
1427 for(i=0;i<maxfont;i++)
1428 t_fontlist[i]=readfname(fda1); /* Font filenames */
1429
1430 for(i=0;i<MAX_USTR;i++)
1431 readtext(fda1,userstr[i]); /* This is just a guess-- should be
1432 tested. */
1433 } else {
1434 for(i=0;i<maxpix;i++) pix_name[i]=0;
1435 maxpict=maxpix=maxsong=maxfont=0;
1436 }
1437 if ((aver==AGT135 || aver==0) && isnum(fda1)) return AGT183;
1438 if (aver==AGT183) {
1439 long tval;
1440 tval=readnum(fda1); /* Needs to be translated */
1441 if (tval>=1 && tval<=4)
1442 statusmode=agt18_statmode[tval-1];
1443 else statusmode=0;
1444 tval=readnum(fda1); /* Hours */
1445 start_time=readnum(fda1); /* Minutes */
1446 tval+=start_time/60; start_time=(start_time%60)+100*tval;
1447 if (readrbool(fda1) && start_time<1200)
1448 start_time+=1200;
1449 milltime_mode=readrbool(fda1); /* Military time */
1450 delta_time=readnum(fda1);
1451 }
1452 if (DIAG) rprintf("Read in %d lines\n",linenum);
1453 return 0;
1454 }
1455
1456
1457
1458 static void set_da1_null(void)
1459 /* Set pointers that are malloc'd by try_read_da1 to NULL, to clear
1460 the way for free_da1_stuff to recover them */
1461 {
1462 static_str=NULL;ss_end=ss_size=0;
1463 command=NULL; cmd_ptr=NULL; synlist=NULL;
1464 userstr=NULL; sub_name=NULL; globalnoun=NULL; err_ptr=NULL;
1465 quest_ptr=ans_ptr=NULL;
1466 question=answer=NULL;
1467 msg_ptr=room_ptr=help_ptr=special_ptr=NULL;
1468 noun_ptr=push_ptr=pull_ptr=text_ptr=turn_ptr=play_ptr=NULL;
1469 room_inside=noun_inside=creat_inside=NULL;
1470 creat_ptr=ask_ptr=talk_ptr=NULL;
1471 pictlist=pixlist=fontlist=songlist=NULL;
1472 room=NULL;noun=NULL;creature=NULL;command=NULL;
1473 t_pictlist=t_pixlist=t_fontlist=t_songlist=NULL;
1474 }
1475
1476
1477
1478 static void free_da1_stuff(void)
1479 /* Free all data structures malloc'd by try_read_da1 */
1480 /* (This is neccessary since try_read_da1 may have to restart) */
1481 /* Note that if a pointer is NULL, rfree does nothing */
1482 /* Recall that rfree() is a macro that sets its argument to NULL */
1483 /* after freeing it */
1484 {
1485 rfree(static_str);ss_end=ss_size=0;
1486 rfree(userstr);rfree(sub_name);rfree(globalnoun);rfree(err_ptr);
1487 rfree(synlist);
1488 rfree(quest_ptr);rfree(ans_ptr);
1489 rfree(question);rfree(answer);
1490 rfree(msg_ptr);rfree(room_ptr);rfree(help_ptr);rfree(special_ptr);
1491 rfree(noun_ptr);rfree(push_ptr);rfree(pull_ptr);rfree(text_ptr);
1492 rfree(room_inside);rfree(noun_inside);rfree(creat_inside);
1493 rfree(turn_ptr);rfree(play_ptr);
1494 rfree(creat_ptr);rfree(ask_ptr);rfree(talk_ptr);
1495 rfree(t_pictlist);rfree(t_pixlist);rfree(t_songlist);rfree(t_fontlist);
1496 rfree(room);rfree(noun);rfree(creature);rfree(command);
1497 free_dict();
1498 }
1499
1500 static rbool read_da1(fc_type fc, rbool diag)
1501 /* diag is set by agtout to save extra diagnostic information */
1502 /* It has nothing to do with DIAG */
1503 {
1504 genfile fda1;
1505 int i;
1506
1507 ver=0;aver=0;
1508 top_quest=0; /* Highest question actually referenced; set by fixcmd */
1509 fda1=openfile(fc,fDA1,NULL,0);
1510 if (!filevalid(fda1,fDA1)) return 0;
1511
1512 if (DIAG) {
1513 char *s;
1514 s=formal_name(fc,fDA1);
1515 rprintf("Reading info file %s\n",s);
1516 rfree(s);
1517 }
1518 set_da1_null();
1519 while( (i=try_read_da1(fc,fda1,diag))!=0)
1520 {
1521 if (aver==i) {
1522 rprintf("[Recoginiton loop: AVER=%d]\n",aver);
1523 fatal("AGT version not recognized\n");
1524 }
1525 aver=i;
1526 /* fseek(fda1,0,SEEK_SET); Go back to beginning... */
1527 textrewind(fda1);
1528 if (DIAG)
1529 rprintf("...Found incompatibility; restarting, w/ AVER=%d\n",aver);
1530 free_da1_stuff();
1531 /* set_da1_null();*/
1532 ver=0;
1533 }
1534 if (aver==0) aver=AGTSTD; /* i.e. if we didn't notice any differences from
1535 standard format, it must be a standard file. */
1536 readclose(fda1);
1537 return 1; /* Success */
1538 }
1539
1540
1541
1542 /*-------------------------------------------------------------------------*/
1543 /* Miscellaneous routines to tie up loose ends and clean up afterwards. */
1544 /*-------------------------------------------------------------------------*/
1545
1546 static void finish_read(rbool cleanup)
1547 /* cleanup=0 means it will leave cmd_ptr, 1=it cleans up cmd_ptr */
1548 /* The only reason to set cleanup==0 is if we are writing a diagnostic
1549 program of some sort */
1550 {
1551 int i;
1552
1553 if (aver>=AGT18 && aver<=AGT18MAX) {
1554 intro_first=1;
1555 max_lives=1;
1556 TWO_CYCLE=1;
1557 PURE_AFTER=0;
1558 } else {
1559 intro_first=0;
1560 TWO_CYCLE=0;
1561 PURE_AFTER=1;
1562 }
1563
1564 min_ver=0; /* All original AGT games will run with any version of
1565 AGiliTy. */
1566
1567 if (aver>=AGTME10)
1568 PURE_ROOMTITLE=0;
1569
1570 if (aver>=AGT15)
1571 box_title=1;
1572 else box_title=0;
1573
1574 /* Compute max_score if it isn't already computed */
1575 if (max_score==0) {
1576 for(i=0;i<maxroom-first_room+1;i++)
1577 if (!room[i].unused) max_score+=room[i].points;
1578 for(i=0;i<maxnoun-first_noun+1;i++)
1579 if (!noun[i].unused) max_score+=noun[i].points;
1580 for(i=0;i<maxcreat-first_creat+1;i++)
1581 if (!creature[i].unused) max_score+=creature[i].points;
1582 }
1583
1584 if (cleanup) rfree(cmd_ptr);
1585 if (ss_end>0)
1586 static_str=rrealloc(static_str,sizeof(char)*ss_end);
1587
1588 /* Now convert string handles into honest pointers */
1589 for(i=0;i<=maxroom-first_room;i++)
1590 room[i].name=static_str+room_name[i];
1591 for(i=0;i<=maxnoun-first_noun;i++) {
1592 noun[i].shortdesc=static_str+noun_sdesc[i];
1593 noun[i].position=static_str+noun_pos[i];
1594 }
1595 for(i=0;i<=maxcreat-first_creat;i++)
1596 creature[i].shortdesc=static_str+creat_sdesc[i];
1597
1598 if (aver>=AGTME10) {
1599 pictlist=rmalloc(sizeof(filename)*maxpict);
1600 pixlist=rmalloc(sizeof(filename)*maxpix);
1601 songlist=rmalloc(sizeof(filename)*maxsong);
1602 fontlist=rmalloc(sizeof(filename)*maxfont);
1603
1604 for(i=0;i<maxpict;i++)
1605 pictlist[i]=static_str+t_pictlist[i];
1606 for(i=0;i<maxpix;i++)
1607 pixlist[i]=static_str+t_pixlist[i];
1608 for(i=0;i<maxsong;i++)
1609 songlist[i]=static_str+t_songlist[i];
1610 for(i=0;i<maxfont;i++)
1611 fontlist[i]=static_str+t_fontlist[i];
1612 }
1613
1614 /* Free the various temporary arrays */
1615 rfree(room_name);rfree(noun_sdesc);rfree(noun_pos);rfree(creat_sdesc);
1616 rfree(t_pictlist);rfree(t_pixlist);rfree(t_songlist);rfree(t_fontlist);
1617
1618 /* Reallocate questions and answers to only use the space that they need */
1619 if (!RAW_CMD_OUT && top_quest<MaxQuestion) {
1620 MaxQuestion=top_quest; /* top_quest is computed by fixcmd */
1621 if (top_quest==0)
1622 {rfree(question);rfree(answer);rfree(quest_ptr);rfree(ans_ptr);}
1623 else {
1624 if (question!=NULL)
1625 question=rrealloc(question,top_quest*sizeof(tline));
1626 if (answer!=NULL)
1627 answer=rrealloc(answer,top_quest*sizeof(tline));
1628 if (quest_ptr!=NULL)
1629 quest_ptr=rrealloc(quest_ptr,top_quest*sizeof(descr_ptr));
1630 if (ans_ptr!=NULL)
1631 ans_ptr=rrealloc(ans_ptr,top_quest*sizeof(descr_ptr));
1632 }
1633 }
1634
1635 /* Set the flag number for nouns mentioned in the flag_noun list */
1636 {
1637 int flagnum=1;
1638 for(i=0;i<MAX_FLAG_NOUN;i++) {
1639 if(flag_noun[i]!=0) {
1640 int j;
1641 for(j=0;j<=maxnoun-first_noun;j++) {
1642 if(noun[j].name==flag_noun[i])
1643 noun[j].flagnum=flagnum;
1644 }
1645 flagnum++;
1646 }
1647 }
1648 }
1649
1650 cmds_syns_canon();
1651 }
1652
1653 void free_all_agtread()
1654 {
1655 int i;
1656
1657 if (!agx_file)
1658 for(i=0;i<last_cmd;i++)
1659 rfree(command[i].data);
1660 free_da1_stuff();
1661 /* userstr, globalnoun, quest_ptr, ans_ptr, question, answer, msg_ptr,
1662 room_ptr, help_ptr, special_ptr, noun_ptr, push_ptr, pull_ptr,
1663 text_ptr, turn_ptr, play_ptr, creat_ptr, ask_ptr, talk_ptr,
1664 room_inside, noun_inside, creat_inside
1665 pictlist, pixlist, songlist, fontlist,
1666 room, noun, creature, command,
1667 dictionary data structures */
1668 }
1669
1670 rbool readagt(fc_type fc, rbool diag)
1671 /* If diag==1, then extra diagnostic information is preserved */
1672 {
1673 agx_file=0;
1674 mem_descr=NULL;
1675 build_fixchar();
1676 init_dict();
1677 if (!read_da1(fc,diag)) return 0; /* Couldn't open DA1 file */
1678 read_da2(fc);
1679 read_da3(fc);
1680 read_da4(fc);
1681 read_da5(fc);
1682 read_voc(fc);
1683 read_opt(fc);
1684 finish_read(!diag);
1685 return 1;
1686 }
1687