OSDN Git Service

This commit was manufactured by cvs2svn to create tag
[hengbandforosx/hengbandosx.git] / src / generate.c
1 /* File: generate.c */
2
3 /*
4  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
5  *
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  */
10
11 /* Purpose: Dungeon generation */
12
13 /*
14  * Note that Level generation is *not* an important bottleneck,
15  * though it can be annoyingly slow on older machines...  Thus
16  * we emphasize "simplicity" and "correctness" over "speed".
17  *
18  * This entire file is only needed for generating levels.
19  * This may allow smart compilers to only load it when needed.
20  *
21  * Consider the "v_info.txt" file for vault generation.
22  *
23  * In this file, we use the "special" granite and perma-wall sub-types,
24  * where "basic" is normal, "inner" is inside a room, "outer" is the
25  * outer wall of a room, and "solid" is the outer wall of the dungeon
26  * or any walls that may not be pierced by corridors.  Thus the only
27  * wall type that may be pierced by a corridor is the "outer granite"
28  * type.  The "basic granite" type yields the "actual" corridors.
29  *
30  * Note that we use the special "solid" granite wall type to prevent
31  * multiple corridors from piercing a wall in two adjacent locations,
32  * which would be messy, and we use the special "outer" granite wall
33  * to indicate which walls "surround" rooms, and may thus be "pierced"
34  * by corridors entering or leaving the room.
35  *
36  * Note that a tunnel which attempts to leave a room near the "edge"
37  * of the dungeon in a direction toward that edge will cause "silly"
38  * wall piercings, but will have no permanently incorrect effects,
39  * as long as the tunnel can *eventually* exit from another side.
40  * And note that the wall may not come back into the room by the
41  * hole it left through, so it must bend to the left or right and
42  * then optionally re-enter the room (at least 2 grids away).  This
43  * is not a problem since every room that is large enough to block
44  * the passage of tunnels is also large enough to allow the tunnel
45  * to pierce the room itself several times.
46  *
47  * Note that no two corridors may enter a room through adjacent grids,
48  * they must either share an entryway or else use entryways at least
49  * two grids apart.  This prevents "large" (or "silly") doorways.
50  *
51  * To create rooms in the dungeon, we first divide the dungeon up
52  * into "blocks" of 11x11 grids each, and require that all rooms
53  * occupy a rectangular group of blocks.  As long as each room type
54  * reserves a sufficient number of blocks, the room building routines
55  * will not need to check bounds.  Note that most of the normal rooms
56  * actually only use 23x11 grids, and so reserve 33x11 grids.
57  *
58  * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)
59  * allows more variability in the horizontal placement of rooms, and
60  * at the same time has the disadvantage that some rooms (two thirds
61  * of the normal rooms) may be "split" by panel boundaries.  This can
62  * induce a situation where a player is in a room and part of the room
63  * is off the screen.  It may be annoying enough to go back to 33x11
64  * blocks to prevent this visual situation.
65  *
66  * Note that the dungeon generation routines are much different (2.7.5)
67  * and perhaps "DUN_ROOMS" should be less than 50.
68  *
69  * XXX XXX XXX Note that it is possible to create a room which is only
70  * connected to itself, because the "tunnel generation" code allows a
71  * tunnel to leave a room, wander around, and then re-enter the room.
72  *
73  * XXX XXX XXX Note that it is possible to create a set of rooms which
74  * are only connected to other rooms in that set, since there is nothing
75  * explicit in the code to prevent this from happening.  But this is less
76  * likely than the "isolated room" problem, because each room attempts to
77  * connect to another room, in a giant cycle, thus requiring at least two
78  * bizarre occurances to create an isolated section of the dungeon.
79  *
80  * Note that (2.7.9) monster pits have been split into monster "nests"
81  * and monster "pits".  The "nests" have a collection of monsters of a
82  * given type strewn randomly around the room (jelly, animal, or undead),
83  * while the "pits" have a collection of monsters of a given type placed
84  * around the room in an organized manner (orc, troll, giant, dragon, or
85  * demon).  Note that both "nests" and "pits" are now "level dependant",
86  * and both make 16 "expensive" calls to the "get_mon_num()" function.
87  *
88  * Note that the cave grid flags changed in a rather drastic manner
89  * for Angband 2.8.0 (and 2.7.9+), in particular, dungeon terrain
90  * features, such as doors and stairs and traps and rubble and walls,
91  * are all handled as a set of 64 possible "terrain features", and
92  * not as "fake" objects (440-479) as in pre-2.8.0 versions.
93  *
94  * The 64 new "dungeon features" will also be used for "visual display"
95  * but we must be careful not to allow, for example, the user to display
96  * hidden traps in a different way from floors, or secret doors in a way
97  * different from granite walls, or even permanent granite in a different
98  * way from granite.  XXX XXX XXX
99  */
100
101 #include "angband.h"
102 #include "generate.h"
103 #include "grid.h"
104 #include "rooms.h"
105 #include "streams.h"
106
107 int dun_rooms;
108
109 int dun_tun_rnd;
110 int dun_tun_chg;
111 int dun_tun_con;
112 int dun_tun_pen;
113 int dun_tun_jct;
114
115
116 /*
117  * Dungeon generation data -- see "cave_gen()"
118  */
119 dun_data *dun;
120
121 /*
122  * Places some staircases near walls
123  */
124 static bool alloc_stairs(int feat, int num, int walls)
125 {
126         int         y, x, i, j, flag;
127         int         more_num = 0;
128         cave_type   *c_ptr;
129
130         if (feat == FEAT_LESS)
131         {
132                 /* No up stairs in town or in ironman mode */
133                 if (ironman_downward || !dun_level) return TRUE;
134
135                 if (dun_level > d_info[dungeon_type].mindepth)
136                         more_num = (randint1(num+1))/2;
137         }
138         else if (feat == FEAT_MORE)
139         {
140                 int q_idx = quest_number(dun_level);
141
142                 /* No downstairs on quest levels */
143                 if (dun_level > 1 && q_idx)
144                 {
145                         monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
146
147                         /* The quest monster(s) is still alive? */
148                         if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
149                                 return TRUE;
150                 }
151
152                 /* No downstairs at the bottom */
153                 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
154
155                 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
156                         more_num = (randint1(num)+1)/2;
157         }
158
159         /* Place "num" stairs */
160         for (i = 0; i < num; i++)
161         {
162                 /* Place some stairs */
163                 for (flag = FALSE; !flag; )
164                 {
165                         /* Try several times, then decrease "walls" */
166                         for (j = 0; !flag && j <= 3000; j++)
167                         {
168                                 /* Pick a random grid */
169                                 y = randint1(cur_hgt-2);
170                                 x = randint1(cur_wid-2);
171
172                                 /* Access the grid */
173                                 c_ptr = &cave[y][x];
174
175                                 /* Require "naked" floor grid */
176                                 if (!is_floor_grid(c_ptr) || pattern_tile(y,x) || c_ptr->o_idx || c_ptr->m_idx) continue;
177
178                                 /* Require a certain number of adjacent walls */
179                                 if (next_to_walls(y, x) < walls) continue;
180
181                                 /* Clear possible garbage of hidden trap */
182                                 c_ptr->mimic = 0;
183
184                                 /* Clear previous contents, add stairs */
185                                 if (i < more_num) c_ptr->feat = feat+0x07;
186                                 else c_ptr->feat = feat;
187
188                                 /* No longer a "FLOOR" */
189                                 c_ptr->info &= ~(CAVE_FLOOR);
190
191                                 /* All done */
192                                 flag = TRUE;
193                         }
194
195                         if (!flag) return FALSE;
196                         /* Require fewer walls */
197                         if (walls) walls--;
198                 }
199         }
200         return TRUE;
201 }
202
203
204 /*
205  * Allocates some objects (using "place" and "type")
206  */
207 static void alloc_object(int set, int typ, int num)
208 {
209         int y = 0, x = 0, k;
210         int dummy = 0;
211         cave_type *c_ptr;
212
213         /* Place some objects */
214         for (k = 0; k < num; k++)
215         {
216                 /* Pick a "legal" spot */
217                 while (dummy < SAFE_MAX_ATTEMPTS)
218                 {
219                         bool room;
220
221                         dummy++;
222
223                         /* Location */
224                         y = randint0(cur_hgt);
225                         x = randint0(cur_wid);
226
227                         c_ptr = &cave[y][x];
228
229                         /* Require "naked" floor grid */
230                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
231
232                         /* Avoid player location */
233                         if (player_bold(y, x)) continue;
234
235                         /* Check for "room" */
236                         room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
237
238                         /* Require corridor? */
239                         if ((set == ALLOC_SET_CORR) && room) continue;
240
241                         /* Require room? */
242                         if ((set == ALLOC_SET_ROOM) && !room) continue;
243
244                         /* Accept it */
245                         break;
246                 }
247
248                 if (dummy >= SAFE_MAX_ATTEMPTS)
249                 {
250                         if (cheat_room)
251                         {
252 #ifdef JP
253 msg_print("·Ù¹ð¡ª¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
254 #else
255                                 msg_print("Warning! Could not place object!");
256 #endif
257
258                         }
259                         return;
260                 }
261
262
263                 /* Place something */
264                 switch (typ)
265                 {
266                         case ALLOC_TYP_RUBBLE:
267                         {
268                                 place_rubble(y, x);
269                                 cave[y][x].info &= ~(CAVE_FLOOR);
270                                 break;
271                         }
272
273                         case ALLOC_TYP_TRAP:
274                         {
275                                 place_trap(y, x);
276                                 cave[y][x].info &= ~(CAVE_FLOOR);
277                                 break;
278                         }
279
280                         case ALLOC_TYP_GOLD:
281                         {
282                                 place_gold(y, x);
283                                 break;
284                         }
285
286                         case ALLOC_TYP_OBJECT:
287                         {
288                                 place_object(y, x, 0L);
289                                 break;
290                         }
291                 }
292         }
293 }
294
295
296 /*
297  * Count the number of "corridor" grids adjacent to the given grid.
298  *
299  * Note -- Assumes "in_bounds(y1, x1)"
300  *
301  * XXX XXX This routine currently only counts actual "empty floor"
302  * grids which are not in rooms.  We might want to also count stairs,
303  * open doors, closed doors, etc.
304  */
305 static int next_to_corr(int y1, int x1)
306 {
307         int i, y, x, k = 0;
308
309         cave_type *c_ptr;
310
311         /* Scan adjacent grids */
312         for (i = 0; i < 4; i++)
313         {
314                 /* Extract the location */
315                 y = y1 + ddy_ddd[i];
316                 x = x1 + ddx_ddd[i];
317
318                 /* Access the grid */
319                 c_ptr = &cave[y][x];
320
321                 /* Skip non floors */
322                 if (!cave_floor_grid(c_ptr)) continue;
323
324                 /* Skip non "empty floor" grids */
325                 if (!is_floor_grid(c_ptr))
326                         continue;
327
328                 /* Skip grids inside rooms */
329                 if (c_ptr->info & (CAVE_ROOM)) continue;
330
331                 /* Count these grids */
332                 k++;
333         }
334
335         /* Return the number of corridors */
336         return (k);
337 }
338
339
340 /*
341  * Determine if the given location is "between" two walls,
342  * and "next to" two corridor spaces.  XXX XXX XXX
343  *
344  * Assumes "in_bounds(y, x)"
345  */
346 static bool possible_doorway(int y, int x)
347 {
348         /* Count the adjacent corridors */
349         if (next_to_corr(y, x) >= 2)
350         {
351                 /* Check Vertical */
352                 if ((cave[y-1][x].feat >= FEAT_MAGMA) &&
353                     (cave[y+1][x].feat >= FEAT_MAGMA))
354                 {
355                         return (TRUE);
356                 }
357
358                 /* Check Horizontal */
359                 if ((cave[y][x-1].feat >= FEAT_MAGMA) &&
360                     (cave[y][x+1].feat >= FEAT_MAGMA))
361                 {
362                         return (TRUE);
363                 }
364         }
365
366         /* No doorway */
367         return (FALSE);
368 }
369
370
371 /*
372  * Places door at y, x position if at least 2 walls found
373  */
374 static void try_door(int y, int x)
375 {
376         /* Paranoia */
377         if (!in_bounds(y, x)) return;
378
379         /* Ignore walls */
380         if (!cave_floor_bold(y, x)) return;
381
382         /* Ignore room grids */
383         if (cave[y][x].info & (CAVE_ROOM)) return;
384
385         /* Occasional door (if allowed) */
386         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
387         {
388                 /* Place a door */
389                 place_random_door(y, x, FALSE);
390         }
391 }
392
393
394 /* Place quest monsters */
395 bool place_quest_monsters(void)
396 {
397         int i;
398
399         /* Handle the quest monster placements */
400         for (i = 0; i < max_quests; i++)
401         {
402                 monster_race *r_ptr;
403                 u32b mode;
404                 int j;
405                         
406                 if (quest[i].status != QUEST_STATUS_TAKEN ||
407                     (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
408                      quest[i].type != QUEST_TYPE_RANDOM) ||
409                     quest[i].level != dun_level ||
410                     dungeon_type != quest[i].dungeon ||
411                     (quest[i].flags & QUEST_FLAG_PRESET))
412                 {
413                         /* Ignore it */
414                         continue;
415                 }
416
417                 r_ptr = &r_info[quest[i].r_idx];
418
419                 /* Hack -- "unique" monsters must be "unique" */
420                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
421                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
422
423                 mode = (PM_NO_KAGE | PM_NO_PET);
424
425                 if (!(r_ptr->flags1 & RF1_FRIENDS))
426                         mode |= PM_ALLOW_GROUP;
427
428                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
429                 {
430                         int k;
431
432                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
433                         {
434                                 int x, y;
435                                 int l;
436
437                                 /* Find an empty grid */
438                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
439                                 {
440                                         cave_type *c_ptr;
441
442                                         y = randint0(cur_hgt);
443                                         x = randint0(cur_wid);
444                                         c_ptr = &cave[y][x];
445
446                                         if (!cave_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
447                                         if (distance(y, x, py, px) < 10) continue;
448                                         if (c_ptr->info & CAVE_ICKY) continue;
449                                         else break;
450                                 }
451
452                                 /* Failed to place */
453                                 if (!l) return FALSE;
454
455                                 /* Try to place the monster */
456                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
457                                 {
458                                         /* Success */
459                                         break;
460                                 }
461                                 else
462                                 {
463                                         /* Failure - Try again */
464                                         continue;
465                                 }
466                         }
467
468                         /* Failed to place */
469                         if (k == SAFE_MAX_ATTEMPTS) return FALSE;
470                 }
471         }
472
473         return TRUE;
474 }
475
476
477 /*
478  * Set boundary mimic and add "solid" perma-wall
479  */
480 static void set_bound_perm_wall(cave_type *c_ptr)
481 {
482         if (bound_walls_perm)
483         {
484                 /* Clear boundary mimic */
485                 c_ptr->mimic = 0;
486         }
487         else
488         {
489                 /* Hack -- Decline boundary walls with known treasure  */
490                 if ((c_ptr->feat == FEAT_MAGMA_K) || (c_ptr->feat == FEAT_QUARTZ_K))
491                         c_ptr->feat -= (FEAT_MAGMA_K - FEAT_MAGMA);
492
493                 /* Set boundary mimic */
494                 c_ptr->mimic = c_ptr->feat;
495         }
496
497         /* Add "solid" perma-wall */
498         c_ptr->feat = FEAT_PERM_SOLID;
499 }
500
501
502 /*
503  * Generate a new dungeon level
504  *
505  * Note that "dun_body" adds about 4000 bytes of memory to the stack.
506  */
507 static bool cave_gen(void)
508 {
509         int i, j, k, y, x, y1, x1;
510
511         int max_vault_ok = 2;
512
513         int feat1 = 0, feat2 = 0;
514
515         cave_type *c_ptr;
516
517         bool destroyed = FALSE;
518         bool empty_level = FALSE;
519         bool cavern = FALSE;
520         int laketype = 0;
521
522
523         dun_data dun_body;
524
525         /* Fill the arrays of floors and walls in the good proportions */
526         set_floor_and_wall(dungeon_type);
527
528
529         /* Prepare allocation table */
530         get_mon_num_prep(get_monster_hook(), NULL);
531
532         feat_wall_outer = d_info[dungeon_type].outer_wall;
533         feat_wall_inner = d_info[dungeon_type].inner_wall;
534         feat_wall_solid = d_info[dungeon_type].outer_wall;
535
536         /* Global data */
537         dun = &dun_body;
538
539         if (cur_hgt <= SCREEN_HGT / 2 - 2) max_vault_ok--;
540         if (cur_wid <= SCREEN_WID / 2 - 2) max_vault_ok--;
541
542         /* Randomize the dungeon creation values */
543         dun_rooms = rand_range(DUN_ROOMS_MIN, DUN_ROOMS_MAX);
544         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
545         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
546         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
547         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
548         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
549
550         /* Actual maximum number of rooms on this level */
551         dun->row_rooms = cur_hgt / BLOCK_HGT;
552         dun->col_rooms = cur_wid / BLOCK_WID;
553
554         /* Initialize the room table */
555         for (y = 0; y < dun->row_rooms; y++)
556         {
557                 for (x = 0; x < dun->col_rooms; x++)
558                 {
559                         dun->room_map[y][x] = FALSE;
560                 }
561         }
562
563         /* No "crowded" rooms yet */
564         dun->crowded = 0;
565
566         /* No rooms yet */
567         dun->cent_n = 0;
568
569         /* Empty arena levels */
570         if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
571         {
572                 empty_level = TRUE;
573
574                 if (cheat_room)
575 #ifdef JP
576                         msg_print("¥¢¥ê¡¼¥Ê¥ì¥Ù¥ë");
577 #else
578                         msg_print("Arena level.");
579 #endif
580         }
581
582         if (empty_level)
583         {
584                 /* Start with floors */
585                 for (y = 0; y < cur_hgt; y++)
586                 {
587                         for (x = 0; x < cur_wid; x++)
588                         {
589                                 place_floor_bold(y, x);
590                         }
591                 }
592
593                 /* Special boundary walls -- Top and bottom */
594                 for (x = 0; x < cur_wid; x++)
595                 {
596                         place_extra_bold(0, x);
597                         place_extra_bold(cur_hgt - 1, x);
598                 }
599
600                 /* Special boundary walls -- Left and right */
601                 for (y = 1; y < (cur_hgt - 1); y++)
602                 {
603                         place_extra_bold(y, 0);
604                         place_extra_bold(y, cur_wid - 1);
605                 }
606         }
607         else
608         {
609                 /* Start with walls */
610                 for (y = 0; y < cur_hgt; y++)
611                 {
612                         for (x = 0; x < cur_wid; x++)
613                         {
614                                 place_extra_bold(y, x);
615                         }
616                 }
617         }
618
619 #ifdef ALLOW_CAVERNS_AND_LAKES
620         /* Possible "destroyed" level */
621         if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
622         {
623                 destroyed = TRUE;
624
625                 /* extra rubble around the place looks cool */
626                 build_lake(one_in_(2) ? GEN_LAKE_TYPE_CAVE : GEN_LAKE_TYPE_EARTH_VAULT);
627         }
628
629         /* Make a lake some of the time */
630         if (one_in_(LAKE_LEVEL) && !empty_level && !destroyed &&
631             (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
632         {
633                 int count = 0;
634                 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
635                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
636                 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
637                 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
638
639                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
640                 {
641                         /* Lake of Lava */
642                         if ((dun_level > 80) && (randint0(count) < 2)) laketype = GEN_LAKE_TYPE_LAVA;
643                         count -= 2;
644
645                         /* Lake of Lava2 */
646                         if (!laketype && (dun_level > 80) && one_in_(count)) laketype = GEN_LAKE_TYPE_FIRE_VAULT;
647                         count--;
648                 }
649
650                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !laketype)
651                 {
652                         /* Lake of Water */
653                         if ((dun_level > 50) && randint0(count) < 2) laketype = GEN_LAKE_TYPE_WATER;
654                         count -= 2;
655
656                         /* Lake of Water2 */
657                         if (!laketype && (dun_level > 50) && one_in_(count)) laketype = GEN_LAKE_TYPE_WATER_VAULT;
658                         count--;
659                 }
660
661                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !laketype)
662                 {
663                         /* Lake of rubble */
664                         if ((dun_level > 35) && (randint0(count) < 2)) laketype = GEN_LAKE_TYPE_CAVE;
665                         count -= 2;
666
667                         /* Lake of rubble2 */
668                         if (!laketype && (dun_level > 35) && one_in_(count)) laketype = GEN_LAKE_TYPE_EARTH_VAULT;
669                         count--;
670                 }
671
672                 /* Lake of tree */
673                 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !laketype) laketype = GEN_LAKE_TYPE_AIR_VAULT;
674
675                 if (laketype)
676                 {
677                         if (cheat_room)
678 #ifdef JP
679                                 msg_print("¸Ð¤òÀ¸À®¡£");
680 #else
681                                 msg_print("Lake on the level.");
682 #endif
683
684                         build_lake(laketype);
685                 }
686         }
687
688         if ((dun_level > DUN_CAVERN) && !empty_level &&
689             (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
690             !laketype && !destroyed && (randint1(1000) < dun_level))
691         {
692                 cavern = TRUE;
693
694                 /* make a large fractal cave in the middle of the dungeon */
695
696                 if (cheat_room)
697 #ifdef JP
698                         msg_print("ƶ·¢¤òÀ¸À®¡£");
699 #else
700                         msg_print("Cavern on level.");
701 #endif
702
703                 build_cavern();
704         }
705 #endif /* ALLOW_CAVERNS_AND_LAKES */
706
707         /* Hack -- No destroyed "quest" levels */
708         if (quest_number(dun_level)) destroyed = FALSE;
709
710         /* Build maze */
711         if (d_info[dungeon_type].flags1 & DF1_MAZE)
712         {
713                 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
714
715                 /* Place 3 or 4 down stairs near some walls */
716                 if (!alloc_stairs(FEAT_MORE, rand_range(2, 3), 3)) return FALSE;
717
718                 /* Place 1 or 2 up stairs near some walls */
719                 if (!alloc_stairs(FEAT_LESS, 1, 3)) return FALSE;
720         }
721
722         /* Build some rooms */
723         else
724         {
725                 for (i = 0; i < dun_rooms; i++)
726                 {
727                         bool force_rooms = (ironman_rooms && !((d_info[dungeon_type].flags1 & DF1_BEGINNER) || (d_info[dungeon_type].flags1 & DF1_CHAMELEON)));
728
729                         /* Pick a block for the room */
730                         y = randint0(dun->row_rooms);
731                         x = randint0(dun->col_rooms);
732
733                         /* Align dungeon rooms */
734                         if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
735                         {
736                                 /* Slide some rooms right */
737                                 if ((x % 3) == 0) x++;
738
739                                 /* Slide some rooms left */
740                                 if ((x % 3) == 2) x--;
741                         }
742
743                         /* Attempt an "unusual" room */
744                         if (force_rooms || (randint0(DUN_UNUSUAL) < dun_level))
745                         {
746                                 /* Roll for room type */
747                                 while (1)
748                                 {
749                                         k = (force_rooms ? 0 : randint0(100));
750                                         if (force_rooms) break;
751                                         if ((d_info[dungeon_type].flags1 & DF1_NO_VAULT) && (k < 14)) continue;
752                                         break;
753                                 }
754
755                                 /* Attempt a very unusual room */
756                                 if (force_rooms || (randint0(DUN_UNUSUAL) < dun_level))
757                                 {
758 #ifdef FORCE_V_IDX
759                                         if (room_build(y, x, ROOM_BUILD_TYPE_GREATER_VAULT)) continue;
760 #else
761                                         /* Type 8 -- Greater vault (4%) */
762                                         if (k < 4)
763                                         {
764                                                 if (max_vault_ok > 1)
765                                                 {
766                                                         if (room_build(y, x, ROOM_BUILD_TYPE_GREATER_VAULT)) continue;
767                                                 }
768                                                 else
769                                                 {
770 #ifdef JP
771                                                         if (cheat_room) msg_print("µðÂç¤ÊÃϲ¼¼¼¤òµÑ²¼¤·¤Þ¤¹¡£");
772 #else
773                                                         if (cheat_room) msg_print("Refusing a greater vault.");
774 #endif
775                                                 }
776                                         }
777
778                                         /* Type 7 -- Lesser vault (6%) */
779                                         if (k < 10)
780                                         {
781                                                 if (max_vault_ok > 0)
782                                                 {
783                                                         if (room_build(y, x, ROOM_BUILD_TYPE_LESSER_VAULT)) continue;
784                                                 }
785                                                 else
786                                                 {
787 #ifdef JP
788                                                         if (cheat_room) msg_print("¾®¤µ¤ÊÃϲ¼¼¼¤òµÑ²¼¤·¤Þ¤¹¡£");
789 #else
790                                                         if (cheat_room) msg_print("Refusing a lesser vault.");
791 #endif
792                                                 }
793                                         }
794
795
796                                         /* Type 10 -- Random vault (4%) */
797                                         if ((k < 14) && room_build(y, x, ROOM_BUILD_TYPE_RANDOM_VAULT)) continue;
798
799                                         /* Type 5 -- Monster nest (8%) */
800                                         if ((k < 22) && room_build(y, x, ROOM_BUILD_TYPE_NEST)) continue;
801
802                                         /* Type 6 -- Monster pit (10%) */
803                                         if ((k < 32) && room_build(y, x, ROOM_BUILD_TYPE_PIT)) continue;
804
805                                         /* Type 13 -- Trapped monster pit (5%) */
806                                         if ((k < 37) && room_build(y, x, ROOM_BUILD_TYPE_TRAP_PIT)) continue;
807
808                                         /* Type 14 -- Trapped room (5%) */
809                                         if ((k < 42) && room_build(y, x, ROOM_BUILD_TYPE_TRAP)) continue;
810 #endif
811                                 }
812
813                                 /* Type 2 -- Overlapping (25%) */
814                                 if ((k < 25) && room_build(y, x, ROOM_BUILD_TYPE_OVERLAP)) continue;
815
816                                 /* Type 3 -- Cross room (25%) */
817                                 if ((k < 50) && room_build(y, x, ROOM_BUILD_TYPE_CROSS)) continue;
818
819                                 if (d_info[dungeon_type].flags1 & DF1_NO_CAVE)
820                                 {
821                                         if (room_build(y, x, ROOM_BUILD_TYPE_INNER_FEAT)) continue;
822                                 }
823                                 else
824                                 {
825                                         /* Type 4 -- Large room (25%) */
826                                         if ((k < 75) && room_build(y, x, ROOM_BUILD_TYPE_INNER_FEAT)) continue;
827
828                                         /* Type 11 -- Circular (10%) */
829                                         if ((k < 85) && room_build(y, x, ROOM_BUILD_TYPE_OVAL)) continue;
830
831                                         /* Type 12 -- Crypt (15%) */
832                                         if ((k < 100) && room_build(y, x, ROOM_BUILD_TYPE_CRYPT)) continue;
833                                 }
834                         }
835
836                         /* The deeper you are, the more cavelike the rooms are */
837                         k = randint1(100);
838
839                         /* No caves when a cavern exists: they look bad */
840                         if (((k < dun_level) || (d_info[dungeon_type].flags1 & DF1_CAVE))
841                             && !cavern && !empty_level && !laketype
842                             && !(d_info[dungeon_type].flags1 & DF1_NO_CAVE))
843                         {
844                                 /* Type 9 -- Fractal cave */
845                                 if (room_build(y, x, ROOM_BUILD_TYPE_FRACAVE)) continue;
846                         }
847                         else
848                         {
849                                 /* Attempt a "trivial" room */
850                                 if (room_build(y, x, ROOM_BUILD_TYPE_NORMAL)) continue;
851                         }
852
853                         continue;
854                 }
855
856                 /* Make a hole in the dungeon roof sometimes at level 1 */
857                 if (dun_level == 1)
858                 {
859                         while (one_in_(DUN_MOS_DEN))
860                         {
861                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
862                         }
863                 }
864
865                 /* Destroy the level if necessary */
866                 if (destroyed) destroy_level();
867
868                 /* Hack -- Add some rivers */
869                 if (one_in_(3) && (randint1(dun_level) > 5))
870                 {
871                         /* Choose water or lava */
872                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
873                         {
874                                 feat1 = FEAT_DEEP_WATER;
875                                 feat2 = FEAT_SHAL_WATER;
876                         }
877                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
878                         {
879                                 feat1 = FEAT_DEEP_LAVA;
880                                 feat2 = FEAT_SHAL_LAVA;
881                         }
882                         else feat1 = 0;
883
884
885                         /* Only add river if matches lake type or if have no lake at all */
886                         if ((((laketype == GEN_LAKE_TYPE_LAVA) && (feat1 == FEAT_DEEP_LAVA)) ||
887                              ((laketype == GEN_LAKE_TYPE_WATER) && (feat1 == FEAT_DEEP_WATER)) ||
888                               !laketype) && feat1)
889                         {
890                                 add_river(feat1, feat2);
891                         }
892                 }
893
894                 /* Hack -- Scramble the room order */
895                 for (i = 0; i < dun->cent_n; i++)
896                 {
897                         int pick1 = randint0(dun->cent_n);
898                         int pick2 = randint0(dun->cent_n);
899                         y1 = dun->cent[pick1].y;
900                         x1 = dun->cent[pick1].x;
901                         dun->cent[pick1].y = dun->cent[pick2].y;
902                         dun->cent[pick1].x = dun->cent[pick2].x;
903                         dun->cent[pick2].y = y1;
904                         dun->cent[pick2].x = x1;
905                 }
906
907                 /* Start with no tunnel doors */
908                 dun->door_n = 0;
909
910                 /* Hack -- connect the first room to the last room */
911                 y = dun->cent[dun->cent_n-1].y;
912                 x = dun->cent[dun->cent_n-1].x;
913
914                 /* Connect all the rooms together */
915                 for (i = 0; i < dun->cent_n; i++)
916                 {
917                         /* Reset the arrays */
918                         dun->tunn_n = 0;
919                         dun->wall_n = 0;
920
921                         /* Connect the room to the previous room */
922                         if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
923                         {
924                                 /* make cave-like tunnel */
925                                 build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
926                         }
927                         else
928                         {
929                                 /* make normal tunnel */
930                                 build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x);
931                         }
932
933                         /* Turn the tunnel into corridor */
934                         for (j = 0; j < dun->tunn_n; j++)
935                         {
936                                 /* Access the grid */
937                                 y = dun->tunn[j].y;
938                                 x = dun->tunn[j].x;
939
940                                 /* Access the grid */
941                                 c_ptr = &cave[y][x];
942
943                                 /* Clear previous contents (if not a lake), add a floor */
944                                 if ((c_ptr->feat < FEAT_DEEP_WATER) ||
945                                     (c_ptr->feat > FEAT_SHAL_LAVA))
946                                 {
947                                         /* Clear mimic type */
948                                         c_ptr->mimic = 0;
949
950                                         place_floor_grid(c_ptr);
951                                 }
952                         }
953
954                         /* Apply the piercings that we found */
955                         for (j = 0; j < dun->wall_n; j++)
956                         {
957                                 /* Access the grid */
958                                 y = dun->wall[j].y;
959                                 x = dun->wall[j].x;
960
961                                 /* Access the grid */
962                                 c_ptr = &cave[y][x];
963
964                                 /* Clear mimic type */
965                                 c_ptr->mimic = 0;
966
967                                 /* Clear previous contents, add up floor */
968                                 place_floor_grid(c_ptr);
969
970                                 /* Occasional doorway */
971                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
972                                 {
973                                         /* Place a random door */
974                                         place_random_door(y, x, TRUE);
975                                 }
976                         }
977
978                         /* Remember the "previous" room */
979                         y = dun->cent[i].y;
980                         x = dun->cent[i].x;
981                 }
982
983                 /* Place intersection doors */
984                 for (i = 0; i < dun->door_n; i++)
985                 {
986                         /* Extract junction location */
987                         y = dun->door[i].y;
988                         x = dun->door[i].x;
989
990                         /* Try placing doors */
991                         try_door(y, x - 1);
992                         try_door(y, x + 1);
993                         try_door(y - 1, x);
994                         try_door(y + 1, x);
995                 }
996
997                 /* Place 3 or 4 down stairs near some walls */
998                 if (!alloc_stairs(FEAT_MORE, rand_range(3, 4), 3)) return FALSE;
999
1000                 /* Place 1 or 2 up stairs near some walls */
1001                 if (!alloc_stairs(FEAT_LESS, rand_range(1, 2), 3)) return FALSE;
1002         }
1003
1004         if (!laketype)
1005         {
1006                 if (d_info[dungeon_type].stream2)
1007                 {
1008                         /* Hack -- Add some quartz streamers */
1009                         for (i = 0; i < DUN_STR_QUA; i++)
1010                         {
1011                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
1012                         }
1013                 }
1014
1015                 if (d_info[dungeon_type].stream1)
1016                 {
1017                         /* Hack -- Add some magma streamers */
1018                         for (i = 0; i < DUN_STR_MAG; i++)
1019                         {
1020                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
1021                         }
1022                 }
1023         }
1024
1025         /* Special boundary walls -- Top and bottom */
1026         for (x = 0; x < cur_wid; x++)
1027         {
1028                 set_bound_perm_wall(&cave[0][x]);
1029                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
1030         }
1031
1032         /* Special boundary walls -- Left and right */
1033         for (y = 1; y < (cur_hgt - 1); y++)
1034         {
1035                 set_bound_perm_wall(&cave[y][0]);
1036                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
1037         }
1038
1039         /* Determine the character location */
1040         if (!new_player_spot()) return FALSE;
1041
1042         if (!place_quest_monsters()) return FALSE;
1043
1044         /* Basic "amount" */
1045         k = (dun_level / 3);
1046         if (k > 10) k = 10;
1047         if (k < 2) k = 2;
1048
1049         /* Pick a base number of monsters */
1050         i = d_info[dungeon_type].min_m_alloc_level;
1051
1052         /* To make small levels a bit more playable */
1053         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1054         {
1055                 int small_tester = i;
1056
1057                 i = (i * cur_hgt) / MAX_HGT;
1058                 i = (i * cur_wid) / MAX_WID;
1059                 i += 1;
1060
1061                 if (i > small_tester) i = small_tester;
1062                 else if (cheat_hear)
1063                 {
1064 #ifdef JP
1065 msg_format("¥â¥ó¥¹¥¿¡¼¿ô´ðËÜÃͤò %d ¤«¤é %d ¤Ë¸º¤é¤·¤Þ¤¹", small_tester, i);
1066 #else
1067                         msg_format("Reduced monsters base from %d to %d", small_tester, i);
1068 #endif
1069
1070                 }
1071         }
1072
1073         i += randint1(8);
1074
1075         /* Put some monsters in the dungeon */
1076         for (i = i + k; i > 0; i--)
1077         {
1078                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1079         }
1080
1081         /* Place some traps in the dungeon */
1082         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1083
1084         /* Put some rubble in corridors */
1085         alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1086
1087         /* Mega Hack -- No object at first level of deeper dungeon */
1088         if (p_ptr->enter_dungeon && dun_level > 1)
1089         {
1090                 /* No stair scum! */
1091         }
1092         else
1093         {
1094                 /* Put some objects in rooms */
1095                 alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1096
1097                 /* Put some objects/gold in the dungeon */
1098                 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1099                 alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1100         }
1101
1102         /* Put the Guardian */
1103         if (!alloc_guardian(TRUE)) return FALSE;
1104
1105         if (empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1106         {
1107                 /* Lite the cave */
1108                 for (y = 0; y < cur_hgt; y++)
1109                 {
1110                         for (x = 0; x < cur_wid; x++)
1111                         {
1112                                 cave[y][x].info |= (CAVE_GLOW);
1113                         }
1114                 }
1115         }
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_info[p_ptr->arena_number].r_idx,
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->mflag2 |= (MFLAG2_MARK | MFLAG2_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_wid, cur_hgt);
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 /*
1467  * Wipe all unnecessary flags after cave generation
1468  */
1469 void wipe_generate_cave_flags(void)
1470 {
1471         int x, y;
1472
1473         for (y = 0; y < cur_hgt; y++)
1474         {
1475                 for (x = 0; x < cur_wid; x++)
1476                 {
1477                         /* Wipe unused flags */
1478                         cave[y][x].info &= ~(CAVE_MASK);
1479                 }
1480         }
1481
1482         if (dun_level)
1483         {
1484                 for (y = 1; y < cur_hgt - 1; y++)
1485                 {
1486                         for (x = 1; x < cur_wid - 1; x++)
1487                         {
1488                                 /* There might be trap */
1489                                 cave[y][x].info |= CAVE_UNSAFE;
1490                         }
1491                 }
1492         }
1493 }
1494
1495
1496 /*
1497  *  Clear and empty the cave
1498  */
1499 void clear_cave(void)
1500 {
1501         int x, y, i;
1502
1503         /* Very simplified version of wipe_o_list() */
1504         C_WIPE(o_list, o_max, object_type);
1505         o_max = 1;
1506         o_cnt = 0;
1507
1508         /* Very simplified version of wipe_m_list() */
1509         for (i = 1; i < max_r_idx; i++)
1510                 r_info[i].cur_num = 0;
1511         C_WIPE(m_list, m_max, monster_type);
1512         m_max = 1;
1513         m_cnt = 0;
1514         precalc_cur_num_of_pet();
1515
1516
1517         /* Start with a blank cave */
1518         for (y = 0; y < MAX_HGT; y++)
1519         {
1520                 for (x = 0; x < MAX_WID; x++)
1521                 {
1522                         cave_type *c_ptr = &cave[y][x];
1523
1524                         /* No flags */
1525                         c_ptr->info = 0;
1526
1527                         /* No features */
1528                         c_ptr->feat = 0;
1529
1530                         /* No objects */
1531                         c_ptr->o_idx = 0;
1532
1533                         /* No monsters */
1534                         c_ptr->m_idx = 0;
1535
1536                         /* No special */
1537                         c_ptr->special = 0;
1538
1539                         /* No mimic */
1540                         c_ptr->mimic = 0;
1541
1542                         /* No flow */
1543                         c_ptr->cost = 0;
1544                         c_ptr->dist = 0;
1545                         c_ptr->when = 0;
1546                 }
1547         }
1548
1549         /* Mega-Hack -- no player yet */
1550         px = py = 0;
1551
1552         /* Set the base level */
1553         base_level = dun_level;
1554
1555         /* Reset the monster generation level */
1556         monster_level = base_level;
1557
1558         /* Reset the object generation level */
1559         object_level = base_level;
1560
1561         /* Nothing special here yet */
1562         good_item_flag = FALSE;
1563
1564         /* Nothing good here yet */
1565         rating = 0;
1566 }
1567
1568
1569 /*
1570  * Generates a random dungeon level                     -RAK-
1571  *
1572  * Hack -- regenerate any "overflow" levels
1573  *
1574  * Hack -- allow auto-scumming via a gameplay option.
1575  */
1576 void generate_cave(void)
1577 {
1578         int num;
1579
1580         /* Fill the arrays of floors and walls in the good proportions */
1581         set_floor_and_wall(dungeon_type);
1582
1583         /* Generate */
1584         for (num = 0; TRUE; num++)
1585         {
1586                 bool okay = TRUE;
1587
1588                 cptr why = NULL;
1589
1590                 /* Clear and empty the cave */
1591                 clear_cave();
1592
1593                 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;
1594
1595                 /* Build the arena -KMW- */
1596                 if (p_ptr->inside_arena)
1597                 {
1598                         /* Small arena */
1599                         arena_gen();
1600                 }
1601
1602                 /* Build the battle -KMW- */
1603                 else if (p_ptr->inside_battle)
1604                 {
1605                         /* Small arena */
1606                         battle_gen();
1607                 }
1608
1609                 else if (p_ptr->inside_quest)
1610                 {
1611                         quest_gen();
1612                 }
1613
1614                 /* Build the town */
1615                 else if (!dun_level)
1616                 {
1617                         /* Make the wilderness */
1618                         if (p_ptr->wild_mode) wilderness_gen_small();
1619                         else wilderness_gen();
1620                 }
1621
1622                 /* Build a real level */
1623                 else
1624                 {
1625                         okay = level_gen(&why);
1626                 }
1627
1628                 /* Extract the feeling */
1629                 feeling = extract_feeling();
1630
1631                 /* Prevent object over-flow */
1632                 if (o_max >= max_o_idx)
1633                 {
1634                         /* Message */
1635 #ifdef JP
1636 why = "¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë";
1637 #else
1638                         why = "too many objects";
1639 #endif
1640
1641
1642                         /* Message */
1643                         okay = FALSE;
1644                 }
1645                 /* Prevent monster over-flow */
1646                 else if (m_max >= max_m_idx)
1647                 {
1648                         /* Message */
1649 #ifdef JP
1650 why = "¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë";
1651 #else
1652                         why = "too many monsters";
1653 #endif
1654
1655
1656                         /* Message */
1657                         okay = FALSE;
1658                 }
1659
1660                 /* Mega-Hack -- "auto-scum" */
1661                 else if ((auto_scum || ironman_autoscum) && (num < 100) &&
1662                          !p_ptr->inside_quest &&
1663                          !(d_info[dungeon_type].flags1 & DF1_BEGINNER) &&
1664                          !p_ptr->enter_dungeon)
1665                 {
1666                         /* Require "goodness" */
1667                         if ((feeling > 9) ||
1668                             ((dun_level >= 7) && (feeling > 8)) ||
1669                             ((dun_level >= 15) && (feeling > 7)) ||
1670                             ((dun_level >= 35) && (feeling > 6)) ||
1671                             ((dun_level >= 70) && (feeling > 5)))
1672                         {
1673                                 /* Give message to cheaters */
1674                                 if (cheat_room || cheat_hear ||
1675                                     cheat_peek || cheat_xtra)
1676                                 {
1677                                         /* Message */
1678 #ifdef JP
1679 why = "Âà¶þ¤Ê³¬";
1680 #else
1681                                         why = "boring level";
1682 #endif
1683
1684                                 }
1685
1686                                 /* Try again */
1687                                 okay = FALSE;
1688                         }
1689                 }
1690
1691                 /* Accept */
1692                 if (okay) break;
1693
1694                 /* Message */
1695 #ifdef JP
1696 if (why) msg_format("À¸À®¤ä¤êľ¤·(%s)", why);
1697 #else
1698                 if (why) msg_format("Generation restarted (%s)", why);
1699 #endif
1700
1701
1702                 /* Wipe the objects */
1703                 wipe_o_list();
1704
1705                 /* Wipe the monsters */
1706                 wipe_m_list();
1707         }
1708
1709         /* Glow deep lava and building entrances */
1710         glow_deep_lava_and_bldg();
1711
1712         /* Reset flag */
1713         p_ptr->enter_dungeon = FALSE;
1714
1715         wipe_generate_cave_flags();
1716 }