OSDN Git Service

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