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 (!cave_clean_bold(y, x) || c_ptr->m_idx) continue;
40 if (!in_bounds(y,x)) continue;
42 /* Refuse to start on anti-teleport grids */
43 if (c_ptr->info & (CAVE_ICKY)) continue;
49 if (max_attempts < 1) /* Should be -1, actually if we failed... */
52 /* Save the new player grid */
61 * Place an up/down staircase at given location
63 void place_random_stairs(int y, int x)
65 bool up_stairs = TRUE;
66 bool down_stairs = TRUE;
71 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) return;
82 if (dun_level >= d_info[dungeon_type].maxdepth)
86 if (quest_number(dun_level) && (dun_level > 1))
89 /* We can't place both */
90 if (down_stairs && up_stairs)
92 /* Choose a staircase randomly */
93 if (randint0(100) < 50)
99 /* Place the stairs */
101 place_up_stairs(y, x);
102 else if (down_stairs)
103 place_down_stairs(y, x);
108 * Place a random type of door at the given location
110 void place_random_door(int y, int x, bool room)
113 cave_type *c_ptr = &cave[y][x];
115 /* Initialize mimic info */
118 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
120 place_floor_bold(y, x);
124 /* Choose an object */
125 tmp = randint0(1000);
127 /* Open doors (300/1000) */
130 /* Create open door */
131 set_cave_feat(y, x, FEAT_OPEN);
134 /* Broken doors (100/1000) */
137 /* Create broken door */
138 set_cave_feat(y, x, FEAT_BROKEN);
141 /* Secret doors (200/1000) */
144 /* Create secret door */
145 place_closed_door(y, x);
147 /* Hide. If on the edge of room, use outer wall. */
148 c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
150 /* Floor type terrain cannot hide a door */
151 if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
153 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE)) c_ptr->feat = c_ptr->mimic;
158 /* Closed, locked, or stuck doors (400/1000) */
159 else place_closed_door(y, x);
164 * Place a random type of normal door at the given location.
166 void place_closed_door(int y, int x)
170 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
172 place_floor_bold(y, x);
176 /* Choose an object */
179 /* Closed doors (300/400) */
182 /* Create closed door */
183 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
186 /* Locked doors (99/400) */
189 /* Create locked door */
190 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
193 /* Stuck doors (1/400) */
196 /* Create jammed door */
197 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
200 /* Now it is not floor */
201 cave[y][x].info &= ~(CAVE_MASK);
206 * Make an empty square floor, for the middle of rooms
208 void place_floor(int x1, int x2, int y1, int y2, bool light)
212 /* Place a full floor under the room */
213 for (y = y1 - 1; y <= y2 + 1; y++)
215 for (x = x1 - 1; x <= x2 + 1; x++)
217 place_floor_bold(y, x);
218 add_cave_info(y, x, CAVE_ROOM);
219 if (light) add_cave_info(y, x, CAVE_GLOW);
226 * Make an empty square room, only floor and wall grids
228 void place_room(int x1, int x2, int y1, int y2, bool light)
232 place_floor(x1, x2, y1, y2, light);
234 /* Walls around the room */
235 for (y = y1 - 1; y <= y2 + 1; y++)
237 place_outer_bold(y, x1 - 1);
238 place_outer_bold(y, x2 + 1);
240 for (x = x1 - 1; x <= x2 + 1; x++)
242 place_outer_bold(y1 - 1, x);
243 place_outer_bold(y2 + 1, x);
249 * Create up to "num" objects near the given coordinates
250 * Only really called by some of the "vault" routines.
252 void vault_objects(int y, int x, int num)
255 int i = 0, j = y, k = x;
260 /* Attempt to place 'num' objects */
261 for (; num > 0; --num)
263 /* Try up to 11 spots looking for empty space */
264 for (i = 0; i < 11; ++i)
266 /* Pick a random location */
267 while (dummy < SAFE_MAX_ATTEMPTS)
269 j = rand_spread(y, 2);
270 k = rand_spread(x, 3);
272 if (!in_bounds(j, k)) continue;
277 if (dummy >= SAFE_MAX_ATTEMPTS)
282 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
284 msg_print("Warning! Could not place vault object!");
291 /* Require "clean" floor space */
293 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
296 if (randint0(100) < 75)
298 place_object(j, k, 0L);
307 /* Placement accomplished */
315 * Place a trap with a given displacement of point
317 void vault_trap_aux(int y, int x, int yd, int xd)
319 int count = 0, y1 = y, x1 = x;
325 for (count = 0; count <= 5; count++)
328 while (dummy < SAFE_MAX_ATTEMPTS)
330 y1 = rand_spread(y, yd);
331 x1 = rand_spread(x, xd);
333 if (!in_bounds(y1, x1)) continue;
337 if (dummy >= SAFE_MAX_ATTEMPTS)
342 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
344 msg_print("Warning! Could not place vault trap!");
350 /* Require "naked" floor grids */
351 c_ptr = &cave[y1][x1];
352 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
364 * Place some traps with a given displacement of given location
366 void vault_traps(int y, int x, int yd, int xd, int num)
370 for (i = 0; i < num; i++)
372 vault_trap_aux(y, x, yd, xd);
378 * Hack -- Place some sleeping monsters near the given location
380 void vault_monsters(int y1, int x1, int num)
385 /* Try to summon "num" monsters "near" the given location */
386 for (k = 0; k < num; k++)
388 /* Try nine locations */
389 for (i = 0; i < 9; i++)
393 /* Pick a nearby location */
394 scatter(&y, &x, y1, x1, d, 0);
396 /* Require "empty" floor grids */
398 if (!cave_empty_grid(c_ptr)) continue;
400 /* Place the monster (allow groups) */
401 monster_level = base_level + 2;
402 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
403 monster_level = base_level;
410 * Count the number of walls adjacent to the given grid.
412 * Note -- Assumes "in_bounds(y, x)"
414 * We count only granite walls and permanent walls.
416 int next_to_walls(int y, int x)
420 if (cave_floor_grid(&cave[y + 1][x])) k++;
421 if (cave_floor_grid(&cave[y - 1][x])) k++;
422 if (cave_floor_grid(&cave[y][x + 1])) k++;
423 if (cave_floor_grid(&cave[y][x - 1])) k++;
430 * Always picks a correct direction
432 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
434 /* Extract vertical and horizontal directions */
435 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
436 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
438 /* Never move diagonally */
441 if (randint0(100) < 50)
450 * Pick a random direction
452 void rand_dir(int *rdir, int *cdir)
454 /* Pick a random direction */
457 /* Extract the dy/dx components */
463 /* Function that sees if a square is a floor. (Includes range checking.) */
464 bool get_is_floor(int x, int y)
466 if (!in_bounds(y, x))
472 /* Do the real check */
473 if (is_floor_bold(y, x)) return (TRUE);
479 /* Set a square to be floor. (Includes range checking.) */
480 void set_floor(int x, int y)
482 if (!in_bounds(y, x))
488 if (cave[y][x].info & CAVE_ROOM)
490 /* A room border don't touch. */
494 /* Set to be floor if is a wall (don't touch lakes). */
495 if (is_extra_bold(y, x))
496 place_floor_bold(y, x);
502 * Constructs a tunnel between two points
504 * This function must be called BEFORE any streamers are created,
505 * since we use the special "granite wall" sub-types to keep track
506 * of legal places for corridors to pierce rooms.
508 * We use "door_flag" to prevent excessive construction of doors
509 * along overlapping corridors.
511 * We queue the tunnel grids to prevent door creation along a corridor
512 * which intersects itself.
514 * We queue the wall piercing grids to prevent a corridor from leaving
515 * a room and then coming back in through the same entrance.
517 * We "pierce" grids which are "outer" walls of rooms, and when we
518 * do so, we change all adjacent "outer" walls of rooms into "solid"
519 * walls so that no two corridors may use adjacent grids for exits.
521 * The "solid" wall check prevents corridors from "chopping" the
522 * corners of rooms off, as well as "silly" door placement, and
523 * "excessively wide" room entrances.
527 * inner -- inner room walls
528 * outer -- outer room walls
529 * solid -- solid room walls
531 void build_tunnel(int row1, int col1, int row2, int col2)
534 int tmp_row, tmp_col;
535 int row_dir, col_dir;
536 int start_row, start_col;
537 int main_loop_count = 0;
539 bool door_flag = FALSE;
544 /* Save the starting location */
548 /* Start out in the correct direction */
549 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
551 /* Keep going until done (or bored) */
552 while ((row1 != row2) || (col1 != col2))
554 /* Mega-Hack -- Paranoia -- prevent infinite loops */
555 if (main_loop_count++ > 2000) break;
557 /* Allow bends in the tunnel */
558 if (randint0(100) < dun_tun_chg)
560 /* Acquire the correct direction */
561 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
563 /* Random direction */
564 if (randint0(100) < dun_tun_rnd)
566 rand_dir(&row_dir, &col_dir);
570 /* Get the next location */
571 tmp_row = row1 + row_dir;
572 tmp_col = col1 + col_dir;
575 /* Extremely Important -- do not leave the dungeon */
576 while (!in_bounds(tmp_row, tmp_col))
578 /* Acquire the correct direction */
579 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
581 /* Random direction */
582 if (randint0(100) < dun_tun_rnd)
584 rand_dir(&row_dir, &col_dir);
587 /* Get the next location */
588 tmp_row = row1 + row_dir;
589 tmp_col = col1 + col_dir;
593 /* Access the location */
594 c_ptr = &cave[tmp_row][tmp_col];
595 f_ptr = &f_info[c_ptr->feat];
597 if (have_flag(f_ptr->flags, FF_WALL) && have_flag(f_ptr->flags, FF_PERMANENT))
599 /* Avoid the edge of vaults */
600 if (is_inner_grid(c_ptr)) continue;
603 /* Avoid "solid" walls */
604 if (is_solid_grid(c_ptr)) continue;
606 /* Pierce "outer" walls of rooms */
607 if (is_outer_grid(c_ptr))
609 feature_type *ff_ptr;
611 /* Acquire the "next" location */
612 y = tmp_row + row_dir;
613 x = tmp_col + col_dir;
615 ff_ptr = &f_info[cave[y][x].feat];
617 /* Hack -- Avoid outer/solid walls */
618 if (is_outer_bold(y, x)) continue;
619 if (is_solid_bold(y, x)) continue;
621 /* Accept this location */
625 /* Save the wall location */
626 if (dun->wall_n < WALL_MAX)
628 dun->wall[dun->wall_n].y = row1;
629 dun->wall[dun->wall_n].x = col1;
633 /* Forbid re-entry near this piercing */
634 for (y = row1 - 1; y <= row1 + 1; y++)
636 for (x = col1 - 1; x <= col1 + 1; x++)
638 /* Convert adjacent "outer" walls as "solid" walls */
639 if (is_outer_bold(y, x))
641 /* Change the wall to a "solid" wall */
642 place_solid_noperm_bold(y, x);
648 /* Travel quickly through rooms */
649 else if (c_ptr->info & (CAVE_ROOM))
651 /* Accept the location */
656 /* Tunnel through all other walls */
657 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
659 /* Accept this location */
663 /* Save the tunnel location */
664 if (dun->tunn_n < TUNN_MAX)
666 dun->tunn[dun->tunn_n].y = row1;
667 dun->tunn[dun->tunn_n].x = col1;
671 /* Allow door in next grid */
675 /* Handle corridor intersections or overlaps */
678 /* Accept the location */
682 /* Collect legal door locations */
685 /* Save the door location */
686 if (dun->door_n < DOOR_MAX)
688 dun->door[dun->door_n].y = row1;
689 dun->door[dun->door_n].x = col1;
693 /* No door in next grid */
697 /* Hack -- allow pre-emptive tunnel termination */
698 if (randint0(100) >= dun_tun_con)
700 /* Distance between row1 and start_row */
701 tmp_row = row1 - start_row;
702 if (tmp_row < 0) tmp_row = (-tmp_row);
704 /* Distance between col1 and start_col */
705 tmp_col = col1 - start_col;
706 if (tmp_col < 0) tmp_col = (-tmp_col);
708 /* Terminate the tunnel */
709 if ((tmp_row > 10) || (tmp_col > 10)) break;
717 * This routine adds the square to the tunnel
718 * It also checks for SOLID walls - and returns a nearby
719 * non-SOLID square in (x,y) so that a simple avoiding
720 * routine can be used. The returned boolean value reflects
721 * whether or not this routine hit a SOLID wall.
723 * "affectwall" toggles whether or not this new square affects
724 * the boundaries of rooms. - This is used by the catacomb
727 static bool set_tunnel(int *x, int *y, bool affectwall)
729 int feat, i, j, dx, dy;
731 cave_type *c_ptr = &cave[*y][*x];
734 if (!in_bounds(*y, *x)) return TRUE;
737 f_ptr = &f_info[feat];
739 if (have_flag(f_ptr->flags, FF_WALL) && have_flag(f_ptr->flags, FF_PERMANENT))
742 * Ignore permanent walls - sometimes cannot tunnel around them anyway
743 * so don't try - it just complicates things unnecessarily.
748 if (is_inner_grid(c_ptr))
753 if (is_extra_bold(*y,*x))
755 /* Save the tunnel location */
756 if (dun->tunn_n < TUNN_MAX)
758 dun->tunn[dun->tunn_n].y = *y;
759 dun->tunn[dun->tunn_n].x = *x;
766 if (is_floor_bold(*y, *x))
768 /* Don't do anything */
772 if (is_outer_grid(c_ptr) && affectwall)
774 /* Save the wall location */
775 if (dun->wall_n < WALL_MAX)
777 dun->wall[dun->wall_n].y = *y;
778 dun->wall[dun->wall_n].x = *x;
782 /* Forbid re-entry near this piercing */
783 for (j = *y - 1; j <= *y + 1; j++)
785 for (i = *x - 1; i <= *x + 1; i++)
787 /* Convert adjacent "outer" walls as "solid" walls */
788 if (is_outer_bold(j, i))
790 /* Change the wall to a "solid" wall */
791 place_solid_noperm_bold(j, i);
796 /* Clear mimic type */
797 cave[*y][*x].mimic = 0;
799 place_floor_bold(*y, *x);
804 if (is_solid_grid(c_ptr) && affectwall)
806 /* cannot place tunnel here - use a square to the side */
808 /* find usable square and return value in (x,y) */
814 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
816 dy = randint0(3) - 1;
817 dx = randint0(3) - 1;
819 if (!in_bounds(*y + dy, *x + dx))
830 /* Failed for some reason: hack - ignore the solidness */
831 place_outer_grid(c_ptr);
836 /* Give new, acceptable coordinate. */
848 * This routine creates the catacomb-like tunnels by removing extra rock.
849 * Note that this routine is only called on "even" squares - so it gives
850 * a natural checkerboard pattern.
852 static void create_cata_tunnel(int x, int y)
859 set_tunnel(&x1, &y1, FALSE);
863 set_tunnel(&x1, &y1, FALSE);
867 set_tunnel(&x1, &y1, FALSE);
871 set_tunnel(&x1, &y1, FALSE);
876 * This routine does the bulk of the work in creating the new types of tunnels.
877 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
878 * It doesn't need to add any complexity - straight lines are fine.
879 * The SOLID walls are avoided by a recursive algorithm which tries random ways
880 * around the obstical until it works. The number of itterations is counted, and it
881 * this gets too large the routine exits. This should stop any crashes - but may leave
882 * small gaps in the tunnel where there are too many SOLID walls.
884 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
885 * as a part of the dodge SOLID walls algorithm.
887 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
888 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
890 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
891 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
894 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
899 /* Check for early exit */
900 if (!(*fail)) return;
902 length = distance(x1, y1, x2, y2);
906 if ((type == 1) && (length != 0))
909 for (i = 0; i <= length; i++)
911 x = x1 + i * (x2 - x1) / length;
912 y = y1 + i * (y2 - y1) / length;
913 if (!set_tunnel(&x, &y, TRUE))
917 /* This isn't working - probably have an infinite loop */
922 /* solid wall - so try to go around */
923 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
924 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
928 else if ((type == 2) || (type == 3))
932 for (i = x1; i <= x2; i++)
936 if (!set_tunnel(&x, &y, TRUE))
938 /* solid wall - so try to go around */
939 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
940 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
942 if ((type == 3) && ((x + y) % 2))
944 create_cata_tunnel(i, y1);
950 for (i = x2; i <= x1; i++)
954 if (!set_tunnel(&x, &y, TRUE))
956 /* solid wall - so try to go around */
957 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
958 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
960 if ((type == 3) && ((x + y) % 2))
962 create_cata_tunnel(i, y1);
969 for (i = y1; i <= y2; i++)
973 if (!set_tunnel(&x, &y, TRUE))
975 /* solid wall - so try to go around */
976 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
977 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
979 if ((type == 3) && ((x + y) % 2))
981 create_cata_tunnel(x2, i);
987 for (i = y2; i <= y1; i++)
991 if (!set_tunnel(&x, &y, TRUE))
993 /* solid wall - so try to go around */
994 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
995 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
997 if ((type == 3) && ((x + y) % 2))
999 create_cata_tunnel(x2, i);
1008 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1009 * Permanent rock is ignored in this path finding- sometimes there is no
1010 * path around anyway -so there will be a crash if we try to find one.
1011 * This routine is much like the river creation routine in Zangband.
1012 * It works by dividing a line segment into two. The segments are divided
1013 * until they are less than "cutoff" - when the corresponding routine from
1014 * "short_seg_hack" is called.
1015 * Note it is VERY important that the "stop if hit another passage" logic
1016 * stays as is. Without this the dungeon turns into Swiss Cheese...
1018 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1021 int changex, changey;
1024 bool retval, firstsuccede;
1027 length = distance(x1, y1, x2, y2);
1029 if (length > cutoff)
1032 * Divide path in half and call routine twice.
1037 /* perturbation perpendicular to path */
1038 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1040 /* perturbation perpendicular to path */
1041 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1043 /* Work out "mid" ponit */
1044 x3 = x1 + dx + changex;
1045 y3 = y1 + dy + changey;
1047 /* See if in bounds - if not - do not perturb point */
1048 if (!in_bounds(y3, x3))
1054 c_ptr = &cave[y3][x3];
1055 if (is_solid_grid(c_ptr))
1057 /* move midpoint a bit to avoid problem. */
1063 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1065 dy = randint0(3) - 1;
1066 dx = randint0(3) - 1;
1067 if (!in_bounds(y3 + dy, x3 + dx))
1077 /* Failed for some reason: hack - ignore the solidness */
1078 place_outer_bold(y3, x3);
1084 c_ptr = &cave[y3][x3];
1087 if (is_floor_grid(c_ptr))
1089 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1091 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1093 /* do second half only if works + if have hit a room */
1094 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1098 /* have hit another tunnel - make a set of doors here */
1101 /* Save the door location */
1102 if (dun->door_n < DOOR_MAX)
1104 dun->door[dun->door_n].y = y3;
1105 dun->door[dun->door_n].x = x3;
1109 firstsuccede = TRUE;
1113 /* false- didn't work all the way */
1115 firstsuccede = FALSE;
1120 /* tunnel through walls */
1121 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1123 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1124 firstsuccede = TRUE;
1128 /* false- didn't work all the way */
1130 firstsuccede = FALSE;
1135 /* only do this if the first half has worked */
1136 set_tunnel(&x3, &y3, TRUE);
1138 /* return value calculated above */
1143 /* Do a short segment */
1145 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1147 /* Hack - ignore return value so avoid infinite loops */