OSDN Git Service

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