OSDN Git Service

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