OSDN Git Service

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