OSDN Git Service

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