OSDN Git Service

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