OSDN Git Service

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