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 = 5000;
29 /* Place the player */
30 while (max_attempts--)
32 /* Pick a legal spot */
33 y = rand_range(1, cur_hgt - 2);
34 x = rand_range(1, cur_wid - 2);
38 /* Must be a "naked" floor grid */
39 if (c_ptr->m_idx) continue;
40 if (!have_flag(f_flags_grid(c_ptr), FF_MOVE)) continue;
41 if (!player_can_enter(c_ptr->feat, 0)) continue;
42 if (!in_bounds(y, x)) continue;
44 /* Refuse to start on anti-teleport grids */
45 if (c_ptr->info & (CAVE_ICKY)) continue;
51 if (max_attempts < 1) /* Should be -1, actually if we failed... */
54 /* Save the new player grid */
63 * Place an up/down staircase at given location
65 void place_random_stairs(int y, int x)
67 bool up_stairs = TRUE;
68 bool down_stairs = TRUE;
73 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) return;
84 if (dun_level >= d_info[dungeon_type].maxdepth)
88 if (quest_number(dun_level) && (dun_level > 1))
91 /* We can't place both */
92 if (down_stairs && up_stairs)
94 /* Choose a staircase randomly */
95 if (randint0(100) < 50)
101 /* Place the stairs */
103 place_up_stairs(y, x);
104 else if (down_stairs)
105 place_down_stairs(y, x);
110 * Place a random type of door at the given location
112 void place_random_door(int y, int x, bool room)
115 cave_type *c_ptr = &cave[y][x];
117 /* Initialize mimic info */
120 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
122 place_floor_bold(y, x);
126 /* Choose an object */
127 tmp = randint0(1000);
129 /* Open doors (300/1000) */
132 /* Create open door */
133 set_cave_feat(y, x, FEAT_OPEN);
136 /* Broken doors (100/1000) */
139 /* Create broken door */
140 set_cave_feat(y, x, FEAT_BROKEN);
143 /* Secret doors (200/1000) */
146 /* Create secret door */
147 place_closed_door(y, x);
149 /* Hide. If on the edge of room, use outer wall. */
150 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
152 /* Floor type terrain cannot hide a door */
153 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
155 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
157 c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
163 /* Closed, locked, or stuck doors (400/1000) */
164 else place_closed_door(y, x);
169 * Place a random type of normal door at the given location.
171 void place_closed_door(int y, int x)
175 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
177 place_floor_bold(y, x);
181 /* Choose an object */
184 /* Closed doors (300/400) */
187 /* Create closed door */
188 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
191 /* Locked doors (99/400) */
194 /* Create locked door */
195 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
198 /* Stuck doors (1/400) */
201 /* Create jammed door */
202 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
205 /* Now it is not floor */
206 cave[y][x].info &= ~(CAVE_MASK);
211 * Make an empty square floor, for the middle of rooms
213 void place_floor(int x1, int x2, int y1, int y2, bool light)
217 /* Place a full floor under the room */
218 for (y = y1 - 1; y <= y2 + 1; y++)
220 for (x = x1 - 1; x <= x2 + 1; x++)
222 place_floor_bold(y, x);
223 add_cave_info(y, x, CAVE_ROOM);
224 if (light) add_cave_info(y, x, CAVE_GLOW);
231 * Make an empty square room, only floor and wall grids
233 void place_room(int x1, int x2, int y1, int y2, bool light)
237 place_floor(x1, x2, y1, y2, light);
239 /* Walls around the room */
240 for (y = y1 - 1; y <= y2 + 1; y++)
242 place_outer_bold(y, x1 - 1);
243 place_outer_bold(y, x2 + 1);
245 for (x = x1 - 1; x <= x2 + 1; x++)
247 place_outer_bold(y1 - 1, x);
248 place_outer_bold(y2 + 1, x);
254 * Create up to "num" objects near the given coordinates
255 * Only really called by some of the "vault" routines.
257 void vault_objects(int y, int x, int num)
260 int i = 0, j = y, k = x;
265 /* Attempt to place 'num' objects */
266 for (; num > 0; --num)
268 /* Try up to 11 spots looking for empty space */
269 for (i = 0; i < 11; ++i)
271 /* Pick a random location */
272 while (dummy < SAFE_MAX_ATTEMPTS)
274 j = rand_spread(y, 2);
275 k = rand_spread(x, 3);
277 if (!in_bounds(j, k)) continue;
282 if (dummy >= SAFE_MAX_ATTEMPTS)
287 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
289 msg_print("Warning! Could not place vault object!");
296 /* Require "clean" floor space */
298 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
301 if (randint0(100) < 75)
303 place_object(j, k, 0L);
312 /* Placement accomplished */
320 * Place a trap with a given displacement of point
322 void vault_trap_aux(int y, int x, int yd, int xd)
324 int count = 0, y1 = y, x1 = x;
330 for (count = 0; count <= 5; count++)
333 while (dummy < SAFE_MAX_ATTEMPTS)
335 y1 = rand_spread(y, yd);
336 x1 = rand_spread(x, xd);
338 if (!in_bounds(y1, x1)) continue;
342 if (dummy >= SAFE_MAX_ATTEMPTS)
347 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
349 msg_print("Warning! Could not place vault trap!");
355 /* Require "naked" floor grids */
356 c_ptr = &cave[y1][x1];
357 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
369 * Place some traps with a given displacement of given location
371 void vault_traps(int y, int x, int yd, int xd, int num)
375 for (i = 0; i < num; i++)
377 vault_trap_aux(y, x, yd, xd);
383 * Hack -- Place some sleeping monsters near the given location
385 void vault_monsters(int y1, int x1, int num)
390 /* Try to summon "num" monsters "near" the given location */
391 for (k = 0; k < num; k++)
393 /* Try nine locations */
394 for (i = 0; i < 9; i++)
398 /* Pick a nearby location */
399 scatter(&y, &x, y1, x1, d, 0);
401 /* Require "empty" floor grids */
403 if (!cave_empty_grid(c_ptr)) continue;
405 /* Place the monster (allow groups) */
406 monster_level = base_level + 2;
407 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
408 monster_level = base_level;
415 * Count the number of walls adjacent to the given grid.
417 * Note -- Assumes "in_bounds(y, x)"
419 * We count only granite walls and permanent walls.
421 int next_to_walls(int y, int x)
425 if (cave_floor_grid(&cave[y + 1][x])) k++;
426 if (cave_floor_grid(&cave[y - 1][x])) k++;
427 if (cave_floor_grid(&cave[y][x + 1])) k++;
428 if (cave_floor_grid(&cave[y][x - 1])) k++;
435 * Always picks a correct direction
437 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
439 /* Extract vertical and horizontal directions */
440 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
441 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
443 /* Never move diagonally */
446 if (randint0(100) < 50)
455 * Pick a random direction
457 void rand_dir(int *rdir, int *cdir)
459 /* Pick a random direction */
462 /* Extract the dy/dx components */
468 /* Function that sees if a square is a floor. (Includes range checking.) */
469 bool get_is_floor(int x, int y)
471 if (!in_bounds(y, x))
477 /* Do the real check */
478 if (is_floor_bold(y, x)) return (TRUE);
484 /* Set a square to be floor. (Includes range checking.) */
485 void set_floor(int x, int y)
487 if (!in_bounds(y, x))
493 if (cave[y][x].info & CAVE_ROOM)
495 /* A room border don't touch. */
499 /* Set to be floor if is a wall (don't touch lakes). */
500 if (is_extra_bold(y, x))
501 place_floor_bold(y, x);
507 * Constructs a tunnel between two points
509 * This function must be called BEFORE any streamers are created,
510 * since we use the special "granite wall" sub-types to keep track
511 * of legal places for corridors to pierce rooms.
513 * We use "door_flag" to prevent excessive construction of doors
514 * along overlapping corridors.
516 * We queue the tunnel grids to prevent door creation along a corridor
517 * which intersects itself.
519 * We queue the wall piercing grids to prevent a corridor from leaving
520 * a room and then coming back in through the same entrance.
522 * We "pierce" grids which are "outer" walls of rooms, and when we
523 * do so, we change all adjacent "outer" walls of rooms into "solid"
524 * walls so that no two corridors may use adjacent grids for exits.
526 * The "solid" wall check prevents corridors from "chopping" the
527 * corners of rooms off, as well as "silly" door placement, and
528 * "excessively wide" room entrances.
532 * inner -- inner room walls
533 * outer -- outer room walls
534 * solid -- solid room walls
536 void build_tunnel(int row1, int col1, int row2, int col2)
539 int tmp_row, tmp_col;
540 int row_dir, col_dir;
541 int start_row, start_col;
542 int main_loop_count = 0;
544 bool door_flag = FALSE;
549 /* Save the starting location */
553 /* Start out in the correct direction */
554 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
556 /* Keep going until done (or bored) */
557 while ((row1 != row2) || (col1 != col2))
559 /* Mega-Hack -- Paranoia -- prevent infinite loops */
560 if (main_loop_count++ > 2000) break;
562 /* Allow bends in the tunnel */
563 if (randint0(100) < dun_tun_chg)
565 /* Acquire the correct direction */
566 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
568 /* Random direction */
569 if (randint0(100) < dun_tun_rnd)
571 rand_dir(&row_dir, &col_dir);
575 /* Get the next location */
576 tmp_row = row1 + row_dir;
577 tmp_col = col1 + col_dir;
580 /* Extremely Important -- do not leave the dungeon */
581 while (!in_bounds(tmp_row, tmp_col))
583 /* Acquire the correct direction */
584 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
586 /* Random direction */
587 if (randint0(100) < dun_tun_rnd)
589 rand_dir(&row_dir, &col_dir);
592 /* Get the next location */
593 tmp_row = row1 + row_dir;
594 tmp_col = col1 + col_dir;
598 /* Access the location */
599 c_ptr = &cave[tmp_row][tmp_col];
600 f_ptr = &f_info[c_ptr->feat];
602 if (permanent_wall(f_ptr))
604 /* Avoid the edge of vaults */
605 if (is_inner_grid(c_ptr)) continue;
608 /* Avoid "solid" walls */
609 if (is_solid_grid(c_ptr)) continue;
611 /* Pierce "outer" walls of rooms */
612 if (is_outer_grid(c_ptr))
614 feature_type *ff_ptr;
616 /* Acquire the "next" location */
617 y = tmp_row + row_dir;
618 x = tmp_col + col_dir;
620 ff_ptr = &f_info[cave[y][x].feat];
622 /* Hack -- Avoid outer/solid walls */
623 if (is_outer_bold(y, x)) continue;
624 if (is_solid_bold(y, x)) continue;
626 /* Accept this location */
630 /* Save the wall location */
631 if (dun->wall_n < WALL_MAX)
633 dun->wall[dun->wall_n].y = row1;
634 dun->wall[dun->wall_n].x = col1;
638 /* Forbid re-entry near this piercing */
639 for (y = row1 - 1; y <= row1 + 1; y++)
641 for (x = col1 - 1; x <= col1 + 1; x++)
643 /* Convert adjacent "outer" walls as "solid" walls */
644 if (is_outer_bold(y, x))
646 /* Change the wall to a "solid" wall */
647 place_solid_noperm_bold(y, x);
653 /* Travel quickly through rooms */
654 else if (c_ptr->info & (CAVE_ROOM))
656 /* Accept the location */
661 /* Tunnel through all other walls */
662 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
664 /* Accept this location */
668 /* Save the tunnel location */
669 if (dun->tunn_n < TUNN_MAX)
671 dun->tunn[dun->tunn_n].y = row1;
672 dun->tunn[dun->tunn_n].x = col1;
676 /* Allow door in next grid */
680 /* Handle corridor intersections or overlaps */
683 /* Accept the location */
687 /* Collect legal door locations */
690 /* Save the door location */
691 if (dun->door_n < DOOR_MAX)
693 dun->door[dun->door_n].y = row1;
694 dun->door[dun->door_n].x = col1;
698 /* No door in next grid */
702 /* Hack -- allow pre-emptive tunnel termination */
703 if (randint0(100) >= dun_tun_con)
705 /* Distance between row1 and start_row */
706 tmp_row = row1 - start_row;
707 if (tmp_row < 0) tmp_row = (-tmp_row);
709 /* Distance between col1 and start_col */
710 tmp_col = col1 - start_col;
711 if (tmp_col < 0) tmp_col = (-tmp_col);
713 /* Terminate the tunnel */
714 if ((tmp_row > 10) || (tmp_col > 10)) break;
722 * This routine adds the square to the tunnel
723 * It also checks for SOLID walls - and returns a nearby
724 * non-SOLID square in (x,y) so that a simple avoiding
725 * routine can be used. The returned boolean value reflects
726 * whether or not this routine hit a SOLID wall.
728 * "affectwall" toggles whether or not this new square affects
729 * the boundaries of rooms. - This is used by the catacomb
732 static bool set_tunnel(int *x, int *y, bool affectwall)
734 int feat, i, j, dx, dy;
736 cave_type *c_ptr = &cave[*y][*x];
739 if (!in_bounds(*y, *x)) return TRUE;
742 f_ptr = &f_info[feat];
744 if (is_inner_grid(c_ptr))
749 if (is_extra_bold(*y,*x))
751 /* Save the tunnel location */
752 if (dun->tunn_n < TUNN_MAX)
754 dun->tunn[dun->tunn_n].y = *y;
755 dun->tunn[dun->tunn_n].x = *x;
762 if (is_floor_bold(*y, *x))
764 /* Don't do anything */
768 if (is_outer_grid(c_ptr) && affectwall)
770 /* Save the wall location */
771 if (dun->wall_n < WALL_MAX)
773 dun->wall[dun->wall_n].y = *y;
774 dun->wall[dun->wall_n].x = *x;
778 /* Forbid re-entry near this piercing */
779 for (j = *y - 1; j <= *y + 1; j++)
781 for (i = *x - 1; i <= *x + 1; i++)
783 /* Convert adjacent "outer" walls as "solid" walls */
784 if (is_outer_bold(j, i))
786 /* Change the wall to a "solid" wall */
787 place_solid_noperm_bold(j, i);
792 /* Clear mimic type */
793 cave[*y][*x].mimic = 0;
795 place_floor_bold(*y, *x);
800 if (is_solid_grid(c_ptr) && affectwall)
802 /* cannot place tunnel here - use a square to the side */
804 /* find usable square and return value in (x,y) */
810 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
812 dy = randint0(3) - 1;
813 dx = randint0(3) - 1;
815 if (!in_bounds(*y + dy, *x + dx))
826 /* Failed for some reason: hack - ignore the solidness */
827 place_outer_grid(c_ptr);
832 /* Give new, acceptable coordinate. */
844 * This routine creates the catacomb-like tunnels by removing extra rock.
845 * Note that this routine is only called on "even" squares - so it gives
846 * a natural checkerboard pattern.
848 static void create_cata_tunnel(int x, int y)
855 set_tunnel(&x1, &y1, FALSE);
859 set_tunnel(&x1, &y1, FALSE);
863 set_tunnel(&x1, &y1, FALSE);
867 set_tunnel(&x1, &y1, FALSE);
872 * This routine does the bulk of the work in creating the new types of tunnels.
873 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
874 * It doesn't need to add any complexity - straight lines are fine.
875 * The SOLID walls are avoided by a recursive algorithm which tries random ways
876 * around the obstical until it works. The number of itterations is counted, and it
877 * this gets too large the routine exits. This should stop any crashes - but may leave
878 * small gaps in the tunnel where there are too many SOLID walls.
880 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
881 * as a part of the dodge SOLID walls algorithm.
883 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
884 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
886 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
887 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
890 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
895 /* Check for early exit */
896 if (!(*fail)) return;
898 length = distance(x1, y1, x2, y2);
902 if ((type == 1) && (length != 0))
905 for (i = 0; i <= length; i++)
907 x = x1 + i * (x2 - x1) / length;
908 y = y1 + i * (y2 - y1) / length;
909 if (!set_tunnel(&x, &y, TRUE))
913 /* This isn't working - probably have an infinite loop */
918 /* solid wall - so try to go around */
919 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
920 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
924 else if ((type == 2) || (type == 3))
928 for (i = x1; i <= x2; i++)
932 if (!set_tunnel(&x, &y, TRUE))
934 /* solid wall - so try to go around */
935 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
936 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
938 if ((type == 3) && ((x + y) % 2))
940 create_cata_tunnel(i, y1);
946 for (i = x2; i <= x1; 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);
965 for (i = y1; i <= y2; i++)
969 if (!set_tunnel(&x, &y, TRUE))
971 /* solid wall - so try to go around */
972 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
973 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
975 if ((type == 3) && ((x + y) % 2))
977 create_cata_tunnel(x2, i);
983 for (i = y2; i <= y1; 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);
1004 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1005 * Permanent rock is ignored in this path finding- sometimes there is no
1006 * path around anyway -so there will be a crash if we try to find one.
1007 * This routine is much like the river creation routine in Zangband.
1008 * It works by dividing a line segment into two. The segments are divided
1009 * until they are less than "cutoff" - when the corresponding routine from
1010 * "short_seg_hack" is called.
1011 * Note it is VERY important that the "stop if hit another passage" logic
1012 * stays as is. Without this the dungeon turns into Swiss Cheese...
1014 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1017 int changex, changey;
1020 bool retval, firstsuccede;
1023 length = distance(x1, y1, x2, y2);
1025 if (length > cutoff)
1028 * Divide path in half and call routine twice.
1033 /* perturbation perpendicular to path */
1034 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1036 /* perturbation perpendicular to path */
1037 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1039 /* Work out "mid" ponit */
1040 x3 = x1 + dx + changex;
1041 y3 = y1 + dy + changey;
1043 /* See if in bounds - if not - do not perturb point */
1044 if (!in_bounds(y3, x3))
1050 c_ptr = &cave[y3][x3];
1051 if (is_solid_grid(c_ptr))
1053 /* move midpoint a bit to avoid problem. */
1059 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1061 dy = randint0(3) - 1;
1062 dx = randint0(3) - 1;
1063 if (!in_bounds(y3 + dy, x3 + dx))
1073 /* Failed for some reason: hack - ignore the solidness */
1074 place_outer_bold(y3, x3);
1080 c_ptr = &cave[y3][x3];
1083 if (is_floor_grid(c_ptr))
1085 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1087 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1089 /* do second half only if works + if have hit a room */
1090 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1094 /* have hit another tunnel - make a set of doors here */
1097 /* Save the door location */
1098 if (dun->door_n < DOOR_MAX)
1100 dun->door[dun->door_n].y = y3;
1101 dun->door[dun->door_n].x = x3;
1105 firstsuccede = TRUE;
1109 /* false- didn't work all the way */
1111 firstsuccede = FALSE;
1116 /* tunnel through walls */
1117 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1119 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1120 firstsuccede = TRUE;
1124 /* false- didn't work all the way */
1126 firstsuccede = FALSE;
1131 /* only do this if the first half has worked */
1132 set_tunnel(&x3, &y3, TRUE);
1134 /* return value calculated above */
1139 /* Do a short segment */
1141 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1143 /* Hack - ignore return value so avoid infinite loops */