OSDN Git Service

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