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