OSDN Git Service

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