OSDN Git Service

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