OSDN Git Service

refactor compare_weapon_aux
[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_down_stair, rand_range(2, 3), 3)) return FALSE;
797
798                 /* Place 1 or 2 up stairs near some walls */
799                 if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
800         }
801
802         /* Build some rooms */
803         else
804         {
805                 int tunnel_fail_count = 0;
806
807                 /*
808                  * Build each type of room in turn until we cannot build any more.
809                  */
810                 if (!generate_rooms()) return FALSE;
811
812
813                 /* Make a hole in the dungeon roof sometimes at level 1 */
814                 if (dun_level == 1)
815                 {
816                         while (one_in_(DUN_MOS_DEN))
817                         {
818                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
819                         }
820                 }
821
822                 /* Destroy the level if necessary */
823                 if (dun->destroyed) destroy_level();
824
825                 /* Hack -- Add some rivers */
826                 if (one_in_(3) && (randint1(dun_level) > 5))
827                 {
828                         int feat1 = 0, feat2 = 0;
829
830                         /* Choose water or lava */
831                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
832                         {
833                                 feat1 = feat_deep_water;
834                                 feat2 = feat_shallow_water;
835                         }
836                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
837                         {
838                                 feat1 = feat_deep_lava;
839                                 feat2 = feat_shallow_lava;
840                         }
841                         else feat1 = 0;
842
843                         if (feat1)
844                         {
845                                 feature_type *f_ptr = &f_info[feat1];
846
847                                 /* Only add river if matches lake type or if have no lake at all */
848                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
849                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
850                                      !dun->laketype)
851                                 {
852                                         add_river(feat1, feat2);
853                                 }
854                         }
855                 }
856
857                 /* Hack -- Scramble the room order */
858                 for (i = 0; i < dun->cent_n; i++)
859                 {
860                         int ty, tx;
861                         int pick = rand_range(0, i);
862
863                         ty = dun->cent[i].y;
864                         tx = dun->cent[i].x;
865                         dun->cent[i].y = dun->cent[pick].y;
866                         dun->cent[i].x = dun->cent[pick].x;
867                         dun->cent[pick].y = ty;
868                         dun->cent[pick].x = tx;
869                 }
870
871                 /* Start with no tunnel doors */
872                 dun->door_n = 0;
873
874                 /* Hack -- connect the first room to the last room */
875                 y = dun->cent[dun->cent_n-1].y;
876                 x = dun->cent[dun->cent_n-1].x;
877
878                 /* Connect all the rooms together */
879                 for (i = 0; i < dun->cent_n; i++)
880                 {
881                         int j;
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                                 (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
892                         }
893                         else
894                         {
895                                 /* make normal tunnel */
896                                 if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
897                         }
898
899                         if (tunnel_fail_count >= 2) return FALSE;
900
901                         /* Turn the tunnel into corridor */
902                         for (j = 0; j < dun->tunn_n; j++)
903                         {
904                                 cave_type *c_ptr;
905                                 feature_type *f_ptr;
906
907                                 /* Access the grid */
908                                 y = dun->tunn[j].y;
909                                 x = dun->tunn[j].x;
910
911                                 /* Access the grid */
912                                 c_ptr = &cave[y][x];
913                                 f_ptr = &f_info[c_ptr->feat];
914
915                                 /* Clear previous contents (if not a lake), add a floor */
916                                 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
917                                 {
918                                         /* Clear mimic type */
919                                         c_ptr->mimic = 0;
920
921                                         place_floor_grid(c_ptr);
922                                 }
923                         }
924
925                         /* Apply the piercings that we found */
926                         for (j = 0; j < dun->wall_n; j++)
927                         {
928                                 cave_type *c_ptr;
929
930                                 /* Access the grid */
931                                 y = dun->wall[j].y;
932                                 x = dun->wall[j].x;
933
934                                 /* Access the grid */
935                                 c_ptr = &cave[y][x];
936
937                                 /* Clear mimic type */
938                                 c_ptr->mimic = 0;
939
940                                 /* Clear previous contents, add up floor */
941                                 place_floor_grid(c_ptr);
942
943                                 /* Occasional doorway */
944                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
945                                 {
946                                         /* Place a random door */
947                                         place_random_door(y, x, TRUE);
948                                 }
949                         }
950
951                         /* Remember the "previous" room */
952                         y = dun->cent[i].y;
953                         x = dun->cent[i].x;
954                 }
955
956                 /* Place intersection doors */
957                 for (i = 0; i < dun->door_n; i++)
958                 {
959                         /* Extract junction location */
960                         y = dun->door[i].y;
961                         x = dun->door[i].x;
962
963                         /* Try placing doors */
964                         try_door(y, x - 1);
965                         try_door(y, x + 1);
966                         try_door(y - 1, x);
967                         try_door(y + 1, x);
968                 }
969
970                 /* Place 3 or 4 down stairs near some walls */
971                 if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;
972
973                 /* Place 1 or 2 up stairs near some walls */
974                 if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
975         }
976
977         if (!dun->laketype)
978         {
979                 if (d_info[dungeon_type].stream2)
980                 {
981                         /* Hack -- Add some quartz streamers */
982                         for (i = 0; i < DUN_STR_QUA; i++)
983                         {
984                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
985                         }
986                 }
987
988                 if (d_info[dungeon_type].stream1)
989                 {
990                         /* Hack -- Add some magma streamers */
991                         for (i = 0; i < DUN_STR_MAG; i++)
992                         {
993                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
994                         }
995                 }
996         }
997
998         /* Special boundary walls -- Top and bottom */
999         for (x = 0; x < cur_wid; x++)
1000         {
1001                 set_bound_perm_wall(&cave[0][x]);
1002                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
1003         }
1004
1005         /* Special boundary walls -- Left and right */
1006         for (y = 1; y < (cur_hgt - 1); y++)
1007         {
1008                 set_bound_perm_wall(&cave[y][0]);
1009                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
1010         }
1011
1012         /* Determine the character location */
1013         if (!new_player_spot()) return FALSE;
1014
1015         if (!place_quest_monsters()) return FALSE;
1016
1017         /* Basic "amount" */
1018         k = (dun_level / 3);
1019         if (k > 10) k = 10;
1020         if (k < 2) k = 2;
1021
1022         /* Pick a base number of monsters */
1023         i = d_info[dungeon_type].min_m_alloc_level;
1024
1025         /* To make small levels a bit more playable */
1026         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1027         {
1028                 int small_tester = i;
1029
1030                 i = (i * cur_hgt) / MAX_HGT;
1031                 i = (i * cur_wid) / MAX_WID;
1032                 i += 1;
1033
1034                 if (i > small_tester) i = small_tester;
1035                 else if (cheat_hear)
1036                 {
1037 #ifdef JP
1038 msg_format("¥â¥ó¥¹¥¿¡¼¿ô´ðËÜÃͤò %d ¤«¤é %d ¤Ë¸º¤é¤·¤Þ¤¹", small_tester, i);
1039 #else
1040                         msg_format("Reduced monsters base from %d to %d", small_tester, i);
1041 #endif
1042
1043                 }
1044         }
1045
1046         i += randint1(8);
1047
1048         /* Put some monsters in the dungeon */
1049         for (i = i + k; i > 0; i--)
1050         {
1051                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1052         }
1053
1054         /* Place some traps in the dungeon */
1055         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1056
1057         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
1058         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1059
1060         /* Mega Hack -- No object at first level of deeper dungeon */
1061         if (p_ptr->enter_dungeon && dun_level > 1)
1062         {
1063                 /* No stair scum! */
1064                 object_level = 1;
1065         }
1066
1067         /* Put some objects in rooms */
1068         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1069
1070         /* Put some objects/gold in the dungeon */
1071         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1072         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1073
1074         /* Set back to default */
1075         object_level = base_level;
1076
1077         /* Put the Guardian */
1078         if (!alloc_guardian(TRUE)) return FALSE;
1079
1080         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1081         {
1082                 /* Lite the cave */
1083                 for (y = 0; y < cur_hgt; y++)
1084                 {
1085                         for (x = 0; x < cur_wid; x++)
1086                         {
1087                                 cave[y][x].info |= (CAVE_GLOW);
1088                         }
1089                 }
1090         }
1091
1092         return TRUE;
1093 }
1094
1095
1096 /*
1097  * Builds the arena after it is entered -KMW-
1098  */
1099 static void build_arena(void)
1100 {
1101         int yval, y_height, y_depth, xval, x_left, x_right;
1102         register int i, j;
1103
1104         yval = SCREEN_HGT / 2;
1105         xval = SCREEN_WID / 2;
1106         y_height = yval - 10;
1107         y_depth = yval + 10;
1108         x_left = xval - 32;
1109         x_right = xval + 32;
1110
1111         for (i = y_height; i <= y_height + 5; i++)
1112                 for (j = x_left; j <= x_right; j++)
1113                 {
1114                         place_extra_perm_bold(i, j);
1115                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1116                 }
1117         for (i = y_depth; i >= y_depth - 5; i--)
1118                 for (j = x_left; j <= x_right; j++)
1119                 {
1120                         place_extra_perm_bold(i, j);
1121                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1122                 }
1123         for (j = x_left; j <= x_left + 17; j++)
1124                 for (i = y_height; i <= y_depth; i++)
1125                 {
1126                         place_extra_perm_bold(i, j);
1127                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1128                 }
1129         for (j = x_right; j >= x_right - 17; j--)
1130                 for (i = y_height; i <= y_depth; i++)
1131                 {
1132                         place_extra_perm_bold(i, j);
1133                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1134                 }
1135
1136         place_extra_perm_bold(y_height+6, x_left+18);
1137         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1138         place_extra_perm_bold(y_depth-6, x_left+18);
1139         cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1140         place_extra_perm_bold(y_height+6, x_right-18);
1141         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1142         place_extra_perm_bold(y_depth-6, x_right-18);
1143         cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1144
1145         i = y_height + 5;
1146         j = xval;
1147         cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1148         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1149         player_place(i, j);
1150 }
1151
1152
1153 /*
1154  * Town logic flow for generation of arena -KMW-
1155  */
1156 static void arena_gen(void)
1157 {
1158         int y, x;
1159         int qy = 0;
1160         int qx = 0;
1161
1162         /* Smallest area */
1163         cur_hgt = SCREEN_HGT;
1164         cur_wid = SCREEN_WID;
1165
1166         /* Start with solid walls */
1167         for (y = 0; y < MAX_HGT; y++)
1168         {
1169                 for (x = 0; x < MAX_WID; x++)
1170                 {
1171                         /* Create "solid" perma-wall */
1172                         place_solid_perm_bold(y, x);
1173
1174                         /* Illuminate and memorize the walls */
1175                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1176                 }
1177         }
1178
1179         /* Then place some floors */
1180         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1181         {
1182                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1183                 {
1184                         /* Create empty floor */
1185                         cave[y][x].feat = feat_floor;
1186                 }
1187         }
1188
1189         build_arena();
1190
1191         if(!place_monster_aux(0, py + 5, px, arena_info[p_ptr->arena_number].r_idx, (PM_NO_KAGE | PM_NO_PET)))
1192         {
1193                 p_ptr->exit_bldg = TRUE;
1194                 p_ptr->arena_number++;
1195 #ifdef JP
1196                 msg_print("Áê¼ê¤Ï·ç¾ì¤·¤¿¡£¤¢¤Ê¤¿¤ÎÉÔÀᄀ¤À¡£");
1197 #else
1198                 msg_print("The enemy is unable appear. You won by default.");
1199 #endif
1200         }
1201
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                         place_extra_perm_bold(i, j);
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                         place_extra_perm_bold(i, j);
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                         place_extra_perm_bold(i, j);
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                         place_extra_perm_bold(i, j);
1243                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1244                 }
1245
1246         place_extra_perm_bold(y_height+6, x_left+18);
1247         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1248         place_extra_perm_bold(y_depth-4, x_left+18);
1249         cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1250         place_extra_perm_bold(y_height+6, x_right-18);
1251         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1252         place_extra_perm_bold(y_depth-4, x_right-18);
1253         cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1254
1255         for (i = y_height + 1; i <= y_height + 5; i++)
1256                 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1257                 {
1258                         cave[i][j].feat = feat_permanent_glass_wall;
1259                 }
1260
1261         i = y_height + 1;
1262         j = xval;
1263         cave[i][j].feat = f_tag_to_index("BUILDING_3");
1264         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1265         player_place(i, j);
1266 }
1267
1268
1269 /*
1270  * Town logic flow for generation of arena -KMW-
1271  */
1272 static void battle_gen(void)
1273 {
1274         int y, x, i;
1275         int qy = 0;
1276         int qx = 0;
1277
1278         /* Start with solid walls */
1279         for (y = 0; y < MAX_HGT; y++)
1280         {
1281                 for (x = 0; x < MAX_WID; x++)
1282                 {
1283                         /* Create "solid" perma-wall */
1284                         place_solid_perm_bold(y, x);
1285
1286                         /* Illuminate and memorize the walls */
1287                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1288                 }
1289         }
1290
1291         /* Then place some floors */
1292         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1293         {
1294                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1295                 {
1296                         /* Create empty floor */
1297                         cave[y][x].feat = feat_floor;
1298                 }
1299         }
1300
1301         build_battle();
1302
1303         for(i=0;i<4;i++)
1304         {
1305                 place_monster_aux(0, py + 8 + (i/2)*4, px - 2 + (i%2)*4, battle_mon[i],
1306                                   (PM_NO_KAGE | PM_NO_PET));
1307                 set_friendly(&m_list[cave[py+8+(i/2)*4][px-2+(i%2)*4].m_idx]);
1308         }
1309         for(i = 1; i < m_max; i++)
1310         {
1311                 monster_type *m_ptr = &m_list[i];
1312
1313                 if (!m_ptr->r_idx) continue;
1314
1315                 /* Hack -- Detect monster */
1316                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1317
1318                 /* Update the monster */
1319                 update_mon(i, FALSE);
1320         }
1321 }
1322
1323
1324 /*
1325  * Generate a quest level
1326  */
1327 static void quest_gen(void)
1328 {
1329         int x, y;
1330
1331
1332         /* Start with perm walls */
1333         for (y = 0; y < cur_hgt; y++)
1334         {
1335                 for (x = 0; x < cur_wid; x++)
1336                 {
1337                         place_solid_perm_bold(y, x);
1338                 }
1339         }
1340
1341         /* Set the quest level */
1342         base_level = quest[p_ptr->inside_quest].level;
1343         dun_level = base_level;
1344         object_level = base_level;
1345         monster_level = base_level;
1346
1347         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1348
1349         /* Prepare allocation table */
1350         get_mon_num_prep(get_monster_hook(), NULL);
1351
1352         init_flags = INIT_CREATE_DUNGEON;
1353
1354         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1355 }
1356
1357 /* Make a real level */
1358 static bool level_gen(cptr *why)
1359 {
1360         int level_height, level_width;
1361
1362         if ((always_small_levels || ironman_small_levels ||
1363             (one_in_(SMALL_LEVEL) && small_levels) ||
1364              (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1365             (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1366             !(d_info[dungeon_type].flags1 & DF1_BIG))
1367         {
1368                 if (cheat_room)
1369 #ifdef JP
1370                         msg_print("¾®¤µ¤Ê¥Õ¥í¥¢");
1371 #else
1372                         msg_print("A 'small' dungeon level.");
1373 #endif
1374
1375                 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1376                 {
1377                         level_height = 1;
1378                         level_width = 1;
1379                 }
1380                 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1381                 {
1382                         level_height = 2;
1383                         level_width = 2;
1384                 }
1385                 else
1386                 {
1387                         do
1388                         {
1389                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1390                                 level_width = randint1(MAX_WID/SCREEN_WID);
1391                         }
1392                         while ((level_height == MAX_HGT/SCREEN_HGT) &&
1393                                    (level_width == MAX_WID/SCREEN_WID));
1394                 }
1395
1396                 cur_hgt = level_height * SCREEN_HGT;
1397                 cur_wid = level_width * SCREEN_WID;
1398
1399                 /* Assume illegal panel */
1400                 panel_row_min = cur_hgt;
1401                 panel_col_min = cur_wid;
1402
1403                 if (cheat_room)
1404                   msg_format("X:%d, Y:%d.", cur_wid, cur_hgt);
1405         }
1406         else
1407         {
1408                 /* Big dungeon */
1409                 cur_hgt = MAX_HGT;
1410                 cur_wid = MAX_WID;
1411
1412                 /* Assume illegal panel */
1413                 panel_row_min = cur_hgt;
1414                 panel_col_min = cur_wid;
1415         }
1416
1417         /* Make a dungeon */
1418         if (!cave_gen())
1419         {
1420 #ifdef JP
1421 *why = "¥À¥ó¥¸¥ç¥óÀ¸À®¤Ë¼ºÇÔ";
1422 #else
1423                 *why = "could not place player";
1424 #endif
1425
1426                 return FALSE;
1427         }
1428         else return TRUE;
1429 }
1430
1431
1432 /*
1433  * Wipe all unnecessary flags after cave generation
1434  */
1435 void wipe_generate_cave_flags(void)
1436 {
1437         int x, y;
1438
1439         for (y = 0; y < cur_hgt; y++)
1440         {
1441                 for (x = 0; x < cur_wid; x++)
1442                 {
1443                         /* Wipe unused flags */
1444                         cave[y][x].info &= ~(CAVE_MASK);
1445                 }
1446         }
1447
1448         if (dun_level)
1449         {
1450                 for (y = 1; y < cur_hgt - 1; y++)
1451                 {
1452                         for (x = 1; x < cur_wid - 1; x++)
1453                         {
1454                                 /* There might be trap */
1455                                 cave[y][x].info |= CAVE_UNSAFE;
1456                         }
1457                 }
1458         }
1459 }
1460
1461
1462 /*
1463  *  Clear and empty the cave
1464  */
1465 void clear_cave(void)
1466 {
1467         int x, y, i;
1468
1469         /* Very simplified version of wipe_o_list() */
1470         (void)C_WIPE(o_list, o_max, object_type);
1471         o_max = 1;
1472         o_cnt = 0;
1473
1474         /* Very simplified version of wipe_m_list() */
1475         for (i = 1; i < max_r_idx; i++)
1476                 r_info[i].cur_num = 0;
1477         (void)C_WIPE(m_list, m_max, monster_type);
1478         m_max = 1;
1479         m_cnt = 0;
1480         for (i = 0; i < MAX_MTIMED; i++) mproc_max[i] = 0;
1481
1482         /* Pre-calc cur_num of pets in party_mon[] */
1483         precalc_cur_num_of_pet();
1484
1485
1486         /* Start with a blank cave */
1487         for (y = 0; y < MAX_HGT; y++)
1488         {
1489                 for (x = 0; x < MAX_WID; x++)
1490                 {
1491                         cave_type *c_ptr = &cave[y][x];
1492
1493                         /* No flags */
1494                         c_ptr->info = 0;
1495
1496                         /* No features */
1497                         c_ptr->feat = 0;
1498
1499                         /* No objects */
1500                         c_ptr->o_idx = 0;
1501
1502                         /* No monsters */
1503                         c_ptr->m_idx = 0;
1504
1505                         /* No special */
1506                         c_ptr->special = 0;
1507
1508                         /* No mimic */
1509                         c_ptr->mimic = 0;
1510
1511                         /* No flow */
1512                         c_ptr->cost = 0;
1513                         c_ptr->dist = 0;
1514                         c_ptr->when = 0;
1515                 }
1516         }
1517
1518         /* Mega-Hack -- no player yet */
1519         px = py = 0;
1520
1521         /* Set the base level */
1522         base_level = dun_level;
1523
1524         /* Reset the monster generation level */
1525         monster_level = base_level;
1526
1527         /* Reset the object generation level */
1528         object_level = base_level;
1529 }
1530
1531
1532 /*
1533  * Generates a random dungeon level                     -RAK-
1534  *
1535  * Hack -- regenerate any "overflow" levels
1536  */
1537 void generate_cave(void)
1538 {
1539         int num;
1540
1541         /* Fill the arrays of floors and walls in the good proportions */
1542         set_floor_and_wall(dungeon_type);
1543
1544         /* Generate */
1545         for (num = 0; TRUE; num++)
1546         {
1547                 bool okay = TRUE;
1548
1549                 cptr why = NULL;
1550
1551                 /* Clear and empty the cave */
1552                 clear_cave();
1553
1554                 /* Build the arena -KMW- */
1555                 if (p_ptr->inside_arena)
1556                 {
1557                         /* Small arena */
1558                         arena_gen();
1559                 }
1560
1561                 /* Build the battle -KMW- */
1562                 else if (p_ptr->inside_battle)
1563                 {
1564                         /* Small arena */
1565                         battle_gen();
1566                 }
1567
1568                 else if (p_ptr->inside_quest)
1569                 {
1570                         quest_gen();
1571                 }
1572
1573                 /* Build the town */
1574                 else if (!dun_level)
1575                 {
1576                         /* Make the wilderness */
1577                         if (p_ptr->wild_mode) wilderness_gen_small();
1578                         else wilderness_gen();
1579                 }
1580
1581                 /* Build a real level */
1582                 else
1583                 {
1584                         okay = level_gen(&why);
1585                 }
1586
1587
1588                 /* Prevent object over-flow */
1589                 if (o_max >= max_o_idx)
1590                 {
1591                         /* Message */
1592 #ifdef JP
1593 why = "¥¢¥¤¥Æ¥à¤¬Â¿¤¹¤®¤ë";
1594 #else
1595                         why = "too many objects";
1596 #endif
1597
1598
1599                         /* Message */
1600                         okay = FALSE;
1601                 }
1602                 /* Prevent monster over-flow */
1603                 else if (m_max >= max_m_idx)
1604                 {
1605                         /* Message */
1606 #ifdef JP
1607 why = "¥â¥ó¥¹¥¿¡¼¤¬Â¿¤¹¤®¤ë";
1608 #else
1609                         why = "too many monsters";
1610 #endif
1611
1612
1613                         /* Message */
1614                         okay = FALSE;
1615                 }
1616
1617                 /* Accept */
1618                 if (okay) break;
1619
1620                 /* Message */
1621 #ifdef JP
1622 if (why) msg_format("À¸À®¤ä¤êľ¤·(%s)", why);
1623 #else
1624                 if (why) msg_format("Generation restarted (%s)", why);
1625 #endif
1626
1627
1628                 /* Wipe the objects */
1629                 wipe_o_list();
1630
1631                 /* Wipe the monsters */
1632                 wipe_m_list();
1633         }
1634
1635         /* Glow deep lava and building entrances */
1636         glow_deep_lava_and_bldg();
1637
1638         /* Reset flag */
1639         p_ptr->enter_dungeon = FALSE;
1640
1641         wipe_generate_cave_flags();
1642 }