OSDN Git Service

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