3 * Purpose: low-level dungeon creation primitives
7 * Copyright (c) 1989 James E. Wilson, Robert A. Koeneke
9 * This software may be copied and distributed for educational, research, and
10 * not for profit purposes provided that this copyright and statement are
11 * included in all such copies.
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)
114 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
116 place_floor_bold(y, x);
120 /* Choose an object */
121 tmp = randint0(1000);
123 /* Open doors (300/1000) */
126 /* Create open door */
127 cave_set_feat(y, x, FEAT_OPEN);
130 /* Broken doors (100/1000) */
133 /* Create broken door */
134 cave_set_feat(y, x, FEAT_BROKEN);
137 /* Secret doors (200/1000) */
140 /* Create secret door */
141 cave_set_feat(y, x, FEAT_SECRET);
144 /* Closed, locked, or stuck doors (400/1000) */
145 else place_closed_door(y, x);
150 * Place a random type of normal door at the given location.
152 void place_closed_door(int y, int x)
156 if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
158 place_floor_bold(y, x);
162 /* Choose an object */
165 /* Closed doors (300/400) */
168 /* Create closed door */
169 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x00);
172 /* Locked doors (99/400) */
175 /* Create locked door */
176 cave_set_feat(y, x, FEAT_DOOR_HEAD + randint1(7));
179 /* Stuck doors (1/400) */
182 /* Create jammed door */
183 cave_set_feat(y, x, FEAT_DOOR_HEAD + 0x08 + randint0(8));
189 * Make an empty square floor, for the middle of rooms
191 void place_floor(int x1, int x2, int y1, int y2, bool light)
195 /* Place a full floor under the room */
196 for (y = y1 - 1; y <= y2 + 1; y++)
198 for (x = x1 - 1; x <= x2 + 1; x++)
200 place_floor_bold(y, x);
201 add_cave_info(y, x, CAVE_ROOM);
202 if (light) add_cave_info(y, x, CAVE_GLOW);
209 * Make an empty square room, only floor and wall grids
211 void place_room(int x1, int x2, int y1, int y2, bool light)
215 place_floor(x1, x2, y1, y2, light);
217 /* Walls around the room */
218 for (y = y1 - 1; y <= y2 + 1; y++)
220 place_outer_bold(y, x1 - 1);
221 place_outer_bold(y, x2 + 1);
223 for (x = x1 - 1; x <= x2 + 1; x++)
225 place_outer_bold(y1 - 1, x);
226 place_outer_bold(y2 + 1, x);
232 * Create up to "num" objects near the given coordinates
233 * Only really called by some of the "vault" routines.
235 void vault_objects(int y, int x, int num)
238 int i = 0, j = y, k = x;
243 /* Attempt to place 'num' objects */
244 for (; num > 0; --num)
246 /* Try up to 11 spots looking for empty space */
247 for (i = 0; i < 11; ++i)
249 /* Pick a random location */
250 while (dummy < SAFE_MAX_ATTEMPTS)
252 j = rand_spread(y, 2);
253 k = rand_spread(x, 3);
255 if (!in_bounds(j, k)) continue;
260 if (dummy >= SAFE_MAX_ATTEMPTS)
265 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
267 msg_print("Warning! Could not place vault object!");
274 /* Require "clean" floor space */
276 if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
279 if (randint0(100) < 75)
281 place_object(j, k, FALSE, FALSE);
290 /* Placement accomplished */
298 * Place a trap with a given displacement of point
300 void vault_trap_aux(int y, int x, int yd, int xd)
302 int count = 0, y1 = y, x1 = x;
308 for (count = 0; count <= 5; count++)
311 while (dummy < SAFE_MAX_ATTEMPTS)
313 y1 = rand_spread(y, yd);
314 x1 = rand_spread(x, xd);
316 if (!in_bounds(y1, x1)) continue;
320 if (dummy >= SAFE_MAX_ATTEMPTS)
325 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
327 msg_print("Warning! Could not place vault trap!");
333 /* Require "naked" floor grids */
334 c_ptr = &cave[y1][x1];
335 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
347 * Place some traps with a given displacement of given location
349 void vault_traps(int y, int x, int yd, int xd, int num)
353 for (i = 0; i < num; i++)
355 vault_trap_aux(y, x, yd, xd);
361 * Hack -- Place some sleeping monsters near the given location
363 void vault_monsters(int y1, int x1, int num)
368 /* Try to summon "num" monsters "near" the given location */
369 for (k = 0; k < num; k++)
371 /* Try nine locations */
372 for (i = 0; i < 9; i++)
376 /* Pick a nearby location */
377 scatter(&y, &x, y1, x1, d, 0);
379 /* Require "empty" floor grids */
381 if (!cave_empty_grid(c_ptr)) continue;
383 /* Place the monster (allow groups) */
384 monster_level = base_level + 2;
385 (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
386 monster_level = base_level;
393 * Count the number of walls adjacent to the given grid.
395 * Note -- Assumes "in_bounds(y, x)"
397 * We count only granite walls and permanent walls.
399 int next_to_walls(int y, int x)
403 if (cave_floor_grid(&cave[y + 1][x])) k++;
404 if (cave_floor_grid(&cave[y - 1][x])) k++;
405 if (cave_floor_grid(&cave[y][x + 1])) k++;
406 if (cave_floor_grid(&cave[y][x - 1])) k++;
413 * Always picks a correct direction
415 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
417 /* Extract vertical and horizontal directions */
418 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
419 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
421 /* Never move diagonally */
424 if (randint0(100) < 50)
434 * Pick a random direction
436 void rand_dir(int *rdir, int *cdir)
438 /* Pick a random direction */
441 /* Extract the dy/dx components */
447 /* Function that sees if a square is a floor. (Includes range checking.) */
448 bool get_is_floor(int x, int y)
450 if (!in_bounds(y, x))
456 /* Do the real check */
457 if (is_floor_bold(y, x)) return (TRUE);
463 /* Set a square to be floor. (Includes range checking.) */
464 void set_floor(int x, int y)
466 if (!in_bounds(y, x))
472 if (cave[y][x].info & CAVE_ROOM)
474 /* A room border don't touch. */
478 /* Set to be floor if is a wall (don't touch lakes). */
479 if (is_extra_bold(y, x))
480 place_floor_bold(y, x);
486 * Constructs a tunnel between two points
488 * This function must be called BEFORE any streamers are created,
489 * since we use the special "granite wall" sub-types to keep track
490 * of legal places for corridors to pierce rooms.
492 * We use "door_flag" to prevent excessive construction of doors
493 * along overlapping corridors.
495 * We queue the tunnel grids to prevent door creation along a corridor
496 * which intersects itself.
498 * We queue the wall piercing grids to prevent a corridor from leaving
499 * a room and then coming back in through the same entrance.
501 * We "pierce" grids which are "outer" walls of rooms, and when we
502 * do so, we change all adjacent "outer" walls of rooms into "solid"
503 * walls so that no two corridors may use adjacent grids for exits.
505 * The "solid" wall check prevents corridors from "chopping" the
506 * corners of rooms off, as well as "silly" door placement, and
507 * "excessively wide" room entrances.
509 * Useful "feat" values:
510 * FEAT_WALL_EXTRA -- granite walls
511 * FEAT_WALL_INNER -- inner room walls
512 * FEAT_WALL_OUTER -- outer room walls
513 * FEAT_WALL_SOLID -- solid room walls
514 * FEAT_PERM_EXTRA -- shop walls (perma)
515 * FEAT_PERM_INNER -- inner room walls (perma)
516 * FEAT_PERM_OUTER -- outer room walls (perma)
517 * FEAT_PERM_SOLID -- dungeon border (perma)
519 void build_tunnel(int row1, int col1, int row2, int col2)
522 int tmp_row, tmp_col;
523 int row_dir, col_dir;
524 int start_row, start_col;
525 int main_loop_count = 0;
527 bool door_flag = FALSE;
531 /* Save the starting location */
535 /* Start out in the correct direction */
536 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
538 /* Keep going until done (or bored) */
539 while ((row1 != row2) || (col1 != col2))
541 /* Mega-Hack -- Paranoia -- prevent infinite loops */
542 if (main_loop_count++ > 2000) break;
544 /* Allow bends in the tunnel */
545 if (randint0(100) < dun_tun_chg)
547 /* Acquire the correct direction */
548 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
550 /* Random direction */
551 if (randint0(100) < dun_tun_rnd)
553 rand_dir(&row_dir, &col_dir);
557 /* Get the next location */
558 tmp_row = row1 + row_dir;
559 tmp_col = col1 + col_dir;
562 /* Extremely Important -- do not leave the dungeon */
563 while (!in_bounds(tmp_row, tmp_col))
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);
574 /* Get the next location */
575 tmp_row = row1 + row_dir;
576 tmp_col = col1 + col_dir;
580 /* Access the location */
581 c_ptr = &cave[tmp_row][tmp_col];
584 /* Avoid the edge of the dungeon */
585 if (c_ptr->feat == FEAT_PERM_SOLID) continue;
587 /* Avoid the edge of vaults */
588 if (c_ptr->feat == FEAT_PERM_OUTER) continue;
590 /* Avoid "solid" granite walls */
591 if (is_solid_grid(c_ptr)) continue;
593 /* Pierce "outer" walls of rooms */
594 if (is_outer_grid(c_ptr))
596 /* Acquire the "next" location */
597 y = tmp_row + row_dir;
598 x = tmp_col + col_dir;
600 /* Hack -- Avoid outer/solid permanent walls */
601 if (cave[y][x].feat == FEAT_PERM_SOLID) continue;
602 if (cave[y][x].feat == FEAT_PERM_OUTER) continue;
604 /* Hack -- Avoid outer/solid granite walls */
605 if (is_outer_bold(y, x)) continue;
606 if (is_solid_bold(y, x)) continue;
608 /* Accept this location */
612 /* Save the wall location */
613 if (dun->wall_n < WALL_MAX)
615 dun->wall[dun->wall_n].y = row1;
616 dun->wall[dun->wall_n].x = col1;
620 /* Forbid re-entry near this piercing */
621 for (y = row1 - 1; y <= row1 + 1; y++)
623 for (x = col1 - 1; x <= col1 + 1; x++)
625 /* Convert adjacent "outer" walls as "solid" walls */
626 if (is_outer_bold(y, x))
628 /* Change the wall to a "solid" wall */
629 place_solid_noperm_bold(y, x);
635 /* Travel quickly through rooms */
636 else if (c_ptr->info & (CAVE_ROOM))
638 /* Accept the location */
643 /* Tunnel through all other walls */
644 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
646 /* Accept this location */
650 /* Save the tunnel location */
651 if (dun->tunn_n < TUNN_MAX)
653 dun->tunn[dun->tunn_n].y = row1;
654 dun->tunn[dun->tunn_n].x = col1;
658 /* Allow door in next grid */
662 /* Handle corridor intersections or overlaps */
665 /* Accept the location */
669 /* Collect legal door locations */
672 /* Save the door location */
673 if (dun->door_n < DOOR_MAX)
675 dun->door[dun->door_n].y = row1;
676 dun->door[dun->door_n].x = col1;
680 /* No door in next grid */
684 /* Hack -- allow pre-emptive tunnel termination */
685 if (randint0(100) >= dun_tun_con)
687 /* Distance between row1 and start_row */
688 tmp_row = row1 - start_row;
689 if (tmp_row < 0) tmp_row = (-tmp_row);
691 /* Distance between col1 and start_col */
692 tmp_col = col1 - start_col;
693 if (tmp_col < 0) tmp_col = (-tmp_col);
695 /* Terminate the tunnel */
696 if ((tmp_row > 10) || (tmp_col > 10)) break;
704 * This routine adds the square to the tunnel
705 * It also checks for SOLID walls - and returns a nearby
706 * non-SOLID square in (x,y) so that a simple avoiding
707 * routine can be used. The returned boolean value reflects
708 * whether or not this routine hit a SOLID wall.
710 * "affectwall" toggles whether or not this new square affects
711 * the boundaries of rooms. - This is used by the catacomb
714 static bool set_tunnel(int *x, int *y, bool affectwall)
716 int feat, i, j, dx, dy;
718 cave_type *c_ptr = &cave[*y][*x];
720 if (!in_bounds(*y, *x)) return TRUE;
724 if ((feat == FEAT_PERM_OUTER) ||
725 (feat == FEAT_PERM_INNER) ||
726 is_inner_grid(c_ptr))
729 * Ignore permanent walls - sometimes cannot tunnel around them anyway
730 * so don't try - it just complicates things unnecessarily.
735 if (is_extra_bold(*y,*x))
737 /* Save the tunnel location */
738 if (dun->tunn_n < TUNN_MAX)
740 dun->tunn[dun->tunn_n].y = *y;
741 dun->tunn[dun->tunn_n].x = *x;
748 if (is_floor_bold(*y, *x))
750 /* Don't do anything */
754 if (is_outer_grid(c_ptr) && affectwall)
756 /* Save the wall location */
757 if (dun->wall_n < WALL_MAX)
759 dun->wall[dun->wall_n].y = *y;
760 dun->wall[dun->wall_n].x = *x;
764 /* Forbid re-entry near this piercing */
765 for (j = *y - 1; j <= *y + 1; j++)
767 for (i = *x - 1; i <= *x + 1; i++)
769 /* Convert adjacent "outer" walls as "solid" walls */
770 if (is_outer_bold(j, i))
772 /* Change the wall to a "solid" wall */
773 place_solid_noperm_bold(j, i);
777 place_floor_bold(*y, *x);
782 if (is_solid_grid(c_ptr) && affectwall)
784 /* cannot place tunnel here - use a square to the side */
786 /* find usable square and return value in (x,y) */
792 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
794 dy = randint0(3) - 1;
795 dx = randint0(3) - 1;
797 if (!in_bounds(*y + dy, *x + dx))
808 /* Failed for some reason: hack - ignore the solidness */
809 place_outer_grid(c_ptr);
814 /* Give new, acceptable coordinate. */
826 * This routine creates the catacomb-like tunnels by removing extra rock.
827 * Note that this routine is only called on "even" squares - so it gives
828 * a natural checkerboard pattern.
830 static void create_cata_tunnel(int x, int y)
837 set_tunnel(&x1, &y1, FALSE);
841 set_tunnel(&x1, &y1, FALSE);
845 set_tunnel(&x1, &y1, FALSE);
849 set_tunnel(&x1, &y1, FALSE);
854 * This routine does the bulk of the work in creating the new types of tunnels.
855 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)
856 * It doesn't need to add any complexity - straight lines are fine.
857 * The SOLID walls are avoided by a recursive algorithm which tries random ways
858 * around the obstical until it works. The number of itterations is counted, and it
859 * this gets too large the routine exits. This should stop any crashes - but may leave
860 * small gaps in the tunnel where there are too many SOLID walls.
862 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used
863 * as a part of the dodge SOLID walls algorithm.
865 * Type 2 tunnels are made of two straight lines at right angles. When this is used with
866 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.
868 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.
869 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near
872 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
877 /* Check for early exit */
878 if (!(*fail)) return;
880 length = distance(x1, y1, x2, y2);
884 if ((type == 1) && (length != 0))
887 for (i = 0; i <= length; i++)
889 x = x1 + i * (x2 - x1) / length;
890 y = y1 + i * (y2 - y1) / length;
891 if (!set_tunnel(&x, &y, TRUE))
895 /* This isn't working - probably have an infinite loop */
900 /* solid wall - so try to go around */
901 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
902 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
906 else if ((type == 2) || (type == 3))
910 for (i = x1; i <= x2; i++)
914 if (!set_tunnel(&x, &y, TRUE))
916 /* solid wall - so try to go around */
917 short_seg_hack(x, y, i - 1, y1, 1, count, fail);
918 short_seg_hack(x, y, i + 1, y1, 1, count, fail);
920 if ((type == 3) && ((x + y) % 2))
922 create_cata_tunnel(i, y1);
928 for (i = x2; i <= x1; 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);
947 for (i = y1; i <= y2; i++)
951 if (!set_tunnel(&x, &y, TRUE))
953 /* solid wall - so try to go around */
954 short_seg_hack(x, y, x2, i - 1, 1, count, fail);
955 short_seg_hack(x, y, x2, i + 1, 1, count, fail);
957 if ((type == 3) && ((x + y) % 2))
959 create_cata_tunnel(x2, i);
965 for (i = y2; i <= y1; 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);
986 * This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
987 * Permanent rock is ignored in this path finding- sometimes there is no
988 * path around anyway -so there will be a crash if we try to find one.
989 * This routine is much like the river creation routine in Zangband.
990 * It works by dividing a line segment into two. The segments are divided
991 * until they are less than "cutoff" - when the corresponding routine from
992 * "short_seg_hack" is called.
993 * Note it is VERY important that the "stop if hit another passage" logic
994 * stays as is. Without this the dungeon turns into Swiss Cheese...
996 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
999 int changex, changey;
1003 bool retval, firstsuccede;
1006 length = distance(x1, y1, x2, y2);
1008 if (length > cutoff)
1011 * Divide path in half and call routine twice.
1016 /* perturbation perpendicular to path */
1017 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1019 /* perturbation perpendicular to path */
1020 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1022 /* Work out "mid" ponit */
1023 x3 = x1 + dx + changex;
1024 y3 = y1 + dy + changey;
1026 /* See if in bounds - if not - do not perturb point */
1027 if (!in_bounds(y3, x3))
1032 /* cache midvalue */
1033 c_ptr = &cave[y3][x3];
1034 midval = cave[y3][x3].feat;
1035 if (is_solid_grid(c_ptr))
1037 /* move midpoint a bit to avoid problem. */
1043 while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1045 dy = randint0(3) - 1;
1046 dx = randint0(3) - 1;
1047 if (!in_bounds(y3 + dy, x3 + dx))
1057 /* Failed for some reason: hack - ignore the solidness */
1058 place_outer_bold(y3, x3);
1064 c_ptr = &cave[y3][x3];
1065 midval = cave[y3][x3].feat;
1068 if (is_floor_grid(c_ptr))
1070 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1072 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1074 /* do second half only if works + if have hit a room */
1075 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1079 /* have hit another tunnel - make a set of doors here */
1082 /* Save the door location */
1083 if (dun->door_n < DOOR_MAX)
1085 dun->door[dun->door_n].y = y3;
1086 dun->door[dun->door_n].x = x3;
1090 firstsuccede = TRUE;
1094 /* false- didn't work all the way */
1096 firstsuccede = FALSE;
1101 /* tunnel through walls */
1102 if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1104 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1105 firstsuccede = TRUE;
1109 /* false- didn't work all the way */
1111 firstsuccede = FALSE;
1116 /* only do this if the first half has worked */
1117 set_tunnel(&x3, &y3, TRUE);
1119 /* return value calculated above */
1124 /* Do a short segment */
1126 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1128 /* Hack - ignore return value so avoid infinite loops */