OSDN Git Service

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