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"
115 * Dungeon generation data -- see "cave_gen()"
121 * Count the number of walls adjacent to the given grid.
123 * Note -- Assumes "in_bounds(y, x)"
125 * We count only granite walls and permanent walls.
127 static int next_to_walls(int y, int x)
131 if (cave_have_flag_bold(y + 1, x, FF_WALL)) k++;
132 if (cave_have_flag_bold(y - 1, x, FF_WALL)) k++;
133 if (cave_have_flag_bold(y, x + 1, FF_WALL)) k++;
134 if (cave_have_flag_bold(y, x - 1, FF_WALL)) k++;
141 * Places some staircases near walls
143 static bool alloc_stairs(int feat, int num, int walls)
145 int y, x, i, j, flag;
149 feature_type *f_ptr = &f_info[feat];
151 if (have_flag(f_ptr->flags, FF_LESS))
153 /* No up stairs in town or in ironman mode */
154 if (ironman_downward || !dun_level) return TRUE;
156 if (dun_level > d_info[dungeon_type].mindepth)
157 shaft_num = (randint1(num+1))/2;
159 else if (have_flag(f_ptr->flags, FF_MORE))
161 int q_idx = quest_number(dun_level);
163 /* No downstairs on quest levels */
164 if (dun_level > 1 && q_idx)
166 monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
168 /* The quest monster(s) is still alive? */
169 if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
173 /* No downstairs at the bottom */
174 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
176 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
177 shaft_num = (randint1(num)+1)/2;
184 /* There very few walls in an arena level */
185 if (dun->empty_level) walls = 1;
187 /* Place "num" stairs */
188 for (i = 0; i < num; i++)
190 /* Try several times */
191 for (flag = FALSE; !flag && (walls >= 0); walls--)
193 /* Try several times, then decrease "walls" */
194 for (j = 0; j <= 3000; j++)
196 /* Pick a random grid */
197 y = randint1(cur_hgt-2);
198 x = randint1(cur_wid-2);
200 /* Access the grid */
203 /* Require "naked" floor grid */
204 if (!is_floor_grid(c_ptr) || pattern_tile(y,x) || c_ptr->o_idx || c_ptr->m_idx) continue;
206 /* Require a certain number of adjacent walls */
207 if (next_to_walls(y, x) < walls) continue;
209 /* Clear possible garbage of hidden trap */
212 /* Clear previous contents, add stairs */
213 c_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
220 /* Require fewer walls */
223 if (!flag) return FALSE;
230 * Allocates some objects (using "place" and "type")
232 static void alloc_object(int set, int typ, int num)
238 /* A small level has few objects. */
239 num = num * cur_hgt * cur_wid / (MAX_HGT*MAX_WID) +1;
241 /* Place some objects */
242 for (k = 0; k < num; k++)
244 /* Pick a "legal" spot */
245 while (dummy < SAFE_MAX_ATTEMPTS)
252 y = randint0(cur_hgt);
253 x = randint0(cur_wid);
257 /* Require "naked" floor grid */
258 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
260 /* Avoid player location */
261 if (player_bold(y, x)) continue;
263 /* Check for "room" */
264 room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
266 /* Require corridor? */
267 if ((set == ALLOC_SET_CORR) && room) continue;
270 if ((set == ALLOC_SET_ROOM) && !room) continue;
276 if (dummy >= SAFE_MAX_ATTEMPTS)
281 msg_print("·Ù¹ð¡ª¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ¤Þ¤»¤ó¡ª");
283 msg_print("Warning! Could not place object!");
291 /* Place something */
294 case ALLOC_TYP_RUBBLE:
297 cave[y][x].info &= ~(CAVE_FLOOR);
304 cave[y][x].info &= ~(CAVE_FLOOR);
314 case ALLOC_TYP_OBJECT:
316 place_object(y, x, 0L);
325 * Count the number of "corridor" grids adjacent to the given grid.
327 * Note -- Assumes "in_bounds(y1, x1)"
329 * XXX XXX This routine currently only counts actual "empty floor"
330 * grids which are not in rooms. We might want to also count stairs,
331 * open doors, closed doors, etc.
333 static int next_to_corr(int y1, int x1)
339 /* Scan adjacent grids */
340 for (i = 0; i < 4; i++)
342 /* Extract the location */
346 /* Access the grid */
349 /* Skip non floors */
350 if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
352 /* Skip non "empty floor" grids */
353 if (!is_floor_grid(c_ptr))
356 /* Skip grids inside rooms */
357 if (c_ptr->info & (CAVE_ROOM)) continue;
359 /* Count these grids */
363 /* Return the number of corridors */
369 * Determine if the given location is "between" two walls,
370 * and "next to" two corridor spaces. XXX XXX XXX
372 * Assumes "in_bounds(y, x)"
374 static bool possible_doorway(int y, int x)
376 /* Count the adjacent corridors */
377 if (next_to_corr(y, x) >= 2)
380 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
381 cave_have_flag_bold(y + 1, x, FF_WALL))
386 /* Check Horizontal */
387 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
388 cave_have_flag_bold(y, x + 1, FF_WALL))
400 * Places door at y, x position if at least 2 walls found
402 static void try_door(int y, int x)
405 if (!in_bounds(y, x)) return;
408 if (cave_have_flag_bold(y, x, FF_WALL)) return;
410 /* Ignore room grids */
411 if (cave[y][x].info & (CAVE_ROOM)) return;
413 /* Occasional door (if allowed) */
414 if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
417 place_random_door(y, x, FALSE);
422 /* Place quest monsters */
423 void place_quest_monsters(void)
427 /* Handle the quest monster placements */
428 for (i = 0; i < max_quests; i++)
434 if (quest[i].status != QUEST_STATUS_TAKEN ||
435 (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
436 quest[i].type != QUEST_TYPE_RANDOM) ||
437 quest[i].level != dun_level ||
438 dungeon_type != quest[i].dungeon ||
439 (quest[i].flags & QUEST_FLAG_PRESET))
445 r_ptr = &r_info[quest[i].r_idx];
447 /* Hack -- "unique" monsters must be "unique" */
448 if ((r_ptr->flags1 & RF1_UNIQUE) &&
449 (r_ptr->cur_num >= r_ptr->max_num)) continue;
451 mode = (PM_NO_KAGE | PM_NO_PET);
453 if (!(r_ptr->flags1 & RF1_FRIENDS))
454 mode |= PM_ALLOW_GROUP;
456 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
460 for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
465 /* Find an empty grid */
466 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
468 y = randint0(cur_hgt);
469 x = randint0(cur_wid);
471 if (!monster_can_enter(y, x, r_ptr, 0)) continue;
472 if (distance(y, x, py, px) < 10) continue;
476 /* Failed to place */
479 /* Try to place the monster */
480 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
487 /* Failure - Try again */
497 * Set boundary mimic and add "solid" perma-wall
499 static void set_bound_perm_wall(cave_type *c_ptr)
501 if (bound_walls_perm)
503 /* Clear boundary mimic */
508 feature_type *f_ptr = &f_info[c_ptr->feat];
510 /* Hack -- Decline boundary walls with known treasure */
511 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
512 !have_flag(f_ptr->flags, FF_SECRET))
513 c_ptr->feat = feat_state(c_ptr->feat, FF_ENSECRET);
515 /* Set boundary mimic */
516 c_ptr->mimic = c_ptr->feat;
519 /* Add "solid" perma-wall */
520 place_solid_perm_grid(c_ptr);
525 * Generate various caverns and lakes
527 * There were moved from cave_gen().
529 static void gen_caverns_and_lakes(void)
531 #ifdef ALLOW_CAVERNS_AND_LAKES
532 /* Possible "destroyed" level */
533 if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
535 dun->destroyed = TRUE;
537 /* extra rubble around the place looks cool */
538 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
541 /* Make a lake some of the time */
542 if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
543 (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
546 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
547 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
548 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
549 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
551 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
554 if ((dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
558 if (!dun->laketype && (dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
562 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !dun->laketype)
565 if ((dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
569 if (!dun->laketype && (dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
573 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
576 if ((dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
579 /* Lake of rubble2 */
580 if (!dun->laketype && (dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
585 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
591 msg_print("¸Ð¤òÀ¸À®¡£");
593 msg_print("Lake on the level.");
596 build_lake(dun->laketype);
600 if ((dun_level > DUN_CAVERN) && !dun->empty_level &&
601 (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
602 !dun->laketype && !dun->destroyed && (randint1(1000) < dun_level))
606 /* make a large fractal cave in the middle of the dungeon */
610 msg_print("ƶ·¢¤òÀ¸À®¡£");
612 msg_print("Cavern on level.");
617 #endif /* ALLOW_CAVERNS_AND_LAKES */
619 /* Hack -- No destroyed "quest" levels */
620 if (quest_number(dun_level)) dun->destroyed = FALSE;
626 * Generate a new dungeon level
628 * Note that "dun_body" adds about 4000 bytes of memory to the stack.
630 static bool cave_gen(void)
639 dun->destroyed = FALSE;
640 dun->empty_level = FALSE;
644 /* Fill the arrays of floors and walls in the good proportions */
645 set_floor_and_wall(dungeon_type);
647 /* Prepare allocation table */
648 get_mon_num_prep(get_monster_hook(), NULL);
650 /* Randomize the dungeon creation values */
651 dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
652 dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
653 dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
654 dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
655 dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
657 /* Actual maximum number of rooms on this level */
658 dun->row_rooms = cur_hgt / BLOCK_HGT;
659 dun->col_rooms = cur_wid / BLOCK_WID;
661 /* Initialize the room table */
662 for (y = 0; y < dun->row_rooms; y++)
664 for (x = 0; x < dun->col_rooms; x++)
666 dun->room_map[y][x] = FALSE;
673 /* Empty arena levels */
674 if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
676 dun->empty_level = TRUE;
680 msg_print("¥¢¥ê¡¼¥Ê¥ì¥Ù¥ë");
682 msg_print("Arena level.");
686 if (dun->empty_level)
688 /* Start with floors */
689 for (y = 0; y < cur_hgt; y++)
691 for (x = 0; x < cur_wid; x++)
693 place_floor_bold(y, x);
697 /* Special boundary walls -- Top and bottom */
698 for (x = 0; x < cur_wid; x++)
700 place_extra_bold(0, x);
701 place_extra_bold(cur_hgt - 1, x);
704 /* Special boundary walls -- Left and right */
705 for (y = 1; y < (cur_hgt - 1); y++)
707 place_extra_bold(y, 0);
708 place_extra_bold(y, cur_wid - 1);
713 /* Start with walls */
714 for (y = 0; y < cur_hgt; y++)
716 for (x = 0; x < cur_wid; x++)
718 place_extra_bold(y, x);
724 /* Generate various caverns and lakes */
725 gen_caverns_and_lakes();
729 if (d_info[dungeon_type].flags1 & DF1_MAZE)
731 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
733 /* Place 3 or 4 down stairs near some walls */
734 if (!alloc_stairs(FEAT_MORE, rand_range(2, 3), 3)) return FALSE;
736 /* Place 1 or 2 up stairs near some walls */
737 if (!alloc_stairs(FEAT_LESS, 1, 3)) return FALSE;
740 /* Build some rooms */
744 * Build each type of room in turn until we cannot build any more.
746 if (!generate_rooms()) return FALSE;
749 /* Make a hole in the dungeon roof sometimes at level 1 */
752 while (one_in_(DUN_MOS_DEN))
754 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
758 /* Destroy the level if necessary */
759 if (dun->destroyed) destroy_level();
761 /* Hack -- Add some rivers */
762 if (one_in_(3) && (randint1(dun_level) > 5))
764 int feat1 = 0, feat2 = 0;
766 /* Choose water or lava */
767 if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
769 feat1 = FEAT_DEEP_WATER;
770 feat2 = FEAT_SHAL_WATER;
772 else if (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
774 feat1 = FEAT_DEEP_LAVA;
775 feat2 = FEAT_SHAL_LAVA;
781 feature_type *f_ptr = &f_info[feat1];
783 /* Only add river if matches lake type or if have no lake at all */
784 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
785 ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
788 add_river(feat1, feat2);
793 /* Hack -- Scramble the room order */
794 for (i = 0; i < dun->cent_n; i++)
797 int pick = rand_range(0, i);
801 dun->cent[i].y = dun->cent[pick].y;
802 dun->cent[i].x = dun->cent[pick].x;
803 dun->cent[pick].y = ty;
804 dun->cent[pick].x = tx;
807 /* Start with no tunnel doors */
810 /* Hack -- connect the first room to the last room */
811 y = dun->cent[dun->cent_n-1].y;
812 x = dun->cent[dun->cent_n-1].x;
814 /* Connect all the rooms together */
815 for (i = 0; i < dun->cent_n; i++)
819 /* Reset the arrays */
823 /* Connect the room to the previous room */
824 if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
826 /* make cave-like tunnel */
827 build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
831 /* make normal tunnel */
832 build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x);
835 /* Turn the tunnel into corridor */
836 for (j = 0; j < dun->tunn_n; j++)
841 /* Access the grid */
845 /* Access the grid */
847 f_ptr = &f_info[c_ptr->feat];
849 /* Clear previous contents (if not a lake), add a floor */
850 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
852 /* Clear mimic type */
855 place_floor_grid(c_ptr);
859 /* Apply the piercings that we found */
860 for (j = 0; j < dun->wall_n; j++)
864 /* Access the grid */
868 /* Access the grid */
871 /* Clear mimic type */
874 /* Clear previous contents, add up floor */
875 place_floor_grid(c_ptr);
877 /* Occasional doorway */
878 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
880 /* Place a random door */
881 place_random_door(y, x, TRUE);
885 /* Remember the "previous" room */
890 /* Place intersection doors */
891 for (i = 0; i < dun->door_n; i++)
893 /* Extract junction location */
897 /* Try placing doors */
904 /* Place 3 or 4 down stairs near some walls */
905 if (!alloc_stairs(FEAT_MORE, rand_range(3, 4), 3)) return FALSE;
907 /* Place 1 or 2 up stairs near some walls */
908 if (!alloc_stairs(FEAT_LESS, rand_range(1, 2), 3)) return FALSE;
913 if (d_info[dungeon_type].stream2)
915 /* Hack -- Add some quartz streamers */
916 for (i = 0; i < DUN_STR_QUA; i++)
918 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
922 if (d_info[dungeon_type].stream1)
924 /* Hack -- Add some magma streamers */
925 for (i = 0; i < DUN_STR_MAG; i++)
927 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
932 /* Special boundary walls -- Top and bottom */
933 for (x = 0; x < cur_wid; x++)
935 set_bound_perm_wall(&cave[0][x]);
936 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
939 /* Special boundary walls -- Left and right */
940 for (y = 1; y < (cur_hgt - 1); y++)
942 set_bound_perm_wall(&cave[y][0]);
943 set_bound_perm_wall(&cave[y][cur_wid - 1]);
946 /* Determine the character location */
947 if (!new_player_spot()) return FALSE;
949 place_quest_monsters();
956 /* Pick a base number of monsters */
957 i = d_info[dungeon_type].min_m_alloc_level;
959 /* To make small levels a bit more playable */
960 if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
962 int small_tester = i;
964 i = (i * cur_hgt) / MAX_HGT;
965 i = (i * cur_wid) / MAX_WID;
968 if (i > small_tester) i = small_tester;
972 msg_format("¥â¥ó¥¹¥¿¡¼¿ô´ðËÜÃͤò %d ¤«¤é %d ¤Ë¸º¤é¤·¤Þ¤¹", small_tester, i);
974 msg_format("Reduced monsters base from %d to %d", small_tester, i);
982 /* Put some monsters in the dungeon */
983 for (i = i + k; i > 0; i--)
985 (void)alloc_monster(0, PM_ALLOW_SLEEP);
988 /* Place some traps in the dungeon */
989 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
991 /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
992 if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
994 /* Mega Hack -- No object at first level of deeper dungeon */
995 if (p_ptr->enter_dungeon && dun_level > 1)
1001 /* Put some objects in rooms */
1002 alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1004 /* Put some objects/gold in the dungeon */
1005 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1006 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1008 /* Set back to default */
1009 object_level = base_level;
1011 /* Put the Guardian */
1012 (void)alloc_guardian();
1014 if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1017 for (y = 0; y < cur_hgt; y++)
1019 for (x = 0; x < cur_wid; x++)
1021 cave[y][x].info |= (CAVE_GLOW);
1031 * Builds the arena after it is entered -KMW-
1033 static void build_arena(void)
1035 int yval, y_height, y_depth, xval, x_left, x_right;
1038 yval = SCREEN_HGT / 2;
1039 xval = SCREEN_WID / 2;
1040 y_height = yval - 10;
1041 y_depth = yval + 10;
1043 x_right = xval + 32;
1045 for (i = y_height; i <= y_height + 5; i++)
1046 for (j = x_left; j <= x_right; j++)
1048 place_extra_perm_bold(i, j);
1049 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1051 for (i = y_depth; i >= y_depth - 5; i--)
1052 for (j = x_left; j <= x_right; j++)
1054 place_extra_perm_bold(i, j);
1055 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1057 for (j = x_left; j <= x_left + 17; j++)
1058 for (i = y_height; i <= y_depth; i++)
1060 place_extra_perm_bold(i, j);
1061 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1063 for (j = x_right; j >= x_right - 17; j--)
1064 for (i = y_height; i <= y_depth; i++)
1066 place_extra_perm_bold(i, j);
1067 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1070 place_extra_perm_bold(y_height+6, x_left+18);
1071 cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1072 place_extra_perm_bold(y_depth-6, x_left+18);
1073 cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1074 place_extra_perm_bold(y_height+6, x_right-18);
1075 cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1076 place_extra_perm_bold(y_depth-6, x_right-18);
1077 cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1081 cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1082 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1088 * Town logic flow for generation of arena -KMW-
1090 static void arena_gen(void)
1097 cur_hgt = SCREEN_HGT;
1098 cur_wid = SCREEN_WID;
1100 /* Start with solid walls */
1101 for (y = 0; y < MAX_HGT; y++)
1103 for (x = 0; x < MAX_WID; x++)
1105 /* Create "solid" perma-wall */
1106 place_solid_perm_bold(y, x);
1108 /* Illuminate and memorize the walls */
1109 cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1113 /* Then place some floors */
1114 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1116 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1118 /* Create empty floor */
1119 cave[y][x].feat = FEAT_FLOOR;
1125 place_monster_aux(0, py + 5, px, arena_info[p_ptr->arena_number].r_idx,
1126 (PM_NO_KAGE | PM_NO_PET));
1132 * Builds the arena after it is entered -KMW-
1134 static void build_battle(void)
1136 int yval, y_height, y_depth, xval, x_left, x_right;
1139 yval = SCREEN_HGT / 2;
1140 xval = SCREEN_WID / 2;
1141 y_height = yval - 10;
1142 y_depth = yval + 10;
1144 x_right = xval + 32;
1146 for (i = y_height; i <= y_height + 5; i++)
1147 for (j = x_left; j <= x_right; j++)
1149 place_extra_perm_bold(i, j);
1150 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1152 for (i = y_depth; i >= y_depth - 3; i--)
1153 for (j = x_left; j <= x_right; j++)
1155 place_extra_perm_bold(i, j);
1156 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1158 for (j = x_left; j <= x_left + 17; j++)
1159 for (i = y_height; i <= y_depth; i++)
1161 place_extra_perm_bold(i, j);
1162 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1164 for (j = x_right; j >= x_right - 17; j--)
1165 for (i = y_height; i <= y_depth; i++)
1167 place_extra_perm_bold(i, j);
1168 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1171 place_extra_perm_bold(y_height+6, x_left+18);
1172 cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1173 place_extra_perm_bold(y_depth-4, x_left+18);
1174 cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1175 place_extra_perm_bold(y_height+6, x_right-18);
1176 cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1177 place_extra_perm_bold(y_depth-4, x_right-18);
1178 cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1182 cave[i][j].feat = FEAT_BLDG_HEAD + 3;
1183 cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1189 * Town logic flow for generation of arena -KMW-
1191 static void battle_gen(void)
1197 /* Start with solid walls */
1198 for (y = 0; y < MAX_HGT; y++)
1200 for (x = 0; x < MAX_WID; x++)
1202 /* Create "solid" perma-wall */
1203 place_solid_perm_bold(y, x);
1205 /* Illuminate and memorize the walls */
1206 cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1210 /* Then place some floors */
1211 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1213 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1215 /* Create empty floor */
1216 cave[y][x].feat = FEAT_FLOOR;
1224 place_monster_aux(0, py + 5 + (i/2)*4, px - 2 + (i%2)*4, battle_mon[i],
1225 (PM_NO_KAGE | PM_NO_PET));
1226 set_friendly(&m_list[cave[py+5+(i/2)*4][px-2+(i%2)*4].m_idx]);
1228 for(i = 1; i < m_max; i++)
1230 monster_type *m_ptr = &m_list[i];
1232 if (!m_ptr->r_idx) continue;
1234 /* Hack -- Detect monster */
1235 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1237 /* Update the monster */
1238 update_mon(i, FALSE);
1244 * Generate a quest level
1246 static void quest_gen(void)
1251 /* Start with perm walls */
1252 for (y = 0; y < cur_hgt; y++)
1254 for (x = 0; x < cur_wid; x++)
1256 place_solid_perm_bold(y, x);
1260 /* Set the quest level */
1261 base_level = quest[p_ptr->inside_quest].level;
1262 dun_level = base_level;
1263 object_level = base_level;
1264 monster_level = base_level;
1266 if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1268 /* Prepare allocation table */
1269 get_mon_num_prep(get_monster_hook(), NULL);
1271 init_flags = INIT_CREATE_DUNGEON | INIT_ASSIGN;
1273 process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1276 /* Make a real level */
1277 static bool level_gen(cptr *why)
1279 int level_height, level_width;
1281 if ((always_small_levels || ironman_small_levels ||
1282 (one_in_(SMALL_LEVEL) && small_levels) ||
1283 (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1284 (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1285 !(d_info[dungeon_type].flags1 & DF1_BIG))
1289 msg_print("¾®¤µ¤Ê¥Õ¥í¥¢");
1291 msg_print("A 'small' dungeon level.");
1294 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1299 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1308 level_height = randint1(MAX_HGT/SCREEN_HGT);
1309 level_width = randint1(MAX_WID/SCREEN_WID);
1311 while ((level_height == MAX_HGT/SCREEN_HGT) &&
1312 (level_width == MAX_WID/SCREEN_WID));
1315 cur_hgt = level_height * SCREEN_HGT;
1316 cur_wid = level_width * SCREEN_WID;
1318 /* Assume illegal panel */
1319 panel_row_min = cur_hgt;
1320 panel_col_min = cur_wid;
1323 msg_format("X:%d, Y:%d.", cur_hgt, cur_wid);
1331 /* Assume illegal panel */
1332 panel_row_min = cur_hgt;
1333 panel_col_min = cur_wid;
1336 /* Make a dungeon */
1340 *why = "¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ";
1342 *why = "could not place player";
1350 static byte extract_feeling(void)
1352 /* Hack -- no feeling in the town */
1353 if (!dun_level) return 0;
1355 /* Hack -- Have a special feeling sometimes */
1356 if (good_item_flag && !preserve_mode) return 1;
1358 if (rating > 100) return 2;
1359 if (rating > 80) return 3;
1360 if (rating > 60) return 4;
1361 if (rating > 40) return 5;
1362 if (rating > 30) return 6;
1363 if (rating > 20) return 7;
1364 if (rating > 10) return 8;
1365 if (rating > 0) return 9;
1367 if((turn - old_turn) > TURNS_PER_TICK * TOWN_DAWN /2)
1368 chg_virtue(V_PATIENCE, 1);
1375 * Wipe all unnecessary flags after cave generation
1377 static void wipe_generate_cave_flags(void)
1381 for (y = 0; y < cur_hgt; y++)
1383 for (x = 0; x < cur_wid; x++)
1385 /* Wipe unused flags */
1386 cave[y][x].info &= ~(CAVE_MASK);
1392 for (y = 1; y < cur_hgt - 1; y++)
1394 for (x = 1; x < cur_wid - 1; x++)
1396 /* There might be trap */
1397 cave[y][x].info |= CAVE_UNSAFE;
1405 * Clear and empty the cave
1407 void clear_cave(void)
1411 /* Very simplified version of wipe_o_list() */
1412 C_WIPE(o_list, o_max, object_type);
1416 /* Very simplified version of wipe_m_list() */
1417 for (i = 1; i < max_r_idx; i++)
1418 r_info[i].cur_num = 0;
1419 C_WIPE(m_list, m_max, monster_type);
1423 /* Pre-calc cur_num of pets in party_mon[] */
1424 precalc_cur_num_of_pet();
1427 /* Start with a blank cave */
1428 for (y = 0; y < MAX_HGT; y++)
1430 for (x = 0; x < MAX_WID; x++)
1432 cave_type *c_ptr = &cave[y][x];
1459 /* Mega-Hack -- no player yet */
1462 /* Set the base level */
1463 base_level = dun_level;
1465 /* Reset the monster generation level */
1466 monster_level = base_level;
1468 /* Reset the object generation level */
1469 object_level = base_level;
1471 /* Nothing special here yet */
1472 good_item_flag = FALSE;
1474 /* Nothing good here yet */
1480 * Generates a random dungeon level -RAK-
1482 * Hack -- regenerate any "overflow" levels
1484 void generate_cave(void)
1488 /* Fill the arrays of floors and walls in the good proportions */
1489 set_floor_and_wall(dungeon_type);
1492 for (num = 0; TRUE; num++)
1498 /* Clear and empty the cave */
1501 for (i = 0; i < DUNGEON_FEAT_PROB_NUM; i++)
1503 if (have_flag(f_info[d_info[dungeon_type].fill[i].feat].flags, FF_HAS_GOLD))
1510 /* Build the arena -KMW- */
1511 if (p_ptr->inside_arena)
1517 /* Build the battle -KMW- */
1518 else if (p_ptr->inside_battle)
1524 else if (p_ptr->inside_quest)
1529 /* Build the town */
1530 else if (!dun_level)
1532 /* Make the wilderness */
1533 if (p_ptr->wild_mode) wilderness_gen_small();
1534 else wilderness_gen();
1537 /* Build a real level */
1540 okay = level_gen(&why);
1543 /* Extract the feeling */
1544 feeling = extract_feeling();
1546 /* Prevent object over-flow */
1547 if (o_max >= max_o_idx)
1551 why = "¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë";
1553 why = "too many objects";
1560 /* Prevent monster over-flow */
1561 else if (m_max >= max_m_idx)
1565 why = "¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë";
1567 why = "too many monsters";
1580 if (why) msg_format("À¸À®¤ä¤êľ¤·(%s)", why);
1582 if (why) msg_format("Generation restarted (%s)", why);
1586 /* Wipe the objects */
1589 /* Wipe the monsters */
1593 /* Glow deep lava and building entrances */
1594 glow_deep_lava_and_bldg();
1597 p_ptr->enter_dungeon = FALSE;
1599 wipe_generate_cave_flags();