OSDN Git Service

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