OSDN Git Service

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