OSDN Git Service

#37287 #37353 (2.2.0.89) 型の置換を継続中。 / Ongoing type replacement.
[hengband/hengband.git] / src / 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  * XXX XXX XXX 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  * XXX XXX XXX 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 cave 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.  XXX XXX XXX\n
97  */
98
99 #include "angband.h"
100 #include "generate.h"
101 #include "grid.h"
102 #include "rooms.h"
103 #include "streams.h"
104
105 int dun_tun_rnd; 
106 int dun_tun_chg;
107 int dun_tun_con;
108 int dun_tun_pen;
109 int dun_tun_jct;
110
111
112 /*!
113  * Dungeon generation data -- see "cave_gen()"
114  */
115 dun_data *dun;
116
117
118 /*!
119  * @brief 上下左右の外壁数をカウントする / Count the number of walls adjacent to the given grid.
120  * @param y 基準のy座標
121  * @param x 基準のx座標
122  * @return 隣接する外壁の数
123  * @note Assumes "in_bounds(y, x)"
124  * @details We count only granite walls and permanent walls.
125  */
126 static int next_to_walls(int y, int x)
127 {
128         int k = 0;
129
130         if (in_bounds(y + 1, x) && is_extra_bold(y + 1, x)) k++;
131         if (in_bounds(y - 1, x) && is_extra_bold(y - 1, x)) k++;
132         if (in_bounds(y, x + 1) && is_extra_bold(y, x + 1)) k++;
133         if (in_bounds(y, x - 1) && is_extra_bold(y, x - 1)) k++;
134
135         return (k);
136 }
137
138 /*!
139  * @brief alloc_stairs()の補助として指定の位置に階段を生成できるかの判定を行う / Helper function for alloc_stairs(). Is this a good location for stairs?
140  * @param y 基準のy座標
141  * @param x 基準のx座標
142  * @param walls 最低減隣接させたい外壁の数
143  * @return 階段を生成して問題がないならばTRUEを返す。
144  */
145 static bool alloc_stairs_aux(int y, int x, int walls)
146 {
147         /* Access the grid */
148         cave_type *c_ptr = &cave[y][x];
149
150         /* Require "naked" floor grid */
151         if (!is_floor_grid(c_ptr)) return FALSE;
152         if (pattern_tile(y, x)) return FALSE;
153         if (c_ptr->o_idx || c_ptr->m_idx) return FALSE;
154
155         /* Require a certain number of adjacent walls */
156         if (next_to_walls(y, x) < walls) return FALSE;
157
158         return TRUE;
159 }
160
161
162 /*!
163  * @brief 外壁に隣接させて階段を生成する / Places some staircases near walls
164  * @param feat 配置したい地形ID
165  * @param num 配置したい階段の数
166  * @param walls 最低減隣接させたい外壁の数
167  * @return 規定数通りに生成に成功したらTRUEを返す。
168  */
169 static bool alloc_stairs(IDX feat, int num, int walls)
170 {
171         int i;
172         int shaft_num = 0;
173
174         feature_type *f_ptr = &f_info[feat];
175
176         if (have_flag(f_ptr->flags, FF_LESS))
177         {
178                 /* No up stairs in town or in ironman mode */
179                 if (ironman_downward || !dun_level) return TRUE;
180
181                 if (dun_level > d_info[dungeon_type].mindepth)
182                         shaft_num = (randint1(num+1))/2;
183         }
184         else if (have_flag(f_ptr->flags, FF_MORE))
185         {
186                 int q_idx = quest_number(dun_level);
187
188                 /* No downstairs on quest levels */
189                 if (dun_level > 1 && q_idx)
190                 {
191                         monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
192
193                         /* The quest monster(s) is still alive? */
194                         if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
195                                 return TRUE;
196                 }
197
198                 /* No downstairs at the bottom */
199                 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
200
201                 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
202                         shaft_num = (randint1(num)+1)/2;
203         }
204
205         /* Paranoia */
206         else return FALSE;
207
208
209         /* Place "num" stairs */
210         for (i = 0; i < num; i++)
211         {
212                 while (TRUE)
213                 {
214                         int y = 0, x = 0;
215                         cave_type *c_ptr;
216
217                         int candidates = 0;
218                         int pick;
219
220                         for (y = 1; y < cur_hgt - 1; y++)
221                         {
222                                 for (x = 1; x < cur_wid - 1; x++)
223                                 {
224                                         if (alloc_stairs_aux(y, x, walls))
225                                         {
226                                                 /* A valid space found */
227                                                 candidates++;
228                                         }
229                                 }
230                         }
231
232                         /* No valid place! */
233                         if (!candidates)
234                         {
235                                 /* There are exactly no place! */
236                                 if (walls <= 0) return FALSE;
237
238                                 /* Decrease walls limit, and try again */
239                                 walls--;
240                                 continue;
241                         }
242
243                         /* Choose a random one */
244                         pick = randint1(candidates);
245
246                         for (y = 1; y < cur_hgt - 1; y++)
247                         {
248                                 for (x = 1; x < cur_wid - 1; x++)
249                                 {
250                                         if (alloc_stairs_aux(y, x, walls))
251                                         {
252                                                 pick--;
253
254                                                 /* Is this a picked one? */
255                                                 if (!pick) break;
256                                         }
257                                 }
258
259                                 if (!pick) break;
260                         }
261
262                         /* Access the grid */
263                         c_ptr = &cave[y][x];
264
265                         /* Clear possible garbage of hidden trap */
266                         c_ptr->mimic = 0;
267
268                         /* Clear previous contents, add stairs */
269                         c_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
270
271                         /* No longer "FLOOR" */
272                         c_ptr->info &= ~(CAVE_FLOOR);
273
274                         /* Success */
275                         break;
276                 }
277         }
278         return TRUE;
279 }
280
281 /*!
282  * @brief フロア上のランダム位置に各種オブジェクトを配置する / Allocates some objects (using "place" and "type")
283  * @param set 配置したい地形の種類
284  * @param typ 配置したいオブジェクトの種類
285  * @param num 配置したい数
286  * @return 規定数通りに生成に成功したらTRUEを返す。
287  */
288 static void alloc_object(int set, int typ, int num)
289 {
290         int y = 0, x = 0, k;
291         int dummy = 0;
292         cave_type *c_ptr;
293
294         /* A small level has few objects. */
295         num = num * cur_hgt * cur_wid / (MAX_HGT*MAX_WID) +1;
296
297         /* Place some objects */
298         for (k = 0; k < num; k++)
299         {
300                 /* Pick a "legal" spot */
301                 while (dummy < SAFE_MAX_ATTEMPTS)
302                 {
303                         bool room;
304
305                         dummy++;
306
307                         /* Location */
308                         y = randint0(cur_hgt);
309                         x = randint0(cur_wid);
310
311                         c_ptr = &cave[y][x];
312
313                         /* Require "naked" floor grid */
314                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
315
316                         /* Avoid player location */
317                         if (player_bold(y, x)) continue;
318
319                         /* Check for "room" */
320                         room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
321
322                         /* Require corridor? */
323                         if ((set == ALLOC_SET_CORR) && room) continue;
324
325                         /* Require room? */
326                         if ((set == ALLOC_SET_ROOM) && !room) continue;
327
328                         /* Accept it */
329                         break;
330                 }
331
332                 if (dummy >= SAFE_MAX_ATTEMPTS)
333                 {
334                         msg_print_wizard(CHEAT_DUNGEON, _("アイテムの配置に失敗しました。", "Failed to place object."));
335                         return;
336                 }
337
338
339                 /* Place something */
340                 switch (typ)
341                 {
342                         case ALLOC_TYP_RUBBLE:
343                         {
344                                 place_rubble(y, x);
345                                 cave[y][x].info &= ~(CAVE_FLOOR);
346                                 break;
347                         }
348
349                         case ALLOC_TYP_TRAP:
350                         {
351                                 place_trap(y, x);
352                                 cave[y][x].info &= ~(CAVE_FLOOR);
353                                 break;
354                         }
355
356                         case ALLOC_TYP_GOLD:
357                         {
358                                 place_gold(y, x);
359                                 break;
360                         }
361
362                         case ALLOC_TYP_OBJECT:
363                         {
364                                 place_object(y, x, 0L);
365                                 break;
366                         }
367                 }
368         }
369 }
370
371 /*!
372  * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
373  * @param y1 基準となるマスのY座標
374  * @param x1 基準となるマスのX座標
375  * @return 通路の数
376  * @note Assumes "in_bounds(y1, x1)"
377  * @details
378  * XXX XXX This routine currently only counts actual "empty floor"\n
379  * grids which are not in rooms.  We might want to also count stairs,\n
380  * open doors, closed doors, etc.
381  */
382 static int next_to_corr(int y1, int x1)
383 {
384         int i, y, x, k = 0;
385
386         cave_type *c_ptr;
387
388         /* Scan adjacent grids */
389         for (i = 0; i < 4; i++)
390         {
391                 /* Extract the location */
392                 y = y1 + ddy_ddd[i];
393                 x = x1 + ddx_ddd[i];
394
395                 /* Access the grid */
396                 c_ptr = &cave[y][x];
397
398                 /* Skip non floors */
399                 if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
400
401                 /* Skip non "empty floor" grids */
402                 if (!is_floor_grid(c_ptr))
403                         continue;
404
405                 /* Skip grids inside rooms */
406                 if (c_ptr->info & (CAVE_ROOM)) continue;
407
408                 /* Count these grids */
409                 k++;
410         }
411
412         /* Return the number of corridors */
413         return (k);
414 }
415
416
417 /*!
418  * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
419  * @param y 判定を行いたいマスのY座標
420  * @param x 判定を行いたいマスのX座標
421  * @return ドアを設置可能ならばTRUEを返す
422  * @note Assumes "in_bounds(y1, x1)"
423  * @details
424  * XXX XXX XXX\n
425  * Assumes "in_bounds(y, x)"\n
426  */
427 static bool possible_doorway(int y, int x)
428 {
429         /* Count the adjacent corridors */
430         if (next_to_corr(y, x) >= 2)
431         {
432                 /* Check Vertical */
433                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
434                     cave_have_flag_bold(y + 1, x, FF_WALL))
435                 {
436                         return (TRUE);
437                 }
438
439                 /* Check Horizontal */
440                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
441                     cave_have_flag_bold(y, x + 1, FF_WALL))
442                 {
443                         return (TRUE);
444                 }
445         }
446
447         /* No doorway */
448         return (FALSE);
449 }
450
451 /*!
452  * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
453  * @param y 設置を行いたいマスのY座標
454  * @param x 設置を行いたいマスのX座標
455  * @return なし
456  */
457 static void try_door(int y, int x)
458 {
459         /* Paranoia */
460         if (!in_bounds(y, x)) return;
461
462         /* Ignore walls */
463         if (cave_have_flag_bold(y, x, FF_WALL)) return;
464
465         /* Ignore room grids */
466         if (cave[y][x].info & (CAVE_ROOM)) return;
467
468         /* Occasional door (if allowed) */
469         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
470         {
471                 /* Place a door */
472                 place_random_door(y, x, FALSE);
473         }
474 }
475
476
477 /*!
478  * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
479  * @return 成功したならばTRUEを返す
480  */
481 bool place_quest_monsters(void)
482 {
483         int i;
484
485         /* Handle the quest monster placements */
486         for (i = 0; i < max_quests; i++)
487         {
488                 monster_race *r_ptr;
489                 u32b mode;
490                 int j;
491
492                 if (quest[i].status != QUEST_STATUS_TAKEN ||
493                     (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
494                      quest[i].type != QUEST_TYPE_RANDOM) ||
495                     quest[i].level != dun_level ||
496                     dungeon_type != quest[i].dungeon ||
497                     (quest[i].flags & QUEST_FLAG_PRESET))
498                 {
499                         /* Ignore it */
500                         continue;
501                 }
502
503                 r_ptr = &r_info[quest[i].r_idx];
504
505                 /* Hack -- "unique" monsters must be "unique" */
506                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
507                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
508
509                 mode = (PM_NO_KAGE | PM_NO_PET);
510
511                 if (!(r_ptr->flags1 & RF1_FRIENDS))
512                         mode |= PM_ALLOW_GROUP;
513
514                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
515                 {
516                         int k;
517
518                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
519                         {
520                                 int x = 0, y = 0;
521                                 int l;
522
523                                 /* Find an empty grid */
524                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
525                                 {
526                                         cave_type    *c_ptr;
527                                         feature_type *f_ptr;
528
529                                         y = randint0(cur_hgt);
530                                         x = randint0(cur_wid);
531
532                                         c_ptr = &cave[y][x];
533                                         f_ptr = &f_info[c_ptr->feat];
534
535                                         if (!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) continue;
536                                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
537                                         if (distance(y, x, p_ptr->y, p_ptr->x) < 10) continue;
538                                         if (c_ptr->info & CAVE_ICKY) continue;
539                                         else break;
540                                 }
541
542                                 /* Failed to place */
543                                 if (!l) return FALSE;
544
545                                 /* Try to place the monster */
546                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
547                                 {
548                                         /* Success */
549                                         break;
550                                 }
551                                 else
552                                 {
553                                         /* Failure - Try again */
554                                         continue;
555                                 }
556                         }
557
558                         /* Failed to place */
559                         if (k == SAFE_MAX_ATTEMPTS) return FALSE;
560                 }
561         }
562
563         return TRUE;
564 }
565
566
567 /*!
568  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
569  * @param c_ptr 永久壁を廃止したいマス構造体の参照ポインタ
570  * @return なし
571  */
572 static void set_bound_perm_wall(cave_type *c_ptr)
573 {
574         if (bound_walls_perm)
575         {
576                 /* Clear boundary mimic */
577                 c_ptr->mimic = 0;
578         }
579         else
580         {
581                 feature_type *f_ptr = &f_info[c_ptr->feat];
582
583                 /* Hack -- Decline boundary walls with known treasure  */
584                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
585                     !have_flag(f_ptr->flags, FF_SECRET))
586                         c_ptr->feat = feat_state(c_ptr->feat, FF_ENSECRET);
587
588                 /* Set boundary mimic */
589                 c_ptr->mimic = c_ptr->feat;
590         }
591
592         /* Add "solid" perma-wall */
593         place_solid_perm_grid(c_ptr);
594 }
595
596 /*!
597  * @brief フロアに洞窟や湖を配置する / Generate various caverns and lakes
598  * @details There were moved from cave_gen().
599  * @return なし
600  */
601 static void gen_caverns_and_lakes(void)
602 {
603 #ifdef ALLOW_CAVERNS_AND_LAKES
604         /* Possible "destroyed" level */
605         if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
606         {
607                 dun->destroyed = TRUE;
608
609                 /* extra rubble around the place looks cool */
610                 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
611         }
612
613         /* Make a lake some of the time */
614         if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
615             (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
616         {
617                 int count = 0;
618                 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
619                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
620                 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
621                 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
622
623                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
624                 {
625                         /* Lake of Lava */
626                         if ((dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
627                         count -= 2;
628
629                         /* Lake of Lava2 */
630                         if (!dun->laketype && (dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
631                         count--;
632                 }
633
634                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !dun->laketype)
635                 {
636                         /* Lake of Water */
637                         if ((dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
638                         count -= 2;
639
640                         /* Lake of Water2 */
641                         if (!dun->laketype && (dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
642                         count--;
643                 }
644
645                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
646                 {
647                         /* Lake of rubble */
648                         if ((dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
649                         count -= 2;
650
651                         /* Lake of rubble2 */
652                         if (!dun->laketype && (dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
653                         count--;
654                 }
655
656                 /* Lake of tree */
657                 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
658
659                 if (dun->laketype)
660                 {
661                         msg_print_wizard(CHEAT_DUNGEON, _("湖を生成します。", "Lake on the level."));
662                         build_lake(dun->laketype);
663                 }
664         }
665
666         if ((dun_level > DUN_CAVERN) && !dun->empty_level &&
667             (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
668             !dun->laketype && !dun->destroyed && (randint1(1000) < dun_level))
669         {
670                 dun->cavern = TRUE;
671
672                 /* make a large fractal cave in the middle of the dungeon */
673
674                 msg_print_wizard(CHEAT_DUNGEON, _("洞窟を生成。", "Cavern on level."));
675                 build_cavern();
676         }
677 #endif /* ALLOW_CAVERNS_AND_LAKES */
678
679         /* Hack -- No destroyed "quest" levels */
680         if (quest_number(dun_level)) dun->destroyed = FALSE;
681 }
682
683
684 /*!
685  * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
686  * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
687  * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
688  */
689 static bool cave_gen(void)
690 {
691         int i, k, y, x;
692
693         dun_data dun_body;
694
695         /* Global data */
696         dun = &dun_body;
697
698         dun->destroyed = FALSE;
699         dun->empty_level = FALSE;
700         dun->cavern = FALSE;
701         dun->laketype = 0;
702
703         /* Fill the arrays of floors and walls in the good proportions */
704         set_floor_and_wall(dungeon_type);
705
706         /* Prepare allocation table */
707         get_mon_num_prep(get_monster_hook(), NULL);
708
709         /* Randomize the dungeon creation values */
710         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
711         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
712         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
713         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
714         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
715
716         /* Actual maximum number of rooms on this level */
717         dun->row_rooms = cur_hgt / BLOCK_HGT;
718         dun->col_rooms = cur_wid / BLOCK_WID;
719
720         /* Initialize the room table */
721         for (y = 0; y < dun->row_rooms; y++)
722         {
723                 for (x = 0; x < dun->col_rooms; x++)
724                 {
725                         dun->room_map[y][x] = FALSE;
726                 }
727         }
728
729         /* No rooms yet */
730         dun->cent_n = 0;
731
732         /* Empty arena levels */
733         if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
734         {
735                 dun->empty_level = TRUE;
736                 msg_print_wizard(CHEAT_DUNGEON, _("アリーナレベルを生成。", "Arena level."));
737         }
738
739         if (dun->empty_level)
740         {
741                 /* Start with floors */
742                 for (y = 0; y < cur_hgt; y++)
743                 {
744                         for (x = 0; x < cur_wid; x++)
745                         {
746                                 place_floor_bold(y, x);
747                         }
748                 }
749
750                 /* Special boundary walls -- Top and bottom */
751                 for (x = 0; x < cur_wid; x++)
752                 {
753                         place_extra_bold(0, x);
754                         place_extra_bold(cur_hgt - 1, x);
755                 }
756
757                 /* Special boundary walls -- Left and right */
758                 for (y = 1; y < (cur_hgt - 1); y++)
759                 {
760                         place_extra_bold(y, 0);
761                         place_extra_bold(y, cur_wid - 1);
762                 }
763         }
764         else
765         {
766                 /* Start with walls */
767                 for (y = 0; y < cur_hgt; y++)
768                 {
769                         for (x = 0; x < cur_wid; x++)
770                         {
771                                 place_extra_bold(y, x);
772                         }
773                 }
774         }
775
776
777         /* Generate various caverns and lakes */
778         gen_caverns_and_lakes();
779
780
781         /* Build maze */
782         if (d_info[dungeon_type].flags1 & DF1_MAZE)
783         {
784                 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
785
786                 /* Place 3 or 4 down stairs near some walls */
787                 if (!alloc_stairs(feat_down_stair, rand_range(2, 3), 3)) return FALSE;
788
789                 /* Place 1 or 2 up stairs near some walls */
790                 if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
791         }
792
793         /* Build some rooms */
794         else
795         {
796                 int tunnel_fail_count = 0;
797
798                 /*
799                  * Build each type of room in turn until we cannot build any more.
800                  */
801                 if (!generate_rooms()) return FALSE;
802
803
804                 /* Make a hole in the dungeon roof sometimes at level 1 */
805                 if (dun_level == 1)
806                 {
807                         while (one_in_(DUN_MOS_DEN))
808                         {
809                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
810                         }
811                 }
812
813                 /* Destroy the level if necessary */
814                 if (dun->destroyed) destroy_level();
815
816                 /* Hack -- Add some rivers */
817                 if (one_in_(3) && (randint1(dun_level) > 5))
818                 {
819                         IDX feat1 = 0, feat2 = 0;
820
821                         /* Choose water or lava */
822                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
823                         {
824                                 feat1 = feat_deep_water;
825                                 feat2 = feat_shallow_water;
826                         }
827                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
828                         {
829                                 feat1 = feat_deep_lava;
830                                 feat2 = feat_shallow_lava;
831                         }
832                         else feat1 = 0;
833
834                         if (feat1)
835                         {
836                                 feature_type *f_ptr = &f_info[feat1];
837
838                                 /* Only add river if matches lake type or if have no lake at all */
839                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
840                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
841                                      !dun->laketype)
842                                 {
843                                         add_river(feat1, feat2);
844                                 }
845                         }
846                 }
847
848                 /* Hack -- Scramble the room order */
849                 for (i = 0; i < dun->cent_n; i++)
850                 {
851                         int ty, tx;
852                         int pick = rand_range(0, i);
853
854                         ty = dun->cent[i].y;
855                         tx = dun->cent[i].x;
856                         dun->cent[i].y = dun->cent[pick].y;
857                         dun->cent[i].x = dun->cent[pick].x;
858                         dun->cent[pick].y = ty;
859                         dun->cent[pick].x = tx;
860                 }
861
862                 /* Start with no tunnel doors */
863                 dun->door_n = 0;
864
865                 /* Hack -- connect the first room to the last room */
866                 y = dun->cent[dun->cent_n-1].y;
867                 x = dun->cent[dun->cent_n-1].x;
868
869                 /* Connect all the rooms together */
870                 for (i = 0; i < dun->cent_n; i++)
871                 {
872                         int j;
873
874                         /* Reset the arrays */
875                         dun->tunn_n = 0;
876                         dun->wall_n = 0;
877
878                         /* Connect the room to the previous room */
879                         if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
880                         {
881                                 /* make cave-like tunnel */
882                                 (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
883                         }
884                         else
885                         {
886                                 /* make normal tunnel */
887                                 if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
888                         }
889
890                         if (tunnel_fail_count >= 2) return FALSE;
891
892                         /* Turn the tunnel into corridor */
893                         for (j = 0; j < dun->tunn_n; j++)
894                         {
895                                 cave_type *c_ptr;
896                                 feature_type *f_ptr;
897
898                                 /* Access the grid */
899                                 y = dun->tunn[j].y;
900                                 x = dun->tunn[j].x;
901
902                                 /* Access the grid */
903                                 c_ptr = &cave[y][x];
904                                 f_ptr = &f_info[c_ptr->feat];
905
906                                 /* Clear previous contents (if not a lake), add a floor */
907                                 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
908                                 {
909                                         /* Clear mimic type */
910                                         c_ptr->mimic = 0;
911
912                                         place_floor_grid(c_ptr);
913                                 }
914                         }
915
916                         /* Apply the piercings that we found */
917                         for (j = 0; j < dun->wall_n; j++)
918                         {
919                                 cave_type *c_ptr;
920
921                                 /* Access the grid */
922                                 y = dun->wall[j].y;
923                                 x = dun->wall[j].x;
924
925                                 /* Access the grid */
926                                 c_ptr = &cave[y][x];
927
928                                 /* Clear mimic type */
929                                 c_ptr->mimic = 0;
930
931                                 /* Clear previous contents, add up floor */
932                                 place_floor_grid(c_ptr);
933
934                                 /* Occasional doorway */
935                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
936                                 {
937                                         /* Place a random door */
938                                         place_random_door(y, x, TRUE);
939                                 }
940                         }
941
942                         /* Remember the "previous" room */
943                         y = dun->cent[i].y;
944                         x = dun->cent[i].x;
945                 }
946
947                 /* Place intersection doors */
948                 for (i = 0; i < dun->door_n; i++)
949                 {
950                         /* Extract junction location */
951                         y = dun->door[i].y;
952                         x = dun->door[i].x;
953
954                         /* Try placing doors */
955                         try_door(y, x - 1);
956                         try_door(y, x + 1);
957                         try_door(y - 1, x);
958                         try_door(y + 1, x);
959                 }
960
961                 /* Place 3 or 4 down stairs near some walls */
962                 if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;
963
964                 /* Place 1 or 2 up stairs near some walls */
965                 if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
966         }
967
968         if (!dun->laketype)
969         {
970                 if (d_info[dungeon_type].stream2)
971                 {
972                         /* Hack -- Add some quartz streamers */
973                         for (i = 0; i < DUN_STR_QUA; i++)
974                         {
975                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
976                         }
977                 }
978
979                 if (d_info[dungeon_type].stream1)
980                 {
981                         /* Hack -- Add some magma streamers */
982                         for (i = 0; i < DUN_STR_MAG; i++)
983                         {
984                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
985                         }
986                 }
987         }
988
989         /* Special boundary walls -- Top and bottom */
990         for (x = 0; x < cur_wid; x++)
991         {
992                 set_bound_perm_wall(&cave[0][x]);
993                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
994         }
995
996         /* Special boundary walls -- Left and right */
997         for (y = 1; y < (cur_hgt - 1); y++)
998         {
999                 set_bound_perm_wall(&cave[y][0]);
1000                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
1001         }
1002
1003         /* Determine the character location */
1004         if (!new_player_spot()) return FALSE;
1005
1006         if (!place_quest_monsters()) return FALSE;
1007
1008         /* Basic "amount" */
1009         k = (dun_level / 3);
1010         if (k > 10) k = 10;
1011         if (k < 2) k = 2;
1012
1013         /* Pick a base number of monsters */
1014         i = d_info[dungeon_type].min_m_alloc_level;
1015
1016         /* To make small levels a bit more playable */
1017         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1018         {
1019                 int small_tester = i;
1020
1021                 i = (i * cur_hgt) / MAX_HGT;
1022                 i = (i * cur_wid) / MAX_WID;
1023                 i += 1;
1024
1025                 if (i > small_tester) i = small_tester;
1026                 else msg_format_wizard(CHEAT_DUNGEON,
1027                         _("モンスター数基本値を %d から %d に減らします", "Reduced monsters base from %d to %d"), small_tester, i);
1028
1029         }
1030
1031         i += randint1(8);
1032
1033         /* Put some monsters in the dungeon */
1034         for (i = i + k; i > 0; i--)
1035         {
1036                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1037         }
1038
1039         /* Place some traps in the dungeon */
1040         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1041
1042         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
1043         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1044
1045         /* Mega Hack -- No object at first level of deeper dungeon */
1046         if (p_ptr->enter_dungeon && dun_level > 1)
1047         {
1048                 /* No stair scum! */
1049                 object_level = 1;
1050         }
1051
1052         /* Put some objects in rooms */
1053         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1054
1055         /* Put some objects/gold in the dungeon */
1056         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1057         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1058
1059         /* Set back to default */
1060         object_level = base_level;
1061
1062         /* Put the Guardian */
1063         if (!alloc_guardian(TRUE)) return FALSE;
1064
1065         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1066         {
1067                 /* Lite the cave */
1068                 for (y = 0; y < cur_hgt; y++)
1069                 {
1070                         for (x = 0; x < cur_wid; x++)
1071                         {
1072                                 cave[y][x].info |= (CAVE_GLOW);
1073                         }
1074                 }
1075         }
1076
1077         return TRUE;
1078 }
1079
1080 /*!
1081  * @brief 闘技場用のアリーナ地形を作成する / Builds the arena after it is entered -KMW-
1082  * @return なし
1083  */
1084 static void build_arena(void)
1085 {
1086         int yval, y_height, y_depth, xval, x_left, x_right;
1087         register int i, j;
1088
1089         yval = SCREEN_HGT / 2;
1090         xval = SCREEN_WID / 2;
1091         y_height = yval - 10;
1092         y_depth = yval + 10;
1093         x_left = xval - 32;
1094         x_right = xval + 32;
1095
1096         for (i = y_height; i <= y_height + 5; i++)
1097                 for (j = x_left; j <= x_right; j++)
1098                 {
1099                         place_extra_perm_bold(i, j);
1100                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1101                 }
1102         for (i = y_depth; i >= y_depth - 5; i--)
1103                 for (j = x_left; j <= x_right; j++)
1104                 {
1105                         place_extra_perm_bold(i, j);
1106                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1107                 }
1108         for (j = x_left; j <= x_left + 17; j++)
1109                 for (i = y_height; i <= y_depth; i++)
1110                 {
1111                         place_extra_perm_bold(i, j);
1112                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1113                 }
1114         for (j = x_right; j >= x_right - 17; j--)
1115                 for (i = y_height; i <= y_depth; i++)
1116                 {
1117                         place_extra_perm_bold(i, j);
1118                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1119                 }
1120
1121         place_extra_perm_bold(y_height+6, x_left+18);
1122         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1123         place_extra_perm_bold(y_depth-6, x_left+18);
1124         cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1125         place_extra_perm_bold(y_height+6, x_right-18);
1126         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1127         place_extra_perm_bold(y_depth-6, x_right-18);
1128         cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1129
1130         i = y_height + 5;
1131         j = xval;
1132         cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1133         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1134         player_place(i, j);
1135 }
1136
1137 /*!
1138  * @brief 闘技場への入場処理 / Town logic flow for generation of arena -KMW-
1139  * @return なし
1140  */
1141 static void arena_gen(void)
1142 {
1143         int y, x;
1144         int qy = 0;
1145         int qx = 0;
1146
1147         /* Smallest area */
1148         cur_hgt = SCREEN_HGT;
1149         cur_wid = SCREEN_WID;
1150
1151         /* Start with solid walls */
1152         for (y = 0; y < MAX_HGT; y++)
1153         {
1154                 for (x = 0; x < MAX_WID; x++)
1155                 {
1156                         /* Create "solid" perma-wall */
1157                         place_solid_perm_bold(y, x);
1158
1159                         /* Illuminate and memorize the walls */
1160                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1161                 }
1162         }
1163
1164         /* Then place some floors */
1165         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1166         {
1167                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1168                 {
1169                         /* Create empty floor */
1170                         cave[y][x].feat = feat_floor;
1171                 }
1172         }
1173
1174         build_arena();
1175
1176         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)))
1177         {
1178                 p_ptr->exit_bldg = TRUE;
1179                 p_ptr->arena_number++;
1180 #ifdef JP
1181                 msg_print("相手は欠場した。あなたの不戦勝だ。");
1182 #else
1183                 msg_print("The enemy is unable appear. You won by default.");
1184 #endif
1185         }
1186
1187 }
1188
1189 /*!
1190  * @brief モンスター闘技場のフロア生成 / Builds the arena after it is entered -KMW-
1191  * @return なし
1192  */
1193 static void build_battle(void)
1194 {
1195         int yval, y_height, y_depth, xval, x_left, x_right;
1196         register int i, j;
1197
1198         yval = SCREEN_HGT / 2;
1199         xval = SCREEN_WID / 2;
1200         y_height = yval - 10;
1201         y_depth = yval + 10;
1202         x_left = xval - 32;
1203         x_right = xval + 32;
1204
1205         for (i = y_height; i <= y_height + 5; i++)
1206                 for (j = x_left; j <= x_right; j++)
1207                 {
1208                         place_extra_perm_bold(i, j);
1209                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1210                 }
1211         for (i = y_depth; i >= y_depth - 3; i--)
1212                 for (j = x_left; j <= x_right; j++)
1213                 {
1214                         place_extra_perm_bold(i, j);
1215                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1216                 }
1217         for (j = x_left; j <= x_left + 17; j++)
1218                 for (i = y_height; i <= y_depth; i++)
1219                 {
1220                         place_extra_perm_bold(i, j);
1221                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1222                 }
1223         for (j = x_right; j >= x_right - 17; j--)
1224                 for (i = y_height; i <= y_depth; i++)
1225                 {
1226                         place_extra_perm_bold(i, j);
1227                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1228                 }
1229
1230         place_extra_perm_bold(y_height+6, x_left+18);
1231         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1232         place_extra_perm_bold(y_depth-4, x_left+18);
1233         cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1234         place_extra_perm_bold(y_height+6, x_right-18);
1235         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1236         place_extra_perm_bold(y_depth-4, x_right-18);
1237         cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1238
1239         for (i = y_height + 1; i <= y_height + 5; i++)
1240                 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1241                 {
1242                         cave[i][j].feat = feat_permanent_glass_wall;
1243                 }
1244
1245         i = y_height + 1;
1246         j = xval;
1247         cave[i][j].feat = f_tag_to_index("BUILDING_3");
1248         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1249         player_place(i, j);
1250 }
1251
1252 /*!
1253  * @brief モンスター闘技場への導入処理 / Town logic flow for generation of arena -KMW-
1254  * @return なし
1255  */
1256 static void battle_gen(void)
1257 {
1258         int y, x;
1259         MONSTER_IDX i;
1260         int qy = 0;
1261         int qx = 0;
1262
1263         /* Start with solid walls */
1264         for (y = 0; y < MAX_HGT; y++)
1265         {
1266                 for (x = 0; x < MAX_WID; x++)
1267                 {
1268                         /* Create "solid" perma-wall */
1269                         place_solid_perm_bold(y, x);
1270
1271                         /* Illuminate and memorize the walls */
1272                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1273                 }
1274         }
1275
1276         /* Then place some floors */
1277         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1278         {
1279                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1280                 {
1281                         /* Create empty floor */
1282                         cave[y][x].feat = feat_floor;
1283                 }
1284         }
1285
1286         build_battle();
1287
1288         for(i = 0; i < 4; i++)
1289         {
1290                 place_monster_aux(0, p_ptr->y + 8 + (i/2)*4, p_ptr->x - 2 + (i%2)*4, battle_mon[i],
1291                                   (PM_NO_KAGE | PM_NO_PET));
1292                 set_friendly(&m_list[cave[p_ptr->y+8+(i/2)*4][p_ptr->x-2+(i%2)*4].m_idx]);
1293         }
1294         for(i = 1; i < m_max; i++)
1295         {
1296                 monster_type *m_ptr = &m_list[i];
1297
1298                 if (!m_ptr->r_idx) continue;
1299
1300                 /* Hack -- Detect monster */
1301                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1302
1303                 /* Update the monster */
1304                 update_mon(i, FALSE);
1305         }
1306 }
1307
1308 /*!
1309  * @brief 固定マップクエストのフロア生成 / Generate a quest level
1310  * @return なし
1311  */
1312 static void quest_gen(void)
1313 {
1314         int x, y;
1315
1316
1317         /* Start with perm walls */
1318         for (y = 0; y < cur_hgt; y++)
1319         {
1320                 for (x = 0; x < cur_wid; x++)
1321                 {
1322                         place_solid_perm_bold(y, x);
1323                 }
1324         }
1325
1326         /* Set the quest level */
1327         base_level = quest[p_ptr->inside_quest].level;
1328         dun_level = base_level;
1329         object_level = base_level;
1330         monster_level = base_level;
1331
1332         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1333
1334         /* Prepare allocation table */
1335         get_mon_num_prep(get_monster_hook(), NULL);
1336
1337         init_flags = INIT_CREATE_DUNGEON;
1338
1339         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1340 }
1341
1342 /*!
1343  * @brief ダンジョン時のランダムフロア生成 / Make a real level
1344  * @return フロアの生成に成功したらTRUE
1345  */
1346 static bool level_gen(cptr *why)
1347 {
1348         int level_height, level_width;
1349
1350         if ((always_small_levels || ironman_small_levels ||
1351             (one_in_(SMALL_LEVEL) && small_levels) ||
1352              (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1353             (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1354             !(d_info[dungeon_type].flags1 & DF1_BIG))
1355         {
1356                 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1357                 {
1358                         level_height = 1;
1359                         level_width = 1;
1360                 }
1361                 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1362                 {
1363                         level_height = 2;
1364                         level_width = 2;
1365                 }
1366                 else
1367                 {
1368                         do
1369                         {
1370                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1371                                 level_width = randint1(MAX_WID/SCREEN_WID);
1372                         }
1373                         while ((level_height == MAX_HGT/SCREEN_HGT) &&
1374                                    (level_width == MAX_WID/SCREEN_WID));
1375                 }
1376
1377                 cur_hgt = level_height * SCREEN_HGT;
1378                 cur_wid = level_width * SCREEN_WID;
1379
1380                 /* Assume illegal panel */
1381                 panel_row_min = cur_hgt;
1382                 panel_col_min = cur_wid;
1383
1384                 msg_format_wizard(CHEAT_DUNGEON,
1385                         _("小さなフロア: X:%d, Y:%d", "A 'small' dungeon level: X:%d, Y:%d."),
1386                         cur_wid, cur_hgt);
1387         }
1388         else
1389         {
1390                 /* Big dungeon */
1391                 cur_hgt = MAX_HGT;
1392                 cur_wid = MAX_WID;
1393
1394                 /* Assume illegal panel */
1395                 panel_row_min = cur_hgt;
1396                 panel_col_min = cur_wid;
1397         }
1398
1399         /* Make a dungeon */
1400         if (!cave_gen())
1401         {
1402 #ifdef JP
1403 *why = "ダンジョン生成に失敗";
1404 #else
1405                 *why = "could not place player";
1406 #endif
1407
1408                 return FALSE;
1409         }
1410         else return TRUE;
1411 }
1412
1413 /*!
1414  * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after cave generation
1415  * @return なし
1416  */
1417 void wipe_generate_cave_flags(void)
1418 {
1419         int x, y;
1420
1421         for (y = 0; y < cur_hgt; y++)
1422         {
1423                 for (x = 0; x < cur_wid; x++)
1424                 {
1425                         /* Wipe unused flags */
1426                         cave[y][x].info &= ~(CAVE_MASK);
1427                 }
1428         }
1429
1430         if (dun_level)
1431         {
1432                 for (y = 1; y < cur_hgt - 1; y++)
1433                 {
1434                         for (x = 1; x < cur_wid - 1; x++)
1435                         {
1436                                 /* There might be trap */
1437                                 cave[y][x].info |= CAVE_UNSAFE;
1438                         }
1439                 }
1440         }
1441 }
1442
1443 /*!
1444  * @brief フロアの全情報を初期化する / Clear and empty the cave
1445  * @return なし
1446  */
1447 void clear_cave(void)
1448 {
1449         int x, y, i;
1450
1451         /* Very simplified version of wipe_o_list() */
1452         (void)C_WIPE(o_list, o_max, object_type);
1453         o_max = 1;
1454         o_cnt = 0;
1455
1456         /* Very simplified version of wipe_m_list() */
1457         for (i = 1; i < max_r_idx; i++)
1458                 r_info[i].cur_num = 0;
1459         (void)C_WIPE(m_list, m_max, monster_type);
1460         m_max = 1;
1461         m_cnt = 0;
1462         for (i = 0; i < MAX_MTIMED; i++) mproc_max[i] = 0;
1463
1464         /* Pre-calc cur_num of pets in party_mon[] */
1465         precalc_cur_num_of_pet();
1466
1467
1468         /* Start with a blank cave */
1469         for (y = 0; y < MAX_HGT; y++)
1470         {
1471                 for (x = 0; x < MAX_WID; x++)
1472                 {
1473                         cave_type *c_ptr = &cave[y][x];
1474
1475                         /* No flags */
1476                         c_ptr->info = 0;
1477
1478                         /* No features */
1479                         c_ptr->feat = 0;
1480
1481                         /* No objects */
1482                         c_ptr->o_idx = 0;
1483
1484                         /* No monsters */
1485                         c_ptr->m_idx = 0;
1486
1487                         /* No special */
1488                         c_ptr->special = 0;
1489
1490                         /* No mimic */
1491                         c_ptr->mimic = 0;
1492
1493                         /* No flow */
1494                         c_ptr->cost = 0;
1495                         c_ptr->dist = 0;
1496                         c_ptr->when = 0;
1497                 }
1498         }
1499
1500         /* Mega-Hack -- no player yet */
1501         p_ptr->x = p_ptr->y = 0;
1502
1503         /* Set the base level */
1504         base_level = dun_level;
1505
1506         /* Reset the monster generation level */
1507         monster_level = base_level;
1508
1509         /* Reset the object generation level */
1510         object_level = base_level;
1511 }
1512
1513
1514 /*!
1515  * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
1516  * @return なし
1517  * @note Hack -- regenerate any "overflow" levels
1518  */
1519 void generate_cave(void)
1520 {
1521         int num;
1522
1523         /* Fill the arrays of floors and walls in the good proportions */
1524         set_floor_and_wall(dungeon_type);
1525
1526         /* Generate */
1527         for (num = 0; TRUE; num++)
1528         {
1529                 bool okay = TRUE;
1530
1531                 cptr why = NULL;
1532
1533                 /* Clear and empty the cave */
1534                 clear_cave();
1535
1536                 /* Build the arena -KMW- */
1537                 if (p_ptr->inside_arena)
1538                 {
1539                         /* Small arena */
1540                         arena_gen();
1541                 }
1542
1543                 /* Build the battle -KMW- */
1544                 else if (p_ptr->inside_battle)
1545                 {
1546                         /* Small arena */
1547                         battle_gen();
1548                 }
1549
1550                 else if (p_ptr->inside_quest)
1551                 {
1552                         quest_gen();
1553                 }
1554
1555                 /* Build the town */
1556                 else if (!dun_level)
1557                 {
1558                         /* Make the wilderness */
1559                         if (p_ptr->wild_mode) wilderness_gen_small();
1560                         else wilderness_gen();
1561                 }
1562
1563                 /* Build a real level */
1564                 else
1565                 {
1566                         okay = level_gen(&why);
1567                 }
1568
1569
1570                 /* Prevent object over-flow */
1571                 if (o_max >= max_o_idx)
1572                 {
1573                         /* Message */
1574 #ifdef JP
1575 why = "アイテムが多すぎる";
1576 #else
1577                         why = "too many objects";
1578 #endif
1579
1580
1581                         /* Message */
1582                         okay = FALSE;
1583                 }
1584                 /* Prevent monster over-flow */
1585                 else if (m_max >= max_m_idx)
1586                 {
1587                         /* Message */
1588 #ifdef JP
1589 why = "モンスターが多すぎる";
1590 #else
1591                         why = "too many monsters";
1592 #endif
1593
1594
1595                         /* Message */
1596                         okay = FALSE;
1597                 }
1598
1599                 /* Accept */
1600                 if (okay) break;
1601
1602                 /* Message */
1603 #ifdef JP
1604 if (why) msg_format("生成やり直し(%s)", why);
1605 #else
1606                 if (why) msg_format("Generation restarted (%s)", why);
1607 #endif
1608
1609
1610                 /* Wipe the objects */
1611                 wipe_o_list();
1612
1613                 /* Wipe the monsters */
1614                 wipe_m_list();
1615         }
1616
1617         /* Glow deep lava and building entrances */
1618         glow_deep_lava_and_bldg();
1619
1620         /* Reset flag */
1621         p_ptr->enter_dungeon = FALSE;
1622
1623         wipe_generate_cave_flags();
1624 }