OSDN Git Service

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