OSDN Git Service

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