| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "exe.h" |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
#include <time.h> |
| 13 |
|
| 14 |
|
| 15 |
#include <strings.h> |
| 16 |
|
| 17 |
#include "types.h" |
| 18 |
#include "sysdep.h" |
| 19 |
|
| 20 |
#include "lists.h" |
| 21 |
#include "state.h" |
| 22 |
#include "syserr.h" |
| 23 |
#include "term.h" |
| 24 |
#include "utils.h" |
| 25 |
#include "instance.h" |
| 26 |
#include "inter.h" |
| 27 |
#include "decode.h" |
| 28 |
#include "save.h" |
| 29 |
#include "memory.h" |
| 30 |
#include "output.h" |
| 31 |
#include "score.h" |
| 32 |
#include "event.h" |
| 33 |
#include "current.h" |
| 34 |
#include "word.h" |
| 35 |
#include "msg.h" |
| 36 |
#include "actor.h" |
| 37 |
#include "options.h" |
| 38 |
#include "args.h" |
| 39 |
|
| 40 |
|
| 41 |
#ifdef USE_READLINE |
| 42 |
#include "readline.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifdef HAVE_GLK |
| 46 |
#include "glk.h" |
| 47 |
#define MAP_STDIO_TO_GLK |
| 48 |
#include "glkio.h" |
| 49 |
#endif |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
FILE *textFile; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
jmp_buf restartLabel; |
| 59 |
jmp_buf returnLabel; |
| 60 |
jmp_buf forfeitLabel; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define WIDTH 80 |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
static char transcriptFileName[256] = ""; |
| 70 |
static char commandLogFileName[256] = ""; |
| 71 |
|
| 72 |
|
| 73 |
void setStyle(int style) |
| 74 |
{ |
| 75 |
#ifdef HAVE_GLK |
| 76 |
switch (style) { |
| 77 |
case NORMAL_STYLE: glk_set_style(style_Normal); break; |
| 78 |
case EMPHASIZED_STYLE: glk_set_style(style_Emphasized); break; |
| 79 |
case PREFORMATTED_STYLE: glk_set_style(style_Preformatted); break; |
| 80 |
case ALERT_STYLE: glk_set_style(style_Alert); break; |
| 81 |
case QUOTE_STYLE: glk_set_style(style_BlockQuote); break; |
| 82 |
} |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
void print(Aword fpos, Aword len) |
| 88 |
{ |
| 89 |
char str[2*WIDTH]; |
| 90 |
int outlen = 0; |
| 91 |
int ch = 0; |
| 92 |
int i; |
| 93 |
long savfp = 0; |
| 94 |
static bool printFlag = false; |
| 95 |
bool savedPrintFlag = printFlag; |
| 96 |
void *info = NULL; |
| 97 |
|
| 98 |
|
| 99 |
if (len == 0) return; |
| 100 |
|
| 101 |
if (isHere(HERO, true)) { |
| 102 |
if (printFlag) { |
| 103 |
|
| 104 |
if (header->pack) |
| 105 |
info = pushDecode(); |
| 106 |
else |
| 107 |
savfp = ftell(textFile); |
| 108 |
} |
| 109 |
printFlag = true; |
| 110 |
|
| 111 |
|
| 112 |
fseek(textFile, fpos+header->stringOffset, 0); |
| 113 |
|
| 114 |
if (header->pack) |
| 115 |
startDecoding(); |
| 116 |
for (outlen = 0; outlen != len; outlen = outlen + strlen(str)) { |
| 117 |
|
| 118 |
for (i = 0; i <= WIDTH || (i > WIDTH && ch != ' '); i++) { |
| 119 |
if (outlen + i == len) |
| 120 |
break; |
| 121 |
if (header->pack) |
| 122 |
ch = decodeChar(); |
| 123 |
else |
| 124 |
ch = getc(textFile); |
| 125 |
if (ch == EOFChar) |
| 126 |
break; |
| 127 |
str[i] = ch; |
| 128 |
} |
| 129 |
str[i] = '\0'; |
| 130 |
output(str); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
printFlag = savedPrintFlag; |
| 135 |
if (printFlag) { |
| 136 |
if (header->pack) |
| 137 |
popDecode(info); |
| 138 |
else |
| 139 |
fseek(textFile, savfp, 0); |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
void sys(Aword fpos, Aword len) |
| 147 |
{ |
| 148 |
char *command; |
| 149 |
|
| 150 |
command = getStringFromFile(fpos, len); |
| 151 |
|
| 152 |
#ifndef GARGLK |
| 153 |
if (system(command) == -1) |
| 154 |
; |
| 155 |
#endif |
| 156 |
deallocate(command); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
char *getStringFromFile(Aword fpos, Aword len) |
| 162 |
{ |
| 163 |
char *buf = allocate(len+1); |
| 164 |
char *bufp = buf; |
| 165 |
|
| 166 |
|
| 167 |
fseek(textFile, fpos+header->stringOffset, 0); |
| 168 |
|
| 169 |
if (header->pack) |
| 170 |
startDecoding(); |
| 171 |
while (len--) |
| 172 |
if (header->pack) |
| 173 |
*(bufp++) = decodeChar(); |
| 174 |
else |
| 175 |
*(bufp++) = getc(textFile); |
| 176 |
|
| 177 |
|
| 178 |
*bufp = '\0'; |
| 179 |
|
| 180 |
return buf; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
void score(Aword sc) |
| 187 |
{ |
| 188 |
if (sc == 0) { |
| 189 |
ParameterArray messageParameters = newParameterArray(); |
| 190 |
addParameterForInteger(messageParameters, current.score); |
| 191 |
addParameterForInteger(messageParameters, header->maximumScore); |
| 192 |
addParameterForInteger(messageParameters, current.tick); |
| 193 |
printMessageWithParameters(M_SCORE, messageParameters); |
| 194 |
freeParameterArray(messageParameters); |
| 195 |
} else { |
| 196 |
current.score += scores[sc-1]; |
| 197 |
scores[sc-1] = 0; |
| 198 |
gameStateChanged = true; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
void visits(Aword v) |
| 205 |
{ |
| 206 |
current.visits = v; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
static void sayUndoneCommand(char *words) { |
| 212 |
static Parameter *messageParameters = NULL; |
| 213 |
messageParameters = ensureParameterArrayAllocated(messageParameters); |
| 214 |
|
| 215 |
current.location = where(HERO, DIRECT); |
| 216 |
clearParameterArray(messageParameters); |
| 217 |
addParameterForString(&messageParameters[0], words); |
| 218 |
setEndOfArray(&messageParameters[1]); |
| 219 |
printMessageWithParameters(M_UNDONE, messageParameters); |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
void undo(void) { |
| 225 |
forgetGameState(); |
| 226 |
if (anySavedState()) { |
| 227 |
recallGameState(); |
| 228 |
sayUndoneCommand(recreatePlayerCommand()); |
| 229 |
} else { |
| 230 |
printMessage(M_NO_UNDO); |
| 231 |
} |
| 232 |
longjmp(returnLabel, UNDO_RETURN); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
void quitGame(void) |
| 238 |
{ |
| 239 |
char buf[80]; |
| 240 |
|
| 241 |
current.location = where(HERO, DIRECT); |
| 242 |
para(); |
| 243 |
while (true) { |
| 244 |
col = 1; |
| 245 |
statusline(); |
| 246 |
printMessage(M_QUITACTION); |
| 247 |
#ifdef USE_READLINE |
| 248 |
if (!readline(buf)) terminate(0); |
| 249 |
#else |
| 250 |
if (gets(buf) == NULL) terminate(0); |
| 251 |
#endif |
| 252 |
if (strcasecmp(buf, "restart") == 0) |
| 253 |
longjmp(restartLabel, true); |
| 254 |
else if (strcasecmp(buf, "restore") == 0) { |
| 255 |
restore(); |
| 256 |
return; |
| 257 |
} else if (strcasecmp(buf, "quit") == 0) { |
| 258 |
terminate(0); |
| 259 |
} else if (strcasecmp(buf, "undo") == 0) { |
| 260 |
if (gameStateChanged) { |
| 261 |
rememberCommands(); |
| 262 |
rememberGameState(); |
| 263 |
undo(); |
| 264 |
} else { |
| 265 |
if (anySavedState()) { |
| 266 |
recallGameState(); |
| 267 |
sayUndoneCommand(playerWordsAsCommandString()); |
| 268 |
} else |
| 269 |
printMessage(M_NO_UNDO); |
| 270 |
longjmp(returnLabel, UNDO_RETURN); |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
syserr("Fallthrough in QUIT"); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
void restartGame(void) |
| 281 |
{ |
| 282 |
Aint previousLocation = current.location; |
| 283 |
|
| 284 |
current.location = where(HERO, DIRECT); |
| 285 |
para(); |
| 286 |
if (confirm(M_REALLY)) { |
| 287 |
longjmp(restartLabel, true); |
| 288 |
} |
| 289 |
current.location = previousLocation; |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
void cancelEvent(Aword theEvent) |
| 296 |
{ |
| 297 |
int i; |
| 298 |
|
| 299 |
for (i = eventQueueTop-1; i>=0; i--) |
| 300 |
if (eventQueue[i].event == theEvent) { |
| 301 |
while (i < eventQueueTop-1) { |
| 302 |
eventQueue[i].event = eventQueue[i+1].event; |
| 303 |
eventQueue[i].after = eventQueue[i+1].after; |
| 304 |
eventQueue[i].where = eventQueue[i+1].where; |
| 305 |
i++; |
| 306 |
} |
| 307 |
eventQueueTop--; |
| 308 |
return; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
static void increaseEventQueue(void) |
| 315 |
{ |
| 316 |
eventQueue = realloc(eventQueue, (eventQueueTop+2)*sizeof(EventQueueEntry)); |
| 317 |
if (eventQueue == NULL) syserr("Out of memory in increaseEventQueue()"); |
| 318 |
|
| 319 |
eventQueueSize = eventQueueTop + 2; |
| 320 |
} |
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
static void moveEvent(int to, int from) { |
| 325 |
eventQueue[to].event = eventQueue[from].event; |
| 326 |
eventQueue[to].after = eventQueue[from].after; |
| 327 |
eventQueue[to].where = eventQueue[from].where; |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
void schedule(Aword event, Aword where, Aword after) |
| 333 |
{ |
| 334 |
int i; |
| 335 |
|
| 336 |
if (event == 0) syserr("NULL event"); |
| 337 |
|
| 338 |
cancelEvent(event); |
| 339 |
|
| 340 |
if (eventQueue == NULL || eventQueueTop == eventQueueSize) |
| 341 |
increaseEventQueue(); |
| 342 |
|
| 343 |
|
| 344 |
for (i = eventQueueTop; i >= 1 && eventQueue[i-1].after <= after; i--) { |
| 345 |
moveEvent(i, i-1); |
| 346 |
} |
| 347 |
|
| 348 |
eventQueue[i].after = after; |
| 349 |
eventQueue[i].where = where; |
| 350 |
eventQueue[i].event = event; |
| 351 |
eventQueueTop++; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
Aptr concat(Aptr as1, Aptr as2) |
| 358 |
{ |
| 359 |
char *s1 = fromAptr(as1); |
| 360 |
char *s2 = fromAptr(as2); |
| 361 |
char *result = allocate(strlen((char*)s1)+strlen((char*)s2)+1); |
| 362 |
strcpy(result, s1); |
| 363 |
strcat(result, s2); |
| 364 |
return toAptr(result); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static char *stripCharsFromStringForwards(int count, char *initialString, char **theRest) |
| 370 |
{ |
| 371 |
int stripPosition; |
| 372 |
char *strippedString; |
| 373 |
char *rest; |
| 374 |
|
| 375 |
if (count > strlen(initialString)) |
| 376 |
stripPosition = strlen(initialString); |
| 377 |
else |
| 378 |
stripPosition = count; |
| 379 |
rest = strdup(&initialString[stripPosition]); |
| 380 |
strippedString = strdup(initialString); |
| 381 |
strippedString[stripPosition] = '\0'; |
| 382 |
*theRest = rest; |
| 383 |
return strippedString; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
static char *stripCharsFromStringBackwards(Aint count, char *initialString, char **theRest) { |
| 388 |
int stripPosition; |
| 389 |
char *strippedString; |
| 390 |
char *rest; |
| 391 |
|
| 392 |
if (count > strlen(initialString)) |
| 393 |
stripPosition = 0; |
| 394 |
else |
| 395 |
stripPosition = strlen(initialString)-count; |
| 396 |
strippedString = strdup(&initialString[stripPosition]); |
| 397 |
rest = strdup(initialString); |
| 398 |
rest[stripPosition] = '\0'; |
| 399 |
*theRest = rest; |
| 400 |
return strippedString; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
static int countLeadingBlanks(char *string, int position) { |
| 406 |
static char blanks[] = " "; |
| 407 |
return strspn(&string[position], blanks); |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static int skipWordForwards(char *string, int position) |
| 413 |
{ |
| 414 |
char separators[] = " .,?"; |
| 415 |
|
| 416 |
int i; |
| 417 |
|
| 418 |
for (i = position; i<=strlen(string) && strchr(separators, string[i]) == NULL; i++) |
| 419 |
; |
| 420 |
return i; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
static char *stripWordsFromStringForwards(Aint count, char *initialString, char **theRest) { |
| 426 |
int skippedChars; |
| 427 |
int position = 0; |
| 428 |
char *stripped; |
| 429 |
int i; |
| 430 |
|
| 431 |
for (i = count; i>0; i--) { |
| 432 |
|
| 433 |
skippedChars = countLeadingBlanks(initialString, position); |
| 434 |
position += skippedChars; |
| 435 |
position = skipWordForwards(initialString, position); |
| 436 |
} |
| 437 |
|
| 438 |
stripped = (char *)allocate(position+1); |
| 439 |
strncpy(stripped, initialString, position); |
| 440 |
stripped[position] = '\0'; |
| 441 |
|
| 442 |
skippedChars = countLeadingBlanks(initialString, position); |
| 443 |
*theRest = strdup(&initialString[position+skippedChars]); |
| 444 |
|
| 445 |
return(stripped); |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
static int skipWordBackwards(char *string, int position) |
| 451 |
{ |
| 452 |
char separators[] = " .,?"; |
| 453 |
int i; |
| 454 |
|
| 455 |
for (i = position; i>0 && strchr(separators, string[i-1]) == NULL; i--) |
| 456 |
; |
| 457 |
return i; |
| 458 |
} |
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
static int countTrailingBlanks(char *string, int position) { |
| 463 |
int skippedChars, i; |
| 464 |
skippedChars = 0; |
| 465 |
|
| 466 |
if (position > strlen(string)-1) |
| 467 |
syserr("position > length in countTrailingBlanks"); |
| 468 |
for (i = position; i >= 0 && string[i] == ' '; i--) |
| 469 |
skippedChars++; |
| 470 |
return(skippedChars); |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
static char *stripWordsFromStringBackwards(Aint count, char *initialString, char **theRest) { |
| 476 |
int skippedChars; |
| 477 |
char *stripped; |
| 478 |
int strippedLength; |
| 479 |
int position = strlen(initialString); |
| 480 |
int i; |
| 481 |
|
| 482 |
for (i = count; i>0 && position>0; i--) { |
| 483 |
position -= 1; |
| 484 |
|
| 485 |
skippedChars = countTrailingBlanks(initialString, position); |
| 486 |
if (position - skippedChars < 0) break; |
| 487 |
position -= skippedChars; |
| 488 |
position = skipWordBackwards(initialString, position); |
| 489 |
} |
| 490 |
|
| 491 |
skippedChars = countLeadingBlanks(initialString, 0); |
| 492 |
strippedLength = strlen(initialString)-position-skippedChars; |
| 493 |
stripped = (char *)allocate(strippedLength+1); |
| 494 |
strncpy(stripped, &initialString[position+skippedChars], strippedLength); |
| 495 |
stripped[strippedLength] = '\0'; |
| 496 |
|
| 497 |
if (position > 0) { |
| 498 |
skippedChars = countTrailingBlanks(initialString, position-1); |
| 499 |
position -= skippedChars; |
| 500 |
} |
| 501 |
*theRest = strdup(initialString); |
| 502 |
(*theRest)[position] = '\0'; |
| 503 |
return(stripped); |
| 504 |
} |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
Aptr strip(bool stripFromBeginningNotEnd, int count, bool stripWordsNotChars, int id, int atr) |
| 510 |
{ |
| 511 |
char *initialString = (char *)fromAptr(getInstanceAttribute(id, atr)); |
| 512 |
char *theStripped; |
| 513 |
char *theRest; |
| 514 |
|
| 515 |
if (stripFromBeginningNotEnd) { |
| 516 |
if (stripWordsNotChars) |
| 517 |
theStripped = stripWordsFromStringForwards(count, initialString, &theRest); |
| 518 |
else |
| 519 |
theStripped = stripCharsFromStringForwards(count, initialString, &theRest); |
| 520 |
} else { |
| 521 |
if (stripWordsNotChars) |
| 522 |
theStripped = stripWordsFromStringBackwards(count, initialString, &theRest); |
| 523 |
else |
| 524 |
theStripped = stripCharsFromStringBackwards(count, initialString, &theRest); |
| 525 |
} |
| 526 |
setInstanceStringAttribute(id, atr, theRest); |
| 527 |
return toAptr(theStripped); |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
int getContainerMember(int container, int index, bool directly) { |
| 533 |
Aint i; |
| 534 |
Aint count = 0; |
| 535 |
|
| 536 |
for (i = 1; i <= header->instanceMax; i++) { |
| 537 |
if (isIn(i, container, DIRECT)) { |
| 538 |
count++; |
| 539 |
if (count == index) |
| 540 |
return i; |
| 541 |
} |
| 542 |
} |
| 543 |
apperr("Index not in container in 'containerMember()'"); |
| 544 |
return 0; |
| 545 |
} |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
void showImage(int image, int align) |
| 557 |
{ |
| 558 |
#ifdef HAVE_GLK |
| 559 |
glui32 ecode; |
| 560 |
|
| 561 |
if ((glk_gestalt(gestalt_Graphics, 0) == 1) && |
| 562 |
(glk_gestalt(gestalt_DrawImage, wintype_TextBuffer) == 1)) { |
| 563 |
glk_window_flow_break(glkMainWin); |
| 564 |
printf("\n"); |
| 565 |
|
| 566 |
ecode = glk_image_draw(glkMainWin, image, imagealign_MarginLeft, 0); |
| 567 |
(void)ecode; |
| 568 |
} |
| 569 |
#endif |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
void playSound(int sound) |
| 575 |
{ |
| 576 |
#ifdef HAVE_GLK |
| 577 |
#ifdef GLK_MODULE_SOUND |
| 578 |
static schanid_t soundChannel = NULL; |
| 579 |
glui32 ecode; |
| 580 |
|
| 581 |
if (glk_gestalt(gestalt_Sound, 0) == 1) { |
| 582 |
if (soundChannel == NULL) |
| 583 |
soundChannel = glk_schannel_create(0); |
| 584 |
if (soundChannel != NULL) { |
| 585 |
glk_schannel_stop(soundChannel); |
| 586 |
ecode = glk_schannel_play(soundChannel, sound); |
| 587 |
(void)ecode; |
| 588 |
} |
| 589 |
} |
| 590 |
#endif |
| 591 |
#endif |
| 592 |
} |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
void empty(int cnt, int whr) |
| 598 |
{ |
| 599 |
int i; |
| 600 |
|
| 601 |
for (i = 1; i <= header->instanceMax; i++) |
| 602 |
if (isIn(i, cnt, DIRECT)) |
| 603 |
locate(i, whr); |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
void use(int actor, int script) |
| 610 |
{ |
| 611 |
char str[80]; |
| 612 |
StepEntry *step; |
| 613 |
|
| 614 |
if (!isAActor(actor)) { |
| 615 |
sprintf(str, "Instance is not an Actor (%d).", actor); |
| 616 |
syserr(str); |
| 617 |
} |
| 618 |
|
| 619 |
admin[actor].script = script; |
| 620 |
admin[actor].step = 0; |
| 621 |
step = stepOf(actor); |
| 622 |
if (step != NULL && step->after != 0) { |
| 623 |
admin[actor].waitCount = evaluate(step->after); |
| 624 |
} |
| 625 |
|
| 626 |
gameStateChanged = true; |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
void stop(int act) |
| 631 |
{ |
| 632 |
char str[80]; |
| 633 |
|
| 634 |
if (!isAActor(act)) { |
| 635 |
sprintf(str, "Instance is not an Actor (%d).", act); |
| 636 |
syserr(str); |
| 637 |
} |
| 638 |
|
| 639 |
admin[act].script = 0; |
| 640 |
admin[act].step = 0; |
| 641 |
|
| 642 |
gameStateChanged = true; |
| 643 |
} |
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
static int randomValue = 0; |
| 648 |
|
| 649 |
int randomInteger(int from, int to) |
| 650 |
{ |
| 651 |
if (regressionTestOption) { |
| 652 |
int ret = from + randomValue; |
| 653 |
|
| 654 |
if (ret > to) { |
| 655 |
ret = from; |
| 656 |
randomValue = 1; |
| 657 |
} else if (ret == to) |
| 658 |
randomValue = 0; |
| 659 |
else |
| 660 |
randomValue++; |
| 661 |
return ret; |
| 662 |
} else { |
| 663 |
if (to == from) |
| 664 |
return to; |
| 665 |
else if (to > from) |
| 666 |
return (rand()/10)%(to-from+1)+from; |
| 667 |
else |
| 668 |
return (rand()/10)%(from-to+1)+to; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
bool between(int val, int low, int high) |
| 676 |
{ |
| 677 |
if (high > low) |
| 678 |
return low <= val && val <= high; |
| 679 |
else |
| 680 |
return high <= val && val <= low; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
bool contains(Aptr string, Aptr substring) |
| 687 |
{ |
| 688 |
bool found; |
| 689 |
|
| 690 |
stringToLowerCase((char *)fromAptr(string)); |
| 691 |
stringToLowerCase((char *)fromAptr(substring)); |
| 692 |
|
| 693 |
found = (strstr((char *)fromAptr(string), (char *)fromAptr(substring)) != 0); |
| 694 |
|
| 695 |
return found; |
| 696 |
} |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
bool streq(char a[], char b[]) |
| 701 |
{ |
| 702 |
bool eq; |
| 703 |
|
| 704 |
stringToLowerCase(a); |
| 705 |
stringToLowerCase(b); |
| 706 |
|
| 707 |
eq = (strcmp(a, b) == 0); |
| 708 |
|
| 709 |
return eq; |
| 710 |
} |
| 711 |
|
| 712 |
#if defined(HAVE_GLK) && defined(GLK_MODULE_DATETIME) |
| 713 |
static void createLogfileName(char *createdFileName, const char extension[]) { |
| 714 |
if (glk_gestalt(gestalt_DateTime, 0) != 0 && !regressionTestOption) { |
| 715 |
glktimeval_t tv; |
| 716 |
glkdate_t date; |
| 717 |
glk_current_time(&tv); |
| 718 |
glk_time_to_date_local(&tv, &date); |
| 719 |
|
| 720 |
sprintf(createdFileName, "%s%d%02d%02d%02d%02d%02d%04d%s", |
| 721 |
adventureName, date.year, date.month, |
| 722 |
date.day, date.hour, date.minute, date.second, |
| 723 |
date.microsec, |
| 724 |
extension); |
| 725 |
} else { |
| 726 |
sprintf(createdFileName, "%s%s", adventureName, extension); |
| 727 |
} |
| 728 |
} |
| 729 |
#else |
| 730 |
#include <sys/time.h> |
| 731 |
|
| 732 |
static void createLogfileName(char *createdFileName, const char extension[]) { |
| 733 |
time_t tick; |
| 734 |
|
| 735 |
time(&tick); |
| 736 |
|
| 737 |
struct timeval tv; |
| 738 |
struct tm *tm; |
| 739 |
gettimeofday(&tv, NULL); |
| 740 |
tm = localtime(&tv.tv_sec); |
| 741 |
|
| 742 |
if (!regressionTestOption) |
| 743 |
sprintf(createdFileName, "%s%d%02d%02d%02d%02d%02d%04d%s", |
| 744 |
adventureName, tm->tm_year+1900, tm->tm_mon+1, |
| 745 |
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, |
| 746 |
(int)tv.tv_usec, |
| 747 |
extension); |
| 748 |
else |
| 749 |
sprintf(createdFileName, "%s%s", adventureName, extension); |
| 750 |
} |
| 751 |
#endif |
| 752 |
|
| 753 |
|
| 754 |
void startTranscript(void) { |
| 755 |
if (transcriptFile != NULL) |
| 756 |
return; |
| 757 |
|
| 758 |
createLogfileName(transcriptFileName, ".a3t"); |
| 759 |
#ifdef HAVE_GLK |
| 760 |
glui32 fileUsage = fileusage_Transcript; |
| 761 |
frefid_t logFileRef = glk_fileref_create_by_name(fileUsage, transcriptFileName, 0); |
| 762 |
transcriptFile = glk_stream_open_file(logFileRef, filemode_Write, 0); |
| 763 |
#else |
| 764 |
transcriptFile = fopen(transcriptFileName, "w"); |
| 765 |
#endif |
| 766 |
|
| 767 |
if (transcriptFile == NULL) { |
| 768 |
transcriptOption = false; |
| 769 |
} else { |
| 770 |
transcriptOption = true; |
| 771 |
if (encodingOption == ENCODING_UTF) { |
| 772 |
uchar BOM[3] = {0xEF,0xBB,0xBF}; |
| 773 |
fwrite((char *)BOM, sizeof(BOM), 1, transcriptFile); |
| 774 |
} |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
void startCommandLog(void) { |
| 781 |
if (commandLogFile != NULL) |
| 782 |
return; |
| 783 |
|
| 784 |
createLogfileName(commandLogFileName, ".a3s"); |
| 785 |
#ifdef HAVE_GLK |
| 786 |
glui32 fileUsage = fileusage_InputRecord; |
| 787 |
frefid_t logFileRef = glk_fileref_create_by_name(fileUsage, commandLogFileName, 0); |
| 788 |
commandLogFile = glk_stream_open_file(logFileRef, filemode_Write, 0); |
| 789 |
#else |
| 790 |
commandLogFile = fopen(commandLogFileName, "w"); |
| 791 |
#endif |
| 792 |
|
| 793 |
if (commandLogFile == NULL) { |
| 794 |
commandLogOption = false; |
| 795 |
} else if (encodingOption == ENCODING_UTF) { |
| 796 |
uchar BOM[3] = {0xEF,0xBB,0xBF}; |
| 797 |
fwrite((char *)BOM, sizeof(BOM), 1, commandLogFile); |
| 798 |
} |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
void stopTranscript(void) { |
| 804 |
if (transcriptFile == NULL) |
| 805 |
return; |
| 806 |
|
| 807 |
if (transcriptOption) |
| 808 |
#ifdef HAVE_GLK |
| 809 |
glk_stream_close(transcriptFile, NULL); |
| 810 |
#else |
| 811 |
fclose(transcriptFile); |
| 812 |
#endif |
| 813 |
transcriptFile = NULL; |
| 814 |
transcriptOption = false; |
| 815 |
} |