OSDN Git Service

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