OSDN Git Service

#37287 (2.2.0.40) cmd1.c内のC4457警告に対応。 / Deal C4457 warning in cmd1.c.
[hengband/hengband.git] / src / generate.c
1 /*!
2  * @file generate.c
3  * @brief ダンジョンの生成 / Dungeon generation
4  * @date 2014/01/04
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  * This software may be copied and distributed for educational, research,\n
8  * and not for profit purposes provided that this copyright and statement\n
9  * are included in all such copies.  Other copyrights may also apply.\n
10  * 2014 Deskull rearranged comment for Doxygen. \n
11  * @details
12  * Note that Level generation is *not* an important bottleneck,\n
13  * though it can be annoyingly slow on older machines...  Thus\n
14  * we emphasize "simplicity" and "correctness" over "speed".\n
15  *\n
16  * This entire file is only needed for generating levels.\n
17  * This may allow smart compilers to only load it when needed.\n
18  *\n
19  * Consider the "v_info.txt" file for vault generation.\n
20  *\n
21  * In this file, we use the "special" granite and perma-wall sub-types,\n
22  * where "basic" is normal, "inner" is inside a room, "outer" is the\n
23  * outer wall of a room, and "solid" is the outer wall of the dungeon\n
24  * or any walls that may not be pierced by corridors.  Thus the only\n
25  * wall type that may be pierced by a corridor is the "outer granite"\n
26  * type.  The "basic granite" type yields the "actual" corridors.\n
27  *\n
28  * Note that we use the special "solid" granite wall type to prevent\n
29  * multiple corridors from piercing a wall in two adjacent locations,\n
30  * which would be messy, and we use the special "outer" granite wall\n
31  * to indicate which walls "surround" rooms, and may thus be "pierced"\n
32  * by corridors entering or leaving the room.\n
33  *\n
34  * Note that a tunnel which attempts to leave a room near the "edge"\n
35  * of the dungeon in a direction toward that edge will cause "silly"\n
36  * wall piercings, but will have no permanently incorrect effects,\n
37  * as long as the tunnel can *eventually* exit from another side.\n
38  * And note that the wall may not come back into the room by the\n
39  * hole it left through, so it must bend to the left or right and\n
40  * then optionally re-enter the room (at least 2 grids away).  This\n
41  * is not a problem since every room that is large enough to block\n
42  * the passage of tunnels is also large enough to allow the tunnel\n
43  * to pierce the room itself several times.\n
44  *\n
45  * Note that no two corridors may enter a room through adjacent grids,\n
46  * they must either share an entryway or else use entryways at least\n
47  * two grids apart.  This prevents "large" (or "silly") doorways.\n
48  *\n
49  * To create rooms in the dungeon, we first divide the dungeon up\n
50  * into "blocks" of 11x11 grids each, and require that all rooms\n
51  * occupy a rectangular group of blocks.  As long as each room type\n
52  * reserves a sufficient number of blocks, the room building routines\n
53  * will not need to check bounds.  Note that most of the normal rooms\n
54  * actually only use 23x11 grids, and so reserve 33x11 grids.\n
55  *\n
56  * Note that the use of 11x11 blocks (instead of the old 33x11 blocks)\n
57  * allows more variability in the horizontal placement of rooms, and\n
58  * at the same time has the disadvantage that some rooms (two thirds\n
59  * of the normal rooms) may be "split" by panel boundaries.  This can\n
60  * induce a situation where a player is in a room and part of the room\n
61  * is off the screen.  It may be annoying enough to go back to 33x11\n
62  * blocks to prevent this visual situation.\n
63  *\n
64  * Note that the dungeon generation routines are much different (2.7.5)\n
65  * and perhaps "DUN_ROOMS" should be less than 50.\n
66  *\n
67  * XXX XXX XXX Note that it is possible to create a room which is only\n
68  * connected to itself, because the "tunnel generation" code allows a\n
69  * tunnel to leave a room, wander around, and then re-enter the room.\n
70  *\n
71  * XXX XXX XXX Note that it is possible to create a set of rooms which\n
72  * are only connected to other rooms in that set, since there is nothing\n
73  * explicit in the code to prevent this from happening.  But this is less\n
74  * likely than the "isolated room" problem, because each room attempts to\n
75  * connect to another room, in a giant cycle, thus requiring at least two\n
76  * bizarre occurances to create an isolated section of the dungeon.\n
77  *\n
78  * Note that (2.7.9) monster pits have been split into monster "nests"\n
79  * and monster "pits".  The "nests" have a collection of monsters of a\n
80  * given type strewn randomly around the room (jelly, animal, or undead),\n
81  * while the "pits" have a collection of monsters of a given type placed\n
82  * around the room in an organized manner (orc, troll, giant, dragon, or\n
83  * demon).  Note that both "nests" and "pits" are now "level dependant",\n
84  * and both make 16 "expensive" calls to the "get_mon_num()" function.\n
85  *\n
86  * Note that the cave grid flags changed in a rather drastic manner\n
87  * for Angband 2.8.0 (and 2.7.9+), in particular, dungeon terrain\n
88  * features, such as doors and stairs and traps and rubble and walls,\n
89  * are all handled as a set of 64 possible "terrain features", and\n
90  * not as "fake" objects (440-479) as in pre-2.8.0 versions.\n
91  *\n
92  * The 64 new "dungeon features" will also be used for "visual display"\n
93  * but we must be careful not to allow, for example, the user to display\n
94  * hidden traps in a different way from floors, or secret doors in a way\n
95  * different from granite walls, or even permanent granite in a different\n
96  * way from granite.  XXX XXX XXX\n
97  */
98
99 #include "angband.h"
100 #include "generate.h"
101 #include "grid.h"
102 #include "rooms.h"
103 #include "streams.h"
104
105 int dun_tun_rnd; 
106 int dun_tun_chg;
107 int dun_tun_con;
108 int dun_tun_pen;
109 int dun_tun_jct;
110
111
112 /*!
113  * Dungeon generation data -- see "cave_gen()"
114  */
115 dun_data *dun;
116
117
118 /*!
119  * @brief 上下左右の外壁数をカウントする / Count the number of walls adjacent to the given grid.
120  * @param y 基準のy座標
121  * @param x 基準のx座標
122  * @return 隣接する外壁の数
123  * @note Assumes "in_bounds(y, x)"
124  * @details We count only granite walls and permanent walls.
125  */
126 static int next_to_walls(int y, int x)
127 {
128         int k = 0;
129
130         if (in_bounds(y + 1, x) && is_extra_bold(y + 1, x)) k++;
131         if (in_bounds(y - 1, x) && is_extra_bold(y - 1, x)) k++;
132         if (in_bounds(y, x + 1) && is_extra_bold(y, x + 1)) k++;
133         if (in_bounds(y, x - 1) && is_extra_bold(y, x - 1)) k++;
134
135         return (k);
136 }
137
138 /*!
139  * @brief alloc_stairs()の補助として指定の位置に階段を生成できるかの判定を行う / Helper function for alloc_stairs(). Is this a good location for stairs?
140  * @param y 基準のy座標
141  * @param x 基準のx座標
142  * @param walls 最低減隣接させたい外壁の数
143  * @return 階段を生成して問題がないならばTRUEを返す。
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  * @brief 外壁に隣接させて階段を生成する / Places some staircases near walls
164  * @param feat 配置したい地形ID
165  * @param num 配置したい階段の数
166  * @param walls 最低減隣接させたい外壁の数
167  * @return 規定数通りに生成に成功したらTRUEを返す。
168  */
169 static bool alloc_stairs(int feat, int num, int walls)
170 {
171         int i;
172         int shaft_num = 0;
173
174         feature_type *f_ptr = &f_info[feat];
175
176         if (have_flag(f_ptr->flags, FF_LESS))
177         {
178                 /* No up stairs in town or in ironman mode */
179                 if (ironman_downward || !dun_level) return TRUE;
180
181                 if (dun_level > d_info[dungeon_type].mindepth)
182                         shaft_num = (randint1(num+1))/2;
183         }
184         else if (have_flag(f_ptr->flags, FF_MORE))
185         {
186                 int q_idx = quest_number(dun_level);
187
188                 /* No downstairs on quest levels */
189                 if (dun_level > 1 && q_idx)
190                 {
191                         monster_race *r_ptr = &r_info[quest[q_idx].r_idx];
192
193                         /* The quest monster(s) is still alive? */
194                         if (!(r_ptr->flags1 & RF1_UNIQUE) || 0 < r_ptr->max_num)
195                                 return TRUE;
196                 }
197
198                 /* No downstairs at the bottom */
199                 if (dun_level >= d_info[dungeon_type].maxdepth) return TRUE;
200
201                 if ((dun_level < d_info[dungeon_type].maxdepth-1) && !quest_number(dun_level+1))
202                         shaft_num = (randint1(num)+1)/2;
203         }
204
205         /* Paranoia */
206         else return FALSE;
207
208
209         /* Place "num" stairs */
210         for (i = 0; i < num; i++)
211         {
212                 while (TRUE)
213                 {
214                         int y = 0, x = 0;
215                         cave_type *c_ptr;
216
217                         int candidates = 0;
218                         int pick;
219
220                         for (y = 1; y < cur_hgt - 1; y++)
221                         {
222                                 for (x = 1; x < cur_wid - 1; x++)
223                                 {
224                                         if (alloc_stairs_aux(y, x, walls))
225                                         {
226                                                 /* A valid space found */
227                                                 candidates++;
228                                         }
229                                 }
230                         }
231
232                         /* No valid place! */
233                         if (!candidates)
234                         {
235                                 /* There are exactly no place! */
236                                 if (walls <= 0) return FALSE;
237
238                                 /* Decrease walls limit, and try again */
239                                 walls--;
240                                 continue;
241                         }
242
243                         /* Choose a random one */
244                         pick = randint1(candidates);
245
246                         for (y = 1; y < cur_hgt - 1; y++)
247                         {
248                                 for (x = 1; x < cur_wid - 1; x++)
249                                 {
250                                         if (alloc_stairs_aux(y, x, walls))
251                                         {
252                                                 pick--;
253
254                                                 /* Is this a picked one? */
255                                                 if (!pick) break;
256                                         }
257                                 }
258
259                                 if (!pick) break;
260                         }
261
262                         /* Access the grid */
263                         c_ptr = &cave[y][x];
264
265                         /* Clear possible garbage of hidden trap */
266                         c_ptr->mimic = 0;
267
268                         /* Clear previous contents, add stairs */
269                         c_ptr->feat = (i < shaft_num) ? feat_state(feat, FF_SHAFT) : feat;
270
271                         /* No longer "FLOOR" */
272                         c_ptr->info &= ~(CAVE_FLOOR);
273
274                         /* Success */
275                         break;
276                 }
277         }
278         return TRUE;
279 }
280
281 /*!
282  * @brief フロア上のランダム位置に各種オブジェクトを配置する / Allocates some objects (using "place" and "type")
283  * @param set 配置したい地形の種類
284  * @param typ 配置したいオブジェクトの種類
285  * @param num 配置したい数
286  * @return 規定数通りに生成に成功したらTRUEを返す。
287  */
288 static void alloc_object(int set, int typ, int num)
289 {
290         int y = 0, x = 0, k;
291         int dummy = 0;
292         cave_type *c_ptr;
293
294         /* A small level has few objects. */
295         num = num * cur_hgt * cur_wid / (MAX_HGT*MAX_WID) +1;
296
297         /* Place some objects */
298         for (k = 0; k < num; k++)
299         {
300                 /* Pick a "legal" spot */
301                 while (dummy < SAFE_MAX_ATTEMPTS)
302                 {
303                         bool room;
304
305                         dummy++;
306
307                         /* Location */
308                         y = randint0(cur_hgt);
309                         x = randint0(cur_wid);
310
311                         c_ptr = &cave[y][x];
312
313                         /* Require "naked" floor grid */
314                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
315
316                         /* Avoid player location */
317                         if (player_bold(y, x)) continue;
318
319                         /* Check for "room" */
320                         room = (cave[y][x].info & CAVE_ROOM) ? TRUE : FALSE;
321
322                         /* Require corridor? */
323                         if ((set == ALLOC_SET_CORR) && room) continue;
324
325                         /* Require room? */
326                         if ((set == ALLOC_SET_ROOM) && !room) continue;
327
328                         /* Accept it */
329                         break;
330                 }
331
332                 if (dummy >= SAFE_MAX_ATTEMPTS)
333                 {
334                         if (cheat_room)
335                         {
336 #ifdef JP
337 msg_print("警告!アイテムを配置できません!");
338 #else
339                                 msg_print("Warning! Could not place object!");
340 #endif
341
342                         }
343                         return;
344                 }
345
346
347                 /* Place something */
348                 switch (typ)
349                 {
350                         case ALLOC_TYP_RUBBLE:
351                         {
352                                 place_rubble(y, x);
353                                 cave[y][x].info &= ~(CAVE_FLOOR);
354                                 break;
355                         }
356
357                         case ALLOC_TYP_TRAP:
358                         {
359                                 place_trap(y, x);
360                                 cave[y][x].info &= ~(CAVE_FLOOR);
361                                 break;
362                         }
363
364                         case ALLOC_TYP_GOLD:
365                         {
366                                 place_gold(y, x);
367                                 break;
368                         }
369
370                         case ALLOC_TYP_OBJECT:
371                         {
372                                 place_object(y, x, 0L);
373                                 break;
374                         }
375                 }
376         }
377 }
378
379 /*!
380  * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
381  * @param y1 基準となるマスのY座標
382  * @param x1 基準となるマスのX座標
383  * @return 通路の数
384  * @note Assumes "in_bounds(y1, x1)"
385  * @details
386  * XXX XXX This routine currently only counts actual "empty floor"\n
387  * grids which are not in rooms.  We might want to also count stairs,\n
388  * open doors, closed doors, etc.
389  */
390 static int next_to_corr(int y1, int x1)
391 {
392         int i, y, x, k = 0;
393
394         cave_type *c_ptr;
395
396         /* Scan adjacent grids */
397         for (i = 0; i < 4; i++)
398         {
399                 /* Extract the location */
400                 y = y1 + ddy_ddd[i];
401                 x = x1 + ddx_ddd[i];
402
403                 /* Access the grid */
404                 c_ptr = &cave[y][x];
405
406                 /* Skip non floors */
407                 if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
408
409                 /* Skip non "empty floor" grids */
410                 if (!is_floor_grid(c_ptr))
411                         continue;
412
413                 /* Skip grids inside rooms */
414                 if (c_ptr->info & (CAVE_ROOM)) continue;
415
416                 /* Count these grids */
417                 k++;
418         }
419
420         /* Return the number of corridors */
421         return (k);
422 }
423
424
425 /*!
426  * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
427  * @param y 判定を行いたいマスのY座標
428  * @param x 判定を行いたいマスのX座標
429  * @return ドアを設置可能ならばTRUEを返す
430  * @note Assumes "in_bounds(y1, x1)"
431  * @details
432  * XXX XXX XXX\n
433  * Assumes "in_bounds(y, x)"\n
434  */
435 static bool possible_doorway(int y, int x)
436 {
437         /* Count the adjacent corridors */
438         if (next_to_corr(y, x) >= 2)
439         {
440                 /* Check Vertical */
441                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
442                     cave_have_flag_bold(y + 1, x, FF_WALL))
443                 {
444                         return (TRUE);
445                 }
446
447                 /* Check Horizontal */
448                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
449                     cave_have_flag_bold(y, x + 1, FF_WALL))
450                 {
451                         return (TRUE);
452                 }
453         }
454
455         /* No doorway */
456         return (FALSE);
457 }
458
459 /*!
460  * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
461  * @param y 設置を行いたいマスのY座標
462  * @param x 設置を行いたいマスのX座標
463  * @return なし
464  */
465 static void try_door(int y, int x)
466 {
467         /* Paranoia */
468         if (!in_bounds(y, x)) return;
469
470         /* Ignore walls */
471         if (cave_have_flag_bold(y, x, FF_WALL)) return;
472
473         /* Ignore room grids */
474         if (cave[y][x].info & (CAVE_ROOM)) return;
475
476         /* Occasional door (if allowed) */
477         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
478         {
479                 /* Place a door */
480                 place_random_door(y, x, FALSE);
481         }
482 }
483
484
485 /*!
486  * @brief クエストに関わるモンスターの配置を行う / Place quest monsters
487  * @return 成功したならばTRUEを返す
488  */
489 bool place_quest_monsters(void)
490 {
491         int i;
492
493         /* Handle the quest monster placements */
494         for (i = 0; i < max_quests; i++)
495         {
496                 monster_race *r_ptr;
497                 u32b mode;
498                 int j;
499
500                 if (quest[i].status != QUEST_STATUS_TAKEN ||
501                     (quest[i].type != QUEST_TYPE_KILL_LEVEL &&
502                      quest[i].type != QUEST_TYPE_RANDOM) ||
503                     quest[i].level != dun_level ||
504                     dungeon_type != quest[i].dungeon ||
505                     (quest[i].flags & QUEST_FLAG_PRESET))
506                 {
507                         /* Ignore it */
508                         continue;
509                 }
510
511                 r_ptr = &r_info[quest[i].r_idx];
512
513                 /* Hack -- "unique" monsters must be "unique" */
514                 if ((r_ptr->flags1 & RF1_UNIQUE) &&
515                     (r_ptr->cur_num >= r_ptr->max_num)) continue;
516
517                 mode = (PM_NO_KAGE | PM_NO_PET);
518
519                 if (!(r_ptr->flags1 & RF1_FRIENDS))
520                         mode |= PM_ALLOW_GROUP;
521
522                 for (j = 0; j < (quest[i].max_num - quest[i].cur_num); j++)
523                 {
524                         int k;
525
526                         for (k = 0; k < SAFE_MAX_ATTEMPTS; k++)
527                         {
528                                 int x = 0, y = 0;
529                                 int l;
530
531                                 /* Find an empty grid */
532                                 for (l = SAFE_MAX_ATTEMPTS; l > 0; l--)
533                                 {
534                                         cave_type    *c_ptr;
535                                         feature_type *f_ptr;
536
537                                         y = randint0(cur_hgt);
538                                         x = randint0(cur_wid);
539
540                                         c_ptr = &cave[y][x];
541                                         f_ptr = &f_info[c_ptr->feat];
542
543                                         if (!have_flag(f_ptr->flags, FF_MOVE) && !have_flag(f_ptr->flags, FF_CAN_FLY)) continue;
544                                         if (!monster_can_enter(y, x, r_ptr, 0)) continue;
545                                         if (distance(y, x, p_ptr->y, p_ptr->x) < 10) continue;
546                                         if (c_ptr->info & CAVE_ICKY) continue;
547                                         else break;
548                                 }
549
550                                 /* Failed to place */
551                                 if (!l) return FALSE;
552
553                                 /* Try to place the monster */
554                                 if (place_monster_aux(0, y, x, quest[i].r_idx, mode))
555                                 {
556                                         /* Success */
557                                         break;
558                                 }
559                                 else
560                                 {
561                                         /* Failure - Try again */
562                                         continue;
563                                 }
564                         }
565
566                         /* Failed to place */
567                         if (k == SAFE_MAX_ATTEMPTS) return FALSE;
568                 }
569         }
570
571         return TRUE;
572 }
573
574
575 /*!
576  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
577  * @param c_ptr 永久壁を廃止したいマス構造体の参照ポインタ
578  * @return なし
579  */
580 static void set_bound_perm_wall(cave_type *c_ptr)
581 {
582         if (bound_walls_perm)
583         {
584                 /* Clear boundary mimic */
585                 c_ptr->mimic = 0;
586         }
587         else
588         {
589                 feature_type *f_ptr = &f_info[c_ptr->feat];
590
591                 /* Hack -- Decline boundary walls with known treasure  */
592                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
593                     !have_flag(f_ptr->flags, FF_SECRET))
594                         c_ptr->feat = feat_state(c_ptr->feat, FF_ENSECRET);
595
596                 /* Set boundary mimic */
597                 c_ptr->mimic = c_ptr->feat;
598         }
599
600         /* Add "solid" perma-wall */
601         place_solid_perm_grid(c_ptr);
602 }
603
604 /*!
605  * @brief フロアに洞窟や湖を配置する / Generate various caverns and lakes
606  * @details There were moved from cave_gen().
607  * @return なし
608  */
609 static void gen_caverns_and_lakes(void)
610 {
611 #ifdef ALLOW_CAVERNS_AND_LAKES
612         /* Possible "destroyed" level */
613         if ((dun_level > 30) && one_in_(DUN_DEST*2) && (small_levels) && (d_info[dungeon_type].flags1 & DF1_DESTROY))
614         {
615                 dun->destroyed = TRUE;
616
617                 /* extra rubble around the place looks cool */
618                 build_lake(one_in_(2) ? LAKE_T_CAVE : LAKE_T_EARTH_VAULT);
619         }
620
621         /* Make a lake some of the time */
622         if (one_in_(LAKE_LEVEL) && !dun->empty_level && !dun->destroyed &&
623             (d_info[dungeon_type].flags1 & DF1_LAKE_MASK))
624         {
625                 int count = 0;
626                 if (d_info[dungeon_type].flags1 & DF1_LAKE_WATER) count += 3;
627                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA) count += 3;
628                 if (d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) count += 3;
629                 if (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) count += 3;
630
631                 if (d_info[dungeon_type].flags1 & DF1_LAKE_LAVA)
632                 {
633                         /* Lake of Lava */
634                         if ((dun_level > 80) && (randint0(count) < 2)) dun->laketype = LAKE_T_LAVA;
635                         count -= 2;
636
637                         /* Lake of Lava2 */
638                         if (!dun->laketype && (dun_level > 80) && one_in_(count)) dun->laketype = LAKE_T_FIRE_VAULT;
639                         count--;
640                 }
641
642                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_WATER) && !dun->laketype)
643                 {
644                         /* Lake of Water */
645                         if ((dun_level > 50) && randint0(count) < 2) dun->laketype = LAKE_T_WATER;
646                         count -= 2;
647
648                         /* Lake of Water2 */
649                         if (!dun->laketype && (dun_level > 50) && one_in_(count)) dun->laketype = LAKE_T_WATER_VAULT;
650                         count--;
651                 }
652
653                 if ((d_info[dungeon_type].flags1 & DF1_LAKE_RUBBLE) && !dun->laketype)
654                 {
655                         /* Lake of rubble */
656                         if ((dun_level > 35) && (randint0(count) < 2)) dun->laketype = LAKE_T_CAVE;
657                         count -= 2;
658
659                         /* Lake of rubble2 */
660                         if (!dun->laketype && (dun_level > 35) && one_in_(count)) dun->laketype = LAKE_T_EARTH_VAULT;
661                         count--;
662                 }
663
664                 /* Lake of tree */
665                 if ((dun_level > 5) && (d_info[dungeon_type].flags1 & DF1_LAKE_TREE) && !dun->laketype) dun->laketype = LAKE_T_AIR_VAULT;
666
667                 if (dun->laketype)
668                 {
669                         if (cheat_room)
670 #ifdef JP
671                                 msg_print("湖を生成。");
672 #else
673                                 msg_print("Lake on the level.");
674 #endif
675
676                         build_lake(dun->laketype);
677                 }
678         }
679
680         if ((dun_level > DUN_CAVERN) && !dun->empty_level &&
681             (d_info[dungeon_type].flags1 & DF1_CAVERN) &&
682             !dun->laketype && !dun->destroyed && (randint1(1000) < dun_level))
683         {
684                 dun->cavern = TRUE;
685
686                 /* make a large fractal cave in the middle of the dungeon */
687
688                 if (cheat_room)
689 #ifdef JP
690                         msg_print("洞窟を生成。");
691 #else
692                         msg_print("Cavern on level.");
693 #endif
694
695                 build_cavern();
696         }
697 #endif /* ALLOW_CAVERNS_AND_LAKES */
698
699         /* Hack -- No destroyed "quest" levels */
700         if (quest_number(dun_level)) dun->destroyed = FALSE;
701 }
702
703
704 /*!
705  * @brief ダンジョン生成のメインルーチン / Generate a new dungeon level
706  * @details Note that "dun_body" adds about 4000 bytes of memory to the stack.
707  * @return ダンジョン生成が全て無事に成功したらTRUEを返す。
708  */
709 static bool cave_gen(void)
710 {
711         int i, k, y, x;
712
713         dun_data dun_body;
714
715         /* Global data */
716         dun = &dun_body;
717
718         dun->destroyed = FALSE;
719         dun->empty_level = FALSE;
720         dun->cavern = FALSE;
721         dun->laketype = 0;
722
723         /* Fill the arrays of floors and walls in the good proportions */
724         set_floor_and_wall(dungeon_type);
725
726         /* Prepare allocation table */
727         get_mon_num_prep(get_monster_hook(), NULL);
728
729         /* Randomize the dungeon creation values */
730         dun_tun_rnd = rand_range(DUN_TUN_RND_MIN, DUN_TUN_RND_MAX);
731         dun_tun_chg = rand_range(DUN_TUN_CHG_MIN, DUN_TUN_CHG_MAX);
732         dun_tun_con = rand_range(DUN_TUN_CON_MIN, DUN_TUN_CON_MAX);
733         dun_tun_pen = rand_range(DUN_TUN_PEN_MIN, DUN_TUN_PEN_MAX);
734         dun_tun_jct = rand_range(DUN_TUN_JCT_MIN, DUN_TUN_JCT_MAX);
735
736         /* Actual maximum number of rooms on this level */
737         dun->row_rooms = cur_hgt / BLOCK_HGT;
738         dun->col_rooms = cur_wid / BLOCK_WID;
739
740         /* Initialize the room table */
741         for (y = 0; y < dun->row_rooms; y++)
742         {
743                 for (x = 0; x < dun->col_rooms; x++)
744                 {
745                         dun->room_map[y][x] = FALSE;
746                 }
747         }
748
749         /* No rooms yet */
750         dun->cent_n = 0;
751
752         /* Empty arena levels */
753         if (ironman_empty_levels || ((d_info[dungeon_type].flags1 & DF1_ARENA) && (empty_levels && one_in_(EMPTY_LEVEL))))
754         {
755                 dun->empty_level = TRUE;
756
757                 if (cheat_room)
758 #ifdef JP
759                         msg_print("アリーナレベル");
760 #else
761                         msg_print("Arena level.");
762 #endif
763         }
764
765         if (dun->empty_level)
766         {
767                 /* Start with floors */
768                 for (y = 0; y < cur_hgt; y++)
769                 {
770                         for (x = 0; x < cur_wid; x++)
771                         {
772                                 place_floor_bold(y, x);
773                         }
774                 }
775
776                 /* Special boundary walls -- Top and bottom */
777                 for (x = 0; x < cur_wid; x++)
778                 {
779                         place_extra_bold(0, x);
780                         place_extra_bold(cur_hgt - 1, x);
781                 }
782
783                 /* Special boundary walls -- Left and right */
784                 for (y = 1; y < (cur_hgt - 1); y++)
785                 {
786                         place_extra_bold(y, 0);
787                         place_extra_bold(y, cur_wid - 1);
788                 }
789         }
790         else
791         {
792                 /* Start with walls */
793                 for (y = 0; y < cur_hgt; y++)
794                 {
795                         for (x = 0; x < cur_wid; x++)
796                         {
797                                 place_extra_bold(y, x);
798                         }
799                 }
800         }
801
802
803         /* Generate various caverns and lakes */
804         gen_caverns_and_lakes();
805
806
807         /* Build maze */
808         if (d_info[dungeon_type].flags1 & DF1_MAZE)
809         {
810                 build_maze_vault(cur_wid/2-1, cur_hgt/2-1, cur_wid-4, cur_hgt-4, FALSE);
811
812                 /* Place 3 or 4 down stairs near some walls */
813                 if (!alloc_stairs(feat_down_stair, rand_range(2, 3), 3)) return FALSE;
814
815                 /* Place 1 or 2 up stairs near some walls */
816                 if (!alloc_stairs(feat_up_stair, 1, 3)) return FALSE;
817         }
818
819         /* Build some rooms */
820         else
821         {
822                 int tunnel_fail_count = 0;
823
824                 /*
825                  * Build each type of room in turn until we cannot build any more.
826                  */
827                 if (!generate_rooms()) return FALSE;
828
829
830                 /* Make a hole in the dungeon roof sometimes at level 1 */
831                 if (dun_level == 1)
832                 {
833                         while (one_in_(DUN_MOS_DEN))
834                         {
835                                 place_trees(randint1(cur_wid - 2), randint1(cur_hgt - 2));
836                         }
837                 }
838
839                 /* Destroy the level if necessary */
840                 if (dun->destroyed) destroy_level();
841
842                 /* Hack -- Add some rivers */
843                 if (one_in_(3) && (randint1(dun_level) > 5))
844                 {
845                         int feat1 = 0, feat2 = 0;
846
847                         /* Choose water or lava */
848                         if ((randint1(MAX_DEPTH * 2) - 1 > dun_level) && (d_info[dungeon_type].flags1 & DF1_WATER_RIVER))
849                         {
850                                 feat1 = feat_deep_water;
851                                 feat2 = feat_shallow_water;
852                         }
853                         else if  (d_info[dungeon_type].flags1 & DF1_LAVA_RIVER)
854                         {
855                                 feat1 = feat_deep_lava;
856                                 feat2 = feat_shallow_lava;
857                         }
858                         else feat1 = 0;
859
860                         if (feat1)
861                         {
862                                 feature_type *f_ptr = &f_info[feat1];
863
864                                 /* Only add river if matches lake type or if have no lake at all */
865                                 if (((dun->laketype == LAKE_T_LAVA) && have_flag(f_ptr->flags, FF_LAVA)) ||
866                                     ((dun->laketype == LAKE_T_WATER) && have_flag(f_ptr->flags, FF_WATER)) ||
867                                      !dun->laketype)
868                                 {
869                                         add_river(feat1, feat2);
870                                 }
871                         }
872                 }
873
874                 /* Hack -- Scramble the room order */
875                 for (i = 0; i < dun->cent_n; i++)
876                 {
877                         int ty, tx;
878                         int pick = rand_range(0, i);
879
880                         ty = dun->cent[i].y;
881                         tx = dun->cent[i].x;
882                         dun->cent[i].y = dun->cent[pick].y;
883                         dun->cent[i].x = dun->cent[pick].x;
884                         dun->cent[pick].y = ty;
885                         dun->cent[pick].x = tx;
886                 }
887
888                 /* Start with no tunnel doors */
889                 dun->door_n = 0;
890
891                 /* Hack -- connect the first room to the last room */
892                 y = dun->cent[dun->cent_n-1].y;
893                 x = dun->cent[dun->cent_n-1].x;
894
895                 /* Connect all the rooms together */
896                 for (i = 0; i < dun->cent_n; i++)
897                 {
898                         int j;
899
900                         /* Reset the arrays */
901                         dun->tunn_n = 0;
902                         dun->wall_n = 0;
903
904                         /* Connect the room to the previous room */
905                         if (randint1(dun_level) > d_info[dungeon_type].tunnel_percent)
906                         {
907                                 /* make cave-like tunnel */
908                                 (void)build_tunnel2(dun->cent[i].x, dun->cent[i].y, x, y, 2, 2);
909                         }
910                         else
911                         {
912                                 /* make normal tunnel */
913                                 if (!build_tunnel(dun->cent[i].y, dun->cent[i].x, y, x)) tunnel_fail_count++;
914                         }
915
916                         if (tunnel_fail_count >= 2) return FALSE;
917
918                         /* Turn the tunnel into corridor */
919                         for (j = 0; j < dun->tunn_n; j++)
920                         {
921                                 cave_type *c_ptr;
922                                 feature_type *f_ptr;
923
924                                 /* Access the grid */
925                                 y = dun->tunn[j].y;
926                                 x = dun->tunn[j].x;
927
928                                 /* Access the grid */
929                                 c_ptr = &cave[y][x];
930                                 f_ptr = &f_info[c_ptr->feat];
931
932                                 /* Clear previous contents (if not a lake), add a floor */
933                                 if (!have_flag(f_ptr->flags, FF_MOVE) || (!have_flag(f_ptr->flags, FF_WATER) && !have_flag(f_ptr->flags, FF_LAVA)))
934                                 {
935                                         /* Clear mimic type */
936                                         c_ptr->mimic = 0;
937
938                                         place_floor_grid(c_ptr);
939                                 }
940                         }
941
942                         /* Apply the piercings that we found */
943                         for (j = 0; j < dun->wall_n; j++)
944                         {
945                                 cave_type *c_ptr;
946
947                                 /* Access the grid */
948                                 y = dun->wall[j].y;
949                                 x = dun->wall[j].x;
950
951                                 /* Access the grid */
952                                 c_ptr = &cave[y][x];
953
954                                 /* Clear mimic type */
955                                 c_ptr->mimic = 0;
956
957                                 /* Clear previous contents, add up floor */
958                                 place_floor_grid(c_ptr);
959
960                                 /* Occasional doorway */
961                                 if ((randint0(100) < dun_tun_pen) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
962                                 {
963                                         /* Place a random door */
964                                         place_random_door(y, x, TRUE);
965                                 }
966                         }
967
968                         /* Remember the "previous" room */
969                         y = dun->cent[i].y;
970                         x = dun->cent[i].x;
971                 }
972
973                 /* Place intersection doors */
974                 for (i = 0; i < dun->door_n; i++)
975                 {
976                         /* Extract junction location */
977                         y = dun->door[i].y;
978                         x = dun->door[i].x;
979
980                         /* Try placing doors */
981                         try_door(y, x - 1);
982                         try_door(y, x + 1);
983                         try_door(y - 1, x);
984                         try_door(y + 1, x);
985                 }
986
987                 /* Place 3 or 4 down stairs near some walls */
988                 if (!alloc_stairs(feat_down_stair, rand_range(3, 4), 3)) return FALSE;
989
990                 /* Place 1 or 2 up stairs near some walls */
991                 if (!alloc_stairs(feat_up_stair, rand_range(1, 2), 3)) return FALSE;
992         }
993
994         if (!dun->laketype)
995         {
996                 if (d_info[dungeon_type].stream2)
997                 {
998                         /* Hack -- Add some quartz streamers */
999                         for (i = 0; i < DUN_STR_QUA; i++)
1000                         {
1001                                 build_streamer(d_info[dungeon_type].stream2, DUN_STR_QC);
1002                         }
1003                 }
1004
1005                 if (d_info[dungeon_type].stream1)
1006                 {
1007                         /* Hack -- Add some magma streamers */
1008                         for (i = 0; i < DUN_STR_MAG; i++)
1009                         {
1010                                 build_streamer(d_info[dungeon_type].stream1, DUN_STR_MC);
1011                         }
1012                 }
1013         }
1014
1015         /* Special boundary walls -- Top and bottom */
1016         for (x = 0; x < cur_wid; x++)
1017         {
1018                 set_bound_perm_wall(&cave[0][x]);
1019                 set_bound_perm_wall(&cave[cur_hgt - 1][x]);
1020         }
1021
1022         /* Special boundary walls -- Left and right */
1023         for (y = 1; y < (cur_hgt - 1); y++)
1024         {
1025                 set_bound_perm_wall(&cave[y][0]);
1026                 set_bound_perm_wall(&cave[y][cur_wid - 1]);
1027         }
1028
1029         /* Determine the character location */
1030         if (!new_player_spot()) return FALSE;
1031
1032         if (!place_quest_monsters()) return FALSE;
1033
1034         /* Basic "amount" */
1035         k = (dun_level / 3);
1036         if (k > 10) k = 10;
1037         if (k < 2) k = 2;
1038
1039         /* Pick a base number of monsters */
1040         i = d_info[dungeon_type].min_m_alloc_level;
1041
1042         /* To make small levels a bit more playable */
1043         if (cur_hgt < MAX_HGT || cur_wid < MAX_WID)
1044         {
1045                 int small_tester = i;
1046
1047                 i = (i * cur_hgt) / MAX_HGT;
1048                 i = (i * cur_wid) / MAX_WID;
1049                 i += 1;
1050
1051                 if (i > small_tester) i = small_tester;
1052                 else if (cheat_hear)
1053                 {
1054 #ifdef JP
1055 msg_format("モンスター数基本値を %d から %d に減らします", small_tester, i);
1056 #else
1057                         msg_format("Reduced monsters base from %d to %d", small_tester, i);
1058 #endif
1059
1060                 }
1061         }
1062
1063         i += randint1(8);
1064
1065         /* Put some monsters in the dungeon */
1066         for (i = i + k; i > 0; i--)
1067         {
1068                 (void)alloc_monster(0, PM_ALLOW_SLEEP);
1069         }
1070
1071         /* Place some traps in the dungeon */
1072         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_TRAP, randint1(k));
1073
1074         /* Put some rubble in corridors (except NO_CAVE dungeon (Castle)) */
1075         if (!(d_info[dungeon_type].flags1 & DF1_NO_CAVE)) alloc_object(ALLOC_SET_CORR, ALLOC_TYP_RUBBLE, randint1(k));
1076
1077         /* Mega Hack -- No object at first level of deeper dungeon */
1078         if (p_ptr->enter_dungeon && dun_level > 1)
1079         {
1080                 /* No stair scum! */
1081                 object_level = 1;
1082         }
1083
1084         /* Put some objects in rooms */
1085         alloc_object(ALLOC_SET_ROOM, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ROOM, 3));
1086
1087         /* Put some objects/gold in the dungeon */
1088         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_OBJECT, randnor(DUN_AMT_ITEM, 3));
1089         alloc_object(ALLOC_SET_BOTH, ALLOC_TYP_GOLD, randnor(DUN_AMT_GOLD, 3));
1090
1091         /* Set back to default */
1092         object_level = base_level;
1093
1094         /* Put the Guardian */
1095         if (!alloc_guardian(TRUE)) return FALSE;
1096
1097         if (dun->empty_level && (!one_in_(DARK_EMPTY) || (randint1(100) > dun_level)) && !(d_info[dungeon_type].flags1 & DF1_DARKNESS))
1098         {
1099                 /* Lite the cave */
1100                 for (y = 0; y < cur_hgt; y++)
1101                 {
1102                         for (x = 0; x < cur_wid; x++)
1103                         {
1104                                 cave[y][x].info |= (CAVE_GLOW);
1105                         }
1106                 }
1107         }
1108
1109         return TRUE;
1110 }
1111
1112 /*!
1113  * @brief 闘技場用のアリーナ地形を作成する / Builds the arena after it is entered -KMW-
1114  * @return なし
1115  */
1116 static void build_arena(void)
1117 {
1118         int yval, y_height, y_depth, xval, x_left, x_right;
1119         register int i, j;
1120
1121         yval = SCREEN_HGT / 2;
1122         xval = SCREEN_WID / 2;
1123         y_height = yval - 10;
1124         y_depth = yval + 10;
1125         x_left = xval - 32;
1126         x_right = xval + 32;
1127
1128         for (i = y_height; i <= y_height + 5; i++)
1129                 for (j = x_left; j <= x_right; j++)
1130                 {
1131                         place_extra_perm_bold(i, j);
1132                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1133                 }
1134         for (i = y_depth; i >= y_depth - 5; i--)
1135                 for (j = x_left; j <= x_right; j++)
1136                 {
1137                         place_extra_perm_bold(i, j);
1138                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1139                 }
1140         for (j = x_left; j <= x_left + 17; j++)
1141                 for (i = y_height; i <= y_depth; i++)
1142                 {
1143                         place_extra_perm_bold(i, j);
1144                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1145                 }
1146         for (j = x_right; j >= x_right - 17; j--)
1147                 for (i = y_height; i <= y_depth; i++)
1148                 {
1149                         place_extra_perm_bold(i, j);
1150                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1151                 }
1152
1153         place_extra_perm_bold(y_height+6, x_left+18);
1154         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1155         place_extra_perm_bold(y_depth-6, x_left+18);
1156         cave[y_depth-6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1157         place_extra_perm_bold(y_height+6, x_right-18);
1158         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1159         place_extra_perm_bold(y_depth-6, x_right-18);
1160         cave[y_depth-6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1161
1162         i = y_height + 5;
1163         j = xval;
1164         cave[i][j].feat = f_tag_to_index("ARENA_GATE");
1165         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1166         player_place(i, j);
1167 }
1168
1169 /*!
1170  * @brief 闘技場への入場処理 / Town logic flow for generation of arena -KMW-
1171  * @return なし
1172  */
1173 static void arena_gen(void)
1174 {
1175         int y, x;
1176         int qy = 0;
1177         int qx = 0;
1178
1179         /* Smallest area */
1180         cur_hgt = SCREEN_HGT;
1181         cur_wid = SCREEN_WID;
1182
1183         /* Start with solid walls */
1184         for (y = 0; y < MAX_HGT; y++)
1185         {
1186                 for (x = 0; x < MAX_WID; x++)
1187                 {
1188                         /* Create "solid" perma-wall */
1189                         place_solid_perm_bold(y, x);
1190
1191                         /* Illuminate and memorize the walls */
1192                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1193                 }
1194         }
1195
1196         /* Then place some floors */
1197         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1198         {
1199                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1200                 {
1201                         /* Create empty floor */
1202                         cave[y][x].feat = feat_floor;
1203                 }
1204         }
1205
1206         build_arena();
1207
1208         if(!place_monster_aux(0, p_ptr->y + 5, p_ptr->x, arena_info[p_ptr->arena_number].r_idx, (PM_NO_KAGE | PM_NO_PET)))
1209         {
1210                 p_ptr->exit_bldg = TRUE;
1211                 p_ptr->arena_number++;
1212 #ifdef JP
1213                 msg_print("相手は欠場した。あなたの不戦勝だ。");
1214 #else
1215                 msg_print("The enemy is unable appear. You won by default.");
1216 #endif
1217         }
1218
1219 }
1220
1221 /*!
1222  * @brief モンスター闘技場のフロア生成 / Builds the arena after it is entered -KMW-
1223  * @return なし
1224  */
1225 static void build_battle(void)
1226 {
1227         int yval, y_height, y_depth, xval, x_left, x_right;
1228         register int i, j;
1229
1230         yval = SCREEN_HGT / 2;
1231         xval = SCREEN_WID / 2;
1232         y_height = yval - 10;
1233         y_depth = yval + 10;
1234         x_left = xval - 32;
1235         x_right = xval + 32;
1236
1237         for (i = y_height; i <= y_height + 5; i++)
1238                 for (j = x_left; j <= x_right; j++)
1239                 {
1240                         place_extra_perm_bold(i, j);
1241                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1242                 }
1243         for (i = y_depth; i >= y_depth - 3; i--)
1244                 for (j = x_left; j <= x_right; j++)
1245                 {
1246                         place_extra_perm_bold(i, j);
1247                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1248                 }
1249         for (j = x_left; j <= x_left + 17; j++)
1250                 for (i = y_height; i <= y_depth; i++)
1251                 {
1252                         place_extra_perm_bold(i, j);
1253                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1254                 }
1255         for (j = x_right; j >= x_right - 17; j--)
1256                 for (i = y_height; i <= y_depth; i++)
1257                 {
1258                         place_extra_perm_bold(i, j);
1259                         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1260                 }
1261
1262         place_extra_perm_bold(y_height+6, x_left+18);
1263         cave[y_height+6][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1264         place_extra_perm_bold(y_depth-4, x_left+18);
1265         cave[y_depth-4][x_left+18].info |= (CAVE_GLOW | CAVE_MARK);
1266         place_extra_perm_bold(y_height+6, x_right-18);
1267         cave[y_height+6][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1268         place_extra_perm_bold(y_depth-4, x_right-18);
1269         cave[y_depth-4][x_right-18].info |= (CAVE_GLOW | CAVE_MARK);
1270
1271         for (i = y_height + 1; i <= y_height + 5; i++)
1272                 for (j = x_left + 20 + 2 * (y_height + 5 - i); j <= x_right - 20 - 2 * (y_height + 5 - i); j++)
1273                 {
1274                         cave[i][j].feat = feat_permanent_glass_wall;
1275                 }
1276
1277         i = y_height + 1;
1278         j = xval;
1279         cave[i][j].feat = f_tag_to_index("BUILDING_3");
1280         cave[i][j].info |= (CAVE_GLOW | CAVE_MARK);
1281         player_place(i, j);
1282 }
1283
1284 /*!
1285  * @brief モンスター闘技場への導入処理 / Town logic flow for generation of arena -KMW-
1286  * @return なし
1287  */
1288 static void battle_gen(void)
1289 {
1290         int y, x, i;
1291         int qy = 0;
1292         int qx = 0;
1293
1294         /* Start with solid walls */
1295         for (y = 0; y < MAX_HGT; y++)
1296         {
1297                 for (x = 0; x < MAX_WID; x++)
1298                 {
1299                         /* Create "solid" perma-wall */
1300                         place_solid_perm_bold(y, x);
1301
1302                         /* Illuminate and memorize the walls */
1303                         cave[y][x].info |= (CAVE_GLOW | CAVE_MARK);
1304                 }
1305         }
1306
1307         /* Then place some floors */
1308         for (y = qy + 1; y < qy + SCREEN_HGT - 1; y++)
1309         {
1310                 for (x = qx + 1; x < qx + SCREEN_WID - 1; x++)
1311                 {
1312                         /* Create empty floor */
1313                         cave[y][x].feat = feat_floor;
1314                 }
1315         }
1316
1317         build_battle();
1318
1319         for(i=0;i<4;i++)
1320         {
1321                 place_monster_aux(0, p_ptr->y + 8 + (i/2)*4, p_ptr->x - 2 + (i%2)*4, battle_mon[i],
1322                                   (PM_NO_KAGE | PM_NO_PET));
1323                 set_friendly(&m_list[cave[p_ptr->y+8+(i/2)*4][p_ptr->x-2+(i%2)*4].m_idx]);
1324         }
1325         for(i = 1; i < m_max; i++)
1326         {
1327                 monster_type *m_ptr = &m_list[i];
1328
1329                 if (!m_ptr->r_idx) continue;
1330
1331                 /* Hack -- Detect monster */
1332                 m_ptr->mflag2 |= (MFLAG2_MARK | MFLAG2_SHOW);
1333
1334                 /* Update the monster */
1335                 update_mon(i, FALSE);
1336         }
1337 }
1338
1339 /*!
1340  * @brief 固定マップクエストのフロア生成 / Generate a quest level
1341  * @return なし
1342  */
1343 static void quest_gen(void)
1344 {
1345         int x, y;
1346
1347
1348         /* Start with perm walls */
1349         for (y = 0; y < cur_hgt; y++)
1350         {
1351                 for (x = 0; x < cur_wid; x++)
1352                 {
1353                         place_solid_perm_bold(y, x);
1354                 }
1355         }
1356
1357         /* Set the quest level */
1358         base_level = quest[p_ptr->inside_quest].level;
1359         dun_level = base_level;
1360         object_level = base_level;
1361         monster_level = base_level;
1362
1363         if (record_stair) do_cmd_write_nikki(NIKKI_TO_QUEST, p_ptr->inside_quest, NULL);
1364
1365         /* Prepare allocation table */
1366         get_mon_num_prep(get_monster_hook(), NULL);
1367
1368         init_flags = INIT_CREATE_DUNGEON;
1369
1370         process_dungeon_file("q_info.txt", 0, 0, MAX_HGT, MAX_WID);
1371 }
1372
1373 /*!
1374  * @brief ダンジョン時のランダムフロア生成 / Make a real level
1375  * @return フロアの生成に成功したらTRUE
1376  */
1377 static bool level_gen(cptr *why)
1378 {
1379         int level_height, level_width;
1380
1381         if ((always_small_levels || ironman_small_levels ||
1382             (one_in_(SMALL_LEVEL) && small_levels) ||
1383              (d_info[dungeon_type].flags1 & DF1_BEGINNER) ||
1384             (d_info[dungeon_type].flags1 & DF1_SMALLEST)) &&
1385             !(d_info[dungeon_type].flags1 & DF1_BIG))
1386         {
1387                 if (cheat_room)
1388 #ifdef JP
1389                         msg_print("小さなフロア");
1390 #else
1391                         msg_print("A 'small' dungeon level.");
1392 #endif
1393
1394                 if (d_info[dungeon_type].flags1 & DF1_SMALLEST)
1395                 {
1396                         level_height = 1;
1397                         level_width = 1;
1398                 }
1399                 else if (d_info[dungeon_type].flags1 & DF1_BEGINNER)
1400                 {
1401                         level_height = 2;
1402                         level_width = 2;
1403                 }
1404                 else
1405                 {
1406                         do
1407                         {
1408                                 level_height = randint1(MAX_HGT/SCREEN_HGT);
1409                                 level_width = randint1(MAX_WID/SCREEN_WID);
1410                         }
1411                         while ((level_height == MAX_HGT/SCREEN_HGT) &&
1412                                    (level_width == MAX_WID/SCREEN_WID));
1413                 }
1414
1415                 cur_hgt = level_height * SCREEN_HGT;
1416                 cur_wid = level_width * SCREEN_WID;
1417
1418                 /* Assume illegal panel */
1419                 panel_row_min = cur_hgt;
1420                 panel_col_min = cur_wid;
1421
1422                 if (cheat_room)
1423                   msg_format("X:%d, Y:%d.", cur_wid, cur_hgt);
1424         }
1425         else
1426         {
1427                 /* Big dungeon */
1428                 cur_hgt = MAX_HGT;
1429                 cur_wid = MAX_WID;
1430
1431                 /* Assume illegal panel */
1432                 panel_row_min = cur_hgt;
1433                 panel_col_min = cur_wid;
1434         }
1435
1436         /* Make a dungeon */
1437         if (!cave_gen())
1438         {
1439 #ifdef JP
1440 *why = "ダンジョン生成に失敗";
1441 #else
1442                 *why = "could not place player";
1443 #endif
1444
1445                 return FALSE;
1446         }
1447         else return TRUE;
1448 }
1449
1450 /*!
1451  * @brief フロアに存在する全マスの記憶状態を初期化する / Wipe all unnecessary flags after cave generation
1452  * @return なし
1453  */
1454 void wipe_generate_cave_flags(void)
1455 {
1456         int x, y;
1457
1458         for (y = 0; y < cur_hgt; y++)
1459         {
1460                 for (x = 0; x < cur_wid; x++)
1461                 {
1462                         /* Wipe unused flags */
1463                         cave[y][x].info &= ~(CAVE_MASK);
1464                 }
1465         }
1466
1467         if (dun_level)
1468         {
1469                 for (y = 1; y < cur_hgt - 1; y++)
1470                 {
1471                         for (x = 1; x < cur_wid - 1; x++)
1472                         {
1473                                 /* There might be trap */
1474                                 cave[y][x].info |= CAVE_UNSAFE;
1475                         }
1476                 }
1477         }
1478 }
1479
1480 /*!
1481  * @brief フロアの全情報を初期化する / Clear and empty the cave
1482  * @return なし
1483  */
1484 void clear_cave(void)
1485 {
1486         int x, y, i;
1487
1488         /* Very simplified version of wipe_o_list() */
1489         (void)C_WIPE(o_list, o_max, object_type);
1490         o_max = 1;
1491         o_cnt = 0;
1492
1493         /* Very simplified version of wipe_m_list() */
1494         for (i = 1; i < max_r_idx; i++)
1495                 r_info[i].cur_num = 0;
1496         (void)C_WIPE(m_list, m_max, monster_type);
1497         m_max = 1;
1498         m_cnt = 0;
1499         for (i = 0; i < MAX_MTIMED; i++) mproc_max[i] = 0;
1500
1501         /* Pre-calc cur_num of pets in party_mon[] */
1502         precalc_cur_num_of_pet();
1503
1504
1505         /* Start with a blank cave */
1506         for (y = 0; y < MAX_HGT; y++)
1507         {
1508                 for (x = 0; x < MAX_WID; x++)
1509                 {
1510                         cave_type *c_ptr = &cave[y][x];
1511
1512                         /* No flags */
1513                         c_ptr->info = 0;
1514
1515                         /* No features */
1516                         c_ptr->feat = 0;
1517
1518                         /* No objects */
1519                         c_ptr->o_idx = 0;
1520
1521                         /* No monsters */
1522                         c_ptr->m_idx = 0;
1523
1524                         /* No special */
1525                         c_ptr->special = 0;
1526
1527                         /* No mimic */
1528                         c_ptr->mimic = 0;
1529
1530                         /* No flow */
1531                         c_ptr->cost = 0;
1532                         c_ptr->dist = 0;
1533                         c_ptr->when = 0;
1534                 }
1535         }
1536
1537         /* Mega-Hack -- no player yet */
1538         p_ptr->x = p_ptr->y = 0;
1539
1540         /* Set the base level */
1541         base_level = dun_level;
1542
1543         /* Reset the monster generation level */
1544         monster_level = base_level;
1545
1546         /* Reset the object generation level */
1547         object_level = base_level;
1548 }
1549
1550
1551 /*!
1552  * ダンジョンのランダムフロアを生成する / Generates a random dungeon level -RAK-
1553  * @return なし
1554  * @note Hack -- regenerate any "overflow" levels
1555  */
1556 void generate_cave(void)
1557 {
1558         int num;
1559
1560         /* Fill the arrays of floors and walls in the good proportions */
1561         set_floor_and_wall(dungeon_type);
1562
1563         /* Generate */
1564         for (num = 0; TRUE; num++)
1565         {
1566                 bool okay = TRUE;
1567
1568                 cptr why = NULL;
1569
1570                 /* Clear and empty the cave */
1571                 clear_cave();
1572
1573                 /* Build the arena -KMW- */
1574                 if (p_ptr->inside_arena)
1575                 {
1576                         /* Small arena */
1577                         arena_gen();
1578                 }
1579
1580                 /* Build the battle -KMW- */
1581                 else if (p_ptr->inside_battle)
1582                 {
1583                         /* Small arena */
1584                         battle_gen();
1585                 }
1586
1587                 else if (p_ptr->inside_quest)
1588                 {
1589                         quest_gen();
1590                 }
1591
1592                 /* Build the town */
1593                 else if (!dun_level)
1594                 {
1595                         /* Make the wilderness */
1596                         if (p_ptr->wild_mode) wilderness_gen_small();
1597                         else wilderness_gen();
1598                 }
1599
1600                 /* Build a real level */
1601                 else
1602                 {
1603                         okay = level_gen(&why);
1604                 }
1605
1606
1607                 /* Prevent object over-flow */
1608                 if (o_max >= max_o_idx)
1609                 {
1610                         /* Message */
1611 #ifdef JP
1612 why = "アイテムが多すぎる";
1613 #else
1614                         why = "too many objects";
1615 #endif
1616
1617
1618                         /* Message */
1619                         okay = FALSE;
1620                 }
1621                 /* Prevent monster over-flow */
1622                 else if (m_max >= max_m_idx)
1623                 {
1624                         /* Message */
1625 #ifdef JP
1626 why = "モンスターが多すぎる";
1627 #else
1628                         why = "too many monsters";
1629 #endif
1630
1631
1632                         /* Message */
1633                         okay = FALSE;
1634                 }
1635
1636                 /* Accept */
1637                 if (okay) break;
1638
1639                 /* Message */
1640 #ifdef JP
1641 if (why) msg_format("生成やり直し(%s)", why);
1642 #else
1643                 if (why) msg_format("Generation restarted (%s)", why);
1644 #endif
1645
1646
1647                 /* Wipe the objects */
1648                 wipe_o_list();
1649
1650                 /* Wipe the monsters */
1651                 wipe_m_list();
1652         }
1653
1654         /* Glow deep lava and building entrances */
1655         glow_deep_lava_and_bldg();
1656
1657         /* Reset flag */
1658         p_ptr->enter_dungeon = FALSE;
1659
1660         wipe_generate_cave_flags();
1661 }