OSDN Git Service

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