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 int type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
136 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN : DOOR_DOOR;
138 /* Initialize mimic info */
141 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
143 place_floor_bold(y, x);
147 /* Choose an object */
148 tmp = randint0(1000);
150 /* Open doors (300/1000) */
153 /* Create open door */
154 feat = feat_door[type].open;
157 /* Broken doors (100/1000) */
160 /* Create broken door */
161 feat = feat_door[type].broken;
164 /* Secret doors (200/1000) */
167 /* Create secret door */
168 place_closed_door(y, x, type);
170 if (type != DOOR_CURTAIN)
172 /* Hide. If on the edge of room, use outer wall. */
173 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
175 /* Floor type terrain cannot hide a door */
176 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
178 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
180 c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
187 /* Closed, locked, or stuck doors (400/1000) */
188 else place_closed_door(y, x, type);
192 if (feat != feat_none)
194 set_cave_feat(y, x, feat);
198 place_floor_bold(y, x);
202 delete_monster(y, x);
207 * Place a random type of normal door at the given location.
209 void place_closed_door(int y, int x, int type)
212 s16b feat = feat_none;
214 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
216 place_floor_bold(y, x);
220 /* Choose an object */
223 /* Closed doors (300/400) */
226 /* Create closed door */
227 feat = feat_door[type].closed;
230 /* Locked doors (99/400) */
233 /* Create locked door */
234 feat = feat_locked_door_random(type);
237 /* Stuck doors (1/400) */
240 /* Create jammed door */
241 feat = feat_jammed_door_random(type);
244 if (feat != feat_none)
246 cave_set_feat(y, x, feat);
248 /* Now it is not floor */
249 cave[y][x].info &= ~(CAVE_MASK);
253 place_floor_bold(y, x);
259 * Make an empty square floor, for the middle of rooms
261 void place_floor(int x1, int x2, int y1, int y2, bool light)
265 /* Place a full floor under the room */
266 for (y = y1 - 1; y <= y2 + 1; y++)
268 for (x = x1 - 1; x <= x2 + 1; x++)
270 place_floor_bold(y, x);
271 add_cave_info(y, x, CAVE_ROOM);
272 if (light) add_cave_info(y, x, CAVE_GLOW);
279 * Make an empty square room, only floor and wall grids
281 void place_room(int x1, int x2, int y1, int y2, bool light)
285 place_floor(x1, x2, y1, y2, light);
287 /* Walls around the room */
288 for (y = y1 - 1; y <= y2 + 1; y++)
290 place_outer_bold(y, x1 - 1);
291 place_outer_bold(y, x2 + 1);
293 for (x = x1 - 1; x <= x2 + 1; x++)
295 place_outer_bold(y1 - 1, x);
296 place_outer_bold(y2 + 1, x);
302 * Create up to "num" objects near the given coordinates
303 * Only really called by some of the "vault" routines.
305 void vault_objects(int y, int x, int num)
308 int i = 0, j = y, k = x;
313 /* Attempt to place 'num' objects */
314 for (; num > 0; --num)
316 /* Try up to 11 spots looking for empty space */
317 for (i = 0; i < 11; ++i)
319 /* Pick a random location */
320 while (dummy < SAFE_MAX_ATTEMPTS)
322 j = rand_spread(y, 2);
323 k = rand_spread(x, 3);
325 if (!in_bounds(j, k)) continue;
330 if (dummy >= SAFE_MAX_ATTEMPTS)
335 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
337 msg_print("Warning! Could not place vault object!");
344 /* Require "clean" floor space */
346 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
349 if (randint0(100) < 75)
351 place_object(j, k, 0L);
360 /* Placement accomplished */
368 * Place a trap with a given displacement of point
370 void vault_trap_aux(int y, int x, int yd, int xd)
372 int count = 0, y1 = y, x1 = x;
378 for (count = 0; count <= 5; count++)
381 while (dummy < SAFE_MAX_ATTEMPTS)
383 y1 = rand_spread(y, yd);
384 x1 = rand_spread(x, xd);
386 if (!in_bounds(y1, x1)) continue;
390 if (dummy >= SAFE_MAX_ATTEMPTS)
395 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
397 msg_print("Warning! Could not place vault trap!");
403 /* Require "naked" floor grids */
404 c_ptr = &cave[y1][x1];
405 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
417 * Place some traps with a given displacement of given location
419 void vault_traps(int y, int x, int yd, int xd, int num)
423 for (i = 0; i < num; i++)
425 vault_trap_aux(y, x, yd, xd);
431 * Hack -- Place some sleeping monsters near the given location
433 void vault_monsters(int y1, int x1, int num)
438 /* Try to summon "num" monsters "near" the given location */
439 for (k = 0; k < num; k++)
441 /* Try nine locations */
442 for (i = 0; i < 9; i++)
446 /* Pick a nearby location */
447 scatter(&y, &x, y1, x1, d, 0);
449 /* Require "empty" floor grids */
451 if (!cave_empty_grid(c_ptr)) continue;
453 /* Place the monster (allow groups) */
454 monster_level = base_level + 2;
455 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
456 monster_level = base_level;
463 * Always picks a correct direction
465 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
467 /* Extract vertical and horizontal directions */
468 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
469 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
471 /* Never move diagonally */
474 if (randint0(100) < 50)
483 * Pick a random direction
485 void rand_dir(int *rdir, int *cdir)
487 /* Pick a random direction */
490 /* Extract the dy/dx components */
496 /* Function that sees if a square is a floor. (Includes range checking.) */
497 bool get_is_floor(int x, int y)
499 if (!in_bounds(y, x))
505 /* Do the real check */
506 if (is_floor_bold(y, x)) return (TRUE);
512 /* Set a square to be floor. (Includes range checking.) */
513 void set_floor(int x, int y)
515 if (!in_bounds(y, x))
521 if (cave[y][x].info & CAVE_ROOM)
523 /* A room border don't touch. */
527 /* Set to be floor if is a wall (don't touch lakes). */
528 if (is_extra_bold(y, x))
529 place_floor_bold(y, x);
535 * Constructs a tunnel between two points
537 * This function must be called BEFORE any streamers are created,
538 * since we use the special "granite wall" sub-types to keep track
539 * of legal places for corridors to pierce rooms.
541 * We use "door_flag" to prevent excessive construction of doors
542 * along overlapping corridors.
544 * We queue the tunnel grids to prevent door creation along a corridor
545 * which intersects itself.
547 * We queue the wall piercing grids to prevent a corridor from leaving
548 * a room and then coming back in through the same entrance.
550 * We "pierce" grids which are "outer" walls of rooms, and when we
551 * do so, we change all adjacent "outer" walls of rooms into "solid"
552 * walls so that no two corridors may use adjacent grids for exits.
554 * The "solid" wall check prevents corridors from "chopping" the
555 * corners of rooms off, as well as "silly" door placement, and
556 * "excessively wide" room entrances.
560 * inner -- inner room walls
561 * outer -- outer room walls
562 * solid -- solid room walls
564 void build_tunnel(int row1, int col1, int row2, int col2)
567 int tmp_row, tmp_col;
568 int row_dir, col_dir;
569 int start_row, start_col;
570 int main_loop_count = 0;
572 bool door_flag = FALSE;
576 /* Save the starting location */
580 /* Start out in the correct direction */
581 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
583 /* Keep going until done (or bored) */
584 while ((row1 != row2) || (col1 != col2))
586 /* Mega-Hack -- Paranoia -- prevent infinite loops */
587 if (main_loop_count++ > 2000) break;
589 /* Allow bends in the tunnel */
590 if (randint0(100) < dun_tun_chg)
592 /* Acquire the correct direction */
593 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
595 /* Random direction */
596 if (randint0(100) < dun_tun_rnd)
598 rand_dir(&row_dir, &col_dir);
602 /* Get the next location */
603 tmp_row = row1 + row_dir;
604 tmp_col = col1 + col_dir;
607 /* Extremely Important -- do not leave the dungeon */
608 while (!in_bounds(tmp_row, tmp_col))
610 /* Acquire the correct direction */
611 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
613 /* Random direction */
614 if (randint0(100) < dun_tun_rnd)
616 rand_dir(&row_dir, &col_dir);
619 /* Get the next location */
620 tmp_row = row1 + row_dir;
621 tmp_col = col1 + col_dir;
625 /* Access the location */
626 c_ptr = &cave[tmp_row][tmp_col];
628 if (permanent_wall(&f_info[c_ptr->feat]))
630 /* Avoid the edge of vaults */
631 if (is_inner_grid(c_ptr)) continue;
634 /* Avoid "solid" walls */
635 if (is_solid_grid(c_ptr)) continue;
637 /* Pierce "outer" walls of rooms */
638 if (is_outer_grid(c_ptr))
640 /* Acquire the "next" location */
641 y = tmp_row + row_dir;
642 x = tmp_col + col_dir;
644 /* Hack -- Avoid outer/solid walls */
645 if (is_outer_bold(y, x)) continue;
646 if (is_solid_bold(y, x)) continue;
648 /* Accept this location */
652 /* Save the wall location */
653 if (dun->wall_n < WALL_MAX)
655 dun->wall[dun->wall_n].y = row1;
656 dun->wall[dun->wall_n].x = col1;
660 /* Forbid re-entry near this piercing */
661 for (y = row1 - 1; y <= row1 + 1; y++)
663 for (x = col1 - 1; x <= col1 + 1; x++)
665 /* Convert adjacent "outer" walls as "solid" walls */
666 if (is_outer_bold(y, x))
668 /* Change the wall to a "solid" wall */
669 place_solid_noperm_bold(y, x);
675 /* Travel quickly through rooms */
676 else if (c_ptr->info & (CAVE_ROOM))
678 /* Accept the location */
683 /* Tunnel through all other walls */
684 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
686 /* Accept this location */
690 /* Save the tunnel location */
691 if (dun->tunn_n < TUNN_MAX)
693 dun->tunn[dun->tunn_n].y = row1;
694 dun->tunn[dun->tunn_n].x = col1;
698 /* Allow door in next grid */
702 /* Handle corridor intersections or overlaps */
705 /* Accept the location */
709 /* Collect legal door locations */
712 /* Save the door location */
713 if (dun->door_n < DOOR_MAX)
715 dun->door[dun->door_n].y = row1;
716 dun->door[dun->door_n].x = col1;
720 /* No door in next grid */
724 /* Hack -- allow pre-emptive tunnel termination */
725 if (randint0(100) >= dun_tun_con)
727 /* Distance between row1 and start_row */
728 tmp_row = row1 - start_row;
729 if (tmp_row < 0) tmp_row = (-tmp_row);
731 /* Distance between col1 and start_col */
732 tmp_col = col1 - start_col;
733 if (tmp_col < 0) tmp_col = (-tmp_col);
735 /* Terminate the tunnel */
736 if ((tmp_row > 10) || (tmp_col > 10)) break;
744 * This routine adds the square to the tunnel
745 * It also checks for SOLID walls - and returns a nearby
746 * non-SOLID square in (x,y) so that a simple avoiding
747 * routine can be used. The returned boolean value reflects
748 * whether or not this routine hit a SOLID wall.
750 * "affectwall" toggles whether or not this new square affects
751 * the boundaries of rooms. - This is used by the catacomb
754 static bool set_tunnel(int *x, int *y, bool affectwall)
758 cave_type *c_ptr = &cave[*y][*x];
760 if (!in_bounds(*y, *x)) return TRUE;
762 if (is_inner_grid(c_ptr))
767 if (is_extra_bold(*y,*x))
769 /* Save the tunnel location */
770 if (dun->tunn_n < TUNN_MAX)
772 dun->tunn[dun->tunn_n].y = *y;
773 dun->tunn[dun->tunn_n].x = *x;
780 if (is_floor_bold(*y, *x))
782 /* Don't do anything */
786 if (is_outer_grid(c_ptr) && affectwall)
788 /* Save the wall location */
789 if (dun->wall_n < WALL_MAX)
791 dun->wall[dun->wall_n].y = *y;
792 dun->wall[dun->wall_n].x = *x;
796 /* Forbid re-entry near this piercing */
797 for (j = *y - 1; j <= *y + 1; j++)
799 for (i = *x - 1; i <= *x + 1; i++)
801 /* Convert adjacent "outer" walls as "solid" walls */
802 if (is_outer_bold(j, i))
804 /* Change the wall to a "solid" wall */
805 place_solid_noperm_bold(j, i);
810 /* Clear mimic type */
811 cave[*y][*x].mimic = 0;
813 place_floor_bold(*y, *x);
818 if (is_solid_grid(c_ptr) && affectwall)
820 /* cannot place tunnel here - use a square to the side */
822 /* find usable square and return value in (x,y) */
828 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
830 dy = randint0(3) - 1;
831 dx = randint0(3) - 1;
833 if (!in_bounds(*y + dy, *x + dx))
844 /* Failed for some reason: hack - ignore the solidness */
845 place_outer_grid(c_ptr);
850 /* Give new, acceptable coordinate. */
862 * This routine creates the catacomb-like tunnels by removing extra rock.
863 * Note that this routine is only called on "even" squares - so it gives
864 * a natural checkerboard pattern.
866 static void create_cata_tunnel(int x, int y)
873 set_tunnel(&x1, &y1, FALSE);
877 set_tunnel(&x1, &y1, FALSE);
881 set_tunnel(&x1, &y1, FALSE);
885 set_tunnel(&x1, &y1, FALSE);
890 * This routine does the bulk of the work in creating the new types of tunnels.
891 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
892 * It doesn't need to add any complexity - straight lines are fine.
893 * The SOLID walls are avoided by a recursive algorithm which tries random ways
894 * around the obstical until it works. The number of itterations is counted, and it
895 * this gets too large the routine exits. This should stop any crashes - but may leave
896 * small gaps in the tunnel where there are too many SOLID walls.
898 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
899 * as a part of the dodge SOLID walls algorithm.
901 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
902 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
904 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
905 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
908 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
913 /* Check for early exit */
914 if (!(*fail)) return;
916 length = distance(x1, y1, x2, y2);
920 if ((type == 1) && (length != 0))
923 for (i = 0; i <= length; i++)
925 x = x1 + i * (x2 - x1) / length;
926 y = y1 + i * (y2 - y1) / length;
927 if (!set_tunnel(&x, &y, TRUE))
931 /* This isn't working - probably have an infinite loop */
936 /* solid wall - so try to go around */
937 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
938 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
942 else if ((type == 2) || (type == 3))
946 for (i = x1; i <= x2; i++)
950 if (!set_tunnel(&x, &y, TRUE))
952 /* solid wall - so try to go around */
953 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
954 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
956 if ((type == 3) && ((x + y) % 2))
958 create_cata_tunnel(i, y1);
964 for (i = x2; i <= x1; i++)
968 if (!set_tunnel(&x, &y, TRUE))
970 /* solid wall - so try to go around */
971 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
972 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
974 if ((type == 3) && ((x + y) % 2))
976 create_cata_tunnel(i, y1);
983 for (i = y1; i <= y2; i++)
987 if (!set_tunnel(&x, &y, TRUE))
989 /* solid wall - so try to go around */
990 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
991 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
993 if ((type == 3) && ((x + y) % 2))
995 create_cata_tunnel(x2, i);
1001 for (i = y2; i <= y1; i++)
1005 if (!set_tunnel(&x, &y, TRUE))
1007 /* solid wall - so try to go around */
1008 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1009 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1011 if ((type == 3) && ((x + y) % 2))
1013 create_cata_tunnel(x2, i);
1022 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1023 * Permanent rock is ignored in this path finding- sometimes there is no
1024 * path around anyway -so there will be a crash if we try to find one.
1025 * This routine is much like the river creation routine in Zangband.
1026 * It works by dividing a line segment into two. The segments are divided
1027 * until they are less than "cutoff" - when the corresponding routine from
1028 * "short_seg_hack" is called.
1029 * Note it is VERY important that the "stop if hit another passage" logic
1030 * stays as is. Without this the dungeon turns into Swiss Cheese...
1032 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1035 int changex, changey;
1038 bool retval, firstsuccede;
1041 length = distance(x1, y1, x2, y2);
1043 if (length > cutoff)
1046 * Divide path in half and call routine twice.
1051 /* perturbation perpendicular to path */
1052 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1054 /* perturbation perpendicular to path */
1055 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1057 /* Work out "mid" ponit */
1058 x3 = x1 + dx + changex;
1059 y3 = y1 + dy + changey;
1061 /* See if in bounds - if not - do not perturb point */
1062 if (!in_bounds(y3, x3))
1068 c_ptr = &cave[y3][x3];
1069 if (is_solid_grid(c_ptr))
1071 /* move midpoint a bit to avoid problem. */
1077 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1079 dy = randint0(3) - 1;
1080 dx = randint0(3) - 1;
1081 if (!in_bounds(y3 + dy, x3 + dx))
1091 /* Failed for some reason: hack - ignore the solidness */
1092 place_outer_bold(y3, x3);
1098 c_ptr = &cave[y3][x3];
1101 if (is_floor_grid(c_ptr))
1103 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1105 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1107 /* do second half only if works + if have hit a room */
1108 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1112 /* have hit another tunnel - make a set of doors here */
1115 /* Save the door location */
1116 if (dun->door_n < DOOR_MAX)
1118 dun->door[dun->door_n].y = y3;
1119 dun->door[dun->door_n].x = x3;
1123 firstsuccede = TRUE;
1127 /* false- didn't work all the way */
1129 firstsuccede = FALSE;
1134 /* tunnel through walls */
1135 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1137 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1138 firstsuccede = TRUE;
1142 /* false- didn't work all the way */
1144 firstsuccede = FALSE;
1149 /* only do this if the first half has worked */
1150 set_tunnel(&x3, &y3, TRUE);
1152 /* return value calculated above */
1157 /* Do a short segment */
1159 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1161 /* Hack - ignore return value so avoid infinite loops */