OSDN Git Service

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