| 1 |
#include <string.h> |
| 2 |
|
| 3 |
#include "glk.h" |
| 4 |
#include "scott.h" |
| 5 |
|
| 6 |
#include "load_ti99_4a.h" |
| 7 |
#include "ti99_4a_terp.h" |
| 8 |
|
| 9 |
static ActionResultType PerformTI99Line(const uint8_t *action_line) |
| 10 |
{ |
| 11 |
if (action_line == NULL) |
| 12 |
return ACT_FAILURE; |
| 13 |
|
| 14 |
const uint8_t *ptr = action_line; |
| 15 |
int run_code = 0; |
| 16 |
int index = 0; |
| 17 |
ActionResultType result = ACT_FAILURE; |
| 18 |
int opcode, param; |
| 19 |
|
| 20 |
int try_index; |
| 21 |
int try[32]; |
| 22 |
|
| 23 |
try_index = 0; |
| 24 |
|
| 25 |
while (run_code == 0) { |
| 26 |
opcode = *(ptr++); |
| 27 |
|
| 28 |
switch (opcode) { |
| 29 |
case 183: |
| 30 |
#ifdef DEBUG_ACTIONS |
| 31 |
debug_print("Does the player carry %s?\n", Items[*ptr].Text); |
| 32 |
#endif |
| 33 |
if (Items[*(ptr++)].Location != CARRIED) { |
| 34 |
run_code = 1; |
| 35 |
result = ACT_FAILURE; |
| 36 |
} |
| 37 |
break; |
| 38 |
|
| 39 |
case 184: |
| 40 |
#ifdef DEBUG_ACTIONS |
| 41 |
debug_print("Is %s in location?\n", Items[*ptr].Text); |
| 42 |
#endif |
| 43 |
if (Items[*(ptr++)].Location != MyLoc) { |
| 44 |
run_code = 1; |
| 45 |
result = ACT_FAILURE; |
| 46 |
} |
| 47 |
|
| 48 |
break; |
| 49 |
|
| 50 |
case 185: |
| 51 |
#ifdef DEBUG_ACTIONS |
| 52 |
debug_print("Is %s held or in location?\n", Items[*ptr].Text); |
| 53 |
#endif |
| 54 |
if (Items[*ptr].Location != CARRIED && Items[*ptr].Location != MyLoc) { |
| 55 |
run_code = 1; |
| 56 |
result = ACT_FAILURE; |
| 57 |
} |
| 58 |
ptr++; |
| 59 |
break; |
| 60 |
|
| 61 |
case 186: |
| 62 |
#ifdef DEBUG_ACTIONS |
| 63 |
debug_print("Is %s NOT in location?\n", Items[*ptr].Text); |
| 64 |
#endif |
| 65 |
if (Items[*(ptr++)].Location == MyLoc) { |
| 66 |
run_code = 1; |
| 67 |
result = ACT_FAILURE; |
| 68 |
} |
| 69 |
break; |
| 70 |
|
| 71 |
case 187: |
| 72 |
#ifdef DEBUG_ACTIONS |
| 73 |
debug_print("Does the player NOT carry %s?\n", Items[*ptr].Text); |
| 74 |
#endif |
| 75 |
if (Items[*(ptr++)].Location == CARRIED) { |
| 76 |
run_code = 1; |
| 77 |
result = ACT_FAILURE; |
| 78 |
} |
| 79 |
break; |
| 80 |
|
| 81 |
case 188: |
| 82 |
#ifdef DEBUG_ACTIONS |
| 83 |
debug_print("Is %s neither carried nor in room?\n", Items[*ptr].Text); |
| 84 |
#endif |
| 85 |
|
| 86 |
if (Items[*ptr].Location == CARRIED || Items[*ptr].Location == MyLoc) { |
| 87 |
run_code = 1; |
| 88 |
result = ACT_FAILURE; |
| 89 |
} |
| 90 |
ptr++; |
| 91 |
break; |
| 92 |
|
| 93 |
case 189: |
| 94 |
#ifdef DEBUG_ACTIONS |
| 95 |
debug_print("Is %s (%d) in play?\n", Items[*ptr].Text, dv); |
| 96 |
#endif |
| 97 |
if (Items[*(ptr++)].Location == 0) { |
| 98 |
run_code = 1; |
| 99 |
result = ACT_FAILURE; |
| 100 |
} |
| 101 |
break; |
| 102 |
|
| 103 |
case 190: |
| 104 |
#ifdef DEBUG_ACTIONS |
| 105 |
debug_print("Is %s NOT in play?\n", Items[*ptr].Text); |
| 106 |
#endif |
| 107 |
if (Items[*(ptr++)].Location != 0) { |
| 108 |
run_code = 1; |
| 109 |
result = ACT_FAILURE; |
| 110 |
} |
| 111 |
break; |
| 112 |
|
| 113 |
case 191: |
| 114 |
#ifdef DEBUG_ACTIONS |
| 115 |
debug_print("Is location %s?\n", Rooms[*ptr].Text); |
| 116 |
#endif |
| 117 |
if (MyLoc != *(ptr++)) { |
| 118 |
run_code = 1; |
| 119 |
result = ACT_FAILURE; |
| 120 |
} |
| 121 |
break; |
| 122 |
|
| 123 |
case 192: |
| 124 |
#ifdef DEBUG_ACTIONS |
| 125 |
debug_print("Is location NOT %s?\n", Rooms[*ptr].Text); |
| 126 |
#endif |
| 127 |
if (MyLoc == *(ptr++)) { |
| 128 |
run_code = 1; |
| 129 |
result = ACT_FAILURE; |
| 130 |
} |
| 131 |
break; |
| 132 |
|
| 133 |
case 193: |
| 134 |
#ifdef DEBUG_ACTIONS |
| 135 |
debug_print("Is bitflag %d set?\n", *ptr); |
| 136 |
#endif |
| 137 |
if ((BitFlags & (1 << *(ptr++))) == 0) { |
| 138 |
run_code = 1; |
| 139 |
result = ACT_FAILURE; |
| 140 |
} |
| 141 |
break; |
| 142 |
|
| 143 |
case 194: |
| 144 |
#ifdef DEBUG_ACTIONS |
| 145 |
debug_print("Is bitflag %d NOT set?\n", *ptr); |
| 146 |
#endif |
| 147 |
if (BitFlags & (1 << *(ptr++))) { |
| 148 |
run_code = 1; |
| 149 |
result = ACT_FAILURE; |
| 150 |
} |
| 151 |
break; |
| 152 |
|
| 153 |
case 195: |
| 154 |
#ifdef DEBUG_ACTIONS |
| 155 |
debug_print("Does the player carry anything?\n"); |
| 156 |
#endif |
| 157 |
if (CountCarried() == 0) { |
| 158 |
run_code = 1; |
| 159 |
result = ACT_FAILURE; |
| 160 |
} |
| 161 |
break; |
| 162 |
|
| 163 |
case 196: |
| 164 |
#ifdef DEBUG_ACTIONS |
| 165 |
debug_print("Does the player carry nothing?\n"); |
| 166 |
#endif |
| 167 |
if (CountCarried()) { |
| 168 |
run_code = 1; |
| 169 |
result = ACT_FAILURE; |
| 170 |
} |
| 171 |
break; |
| 172 |
|
| 173 |
case 197: |
| 174 |
#ifdef DEBUG_ACTIONS |
| 175 |
debug_print("Is CurrentCounter <= %d?\n", *ptr); |
| 176 |
#endif |
| 177 |
if (CurrentCounter > *(ptr++)) { |
| 178 |
run_code = 1; |
| 179 |
result = ACT_FAILURE; |
| 180 |
} |
| 181 |
break; |
| 182 |
|
| 183 |
case 198: |
| 184 |
#ifdef DEBUG_ACTIONS |
| 185 |
debug_print("Is CurrentCounter > %d?\n", *ptr); |
| 186 |
#endif |
| 187 |
if (CurrentCounter <= *(ptr++)) { |
| 188 |
run_code = 1; |
| 189 |
result = ACT_FAILURE; |
| 190 |
} |
| 191 |
break; |
| 192 |
|
| 193 |
case 199: |
| 194 |
#ifdef DEBUG_ACTIONS |
| 195 |
debug_print("Is current counter == %d?\n", *ptr); |
| 196 |
#endif |
| 197 |
if (CurrentCounter != *(ptr++)) { |
| 198 |
run_code = 1; |
| 199 |
result = ACT_FAILURE; |
| 200 |
} |
| 201 |
break; |
| 202 |
|
| 203 |
case 200: |
| 204 |
#ifdef DEBUG_ACTIONS |
| 205 |
debug_print("Is %s still in initial room?\n", Items[*ptr].Text); |
| 206 |
#endif |
| 207 |
if (Items[*ptr].Location != Items[*ptr].InitialLoc) { |
| 208 |
run_code = 1; |
| 209 |
result = ACT_FAILURE; |
| 210 |
} |
| 211 |
ptr++; |
| 212 |
break; |
| 213 |
|
| 214 |
case 201: |
| 215 |
#ifdef DEBUG_ACTIONS |
| 216 |
debug_print("Has %s been moved?\n", Items[*ptr].Text); |
| 217 |
#endif |
| 218 |
if (Items[*ptr].Location == Items[*ptr].InitialLoc) { |
| 219 |
run_code = 1; |
| 220 |
result = ACT_FAILURE; |
| 221 |
} |
| 222 |
ptr++; |
| 223 |
break; |
| 224 |
|
| 225 |
case 212: |
| 226 |
glk_window_clear(Bottom); |
| 227 |
break; |
| 228 |
|
| 229 |
case 214: |
| 230 |
AutoInventory = 1; |
| 231 |
break; |
| 232 |
|
| 233 |
case 215: |
| 234 |
AutoInventory = 0; |
| 235 |
break; |
| 236 |
|
| 237 |
case 216: |
| 238 |
case 217: |
| 239 |
break; |
| 240 |
|
| 241 |
case 218: |
| 242 |
if (try_index >= 32) { |
| 243 |
Fatal("ERROR Hit upper limit on try method.\n"); |
| 244 |
} |
| 245 |
try[try_index++] = ptr - action_line + *ptr; |
| 246 |
ptr++; |
| 247 |
break; |
| 248 |
|
| 249 |
case 219: |
| 250 |
if (CountCarried() >= GameHeader.MaxCarry) { |
| 251 |
Output(sys[YOURE_CARRYING_TOO_MUCH]); |
| 252 |
run_code = 1; |
| 253 |
result = ACT_FAILURE; |
| 254 |
break; |
| 255 |
} else { |
| 256 |
Items[*ptr].Location = CARRIED; |
| 257 |
} |
| 258 |
ptr++; |
| 259 |
break; |
| 260 |
|
| 261 |
case 220: |
| 262 |
#ifdef DEBUG_ACTIONS |
| 263 |
debug_print("item %d (\"%s\") is now in location.\n", *ptr, |
| 264 |
Items[*ptr].Text); |
| 265 |
#endif |
| 266 |
Items[*(ptr++)].Location = MyLoc; |
| 267 |
should_look_in_transcript = 1; |
| 268 |
break; |
| 269 |
|
| 270 |
case 221: |
| 271 |
GoTo(*(ptr++)); |
| 272 |
break; |
| 273 |
|
| 274 |
case 222: |
| 275 |
#ifdef DEBUG_ACTIONS |
| 276 |
debug_print( |
| 277 |
"Item %d (%s) is removed from the game (put in room 0).\n", |
| 278 |
*ptr, Items[*ptr].Text); |
| 279 |
#endif |
| 280 |
Items[*(ptr++)].Location = 0; |
| 281 |
break; |
| 282 |
|
| 283 |
case 223: |
| 284 |
SetDark(); |
| 285 |
break; |
| 286 |
|
| 287 |
case 224: |
| 288 |
SetLight(); |
| 289 |
break; |
| 290 |
|
| 291 |
case 225: |
| 292 |
SetBitFlag(*(ptr++)); |
| 293 |
break; |
| 294 |
|
| 295 |
case 226: |
| 296 |
ClearBitFlag(*(ptr++)); |
| 297 |
break; |
| 298 |
case 227: |
| 299 |
SetBitFlag(0); |
| 300 |
break; |
| 301 |
|
| 302 |
case 228: |
| 303 |
ClearBitFlag(0); |
| 304 |
break; |
| 305 |
|
| 306 |
case 229: |
| 307 |
#ifdef DEBUG_ACTIONS |
| 308 |
debug_print("Player is dead\n"); |
| 309 |
#endif |
| 310 |
PlayerIsDead(); |
| 311 |
DoneIt(); |
| 312 |
result = ACT_GAMEOVER; |
| 313 |
break; |
| 314 |
|
| 315 |
case 230: |
| 316 |
param = *(ptr++); |
| 317 |
PutItemAInRoomB(*(ptr++), param); |
| 318 |
break; |
| 319 |
|
| 320 |
case 231: |
| 321 |
DoneIt(); |
| 322 |
return ACT_GAMEOVER; |
| 323 |
|
| 324 |
case 232: |
| 325 |
if (PrintScore() == 1) |
| 326 |
return ACT_GAMEOVER; |
| 327 |
StopTime = 2; |
| 328 |
break; |
| 329 |
|
| 330 |
case 233: |
| 331 |
ListInventory(0); |
| 332 |
StopTime = 2; |
| 333 |
break; |
| 334 |
|
| 335 |
case 234: |
| 336 |
GameHeader.LightTime = LightRefill; |
| 337 |
Items[LIGHT_SOURCE].Location = CARRIED; |
| 338 |
ClearBitFlag(LIGHTOUTBIT); |
| 339 |
break; |
| 340 |
|
| 341 |
case 235: |
| 342 |
SaveGame(); |
| 343 |
StopTime = 2; |
| 344 |
break; |
| 345 |
|
| 346 |
case 236: |
| 347 |
param = *(ptr++); |
| 348 |
SwapItemLocations(param, *(ptr++)); |
| 349 |
break; |
| 350 |
|
| 351 |
case 237: |
| 352 |
#ifdef DEBUG_ACTIONS |
| 353 |
fprintf(stderr, |
| 354 |
"Player now carries item %d (%s).\n", |
| 355 |
*ptr, Items[*ptr].Text); |
| 356 |
#endif |
| 357 |
Items[*(ptr++)].Location = CARRIED; |
| 358 |
break; |
| 359 |
|
| 360 |
case 238: |
| 361 |
param = *(ptr++); |
| 362 |
MoveItemAToLocOfItemB(param, *(ptr++)); |
| 363 |
break; |
| 364 |
|
| 365 |
case 239: |
| 366 |
break; |
| 367 |
|
| 368 |
case 240: |
| 369 |
Look(); |
| 370 |
should_look_in_transcript = 1; |
| 371 |
break; |
| 372 |
|
| 373 |
case 241: |
| 374 |
break; |
| 375 |
|
| 376 |
case 242: |
| 377 |
CurrentCounter++; |
| 378 |
break; |
| 379 |
|
| 380 |
case 243: |
| 381 |
if (CurrentCounter >= 1) |
| 382 |
CurrentCounter--; |
| 383 |
break; |
| 384 |
|
| 385 |
case 244: |
| 386 |
OutputNumber(CurrentCounter); |
| 387 |
Output(" "); |
| 388 |
break; |
| 389 |
|
| 390 |
case 245: |
| 391 |
#ifdef DEBUG_ACTIONS |
| 392 |
debug_print("CurrentCounter is set to %d.\n", dv); |
| 393 |
#endif |
| 394 |
CurrentCounter = *(ptr++); |
| 395 |
break; |
| 396 |
|
| 397 |
case 246: |
| 398 |
#ifdef DEBUG_ACTIONS |
| 399 |
fprintf(stderr, |
| 400 |
"%d is added to currentCounter. Result: %d\n", |
| 401 |
*ptr, CurrentCounter + *ptr); |
| 402 |
#endif |
| 403 |
CurrentCounter += *(ptr++); |
| 404 |
break; |
| 405 |
|
| 406 |
case 247: |
| 407 |
CurrentCounter -= *(ptr++); |
| 408 |
if (CurrentCounter < -1) |
| 409 |
CurrentCounter = -1; |
| 410 |
break; |
| 411 |
|
| 412 |
case 248: |
| 413 |
GoToStoredLoc(); |
| 414 |
break; |
| 415 |
|
| 416 |
case 249: |
| 417 |
SwapLocAndRoomflag(*(ptr++)); |
| 418 |
break; |
| 419 |
|
| 420 |
case 250: |
| 421 |
SwapCounters(*(ptr++)); |
| 422 |
break; |
| 423 |
|
| 424 |
case 251: |
| 425 |
PrintNoun(); |
| 426 |
break; |
| 427 |
|
| 428 |
case 252: |
| 429 |
PrintNoun(); |
| 430 |
Output("\n"); |
| 431 |
break; |
| 432 |
|
| 433 |
case 253: |
| 434 |
Output("\n"); |
| 435 |
break; |
| 436 |
|
| 437 |
case 254: |
| 438 |
Delay(1); |
| 439 |
break; |
| 440 |
|
| 441 |
case 255: |
| 442 |
result = 0; |
| 443 |
run_code = 1; |
| 444 |
try_index = 0; |
| 445 |
break; |
| 446 |
|
| 447 |
default: |
| 448 |
if (opcode <= 182 && opcode <= GameHeader.NumMessages + 1) { |
| 449 |
PrintMessage(opcode); |
| 450 |
} else { |
| 451 |
debug_print("Unknown action %d [Param begins %d %d]\n", opcode, action_line[ptr - action_line], action_line[ptr - action_line + 1]); |
| 452 |
break; |
| 453 |
} |
| 454 |
break; |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
if (run_code == 1 && try_index > 0) { |
| 459 |
if (opcode == 0xff) { |
| 460 |
run_code = 1; |
| 461 |
} else { |
| 462 |
|
| 463 |
|
| 464 |
index = try[try_index - 1]; |
| 465 |
|
| 466 |
try_index -= 1; |
| 467 |
try[try_index] = 0; |
| 468 |
run_code = 0; |
| 469 |
ptr = action_line + index; |
| 470 |
} |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
return result; |
| 475 |
} |
| 476 |
|
| 477 |
void RunImplicitTI99Actions(void) |
| 478 |
{ |
| 479 |
int probability; |
| 480 |
uint8_t *ptr; |
| 481 |
int loop_flag; |
| 482 |
|
| 483 |
ptr = ti99_implicit_actions; |
| 484 |
loop_flag = 0; |
| 485 |
|
| 486 |
|
| 487 |
if (*ptr == 0x0) |
| 488 |
loop_flag = 1; |
| 489 |
|
| 490 |
while (loop_flag == 0) { |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
probability = ptr[0]; |
| 498 |
|
| 499 |
if (RandomPercent(probability)) |
| 500 |
PerformTI99Line(ptr + 2); |
| 501 |
|
| 502 |
if (ptr[1] == 0 || ptr - ti99_implicit_actions >= ti99_implicit_extent) |
| 503 |
loop_flag = 1; |
| 504 |
|
| 505 |
|
| 506 |
ptr += 1 + ptr[1]; |
| 507 |
} |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
ExplicitResultType RunExplicitTI99Actions(int verb_num, int noun_num) |
| 512 |
{ |
| 513 |
uint8_t *p; |
| 514 |
ExplicitResultType flag = ER_NO_RESULT; |
| 515 |
int match = 0; |
| 516 |
ActionResultType runcode; |
| 517 |
|
| 518 |
p = VerbActionOffsets[verb_num]; |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
while (flag == ER_NO_RESULT) { |
| 524 |
|
| 525 |
if (p != NULL && (p[0] == noun_num || p[0] == 0)) { |
| 526 |
match = 1; |
| 527 |
runcode = PerformTI99Line(p + 2); |
| 528 |
|
| 529 |
if (runcode == ACT_SUCCESS) { |
| 530 |
return ER_SUCCESS; |
| 531 |
} else { |
| 532 |
if (p[1] == 0) |
| 533 |
flag = ER_RAN_ALL_LINES; |
| 534 |
else |
| 535 |
p += 1 + p[1]; |
| 536 |
} |
| 537 |
} else { |
| 538 |
if (p == NULL || p[1] == 0) |
| 539 |
flag = ER_RAN_ALL_LINES_NO_MATCH; |
| 540 |
else |
| 541 |
p += 1 + p[1]; |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
if (match) |
| 546 |
flag = ER_RAN_ALL_LINES; |
| 547 |
|
| 548 |
return flag; |
| 549 |
} |