Debian Codegraph @

terps/agility/auxfile.c

raw symbols SCIP
1 /* auxfile.c-- Routines for reading and parsing the various secondary */
2 /* data files: OPT, VOC, TTL, INS, CFG */
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 both AGiliTy: the (Mostly) Universal */
8 /* AGT Interpreter and for the Magx adventure game compiler. */
9
10 #include <assert.h>
11 #include <ctype.h>
12 #include "agility.h"
13
14 /* ------------------------------------------------------------------- */
15 /* Purity flag initialization */
16 /* Logically, these belong in agtdata.c, but I wanted to keep them */
17 /* near the CFG reading routines. */
18 /* ------------------------------------------------------------------- */
19 /* The following are AGT 'purity' flags; they turn off features of */
20 /* my interpreter that are not fully consistent with the original AGT */
21 /* and so could break some games. Some of these are trivial improvements; */
22 /* some are more radical and should be used with caution. Several are */
23 /* only useful if a game was designed with them in mind. */
24 /* In all cases, setting the flag to 1 more closely follows the */
25 /* behavior of the original interpreters */
26 /* WARNING: Many of these haven't been tested extenstivly in the non-default
27 state. */
28
29
30 rbool PURE_ANSWER=0; /* For ME questions, requires that AND-separated
31 answers be in the same order in the player's
32 answer as they are in the game file. According
33 to the AGT documentation, AND should ignore
34 the order, but the original AGT interpreters
35 (at least the one I've tested) don't conform
36 to this. */
37
38 rbool PURE_TIME=1; /* Set to 0 causes time to always be increased
39 by delta_time rather than by a random amount
40 between 0 and delta_time. Only really of any use
41 to a game author who wanted to write a game
42 explicitly for AGiliTy. */
43
44 /* rbool PURE_BOLD=1; Set to 0 causes the backslash to toggle bold on and
45 off for all versions of AGT, not just 1.8x.
46 I can think of no reason to do this unless
47 you are an AGT author who wants to use the 1.8x
48 bold feature with the Master's Edition compiler. */
49
50 rbool PURE_AND=1; /* increment the turn counter for each noun in a
51 chain of <noun> AND <noun> AND ... If 0, the turn
52 counter will only be incremented by one in such a case.
53 (need to do something about metacommands, as well...) */
54
55 rbool PURE_METAVERB=1; /* If set, ANY and AFTER commands are run even
56 if you type in a metaverb (SAVE, RESTORE,...
57 that is, any verb that doesn't cause time to
58 pass). Verb specific metacommands are _always_
59 run. */
60
61 rbool PURE_ROOMTITLE=1; /* If 0, the interpreter will print out room
62 names before room descriptions even for
63 pre-ME games */
64
65 rbool PURE_SYN=0; /* Treats synonyms as nouns when parsing: that is, they
66 must show up only as the last word and they have the
67 same priority as noun matches during disambiguation.
68 If this is 0, then synonyms can appear anywhere in
69 the name the player types in but are still
70 disambiguated as nouns. */
71
72 rbool PURE_NOUN=0; /* _Requires_ a noun to end a word. This is only
73 imperfectly supported: if there are no other
74 possible matches the parser will take the adjective-
75 only one anyhow. Frankly, I can't think of any reason
76 to set this to 1, but it's included for completeness
77 sake (and for any AGT Purists out there :-) ) */
78
79 rbool PURE_ADJ=1; /* Picks noun/syn-matches over pure adj matches
80 when disambiguating. This is redundant if PURE_NOUN=1
81 since in that case pure adjective matches will
82 be rejected anyhow. */
83
84 rbool PURE_DUMMY=0; /* If set, the player can running dummy verbs
85 in the game by typing 'dummy_verb3'; otherwise,
86 this will produce an error message */
87
88 rbool PURE_SUBNAME=0; /* If set, the player can run subroutines from
89 the parse line by typing (e.g.) 'subroutine4'
90 (yes, the original AGT interpreters actually
91 allow this). If cleared, this cheat isn't
92 available */
93 rbool PURE_PROSUB=0; /* If clear, then $you$ substitutions are done
94 everywhere $$ substitutions are, even in
95 messages written by the game author.
96 If set, these substitutions are only made
97 in internal game messages */
98
99 rbool PURE_HOSTILE=1; /* =0 Will allow you to leave a room with a hostile
100 creature if you go back the way you came */
101 rbool PURE_ALL=1; /* =0 will cause the parser to expand ALL */
102 rbool PURE_DISAMBIG=1; /* =0 will cause intelligent disambiguation */
103 rbool PURE_GETHOSTILE=1; /* =0 will prevent the player from picking things
104 up in a room with a hostile creature */
105
106 rbool PURE_OBJ_DESC=1; /* =0 prevents [providing light] messages
107 from being shown */
108
109 rbool PURE_ERROR=0; /* =1 means no GAME ERROR messages will be printed
110 out */
111
112 rbool PURE_SIZE=1; /* =0 eliminates size/weight limits on how many
113 things the player can wear or carry. (But it's
114 still impossible to pick things up that are
115 in themselves larger than the player's capacity) */
116
117 rbool PURE_GRAMMAR=1; /* =0 prints error messages if the player uses a
118 built in verb with an extra object.
119 (e.g. YELL CHAIR). Otherwise, the extra object
120 will just be ignored. */
121
122 rbool PURE_SYSMSG=1; /* =0 causes AGiliTy to always use the default
123 messages even if the game file has its own
124 standard error messages. */
125
126 rbool PURE_AFTER=1; /* =0 causes LOOK and other end-of-turn events
127 to happen *before* AFTER commands run. */
128
129 rbool PURE_PROPER=1; /* Don't automatically treat creatures as proper nouns */
130
131 rbool TWO_CYCLE=0; /* AGT 1.83-style two-cycle metacommand execution. */
132 rbool FORCE_VERSION=0; /* Load even if the version is wrong. */
133
134
135 /*-------------------------------------------------------------------------*/
136 /* .CFG reading routines */
137 /*-------------------------------------------------------------------------*/
138
139 /* The main interpreter handles configuration in this order:
140 1) Global configuration file
141 2) First pass through game specific CFG to get the settings for
142 SLASH_BOLD and IBM_CHAR which we need to know _before_ reading
143 in the game.
144 3) Read in the game.
145 4) Main pass through game specific CFG. Doing it here ensures that
146 its settings will override those in the gamefile.
147 Secondary programs (such as agt2agx) usually only call this once, for
148 the game specific configuration file.
149 */
150
151 #define opt(s) (strcasecmp(optstr[0],s)==0)
152
153 static void cfg_option(int optnum,char *optstr[], rbool lastpass)
154 /* This is passed each of the options; it is responsible for parsing
155 them or passing them on to the platform-specific option handler
156 agt_option() */
157 /* lastpass is set if it is the last pass through this configuration
158 file; it is false only on the first pass through the game specific
159 configuration file during the run of the main interpreter */
160 {
161 rbool setflag;
162
163 if (optnum==0 || optstr[0]==NULL) return;
164
165 if (strncasecmp(optstr[0],"no_",3)==0) {
166 optstr[0]+=3;
167 setflag=0;
168 } else setflag=1;
169
170 if (opt("slash_bold")) bold_mode=setflag;
171 else if (!lastpass) {
172 /* On the first pass, we ignore all but a few options */
173 agil_option(optnum,optstr,setflag,lastpass);
174 return;
175 }
176 else if (opt("irun")) irun_mode=setflag;
177 else if (opt("block_hostile")) PURE_HOSTILE=setflag;
178 else if (opt("get_hostile")) PURE_GETHOSTILE=setflag;
179 else if (opt("debug")) {
180 if (!agx_file && aver<=AGTME10) debug_mode=setflag;
181 if (setflag==0) debug_mode=0; /* Can always turn debugging support off */
182 }
183 else if (opt("pure_answer")) PURE_ANSWER=setflag;
184 else if (opt("const_time")) PURE_TIME=!setflag;
185 else if (opt("fix_multinoun")) PURE_AND=!setflag;
186 else if (opt("fix_metaverb")) PURE_METAVERB=!setflag;
187 else if (opt("roomtitle")) PURE_ROOMTITLE=!setflag;
188 else if (opt("pure_synonym")) PURE_SYN=setflag;
189 else if (opt("adj_noun")) PURE_ADJ=!setflag;
190 else if (opt("pure_dummy")) PURE_DUMMY=setflag;
191 else if (opt("pure_subroutine")) PURE_SUBNAME=setflag;
192 else if (opt("pronoun_subs")) PURE_PROSUB=!setflag;
193 else if (opt("verbose")) verboseflag=setflag;
194 else if (opt("fixed_font")) font_status=1+!setflag;
195 else if (opt("alt_any")) mars_fix=setflag;
196 else if (opt("smart_disambig")) PURE_DISAMBIG=!setflag;
197 else if (opt("expand_all")) PURE_ALL=!setflag;
198 else if (opt("object_notes")) PURE_OBJ_DESC=setflag;
199 else if (opt("error")) PURE_ERROR=!setflag;
200 else if (opt("ignore_size")) PURE_SIZE=!setflag;
201 else if (opt("check_grammar")) PURE_GRAMMAR=!setflag;
202 else if (opt("default_errors")) PURE_SYSMSG=!setflag;
203 else if (opt("pure_after")) PURE_AFTER=!setflag;
204 else if (opt("proper_creature")) PURE_PROPER=!setflag;
205 else agil_option(optnum,optstr,setflag,lastpass);
206 }
207
208 #undef opt
209
210 /* Returns false if it there are too many tokens on the line */
211 rbool parse_config_line(char *buff, rbool lastpass)
212 {
213 char *opt[50], *p;
214 int optc;
215
216 optc=0;
217 opt[0]=NULL;
218 for(p=buff;*p;p++) {
219 if (isspace(*p)) { /* Whitespace */
220 if (opt[optc]!=NULL) {/*... which means this is the first whitespace */
221 if (optc==50) return 0; /* Too many */
222 opt[++optc]=NULL;
223 }
224 *p=0;
225 } else /* No whitespace */
226 if (opt[optc]==NULL) /* ...this is the first non-whitespace */
227 opt[optc]=p;
228 }
229 if (opt[optc]!=NULL) opt[++optc]=NULL;
230 cfg_option(optc,opt,lastpass);
231 return 1;
232 }
233
234
235 /* For the meaning of lastpass, see comments to cfg_option() above */
236 void read_config(genfile cfgfile, rbool lastpass)
237 {
238 char buff[100];
239
240 if (lastpass) {
241 /* Default to smart disambiguation for 1.5 onwards */
242 if (aver>=AGT15) PURE_DISAMBIG=0;
243 }
244
245 if (!filevalid(cfgfile,fCFG)) return;
246
247 while(readln(cfgfile,buff,99)) {
248 if (buff[0]=='#') continue; /* Comments */
249 /* Now we parse the line into words, with opt[] pointing at the words
250 and optc counting how many there are. */
251 if (!parse_config_line(buff,lastpass))
252 rprintf("Too many tokens on configuration line.\n");
253 }
254 readclose(cfgfile);
255 }
256
257
258
259 /*-------------------------------------------------------------------------*/
260 /* Read OPT file */
261 /* (most of these routines used to be in agil.c) */
262 /*-------------------------------------------------------------------------*/
263
264 /* .OPT reading routines */
265 /* I've put the comments on the format here because they don't really
266 belong anywhere else. (Maybe in agility.h, but I don't want to further
267 clutter that already quite cluttered file with something as peripheral
268 as this) */
269 /* OPT file format: the .OPT file consists of 14 bytes. They are:
270 0 Screen size(0=43/50 rows, 1=25 rows)
271 1 Status line(1=top, 0=none, -1=bottom)
272 2 Unknown, always seems to be 0
273 3 Put box around status line?
274 4 Sound on?
275 5 Menus on?
276 6 Fixed input line?
277 7 Print transcript?
278 8 Height of menus (3, 4, 5, 6, 7, or 8)
279 9 Unknown, always seems to be 0
280 10-13 Color scheme: output/status/input/menu, specified in DOS attribute
281 format (Bbbbffff, B=blink, b=backround, f=foreground,
282 MSB of foreground specifies intensity ("bold") ). */
283 /* The interpreter ignores almost all of this. */
284
285 void read_opt(fc_type fc)
286 {
287 char *errstr;
288 genfile optfile;
289
290 have_opt=0;
291 optfile=openbin(fc,fOPT,NULL,0);
292 if (filevalid(optfile,fOPT)) {
293 if (!binread(optfile,opt_data,14,1,&errstr))
294 fatal("Invalid OPT file.");
295 have_opt=1;
296 readclose(optfile);
297 }
298 }
299
300
301 /*-------------------------------------------------------------------------*/
302 /* Read and process TTL */
303 /* (most of these routines used to be in agil.c) */
304 /*-------------------------------------------------------------------------*/
305
306 /* Shades of Gray uses a custom interpreter that prints out the names
307 of the authors as the program loads. */
308 /* Normally I wouldn't bother with this, but Shades of Gray is probably
309 the best known of all AGT games */
310
311 #define SOGCREDIT 7
312 static const char *sogauthor[SOGCREDIT]={
313 "Mark \"Sam\" Baker",
314 "Steve \"Aaargh\" Bauman",
315 "Belisana \"The\" Magnificent",
316 "Mike \"of Locksley\" Laskey",
317 "Judith \"Teela Brown\" Pintar",
318 "Hercules \"The Loyal\" SysOp",
319 "Cindy \"Nearly Amelia\" Yans"
320 };
321
322 static rbool check_dollar(char *s)
323 /* Determines if s consists of an empty string with a single dollar sign
324 and possibly whitespace */
325 {
326 rbool dfound;
327 dfound=0;
328 for(;*s!=0;s++)
329 if (*s=='$' && !dfound) dfound=1;
330 else if (!rspace(*s)) return 0;
331 return dfound;
332 }
333
334 descr_line *read_ttl(fc_type fc)
335 {
336 genfile ttlfile;
337 int i, j, height;
338 descr_line *buff;
339
340 ttlfile=openfile(fc,fTTL,NULL,0);
341 /* "Warning: Could not open title file '%s'." */
342 if (!filevalid(ttlfile,fTTL)) return NULL;
343 build_fixchar();
344
345 buff=rmalloc(sizeof(descr_line));
346 i=0;
347 while( NULL!=(buff[i]=readln(ttlfile,NULL,0)) ) {
348 if (strncmp(buff[i],"END OF FILE",11)==0) break;
349 else if (aver>=AGT18 && aver<=AGT18MAX && check_dollar(buff[i]))
350 statusmode=4;
351 else {
352 for(j=0;buff[i][j]!=0;j++)
353 buff[i][j]=fixchar[(uchar)buff[i][j]];
354 /* Advance i and set the next pointer to NULL */
355 buff=rrealloc(buff,sizeof(descr_line)*(++i + 1));
356 buff[i]=NULL;
357 }
358 rfree(buff[i]);
359 }
360 readclose(ttlfile);
361
362 rfree(buff[i]);
363 while(buff[i]==NULL || strlen(buff[i])<=1) { /* Discard 'empty' lines */
364 if (i==0) break;
365 rfree(buff[i]);
366 i--;
367 }
368 height=i;
369
370 if (aver==AGTCOS && ver==4 && height>=17) /* SOGGY */
371 for(i=0;i<SOGCREDIT;i++)
372 if (strlen(sogauthor[i])+9+i<strlen(buff[i+7]))
373 memcpy(buff[i+7]+9+i,sogauthor[i],strlen(sogauthor[i]));
374
375 return buff;
376 }
377
378 void free_ttl(descr_line *title)
379 {
380 int i;
381 if (title==NULL) return;
382 for(i=0;title[i]!=NULL;i++)
383 rfree(title[i]);
384 rfree(title);
385 }
386
387
388 /*-------------------------------------------------------------------------*/
389 /* Read and convert VOC */
390 /* (most of these routines used to be in agil.c) */
391 /*-------------------------------------------------------------------------*/
392
393
394 static char *newvoc[]={"1 Menu","1 Restart","1 Undo"};
395 static int newindex=0; /* Points into newvoc */
396
397 void add_verbrec(char *verbline,rbool addnew)
398 {
399 char s[3],*p;
400
401 while(rspace(*verbline)) verbline++;
402 if (*verbline==0 || verbline[0]=='!') return; /* Comment or empty line */
403
404 /* The following guarentees automatic initialization of the
405 verbrec structures */
406 if (!addnew)
407 while (newindex<3 && strcasecmp(verbline+2,newvoc[newindex]+2)>0)
408 add_verbrec(newvoc[newindex++],1);
409
410 verbinfo=rrealloc(verbinfo,(vm_size+1)*sizeof(verbentry_rec));
411
412 s[0]=verbline[0];s[1]=0;
413 verbinfo[vm_size].objnum=strtol(s,NULL,10)-1;
414
415 verbline+=2; /* Skip over number */
416
417 verbinfo[vm_size].verb=verbinfo[vm_size].prep=0;
418 p=verbline;
419 if (*p!=0) {
420 while(*p && !rspace(*p)) p++;
421 if (*p!=0) {
422 *p=0;
423 p++;
424 }
425 verbinfo[vm_size].verb=search_dict(verbline);
426 if (verbinfo[vm_size].verb==-1) {
427 verbinfo[vm_size].verb=0;return;}
428 if (*p!=0) {
429 verbinfo[vm_size].prep=search_dict(p);
430 if (verbinfo[vm_size].prep==-1)
431 verbinfo[vm_size].prep=0;
432 }
433 }
434 vm_size++;
435 }
436
437 void init_verbrec(void)
438 /* Need to insert special verbs into verbinfo */
439 /* Fill in vnum field */
440 /* UNDO, RESTART, MENU */
441 {
442 verbinfo=NULL;
443 vm_size=0;
444 newindex=0;
445 if (freeze_mode) newindex=1; /* Don't include MENU option if we can't
446 use it. */
447 }
448
449 void finish_verbrec(void)
450 {
451 for(;newindex<3;newindex++) add_verbrec(newvoc[newindex],1);
452 }
453
454
455 void read_voc(fc_type fc)
456 {
457 char linbuf[80];
458 genfile vocfile;
459
460 init_verbrec();
461 vocfile=openfile(fc,fVOC,NULL,0);
462 if (filevalid(vocfile,fVOC)) { /* Vocabulary file exists */
463 while(readln(vocfile,linbuf,79))
464 add_verbrec(linbuf,0);
465 readclose(vocfile);
466 finish_verbrec();
467 }
468 }
469
470
471
472
473 /*-------------------------------------------------------------------------*/
474 /* Read INS file */
475 /* (most of these routines used to be in agil.c) */
476 /*-------------------------------------------------------------------------*/
477
478
479 static genfile insfile=BAD_TEXTFILE;
480 static char *ins_buff;
481
482 static descr_line *ins_descr=NULL;
483 static int ins_line; /* Current instruction line */
484
485
486 /* Return 1 on success, 0 on failure */
487 rbool open_ins_file(fc_type fc, rbool report_error)
488 {
489 ins_buff=NULL;
490 ins_line=0;
491
492 if (ins_descr!=NULL) return 1;
493
494 if (filevalid(insfile,fINS)) {
495 textrewind(insfile);
496 return 1;
497 }
498
499 if (agx_file) {
500 ins_descr=read_descr(ins_ptr.start,ins_ptr.size);
501 if (ins_descr!=NULL) return 1;
502
503 /* Note that if the AGX file doesn't contain an INS block, we
504 don't immediatly give up but try opening <fname>.INS */
505 }
506
507 insfile=openfile(fc, fINS,
508 report_error
509 ? "Sorry, Instructions aren't available for this game"
510 : NULL,
511 0);
512 return(filevalid(insfile,fINS));
513 }
514
515 char *read_ins_line(void)
516 {
517 if (ins_descr) {
518 if (ins_descr[ins_line]!=NULL)
519 return ins_descr[ins_line++];
520 else return NULL;
521 } else {
522 rfree(ins_buff);
523 ins_buff=readln(insfile,NULL,0);
524 return ins_buff;
525 }
526 }
527
528 void close_ins_file(void)
529 {
530 if (ins_descr) {
531 free_descr(ins_descr);
532 ins_descr=NULL;
533 } else if (filevalid(insfile,fINS)) {
534 rfree(ins_buff);
535 readclose(insfile);
536 insfile=BAD_TEXTFILE;
537 }
538 }
539
540
541
542 descr_line *read_ins(fc_type fc)
543 {
544 descr_line *txt;
545 char *buff;
546 int i;
547
548 i=0;
549 txt=NULL;
550 if (open_ins_file(fc,0)) { /* Instruction file exists */
551 while ( NULL!=(buff=read_ins_line()) ) {
552 /* Enlarge txt; we use (i+2) here to leave space for the trailing \0 */
553 txt=rrealloc(txt,sizeof(descr_ptr)*(i+2));
554 txt[i++]=rstrdup(buff);
555 }
556 if (txt!=NULL)
557 txt[i]=0; /* There is space for this since we used (i+2) above */
558 close_ins_file();
559 }
560 return txt;
561 }
562
563
564 void free_ins(descr_line *instr)
565 {
566 int i;
567 if (instr==NULL) return;
568 for(i=0;instr[i]!=NULL;i++)
569 rfree(instr[i]);
570 rfree(instr);
571 }
572
573
574
575 /* Character translation routines, used by agtread.c and read_ttl() */
576 void build_fixchar(void)
577 {
578 int i;
579 for(i=0;i<256;i++) {
580 if (i=='\r' || i=='\n') fixchar[i]=' ';
581 else if (i=='\\' && bold_mode) fixchar[i]=FORMAT_CODE;
582 else if (i>=0x80 && fix_ascii_flag)
583 fixchar[i]=trans_ibm[i & 0x7f];
584 else if (i==0) /* Fix color and blink codes */
585 fixchar[i]=FORMAT_CODE;
586 else fixchar[i]=i;
587 }
588 }
589
590
591