Debian Codegraph @

terps/agility/parser.c

raw symbols SCIP
1 /* parser.c-- the parser and the menu manager. */
2 /* Copyright (C) 1996-1999,2001 Robert Masenten */
3 /* This program may be redistributed under the terms of the
4 GNU General Public License, version 2; see agility.h for details. */
5 /* */
6 /* This is part of the source for AGiliTy: the (Mostly) */
7 /* Universal AGT Interpreter */
8
9 /*
10
11 This is probably the ugliest and least readable of all of the
12 source files. The parser isn't really that complex in principle,
13 but it has to deal with a lot of of special cases and also be
14 downward-compatible with the original parsers (which sometimes did
15 strange things) which adds a lot of additional code. The noun parsing
16 code is particularly convoluted.
17
18 Also, there are a fair number of global variables in this module
19 that are really local variables at heart but ended up global
20 because they were needed in too many different places and I didn't
21 want to complicate things even more by having to explicitly pass
22 them all up and down through the parsing routines. (One of those
23 times it would be convenient to have the ability to nest procedures
24 and functions like Pascal.)
25
26 */
27
28
29 #include <assert.h>
30 #include <stdio.h>
31
32 #include "agility.h"
33 #include "interp.h"
34
35
36 /* These store the previous turn's values for use in disambiguation and
37 implementing OOPS */
38 /* parse_ip saves the ip after parsing, ip_back saves it before. */
39 word input_back[MAXINPUT];
40 words in_text_back[MAXINPUT];
41 int ip_back, parse_ip;
42
43 /* The following are global only for rfree() purposes and
44 in order to maintain state for disambiguation */
45 static int vnum; /* Verb number from synonym scan */
46
47 /* Pointers to negative-terminated arrays of possible nouns */
48 static parse_rec *lactor=NULL, *lobj=NULL, *lnoun=NULL;
49
50 static int ambig_flag=0;
51 /* Was last input ambiguous? (so player could be entering
52 disambiguation info?) 1=ambig actor, 2=ambig noun,
53 3=ambig obj */
54
55
56 /* Empty ALL error messages, for those verbs that have their own */
57 int all_err_msg[]={73,83,113,103, /* open, close, lock, unlock: 15 - 18 */
58 239,239,239,239, /* 19 - 22 */
59 123,125}; /* eat, drink: 23 - 24 */
60
61
62
63 /*-------------------------------------------------------------------*/
64 /* DEBUGGING OUTPUT FUNCTIONS & MISC UTILITIES */
65 /*-------------------------------------------------------------------*/
66
67 /* This routine frees the space used by the parsing data structures */
68 static void freeall(void)
69 {
70 rfree(lnoun);rfree(lobj);
71 lnoun=lobj=NULL;
72 }
73
74
75 /* Print out parse error message and abort the parse */
76 static int parseerr(int msgid,char *s,int n)
77 {
78 if (n>=0)
79 gen_sysmsg(msgid,s,MSG_PARSE,in_text[n]);
80 else
81 gen_sysmsg(msgid,s,MSG_PARSE,"");
82 freeall();
83 ep=n;
84 ip=-1;
85 return -1;
86 }
87
88
89 /* Print out noun list; used for debugging the parser */
90 static void print_nlist(parse_rec *n)
91 {
92 char *s;
93 int c;
94 char buff[100];
95
96 if (n->info==D_END)
97 writestr("----");
98 if (n->info==D_ALL) {
99 writestr("ALL ");n++;}
100 for(c=0;n->info!=D_END && c<20;n++,c++)
101 if (n->info==D_AND) writestr(" AND ");
102 else if (n->info==D_NUM) { /* Number entered */
103 sprintf(buff,"#%ld(%d); ",n->num,n->obj);
104 writestr(buff);
105 } else if (n->obj<0) {
106 writestr(dict[-(n->obj)]);
107 sprintf(buff,"(%d); ",n->obj);
108 writestr(buff);
109 } else {
110 s=objname(n->obj);writestr(s);rfree(s);
111 sprintf(buff,"(%d) ['%s %s']; ",n->obj,dict[n->adj],dict[n->noun]);
112 writestr(buff);
113 }
114 if (n->info!=D_END) writestr("///");
115 writeln("");
116 }
117
118 /* Output the parser's analysis of the current input line; used
119 for debugging */
120 static int parse_out(parse_rec *lactor,int vb,parse_rec *lnoun,int prep,
121 parse_rec *lobj)
122 {
123 writeln("ANALYSIS:");
124 writestr("Actor: ");print_nlist(lactor);
125 writestr("Verb:");writeln(dict[ syntbl[auxsyn[vb]] ]);
126 writestr("DObj: ");print_nlist(lnoun);
127 writestr("Prep: ");
128 if (prep!=0) writeln(dict[prep]); else writeln("---");
129 writestr("IObj: ");print_nlist(lobj);
130 return 0;
131 }
132
133
134 static void save_input(void)
135 {
136 int i;
137
138 for(i=0;input[i]!=-1 && i<MAXINPUT;i++) {
139 input_back[i]=input[i];
140 strncpy(in_text_back[i],in_text[i],24);
141 }
142 input_back[i]=-1;
143 ip_back=ip;
144 }
145
146 static void restore_input(void)
147 {
148 int i;
149
150 for(i=0;input_back[i]!=-1 && i<MAXINPUT;i++) {
151 input[i]=input_back[i];
152 strncpy(in_text[i],in_text_back[i],24);
153 }
154 input[i]=-1;
155 ip=ip_back;
156 }
157
158
159
160 /*-------------------------------------------------------------------*/
161 /* Misc. Parsing Routines (includes parsing of verbs and preps) */
162 /*-------------------------------------------------------------------*/
163
164 #define w_and(w) (w==ext_code[wand] || w==ext_code[wc])
165 #define w_but(w) (w==ext_code[wbut] || w==ext_code[wexcept])
166 #define w_isterm(w) (w==ext_code[wp] || w==ext_code[wthen] || \
167 w==ext_code[wsc] || w_and(w) || w==-1)
168
169
170
171 static word check_comb(int combptr)
172 {
173 int k;
174 word w;
175
176 if (combptr==0) return 0;
177 w=syntbl[combptr];
178 for(combptr+=1, k=ip;syntbl[combptr]!=0;combptr++,k++)
179 if (syntbl[combptr]!=input[k]) break;
180 if (syntbl[combptr]==0) {
181 ip=k-1;
182 return w;
183 }
184 return 0;
185 }
186
187
188 static int comb_verb(void)
189 /* This eliminates two-word verbs */
190 {
191 int i;
192 word w;
193
194 for(i=0;i<num_comb;i++) {
195 w=check_comb(comblist[i]);
196 if (w!=0) return w;
197 }
198
199 if (input[ip]==ext_code[wgo] && verb_authorsyn(ext_code[wgo])==0) {
200 /* GO <dir> --> <dir> */
201 w=input[ip+1];
202 if (w!=0) i=verb_builtin(w);
203 else i=0;
204 if (i!=0) {
205 ip++;
206 return w;
207 }
208 }
209
210 for(i=0;i<num_auxcomb;i++) {
211 w=check_comb(auxcomb[i]);
212 if (w!=0) return w;
213 }
214
215 return input[ip];
216 }
217
218
219
220
221
222 /* This return true if the word in question is in the list
223 of original AGT verbs, but is not the canonical verb.
224 This is needed because verbs on this list
225 are not overridden by dummy_verbs in pre-Magx games. */
226 static rbool orig_agt_verb(word w)
227 {
228 int i;
229 if (aver<=AGT10 && w==ext_code[wg]) return 0; /* AGT 1.0 didn't have AGAIN */
230 /* SOGGY overrides SCREAM with a dummy_verb, earlier versions too? */
231 if (aver<=AGTCOS && w==ext_code[wscream]) return 0;
232 for(i=0;old_agt_verb[i]!=-1 && old_agt_verb[i]!=w;i++);
233 return(old_agt_verb[i]==w);
234 }
235
236 /* A few comments on id_verb:
237 The sequence is as follows:
238 i) Convert built-in synonyms to the base form (TAKE-->GET, for example)
239 _unless_ there is an author-defined synonym. (The original AGT
240 didn't have this 'unless'.)
241 ii) Check room-synonyms
242 iii) ID the verb based first on game-specific synonyms and then
243 falling back to the built-in ones.
244 (AGT gave the built-in ones higher priority than the game-specific
245 ones, but this causes problems and is generally a bad idea.)
246 */
247
248 static int id_verb(void)
249 /* Identify verb at ip=i ; return verb id if found, 0 otherwise */
250 {
251 word w;
252 int j, canon_word, tmp;
253
254 w=comb_verb(); /* Combine 2-word verbs */
255 if (w==0) return 0;
256
257 /* Pre-Canonization of w: see if w has any built-in synonyms */
258 canon_word=verb_builtin(w);
259 if (canon_word!=0) {
260 if (aver<AGX00 && orig_agt_verb(w))
261 /* In orig AGT, author-defined verbs don't override builtin syns */
262 tmp=0;
263 else
264 tmp=verb_authorsyn(w); /* Author-defined verbs override built-in ones */
265 if (tmp==0 || tmp==canon_word)
266 w=syntbl[auxsyn[canon_word]];
267 }
268
269 /* Now check room-specific synonyms (being the most localized,
270 they have the highest priority) */
271 for(j=room[loc].replacing_word;syntbl[j]!=0;j++)
272 if (w==syntbl[j])
273 w=room[loc].replace_word;
274
275 /* Next check to see if we already have the canonical form of the verb. */
276 /* and go through the built-in list of synonyms */
277 canon_word=verb_code(w);
278 if (!PURE_DUMMY && canon_word==57) canon_word=0; /* AFTER */
279 return canon_word;
280 }
281
282
283 #define compr_prep(w1,w2,r) {if (w1==input[ip] && w2==input[ip+1]) \
284 {ip+=2;return r;}}
285 #define cprep(c1,c2,r) compr_prep(ext_code[c1],ext_code[c2],ext_code[r])
286
287 /* Eventually should add support for the handful of two word preps */
288 /* (eg IN TO, OUT OF,...); otherwise, this is pretty trivial. */
289 static int parse_prep(void)
290 {
291 wtype i;
292 int j,k;
293
294 for(j=0;j<num_prep;j++) { /* Scan user-defined prepositions */
295 /* This table is formatted like the multi-verb table:
296 end-prep prep-word1 prepword2 etc. */
297 for(k=0;syntbl[userprep[j]+k+1]!=0;k++)
298 if (syntbl[userprep[j]+k+1]!=input[ip+k]) break;
299 if (syntbl[userprep[j]+k+1]==0) {
300 ip+=k;
301 return syntbl[userprep[j]];
302 }
303 }
304 cprep(win,wto,winto);
305 cprep(wout,wof,wfrom);
306 for(i=win;i<=wabout;i++)
307 if (ext_code[i]==input[ip]) return input[ip++];
308 return 0;
309 }
310
311
312
313 static int noun_syn(word w,int obj)
314 /* Is the word w a synonym for the object obj? */
315 /* obj is encoded as ususal. */
316 /* 0=no match, 1=adjective match, 2=synonym match, 3=noun match */
317 /* 2 will only occur if PURE_SYN is false */
318 {
319 int i;
320
321 if (w<=0) return 0;
322
323 if (obj>=first_noun && obj<=maxnoun)
324 {
325 obj=obj-first_noun;
326 if (w==noun[obj].name) return 3;
327 if (noun[obj].has_syns)
328 for(i=noun[obj].syns;syntbl[i]!=0;i++)
329 if (w==syntbl[i]) return (PURE_SYN ? 3 : 2);
330 if (w==noun[obj].adj) return 1;
331 return 0;
332 }
333 if (obj>=first_creat && obj<=maxcreat)
334 {
335 obj=obj-first_creat;
336 if (w==creature[obj].name) return 3;
337 if (creature[obj].has_syns)
338 for(i=creature[obj].syns;syntbl[i]!=0;i++)
339 if (w==syntbl[i]) return (PURE_SYN ? 3: 2);
340 if (w==creature[obj].adj) return 1;
341 return 0;
342 }
343 return 0; /* If the object doesn't exist, can't have synonyms */
344 }
345
346
347
348 /*-------------------------------------------------------------------*/
349 /* Noun-list manipulation functions. */
350 /*-------------------------------------------------------------------*/
351
352 static parse_rec *new_list(void)
353 {
354 parse_rec *list;
355
356 list=rmalloc(sizeof(parse_rec));
357 list[0].obj=0;
358 list[0].num=0;
359 list[0].adj=list[0].noun=0;
360 list[0].info=D_END;
361 return list;
362 }
363
364 static parse_rec *add_w_rec(parse_rec *pold,int obj0,long num0,int info0,
365 word adj0, word noun0)
366 {
367 parse_rec *pnew;
368 int n;
369
370 for(n=0;pold[n].info!=D_END;n++);
371 pnew=rrealloc(pold,(n+2)*sizeof(parse_rec));
372 pnew[n].obj=obj0;
373 pnew[n].num=num0;
374 pnew[n].info=info0;
375 pnew[n].adj=adj0;
376 pnew[n].noun=noun0;
377 pnew[n+1].obj=0;
378 pnew[n+1].info=D_END;
379 return pnew;
380 }
381
382 static parse_rec *add_rec(parse_rec *old,int obj0,long num0,int info0)
383 {
384 word w;
385
386 if (obj0<0) w=-obj0; /* The NOUN field of literal words will just be
387 that word */
388 else w=0;
389 return add_w_rec(old,obj0,num0,info0,0,w);
390 }
391
392
393 static parse_rec *kill_rec(parse_rec *old,int index)
394 /* Remove record old[index] */
395 {
396 parse_rec *pnew;
397 int i;
398
399 for(i=index;old[i].info!=D_END;i++) {
400 old[i].obj=old[i+1].obj;
401 old[i].num=old[i+1].num;
402 old[i].noun=old[i+1].noun;
403 old[i].adj=old[i+1].adj;
404 old[i].info=old[i+1].info;
405 old[i].score=old[i+1].score;
406 }
407 pnew=rrealloc(old,i*sizeof(parse_rec)); /* Shrink it by one */
408 return pnew;
409 }
410
411 static parse_rec *concat_list(parse_rec *dest,parse_rec *src)
412 {
413 int i,j;
414
415 for(i=0;dest[i].info!=D_END;i++); /* Put i at end of first list */
416 for(j=0;src[j].info!=D_END;j++); /* j marks end of the second list */
417 dest=rrealloc(dest,sizeof(parse_rec)*(i+j+1));
418 memcpy(dest+i,src,(j+1)*sizeof(parse_rec));
419 return dest;
420 }
421
422 static parse_rec *purge_list(parse_rec *list)
423 /* It should be possible to make this more efficiant */
424 {
425 int i;
426
427 for(i=0;list[i].info!=D_END;)
428 if ( (list[i].info & D_MARK) != 0)
429 list=kill_rec(list,i);
430 else i++; /* (Note: we only increment i if we _don't_ kill) */
431 return list;
432 }
433
434 static void clean_list(parse_rec *list)
435 {
436 for(;list->info!=D_END;list++)
437 list->info&=~D_MARK; /* Clear mark */
438 }
439
440
441 static parse_rec *copy_list(parse_rec *list)
442 {
443 parse_rec *cpy;
444 int i;
445
446 cpy=new_list();
447 for(i=0;list[i].info!=D_END;i++);
448 cpy=rmalloc(sizeof(parse_rec)*(i+1));
449 memcpy(cpy,list,(i+1)*sizeof(parse_rec));
450 return cpy;
451 }
452
453
454
455 /* Among other things, this is used to add disambiguation information */
456 static int scan_rec(int item,int num,parse_rec *list)
457 {
458 int i;
459
460 for(i=0;list[i].info!=D_END && list[i].info!=D_AND;i++)
461 if (list[i].obj==item && list[i].num==num)
462 return i;
463 return -1;
464 }
465
466 static rbool scan_andrec(int item,parse_rec *list)
467 {
468 for(;list->info!=D_END;list++)
469 if (list->obj==item && list->info!=D_AND
470 && list->info!=D_ALL) return 1;
471 return 0;
472 }
473
474 static rbool multinoun(parse_rec *list)
475 /* Determines if LIST refers to a multiple object */
476 {
477 if (list->info==D_ALL) return 1;
478 for(;list->info!=D_END;list++)
479 if (list->info==D_AND) return 1;
480 return 0;
481 }
482
483 /* This updates the adj and noun fields of a record and also
484 updates the disambiguation priority */
485 /* We are already either D_ADJ or D_SYN; this just handles
486 possible promotion */
487 static void add_words_to_rec(parse_rec *nrec, word w, int tmp)
488 {
489 word w2;
490
491 if (tmp==2 || tmp==3) {
492 w2=nrec->noun;
493 nrec->noun=w;
494 w=w2;
495 }
496 if (nrec->adj==0)
497 nrec->adj=w;
498 if (tmp==2) nrec->info=D_SYN;
499 else if (tmp==3) nrec->info=D_NOUN;
500 }
501
502 /* This updates nrec with the information in newobj; this routine
503 is called while adding disambiguation information; newobj is from
504 what the player just typed in to clarify the ambiguity */
505 void update_rec_words(parse_rec *nrec, parse_rec *newobj)
506 {
507 int tmp;
508
509 if (nrec->adj==0) nrec->adj=newobj->adj;
510 if (newobj->info==D_ADJ) tmp=1;
511 else if (newobj->info==D_SYN) tmp=2;
512 else if (newobj->info==D_NOUN) tmp=3;
513 else return;
514 add_words_to_rec(nrec,newobj->noun,tmp);
515 }
516
517
518 static rbool ident_objrec(parse_rec *p1, parse_rec *p2)
519 {
520 word noun1,adj1, noun2, adj2;
521
522 if (p1->obj==p2->obj) return 1;
523 if (p1->obj<=0 || p2->obj<=0) return 0;
524 if (tnoun(p1->obj)) {
525 noun1=noun[p1->obj-first_noun].name;
526 adj1=noun[p1->obj-first_noun].adj;
527 } else if (tcreat(p1->obj)) {
528 noun1=creature[p1->obj-first_creat].name;
529 adj1=creature[p1->obj-first_creat].adj;
530 } else return 0;
531 if (tnoun(p2->obj)) {
532 noun2=noun[p2->obj-first_noun].name;
533 adj2=noun[p2->obj-first_noun].adj;
534 } else if (tcreat(p2->obj)) {
535 noun2=creature[p2->obj-first_creat].name;
536 adj2=creature[p2->obj-first_creat].adj;
537 } else return 0;
538 return (noun1==noun2 && adj1==adj2);
539 }
540
541 /*-------------------------------------------------------------------*/
542 /* Disambiguation and ALL expansion routines */
543 /*-------------------------------------------------------------------*/
544
545 /* Eliminate non-creatures and non-present creatures from list. */
546 /* Very similar to disambig below, but simpler. */
547 static parse_rec *fix_actor(parse_rec *alist)
548 {
549 int i, cnt;
550
551 assert(alist!=NULL);
552 if (alist[0].info==D_ALL) { /* ALL?! */
553 rfree(alist);
554 return new_list();}
555
556 /* Start by eliminating non-creatures */
557 cnt=0;
558 for(i=0;alist[i].info!=D_END;i++)
559 if ( (alist[i].obj<first_creat || alist[i].obj>maxcreat)
560 && alist[i].obj!=-ext_code[weverybody]) {
561 if (alist[i].info!=D_AND)
562 alist[i].info|=D_MARK;
563 } else cnt++;
564 alist=purge_list(alist);
565 if (cnt<=1) return alist;
566
567 /* Now eliminate those not present */
568 cnt=0;
569 for(i=0;alist[i].info!=D_END;i++)
570 if (!genvisible(&alist[i])) {
571 if (alist[i].info!=D_AND)
572 alist[i].info|=D_MARK;
573 } else cnt++;
574
575 if (cnt==0) alist[0].info&=~D_MARK;
576
577 return purge_list(alist);
578 }
579
580
581 /* Convert disambig list to all list, moving things up. */
582 static parse_rec *convert_to_all(parse_rec *list, int *ofsref)
583 {
584 int i;
585 int cnt;
586
587 for(i=*ofsref;list[i].info!=D_AND && list[i].info!=D_END;i++);
588 cnt=i-*ofsref; /* Number of objects. We will add cnt-1 ANDs */
589
590 while(list[i].info!=D_END) i++;
591 list=rrealloc(list,(i+cnt)*sizeof(parse_rec));
592 memmove(list+*ofsref+2*cnt-1,list+*ofsref+cnt,
593 (i+1-cnt-*ofsref)*sizeof(parse_rec));
594 for(i=cnt-1;i>=0;i--) {
595 int k;
596 list[*ofsref+2*i]=list[*ofsref+i];
597 if (i==0) break;
598 k=*ofsref+2*i-1;
599 list[k].obj=0;
600 list[k].num=0;
601 list[k].adj=list[k].noun=0;
602 list[k].info=D_AND;
603 }
604 /* *ofsref+=2*cnt-1; */
605 return list;
606 }
607
608
609
610 static parse_rec *add_disambig_info(parse_rec *ilist, int ofs,
611 parse_rec *truenoun)
612 /* Basically, try to find interesection of tmp and truenoun,
613 or failing that, between ilist and truenoun */
614 /* truenoun is what the player just typed in to resolve
615 the ambiguity */
616 {
617 int i, n;
618
619 for(i=ofs;ilist[i].info!=D_AND && ilist[i].info!=D_END;i++) {
620 if (truenoun[0].info==D_EITHER) {
621 /* Mark all but the first for deletion */
622 if (i>ofs) ilist[i].info|=D_MARK;
623 } else {
624 n=scan_rec(ilist[i].obj,ilist[i].num,truenoun);
625 if (n==-1)
626 ilist[i].info|=D_MARK;
627 else /* Add any new information to the words */
628 update_rec_words(&ilist[i],&truenoun[n]);
629 }
630 }
631 ilist=purge_list(ilist);
632 truenoun[0].obj=0;
633 truenoun[0].num=0;
634 truenoun[0].info=D_END; /* Truenoun no longer useful */
635 return ilist;
636 }
637
638 static int max_disambig_score;
639
640 static int score_disambig(parse_rec *rec, int ambig_type)
641 /* This is just a wrapper for check_obj (defined in runverb.c) */
642 /* ambig_type=1 for actor, 2 for noun, 3 for object */
643 /* We can assume that the earlier bits have already been disambiguated */
644 /* Parse of current command in lactor, vnum, lnoun, prep, and lobj */
645 {
646 if (ambig_type==1) /* ACTOR */
647 return DISAMBIG_SUCC;
648 else if (ambig_type==2) /* NOUN */
649 return check_obj(lactor,vnum,rec,prep,NULL);
650 else if (ambig_type==3) /* IOBJ */
651 return check_obj(lactor,vnum,lnoun,prep,rec);
652 else fatal("Invalid ambig_type!");
653 return 0;
654 }
655
656
657 /* This routine does all expansion: it returns a list of ALL objects in the
658 current context (lactor, vnum, <ALL>, prep, lobj) */
659 /* lnoun is assumed to be a list of exceptions, which needs to be freed
660 at the end. (i.e. we want to expand to ALL EXCEPT <lnoun>) */
661 static parse_rec *expand_all(parse_rec *lnoun)
662 {
663 parse_rec *list;
664 int i, j;
665 rbool prev_obj; /* Is there a previous object on the list? */
666 rbool kill_obj; /* Don't put current object on ALL list after all */
667 parse_rec temp_obj; /* Used to pass object info to disambiguation routine */
668
669 if (debug_parse) {
670 writestr("ALL BUT:");
671 print_nlist(lnoun);
672 }
673 tmpobj(&temp_obj);
674 nounloop(i)
675 noun[i].scratch=0;
676 creatloop(i)
677 creature[i].scratch=0;
678 objloop(i)
679 if ( ( (verbflag[vnum]&VERB_GLOBAL)!=0 || visible(i) )
680 && (lnoun==NULL || !scan_andrec(i,lnoun))) {
681 temp_obj.obj=i;
682 if (score_disambig(&temp_obj,2)>=500) {
683 if (tnoun(i)) noun[i-first_noun].scratch=1;
684 else if (tcreat(i)) creature[i-first_creat].scratch=1;
685 else writeln("INTERNAL ERROR: Invalid object type in expand_all().");
686 }
687 }
688
689 /* The following ensures that if an object and it's container
690 are both selected, only the container will actually make it
691 onto the list.*/
692 list=new_list();
693 prev_obj=0;
694 objloop(i)
695 if (it_scratch(i)) {
696 kill_obj=0;
697 for(j=it_loc(i);tnoun(j)||tcreat(j);j=it_loc(j))
698 if (it_scratch(j)) {kill_obj=1;break;}
699 if (kill_obj) continue;
700 /* Now actually add object to list. */
701 if (prev_obj) list=add_rec(list,0,0,D_AND);
702 list=add_rec(list,i,0,D_SYN);
703 prev_obj=1;
704 }
705
706 if (debug_parse) {
707 writestr("ALL==>");
708 print_nlist(list);
709 }
710 rfree(lnoun);
711 return list;
712 }
713
714
715
716 /* disambig check checks for the various things that can eliminate a noun
717 from the list. The higher dlev is, the more things that are bad */
718 /* Returns 1 if we should keep it, 0 if we should kill it */
719 /* The elimination order is based on AGT:
720 0-Eliminate adjective matches if PURE_NOUN is set and
721 out-of-scope adjectives that are not at the head of the list.
722 1-Eliminate adj matches if the adjective is out-of-scope.
723 2-eliminate SCENE and DOOR (D_INTERN)
724 3-eliminate out-of-scope nouns that are not at the head of the list
725 4-eliminate out-of-scope nouns
726 5-eliminate numbers that don't have associated dictionary words
727 6-eliminate adj only matches (i.e. noun-free) [only if PURE_ADJ]
728 7-eliminate pronouns (D_PRO)
729 -eliminate ALL (D_ALL)
730 -eliminate numbers
731 (Never eliminated: FLAG,GLOBAL,PIX,SYN,NOUN)
732 */
733
734 #define MAX_DSCHEME 3
735 #define MAX_DLEV 2
736 /* ambig_type=1 for actor, 2 for noun, 3 for object */
737 /* We can assume that the earlier bits have already been disambiguated */
738 /* Parse of current command in lactor, vnum, lnoun, prep, and lobj */
739 /* pick_one is used to select the first noun in the case that all of
740 them get eliminated during visibility testing (if none of the nouns
741 are visible, we don't want to ask disambiguation questions) */
742
743 static rbool disambig_check(parse_rec *rec, int dsch, int dlev,
744 int ambig_type, rbool pick_one)
745 {
746 switch(dsch) {
747 case 0:
748 switch(dlev) { /* Syntactic checks: pre-scope */
749 case 0:return (!PURE_ADJ || rec->info!=D_ADJ);
750 case 1:return (rec->info!=D_INTERN); /* Elim SCENE and DOOR */
751 case 2: return (rec->info!=D_NUM || rec->obj!=0); /* Eliminate numbers w/o
752 corrosponding word matches */
753 default:return 0;
754 }
755 case 1:
756 switch(dlev) { /* Scope checks */
757 case 0:
758 /* Just compute the scores, but don't eliminate anything yet */
759 /* if PURE_DISAMBIG, no intel dismbig */
760 if (PURE_DISAMBIG || rec->info==D_NUM)
761 rec->score=DISAMBIG_SUCC;
762 else rec->score=score_disambig(rec,ambig_type);
763 if (rec->score>=max_disambig_score)
764 max_disambig_score=rec->score;
765 return 1;
766 case 1: return (rec->score==max_disambig_score);
767 case 2:
768 return (rec->info==D_NUM
769 || ( (verbflag[vnum]&VERB_GLOBAL)!=0 && rec->score>=500 )
770 || ( tnoun(rec->obj) && noun[rec->obj-first_noun].scope )
771 || ( tcreat(rec->obj) && creature[rec->obj-first_creat].scope ));
772 default:return 1;
773 }
774 case 2:
775 switch(dlev) {/* Syntax, take 2 */
776 case 0: /* Reserved for alternative adjective checking */
777 return 1;
778 /* Kill internal matches */
779 case 1:return (rec->info!=D_PRO && rec->info!=D_ALL &&
780 rec->info!=D_INTERN && rec->info!=D_NUM);
781 default: return 0;
782 }
783 case 3: return pick_one;
784 default: return 0;
785 }
786 }
787
788
789 /* disambig_a_noun does disambiguation for a single AND-terminated block */
790 /* *list* contains the list of possible objects, */
791 /* *ofs* is our starting offset within the list (since with ANDs we */
792 /* may not be starting at list[0]) */
793
794 static parse_rec *disambig_a_noun(parse_rec *list, int ofs, int ambig_type)
795 /* ambig_type=1 for actor, 2 for noun, 3 for object */
796 /* We can assume that the earlier bits have already been disambiguated */
797 /* Parse of current command in lactor, vnum, lnoun, prep, and lobj */
798 {
799 int i,cnt; /* cnt keeps track of how many nouns we've let through */
800 int dsch; /* Dismabiguation scheme; we run through these in turn,
801 pushing dlev as high as possible for each scheme */
802 int dlev; /* Disambiguation level: how picky do we want to be?
803 We keep raising this until we get a unique choice
804 or until we reach MAX_DLEV and have to give up */
805 rbool one_in_scope; /* True if at least one noun is visible; if no nouns
806 are visible then we never ask for disambiguation
807 from the player (to avoid giving things away) but
808 just take the first one. */
809
810 cnt=2; /* Arbitrary number > 1 */
811 one_in_scope=0;
812 max_disambig_score=-1000; /* Nothing built in returns anything lower than 0,
813 but some game author might come up with a
814 clever application of negative scores */
815 for(dsch=0;dsch<=MAX_DSCHEME;dsch++)
816 for(dlev=0;dlev<=MAX_DLEV;dlev++) {
817 if (DEBUG_DISAMBIG)
818 rprintf("\nDISAMBIG%c%d:%d: ",(dsch==1?'*':' '),dsch,dlev);
819 cnt=0;
820 for(i=ofs;list[i].info!=D_END && list[i].info!=D_AND;i++)
821 if ( disambig_check( &list[i], dsch, dlev, ambig_type,
822 one_in_scope || (i==ofs))
823 )
824 {
825 cnt++;
826 if (DEBUG_DISAMBIG)
827 rprintf("+%d ",list[i].obj);
828 } else {
829 if (DEBUG_DISAMBIG)
830 rprintf("-%d ",list[i].obj);
831 list[i].info|=D_MARK; /* Mark it for deletion */
832 }
833 if (cnt!=0) {
834 list=purge_list(list); /* Delete marked items */
835 if (cnt==1) return list;
836 if (dsch==1 && dlev==MAX_DLEV)
837 one_in_scope=1;
838 } else {
839 clean_list(list); /* Remove marks; we're not purging */
840 break;
841 }
842 }
843 /* Check to make sure we don't have a list of multiple identical items */
844 for(i=ofs;list[i].info!=D_END && list[i].info!=D_AND;i++) {
845 if (!ident_objrec(&list[i],&list[ofs])) break;
846 list[i].info|=D_MARK;
847 }
848 if (list[i].info==D_END || list[i].info==D_AND) {
849 /* If all of the items are identical, just pick the first */
850 if (one_in_scope) writeln("(Picking one at random)");
851 list[0].info&=~D_MARK;
852 list=purge_list(list);
853 } else clean_list(list);
854 if (DEBUG_DISAMBIG) rprintf("\n");
855 return list;
856 }
857
858
859
860 /* Note that this routine must be _restartable_, when new input comes in. */
861 /* Truenoun is 0 or else the "correct" noun for where disambig first
862 got stuck. */
863 /* Returns the offset at which it got stuck, or -1 if everything
864 went ok. Return -2 if we eliminate everything */
865 /* *tn_ofs* contains the offset where truenoun is supposed to be used */
866
867 #define list (*ilist) /* in disambig_phrase only */
868
869
870 static int disambig_phrase(parse_rec **ilist,parse_rec *truenoun,int tn_ofs,
871 int ambig_type)
872 /* Note that ilist is double dereferenced: this is so we can realloc it */
873 /* ambig_type=1 for actor, 2 for noun, 3 for object */
874 /* We can assume that the earlier bits have already been disambiguated */
875 {
876 int ofs, i;
877 char *s;
878
879 ofs=0;
880 if (list[0].info==D_END) return -1; /* No nouns, so no ambiguity */
881 if (list[0].info==D_ALL) ofs=1; /* might have ALL EXCEPT construction */
882 #ifdef OMEGA
883 return -1; /* No ambiguity over ALL, either */
884 /* (at least if it appears as the first element of the list) */
885 #endif
886
887 while(list[ofs].info!=D_END) { /* Go through each AND block */
888 if (ofs==tn_ofs) {
889 if (truenoun[0].info==D_ALL) /* Convert to ALL list */
890 list=convert_to_all(list,&ofs);
891 else {
892 list=add_disambig_info(list,ofs,truenoun);
893 if ( list[ofs].info==D_END) { /* We have eliminated all matches */
894 gen_sysmsg(240,"In that case, I don't know what you mean.",
895 MSG_PARSE,"");
896 return -2;
897 }
898 }
899 }
900 list=disambig_a_noun(list,ofs,ambig_type);
901 assert(list[ofs].info!=D_END && list[ofs].info!=D_AND);
902 if (list[ofs+1].info!=D_END && list[ofs+1].info!=D_AND)
903 /* Disambiguation failed */
904 { writestr("Do you mean");
905 for(i=ofs;list[i].info!=D_END && list[i].info!=D_AND;i++) {
906 if (list[i+1].info==D_END || list[i+1].info==D_AND)
907 if (i>ofs+1) writestr(", or");
908 else writestr(" or");
909 else if (i>ofs) writestr(",");
910 writestr(" the ");
911 if (list[i].info!=D_NUM || list[i].obj!=0)
912 s=objname(list[i].obj);
913 else { s=rmalloc(30*sizeof(char));
914 sprintf(s,"%ld",list[i].num);}
915 writestr(s);
916 rfree(s);
917 }
918 writeln("?");
919 return ofs;}
920 /* Skip forward to next AND */
921 while((*ilist)[ofs].info!=D_END && (*ilist)[ofs].info!=D_AND)
922 ofs++;
923 if ((*ilist)[ofs].info==D_AND) ofs++;
924 }
925 return -1;
926 }
927
928 #undef list
929
930 static int disambig_ofs; /* Offset where disambig failed */
931
932 /* ambig_flag stores what we were disambiguating the last time this
933 routine was called: it tells us where we failed so that if the
934 player enters new disambiguation information, we can figure out where
935 it should go */
936
937 static parse_rec *disambig(int ambig_set, parse_rec *list, parse_rec *truenoun)
938 /* ambig_set = 1 for actor, 2 for noun, 3 for object */
939 {
940 if (ambig_flag==ambig_set || ambig_flag==0) /* restart where we left off...*/
941 {
942 if (truenoun==NULL || truenoun[0].info==D_END) disambig_ofs=-1;
943 disambig_ofs=disambig_phrase(&list,truenoun,disambig_ofs,ambig_set);
944 if (disambig_ofs==-1) ambig_flag=0; /* Success */
945 else if (disambig_ofs==-2) ambig_flag=-1; /* Error: elim all choices */
946 else ambig_flag=ambig_set;
947 }
948 return list;
949 }
950
951
952
953
954 /*-------------------------------------------------------------------*/
955 /* Noun parsing routines */
956 /*-------------------------------------------------------------------*/
957
958
959 /* PARSE_A_NOUN(), parses a single noun, leaves ip pointing after it. */
960 /* Just be greedy: grab as many of the input words as possible */
961 /* Leave ip pointing _after_ last word we get. */
962 /* Return list of all possible objects */
963 /* Go to some difficullty to make sure "all the kings men" will
964 not be accidentally parsed as "all" + "the kings men" */
965 /* (Yes, yes, I know -- you can't have an AGT object with a name as
966 complex as 'all the king's men'-- but you could try to simulate it using
967 synonyms) */
968 /* May also want to use more intellegence along the adj--noun distinction */
969 /* all_ok indicates whether ALL is acceptable */
970
971 static parse_rec *parse_a_noun(void)
972 /* Returns a list of parse_rec's containing the various possible
973 nouns. */
974 {
975 parse_rec *nlist;
976 char *errptr;
977 int i, tmp, numval, num, oip;
978
979 nlist=new_list();
980 oip=ip; /* Save starting input pointer value */
981
982 if (input[ip]==-1) /* End of input */
983 return nlist;
984 if (input[ip]==0) { /* i.e. tokeniser threw up its hands */
985 numval=strtol(in_text[ip],&errptr,10); /* Is it a number? */
986 if (errptr==in_text[ip]) /* Nope. */
987 return nlist;
988 if (*errptr!=0) return nlist; /* May want to be less picky */
989 nlist=add_rec(nlist,0,numval,D_NUM);
990 ip++;
991 return nlist;
992 }
993
994 /* Basic strategy: try to match nouns. If all matches are of length<=1,
995 then go looking for flag nouns, global nouns, ALL, DOOR, etc. */
996 num=0;
997 objloop(i)
998 if ((tmp=noun_syn(input[ip],i))!=0) {
999 numval=strtol(in_text[ip],&errptr,10); /* Is it a number, too? */
1000 if (*errptr!=0) numval=0; /* Only accept perfectly formed numbers */
1001 nlist=add_w_rec(nlist,i,numval,
1002 (tmp==1) ? D_ADJ : (tmp==2 ? D_SYN : D_NOUN),
1003 (tmp==1) ? input[ip] : 0, /* Adjective */
1004 (tmp==1) ? 0 : input[ip]); /* Noun */
1005 num++;
1006 }
1007
1008 /* Now we need to try to match more words and reduce our list. */
1009 /* Basically, we keep advancing ip until we get no matches at all. */
1010 /* Note that nouns may only come at the end, so if we find one we know */
1011 /* we are done. Synonyms and adjectives can come anywhere */
1012 /* (If PURE_SYN is set, then we won't see any synonyms-- they */
1013 /* get converted into nouns by noun_syn() */
1014 /* *num* is used to keep track of how many matches we have so we know */
1015 /* when to stop. */
1016
1017 /* compare against the next word in the queue */
1018 while(num>0 && input[ip]!=-1 && ip<MAXINPUT) {
1019 ip++;
1020 if (input[ip]==-1) break;
1021 num=0;
1022 for(i=0;nlist[i].info!=D_END;i++) /* Which nouns match the new word? */
1023 if (nlist[i].info==D_NOUN) /* Nothing can come after a noun */
1024 nlist[i].info|=D_MARK;
1025 else if ( (tmp=noun_syn(input[ip],nlist[i].obj))==0)
1026 nlist[i].info|=D_MARK; /* Mark this noun to be eliminated */
1027 else { /* Noun _does_ match */
1028 num++; /* Count them up */
1029 add_words_to_rec(&nlist[i], input[ip], tmp);
1030 }
1031 /* If we had any matches, kill the nouns that didn't match */
1032 if (num!=0) {
1033 nlist=purge_list(nlist);
1034 for(i=0;nlist[i].info!=D_END;i++)
1035 nlist[i].num=0; /* Multi-word nouns can't be numbers */
1036 }
1037 else /* num==0; we need to clear all of the marks */
1038 clean_list(nlist);
1039 }
1040
1041 /* So at this point num is zero-- that is we have reached the limit
1042 of our matches. */
1043 /* If ip==oip is 0 (meaning no matches so far) or ip==oip+1
1044 (meaning we have a one-word match) then we need to check for
1045 flag nouns, global nouns, ALL, DOOR, pronouns, etc.
1046 and add them to the list if neccessary */
1047
1048 if (ip==oip || ip==oip+1) {
1049
1050 /* First match the built in things... */
1051 if ( (input[oip]==ext_code[wdoor] && aver<=AGX00)
1052 || input[oip]==ext_code[wscene])
1053 nlist=add_rec(nlist,-input[oip],0,D_INTERN);
1054 else if (input[oip]==ext_code[wall] ||
1055 input[oip]==ext_code[weverything])
1056 nlist=add_rec(nlist,ALL_MARK,0,D_ALL);
1057 else if (input[oip]==ext_code[whe] || input[oip]==ext_code[whim])
1058 nlist=add_rec(nlist,last_he,0,D_PRO);
1059 else if (input[oip]==ext_code[wshe] || input[oip]==ext_code[wher])
1060 nlist=add_rec(nlist,last_she,0,D_PRO);
1061 else if (input[oip]==ext_code[wit])
1062 nlist=add_rec(nlist,last_it,0,D_PRO);
1063 else if (input[oip]==ext_code[wthey] || input[oip]==ext_code[wthem])
1064 nlist=add_rec(nlist,last_they,0,D_PRO);
1065 else for (i=0;i<10;i++) /* Match direction names */
1066 if (input[oip]==syntbl[auxsyn[i]])
1067 nlist=add_rec(nlist,-syntbl[auxsyn[i]],0,D_INTERN);
1068 else if (input[oip]==ext_code[weveryone] ||
1069 input[oip]==ext_code[weverybody])
1070 nlist=add_rec(nlist,-ext_code[weverybody],0,D_INTERN);
1071
1072 /* Next look for number word matches */
1073 numval=strtol(in_text[oip],&errptr,10); /* Is it a number? */
1074 if (*errptr==0) /* Yes */
1075 nlist=add_rec(nlist,-input[oip],numval,D_NUM);
1076
1077 /* Next handle the flag nouns and global nouns */
1078 if (globalnoun!=NULL)
1079 for(i=0;i<numglobal;i++)
1080 if (input[oip]==globalnoun[i])
1081 nlist=add_rec(nlist,-input[oip],0,D_GLOBAL);
1082 for(i=0;i<MAX_FLAG_NOUN;i++)
1083 if (flag_noun[i]!=0 && input[oip]==flag_noun[i]
1084 #if 0
1085 && (room[loc].flag_noun_bits & (1L<<i))!=0
1086 #endif
1087 )
1088 nlist=add_rec(nlist,-input[oip],0,D_FLAG);
1089
1090 /* Finally the PIX names */
1091 for(i=0;i<MAX_PIX;i++)
1092 if (pix_name[i]!=0 && input[oip]==pix_name[i] &&
1093 (room[loc].PIX_bits & (1L<<i))!=0)
1094 nlist=add_rec(nlist,-input[oip],0,D_PIX);
1095
1096 if (nlist[0].info!=D_END) ip=oip+1;
1097 }
1098 return nlist;
1099 }
1100
1101 static parse_rec *parse_noun(int and_ok,int is_actor)
1102 /* handles multiple nouns */
1103 /* and_ok indicates wheter multiple objects are acceptable */
1104 /* Either_ok indicates whether EITHER is okay (only true when
1105 resolving disambiguation) */
1106 {
1107 parse_rec *next, *lnoun;
1108 int saveinfo;
1109 rbool all_except;
1110
1111 all_except=0;
1112 next=lnoun=parse_a_noun();
1113 saveinfo=next[0].info;
1114
1115 #if 0 /* Let the main parser sort this out */
1116 if (!and_ok) return lnoun; /* If no ANDs allowed, stop here. */
1117 #endif
1118 /* We need to explicitly handle the actor case here because
1119 the comma after an actor can be confused with the comma
1120 used to separate multiple objects */
1121 if (is_actor) return lnoun; /* If no ANDs allowed, stop here. */
1122
1123 if (lnoun[0].info==D_ALL && w_but(input[ip])) /* ALL EXCEPT ... */
1124 all_except=1; /* This will cause us to skip over EXCEPT and
1125 start creating an AND list */
1126
1127 /* Now, itereate over <noun> AND <noun> AND ... */
1128 while ( (all_except || w_and(input[ip])) &&
1129 saveinfo!=D_END) /* && saveinfo!=D_ALL */
1130 {
1131 ip++; /* Skip over AND or EXCEPT */
1132 next=parse_a_noun(); saveinfo=next[0].info;
1133 if (next[0].info!=D_END) { /* We found a word */
1134 if (!all_except) lnoun=add_rec(lnoun,AND_MARK,0,D_AND);
1135 lnoun=concat_list(lnoun,next);
1136 } else ip--; /* We hit trouble: back up to the AND */
1137 all_except=0; /* Only skip EXCEPT once */
1138 rfree(next);
1139 }
1140 return lnoun;
1141 }
1142
1143
1144 parse_rec *parse_disambig_answer(void)
1145 {
1146 parse_rec *temp;
1147
1148 if (input[ip+1]==-1) {
1149 if (input[ip]==ext_code[wall] || input[ip]==ext_code[weverything]
1150 || input[ip]==ext_code[wboth]) {
1151 temp=new_list();
1152 ip++;
1153 return add_rec(temp,ALL_MARK,0,D_ALL);
1154 }
1155 if (input[ip]==ext_code[weither] || input[ip]==ext_code[w_any]) {
1156 temp=new_list();
1157 ip++;
1158 return add_rec(temp,0,0,D_EITHER);
1159 }
1160 }
1161 return parse_noun(0,0);
1162 }
1163
1164
1165
1166 /*-------------------------------------------------------------------*/
1167 /* Main parsing routines */
1168 /*-------------------------------------------------------------------*/
1169
1170
1171 static int parse_cmd(void)
1172 /* Parse entered text and execute it, one statement at a time */
1173 /* Needs to leave ip pointing at beginning of next statement */
1174 {
1175 rbool new_actor; /* This is used for some error checking; it is set
1176 if we just found an actor on this command
1177 (as opposed to inheriting one from a previous
1178 command in a multiple statement) */
1179 parse_rec *tmp;
1180 int tp;
1181
1182 /* First go looking for an actor. */
1183 ap=ip;
1184 new_actor=0;
1185 if (lactor==NULL) {
1186 new_actor=1;
1187 lactor=parse_noun(0,1);
1188 /* Check that actor is a creature. */
1189 if (lactor[0].info!=D_END) {
1190 lactor=fix_actor(lactor); /* eliminate non-creatures */
1191 if (lactor[0].info==D_END) { /* Not a creature. */
1192 /* print intelligent error message */
1193 if (input[ip]==ext_code[wc]) /* ie there is a comma */
1194 return parseerr(229,"Who is this '$word$' you are addressing?",ap);
1195 else ip=ap; /* Otherwise, assume we shouldn't have parsed it as
1196 an actor-- it may be a verb. */
1197 }}
1198 if (lactor[0].info!=D_END && input[ip]==ext_code[wc])
1199 ip++; /* this skips over a comma after an actor. */
1200 }
1201 /* Now onwards... */
1202 vp=ip;
1203 vnum=id_verb(); /* May increment ip (ip will point at last word in verb) */
1204 if (vnum==0 && new_actor && lactor[0].info!=D_END)
1205 { /* maybe actor is messing us up. */
1206 ip=ap; /* restart from beginning */
1207 vnum=id_verb();
1208 if (vnum==0) /* if it's still bad, probably we really have an actor */
1209 ip=vp;
1210 else { /* no actor; really a verb */
1211 lactor[0].obj=0;
1212 lactor[0].info=D_END;
1213 vp=ap;
1214 }
1215 }
1216
1217 TELLHack: /* This is used to restart the noun/prep/object scan
1218 if we find a TELL <actor> TO <verb> ... command */
1219
1220 if (vnum==0)
1221 return parseerr(230,"I don't understand '$word$' as a verb.",ip);
1222
1223 /* Now we need to find noun, obj, and prep (if they all exist) */
1224 /* standard grammer: verb noun prep obj */
1225 prep=0;
1226 np=++ip; /* ip now points just _after_ verb */
1227 lnoun=parse_noun( (verbflag[vnum]&VERB_MULTI)!=0, 0);
1228 /* leaves ip pointing just after it.;
1229 lnoun[0].info==D_END means no noun. */
1230 if (prep==0) { /* prep==0 unless we've met the special TURN ON|OFF case */
1231 pp=ip;prep=parse_prep(); /* Should be trivial */
1232 op=ip;lobj=parse_noun(prep==0,0); /* and_ok if no prep */
1233 }
1234
1235 /* Check for TELL <actor> TO <verb> ... construction */
1236 /* If found, convert to <actor>, <verb> ... construction */
1237 if (lactor[0].info==D_END && lnoun[0].info!=D_END &&
1238 vnum==31 && prep==ext_code[wto] && !multinoun(lnoun))
1239 {
1240 ip=op; /* Back up */
1241 rfree(lactor);rfree(lobj);
1242 lactor=lnoun;lnoun=NULL;
1243 vp=ip; /* Replace TELL with new verb */
1244 vnum=id_verb(); /* May increment ip (ip points att last word in verb) */
1245 goto TELLHack; /* Go back up and reparse the sentence from
1246 the new start point. */
1247 }
1248
1249 /* Convert TURN <noun> ON to TURN ON <noun> */
1250 if ( vnum==35 && (prep==ext_code[won] || prep==ext_code[woff])
1251 && lobj[0].info==D_END )
1252 {
1253 tmp=lobj; lobj=lnoun; lnoun=tmp;
1254 tp=op; np=op; op=tp;
1255 }
1256
1257
1258 /* For pre-Magx versions of AGT,
1259 convert <verb> <prep> <noun> ==> <verb> <noun> <prep> <noun> */
1260 if (aver<AGX00 && lnoun[0].info==D_END && lobj[0].info!=D_END)
1261 {
1262 rfree(lnoun);
1263 lnoun=copy_list(lobj);
1264 np=op;
1265 }
1266
1267 /* Next we check to convert SHOOT <noun> AT <object> to
1268 SHOOT <object> WITH <noun> */
1269 if (vnum==49 && prep==ext_code[wat] && !multinoun(lnoun)) {
1270 tmp=lobj; lobj=lnoun; lnoun=tmp;
1271 tp=np;np=op;op=tp;
1272 prep=ext_code[wwith];
1273 }
1274
1275 /* Now to convert SHOW <*nothing*> to SHOW EXITS */
1276 if (vnum==40 && prep==0 && lnoun[0].info==D_END && lobj[0].info==D_END)
1277 vnum=42; /* LISTEXITS */
1278
1279 /* Convert LOOK <something> into EXAMINE <something> */
1280 if (smart_look && vnum==19 && lnoun[0].info!=D_END) vnum=20;
1281
1282 /* need better error msgs */
1283 if ( (verbflag[vnum]&VERB_MULTI)==0 && multinoun(lnoun) ) {
1284 /* Multiple objects when they are not allowed */
1285 int msgnum;
1286 if (vnum==31) msgnum=155; /* TALK */
1287 else if (vnum==34) msgnum=160; /* ASK */
1288 else msgnum=231;
1289 return parseerr(msgnum,
1290 "The verb '$word$' doesn't take multiple objects.",vp);
1291 } else if ( multinoun(lobj) )
1292 return parseerr(232,"You can't use multiple indirect objects.",op);
1293 else if (lnoun[0].info==D_END && !w_isterm(input[np]) && np!=pp)
1294 /* i.e. invalid noun */
1295 return parseerr(233,"I don't understand the word '$word$' as a noun.",np);
1296 else if (lnoun[0].obj==0 && lnoun[0].info==D_PRO)
1297 return parseerr(234,"I don't know to what '$word$' refers.",np);
1298 else if (lobj[0].info==D_END && !w_isterm(input[op]))
1299 /* i.e. invalid object */
1300 return parseerr(235,"I don't understand the word '$word$' as a noun.",op);
1301 else if (lobj[0].obj==0 && lobj[0].info==D_PRO)
1302 return parseerr(236,"I don't know to what '$word$' refers.",op);
1303 else if (!w_isterm(input[ip]))
1304 return parseerr(238,"Extra input found: \"$word$...\"",ip);
1305
1306 return 0;
1307 }
1308
1309
1310 static void v_undo(void)
1311 {
1312 if (undo_state==NULL) {
1313 writeln("There is insufficiant memory to support UNDO");
1314 ip=-1;
1315 return;
1316 }
1317 if (can_undo==0) {
1318 if (newlife_flag)
1319 writeln("You can't UNDO on the first turn.");
1320 else writeln("You can only UNDO one turn.");
1321 ip=-1;
1322 return;
1323 }
1324 writeln("");
1325 writeln("UNDOing a turn...");
1326 can_undo=0;
1327 putstate(undo_state);
1328 ip=1;
1329 set_statline();
1330 return;
1331 }
1332
1333 rbool parse(void)
1334 /* Wrapper around parse_cmd to handle disambiguation, etc */
1335 /* It returns 1 if everything is okay; 0 if there is ambiguity */
1336 {
1337 parse_rec *currnoun;
1338 int fixword;
1339 int start_ip;
1340
1341 currnoun=NULL;start_ip=ip;
1342 /* First, we need to see if someone has issued an OOPS command.
1343 OOPS commands are always assumed to stand alone. (i.e. no
1344 THEN nonsense). OOPS commands are always of the form
1345 OOPS <word> */
1346 if (ip==0 && input[0]==ext_code[woops] && input[1]>0 &&
1347 input[2]==-1 && ep>-1) {
1348 fixword=input[ip+1];
1349 restore_input();
1350 input[ep]=fixword;
1351 ambig_flag=0;
1352 }
1353 ep=-1;
1354
1355
1356 /* Next, we need to determine if someone is trying to do
1357 disambiguation. This is only the case if
1358 i)ambig_flag is set
1359 ii)ip=0 (no multiple command nonsense)
1360 iii)there is only one noun on the line. */
1361 if (ip!=0) ambig_flag=0;
1362 if (ambig_flag) {
1363 currnoun=parse_disambig_answer();
1364 if (input[ip]==-1 && currnoun[0].info!=D_END)
1365 {restore_input();ip=parse_ip;} /* Yep, we're disambiguaing. */
1366 else { /* nope; it's a new command */
1367 ip=0;
1368 ambig_flag=0;
1369 rfree(currnoun);
1370 freeall();
1371 currnoun=NULL;
1372 }
1373 }
1374
1375 /* Next we go looking for UNDO; again, this must be the first
1376 thing on an empty line. */
1377 if (ip==0 && input[0]==ext_code[wundo] && input[1]==-1)
1378 {
1379 v_undo();
1380 return 1;
1381 }
1382
1383 save_input();
1384
1385 /* If starting a new line, clear out old the old actor */
1386 if (ip==0) {
1387 actor_in_scope=0; /* Clear this */
1388 rfree(lactor); /* This resets lactor to NULL */
1389 }
1390
1391 if (!ambig_flag)
1392 if (parse_cmd()==-1)
1393 return 1; /* error condition */
1394
1395 parse_ip=ip;
1396
1397 if (debug_parse)
1398 parse_out(lactor,vnum,lnoun,prep,lobj);
1399
1400 /*Disambiguation routines; do it here instead of earlier to get
1401 error messages in the right place (it's silly and annoying to ask the
1402 player for disambiguation and then die on a parse error after they've
1403 provided it) */
1404 compute_scope(); /* The disambig routines use this information */
1405 lactor=disambig(1,lactor,currnoun);
1406 lnoun=disambig(2,lnoun,currnoun);
1407 lobj=disambig(3,lobj,currnoun);
1408 if (ambig_flag>0) return 0; /* We need to get disambig info */
1409 if (ambig_flag==-1) {ambig_flag=0;return 1;}
1410 /* We got rid of too much */
1411 rfree(currnoun);
1412
1413 /* Next, expand ALL if neccessary */
1414 if (!PURE_ALL && lnoun[0].info==D_ALL) {
1415 lnoun=expand_all(lnoun);
1416 if (lnoun[0].info==D_END) { /* ALL expands to nothing */
1417 int msgnum;
1418 if (vnum>=15 && vnum<=24)
1419 msgnum=all_err_msg[vnum-15];
1420 else
1421 msgnum=239;
1422 parseerr(msgnum,"I don't know what you are referring to.",np);
1423 return 1;
1424 }
1425 }
1426
1427 /* Now that we know that we have an executable command,
1428 we save the undo state before executing if this is the first command
1429 in a sequence. (That is, UNDO undoes whole lines of commands,
1430 not just individual commands) */
1431 if (start_ip==0 && undo_state!=NULL) {
1432 undo_state=getstate(undo_state);
1433 can_undo=1;
1434 }
1435
1436 /* Now to actually execute the command that has been parsed. */
1437 /* Remember: disambiguation has been done by this time. */
1438
1439 exec(&lactor[0],vnum,lnoun,prep,&lobj[0]);
1440 rfree(lobj);
1441
1442 /* exec is responsible for freeing or whatever lnoun (this is for AGAIN) */
1443
1444 /* Now we clear lnoun and lobj; lactor is handled elsewhere since
1445 we might have FRED, GET ROCK THEN GO NORTH */
1446 lnoun=lobj=NULL;
1447
1448 /* Finally check for THENs */
1449
1450 if (ip!=-1 && w_and(input[ip]) && input[ip+1]==ext_code[wthen])
1451 ip++; /* AND THEN construction */
1452 if (ip!=-1 && input[ip]!=-1) ip++;
1453 return 1;
1454 }
1455
1456
1457
1458
1459 void menu_cmd(void)
1460 {
1461 int i,j;
1462 int choice;
1463 char *curr_cmd, *tmp1, *tmp2; /* String of current command */
1464 int objcnt; /* Number of objects taken by the current verb */
1465 int verbword; /* Verb word */
1466 parse_rec actrec;
1467
1468 parse_rec mobj;
1469 int vnum; /* Verb number */
1470 word prep;
1471
1472 menuentry *nounmenu;
1473 int *nounval; /* Object id's for the menu entries */
1474 int nm_size, nm_width; /* Size and width of noun menu */
1475
1476
1477 nounval=NULL;
1478 nounmenu=NULL;
1479 /* Get verb+prep */
1480 choice=agt_menu("",vm_size,vm_width,verbmenu);
1481 if (choice==-1 || doing_restore) return;
1482
1483 verbword=verbinfo[choice].verb;
1484 prep=verbinfo[choice].prep;
1485 objcnt=verbinfo[choice].objnum;
1486
1487 /* Now identify the verb */
1488 ip=0;
1489 input[0]=verbword;input[1]=input[2]=-1;
1490 if (objcnt<=1 && prep!=0) input[1]=prep;
1491 vnum=id_verb();
1492
1493 lnoun=rmalloc(sizeof(parse_rec)*2);
1494 lnoun[0].obj=0;lnoun[0].num=0;lnoun[0].info=D_END;
1495
1496 nm_size=nm_width=0;
1497
1498 if (objcnt>=1) {
1499 /* Construct noun list */
1500 nounval=get_nouns();
1501 for(nm_size=0;nounval[nm_size]!=0;nm_size++);
1502 nounmenu=rmalloc(nm_size*sizeof(menuentry));
1503 nm_width=0;
1504 for(i=0;i<nm_size;i++) {
1505 tmp1=objname(nounval[i]);
1506 strncpy(nounmenu[i],tmp1,MENU_WIDTH);
1507 j=strlen(tmp1);
1508 if (j>nm_width) nm_width=j;
1509 }
1510 if (nm_width>MENU_WIDTH) nm_width=MENU_WIDTH;
1511
1512 if (objcnt>=2 || prep==0)
1513 curr_cmd=rstrdup(dict[verbword]);
1514 else
1515 curr_cmd=concdup(dict[verbword],dict[prep]);
1516
1517 choice=agt_menu(curr_cmd,nm_size,nm_width,nounmenu);
1518 rfree(curr_cmd);
1519 if (choice==-1 || doing_restore)
1520 {rfree(nounmenu);rfree(nounval);rfree(lnoun);return;}
1521
1522 if (objcnt==1 && prep!=0) { /* VERB PREP OBJ construction */
1523 mobj.obj=nounval[choice];
1524 mobj.num=0;
1525 mobj.info=D_NOUN;
1526 } else { /* Normal VERB OBJ construction */
1527 lnoun[0].obj=nounval[choice];
1528 lnoun[0].num=0;
1529 lnoun[0].info=D_NOUN;
1530 lnoun[1].obj=0;lnoun[1].num=0;lnoun[1].info=D_END;
1531 }
1532 }
1533
1534 if (objcnt>=2) {
1535 tmp1=objname(lnoun[0].obj); /* Build up current command line */
1536 tmp2=concdup(dict[verbword],tmp1); /* VERB NOUN */
1537 rfree(tmp1);
1538 curr_cmd=concdup(tmp2,dict[prep]); /* VERB NOUN PREP */
1539 rfree(tmp2);
1540
1541 choice=agt_menu(curr_cmd,nm_size,nm_width,nounmenu);
1542 rfree(curr_cmd);
1543 if (choice==-1 || doing_restore)
1544 {rfree(lnoun);rfree(nounmenu);rfree(nounval);return;}
1545
1546 mobj.obj=nounval[choice];
1547 mobj.num=0;
1548 mobj.info=D_NOUN;
1549 }
1550
1551 rfree(nounmenu);rfree(nounval);
1552
1553 if (vnum==OLD_VERB+3) { /* UNDO */
1554 v_undo();
1555 return;
1556 }
1557
1558 if (undo_state!=NULL) {
1559 undo_state=getstate(undo_state);
1560 can_undo=1;
1561 }
1562
1563 /* Now to actually execute the command that has been selected. */
1564 tmpobj(&actrec);
1565 actrec.obj=0;
1566 exec(&actrec,vnum,lnoun,prep,&mobj);
1567 lnoun=NULL; /* exec() is responsible for freeing lnoun */
1568 }
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579 /* Grammer structures:
1580 sverb, dverb (n,s,e,w,...,q,l,....)
1581 overb noun (close,examine,read,eat,drink,pull,light,ext)
1582 averb noun|ALL (drop,get,wear,remove)
1583 TURN noun ON|OFF
1584 TURN ON|OFF noun
1585 a-verb noun ABOUT obj (tell, ask)
1586 pverb noun [prep object] (put, throw)
1587 w-verb noun [WITH object] (attack, open, lock, unlock, push, shoot)
1588 SHOOT noun [AT object]
1589 dummy noun [prep obj]
1590 ( pverb obj noun ==> pverb noun TO obj e.g. give dog the bone)
1591 */