1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
|
/* Random utilities not classifiable elsewhere.
Copyright (C) 1987-1989, 1991-1997 Stanley T. Shebs.
Xconq is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version. See the file COPYING. */
#include "config.h"
#include "misc.h"
extern int select_by_weight PARAMS ((int *arr, int numvals));
extern void debugging_string PARAMS ((char *buf));
#include "lisp.h"
#include "dir.h"
/* Accommodate certain obsolete (pre-ANSI) Unix systems. (?) */
#ifdef TIME_RETURNS_LONG
#ifndef time_t
#define time_t long
#endif
#endif
/* Direction-to-delta conversions. */
int dirx[NUMDIRS] = DIRX;
int diry[NUMDIRS] = DIRY;
/* If true, warnings will not be issued. */
int warnings_suppressed;
/* If true, allocation attempts will garner complaints, but not actually
fail. (This is primarily for debugging addon code, such as new AIs
and user interfaces.) */
int xmalloc_warnings;
/* If true, all allocation will fail, suppresses further attempts. */
int memory_exhausted;
/* If true, the run error is a bug in Xconq, so termination should do
more to help debugging (such as dumping core). */
int error_is_bug;
static char *padbuf;
/* Random number handling is important to game but terrible/nonexistent
in some systems. Do it ourselves and hope it's better. Unfortunately,
it's hard to prevent clueless hackers from calling rand or random
and thereby confusing everything, so there (should be) is a check in
the test dir that searches for calls to these. */
/* The random state *must* be at least 32 bits. */
long initrandstate = -1;
long randstate = -1;
/* Seed can come from elsewhere, for repeatability. Otherwise, it comes
from the current time, scaled down to where 32-bit arithmetic won't
overflow. */
void
init_xrandom(seed)
int seed;
{
time_t tm;
if (seed >= 0) {
/* If the random state is already set, changes are somewhat
suspicious. */
if (randstate >= 0) {
Dprintf("Randstate being changed from %d to %d\n",
randstate, seed);
}
randstate = seed;
} else {
time(&tm);
randstate = (long) tm;
}
/* Whatever its source, put the randstate into known range (0 - 99999). */
randstate = ABS(randstate);
randstate %= 100000L;
/* This is kept around for the sake of error reporting. */
initrandstate = randstate;
}
/* Numbers lifted from Numerical Recipes, p. 198. */
/* Arithmetic must be 32-bit. */
int
xrandom(m)
int m;
{
randstate = (8121 * randstate + 28411) % 134456L;
return ((m * randstate) / 134456L);
}
/* Percentage probability, with bounds checking. */
int
probability(prob)
int prob;
{
if (prob <= 0)
return FALSE;
if (prob >= 100)
return TRUE;
return (xrandom(100) < prob);
}
int
roll_dice(n)
int n;
{
int numdice, dice, i, rslt;
if (n >> 14 == 0 || n >> 14 == 3)
return n;
numdice = (n >> 11) & 0x07;
dice = (n >> 7) & 0x0f;
rslt = n & 0x7f;
for (i = 0; i < numdice; ++i) {
rslt += xrandom(dice);
}
return rslt;
}
int
multiply_dice(n, mult)
int n, mult;
{
int numdice, dice, addend;
if (n >> 14 == 0 || n >> 14 == 3)
return (n * mult) / 100;
numdice = (n >> 11) & 0x07;
dice = (n >> 7) & 0x0f;
dice = (dice * mult) / 100;
addend = ((n & 0x7f) * mult) / 100;
return (1 << 14) | (numdice << 11) | (dice << 7) | (addend & 0x7f);
}
/* For a number in the range 0 - 10000, divide it by 100 and use the
remainder as the probability of adding 1. */
int
prob_fraction(n)
int n;
{
return (n / 100 + (probability(n % 100) ? 1 : 0));
}
#ifdef DEBUGGING
/* This tracks our total space allocation. */
#define NUMMALLOCRECS 200
int overflow;
int numoverflow;
int totmalloc;
int nextmalloc;
int copymalloc;
int grandtotmalloc;
struct a_malloc {
int size;
int count;
} *mallocs = NULL;
static void record_malloc PARAMS ((int amt));
static int malloc_record_compare PARAMS ((const void *m1, const void *m2));
/* Given an amount of memory allocated, record it with the others. */
static void
record_malloc(amt)
int amt;
{
int i;
/* Might need to allocate the record of allocations. */
if (mallocs == NULL) {
/* Don't use xmalloc here!! */
mallocs = (struct a_malloc *)
malloc(NUMMALLOCRECS * sizeof(struct a_malloc));
overflow = 0;
numoverflow = 0;
}
/* Search for already-recorded allocations of same-size blocks. */
for (i = 0; i < nextmalloc; ++i) {
if (amt == mallocs[i].size) {
++(mallocs[i].count);
return;
}
}
if (nextmalloc < NUMMALLOCRECS) {
/* Record this allocation as a new size of allocation. */
mallocs[nextmalloc].size = amt;
mallocs[nextmalloc].count = 1;
++nextmalloc;
} else {
/* Add to the overflow bucket, which is used for allocations whose
sizes could not be recorded individually. Yes, we could allocate
more buckets, but this seems like too much trouble for an
uncommon case. */
overflow += amt;
++numoverflow;
}
}
static int
malloc_record_compare(m1, m2)
CONST void *m1, *m2;
{
return (((struct a_malloc *) m1)->size * ((struct a_malloc *) m1)->count
- ((struct a_malloc *) m2)->size * ((struct a_malloc *) m2)->count);
}
/* Display memory consumption and reset records. This does not account
for freeing, but Xconq usually tries to hang onto and reuse anything
it allocates, very rarely calls free(). */
void
report_malloc()
{
extern int lispmalloc, numsymbols;
int i;
/* If this is called too soon, just leave silently. */
if (mallocs == NULL)
return;
if (nextmalloc == 0) {
Dprintf("No mallocs since last report.\n");
return;
}
Dprintf("Mallocs since last report:\n");
Dprintf(" Amount = Bytes x Times\n");
/* Sort the entries. */
qsort(mallocs, nextmalloc, sizeof(struct a_malloc), malloc_record_compare);
/* Write out all the records, formatting nicely. */
for (i = 0; i < nextmalloc; ++i) {
Dprintf("%10d = %6d x %6d\n",
mallocs[i].size * mallocs[i].count,
mallocs[i].size, mallocs[i].count);
}
if (overflow > 0)
Dprintf("%10d = ?????? x %6d\n", overflow, numoverflow);
Dprintf("Total malloced = %d bytes.\n", totmalloc);
Dprintf("String copies = %d bytes.\n", copymalloc);
Dprintf("Lisp malloced = %d bytes.\n", lispmalloc);
Dprintf("Symbols interned = %d.\n", numsymbols);
Dprintf("Grand total allocation = %d bytes.\n", grandtotmalloc);
/* Reset all the counters for next time. */
nextmalloc = 0;
overflow = numoverflow = 0;
totmalloc = copymalloc = lispmalloc = 0;
}
#endif /* DEBUGGING */
/* This is our improved and adapted version of malloc, that guarantees
zeroing of the block, checks for memory exhaustion, and collects
usage statistics. */
char *
xmalloc(amt)
int amt;
{
char *value;
if (xmalloc_warnings) {
run_warning("Should not be calling xmalloc (requested %d bytes)", amt);
/* Keep going though. */
}
if (memory_exhausted)
return NULL;
/* Do the actual allocation. */
value = (char *) malloc(amt);
if (value == NULL) {
#ifdef DEBUGGING
if (Debug) {
Dprintf("Unable to allocate %d bytes.\n", amt);
/* Write out the allocation report if possible. */
report_malloc();
}
#endif /* DEBUGGING */
/* This is pretty serious, no way to recover. */
memory_exhausted = TRUE;
run_error("Memory exhausted!!");
/* In case run_error doesn't exit. */
exit(1);
}
/* Save callers from having to clear things themselves. */
memset(value, 0, amt);
#ifdef DEBUGGING
/* This can't be controlled by `Debug', because much allocation
happens before any command line or dialog options are interpreted. */
totmalloc += amt;
grandtotmalloc += amt;
record_malloc(amt);
#endif /* DEBUGGING */
return value;
}
/* Like sprintf, but appends. */
#ifdef __STDC__
void
tprintf(char *buf, char *str, ...)
{
va_list ap;
char line[300];
va_start(ap, str);
vsprintf(line, str, ap);
strcat(buf, line);
va_end(ap);
}
void
tnprintf(char *buf, int n, char *str, ...)
{
va_list ap;
int n1 = n - strlen(buf);
char line[300];
if (n1 > 0) {
va_start(ap, str);
vsprintf(line, str, ap);
strncat(buf, line, n1);
va_end(ap);
}
}
void
vtprintf(char *buf, char *str, va_list ap)
{
char line[300];
vsprintf(line, str, ap);
strcat(buf, line);
}
#else
void
tprintf(buf, str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *buf, *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
{
char line[300];
sprintf(line, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
strcat(buf, line);
}
void
tnprintf(buf, n, str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *buf, *str;
int n;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
{
int n1 = n - strlen(buf);
char line[300];
if (n1 > 0) {
sprintf(line, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
strncat(buf, line, n1);
}
}
#endif
int
select_by_weight(arr, numvals)
int *arr, numvals;
{
int sum = 0, i, n;
sum = 0;
for (i = 0; i < numvals; ++i) {
if (arr[i] <= 0)
continue;
sum += arr[i];
}
if (sum == 0)
return -1;
/* We now know the range, make a random index into it. */
n = xrandom(sum);
/* Go through again to figure out which choice the index refs. */
sum = 0;
for (i = 0; i < numvals; ++i) {
if (arr[i] <= 0)
continue;
sum += arr[i];
if (sum >= n) {
return i;
}
}
run_error("Ooh weird");
return -1;
}
/* Copy a string to newly-allocated space. The new space is never freed. */
char *
copy_string(str)
char *str;
{
int len = strlen(str);
char *rslt;
rslt = xmalloc(len + 1);
strcpy(rslt, str);
#ifdef DEBUGGING
copymalloc += len + 1;
#endif
return rslt;
}
/* Insert the given number of blanks between each char of the string. */
char *
pad_blanks(str, n)
char *str;
int n;
{
char *pb;
int i;
if (padbuf == NULL)
padbuf = xmalloc(BUFSIZE);
pb = padbuf;
while (*str && pb < padbuf + BUFSIZE - 2) {
*pb++ = *str++;
if (*str) {
for (i = 0; i < n; i++)
*pb++ = ' ';
}
}
*pb = '\0';
return padbuf;
}
/* Get a *numeric* index into a string (more useful than ptr, in Xconq).
Return -1 on failed search. */
int
iindex(ch, str)
int ch;
char *str;
{
int i;
if (str == NULL)
return (-1);
for (i = 0; str[i] != '\0'; ++i)
if (ch == str[i])
return i;
return (-1);
}
/* Return a time difference as an long integer number of seconds. */
long
idifftime(t1, t0)
time_t t1, t0;
{
#ifdef MAC /* (should be anywhere that actually has a difftime function) */
return ((long) difftime(t1, t0));
#else
return ((long) t1 - (long) t0);
#endif
}
/* This little routine goes at the end of all switch statements on internal
data values. */
void
case_panic(str, var)
char *str;
int var;
{
error_is_bug = TRUE;
run_error("Panic! Unknown %s %d", str, var);
}
/* Integer square root - good enough, no float trickery or libs needed. */
/* Improved version from Bruce Fast, via Scott Herod. */
int
isqrt(i)
int i;
{
int j, k;
if (i > 3) {
for (j = i, k = -1; j >>= 2; k <<= 1);
k = (~k + i / ~k) / 2;
k = (k + i / k) / 2;
k = (1 + k + i / k) / 2;
return (k + i / k) / 2;
} else if (i > 0) {
return 1;
} else {
return 0;
}
}
#ifdef DEBUGGING
/* Debugging flags.
These are set up so that they can be macros or variables, as desired. */
#ifndef Debug
int Debug = FALSE;
#endif
#ifndef DebugM
int DebugM = FALSE;
#endif
#ifndef DebugG
int DebugG = FALSE;
#endif
/* These are where normal debugging output goes. */
FILE *dfp = NULL;
FILE *dgfp = NULL;
FILE *dmfp = NULL;
/* Map all the debugging outputs to stdout. */
/* You might think stderr would be a better choice, but this way
the debugging output is sync'ed properly (read: consistently!)
with "normal" stdout output. */
void
init_debug_to_stdout()
{
dfp = stdout;
dgfp = stdout;
dmfp = stdout;
}
/* Junk associated with debug output. */
/* This is all fairly elaborate because we need to be able to collect
detailed logs of AI activity over different periods of time, and just
dumping to stdout doesn't work in a window system. */
FILE *ffp = NULL;
int firstdebug = TRUE;
void
update_debugging()
{
if (Debug || DebugG || DebugM) {
/* Always close the file if open, forces to desirable state. */
if (ffp != NULL) {
fclose(ffp);
ffp = NULL;
}
/* Reopen the file. */
if (ffp == NULL) {
ffp = fopen("Xconq.DebugOut", "a");
}
if (ffp != NULL) {
if (Debug)
dfp = ffp;
if (DebugG)
dgfp = ffp;
if (DebugM)
dmfp = ffp;
}
}
}
/* Debug output goes to a file. */
void
toggle_debugging(flagp)
int *flagp;
{
/* Always close the file if open, forces to desirable state. */
if (ffp != NULL) {
fclose(ffp);
ffp = NULL;
}
/* Flip the state of the debugging flag, if supplied. */
if (flagp != NULL) {
*flagp = ! *flagp;
}
/* (Re-)open the debugging transcript file. */
if (ffp == NULL) {
ffp = fopen("Xconq.DebugOut", (firstdebug ? "w" : "a"));
firstdebug = FALSE;
}
if (ffp != NULL && flagp != NULL) {
/* Indicate which flags are now on. */
fprintf(ffp, "\n\n*********** %s %s %s **********\n\n",
(Debug ? "Debug" : ""), (DebugM ? "DebugM" : ""),
(DebugG ? "DebugG" : ""));
}
/* Set specific debug file pointers to be the same as the
pointer to the file. */
if (ffp != NULL) {
if (Debug)
dfp = ffp;
if (DebugG)
dgfp = ffp;
if (DebugM)
dmfp = ffp;
}
/* If all debugging flags have been turned off, close the file too. */
if (!Debug && !DebugG && !DebugM) {
if (ffp != NULL) {
fclose(ffp);
ffp = NULL;
}
}
prealloc_debug();
}
void
debugging_string(buf)
char *buf;
{
buf[0] = '\0';
if (Debug || DebugM || DebugG)
strcat(buf, "-D");
if (!Debug)
strcat(buf, "-");
if (DebugM)
strcat(buf, "M");
if (DebugG)
strcat(buf, "G");
}
#ifdef __STDC__
void
debug_printf(char *str, ...)
{
va_list ap;
va_start(ap, str);
vfprintf(dfp, str, ap);
va_end(ap);
}
void
debugm_printf(char *str, ...)
{
va_list ap;
va_start(ap, str);
vfprintf(dmfp, str, ap);
va_end(ap);
}
void
debugg_printf(char *str, ...)
{
va_list ap;
va_start(ap, str);
vfprintf(dgfp, str, ap);
va_end(ap);
}
#else
/* Kind of cheesy, should only use if real ANSI not available. */
void
debug_printf(str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
{
fprintf(dfp, str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
}
void
debugm_printf(str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
{
fprintf(dmfp, str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
}
void
debugg_printf(str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12;
{
fprintf(dgfp, str, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
}
#endif /* __STDC__ */
#endif /* DEBUGGING */
void
#ifdef __STDC__
init_error(char *str, ...)
#else
init_error(str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
#endif
{
char buf[BUFSIZE];
#ifdef __STDC__
{
va_list ap;
va_start(ap, str);
vsprintf(buf, str, ap);
va_end(ap);
}
#else
sprintf(buf, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif
Dprintf("INIT ERROR %s INIT ERROR\n", buf);
low_init_error(buf);
}
void
#ifdef __STDC__
init_warning(char *str, ...)
#else
init_warning(str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
#endif
{
char buf[BUFSIZE];
#ifdef __STDC__
{
va_list ap;
va_start(ap, str);
vsprintf(buf, str, ap);
va_end(ap);
}
#else
sprintf(buf, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif
Dprintf("INIT WARNING %s INIT WARNING\n", buf);
if (warnings_suppressed)
return;
low_init_warning(buf);
}
/* A run error is fatal. */
void
#ifdef __STDC__
run_error(char *str, ...)
#else
run_error(str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
#endif
{
char buf[BUFSIZE];
#ifdef __STDC__
{
va_list ap;
va_start(ap, str);
vsprintf(buf, str, ap);
va_end(ap);
}
#else
sprintf(buf, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif
Dprintf("RUN ERROR %s RUN ERROR\n", buf);
low_run_error(buf);
}
/* Runtime warnings are for when it's important to bug the players,
usually a problem with Xconq or a game design. */
void
#ifdef __STDC__
run_warning(char *str, ...)
#else
run_warning(str, a1, a2, a3, a4, a5, a6, a7, a8, a9)
char *str;
long a1, a2, a3, a4, a5, a6, a7, a8, a9;
#endif
{
char buf[BUFSIZE];
#ifdef __STDC__
{
va_list ap;
va_start(ap, str);
vsprintf(buf, str, ap);
va_end(ap);
}
#else
sprintf(buf, str, a1, a2, a3, a4, a5, a6, a7, a8, a9);
#endif
Dprintf("RUN WARNING %s RUN WARNING\n", buf);
if (warnings_suppressed)
return;
low_run_warning(buf);
}
|