OSDN Git Service

[Refactor] #37353 cmd1~melee1間整理。 / Refactor between cmd1 and melee1.
[hengband/hengband.git] / src / floor-generate.c
1 /*!
2  * @file generate.c
3  * @brief ダンジョンの生成 / Dungeon generation
4  * @date 2014/01/04
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  * @details
12  * Note that Level generation is *not* an important bottleneck,\n
13  * though it can be annoyingly slow on older machines...  Thus\n
14  * we emphasize "simplicity" and "correctness" over "speed".\n
15  *\n
16  * This entire file is only needed for generating levels.\n
17  * This may allow smart compilers to only load it when needed.\n
18  *\n
19  * Consider the "v_info.txt" file for vault generation.\n
20  *\n
21  * In this file, we use the "special" granite and perma-wall sub-types,\n
22  * where "basic" is normal, "inner" is inside a room, "outer" is the\n
23  * outer wall of a room, and "solid" is the outer wall of the dungeon\n
24  * or any walls that may not be pierced by corridors.  Thus the only\n
25  * wall type that may be pierced by a corridor is the "outer granite"\n
26  * type.  The "basic granite" type yields the "actual" corridors.\n
27  *\n
28  * Note that we use the special "solid" granite wall type to prevent\n
29  * multiple corridors from piercing a wall in two adjacent locations,\n
30  * which would be messy, and we use the special "outer" granite wall\n
31  * to indicate which walls "surround" rooms, and may thus be "pierced"\n
32  * by corridors entering or leaving the room.\n
33  *\n
34  * Note that a tunnel which attempts to leave a room near the "edge"\n
35  * of the dungeon in a direction toward that edge will cause "silly"\n
36  * wall piercings, but will have no permanently incorrect effects,\n
37  * as long as the tunnel can *eventually* exit from another side.\n
38  * And note that the wall may not come back into the room by the\n
39  * hole it left through, so it must bend to the left or right and\n
40  * then optionally re-enter the room (at least 2 grids away).  This\n
41  * is not a problem since every room that is large enough to block\n
42  * the passage of tunnels is also large enough to allow the tunnel\n
43  * to pierce the room itself several times.\n
44  *\n
45  * Note that no two corridors may enter a room through adjacent grids,\n
46  * they must either share an entryway or else use entryways at least\n
47  * two grids apart.  This prevents "large" (or "silly") doorways.\n
48  *\n
49  * To create rooms in the dungeon, we first divide the dungeon up\n
50  * into "blocks" of 11x11 grids each, and require that all rooms\n
51  * occupy a rectangular group of blocks.  As long as each room type\n
52  * reserves a sufficient number of blocks, the room building routines\n
53  * will not need to check bounds.  Note that most of the normal rooms\n
54  * actually only use 23x11 grids, and so reserve 33x11 grids.\n
55  *\n
56  * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)\n
57  * allows more variability in the horizontal placement of rooms, and\n
58  * at the same time has the disadvantage that some rooms (two thirds\n
59  * of the normal rooms) may be "split" by panel boundaries.  This can\n
60  * induce a situation where a player is in a room and part of the room\n
61  * is off the screen.  It may be annoying enough to go back to 33x11\n
62  * blocks to prevent this visual situation.\n
63  *\n
64  * Note that the dungeon generation routines are much different (2.7.5)\n
65  * and perhaps "DUN_ROOMS" should be less than 50.\n
66  *\n
67  * 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
373
374 /*!
375  * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
376  * @return 成功したならばTRUEを返す
377  */
378 bool place_quest_monsters(void)
379 {
380         int i;
381
382         /* Handle the quest monster placements */
383         for (i = 0; i < max_q_idx; i++)
384         {
385                 monster_race *r_ptr;
386                 BIT_FLAGS mode;
387                 int j;
388
389                 if (quest[i].status != QUEST_STATUS_TAKEN ||
390                     (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
391                      quest[i].type != QUEST_TYPE_RANDOM) ||
392                     quest[i].level != dun_level ||
393                     dungeon_type != quest[i].dungeon ||
394                     (quest[i].flags & QUEST_FLAG_PRESET))
395                 {
396                         /* Ignore it */
397                         continue;
398                 }
399
400                 r_ptr = &r_info[quest[i].r_idx];
401
402                 /* Hack -- "unique" monsters must be "unique" */
403                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
404                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
405
406                 mode = (PM_NO_KAGE | PM_NO_PET);
407
408                 if (!(r_ptr->flags1 & RF1_FRIENDS))
409                         mode |= PM_ALLOW_GROUP;
410
411                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
412                 {
413                         int k;
414
415                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
416                         {
417                                 int x = 0, y = 0;
418                                 int l;
419
420                                 /* Find an empty grid */
421                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
422                                 {
423                                         cave_type    *c_ptr;
424                                         feature_type *f_ptr;
425
426                                         y = randint0(cur_hgt);
427                                         x = randint0(cur_wid);
428
429                                         c_ptr = &cave[y][x];
430                                         f_ptr = &f_info[c_ptr->feat];
431
432                                         if (!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) continue;
433                                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
434                                         if (distance(y, x, p_ptr->y, p_ptr->x) < 10) continue;
435                                         if (c_ptr->info & CAVE_ICKY) continue;
436                                         else break;
437                                 }
438
439                                 /* Failed to place */
440                                 if (!l) return FALSE;
441
442                                 /* Try to place the monster */
443                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
444                                 {
445                                         /* Success */
446                                         break;
447                                 }
448                                 else
449                                 {
450                                         /* Failure - Try again */
451                                         continue;
452                                 }
453                         }
454
455                         /* Failed to place */
456                         if (k == SAFE_MAX_ATTEMPTS) return FALSE;
457                 }
458         }
459
460         return TRUE;
461 }
462
463
464 /*!
465  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
466  * @param c_ptr 永久壁を廃止したいマス構造体の参照ポインタ
467  * @return なし
468  */
469 static void set_bound_perm_wall(cave_type *c_ptr)
470 {
471         if (bound_walls_perm)
472         {
473                 /* Clear boundary mimic */
474                 c_ptr->mimic = 0;
475         }
476         else
477         {
478                 feature_type *f_ptr = &f_info[c_ptr->feat];
479
480                 /* Hack -- Decline boundary walls with known treasure  */
481                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
482                     !have_flag(f_ptr->flags, FF_SECRET))
483                         c_ptr->feat = feat_state(c_ptr->feat, FF_ENSECRET);
484
485                 /* Set boundary mimic */
486                 c_ptr->mimic = c_ptr->feat;
487         }
488
489         /* Add "solid" perma-wall */
490         place_solid_perm_grid(c_ptr);
491 }
492
493 /*!
494  * @brief フロアに洞窟や湖を配置する / Generate various caverns and lakes
495  * @details There were moved from cave_gen().
496  * @return なし
497  */
498 static void gen_caverns_and_lakes(void)
499 {
500 #ifdef ALLOW_CAVERNS_AND_LAKES
501         /* Possible "destroyed" level */
502         if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
503         {
504                 dun->destroyed = TRUE;
505
506                 /* extra rubble around the place looks cool */
507                 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
508         }
509
510         /* Make a lake some of the time */
511         if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
512             (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
513         {
514                 int count = 0;
515                 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
516                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
517                 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
518                 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
519
520                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
521                 {
522                         /* Lake of Lava */
523                         if ((dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
524                         count -= 2;
525
526                         /* Lake of Lava2 */
527                         if (!dun->laketype && (dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
528                         count--;
529                 }
530
531                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !dun->laketype)
532                 {
533                         /* Lake of Water */
534                         if ((dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
535                         count -= 2;
536
537                         /* Lake of Water2 */
538                         if (!dun->laketype && (dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
539                         count--;
540                 }
541
542                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
543                 {
544                         /* Lake of rubble */
545                         if ((dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
546                         count -= 2;
547
548                         /* Lake of rubble2 */
549                         if (!dun->laketype && (dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
550                         count--;
551                 }
552
553                 /* Lake of tree */
554                 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
555
556                 if (dun->laketype)
557                 {
558                         msg_print_wizard(CHEAT_DUNGEON, _("湖を生成します。", "Lake on the level."));
559                         build_lake(dun->laketype);
560                 }
561         }
562
563         if ((dun_level > DUN_CAVERN) && !dun->empty_level &&
564             (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
565             !dun->laketype && !dun->destroyed && (randint1(1000) < dun_level))
566         {
567                 dun->cavern = TRUE;
568
569                 /* make a large fractal cave in the middle of the dungeon */
570
571                 msg_print_wizard(CHEAT_DUNGEON, _("洞窟を生成。", "Cavern on level."));
572                 build_cavern();
573         }
574 #endif /* ALLOW_CAVERNS_AND_LAKES */
575
576         /* Hack -- No destroyed "quest" levels */
577         if (quest_number(dun_level)) dun->destroyed = FALSE;
578 }
579
580
581 /*!
582  * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
583  * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
584  * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
585  */
586 static bool cave_gen(void)
587 {
588         int i, k, y, x;
589
590         dun_data dun_body;
591
592         /* Global data */
593         dun = &dun_body;
594
595         dun->destroyed = FALSE;
596         dun->empty_level = FALSE;
597         dun->cavern = FALSE;
598         dun->laketype = 0;
599
600         /* Fill the arrays of floors and walls in the good proportions */
601         set_floor_and_wall(dungeon_type);
602
603         /* Prepare allocation table */
604         get_mon_num_prep(get_monster_hook(), NULL);
605
606         /* Randomize the dungeon creation values */
607         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
608         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
609         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
610         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
611         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
612
613         /* Actual maximum number of rooms on this level */
614         dun->row_rooms = cur_hgt / BLOCK_HGT;
615         dun->col_rooms = cur_wid / BLOCK_WID;
616
617         /* Initialize the room table */
618         for (y = 0; y < dun->row_rooms; y++)
619         {
620                 for (x = 0; x < dun->col_rooms; x++)
621                 {
622                         dun->room_map[y][x] = FALSE;
623                 }
624         }
625
626         /* No rooms yet */
627         dun->cent_n = 0;
628
629         /* Empty arena levels */
630         if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
631         {
632                 dun->empty_level = TRUE;
633                 msg_print_wizard(CHEAT_DUNGEON, _("アリーナレベルを生成。", "Arena level."));
634         }
635
636         if (dun->empty_level)
637         {
638                 /* Start with floors */
639                 for (y = 0; y < cur_hgt; y++)
640                 {
641                         for (x = 0; x < cur_wid; x++)
642                         {
643                                 place_floor_bold(y, x);
644                         }
645                 }
646
647                 /* Special boundary walls -- Top and bottom */
648                 for (x = 0; x < cur_wid; x++)
649                 {
650                         place_extra_bold(0, x);
651                         place_extra_bold(cur_hgt - 1, x);
652                 }
653
654                 /* Special boundary walls -- Left and right */
655                 for (y = 1; y < (cur_hgt - 1); y++)
656                 {
657                         place_extra_bold(y, 0);
658                         place_extra_bold(y, cur_wid - 1);
659                 }
660         }
661         else
662         {
663                 /* Start with walls */
664                 for (y = 0; y < cur_hgt; y++)
665                 {
666                         for (x = 0; x < cur_wid; x++)
667                         {
668                                 place_extra_bold(y, x);
669                         }
670                 }
671         }
672
673
674         /* Generate various caverns and lakes */
675         gen_caverns_and_lakes();
676
677
678         /* Build maze */
679         if (d_info[dungeon_type].flags1 & DF1_MAZE)
680         {
681                 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
682
683                 /* Place 3 or 4 down stairs near some walls */
684                 if (!alloc_stairs(feat_down_stair, rand_range(2, 3), 3)) return FALSE;
685
686                 /* Place 1 or 2 up stairs near some walls */
687                 if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
688         }
689
690         /* Build some rooms */
691         else
692         {
693                 int tunnel_fail_count = 0;
694
695                 /*
696                  * Build each type of room in turn until we cannot build any more.
697                  */
698                 if (!generate_rooms()) return FALSE;
699
700
701                 /* Make a hole in the dungeon roof sometimes at level 1 */
702                 if (dun_level == 1)
703                 {
704                         while (one_in_(DUN_MOS_DEN))
705                         {
706                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
707                         }
708                 }
709
710                 /* Destroy the level if necessary */
711                 if (dun->destroyed) destroy_level();
712
713                 /* Hack -- Add some rivers */
714                 if (one_in_(3) && (randint1(dun_level) > 5))
715                 {
716                         IDX feat1 = 0, feat2 = 0;
717
718                         /* Choose water or lava */
719                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
720                         {
721                                 feat1 = feat_deep_water;
722                                 feat2 = feat_shallow_water;
723                         }
724                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
725                         {
726                                 feat1 = feat_deep_lava;
727                                 feat2 = feat_shallow_lava;
728                         }
729                         else feat1 = 0;
730
731                         if (feat1)
732                         {
733                                 feature_type *f_ptr = &f_info[feat1];
734
735                                 /* Only add river if matches lake type or if have no lake at all */
736                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
737                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
738                                      !dun->laketype)
739                                 {
740                                         add_river(feat1, feat2);
741                                 }
742                         }
743                 }
744
745                 /* Hack -- Scramble the room order */
746                 for (i = 0; i < dun->cent_n; i++)
747                 {
748                         int ty, tx;
749                         int pick = rand_range(0, i);
750
751                         ty = dun->cent[i].y;
752                         tx = dun->cent[i].x;
753                         dun->cent[i].y = dun->cent[pick].y;
754                         dun->cent[i].x = dun->cent[pick].x;
755                         dun->cent[pick].y = ty;
756                         dun->cent[pick].x = tx;
757                 }
758
759                 /* Start with no tunnel doors */
760                 dun->door_n = 0;
761
762                 /* Hack -- connect the first room to the last room */
763                 y = dun->cent[dun->cent_n-1].y;
764                 x = dun->cent[dun->cent_n-1].x;
765
766                 /* Connect all the rooms together */
767                 for (i = 0; i < dun->cent_n; i++)
768                 {
769                         int j;
770
771                         /* Reset the arrays */
772                         dun->tunn_n = 0;
773                         dun->wall_n = 0;
774
775                         /* Connect the room to the previous room */
776                         if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
777                         {
778                                 /* make cave-like tunnel */
779                                 (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
780                         }
781                         else
782                         {
783                                 /* make normal tunnel */
784                                 if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
785                         }
786
787                         if (tunnel_fail_count >= 2) return FALSE;
788
789                         /* Turn the tunnel into corridor */
790                         for (j = 0; j < dun->tunn_n; j++)
791                         {
792                                 cave_type *c_ptr;
793                                 feature_type *f_ptr;
794
795                                 /* Access the grid */
796                                 y = dun->tunn[j].y;
797                                 x = dun->tunn[j].x;
798
799                                 /* Access the grid */
800                                 c_ptr = &cave[y][x];
801                                 f_ptr = &f_info[c_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                                         c_ptr->mimic = 0;
808
809                                         place_floor_grid(c_ptr);
810                                 }
811                         }
812
813                         /* Apply the piercings that we found */
814                         for (j = 0; j < dun->wall_n; j++)
815                         {
816                                 cave_type *c_ptr;
817
818                                 /* Access the grid */
819                                 y = dun->wall[j].y;
820                                 x = dun->wall[j].x;
821
822                                 /* Access the grid */
823                                 c_ptr = &cave[y][x];
824
825                                 /* Clear mimic type */
826                                 c_ptr->mimic = 0;
827
828                                 /* Clear previous contents, add up floor */
829                                 place_floor_grid(c_ptr);
830
831                                 /* Occasional doorway */
832                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
833                                 {
834                                         /* Place a random door */
835                                         place_random_door(y, x, TRUE);
836                                 }
837                         }
838
839                         /* Remember the "previous" room */
840                         y = dun->cent[i].y;
841                         x = dun->cent[i].x;
842                 }
843
844                 /* Place intersection doors */
845                 for (i = 0; i < dun->door_n; i++)
846                 {
847                         /* Extract junction location */
848                         y = dun->door[i].y;
849                         x = dun->door[i].x;
850
851                         /* Try placing doors */
852                         try_door(y, x - 1);
853                         try_door(y, x + 1);
854                         try_door(y - 1, x);
855                         try_door(y + 1, x);
856                 }
857
858                 /* Place 3 or 4 down stairs near some walls */
859                 if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;
860
861                 /* Place 1 or 2 up stairs near some walls */
862                 if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
863         }
864
865         if (!dun->laketype)
866         {
867                 if (d_info[dungeon_type].stream2)
868                 {
869                         /* Hack -- Add some quartz streamers */
870                         for (i = 0; i < DUN_STR_QUA; i++)
871                         {
872                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
873                         }
874                 }
875
876                 if (d_info[dungeon_type].stream1)
877                 {
878                         /* Hack -- Add some magma streamers */
879                         for (i = 0; i < DUN_STR_MAG; i++)
880                         {
881                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
882                         }
883                 }
884         }
885
886         /* Special boundary walls -- Top and bottom */
887         for (x = 0; x < cur_wid; x++)
888         {
889                 set_bound_perm_wall(&cave[0][x]);
890                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
891         }
892
893         /* Special boundary walls -- Left and right */
894         for (y = 1; y < (cur_hgt - 1); y++)
895         {
896                 set_bound_perm_wall(&cave[y][0]);
897                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
898         }
899
900         /* Determine the character location */
901         if (!new_player_spot()) return FALSE;
902
903         if (!place_quest_monsters()) return FALSE;
904
905         /* Basic "amount" */
906         k = (dun_level / 3);
907         if (k > 10) k = 10;
908         if (k < 2) k = 2;
909
910         /* Pick a base number of monsters */
911         i = d_info[dungeon_type].min_m_alloc_level;
912
913         /* To make small levels a bit more playable */
914         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
915         {
916                 int small_tester = i;
917
918                 i = (i * cur_hgt) / MAX_HGT;
919                 i = (i * cur_wid) / MAX_WID;
920                 i += 1;
921
922                 if (i > small_tester) i = small_tester;
923                 else msg_format_wizard(CHEAT_DUNGEON,
924                         _("モンスター数基本値を %d から %d に減らします", "Reduced monsters base from %d to %d"), small_tester, i);
925
926         }
927
928         i += randint1(8);
929
930         /* Put some monsters in the dungeon */
931         for (i = i + k; i > 0; i--)
932         {
933                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
934         }
935
936         /* Place some traps in the dungeon */
937         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
938
939         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
940         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
941
942         /* Mega Hack -- No object at first level of deeper dungeon */
943         if (p_ptr->enter_dungeon && dun_level > 1)
944         {
945                 /* No stair scum! */
946                 object_level = 1;
947         }
948
949         /* Put some objects in rooms */
950         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
951
952         /* Put some objects/gold in the dungeon */
953         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
954         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
955
956         /* Set back to default */
957         object_level = base_level;
958
959         /* Put the Guardian */
960         if (!alloc_guardian(TRUE)) return FALSE;
961
962         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
963         {
964                 /* Lite the cave */
965                 for (y = 0; y < cur_hgt; y++)
966                 {
967                         for (x = 0; x < cur_wid; x++)
968                         {
969                                 cave[y][x].info |= (CAVE_GLOW);
970                         }
971                 }
972         }
973
974         return TRUE;
975 }
976
977 /*!
978  * @brief 闘技場用のアリーナ地形を作成する / Builds the arena after it is entered -KMW-
979  * @return なし
980  */
981 static void build_arena(void)
982 {
983         int yval, y_height, y_depth, xval, x_left, x_right;
984         register int i, j;
985
986         yval = SCREEN_HGT / 2;
987         xval = SCREEN_WID / 2;
988         y_height = yval - 10;
989         y_depth = yval + 10;
990         x_left = xval - 32;
991         x_right = xval + 32;
992
993         for (i = y_height; i <= y_height + 5; i++)
994                 for (j = x_left; j <= x_right; j++)
995                 {
996                         place_extra_perm_bold(i, j);
997                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
998                 }
999         for (i = y_depth; i >= y_depth - 5; i--)
1000                 for (j = x_left; j <= x_right; j++)
1001                 {
1002                         place_extra_perm_bold(i, j);
1003                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1004                 }
1005         for (j = x_left; j <= x_left + 17; j++)
1006                 for (i = y_height; i <= y_depth; i++)
1007                 {
1008                         place_extra_perm_bold(i, j);
1009                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1010                 }
1011         for (j = x_right; j >= x_right - 17; j--)
1012                 for (i = y_height; i <= y_depth; i++)
1013                 {
1014                         place_extra_perm_bold(i, j);
1015                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1016                 }
1017
1018         place_extra_perm_bold(y_height+6, x_left+18);
1019         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1020         place_extra_perm_bold(y_depth-6, x_left+18);
1021         cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1022         place_extra_perm_bold(y_height+6, x_right-18);
1023         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1024         place_extra_perm_bold(y_depth-6, x_right-18);
1025         cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1026
1027         i = y_height + 5;
1028         j = xval;
1029         cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1030         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1031         player_place(i, j);
1032 }
1033
1034 /*!
1035  * @brief 闘技場への入場処理 / Town logic flow for generation of arena -KMW-
1036  * @return なし
1037  */
1038 static void arena_gen(void)
1039 {
1040         int y, x;
1041         int qy = 0;
1042         int qx = 0;
1043
1044         /* Smallest area */
1045         cur_hgt = SCREEN_HGT;
1046         cur_wid = SCREEN_WID;
1047
1048         /* Start with solid walls */
1049         for (y = 0; y < MAX_HGT; y++)
1050         {
1051                 for (x = 0; x < MAX_WID; x++)
1052                 {
1053                         /* Create "solid" perma-wall */
1054                         place_solid_perm_bold(y, x);
1055
1056                         /* Illuminate and memorize the walls */
1057                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1058                 }
1059         }
1060
1061         /* Then place some floors */
1062         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1063         {
1064                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1065                 {
1066                         /* Create empty floor */
1067                         cave[y][x].feat = feat_floor;
1068                 }
1069         }
1070
1071         build_arena();
1072
1073         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)))
1074         {
1075                 p_ptr->exit_bldg = TRUE;
1076                 p_ptr->arena_number++;
1077                 msg_print(_("相手は欠場した。あなたの不戦勝だ。", "The enemy is unable appear. You won by default."));
1078         }
1079
1080 }
1081
1082 /*!
1083  * @brief モンスター闘技場のフロア生成 / Builds the arena after it is entered -KMW-
1084  * @return なし
1085  */
1086 static void build_battle(void)
1087 {
1088         int yval, y_height, y_depth, xval, x_left, x_right;
1089         register int i, j;
1090
1091         yval = SCREEN_HGT / 2;
1092         xval = SCREEN_WID / 2;
1093         y_height = yval - 10;
1094         y_depth = yval + 10;
1095         x_left = xval - 32;
1096         x_right = xval + 32;
1097
1098         for (i = y_height; i <= y_height + 5; i++)
1099                 for (j = x_left; j <= x_right; j++)
1100                 {
1101                         place_extra_perm_bold(i, j);
1102                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1103                 }
1104         for (i = y_depth; i >= y_depth - 3; i--)
1105                 for (j = x_left; j <= x_right; j++)
1106                 {
1107                         place_extra_perm_bold(i, j);
1108                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1109                 }
1110         for (j = x_left; j <= x_left + 17; j++)
1111                 for (i = y_height; i <= y_depth; i++)
1112                 {
1113                         place_extra_perm_bold(i, j);
1114                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1115                 }
1116         for (j = x_right; j >= x_right - 17; j--)
1117                 for (i = y_height; i <= y_depth; i++)
1118                 {
1119                         place_extra_perm_bold(i, j);
1120                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1121                 }
1122
1123         place_extra_perm_bold(y_height+6, x_left+18);
1124         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1125         place_extra_perm_bold(y_depth-4, x_left+18);
1126         cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1127         place_extra_perm_bold(y_height+6, x_right-18);
1128         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1129         place_extra_perm_bold(y_depth-4, x_right-18);
1130         cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1131
1132         for (i = y_height + 1; i <= y_height + 5; i++)
1133                 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1134                 {
1135                         cave[i][j].feat = feat_permanent_glass_wall;
1136                 }
1137
1138         i = y_height + 1;
1139         j = xval;
1140         cave[i][j].feat = f_tag_to_index("BUILDING_3");
1141         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1142         player_place(i, j);
1143 }
1144
1145 /*!
1146  * @brief モンスター闘技場への導入処理 / Town logic flow for generation of arena -KMW-
1147  * @return なし
1148  */
1149 static void battle_gen(void)
1150 {
1151         int y, x;
1152         MONSTER_IDX i;
1153         int qy = 0;
1154         int qx = 0;
1155
1156         /* Start with solid walls */
1157         for (y = 0; y < MAX_HGT; y++)
1158         {
1159                 for (x = 0; x < MAX_WID; x++)
1160                 {
1161                         /* Create "solid" perma-wall */
1162                         place_solid_perm_bold(y, x);
1163
1164                         /* Illuminate and memorize the walls */
1165                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1166                 }
1167         }
1168
1169         /* Then place some floors */
1170         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1171         {
1172                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1173                 {
1174                         /* Create empty floor */
1175                         cave[y][x].feat = feat_floor;
1176                 }
1177         }
1178
1179         build_battle();
1180
1181         for(i = 0; i < 4; i++)
1182         {
1183                 place_monster_aux(0, p_ptr->y + 8 + (i/2)*4, p_ptr->x - 2 + (i%2)*4, battle_mon[i],
1184                                   (PM_NO_KAGE | PM_NO_PET));
1185                 set_friendly(&m_list[cave[p_ptr->y+8+(i/2)*4][p_ptr->x-2+(i%2)*4].m_idx]);
1186         }
1187         for(i = 1; i < m_max; i++)
1188         {
1189                 monster_type *m_ptr = &m_list[i];
1190
1191                 if (!m_ptr->r_idx) continue;
1192
1193                 /* Hack -- Detect monster */
1194                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1195
1196                 /* Update the monster */
1197                 update_mon(i, FALSE);
1198         }
1199 }
1200
1201 /*!
1202  * @brief 固定マップクエストのフロア生成 / Generate a quest level
1203  * @return なし
1204  */
1205 static void quest_gen(void)
1206 {
1207         int x, y;
1208
1209
1210         /* Start with perm walls */
1211         for (y = 0; y < cur_hgt; y++)
1212         {
1213                 for (x = 0; x < cur_wid; x++)
1214                 {
1215                         place_solid_perm_bold(y, x);
1216                 }
1217         }
1218
1219         /* Set the quest level */
1220         base_level = quest[p_ptr->inside_quest].level;
1221         dun_level = base_level;
1222         object_level = base_level;
1223         monster_level = base_level;
1224
1225         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1226
1227         /* Prepare allocation table */
1228         get_mon_num_prep(get_monster_hook(), NULL);
1229
1230         init_flags = INIT_CREATE_DUNGEON;
1231
1232         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1233 }
1234
1235 /*!
1236  * @brief ダンジョン時のランダムフロア生成 / Make a real level
1237  * @return フロアの生成に成功したらTRUE
1238  */
1239 static bool level_gen(cptr *why)
1240 {
1241         int level_height, level_width;
1242
1243         if ((always_small_levels || ironman_small_levels ||
1244             (one_in_(SMALL_LEVEL) && small_levels) ||
1245              (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1246             (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1247             !(d_info[dungeon_type].flags1 & DF1_BIG))
1248         {
1249                 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1250                 {
1251                         level_height = 1;
1252                         level_width = 1;
1253                 }
1254                 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1255                 {
1256                         level_height = 2;
1257                         level_width = 2;
1258                 }
1259                 else
1260                 {
1261                         do
1262                         {
1263                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1264                                 level_width = randint1(MAX_WID/SCREEN_WID);
1265                         }
1266                         while ((level_height == MAX_HGT/SCREEN_HGT) &&
1267                                    (level_width == MAX_WID/SCREEN_WID));
1268                 }
1269
1270                 cur_hgt = level_height * SCREEN_HGT;
1271                 cur_wid = level_width * SCREEN_WID;
1272
1273                 /* Assume illegal panel */
1274                 panel_row_min = cur_hgt;
1275                 panel_col_min = cur_wid;
1276
1277                 msg_format_wizard(CHEAT_DUNGEON,
1278                         _("小さなフロア: X:%d, Y:%d", "A 'small' dungeon level: X:%d, Y:%d."),
1279                         cur_wid, cur_hgt);
1280         }
1281         else
1282         {
1283                 /* Big dungeon */
1284                 cur_hgt = MAX_HGT;
1285                 cur_wid = MAX_WID;
1286
1287                 /* Assume illegal panel */
1288                 panel_row_min = cur_hgt;
1289                 panel_col_min = cur_wid;
1290         }
1291
1292         /* Make a dungeon */
1293         if (!cave_gen())
1294         {
1295 #ifdef JP
1296 *why = "ダンジョン生成に失敗";
1297 #else
1298                 *why = "could not place player";
1299 #endif
1300
1301                 return FALSE;
1302         }
1303         else return TRUE;
1304 }
1305
1306 /*!
1307  * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after cave generation
1308  * @return なし
1309  */
1310 void wipe_generate_cave_flags(void)
1311 {
1312         int x, y;
1313
1314         for (y = 0; y < cur_hgt; y++)
1315         {
1316                 for (x = 0; x < cur_wid; x++)
1317                 {
1318                         /* Wipe unused flags */
1319                         cave[y][x].info &= ~(CAVE_MASK);
1320                 }
1321         }
1322
1323         if (dun_level)
1324         {
1325                 for (y = 1; y < cur_hgt - 1; y++)
1326                 {
1327                         for (x = 1; x < cur_wid - 1; x++)
1328                         {
1329                                 /* There might be trap */
1330                                 cave[y][x].info |= CAVE_UNSAFE;
1331                         }
1332                 }
1333         }
1334 }
1335
1336 /*!
1337  * @brief フロアの全情報を初期化する / Clear and empty the cave
1338  * @return なし
1339  */
1340 void clear_cave(void)
1341 {
1342         int x, y, i;
1343
1344         /* Very simplified version of wipe_o_list() */
1345         (void)C_WIPE(o_list, o_max, object_type);
1346         o_max = 1;
1347         o_cnt = 0;
1348
1349         /* Very simplified version of wipe_m_list() */
1350         for (i = 1; i < max_r_idx; i++)
1351                 r_info[i].cur_num = 0;
1352         (void)C_WIPE(m_list, m_max, monster_type);
1353         m_max = 1;
1354         m_cnt = 0;
1355         for (i = 0; i < MAX_MTIMED; i++) mproc_max[i] = 0;
1356
1357         /* Pre-calc cur_num of pets in party_mon[] */
1358         precalc_cur_num_of_pet();
1359
1360
1361         /* Start with a blank cave */
1362         for (y = 0; y < MAX_HGT; y++)
1363         {
1364                 for (x = 0; x < MAX_WID; x++)
1365                 {
1366                         cave_type *c_ptr = &cave[y][x];
1367
1368                         /* No flags */
1369                         c_ptr->info = 0;
1370
1371                         /* No features */
1372                         c_ptr->feat = 0;
1373
1374                         /* No objects */
1375                         c_ptr->o_idx = 0;
1376
1377                         /* No monsters */
1378                         c_ptr->m_idx = 0;
1379
1380                         /* No special */
1381                         c_ptr->special = 0;
1382
1383                         /* No mimic */
1384                         c_ptr->mimic = 0;
1385
1386                         /* No flow */
1387                         c_ptr->cost = 0;
1388                         c_ptr->dist = 0;
1389                         c_ptr->when = 0;
1390                 }
1391         }
1392
1393         /* Mega-Hack -- no player yet */
1394         p_ptr->x = p_ptr->y = 0;
1395
1396         /* Set the base level */
1397         base_level = dun_level;
1398
1399         /* Reset the monster generation level */
1400         monster_level = base_level;
1401
1402         /* Reset the object generation level */
1403         object_level = base_level;
1404 }
1405
1406
1407 /*!
1408  * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
1409  * @return なし
1410  * @note Hack -- regenerate any "overflow" levels
1411  */
1412 void generate_cave(void)
1413 {
1414         int num;
1415
1416         /* Fill the arrays of floors and walls in the good proportions */
1417         set_floor_and_wall(dungeon_type);
1418
1419         /* Generate */
1420         for (num = 0; TRUE; num++)
1421         {
1422                 bool okay = TRUE;
1423
1424                 cptr why = NULL;
1425
1426                 /* Clear and empty the cave */
1427                 clear_cave();
1428
1429                 /* Build the arena -KMW- */
1430                 if (p_ptr->inside_arena)
1431                 {
1432                         /* Small arena */
1433                         arena_gen();
1434                 }
1435
1436                 /* Build the battle -KMW- */
1437                 else if (p_ptr->inside_battle)
1438                 {
1439                         /* Small arena */
1440                         battle_gen();
1441                 }
1442
1443                 else if (p_ptr->inside_quest)
1444                 {
1445                         quest_gen();
1446                 }
1447
1448                 /* Build the town */
1449                 else if (!dun_level)
1450                 {
1451                         /* Make the wilderness */
1452                         if (p_ptr->wild_mode) wilderness_gen_small();
1453                         else wilderness_gen();
1454                 }
1455
1456                 /* Build a real level */
1457                 else
1458                 {
1459                         okay = level_gen(&why);
1460                 }
1461
1462
1463                 /* Prevent object over-flow */
1464                 if (o_max >= max_o_idx)
1465                 {
1466                         /* Message */
1467 #ifdef JP
1468 why = "アイテムが多すぎる";
1469 #else
1470                         why = "too many objects";
1471 #endif
1472
1473
1474                         /* Message */
1475                         okay = FALSE;
1476                 }
1477                 /* Prevent monster over-flow */
1478                 else if (m_max >= max_m_idx)
1479                 {
1480                         /* Message */
1481 #ifdef JP
1482 why = "モンスターが多すぎる";
1483 #else
1484                         why = "too many monsters";
1485 #endif
1486
1487
1488                         /* Message */
1489                         okay = FALSE;
1490                 }
1491
1492                 /* Accept */
1493                 if (okay) break;
1494
1495                 /* Message */
1496 #ifdef JP
1497 if (why) msg_format("生成やり直し(%s)", why);
1498 #else
1499                 if (why) msg_format("Generation restarted (%s)", why);
1500 #endif
1501
1502
1503                 /* Wipe the objects */
1504                 wipe_o_list();
1505
1506                 /* Wipe the monsters */
1507                 wipe_m_list();
1508         }
1509
1510         /* Glow deep lava and building entrances */
1511         glow_deep_lava_and_bldg();
1512
1513         /* Reset flag */
1514         p_ptr->enter_dungeon = FALSE;
1515
1516         wipe_generate_cave_flags();
1517 }