OSDN Git Service

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