2
/* advexe.c - adventure code executer */
4
Copyright (c) 1986, by David Michael Betz
12
/* external variables */
14
extern int nouns[],*adjectives[];
17
int pc,opcode,p2,p3,sts;
18
int stack[STKSIZE],*sp,*fp,*top;
21
/* Function Prototypes. Gosh */
31
/* execute - execute adventure code */
34
/* setup initial program counter */
35
if ((pc = code) == NIL)
39
sp = fp = top = stack + STKSIZE;
41
/* execute the code */
42
for (sts = 0; sts == 0; )
48
/* exe_one - execute one instruction */
52
opcode = getcbyte(pc); pc++;
54
/* execute the instruction */
57
*--sp = getboperand();
59
*--sp = (int)(top - fp);
61
pc = getafield(fp[fp[2]+3],A_CODE);
64
*--sp = getboperand();
66
*--sp = (int)(top - fp);
69
p2 = getofield(p2,O_CLASS);
72
if (p2 && (p2 = getp(p2,fp[fp[2]+1]))) {
73
pc = getafield(p2,A_CODE);
77
/* return NIL if there is no method for this message */
105
error("too few arguments");
111
error("too few arguments");
115
pc = (*sp ? getwoperand() : pc+2);
118
pc = (*sp ? pc+2 : getwoperand());
133
*sp = (*sp ? NIL : T);
149
*sp = (p2 == 0 ? 0 : *sp / p2);
153
*sp = (p2 == 0 ? 0 : *sp % p2);
168
*sp = (*sp < p2 ? T : NIL);
172
*sp = (*sp == p2 ? T : NIL);
176
*sp = (*sp > p2 ? T : NIL);
185
*sp = -getboperand();
188
*sp = getvalue(getwoperand());
191
*sp = getvalue(getboperand());
194
setvalue(getwoperand(),*sp);
197
setvalue(getboperand(),*sp);
206
*sp = setp(*sp,p2,p3);
235
*sp = (line[0] == 'Y' || line[0] == 'y' ? T : NIL);
238
*sp = getofield(*sp,O_CLASS);
242
*sp = (match(*sp,nouns[p2-1],adjectives[p2-1]) ? T : NIL);
245
if ((*sp = db_save()) == NIL) // VB: added if
246
trm_str("Sorry, I couldn't save to this file.\n");
249
if ((*sp = db_restore()) == NIL) // VB: added if
250
trm_str("Sorry, I couldn't restore from this file.\n");
262
case OP_VOWEL: // patch for smart articles by MRP
263
*sp = vowel(*sp); // patch for smart articles by MRP
264
break; // patch for smart articles by MRP
266
if (opcode >= OP_XVAR && opcode < OP_XSET)
267
*sp = getvalue(opcode - OP_XVAR);
268
else if (opcode >= OP_XSET && opcode < OP_XPLIT)
269
setvalue(opcode - OP_XSET,*sp);
270
else if (opcode >= OP_XPLIT && opcode < OP_XNLIT)
271
*sp = opcode - OP_XPLIT;
272
else if (opcode >= OP_XNLIT && opcode < 256)
273
*sp = OP_XNLIT - opcode;
275
trm_str("Bad opcode\n");
280
/* getboperand - get data byte */
284
data = getcbyte(pc); pc += 1;
288
/* getwoperand - get data word */
292
data = getcword(pc); pc += 2;
296
/* print - print a message */
302
while (ch = msg_byte())
306
/* vowel - check for vowel */ // patch for smart articles by MRP
307
int vowel(int msg) // patch for smart articles by MRP
308
{ // patch for smart articles by MRP
309
int ch,r; // patch for smart articles by MRP
310
// patch for smart articles by MRP
311
msg_open(msg); // patch for smart articles by MRP
312
ch = msg_byte(); // patch for smart articles by MRP
313
if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') // patch by MRP
314
r = T; // patch for smart articles by MRP
315
else // patch for smart articles by MRP
316
r = NIL; // patch for smart articles by MRP
317
while (ch) // patch for smart articles by MRP
318
ch = msg_byte(); // patch for smart articles by MRP
319
return (r); // patch for smart articles by MRP
320
} // patch for smart articles by MRP
322
/* pnumber - print a number */
331
/* getrand - get a random number between 0 and n-1 */
336
/* make sure we don't get stuck at zero */
337
if (rseed == 0L) rseed = 1L;
339
/* algorithm taken from Dr. Dobbs Journal, November 1985, page 91 */
340
k1 = rseed / 127773L;
341
if ((rseed = 16807L * (rseed - k1 * 127773L) - k1 * 2836L) < 0L)
342
rseed += 2147483647L;
344
/* return a random number between 0 and n-1 */
345
return ((int)(rseed % (long)n));
348
/* setrand - set the random number seed */