3 * Purpose: low-level dungeon creation primitives
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
9 * This software may be copied and distributed for educational, research,
10 * and not for profit purposes provided that this copyright and statement
11 * are included in all such copies. Other copyrights may also apply.
20 * Returns random co-ordinates for player/monster/object
22 bool new_player_spot(void)
25 int max_attempts = 10000;
30 /* Place the player */
31 while (max_attempts--)
33 /* Pick a legal spot */
34 y = rand_range(1, cur_hgt - 2);
35 x = rand_range(1, cur_wid - 2);
39 /* Must be a "naked" floor grid */
40 if (c_ptr->m_idx) continue;
43 f_ptr = &f_info[c_ptr->feat];
45 if (max_attempts > 5000) /* Rule 1 */
47 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
51 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
52 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
55 /* Refuse to start on anti-teleport grids in dungeon */
56 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
58 if (!player_can_enter(c_ptr->feat, 0)) continue;
59 if (!in_bounds(y, x)) continue;
61 /* Refuse to start on anti-teleport grids */
62 if (c_ptr->info & (CAVE_ICKY)) continue;
68 if (max_attempts < 1) /* Should be -1, actually if we failed... */
71 /* Save the new player grid */
80 * Place an up/down staircase at given location
82 void place_random_stairs(int y, int x)
84 bool up_stairs = TRUE;
85 bool down_stairs = TRUE;
90 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) return;
101 if (dun_level >= d_info[dungeon_type].maxdepth)
105 if (quest_number(dun_level) && (dun_level > 1))
108 /* We can't place both */
109 if (down_stairs && up_stairs)
111 /* Choose a staircase randomly */
112 if (randint0(100) < 50)
118 /* Place the stairs */
120 place_up_stairs(y, x);
121 else if (down_stairs)
122 place_down_stairs(y, x);
127 * Place a random type of door at the given location
129 void place_random_door(int y, int x, bool room)
132 cave_type *c_ptr = &cave[y][x];
134 /* Initialize mimic info */
137 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
139 place_floor_bold(y, x);
143 /* Choose an object */
144 tmp = randint0(1000);
146 /* Open doors (300/1000) */
149 /* Create open door */
150 set_cave_feat(y, x, FEAT_OPEN);
153 /* Broken doors (100/1000) */
156 /* Create broken door */
157 set_cave_feat(y, x, FEAT_BROKEN);
160 /* Secret doors (200/1000) */
163 /* Create secret door */
164 place_closed_door(y, x);
166 /* Hide. If on the edge of room, use outer wall. */
167 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
169 /* Floor type terrain cannot hide a door */
170 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
172 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
174 c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
180 /* Closed, locked, or stuck doors (400/1000) */
181 else place_closed_door(y, x);
183 delete_monster(y, x);
188 * Place a random type of normal door at the given location.
190 void place_closed_door(int y, int x)
194 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
196 place_floor_bold(y, x);
200 /* Choose an object */
203 /* Closed doors (300/400) */
206 /* Create closed door */
207 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
210 /* Locked doors (99/400) */
213 /* Create locked door */
214 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
217 /* Stuck doors (1/400) */
220 /* Create jammed door */
221 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
224 /* Now it is not floor */
225 cave[y][x].info &= ~(CAVE_MASK);
230 * Make an empty square floor, for the middle of rooms
232 void place_floor(int x1, int x2, int y1, int y2, bool light)
236 /* Place a full floor under the room */
237 for (y = y1 - 1; y <= y2 + 1; y++)
239 for (x = x1 - 1; x <= x2 + 1; x++)
241 place_floor_bold(y, x);
242 add_cave_info(y, x, CAVE_ROOM);
243 if (light) add_cave_info(y, x, CAVE_GLOW);
250 * Make an empty square room, only floor and wall grids
252 void place_room(int x1, int x2, int y1, int y2, bool light)
256 place_floor(x1, x2, y1, y2, light);
258 /* Walls around the room */
259 for (y = y1 - 1; y <= y2 + 1; y++)
261 place_outer_bold(y, x1 - 1);
262 place_outer_bold(y, x2 + 1);
264 for (x = x1 - 1; x <= x2 + 1; x++)
266 place_outer_bold(y1 - 1, x);
267 place_outer_bold(y2 + 1, x);
273 * Create up to "num" objects near the given coordinates
274 * Only really called by some of the "vault" routines.
276 void vault_objects(int y, int x, int num)
279 int i = 0, j = y, k = x;
284 /* Attempt to place 'num' objects */
285 for (; num > 0; --num)
287 /* Try up to 11 spots looking for empty space */
288 for (i = 0; i < 11; ++i)
290 /* Pick a random location */
291 while (dummy < SAFE_MAX_ATTEMPTS)
293 j = rand_spread(y, 2);
294 k = rand_spread(x, 3);
296 if (!in_bounds(j, k)) continue;
301 if (dummy >= SAFE_MAX_ATTEMPTS)
306 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
308 msg_print("Warning! Could not place vault object!");
315 /* Require "clean" floor space */
317 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
320 if (randint0(100) < 75)
322 place_object(j, k, 0L);
331 /* Placement accomplished */
339 * Place a trap with a given displacement of point
341 void vault_trap_aux(int y, int x, int yd, int xd)
343 int count = 0, y1 = y, x1 = x;
349 for (count = 0; count <= 5; count++)
352 while (dummy < SAFE_MAX_ATTEMPTS)
354 y1 = rand_spread(y, yd);
355 x1 = rand_spread(x, xd);
357 if (!in_bounds(y1, x1)) continue;
361 if (dummy >= SAFE_MAX_ATTEMPTS)
366 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
368 msg_print("Warning! Could not place vault trap!");
374 /* Require "naked" floor grids */
375 c_ptr = &cave[y1][x1];
376 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
388 * Place some traps with a given displacement of given location
390 void vault_traps(int y, int x, int yd, int xd, int num)
394 for (i = 0; i < num; i++)
396 vault_trap_aux(y, x, yd, xd);
402 * Hack -- Place some sleeping monsters near the given location
404 void vault_monsters(int y1, int x1, int num)
409 /* Try to summon "num" monsters "near" the given location */
410 for (k = 0; k < num; k++)
412 /* Try nine locations */
413 for (i = 0; i < 9; i++)
417 /* Pick a nearby location */
418 scatter(&y, &x, y1, x1, d, 0);
420 /* Require "empty" floor grids */
422 if (!cave_empty_grid(c_ptr)) continue;
424 /* Place the monster (allow groups) */
425 monster_level = base_level + 2;
426 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
427 monster_level = base_level;
434 * Always picks a correct direction
436 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
438 /* Extract vertical and horizontal directions */
439 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
440 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
442 /* Never move diagonally */
445 if (randint0(100) < 50)
454 * Pick a random direction
456 void rand_dir(int *rdir, int *cdir)
458 /* Pick a random direction */
461 /* Extract the dy/dx components */
467 /* Function that sees if a square is a floor. (Includes range checking.) */
468 bool get_is_floor(int x, int y)
470 if (!in_bounds(y, x))
476 /* Do the real check */
477 if (is_floor_bold(y, x)) return (TRUE);
483 /* Set a square to be floor. (Includes range checking.) */
484 void set_floor(int x, int y)
486 if (!in_bounds(y, x))
492 if (cave[y][x].info & CAVE_ROOM)
494 /* A room border don't touch. */
498 /* Set to be floor if is a wall (don't touch lakes). */
499 if (is_extra_bold(y, x))
500 place_floor_bold(y, x);
506 * Constructs a tunnel between two points
508 * This function must be called BEFORE any streamers are created,
509 * since we use the special "granite wall" sub-types to keep track
510 * of legal places for corridors to pierce rooms.
512 * We use "door_flag" to prevent excessive construction of doors
513 * along overlapping corridors.
515 * We queue the tunnel grids to prevent door creation along a corridor
516 * which intersects itself.
518 * We queue the wall piercing grids to prevent a corridor from leaving
519 * a room and then coming back in through the same entrance.
521 * We "pierce" grids which are "outer" walls of rooms, and when we
522 * do so, we change all adjacent "outer" walls of rooms into "solid"
523 * walls so that no two corridors may use adjacent grids for exits.
525 * The "solid" wall check prevents corridors from "chopping" the
526 * corners of rooms off, as well as "silly" door placement, and
527 * "excessively wide" room entrances.
531 * inner -- inner room walls
532 * outer -- outer room walls
533 * solid -- solid room walls
535 void build_tunnel(int row1, int col1, int row2, int col2)
538 int tmp_row, tmp_col;
539 int row_dir, col_dir;
540 int start_row, start_col;
541 int main_loop_count = 0;
543 bool door_flag = FALSE;
547 /* Save the starting location */
551 /* Start out in the correct direction */
552 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
554 /* Keep going until done (or bored) */
555 while ((row1 != row2) || (col1 != col2))
557 /* Mega-Hack -- Paranoia -- prevent infinite loops */
558 if (main_loop_count++ > 2000) break;
560 /* Allow bends in the tunnel */
561 if (randint0(100) < dun_tun_chg)
563 /* Acquire the correct direction */
564 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
566 /* Random direction */
567 if (randint0(100) < dun_tun_rnd)
569 rand_dir(&row_dir, &col_dir);
573 /* Get the next location */
574 tmp_row = row1 + row_dir;
575 tmp_col = col1 + col_dir;
578 /* Extremely Important -- do not leave the dungeon */
579 while (!in_bounds(tmp_row, tmp_col))
581 /* Acquire the correct direction */
582 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
584 /* Random direction */
585 if (randint0(100) < dun_tun_rnd)
587 rand_dir(&row_dir, &col_dir);
590 /* Get the next location */
591 tmp_row = row1 + row_dir;
592 tmp_col = col1 + col_dir;
596 /* Access the location */
597 c_ptr = &cave[tmp_row][tmp_col];
599 if (permanent_wall(&f_info[c_ptr->feat]))
601 /* Avoid the edge of vaults */
602 if (is_inner_grid(c_ptr)) continue;
605 /* Avoid "solid" walls */
606 if (is_solid_grid(c_ptr)) continue;
608 /* Pierce "outer" walls of rooms */
609 if (is_outer_grid(c_ptr))
611 /* Acquire the "next" location */
612 y = tmp_row + row_dir;
613 x = tmp_col + col_dir;
615 /* Hack -- Avoid outer/solid walls */
616 if (is_outer_bold(y, x)) continue;
617 if (is_solid_bold(y, x)) continue;
619 /* Accept this location */
623 /* Save the wall location */
624 if (dun->wall_n < WALL_MAX)
626 dun->wall[dun->wall_n].y = row1;
627 dun->wall[dun->wall_n].x = col1;
631 /* Forbid re-entry near this piercing */
632 for (y = row1 - 1; y <= row1 + 1; y++)
634 for (x = col1 - 1; x <= col1 + 1; x++)
636 /* Convert adjacent "outer" walls as "solid" walls */
637 if (is_outer_bold(y, x))
639 /* Change the wall to a "solid" wall */
640 place_solid_noperm_bold(y, x);
646 /* Travel quickly through rooms */
647 else if (c_ptr->info & (CAVE_ROOM))
649 /* Accept the location */
654 /* Tunnel through all other walls */
655 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
657 /* Accept this location */
661 /* Save the tunnel location */
662 if (dun->tunn_n < TUNN_MAX)
664 dun->tunn[dun->tunn_n].y = row1;
665 dun->tunn[dun->tunn_n].x = col1;
669 /* Allow door in next grid */
673 /* Handle corridor intersections or overlaps */
676 /* Accept the location */
680 /* Collect legal door locations */
683 /* Save the door location */
684 if (dun->door_n < DOOR_MAX)
686 dun->door[dun->door_n].y = row1;
687 dun->door[dun->door_n].x = col1;
691 /* No door in next grid */
695 /* Hack -- allow pre-emptive tunnel termination */
696 if (randint0(100) >= dun_tun_con)
698 /* Distance between row1 and start_row */
699 tmp_row = row1 - start_row;
700 if (tmp_row < 0) tmp_row = (-tmp_row);
702 /* Distance between col1 and start_col */
703 tmp_col = col1 - start_col;
704 if (tmp_col < 0) tmp_col = (-tmp_col);
706 /* Terminate the tunnel */
707 if ((tmp_row > 10) || (tmp_col > 10)) break;
715 * This routine adds the square to the tunnel
716 * It also checks for SOLID walls - and returns a nearby
717 * non-SOLID square in (x,y) so that a simple avoiding
718 * routine can be used. The returned boolean value reflects
719 * whether or not this routine hit a SOLID wall.
721 * "affectwall" toggles whether or not this new square affects
722 * the boundaries of rooms. - This is used by the catacomb
725 static bool set_tunnel(int *x, int *y, bool affectwall)
729 cave_type *c_ptr = &cave[*y][*x];
731 if (!in_bounds(*y, *x)) return TRUE;
733 if (is_inner_grid(c_ptr))
738 if (is_extra_bold(*y,*x))
740 /* Save the tunnel location */
741 if (dun->tunn_n < TUNN_MAX)
743 dun->tunn[dun->tunn_n].y = *y;
744 dun->tunn[dun->tunn_n].x = *x;
751 if (is_floor_bold(*y, *x))
753 /* Don't do anything */
757 if (is_outer_grid(c_ptr) && affectwall)
759 /* Save the wall location */
760 if (dun->wall_n < WALL_MAX)
762 dun->wall[dun->wall_n].y = *y;
763 dun->wall[dun->wall_n].x = *x;
767 /* Forbid re-entry near this piercing */
768 for (j = *y - 1; j <= *y + 1; j++)
770 for (i = *x - 1; i <= *x + 1; i++)
772 /* Convert adjacent "outer" walls as "solid" walls */
773 if (is_outer_bold(j, i))
775 /* Change the wall to a "solid" wall */
776 place_solid_noperm_bold(j, i);
781 /* Clear mimic type */
782 cave[*y][*x].mimic = 0;
784 place_floor_bold(*y, *x);
789 if (is_solid_grid(c_ptr) && affectwall)
791 /* cannot place tunnel here - use a square to the side */
793 /* find usable square and return value in (x,y) */
799 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
801 dy = randint0(3) - 1;
802 dx = randint0(3) - 1;
804 if (!in_bounds(*y + dy, *x + dx))
815 /* Failed for some reason: hack - ignore the solidness */
816 place_outer_grid(c_ptr);
821 /* Give new, acceptable coordinate. */
833 * This routine creates the catacomb-like tunnels by removing extra rock.
834 * Note that this routine is only called on "even" squares - so it gives
835 * a natural checkerboard pattern.
837 static void create_cata_tunnel(int x, int y)
844 set_tunnel(&x1, &y1, FALSE);
848 set_tunnel(&x1, &y1, FALSE);
852 set_tunnel(&x1, &y1, FALSE);
856 set_tunnel(&x1, &y1, FALSE);
861 * This routine does the bulk of the work in creating the new types of tunnels.
862 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
863 * It doesn't need to add any complexity - straight lines are fine.
864 * The SOLID walls are avoided by a recursive algorithm which tries random ways
865 * around the obstical until it works. The number of itterations is counted, and it
866 * this gets too large the routine exits. This should stop any crashes - but may leave
867 * small gaps in the tunnel where there are too many SOLID walls.
869 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
870 * as a part of the dodge SOLID walls algorithm.
872 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
873 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
875 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
876 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
879 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
884 /* Check for early exit */
885 if (!(*fail)) return;
887 length = distance(x1, y1, x2, y2);
891 if ((type == 1) && (length != 0))
894 for (i = 0; i <= length; i++)
896 x = x1 + i * (x2 - x1) / length;
897 y = y1 + i * (y2 - y1) / length;
898 if (!set_tunnel(&x, &y, TRUE))
902 /* This isn't working - probably have an infinite loop */
907 /* solid wall - so try to go around */
908 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
909 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
913 else if ((type == 2) || (type == 3))
917 for (i = x1; i <= x2; i++)
921 if (!set_tunnel(&x, &y, TRUE))
923 /* solid wall - so try to go around */
924 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
925 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
927 if ((type == 3) && ((x + y) % 2))
929 create_cata_tunnel(i, y1);
935 for (i = x2; i <= x1; i++)
939 if (!set_tunnel(&x, &y, TRUE))
941 /* solid wall - so try to go around */
942 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
943 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
945 if ((type == 3) && ((x + y) % 2))
947 create_cata_tunnel(i, y1);
954 for (i = y1; i <= y2; i++)
958 if (!set_tunnel(&x, &y, TRUE))
960 /* solid wall - so try to go around */
961 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
962 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
964 if ((type == 3) && ((x + y) % 2))
966 create_cata_tunnel(x2, i);
972 for (i = y2; i <= y1; i++)
976 if (!set_tunnel(&x, &y, TRUE))
978 /* solid wall - so try to go around */
979 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
980 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
982 if ((type == 3) && ((x + y) % 2))
984 create_cata_tunnel(x2, i);
993 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
994 * Permanent rock is ignored in this path finding- sometimes there is no
995 * path around anyway -so there will be a crash if we try to find one.
996 * This routine is much like the river creation routine in Zangband.
997 * It works by dividing a line segment into two. The segments are divided
998 * until they are less than "cutoff" - when the corresponding routine from
999 * "short_seg_hack" is called.
1000 * Note it is VERY important that the "stop if hit another passage" logic
1001 * stays as is. Without this the dungeon turns into Swiss Cheese...
1003 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1006 int changex, changey;
1009 bool retval, firstsuccede;
1012 length = distance(x1, y1, x2, y2);
1014 if (length > cutoff)
1017 * Divide path in half and call routine twice.
1022 /* perturbation perpendicular to path */
1023 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1025 /* perturbation perpendicular to path */
1026 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1028 /* Work out "mid" ponit */
1029 x3 = x1 + dx + changex;
1030 y3 = y1 + dy + changey;
1032 /* See if in bounds - if not - do not perturb point */
1033 if (!in_bounds(y3, x3))
1039 c_ptr = &cave[y3][x3];
1040 if (is_solid_grid(c_ptr))
1042 /* move midpoint a bit to avoid problem. */
1048 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1050 dy = randint0(3) - 1;
1051 dx = randint0(3) - 1;
1052 if (!in_bounds(y3 + dy, x3 + dx))
1062 /* Failed for some reason: hack - ignore the solidness */
1063 place_outer_bold(y3, x3);
1069 c_ptr = &cave[y3][x3];
1072 if (is_floor_grid(c_ptr))
1074 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1076 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1078 /* do second half only if works + if have hit a room */
1079 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1083 /* have hit another tunnel - make a set of doors here */
1086 /* Save the door location */
1087 if (dun->door_n < DOOR_MAX)
1089 dun->door[dun->door_n].y = y3;
1090 dun->door[dun->door_n].x = x3;
1094 firstsuccede = TRUE;
1098 /* false- didn't work all the way */
1100 firstsuccede = FALSE;
1105 /* tunnel through walls */
1106 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1108 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1109 firstsuccede = TRUE;
1113 /* false- didn't work all the way */
1115 firstsuccede = FALSE;
1120 /* only do this if the first half has worked */
1121 set_tunnel(&x3, &y3, TRUE);
1123 /* return value calculated above */
1128 /* Do a short segment */
1130 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1132 /* Hack - ignore return value so avoid infinite loops */