| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "main.h" |
| 9 |
|
| 10 |
|
| 11 |
#include "state.h" |
| 12 |
#include "lists.h" |
| 13 |
#include "syserr.h" |
| 14 |
#include "options.h" |
| 15 |
#include "utils.h" |
| 16 |
#include "args.h" |
| 17 |
#include "parse.h" |
| 18 |
#include "scan.h" |
| 19 |
#include "inter.h" |
| 20 |
#include "rules.h" |
| 21 |
#include "reverse.h" |
| 22 |
#include "debug.h" |
| 23 |
#include "exe.h" |
| 24 |
#include "term.h" |
| 25 |
#include "instance.h" |
| 26 |
#include "memory.h" |
| 27 |
#include "output.h" |
| 28 |
#include "Container.h" |
| 29 |
#include "Location.h" |
| 30 |
#include "dictionary.h" |
| 31 |
#include "class.h" |
| 32 |
#include "score.h" |
| 33 |
#include "decode.h" |
| 34 |
#include "msg.h" |
| 35 |
#include "event.h" |
| 36 |
#include "syntax.h" |
| 37 |
#include "current.h" |
| 38 |
#include "literal.h" |
| 39 |
#include "compatibility.h" |
| 40 |
|
| 41 |
#include "alan.version.h" |
| 42 |
|
| 43 |
|
| 44 |
#ifdef HAVE_GLK |
| 45 |
#include "glk.h" |
| 46 |
#include "glkio.h" |
| 47 |
#ifdef HAVE_WINGLK |
| 48 |
#include "WinGlk.h" |
| 49 |
#endif |
| 50 |
#endif |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
VerbEntry *vrbs; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
#define STACKSIZE 100 |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#ifdef CHECKOBJ |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
void checkobj(Aword *obj) { |
| 75 |
Aword oldobj; |
| 76 |
|
| 77 |
if (*obj != EOF) |
| 78 |
return; |
| 79 |
|
| 80 |
oldobj = EOF; |
| 81 |
for (cur.obj = OBJMIN; cur.obj <= OBJMAX; cur.obj++) { |
| 82 |
|
| 83 |
if (isHere(cur.obj) && possible()) |
| 84 |
if (oldobj == EOF) |
| 85 |
oldobj = cur.obj; |
| 86 |
else |
| 87 |
error(WANT); |
| 88 |
} |
| 89 |
|
| 90 |
if (oldobj == EOF) |
| 91 |
error(WANT); |
| 92 |
|
| 93 |
*obj = cur.obj = oldobj; |
| 94 |
output("($o)"); |
| 95 |
} |
| 96 |
#endif |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
static char *eventName(int event) { |
| 109 |
return stringAt(events[event].id); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
static void runPendingEvents(Stack theStack) |
| 115 |
{ |
| 116 |
int i; |
| 117 |
|
| 118 |
resetRules(); |
| 119 |
while (eventQueueTop != 0 && eventQueue[eventQueueTop-1].after == 0) { |
| 120 |
eventQueueTop--; |
| 121 |
if (isALocation(eventQueue[eventQueueTop].where)) |
| 122 |
current.location = eventQueue[eventQueueTop].where; |
| 123 |
else |
| 124 |
current.location = where(eventQueue[eventQueueTop].where, TRANSITIVE); |
| 125 |
if (traceSectionOption) { |
| 126 |
printf("\n<EVENT %s[%d] (at ", eventName(eventQueue[eventQueueTop].event), |
| 127 |
eventQueue[eventQueueTop].event); |
| 128 |
traceSay(current.location); |
| 129 |
printf(" [%d]):>\n", current.location); |
| 130 |
} |
| 131 |
interpret(events[eventQueue[eventQueueTop].event].code); |
| 132 |
if (stackDepth(theStack) != 0) |
| 133 |
syserr("Stack is not empty after event execution"); |
| 134 |
evaluateRules(rules); |
| 135 |
} |
| 136 |
|
| 137 |
for (i = 0; i<eventQueueTop; i++) |
| 138 |
eventQueue[i].after--; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static FILE *codfil; |
| 152 |
static char codfnm[256] = ""; |
| 153 |
static char txtfnm[256] = ""; |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
static int crcStart(char version[4]) { |
| 163 |
|
| 164 |
if (isPreAlpha5(version)) |
| 165 |
return sizeof(Pre3_0alpha5Header)/sizeof(Aword); |
| 166 |
else if (isPreBeta2(version)) |
| 167 |
return sizeof(Pre3_0beta2Header)/sizeof(Aword); |
| 168 |
else |
| 169 |
return sizeof(ACodeHeader)/sizeof(Aword); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static void readTemporaryHeader(ACodeHeader *tmphdr) { |
| 175 |
rewind(codfil); |
| 176 |
if (fread(tmphdr, sizeof(*tmphdr), 1, codfil) != 1) |
| 177 |
; |
| 178 |
rewind(codfil); |
| 179 |
if (strncmp((char *)tmphdr, "ALAN", 4) != 0) |
| 180 |
playererr("Not an Alan game file, does not start with \"ALAN\""); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static void reverseMemory() { |
| 186 |
if (littleEndian()) { |
| 187 |
if ((debugOption||traceSectionOption||traceInstructionOption) && !regressionTestOption) |
| 188 |
printf("<Hmm, this is a little-endian machine, fixing byte ordering...."); |
| 189 |
reverseACD(); |
| 190 |
if ((debugOption||traceSectionOption||traceInstructionOption) && !regressionTestOption) |
| 191 |
printf("OK.>\n"); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
static void setupHeader(ACodeHeader tmphdr) { |
| 198 |
if (isPreBeta2(tmphdr.version)) { |
| 199 |
header = duplicate(&memory[0], sizeof(ACodeHeader)); |
| 200 |
if (isPreAlpha5(tmphdr.version)) { |
| 201 |
header->ifids = 0; |
| 202 |
} |
| 203 |
header->prompt = 0; |
| 204 |
} else if (isPreBeta3(tmphdr.version)) { |
| 205 |
header = (ACodeHeader *) pointerTo(0); |
| 206 |
} else { |
| 207 |
header = pointerTo(0); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static void loadAndCheckMemory(ACodeHeader tmphdr, Aword crc, char err[]) { |
| 214 |
int i; |
| 215 |
|
| 216 |
if (memory == NULL) { |
| 217 |
memory = allocate(tmphdr.size*sizeof(Aword)); |
| 218 |
} |
| 219 |
|
| 220 |
memTop = fread(pointerTo(0), sizeof(Aword), tmphdr.size, codfil); |
| 221 |
if (memTop != tmphdr.size) |
| 222 |
syserr("Could not read all ACD code."); |
| 223 |
|
| 224 |
|
| 225 |
for (i = crcStart(tmphdr.version); i < memTop; i++) { |
| 226 |
crc += memory[i]&0xff; |
| 227 |
crc += (memory[i]>>8)&0xff; |
| 228 |
crc += (memory[i]>>16)&0xff; |
| 229 |
crc += (memory[i]>>24)&0xff; |
| 230 |
#ifdef CRCLOG |
| 231 |
printf("%6x\t%6lx\t%6lx\n", i, crc, memory[i]); |
| 232 |
#endif |
| 233 |
} |
| 234 |
if (crc != tmphdr.acdcrc) { |
| 235 |
sprintf(err, "Checksum error in Acode (.a3c) file (0x%lx instead of 0x%lx).", |
| 236 |
(unsigned long) crc, (unsigned long) tmphdr.acdcrc); |
| 237 |
if (!ignoreErrorOption) |
| 238 |
syserr(err); |
| 239 |
else { |
| 240 |
printf("%s%s%s", "<WARNING! ", err, " - Ignored, proceed at your own risk.>\n"); |
| 241 |
} |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static char *decodeState(int c) { |
| 248 |
static char state[2] = "\0\0"; |
| 249 |
switch (c) { |
| 250 |
case 0: return "."; |
| 251 |
case 'd': return "dev"; |
| 252 |
case 'a': return "alpha"; |
| 253 |
case 'b': return "beta"; |
| 254 |
default: |
| 255 |
state[0] = header->version[3]; |
| 256 |
return state; |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
|
| 261 |
char *decodedGameVersion(char version[]) { |
| 262 |
static char str[100]; |
| 263 |
sprintf(str, "%d.%d%s%d", |
| 264 |
(int)version[3], |
| 265 |
(int)version[2], |
| 266 |
decodeState(version[0]), |
| 267 |
(int)version[1]); |
| 268 |
return str; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
static void incompatibleDevelopmentVersion(ACodeHeader *header) { |
| 273 |
char str[500]; |
| 274 |
sprintf(str, "Incompatible version of ACODE program. Development versions always require exact match. Game is %ld.%ld%s%ld, interpreter %ld.%ld%s%ld!", |
| 275 |
(long)(header->version[0]), |
| 276 |
(long)(header->version[1]), |
| 277 |
decodeState(header->version[3]), |
| 278 |
(long)(header->version[2]), |
| 279 |
(long)alan.version.version, |
| 280 |
(long)alan.version.revision, |
| 281 |
alan.version.state, |
| 282 |
(long)alan.version.correction); |
| 283 |
apperr(str); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
static void incompatibleVersion(ACodeHeader *header) { |
| 289 |
char str[500]; |
| 290 |
sprintf(str, "Incompatible version of ACODE program. Game is %ld.%ld, interpreter %ld.%ld.", |
| 291 |
(long)(header->version[0]), |
| 292 |
(long)(header->version[1]), |
| 293 |
(long)alan.version.version, |
| 294 |
(long)alan.version.revision); |
| 295 |
apperr(str); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
static void alphaRunningLaterGame(char gameState) { |
| 301 |
printf("<WARNING! You are running an alpha interpreter, but the game is generated by a "); |
| 302 |
if (gameState == 'b') |
| 303 |
printf("beta"); |
| 304 |
else |
| 305 |
printf("release"); |
| 306 |
printf(" state compiler which was released later. This might cause the game to not work fully as intended. Look for an upgraded game file.>\n"); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
static void nonDevelopmentRunningDevelopmentStateGame(char version[]) { |
| 311 |
char errorMessage[200]; |
| 312 |
char versionString[100]; |
| 313 |
|
| 314 |
strcpy(errorMessage, "Games generated by a development state compiler"); |
| 315 |
sprintf(versionString, "(this game is v%d.%d.%d%s)", version[0], version[1], |
| 316 |
version[2], decodeState(version[3])); |
| 317 |
strcat(errorMessage, versionString); |
| 318 |
strcat(errorMessage, "can only be run with a matching interpreter. Look for a game file generated with an alpha, beta or release state compiler.>\n"); |
| 319 |
apperr(errorMessage); |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
void checkVersion(ACodeHeader *header) |
| 325 |
{ |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
char interpreterVersion[4]; |
| 340 |
bool developmentVersion; |
| 341 |
bool alphaVersion; |
| 342 |
int compareLength; |
| 343 |
char gameState = header->version[3]; |
| 344 |
|
| 345 |
|
| 346 |
interpreterVersion[0] = alan.version.version; |
| 347 |
interpreterVersion[1] = alan.version.revision; |
| 348 |
interpreterVersion[2] = alan.version.correction; |
| 349 |
interpreterVersion[3] = alan.version.state[0]; |
| 350 |
|
| 351 |
|
| 352 |
if ((debugOption && !regressionTestOption) || verboseOption) { |
| 353 |
if (debugOption) printf("<"); |
| 354 |
printf("Version of '%s' is %d.%d%s%d!", |
| 355 |
adventureFileName, |
| 356 |
(int)header->version[0], |
| 357 |
(int)header->version[1], |
| 358 |
decodeState(header->version[3]), |
| 359 |
(int)header->version[2]); |
| 360 |
if (debugOption) printf(">"); |
| 361 |
newline(); |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
developmentVersion = (strcmp(alan.version.state, "dev") == 0); |
| 366 |
alphaVersion = (strcmp(alan.version.state, "alpha") == 0); |
| 367 |
compareLength = (developmentVersion? 3 : 2); |
| 368 |
|
| 369 |
if (gameState == 'd' && !developmentVersion) |
| 370 |
|
| 371 |
nonDevelopmentRunningDevelopmentStateGame(header->version); |
| 372 |
else { |
| 373 |
|
| 374 |
if (memcmp(header->version, interpreterVersion, compareLength) != 0) { |
| 375 |
|
| 376 |
if (!ignoreErrorOption) { |
| 377 |
if (developmentVersion) |
| 378 |
incompatibleDevelopmentVersion(header); |
| 379 |
else |
| 380 |
incompatibleVersion(header); |
| 381 |
} else |
| 382 |
printf("<WARNING! Incompatible version of ACODE program.>\n"); |
| 383 |
} else if (developmentVersion && gameState != 'd') |
| 384 |
|
| 385 |
incompatibleDevelopmentVersion(header); |
| 386 |
else if (alphaVersion && gameState != 'a') { |
| 387 |
|
| 388 |
alphaRunningLaterGame(gameState); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
static void load(void) |
| 395 |
{ |
| 396 |
ACodeHeader tmphdr; |
| 397 |
Aword crc = 0; |
| 398 |
char err[100]; |
| 399 |
|
| 400 |
readTemporaryHeader(&tmphdr); |
| 401 |
checkVersion(&tmphdr); |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
if (littleEndian()) |
| 406 |
reverseHdr(&tmphdr); |
| 407 |
|
| 408 |
if (tmphdr.size <= sizeof(ACodeHeader)/sizeof(Aword)) |
| 409 |
syserr("Malformed game file. Too small."); |
| 410 |
|
| 411 |
loadAndCheckMemory(tmphdr, crc, err); |
| 412 |
|
| 413 |
reverseMemory(); |
| 414 |
setupHeader(tmphdr); |
| 415 |
|
| 416 |
} |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
static void checkDebug(void) |
| 421 |
{ |
| 422 |
|
| 423 |
if (!header->debug) { |
| 424 |
if (debugOption|traceSectionOption|traceInstructionOption) { |
| 425 |
printf("<Sorry, '%s' is not compiled for debug! Exiting.>\n", adventureFileName); |
| 426 |
terminate(0); |
| 427 |
} |
| 428 |
debugOption = false; |
| 429 |
traceSectionOption = false; |
| 430 |
traceInstructionOption = false; |
| 431 |
tracePushOption = false; |
| 432 |
} |
| 433 |
|
| 434 |
if (debugOption || regressionTestOption) |
| 435 |
srand(1); |
| 436 |
else |
| 437 |
srand(time(0)); |
| 438 |
} |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
static void initStaticData(void) |
| 443 |
{ |
| 444 |
|
| 445 |
dictionary = (DictionaryEntry *) pointerTo(header->dictionary); |
| 446 |
|
| 447 |
for (dictionarySize = 0; !isEndOfArray(&dictionary[dictionarySize]); dictionarySize++); |
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
if (header->instanceTableAddress == 0) |
| 456 |
syserr("Instance table pointer == 0"); |
| 457 |
instances = (InstanceEntry *) pointerTo(header->instanceTableAddress); |
| 458 |
instances--; |
| 459 |
|
| 460 |
|
| 461 |
if (header->classTableAddress == 0) |
| 462 |
syserr("Class table pointer == 0"); |
| 463 |
classes = (ClassEntry *) pointerTo(header->classTableAddress); |
| 464 |
classes--; |
| 465 |
|
| 466 |
if (header->containerTableAddress != 0) { |
| 467 |
containers = (ContainerEntry *) pointerTo(header->containerTableAddress); |
| 468 |
containers--; |
| 469 |
} |
| 470 |
|
| 471 |
if (header->eventTableAddress != 0) { |
| 472 |
events = (EventEntry *) pointerTo(header->eventTableAddress); |
| 473 |
events--; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
if (scores == NULL) |
| 478 |
scores = duplicate((Aword *) pointerTo(header->scores), header->scoreCount*sizeof(Aword)); |
| 479 |
else |
| 480 |
memcpy(scores, pointerTo(header->scores), header->scoreCount*sizeof(Aword)); |
| 481 |
|
| 482 |
if (literals == NULL) |
| 483 |
literals = allocate(sizeof(Aword)*(MAXPARAMS+1)); |
| 484 |
|
| 485 |
stxs = (SyntaxEntry *) pointerTo(header->syntaxTableAddress); |
| 486 |
vrbs = (VerbEntry *) pointerTo(header->verbTableAddress); |
| 487 |
msgs = (MessageEntry *) pointerTo(header->messageTableAddress); |
| 488 |
initRules(header->ruleTableAddress); |
| 489 |
|
| 490 |
if (header->pack) |
| 491 |
freq = (Aword *) pointerTo(header->freq); |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
static void initStrings(void) |
| 497 |
{ |
| 498 |
StringInitEntry *init; |
| 499 |
|
| 500 |
for (init = (StringInitEntry *) pointerTo(header->stringInitTable); !isEndOfArray(init); init++) |
| 501 |
setInstanceAttribute(init->instanceCode, init->attributeCode, toAptr(getStringFromFile(init->fpos, init->len))); |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
static Aint sizeOfAttributeData(void) |
| 506 |
{ |
| 507 |
int i; |
| 508 |
int size = 0; |
| 509 |
|
| 510 |
for (i=1; i<=header->instanceMax; i++) { |
| 511 |
AttributeEntry *attribute = pointerTo(instances[i].initialAttributes); |
| 512 |
while (!isEndOfArray(attribute)) { |
| 513 |
size += AwordSizeOf(AttributeEntry); |
| 514 |
attribute++; |
| 515 |
} |
| 516 |
size += 1; |
| 517 |
} |
| 518 |
|
| 519 |
if (size != header->attributesAreaSize |
| 520 |
&& (sizeof(AttributeHeaderEntry) == sizeof(AttributeEntry))) |
| 521 |
syserr("Attribute area size calculated wrong."); |
| 522 |
return size; |
| 523 |
} |
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
static AttributeEntry *initializeAttributes(int awordSize) |
| 528 |
{ |
| 529 |
Aword *attributeArea = allocate(awordSize*sizeof(Aword)); |
| 530 |
Aword *currentAttributeArea = attributeArea; |
| 531 |
int i; |
| 532 |
|
| 533 |
for (i=1; i<=header->instanceMax; i++) { |
| 534 |
AttributeHeaderEntry *originalAttribute = pointerTo(instances[i].initialAttributes); |
| 535 |
admin[i].attributes = (AttributeEntry *)currentAttributeArea; |
| 536 |
while (!isEndOfArray(originalAttribute)) { |
| 537 |
((AttributeEntry *)currentAttributeArea)->code = originalAttribute->code; |
| 538 |
((AttributeEntry *)currentAttributeArea)->value = originalAttribute->value; |
| 539 |
((AttributeEntry *)currentAttributeArea)->id = originalAttribute->id; |
| 540 |
currentAttributeArea += AwordSizeOf(AttributeEntry); |
| 541 |
originalAttribute++; |
| 542 |
} |
| 543 |
*((Aword*)currentAttributeArea) = EOF; |
| 544 |
currentAttributeArea += 1; |
| 545 |
} |
| 546 |
|
| 547 |
return (AttributeEntry *)attributeArea; |
| 548 |
} |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
static void initDynamicData(void) |
| 555 |
{ |
| 556 |
int instanceId; |
| 557 |
|
| 558 |
|
| 559 |
admin = (AdminEntry *)allocate((header->instanceMax+1)*sizeof(AdminEntry)); |
| 560 |
|
| 561 |
|
| 562 |
attributes = initializeAttributes(sizeOfAttributeData()); |
| 563 |
|
| 564 |
|
| 565 |
initStrings(); |
| 566 |
initSets((SetInitEntry*)pointerTo(header->setInitTable)); |
| 567 |
|
| 568 |
|
| 569 |
for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) |
| 570 |
admin[instanceId].location = instances[instanceId].initialLocation; |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
static void runInheritedInitialize(Aint theClass) { |
| 576 |
if (theClass == 0) return; |
| 577 |
runInheritedInitialize(classes[theClass].parent); |
| 578 |
if (classes[theClass].initialize) |
| 579 |
interpret(classes[theClass].initialize); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
static void runInitialize(Aint theInstance) { |
| 585 |
runInheritedInitialize(instances[theInstance].parent); |
| 586 |
if (instances[theInstance].initialize != 0) |
| 587 |
interpret(instances[theInstance].initialize); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
static void initializeInstances() { |
| 593 |
int instanceId; |
| 594 |
|
| 595 |
|
| 596 |
for (instanceId = 1; instanceId <= header->instanceMax; instanceId++) { |
| 597 |
current.instance = instanceId; |
| 598 |
runInitialize(instanceId); |
| 599 |
} |
| 600 |
} |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
static void start(Stack theStack) |
| 605 |
{ |
| 606 |
int startloc; |
| 607 |
|
| 608 |
current.tick = 0; |
| 609 |
current.location = startloc = where(HERO, TRANSITIVE); |
| 610 |
current.actor = HERO; |
| 611 |
current.score = 0; |
| 612 |
|
| 613 |
para(); |
| 614 |
if (traceSectionOption) |
| 615 |
printf("\n<INITIALIZE:>\n"); |
| 616 |
initializeInstances(); |
| 617 |
|
| 618 |
if (traceSectionOption) |
| 619 |
printf("\n<START:>\n"); |
| 620 |
interpret(header->start); |
| 621 |
para(); |
| 622 |
|
| 623 |
if (where(HERO, TRANSITIVE) == startloc) { |
| 624 |
if (traceSectionOption) { |
| 625 |
printf("<CURRENT LOCATION:>\n"); |
| 626 |
anyOutput = false; |
| 627 |
} |
| 628 |
look(); |
| 629 |
} |
| 630 |
resetAndEvaluateRules(rules, header->version, theStack); |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
static void openFiles(void) |
| 636 |
{ |
| 637 |
char str[256]; |
| 638 |
|
| 639 |
|
| 640 |
strcpy(codfnm, adventureFileName); |
| 641 |
if ((codfil = fopen(codfnm, READ_MODE)) == NULL) { |
| 642 |
strcpy(str, "Can't open adventure code file '"); |
| 643 |
strcat(str, codfnm); |
| 644 |
strcat(str, "'."); |
| 645 |
playererr(str); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
strcpy(txtfnm, adventureFileName); |
| 650 |
if ((textFile = fopen(txtfnm, READ_MODE)) == NULL) { |
| 651 |
strcpy(str, "Can't open adventure text data file '"); |
| 652 |
strcat(str, txtfnm); |
| 653 |
strcat(str, "'."); |
| 654 |
apperr(str); |
| 655 |
} |
| 656 |
|
| 657 |
|
| 658 |
if (transcriptOption) { |
| 659 |
startTranscript(); |
| 660 |
} |
| 661 |
if (commandLogOption) { |
| 662 |
startCommandLog(); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
static void init(Stack theStack) |
| 669 |
{ |
| 670 |
int i; |
| 671 |
|
| 672 |
|
| 673 |
eventQueueTop = 0; |
| 674 |
initStaticData(); |
| 675 |
initDynamicData(); |
| 676 |
initParsing(); |
| 677 |
checkDebug(); |
| 678 |
|
| 679 |
getPageSize(); |
| 680 |
|
| 681 |
|
| 682 |
for (i = 0; i < dictionarySize; i++) |
| 683 |
if (isConjunction(i)) { |
| 684 |
conjWord = i; |
| 685 |
break; |
| 686 |
} |
| 687 |
|
| 688 |
|
| 689 |
if (debugOption) |
| 690 |
debug(false, 0, 0); |
| 691 |
else |
| 692 |
clear(); |
| 693 |
|
| 694 |
start(theStack); |
| 695 |
} |
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
static bool traceActor(int theActor) |
| 701 |
{ |
| 702 |
if (traceSectionOption) { |
| 703 |
printf("\n<ACTOR "); |
| 704 |
traceSay(theActor); |
| 705 |
printf("[%d]", theActor); |
| 706 |
if (current.location != 0) { |
| 707 |
printf(" (at "); |
| 708 |
traceSay(current.location); |
| 709 |
} else |
| 710 |
printf(" (nowhere"); |
| 711 |
printf("[%d])", current.location); |
| 712 |
} |
| 713 |
return traceSectionOption; |
| 714 |
} |
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
static char *scriptName(int theActor, int theScript) |
| 719 |
{ |
| 720 |
ScriptEntry *scriptEntry = pointerTo(header->scriptTableAddress); |
| 721 |
|
| 722 |
while (theScript > 1) { |
| 723 |
scriptEntry++; |
| 724 |
theScript--; |
| 725 |
} |
| 726 |
return pointerTo(scriptEntry->id); |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
static void moveActor(int theActor) |
| 732 |
{ |
| 733 |
ScriptEntry *scr; |
| 734 |
StepEntry *step; |
| 735 |
Aint previousInstance = current.instance; |
| 736 |
|
| 737 |
current.actor = theActor; |
| 738 |
current.instance = theActor; |
| 739 |
current.location = where(theActor, TRANSITIVE); |
| 740 |
if (theActor == HERO) { |
| 741 |
|
| 742 |
if (setjmp(forfeitLabel) == 0) { |
| 743 |
parse(); |
| 744 |
capitalize = true; |
| 745 |
fail = false; |
| 746 |
} |
| 747 |
} else if (admin[theActor].script != 0) { |
| 748 |
for (scr = (ScriptEntry *) pointerTo(header->scriptTableAddress); !isEndOfArray(scr); scr++) { |
| 749 |
if (scr->code == admin[theActor].script) { |
| 750 |
|
| 751 |
step = (StepEntry *) pointerTo(scr->steps); |
| 752 |
step = (StepEntry *) &step[admin[theActor].step]; |
| 753 |
|
| 754 |
if (admin[theActor].waitCount > 0) { |
| 755 |
if (traceActor(theActor)) |
| 756 |
printf(", SCRIPT %s[%ld], STEP %ld, Waiting another %ld turns>\n", |
| 757 |
scriptName(theActor, admin[theActor].script), |
| 758 |
(long)admin[theActor].script, (long)admin[theActor].step+1, |
| 759 |
(long)admin[theActor].waitCount); |
| 760 |
admin[theActor].waitCount--; |
| 761 |
break; |
| 762 |
} |
| 763 |
|
| 764 |
if (step->exp != 0) { |
| 765 |
if (traceActor(theActor)) |
| 766 |
printf(", SCRIPT %s[%ld], STEP %ld, Evaluating:>\n", |
| 767 |
scriptName(theActor, admin[theActor].script), |
| 768 |
(long)admin[theActor].script, (long)admin[theActor].step+1); |
| 769 |
if (!evaluate(step->exp)) |
| 770 |
break; |
| 771 |
} |
| 772 |
|
| 773 |
admin[theActor].step++; |
| 774 |
if (!isEndOfArray(step+1) && (step+1)->after != 0) { |
| 775 |
admin[theActor].waitCount = evaluate((step+1)->after); |
| 776 |
} |
| 777 |
if (traceActor(theActor)) |
| 778 |
printf(", SCRIPT %s[%ld], STEP %ld, Executing:>\n", |
| 779 |
scriptName(theActor, admin[theActor].script), |
| 780 |
(long)admin[theActor].script, |
| 781 |
(long)admin[theActor].step); |
| 782 |
interpret(step->stms); |
| 783 |
step++; |
| 784 |
|
| 785 |
if (fail || (admin[theActor].step != 0 && isEndOfArray(step))) |
| 786 |
|
| 787 |
admin[theActor].script = 0; |
| 788 |
fail = false; |
| 789 |
break; |
| 790 |
} |
| 791 |
} |
| 792 |
if (isEndOfArray(scr)) |
| 793 |
syserr("Unknown actor script."); |
| 794 |
} else { |
| 795 |
if (traceActor(theActor)) { |
| 796 |
printf(", Idle>\n"); |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
current.instance = previousInstance; |
| 801 |
} |
| 802 |
|
| 803 |
#define RESTARTED (setjmp(restartLabel) != NO_JUMP_RETURN) |
| 804 |
#define ERROR_RETURNED (setjmp(returnLabel) != NO_JUMP_RETURN) |
| 805 |
|
| 806 |
|
| 807 |
void run(void) |
| 808 |
{ |
| 809 |
int i; |
| 810 |
static Stack theStack = NULL; |
| 811 |
|
| 812 |
openFiles(); |
| 813 |
load(); |
| 814 |
|
| 815 |
if ((debugOption && !regressionTestOption) || verboseOption) { |
| 816 |
if (!isPreBeta7(header->version)) { |
| 817 |
if (debugOption) printf("<"); |
| 818 |
printIFIDs(adventureName); |
| 819 |
if (debugOption) printf(">"); |
| 820 |
newline(); |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
if (RESTARTED) { |
| 825 |
deleteStack(theStack); |
| 826 |
} |
| 827 |
|
| 828 |
theStack = createStack(STACKSIZE); |
| 829 |
setInterpreterStack(theStack); |
| 830 |
|
| 831 |
initStateStack(); |
| 832 |
|
| 833 |
if (!ERROR_RETURNED) |
| 834 |
init(theStack); |
| 835 |
|
| 836 |
while (true) { |
| 837 |
if (debugOption) |
| 838 |
debug(false, 0, 0); |
| 839 |
|
| 840 |
if (stackDepth(theStack) != 0) |
| 841 |
syserr("Stack is not empty in main loop"); |
| 842 |
|
| 843 |
if (!current.meta) |
| 844 |
runPendingEvents(theStack); |
| 845 |
|
| 846 |
|
| 847 |
switch (setjmp(returnLabel)) { |
| 848 |
case NO_JUMP_RETURN: |
| 849 |
break; |
| 850 |
case ERROR_RETURN: |
| 851 |
forgetGameState(); |
| 852 |
forceNewPlayerInput(); |
| 853 |
break; |
| 854 |
case UNDO_RETURN: |
| 855 |
forceNewPlayerInput(); |
| 856 |
break; |
| 857 |
default: |
| 858 |
syserr("Unexpected longjmp() return value"); |
| 859 |
} |
| 860 |
|
| 861 |
recursionDepth = 0; |
| 862 |
|
| 863 |
|
| 864 |
rememberGameState(); |
| 865 |
current.meta = false; |
| 866 |
moveActor(header->theHero); |
| 867 |
|
| 868 |
if (gameStateChanged) |
| 869 |
rememberCommands(); |
| 870 |
else |
| 871 |
forgetGameState(); |
| 872 |
|
| 873 |
if (!current.meta) { |
| 874 |
current.tick++; |
| 875 |
|
| 876 |
|
| 877 |
resetAndEvaluateRules(rules, header->version, theStack); |
| 878 |
|
| 879 |
|
| 880 |
for (i = 1; i <= header->instanceMax; i++) |
| 881 |
if (i != header->theHero && isAActor(i)) { |
| 882 |
moveActor(i); |
| 883 |
resetAndEvaluateRules(rules, header->version, theStack); |
| 884 |
} |
| 885 |
} |
| 886 |
} |
| 887 |
} |