OSDN Git Service

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