OSDN Git Service

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