| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include "global.h" |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "random_map.h" |
| 27 |
#include "rproto.h" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#define CONCENTRATED 1 |
| 36 |
#define HIDDEN 2 |
| 37 |
#define KEYREQUIRED 4 |
| 38 |
#define DOORED 8 |
| 39 |
#define TRAPPED 16 |
| 40 |
#define SPARSE 32 |
| 41 |
#define RICH 64 |
| 42 |
#define FILLED 128 |
| 43 |
#define LAST_OPTION 64 |
| 44 |
|
| 45 |
|
| 46 |
#define NO_PASS_DOORS 0 |
| 47 |
#define PASS_DOORS 1 |
| 48 |
|
| 49 |
static object **surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts); |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
int wall_blocked(mapstruct *m, int x, int y) |
| 63 |
{ |
| 64 |
int r; |
| 65 |
|
| 66 |
if (OUT_OF_REAL_MAP(m, x, y)) { |
| 67 |
return 1; |
| 68 |
} |
| 69 |
r = GET_MAP_MOVE_BLOCK(m, x, y)&~MOVE_BLOCK_DEFAULT; |
| 70 |
return r; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
void place_treasure(mapstruct *map, char **layout, char *treasure_style, int treasureoptions, RMParms *RP) |
| 93 |
{ |
| 94 |
int num_treasures; |
| 95 |
|
| 96 |
|
| 97 |
if (treasure_style) |
| 98 |
if (!strcmp(treasure_style, "none")) { |
| 99 |
return; |
| 100 |
} |
| 101 |
if (treasureoptions <= 0) { |
| 102 |
treasureoptions = RANDOM()%(2*LAST_OPTION); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
if ((treasureoptions&RICH) && (treasureoptions&SPARSE)) { |
| 107 |
if (RANDOM()%2) { |
| 108 |
treasureoptions -= 1; |
| 109 |
} else { |
| 110 |
treasureoptions -= 2; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
if (treasureoptions&SPARSE) { |
| 116 |
num_treasures = BC_RANDOM(RP->total_map_hp/600+RP->difficulty/2+1); |
| 117 |
} else if (treasureoptions&RICH) { |
| 118 |
num_treasures = BC_RANDOM(RP->total_map_hp/150+2*RP->difficulty+1); |
| 119 |
} else { |
| 120 |
num_treasures = BC_RANDOM(RP->total_map_hp/300+RP->difficulty+1); |
| 121 |
} |
| 122 |
|
| 123 |
if (num_treasures <= 0) { |
| 124 |
return; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if (treasureoptions&CONCENTRATED) { |
| 129 |
|
| 130 |
switch (RP->map_layout_style) { |
| 131 |
case ONION_LAYOUT: |
| 132 |
case SPIRAL_LAYOUT: |
| 133 |
case SQUARE_SPIRAL_LAYOUT: { |
| 134 |
int i, j; |
| 135 |
|
| 136 |
|
| 137 |
for (i = 0; i < RP->Xsize; i++) { |
| 138 |
for (j = 0; j < RP->Ysize; j++) { |
| 139 |
if (layout[i][j] == 'C' || layout[i][j] == '>') { |
| 140 |
int tdiv = RP->symmetry_used; |
| 141 |
object **doorlist; |
| 142 |
object *chest; |
| 143 |
|
| 144 |
if (tdiv == 3) { |
| 145 |
tdiv = 2; |
| 146 |
} |
| 147 |
|
| 148 |
chest = place_chest(treasureoptions, i, j, map, num_treasures/tdiv, RP); |
| 149 |
if (!chest) { |
| 150 |
continue; |
| 151 |
} |
| 152 |
if (treasureoptions&(DOORED|HIDDEN)) { |
| 153 |
doorlist = find_doors_in_room(map, i, j, RP); |
| 154 |
lock_and_hide_doors(doorlist, map, treasureoptions, RP); |
| 155 |
free(doorlist); |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
break; |
| 161 |
} |
| 162 |
default: { |
| 163 |
int i, j, tries; |
| 164 |
object *chest; |
| 165 |
object **doorlist; |
| 166 |
|
| 167 |
i = j = -1; |
| 168 |
tries = 0; |
| 169 |
while (i == -1 && tries < 100) { |
| 170 |
i = RANDOM()%(RP->Xsize-2)+1; |
| 171 |
j = RANDOM()%(RP->Ysize-2)+1; |
| 172 |
find_enclosed_spot(map, &i, &j, RP); |
| 173 |
if (wall_blocked(map, i, j)) { |
| 174 |
i = -1; |
| 175 |
} |
| 176 |
tries++; |
| 177 |
} |
| 178 |
chest = place_chest(treasureoptions, i, j, map, num_treasures, RP); |
| 179 |
if (!chest) { |
| 180 |
return; |
| 181 |
} |
| 182 |
i = chest->x; |
| 183 |
j = chest->y; |
| 184 |
if (treasureoptions&(DOORED|HIDDEN)) { |
| 185 |
doorlist = surround_by_doors(map, layout, i, j, treasureoptions); |
| 186 |
lock_and_hide_doors(doorlist, map, treasureoptions, RP); |
| 187 |
free(doorlist); |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
} else { |
| 192 |
int ti, i, j; |
| 193 |
|
| 194 |
for (ti = 0; ti < num_treasures; ti++) { |
| 195 |
i = RANDOM()%(RP->Xsize-2)+1; |
| 196 |
j = RANDOM()%(RP->Ysize-2)+1; |
| 197 |
place_chest(treasureoptions, i, j, map, 1, RP); |
| 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 |
object *place_chest(int treasureoptions, int x, int y, mapstruct *map, int n_treasures, RMParms *RP) |
| 224 |
{ |
| 225 |
object *the_chest = NULL; |
| 226 |
int i, xl, yl; |
| 227 |
treasurelist *tlist; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
if (map->difficulty > 5 && RANDOM() % 1000000 < map->difficulty*map->difficulty*map->difficulty) |
| 232 |
{ |
| 233 |
the_chest = create_archetype("mimic"); |
| 234 |
} |
| 235 |
else |
| 236 |
{ |
| 237 |
mapstruct *chests_map = find_style("/styles/cheststyles", RP->cheststyle, map->difficulty); |
| 238 |
if (chests_map) { |
| 239 |
object *item = pick_random_object(chests_map); |
| 240 |
if (item) { |
| 241 |
the_chest = object_new(); |
| 242 |
object_copy_with_inv(item, the_chest); |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
if (the_chest == NULL) { |
| 247 |
return NULL; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
i = object_find_first_free_spot(the_chest, map, x, y); |
| 252 |
if (i == -1) { |
| 253 |
object_free_drop_inventory(the_chest); |
| 254 |
return NULL; |
| 255 |
} |
| 256 |
xl = x+freearr_x[i]; |
| 257 |
yl = y+freearr_y[i]; |
| 258 |
|
| 259 |
|
| 260 |
if (wall_blocked(map, xl, yl)) { |
| 261 |
object_free_drop_inventory(the_chest); |
| 262 |
return NULL; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
if (the_chest->type == TREASURE) |
| 267 |
{ |
| 268 |
tlist = find_treasurelist("chest"); |
| 269 |
the_chest->randomitems = tlist; |
| 270 |
the_chest->stats.hp = n_treasures; |
| 271 |
} |
| 272 |
|
| 273 |
else if (the_chest->type == MIMIC) |
| 274 |
{ |
| 275 |
tlist = find_treasurelist("mimic"); |
| 276 |
the_chest->randomitems = tlist; |
| 277 |
|
| 278 |
} |
| 279 |
|
| 280 |
if (treasureoptions&TRAPPED) { |
| 281 |
mapstruct *trap_map = find_style("/styles/trapstyles", "traps", -1); |
| 282 |
object *the_trap; |
| 283 |
|
| 284 |
if (trap_map) { |
| 285 |
the_trap = pick_random_object(trap_map); |
| 286 |
if (the_trap) { |
| 287 |
object *new_trap; |
| 288 |
|
| 289 |
new_trap = object_new(); |
| 290 |
object_copy_with_inv(the_trap, new_trap); |
| 291 |
new_trap->stats.Cha = 10+RP->difficulty; |
| 292 |
new_trap->level = BC_RANDOM((3*RP->difficulty)/2); |
| 293 |
if (new_trap->level == 0) { |
| 294 |
new_trap->level = 1; |
| 295 |
} |
| 296 |
new_trap->x = x; |
| 297 |
new_trap->y = y; |
| 298 |
object_insert_in_ob(new_trap, the_chest); |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
if ((treasureoptions&KEYREQUIRED) && n_treasures > 1) { |
| 307 |
char keybuf[256]; |
| 308 |
|
| 309 |
snprintf(keybuf, sizeof(keybuf), "%d", (int)RANDOM()); |
| 310 |
if (keyplace(map, x, y, keybuf, PASS_DOORS, 1, RP)) { |
| 311 |
the_chest->slaying = add_string(keybuf); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
object_insert_in_map_at(the_chest, map, NULL, 0, xl, yl); |
| 317 |
return the_chest; |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
object *find_closest_monster(mapstruct *map, int x, int y, RMParms *RP) |
| 335 |
{ |
| 336 |
int i; |
| 337 |
|
| 338 |
for (i = 0; i < SIZEOFFREE; i++) { |
| 339 |
int lx, ly; |
| 340 |
|
| 341 |
lx = x+freearr_x[i]; |
| 342 |
ly = y+freearr_y[i]; |
| 343 |
|
| 344 |
if (lx >= 0 && ly >= 0 && lx < RP->Xsize && ly < RP->Ysize) |
| 345 |
|
| 346 |
if (GET_MAP_FLAGS(map, lx, ly)&P_IS_ALIVE) { |
| 347 |
FOR_MAP_PREPARE(map, lx, ly, the_monster) |
| 348 |
if (QUERY_FLAG(the_monster, FLAG_MONSTER)) { |
| 349 |
return the_monster; |
| 350 |
} |
| 351 |
FOR_MAP_FINISH(); |
| 352 |
} |
| 353 |
} |
| 354 |
return NULL; |
| 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 |
int keyplace(mapstruct *map, int x, int y, char *keycode, int door_flag, int n_keys, RMParms *RP) |
| 385 |
{ |
| 386 |
int i, j; |
| 387 |
int kx, ky; |
| 388 |
object *the_keymaster; |
| 389 |
object *the_key; |
| 390 |
char keybuf[256]; |
| 391 |
const char *keys[] = { "key2", "blue_key", "brown_key", "darkgray_key", "darkgreen_key", "gray_key", "green_key", "magenta_key", "red_key", "white_key" }; |
| 392 |
|
| 393 |
|
| 394 |
the_key = create_archetype(keys[RANDOM() % (sizeof(keys)/sizeof(*keys))]); |
| 395 |
the_key->slaying = add_string(keycode); |
| 396 |
free_string(the_key->name); |
| 397 |
snprintf(keybuf, 256, "key from level %d of %s", RP->dungeon_level-1, RP->dungeon_name[0] != '\0' ? RP->dungeon_name : "a random map"); |
| 398 |
the_key->name = add_string(keybuf); |
| 399 |
|
| 400 |
if (door_flag == PASS_DOORS) { |
| 401 |
int tries = 0; |
| 402 |
|
| 403 |
the_keymaster = NULL; |
| 404 |
while (tries < 15 && the_keymaster == NULL) { |
| 405 |
i = (RANDOM()%(RP->Xsize-2))+1; |
| 406 |
j = (RANDOM()%(RP->Ysize-2))+1; |
| 407 |
tries++; |
| 408 |
the_keymaster = find_closest_monster(map, i, j, RP); |
| 409 |
} |
| 410 |
|
| 411 |
if (the_keymaster == NULL) { |
| 412 |
int freeindex; |
| 413 |
|
| 414 |
freeindex = -1; |
| 415 |
for (tries = 0; tries < 15 && freeindex == -1; tries++) { |
| 416 |
kx = (RANDOM()%(RP->Xsize-2))+1; |
| 417 |
ky = (RANDOM()%(RP->Ysize-2))+1; |
| 418 |
freeindex = object_find_first_free_spot(the_key, map, kx, ky); |
| 419 |
} |
| 420 |
if (freeindex != -1) { |
| 421 |
kx += freearr_x[freeindex]; |
| 422 |
ky += freearr_y[freeindex]; |
| 423 |
} |
| 424 |
} |
| 425 |
} else { |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
if (n_keys == 1) { |
| 430 |
if (wall_blocked(map, x, y)) { |
| 431 |
return 0; |
| 432 |
} |
| 433 |
the_keymaster = find_monster_in_room(map, x, y, RP); |
| 434 |
if (the_keymaster == NULL) |
| 435 |
if (!find_spot_in_room(map, x, y, &kx, &ky, RP)) { |
| 436 |
return 0; |
| 437 |
} |
| 438 |
} else { |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
int sum = 0; |
| 443 |
int distance = 1; |
| 444 |
|
| 445 |
while (distance < 5) { |
| 446 |
|
| 447 |
sum += keyplace(map, x+distance, y, keycode, NO_PASS_DOORS, 1, RP); |
| 448 |
sum += keyplace(map, x, y+distance, keycode, NO_PASS_DOORS, 1, RP); |
| 449 |
sum += keyplace(map, x-distance, y, keycode, NO_PASS_DOORS, 1, RP); |
| 450 |
sum += keyplace(map, x, y-distance, keycode, NO_PASS_DOORS, 1, RP); |
| 451 |
if (sum < 2) { |
| 452 |
|
| 453 |
keyplace(map, x+distance, y+distance, keycode, NO_PASS_DOORS, 1, RP); |
| 454 |
keyplace(map, x+distance, y-distance, keycode, NO_PASS_DOORS, 1, RP); |
| 455 |
keyplace(map, x-distance, y+distance, keycode, NO_PASS_DOORS, 1, RP); |
| 456 |
keyplace(map, x-distance, y-distance, keycode, NO_PASS_DOORS, 1, RP); |
| 457 |
} |
| 458 |
if (sum > 0) { |
| 459 |
return 1; |
| 460 |
} |
| 461 |
distance++; |
| 462 |
} |
| 463 |
return 0; |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
if (the_keymaster == NULL) { |
| 468 |
object_insert_in_map_at(the_key, map, NULL, 0, kx, ky); |
| 469 |
return 1; |
| 470 |
} |
| 471 |
|
| 472 |
object_insert_in_ob(the_key, the_keymaster); |
| 473 |
return 1; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
object *find_monster_in_room_recursive(char **layout, mapstruct *map, int x, int y, RMParms *RP) |
| 492 |
{ |
| 493 |
int i, j; |
| 494 |
|
| 495 |
|
| 496 |
if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) { |
| 497 |
return NULL; |
| 498 |
} |
| 499 |
|
| 500 |
|
| 501 |
if (layout[x][y] != 0) { |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
layout[x][y] = 1; |
| 508 |
if (GET_MAP_FLAGS(map, x, y)&P_IS_ALIVE) { |
| 509 |
FOR_MAP_PREPARE(map, x, y, the_monster) |
| 510 |
if (QUERY_FLAG(the_monster, FLAG_ALIVE)) { |
| 511 |
return HEAD(the_monster); |
| 512 |
} |
| 513 |
FOR_MAP_FINISH(); |
| 514 |
} |
| 515 |
|
| 516 |
|
| 517 |
for (i = RANDOM()%8, j = 0; j < 8; i++, j++) { |
| 518 |
object *the_monster; |
| 519 |
|
| 520 |
the_monster = find_monster_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP); |
| 521 |
if (the_monster != NULL) { |
| 522 |
return the_monster; |
| 523 |
} |
| 524 |
} |
| 525 |
return NULL; |
| 526 |
} |
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
object *find_monster_in_room(mapstruct *map, int x, int y, RMParms *RP) |
| 543 |
{ |
| 544 |
char **layout2; |
| 545 |
int i, j; |
| 546 |
object *theMonsterToFind; |
| 547 |
|
| 548 |
layout2 = (char **)calloc(sizeof(char *), RP->Xsize); |
| 549 |
|
| 550 |
for (i = 0; i < RP->Xsize; i++) { |
| 551 |
layout2[i] = (char *)calloc(sizeof(char), RP->Ysize); |
| 552 |
for (j = 0; j < RP->Ysize; j++) { |
| 553 |
if (wall_blocked(map, i, j)) { |
| 554 |
layout2[i][j] = '#'; |
| 555 |
} |
| 556 |
} |
| 557 |
} |
| 558 |
theMonsterToFind = find_monster_in_room_recursive(layout2, map, x, y, RP); |
| 559 |
|
| 560 |
|
| 561 |
for (i = 0; i < RP->Xsize; i++) { |
| 562 |
free(layout2[i]); |
| 563 |
} |
| 564 |
free(layout2); |
| 565 |
|
| 566 |
return theMonsterToFind; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
typedef struct free_spots_struct { |
| 571 |
int *room_free_spots_x; |
| 572 |
int *room_free_spots_y; |
| 573 |
int number_of_free_spots_in_room; |
| 574 |
} free_spots_struct; |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
static void find_spot_in_room_recursive(char **layout, int x, int y, RMParms *RP, free_spots_struct *spots) |
| 591 |
{ |
| 592 |
int i, j; |
| 593 |
|
| 594 |
|
| 595 |
if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) { |
| 596 |
return; |
| 597 |
} |
| 598 |
|
| 599 |
|
| 600 |
if (layout[x][y] != 0) { |
| 601 |
return; |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
layout[x][y] = 1; |
| 607 |
spots->room_free_spots_x[spots->number_of_free_spots_in_room] = x; |
| 608 |
spots->room_free_spots_y[spots->number_of_free_spots_in_room] = y; |
| 609 |
spots->number_of_free_spots_in_room++; |
| 610 |
|
| 611 |
for (i = RANDOM()%8, j = 0; j < 8; i++, j++) { |
| 612 |
find_spot_in_room_recursive(layout, x+freearr_x[i%8+1], y+freearr_y[i%8+1], RP, spots); |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
int find_spot_in_room(mapstruct *map, int x, int y, int *kx, int *ky, RMParms *RP) |
| 635 |
{ |
| 636 |
char **layout2; |
| 637 |
int i, j; |
| 638 |
free_spots_struct spots; |
| 639 |
|
| 640 |
spots.number_of_free_spots_in_room = 0; |
| 641 |
spots.room_free_spots_x = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize); |
| 642 |
spots.room_free_spots_y = (int *)calloc(sizeof(int), RP->Xsize*RP->Ysize); |
| 643 |
|
| 644 |
layout2 = (char **)calloc(sizeof(char *), RP->Xsize); |
| 645 |
|
| 646 |
for (i = 0; i < RP->Xsize; i++) { |
| 647 |
layout2[i] = (char *)calloc(sizeof(char), RP->Ysize); |
| 648 |
for (j = 0; j < RP->Ysize; j++) { |
| 649 |
if (wall_blocked(map, i, j)) { |
| 650 |
layout2[i][j] = '#'; |
| 651 |
} |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
find_spot_in_room_recursive(layout2, x, y, RP, &spots); |
| 657 |
|
| 658 |
if (spots.number_of_free_spots_in_room > 0) { |
| 659 |
i = RANDOM()%spots.number_of_free_spots_in_room; |
| 660 |
*kx = spots.room_free_spots_x[i]; |
| 661 |
*ky = spots.room_free_spots_y[i]; |
| 662 |
} |
| 663 |
|
| 664 |
|
| 665 |
for (i = 0; i < RP->Xsize; i++) { |
| 666 |
free(layout2[i]); |
| 667 |
} |
| 668 |
free(layout2); |
| 669 |
free(spots.room_free_spots_x); |
| 670 |
free(spots.room_free_spots_y); |
| 671 |
|
| 672 |
if (spots.number_of_free_spots_in_room > 0) { |
| 673 |
return 1; |
| 674 |
} |
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
void find_enclosed_spot(mapstruct *map, int *cx, int *cy, RMParms *RP) |
| 691 |
{ |
| 692 |
int x, y; |
| 693 |
int i; |
| 694 |
|
| 695 |
x = *cx; |
| 696 |
y = *cy; |
| 697 |
|
| 698 |
for (i = 0; i <= SIZEOFFREE1; i++) { |
| 699 |
int lx, ly, sindex; |
| 700 |
lx = x+freearr_x[i]; |
| 701 |
ly = y+freearr_y[i]; |
| 702 |
sindex = surround_flag3(map, lx, ly, RP); |
| 703 |
|
| 704 |
if (sindex == 7 || sindex == 11 || sindex == 13 || sindex == 14) { |
| 705 |
*cx = lx; |
| 706 |
*cy = ly; |
| 707 |
return; |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
for (i = 0; i <= SIZEOFFREE1; i++) { |
| 714 |
int lx, ly, sindex; |
| 715 |
|
| 716 |
lx = x+freearr_x[i]; |
| 717 |
ly = y+freearr_y[i]; |
| 718 |
sindex = surround_flag3(map, lx, ly, RP); |
| 719 |
|
| 720 |
if (sindex == 3 || sindex == 5 || sindex == 9 || sindex == 6 || sindex == 10 || sindex == 12) { |
| 721 |
*cx = lx; |
| 722 |
*cy = ly; |
| 723 |
return; |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
for (i = 0; i <= SIZEOFFREE1; i++) { |
| 729 |
int lx, ly, sindex; |
| 730 |
|
| 731 |
lx = x+freearr_x[i]; |
| 732 |
ly = y+freearr_y[i]; |
| 733 |
sindex = surround_flag3(map, lx, ly, RP); |
| 734 |
|
| 735 |
if (sindex) { |
| 736 |
*cx = lx; |
| 737 |
*cy = ly; |
| 738 |
return; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
|
| 743 |
archetype *arch = find_archetype("chest"); |
| 744 |
if (arch != NULL) { |
| 745 |
i = object_find_first_free_spot(&arch->clone, map, x, y); |
| 746 |
if (i != -1 && i <= SIZEOFFREE1) { |
| 747 |
*cx = x+freearr_x[i]; |
| 748 |
*cy = y+freearr_y[i]; |
| 749 |
return; |
| 750 |
} |
| 751 |
} |
| 752 |
|
| 753 |
*cx = *cy = -1; |
| 754 |
} |
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
void remove_monsters(int x, int y, mapstruct *map) |
| 764 |
{ |
| 765 |
FOR_MAP_PREPARE(map, x, y, tmp) { |
| 766 |
if (QUERY_FLAG(tmp, FLAG_ALIVE)) { |
| 767 |
tmp = HEAD(tmp); |
| 768 |
object_remove(tmp); |
| 769 |
object_free_drop_inventory(tmp); |
| 770 |
tmp = GET_MAP_OB(map, x, y); |
| 771 |
if (tmp == NULL) { |
| 772 |
break; |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
FOR_MAP_FINISH(); |
| 777 |
} |
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
static object **surround_by_doors(mapstruct *map, char **layout, int x, int y, int opts) |
| 798 |
{ |
| 799 |
int i; |
| 800 |
const char *doors[2]; |
| 801 |
object **doorlist; |
| 802 |
int ndoors_made = 0; |
| 803 |
doorlist = (object **)calloc(9, sizeof(object *)); |
| 804 |
|
| 805 |
|
| 806 |
if (opts&DOORED) { |
| 807 |
doors[0] = "locked_door2"; |
| 808 |
doors[1] = "locked_door1"; |
| 809 |
} else { |
| 810 |
doors[0] = "door_1"; |
| 811 |
doors[1] = "door_2"; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
for (i = 1; i < 9; i++) { |
| 816 |
int x1 = x+freearr_x[i], y1 = y+freearr_y[i]; |
| 817 |
|
| 818 |
if (!wall_blocked(map, x1, y1) |
| 819 |
|| layout[x1][y1] == '>') { |
| 820 |
object *new_door = create_archetype((freearr_x[i] == 0) ? doors[1] : doors[0]); |
| 821 |
int16_t nx, ny; |
| 822 |
|
| 823 |
nx = x+freearr_x[i]; |
| 824 |
ny = y+freearr_y[i]; |
| 825 |
remove_monsters(nx, ny, map); |
| 826 |
object_insert_in_map_at(new_door, map, NULL, 0, nx, ny); |
| 827 |
doorlist[ndoors_made] = new_door; |
| 828 |
ndoors_made++; |
| 829 |
} |
| 830 |
} |
| 831 |
return doorlist; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
static object *door_in_square(mapstruct *map, int x, int y) |
| 846 |
{ |
| 847 |
FOR_MAP_PREPARE(map, x, y, tmp) |
| 848 |
if (tmp->type == DOOR || tmp->type == LOCKED_DOOR) { |
| 849 |
return tmp; |
| 850 |
} |
| 851 |
FOR_MAP_FINISH(); |
| 852 |
return NULL; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
void find_doors_in_room_recursive(char **layout, mapstruct *map, int x, int y, object **doorlist, int *ndoors, RMParms *RP) |
| 870 |
{ |
| 871 |
int i, j; |
| 872 |
object *door; |
| 873 |
|
| 874 |
|
| 875 |
if (!(x >= 0 && y >= 0 && x < RP->Xsize && y < RP->Ysize)) { |
| 876 |
return; |
| 877 |
} |
| 878 |
|
| 879 |
|
| 880 |
if (layout[x][y] == 1) { |
| 881 |
return; |
| 882 |
} |
| 883 |
|
| 884 |
|
| 885 |
if (layout[x][y] == '#') { |
| 886 |
layout[x][y] = 1; |
| 887 |
door = door_in_square(map, x, y); |
| 888 |
if (door != NULL) { |
| 889 |
doorlist[*ndoors] = door; |
| 890 |
if (*ndoors > 254) { |
| 891 |
LOG(llevError, "find_doors_in_room_recursive:Too many doors for memory allocated!\n"); |
| 892 |
return; |
| 893 |
} |
| 894 |
*ndoors = *ndoors+1; |
| 895 |
} |
| 896 |
} else { |
| 897 |
layout[x][y] = 1; |
| 898 |
|
| 899 |
for (i = RANDOM()%8, j = 0; j < 8; i++, j++) { |
| 900 |
find_doors_in_room_recursive(layout, map, x+freearr_x[i%8+1], y+freearr_y[i%8+1], doorlist, ndoors, RP); |
| 901 |
} |
| 902 |
} |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
object **find_doors_in_room(mapstruct *map, int x, int y, RMParms *RP) |
| 920 |
{ |
| 921 |
char **layout2; |
| 922 |
object **doorlist; |
| 923 |
int i, j; |
| 924 |
int ndoors = 0; |
| 925 |
|
| 926 |
doorlist = (object **)calloc(sizeof(*doorlist), 256); |
| 927 |
|
| 928 |
|
| 929 |
layout2 = (char **)calloc(sizeof(char *), RP->Xsize); |
| 930 |
|
| 931 |
for (i = 0; i < RP->Xsize; i++) { |
| 932 |
layout2[i] = (char *)calloc(sizeof(char), RP->Ysize); |
| 933 |
for (j = 0; j < RP->Ysize; j++) { |
| 934 |
if (wall_blocked(map, i, j)) { |
| 935 |
layout2[i][j] = '#'; |
| 936 |
} |
| 937 |
} |
| 938 |
} |
| 939 |
|
| 940 |
|
| 941 |
find_doors_in_room_recursive(layout2, map, x, y, doorlist, &ndoors, RP); |
| 942 |
|
| 943 |
|
| 944 |
for (i = 0; i < RP->Xsize; i++) { |
| 945 |
free(layout2[i]); |
| 946 |
} |
| 947 |
free(layout2); |
| 948 |
return doorlist; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
static void remove_adjacent_doors(object *door) |
| 961 |
{ |
| 962 |
mapstruct *m = door->map; |
| 963 |
int x = door->x; |
| 964 |
int y = door->y; |
| 965 |
int i, flags; |
| 966 |
|
| 967 |
for (i = 1; i <= 8; i++) { |
| 968 |
flags = get_map_flags(m, NULL, x+freearr_x[i], y+freearr_y[i], NULL, NULL); |
| 969 |
if (flags&P_OUT_OF_MAP) { |
| 970 |
continue; |
| 971 |
} |
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
if (flags&P_IS_ALIVE) { |
| 977 |
FOR_MAP_PREPARE(m, x+freearr_x[i], y+freearr_y[i], tmp) { |
| 978 |
if (tmp->type == DOOR) { |
| 979 |
object_remove(tmp); |
| 980 |
object_free_drop_inventory(tmp); |
| 981 |
break; |
| 982 |
} |
| 983 |
} |
| 984 |
FOR_MAP_FINISH(); |
| 985 |
} |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
void lock_and_hide_doors(object **doorlist, mapstruct *map, int opts, RMParms *RP) |
| 1005 |
{ |
| 1006 |
object *door; |
| 1007 |
int i; |
| 1008 |
|
| 1009 |
|
| 1010 |
if (opts&DOORED) { |
| 1011 |
for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) { |
| 1012 |
object *new_door = create_archetype("locked_door1"); |
| 1013 |
char keybuf[256]; |
| 1014 |
|
| 1015 |
door = doorlist[i]; |
| 1016 |
new_door->face = door->face; |
| 1017 |
object_remove(door); |
| 1018 |
object_free_drop_inventory(door); |
| 1019 |
doorlist[i] = new_door; |
| 1020 |
object_insert_in_map_at(new_door, map, NULL, 0, door->x, door->y); |
| 1021 |
|
| 1022 |
snprintf(keybuf, 256, "%d", (int)RANDOM()); |
| 1023 |
if (keyplace(map, new_door->x, new_door->y, keybuf, NO_PASS_DOORS, 2, RP)) { |
| 1024 |
new_door->slaying = add_string(keybuf); |
| 1025 |
} |
| 1026 |
} |
| 1027 |
for (i = 0; doorlist[i] != NULL; i++) { |
| 1028 |
remove_adjacent_doors(doorlist[i]); |
| 1029 |
} |
| 1030 |
} |
| 1031 |
|
| 1032 |
|
| 1033 |
if (opts&HIDDEN) { |
| 1034 |
for (i = 0, door = doorlist[0]; doorlist[i] != NULL; i++) { |
| 1035 |
object *wallface; |
| 1036 |
|
| 1037 |
door = doorlist[i]; |
| 1038 |
wallface = retrofit_joined_wall(map, door->x, door->y, 1, RP); |
| 1039 |
if (wallface != NULL) { |
| 1040 |
retrofit_joined_wall(map, door->x-1, door->y, 0, RP); |
| 1041 |
retrofit_joined_wall(map, door->x+1, door->y, 0, RP); |
| 1042 |
retrofit_joined_wall(map, door->x, door->y-1, 0, RP); |
| 1043 |
retrofit_joined_wall(map, door->x, door->y+1, 0, RP); |
| 1044 |
door->face = wallface->face; |
| 1045 |
if (!QUERY_FLAG(wallface, FLAG_REMOVED)) { |
| 1046 |
object_remove(wallface); |
| 1047 |
} |
| 1048 |
object_free_drop_inventory(wallface); |
| 1049 |
} |
| 1050 |
} |
| 1051 |
} |
| 1052 |
} |