OSDN Git Service

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