3 * @brief ダンジョンの生成 / Dungeon generation
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7 * This software may be copied and distributed for educational, research,\n
8 * and not for profit purposes provided that this copyright and statement\n
9 * are included in all such copies. Other copyrights may also apply.\n
10 * 2014 Deskull rearranged comment for Doxygen. \n
12 * Note that Level generation is *not* an important bottleneck,\n
13 * though it can be annoyingly slow on older machines... Thus\n
14 * we emphasize "simplicity" and "correctness" over "speed".\n
16 * This entire file is only needed for generating levels.\n
17 * This may allow smart compilers to only load it when needed.\n
19 * Consider the "v_info.txt" file for vault generation.\n
21 * In this file, we use the "special" granite and perma-wall sub-types,\n
22 * where "basic" is normal, "inner" is inside a room, "outer" is the\n
23 * outer wall of a room, and "solid" is the outer wall of the dungeon\n
24 * or any walls that may not be pierced by corridors. Thus the only\n
25 * wall type that may be pierced by a corridor is the "outer granite"\n
26 * type. The "basic granite" type yields the "actual" corridors.\n
28 * Note that we use the special "solid" granite wall type to prevent\n
29 * multiple corridors from piercing a wall in two adjacent locations,\n
30 * which would be messy, and we use the special "outer" granite wall\n
31 * to indicate which walls "surround" rooms, and may thus be "pierced"\n
32 * by corridors entering or leaving the room.\n
34 * Note that a tunnel which attempts to leave a room near the "edge"\n
35 * of the dungeon in a direction toward that edge will cause "silly"\n
36 * wall piercings, but will have no permanently incorrect effects,\n
37 * as long as the tunnel can *eventually* exit from another side.\n
38 * And note that the wall may not come back into the room by the\n
39 * hole it left through, so it must bend to the left or right and\n
40 * then optionally re-enter the room (at least 2 grids away). This\n
41 * is not a problem since every room that is large enough to block\n
42 * the passage of tunnels is also large enough to allow the tunnel\n
43 * to pierce the room itself several times.\n
45 * Note that no two corridors may enter a room through adjacent grids,\n
46 * they must either share an entryway or else use entryways at least\n
47 * two grids apart. This prevents "large" (or "silly") doorways.\n
49 * To create rooms in the dungeon, we first divide the dungeon up\n
50 * into "blocks" of 11x11 grids each, and require that all rooms\n
51 * occupy a rectangular group of blocks. As long as each room type\n
52 * reserves a sufficient number of blocks, the room building routines\n
53 * will not need to check bounds. Note that most of the normal rooms\n
54 * actually only use 23x11 grids, and so reserve 33x11 grids.\n
56 * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)\n
57 * allows more variability in the horizontal placement of rooms, and\n
58 * at the same time has the disadvantage that some rooms (two thirds\n
59 * of the normal rooms) may be "split" by panel boundaries. This can\n
60 * induce a situation where a player is in a room and part of the room\n
61 * is off the screen. It may be annoying enough to go back to 33x11\n
62 * blocks to prevent this visual situation.\n
64 * Note that the dungeon generation routines are much different (2.7.5)\n
65 * and perhaps "DUN_ROOMS" should be less than 50.\n
67 * Note that it is possible to create a room which is only\n
68 * connected to itself, because the "tunnel generation" code allows a\n
69 * tunnel to leave a room, wander around, and then re-enter the room.\n
71 * Note that it is possible to create a set of rooms which\n
72 * are only connected to other rooms in that set, since there is nothing\n
73 * explicit in the code to prevent this from happening. But this is less\n
74 * likely than the "isolated room" problem, because each room attempts to\n
75 * connect to another room, in a giant cycle, thus requiring at least two\n
76 * bizarre occurances to create an isolated section of the dungeon.\n
78 * Note that (2.7.9) monster pits have been split into monster "nests"\n
79 * and monster "pits". The "nests" have a collection of monsters of a\n
80 * given type strewn randomly around the room (jelly, animal, or undead),\n
81 * while the "pits" have a collection of monsters of a given type placed\n
82 * around the room in an organized manner (orc, troll, giant, dragon, or\n
83 * demon). Note that both "nests" and "pits" are now "level dependant",\n
84 * and both make 16 "expensive" calls to the "get_mon_num()" function.\n
86 * Note that the grid flags changed in a rather drastic manner\n
87 * for Angband 2.8.0 (and 2.7.9+), in particular, dungeon terrain\n
88 * features, such as doors and stairs and traps and rubble and walls,\n
89 * are all handled as a set of 64 possible "terrain features", and\n
90 * not as "fake" objects (440-479) as in pre-2.8.0 versions.\n
92 * The 64 new "dungeon features" will also be used for "visual display"\n
93 * but we must be careful not to allow, for example, the user to display\n
94 * hidden traps in a different way from floors, or secret doors in a way\n
95 * different from granite walls, or even permanent granite in a different\n
96 * way from granite. \n
104 #include "cmd-dump.h"
109 #include "floor-save.h"
110 #include "floor-streams.h"
111 #include "floor-generate.h"
112 #include "floor-events.h"
113 #include "floor-generate.h"
118 #include "player-status.h"
120 #include "monster-status.h"
121 #include "dungeon-file.h"
127 #include "view-mainwindow.h"
137 * Dungeon generation data -- see "cave_gen()"
143 * @brief 上下左右の外壁数をカウントする / Count the number of walls adjacent to the given grid.
147 * @note Assumes "in_bounds()"
148 * @details We count only granite walls and permanent walls.
150 static int next_to_walls(floor_type* floor_ptr, POSITION y, POSITION x)
154 if (in_bounds(floor_ptr, y + 1, x) && is_extra_bold(floor_ptr, y + 1, x)) k++;
155 if (in_bounds(floor_ptr, y - 1, x) && is_extra_bold(floor_ptr, y - 1, x)) k++;
156 if (in_bounds(floor_ptr, y, x + 1) && is_extra_bold(floor_ptr, y, x + 1)) k++;
157 if (in_bounds(floor_ptr, y, x - 1) && is_extra_bold(floor_ptr, y, x - 1)) k++;
163 * @brief alloc_stairs()の補助として指定の位置に階段を生成できるかの判定を行う / Helper function for alloc_stairs(). Is this a good location for stairs?
166 * @param walls 最低減隣接させたい外壁の数
167 * @return 階段を生成して問題がないならばTRUEを返す。
169 static bool alloc_stairs_aux(floor_type *floor_ptr, POSITION y, POSITION x, int walls)
171 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
173 /* Require "naked" floor grid */
174 if (!is_floor_grid(g_ptr)) return FALSE;
175 if (pattern_tile(y, x)) return FALSE;
176 if (g_ptr->o_idx || g_ptr->m_idx) return FALSE;
178 /* Require a certain number of adjacent walls */
179 if (next_to_walls(floor_ptr, y, x) < walls) return FALSE;
186 * @brief 外壁に隣接させて階段を生成する / Places some staircases near walls
187 * @param owner_ptr プレーヤーへの参照ポインタ
188 * @param feat 配置したい地形ID
189 * @param num 配置したい階段の数
190 * @param walls 最低減隣接させたい外壁の数
191 * @return 規定数通りに生成に成功したらTRUEを返す。
193 static bool alloc_stairs(player_type *owner_ptr, FEAT_IDX feat, int num, int walls)
198 feature_type *f_ptr = &f_info[feat];
200 floor_type *floor_ptr = owner_ptr->current_floor_ptr;
201 if (have_flag(f_ptr->flags, FF_LESS))
203 /* No up stairs in town or in ironman mode */
204 if (ironman_downward || !floor_ptr->dun_level) return TRUE;
206 if (floor_ptr->dun_level > d_info[floor_ptr->dungeon_idx].mindepth)
207 shaft_num = (randint1(num+1))/2;
209 else if (have_flag(f_ptr->flags, FF_MORE))
211 QUEST_IDX q_idx = quest_number(owner_ptr, floor_ptr->dun_level);
213 /* No downstairs on quest levels */
214 if (floor_ptr->dun_level > 1 && q_idx)
216 monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
218 /* The quest monster(s) is still alive? */
219 if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
223 /* No downstairs at the bottom */
224 if (floor_ptr->dun_level >= d_info[floor_ptr->dungeon_idx].maxdepth) return TRUE;
226 if ((floor_ptr->dun_level < d_info[floor_ptr->dungeon_idx].maxdepth-1) && !quest_number(owner_ptr, floor_ptr->dun_level+1))
227 shaft_num = (randint1(num)+1)/2;
232 /* Place "num" stairs */
233 for (i = 0; i < num; i++)
243 for (y = 1; y < floor_ptr->height - 1; y++)
245 for (x = 1; x < floor_ptr->width - 1; x++)
247 if (alloc_stairs_aux(floor_ptr, y, x, walls))
249 /* A valid space found */
255 /* No valid place! */
258 /* There are exactly no place! */
259 if (walls <= 0) return FALSE;
261 /* Decrease walls limit, and try again */
266 /* Choose a random one */
267 pick = randint1(candidates);
269 for (y = 1; y < floor_ptr->height - 1; y++)
271 for (x = 1; x < floor_ptr->width - 1; x++)
273 if (alloc_stairs_aux(floor_ptr, y, x, walls))
277 /* Is this a picked one? */
284 g_ptr = &floor_ptr->grid_array[y][x];
286 /* Clear possible garbage of hidden trap */
289 /* Clear previous contents, add stairs */
290 g_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
292 /* No longer "FLOOR" */
293 g_ptr->info &= ~(CAVE_FLOOR);
303 * @brief フロア上のランダム位置に各種オブジェクトを配置する / Allocates some objects (using "place" and "type")
304 * @param set 配置したい地形の種類
305 * @param typ 配置したいオブジェクトの種類
307 * @return 規定数通りに生成に成功したらTRUEを返す。
309 static void alloc_object(player_type *owner_ptr, int set, EFFECT_ID typ, int num)
311 POSITION y = 0, x = 0;
316 /* A small level has few objects. */
317 floor_type *floor_ptr = owner_ptr->current_floor_ptr;
318 num = num * floor_ptr->height * floor_ptr->width / (MAX_HGT*MAX_WID) +1;
320 /* Place some objects */
321 for (k = 0; k < num; k++)
323 /* Pick a "legal" spot */
324 while (dummy < SAFE_MAX_ATTEMPTS)
330 y = randint0(floor_ptr->height);
331 x = randint0(floor_ptr->width);
333 g_ptr = &floor_ptr->grid_array[y][x];
335 /* Require "naked" floor grid */
336 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
338 /* Avoid player location */
339 if (player_bold(p_ptr, y, x)) continue;
341 /* Check for "room" */
342 room = (floor_ptr->grid_array[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
344 /* Require corridor? */
345 if ((set == ALLOC_SET_CORR) && room) continue;
348 if ((set == ALLOC_SET_ROOM) && !room) continue;
354 if (dummy >= SAFE_MAX_ATTEMPTS)
356 msg_print_wizard(CHEAT_DUNGEON, _("アイテムの配置に失敗しました。", "Failed to place object."));
361 /* Place something */
364 case ALLOC_TYP_RUBBLE:
366 place_rubble(floor_ptr, y, x);
367 floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
373 place_trap(owner_ptr, y, x);
374 floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
380 place_gold(floor_ptr, y, x);
384 case ALLOC_TYP_OBJECT:
386 place_object(owner_ptr, y, x, 0L);
397 * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
398 * @param floor_ptr 配置するフロアの参照ポインタ
399 * @param subject_ptr 近隣への即出現を避けるためのプレイヤークリーチャー参照ポインタ
400 * @return 成功したならばTRUEを返す
402 bool place_quest_monsters(floor_type *floor_ptr, player_type *creature_ptr)
406 /* Handle the quest monster placements */
407 for (i = 0; i < max_q_idx; i++)
413 if (quest[i].status != QUEST_STATUS_TAKEN ||
414 (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
415 quest[i].type != QUEST_TYPE_RANDOM) ||
416 quest[i].level != floor_ptr->dun_level ||
417 creature_ptr->dungeon_idx != quest[i].dungeon ||
418 (quest[i].flags & QUEST_FLAG_PRESET))
424 r_ptr = &r_info[quest[i].r_idx];
426 /* Hack -- "unique" monsters must be "unique" */
427 if ((r_ptr->flags1 & RF1_UNIQUE) &&
428 (r_ptr->cur_num >= r_ptr->max_num)) continue;
430 mode = (PM_NO_KAGE | PM_NO_PET);
432 if (!(r_ptr->flags1 & RF1_FRIENDS))
433 mode |= PM_ALLOW_GROUP;
435 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
439 for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
441 POSITION x = 0, y = 0;
444 /* Find an empty grid */
445 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
450 y = randint0(floor_ptr->height);
451 x = randint0(floor_ptr->width);
453 g_ptr = &floor_ptr->grid_array[y][x];
454 f_ptr = &f_info[g_ptr->feat];
456 if (!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) continue;
457 if (!monster_can_enter(y, x, r_ptr, 0)) continue;
458 if (distance(y, x, creature_ptr->y, creature_ptr->x) < 10) continue;
459 if (g_ptr->info & CAVE_ICKY) continue;
463 /* Failed to place */
464 if (!l) return FALSE;
466 /* Try to place the monster */
467 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
474 /* Failure - Try again */
479 /* Failed to place */
480 if (k == SAFE_MAX_ATTEMPTS) return FALSE;
488 * @brief フロアに洞窟や湖を配置する / Generate various caverns and lakes
489 * @details There were moved from cave_gen().
492 static void gen_caverns_and_lakes(dungeon_type *dungeon_ptr, player_type *owner_ptr)
494 floor_type *floor_ptr = owner_ptr->current_floor_ptr;
495 #ifdef ALLOW_CAVERNS_AND_LAKES
496 /* Possible "destroyed" level */
497 if ((floor_ptr->dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (dungeon_ptr->flags1 & DF1_DESTROY))
499 dun->destroyed = TRUE;
501 /* extra rubble around the place looks cool */
502 build_lake(floor_ptr, one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
505 /* Make a lake some of the time */
506 if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
507 (dungeon_ptr->flags1 & DF1_LAKE_MASK))
510 if (dungeon_ptr->flags1 & DF1_LAKE_WATER) count += 3;
511 if (dungeon_ptr->flags1 & DF1_LAKE_LAVA) count += 3;
512 if (dungeon_ptr->flags1 & DF1_LAKE_RUBBLE) count += 3;
513 if (dungeon_ptr->flags1 & DF1_LAKE_TREE) count += 3;
515 if (dungeon_ptr->flags1 & DF1_LAKE_LAVA)
518 if ((floor_ptr->dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
522 if (!dun->laketype && (floor_ptr->dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
526 if ((dungeon_ptr->flags1 & DF1_LAKE_WATER) && !dun->laketype)
529 if ((floor_ptr->dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
533 if (!dun->laketype && (floor_ptr->dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
537 if ((dungeon_ptr->flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
540 if ((floor_ptr->dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
543 /* Lake of rubble2 */
544 if (!dun->laketype && (floor_ptr->dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
549 if ((floor_ptr->dun_level > 5) && (dungeon_ptr->flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
553 msg_print_wizard(CHEAT_DUNGEON, _("湖を生成します。", "Lake on the level."));
554 build_lake(floor_ptr, dun->laketype);
558 if ((floor_ptr->dun_level > DUN_CAVERN) && !dun->empty_level &&
559 (dungeon_ptr->flags1 & DF1_CAVERN) &&
560 !dun->laketype && !dun->destroyed && (randint1(1000) < floor_ptr->dun_level))
564 /* make a large fractal floor_ptr->grid_array in the middle of the dungeon */
566 msg_print_wizard(CHEAT_DUNGEON, _("洞窟を生成。", "Cavern on level."));
567 build_cavern(floor_ptr);
569 #endif /* ALLOW_CAVERNS_AND_LAKES */
571 /* Hack -- No destroyed "quest" levels */
572 if (quest_number(owner_ptr, floor_ptr->dun_level)) dun->destroyed = FALSE;
577 * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
578 * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
579 * @param player_ptr プレーヤーへの参照ポインタ
580 * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
582 static bool cave_gen(player_type *player_ptr)
588 floor_type *floor_ptr = player_ptr->current_floor_ptr;
589 dungeon_type* dungeon_ptr = &d_info[floor_ptr->dungeon_idx];
591 floor_ptr->lite_n = 0;
592 floor_ptr->mon_lite_n = 0;
593 floor_ptr->redraw_n = 0;
594 floor_ptr->view_n = 0;
599 dun->destroyed = FALSE;
600 dun->empty_level = FALSE;
604 /* Fill the arrays of floors and walls in the good proportions */
605 set_floor_and_wall(floor_ptr->dungeon_idx);
606 get_mon_num_prep(get_monster_hook(), NULL);
608 /* Randomize the dungeon creation values */
609 dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
610 dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
611 dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
612 dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
613 dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
615 /* Actual maximum number of rooms on this level */
616 dun->row_rooms = floor_ptr->height / BLOCK_HGT;
617 dun->col_rooms = floor_ptr->width / BLOCK_WID;
619 /* Initialize the room table */
620 for (y = 0; y < dun->row_rooms; y++)
622 for (x = 0; x < dun->col_rooms; x++)
624 dun->room_map[y][x] = FALSE;
631 /* Empty arena levels */
632 if (ironman_empty_levels || ((dungeon_ptr->flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
634 dun->empty_level = TRUE;
635 msg_print_wizard(CHEAT_DUNGEON, _("アリーナレベルを生成。", "Arena level."));
638 if (dun->empty_level)
640 /* Start with floors */
641 for (y = 0; y < floor_ptr->height; y++)
643 for (x = 0; x < floor_ptr->width; x++)
645 place_floor_bold(floor_ptr, y, x);
649 /* Special boundary walls -- Top and bottom */
650 for (x = 0; x < floor_ptr->width; x++)
652 place_extra_bold(floor_ptr, 0, x);
653 place_extra_bold(floor_ptr, floor_ptr->height - 1, x);
656 /* Special boundary walls -- Left and right */
657 for (y = 1; y < (floor_ptr->height - 1); y++)
659 place_extra_bold(floor_ptr, y, 0);
660 place_extra_bold(floor_ptr, y, floor_ptr->width - 1);
665 /* Start with walls */
666 for (y = 0; y < floor_ptr->height; y++)
668 for (x = 0; x < floor_ptr->width; x++)
670 place_extra_bold(floor_ptr, y, x);
675 /* Generate various caverns and lakes */
676 gen_caverns_and_lakes(dungeon_ptr, player_ptr);
679 if (dungeon_ptr->flags1 & DF1_MAZE)
681 build_maze_vault(player_ptr, floor_ptr->width/2-1, floor_ptr->height/2-1, floor_ptr->width-4, floor_ptr->height-4, FALSE);
683 /* Place 3 or 4 down stairs near some walls */
684 if (!alloc_stairs(player_ptr, feat_down_stair, rand_range(2, 3), 3)) return FALSE;
686 /* Place 1 or 2 up stairs near some walls */
687 if (!alloc_stairs(player_ptr, feat_up_stair, 1, 3)) return FALSE;
690 /* Build some rooms */
693 int tunnel_fail_count = 0;
696 * Build each type of room in turn until we cannot build any more.
698 if (!generate_rooms(player_ptr)) return FALSE;
701 /* Make a hole in the dungeon roof sometimes at level 1 */
702 if (floor_ptr->dun_level == 1)
704 while (one_in_(DUN_MOS_DEN))
706 place_trees(floor_ptr, randint1(floor_ptr->width - 2), randint1(floor_ptr->height - 2));
710 /* Destroy the level if necessary */
711 if (dun->destroyed) destroy_level(player_ptr);
713 /* Hack -- Add some rivers */
714 if (one_in_(3) && (randint1(floor_ptr->dun_level) > 5))
716 FEAT_IDX feat1 = 0, feat2 = 0;
718 /* Choose water mainly */
719 if ((randint1(MAX_DEPTH * 2) - 1 > floor_ptr->dun_level) && (dungeon_ptr->flags1 & DF1_WATER_RIVER))
721 feat1 = feat_deep_water;
722 feat2 = feat_shallow_water;
726 FEAT_IDX select_deep_feat[10];
727 FEAT_IDX select_shallow_feat[10];
728 int select_id_max = 0, selected;
730 if (dungeon_ptr->flags1 & DF1_LAVA_RIVER)
732 select_deep_feat[select_id_max] = feat_deep_lava;
733 select_shallow_feat[select_id_max] = feat_shallow_lava;
736 if (dungeon_ptr->flags1 & DF1_POISONOUS_RIVER)
738 select_deep_feat[select_id_max] = feat_deep_poisonous_puddle;
739 select_shallow_feat[select_id_max] = feat_shallow_poisonous_puddle;
742 if (dungeon_ptr->flags1 & DF1_ACID_RIVER)
744 select_deep_feat[select_id_max] = feat_deep_acid_puddle;
745 select_shallow_feat[select_id_max] = feat_shallow_acid_puddle;
749 if (select_id_max > 0)
751 selected = randint0(select_id_max);
752 feat1 = select_deep_feat[selected];
753 feat2 = select_shallow_feat[selected];
757 feat1 = feat_deep_water;
758 feat2 = feat_shallow_water;
764 feature_type *f_ptr = &f_info[feat1];
766 /* Only add river if matches lake type or if have no lake at all */
767 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
768 ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
771 add_river(floor_ptr, feat1, feat2);
776 /* Hack -- Scramble the room order */
777 for (i = 0; i < dun->cent_n; i++)
780 int pick = rand_range(0, i);
784 dun->cent[i].y = dun->cent[pick].y;
785 dun->cent[i].x = dun->cent[pick].x;
786 dun->cent[pick].y = ty;
787 dun->cent[pick].x = tx;
790 /* Start with no tunnel doors */
793 /* Hack -- connect the first room to the last room */
794 y = dun->cent[dun->cent_n-1].y;
795 x = dun->cent[dun->cent_n-1].x;
797 /* Connect all the rooms together */
798 for (i = 0; i < dun->cent_n; i++)
802 /* Reset the arrays */
806 /* Connect the room to the previous room */
807 if (randint1(floor_ptr->dun_level) > dungeon_ptr->tunnel_percent)
809 /* make cavelike tunnel */
810 (void)build_tunnel2(floor_ptr, dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
814 /* make normal tunnel */
815 if (!build_tunnel(floor_ptr, dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
818 if (tunnel_fail_count >= 2) return FALSE;
820 /* Turn the tunnel into corridor */
821 for (j = 0; j < dun->tunn_n; j++)
827 g_ptr = &floor_ptr->grid_array[y][x];
828 f_ptr = &f_info[g_ptr->feat];
830 /* Clear previous contents (if not a lake), add a floor */
831 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
833 /* Clear mimic type */
836 place_floor_grid(g_ptr);
840 /* Apply the piercings that we found */
841 for (j = 0; j < dun->wall_n; j++)
846 g_ptr = &floor_ptr->grid_array[y][x];
848 /* Clear mimic type */
851 /* Clear previous contents, add up floor */
852 place_floor_grid(g_ptr);
854 /* Occasional doorway */
855 if ((randint0(100) < dun_tun_pen) && !(dungeon_ptr->flags1 & DF1_NO_DOORS))
857 /* Place a random door */
858 place_random_door(floor_ptr, y, x, TRUE);
862 /* Remember the "previous" room */
867 /* Place intersection doors */
868 for (i = 0; i < dun->door_n; i++)
870 /* Extract junction location */
874 /* Try placing doors */
875 try_door(floor_ptr, y, x - 1);
876 try_door(floor_ptr, y, x + 1);
877 try_door(floor_ptr, y - 1, x);
878 try_door(floor_ptr, y + 1, x);
881 /* Place 3 or 4 down stairs near some walls */
882 if (!alloc_stairs(player_ptr, feat_down_stair, rand_range(3, 4), 3)) return FALSE;
884 /* Place 1 or 2 up stairs near some walls */
885 if (!alloc_stairs(player_ptr, feat_up_stair, rand_range(1, 2), 3)) return FALSE;
890 if (dungeon_ptr->stream2)
892 /* Hack -- Add some quartz streamers */
893 for (i = 0; i < DUN_STR_QUA; i++)
895 build_streamer(player_ptr, dungeon_ptr->stream2, DUN_STR_QC);
899 if (dungeon_ptr->stream1)
901 /* Hack -- Add some magma streamers */
902 for (i = 0; i < DUN_STR_MAG; i++)
904 build_streamer(player_ptr, dungeon_ptr->stream1, DUN_STR_MC);
909 /* Special boundary walls -- Top and bottom */
910 for (x = 0; x < floor_ptr->width; x++)
912 place_bound_perm_wall(&floor_ptr->grid_array[0][x]);
913 place_bound_perm_wall(&floor_ptr->grid_array[floor_ptr->height - 1][x]);
916 /* Special boundary walls -- Left and right */
917 for (y = 1; y < (floor_ptr->height - 1); y++)
919 place_bound_perm_wall(&floor_ptr->grid_array[y][0]);
920 place_bound_perm_wall(&floor_ptr->grid_array[y][floor_ptr->width - 1]);
923 /* Determine the character location */
924 if (!new_player_spot(p_ptr)) return FALSE;
926 if (!place_quest_monsters(floor_ptr, p_ptr)) return FALSE;
929 k = (floor_ptr->dun_level / 3);
933 /* Pick a base number of monsters */
934 i = dungeon_ptr->min_m_alloc_level;
936 /* To make small levels a bit more playable */
937 if (floor_ptr->height < MAX_HGT || floor_ptr->width < MAX_WID)
939 int small_tester = i;
941 i = (i * floor_ptr->height) / MAX_HGT;
942 i = (i * floor_ptr->width) / MAX_WID;
945 if (i > small_tester) i = small_tester;
946 else msg_format_wizard(CHEAT_DUNGEON,
947 _("モンスター数基本値を %d から %d に減らします", "Reduced monsters base from %d to %d"), small_tester, i);
953 /* Put some monsters in the dungeon */
954 for (i = i + k; i > 0; i--)
956 (void)alloc_monster(0, PM_ALLOW_SLEEP);
959 /* Place some traps in the dungeon */
960 alloc_object(player_ptr, ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
962 /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
963 if (!(dungeon_ptr->flags1 & DF1_NO_CAVE)) alloc_object(player_ptr, ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
965 /* Mega Hack -- No object at first level of deeper dungeon */
966 if (p_ptr->enter_dungeon && floor_ptr->dun_level > 1)
969 floor_ptr->object_level = 1;
972 /* Put some objects in rooms */
973 alloc_object(player_ptr, ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
975 /* Put some objects/gold in the dungeon */
976 alloc_object(player_ptr, ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
977 alloc_object(player_ptr, ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
979 /* Set back to default */
980 floor_ptr->object_level = floor_ptr->base_level;
982 /* Put the Guardian */
983 if (!alloc_guardian(TRUE)) return FALSE;
985 bool is_empty_or_dark = dun->empty_level;
986 is_empty_or_dark &= !one_in_(DARK_EMPTY) || (randint1(100) > floor_ptr->dun_level);
987 is_empty_or_dark &= !(dungeon_ptr->flags1 & DF1_DARKNESS);
988 if (!is_empty_or_dark) return TRUE;
990 /* Lite the floor_ptr->grid_array */
991 for (y = 0; y < floor_ptr->height; y++)
993 for (x = 0; x < floor_ptr->width; x++)
995 floor_ptr->grid_array[y][x].info |= (CAVE_GLOW);
1003 * @brief 闘技場用のアリーナ地形を作成する / Builds the arena after it is entered -KMW-
1006 static void build_arena(floor_type *floor_ptr, POSITION *start_y, POSITION *start_x)
1008 POSITION yval, y_height, y_depth, xval, x_left, x_right;
1011 yval = SCREEN_HGT / 2;
1012 xval = SCREEN_WID / 2;
1013 y_height = yval - 10;
1014 y_depth = yval + 10;
1016 x_right = xval + 32;
1018 for (i = y_height; i <= y_height + 5; i++)
1019 for (j = x_left; j <= x_right; j++)
1021 place_extra_perm_bold(floor_ptr, i, j);
1022 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1024 for (i = y_depth; i >= y_depth - 5; i--)
1025 for (j = x_left; j <= x_right; j++)
1027 place_extra_perm_bold(floor_ptr, i, j);
1028 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1030 for (j = x_left; j <= x_left + 17; j++)
1031 for (i = y_height; i <= y_depth; i++)
1033 place_extra_perm_bold(floor_ptr, i, j);
1034 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1036 for (j = x_right; j >= x_right - 17; j--)
1037 for (i = y_height; i <= y_depth; i++)
1039 place_extra_perm_bold(floor_ptr, i, j);
1040 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1043 place_extra_perm_bold(floor_ptr, y_height + 6, x_left + 18);
1044 floor_ptr->grid_array[y_height + 6][x_left + 18].info |= (CAVE_GLOW | CAVE_MARK);
1045 place_extra_perm_bold(floor_ptr, y_depth - 6, x_left + 18);
1046 floor_ptr->grid_array[y_depth - 6][x_left + 18].info |= (CAVE_GLOW | CAVE_MARK);
1047 place_extra_perm_bold(floor_ptr, y_height + 6, x_right - 18);
1048 floor_ptr->grid_array[y_height + 6][x_right - 18].info |= (CAVE_GLOW | CAVE_MARK);
1049 place_extra_perm_bold(floor_ptr, y_depth - 6, x_right - 18);
1050 floor_ptr->grid_array[y_depth - 6][x_right - 18].info |= (CAVE_GLOW | CAVE_MARK);
1052 *start_y = y_height + 5;
1054 floor_ptr->grid_array[*start_y][*start_x].feat = f_tag_to_index("ARENA_GATE");
1055 floor_ptr->grid_array[*start_y][*start_x].info |= (CAVE_GLOW | CAVE_MARK);
1059 * @brief 挑戦時闘技場への入場処理 / Town logic flow for generation of arena -KMW-
1062 static void generate_challenge_arena(floor_type *floor_ptr, player_type *challanger_ptr)
1069 floor_ptr->height = SCREEN_HGT;
1070 floor_ptr->width = SCREEN_WID;
1072 /* Start with solid walls */
1073 for (y = 0; y < MAX_HGT; y++)
1075 for (x = 0; x < MAX_WID; x++)
1077 /* Create "solid" perma-wall */
1078 place_solid_perm_bold(floor_ptr, y, x);
1080 /* Illuminate and memorize the walls */
1081 floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1085 /* Then place some floors */
1086 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1088 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1090 /* Create empty floor */
1091 floor_ptr->grid_array[y][x].feat = feat_floor;
1095 build_arena(floor_ptr, &y, &x);
1096 player_place(challanger_ptr, y, x);
1098 if(!place_monster_aux(0, challanger_ptr->y + 5, challanger_ptr->x, arena_info[challanger_ptr->arena_number].r_idx, (PM_NO_KAGE | PM_NO_PET)))
1100 challanger_ptr->exit_bldg = TRUE;
1101 challanger_ptr->arena_number++;
1102 msg_print(_("相手は欠場した。あなたの不戦勝だ。", "The enemy is unable appear. You won by default."));
1108 * @brief モンスター闘技場のフロア生成 / Builds the arena after it is entered -KMW-
1111 static void build_battle(floor_type *floor_ptr, POSITION *y, POSITION *x)
1113 POSITION yval, y_height, y_depth, xval, x_left, x_right;
1116 yval = SCREEN_HGT / 2;
1117 xval = SCREEN_WID / 2;
1118 y_height = yval - 10;
1119 y_depth = yval + 10;
1121 x_right = xval + 32;
1123 for (i = y_height; i <= y_height + 5; i++)
1124 for (j = x_left; j <= x_right; j++)
1126 place_extra_perm_bold(floor_ptr, i, j);
1127 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1129 for (i = y_depth; i >= y_depth - 3; i--)
1130 for (j = x_left; j <= x_right; j++)
1132 place_extra_perm_bold(floor_ptr, i, j);
1133 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1135 for (j = x_left; j <= x_left + 17; j++)
1136 for (i = y_height; i <= y_depth; i++)
1138 place_extra_perm_bold(floor_ptr, i, j);
1139 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1141 for (j = x_right; j >= x_right - 17; j--)
1142 for (i = y_height; i <= y_depth; i++)
1144 place_extra_perm_bold(floor_ptr, i, j);
1145 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1148 place_extra_perm_bold(floor_ptr, y_height+6, x_left+18);
1149 floor_ptr->grid_array[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1150 place_extra_perm_bold(floor_ptr, y_depth-4, x_left+18);
1151 floor_ptr->grid_array[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1152 place_extra_perm_bold(floor_ptr, y_height+6, x_right-18);
1153 floor_ptr->grid_array[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1154 place_extra_perm_bold(floor_ptr, y_depth-4, x_right-18);
1155 floor_ptr->grid_array[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1157 for (i = y_height + 1; i <= y_height + 5; i++)
1158 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1160 floor_ptr->grid_array[i][j].feat = feat_permanent_glass_wall;
1165 floor_ptr->grid_array[i][j].feat = f_tag_to_index("BUILDING_3");
1166 floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1173 * @brief モンスター闘技場への導入処理 / Town logic flow for generation of arena -KMW-
1176 static void generate_gambling_arena(floor_type *floor_ptr, player_type *creature_ptr)
1183 /* Start with solid walls */
1184 for (y = 0; y < MAX_HGT; y++)
1186 for (x = 0; x < MAX_WID; x++)
1188 /* Create "solid" perma-wall */
1189 place_solid_perm_bold(floor_ptr, y, x);
1191 /* Illuminate and memorize the walls */
1192 floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1196 /* Then place some floors */
1197 for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1199 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1201 /* Create empty floor */
1202 floor_ptr->grid_array[y][x].feat = feat_floor;
1206 build_battle(floor_ptr, &y, &x);
1208 player_place(creature_ptr, y, x);
1210 for(i = 0; i < 4; i++)
1212 place_monster_aux(0, creature_ptr->y + 8 + (i/2)*4, creature_ptr->x - 2 + (i%2)*4, battle_mon[i], (PM_NO_KAGE | PM_NO_PET));
1213 set_friendly(&floor_ptr->m_list[floor_ptr->grid_array[creature_ptr->y+8+(i/2)*4][creature_ptr->x-2+(i%2)*4].m_idx]);
1215 for(i = 1; i < floor_ptr->m_max; i++)
1217 monster_type *m_ptr = &floor_ptr->m_list[i];
1219 if (!monster_is_valid(m_ptr)) continue;
1221 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1222 update_monster(p_ptr, i, FALSE);
1227 * @brief 固定マップクエストのフロア生成 / Generate a quest level
1228 * @param player_ptr プレーヤーへの参照ポインタ
1231 static void generate_fixed_floor(player_type *player_ptr)
1235 /* Start with perm walls */
1236 floor_type *floor_ptr = player_ptr->current_floor_ptr;
1237 for (y = 0; y < floor_ptr->height; y++)
1239 for (x = 0; x < floor_ptr->width; x++)
1241 place_solid_perm_bold(floor_ptr, y, x);
1245 /* Set the quest level */
1246 floor_ptr->base_level = quest[floor_ptr->inside_quest].level;
1247 floor_ptr->dun_level = floor_ptr->base_level;
1248 floor_ptr->object_level = floor_ptr->base_level;
1249 floor_ptr->monster_level = floor_ptr->base_level;
1251 if (record_stair) exe_write_diary(p_ptr, NIKKI_TO_QUEST, floor_ptr->inside_quest, NULL);
1252 get_mon_num_prep(get_monster_hook(), NULL);
1254 init_flags = INIT_CREATE_DUNGEON;
1256 process_dungeon_file(player_ptr, "q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1260 * @brief ダンジョン時のランダムフロア生成 / Make a real level
1261 * @param player_ptr プレーヤーへの参照ポインタ
1263 * @return フロアの生成に成功したらTRUE
1265 static bool level_gen(player_type *player_ptr, concptr *why)
1267 floor_type *floor_ptr = player_ptr->current_floor_ptr;
1268 DUNGEON_IDX d_idx = floor_ptr->dungeon_idx;
1270 if ((always_small_levels || ironman_small_levels ||
1271 (one_in_(SMALL_LEVEL) && small_levels) ||
1272 (d_info[d_idx].flags1 & DF1_BEGINNER) ||
1273 (d_info[d_idx].flags1 & DF1_SMALLEST)) &&
1274 !(d_info[d_idx].flags1 & DF1_BIG))
1276 int level_height, level_width;
1277 if (d_info[d_idx].flags1 & DF1_SMALLEST)
1282 else if (d_info[d_idx].flags1 & DF1_BEGINNER)
1289 level_height = randint1(MAX_HGT / SCREEN_HGT);
1290 level_width = randint1(MAX_WID / SCREEN_WID);
1291 bool is_first_level_area = TRUE;
1292 bool is_max_area = (level_height == MAX_HGT / SCREEN_HGT) && (level_width == MAX_WID / SCREEN_WID);
1293 while (is_first_level_area || is_max_area)
1295 level_height = randint1(MAX_HGT / SCREEN_HGT);
1296 level_width = randint1(MAX_WID / SCREEN_WID);
1297 is_first_level_area = FALSE;
1298 is_max_area = (level_height == MAX_HGT / SCREEN_HGT) && (level_width == MAX_WID / SCREEN_WID);
1302 floor_ptr->height = level_height * SCREEN_HGT;
1303 floor_ptr->width = level_width * SCREEN_WID;
1305 /* Assume illegal panel */
1306 panel_row_min = floor_ptr->height;
1307 panel_col_min = floor_ptr->width;
1309 msg_format_wizard(CHEAT_DUNGEON,
1310 _("小さなフロア: X:%d, Y:%d", "A 'small' dungeon level: X:%d, Y:%d."),
1311 floor_ptr->width, floor_ptr->height);
1316 floor_ptr->height = MAX_HGT;
1317 floor_ptr->width = MAX_WID;
1319 /* Assume illegal panel */
1320 panel_row_min = floor_ptr->height;
1321 panel_col_min = floor_ptr->width;
1324 floor_ptr->dungeon_idx = d_idx;
1325 if (!cave_gen(player_ptr))
1327 *why = _("ダンジョン生成に失敗", "could not place player");
1335 * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after grid_array generation
1338 void wipe_generate_random_floor_flags(floor_type *floor_ptr)
1342 for (y = 0; y < floor_ptr->height; y++)
1344 for (x = 0; x < floor_ptr->width; x++)
1346 /* Wipe unused flags */
1347 floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1351 if (floor_ptr->dun_level)
1353 for (y = 1; y < floor_ptr->height - 1; y++)
1355 for (x = 1; x < floor_ptr->width - 1; x++)
1357 /* There might be trap */
1358 floor_ptr->grid_array[y][x].info |= CAVE_UNSAFE;
1365 * @brief フロアの全情報を初期化する / Clear and empty floor.
1366 * @parama player_ptr プレーヤーへの参照ポインタ
1369 void clear_cave(player_type *player_ptr)
1374 /* Very simplified version of wipe_o_list() */
1375 floor_type *floor_ptr = player_ptr->current_floor_ptr;
1376 (void)C_WIPE(floor_ptr->o_list, floor_ptr->o_max, object_type);
1377 floor_ptr->o_max = 1;
1378 floor_ptr->o_cnt = 0;
1380 /* Very simplified version of wipe_m_list() */
1381 for (i = 1; i < max_r_idx; i++)
1382 r_info[i].cur_num = 0;
1383 (void)C_WIPE(floor_ptr->m_list, floor_ptr->m_max, monster_type);
1384 floor_ptr->m_max = 1;
1385 floor_ptr->m_cnt = 0;
1386 for (i = 0; i < MAX_MTIMED; i++) floor_ptr->mproc_max[i] = 0;
1388 /* Pre-calc cur_num of pets in party_mon[] */
1389 precalc_cur_num_of_pet(player_ptr);
1392 /* Start with a blank floor_ptr->grid_array */
1393 for (y = 0; y < MAX_HGT; y++)
1395 for (x = 0; x < MAX_WID; x++)
1397 grid_type *g_ptr = &floor_ptr->grid_array[y][x];
1410 /* Set the base level */
1411 floor_ptr->base_level = floor_ptr->dun_level;
1413 /* Reset the monster generation level */
1414 floor_ptr->monster_level = floor_ptr->base_level;
1416 /* Reset the object generation level */
1417 floor_ptr->object_level = floor_ptr->base_level;
1422 * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
1423 * @parama player_ptr プレーヤーへの参照ポインタ
1425 * @note Hack -- regenerate any "overflow" levels
1427 void generate_floor(player_type *player_ptr)
1431 /* Fill the arrays of floors and walls in the good proportions */
1432 floor_type *floor_ptr = player_ptr->current_floor_ptr;
1433 set_floor_and_wall(floor_ptr->dungeon_idx);
1436 for (num = 0; TRUE; num++)
1441 clear_cave(player_ptr);
1443 /* Mega-Hack -- no player yet */
1444 player_ptr->x = player_ptr->y = 0;
1446 if (floor_ptr->inside_arena)
1448 generate_challenge_arena(floor_ptr, player_ptr);
1451 else if (player_ptr->phase_out)
1453 generate_gambling_arena(floor_ptr, player_ptr);
1456 else if (floor_ptr->inside_quest)
1458 generate_fixed_floor(player_ptr);
1461 /* Build the town */
1462 else if (!floor_ptr->dun_level)
1464 /* Make the wilderness */
1465 if (player_ptr->wild_mode) wilderness_gen_small(player_ptr);
1466 else wilderness_gen(player_ptr);
1469 /* Build a real level */
1472 okay = level_gen(player_ptr, &why);
1476 /* Prevent object over-flow */
1477 if (floor_ptr->o_max >= current_world_ptr->max_o_idx)
1479 why = _("アイテムが多すぎる", "too many objects");
1482 /* Prevent monster over-flow */
1483 else if (floor_ptr->m_max >= current_world_ptr->max_m_idx)
1485 why = _("モンスターが多すぎる", "too many monsters");
1492 if (why) msg_format(_("生成やり直し(%s)", "Generation restarted (%s)"), why);
1494 wipe_o_list(floor_ptr);
1498 /* Glow deep lava and building entrances tempor
1499 glow_deep_lava_and_bldg(floor_ptr);
1502 player_ptr->enter_dungeon = FALSE;
1504 wipe_generate_random_floor_flags(floor_ptr);
1508 * @brief build_tunnel用に通路を掘るための方向をランダムに決める / Pick a random direction
1509 * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
1510 * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
1513 static void rand_dir(POSITION *rdir, POSITION *cdir)
1515 /* Pick a random direction */
1516 int i = randint0(4);
1518 /* Extract the dy/dx components */
1524 * @brief build_tunnel用に通路を掘るための方向を位置関係通りに決める / Always picks a correct direction
1525 * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
1526 * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
1533 static void correct_dir(POSITION *rdir, POSITION *cdir, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
1535 /* Extract vertical and horizontal directions */
1536 *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
1537 *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
1539 /* Never move diagonally */
1542 if (randint0(100) < 50)
1550 * @brief 部屋間のトンネルを生成する / Constructs a tunnel between two points
1555 * @return 生成に成功したらTRUEを返す
1557 * This function must be called BEFORE any streamers are created,\n
1558 * since we use the special "granite wall" sub-types to keep track\n
1559 * of legal places for corridors to pierce rooms.\n
1561 * We use "door_flag" to prevent excessive construction of doors\n
1562 * along overlapping corridors.\n
1564 * We queue the tunnel grids to prevent door creation along a corridor\n
1565 * which intersects itself.\n
1567 * We queue the wall piercing grids to prevent a corridor from leaving\n
1568 * a room and then coming back in through the same entrance.\n
1570 * We "pierce" grids which are "outer" walls of rooms, and when we\n
1571 * do so, we change all adjacent "outer" walls of rooms into "solid"\n
1572 * walls so that no two corridors may use adjacent grids for exits.\n
1574 * The "solid" wall check prevents corridors from "chopping" the\n
1575 * corners of rooms off, as well as "silly" door placement, and\n
1576 * "excessively wide" room entrances.\n
1580 * inner -- inner room walls\n
1581 * outer -- outer room walls\n
1582 * solid -- solid room walls\n
1584 bool build_tunnel(floor_type *floor_ptr, POSITION row1, POSITION col1, POSITION row2, POSITION col2)
1587 POSITION tmp_row, tmp_col;
1588 POSITION row_dir, col_dir;
1589 POSITION start_row, start_col;
1590 int main_loop_count = 0;
1592 bool door_flag = FALSE;
1596 /* Save the starting location */
1600 /* Start out in the correct direction */
1601 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1603 /* Keep going until done (or bored) */
1604 while ((row1 != row2) || (col1 != col2))
1606 /* Mega-Hack -- Paranoia -- prevent infinite loops */
1607 if (main_loop_count++ > 2000) return FALSE;
1609 /* Allow bends in the tunnel */
1610 if (randint0(100) < dun_tun_chg)
1612 /* Acquire the correct direction */
1613 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1615 /* Random direction */
1616 if (randint0(100) < dun_tun_rnd)
1618 rand_dir(&row_dir, &col_dir);
1622 /* Get the next location */
1623 tmp_row = row1 + row_dir;
1624 tmp_col = col1 + col_dir;
1627 /* Extremely Important -- do not leave the dungeon */
1628 while (!in_bounds(floor_ptr, tmp_row, tmp_col))
1630 /* Acquire the correct direction */
1631 correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1633 /* Random direction */
1634 if (randint0(100) < dun_tun_rnd)
1636 rand_dir(&row_dir, &col_dir);
1639 /* Get the next location */
1640 tmp_row = row1 + row_dir;
1641 tmp_col = col1 + col_dir;
1644 g_ptr = &floor_ptr->grid_array[tmp_row][tmp_col];
1646 /* Avoid "solid" walls */
1647 if (is_solid_grid(g_ptr)) continue;
1649 /* Pierce "outer" walls of rooms */
1650 if (is_outer_grid(g_ptr))
1652 /* Acquire the "next" location */
1653 y = tmp_row + row_dir;
1654 x = tmp_col + col_dir;
1656 /* Hack -- Avoid outer/solid walls */
1657 if (is_outer_bold(floor_ptr, y, x)) continue;
1658 if (is_solid_bold(floor_ptr, y, x)) continue;
1660 /* Accept this location */
1664 /* Save the wall location */
1665 if (dun->wall_n < WALL_MAX)
1667 dun->wall[dun->wall_n].y = row1;
1668 dun->wall[dun->wall_n].x = col1;
1673 /* Forbid re-entry near this piercing */
1674 for (y = row1 - 1; y <= row1 + 1; y++)
1676 for (x = col1 - 1; x <= col1 + 1; x++)
1678 /* Convert adjacent "outer" walls as "solid" walls */
1679 if (is_outer_bold(floor_ptr, y, x))
1681 /* Change the wall to a "solid" wall */
1682 place_solid_noperm_bold(floor_ptr, y, x);
1688 /* Travel quickly through rooms */
1689 else if (g_ptr->info & (CAVE_ROOM))
1691 /* Accept the location */
1696 /* Tunnel through all other walls */
1697 else if (is_extra_grid(g_ptr) || is_inner_grid(g_ptr) || is_solid_grid(g_ptr))
1699 /* Accept this location */
1703 /* Save the tunnel location */
1704 if (dun->tunn_n < TUNN_MAX)
1706 dun->tunn[dun->tunn_n].y = row1;
1707 dun->tunn[dun->tunn_n].x = col1;
1712 /* Allow door in next grid */
1716 /* Handle corridor intersections or overlaps */
1719 /* Accept the location */
1723 /* Collect legal door locations */
1726 /* Save the door location */
1727 if (dun->door_n < DOOR_MAX)
1729 dun->door[dun->door_n].y = row1;
1730 dun->door[dun->door_n].x = col1;
1735 /* No door in next grid */
1739 /* Hack -- allow pre-emptive tunnel termination */
1740 if (randint0(100) >= dun_tun_con)
1742 /* Distance between row1 and start_row */
1743 tmp_row = row1 - start_row;
1744 if (tmp_row < 0) tmp_row = (-tmp_row);
1746 /* Distance between col1 and start_col */
1747 tmp_col = col1 - start_col;
1748 if (tmp_col < 0) tmp_col = (-tmp_col);
1750 /* Terminate the tunnel */
1751 if ((tmp_row > 10) || (tmp_col > 10)) break;
1761 * @brief トンネル生成のための基準点を指定する。
1762 * @param x 基準点を指定するX座標の参照ポインタ、適時値が修正される。
1763 * @param y 基準点を指定するY座標の参照ポインタ、適時値が修正される。
1764 * @param affectwall (調査中)
1767 * This routine adds the square to the tunnel\n
1768 * It also checks for SOLID walls - and returns a nearby\n
1769 * non-SOLID square in (x,y) so that a simple avoiding\n
1770 * routine can be used. The returned boolean value reflects\n
1771 * whether or not this routine hit a SOLID wall.\n
1773 * "affectwall" toggles whether or not this new square affects\n
1774 * the boundaries of rooms. - This is used by the catacomb\n
1776 * @todo 特に詳細な処理の意味を調査すべし
1778 static bool set_tunnel(floor_type *floor_ptr, POSITION *x, POSITION *y, bool affectwall)
1782 grid_type *g_ptr = &floor_ptr->grid_array[*y][*x];
1784 if (!in_bounds(floor_ptr, *y, *x)) return TRUE;
1786 if (is_inner_grid(g_ptr))
1791 if (is_extra_bold(floor_ptr, *y, *x))
1793 /* Save the tunnel location */
1794 if (dun->tunn_n < TUNN_MAX)
1796 dun->tunn[dun->tunn_n].y = *y;
1797 dun->tunn[dun->tunn_n].x = *x;
1805 if (is_floor_bold(floor_ptr, *y, *x))
1807 /* Don't do anything */
1811 if (is_outer_grid(g_ptr) && affectwall)
1813 /* Save the wall location */
1814 if (dun->wall_n < WALL_MAX)
1816 dun->wall[dun->wall_n].y = *y;
1817 dun->wall[dun->wall_n].x = *x;
1822 /* Forbid re-entry near this piercing */
1823 for (j = *y - 1; j <= *y + 1; j++)
1825 for (i = *x - 1; i <= *x + 1; i++)
1827 /* Convert adjacent "outer" walls as "solid" walls */
1828 if (is_outer_bold(floor_ptr, j, i))
1830 /* Change the wall to a "solid" wall */
1831 place_solid_noperm_bold(floor_ptr, j, i);
1836 /* Clear mimic type */
1837 floor_ptr->grid_array[*y][*x].mimic = 0;
1839 place_floor_bold(floor_ptr, *y, *x);
1844 if (is_solid_grid(g_ptr) && affectwall)
1846 /* cannot place tunnel here - use a square to the side */
1848 /* find usable square and return value in (x,y) */
1854 while ((i > 0) && is_solid_bold(floor_ptr, *y + dy, *x + dx))
1856 dy = randint0(3) - 1;
1857 dx = randint0(3) - 1;
1859 if (!in_bounds(floor_ptr, *y + dy, *x + dx))
1870 /* Failed for some reason: hack - ignore the solidness */
1871 place_outer_grid(g_ptr);
1876 /* Give new, acceptable coordinate. */
1888 * @brief 外壁を削って「カタコンベ状」の通路を作成する / This routine creates the catacomb-like tunnels by removing extra rock.
1893 * Note that this routine is only called on "even" squares - so it gives
1894 * a natural checkerboard pattern.
1896 static void create_cata_tunnel(floor_type *floor_ptr, POSITION x, POSITION y)
1903 set_tunnel(floor_ptr, &x1, &y1, FALSE);
1907 set_tunnel(floor_ptr, &x1, &y1, FALSE);
1911 set_tunnel(floor_ptr, &x1, &y1, FALSE);
1915 set_tunnel(floor_ptr, &x1, &y1, FALSE);
1920 * @brief トンネル生成処理(詳細調査中)/ This routine does the bulk of the work in creating the new types of tunnels.
1924 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)\n
1925 * It doesn't need to add any complexity - straight lines are fine.\n
1926 * The SOLID walls are avoided by a recursive algorithm which tries random ways\n
1927 * around the obstical until it works. The number of itterations is counted, and it\n
1928 * this gets too large the routine exits. This should stop any crashes - but may leave\n
1929 * small gaps in the tunnel where there are too many SOLID walls.\n
1931 * Type 1 tunnels are extremely simple - straight line from A to B. This is only used\n
1932 * as a part of the dodge SOLID walls algorithm.\n
1934 * Type 2 tunnels are made of two straight lines at right angles. When this is used with\n
1935 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.\n
1937 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.\n
1938 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near\n
1941 static void short_seg_hack(floor_type *floor_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int count, bool *fail)
1947 /* Check for early exit */
1948 if (!(*fail)) return;
1950 length = distance(x1, y1, x2, y2);
1954 if ((type == 1) && (length != 0))
1957 for (i = 0; i <= length; i++)
1959 x = x1 + i * (x2 - x1) / length;
1960 y = y1 + i * (y2 - y1) / length;
1961 if (!set_tunnel(floor_ptr, &x, &y, TRUE))
1965 /* This isn't working - probably have an infinite loop */
1970 /* solid wall - so try to go around */
1971 short_seg_hack(floor_ptr, x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
1972 short_seg_hack(floor_ptr, x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
1976 else if ((type == 2) || (type == 3))
1980 for (i = x1; i <= x2; i++)
1984 if (!set_tunnel(floor_ptr, &x, &y, TRUE))
1986 /* solid wall - so try to go around */
1987 short_seg_hack(floor_ptr, x, y, i - 1, y1, 1, count, fail);
1988 short_seg_hack(floor_ptr, x, y, i + 1, y1, 1, count, fail);
1990 if ((type == 3) && ((x + y) % 2))
1992 create_cata_tunnel(floor_ptr, i, y1);
1998 for (i = x2; i <= x1; i++)
2002 if (!set_tunnel(floor_ptr, &x, &y, TRUE))
2004 /* solid wall - so try to go around */
2005 short_seg_hack(floor_ptr, x, y, i - 1, y1, 1, count, fail);
2006 short_seg_hack(floor_ptr, x, y, i + 1, y1, 1, count, fail);
2008 if ((type == 3) && ((x + y) % 2))
2010 create_cata_tunnel(floor_ptr, i, y1);
2017 for (i = y1; i <= y2; i++)
2021 if (!set_tunnel(floor_ptr, &x, &y, TRUE))
2023 /* solid wall - so try to go around */
2024 short_seg_hack(floor_ptr, x, y, x2, i - 1, 1, count, fail);
2025 short_seg_hack(floor_ptr, x, y, x2, i + 1, 1, count, fail);
2027 if ((type == 3) && ((x + y) % 2))
2029 create_cata_tunnel(floor_ptr, x2, i);
2035 for (i = y2; i <= y1; i++)
2039 if (!set_tunnel(floor_ptr, &x, &y, TRUE))
2041 /* solid wall - so try to go around */
2042 short_seg_hack(floor_ptr, x, y, x2, i - 1, 1, count, fail);
2043 short_seg_hack(floor_ptr, x, y, x2, i + 1, 1, count, fail);
2045 if ((type == 3) && ((x + y) % 2))
2047 create_cata_tunnel(floor_ptr, x2, i);
2056 * @brief 特定の壁(永久壁など)を避けながら部屋間の通路を作成する / This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
2060 * Permanent rock is ignored in this path finding- sometimes there is no\n
2061 * path around anyway -so there will be a crash if we try to find one.\n
2062 * This routine is much like the river creation routine in Zangband.\n
2063 * It works by dividing a line segment into two. The segments are divided\n
2064 * until they are less than "cutoff" - when the corresponding routine from\n
2065 * "short_seg_hack" is called.\n
2066 * Note it is VERY important that the "stop if hit another passage" logic\n
2067 * stays as is. Without this the dungeon turns into Swiss Cheese...\n
2069 bool build_tunnel2(floor_type *floor_ptr, POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int cutoff)
2071 POSITION x3, y3, dx, dy;
2072 POSITION changex, changey;
2075 bool retval, firstsuccede;
2078 length = distance(x1, y1, x2, y2);
2080 if (length > cutoff)
2083 * Divide path in half and call routine twice.
2088 /* perturbation perpendicular to path */
2089 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
2090 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
2092 /* Work out "mid" ponit */
2093 x3 = x1 + dx + changex;
2094 y3 = y1 + dy + changey;
2096 /* See if in bounds - if not - do not perturb point */
2097 if (!in_bounds(floor_ptr, y3, x3))
2103 g_ptr = &floor_ptr->grid_array[y3][x3];
2104 if (is_solid_grid(g_ptr))
2106 /* move midpoint a bit to avoid problem. */
2112 while ((i > 0) && is_solid_bold(floor_ptr, y3 + dy, x3 + dx))
2114 dy = randint0(3) - 1;
2115 dx = randint0(3) - 1;
2116 if (!in_bounds(floor_ptr, y3 + dy, x3 + dx))
2126 /* Failed for some reason: hack - ignore the solidness */
2127 place_outer_bold(floor_ptr, y3, x3);
2133 g_ptr = &floor_ptr->grid_array[y3][x3];
2136 if (is_floor_grid(g_ptr))
2138 if (build_tunnel2(floor_ptr, x1, y1, x3, y3, type, cutoff))
2140 if ((floor_ptr->grid_array[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
2142 /* do second half only if works + if have hit a room */
2143 retval = build_tunnel2(floor_ptr, x3, y3, x2, y2, type, cutoff);
2147 /* have hit another tunnel - make a set of doors here */
2150 /* Save the door location */
2151 if (dun->door_n < DOOR_MAX)
2153 dun->door[dun->door_n].y = y3;
2154 dun->door[dun->door_n].x = x3;
2159 firstsuccede = TRUE;
2163 /* false- didn't work all the way */
2165 firstsuccede = FALSE;
2170 /* tunnel through walls */
2171 if (build_tunnel2(floor_ptr, x1, y1, x3, y3, type, cutoff))
2173 retval = build_tunnel2(floor_ptr, x3, y3, x2, y2, type, cutoff);
2174 firstsuccede = TRUE;
2178 /* false- didn't work all the way */
2180 firstsuccede = FALSE;
2185 /* only do this if the first half has worked */
2186 set_tunnel(floor_ptr, &x3, &y3, TRUE);
2188 /* return value calculated above */
2193 /* Do a short segment */
2195 short_seg_hack(floor_ptr, x1, y1, x2, y2, type, 0, &retval);
2197 /* Hack - ignore return value so avoid infinite loops */