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 s16b feat = feat_none;
133 cave_type *c_ptr = &cave[y][x];
135 /* Initialize mimic info */
138 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
140 place_floor_bold(y, x);
144 type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
145 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
146 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
148 /* Choose an object */
149 tmp = randint0(1000);
151 /* Open doors (300/1000) */
154 /* Create open door */
155 feat = feat_door[type].open;
158 /* Broken doors (100/1000) */
161 /* Create broken door */
162 feat = feat_door[type].broken;
165 /* Secret doors (200/1000) */
168 /* Create secret door */
169 place_closed_door(y, x, type);
171 if (type != DOOR_CURTAIN)
173 /* Hide. If on the edge of room, use outer wall. */
174 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
176 /* Floor type terrain cannot hide a door */
177 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
179 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
181 c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
188 /* Closed, locked, or stuck doors (400/1000) */
189 else place_closed_door(y, x, type);
193 if (feat != feat_none)
195 set_cave_feat(y, x, feat);
199 place_floor_bold(y, x);
203 delete_monster(y, x);
208 * Place a random type of normal door at the given location.
210 void place_closed_door(int y, int x, int type)
213 s16b feat = feat_none;
215 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
217 place_floor_bold(y, x);
221 /* Choose an object */
224 /* Closed doors (300/400) */
227 /* Create closed door */
228 feat = feat_door[type].closed;
231 /* Locked doors (99/400) */
234 /* Create locked door */
235 feat = feat_locked_door_random(type);
238 /* Stuck doors (1/400) */
241 /* Create jammed door */
242 feat = feat_jammed_door_random(type);
245 if (feat != feat_none)
247 cave_set_feat(y, x, feat);
249 /* Now it is not floor */
250 cave[y][x].info &= ~(CAVE_MASK);
254 place_floor_bold(y, x);
260 * Make an empty square floor, for the middle of rooms
262 void place_floor(int x1, int x2, int y1, int y2, bool light)
266 /* Place a full floor under the room */
267 for (y = y1 - 1; y <= y2 + 1; y++)
269 for (x = x1 - 1; x <= x2 + 1; x++)
271 place_floor_bold(y, x);
272 add_cave_info(y, x, CAVE_ROOM);
273 if (light) add_cave_info(y, x, CAVE_GLOW);
280 * Make an empty square room, only floor and wall grids
282 void place_room(int x1, int x2, int y1, int y2, bool light)
286 place_floor(x1, x2, y1, y2, light);
288 /* Walls around the room */
289 for (y = y1 - 1; y <= y2 + 1; y++)
291 place_outer_bold(y, x1 - 1);
292 place_outer_bold(y, x2 + 1);
294 for (x = x1 - 1; x <= x2 + 1; x++)
296 place_outer_bold(y1 - 1, x);
297 place_outer_bold(y2 + 1, x);
303 * Create up to "num" objects near the given coordinates
304 * Only really called by some of the "vault" routines.
306 void vault_objects(int y, int x, int num)
309 int i = 0, j = y, k = x;
314 /* Attempt to place 'num' objects */
315 for (; num > 0; --num)
317 /* Try up to 11 spots looking for empty space */
318 for (i = 0; i < 11; ++i)
320 /* Pick a random location */
321 while (dummy < SAFE_MAX_ATTEMPTS)
323 j = rand_spread(y, 2);
324 k = rand_spread(x, 3);
326 if (!in_bounds(j, k)) continue;
331 if (dummy >= SAFE_MAX_ATTEMPTS)
336 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
338 msg_print("Warning! Could not place vault object!");
345 /* Require "clean" floor space */
347 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
350 if (randint0(100) < 75)
352 place_object(j, k, 0L);
361 /* Placement accomplished */
369 * Place a trap with a given displacement of point
371 void vault_trap_aux(int y, int x, int yd, int xd)
373 int count = 0, y1 = y, x1 = x;
379 for (count = 0; count <= 5; count++)
382 while (dummy < SAFE_MAX_ATTEMPTS)
384 y1 = rand_spread(y, yd);
385 x1 = rand_spread(x, xd);
387 if (!in_bounds(y1, x1)) continue;
391 if (dummy >= SAFE_MAX_ATTEMPTS)
396 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
398 msg_print("Warning! Could not place vault trap!");
404 /* Require "naked" floor grids */
405 c_ptr = &cave[y1][x1];
406 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
418 * Place some traps with a given displacement of given location
420 void vault_traps(int y, int x, int yd, int xd, int num)
424 for (i = 0; i < num; i++)
426 vault_trap_aux(y, x, yd, xd);
432 * Hack -- Place some sleeping monsters near the given location
434 void vault_monsters(int y1, int x1, int num)
439 /* Try to summon "num" monsters "near" the given location */
440 for (k = 0; k < num; k++)
442 /* Try nine locations */
443 for (i = 0; i < 9; i++)
447 /* Pick a nearby location */
448 scatter(&y, &x, y1, x1, d, 0);
450 /* Require "empty" floor grids */
452 if (!cave_empty_grid(c_ptr)) continue;
454 /* Place the monster (allow groups) */
455 monster_level = base_level + 2;
456 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
457 monster_level = base_level;
464 * Always picks a correct direction
466 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
468 /* Extract vertical and horizontal directions */
469 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
470 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
472 /* Never move diagonally */
475 if (randint0(100) < 50)
484 * Pick a random direction
486 void rand_dir(int *rdir, int *cdir)
488 /* Pick a random direction */
491 /* Extract the dy/dx components */
497 /* Function that sees if a square is a floor. (Includes range checking.) */
498 bool get_is_floor(int x, int y)
500 if (!in_bounds(y, x))
506 /* Do the real check */
507 if (is_floor_bold(y, x)) return (TRUE);
513 /* Set a square to be floor. (Includes range checking.) */
514 void set_floor(int x, int y)
516 if (!in_bounds(y, x))
522 if (cave[y][x].info & CAVE_ROOM)
524 /* A room border don't touch. */
528 /* Set to be floor if is a wall (don't touch lakes). */
529 if (is_extra_bold(y, x))
530 place_floor_bold(y, x);
536 * Constructs a tunnel between two points
538 * This function must be called BEFORE any streamers are created,
539 * since we use the special "granite wall" sub-types to keep track
540 * of legal places for corridors to pierce rooms.
542 * We use "door_flag" to prevent excessive construction of doors
543 * along overlapping corridors.
545 * We queue the tunnel grids to prevent door creation along a corridor
546 * which intersects itself.
548 * We queue the wall piercing grids to prevent a corridor from leaving
549 * a room and then coming back in through the same entrance.
551 * We "pierce" grids which are "outer" walls of rooms, and when we
552 * do so, we change all adjacent "outer" walls of rooms into "solid"
553 * walls so that no two corridors may use adjacent grids for exits.
555 * The "solid" wall check prevents corridors from "chopping" the
556 * corners of rooms off, as well as "silly" door placement, and
557 * "excessively wide" room entrances.
561 * inner -- inner room walls
562 * outer -- outer room walls
563 * solid -- solid room walls
565 void build_tunnel(int row1, int col1, int row2, int col2)
568 int tmp_row, tmp_col;
569 int row_dir, col_dir;
570 int start_row, start_col;
571 int main_loop_count = 0;
573 bool door_flag = FALSE;
577 /* Save the starting location */
581 /* Start out in the correct direction */
582 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
584 /* Keep going until done (or bored) */
585 while ((row1 != row2) || (col1 != col2))
587 /* Mega-Hack -- Paranoia -- prevent infinite loops */
588 if (main_loop_count++ > 2000) break;
590 /* Allow bends in the tunnel */
591 if (randint0(100) < dun_tun_chg)
593 /* Acquire the correct direction */
594 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
596 /* Random direction */
597 if (randint0(100) < dun_tun_rnd)
599 rand_dir(&row_dir, &col_dir);
603 /* Get the next location */
604 tmp_row = row1 + row_dir;
605 tmp_col = col1 + col_dir;
608 /* Extremely Important -- do not leave the dungeon */
609 while (!in_bounds(tmp_row, tmp_col))
611 /* Acquire the correct direction */
612 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
614 /* Random direction */
615 if (randint0(100) < dun_tun_rnd)
617 rand_dir(&row_dir, &col_dir);
620 /* Get the next location */
621 tmp_row = row1 + row_dir;
622 tmp_col = col1 + col_dir;
626 /* Access the location */
627 c_ptr = &cave[tmp_row][tmp_col];
629 if (permanent_wall(&f_info[c_ptr->feat]))
631 /* Avoid the edge of vaults */
632 if (is_outer_grid(c_ptr)) continue;
635 /* Avoid "solid" walls */
636 if (is_solid_grid(c_ptr)) continue;
638 /* Pierce "outer" walls of rooms */
639 if (is_outer_grid(c_ptr))
641 /* Acquire the "next" location */
642 y = tmp_row + row_dir;
643 x = tmp_col + col_dir;
645 /* Hack -- Avoid outer/solid walls */
646 if (is_outer_bold(y, x)) continue;
647 if (is_solid_bold(y, x)) continue;
649 /* Accept this location */
653 /* Save the wall location */
654 if (dun->wall_n < WALL_MAX)
656 dun->wall[dun->wall_n].y = row1;
657 dun->wall[dun->wall_n].x = col1;
661 /* Forbid re-entry near this piercing */
662 for (y = row1 - 1; y <= row1 + 1; y++)
664 for (x = col1 - 1; x <= col1 + 1; x++)
666 /* Convert adjacent "outer" walls as "solid" walls */
667 if (is_outer_bold(y, x))
669 /* Change the wall to a "solid" wall */
670 place_solid_noperm_bold(y, x);
676 /* Travel quickly through rooms */
677 else if (c_ptr->info & (CAVE_ROOM))
679 /* Accept the location */
684 /* Tunnel through all other walls */
685 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
687 /* Accept this location */
691 /* Save the tunnel location */
692 if (dun->tunn_n < TUNN_MAX)
694 dun->tunn[dun->tunn_n].y = row1;
695 dun->tunn[dun->tunn_n].x = col1;
699 /* Allow door in next grid */
703 /* Handle corridor intersections or overlaps */
706 /* Accept the location */
710 /* Collect legal door locations */
713 /* Save the door location */
714 if (dun->door_n < DOOR_MAX)
716 dun->door[dun->door_n].y = row1;
717 dun->door[dun->door_n].x = col1;
721 /* No door in next grid */
725 /* Hack -- allow pre-emptive tunnel termination */
726 if (randint0(100) >= dun_tun_con)
728 /* Distance between row1 and start_row */
729 tmp_row = row1 - start_row;
730 if (tmp_row < 0) tmp_row = (-tmp_row);
732 /* Distance between col1 and start_col */
733 tmp_col = col1 - start_col;
734 if (tmp_col < 0) tmp_col = (-tmp_col);
736 /* Terminate the tunnel */
737 if ((tmp_row > 10) || (tmp_col > 10)) break;
745 * This routine adds the square to the tunnel
746 * It also checks for SOLID walls - and returns a nearby
747 * non-SOLID square in (x,y) so that a simple avoiding
748 * routine can be used. The returned boolean value reflects
749 * whether or not this routine hit a SOLID wall.
751 * "affectwall" toggles whether or not this new square affects
752 * the boundaries of rooms. - This is used by the catacomb
755 static bool set_tunnel(int *x, int *y, bool affectwall)
759 cave_type *c_ptr = &cave[*y][*x];
761 if (!in_bounds(*y, *x)) return TRUE;
763 if (is_inner_grid(c_ptr))
768 if (is_extra_bold(*y,*x))
770 /* Save the tunnel location */
771 if (dun->tunn_n < TUNN_MAX)
773 dun->tunn[dun->tunn_n].y = *y;
774 dun->tunn[dun->tunn_n].x = *x;
781 if (is_floor_bold(*y, *x))
783 /* Don't do anything */
787 if (is_outer_grid(c_ptr) && affectwall)
789 /* Save the wall location */
790 if (dun->wall_n < WALL_MAX)
792 dun->wall[dun->wall_n].y = *y;
793 dun->wall[dun->wall_n].x = *x;
797 /* Forbid re-entry near this piercing */
798 for (j = *y - 1; j <= *y + 1; j++)
800 for (i = *x - 1; i <= *x + 1; i++)
802 /* Convert adjacent "outer" walls as "solid" walls */
803 if (is_outer_bold(j, i))
805 /* Change the wall to a "solid" wall */
806 place_solid_noperm_bold(j, i);
811 /* Clear mimic type */
812 cave[*y][*x].mimic = 0;
814 place_floor_bold(*y, *x);
819 if (is_solid_grid(c_ptr) && affectwall)
821 /* cannot place tunnel here - use a square to the side */
823 /* find usable square and return value in (x,y) */
829 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
831 dy = randint0(3) - 1;
832 dx = randint0(3) - 1;
834 if (!in_bounds(*y + dy, *x + dx))
845 /* Failed for some reason: hack - ignore the solidness */
846 place_outer_grid(c_ptr);
851 /* Give new, acceptable coordinate. */
863 * This routine creates the catacomb-like tunnels by removing extra rock.
864 * Note that this routine is only called on "even" squares - so it gives
865 * a natural checkerboard pattern.
867 static void create_cata_tunnel(int x, int y)
874 set_tunnel(&x1, &y1, FALSE);
878 set_tunnel(&x1, &y1, FALSE);
882 set_tunnel(&x1, &y1, FALSE);
886 set_tunnel(&x1, &y1, FALSE);
891 * This routine does the bulk of the work in creating the new types of tunnels.
892 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
893 * It doesn't need to add any complexity - straight lines are fine.
894 * The SOLID walls are avoided by a recursive algorithm which tries random ways
895 * around the obstical until it works. The number of itterations is counted, and it
896 * this gets too large the routine exits. This should stop any crashes - but may leave
897 * small gaps in the tunnel where there are too many SOLID walls.
899 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
900 * as a part of the dodge SOLID walls algorithm.
902 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
903 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
905 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
906 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
909 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
914 /* Check for early exit */
915 if (!(*fail)) return;
917 length = distance(x1, y1, x2, y2);
921 if ((type == 1) && (length != 0))
924 for (i = 0; i <= length; i++)
926 x = x1 + i * (x2 - x1) / length;
927 y = y1 + i * (y2 - y1) / length;
928 if (!set_tunnel(&x, &y, TRUE))
932 /* This isn't working - probably have an infinite loop */
937 /* solid wall - so try to go around */
938 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
939 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
943 else if ((type == 2) || (type == 3))
947 for (i = x1; i <= x2; i++)
951 if (!set_tunnel(&x, &y, TRUE))
953 /* solid wall - so try to go around */
954 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
955 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
957 if ((type == 3) && ((x + y) % 2))
959 create_cata_tunnel(i, y1);
965 for (i = x2; i <= x1; i++)
969 if (!set_tunnel(&x, &y, TRUE))
971 /* solid wall - so try to go around */
972 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
973 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
975 if ((type == 3) && ((x + y) % 2))
977 create_cata_tunnel(i, y1);
984 for (i = y1; i <= y2; i++)
988 if (!set_tunnel(&x, &y, TRUE))
990 /* solid wall - so try to go around */
991 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
992 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
994 if ((type == 3) && ((x + y) % 2))
996 create_cata_tunnel(x2, i);
1002 for (i = y2; i <= y1; i++)
1006 if (!set_tunnel(&x, &y, TRUE))
1008 /* solid wall - so try to go around */
1009 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1010 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1012 if ((type == 3) && ((x + y) % 2))
1014 create_cata_tunnel(x2, i);
1023 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1024 * Permanent rock is ignored in this path finding- sometimes there is no
1025 * path around anyway -so there will be a crash if we try to find one.
1026 * This routine is much like the river creation routine in Zangband.
1027 * It works by dividing a line segment into two. The segments are divided
1028 * until they are less than "cutoff" - when the corresponding routine from
1029 * "short_seg_hack" is called.
1030 * Note it is VERY important that the "stop if hit another passage" logic
1031 * stays as is. Without this the dungeon turns into Swiss Cheese...
1033 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1036 int changex, changey;
1039 bool retval, firstsuccede;
1042 length = distance(x1, y1, x2, y2);
1044 if (length > cutoff)
1047 * Divide path in half and call routine twice.
1052 /* perturbation perpendicular to path */
1053 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1055 /* perturbation perpendicular to path */
1056 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1058 /* Work out "mid" ponit */
1059 x3 = x1 + dx + changex;
1060 y3 = y1 + dy + changey;
1062 /* See if in bounds - if not - do not perturb point */
1063 if (!in_bounds(y3, x3))
1069 c_ptr = &cave[y3][x3];
1070 if (is_solid_grid(c_ptr))
1072 /* move midpoint a bit to avoid problem. */
1078 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1080 dy = randint0(3) - 1;
1081 dx = randint0(3) - 1;
1082 if (!in_bounds(y3 + dy, x3 + dx))
1092 /* Failed for some reason: hack - ignore the solidness */
1093 place_outer_bold(y3, x3);
1099 c_ptr = &cave[y3][x3];
1102 if (is_floor_grid(c_ptr))
1104 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1106 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1108 /* do second half only if works + if have hit a room */
1109 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1113 /* have hit another tunnel - make a set of doors here */
1116 /* Save the door location */
1117 if (dun->door_n < DOOR_MAX)
1119 dun->door[dun->door_n].y = y3;
1120 dun->door[dun->door_n].x = x3;
1124 firstsuccede = TRUE;
1128 /* false- didn't work all the way */
1130 firstsuccede = FALSE;
1135 /* tunnel through walls */
1136 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1138 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1139 firstsuccede = TRUE;
1143 /* false- didn't work all the way */
1145 firstsuccede = FALSE;
1150 /* only do this if the first half has worked */
1151 set_tunnel(&x3, &y3, TRUE);
1153 /* return value calculated above */
1158 /* Do a short segment */
1160 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1162 /* Hack - ignore return value so avoid infinite loops */