| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 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 |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
#define MAX_SUBCALL 2047 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
rbool argvalid(int argtype,int arg) |
| 67 |
{ |
| 68 |
if (argtype&AGT_VAR) { |
| 69 |
|
| 70 |
if (arg<0 || arg>VAR_NUM) return 0; |
| 71 |
|
| 72 |
if (argtype==AGT_VAR) return 1; |
| 73 |
|
| 74 |
|
| 75 |
arg=agt_var[arg]; |
| 76 |
argtype&=~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) ); |
| 107 |
case AGT_OBJFLAG: return (arg>=0 && arg<oflag_cnt); |
| 108 |
case AGT_OBJPROP: return(arg>=0 && arg<oprop_cnt); |
| 109 |
case AGT_ATTR: |
| 110 |
case AGT_PROP: |
| 111 |
return 1; |
| 112 |
default:writeln("INTERNAL ERROR:Unrecognized type specifier."); |
| 113 |
} |
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 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; |
| 126 |
case 1: |
| 127 |
if (*arg==-1) { |
| 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; |
| 135 |
case 3: *arg=iobj; *special=(iobj==0); break; |
| 136 |
default: rprintf("Internal error: Invalid optype."); |
| 137 |
return 0; |
| 138 |
} |
| 139 |
if (!(optype&2)) { |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
if (argtype<64) { |
| 144 |
if (*arg==-1) |
| 145 |
{*arg=actor; *special=(actor==0);} |
| 146 |
else if (*arg==-2) |
| 147 |
{*arg=dobj; *special=(dobj==0);} |
| 148 |
else if (*arg==-3) |
| 149 |
{*arg=iobj; *special=(iobj==0);} |
| 150 |
} |
| 151 |
} |
| 152 |
return argvalid(argtype,*arg); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 173 |
|
| 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; |
| 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; |
| 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; |
| 214 |
|
| 215 |
|
| 216 |
switch(op) |
| 217 |
{ |
| 218 |
case 89: case 95: case 96: case 97: |
| 219 |
oprec->disambig=1; break; |
| 220 |
case WIN_ACT: case WIN_ACT+1: |
| 221 |
oprec->disambig=1; break; |
| 222 |
|
| 223 |
case 1037: case 1038: |
| 224 |
case 1062: case 1115: |
| 225 |
case 1132: |
| 226 |
case 1149: case 1150: |
| 227 |
case 1151: |
| 228 |
case 1152: |
| 229 |
break; |
| 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; |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
default: |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
oprec->disambig=(op>=START_ACT && op<WIN_ACT); |
| 243 |
} |
| 244 |
|
| 245 |
if (op>=1128 && op<=1131) |
| 246 |
oprec->failmsg=1; |
| 247 |
|
| 248 |
leng=oprec->opdata->argnum+1; |
| 249 |
if (optype!=0) { |
| 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) { |
| 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 |
|
| 283 |
|
| 284 |
static rbool decode_args(int ip,op_rec *oprec) |
| 285 |
{ |
| 286 |
rbool grammer_arg; |
| 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) { |
| 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 |
|
| 304 |
if (!argok(oprec->opdata,&(oprec->arg1),&(oprec->arg2), |
| 305 |
oprec->optype,&grammer_arg)) { |
| 306 |
|
| 307 |
|
| 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 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
typedef struct { |
| 352 |
|
| 353 |
short cnum, ip, failaddr; |
| 354 |
|
| 355 |
integer mactor, mdobj, miobj; |
| 356 |
word mprep; |
| 357 |
short vcode; |
| 358 |
|
| 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; |
| 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) { |
| 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 |
|
| 394 |
static void pop_subcall(int *rcnum, int *rip, int *rfailaddr) |
| 395 |
{ |
| 396 |
assert(*rcnum==substack[subcnt-1].cnum); |
| 397 |
|
| 398 |
*rip=substack[subcnt-1].ip; |
| 399 |
*rfailaddr=substack[subcnt-1].failaddr; |
| 400 |
subcnt--; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 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 |
|
| 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 |
|
| 418 |
|
| 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 |
|
| 440 |
|
| 441 |
|
| 442 |
static int run_metacommand(int cnum, int *redir_offset) |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
{ |
| 454 |
int ip, oip; |
| 455 |
int r; |
| 456 |
int fail_addr; |
| 457 |
rbool fail; |
| 458 |
rbool ortrue, blocktrue, orflag; |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
static rbool restart=0; |
| 464 |
op_rec currop; |
| 465 |
|
| 466 |
fail_addr=32000; |
| 467 |
fail=0; |
| 468 |
ip=0; |
| 469 |
orflag=blocktrue=ortrue=0; |
| 470 |
*redir_offset=1; |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
if (restart) |
| 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 |
|
| 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 |
|
| 492 |
if (currop.op==109) { |
| 493 |
if (!orflag) { |
| 494 |
orflag=1; |
| 495 |
ortrue=0; |
| 496 |
blocktrue=1; |
| 497 |
} |
| 498 |
blocktrue=blocktrue && !fail; |
| 499 |
fail=0; |
| 500 |
ortrue=ortrue||blocktrue; |
| 501 |
blocktrue=1; |
| 502 |
} |
| 503 |
else if (orflag) { |
| 504 |
blocktrue=blocktrue&&!fail; |
| 505 |
fail=0; |
| 506 |
if (currop.endor) { |
| 507 |
orflag=0; |
| 508 |
ortrue=ortrue||blocktrue; |
| 509 |
fail=!ortrue; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
|
| 514 |
if (currop.failmsg) { |
| 515 |
if (!fail) continue; |
| 516 |
|
| 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; |
| 524 |
} |
| 525 |
|
| 526 |
} |
| 527 |
|
| 528 |
else if (fail) { |
| 529 |
|
| 530 |
fail=0; |
| 531 |
ip=fail_addr; |
| 532 |
fail_addr=32000; |
| 533 |
continue; |
| 534 |
|
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
if (!decode_args(oip,&currop)) { |
| 539 |
if (currop.op<1000) fail=currop.negate ? 0 : 1; |
| 540 |
continue; |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
if (currop.op==109) { |
| 546 |
if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0); |
| 547 |
continue; |
| 548 |
} |
| 549 |
|
| 550 |
if (currop.op==1037) { |
| 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) { |
| 561 |
restart=1; |
| 562 |
if (DEBUG_AGT_CMD && !supress_debug) debugout("--> Return\n"); |
| 563 |
return 5; |
| 564 |
} |
| 565 |
|
| 566 |
if (currop.op==1149) { |
| 567 |
ip=currop.arg1; |
| 568 |
if (DEBUG_AGT_CMD && !supress_debug) debugout("\n"); |
| 569 |
continue; |
| 570 |
} |
| 571 |
|
| 572 |
if (currop.op==1150) { |
| 573 |
fail_addr=currop.arg1; |
| 574 |
if (DEBUG_AGT_CMD && !supress_debug) debugout("\n"); |
| 575 |
continue; |
| 576 |
} |
| 577 |
|
| 578 |
if (currop.op==1152) |
| 579 |
*redir_offset=currop.arg1; |
| 580 |
|
| 581 |
|
| 582 |
if (do_disambig && currop.disambig) { |
| 583 |
if (DEBUG_AGT_CMD && !supress_debug) debugout("==> ACTION\n"); |
| 584 |
return -2; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
switch(r=exec_instr(&currop)) |
| 589 |
{ |
| 590 |
case 0: |
| 591 |
if (DEBUG_AGT_CMD && !supress_debug) debug_newline(op,0); |
| 592 |
continue; |
| 593 |
case 1: |
| 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: |
| 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 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
static integer expand_redirect(word w) |
| 629 |
{ |
| 630 |
assert(w!=-1); |
| 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; |
| 645 |
if (tcreat(actnum)) return actnum; |
| 646 |
else return 0; |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
static int extract_obj(word name, word adj) |
| 652 |
{ |
| 653 |
int i,obj; |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
if (name==-1) |
| 659 |
return 0; |
| 660 |
|
| 661 |
obj=expand_redirect(name); |
| 662 |
adj=it_name(expand_redirect(adj)); |
| 663 |
|
| 664 |
if (obj>0) { |
| 665 |
if (adj==0 || adj==it_adj(obj)) |
| 666 |
return obj; |
| 667 |
name=it_name(obj); |
| 668 |
} else |
| 669 |
name=-obj; |
| 670 |
|
| 671 |
if (adj==0) return -name; |
| 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 |
|
| 678 |
writeln("GAME ERROR: Redirect statement with bad object name."); |
| 679 |
return -name; |
| 680 |
} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
#define wordcode_fix(w) it_name(expand_redirect(w)); |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 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 |
|
| 706 |
|
| 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; |
| 717 |
|
| 718 |
return 1; |
| 719 |
} |
| 720 |
|
| 721 |
static void objcode_fix(cmd_rec *cmd) |
| 722 |
|
| 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; |
| 730 |
|
| 731 |
|
| 732 |
actorword=cmd->actor; nounword=cmd->nouncmd; objword=cmd->objcmd; |
| 733 |
dobj_obj=cmd->noun_obj; iobj_obj=cmd->obj_obj; |
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
saveactor=actor; saveactrec=actor_rec; |
| 739 |
savedobj=dobj; savedrec=dobj_rec; |
| 740 |
saveirec=iobj_rec; |
| 741 |
|
| 742 |
|
| 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 |
|
| 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 |
|
| 753 |
if (achange) rfree(saveactrec); |
| 754 |
if (dchange) rfree(savedrec); |
| 755 |
if (ichange) rfree(saveirec); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 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 |
|
| 777 |
|
| 778 |
objcode_fix(cmd); |
| 779 |
} |
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
#define cmatch(w1,w2) ((w1)==0 || (w1)==(w2) || ((w1)==-1 && (w2)==0)) |
| 791 |
|
| 792 |
static int cm_actor(int actnum,int actor) |
| 793 |
|
| 794 |
{ |
| 795 |
if (aver<AGX00) return 1; |
| 796 |
if (actnum==1) return actor==0; |
| 797 |
if (tcreat(actnum)) |
| 798 |
return (creat_fix[actor-first_creat]==creat_fix[actnum-first_creat]); |
| 799 |
if (actnum==2) return (actor!=0); |
| 800 |
return (actor==0); |
| 801 |
} |
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
static rbool cm_x_obj(int x_obj,int real_obj) |
| 806 |
{ |
| 807 |
if (x_obj==0) return 1; |
| 808 |
|
| 809 |
|
| 810 |
|
| 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 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 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); |
| 825 |
|
| 826 |
if (x_obj && !cm_x_obj(x_obj,real_obj)) return 0; |
| 827 |
|
| 828 |
|
| 829 |
if (obj==-ext_code[wall]) |
| 830 |
return(name==ext_code[wall] && adj==0); |
| 831 |
|
| 832 |
do { |
| 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; |
| 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 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
static rbool redir_narrows_grammar(cmd_rec *cmd1, cmd_rec *cmd2) |
| 867 |
{ |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 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; |
| 881 |
|
| 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; |
| 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) { |
| 902 |
if (cmd->actor==0 && aver>=AGX00) |
| 903 |
return (m_verb==0); |
| 904 |
|
| 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 |
|
| 947 |
|
| 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 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
{ |
| 960 |
int i, oldi; |
| 961 |
word m_verb; |
| 962 |
int scanend; |
| 963 |
int redir_offset; |
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
long redirect_count; |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 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 |
|
| 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) {;} |
| 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 |
|
| 999 |
|
| 1000 |
case 0:break; |
| 1001 |
case 1:rfree(substack); return 1; |
| 1002 |
case 2:rfree(substack); return 2; |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 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 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
if (redir_flag!=NULL) { |
| 1029 |
if (*redir_flag<2 |
| 1030 |
&& redir_narrows_grammar(&command[oldi],&command[i])) |
| 1031 |
*redir_flag=2; |
| 1032 |
|
| 1033 |
|
| 1034 |
if (!*redir_flag) *redir_flag=1; |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
redirect_exec(&command[i],&m_actor,&vcode, |
| 1041 |
&m_dobj,&m_prep,&m_iobj); |
| 1042 |
|
| 1043 |
|
| 1044 |
if (!mars_fix) { |
| 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--; |
| 1052 |
|
| 1053 |
} |
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
m_verb=syntbl[auxsyn[vcode]]; |
| 1060 |
break; |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
case 4: |
| 1066 |
|
| 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) |
| 1072 |
i=verbptr[vcode]-1; |
| 1073 |
scanend=verbend[vcode]; |
| 1074 |
m_verb=syntbl[auxsyn[vcode]]; |
| 1075 |
break; |
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
case 5: |
| 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--; |
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
break; |
| 1098 |
} |
| 1099 |
rfree(substack); |
| 1100 |
return 0; |
| 1101 |
} |
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
#undef cm |
| 1106 |
|