OSDN Git Service

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