OSDN Git Service

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