OSDN Git Service

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