OSDN Git Service

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