4 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6 * This software may be copied and distributed for educational, research,
7 * and not for profit purposes provided that this copyright and statement
8 * are included in all such copies. Other copyrights may also apply.
11 /* Purpose: Dungeon generation */
14 * Note that Level generation is *not* an important bottleneck,
15 * though it can be annoyingly slow on older machines... Thus
16 * we emphasize "simplicity" and "correctness" over "speed".
18 * This entire file is only needed for generating levels.
19 * This may allow smart compilers to only load it when needed.
21 * Consider the "v_info.txt" file for vault generation.
23 * In this file, we use the "special" granite and perma-wall sub-types,
24 * where "basic" is normal, "inner" is inside a room, "outer" is the
25 * outer wall of a room, and "solid" is the outer wall of the dungeon
26 * or any walls that may not be pierced by corridors. Thus the only
27 * wall type that may be pierced by a corridor is the "outer granite"
28 * type. The "basic granite" type yields the "actual" corridors.
30 * Note that we use the special "solid" granite wall type to prevent
31 * multiple corridors from piercing a wall in two adjacent locations,
32 * which would be messy, and we use the special "outer" granite wall
33 * to indicate which walls "surround" rooms, and may thus be "pierced"
34 * by corridors entering or leaving the room.
36 * Note that a tunnel which attempts to leave a room near the "edge"
37 * of the dungeon in a direction toward that edge will cause "silly"
38 * wall piercings, but will have no permanently incorrect effects,
39 * as long as the tunnel can *eventually* exit from another side.
40 * And note that the wall may not come back into the room by the
41 * hole it left through, so it must bend to the left or right and
42 * then optionally re-enter the room (at least 2 grids away). This
43 * is not a problem since every room that is large enough to block
44 * the passage of tunnels is also large enough to allow the tunnel
45 * to pierce the room itself several times.
47 * Note that no two corridors may enter a room through adjacent grids,
48 * they must either share an entryway or else use entryways at least
49 * two grids apart. This prevents "large" (or "silly") doorways.
51 * To create rooms in the dungeon, we first divide the dungeon up
52 * into "blocks" of 11x11 grids each, and require that all rooms
53 * occupy a rectangular group of blocks. As long as each room type
54 * reserves a sufficient number of blocks, the room building routines
55 * will not need to check bounds. Note that most of the normal rooms
56 * actually only use 23x11 grids, and so reserve 33x11 grids.
58 * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)
59 * allows more variability in the horizontal placement of rooms, and
60 * at the same time has the disadvantage that some rooms (two thirds
61 * of the normal rooms) may be "split" by panel boundaries. This can
62 * induce a situation where a player is in a room and part of the room
63 * is off the screen. It may be annoying enough to go back to 33x11
64 * blocks to prevent this visual situation.
66 * Note that the dungeon generation routines are much different (2.7.5)
67 * and perhaps "DUN_ROOMS" should be less than 50.
69 * XXX XXX XXX Note that it is possible to create a room which is only
70 * connected to itself, because the "tunnel generation" code allows a
71 * tunnel to leave a room, wander around, and then re-enter the room.
73 * XXX XXX XXX Note that it is possible to create a set of rooms which
74 * are only connected to other rooms in that set, since there is nothing
75 * explicit in the code to prevent this from happening. But this is less
76 * likely than the "isolated room" problem, because each room attempts to
77 * connect to another room, in a giant cycle, thus requiring at least two
78 * bizarre occurances to create an isolated section of the dungeon.
80 * Note that (2.7.9) monster pits have been split into monster "nests"
81 * and monster "pits". The "nests" have a collection of monsters of a
82 * given type strewn randomly around the room (jelly, animal, or undead),
83 * while the "pits" have a collection of monsters of a given type placed
84 * around the room in an organized manner (orc, troll, giant, dragon, or
85 * demon). Note that both "nests" and "pits" are now "level dependant",
86 * and both make 16 "expensive" calls to the "get_mon_num()" function.
88 * Note that the cave grid flags changed in a rather drastic manner
89 * for Angband 2.8.0 (and 2.7.9+), in particular, dungeon terrain
90 * features, such as doors and stairs and traps and rubble and walls,
91 * are all handled as a set of 64 possible "terrain features", and
92 * not as "fake" objects (440-479) as in pre-2.8.0 versions.
94 * The 64 new "dungeon features" will also be used for "visual display"
95 * but we must be careful not to allow, for example, the user to display
96 * hidden traps in a different way from floors, or secret doors in a way
97 * different from granite walls, or even permanent granite in a different
98 * way from granite. XXX XXX XXX
102 #include "generate.h"
117 * Dungeon generation data -- see "cave_gen()"
122 * Places some staircases near walls
124 static bool alloc_stairs(int feat, int num, int walls)
126 int y, x, i, j, flag;
130 if (feat == FEAT_LESS)
132 /* No up stairs in town or in ironman mode */
133 if (ironman_downward || !dun_level) return TRUE;
135 if (dun_level > d_info[dungeon_type].mindepth)
136 more_num = (randint1(num+1))/2;
138 else if (feat == FEAT_MORE)
140 int q_idx = quest_number(dun_level);
142 /* No downstairs on quest levels */
143 if (dun_level > 1 && q_idx)
145 monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
147 /* The quest monster(s) is still alive? */
148 if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
152 /* No downstairs at the bottom */
153 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
155 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
156 more_num = (randint1(num)+1)/2;
159 /* Place "num" stairs */
160 for (i = 0; i < num; i++)
162 /* Place some stairs */
163 for (flag = FALSE; !flag; )
165 /* Try several times, then decrease "walls" */
166 for (j = 0; !flag && j <= 3000; j++)
168 /* Pick a random grid */
169 y = randint1(cur_hgt-2);
170 x = randint1(cur_wid-2);
172 /* Access the grid */
175 /* Require "naked" floor grid */
176 if (!is_floor_grid(c_ptr) || pattern_tile(y,x) || c_ptr->o_idx || c_ptr->m_idx) continue;
178 /* Require a certain number of adjacent walls */
179 if (next_to_walls(y, x) < walls) continue;
181 /* Clear possible garbage of hidden trap */
184 /* Clear previous contents, add stairs */
185 if (i < more_num) c_ptr->feat = feat+0x07;
186 else c_ptr->feat = feat;
192 if (!flag) return FALSE;
193 /* Require fewer walls */
202 * Allocates some objects (using "place" and "type")
204 static void alloc_object(int set, int typ, int num)
210 /* Place some objects */
211 for (k = 0; k < num; k++)
213 /* Pick a "legal" spot */
214 while (dummy < SAFE_MAX_ATTEMPTS)
221 y = randint0(cur_hgt);
222 x = randint0(cur_wid);
226 /* Require "naked" floor grid */
227 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
229 /* Avoid player location */
230 if (py == y && px == x) continue;
232 /* Check for "room" */
233 room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
235 /* Require corridor? */
236 if ((set == ALLOC_SET_CORR) && room) continue;
239 if ((set == ALLOC_SET_ROOM) && !room) continue;
245 if (dummy >= SAFE_MAX_ATTEMPTS)
250 msg_print("·Ù¹ð¡ª¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
252 msg_print("Warning! Could not place object!");
260 /* Place something */
263 case ALLOC_TYP_RUBBLE:
266 cave[y][x].info &= ~(CAVE_FLOOR);
273 cave[y][x].info &= ~(CAVE_FLOOR);
283 case ALLOC_TYP_OBJECT:
285 place_object(y, x, FALSE, FALSE);
294 * Count the number of "corridor" grids adjacent to the given grid.
296 * Note -- Assumes "in_bounds(y1, x1)"
298 * XXX XXX This routine currently only counts actual "empty floor"
299 * grids which are not in rooms. We might want to also count stairs,
300 * open doors, closed doors, etc.
302 static int next_to_corr(int y1, int x1)
308 /* Scan adjacent grids */
309 for (i = 0; i < 4; i++)
311 /* Extract the location */
315 /* Access the grid */
318 /* Skip non floors */
319 if (!cave_floor_grid(c_ptr)) continue;
321 /* Skip non "empty floor" grids */
322 if (!is_floor_grid(c_ptr))
325 /* Skip grids inside rooms */
326 if (c_ptr->info & (CAVE_ROOM)) continue;
328 /* Count these grids */
332 /* Return the number of corridors */
338 * Determine if the given location is "between" two walls,
339 * and "next to" two corridor spaces. XXX XXX XXX
341 * Assumes "in_bounds(y, x)"
343 static bool possible_doorway(int y, int x)
345 /* Count the adjacent corridors */
346 if (next_to_corr(y, x) >= 2)
349 if ((cave[y-1][x].feat >= FEAT_MAGMA) &&
350 (cave[y+1][x].feat >= FEAT_MAGMA))
355 /* Check Horizontal */
356 if ((cave[y][x-1].feat >= FEAT_MAGMA) &&
357 (cave[y][x+1].feat >= FEAT_MAGMA))
369 * Places door at y, x position if at least 2 walls found
371 static void try_door(int y, int x)
374 if (!in_bounds(y, x)) return;
377 if (!cave_floor_bold(y, x)) return;
379 /* Ignore room grids */
380 if (cave[y][x].info & (CAVE_ROOM)) return;
382 /* Occasional door (if allowed) */
383 if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
386 place_random_door(y, x);
391 /* Place quest monsters */
392 void place_quest_monsters(void)
396 /* Handle the quest monster placements */
397 for (i = 0; i < max_quests; i++)
403 if (quest[i].status != QUEST_STATUS_TAKEN ||
404 (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
405 quest[i].type != QUEST_TYPE_RANDOM) ||
406 quest[i].level != dun_level ||
407 dungeon_type != quest[i].dungeon ||
408 (quest[i].flags & QUEST_FLAG_PRESET))
414 r_ptr = &r_info[quest[i].r_idx];
416 /* Hack -- "unique" monsters must be "unique" */
417 if ((r_ptr->flags1 & RF1_UNIQUE) &&
418 (r_ptr->cur_num >= r_ptr->max_num)) continue;
420 mode = (PM_NO_KAGE | PM_NO_PET);
422 if (!(r_ptr->flags1 & RF1_FRIENDS))
423 mode |= PM_ALLOW_GROUP;
425 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
429 for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
434 /* Find an empty grid */
435 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
439 y = randint0(cur_hgt);
440 x = randint0(cur_wid);
443 if (!cave_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
444 if (distance(y, x, py, px) < 10) continue;
448 /* Failed to place */
451 /* Try to place the monster */
452 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
459 /* Failure - Try again */
469 * Generate a new dungeon level
471 * Note that "dun_body" adds about 4000 bytes of memory to the stack.
473 static bool cave_gen(void)
475 int i, j, k, y, x, y1, x1;
477 int max_vault_ok = 2;
479 int feat1 = 0, feat2 = 0;
483 bool destroyed = FALSE;
484 bool empty_level = FALSE;
491 /* Fill the arrays of floors and walls in the good proportions */
492 set_floor_and_wall(dungeon_type);
495 /* Prepare allocation table */
496 get_mon_num_prep(get_monster_hook(), NULL);
498 feat_wall_outer = d_info[dungeon_type].outer_wall;
499 feat_wall_inner = d_info[dungeon_type].inner_wall;
500 feat_wall_solid = d_info[dungeon_type].outer_wall;
505 if (cur_hgt <= SCREEN_HGT / 2 - 2) max_vault_ok--;
506 if (cur_wid <= SCREEN_WID / 2 - 2) max_vault_ok--;
508 /* Randomize the dungeon creation values */
509 dun_rooms = rand_range(DUN_ROOMS_MIN, DUN_ROOMS_MAX);
510 dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
511 dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
512 dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
513 dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
514 dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
516 /* Empty arena levels */
517 if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
523 msg_print("¥¢¥ê¡¼¥Ê¥ì¥Ù¥ë");
525 msg_print("Arena level.");
531 /* Hack -- Start with basic granite */
532 for (y = 0; y < cur_hgt; y++)
534 for (x = 0; x < cur_wid; x++)
536 cave_type *c_ptr = &cave[y][x];
540 place_floor_grid(c_ptr);
544 /* Create granite wall */
545 place_extra_grid(c_ptr);
550 #ifdef ALLOW_CAVERNS_AND_LAKES
551 /* Possible "destroyed" level */
552 if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
556 /* extra rubble around the place looks cool */
557 build_lake(3+randint0(2));
560 /* Make a lake some of the time */
561 if (one_in_(LAKE_LEVEL) && !empty_level && !destroyed &&
562 (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
565 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
566 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
567 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
568 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
570 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
573 if ((dun_level > 80) && (randint0(count) < 2)) laketype = 1;
577 if (!laketype && (dun_level > 80) && one_in_(count)) laketype = 7;
581 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !laketype)
584 if ((dun_level > 50) && randint0(count) < 2) laketype = 2;
588 if (!laketype && (dun_level > 50) && one_in_(count)) laketype = 6;
592 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !laketype)
595 if ((dun_level > 35) && (randint0(count) < 2)) laketype = 3;
598 /* Lake of rubble2 */
599 if (!laketype && (dun_level > 35) && one_in_(count)) laketype = 4;
604 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !laketype) laketype = 5;
610 msg_print("¸Ð¤òÀ¸À®¡£");
612 msg_print("Lake on the level.");
615 build_lake(laketype);
619 if ((dun_level > DUN_CAVERN) && !empty_level &&
620 (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
621 (laketype == 0) && !destroyed && (randint1(1000) < dun_level))
625 /* make a large fractal cave in the middle of the dungeon */
629 msg_print("ƶ·¢¤òÀ¸À®¡£");
631 msg_print("Cavern on level.");
637 #endif /* ALLOW_CAVERNS_AND_LAKES */
639 /* Hack -- No destroyed "quest" levels */
640 if (quest_number(dun_level)) destroyed = FALSE;
642 /* Actual maximum number of rooms on this level */
643 dun->row_rooms = cur_hgt / BLOCK_HGT;
644 dun->col_rooms = cur_wid / BLOCK_WID;
646 /* Initialize the room table */
647 for (y = 0; y < dun->row_rooms; y++)
649 for (x = 0; x < dun->col_rooms; x++)
651 dun->room_map[y][x] = FALSE;
656 /* No "crowded" rooms yet */
664 /* Build some rooms */
665 if (!(d_info[dungeon_type].flags1 & DF1_MAZE))
667 for (i = 0; i < dun_rooms; i++)
669 bool force_rooms = (ironman_rooms && !((d_info[dungeon_type].flags1 & DF1_BEGINNER) || (d_info[dungeon_type].flags1 & DF1_CHAMELEON)));
671 /* Pick a block for the room */
672 y = randint0(dun->row_rooms);
673 x = randint0(dun->col_rooms);
675 /* Align dungeon rooms */
676 if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
678 /* Slide some rooms right */
679 if ((x % 3) == 0) x++;
681 /* Slide some rooms left */
682 if ((x % 3) == 2) x--;
685 /* Attempt an "unusual" room */
686 if (force_rooms || (randint0(DUN_UNUSUAL) < dun_level))
688 /* Roll for room type */
691 k = (force_rooms ? 0 : randint0(100));
692 if (force_rooms) break;
693 if ((d_info[dungeon_type].flags1 & DF1_NO_VAULT) && (k < 14)) continue;
697 /* Attempt a very unusual room */
698 if (force_rooms || (randint0(DUN_UNUSUAL) < dun_level))
701 if (room_build(y, x, 8)) continue;
703 /* Type 8 -- Greater vault (4%) */
706 if (max_vault_ok > 1)
708 if (room_build(y, x, 8)) continue;
713 if (cheat_room) msg_print("µðÂç¤ÊÃϲ¼¼¼¤òµÑ²¼¤·¤Þ¤¹¡£");
715 if (cheat_room) msg_print("Refusing a greater vault.");
721 /* Type 7 -- Lesser vault (6%) */
724 if (max_vault_ok > 0)
726 if (room_build(y, x, 7)) continue;
731 if (cheat_room) msg_print("¾®¤µ¤ÊÃϲ¼¼¼¤òµÑ²¼¤·¤Þ¤¹¡£");
733 if (cheat_room) msg_print("Refusing a lesser vault.");
740 /* Type 10 -- Random vault (4%) */
741 if ((k < 14) && room_build(y, x, 10)) continue;
743 /* Type 5 -- Monster nest (8%) */
744 if ((k < 22) && room_build(y, x, 5)) continue;
746 /* Type 6 -- Monster pit (10%) */
747 if ((k < 32) && room_build(y, x, 6)) continue;
749 /* Type 13 -- Trapped monster pit (5%) */
750 if ((k < 37) && room_build(y, x, 13)) continue;
752 /* Type 14 -- Trapped room (5%) */
753 if ((k < 42) && room_build(y, x, 14)) continue;
758 /* Type 2 -- Overlapping (25%) */
759 if ((k < 25) && room_build(y, x, 2)) continue;
761 /* Type 3 -- Cross room (25%) */
762 if ((k < 50) && room_build(y, x, 3)) continue;
764 if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
766 if (room_build(y, x, 4)) continue;
770 /* Type 4 -- Large room (25%) */
771 if ((k < 75) && room_build(y, x, 4)) continue;
773 /* Type 11 -- Circular (10%) */
774 if ((k < 85) && room_build(y, x, 11)) continue;
776 /* Type 12 -- Crypt (15%) */
777 if ((k < 100) && room_build(y, x, 12)) continue;
781 /* The deeper you are, the more cavelike the rooms are */
784 /* No caves when a cavern exists: they look bad */
785 if (((k < dun_level) || (d_info[dungeon_type].flags1 & DF1_CAVE)) && (!cavern) && (!empty_level) && (laketype == 0) && !(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
787 /* Type 9 -- Fractal cave */
788 if (room_build(y, x, 9)) continue;
791 /* Attempt a "trivial" room */
793 if (room_build(y, x, 1)) continue;
797 /* Make a hole in the dungeon roof sometimes at level 1 */
800 while (one_in_(DUN_MOS_DEN))
802 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
806 /* Destroy the level if necessary */
807 if (destroyed) destroy_level();
809 /* Hack -- Add some rivers */
810 if (one_in_(3) && (randint1(dun_level) > 5))
812 /* Choose water or lava */
813 if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
815 feat1 = FEAT_DEEP_WATER;
816 feat2 = FEAT_SHAL_WATER;
818 else if (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
820 feat1 = FEAT_DEEP_LAVA;
821 feat2 = FEAT_SHAL_LAVA;
826 /* Only add river if matches lake type or if have no lake at all */
827 if ((((laketype == 1) && (feat1 == FEAT_DEEP_LAVA)) ||
828 ((laketype == 2) && (feat1 == FEAT_DEEP_WATER)) ||
829 (laketype == 0)) && feat1)
831 add_river(feat1, feat2);
837 /* Reset flags from boundary walls -- Top and bottom */
838 for (x = 0; x < cur_wid; x++)
840 cave[0][x].info &= ~(CAVE_MASK);
841 cave[cur_hgt - 1][x].info &= ~(CAVE_MASK);
844 /* Reset flags from boundary walls -- Left and right */
845 for (y = 1; y < (cur_hgt - 1); y++)
847 cave[y][0].info &= ~(CAVE_MASK);
848 cave[y][cur_wid - 1].info &= ~(CAVE_MASK);
853 if (d_info[dungeon_type].flags1 & DF1_MAZE)
855 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
857 /* Place 3 or 4 down stairs near some walls */
858 if (!alloc_stairs(FEAT_MORE, rand_range(2, 3), 3)) return FALSE;
860 /* Place 1 or 2 up stairs near some walls */
861 if (!alloc_stairs(FEAT_LESS, 1, 3)) return FALSE;
865 /* Hack -- Scramble the room order */
866 for (i = 0; i < dun->cent_n; i++)
868 int pick1 = randint0(dun->cent_n);
869 int pick2 = randint0(dun->cent_n);
870 y1 = dun->cent[pick1].y;
871 x1 = dun->cent[pick1].x;
872 dun->cent[pick1].y = dun->cent[pick2].y;
873 dun->cent[pick1].x = dun->cent[pick2].x;
874 dun->cent[pick2].y = y1;
875 dun->cent[pick2].x = x1;
878 /* Start with no tunnel doors */
881 /* Hack -- connect the first room to the last room */
882 y = dun->cent[dun->cent_n-1].y;
883 x = dun->cent[dun->cent_n-1].x;
885 /* Connect all the rooms together */
886 for (i = 0; i < dun->cent_n; i++)
889 /* Reset the arrays */
893 /* Connect the room to the previous room */
894 if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
896 /* make cave-like tunnel */
897 build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
901 /* make normal tunnel */
902 build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x);
905 /* Turn the tunnel into corridor */
906 for (j = 0; j < dun->tunn_n; j++)
908 /* Access the grid */
912 /* Access the grid */
915 /* Clear previous contents (if not a lake), add a floor */
916 if ((c_ptr->feat < FEAT_DEEP_WATER) ||
917 (c_ptr->feat > FEAT_SHAL_LAVA))
919 /* Clear mimic type */
922 place_floor_grid(c_ptr);
926 /* Apply the piercings that we found */
927 for (j = 0; j < dun->wall_n; j++)
929 /* Access the grid */
933 /* Access the grid */
936 /* Clear mimic type */
939 /* Clear previous contents, add up floor */
940 place_floor_grid(c_ptr);
942 /* Occasional doorway */
943 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
945 /* Place a random door */
946 place_random_door(y, x);
950 /* Remember the "previous" room */
955 /* Place intersection doors */
956 for (i = 0; i < dun->door_n; i++)
958 /* Extract junction location */
962 /* Try placing doors */
969 /* Place 3 or 4 down stairs near some walls */
970 if (!alloc_stairs(FEAT_MORE, rand_range(3, 4), 3)) return FALSE;
972 /* Place 1 or 2 up stairs near some walls */
973 if (!alloc_stairs(FEAT_LESS, rand_range(1, 2), 3)) return FALSE;
979 if (d_info[dungeon_type].stream2)
981 /* Hack -- Add some quartz streamers */
982 for (i = 0; i < DUN_STR_QUA; i++)
984 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
988 if (d_info[dungeon_type].stream1)
990 /* Hack -- Add some magma streamers */
991 for (i = 0; i < DUN_STR_MAG; i++)
993 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
998 /* Special boundary walls -- Top and bottom */
999 for (x = 0; x < cur_wid; x++)
1001 cave_type *c_ptr = &cave[0][x];
1003 /* Set boundary mimic and add "solid" perma-wall */
1004 c_ptr->mimic = f_info[c_ptr->feat].mimic;
1005 c_ptr->feat = FEAT_PERM_SOLID;
1007 c_ptr = &cave[cur_hgt - 1][x];
1009 /* Set boundary mimic and add "solid" perma-wall */
1010 c_ptr->mimic = f_info[c_ptr->feat].mimic;
1011 c_ptr->feat = FEAT_PERM_SOLID;
1014 /* Special boundary walls -- Left and right */
1015 for (y = 1; y < (cur_hgt - 1); y++)
1017 cave_type *c_ptr = &cave[y][0];
1019 /* Set boundary mimic and add "solid" perma-wall */
1020 c_ptr->mimic = f_info[c_ptr->feat].mimic;
1021 c_ptr->feat = FEAT_PERM_SOLID;
1023 c_ptr = &cave[y][cur_wid - 1];
1025 /* Set boundary mimic and add "solid" perma-wall */
1026 c_ptr->mimic = f_info[c_ptr->feat].mimic;
1027 c_ptr->feat = FEAT_PERM_SOLID;
1030 /* Determine the character location */
1031 if (!new_player_spot()) return FALSE;
1033 place_quest_monsters();
1035 /* Basic "amount" */
1036 k = (dun_level / 3);
1040 /* Pick a base number of monsters */
1041 i = d_info[dungeon_type].min_m_alloc_level;
1043 /* To make small levels a bit more playable */
1044 if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1046 int small_tester = i;
1048 i = (i * cur_hgt) / MAX_HGT;
1049 i = (i * cur_wid) / MAX_WID;
1052 if (i > small_tester) i = small_tester;
1053 else if (cheat_hear)
1056 msg_format("¥â¥ó¥¹¥¿¡¼¿ô´ðËÜÃͤò %d ¤«¤é %d ¤Ë¸º¤é¤·¤Þ¤¹", small_tester, i);
1058 msg_format("Reduced monsters base from %d to %d", small_tester, i);
1066 /* Put some monsters in the dungeon */
1067 for (i = i + k; i > 0; i--)
1069 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1072 /* Place some traps in the dungeon */
1073 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1075 /* Put some rubble in corridors */
1076 alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1078 /* Mega Hack -- No object at first level of deeper dungeon */
1079 if (p_ptr->enter_dungeon && dun_level > 1)
1081 /* No stair scum! */
1085 /* Put some objects in rooms */
1086 alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1088 /* Put some objects/gold in the dungeon */
1089 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1090 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1093 /* Put an Artifact and Artifact Guardian is requested */
1094 if(d_info[dungeon_type].final_guardian && (d_info[dungeon_type].maxdepth == dun_level))
1100 /* Find a good position */
1103 /* Get a random spot */
1104 oy = randint1(cur_hgt - 4) + 2;
1105 ox = randint1(cur_wid - 4) + 2;
1107 /* Is it a good spot ? */
1108 if (cave_empty_bold2(oy, ox) && monster_can_cross_terrain(cave[oy][ox].feat, &r_info[d_info[dungeon_type].final_guardian]))
1110 /* Place the guardian */
1111 if (place_monster_aux(0, oy, ox, d_info[dungeon_type].final_guardian, (PM_ALLOW_GROUP | PM_NO_KAGE | PM_NO_PET))) break;
1118 if ((empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level))) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1121 for (y = 0; y < cur_hgt; y++)
1123 for (x = 0; x < cur_wid; x++)
1125 cave[y][x].info |= (CAVE_GLOW);
1135 * Builds the arena after it is entered -KMW-
1137 static void build_arena(void)
1139 int yval, y_height, y_depth, xval, x_left, x_right;
1142 yval = SCREEN_HGT / 2;
1143 xval = SCREEN_WID / 2;
1144 y_height = yval - 10;
1145 y_depth = yval + 10;
1147 x_right = xval + 32;
1149 for (i = y_height; i <= y_height + 5; i++)
1150 for (j = x_left; j <= x_right; j++)
1152 cave[i][j].feat = FEAT_PERM_EXTRA;
1153 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1155 for (i = y_depth; i >= y_depth - 5; i--)
1156 for (j = x_left; j <= x_right; j++)
1158 cave[i][j].feat = FEAT_PERM_EXTRA;
1159 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1161 for (j = x_left; j <= x_left + 17; j++)
1162 for (i = y_height; i <= y_depth; i++)
1164 cave[i][j].feat = FEAT_PERM_EXTRA;
1165 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1167 for (j = x_right; j >= x_right - 17; j--)
1168 for (i = y_height; i <= y_depth; i++)
1170 cave[i][j].feat = FEAT_PERM_EXTRA;
1171 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1174 cave[y_height+6][x_left+18].feat = FEAT_PERM_EXTRA;
1175 cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1176 cave[y_depth-6][x_left+18].feat = FEAT_PERM_EXTRA;
1177 cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1178 cave[y_height+6][x_right-18].feat = FEAT_PERM_EXTRA;
1179 cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1180 cave[y_depth-6][x_right-18].feat = FEAT_PERM_EXTRA;
1181 cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1185 cave[i][j].feat = FEAT_BLDG_HEAD + 2;
1186 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1187 player_place(i + 1, j);
1192 * Town logic flow for generation of arena -KMW-
1194 static void arena_gen(void)
1201 cur_hgt = SCREEN_HGT;
1202 cur_wid = SCREEN_WID;
1204 /* Start with solid walls */
1205 for (y = 0; y < MAX_HGT; y++)
1207 for (x = 0; x < MAX_WID; x++)
1209 /* Create "solid" perma-wall */
1210 cave[y][x].feat = FEAT_PERM_SOLID;
1212 /* Illuminate and memorize the walls */
1213 cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1217 /* Then place some floors */
1218 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1220 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1222 /* Create empty floor */
1223 cave[y][x].feat = FEAT_FLOOR;
1229 place_monster_aux(0, py + 5, px, arena_info[p_ptr->arena_number].r_idx,
1230 (PM_NO_KAGE | PM_NO_PET));
1236 * Builds the arena after it is entered -KMW-
1238 static void build_battle(void)
1240 int yval, y_height, y_depth, xval, x_left, x_right;
1243 yval = SCREEN_HGT / 2;
1244 xval = SCREEN_WID / 2;
1245 y_height = yval - 10;
1246 y_depth = yval + 10;
1248 x_right = xval + 32;
1250 for (i = y_height; i <= y_height + 5; i++)
1251 for (j = x_left; j <= x_right; j++)
1253 cave[i][j].feat = FEAT_PERM_EXTRA;
1254 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1256 for (i = y_depth; i >= y_depth - 3; i--)
1257 for (j = x_left; j <= x_right; j++)
1259 cave[i][j].feat = FEAT_PERM_EXTRA;
1260 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1262 for (j = x_left; j <= x_left + 17; j++)
1263 for (i = y_height; i <= y_depth; i++)
1265 cave[i][j].feat = FEAT_PERM_EXTRA;
1266 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1268 for (j = x_right; j >= x_right - 17; j--)
1269 for (i = y_height; i <= y_depth; i++)
1271 cave[i][j].feat = FEAT_PERM_EXTRA;
1272 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1275 cave[y_height+6][x_left+18].feat = FEAT_PERM_EXTRA;
1276 cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1277 cave[y_depth-4][x_left+18].feat = FEAT_PERM_EXTRA;
1278 cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1279 cave[y_height+6][x_right-18].feat = FEAT_PERM_EXTRA;
1280 cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1281 cave[y_depth-4][x_right-18].feat = FEAT_PERM_EXTRA;
1282 cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1286 cave[i][j].feat = FEAT_BLDG_HEAD + 3;
1287 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1293 * Town logic flow for generation of arena -KMW-
1295 static void battle_gen(void)
1301 /* Start with solid walls */
1302 for (y = 0; y < MAX_HGT; y++)
1304 for (x = 0; x < MAX_WID; x++)
1306 /* Create "solid" perma-wall */
1307 cave[y][x].feat = FEAT_PERM_SOLID;
1309 /* Illuminate and memorize the walls */
1310 cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1314 /* Then place some floors */
1315 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1317 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1319 /* Create empty floor */
1320 cave[y][x].feat = FEAT_FLOOR;
1328 place_monster_aux(0, py + 5 + (i/2)*4, px - 2 + (i%2)*4, battle_mon[i],
1329 (PM_NO_KAGE | PM_NO_PET));
1330 set_friendly(&m_list[cave[py+5+(i/2)*4][px-2+(i%2)*4].m_idx]);
1332 for(i = 1; i < m_max; i++)
1334 monster_type *m_ptr = &m_list[i];
1336 if (!m_ptr->r_idx) continue;
1338 /* Hack -- Detect monster */
1339 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1341 /* Update the monster */
1342 update_mon(i, FALSE);
1348 * Generate a quest level
1350 static void quest_gen(void)
1355 /* Start with perm walls */
1356 for (y = 0; y < cur_hgt; y++)
1358 for (x = 0; x < cur_wid; x++)
1360 cave[y][x].feat = FEAT_PERM_SOLID;
1364 /* Set the quest level */
1365 base_level = quest[p_ptr->inside_quest].level;
1366 dun_level = base_level;
1367 object_level = base_level;
1368 monster_level = base_level;
1370 if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1372 /* Prepare allocation table */
1373 get_mon_num_prep(get_monster_hook(), NULL);
1375 init_flags = INIT_CREATE_DUNGEON | INIT_ASSIGN;
1377 process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1380 /* Make a real level */
1381 static bool level_gen(cptr *why)
1383 int level_height, level_width;
1385 if ((always_small_levels || ironman_small_levels ||
1386 (one_in_(SMALL_LEVEL) && small_levels) ||
1387 (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1388 (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1389 !(d_info[dungeon_type].flags1 & DF1_BIG))
1393 msg_print("¾®¤µ¤Ê¥Õ¥í¥¢");
1395 msg_print("A 'small' dungeon level.");
1399 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1404 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1413 level_height = randint1(MAX_HGT/SCREEN_HGT);
1414 level_width = randint1(MAX_WID/SCREEN_WID);
1416 while ((level_height == MAX_HGT/SCREEN_HGT) &&
1417 (level_width == MAX_WID/SCREEN_WID));
1420 cur_hgt = level_height * SCREEN_HGT;
1421 cur_wid = level_width * SCREEN_WID;
1423 /* Assume illegal panel */
1424 panel_row_min = cur_hgt;
1425 panel_col_min = cur_wid;
1428 msg_format("X:%d, Y:%d.", cur_hgt, cur_wid);
1436 /* Assume illegal panel */
1437 panel_row_min = cur_hgt;
1438 panel_col_min = cur_wid;
1441 /* Make a dungeon */
1445 *why = "¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ";
1447 *why = "could not place player";
1455 static byte extract_feeling(void)
1457 /* Hack -- no feeling in the town */
1458 if (!dun_level) return 0;
1460 /* Hack -- Have a special feeling sometimes */
1461 if (good_item_flag && !preserve_mode) return 1;
1463 if (rating > 100) return 2;
1464 if (rating > 80) return 3;
1465 if (rating > 60) return 4;
1466 if (rating > 40) return 5;
1467 if (rating > 30) return 6;
1468 if (rating > 20) return 7;
1469 if (rating > 10) return 8;
1470 if (rating > 0) return 9;
1472 if((turn - old_turn) > TURNS_PER_TICK * TOWN_DAWN /2)
1473 chg_virtue(V_PATIENCE, 1);
1480 * Wipe all unnecessary flags after cave generation
1482 static void wipe_generate_cave_flags(void)
1486 for (y = 0; y < cur_hgt; y++)
1488 for (x = 0; x < cur_wid; x++)
1490 /* Wipe unused flags */
1491 cave[y][x].info &= ~(CAVE_MASK);
1497 for (y = 1; y < cur_hgt - 1; y++)
1499 for (x = 1; x < cur_wid - 1; x++)
1501 /* There might be trap */
1502 cave[y][x].info |= CAVE_UNSAFE;
1510 * Clear and empty the cave
1512 void clear_cave(void)
1516 /* Very simplified version of wipe_o_list() */
1517 C_WIPE(o_list, o_max, object_type);
1521 /* Very simplified version of wipe_m_list() */
1522 for (i = 1; i < max_r_idx; i++)
1523 r_info[i].cur_num = 0;
1524 C_WIPE(m_list, m_max, monster_type);
1529 /* Start with a blank cave */
1530 for (y = 0; y < MAX_HGT; y++)
1532 for (x = 0; x < MAX_WID; x++)
1534 cave_type *c_ptr = &cave[y][x];
1561 /* Mega-Hack -- no player yet */
1564 /* Set the base level */
1565 base_level = dun_level;
1567 /* Reset the monster generation level */
1568 monster_level = base_level;
1570 /* Reset the object generation level */
1571 object_level = base_level;
1573 /* Nothing special here yet */
1574 good_item_flag = FALSE;
1576 /* Nothing good here yet */
1579 /* Fill the arrays of floors and walls in the good proportions */
1580 set_floor_and_wall(0);
1585 * Generates a random dungeon level -RAK-
1587 * Hack -- regenerate any "overflow" levels
1589 * Hack -- allow auto-scumming via a gameplay option.
1591 void generate_cave(void)
1596 for (num = 0; TRUE; num++)
1602 /* Clear and empty the cave */
1605 if ((d_info[dungeon_type].fill_type1 == FEAT_MAGMA_K) || (d_info[dungeon_type].fill_type2 == FEAT_MAGMA_K) || (d_info[dungeon_type].fill_type3 == FEAT_MAGMA_K)) rating += 40;
1607 /* Build the arena -KMW- */
1608 if (p_ptr->inside_arena)
1614 /* Build the battle -KMW- */
1615 else if (p_ptr->inside_battle)
1621 else if (p_ptr->inside_quest)
1626 /* Build the town */
1627 else if (!dun_level)
1629 /* Make the wilderness */
1630 if (p_ptr->wild_mode) wilderness_gen_small();
1631 else wilderness_gen();
1634 /* Build a real level */
1637 okay = level_gen(&why);
1640 /* Extract the feeling */
1641 feeling = extract_feeling();
1643 /* Prevent object over-flow */
1644 if (o_max >= max_o_idx)
1648 why = "¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë";
1650 why = "too many objects";
1657 /* Prevent monster over-flow */
1658 else if (m_max >= max_m_idx)
1662 why = "¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë";
1664 why = "too many monsters";
1672 /* Mega-Hack -- "auto-scum" */
1673 else if ((auto_scum || ironman_autoscum) && (num < 100) &&
1674 !p_ptr->inside_quest &&
1675 !(d_info[dungeon_type].flags1 & DF1_BEGINNER) &&
1676 !p_ptr->enter_dungeon)
1678 /* Require "goodness" */
1679 if ((feeling > 9) ||
1680 ((dun_level >= 7) && (feeling > 8)) ||
1681 ((dun_level >= 15) && (feeling > 7)) ||
1682 ((dun_level >= 35) && (feeling > 6)) ||
1683 ((dun_level >= 70) && (feeling > 5)))
1685 /* Give message to cheaters */
1686 if (cheat_room || cheat_hear ||
1687 cheat_peek || cheat_xtra)
1693 why = "boring level";
1708 if (why) msg_format("À¸À®¤ä¤êľ¤·(%s)", why);
1710 if (why) msg_format("Generation restarted (%s)", why);
1714 /* Wipe the objects */
1717 /* Wipe the monsters */
1721 /* Glow deep lava */
1725 p_ptr->enter_dungeon = FALSE;
1727 wipe_generate_cave_flags();