OSDN Git Service

[Refactor] #38993 m_cnt, m_max を floor_type 構造体に取り込み.
[hengband/hengband.git] / src / floor-generate.c
1 /*!
2  * @file generate.c
3  * @brief ダンジョンの生成 / Dungeon generation
4  * @date 2014/01/04
5  * @author
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
11  * @details
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
15  *\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
18  *\n
19  * Consider the "v_info.txt" file for vault generation.\n
20  *\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
27  *\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
33  *\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
44  *\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
48  *\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
55  *\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
63  *\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
66  *\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
70  *\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
77  *\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
85  *\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
91  *\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
97  */
98
99 #include "angband.h"
100 #include "util.h"
101 #include "bldg.h"
102
103 #include "grid.h"
104 #include "rooms.h"
105 #include "dungeon.h"
106 #include "floor.h"
107 #include "floor-save.h"
108 #include "floor-streams.h"
109 #include "floor-generate.h"
110 #include "floor-events.h"
111 #include "floor-generate.h"
112 #include "feature.h"
113 #include "trap.h"
114 #include "monster.h"
115 #include "quest.h"
116 #include "player-status.h"
117 #include "wild.h"
118 #include "monster-status.h"
119 #include "dungeon-file.h"
120 #include "init.h"
121 #include "feature.h"
122 #include "spells.h"
123
124 int dun_tun_rnd; 
125 int dun_tun_chg;
126 int dun_tun_con;
127 int dun_tun_pen;
128 int dun_tun_jct;
129
130
131 /*!
132  * Dungeon generation data -- see "cave_gen()"
133  */
134 dun_data *dun;
135
136
137 /*!
138  * @brief 上下左右の外壁数をカウントする / Count the number of walls adjacent to the given grid.
139  * @param y 基準のy座標
140  * @param x 基準のx座標
141  * @return 隣接する外壁の数
142  * @note Assumes "in_bounds(y, x)"
143  * @details We count only granite walls and permanent walls.
144  */
145 static int next_to_walls(POSITION y, POSITION x)
146 {
147         int k = 0;
148
149         if (in_bounds(y + 1, x) && is_extra_bold(y + 1, x)) k++;
150         if (in_bounds(y - 1, x) && is_extra_bold(y - 1, x)) k++;
151         if (in_bounds(y, x + 1) && is_extra_bold(y, x + 1)) k++;
152         if (in_bounds(y, x - 1) && is_extra_bold(y, x - 1)) k++;
153
154         return (k);
155 }
156
157 /*!
158  * @brief alloc_stairs()の補助として指定の位置に階段を生成できるかの判定を行う / Helper function for alloc_stairs(). Is this a good location for stairs?
159  * @param y 基準のy座標
160  * @param x 基準のx座標
161  * @param walls 最低減隣接させたい外壁の数
162  * @return 階段を生成して問題がないならばTRUEを返す。
163  */
164 static bool alloc_stairs_aux(POSITION y, POSITION x, int walls)
165 {
166         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
167
168         /* Require "naked" floor grid */
169         if (!is_floor_grid(g_ptr)) return FALSE;
170         if (pattern_tile(y, x)) return FALSE;
171         if (g_ptr->o_idx || g_ptr->m_idx) return FALSE;
172
173         /* Require a certain number of adjacent walls */
174         if (next_to_walls(y, x) < walls) return FALSE;
175
176         return TRUE;
177 }
178
179
180 /*!
181  * @brief 外壁に隣接させて階段を生成する / Places some staircases near walls
182  * @param feat 配置したい地形ID
183  * @param num 配置したい階段の数
184  * @param walls 最低減隣接させたい外壁の数
185  * @return 規定数通りに生成に成功したらTRUEを返す。
186  */
187 static bool alloc_stairs(FEAT_IDX feat, int num, int walls)
188 {
189         int i;
190         int shaft_num = 0;
191
192         feature_type *f_ptr = &f_info[feat];
193
194         if (have_flag(f_ptr->flags, FF_LESS))
195         {
196                 /* No up stairs in town or in ironman mode */
197                 if (ironman_downward || !current_floor_ptr->dun_level) return TRUE;
198
199                 if (current_floor_ptr->dun_level > d_info[p_ptr->dungeon_idx].mindepth)
200                         shaft_num = (randint1(num+1))/2;
201         }
202         else if (have_flag(f_ptr->flags, FF_MORE))
203         {
204                 QUEST_IDX q_idx = quest_number(current_floor_ptr->dun_level);
205
206                 /* No downstairs on quest levels */
207                 if (current_floor_ptr->dun_level > 1 && q_idx)
208                 {
209                         monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
210
211                         /* The quest monster(s) is still alive? */
212                         if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
213                                 return TRUE;
214                 }
215
216                 /* No downstairs at the bottom */
217                 if (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) return TRUE;
218
219                 if ((current_floor_ptr->dun_level < d_info[p_ptr->dungeon_idx].maxdepth-1) && !quest_number(current_floor_ptr->dun_level+1))
220                         shaft_num = (randint1(num)+1)/2;
221         }
222         else return FALSE;
223
224
225         /* Place "num" stairs */
226         for (i = 0; i < num; i++)
227         {
228                 while (TRUE)
229                 {
230                         POSITION y, x = 0;
231                         grid_type *g_ptr;
232
233                         int candidates = 0;
234                         int pick;
235
236                         for (y = 1; y < current_floor_ptr->height - 1; y++)
237                         {
238                                 for (x = 1; x < current_floor_ptr->width - 1; x++)
239                                 {
240                                         if (alloc_stairs_aux(y, x, walls))
241                                         {
242                                                 /* A valid space found */
243                                                 candidates++;
244                                         }
245                                 }
246                         }
247
248                         /* No valid place! */
249                         if (!candidates)
250                         {
251                                 /* There are exactly no place! */
252                                 if (walls <= 0) return FALSE;
253
254                                 /* Decrease walls limit, and try again */
255                                 walls--;
256                                 continue;
257                         }
258
259                         /* Choose a random one */
260                         pick = randint1(candidates);
261
262                         for (y = 1; y < current_floor_ptr->height - 1; y++)
263                         {
264                                 for (x = 1; x < current_floor_ptr->width - 1; x++)
265                                 {
266                                         if (alloc_stairs_aux(y, x, walls))
267                                         {
268                                                 pick--;
269
270                                                 /* Is this a picked one? */
271                                                 if (!pick) break;
272                                         }
273                                 }
274
275                                 if (!pick) break;
276                         }
277                         g_ptr = &current_floor_ptr->grid_array[y][x];
278
279                         /* Clear possible garbage of hidden trap */
280                         g_ptr->mimic = 0;
281
282                         /* Clear previous contents, add stairs */
283                         g_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
284
285                         /* No longer "FLOOR" */
286                         g_ptr->info &= ~(CAVE_FLOOR);
287
288                         /* Success */
289                         break;
290                 }
291         }
292         return TRUE;
293 }
294
295 /*!
296  * @brief フロア上のランダム位置に各種オブジェクトを配置する / Allocates some objects (using "place" and "type")
297  * @param set 配置したい地形の種類
298  * @param typ 配置したいオブジェクトの種類
299  * @param num 配置したい数
300  * @return 規定数通りに生成に成功したらTRUEを返す。
301  */
302 static void alloc_object(int set, EFFECT_ID typ, int num)
303 {
304         POSITION y = 0, x = 0;
305         int k;
306         int dummy = 0;
307         grid_type *g_ptr;
308
309         /* A small level has few objects. */
310         num = num * current_floor_ptr->height * current_floor_ptr->width / (MAX_HGT*MAX_WID) +1;
311
312         /* Place some objects */
313         for (k = 0; k < num; k++)
314         {
315                 /* Pick a "legal" spot */
316                 while (dummy < SAFE_MAX_ATTEMPTS)
317                 {
318                         bool room;
319
320                         dummy++;
321
322                         y = randint0(current_floor_ptr->height);
323                         x = randint0(current_floor_ptr->width);
324
325                         g_ptr = &current_floor_ptr->grid_array[y][x];
326
327                         /* Require "naked" floor grid */
328                         if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
329
330                         /* Avoid player location */
331                         if (player_bold(y, x)) continue;
332
333                         /* Check for "room" */
334                         room = (current_floor_ptr->grid_array[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
335
336                         /* Require corridor? */
337                         if ((set == ALLOC_SET_CORR) && room) continue;
338
339                         /* Require room? */
340                         if ((set == ALLOC_SET_ROOM) && !room) continue;
341
342                         /* Accept it */
343                         break;
344                 }
345
346                 if (dummy >= SAFE_MAX_ATTEMPTS)
347                 {
348                         msg_print_wizard(CHEAT_DUNGEON, _("アイテムの配置に失敗しました。", "Failed to place object."));
349                         return;
350                 }
351
352
353                 /* Place something */
354                 switch (typ)
355                 {
356                         case ALLOC_TYP_RUBBLE:
357                         {
358                                 place_rubble(y, x);
359                                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
360                                 break;
361                         }
362
363                         case ALLOC_TYP_TRAP:
364                         {
365                                 place_trap(y, x);
366                                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
367                                 break;
368                         }
369
370                         case ALLOC_TYP_GOLD:
371                         {
372                                 place_gold(y, x);
373                                 break;
374                         }
375
376                         case ALLOC_TYP_OBJECT:
377                         {
378                                 place_object(y, x, 0L);
379                                 break;
380                         }
381                 }
382         }
383 }
384
385
386
387
388 /*!
389  * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
390  * @return 成功したならばTRUEを返す
391  */
392 bool place_quest_monsters(void)
393 {
394         int i;
395
396         /* Handle the quest monster placements */
397         for (i = 0; i < max_q_idx; i++)
398         {
399                 monster_race *r_ptr;
400                 BIT_FLAGS mode;
401                 int j;
402
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 != current_floor_ptr->dun_level ||
407                     p_ptr->dungeon_idx != quest[i].dungeon ||
408                     (quest[i].flags & QUEST_FLAG_PRESET))
409                 {
410                         /* Ignore it */
411                         continue;
412                 }
413
414                 r_ptr = &r_info[quest[i].r_idx];
415
416                 /* Hack -- "unique" monsters must be "unique" */
417                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
418                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
419
420                 mode = (PM_NO_KAGE | PM_NO_PET);
421
422                 if (!(r_ptr->flags1 & RF1_FRIENDS))
423                         mode |= PM_ALLOW_GROUP;
424
425                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
426                 {
427                         int k;
428
429                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
430                         {
431                                 POSITION x = 0, y = 0;
432                                 int l;
433
434                                 /* Find an empty grid */
435                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
436                                 {
437                                         grid_type    *g_ptr;
438                                         feature_type *f_ptr;
439
440                                         y = randint0(current_floor_ptr->height);
441                                         x = randint0(current_floor_ptr->width);
442
443                                         g_ptr = &current_floor_ptr->grid_array[y][x];
444                                         f_ptr = &f_info[g_ptr->feat];
445
446                                         if (!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) continue;
447                                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
448                                         if (distance(y, x, p_ptr->y, p_ptr->x) < 10) continue;
449                                         if (g_ptr->info & CAVE_ICKY) continue;
450                                         else break;
451                                 }
452
453                                 /* Failed to place */
454                                 if (!l) return FALSE;
455
456                                 /* Try to place the monster */
457                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
458                                 {
459                                         /* Success */
460                                         break;
461                                 }
462                                 else
463                                 {
464                                         /* Failure - Try again */
465                                         continue;
466                                 }
467                         }
468
469                         /* Failed to place */
470                         if (k == SAFE_MAX_ATTEMPTS) return FALSE;
471                 }
472         }
473
474         return TRUE;
475 }
476
477 /*!
478  * @brief フロアに洞窟や湖を配置する / Generate various caverns and lakes
479  * @details There were moved from cave_gen().
480  * @return なし
481  */
482 static void gen_caverns_and_lakes(void)
483 {
484 #ifdef ALLOW_CAVERNS_AND_LAKES
485         /* Possible "destroyed" level */
486         if ((current_floor_ptr->dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DESTROY))
487         {
488                 dun->destroyed = TRUE;
489
490                 /* extra rubble around the place looks cool */
491                 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
492         }
493
494         /* Make a lake some of the time */
495         if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
496             (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_MASK))
497         {
498                 int count = 0;
499                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_WATER) count += 3;
500                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_LAVA) count += 3;
501                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_RUBBLE) count += 3;
502                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_TREE) count += 3;
503
504                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_LAVA)
505                 {
506                         /* Lake of Lava */
507                         if ((current_floor_ptr->dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
508                         count -= 2;
509
510                         /* Lake of Lava2 */
511                         if (!dun->laketype && (current_floor_ptr->dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
512                         count--;
513                 }
514
515                 if ((d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_WATER) && !dun->laketype)
516                 {
517                         /* Lake of Water */
518                         if ((current_floor_ptr->dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
519                         count -= 2;
520
521                         /* Lake of Water2 */
522                         if (!dun->laketype && (current_floor_ptr->dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
523                         count--;
524                 }
525
526                 if ((d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
527                 {
528                         /* Lake of rubble */
529                         if ((current_floor_ptr->dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
530                         count -= 2;
531
532                         /* Lake of rubble2 */
533                         if (!dun->laketype && (current_floor_ptr->dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
534                         count--;
535                 }
536
537                 /* Lake of tree */
538                 if ((current_floor_ptr->dun_level > 5) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
539
540                 if (dun->laketype)
541                 {
542                         msg_print_wizard(CHEAT_DUNGEON, _("湖を生成します。", "Lake on the level."));
543                         build_lake(dun->laketype);
544                 }
545         }
546
547         if ((current_floor_ptr->dun_level > DUN_CAVERN) && !dun->empty_level &&
548             (d_info[p_ptr->dungeon_idx].flags1 & DF1_CAVERN) &&
549             !dun->laketype && !dun->destroyed && (randint1(1000) < current_floor_ptr->dun_level))
550         {
551                 dun->cavern = TRUE;
552
553                 /* make a large fractal current_floor_ptr->grid_array in the middle of the dungeon */
554
555                 msg_print_wizard(CHEAT_DUNGEON, _("洞窟を生成。", "Cavern on level."));
556                 build_cavern();
557         }
558 #endif /* ALLOW_CAVERNS_AND_LAKES */
559
560         /* Hack -- No destroyed "quest" levels */
561         if (quest_number(current_floor_ptr->dun_level)) dun->destroyed = FALSE;
562 }
563
564
565 /*!
566  * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
567  * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
568  * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
569  */
570 static bool cave_gen(void)
571 {
572         int i, k;
573         POSITION y, x;
574         dun_data dun_body;
575
576         current_floor_ptr->lite_n = 0;
577         current_floor_ptr->mon_lite_n = 0;
578         current_floor_ptr->redraw_n = 0;
579         current_floor_ptr->view_n = 0;
580
581         /* Global data */
582         dun = &dun_body;
583
584         dun->destroyed = FALSE;
585         dun->empty_level = FALSE;
586         dun->cavern = FALSE;
587         dun->laketype = 0;
588
589         /* Fill the arrays of floors and walls in the good proportions */
590         set_floor_and_wall(p_ptr->dungeon_idx);
591         get_mon_num_prep(get_monster_hook(), NULL);
592
593         /* Randomize the dungeon creation values */
594         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
595         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
596         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
597         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
598         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
599
600         /* Actual maximum number of rooms on this level */
601         dun->row_rooms = current_floor_ptr->height / BLOCK_HGT;
602         dun->col_rooms = current_floor_ptr->width / BLOCK_WID;
603
604         /* Initialize the room table */
605         for (y = 0; y < dun->row_rooms; y++)
606         {
607                 for (x = 0; x < dun->col_rooms; x++)
608                 {
609                         dun->room_map[y][x] = FALSE;
610                 }
611         }
612
613         /* No rooms yet */
614         dun->cent_n = 0;
615
616         /* Empty arena levels */
617         if (ironman_empty_levels || ((d_info[p_ptr->dungeon_idx].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
618         {
619                 dun->empty_level = TRUE;
620                 msg_print_wizard(CHEAT_DUNGEON, _("アリーナレベルを生成。", "Arena level."));
621         }
622
623         if (dun->empty_level)
624         {
625                 /* Start with floors */
626                 for (y = 0; y < current_floor_ptr->height; y++)
627                 {
628                         for (x = 0; x < current_floor_ptr->width; x++)
629                         {
630                                 place_floor_bold(y, x);
631                         }
632                 }
633
634                 /* Special boundary walls -- Top and bottom */
635                 for (x = 0; x < current_floor_ptr->width; x++)
636                 {
637                         place_extra_bold(0, x);
638                         place_extra_bold(current_floor_ptr->height - 1, x);
639                 }
640
641                 /* Special boundary walls -- Left and right */
642                 for (y = 1; y < (current_floor_ptr->height - 1); y++)
643                 {
644                         place_extra_bold(y, 0);
645                         place_extra_bold(y, current_floor_ptr->width - 1);
646                 }
647         }
648         else
649         {
650                 /* Start with walls */
651                 for (y = 0; y < current_floor_ptr->height; y++)
652                 {
653                         for (x = 0; x < current_floor_ptr->width; x++)
654                         {
655                                 place_extra_bold(y, x);
656                         }
657                 }
658         }
659
660         /* Generate various caverns and lakes */
661         gen_caverns_and_lakes();
662
663         /* Build maze */
664         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_MAZE)
665         {
666                 build_maze_vault(current_floor_ptr->width/2-1, current_floor_ptr->height/2-1, current_floor_ptr->width-4, current_floor_ptr->height-4, FALSE);
667
668                 /* Place 3 or 4 down stairs near some walls */
669                 if (!alloc_stairs(feat_down_stair, rand_range(2, 3), 3)) return FALSE;
670
671                 /* Place 1 or 2 up stairs near some walls */
672                 if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
673         }
674
675         /* Build some rooms */
676         else
677         {
678                 int tunnel_fail_count = 0;
679
680                 /*
681                  * Build each type of room in current_world_ptr->game_turn until we cannot build any more.
682                  */
683                 if (!generate_rooms()) return FALSE;
684
685
686                 /* Make a hole in the dungeon roof sometimes at level 1 */
687                 if (current_floor_ptr->dun_level == 1)
688                 {
689                         while (one_in_(DUN_MOS_DEN))
690                         {
691                                 place_trees(randint1(current_floor_ptr->width - 2), randint1(current_floor_ptr->height - 2));
692                         }
693                 }
694
695                 /* Destroy the level if necessary */
696                 if (dun->destroyed) destroy_level();
697
698                 /* Hack -- Add some rivers */
699                 if (one_in_(3) && (randint1(current_floor_ptr->dun_level) > 5))
700                 {
701                         FEAT_IDX feat1 = 0, feat2 = 0;
702
703                         /* Choose water mainly */
704                         if ((randint1(MAX_DEPTH * 2) - 1 > current_floor_ptr->dun_level) && (d_info[p_ptr->dungeon_idx].flags1 & DF1_WATER_RIVER))
705                         {
706                                 feat1 = feat_deep_water;
707                                 feat2 = feat_shallow_water;
708                         }
709                         else /* others */
710                         {
711                                 FEAT_IDX select_deep_feat[10];
712                                 FEAT_IDX select_shallow_feat[10];
713                                 int select_id_max = 0, selected;
714
715                                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_LAVA_RIVER)
716                                 {
717                                         select_deep_feat[select_id_max] = feat_deep_lava;
718                                         select_shallow_feat[select_id_max] = feat_shallow_lava;
719                                         select_id_max++;
720                                 }
721                                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_POISONOUS_RIVER)
722                                 {
723                                         select_deep_feat[select_id_max] = feat_deep_poisonous_puddle;
724                                         select_shallow_feat[select_id_max] = feat_shallow_poisonous_puddle;
725                                         select_id_max++;
726                                 }
727                                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_ACID_RIVER)
728                                 {
729                                         select_deep_feat[select_id_max] = feat_deep_acid_puddle;
730                                         select_shallow_feat[select_id_max] = feat_shallow_acid_puddle;
731                                         select_id_max++;
732                                 }
733
734                                 selected = randint0(select_id_max);
735                                 feat1 = select_deep_feat[selected];
736                                 feat2 = select_shallow_feat[selected];
737                         }
738
739                         if (feat1)
740                         {
741                                 feature_type *f_ptr = &f_info[feat1];
742
743                                 /* Only add river if matches lake type or if have no lake at all */
744                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
745                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
746                                      !dun->laketype)
747                                 {
748                                         add_river(feat1, feat2);
749                                 }
750                         }
751                 }
752
753                 /* Hack -- Scramble the room order */
754                 for (i = 0; i < dun->cent_n; i++)
755                 {
756                         POSITION ty, tx;
757                         int pick = rand_range(0, i);
758
759                         ty = dun->cent[i].y;
760                         tx = dun->cent[i].x;
761                         dun->cent[i].y = dun->cent[pick].y;
762                         dun->cent[i].x = dun->cent[pick].x;
763                         dun->cent[pick].y = ty;
764                         dun->cent[pick].x = tx;
765                 }
766
767                 /* Start with no tunnel doors */
768                 dun->door_n = 0;
769
770                 /* Hack -- connect the first room to the last room */
771                 y = dun->cent[dun->cent_n-1].y;
772                 x = dun->cent[dun->cent_n-1].x;
773
774                 /* Connect all the rooms together */
775                 for (i = 0; i < dun->cent_n; i++)
776                 {
777                         int j;
778
779                         /* Reset the arrays */
780                         dun->tunn_n = 0;
781                         dun->wall_n = 0;
782
783                         /* Connect the room to the previous room */
784                         if (randint1(current_floor_ptr->dun_level) > d_info[p_ptr->dungeon_idx].tunnel_percent)
785                         {
786                                 /* make cavelike tunnel */
787                                 (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
788                         }
789                         else
790                         {
791                                 /* make normal tunnel */
792                                 if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
793                         }
794
795                         if (tunnel_fail_count >= 2) return FALSE;
796
797                         /* Turn the tunnel into corridor */
798                         for (j = 0; j < dun->tunn_n; j++)
799                         {
800                                 grid_type *g_ptr;
801                                 feature_type *f_ptr;
802                                 y = dun->tunn[j].y;
803                                 x = dun->tunn[j].x;
804                                 g_ptr = &current_floor_ptr->grid_array[y][x];
805                                 f_ptr = &f_info[g_ptr->feat];
806
807                                 /* Clear previous contents (if not a lake), add a floor */
808                                 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
809                                 {
810                                         /* Clear mimic type */
811                                         g_ptr->mimic = 0;
812
813                                         place_floor_grid(g_ptr);
814                                 }
815                         }
816
817                         /* Apply the piercings that we found */
818                         for (j = 0; j < dun->wall_n; j++)
819                         {
820                                 grid_type *g_ptr;
821                                 y = dun->wall[j].y;
822                                 x = dun->wall[j].x;
823                                 g_ptr = &current_floor_ptr->grid_array[y][x];
824
825                                 /* Clear mimic type */
826                                 g_ptr->mimic = 0;
827
828                                 /* Clear previous contents, add up floor */
829                                 place_floor_grid(g_ptr);
830
831                                 /* Occasional doorway */
832                                 if ((randint0(100) < dun_tun_pen) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
833                                 {
834                                         /* Place a random door */
835                                         place_random_door(y, x, TRUE);
836                                 }
837                         }
838
839                         /* Remember the "previous" room */
840                         y = dun->cent[i].y;
841                         x = dun->cent[i].x;
842                 }
843
844                 /* Place intersection doors */
845                 for (i = 0; i < dun->door_n; i++)
846                 {
847                         /* Extract junction location */
848                         y = dun->door[i].y;
849                         x = dun->door[i].x;
850
851                         /* Try placing doors */
852                         try_door(y, x - 1);
853                         try_door(y, x + 1);
854                         try_door(y - 1, x);
855                         try_door(y + 1, x);
856                 }
857
858                 /* Place 3 or 4 down stairs near some walls */
859                 if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;
860
861                 /* Place 1 or 2 up stairs near some walls */
862                 if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
863         }
864
865         if (!dun->laketype)
866         {
867                 if (d_info[p_ptr->dungeon_idx].stream2)
868                 {
869                         /* Hack -- Add some quartz streamers */
870                         for (i = 0; i < DUN_STR_QUA; i++)
871                         {
872                                 build_streamer(d_info[p_ptr->dungeon_idx].stream2, DUN_STR_QC);
873                         }
874                 }
875
876                 if (d_info[p_ptr->dungeon_idx].stream1)
877                 {
878                         /* Hack -- Add some magma streamers */
879                         for (i = 0; i < DUN_STR_MAG; i++)
880                         {
881                                 build_streamer(d_info[p_ptr->dungeon_idx].stream1, DUN_STR_MC);
882                         }
883                 }
884         }
885
886         /* Special boundary walls -- Top and bottom */
887         for (x = 0; x < current_floor_ptr->width; x++)
888         {
889                 place_bound_perm_wall(&current_floor_ptr->grid_array[0][x]);
890                 place_bound_perm_wall(&current_floor_ptr->grid_array[current_floor_ptr->height - 1][x]);
891         }
892
893         /* Special boundary walls -- Left and right */
894         for (y = 1; y < (current_floor_ptr->height - 1); y++)
895         {
896                 place_bound_perm_wall(&current_floor_ptr->grid_array[y][0]);
897                 place_bound_perm_wall(&current_floor_ptr->grid_array[y][current_floor_ptr->width - 1]);
898         }
899
900         /* Determine the character location */
901         if (!new_player_spot()) return FALSE;
902
903         if (!place_quest_monsters()) return FALSE;
904
905         /* Basic "amount" */
906         k = (current_floor_ptr->dun_level / 3);
907         if (k > 10) k = 10;
908         if (k < 2) k = 2;
909
910         /* Pick a base number of monsters */
911         i = d_info[p_ptr->dungeon_idx].min_m_alloc_level;
912
913         /* To make small levels a bit more playable */
914         if (current_floor_ptr->height < MAX_HGT || current_floor_ptr->width < MAX_WID)
915         {
916                 int small_tester = i;
917
918                 i = (i * current_floor_ptr->height) / MAX_HGT;
919                 i = (i * current_floor_ptr->width) / MAX_WID;
920                 i += 1;
921
922                 if (i > small_tester) i = small_tester;
923                 else msg_format_wizard(CHEAT_DUNGEON,
924                         _("モンスター数基本値を %d から %d に減らします", "Reduced monsters base from %d to %d"), small_tester, i);
925
926         }
927
928         i += randint1(8);
929
930         /* Put some monsters in the dungeon */
931         for (i = i + k; i > 0; i--)
932         {
933                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
934         }
935
936         /* Place some traps in the dungeon */
937         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
938
939         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
940         if (!(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
941
942         /* Mega Hack -- No object at first level of deeper dungeon */
943         if (p_ptr->enter_dungeon && current_floor_ptr->dun_level > 1)
944         {
945                 /* No stair scum! */
946                 current_floor_ptr->object_level = 1;
947         }
948
949         /* Put some objects in rooms */
950         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
951
952         /* Put some objects/gold in the dungeon */
953         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
954         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
955
956         /* Set back to default */
957         current_floor_ptr->object_level = current_floor_ptr->base_level;
958
959         /* Put the Guardian */
960         if (!alloc_guardian(TRUE)) return FALSE;
961
962         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > current_floor_ptr->dun_level)) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
963         {
964                 /* Lite the current_floor_ptr->grid_array */
965                 for (y = 0; y < current_floor_ptr->height; y++)
966                 {
967                         for (x = 0; x < current_floor_ptr->width; x++)
968                         {
969                                 current_floor_ptr->grid_array[y][x].info |= (CAVE_GLOW);
970                         }
971                 }
972         }
973
974         return TRUE;
975 }
976
977 /*!
978  * @brief 闘技場用のアリーナ地形を作成する / Builds the arena after it is entered -KMW-
979  * @return なし
980  */
981 static void build_arena(void)
982 {
983         POSITION yval, y_height, y_depth, xval, x_left, x_right;
984         register int i, j;
985
986         yval = SCREEN_HGT / 2;
987         xval = SCREEN_WID / 2;
988         y_height = yval - 10;
989         y_depth = yval + 10;
990         x_left = xval - 32;
991         x_right = xval + 32;
992
993         for (i = y_height; i <= y_height + 5; i++)
994                 for (j = x_left; j <= x_right; j++)
995                 {
996                         place_extra_perm_bold(i, j);
997                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
998                 }
999         for (i = y_depth; i >= y_depth - 5; i--)
1000                 for (j = x_left; j <= x_right; j++)
1001                 {
1002                         place_extra_perm_bold(i, j);
1003                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1004                 }
1005         for (j = x_left; j <= x_left + 17; j++)
1006                 for (i = y_height; i <= y_depth; i++)
1007                 {
1008                         place_extra_perm_bold(i, j);
1009                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1010                 }
1011         for (j = x_right; j >= x_right - 17; j--)
1012                 for (i = y_height; i <= y_depth; i++)
1013                 {
1014                         place_extra_perm_bold(i, j);
1015                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1016                 }
1017
1018         place_extra_perm_bold(y_height+6, x_left+18);
1019         current_floor_ptr->grid_array[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1020         place_extra_perm_bold(y_depth-6, x_left+18);
1021         current_floor_ptr->grid_array[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1022         place_extra_perm_bold(y_height+6, x_right-18);
1023         current_floor_ptr->grid_array[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1024         place_extra_perm_bold(y_depth-6, x_right-18);
1025         current_floor_ptr->grid_array[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1026
1027         i = y_height + 5;
1028         j = xval;
1029         current_floor_ptr->grid_array[i][j].feat = f_tag_to_index("ARENA_GATE");
1030         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1031         player_place(i, j);
1032 }
1033
1034 /*!
1035  * @brief 挑戦時闘技場への入場処理 / Town logic flow for generation of arena -KMW-
1036  * @return なし
1037  */
1038 static void generate_challenge_arena(void)
1039 {
1040         POSITION y, x;
1041         POSITION qy = 0;
1042         POSITION qx = 0;
1043
1044         /* Smallest area */
1045         current_floor_ptr->height = SCREEN_HGT;
1046         current_floor_ptr->width = SCREEN_WID;
1047
1048         /* Start with solid walls */
1049         for (y = 0; y < MAX_HGT; y++)
1050         {
1051                 for (x = 0; x < MAX_WID; x++)
1052                 {
1053                         /* Create "solid" perma-wall */
1054                         place_solid_perm_bold(y, x);
1055
1056                         /* Illuminate and memorize the walls */
1057                         current_floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1058                 }
1059         }
1060
1061         /* Then place some floors */
1062         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1063         {
1064                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1065                 {
1066                         /* Create empty floor */
1067                         current_floor_ptr->grid_array[y][x].feat = feat_floor;
1068                 }
1069         }
1070
1071         build_arena();
1072
1073         if(!place_monster_aux(0, p_ptr->y + 5, p_ptr->x, arena_info[p_ptr->arena_number].r_idx, (PM_NO_KAGE | PM_NO_PET)))
1074         {
1075                 p_ptr->exit_bldg = TRUE;
1076                 p_ptr->arena_number++;
1077                 msg_print(_("相手は欠場した。あなたの不戦勝だ。", "The enemy is unable appear. You won by default."));
1078         }
1079
1080 }
1081
1082 /*!
1083  * @brief モンスター闘技場のフロア生成 / Builds the arena after it is entered -KMW-
1084  * @return なし
1085  */
1086 static void build_battle(void)
1087 {
1088         POSITION yval, y_height, y_depth, xval, x_left, x_right;
1089         register int i, j;
1090
1091         yval = SCREEN_HGT / 2;
1092         xval = SCREEN_WID / 2;
1093         y_height = yval - 10;
1094         y_depth = yval + 10;
1095         x_left = xval - 32;
1096         x_right = xval + 32;
1097
1098         for (i = y_height; i <= y_height + 5; i++)
1099                 for (j = x_left; j <= x_right; j++)
1100                 {
1101                         place_extra_perm_bold(i, j);
1102                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1103                 }
1104         for (i = y_depth; i >= y_depth - 3; i--)
1105                 for (j = x_left; j <= x_right; j++)
1106                 {
1107                         place_extra_perm_bold(i, j);
1108                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1109                 }
1110         for (j = x_left; j <= x_left + 17; j++)
1111                 for (i = y_height; i <= y_depth; i++)
1112                 {
1113                         place_extra_perm_bold(i, j);
1114                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1115                 }
1116         for (j = x_right; j >= x_right - 17; j--)
1117                 for (i = y_height; i <= y_depth; i++)
1118                 {
1119                         place_extra_perm_bold(i, j);
1120                         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1121                 }
1122
1123         place_extra_perm_bold(y_height+6, x_left+18);
1124         current_floor_ptr->grid_array[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1125         place_extra_perm_bold(y_depth-4, x_left+18);
1126         current_floor_ptr->grid_array[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1127         place_extra_perm_bold(y_height+6, x_right-18);
1128         current_floor_ptr->grid_array[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1129         place_extra_perm_bold(y_depth-4, x_right-18);
1130         current_floor_ptr->grid_array[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1131
1132         for (i = y_height + 1; i <= y_height + 5; i++)
1133                 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1134                 {
1135                         current_floor_ptr->grid_array[i][j].feat = feat_permanent_glass_wall;
1136                 }
1137
1138         i = y_height + 1;
1139         j = xval;
1140         current_floor_ptr->grid_array[i][j].feat = f_tag_to_index("BUILDING_3");
1141         current_floor_ptr->grid_array[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1142         player_place(i, j);
1143 }
1144
1145 /*!
1146  * @brief モンスター闘技場への導入処理 / Town logic flow for generation of arena -KMW-
1147  * @return なし
1148  */
1149 static void generate_gambling_arena(void)
1150 {
1151         POSITION y, x;
1152         MONSTER_IDX i;
1153         POSITION qy = 0;
1154         POSITION qx = 0;
1155
1156         /* Start with solid walls */
1157         for (y = 0; y < MAX_HGT; y++)
1158         {
1159                 for (x = 0; x < MAX_WID; x++)
1160                 {
1161                         /* Create "solid" perma-wall */
1162                         place_solid_perm_bold(y, x);
1163
1164                         /* Illuminate and memorize the walls */
1165                         current_floor_ptr->grid_array[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1166                 }
1167         }
1168
1169         /* Then place some floors */
1170         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1171         {
1172                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1173                 {
1174                         /* Create empty floor */
1175                         current_floor_ptr->grid_array[y][x].feat = feat_floor;
1176                 }
1177         }
1178
1179         build_battle();
1180
1181         for(i = 0; i < 4; i++)
1182         {
1183                 place_monster_aux(0, p_ptr->y + 8 + (i/2)*4, p_ptr->x - 2 + (i%2)*4, battle_mon[i], (PM_NO_KAGE | PM_NO_PET));
1184                 set_friendly(&current_floor_ptr->m_list[current_floor_ptr->grid_array[p_ptr->y+8+(i/2)*4][p_ptr->x-2+(i%2)*4].m_idx]);
1185         }
1186         for(i = 1; i < current_floor_ptr->m_max; i++)
1187         {
1188                 monster_type *m_ptr = &current_floor_ptr->m_list[i];
1189
1190                 if (!monster_is_valid(m_ptr)) continue;
1191
1192                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1193                 update_monster(i, FALSE);
1194         }
1195 }
1196
1197 /*!
1198  * @brief 固定マップクエストのフロア生成 / Generate a quest level
1199  * @return なし
1200  */
1201 static void generate_fixed_floor(void)
1202 {
1203         POSITION x, y;
1204
1205         /* Start with perm walls */
1206         for (y = 0; y < current_floor_ptr->height; y++)
1207         {
1208                 for (x = 0; x < current_floor_ptr->width; x++)
1209                 {
1210                         place_solid_perm_bold(y, x);
1211                 }
1212         }
1213
1214         /* Set the quest level */
1215         current_floor_ptr->base_level = quest[p_ptr->inside_quest].level;
1216         current_floor_ptr->dun_level = current_floor_ptr->base_level;
1217         current_floor_ptr->object_level = current_floor_ptr->base_level;
1218         current_floor_ptr->monster_level = current_floor_ptr->base_level;
1219
1220         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1221         get_mon_num_prep(get_monster_hook(), NULL);
1222
1223         init_flags = INIT_CREATE_DUNGEON;
1224
1225         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1226 }
1227
1228 /*!
1229  * @brief ダンジョン時のランダムフロア生成 / Make a real level
1230  * @return フロアの生成に成功したらTRUE
1231  */
1232 static bool level_gen(concptr *why)
1233 {
1234         int level_height, level_width;
1235
1236         if ((always_small_levels || ironman_small_levels ||
1237             (one_in_(SMALL_LEVEL) && small_levels) ||
1238              (d_info[p_ptr->dungeon_idx].flags1 & DF1_BEGINNER) ||
1239             (d_info[p_ptr->dungeon_idx].flags1 & DF1_SMALLEST)) &&
1240             !(d_info[p_ptr->dungeon_idx].flags1 & DF1_BIG))
1241         {
1242                 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_SMALLEST)
1243                 {
1244                         level_height = 1;
1245                         level_width = 1;
1246                 }
1247                 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_BEGINNER)
1248                 {
1249                         level_height = 2;
1250                         level_width = 2;
1251                 }
1252                 else
1253                 {
1254                         do
1255                         {
1256                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1257                                 level_width = randint1(MAX_WID/SCREEN_WID);
1258                         }
1259                         while ((level_height == MAX_HGT/SCREEN_HGT) && (level_width == MAX_WID/SCREEN_WID));
1260                 }
1261
1262                 current_floor_ptr->height = level_height * SCREEN_HGT;
1263                 current_floor_ptr->width = level_width * SCREEN_WID;
1264
1265                 /* Assume illegal panel */
1266                 panel_row_min = current_floor_ptr->height;
1267                 panel_col_min = current_floor_ptr->width;
1268
1269                 msg_format_wizard(CHEAT_DUNGEON,
1270                         _("小さなフロア: X:%d, Y:%d", "A 'small' dungeon level: X:%d, Y:%d."),
1271                         current_floor_ptr->width, current_floor_ptr->height);
1272         }
1273         else
1274         {
1275                 /* Big dungeon */
1276                 current_floor_ptr->height = MAX_HGT;
1277                 current_floor_ptr->width = MAX_WID;
1278
1279                 /* Assume illegal panel */
1280                 panel_row_min = current_floor_ptr->height;
1281                 panel_col_min = current_floor_ptr->width;
1282         }
1283
1284         /* Make a dungeon */
1285         if (!cave_gen())
1286         {
1287                 *why = _("ダンジョン生成に失敗", "could not place player");
1288                 return FALSE;
1289         }
1290         else return TRUE;
1291 }
1292
1293 /*!
1294  * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after current_floor_ptr->grid_array generation
1295  * @return なし
1296  */
1297 void wipe_generate_random_floor_flags(void)
1298 {
1299         POSITION x, y;
1300
1301         for (y = 0; y < current_floor_ptr->height; y++)
1302         {
1303                 for (x = 0; x < current_floor_ptr->width; x++)
1304                 {
1305                         /* Wipe unused flags */
1306                         current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
1307                 }
1308         }
1309
1310         if (current_floor_ptr->dun_level)
1311         {
1312                 for (y = 1; y < current_floor_ptr->height - 1; y++)
1313                 {
1314                         for (x = 1; x < current_floor_ptr->width - 1; x++)
1315                         {
1316                                 /* There might be trap */
1317                                 current_floor_ptr->grid_array[y][x].info |= CAVE_UNSAFE;
1318                         }
1319                 }
1320         }
1321 }
1322
1323 /*!
1324  * @brief フロアの全情報を初期化する / Clear and empty the current_floor_ptr->grid_array
1325  * @return なし
1326  */
1327 void clear_cave(void)
1328 {
1329         POSITION x, y;
1330         int i;
1331
1332         /* Very simplified version of wipe_o_list() */
1333         (void)C_WIPE(current_floor_ptr->o_list, current_floor_ptr->o_max, object_type);
1334         current_floor_ptr->o_max = 1;
1335         current_floor_ptr->o_cnt = 0;
1336
1337         /* Very simplified version of wipe_m_list() */
1338         for (i = 1; i < max_r_idx; i++)
1339                 r_info[i].cur_num = 0;
1340         (void)C_WIPE(current_floor_ptr->m_list, current_floor_ptr->m_max, monster_type);
1341         current_floor_ptr->m_max = 1;
1342         current_floor_ptr->m_cnt = 0;
1343         for (i = 0; i < MAX_MTIMED; i++) current_floor_ptr->mproc_max[i] = 0;
1344
1345         /* Pre-calc cur_num of pets in party_mon[] */
1346         precalc_cur_num_of_pet();
1347
1348
1349         /* Start with a blank current_floor_ptr->grid_array */
1350         for (y = 0; y < MAX_HGT; y++)
1351         {
1352                 for (x = 0; x < MAX_WID; x++)
1353                 {
1354                         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1355                         g_ptr->info = 0;
1356                         g_ptr->feat = 0;
1357                         g_ptr->o_idx = 0;
1358                         g_ptr->m_idx = 0;
1359                         g_ptr->special = 0;
1360                         g_ptr->mimic = 0;
1361                         g_ptr->cost = 0;
1362                         g_ptr->dist = 0;
1363                         g_ptr->when = 0;
1364                 }
1365         }
1366
1367         /* Mega-Hack -- no player yet */
1368         p_ptr->x = p_ptr->y = 0;
1369
1370         /* Set the base level */
1371         current_floor_ptr->base_level = current_floor_ptr->dun_level;
1372
1373         /* Reset the monster generation level */
1374         current_floor_ptr->monster_level = current_floor_ptr->base_level;
1375
1376         /* Reset the object generation level */
1377         current_floor_ptr->object_level = current_floor_ptr->base_level;
1378 }
1379
1380
1381 /*!
1382  * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
1383  * @return なし
1384  * @note Hack -- regenerate any "overflow" levels
1385  */
1386 void generate_random_floor(void)
1387 {
1388         int num;
1389
1390         /* Fill the arrays of floors and walls in the good proportions */
1391         set_floor_and_wall(p_ptr->dungeon_idx);
1392
1393         /* Generate */
1394         for (num = 0; TRUE; num++)
1395         {
1396                 bool okay = TRUE;
1397
1398                 concptr why = NULL;
1399
1400                 /* Clear and empty the current_floor_ptr->grid_array */
1401                 clear_cave();
1402
1403                 /* Build the arena -KMW- */
1404                 if (p_ptr->inside_arena)
1405                 {
1406                         /* Small arena */
1407                         generate_challenge_arena();
1408                 }
1409
1410                 /* Build the battle -KMW- */
1411                 else if (p_ptr->inside_battle)
1412                 {
1413                         /* Small arena */
1414                         generate_gambling_arena();
1415                 }
1416
1417                 else if (p_ptr->inside_quest)
1418                 {
1419                         generate_fixed_floor();
1420                 }
1421
1422                 /* Build the town */
1423                 else if (!current_floor_ptr->dun_level)
1424                 {
1425                         /* Make the wilderness */
1426                         if (p_ptr->wild_mode) wilderness_gen_small();
1427                         else wilderness_gen();
1428                 }
1429
1430                 /* Build a real level */
1431                 else
1432                 {
1433                         okay = level_gen(&why);
1434                 }
1435
1436
1437                 /* Prevent object over-flow */
1438                 if (current_floor_ptr->o_max >= current_floor_ptr->max_o_idx)
1439                 {
1440                         why = _("アイテムが多すぎる", "too many objects");
1441                         okay = FALSE;
1442                 }
1443                 /* Prevent monster over-flow */
1444                 else if (current_floor_ptr->m_max >= current_floor_ptr->max_m_idx)
1445                 {
1446                         why = _("モンスターが多すぎる", "too many monsters");
1447                         okay = FALSE;
1448                 }
1449
1450                 /* Accept */
1451                 if (okay) break;
1452
1453                 if (why) msg_format(_("生成やり直し(%s)", "Generation restarted (%s)"), why);
1454
1455                 wipe_o_list();
1456                 wipe_m_list();
1457         }
1458
1459         /* Glow deep lava and building entrances */
1460         glow_deep_lava_and_bldg();
1461
1462         /* Reset flag */
1463         p_ptr->enter_dungeon = FALSE;
1464
1465         wipe_generate_random_floor_flags();
1466 }
1467
1468 /*!
1469  * @brief build_tunnel用に通路を掘るための方向をランダムに決める / Pick a random direction
1470  * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
1471  * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
1472  * @return なし
1473  */
1474 static void rand_dir(POSITION *rdir, POSITION *cdir)
1475 {
1476         /* Pick a random direction */
1477         int i = randint0(4);
1478
1479         /* Extract the dy/dx components */
1480         *rdir = ddy_ddd[i];
1481         *cdir = ddx_ddd[i];
1482 }
1483
1484 /*!
1485  * @brief build_tunnel用に通路を掘るための方向を位置関係通りに決める / Always picks a correct direction
1486  * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
1487  * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
1488  * @param y1 始点Y座標
1489  * @param x1 始点X座標
1490  * @param y2 終点Y座標
1491  * @param x2 終点X座標
1492  * @return なし
1493  */
1494 static void correct_dir(POSITION *rdir, POSITION *cdir, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
1495 {
1496         /* Extract vertical and horizontal directions */
1497         *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
1498         *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
1499
1500         /* Never move diagonally */
1501         if (*rdir && *cdir)
1502         {
1503                 if (randint0(100) < 50)
1504                         *rdir = 0;
1505                 else
1506                         *cdir = 0;
1507         }
1508 }
1509
1510 /*!
1511 * @brief 部屋間のトンネルを生成する / Constructs a tunnel between two points
1512 * @param row1 始点Y座標
1513 * @param col1 始点X座標
1514 * @param row2 終点Y座標
1515 * @param col2 終点X座標
1516 * @return 生成に成功したらTRUEを返す
1517 * @details
1518 * This function must be called BEFORE any streamers are created,\n
1519 * since we use the special "granite wall" sub-types to keep track\n
1520 * of legal places for corridors to pierce rooms.\n
1521 *\n
1522 * We use "door_flag" to prevent excessive construction of doors\n
1523 * along overlapping corridors.\n
1524 *\n
1525 * We queue the tunnel grids to prevent door creation along a corridor\n
1526 * which intersects itself.\n
1527 *\n
1528 * We queue the wall piercing grids to prevent a corridor from leaving\n
1529 * a room and then coming back in through the same entrance.\n
1530 *\n
1531 * We "pierce" grids which are "outer" walls of rooms, and when we\n
1532 * do so, we change all adjacent "outer" walls of rooms into "solid"\n
1533 * walls so that no two corridors may use adjacent grids for exits.\n
1534 *\n
1535 * The "solid" wall check prevents corridors from "chopping" the\n
1536 * corners of rooms off, as well as "silly" door placement, and\n
1537 * "excessively wide" room entrances.\n
1538 *\n
1539 * Kind of walls:\n
1540 *   extra -- walls\n
1541 *   inner -- inner room walls\n
1542 *   outer -- outer room walls\n
1543 *   solid -- solid room walls\n
1544 */
1545 bool build_tunnel(POSITION row1, POSITION col1, POSITION row2, POSITION col2)
1546 {
1547         POSITION y, x;
1548         POSITION tmp_row, tmp_col;
1549         POSITION row_dir, col_dir;
1550         POSITION start_row, start_col;
1551         int main_loop_count = 0;
1552
1553         bool door_flag = FALSE;
1554
1555         grid_type *g_ptr;
1556
1557         /* Save the starting location */
1558         start_row = row1;
1559         start_col = col1;
1560
1561         /* Start out in the correct direction */
1562         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1563
1564         /* Keep going until done (or bored) */
1565         while ((row1 != row2) || (col1 != col2))
1566         {
1567                 /* Mega-Hack -- Paranoia -- prevent infinite loops */
1568                 if (main_loop_count++ > 2000) return FALSE;
1569
1570                 /* Allow bends in the tunnel */
1571                 if (randint0(100) < dun_tun_chg)
1572                 {
1573                         /* Acquire the correct direction */
1574                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1575
1576                         /* Random direction */
1577                         if (randint0(100) < dun_tun_rnd)
1578                         {
1579                                 rand_dir(&row_dir, &col_dir);
1580                         }
1581                 }
1582
1583                 /* Get the next location */
1584                 tmp_row = row1 + row_dir;
1585                 tmp_col = col1 + col_dir;
1586
1587
1588                 /* Extremely Important -- do not leave the dungeon */
1589                 while (!in_bounds(tmp_row, tmp_col))
1590                 {
1591                         /* Acquire the correct direction */
1592                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
1593
1594                         /* Random direction */
1595                         if (randint0(100) < dun_tun_rnd)
1596                         {
1597                                 rand_dir(&row_dir, &col_dir);
1598                         }
1599
1600                         /* Get the next location */
1601                         tmp_row = row1 + row_dir;
1602                         tmp_col = col1 + col_dir;
1603                 }
1604
1605                 g_ptr = &current_floor_ptr->grid_array[tmp_row][tmp_col];
1606
1607                 /* Avoid "solid" walls */
1608                 if (is_solid_grid(g_ptr)) continue;
1609
1610                 /* Pierce "outer" walls of rooms */
1611                 if (is_outer_grid(g_ptr))
1612                 {
1613                         /* Acquire the "next" location */
1614                         y = tmp_row + row_dir;
1615                         x = tmp_col + col_dir;
1616
1617                         /* Hack -- Avoid outer/solid walls */
1618                         if (is_outer_bold(y, x)) continue;
1619                         if (is_solid_bold(y, x)) continue;
1620
1621                         /* Accept this location */
1622                         row1 = tmp_row;
1623                         col1 = tmp_col;
1624
1625                         /* Save the wall location */
1626                         if (dun->wall_n < WALL_MAX)
1627                         {
1628                                 dun->wall[dun->wall_n].y = row1;
1629                                 dun->wall[dun->wall_n].x = col1;
1630                                 dun->wall_n++;
1631                         }
1632                         else return FALSE;
1633
1634                         /* Forbid re-entry near this piercing */
1635                         for (y = row1 - 1; y <= row1 + 1; y++)
1636                         {
1637                                 for (x = col1 - 1; x <= col1 + 1; x++)
1638                                 {
1639                                         /* Convert adjacent "outer" walls as "solid" walls */
1640                                         if (is_outer_bold(y, x))
1641                                         {
1642                                                 /* Change the wall to a "solid" wall */
1643                                                 place_solid_noperm_bold(y, x);
1644                                         }
1645                                 }
1646                         }
1647                 }
1648
1649                 /* Travel quickly through rooms */
1650                 else if (g_ptr->info & (CAVE_ROOM))
1651                 {
1652                         /* Accept the location */
1653                         row1 = tmp_row;
1654                         col1 = tmp_col;
1655                 }
1656
1657                 /* Tunnel through all other walls */
1658                 else if (is_extra_grid(g_ptr) || is_inner_grid(g_ptr) || is_solid_grid(g_ptr))
1659                 {
1660                         /* Accept this location */
1661                         row1 = tmp_row;
1662                         col1 = tmp_col;
1663
1664                         /* Save the tunnel location */
1665                         if (dun->tunn_n < TUNN_MAX)
1666                         {
1667                                 dun->tunn[dun->tunn_n].y = row1;
1668                                 dun->tunn[dun->tunn_n].x = col1;
1669                                 dun->tunn_n++;
1670                         }
1671                         else return FALSE;
1672
1673                         /* Allow door in next grid */
1674                         door_flag = FALSE;
1675                 }
1676
1677                 /* Handle corridor intersections or overlaps */
1678                 else
1679                 {
1680                         /* Accept the location */
1681                         row1 = tmp_row;
1682                         col1 = tmp_col;
1683
1684                         /* Collect legal door locations */
1685                         if (!door_flag)
1686                         {
1687                                 /* Save the door location */
1688                                 if (dun->door_n < DOOR_MAX)
1689                                 {
1690                                         dun->door[dun->door_n].y = row1;
1691                                         dun->door[dun->door_n].x = col1;
1692                                         dun->door_n++;
1693                                 }
1694                                 else return FALSE;
1695
1696                                 /* No door in next grid */
1697                                 door_flag = TRUE;
1698                         }
1699
1700                         /* Hack -- allow pre-emptive tunnel termination */
1701                         if (randint0(100) >= dun_tun_con)
1702                         {
1703                                 /* Distance between row1 and start_row */
1704                                 tmp_row = row1 - start_row;
1705                                 if (tmp_row < 0) tmp_row = (-tmp_row);
1706
1707                                 /* Distance between col1 and start_col */
1708                                 tmp_col = col1 - start_col;
1709                                 if (tmp_col < 0) tmp_col = (-tmp_col);
1710
1711                                 /* Terminate the tunnel */
1712                                 if ((tmp_row > 10) || (tmp_col > 10)) break;
1713                         }
1714                 }
1715         }
1716
1717         return TRUE;
1718 }
1719
1720
1721 /*!
1722 * @brief トンネル生成のための基準点を指定する。
1723 * @param x 基準点を指定するX座標の参照ポインタ、適時値が修正される。
1724 * @param y 基準点を指定するY座標の参照ポインタ、適時値が修正される。
1725 * @param affectwall (調査中)
1726 * @return なし
1727 * @details
1728 * This routine adds the square to the tunnel\n
1729 * It also checks for SOLID walls - and returns a nearby\n
1730 * non-SOLID square in (x,y) so that a simple avoiding\n
1731 * routine can be used. The returned boolean value reflects\n
1732 * whether or not this routine hit a SOLID wall.\n
1733 *\n
1734 * "affectwall" toggles whether or not this new square affects\n
1735 * the boundaries of rooms. - This is used by the catacomb\n
1736 * routine.\n
1737 * @todo 特に詳細な処理の意味を調査すべし
1738 */
1739 static bool set_tunnel(POSITION *x, POSITION *y, bool affectwall)
1740 {
1741         int i, j, dx, dy;
1742
1743         grid_type *g_ptr = &current_floor_ptr->grid_array[*y][*x];
1744
1745         if (!in_bounds(*y, *x)) return TRUE;
1746
1747         if (is_inner_grid(g_ptr))
1748         {
1749                 return TRUE;
1750         }
1751
1752         if (is_extra_bold(*y, *x))
1753         {
1754                 /* Save the tunnel location */
1755                 if (dun->tunn_n < TUNN_MAX)
1756                 {
1757                         dun->tunn[dun->tunn_n].y = *y;
1758                         dun->tunn[dun->tunn_n].x = *x;
1759                         dun->tunn_n++;
1760
1761                         return TRUE;
1762                 }
1763                 else return FALSE;
1764         }
1765
1766         if (is_floor_bold(*y, *x))
1767         {
1768                 /* Don't do anything */
1769                 return TRUE;
1770         }
1771
1772         if (is_outer_grid(g_ptr) && affectwall)
1773         {
1774                 /* Save the wall location */
1775                 if (dun->wall_n < WALL_MAX)
1776                 {
1777                         dun->wall[dun->wall_n].y = *y;
1778                         dun->wall[dun->wall_n].x = *x;
1779                         dun->wall_n++;
1780                 }
1781                 else return FALSE;
1782
1783                 /* Forbid re-entry near this piercing */
1784                 for (j = *y - 1; j <= *y + 1; j++)
1785                 {
1786                         for (i = *x - 1; i <= *x + 1; i++)
1787                         {
1788                                 /* Convert adjacent "outer" walls as "solid" walls */
1789                                 if (is_outer_bold(j, i))
1790                                 {
1791                                         /* Change the wall to a "solid" wall */
1792                                         place_solid_noperm_bold(j, i);
1793                                 }
1794                         }
1795                 }
1796
1797                 /* Clear mimic type */
1798                 current_floor_ptr->grid_array[*y][*x].mimic = 0;
1799
1800                 place_floor_bold(*y, *x);
1801
1802                 return TRUE;
1803         }
1804
1805         if (is_solid_grid(g_ptr) && affectwall)
1806         {
1807                 /* cannot place tunnel here - use a square to the side */
1808
1809                 /* find usable square and return value in (x,y) */
1810
1811                 i = 50;
1812
1813                 dy = 0;
1814                 dx = 0;
1815                 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
1816                 {
1817                         dy = randint0(3) - 1;
1818                         dx = randint0(3) - 1;
1819
1820                         if (!in_bounds(*y + dy, *x + dx))
1821                         {
1822                                 dx = 0;
1823                                 dy = 0;
1824                         }
1825
1826                         i--;
1827                 }
1828
1829                 if (i == 0)
1830                 {
1831                         /* Failed for some reason: hack - ignore the solidness */
1832                         place_outer_grid(g_ptr);
1833                         dx = 0;
1834                         dy = 0;
1835                 }
1836
1837                 /* Give new, acceptable coordinate. */
1838                 *x = *x + dx;
1839                 *y = *y + dy;
1840
1841                 return FALSE;
1842         }
1843
1844         return TRUE;
1845 }
1846
1847
1848 /*!
1849 * @brief 外壁を削って「カタコンベ状」の通路を作成する / This routine creates the catacomb-like tunnels by removing extra rock.
1850 * @param x 基準点のX座標
1851 * @param y 基準点のY座標
1852 * @return なし
1853 * @details
1854 * Note that this routine is only called on "even" squares - so it gives
1855 * a natural checkerboard pattern.
1856 */
1857 static void create_cata_tunnel(POSITION x, POSITION y)
1858 {
1859         POSITION x1, y1;
1860
1861         /* Build tunnel */
1862         x1 = x - 1;
1863         y1 = y;
1864         set_tunnel(&x1, &y1, FALSE);
1865
1866         x1 = x + 1;
1867         y1 = y;
1868         set_tunnel(&x1, &y1, FALSE);
1869
1870         x1 = x;
1871         y1 = y - 1;
1872         set_tunnel(&x1, &y1, FALSE);
1873
1874         x1 = x;
1875         y1 = y + 1;
1876         set_tunnel(&x1, &y1, FALSE);
1877 }
1878
1879
1880 /*!
1881 * @brief トンネル生成処理(詳細調査中)/ This routine does the bulk of the work in creating the new types of tunnels.
1882 * @return なし
1883 * @todo 詳細用調査
1884 * @details
1885 * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)\n
1886 * It doesn't need to add any complexity - straight lines are fine.\n
1887 * The SOLID walls are avoided by a recursive algorithm which tries random ways\n
1888 * around the obstical until it works.  The number of itterations is counted, and it\n
1889 * this gets too large the routine exits. This should stop any crashes - but may leave\n
1890 * small gaps in the tunnel where there are too many SOLID walls.\n
1891 *\n
1892 * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used\n
1893 * as a part of the dodge SOLID walls algorithm.\n
1894 *\n
1895 * Type 2 tunnels are made of two straight lines at right angles. When this is used with\n
1896 * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.\n
1897 *\n
1898 * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.\n
1899 * This, when used with longer line segments gives the "catacomb-like" tunnels seen near\n
1900 * the surface.\n
1901 */
1902 static void short_seg_hack(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int count, bool *fail)
1903 {
1904         int i;
1905         POSITION x, y;
1906         int length;
1907
1908         /* Check for early exit */
1909         if (!(*fail)) return;
1910
1911         length = distance(x1, y1, x2, y2);
1912
1913         count++;
1914
1915         if ((type == 1) && (length != 0))
1916         {
1917
1918                 for (i = 0; i <= length; i++)
1919                 {
1920                         x = x1 + i * (x2 - x1) / length;
1921                         y = y1 + i * (y2 - y1) / length;
1922                         if (!set_tunnel(&x, &y, TRUE))
1923                         {
1924                                 if (count > 50)
1925                                 {
1926                                         /* This isn't working - probably have an infinite loop */
1927                                         *fail = FALSE;
1928                                         return;
1929                                 }
1930
1931                                 /* solid wall - so try to go around */
1932                                 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
1933                                 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
1934                         }
1935                 }
1936         }
1937         else if ((type == 2) || (type == 3))
1938         {
1939                 if (x1 < x2)
1940                 {
1941                         for (i = x1; i <= x2; i++)
1942                         {
1943                                 x = i;
1944                                 y = y1;
1945                                 if (!set_tunnel(&x, &y, TRUE))
1946                                 {
1947                                         /* solid wall - so try to go around */
1948                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1949                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1950                                 }
1951                                 if ((type == 3) && ((x + y) % 2))
1952                                 {
1953                                         create_cata_tunnel(i, y1);
1954                                 }
1955                         }
1956                 }
1957                 else
1958                 {
1959                         for (i = x2; i <= x1; i++)
1960                         {
1961                                 x = i;
1962                                 y = y1;
1963                                 if (!set_tunnel(&x, &y, TRUE))
1964                                 {
1965                                         /* solid wall - so try to go around */
1966                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1967                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1968                                 }
1969                                 if ((type == 3) && ((x + y) % 2))
1970                                 {
1971                                         create_cata_tunnel(i, y1);
1972                                 }
1973                         }
1974
1975                 }
1976                 if (y1 < y2)
1977                 {
1978                         for (i = y1; i <= y2; i++)
1979                         {
1980                                 x = x2;
1981                                 y = i;
1982                                 if (!set_tunnel(&x, &y, TRUE))
1983                                 {
1984                                         /* solid wall - so try to go around */
1985                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1986                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1987                                 }
1988                                 if ((type == 3) && ((x + y) % 2))
1989                                 {
1990                                         create_cata_tunnel(x2, i);
1991                                 }
1992                         }
1993                 }
1994                 else
1995                 {
1996                         for (i = y2; i <= y1; i++)
1997                         {
1998                                 x = x2;
1999                                 y = i;
2000                                 if (!set_tunnel(&x, &y, TRUE))
2001                                 {
2002                                         /* solid wall - so try to go around */
2003                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
2004                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
2005                                 }
2006                                 if ((type == 3) && ((x + y) % 2))
2007                                 {
2008                                         create_cata_tunnel(x2, i);
2009                                 }
2010                         }
2011                 }
2012         }
2013 }
2014
2015
2016 /*!
2017 * @brief 特定の壁(永久壁など)を避けながら部屋間の通路を作成する / This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
2018 * @return なし
2019 * @todo 詳細要調査
2020 * @details
2021 * Permanent rock is ignored in this path finding- sometimes there is no\n
2022 * path around anyway -so there will be a crash if we try to find one.\n
2023 * This routine is much like the river creation routine in Zangband.\n
2024 * It works by dividing a line segment into two.  The segments are divided\n
2025 * until they are less than "cutoff" - when the corresponding routine from\n
2026 * "short_seg_hack" is called.\n
2027 * Note it is VERY important that the "stop if hit another passage" logic\n
2028 * stays as is.  Without this the dungeon turns into Swiss Cheese...\n
2029 */
2030 bool build_tunnel2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int cutoff)
2031 {
2032         POSITION x3, y3, dx, dy;
2033         POSITION changex, changey;
2034         int length;
2035         int i;
2036         bool retval, firstsuccede;
2037         grid_type *g_ptr;
2038
2039         length = distance(x1, y1, x2, y2);
2040
2041         if (length > cutoff)
2042         {
2043                 /*
2044                 * Divide path in half and call routine twice.
2045                 */
2046                 dx = (x2 - x1) / 2;
2047                 dy = (y2 - y1) / 2;
2048
2049                 /* perturbation perpendicular to path */
2050                 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
2051                 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
2052
2053                 /* Work out "mid" ponit */
2054                 x3 = x1 + dx + changex;
2055                 y3 = y1 + dy + changey;
2056
2057                 /* See if in bounds - if not - do not perturb point */
2058                 if (!in_bounds(y3, x3))
2059                 {
2060                         x3 = (x1 + x2) / 2;
2061                         y3 = (y1 + y2) / 2;
2062                 }
2063                 /* cache g_ptr */
2064                 g_ptr = &current_floor_ptr->grid_array[y3][x3];
2065                 if (is_solid_grid(g_ptr))
2066                 {
2067                         /* move midpoint a bit to avoid problem. */
2068
2069                         i = 50;
2070
2071                         dy = 0;
2072                         dx = 0;
2073                         while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
2074                         {
2075                                 dy = randint0(3) - 1;
2076                                 dx = randint0(3) - 1;
2077                                 if (!in_bounds(y3 + dy, x3 + dx))
2078                                 {
2079                                         dx = 0;
2080                                         dy = 0;
2081                                 }
2082                                 i--;
2083                         }
2084
2085                         if (i == 0)
2086                         {
2087                                 /* Failed for some reason: hack - ignore the solidness */
2088                                 place_outer_bold(y3, x3);
2089                                 dx = 0;
2090                                 dy = 0;
2091                         }
2092                         y3 += dy;
2093                         x3 += dx;
2094                         g_ptr = &current_floor_ptr->grid_array[y3][x3];
2095                 }
2096
2097                 if (is_floor_grid(g_ptr))
2098                 {
2099                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
2100                         {
2101                                 if ((current_floor_ptr->grid_array[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
2102                                 {
2103                                         /* do second half only if works + if have hit a room */
2104                                         retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
2105                                 }
2106                                 else
2107                                 {
2108                                         /* have hit another tunnel - make a set of doors here */
2109                                         retval = FALSE;
2110
2111                                         /* Save the door location */
2112                                         if (dun->door_n < DOOR_MAX)
2113                                         {
2114                                                 dun->door[dun->door_n].y = y3;
2115                                                 dun->door[dun->door_n].x = x3;
2116                                                 dun->door_n++;
2117                                         }
2118                                         else return FALSE;
2119                                 }
2120                                 firstsuccede = TRUE;
2121                         }
2122                         else
2123                         {
2124                                 /* false- didn't work all the way */
2125                                 retval = FALSE;
2126                                 firstsuccede = FALSE;
2127                         }
2128                 }
2129                 else
2130                 {
2131                         /* tunnel through walls */
2132                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
2133                         {
2134                                 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
2135                                 firstsuccede = TRUE;
2136                         }
2137                         else
2138                         {
2139                                 /* false- didn't work all the way */
2140                                 retval = FALSE;
2141                                 firstsuccede = FALSE;
2142                         }
2143                 }
2144                 if (firstsuccede)
2145                 {
2146                         /* only do this if the first half has worked */
2147                         set_tunnel(&x3, &y3, TRUE);
2148                 }
2149                 /* return value calculated above */
2150                 return retval;
2151         }
2152         else
2153         {
2154                 /* Do a short segment */
2155                 retval = TRUE;
2156                 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
2157
2158                 /* Hack - ignore return value so avoid infinite loops */
2159                 return TRUE;
2160         }
2161 }
2162