Debian Codegraph @

terps/agility/metacommand.c

raw symbols SCIP
1 /* metacommand.c-- handles metacommand execution */
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 the AGiliTy */
7
8 /* This contains the code for scanning and running metacommands.
9 Note that while the code here deals with all of the flow-of-execution
10 details, the code for executing individual tokens is actually
11 in token.c (With a few exceptions for commands that impact
12 the order of execution). */
13
14
15 #include <assert.h>
16 #include <ctype.h>
17 #include <stdio.h>
18
19 #include "agility.h"
20 #include "interp.h"
21 #include "exec.h"
22
23 #define DEBUG_SCAN 1
24
25 #define MAX_REDIR 50000L /* Maximum number of redirects, to catch
26 infinite loops. If this is 0, allow infinitely
27 many */
28
29 #define MAX_SUBCALL 2047 /* Maximum number of subroutine calls.
30 If this is 0, no limit (except for the
31 program's stack size). */
32
33
34 /*
35
36 scan_metacommand
37 -2=end of cycle, something happened (disambiguation only)
38 0=end of this cycle (disambig: end of cycle, nothing happened)
39 1=end of all commands (disambig: end of cycle, nothing happened)
40 2=end of turn (disambig: nothing happened)
41
42 run_metacommand
43 0 to go on to next metacommand,
44 1 to stop running metacommands, and
45 2 to end the turn.
46 3 indicates that redirection has just occured
47 4 indicates a subcall has just occured.
48 5 to go on to next metacommand after a return has occured.
49 -2 means we're doing disambiguation and just hit an action token.
50
51 */
52
53
54 /* ====================================================================*/
55 /* RUN METACOMMAND: The following are the routines used to execute */
56 /* a single metacommand block. run_metacommand is invoked by */
57 /* scan_metacommand, which is further down in this file. */
58 /* ====================================================================*/
59
60 /* ------------------------------------------------------------------- */
61 /* TYPE CHECKING ROUTINES */
62 /* Routines used to do type checking for metacommands. */
63 /* ------------------------------------------------------------------- */
64
65
66 rbool argvalid(int argtype,int arg)
67 {
68 if (argtype&AGT_VAR) { /* We have a variable */
69 /* First, verify that arg actually indexes a variable */
70 if (arg<0 || arg>VAR_NUM) return 0; /* Nope */
71
72 if (argtype==AGT_VAR) return 1; /* Pure variable; contents don't matter */
73
74 /* Next, verify its contents, using the rest of this routine */
75 arg=agt_var[arg];
76 argtype&=~AGT_VAR; /* Mask off AGT_VAR */
77 }
78
79 if (argtype<128) {
80 if (tnoun(arg)) return (argtype&AGT_ITEM)!=0;
81 if (troom(arg)) return (argtype&AGT_ROOM)!=0;
82 if (arg==0) return (argtype&AGT_NONE)!=0;
83 if (arg==1) return (argtype&AGT_SELF)!=0;
84 if (tcreat(arg)) return (argtype&AGT_CREAT)!=0;
85 if (arg==1000) return (argtype&AGT_WORN)!=0;
86 return 0;
87 }
88 else switch(argtype)
89 {
90 case AGT_NUM:return 1;
91 case AGT_DIR:return (arg>=1 && arg<=12);
92 case AGT_FLAG:return (arg>=0 && arg<=FLAG_NUM);
93 case AGT_CNT:return (arg>=0 && arg<=CNT_NUM);
94 case AGT_QUEST:return (arg>=1 && arg<=MaxQuestion);
95 case AGT_MSG:return (arg>=1 && arg<=last_message);
96 case AGT_ERR:return (arg>=1 && arg<=NUM_ERR);
97 case AGT_STR:return (arg>=1 && arg<=MAX_USTR);
98 case AGT_SUB:return (arg>=1 && arg<=MAX_SUB);
99 case AGT_PIC:return (arg>=1 && arg<=maxpict);
100 case AGT_PIX:return (arg>=1 && arg<=maxpix);
101 case AGT_FONT:return (arg>=1 && arg<=maxfont);
102 case AGT_SONG:return (arg>=1 && arg<=maxsong);
103 case AGT_ROOMFLAG:return (arg>=1 && arg<=32);
104 case AGT_EXIT: return ( argvalid(AGT_ROOM|AGT_NONE,arg)
105 || argvalid(AGT_MSG,arg-exitmsg_base)
106 || (arg<0 && aver>=AGX00) ); /* Treat as verb */
107 case AGT_OBJFLAG: return (arg>=0 && arg<oflag_cnt);
108 case AGT_OBJPROP: return(arg>=0 && arg<oprop_cnt);
109 case AGT_ATTR: /* ATTR and PROP are type-checked elsewhere */
110 case AGT_PROP:
111 return 1;
112 default:writeln("INTERNAL ERROR:Unrecognized type specifier.");
113 }
114 return 0;
115 }
116
117 /* <special> is set true for NOUN, OBJECT, NAME variables that are 0 */
118 /* (In this case, some error handling is suppressed) */
119
120 static rbool argfix(int argtype, int *arg, int optype, rbool *special)
121 {
122 *special=0;
123 switch(optype)
124 {
125 case 0:break; /* Direct: The easy case */
126 case 1: /* Variable */
127 if (*arg==-1) { /* Top-of-stack */
128 *arg=pop_expr_stack();
129 break;
130 }
131 if (!argvalid(AGT_VAR,*arg)) return 0;
132 *arg=(int)agt_var[*arg];
133 break;
134 case 2: *arg=dobj; *special=(dobj==0); break; /* NOUN */
135 case 3: *arg=iobj; *special=(iobj==0); break; /* OBJECT */
136 default: rprintf("Internal error: Invalid optype.");
137 return 0;
138 }
139 if (!(optype&2)) {
140 /* i.e. we have direct or variable type */
141 /* The noun and object types below are useless for direct use,
142 but may be useful when used as values of variables. */
143 if (argtype<64) {
144 if (*arg==-1) /* NAME */
145 {*arg=actor; *special=(actor==0);}
146 else if (*arg==-2) /* NOUN */
147 {*arg=dobj; *special=(dobj==0);}
148 else if (*arg==-3) /* OBJECT */
149 {*arg=iobj; *special=(iobj==0);}
150 }
151 }
152 return argvalid(argtype,*arg);
153 }
154
155
156 /* These are handled in the order ARG2 then ARG1 so that
157 top-of-stack references will pop the stack in that order
158 (so that the push-order will corrospond to the argument order) */
159 /* <grammer_arg> is true if "bad" argument is NOUN/OBJECT/etc. and
160 is 0. */
161 static int argok(const opdef *opdata,int *arg1,int *arg2, int optype,
162 rbool *grammer_arg)
163 {
164 if ( (opdata->argnum)>1 && !argfix(opdata->arg2,arg2,optype%4,grammer_arg) )
165 return 0;
166 if ( (opdata->argnum)>0 && !argfix(opdata->arg1,arg1,optype/4,grammer_arg) )
167 return 0;
168 return 1;
169 }
170
171 /* ------------------------------------------------------------------- */
172 /* INSTRUCTION DECODING ROUTINES */
173 /* Routines for decoding opcodes and their arguments */
174 /* ------------------------------------------------------------------- */
175
176 static int decode_instr(op_rec *oprec, const integer *data, int maxleng)
177 {
178 integer op;
179 int optype;
180 int leng;
181 rbool special_arg1; /* Is the first argument a special 0-length argument? */
182
183 oprec->negate=oprec->failmsg=oprec->disambig=0;
184 oprec->errmsg=NULL;
185 oprec->op=-1;
186 oprec->opdata=&illegal_def;
187 oprec->argcnt=0;
188 oprec->endor=1;
189
190 special_arg1=0;
191
192 if (maxleng<=0) {
193 oprec->errmsg="GAME ERROR: Unexpected end of token sequence.";
194 return 1;
195 }
196 op=data[0];
197 if (op<0) {
198 oprec->errmsg="GAME ERROR: Negative token found.";
199 return 1;
200 }
201 oprec->optype=optype=op/2048; /* Split op into operand proper and optype */
202 oprec->op=op=op%2048;
203 oprec->opdata=get_opdef(op);
204
205 if (oprec->opdata==&illegal_def) {
206 if (op<START_ACT)
207 oprec->errmsg="GAME ERROR: Illegal condition token encountered.";
208 else
209 oprec->errmsg="GAME ERROR: Illegal action token encountered.";
210 return 1;
211 }
212
213 if (op<1000) oprec->endor=0; /* Conditional tokens don't end OR block */
214
215 /* Recall that oprec->disambig is initialized to 0 */
216 switch(op)
217 {
218 case 89: case 95: case 96: case 97:
219 oprec->disambig=1; break; /* YesNo and Chance */
220 case WIN_ACT: case WIN_ACT+1:
221 oprec->disambig=1; break; /* WinGame, EndGame */
222
223 case 1037: case 1038: /* DoSubroutine, Return */
224 case 1062: case 1115: /* RedirectTo, SetDisambigPriority */
225 case 1132: /* AND */
226 case 1149: case 1150: /* Goto and OnFailGoto */
227 case 1151: /* EndDisambig */
228 case 1152: /* XRedirect */
229 break; /* Accept default of 0: these tokens don' trigger disambig */
230
231 case 1135: case 1137: case 1138: case 1139: case 1140:
232 case 1141: case 1142: case 1143: case 1147: case 1159:
233 oprec->endor=0;
234 break; /* Operations that only affect the stack don't
235 stop disambiguation, either. They also
236 don't mark the end of an OR block */
237
238 default:
239 /* Aside from the above exceptions, all actions will stop
240 disambiguation (with success) and all conditions will let it
241 continue. */
242 oprec->disambig=(op>=START_ACT && op<WIN_ACT);
243 }
244
245 if (op>=1128 && op<=1131) /* FailMessage group */
246 oprec->failmsg=1;
247
248 leng=oprec->opdata->argnum+1;
249 if (optype!=0) { /* Correct leng for NOUN and OBJECT args */
250 special_arg1= ((optype&8)==8);
251 leng-= special_arg1 + ((optype&2)==2);
252 if (leng<1) {
253 oprec->errmsg="GAME ERROR: Token list corrupted.";
254 return 1;
255 }
256 }
257 if (leng>maxleng) {
258 oprec->errmsg="GAME ERROR: Unexpected end of token sequence";
259 return 1;
260 }
261
262 if (op==108) { /* NOT */
263 leng=1+decode_instr(oprec,data+1,maxleng-1);
264 oprec->negate=!oprec->negate;
265 return leng;
266 }
267 oprec->argcnt=leng-1;
268 oprec->arg1=oprec->arg2=0;
269 if (leng>=2) {
270 if (special_arg1) {
271 assert(leng==2);
272 oprec->arg2=data[1];
273 oprec->arg1=0;
274 } else oprec->arg1=data[1];
275 }
276 if (leng>=3) oprec->arg2=data[2];
277 if (leng>=4) writeln("INTERNAL ERROR: Too many token arguments.");
278 return leng;
279 }
280
281
282 /* decode_args checks and decodes the arguments to metacommand tokens */
283 /* Returns false on an error */
284 static rbool decode_args(int ip,op_rec *oprec)
285 {
286 rbool grammer_arg; /* Have NOUN/OBJECT that is 0 and so failed argok tests */
287
288 if (oprec->errmsg!=NULL) {
289 if (!PURE_ERROR)
290 writeln(oprec->errmsg);
291 return 0;
292 }
293 if (DEBUG_AGT_CMD && !supress_debug) {
294 if (oprec->negate) { /* Output NOT */
295 debug_cmd_out(ip,108,0,0,0);
296 ip++;
297 }
298 }
299
300 if (DEBUG_AGT_CMD && !supress_debug)
301 debug_cmd_out(ip,oprec->op,oprec->arg1,oprec->arg2,oprec->optype);
302
303 /* This checks and translates the arguments */
304 if (!argok(oprec->opdata,&(oprec->arg1),&(oprec->arg2),
305 oprec->optype,&grammer_arg)) {
306 /* Don't report errors for null NOUN/OBJECT/ACTOR arguments
307 used in conditional tokens */
308 if (grammer_arg && oprec->op<=MAX_COND)
309 return 0;
310 if (!PURE_ERROR) {
311 if (DEBUG_AGT_CMD && !supress_debug) debugout("\n");
312 writeln("GAME ERROR: Invalid argument to metacommand token.");
313 }
314 return 0;
315 }
316 return 1;
317 }
318
319
320
321
322 /* ------------------------------------------------------------------- */
323 /* Subroutine Call Stack routines */
324 /* ------------------------------------------------------------------- */
325 /* Note: run_metacommand() passes subroutine calls up to it's parent,
326 but it processes Returns on its own (and is the routine responsible
327 for maintaining the subcall stack-- scan_metacommand treats
328 a subroutine call just like RedirecTo) */
329 /* The progression for subroutine calls goes like this:
330 run_metacommand hits a DoSubroutine token;
331 the subroutine id is saved in subcall_arg by exec_token.
332 run_metacommand does push_subcall, saving cnum and ip,
333 and then returns 4 to scan_metacommand.
334 scan_metacommand saves grammar state to the new stack entry
335 with push_subcall and then starts scanning SUBROUTINEnn
336
337 Many tokens are executed.
338
339 run_metacommand hits Return. It sets restart_state and
340 returns 5 to its parent.
341 scan_metacommand then runs pop_subcall_grammar and restores
342 the original scanning grammer. It subtracts one from cnum
343 so the original cnum will be rerun.
344 run_metacommand sees that restart_state is set and pops the
345 rest of the information (cnum and ip) off of the stack.
346 Things continue as usual.
347 */
348
349
350
351 typedef struct {
352 /* run_metacommand state */
353 short cnum, ip, failaddr;
354 /* scan_metacommand state */
355 integer mactor, mdobj, miobj;
356 word mprep;
357 short vcode;
358 /* Global state (is this really saved?) */
359 short vb;
360 word prep;
361 } subcall_rec;
362
363
364 static subcall_rec *substack=NULL;
365 static short subcnt=0;
366 static short subsize=0;
367
368
369 static rbool push_subcall(int cnum, int ip, int failaddr)
370 {
371 subcall_rec *savestack; /* In case something goes wrong. */
372
373 if (MAX_SUBCALL!=0 && ++subcnt>MAX_SUBCALL)
374 return 0;
375 if (subcnt>subsize) {
376 subsize+=5;
377 savestack=substack;
378 rm_trap=0;
379 substack=rrealloc(substack,subsize*sizeof(subcall_rec));
380 rm_trap=1;
381 if (substack==NULL) { /* out of memory */
382 substack=savestack;
383 return 0;
384 }
385 }
386 substack[subcnt-1].cnum=cnum;
387 substack[subcnt-1].ip=ip;
388 substack[subcnt-1].failaddr=failaddr;
389 return 1;
390 }
391
392
393 /* pop_subcall_grammar is called before this */
394 static void pop_subcall(int *rcnum, int *rip, int *rfailaddr)
395 {
396 assert(*rcnum==substack[subcnt-1].cnum);
397 /* *rcnum=substack[subcnt-1].cnum; */
398 *rip=substack[subcnt-1].ip;
399 *rfailaddr=substack[subcnt-1].failaddr;
400 subcnt--;
401 }
402
403 /* This is called after push_subcall */
404 static void push_subcall_grammar(int m_actor,int vcode,int m_dobj,word m_prep,
405 int m_iobj, int cnum)
406 {
407 /* run_metacommand should already have pushed cnum on the stack */
408 substack[subcnt-1].vb=vb;
409 substack[subcnt-1].prep=prep;
410 substack[subcnt-1].mactor=m_actor;
411 substack[subcnt-1].vcode=vcode;
412 substack[subcnt-1].mdobj=m_dobj;
413 substack[subcnt-1].mprep=m_prep;
414 substack[subcnt-1].miobj=m_iobj;
415 }
416
417 /* Return false if something goes wrong-- such as stack underflow. */
418 /* This is called *before* pop_subcall */
419 static rbool pop_subcall_grammar(integer *m_actor,int *vcode,
420 integer *m_dobj,word *m_prep,integer *m_iobj,
421 int *cnum)
422 {
423 if (subcnt==0) return 0;
424 vb=substack[subcnt-1].vb;
425 prep=substack[subcnt-1].prep;
426 *cnum=substack[subcnt-1].cnum;
427 *m_actor=substack[subcnt-1].mactor;
428 *vcode=substack[subcnt-1].vcode;
429 *m_dobj=substack[subcnt-1].mdobj;
430 *m_prep=substack[subcnt-1].mprep;
431 *m_iobj=substack[subcnt-1].miobj;
432 return 1;
433 }
434
435
436
437
438 /* ------------------------------------------------------------------- */
439 /* Run Metacommand */
440 /* ------------------------------------------------------------------- */
441
442 static int run_metacommand(int cnum, int *redir_offset)
443 /* cnum=command number to run. */
444 /* *redir_offset=offset of redirect header, if we exit with redirection. */
445 /* Return
446 0 to go on to next metacommand,
447 1 to stop running metacommands, and
448 2 to end the turn.
449 3 indicates that redirection has just occured
450 4 indicates a subcall has just occured.
451 5 Is used to go on to the next metacommand after a Return.
452 -2 means we're doing disambiguation and just hit an action token. */
453 {
454 int ip, oip; /* ip=Instruction pointer, oip=Old instruction pointer */
455 int r; /* Used to hold return value from token execution */
456 int fail_addr; /* What address to jump to on failure */
457 rbool fail; /* Last token was a conditional token that failed */
458 rbool ortrue, blocktrue, orflag; /* OR stuff
459 orflag: Are we in an OR group?
460 ortrue: Is current OR group true?
461 blocktrue: Is current block w/in OR true?
462 */
463 static rbool restart=0; /* Restarting after subroutine? */
464 op_rec currop; /* Information on the current token and its args */
465
466 fail_addr=32000; /* Fall off the end when we fail */
467 fail=0;
468 ip=0;
469 orflag=blocktrue=ortrue=0;
470 *redir_offset=1; /* Default: This is what RedirectTo does.
471 Only XRedirect can send a different value */
472
473
474 if (restart) /* finish up Return from subroutine */
475 pop_subcall(&cnum,&ip,&fail_addr);
476
477 if (DEBUG_AGT_CMD && !supress_debug) {
478 debug_head(cnum);
479 if (restart) debugout(" (Resuming after subroutine)\n");
480 }
481
482 restart=0;
483
484
485 /* ========== Main Loop ================= */
486 while(ip<command[cnum].cmdsize) {
487
488 oip=ip;
489 ip+=decode_instr(&currop,command[cnum].data+ip,command[cnum].cmdsize-ip);
490
491 /* ------- OR Logic --------------- */
492 if (currop.op==109) { /* OR */
493 if (!orflag) { /* First OR; set things up */
494 orflag=1;
495 ortrue=0;
496 blocktrue=1;
497 }
498 blocktrue=blocktrue && !fail; /* Was the previous token true? */
499 fail=0;
500 ortrue=ortrue||blocktrue; /* OR in last block */
501 blocktrue=1; /* New block starts out true. */
502 }
503 else if (orflag) { /* we're in the middle of a block */
504 blocktrue=blocktrue&&!fail; /* Add in previous token */
505 fail=0;
506 if (currop.endor) { /* i.e. not a conditional token */
507 orflag=0; /* End of OR block */
508 ortrue=ortrue||blocktrue; /* OR in last block */
509 fail=!ortrue; /* Success of whole group */
510 }
511 }
512
513 /* ------------ FAILMESSAGE handling ------------- */
514 if (currop.failmsg) { /* Is the current token a Fail... token? */
515 if (!fail) continue; /* Skip it; look at next instruction */
516 /* ErrMessage and ErrStdMessage: set disambiguation score */
517 if (do_disambig) {
518 if (currop.op==1130 || currop.op==1131) {
519 if (!decode_args(oip,&currop)) return 2;
520 disambig_score=currop.arg1;
521 return 2;
522 }
523 else return -2; /* FailMessage counts as an action token */
524 }
525 /* Then run the failmessage, skipping the following step... */
526 }
527 /* -------- Failure routines -------------------- */
528 else if (fail) { /* ... and not failmessage */
529 /* consequences of failure */
530 fail=0; /* In case fail_addr doesn't point off the edge of the world */
531 ip=fail_addr;
532 fail_addr=32000; /* Reset fail_addr */
533 continue; /* Usually fail_addr will fall off the end, causing this to
534 return 0 */
535 }
536
537 /* - Finish decoding arguments and print out debugging message - */
538 if (!decode_args(oip,&currop)) {
539 if (currop.op<1000) fail=currop.negate ? 0 : 1;
540 continue;
541 /* return 2;*/
542 }
543
544 /* -------- Commands that need to be handled specially -------------- */
545 if (currop.op==109) { /* OR */
546 if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0);
547 continue; /* OR: skip further processing */
548 }
549
550 if (currop.op==1037) { /* DoSubroutine */
551 if (!push_subcall(cnum,ip,fail_addr)) {
552 writeln("GAME ERROR: Subroutine stack overflow.");
553 return 2;
554 }
555 subcall_arg=currop.arg1;
556 if (DEBUG_AGT_CMD && !supress_debug) debugout("--> Call\n");
557 return 4;
558 }
559
560 if (currop.op==1038) { /* Return */
561 restart=1;
562 if (DEBUG_AGT_CMD && !supress_debug) debugout("--> Return\n");
563 return 5;
564 }
565
566 if (currop.op==1149) { /* Goto */
567 ip=currop.arg1;
568 if (DEBUG_AGT_CMD && !supress_debug) debugout("\n");
569 continue;
570 }
571
572 if (currop.op==1150) { /* OnFailGoto */
573 fail_addr=currop.arg1;
574 if (DEBUG_AGT_CMD && !supress_debug) debugout("\n");
575 continue;
576 }
577
578 if (currop.op==1152) /* XRedirect */
579 *redir_offset=currop.arg1;
580
581 /* ---------- Disambiguation Success -------------- */
582 if (do_disambig && currop.disambig) {
583 if (DEBUG_AGT_CMD && !supress_debug) debugout("==> ACTION\n");
584 return -2;
585 }
586
587 /* ---------- Run normal metacommands -------------- */
588 switch(r=exec_instr(&currop))
589 {
590 case 0: /* Normal action token or successful conditional token */
591 if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0);
592 continue;
593 case 1: /* Conditional token: fail */
594 if (DEBUG_AGT_CMD && !supress_debug) {
595 if (orflag) debugout(" (-->FAIL)\n");
596 else debugout("--->FAIL\n");
597 }
598 fail=1;
599 continue;
600 default: /* Return explicit value */
601 if (DEBUG_AGT_CMD && !supress_debug) {
602 if (r==103) debugout("-->Redirect\n");
603 else debugout("==> END\n");
604 }
605 return r-100;
606 }
607 }
608 return 0;
609 }
610
611
612
613 /* ====================================================================*/
614 /* SCAN METACOMMAND: These are the routines that scan through the */
615 /* metacommand headers and find the appropriate ones to execute */
616 /* Redirection is also handled at this level */
617 /* ====================================================================*/
618
619
620 /* ------------------------------------------------------------------- */
621 /* Support routines for extracting object information */
622 /* ------------------------------------------------------------------- */
623
624 /* For $ strings. Returns object number if there is one, or negative
625 the dictionary index.
626 This is used by the metacommand redirection routines */
627
628 static integer expand_redirect(word w)
629 {
630 assert(w!=-1); /* <*NONE*> object shouldn't make it this far */
631 if (w==0 || aver<AGT15) return -w;
632 if (w==ext_code[wdverb]) return -syntbl[auxsyn[vb]];
633 if (w==ext_code[wdnoun]) return dobj;
634 if (w==ext_code[wdobject]) return iobj;
635 if (w==ext_code[wdname]) return actor;
636 if (w==ext_code[wdadjective]) return -it_adj(dobj);
637 if (w==ext_code[wdprep]) return -prep;
638 return -w;
639 }
640
641
642 static int extract_actor(int actnum)
643 {
644 if (actnum<0) actnum=-actnum; /* Erase redirection stuff */
645 if (tcreat(actnum)) return actnum;
646 else return 0;
647 }
648
649 /* Basically, we need to find an object with a matching noun
650 and adj to our choice. */
651 static int extract_obj(word name, word adj)
652 {
653 int i,obj;
654
655 /* We just take the first one. We split this into separate noun and
656 creature loops for performance reaons */
657
658 if (name==-1) /* <*NONE*> */
659 return 0;
660
661 obj=expand_redirect(name);
662 adj=it_name(expand_redirect(adj));
663
664 if (obj>0) { /* $noun$, $object$, or $name$ */
665 if (adj==0 || adj==it_adj(obj))
666 return obj; /* We're done */
667 name=it_name(obj);
668 } else
669 name=-obj;
670
671 if (adj==0) return -name; /* Adjectives required for CLASS redirect */
672 nounloop(i)
673 if (noun[i].name==name && noun[i].adj==adj) return i+first_noun;
674 creatloop(i)
675 if (creature[i].name==name && creature[i].adj==adj)
676 return i+first_creat;
677 /* Hmm... just hope it's an internal noun. */
678 writeln("GAME ERROR: Redirect statement with bad object name.");
679 return -name;
680 }
681
682
683 /* ------------------------------------------------------------------- */
684 /* Redirection Routines */
685 /* ------------------------------------------------------------------- */
686
687
688 #define wordcode_fix(w) it_name(expand_redirect(w));
689
690 /* 'real_obj' below is the dobj_obj/iobj_obj field; it takes
691 precedence over anything else if it is nonzero.
692 It represents an *explicitly* declared object in
693 the header */
694
695 static void fix_objnum(integer *objnum, word match,
696 int real_obj,
697 int actor, int dobj, int iobj)
698 {
699 if (real_obj) *objnum=real_obj;
700 else if (match==ext_code[wdobject]) *objnum=iobj;
701 else if (match==ext_code[wdnoun]) *objnum=dobj;
702 else if (match==ext_code[wdname]) *objnum=actor;
703 }
704
705 /* Returns TRUE if we changed *objrec, FALSE otherwise */
706 /* (This is needed for memory allocation purposes) */
707 static rbool fix_objrec(parse_rec **objrec, word match,
708 int real_obj,
709 parse_rec *actrec, parse_rec *dobjrec,
710 parse_rec *iobjrec)
711 {
712 if (real_obj) *objrec=make_parserec(real_obj,NULL);
713 else if (match==ext_code[wdobject]) *objrec=copy_parserec(iobjrec);
714 else if (match==ext_code[wdnoun]) *objrec=copy_parserec(dobjrec);
715 else if (match==ext_code[wdname]) *objrec=copy_parserec(actrec);
716 else return 0; /* *objrec unchanged */
717
718 return 1; /* *objrec changed */
719 }
720
721 static void objcode_fix(cmd_rec *cmd)
722 /* For $ strings. Fixes object redirection if neccessary */
723 {
724 int actorword;
725 word nounword, objword;
726 int dobj_obj, iobj_obj;
727 int savedobj, saveactor;
728 parse_rec *savedrec, *saveactrec, *saveirec;
729 rbool achange, dchange, ichange; /* Did the given _rec ptr change? */
730
731 /* dobj_obj/iobj_obj take precedence over anything else */
732 actorword=cmd->actor; nounword=cmd->nouncmd; objword=cmd->objcmd;
733 dobj_obj=cmd->noun_obj; iobj_obj=cmd->obj_obj;
734
735 /* Make temporary copies of things for when more than one thing is
736 being shuffled around; we don't need to save iobj since
737 it's processed last */
738 saveactor=actor; saveactrec=actor_rec;
739 savedobj=dobj; savedrec=dobj_rec;
740 saveirec=iobj_rec; /* Saved only so it can be freed */
741
742 /* Fix object numbers... */
743 fix_objnum(&actor,actorword,0,saveactor,savedobj,iobj);
744 fix_objnum(&dobj,nounword,dobj_obj,saveactor,savedobj,iobj);
745 fix_objnum(&iobj,objword,iobj_obj,saveactor,savedobj,iobj);
746
747 /* ... and records */
748 achange=fix_objrec(&actor_rec,actorword,0,saveactrec,savedrec,iobj_rec);
749 dchange=fix_objrec(&dobj_rec,nounword,dobj_obj,saveactrec,savedrec,iobj_rec);
750 ichange=fix_objrec(&iobj_rec,objword,iobj_obj,saveactrec,savedrec,iobj_rec);
751
752 /* Free up whatever needs freeing */
753 if (achange) rfree(saveactrec);
754 if (dchange) rfree(savedrec);
755 if (ichange) rfree(saveirec);
756 }
757
758
759 /* Redirection is very superficial-- normally all it does is */
760 /* change the matching pattern, not the underlying objects */
761 /* The one exception is when we use the special redirection tokens */
762 /* NOUN or OBJECT */
763
764 void redirect_exec(cmd_rec *cmd,word *m_actor,int *vcode,
765 word *m_dobj,word *m_prep,word *m_iobj)
766 {
767 *m_actor=extract_actor(cmd->actor);
768 vb=*vcode=verb_code(it_name(expand_redirect(cmd->verbcmd)));
769 *m_dobj=extract_obj(cmd->nouncmd,cmd->noun_adj);
770 if (cmd->prep==-1)
771 *m_prep=0;
772 else
773 *m_prep=it_name(expand_redirect(cmd->prep));
774 *m_iobj=extract_obj(cmd->objcmd,cmd->obj_adj);
775
776 /* This shuffles the _real_ objects if $noun$ forms are being
777 used */
778 objcode_fix(cmd);
779 }
780
781
782
783
784 /* ------------------------------------------------------------------- */
785 /* Scan Metacommand and the matching function it uses */
786 /* ------------------------------------------------------------------- */
787
788 /* This is used to match the elements of metacommand trigger patterns */
789 /* Sees if w2 matches COMMMAND pattern word w1; w1==0 corresponds to ANY */
790 #define cmatch(w1,w2) ((w1)==0 || (w1)==(w2) || ((w1)==-1 && (w2)==0))
791
792 static int cm_actor(int actnum,int actor)
793 /* cmd: actnum, player entry: actor */
794 {
795 if (aver<AGX00) return 1; /* Bit of AGT brain-deadness. */
796 if (actnum==1) return actor==0; /* No actor: just the player */
797 if (tcreat(actnum))
798 return (creat_fix[actor-first_creat]==creat_fix[actnum-first_creat]);
799 if (actnum==2) return (actor!=0); /* ANYBODY? */
800 return (actor==0);
801 }
802
803
804 /* Check that the explicit object matches */
805 static rbool cm_x_obj(int x_obj,int real_obj)
806 {
807 if (x_obj==0) return 1; /* No explicit object; automatically match. */
808 /* Explicit object case */
809 /* In this case, we match against the _real_ object */
810 /* However, we also require a "normal" match */
811 do {
812 if (x_obj==real_obj) return 1;
813 real_obj=it_class(real_obj);
814 } while (real_obj!=0);
815 return 0;
816 }
817
818 /* Does [obj] match <adj> <noun> [x_obj]? */
819 /* --[obj] must match up with <adj> <noun> */
820 /* --If x_obj(the explicit object) is defined, it must match with
821 the "real" object-- that is, the global dobj or iobj value. */
822 static rbool cm_obj(word name, word adj, int x_obj, int obj, int real_obj)
823 {
824 if (name==-1) return (obj==0); /* <NONE> */
825
826 if (x_obj && !cm_x_obj(x_obj,real_obj)) return 0;
827
828 /* (Note that ANY does not match ALL) */
829 if (obj==-ext_code[wall])
830 return(name==ext_code[wall] && adj==0);
831
832 do { /* Work our way up the class hierarchy */
833 if (cmatch(name,it_name(obj)) && cmatch(adj,it_adj(obj)))
834 return 1;
835 obj=it_class(obj);
836 } while(obj!=0);
837
838 return 0;
839 }
840
841
842
843 static void scan_dbg(int vcode)
844 {
845 char buff[220];
846 word w;
847
848 if (vcode>=BASE_VERB && vcode<BASE_VERB+DUMB_VERB
849 && syntbl[synlist[vcode]]!=0)
850 w=syntbl[synlist[vcode]];
851 else w=syntbl[auxsyn[vcode]];
852
853 if (strlen(dict[w])>200) return; /* Just in case... */
854 sprintf(buff,"+++++Scanning %s\n",dict[w]);
855 debugout(buff);
856 }
857
858 #define not_any(n,a) (n!=0 || a!=0)
859
860 /* This returns true if we redirect from VERB OBJ {PREP OBJ}
861 to something that has fewer objects or no (explicit) preposition.
862 This is less perfect than I would like since there is currently
863 no way of distinguishing between ANY and an empty slot unless
864 the new "NOMATCH" extension is used. */
865
866 static rbool redir_narrows_grammar(cmd_rec *cmd1, cmd_rec *cmd2)
867 {
868 /* Check inward from obj to prep to noun; if in any of these
869 fields cmd2 has ANY and cmd1 doesn't, return 1.
870 Stop as soon as we find a non-ANY field in either one. */
871
872 /* If we *are* using the new extension, we can just use that info */
873 if (cmd2->objcmd==-1) {
874 if (cmd1->objcmd!=-1) return 1;
875 if (cmd1->prep==-1) {
876 if (cmd1->prep!=-1) return 1;
877 if (cmd2->nouncmd==-1 && cmd1->objcmd!=-1) return 1;
878 }
879 }
880 if (nomatch_aware) return 0; /* If we are using nomatch, don't need
881 to go through the rest of this nonsense. */
882
883 if (not_any(cmd2->objcmd,cmd2->obj_adj)) return 0;
884 if (not_any(cmd1->objcmd,cmd1->obj_adj)) return 1;
885
886 if (cmd2->prep!=0) return 0;
887 if (cmd1->prep!=0) return 1;
888
889 if (not_any(cmd2->nouncmd,cmd2->noun_adj)) return 0;
890 if (not_any(cmd1->nouncmd,cmd1->noun_adj)) return 1;
891
892 return 0; /* They are both all ANY. */
893 }
894
895
896
897 static rbool cm_command(cmd_rec *cmd,
898 integer m_actor, int m_verb,
899 integer m_dobj, word m_prep, integer m_iobj)
900 {
901 if (cmd->verbcmd==0) { /* ANY */
902 if (cmd->actor==0 && aver>=AGX00)
903 return (m_verb==0); /* ANY command: rest of line ignored */
904 /* Else ANY matchs; go on to test other things. */
905 } else
906 if (cmd->verbcmd!=m_verb) return 0;
907
908 return
909 cm_actor(cmd->actor,m_actor)
910 && cm_obj(cmd->nouncmd,cmd->noun_adj,cmd->noun_obj,m_dobj,dobj)
911 && cmatch(cmd->prep,m_prep)
912 && cm_obj(cmd->objcmd,cmd->obj_adj,cmd->obj_obj,m_iobj,iobj);
913 }
914
915
916
917 static void scan_for_actor(integer m_actor, int *start, int *end)
918 {
919 int i;
920
921 assert(m_actor!=0);
922
923 if (aver>=AGX00) {
924 if (start!=NULL) *start=verbptr[DIR_ADDR_CODE];
925 *end=verbend[DIR_ADDR_CODE];
926 return;
927 }
928 for(i=verbend[DIR_ADDR_CODE];i>verbptr[DIR_ADDR_CODE];i--)
929 if (creat_fix[command[i].actor-first_creat]
930 ==creat_fix[m_actor-first_creat]) {
931 i++;
932 break;
933 }
934 *end=i;
935
936 if (start==NULL) return;
937
938 for(i=verbptr[DIR_ADDR_CODE];i<=*end;i++)
939 if (creat_fix[command[i].actor-first_creat]
940 ==creat_fix[m_actor-first_creat])
941 break;
942 *start=i;
943 }
944
945
946 /* m_<word> are the matching criterion; they have no *neccessary*
947 connection to dobj, iobj, etc. */
948
949 int scan_metacommand(integer m_actor,int vcode,
950 integer m_dobj, word m_prep, integer m_iobj,
951 int *redir_flag)
952 /* Return codes: 0=end of this cycle, 1=end of all commands
953 2=end of turn */
954 /* If doing disambiguation, then -2=end of cycle, something happened;
955 0 or 1=end of cycle; nothing happened; 2=end of turn, nothing happened. */
956 /* If redir_flag is non-NULL, it is set when redirection occurs:
957 1+=Redirection occured
958 2=Grammar-changing redirection occured. */
959 {
960 int i, oldi;
961 word m_verb;
962 int scanend;
963 int redir_offset; /* Used for multiple redirects in the same
964 metacommand (which can occur in AGATE-style
965 commands)-- this is used to hold the offset
966 of the given redirect. */
967 long redirect_count; /* This is a safety measure: this keeps track of how
968 many redirections have occured on a single turn, and
969 if there are "too many" it will issue an error message
970 and stop. This is to prevent the system from getting
971 into a redirection loop. The number should be set
972 high enough not to prevent deliberate loops,
973 however. */
974
975 rfree(substack); subcnt=0; subsize=0;
976 redirect_count=0;
977
978 if (mars_fix)
979 if (vcode==0 || m_actor==2) return 0;
980 /* Don't explicity scan ANY metacommands if MARS fix is active. */
981 if (m_actor==-ext_code[weverybody]) m_actor=2;
982
983
984 if (DEBUG_AGT_CMD && DEBUG_SCAN &&!supress_debug) scan_dbg(vcode);
985
986 m_verb=syntbl[auxsyn[vcode]];
987 if (m_actor==0) {
988 i=verbptr[vcode];
989 scanend=verbend[vcode];
990 } else
991 scan_for_actor(m_actor,&i,&scanend);
992 for(;i<scanend;i++)
993 if ( command[i].actor<0) {/* REDIRECT data; skip over it */;}
994 else if (cm_command(&command[i],m_actor,m_verb,m_dobj,m_prep,m_iobj))
995 switch (run_metacommand(i,&redir_offset))
996 {
997 case -2:rfree(substack); return -2;
998 /* We are doing disambiguation and reached
999 an action token */
1000 case 0:break; /* Go onto next metacommand */
1001 case 1:rfree(substack); return 1; /* Done with metacommands */
1002 case 2:rfree(substack); return 2; /* Done with turn */
1003
1004
1005 /* -------- REDIRECTION ------------ */
1006 /* This handles RedirectTo tokens */
1007 case 3:
1008 oldi=i;
1009 i+=redir_offset;
1010 if (i==last_cmd || command[i].actor>0) {
1011 if (!PURE_ERROR) writeln("GAME ERROR: Invalid REDIRECT token.");
1012 rfree(substack);
1013 return 2;
1014 }
1015 if (MAX_REDIR!=0 && ++redirect_count>MAX_REDIR) {
1016 if (!PURE_ERROR) writeln("GAME ERROR: Infinite REDIRECT loop.");
1017 rfree(substack);
1018 return 2;
1019 }
1020 if (DEBUG_AGT_CMD && !supress_debug) {
1021 debugout(" ==>");
1022 debug_head(i);
1023 }
1024
1025 /* REDIRECT :If we do a redirect from a broader grammar to a
1026 narrower grammer, it will be noted so that certain types
1027 of grammer checking can be disabled. */
1028 if (redir_flag!=NULL) {
1029 if (*redir_flag<2
1030 && redir_narrows_grammar(&command[oldi],&command[i]))
1031 *redir_flag=2;
1032
1033 /* Set *redir_flag to at least 1 if we do *any* redirection. */
1034 if (!*redir_flag) *redir_flag=1;
1035 }
1036
1037 /* REDIRECT: Do the actual redirection, building the new command
1038 header and shuffling around nouns and verbs as
1039 neccessary */
1040 redirect_exec(&command[i],&m_actor,&vcode,
1041 &m_dobj,&m_prep,&m_iobj);
1042
1043 /* REDIRECT: Start scanning again from the beginning */
1044 if (!mars_fix) {/* In MARS, we *don't* go back to the top */
1045 if (m_actor!=0)
1046 scan_for_actor(m_actor,&i,&scanend);
1047 else {
1048 i=verbptr[vcode];
1049 scanend=verbend[vcode];
1050 }
1051 i--; /* Back up one so that the following i++ we'll
1052 be at the right location */
1053 }
1054
1055 /* So when i is incremented, we start back at the correct start: i.e.
1056 we start scanning again from the beginning. It's even possible
1057 to use REDIRECT to run verb commands from an AFTER command,
1058 although it precludes other AFTER commands from running. */
1059 m_verb=syntbl[auxsyn[vcode]];
1060 break;
1061
1062
1063
1064 /* -------- SUBROUTINE CALL ------------ */
1065 case 4: /* Subroutine Call -- same idea as RedirectTo,
1066 but less complicated */
1067 push_subcall_grammar(m_actor,vcode,m_dobj,m_prep,m_iobj,i);
1068 vcode=verb_code(sub_name[subcall_arg-1]);
1069 m_actor=m_dobj=m_iobj=0; m_prep=0;
1070
1071 if (!mars_fix) /* In MARS, we *don't* go back to the top */
1072 i=verbptr[vcode]-1;
1073 scanend=verbend[vcode];
1074 m_verb=syntbl[auxsyn[vcode]];
1075 break;
1076
1077
1078 /* -------- RETURN ------------ */
1079 case 5: /* Return: pop grammar state, then ... ? */
1080 if (!pop_subcall_grammar(&m_actor,&vcode,
1081 &m_dobj,&m_prep,&m_iobj,&i)) {
1082 writeln("GAME ERROR: Return without DoSubroutine.");
1083 rfree(substack);
1084 return 2;
1085 }
1086
1087 if (m_actor==0)
1088 scanend=verbend[vcode];
1089 else
1090 scan_for_actor(m_actor,NULL,&scanend);
1091 m_verb=syntbl[auxsyn[vcode]];
1092
1093 i--; /* Cause the last command to restart,
1094 at which point run_command will pop the rest of the
1095 stack. */
1096
1097 break;
1098 }
1099 rfree(substack);
1100 return 0; /* Done with this cycle of metacommands */
1101 }
1102
1103 /* ====================================================================*/
1104
1105 #undef cm
1106