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 bool curtain = (d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
150 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256);
152 /* Create open door */
153 set_cave_feat(y, x, curtain ? feat_open_curtain : feat_open_door);
156 /* Broken doors (100/1000) */
159 /* Create broken door */
160 set_cave_feat(y, x, feat_broken_door);
163 /* Secret doors (200/1000) */
166 /* Create secret door */
167 place_closed_door(y, x);
169 if (c_ptr->feat != feat_closed_curtain)
171 /* Hide. If on the edge of room, use outer wall. */
172 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
174 /* Floor type terrain cannot hide a door */
175 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
177 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
179 c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
186 /* Closed, locked, or stuck doors (400/1000) */
187 else place_closed_door(y, x);
189 delete_monster(y, x);
194 * Place a random type of normal door at the given location.
196 void place_closed_door(int y, int x)
200 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
202 place_floor_bold(y, x);
206 /* Choose an object */
209 /* Closed doors (300/400) */
212 bool curtain = (d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
213 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 48 : 512);
215 /* Create closed door */
216 cave_set_feat(y, x, curtain ? feat_closed_curtain : feat_closed_door);
219 /* Locked doors (99/400) */
222 /* Create locked door */
223 cave_set_feat(y, x, feat_locked_door[randint0(num_locked_door)]);
226 /* Stuck doors (1/400) */
229 /* Create jammed door */
230 cave_set_feat(y, x, feat_jammed_door[randint0(num_jammed_door)]);
233 /* Now it is not floor */
234 cave[y][x].info &= ~(CAVE_MASK);
239 * Make an empty square floor, for the middle of rooms
241 void place_floor(int x1, int x2, int y1, int y2, bool light)
245 /* Place a full floor under the room */
246 for (y = y1 - 1; y <= y2 + 1; y++)
248 for (x = x1 - 1; x <= x2 + 1; x++)
250 place_floor_bold(y, x);
251 add_cave_info(y, x, CAVE_ROOM);
252 if (light) add_cave_info(y, x, CAVE_GLOW);
259 * Make an empty square room, only floor and wall grids
261 void place_room(int x1, int x2, int y1, int y2, bool light)
265 place_floor(x1, x2, y1, y2, light);
267 /* Walls around the room */
268 for (y = y1 - 1; y <= y2 + 1; y++)
270 place_outer_bold(y, x1 - 1);
271 place_outer_bold(y, x2 + 1);
273 for (x = x1 - 1; x <= x2 + 1; x++)
275 place_outer_bold(y1 - 1, x);
276 place_outer_bold(y2 + 1, x);
282 * Create up to "num" objects near the given coordinates
283 * Only really called by some of the "vault" routines.
285 void vault_objects(int y, int x, int num)
288 int i = 0, j = y, k = x;
293 /* Attempt to place 'num' objects */
294 for (; num > 0; --num)
296 /* Try up to 11 spots looking for empty space */
297 for (i = 0; i < 11; ++i)
299 /* Pick a random location */
300 while (dummy < SAFE_MAX_ATTEMPTS)
302 j = rand_spread(y, 2);
303 k = rand_spread(x, 3);
305 if (!in_bounds(j, k)) continue;
310 if (dummy >= SAFE_MAX_ATTEMPTS)
315 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
317 msg_print("Warning! Could not place vault object!");
324 /* Require "clean" floor space */
326 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
329 if (randint0(100) < 75)
331 place_object(j, k, 0L);
340 /* Placement accomplished */
348 * Place a trap with a given displacement of point
350 void vault_trap_aux(int y, int x, int yd, int xd)
352 int count = 0, y1 = y, x1 = x;
358 for (count = 0; count <= 5; count++)
361 while (dummy < SAFE_MAX_ATTEMPTS)
363 y1 = rand_spread(y, yd);
364 x1 = rand_spread(x, xd);
366 if (!in_bounds(y1, x1)) continue;
370 if (dummy >= SAFE_MAX_ATTEMPTS)
375 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
377 msg_print("Warning! Could not place vault trap!");
383 /* Require "naked" floor grids */
384 c_ptr = &cave[y1][x1];
385 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
397 * Place some traps with a given displacement of given location
399 void vault_traps(int y, int x, int yd, int xd, int num)
403 for (i = 0; i < num; i++)
405 vault_trap_aux(y, x, yd, xd);
411 * Hack -- Place some sleeping monsters near the given location
413 void vault_monsters(int y1, int x1, int num)
418 /* Try to summon "num" monsters "near" the given location */
419 for (k = 0; k < num; k++)
421 /* Try nine locations */
422 for (i = 0; i < 9; i++)
426 /* Pick a nearby location */
427 scatter(&y, &x, y1, x1, d, 0);
429 /* Require "empty" floor grids */
431 if (!cave_empty_grid(c_ptr)) continue;
433 /* Place the monster (allow groups) */
434 monster_level = base_level + 2;
435 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
436 monster_level = base_level;
443 * Always picks a correct direction
445 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
447 /* Extract vertical and horizontal directions */
448 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
449 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
451 /* Never move diagonally */
454 if (randint0(100) < 50)
463 * Pick a random direction
465 void rand_dir(int *rdir, int *cdir)
467 /* Pick a random direction */
470 /* Extract the dy/dx components */
476 /* Function that sees if a square is a floor. (Includes range checking.) */
477 bool get_is_floor(int x, int y)
479 if (!in_bounds(y, x))
485 /* Do the real check */
486 if (is_floor_bold(y, x)) return (TRUE);
492 /* Set a square to be floor. (Includes range checking.) */
493 void set_floor(int x, int y)
495 if (!in_bounds(y, x))
501 if (cave[y][x].info & CAVE_ROOM)
503 /* A room border don't touch. */
507 /* Set to be floor if is a wall (don't touch lakes). */
508 if (is_extra_bold(y, x))
509 place_floor_bold(y, x);
515 * Constructs a tunnel between two points
517 * This function must be called BEFORE any streamers are created,
518 * since we use the special "granite wall" sub-types to keep track
519 * of legal places for corridors to pierce rooms.
521 * We use "door_flag" to prevent excessive construction of doors
522 * along overlapping corridors.
524 * We queue the tunnel grids to prevent door creation along a corridor
525 * which intersects itself.
527 * We queue the wall piercing grids to prevent a corridor from leaving
528 * a room and then coming back in through the same entrance.
530 * We "pierce" grids which are "outer" walls of rooms, and when we
531 * do so, we change all adjacent "outer" walls of rooms into "solid"
532 * walls so that no two corridors may use adjacent grids for exits.
534 * The "solid" wall check prevents corridors from "chopping" the
535 * corners of rooms off, as well as "silly" door placement, and
536 * "excessively wide" room entrances.
540 * inner -- inner room walls
541 * outer -- outer room walls
542 * solid -- solid room walls
544 void build_tunnel(int row1, int col1, int row2, int col2)
547 int tmp_row, tmp_col;
548 int row_dir, col_dir;
549 int start_row, start_col;
550 int main_loop_count = 0;
552 bool door_flag = FALSE;
556 /* Save the starting location */
560 /* Start out in the correct direction */
561 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
563 /* Keep going until done (or bored) */
564 while ((row1 != row2) || (col1 != col2))
566 /* Mega-Hack -- Paranoia -- prevent infinite loops */
567 if (main_loop_count++ > 2000) break;
569 /* Allow bends in the tunnel */
570 if (randint0(100) < dun_tun_chg)
572 /* Acquire the correct direction */
573 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
575 /* Random direction */
576 if (randint0(100) < dun_tun_rnd)
578 rand_dir(&row_dir, &col_dir);
582 /* Get the next location */
583 tmp_row = row1 + row_dir;
584 tmp_col = col1 + col_dir;
587 /* Extremely Important -- do not leave the dungeon */
588 while (!in_bounds(tmp_row, tmp_col))
590 /* Acquire the correct direction */
591 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
593 /* Random direction */
594 if (randint0(100) < dun_tun_rnd)
596 rand_dir(&row_dir, &col_dir);
599 /* Get the next location */
600 tmp_row = row1 + row_dir;
601 tmp_col = col1 + col_dir;
605 /* Access the location */
606 c_ptr = &cave[tmp_row][tmp_col];
608 if (permanent_wall(&f_info[c_ptr->feat]))
610 /* Avoid the edge of vaults */
611 if (is_inner_grid(c_ptr)) continue;
614 /* Avoid "solid" walls */
615 if (is_solid_grid(c_ptr)) continue;
617 /* Pierce "outer" walls of rooms */
618 if (is_outer_grid(c_ptr))
620 /* Acquire the "next" location */
621 y = tmp_row + row_dir;
622 x = tmp_col + col_dir;
624 /* Hack -- Avoid outer/solid walls */
625 if (is_outer_bold(y, x)) continue;
626 if (is_solid_bold(y, x)) continue;
628 /* Accept this location */
632 /* Save the wall location */
633 if (dun->wall_n < WALL_MAX)
635 dun->wall[dun->wall_n].y = row1;
636 dun->wall[dun->wall_n].x = col1;
640 /* Forbid re-entry near this piercing */
641 for (y = row1 - 1; y <= row1 + 1; y++)
643 for (x = col1 - 1; x <= col1 + 1; x++)
645 /* Convert adjacent "outer" walls as "solid" walls */
646 if (is_outer_bold(y, x))
648 /* Change the wall to a "solid" wall */
649 place_solid_noperm_bold(y, x);
655 /* Travel quickly through rooms */
656 else if (c_ptr->info & (CAVE_ROOM))
658 /* Accept the location */
663 /* Tunnel through all other walls */
664 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
666 /* Accept this location */
670 /* Save the tunnel location */
671 if (dun->tunn_n < TUNN_MAX)
673 dun->tunn[dun->tunn_n].y = row1;
674 dun->tunn[dun->tunn_n].x = col1;
678 /* Allow door in next grid */
682 /* Handle corridor intersections or overlaps */
685 /* Accept the location */
689 /* Collect legal door locations */
692 /* Save the door location */
693 if (dun->door_n < DOOR_MAX)
695 dun->door[dun->door_n].y = row1;
696 dun->door[dun->door_n].x = col1;
700 /* No door in next grid */
704 /* Hack -- allow pre-emptive tunnel termination */
705 if (randint0(100) >= dun_tun_con)
707 /* Distance between row1 and start_row */
708 tmp_row = row1 - start_row;
709 if (tmp_row < 0) tmp_row = (-tmp_row);
711 /* Distance between col1 and start_col */
712 tmp_col = col1 - start_col;
713 if (tmp_col < 0) tmp_col = (-tmp_col);
715 /* Terminate the tunnel */
716 if ((tmp_row > 10) || (tmp_col > 10)) break;
724 * This routine adds the square to the tunnel
725 * It also checks for SOLID walls - and returns a nearby
726 * non-SOLID square in (x,y) so that a simple avoiding
727 * routine can be used. The returned boolean value reflects
728 * whether or not this routine hit a SOLID wall.
730 * "affectwall" toggles whether or not this new square affects
731 * the boundaries of rooms. - This is used by the catacomb
734 static bool set_tunnel(int *x, int *y, bool affectwall)
738 cave_type *c_ptr = &cave[*y][*x];
740 if (!in_bounds(*y, *x)) return TRUE;
742 if (is_inner_grid(c_ptr))
747 if (is_extra_bold(*y,*x))
749 /* Save the tunnel location */
750 if (dun->tunn_n < TUNN_MAX)
752 dun->tunn[dun->tunn_n].y = *y;
753 dun->tunn[dun->tunn_n].x = *x;
760 if (is_floor_bold(*y, *x))
762 /* Don't do anything */
766 if (is_outer_grid(c_ptr) && affectwall)
768 /* Save the wall location */
769 if (dun->wall_n < WALL_MAX)
771 dun->wall[dun->wall_n].y = *y;
772 dun->wall[dun->wall_n].x = *x;
776 /* Forbid re-entry near this piercing */
777 for (j = *y - 1; j <= *y + 1; j++)
779 for (i = *x - 1; i <= *x + 1; i++)
781 /* Convert adjacent "outer" walls as "solid" walls */
782 if (is_outer_bold(j, i))
784 /* Change the wall to a "solid" wall */
785 place_solid_noperm_bold(j, i);
790 /* Clear mimic type */
791 cave[*y][*x].mimic = 0;
793 place_floor_bold(*y, *x);
798 if (is_solid_grid(c_ptr) && affectwall)
800 /* cannot place tunnel here - use a square to the side */
802 /* find usable square and return value in (x,y) */
808 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
810 dy = randint0(3) - 1;
811 dx = randint0(3) - 1;
813 if (!in_bounds(*y + dy, *x + dx))
824 /* Failed for some reason: hack - ignore the solidness */
825 place_outer_grid(c_ptr);
830 /* Give new, acceptable coordinate. */
842 * This routine creates the catacomb-like tunnels by removing extra rock.
843 * Note that this routine is only called on "even" squares - so it gives
844 * a natural checkerboard pattern.
846 static void create_cata_tunnel(int x, int y)
853 set_tunnel(&x1, &y1, FALSE);
857 set_tunnel(&x1, &y1, FALSE);
861 set_tunnel(&x1, &y1, FALSE);
865 set_tunnel(&x1, &y1, FALSE);
870 * This routine does the bulk of the work in creating the new types of tunnels.
871 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
872 * It doesn't need to add any complexity - straight lines are fine.
873 * The SOLID walls are avoided by a recursive algorithm which tries random ways
874 * around the obstical until it works. The number of itterations is counted, and it
875 * this gets too large the routine exits. This should stop any crashes - but may leave
876 * small gaps in the tunnel where there are too many SOLID walls.
878 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
879 * as a part of the dodge SOLID walls algorithm.
881 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
882 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
884 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
885 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
888 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
893 /* Check for early exit */
894 if (!(*fail)) return;
896 length = distance(x1, y1, x2, y2);
900 if ((type == 1) && (length != 0))
903 for (i = 0; i <= length; i++)
905 x = x1 + i * (x2 - x1) / length;
906 y = y1 + i * (y2 - y1) / length;
907 if (!set_tunnel(&x, &y, TRUE))
911 /* This isn't working - probably have an infinite loop */
916 /* solid wall - so try to go around */
917 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
918 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
922 else if ((type == 2) || (type == 3))
926 for (i = x1; i <= x2; i++)
930 if (!set_tunnel(&x, &y, TRUE))
932 /* solid wall - so try to go around */
933 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
934 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
936 if ((type == 3) && ((x + y) % 2))
938 create_cata_tunnel(i, y1);
944 for (i = x2; i <= x1; i++)
948 if (!set_tunnel(&x, &y, TRUE))
950 /* solid wall - so try to go around */
951 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
952 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
954 if ((type == 3) && ((x + y) % 2))
956 create_cata_tunnel(i, y1);
963 for (i = y1; i <= y2; i++)
967 if (!set_tunnel(&x, &y, TRUE))
969 /* solid wall - so try to go around */
970 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
971 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
973 if ((type == 3) && ((x + y) % 2))
975 create_cata_tunnel(x2, i);
981 for (i = y2; i <= y1; i++)
985 if (!set_tunnel(&x, &y, TRUE))
987 /* solid wall - so try to go around */
988 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
989 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
991 if ((type == 3) && ((x + y) % 2))
993 create_cata_tunnel(x2, i);
1002 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1003 * Permanent rock is ignored in this path finding- sometimes there is no
1004 * path around anyway -so there will be a crash if we try to find one.
1005 * This routine is much like the river creation routine in Zangband.
1006 * It works by dividing a line segment into two. The segments are divided
1007 * until they are less than "cutoff" - when the corresponding routine from
1008 * "short_seg_hack" is called.
1009 * Note it is VERY important that the "stop if hit another passage" logic
1010 * stays as is. Without this the dungeon turns into Swiss Cheese...
1012 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1015 int changex, changey;
1018 bool retval, firstsuccede;
1021 length = distance(x1, y1, x2, y2);
1023 if (length > cutoff)
1026 * Divide path in half and call routine twice.
1031 /* perturbation perpendicular to path */
1032 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1034 /* perturbation perpendicular to path */
1035 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1037 /* Work out "mid" ponit */
1038 x3 = x1 + dx + changex;
1039 y3 = y1 + dy + changey;
1041 /* See if in bounds - if not - do not perturb point */
1042 if (!in_bounds(y3, x3))
1048 c_ptr = &cave[y3][x3];
1049 if (is_solid_grid(c_ptr))
1051 /* move midpoint a bit to avoid problem. */
1057 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1059 dy = randint0(3) - 1;
1060 dx = randint0(3) - 1;
1061 if (!in_bounds(y3 + dy, x3 + dx))
1071 /* Failed for some reason: hack - ignore the solidness */
1072 place_outer_bold(y3, x3);
1078 c_ptr = &cave[y3][x3];
1081 if (is_floor_grid(c_ptr))
1083 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1085 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1087 /* do second half only if works + if have hit a room */
1088 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1092 /* have hit another tunnel - make a set of doors here */
1095 /* Save the door location */
1096 if (dun->door_n < DOOR_MAX)
1098 dun->door[dun->door_n].y = y3;
1099 dun->door[dun->door_n].x = x3;
1103 firstsuccede = TRUE;
1107 /* false- didn't work all the way */
1109 firstsuccede = FALSE;
1114 /* tunnel through walls */
1115 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1117 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1118 firstsuccede = TRUE;
1122 /* false- didn't work all the way */
1124 firstsuccede = FALSE;
1129 /* only do this if the first half has worked */
1130 set_tunnel(&x3, &y3, TRUE);
1132 /* return value calculated above */
1137 /* Do a short segment */
1139 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1141 /* Hack - ignore return value so avoid infinite loops */