OSDN Git Service

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