OSDN Git Service

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