| 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 |
#include "global.h" |
| 27 |
|
| 28 |
#include <ctype.h> |
| 29 |
#include <math.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
|
| 33 |
#include "sproto.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
int random_roll(int min, int max, const object *op, int goodbad) { |
| 43 |
int omin, diff, luck, base, ran; |
| 44 |
|
| 45 |
omin = min; |
| 46 |
diff = max-min+1; |
| 47 |
((diff > 2) ? (base = 20) : (base = 50)); |
| 48 |
|
| 49 |
if (max < 1 || diff < 1) { |
| 50 |
LOG(llevError, "Calling random_roll with min=%d max=%d\n", min, max); |
| 51 |
return(min); |
| 52 |
} |
| 53 |
|
| 54 |
ran = RANDOM(); |
| 55 |
|
| 56 |
if (op->type != PLAYER) |
| 57 |
return((ran%diff)+min); |
| 58 |
|
| 59 |
luck = op->stats.luck; |
| 60 |
if (RANDOM()%base < (unsigned int)MIN(10, abs(luck))) { |
| 61 |
|
| 62 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
| 63 |
diff -= luck; |
| 64 |
if (diff < 1) |
| 65 |
return(omin); |
| 66 |
((goodbad) ? (min += luck) : (diff)); |
| 67 |
|
| 68 |
return(MAX(omin, MIN(max, (ran%diff)+min))); |
| 69 |
} |
| 70 |
return((ran%diff)+min); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
int64_t random_roll64(int64_t min, int64_t max, const object *op, int goodbad) { |
| 78 |
int64_t omin, diff, ran; |
| 79 |
int8_t luck; |
| 80 |
int base; |
| 81 |
|
| 82 |
omin = min; |
| 83 |
diff = max-min+1; |
| 84 |
((diff > 2) ? (base = 20) : (base = 50)); |
| 85 |
|
| 86 |
if (max < 1 || diff < 1) { |
| 87 |
LOG(llevError, "Calling random_roll with min=%"FMT64" max=%"FMT64"\n", min, max); |
| 88 |
return(min); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
ran = (RANDOM()&0xffffffff)|((int64_t)RANDOM()<<32); |
| 97 |
|
| 98 |
if (op->type != PLAYER) |
| 99 |
return((ran%diff)+min); |
| 100 |
|
| 101 |
luck = op->stats.luck; |
| 102 |
if (RANDOM()%base < (unsigned int)MIN(10, abs(luck))) { |
| 103 |
|
| 104 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
| 105 |
diff -= luck; |
| 106 |
if (diff < 1) |
| 107 |
return (omin); |
| 108 |
((goodbad) ? (min += luck) : (diff)); |
| 109 |
|
| 110 |
return (MAX(omin, MIN(max, (ran%diff)+min))); |
| 111 |
} |
| 112 |
return ((ran%diff)+min); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
int die_roll(int num, int size, const object *op, int goodbad) { |
| 123 |
int min, diff, luck, total, i, gotlucky, base, ran; |
| 124 |
|
| 125 |
diff = size; |
| 126 |
min = 1; |
| 127 |
luck = total = gotlucky = 0; |
| 128 |
((diff > 2) ? (base = 20) : (base = 50)); |
| 129 |
if (size < 2 || diff < 1) { |
| 130 |
LOG(llevError, "Calling die_roll with num=%d size=%d\n", num, size); |
| 131 |
return(num); |
| 132 |
} |
| 133 |
|
| 134 |
if (op->type == PLAYER) |
| 135 |
luck = op->stats.luck; |
| 136 |
|
| 137 |
for (i = 0; i < num; i++) { |
| 138 |
if (RANDOM()%base < (unsigned int)MIN(10, abs(luck)) && !gotlucky) { |
| 139 |
|
| 140 |
gotlucky++; |
| 141 |
((luck > 0) ? (luck = 1) : (luck = -1)); |
| 142 |
diff -= luck; |
| 143 |
if (diff < 1) |
| 144 |
return(num); |
| 145 |
((goodbad) ? (min += luck) : (diff)); |
| 146 |
ran = RANDOM(); |
| 147 |
total += MAX(1, MIN(size, (ran%diff)+min)); |
| 148 |
} else { |
| 149 |
total += RANDOM()%size+1; |
| 150 |
} |
| 151 |
} |
| 152 |
return(total); |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
int rndm(int min, int max) { |
| 163 |
int diff; |
| 164 |
|
| 165 |
diff = max-min+1; |
| 166 |
if (max < 1 || diff < 1) |
| 167 |
return (min); |
| 168 |
|
| 169 |
return (RANDOM()%diff+min); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
void decay_objects(mapstruct *m) { |
| 176 |
int x, y, destroy; |
| 177 |
|
| 178 |
if (m->unique) |
| 179 |
return; |
| 180 |
|
| 181 |
for (x = 0; x < MAP_WIDTH(m); x++) |
| 182 |
for (y = 0; y < MAP_HEIGHT(m); y++) |
| 183 |
FOR_MAP_PREPARE(m, x, y, op) { |
| 184 |
destroy = 0; |
| 185 |
if (QUERY_FLAG(op, FLAG_IS_FLOOR) && QUERY_FLAG(op, FLAG_UNIQUE)) |
| 186 |
break; |
| 187 |
if (QUERY_FLAG(op, FLAG_IS_FLOOR) |
| 188 |
|| QUERY_FLAG(op, FLAG_OBJ_ORIGINAL) |
| 189 |
|| QUERY_FLAG(op, FLAG_UNIQUE) |
| 190 |
|| QUERY_FLAG(op, FLAG_OVERLAY_FLOOR) |
| 191 |
|| QUERY_FLAG(op, FLAG_UNPAID) |
| 192 |
|| IS_LIVE(op)) |
| 193 |
continue; |
| 194 |
if (op->head) |
| 195 |
|
| 196 |
continue; |
| 197 |
|
| 198 |
if (IS_WEAPON(op)) { |
| 199 |
op->stats.dam--; |
| 200 |
if (op->stats.dam < 0) |
| 201 |
destroy = 1; |
| 202 |
} else if (IS_ARMOR(op) |
| 203 |
|| IS_SHIELD(op) |
| 204 |
|| op->type == GIRDLE |
| 205 |
|| op->type == GLOVES |
| 206 |
|| op->type == CLOAK) { |
| 207 |
op->stats.ac--; |
| 208 |
if (op->stats.ac < 0) |
| 209 |
destroy = 1; |
| 210 |
} else if (op->type == FOOD) { |
| 211 |
op->stats.food -= rndm(5, 20); |
| 212 |
if (op->stats.food < 0) |
| 213 |
destroy = 1; |
| 214 |
} else { |
| 215 |
if (op->material&M_PAPER |
| 216 |
|| op->material&M_LEATHER |
| 217 |
|| op->material&M_WOOD |
| 218 |
|| op->material&M_ORGANIC |
| 219 |
|| op->material&M_CLOTH |
| 220 |
|| op->material&M_LIQUID) |
| 221 |
destroy = 1; |
| 222 |
if (op->material&M_IRON && rndm(1, 5) == 1) |
| 223 |
destroy = 1; |
| 224 |
if (op->material&M_GLASS && rndm(1, 2) == 1) |
| 225 |
destroy = 1; |
| 226 |
if ((op->material&M_STONE || op->material&M_ADAMANT) && rndm(1, 10) == 1) |
| 227 |
destroy = 1; |
| 228 |
if ((op->material&M_SOFT_METAL || op->material&M_BONE) |
| 229 |
&& rndm(1, 3) == 1) |
| 230 |
destroy = 1; |
| 231 |
if (op->material&M_ICE && rndm(0, 100) > 70) |
| 232 |
destroy = 1; |
| 233 |
} |
| 234 |
|
| 235 |
if (destroy && rndm(0, 1)) { |
| 236 |
object_remove(op); |
| 237 |
object_free_drop_inventory(op); |
| 238 |
} |
| 239 |
} FOR_MAP_FINISH(); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
materialtype_t *name_to_material(const char *name) { |
| 249 |
materialtype_t *mt, *nmt; |
| 250 |
|
| 251 |
mt = NULL; |
| 252 |
for (nmt = materialt; nmt != NULL && nmt->next != NULL; nmt = nmt->next) { |
| 253 |
if (strcmp(name, nmt->name) == 0) { |
| 254 |
mt = nmt; |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
return mt; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
void transmute_materialname(object *op, const object *change) { |
| 268 |
materialtype_t *mt; |
| 269 |
int j; |
| 270 |
|
| 271 |
if (op->materialname == NULL) |
| 272 |
return; |
| 273 |
|
| 274 |
if (change->materialname != NULL |
| 275 |
&& strcmp(op->materialname, change->materialname)) |
| 276 |
return; |
| 277 |
|
| 278 |
if (!(IS_ARMOR(op) || IS_SHIELD(op) || op->type == GIRDLE || op->type == GLOVES || op->type == CLOAK)) |
| 279 |
return; |
| 280 |
|
| 281 |
mt = name_to_material(op->materialname); |
| 282 |
if (!mt) { |
| 283 |
LOG(llevError, "archetype '%s>%s' uses nonexistent material '%s'\n", op->arch->name, op->name, op->materialname); |
| 284 |
return; |
| 285 |
} |
| 286 |
|
| 287 |
for (j = 0; j < NROFATTACKS; j++) |
| 288 |
if (op->resist[j] == 0 && change->resist[j] != 0) { |
| 289 |
op->resist[j] += mt->mod[j]; |
| 290 |
if (op->resist[j] > 100) |
| 291 |
op->resist[j] = 100; |
| 292 |
if (op->resist[j] < -100) |
| 293 |
op->resist[j] = -100; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
void set_materialname(object *op) { |
| 302 |
materialtype_t *mt; |
| 303 |
|
| 304 |
if (op->materialname != NULL) |
| 305 |
return; |
| 306 |
|
| 307 |
for (mt = materialt; mt != NULL; mt = mt->next) { |
| 308 |
if (op->material&mt->material) { |
| 309 |
break; |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
if (mt != NULL) { |
| 314 |
op->materialname = add_string(mt->name); |
| 315 |
return; |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
const char *strrstr(const char *haystack, const char *needle) { |
| 326 |
const char *lastneedle; |
| 327 |
|
| 328 |
lastneedle = NULL; |
| 329 |
while ((haystack = strstr(haystack, needle)) != NULL) { |
| 330 |
lastneedle = haystack; |
| 331 |
haystack++; |
| 332 |
} |
| 333 |
return lastneedle; |
| 334 |
} |
| 335 |
|
| 336 |
#define EOL_SIZE (sizeof("\n")-1) |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
void strip_endline(char *buf) { |
| 342 |
if (strlen(buf) < sizeof("\n")) { |
| 343 |
return; |
| 344 |
} |
| 345 |
if (!strcmp(buf+strlen(buf)-EOL_SIZE, "\n")) |
| 346 |
buf[strlen(buf)-EOL_SIZE] = '\0'; |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
void replace(const char *src, const char *key, const char *replacement, char *result, size_t resultsize) { |
| 355 |
size_t resultlen; |
| 356 |
size_t keylen; |
| 357 |
|
| 358 |
|
| 359 |
if (strcmp(key, replacement) == 0) { |
| 360 |
strlcpy(result, src, resultsize); |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
keylen = strlen(key); |
| 365 |
|
| 366 |
resultlen = 0; |
| 367 |
while (*src != '\0' && resultlen+1 < resultsize) { |
| 368 |
if (strncmp(src, key, keylen) == 0) { |
| 369 |
snprintf(result+resultlen, resultsize-resultlen, "%s", replacement); |
| 370 |
resultlen += strlen(result+resultlen); |
| 371 |
src += keylen; |
| 372 |
} else { |
| 373 |
result[resultlen++] = *src++; |
| 374 |
} |
| 375 |
} |
| 376 |
result[resultlen] = '\0'; |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
void make_list_like(char *input) { |
| 396 |
char *p, tmp[MAX_BUF]; |
| 397 |
int i; |
| 398 |
if (!input || strlen(input) > MAX_BUF-5) |
| 399 |
return; |
| 400 |
|
| 401 |
|
| 402 |
strncpy(tmp, input, MAX_BUF-5); |
| 403 |
|
| 404 |
for (i = strlen(tmp); i >= 0 && !isalnum(tmp[i]); i--) { |
| 405 |
tmp[i] = '\0'; |
| 406 |
} |
| 407 |
strcat(tmp, "."); |
| 408 |
|
| 409 |
p = strrchr(tmp, ','); |
| 410 |
if (p) { |
| 411 |
*p = '\0'; |
| 412 |
strcpy(input, tmp); |
| 413 |
p++; |
| 414 |
strcat(input, " and"); |
| 415 |
strcat(input, p); |
| 416 |
} else |
| 417 |
strcpy(input, tmp); |
| 418 |
return; |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
int get_random_dir(void) { |
| 428 |
return rndm(1, 8); |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
int get_randomized_dir(int dir) { |
| 440 |
return absdir(dir+RANDOM()%3+RANDOM()%3-2); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
int adjust_dir(int dir, int destination_dir) { |
| 454 |
int diff; |
| 455 |
|
| 456 |
diff = (destination_dir-dir)&7; |
| 457 |
if (1 <= diff && diff <= 3) |
| 458 |
dir++; |
| 459 |
else if (5 <= diff && diff <= 7) |
| 460 |
dir--; |
| 461 |
else if (rndm(0, 1) == 0) |
| 462 |
dir++; |
| 463 |
else |
| 464 |
dir--; |
| 465 |
return absdir(dir); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
void replace_unprintable_chars(char *buf) { |
| 475 |
char *p; |
| 476 |
|
| 477 |
for (p = buf; *p != '\0'; p++) { |
| 478 |
if (*p < ' ') { |
| 479 |
*p = ' '; |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
size_t split_string(char *str, char *array[], size_t array_size, char sep) { |
| 501 |
char *p; |
| 502 |
size_t pos; |
| 503 |
|
| 504 |
if (array_size <= 0) |
| 505 |
return 0; |
| 506 |
|
| 507 |
if (*str == '\0') { |
| 508 |
array[0] = str; |
| 509 |
return 1; |
| 510 |
} |
| 511 |
|
| 512 |
pos = 0; |
| 513 |
p = str; |
| 514 |
while (pos < array_size) { |
| 515 |
array[pos++] = p; |
| 516 |
while (*p != '\0' && *p != sep) |
| 517 |
p++; |
| 518 |
if (pos >= array_size) |
| 519 |
break; |
| 520 |
if (*p != sep) |
| 521 |
break; |
| 522 |
*p++ = '\0'; |
| 523 |
} |
| 524 |
return pos; |
| 525 |
} |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
StringBuffer *describe_spellpath_attenuation(const char *attenuation, int value, StringBuffer *buf) { |
| 535 |
if (buf == NULL) |
| 536 |
buf = stringbuffer_new(); |
| 537 |
|
| 538 |
if (value) { |
| 539 |
int i, j = 0; |
| 540 |
stringbuffer_append_printf(buf, "(%s: ", attenuation); |
| 541 |
for (i = 0; i < NRSPELLPATHS; i++) |
| 542 |
if (value&(1<<i)) { |
| 543 |
if (j) |
| 544 |
stringbuffer_append_string(buf, ", "); |
| 545 |
else |
| 546 |
j = 1; |
| 547 |
stringbuffer_append_string(buf, spellpathnames[i]); |
| 548 |
} |
| 549 |
stringbuffer_append_string(buf, ")"); |
| 550 |
} |
| 551 |
|
| 552 |
return buf; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
StringBuffer *describe_attacktype(const char *attack, int value, StringBuffer *buf) { |
| 563 |
if (buf == NULL) |
| 564 |
buf = stringbuffer_new(); |
| 565 |
|
| 566 |
if (value) { |
| 567 |
int i, j = 0; |
| 568 |
stringbuffer_append_printf(buf, "(%s: ", attack); |
| 569 |
for (i = 0; i < NROFATTACKS; i++) |
| 570 |
if (value&(1<<i)) { |
| 571 |
if (j) |
| 572 |
stringbuffer_append_string(buf, ", "); |
| 573 |
else |
| 574 |
j = 1; |
| 575 |
stringbuffer_append_string(buf, attacks[i]); |
| 576 |
} |
| 577 |
stringbuffer_append_string(buf, ")"); |
| 578 |
} |
| 579 |
|
| 580 |
return buf; |
| 581 |
} |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
int isqrt(int n) { |
| 587 |
return (int)sqrt(n); |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
void fatal(enum fatal_error err) { |
| 598 |
const char *fatalmsgs[] = { |
| 599 |
"Failed to allocate memory", |
| 600 |
"Failed repeatedly to load maps", |
| 601 |
"Hashtable for archetypes is too small", |
| 602 |
"Fatal issue in archetype file", |
| 603 |
"See last error", |
| 604 |
}; |
| 605 |
|
| 606 |
fprintf(logfile, "Fatal error: %s\n", fatalmsgs[err]); |
| 607 |
emergency_save(0); |
| 608 |
clean_tmp_files(); |
| 609 |
fprintf(logfile, "Exiting...\n"); |
| 610 |
exit(err); |
| 611 |
} |