OSDN Git Service

ダンジョンの雰囲気が、フロアの生成時では無くリアルタイムに計算されるように変更した。
[hengband/hengband.git] / src / generate.c
1 /* File: generate.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: Dungeon generation */
12
13 /*
14  * Note that Level generation is *not* an important bottleneck,
15  * though it can be annoyingly slow on older machines...  Thus
16  * we emphasize "simplicity" and "correctness" over "speed".
17  *
18  * This entire file is only needed for generating levels.
19  * This may allow smart compilers to only load it when needed.
20  *
21  * Consider the "v_info.txt" file for vault generation.
22  *
23  * In this file, we use the "special" granite and perma-wall sub-types,
24  * where "basic" is normal, "inner" is inside a room, "outer" is the
25  * outer wall of a room, and "solid" is the outer wall of the dungeon
26  * or any walls that may not be pierced by corridors.  Thus the only
27  * wall type that may be pierced by a corridor is the "outer granite"
28  * type.  The "basic granite" type yields the "actual" corridors.
29  *
30  * Note that we use the special "solid" granite wall type to prevent
31  * multiple corridors from piercing a wall in two adjacent locations,
32  * which would be messy, and we use the special "outer" granite wall
33  * to indicate which walls "surround" rooms, and may thus be "pierced"
34  * by corridors entering or leaving the room.
35  *
36  * Note that a tunnel which attempts to leave a room near the "edge"
37  * of the dungeon in a direction toward that edge will cause "silly"
38  * wall piercings, but will have no permanently incorrect effects,
39  * as long as the tunnel can *eventually* exit from another side.
40  * And note that the wall may not come back into the room by the
41  * hole it left through, so it must bend to the left or right and
42  * then optionally re-enter the room (at least 2 grids away).  This
43  * is not a problem since every room that is large enough to block
44  * the passage of tunnels is also large enough to allow the tunnel
45  * to pierce the room itself several times.
46  *
47  * Note that no two corridors may enter a room through adjacent grids,
48  * they must either share an entryway or else use entryways at least
49  * two grids apart.  This prevents "large" (or "silly") doorways.
50  *
51  * To create rooms in the dungeon, we first divide the dungeon up
52  * into "blocks" of 11x11 grids each, and require that all rooms
53  * occupy a rectangular group of blocks.  As long as each room type
54  * reserves a sufficient number of blocks, the room building routines
55  * will not need to check bounds.  Note that most of the normal rooms
56  * actually only use 23x11 grids, and so reserve 33x11 grids.
57  *
58  * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)
59  * allows more variability in the horizontal placement of rooms, and
60  * at the same time has the disadvantage that some rooms (two thirds
61  * of the normal rooms) may be "split" by panel boundaries.  This can
62  * induce a situation where a player is in a room and part of the room
63  * is off the screen.  It may be annoying enough to go back to 33x11
64  * blocks to prevent this visual situation.
65  *
66  * Note that the dungeon generation routines are much different (2.7.5)
67  * and perhaps "DUN_ROOMS" should be less than 50.
68  *
69  * XXX XXX XXX Note that it is possible to create a room which is only
70  * connected to itself, because the "tunnel generation" code allows a
71  * tunnel to leave a room, wander around, and then re-enter the room.
72  *
73  * XXX XXX XXX Note that it is possible to create a set of rooms which
74  * are only connected to other rooms in that set, since there is nothing
75  * explicit in the code to prevent this from happening.  But this is less
76  * likely than the "isolated room" problem, because each room attempts to
77  * connect to another room, in a giant cycle, thus requiring at least two
78  * bizarre occurances to create an isolated section of the dungeon.
79  *
80  * Note that (2.7.9) monster pits have been split into monster "nests"
81  * and monster "pits".  The "nests" have a collection of monsters of a
82  * given type strewn randomly around the room (jelly, animal, or undead),
83  * while the "pits" have a collection of monsters of a given type placed
84  * around the room in an organized manner (orc, troll, giant, dragon, or
85  * demon).  Note that both "nests" and "pits" are now "level dependant",
86  * and both make 16 "expensive" calls to the "get_mon_num()" function.
87  *
88  * Note that the cave grid flags changed in a rather drastic manner
89  * for Angband 2.8.0 (and 2.7.9+), in particular, dungeon terrain
90  * features, such as doors and stairs and traps and rubble and walls,
91  * are all handled as a set of 64 possible "terrain features", and
92  * not as "fake" objects (440-479) as in pre-2.8.0 versions.
93  *
94  * The 64 new "dungeon features" will also be used for "visual display"
95  * but we must be careful not to allow, for example, the user to display
96  * hidden traps in a different way from floors, or secret doors in a way
97  * different from granite walls, or even permanent granite in a different
98  * way from granite.  XXX XXX XXX
99  */
100
101 #include "angband.h"
102 #include "generate.h"
103 #include "grid.h"
104 #include "rooms.h"
105 #include "streams.h"
106
107 int dun_tun_rnd;
108 int dun_tun_chg;
109 int dun_tun_con;
110 int dun_tun_pen;
111 int dun_tun_jct;
112
113
114 /*
115  * Dungeon generation data -- see "cave_gen()"
116  */
117 dun_data *dun;
118
119
120 /*
121  * Count the number of walls adjacent to the given grid.
122  *
123  * Note -- Assumes "in_bounds(y, x)"
124  *
125  * We count only granite walls and permanent walls.
126  */
127 static int next_to_walls(int y, int x)
128 {
129         int k = 0;
130
131         if (is_extra_bold(y + 1, x)) k++;
132         if (is_extra_bold(y - 1, x)) k++;
133         if (is_extra_bold(y, x + 1)) k++;
134         if (is_extra_bold(y, x - 1)) k++;
135
136         return (k);
137 }
138
139
140 /*
141  *  Helper function for alloc_stairs().
142  *
143  *  Is this a good location for stairs?
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  * Places some staircases near walls
164  */
165 static bool alloc_stairs(int feat, int num, int walls)
166 {
167         int i;
168         int shaft_num = 0;
169
170         feature_type *f_ptr = &f_info[feat];
171
172         if (have_flag(f_ptr->flags, FF_LESS))
173         {
174                 /* No up stairs in town or in ironman mode */
175                 if (ironman_downward || !dun_level) return TRUE;
176
177                 if (dun_level > d_info[dungeon_type].mindepth)
178                         shaft_num = (randint1(num+1))/2;
179         }
180         else if (have_flag(f_ptr->flags, FF_MORE))
181         {
182                 int q_idx = quest_number(dun_level);
183
184                 /* No downstairs on quest levels */
185                 if (dun_level > 1 && q_idx)
186                 {
187                         monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
188
189                         /* The quest monster(s) is still alive? */
190                         if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
191                                 return TRUE;
192                 }
193
194                 /* No downstairs at the bottom */
195                 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
196
197                 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
198                         shaft_num = (randint1(num)+1)/2;
199         }
200
201         /* Paranoia */
202         else return FALSE;
203
204
205         /* Place "num" stairs */
206         for (i = 0; i < num; i++)
207         {
208                 while (TRUE)
209                 {
210                         int y = 0, x = 0;
211                         cave_type *c_ptr;
212
213                         int candidates = 0;
214                         int pick;
215
216                         for (y = 1; y < cur_hgt - 1; y++)
217                         {
218                                 for (x = 1; x < cur_wid - 1; x++)
219                                 {
220                                         if (alloc_stairs_aux(y, x, walls))
221                                         {
222                                                 /* A valid space found */
223                                                 candidates++;
224                                         }
225                                 }
226                         }
227
228                         /* No valid place! */
229                         if (!candidates)
230                         {
231                                 /* There are exactly no place! */
232                                 if (walls <= 0) return FALSE;
233
234                                 /* Decrease walls limit, and try again */
235                                 walls--;
236                                 continue;
237                         }
238
239                         /* Choose a random one */
240                         pick = randint1(candidates);
241
242                         for (y = 1; y < cur_hgt - 1; y++)
243                         {
244                                 for (x = 1; x < cur_wid - 1; x++)
245                                 {
246                                         if (alloc_stairs_aux(y, x, walls))
247                                         {
248                                                 pick--;
249
250                                                 /* Is this a picked one? */
251                                                 if (!pick) break;
252                                         }
253                                 }
254
255                                 if (!pick) break;
256                         }
257
258                         /* Access the grid */
259                         c_ptr = &cave[y][x];
260
261                         /* Clear possible garbage of hidden trap */
262                         c_ptr->mimic = 0;
263
264                         /* Clear previous contents, add stairs */
265                         c_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
266
267                         /* Success */
268                         break;
269                 }
270         }
271         return TRUE;
272 }
273
274
275 /*
276  * Allocates some objects (using "place" and "type")
277  */
278 static void alloc_object(int set, int typ, int num)
279 {
280         int y = 0, x = 0, k;
281         int dummy = 0;
282         cave_type *c_ptr;
283
284         /* A small level has few objects. */
285         num = num * cur_hgt * cur_wid / (MAX_HGT*MAX_WID) +1;
286
287         /* Place some objects */
288         for (k = 0; k < num; k++)
289         {
290                 /* Pick a "legal" spot */
291                 while (dummy < SAFE_MAX_ATTEMPTS)
292                 {
293                         bool room;
294
295                         dummy++;
296
297                         /* Location */
298                         y = randint0(cur_hgt);
299                         x = randint0(cur_wid);
300
301                         c_ptr = &cave[y][x];
302
303                         /* Require "naked" floor grid */
304                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
305
306                         /* Avoid player location */
307                         if (player_bold(y, x)) continue;
308
309                         /* Check for "room" */
310                         room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
311
312                         /* Require corridor? */
313                         if ((set == ALLOC_SET_CORR) && room) continue;
314
315                         /* Require room? */
316                         if ((set == ALLOC_SET_ROOM) && !room) continue;
317
318                         /* Accept it */
319                         break;
320                 }
321
322                 if (dummy >= SAFE_MAX_ATTEMPTS)
323                 {
324                         if (cheat_room)
325                         {
326 #ifdef JP
327 msg_print("·Ù¹ð¡ª¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
328 #else
329                                 msg_print("Warning! Could not place object!");
330 #endif
331
332                         }
333                         return;
334                 }
335
336
337                 /* Place something */
338                 switch (typ)
339                 {
340                         case ALLOC_TYP_RUBBLE:
341                         {
342                                 place_rubble(y, x);
343                                 cave[y][x].info &= ~(CAVE_FLOOR);
344                                 break;
345                         }
346
347                         case ALLOC_TYP_TRAP:
348                         {
349                                 place_trap(y, x);
350                                 cave[y][x].info &= ~(CAVE_FLOOR);
351                                 break;
352                         }
353
354                         case ALLOC_TYP_GOLD:
355                         {
356                                 place_gold(y, x);
357                                 break;
358                         }
359
360                         case ALLOC_TYP_OBJECT:
361                         {
362                                 place_object(y, x, 0L);
363                                 break;
364                         }
365                 }
366         }
367 }
368
369
370 /*
371  * Count the number of "corridor" grids adjacent to the given grid.
372  *
373  * Note -- Assumes "in_bounds(y1, x1)"
374  *
375  * XXX XXX This routine currently only counts actual "empty floor"
376  * grids which are not in rooms.  We might want to also count stairs,
377  * open doors, closed doors, etc.
378  */
379 static int next_to_corr(int y1, int x1)
380 {
381         int i, y, x, k = 0;
382
383         cave_type *c_ptr;
384
385         /* Scan adjacent grids */
386         for (i = 0; i < 4; i++)
387         {
388                 /* Extract the location */
389                 y = y1 + ddy_ddd[i];
390                 x = x1 + ddx_ddd[i];
391
392                 /* Access the grid */
393                 c_ptr = &cave[y][x];
394
395                 /* Skip non floors */
396                 if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
397
398                 /* Skip non "empty floor" grids */
399                 if (!is_floor_grid(c_ptr))
400                         continue;
401
402                 /* Skip grids inside rooms */
403                 if (c_ptr->info & (CAVE_ROOM)) continue;
404
405                 /* Count these grids */
406                 k++;
407         }
408
409         /* Return the number of corridors */
410         return (k);
411 }
412
413
414 /*
415  * Determine if the given location is "between" two walls,
416  * and "next to" two corridor spaces.  XXX XXX XXX
417  *
418  * Assumes "in_bounds(y, x)"
419  */
420 static bool possible_doorway(int y, int x)
421 {
422         /* Count the adjacent corridors */
423         if (next_to_corr(y, x) >= 2)
424         {
425                 /* Check Vertical */
426                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
427                     cave_have_flag_bold(y + 1, x, FF_WALL))
428                 {
429                         return (TRUE);
430                 }
431
432                 /* Check Horizontal */
433                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
434                     cave_have_flag_bold(y, x + 1, FF_WALL))
435                 {
436                         return (TRUE);
437                 }
438         }
439
440         /* No doorway */
441         return (FALSE);
442 }
443
444
445 /*
446  * Places door at y, x position if at least 2 walls found
447  */
448 static void try_door(int y, int x)
449 {
450         /* Paranoia */
451         if (!in_bounds(y, x)) return;
452
453         /* Ignore walls */
454         if (cave_have_flag_bold(y, x, FF_WALL)) return;
455
456         /* Ignore room grids */
457         if (cave[y][x].info & (CAVE_ROOM)) return;
458
459         /* Occasional door (if allowed) */
460         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
461         {
462                 /* Place a door */
463                 place_random_door(y, x, FALSE);
464         }
465 }
466
467
468 /* Place quest monsters */
469 void place_quest_monsters(void)
470 {
471         int i;
472
473         /* Handle the quest monster placements */
474         for (i = 0; i < max_quests; i++)
475         {
476                 monster_race *r_ptr;
477                 u32b mode;
478                 int j;
479                         
480                 if (quest[i].status != QUEST_STATUS_TAKEN ||
481                     (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
482                      quest[i].type != QUEST_TYPE_RANDOM) ||
483                     quest[i].level != dun_level ||
484                     dungeon_type != quest[i].dungeon ||
485                     (quest[i].flags & QUEST_FLAG_PRESET))
486                 {
487                         /* Ignore it */
488                         continue;
489                 }
490
491                 r_ptr = &r_info[quest[i].r_idx];
492
493                 /* Hack -- "unique" monsters must be "unique" */
494                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
495                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
496
497                 mode = (PM_NO_KAGE | PM_NO_PET);
498
499                 if (!(r_ptr->flags1 & RF1_FRIENDS))
500                         mode |= PM_ALLOW_GROUP;
501
502                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
503                 {
504                         int k;
505
506                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
507                         {
508                                 int x, y;
509                                 int l;
510
511                                 /* Find an empty grid */
512                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
513                                 {
514                                         y = randint0(cur_hgt);
515                                         x = randint0(cur_wid);
516
517                                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
518                                         if (distance(y, x, py, px) < 10) continue;
519                                         else break;
520                                 }
521
522                                 /* Failed to place */
523                                 if (!l) break;
524
525                                 /* Try to place the monster */
526                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
527                                 {
528                                         /* Success */
529                                         break;
530                                 }
531                                 else
532                                 {
533                                         /* Failure - Try again */
534                                         continue;
535                                 }
536                         }
537                 }
538         }
539 }
540
541
542 /*
543  * Set boundary mimic and add "solid" perma-wall
544  */
545 static void set_bound_perm_wall(cave_type *c_ptr)
546 {
547         if (bound_walls_perm)
548         {
549                 /* Clear boundary mimic */
550                 c_ptr->mimic = 0;
551         }
552         else
553         {
554                 feature_type *f_ptr = &f_info[c_ptr->feat];
555
556                 /* Hack -- Decline boundary walls with known treasure  */
557                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
558                     !have_flag(f_ptr->flags, FF_SECRET))
559                         c_ptr->feat = feat_state(c_ptr->feat, FF_ENSECRET);
560
561                 /* Set boundary mimic */
562                 c_ptr->mimic = c_ptr->feat;
563         }
564
565         /* Add "solid" perma-wall */
566         place_solid_perm_grid(c_ptr);
567 }
568
569
570 /*
571  * Generate various caverns and lakes
572  *
573  * There were moved from cave_gen().
574  */
575 static void gen_caverns_and_lakes(void)
576 {
577 #ifdef ALLOW_CAVERNS_AND_LAKES
578         /* Possible "destroyed" level */
579         if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
580         {
581                 dun->destroyed = TRUE;
582
583                 /* extra rubble around the place looks cool */
584                 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
585         }
586
587         /* Make a lake some of the time */
588         if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
589             (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
590         {
591                 int count = 0;
592                 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
593                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
594                 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
595                 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
596
597                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
598                 {
599                         /* Lake of Lava */
600                         if ((dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
601                         count -= 2;
602
603                         /* Lake of Lava2 */
604                         if (!dun->laketype && (dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
605                         count--;
606                 }
607
608                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !dun->laketype)
609                 {
610                         /* Lake of Water */
611                         if ((dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
612                         count -= 2;
613
614                         /* Lake of Water2 */
615                         if (!dun->laketype && (dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
616                         count--;
617                 }
618
619                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
620                 {
621                         /* Lake of rubble */
622                         if ((dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
623                         count -= 2;
624
625                         /* Lake of rubble2 */
626                         if (!dun->laketype && (dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
627                         count--;
628                 }
629
630                 /* Lake of tree */
631                 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
632
633                 if (dun->laketype)
634                 {
635                         if (cheat_room)
636 #ifdef JP
637                                 msg_print("¸Ð¤òÀ¸À®¡£");
638 #else
639                                 msg_print("Lake on the level.");
640 #endif
641
642                         build_lake(dun->laketype);
643                 }
644         }
645
646         if ((dun_level > DUN_CAVERN) && !dun->empty_level &&
647             (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
648             !dun->laketype && !dun->destroyed && (randint1(1000) < dun_level))
649         {
650                 dun->cavern = TRUE;
651
652                 /* make a large fractal cave in the middle of the dungeon */
653
654                 if (cheat_room)
655 #ifdef JP
656                         msg_print("ƶ·¢¤òÀ¸À®¡£");
657 #else
658                         msg_print("Cavern on level.");
659 #endif
660
661                 build_cavern();
662         }
663 #endif /* ALLOW_CAVERNS_AND_LAKES */
664
665         /* Hack -- No destroyed "quest" levels */
666         if (quest_number(dun_level)) dun->destroyed = FALSE;
667 }
668
669
670
671 /*
672  * Generate a new dungeon level
673  *
674  * Note that "dun_body" adds about 4000 bytes of memory to the stack.
675  */
676 static bool cave_gen(void)
677 {
678         int i, k, y, x;
679
680         dun_data dun_body;
681
682         /* Global data */
683         dun = &dun_body;
684
685         dun->destroyed = FALSE;
686         dun->empty_level = FALSE;
687         dun->cavern = FALSE;
688         dun->laketype = 0;
689
690         /* Fill the arrays of floors and walls in the good proportions */
691         set_floor_and_wall(dungeon_type);
692
693         /* Prepare allocation table */
694         get_mon_num_prep(get_monster_hook(), NULL);
695
696         /* Randomize the dungeon creation values */
697         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
698         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
699         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
700         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
701         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
702
703         /* Actual maximum number of rooms on this level */
704         dun->row_rooms = cur_hgt / BLOCK_HGT;
705         dun->col_rooms = cur_wid / BLOCK_WID;
706
707         /* Initialize the room table */
708         for (y = 0; y < dun->row_rooms; y++)
709         {
710                 for (x = 0; x < dun->col_rooms; x++)
711                 {
712                         dun->room_map[y][x] = FALSE;
713                 }
714         }
715
716         /* No rooms yet */
717         dun->cent_n = 0;
718
719         /* Empty arena levels */
720         if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
721         {
722                 dun->empty_level = TRUE;
723
724                 if (cheat_room)
725 #ifdef JP
726                         msg_print("¥¢¥ê¡¼¥Ê¥ì¥Ù¥ë");
727 #else
728                         msg_print("Arena level.");
729 #endif
730         }
731
732         if (dun->empty_level)
733         {
734                 /* Start with floors */
735                 for (y = 0; y < cur_hgt; y++)
736                 {
737                         for (x = 0; x < cur_wid; x++)
738                         {
739                                 place_floor_bold(y, x);
740                         }
741                 }
742
743                 /* Special boundary walls -- Top and bottom */
744                 for (x = 0; x < cur_wid; x++)
745                 {
746                         place_extra_bold(0, x);
747                         place_extra_bold(cur_hgt - 1, x);
748                 }
749
750                 /* Special boundary walls -- Left and right */
751                 for (y = 1; y < (cur_hgt - 1); y++)
752                 {
753                         place_extra_bold(y, 0);
754                         place_extra_bold(y, cur_wid - 1);
755                 }
756         }
757         else
758         {
759                 /* Start with walls */
760                 for (y = 0; y < cur_hgt; y++)
761                 {
762                         for (x = 0; x < cur_wid; x++)
763                         {
764                                 place_extra_bold(y, x);
765                         }
766                 }
767         }
768
769
770         /* Generate various caverns and lakes */
771         gen_caverns_and_lakes();
772
773
774         /* Build maze */
775         if (d_info[dungeon_type].flags1 & DF1_MAZE)
776         {
777                 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
778
779                 /* Place 3 or 4 down stairs near some walls */
780                 if (!alloc_stairs(FEAT_MORE, rand_range(2, 3), 3)) return FALSE;
781
782                 /* Place 1 or 2 up stairs near some walls */
783                 if (!alloc_stairs(FEAT_LESS, 1, 3)) return FALSE;
784         }
785
786         /* Build some rooms */
787         else
788         {
789                 /*
790                  * Build each type of room in turn until we cannot build any more.
791                  */
792                 if (!generate_rooms()) return FALSE;
793
794
795                 /* Make a hole in the dungeon roof sometimes at level 1 */
796                 if (dun_level == 1)
797                 {
798                         while (one_in_(DUN_MOS_DEN))
799                         {
800                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
801                         }
802                 }
803
804                 /* Destroy the level if necessary */
805                 if (dun->destroyed) destroy_level();
806
807                 /* Hack -- Add some rivers */
808                 if (one_in_(3) && (randint1(dun_level) > 5))
809                 {
810                         int feat1 = 0, feat2 = 0;
811
812                         /* Choose water or lava */
813                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
814                         {
815                                 feat1 = FEAT_DEEP_WATER;
816                                 feat2 = FEAT_SHAL_WATER;
817                         }
818                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
819                         {
820                                 feat1 = FEAT_DEEP_LAVA;
821                                 feat2 = FEAT_SHAL_LAVA;
822                         }
823                         else feat1 = 0;
824
825                         if (feat1)
826                         {
827                                 feature_type *f_ptr = &f_info[feat1];
828
829                                 /* Only add river if matches lake type or if have no lake at all */
830                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
831                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
832                                      !dun->laketype)
833                                 {
834                                         add_river(feat1, feat2);
835                                 }
836                         }
837                 }
838
839                 /* Hack -- Scramble the room order */
840                 for (i = 0; i < dun->cent_n; i++)
841                 {
842                         int ty, tx;
843                         int pick = rand_range(0, i);
844
845                         ty = dun->cent[i].y;
846                         tx = dun->cent[i].x;
847                         dun->cent[i].y = dun->cent[pick].y;
848                         dun->cent[i].x = dun->cent[pick].x;
849                         dun->cent[pick].y = ty;
850                         dun->cent[pick].x = tx;
851                 }
852
853                 /* Start with no tunnel doors */
854                 dun->door_n = 0;
855
856                 /* Hack -- connect the first room to the last room */
857                 y = dun->cent[dun->cent_n-1].y;
858                 x = dun->cent[dun->cent_n-1].x;
859
860                 /* Connect all the rooms together */
861                 for (i = 0; i < dun->cent_n; i++)
862                 {
863                         int j;
864
865                         /* Reset the arrays */
866                         dun->tunn_n = 0;
867                         dun->wall_n = 0;
868
869                         /* Connect the room to the previous room */
870                         if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
871                         {
872                                 /* make cave-like tunnel */
873                                 build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
874                         }
875                         else
876                         {
877                                 /* make normal tunnel */
878                                 build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x);
879                         }
880
881                         /* Turn the tunnel into corridor */
882                         for (j = 0; j < dun->tunn_n; j++)
883                         {
884                                 cave_type *c_ptr;
885                                 feature_type *f_ptr;
886
887                                 /* Access the grid */
888                                 y = dun->tunn[j].y;
889                                 x = dun->tunn[j].x;
890
891                                 /* Access the grid */
892                                 c_ptr = &cave[y][x];
893                                 f_ptr = &f_info[c_ptr->feat];
894
895                                 /* Clear previous contents (if not a lake), add a floor */
896                                 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
897                                 {
898                                         /* Clear mimic type */
899                                         c_ptr->mimic = 0;
900
901                                         place_floor_grid(c_ptr);
902                                 }
903                         }
904
905                         /* Apply the piercings that we found */
906                         for (j = 0; j < dun->wall_n; j++)
907                         {
908                                 cave_type *c_ptr;
909
910                                 /* Access the grid */
911                                 y = dun->wall[j].y;
912                                 x = dun->wall[j].x;
913
914                                 /* Access the grid */
915                                 c_ptr = &cave[y][x];
916
917                                 /* Clear mimic type */
918                                 c_ptr->mimic = 0;
919
920                                 /* Clear previous contents, add up floor */
921                                 place_floor_grid(c_ptr);
922
923                                 /* Occasional doorway */
924                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
925                                 {
926                                         /* Place a random door */
927                                         place_random_door(y, x, TRUE);
928                                 }
929                         }
930
931                         /* Remember the "previous" room */
932                         y = dun->cent[i].y;
933                         x = dun->cent[i].x;
934                 }
935
936                 /* Place intersection doors */
937                 for (i = 0; i < dun->door_n; i++)
938                 {
939                         /* Extract junction location */
940                         y = dun->door[i].y;
941                         x = dun->door[i].x;
942
943                         /* Try placing doors */
944                         try_door(y, x - 1);
945                         try_door(y, x + 1);
946                         try_door(y - 1, x);
947                         try_door(y + 1, x);
948                 }
949
950                 /* Place 3 or 4 down stairs near some walls */
951                 if (!alloc_stairs(FEAT_MORE, rand_range(3, 4), 3)) return FALSE;
952
953                 /* Place 1 or 2 up stairs near some walls */
954                 if (!alloc_stairs(FEAT_LESS, rand_range(1, 2), 3)) return FALSE;
955         }
956
957         if (!dun->laketype)
958         {
959                 if (d_info[dungeon_type].stream2)
960                 {
961                         /* Hack -- Add some quartz streamers */
962                         for (i = 0; i < DUN_STR_QUA; i++)
963                         {
964                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
965                         }
966                 }
967
968                 if (d_info[dungeon_type].stream1)
969                 {
970                         /* Hack -- Add some magma streamers */
971                         for (i = 0; i < DUN_STR_MAG; i++)
972                         {
973                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
974                         }
975                 }
976         }
977
978         /* Special boundary walls -- Top and bottom */
979         for (x = 0; x < cur_wid; x++)
980         {
981                 set_bound_perm_wall(&cave[0][x]);
982                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
983         }
984
985         /* Special boundary walls -- Left and right */
986         for (y = 1; y < (cur_hgt - 1); y++)
987         {
988                 set_bound_perm_wall(&cave[y][0]);
989                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
990         }
991
992         /* Determine the character location */
993         if (!new_player_spot()) return FALSE;
994
995         place_quest_monsters();
996
997         /* Basic "amount" */
998         k = (dun_level / 3);
999         if (k > 10) k = 10;
1000         if (k < 2) k = 2;
1001
1002         /* Pick a base number of monsters */
1003         i = d_info[dungeon_type].min_m_alloc_level;
1004
1005         /* To make small levels a bit more playable */
1006         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1007         {
1008                 int small_tester = i;
1009
1010                 i = (i * cur_hgt) / MAX_HGT;
1011                 i = (i * cur_wid) / MAX_WID;
1012                 i += 1;
1013
1014                 if (i > small_tester) i = small_tester;
1015                 else if (cheat_hear)
1016                 {
1017 #ifdef JP
1018 msg_format("¥â¥ó¥¹¥¿¡¼¿ô´ðËÜÃͤò %d ¤«¤é %d ¤Ë¸º¤é¤·¤Þ¤¹", small_tester, i);
1019 #else
1020                         msg_format("Reduced monsters base from %d to %d", small_tester, i);
1021 #endif
1022
1023                 }
1024         }
1025
1026         i += randint1(8);
1027
1028         /* Put some monsters in the dungeon */
1029         for (i = i + k; i > 0; i--)
1030         {
1031                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1032         }
1033
1034         /* Place some traps in the dungeon */
1035         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1036
1037         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
1038         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1039
1040         /* Mega Hack -- No object at first level of deeper dungeon */
1041         if (p_ptr->enter_dungeon && dun_level > 1)
1042         {
1043                 /* No stair scum! */
1044                 object_level = 1;
1045         }
1046
1047         /* Put some objects in rooms */
1048         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1049
1050         /* Put some objects/gold in the dungeon */
1051         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1052         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1053
1054         /* Set back to default */
1055         object_level = base_level;
1056
1057         /* Put the Guardian */
1058         (void)alloc_guardian();
1059
1060         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1061         {
1062                 /* Lite the cave */
1063                 for (y = 0; y < cur_hgt; y++)
1064                 {
1065                         for (x = 0; x < cur_wid; x++)
1066                         {
1067                                 cave[y][x].info |= (CAVE_GLOW);
1068                         }
1069                 }
1070         }
1071
1072         return TRUE;
1073 }
1074
1075
1076 /*
1077  * Builds the arena after it is entered -KMW-
1078  */
1079 static void build_arena(void)
1080 {
1081         int yval, y_height, y_depth, xval, x_left, x_right;
1082         register int i, j;
1083
1084         yval = SCREEN_HGT / 2;
1085         xval = SCREEN_WID / 2;
1086         y_height = yval - 10;
1087         y_depth = yval + 10;
1088         x_left = xval - 32;
1089         x_right = xval + 32;
1090
1091         for (i = y_height; i <= y_height + 5; i++)
1092                 for (j = x_left; j <= x_right; j++)
1093                 {
1094                         place_extra_perm_bold(i, j);
1095                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1096                 }
1097         for (i = y_depth; i >= y_depth - 5; i--)
1098                 for (j = x_left; j <= x_right; j++)
1099                 {
1100                         place_extra_perm_bold(i, j);
1101                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1102                 }
1103         for (j = x_left; j <= x_left + 17; j++)
1104                 for (i = y_height; i <= y_depth; i++)
1105                 {
1106                         place_extra_perm_bold(i, j);
1107                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1108                 }
1109         for (j = x_right; j >= x_right - 17; j--)
1110                 for (i = y_height; i <= y_depth; i++)
1111                 {
1112                         place_extra_perm_bold(i, j);
1113                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1114                 }
1115
1116         place_extra_perm_bold(y_height+6, x_left+18);
1117         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1118         place_extra_perm_bold(y_depth-6, x_left+18);
1119         cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1120         place_extra_perm_bold(y_height+6, x_right-18);
1121         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1122         place_extra_perm_bold(y_depth-6, x_right-18);
1123         cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1124
1125         i = y_height + 5;
1126         j = xval;
1127         cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1128         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1129         player_place(i, j);
1130 }
1131
1132
1133 /*
1134  * Town logic flow for generation of arena -KMW-
1135  */
1136 static void arena_gen(void)
1137 {
1138         int y, x;
1139         int qy = 0;
1140         int qx = 0;
1141
1142         /* Smallest area */
1143         cur_hgt = SCREEN_HGT;
1144         cur_wid = SCREEN_WID;
1145
1146         /* Start with solid walls */
1147         for (y = 0; y < MAX_HGT; y++)
1148         {
1149                 for (x = 0; x < MAX_WID; x++)
1150                 {
1151                         /* Create "solid" perma-wall */
1152                         place_solid_perm_bold(y, x);
1153
1154                         /* Illuminate and memorize the walls */
1155                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1156                 }
1157         }
1158
1159         /* Then place some floors */
1160         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1161         {
1162                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1163                 {
1164                         /* Create empty floor */
1165                         cave[y][x].feat = FEAT_FLOOR;
1166                 }
1167         }
1168
1169         build_arena();
1170
1171         place_monster_aux(0, py + 5, px, arena_info[p_ptr->arena_number].r_idx,
1172             (PM_NO_KAGE | PM_NO_PET));
1173 }
1174
1175
1176
1177 /*
1178  * Builds the arena after it is entered -KMW-
1179  */
1180 static void build_battle(void)
1181 {
1182         int yval, y_height, y_depth, xval, x_left, x_right;
1183         register int i, j;
1184
1185         yval = SCREEN_HGT / 2;
1186         xval = SCREEN_WID / 2;
1187         y_height = yval - 10;
1188         y_depth = yval + 10;
1189         x_left = xval - 32;
1190         x_right = xval + 32;
1191
1192         for (i = y_height; i <= y_height + 5; i++)
1193                 for (j = x_left; j <= x_right; j++)
1194                 {
1195                         place_extra_perm_bold(i, j);
1196                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1197                 }
1198         for (i = y_depth; i >= y_depth - 3; i--)
1199                 for (j = x_left; j <= x_right; j++)
1200                 {
1201                         place_extra_perm_bold(i, j);
1202                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1203                 }
1204         for (j = x_left; j <= x_left + 17; j++)
1205                 for (i = y_height; i <= y_depth; i++)
1206                 {
1207                         place_extra_perm_bold(i, j);
1208                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1209                 }
1210         for (j = x_right; j >= x_right - 17; j--)
1211                 for (i = y_height; i <= y_depth; i++)
1212                 {
1213                         place_extra_perm_bold(i, j);
1214                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1215                 }
1216
1217         place_extra_perm_bold(y_height+6, x_left+18);
1218         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1219         place_extra_perm_bold(y_depth-4, x_left+18);
1220         cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1221         place_extra_perm_bold(y_height+6, x_right-18);
1222         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1223         place_extra_perm_bold(y_depth-4, x_right-18);
1224         cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1225
1226         i = y_height + 4;
1227         j = xval;
1228         cave[i][j].feat = FEAT_BLDG_HEAD + 3;
1229         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1230         player_place(i, j);
1231 }
1232
1233
1234 /*
1235  * Town logic flow for generation of arena -KMW-
1236  */
1237 static void battle_gen(void)
1238 {
1239         int y, x, i;
1240         int qy = 0;
1241         int qx = 0;
1242
1243         /* Start with solid walls */
1244         for (y = 0; y < MAX_HGT; y++)
1245         {
1246                 for (x = 0; x < MAX_WID; x++)
1247                 {
1248                         /* Create "solid" perma-wall */
1249                         place_solid_perm_bold(y, x);
1250
1251                         /* Illuminate and memorize the walls */
1252                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1253                 }
1254         }
1255
1256         /* Then place some floors */
1257         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1258         {
1259                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1260                 {
1261                         /* Create empty floor */
1262                         cave[y][x].feat = FEAT_FLOOR;
1263                 }
1264         }
1265
1266         build_battle();
1267
1268         for(i=0;i<4;i++)
1269         {
1270                 place_monster_aux(0, py + 5 + (i/2)*4, px - 2 + (i%2)*4, battle_mon[i],
1271                                   (PM_NO_KAGE | PM_NO_PET));
1272                 set_friendly(&m_list[cave[py+5+(i/2)*4][px-2+(i%2)*4].m_idx]);
1273         }
1274         for(i = 1; i < m_max; i++)
1275         {
1276                 monster_type *m_ptr = &m_list[i];
1277
1278                 if (!m_ptr->r_idx) continue;
1279
1280                 /* Hack -- Detect monster */
1281                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1282
1283                 /* Update the monster */
1284                 update_mon(i, FALSE);
1285         }
1286 }
1287
1288
1289 /*
1290  * Generate a quest level
1291  */
1292 static void quest_gen(void)
1293 {
1294         int x, y;
1295
1296
1297         /* Start with perm walls */
1298         for (y = 0; y < cur_hgt; y++)
1299         {
1300                 for (x = 0; x < cur_wid; x++)
1301                 {
1302                         place_solid_perm_bold(y, x);
1303                 }
1304         }
1305
1306         /* Set the quest level */
1307         base_level = quest[p_ptr->inside_quest].level;
1308         dun_level = base_level;
1309         object_level = base_level;
1310         monster_level = base_level;
1311
1312         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1313
1314         /* Prepare allocation table */
1315         get_mon_num_prep(get_monster_hook(), NULL);
1316
1317         init_flags = INIT_CREATE_DUNGEON | INIT_ASSIGN;
1318
1319         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1320 }
1321
1322 /* Make a real level */
1323 static bool level_gen(cptr *why)
1324 {
1325         int level_height, level_width;
1326
1327         if ((always_small_levels || ironman_small_levels ||
1328             (one_in_(SMALL_LEVEL) && small_levels) ||
1329              (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1330             (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1331             !(d_info[dungeon_type].flags1 & DF1_BIG))
1332         {
1333                 if (cheat_room)
1334 #ifdef JP
1335                         msg_print("¾®¤µ¤Ê¥Õ¥í¥¢");
1336 #else
1337                         msg_print("A 'small' dungeon level.");
1338 #endif
1339
1340                 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1341                 {
1342                         level_height = 1;
1343                         level_width = 1;
1344                 }
1345                 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1346                 {
1347                         level_height = 2;
1348                         level_width = 2;
1349                 }
1350                 else
1351                 {
1352                         do
1353                         {
1354                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1355                                 level_width = randint1(MAX_WID/SCREEN_WID);
1356                         }
1357                         while ((level_height == MAX_HGT/SCREEN_HGT) &&
1358                                    (level_width == MAX_WID/SCREEN_WID));
1359                 }
1360
1361                 cur_hgt = level_height * SCREEN_HGT;
1362                 cur_wid = level_width * SCREEN_WID;
1363
1364                 /* Assume illegal panel */
1365                 panel_row_min = cur_hgt;
1366                 panel_col_min = cur_wid;
1367
1368                 if (cheat_room)
1369                   msg_format("X:%d, Y:%d.", cur_hgt, cur_wid);
1370         }
1371         else
1372         {
1373                 /* Big dungeon */
1374                 cur_hgt = MAX_HGT;
1375                 cur_wid = MAX_WID;
1376
1377                 /* Assume illegal panel */
1378                 panel_row_min = cur_hgt;
1379                 panel_col_min = cur_wid;
1380         }
1381
1382         /* Make a dungeon */
1383         if (!cave_gen())
1384         {
1385 #ifdef JP
1386 *why = "¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ";
1387 #else
1388                 *why = "could not place player";
1389 #endif
1390
1391                 return FALSE;
1392         }
1393         else return TRUE;
1394 }
1395
1396
1397 /*
1398  * Wipe all unnecessary flags after cave generation
1399  */
1400 static void wipe_generate_cave_flags(void)
1401 {
1402         int x, y;
1403
1404         for (y = 0; y < cur_hgt; y++)
1405         {
1406                 for (x = 0; x < cur_wid; x++)
1407                 {
1408                         /* Wipe unused flags */
1409                         cave[y][x].info &= ~(CAVE_MASK);
1410                 }
1411         }
1412
1413         if (dun_level)
1414         {
1415                 for (y = 1; y < cur_hgt - 1; y++)
1416                 {
1417                         for (x = 1; x < cur_wid - 1; x++)
1418                         {
1419                                 /* There might be trap */
1420                                 cave[y][x].info |= CAVE_UNSAFE;
1421                         }
1422                 }
1423         }
1424 }
1425
1426
1427 /*
1428  *  Clear and empty the cave
1429  */
1430 void clear_cave(void)
1431 {
1432         int x, y, i;
1433
1434         /* Very simplified version of wipe_o_list() */
1435         C_WIPE(o_list, o_max, object_type);
1436         o_max = 1;
1437         o_cnt = 0;
1438
1439         /* Very simplified version of wipe_m_list() */
1440         for (i = 1; i < max_r_idx; i++)
1441                 r_info[i].cur_num = 0;
1442         C_WIPE(m_list, m_max, monster_type);
1443         m_max = 1;
1444         m_cnt = 0;
1445
1446         /* Pre-calc cur_num of pets in party_mon[] */
1447         precalc_cur_num_of_pet();
1448
1449
1450         /* Start with a blank cave */
1451         for (y = 0; y < MAX_HGT; y++)
1452         {
1453                 for (x = 0; x < MAX_WID; x++)
1454                 {
1455                         cave_type *c_ptr = &cave[y][x];
1456
1457                         /* No flags */
1458                         c_ptr->info = 0;
1459
1460                         /* No features */
1461                         c_ptr->feat = 0;
1462
1463                         /* No objects */
1464                         c_ptr->o_idx = 0;
1465
1466                         /* No monsters */
1467                         c_ptr->m_idx = 0;
1468
1469                         /* No special */
1470                         c_ptr->special = 0;
1471
1472                         /* No mimic */
1473                         c_ptr->mimic = 0;
1474
1475                         /* No flow */
1476                         c_ptr->cost = 0;
1477                         c_ptr->dist = 0;
1478                         c_ptr->when = 0;
1479                 }
1480         }
1481
1482         /* Mega-Hack -- no player yet */
1483         px = py = 0;
1484
1485         /* Set the base level */
1486         base_level = dun_level;
1487
1488         /* Reset the monster generation level */
1489         monster_level = base_level;
1490
1491         /* Reset the object generation level */
1492         object_level = base_level;
1493 }
1494
1495
1496 /*
1497  * Generates a random dungeon level                     -RAK-
1498  *
1499  * Hack -- regenerate any "overflow" levels
1500  */
1501 void generate_cave(void)
1502 {
1503         int num;
1504
1505         /* Fill the arrays of floors and walls in the good proportions */
1506         set_floor_and_wall(dungeon_type);
1507
1508         /* Generate */
1509         for (num = 0; TRUE; num++)
1510         {
1511                 bool okay = TRUE;
1512
1513                 cptr why = NULL;
1514
1515                 /* Clear and empty the cave */
1516                 clear_cave();
1517
1518                 /* Build the arena -KMW- */
1519                 if (p_ptr->inside_arena)
1520                 {
1521                         /* Small arena */
1522                         arena_gen();
1523                 }
1524
1525                 /* Build the battle -KMW- */
1526                 else if (p_ptr->inside_battle)
1527                 {
1528                         /* Small arena */
1529                         battle_gen();
1530                 }
1531
1532                 else if (p_ptr->inside_quest)
1533                 {
1534                         quest_gen();
1535                 }
1536
1537                 /* Build the town */
1538                 else if (!dun_level)
1539                 {
1540                         /* Make the wilderness */
1541                         if (p_ptr->wild_mode) wilderness_gen_small();
1542                         else wilderness_gen();
1543                 }
1544
1545                 /* Build a real level */
1546                 else
1547                 {
1548                         okay = level_gen(&why);
1549                 }
1550
1551
1552                 /* Prevent object over-flow */
1553                 if (o_max >= max_o_idx)
1554                 {
1555                         /* Message */
1556 #ifdef JP
1557 why = "¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë";
1558 #else
1559                         why = "too many objects";
1560 #endif
1561
1562
1563                         /* Message */
1564                         okay = FALSE;
1565                 }
1566                 /* Prevent monster over-flow */
1567                 else if (m_max >= max_m_idx)
1568                 {
1569                         /* Message */
1570 #ifdef JP
1571 why = "¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë";
1572 #else
1573                         why = "too many monsters";
1574 #endif
1575
1576
1577                         /* Message */
1578                         okay = FALSE;
1579                 }
1580
1581                 /* Accept */
1582                 if (okay) break;
1583
1584                 /* Message */
1585 #ifdef JP
1586 if (why) msg_format("À¸À®¤ä¤êľ¤·(%s)", why);
1587 #else
1588                 if (why) msg_format("Generation restarted (%s)", why);
1589 #endif
1590
1591
1592                 /* Wipe the objects */
1593                 wipe_o_list();
1594
1595                 /* Wipe the monsters */
1596                 wipe_m_list();
1597         }
1598
1599         /* Glow deep lava and building entrances */
1600         glow_deep_lava_and_bldg();
1601
1602         /* Reset flag */
1603         p_ptr->enter_dungeon = FALSE;
1604
1605         wipe_generate_cave_flags();
1606 }