OSDN Git Service

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