OSDN Git Service

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