OSDN Git Service

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