OSDN Git Service

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