OSDN Git Service

076a19d854cec13d1a80cc4fc8b47919f72fa24d
[hengband/hengband.git] / src / grid.c
1 /*!
2  * @file grid.c
3  * @brief ダンジョンの生成処理の基幹部分 / low-level dungeon creation primitives
4  * @date 2014/01/04
5  * @author
6  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7  *\n
8  * This software may be copied and distributed for educational, research,\n
9  * and not for profit purposes provided that this copyright and statement\n
10  * are included in all such copies.  Other copyrights may also apply.\n
11  * \n
12  * 2014 Deskull Doxygen向けのコメント整理\n
13  */
14
15 #include "angband.h"
16 #include "generate.h"
17 #include "grid.h"
18 #include "trap.h"
19
20
21 /*!
22  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
23  * @return 配置に成功したらTRUEを返す
24  */
25 bool new_player_spot(void)
26 {
27         POSITION y = 0, x = 0;
28         int max_attempts = 10000;
29
30         cave_type *c_ptr;
31         feature_type *f_ptr;
32
33         /* Place the player */
34         while (max_attempts--)
35         {
36                 /* Pick a legal spot */
37                 y = (POSITION)rand_range(1, cur_hgt - 2);
38                 x = (POSITION)rand_range(1, cur_wid - 2);
39
40                 c_ptr = &cave[y][x];
41
42                 /* Must be a "naked" floor grid */
43                 if (c_ptr->m_idx) continue;
44                 if (dun_level)
45                 {
46                         f_ptr = &f_info[c_ptr->feat];
47
48                         if (max_attempts > 5000) /* Rule 1 */
49                         {
50                                 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
51                         }
52                         else /* Rule 2 */
53                         {
54                                 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
55                                 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
56                         }
57
58                         /* Refuse to start on anti-teleport grids in dungeon */
59                         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
60                 }
61                 if (!player_can_enter(c_ptr->feat, 0)) continue;
62                 if (!in_bounds(y, x)) continue;
63
64                 /* Refuse to start on anti-teleport grids */
65                 if (c_ptr->info & (CAVE_ICKY)) continue;
66
67                 /* Done */
68                 break;
69         }
70
71         if (max_attempts < 1) /* Should be -1, actually if we failed... */
72                 return FALSE;
73
74         /* Save the new player grid */
75         p_ptr->y = y;
76         p_ptr->x = x;
77
78         return TRUE;
79 }
80
81
82
83 /*!
84  * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
85  * @param y 配置を試みたいマスのY座標
86  * @param x 配置を試みたいマスのX座標
87  * @return なし
88  */
89 void place_random_stairs(POSITION y, POSITION x)
90 {
91         bool up_stairs = TRUE;
92         bool down_stairs = TRUE;
93         cave_type *c_ptr;
94
95         /* Paranoia */
96         c_ptr = &cave[y][x];
97         if (!is_floor_grid(c_ptr) || c_ptr->o_idx) return;
98
99         /* Town */
100         if (!dun_level)
101                 up_stairs = FALSE;
102
103         /* Ironman */
104         if (ironman_downward)
105                 up_stairs = FALSE;
106
107         /* Bottom */
108         if (dun_level >= d_info[dungeon_type].maxdepth)
109                 down_stairs = FALSE;
110
111         /* Quest-level */
112         if (quest_number(dun_level) && (dun_level > 1))
113                 down_stairs = FALSE;
114
115         /* We can't place both */
116         if (down_stairs && up_stairs)
117         {
118                 /* Choose a staircase randomly */
119                 if (randint0(100) < 50)
120                         up_stairs = FALSE;
121                 else
122                         down_stairs = FALSE;
123         }
124
125         /* Place the stairs */
126         if (up_stairs)
127                 place_up_stairs(y, x);
128         else if (down_stairs)
129                 place_down_stairs(y, x);
130 }
131
132 /*!
133  * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
134  * @param y ドアの配置を試みたいマスのY座標
135  * @param x ドアの配置を試みたいマスのX座標
136  * @param room 部屋に接している場合向けのドア生成か否か
137  * @return なし
138  */
139 void place_random_door(POSITION y, POSITION x, bool room)
140 {
141         int tmp, type;
142         s16b feat = feat_none;
143         cave_type *c_ptr = &cave[y][x];
144
145         /* Initialize mimic info */
146         c_ptr->mimic = 0;
147
148         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
149         {
150                 place_floor_bold(y, x);
151                 return;
152         }
153
154         type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
155                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
156                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
157
158         /* Choose an object */
159         tmp = randint0(1000);
160
161         /* Open doors (300/1000) */
162         if (tmp < 300)
163         {
164                 /* Create open door */
165                 feat = feat_door[type].open;
166         }
167
168         /* Broken doors (100/1000) */
169         else if (tmp < 400)
170         {
171                 /* Create broken door */
172                 feat = feat_door[type].broken;
173         }
174
175         /* Secret doors (200/1000) */
176         else if (tmp < 600)
177         {
178                 /* Create secret door */
179                 place_closed_door(y, x, type);
180
181                 if (type != DOOR_CURTAIN)
182                 {
183                         /* Hide. If on the edge of room, use outer wall. */
184                         c_ptr->mimic = room ? feat_wall_outer : fill_type[randint0(100)];
185
186                         /* Floor type terrain cannot hide a door */
187                         if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
188                         {
189                                 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
190                                 {
191                                         c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
192                                 }
193                                 c_ptr->mimic = 0;
194                         }
195                 }
196         }
197
198         /* Closed, locked, or stuck doors (400/1000) */
199         else place_closed_door(y, x, type);
200
201         if (tmp < 400)
202         {
203                 if (feat != feat_none)
204                 {
205                         set_cave_feat(y, x, feat);
206                 }
207                 else
208                 {
209                         place_floor_bold(y, x);
210                 }
211         }
212
213         delete_monster(y, x);
214 }
215
216 /*!
217  * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
218  * @param y ドアの配置を試みたいマスのY座標
219  * @param x ドアの配置を試みたいマスのX座標
220  * @param type ドアの地形ID
221  * @return なし
222  */
223 void place_closed_door(POSITION y, POSITION x, int type)
224 {
225         int tmp;
226         s16b feat = feat_none;
227
228         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
229         {
230                 place_floor_bold(y, x);
231                 return;
232         }
233
234         /* Choose an object */
235         tmp = randint0(400);
236
237         /* Closed doors (300/400) */
238         if (tmp < 300)
239         {
240                 /* Create closed door */
241                 feat = feat_door[type].closed;
242         }
243
244         /* Locked doors (99/400) */
245         else if (tmp < 399)
246         {
247                 /* Create locked door */
248                 feat = feat_locked_door_random(type);
249         }
250
251         /* Stuck doors (1/400) */
252         else
253         {
254                 /* Create jammed door */
255                 feat = feat_jammed_door_random(type);
256         }
257
258         if (feat != feat_none)
259         {
260                 cave_set_feat(y, x, feat);
261
262                 /* Now it is not floor */
263                 cave[y][x].info &= ~(CAVE_MASK);
264         }
265         else
266         {
267                 place_floor_bold(y, x);
268         }
269 }
270
271 /*!
272 * @brief 鍵のかかったドアを配置する
273 * @param y 配置したいフロアのY座標
274 * @param x 配置したいフロアのX座標
275 * @return なし
276 */
277 void place_locked_door(POSITION y, POSITION x)
278 {
279         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
280         {
281                 place_floor_bold(y, x);
282         }
283         else
284         {
285                 set_cave_feat(y, x, feat_locked_door_random((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
286                 cave[y][x].info &= ~(CAVE_FLOOR);
287                 delete_monster(y, x);
288         }
289 }
290
291
292 /*!
293 * @brief 隠しドアを配置する
294 * @param y 配置したいフロアのY座標
295 * @param x 配置したいフロアのX座標
296 * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
297 * @return なし
298 */
299 void place_secret_door(POSITION y, POSITION x, int type)
300 {
301         if (d_info[dungeon_type].flags1 & DF1_NO_DOORS)
302         {
303                 place_floor_bold(y, x);
304         }
305         else
306         {
307                 cave_type *c_ptr = &cave[y][x];
308
309                 if (type == DOOR_DEFAULT)
310                 {
311                         type = ((d_info[dungeon_type].flags1 & DF1_CURTAIN) &&
312                                 one_in_((d_info[dungeon_type].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
313                                 ((d_info[dungeon_type].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
314                 }
315
316                 /* Create secret door */
317                 place_closed_door(y, x, type);
318
319                 if (type != DOOR_CURTAIN)
320                 {
321                         /* Hide by inner wall because this is used in rooms only */
322                         c_ptr->mimic = feat_wall_inner;
323
324                         /* Floor type terrain cannot hide a door */
325                         if (feat_supports_los(c_ptr->mimic) && !feat_supports_los(c_ptr->feat))
326                         {
327                                 if (have_flag(f_info[c_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[c_ptr->mimic].flags, FF_CAN_FLY))
328                                 {
329                                         c_ptr->feat = one_in_(2) ? c_ptr->mimic : floor_type[randint0(100)];
330                                 }
331                                 c_ptr->mimic = 0;
332                         }
333                 }
334
335                 c_ptr->info &= ~(CAVE_FLOOR);
336                 delete_monster(y, x);
337         }
338 }
339
340 /*
341  * Routine used by the random vault creators to add a door to a location
342  * Note that range checking has to be done in the calling routine.
343  *
344  * The doors must be INSIDE the allocated region.
345  */
346 void add_door(POSITION x, POSITION y)
347 {
348         /* Need to have a wall in the center square */
349         if (!is_outer_bold(y, x)) return;
350
351         /* look at:
352         *  x#x
353         *  .#.
354         *  x#x
355         *
356         *  where x=don't care
357         *  .=floor, #=wall
358         */
359
360         if (is_floor_bold(y - 1, x) && is_floor_bold(y + 1, x) &&
361                 (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
362         {
363                 /* secret door */
364                 place_secret_door(y, x, DOOR_DEFAULT);
365
366                 /* set boundarys so don't get wide doors */
367                 place_solid_bold(y, x - 1);
368                 place_solid_bold(y, x + 1);
369         }
370
371
372         /* look at:
373         *  x#x
374         *  .#.
375         *  x#x
376         *
377         *  where x = don't care
378         *  .=floor, #=wall
379         */
380         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
381                 is_floor_bold(y, x - 1) && is_floor_bold(y, x + 1))
382         {
383                 /* secret door */
384                 place_secret_door(y, x, DOOR_DEFAULT);
385
386                 /* set boundarys so don't get wide doors */
387                 place_solid_bold(y - 1, x);
388                 place_solid_bold(y + 1, x);
389         }
390 }
391
392 /*!
393 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
394 * @param y1 基準となるマスのY座標
395 * @param x1 基準となるマスのX座標
396 * @return 通路の数
397 * @note Assumes "in_bounds(y1, x1)"
398 * @details
399 * XXX XXX This routine currently only counts actual "empty floor"\n
400 * grids which are not in rooms.  We might want to also count stairs,\n
401 * open doors, closed doors, etc.
402 */
403 static int next_to_corr(int y1, int x1)
404 {
405         int i, y, x, k = 0;
406
407         cave_type *c_ptr;
408
409         /* Scan adjacent grids */
410         for (i = 0; i < 4; i++)
411         {
412                 /* Extract the location */
413                 y = y1 + ddy_ddd[i];
414                 x = x1 + ddx_ddd[i];
415
416                 /* Access the grid */
417                 c_ptr = &cave[y][x];
418
419                 /* Skip non floors */
420                 if (cave_have_flag_grid(c_ptr, FF_WALL)) continue;
421
422                 /* Skip non "empty floor" grids */
423                 if (!is_floor_grid(c_ptr))
424                         continue;
425
426                 /* Skip grids inside rooms */
427                 if (c_ptr->info & (CAVE_ROOM)) continue;
428
429                 /* Count these grids */
430                 k++;
431         }
432
433         /* Return the number of corridors */
434         return (k);
435 }
436
437 /*!
438 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
439 * @param y 判定を行いたいマスのY座標
440 * @param x 判定を行いたいマスのX座標
441 * @return ドアを設置可能ならばTRUEを返す
442 * @note Assumes "in_bounds(y1, x1)"
443 * @details
444 * XXX XXX XXX\n
445 * Assumes "in_bounds(y, x)"\n
446 */
447 static bool possible_doorway(int y, int x)
448 {
449         /* Count the adjacent corridors */
450         if (next_to_corr(y, x) >= 2)
451         {
452                 /* Check Vertical */
453                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
454                         cave_have_flag_bold(y + 1, x, FF_WALL))
455                 {
456                         return (TRUE);
457                 }
458
459                 /* Check Horizontal */
460                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
461                         cave_have_flag_bold(y, x + 1, FF_WALL))
462                 {
463                         return (TRUE);
464                 }
465         }
466
467         /* No doorway */
468         return (FALSE);
469 }
470
471 /*!
472 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
473 * @param y 設置を行いたいマスのY座標
474 * @param x 設置を行いたいマスのX座標
475 * @return なし
476 */
477 void try_door(POSITION y, POSITION x)
478 {
479         /* Paranoia */
480         if (!in_bounds(y, x)) return;
481
482         /* Ignore walls */
483         if (cave_have_flag_bold(y, x, FF_WALL)) return;
484
485         /* Ignore room grids */
486         if (cave[y][x].info & (CAVE_ROOM)) return;
487
488         /* Occasional door (if allowed) */
489         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[dungeon_type].flags1 & DF1_NO_DOORS))
490         {
491                 /* Place a door */
492                 place_random_door(y, x, FALSE);
493         }
494 }
495
496
497 /*!
498  * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
499  * @param x1 長方形の左端X座標(-1)
500  * @param x2 長方形の右端X座標(+1)
501  * @param y1 長方形の上端Y座標(-1)
502  * @param y2 長方形の下端Y座標(+1)
503  * @param light 照明の有無
504  * @return なし
505  */
506 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
507 {
508         int x, y;
509
510         /* Place a full floor under the room */
511         for (y = y1 - 1; y <= y2 + 1; y++)
512         {
513                 for (x = x1 - 1; x <= x2 + 1; x++)
514                 {
515                         place_floor_bold(y, x);
516                         add_cave_info(y, x, CAVE_ROOM);
517                         if (light) add_cave_info(y, x, CAVE_GLOW);
518                 }
519         }
520 }
521
522
523 /*!
524  * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
525  * @param x1 長方形の左端X座標(-1)
526  * @param x2 長方形の右端X座標(+1)
527  * @param y1 長方形の上端Y座標(-1)
528  * @param y2 長方形の下端Y座標(+1)
529  * @param light 照明の有無
530  * @return なし
531  */
532 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
533 {
534         int y, x;
535
536         place_floor(x1, x2, y1, y2, light);
537
538         /* Walls around the room */
539         for (y = y1 - 1; y <= y2 + 1; y++)
540         {
541                 place_outer_bold(y, x1 - 1);
542                 place_outer_bold(y, x2 + 1);
543         }
544         for (x = x1 - 1; x <= x2 + 1; x++)
545         {
546                 place_outer_bold(y1 - 1, x);
547                 place_outer_bold(y2 + 1, x);
548         }
549 }
550
551
552 /*!
553  * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
554  * @param y 配置したい中心マスのY座標
555  * @param x 配置したい中心マスのX座標
556  * @param num 配置したい数
557  * @return なし
558  * @details
559  * Only really called by some of the "vault" routines.
560  */
561 void vault_objects(int y, int x, int num)
562 {
563         int dummy = 0;
564         int i = 0, j = y, k = x;
565
566         cave_type *c_ptr;
567
568
569         /* Attempt to place 'num' objects */
570         for (; num > 0; --num)
571         {
572                 /* Try up to 11 spots looking for empty space */
573                 for (i = 0; i < 11; ++i)
574                 {
575                         /* Pick a random location */
576                         while (dummy < SAFE_MAX_ATTEMPTS)
577                         {
578                                 j = rand_spread(y, 2);
579                                 k = rand_spread(x, 3);
580                                 dummy++;
581                                 if (!in_bounds(j, k)) continue;
582                                 break;
583                         }
584
585
586                         if (dummy >= SAFE_MAX_ATTEMPTS)
587                         {
588                                 if (cheat_room)
589                                 {
590 #ifdef JP
591 msg_print("警告!地下室のアイテムを配置できません!");
592 #else
593                                         msg_print("Warning! Could not place vault object!");
594 #endif
595
596                                 }
597                         }
598
599
600                         /* Require "clean" floor space */
601                         c_ptr = &cave[j][k];
602                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
603
604                         /* Place an item */
605                         if (randint0(100) < 75)
606                         {
607                                 place_object(j, k, 0L);
608                         }
609
610                         /* Place gold */
611                         else
612                         {
613                                 place_gold(j, k);
614                         }
615
616                         /* Placement accomplished */
617                         break;
618                 }
619         }
620 }
621
622 /*!
623  * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
624  * @param y トラップを配置したいマスの中心Y座標
625  * @param x トラップを配置したいマスの中心X座標
626  * @param yd Y方向の配置分散マス数
627  * @param xd X方向の配置分散マス数
628  * @return なし
629  * @details
630  * Only really called by some of the "vault" routines.
631  */
632 void vault_trap_aux(int y, int x, int yd, int xd)
633 {
634         int count = 0, y1 = y, x1 = x;
635         int dummy = 0;
636
637         cave_type *c_ptr;
638
639         /* Place traps */
640         for (count = 0; count <= 5; count++)
641         {
642                 /* Get a location */
643                 while (dummy < SAFE_MAX_ATTEMPTS)
644                 {
645                         y1 = rand_spread(y, yd);
646                         x1 = rand_spread(x, xd);
647                         dummy++;
648                         if (!in_bounds(y1, x1)) continue;
649                         break;
650                 }
651
652                 if (dummy >= SAFE_MAX_ATTEMPTS)
653                 {
654                         if (cheat_room)
655                         {
656 #ifdef JP
657 msg_print("警告!地下室のトラップを配置できません!");
658 #else
659                                 msg_print("Warning! Could not place vault trap!");
660 #endif
661
662                         }
663                 }
664
665                 /* Require "naked" floor grids */
666                 c_ptr = &cave[y1][x1];
667                 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
668
669                 /* Place the trap */
670                 place_trap(y1, x1);
671
672                 /* Done */
673                 break;
674         }
675 }
676
677 /*!
678  * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
679  * @param y トラップを配置したいマスの中心Y座標
680  * @param x トラップを配置したいマスの中心X座標
681  * @param yd Y方向の配置分散マス数
682  * @param xd X方向の配置分散マス数
683  * @param num 配置したいトラップの数
684  * @return なし
685  * @details
686  * Only really called by some of the "vault" routines.
687  */
688 void vault_traps(int y, int x, int yd, int xd, int num)
689 {
690         int i;
691
692         for (i = 0; i < num; i++)
693         {
694                 vault_trap_aux(y, x, yd, xd);
695         }
696 }
697
698 /*!
699  * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
700  * @param y1 モンスターを配置したいマスの中心Y座標
701  * @param x1 モンスターを配置したいマスの中心X座標
702  * @param num 配置したいモンスターの数
703  * @return なし
704  * @details
705  * Only really called by some of the "vault" routines.
706  */
707 void vault_monsters(int y1, int x1, int num)
708 {
709         int k, i;
710         POSITION y, x;
711         cave_type *c_ptr;
712
713         /* Try to summon "num" monsters "near" the given location */
714         for (k = 0; k < num; k++)
715         {
716                 /* Try nine locations */
717                 for (i = 0; i < 9; i++)
718                 {
719                         int d = 1;
720
721                         /* Pick a nearby location */
722                         scatter(&y, &x, y1, x1, d, 0);
723
724                         /* Require "empty" floor grids */
725                         c_ptr = &cave[y][x];
726                         if (!cave_empty_grid(c_ptr)) continue;
727
728                         /* Place the monster (allow groups) */
729                         monster_level = base_level + 2;
730                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
731                         monster_level = base_level;
732                 }
733         }
734 }
735
736
737 /*!
738  * @brief build_tunnel用に通路を掘るための方向を位置関係通りに決める / Always picks a correct direction
739  * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
740  * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
741  * @param y1 始点Y座標
742  * @param x1 始点X座標
743  * @param y2 終点Y座標
744  * @param x2 終点X座標
745  * @return なし
746  */
747 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
748 {
749         /* Extract vertical and horizontal directions */
750         *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
751         *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
752
753         /* Never move diagonally */
754         if (*rdir && *cdir)
755         {
756                 if (randint0(100) < 50)
757                         *rdir = 0;
758                 else
759                         *cdir = 0;
760         }
761 }
762
763 /*!
764  * @brief build_tunnel用に通路を掘るための方向をランダムに決める / Pick a random direction
765  * @param rdir Y方向に取るべきベクトル値を返す参照ポインタ
766  * @param cdir X方向に取るべきベクトル値を返す参照ポインタ
767  * @return なし
768  */
769 void rand_dir(int *rdir, int *cdir)
770 {
771         /* Pick a random direction */
772         int i = randint0(4);
773
774         /* Extract the dy/dx components */
775         *rdir = ddy_ddd[i];
776         *cdir = ddx_ddd[i];
777 }
778
779 /*!
780  * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
781  * @param x チェックするマスのX座標
782  * @param y チェックするマスのY座標
783  * @return 床系地形ならばTRUE
784  */
785 bool get_is_floor(int x, int y)
786 {
787         if (!in_bounds(y, x))
788         {
789                 /* Out of bounds */
790                 return (FALSE);
791         }
792
793         /* Do the real check */
794         if (is_floor_bold(y, x)) return (TRUE);
795
796         return (FALSE);
797 }
798
799 /*!
800  * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
801  * @param x 地形を変えたいマスのX座標
802  * @param y 地形を変えたいマスのY座標
803  * @return なし
804  */
805 void set_floor(int x, int y)
806 {
807         if (!in_bounds(y, x))
808         {
809                 /* Out of bounds */
810                 return;
811         }
812
813         if (cave[y][x].info & CAVE_ROOM)
814         {
815                 /* A room border don't touch. */
816                 return;
817         }
818
819         /* Set to be floor if is a wall (don't touch lakes). */
820         if (is_extra_bold(y, x))
821                 place_floor_bold(y, x);
822 }
823
824
825 /*!
826  * @brief 部屋間のトンネルを生成する / Constructs a tunnel between two points
827  * @param row1 始点Y座標
828  * @param col1 始点X座標
829  * @param row2 終点Y座標
830  * @param col2 終点X座標
831  * @return 生成に成功したらTRUEを返す
832  * @details
833  * This function must be called BEFORE any streamers are created,\n
834  * since we use the special "granite wall" sub-types to keep track\n
835  * of legal places for corridors to pierce rooms.\n
836  *\n
837  * We use "door_flag" to prevent excessive construction of doors\n
838  * along overlapping corridors.\n
839  *\n
840  * We queue the tunnel grids to prevent door creation along a corridor\n
841  * which intersects itself.\n
842  *\n
843  * We queue the wall piercing grids to prevent a corridor from leaving\n
844  * a room and then coming back in through the same entrance.\n
845  *\n
846  * We "pierce" grids which are "outer" walls of rooms, and when we\n
847  * do so, we change all adjacent "outer" walls of rooms into "solid"\n
848  * walls so that no two corridors may use adjacent grids for exits.\n
849  *\n
850  * The "solid" wall check prevents corridors from "chopping" the\n
851  * corners of rooms off, as well as "silly" door placement, and\n
852  * "excessively wide" room entrances.\n
853  *\n
854  * Kind of walls:\n
855  *   extra -- walls\n
856  *   inner -- inner room walls\n
857  *   outer -- outer room walls\n
858  *   solid -- solid room walls\n
859  */
860 bool build_tunnel(POSITION row1, POSITION col1, POSITION row2, POSITION col2)
861 {
862         int y, x;
863         POSITION tmp_row, tmp_col;
864         int row_dir, col_dir;
865         int start_row, start_col;
866         int main_loop_count = 0;
867
868         bool door_flag = FALSE;
869
870         cave_type *c_ptr;
871
872         /* Save the starting location */
873         start_row = row1;
874         start_col = col1;
875
876         /* Start out in the correct direction */
877         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
878
879         /* Keep going until done (or bored) */
880         while ((row1 != row2) || (col1 != col2))
881         {
882                 /* Mega-Hack -- Paranoia -- prevent infinite loops */
883                 if (main_loop_count++ > 2000) return FALSE;
884
885                 /* Allow bends in the tunnel */
886                 if (randint0(100) < dun_tun_chg)
887                 {
888                         /* Acquire the correct direction */
889                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
890
891                         /* Random direction */
892                         if (randint0(100) < dun_tun_rnd)
893                         {
894                                 rand_dir(&row_dir, &col_dir);
895                         }
896                 }
897
898                 /* Get the next location */
899                 tmp_row = row1 + row_dir;
900                 tmp_col = col1 + col_dir;
901
902
903                 /* Extremely Important -- do not leave the dungeon */
904                 while (!in_bounds(tmp_row, tmp_col))
905                 {
906                         /* Acquire the correct direction */
907                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
908
909                         /* Random direction */
910                         if (randint0(100) < dun_tun_rnd)
911                         {
912                                 rand_dir(&row_dir, &col_dir);
913                         }
914
915                         /* Get the next location */
916                         tmp_row = row1 + row_dir;
917                         tmp_col = col1 + col_dir;
918                 }
919
920
921                 /* Access the location */
922                 c_ptr = &cave[tmp_row][tmp_col];
923
924                 /* Avoid "solid" walls */
925                 if (is_solid_grid(c_ptr)) continue;
926
927                 /* Pierce "outer" walls of rooms */
928                 if (is_outer_grid(c_ptr))
929                 {
930                         /* Acquire the "next" location */
931                         y = tmp_row + row_dir;
932                         x = tmp_col + col_dir;
933
934                         /* Hack -- Avoid outer/solid walls */
935                         if (is_outer_bold(y, x)) continue;
936                         if (is_solid_bold(y, x)) continue;
937
938                         /* Accept this location */
939                         row1 = (POSITION)tmp_row;
940                         col1 = (POSITION)tmp_col;
941
942                         /* Save the wall location */
943                         if (dun->wall_n < WALL_MAX)
944                         {
945                                 dun->wall[dun->wall_n].y = row1;
946                                 dun->wall[dun->wall_n].x = col1;
947                                 dun->wall_n++;
948                         }
949                         else return FALSE;
950
951                         /* Forbid re-entry near this piercing */
952                         for (y = row1 - 1; y <= row1 + 1; y++)
953                         {
954                                 for (x = col1 - 1; x <= col1 + 1; x++)
955                                 {
956                                         /* Convert adjacent "outer" walls as "solid" walls */
957                                         if (is_outer_bold(y, x))
958                                         {
959                                                 /* Change the wall to a "solid" wall */
960                                                 place_solid_noperm_bold(y, x);
961                                         }
962                                 }
963                         }
964                 }
965
966                 /* Travel quickly through rooms */
967                 else if (c_ptr->info & (CAVE_ROOM))
968                 {
969                         /* Accept the location */
970                         row1 = tmp_row;
971                         col1 = tmp_col;
972                 }
973
974                 /* Tunnel through all other walls */
975                 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
976                 {
977                         /* Accept this location */
978                         row1 = tmp_row;
979                         col1 = tmp_col;
980
981                         /* Save the tunnel location */
982                         if (dun->tunn_n < TUNN_MAX)
983                         {
984                                 dun->tunn[dun->tunn_n].y = row1;
985                                 dun->tunn[dun->tunn_n].x = col1;
986                                 dun->tunn_n++;
987                         }
988                         else return FALSE;
989
990                         /* Allow door in next grid */
991                         door_flag = FALSE;
992                 }
993
994                 /* Handle corridor intersections or overlaps */
995                 else
996                 {
997                         /* Accept the location */
998                         row1 = tmp_row;
999                         col1 = tmp_col;
1000
1001                         /* Collect legal door locations */
1002                         if (!door_flag)
1003                         {
1004                                 /* Save the door location */
1005                                 if (dun->door_n < DOOR_MAX)
1006                                 {
1007                                         dun->door[dun->door_n].y = row1;
1008                                         dun->door[dun->door_n].x = col1;
1009                                         dun->door_n++;
1010                                 }
1011                                 else return FALSE;
1012
1013                                 /* No door in next grid */
1014                                 door_flag = TRUE;
1015                         }
1016
1017                         /* Hack -- allow pre-emptive tunnel termination */
1018                         if (randint0(100) >= dun_tun_con)
1019                         {
1020                                 /* Distance between row1 and start_row */
1021                                 tmp_row = row1 - start_row;
1022                                 if (tmp_row < 0) tmp_row = (-tmp_row);
1023
1024                                 /* Distance between col1 and start_col */
1025                                 tmp_col = col1 - start_col;
1026                                 if (tmp_col < 0) tmp_col = (-tmp_col);
1027
1028                                 /* Terminate the tunnel */
1029                                 if ((tmp_row > 10) || (tmp_col > 10)) break;
1030                         }
1031                 }
1032         }
1033
1034         return TRUE;
1035 }
1036
1037
1038 /*!
1039  * @brief トンネル生成のための基準点を指定する。
1040  * @param x 基準点を指定するX座標の参照ポインタ、適時値が修正される。
1041  * @param y 基準点を指定するY座標の参照ポインタ、適時値が修正される。
1042  * @param affectwall (調査中)
1043  * @return なし
1044  * @details
1045  * This routine adds the square to the tunnel\n
1046  * It also checks for SOLID walls - and returns a nearby\n
1047  * non-SOLID square in (x,y) so that a simple avoiding\n
1048  * routine can be used. The returned boolean value reflects\n
1049  * whether or not this routine hit a SOLID wall.\n
1050  *\n
1051  * "affectwall" toggles whether or not this new square affects\n
1052  * the boundaries of rooms. - This is used by the catacomb\n
1053  * routine.\n
1054  * @todo 特に詳細な処理の意味を調査すべし
1055  */
1056 static bool set_tunnel(POSITION *x, POSITION *y, bool affectwall)
1057 {
1058         int i, j, dx, dy;
1059
1060         cave_type *c_ptr = &cave[*y][*x];
1061
1062         if (!in_bounds(*y, *x)) return TRUE;
1063
1064         if (is_inner_grid(c_ptr))
1065         {
1066                 return TRUE;
1067         }
1068
1069         if (is_extra_bold(*y,*x))
1070         {
1071                 /* Save the tunnel location */
1072                 if (dun->tunn_n < TUNN_MAX)
1073                 {
1074                         dun->tunn[dun->tunn_n].y = *y;
1075                         dun->tunn[dun->tunn_n].x = *x;
1076                         dun->tunn_n++;
1077
1078                         return TRUE;
1079                 }
1080                 else return FALSE;
1081         }
1082
1083         if (is_floor_bold(*y, *x))
1084         {
1085                 /* Don't do anything */
1086                 return TRUE;
1087         }
1088
1089         if (is_outer_grid(c_ptr) && affectwall)
1090         {
1091                 /* Save the wall location */
1092                 if (dun->wall_n < WALL_MAX)
1093                 {
1094                         dun->wall[dun->wall_n].y = *y;
1095                         dun->wall[dun->wall_n].x = *x;
1096                         dun->wall_n++;
1097                 }
1098                 else return FALSE;
1099
1100                 /* Forbid re-entry near this piercing */
1101                 for (j = *y - 1; j <= *y + 1; j++)
1102                 {
1103                         for (i = *x - 1; i <= *x + 1; i++)
1104                         {
1105                                 /* Convert adjacent "outer" walls as "solid" walls */
1106                                 if (is_outer_bold(j, i))
1107                                 {
1108                                         /* Change the wall to a "solid" wall */
1109                                         place_solid_noperm_bold(j, i);
1110                                 }
1111                         }
1112                 }
1113
1114                 /* Clear mimic type */
1115                 cave[*y][*x].mimic = 0;
1116
1117                 place_floor_bold(*y, *x);
1118
1119                 return TRUE;
1120         }
1121
1122         if (is_solid_grid(c_ptr) && affectwall)
1123         {
1124                 /* cannot place tunnel here - use a square to the side */
1125
1126                 /* find usable square and return value in (x,y) */
1127
1128                 i = 50;
1129
1130                 dy = 0;
1131                 dx = 0;
1132                 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
1133                 {
1134                         dy = randint0(3) - 1;
1135                         dx = randint0(3) - 1;
1136
1137                         if (!in_bounds(*y + dy, *x + dx))
1138                         {
1139                                 dx = 0;
1140                                 dy = 0;
1141                         }
1142
1143                         i--;
1144                 }
1145
1146                 if (i == 0)
1147                 {
1148                         /* Failed for some reason: hack - ignore the solidness */
1149                         place_outer_grid(c_ptr);
1150                         dx = 0;
1151                         dy = 0;
1152                 }
1153
1154                 /* Give new, acceptable coordinate. */
1155                 *x = *x + dx;
1156                 *y = *y + dy;
1157
1158                 return FALSE;
1159         }
1160
1161         return TRUE;
1162 }
1163
1164
1165 /*!
1166  * @brief 外壁を削って「カタコンベ状」の通路を作成する / This routine creates the catacomb-like tunnels by removing extra rock.
1167  * @param x 基準点のX座標
1168  * @param y 基準点のY座標
1169  * @return なし
1170  * @details
1171  * Note that this routine is only called on "even" squares - so it gives
1172  * a natural checkerboard pattern.
1173  */
1174 static void create_cata_tunnel(POSITION x, POSITION y)
1175 {
1176         POSITION x1, y1;
1177
1178         /* Build tunnel */
1179         x1 = x - 1;
1180         y1 = y;
1181         set_tunnel(&x1, &y1, FALSE);
1182
1183         x1 = x + 1;
1184         y1 = y;
1185         set_tunnel(&x1, &y1, FALSE);
1186
1187         x1 = x;
1188         y1 = y - 1;
1189         set_tunnel(&x1, &y1, FALSE);
1190
1191         x1 = x;
1192         y1 = y + 1;
1193         set_tunnel(&x1, &y1, FALSE);
1194 }
1195
1196
1197 /*!
1198  * @brief トンネル生成処理(詳細調査中)/ This routine does the bulk of the work in creating the new types of tunnels.
1199  * @return なし
1200  * @todo 詳細用調査
1201  * @details
1202  * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)\n
1203  * It doesn't need to add any complexity - straight lines are fine.\n
1204  * The SOLID walls are avoided by a recursive algorithm which tries random ways\n
1205  * around the obstical until it works.  The number of itterations is counted, and it\n
1206  * this gets too large the routine exits. This should stop any crashes - but may leave\n
1207  * small gaps in the tunnel where there are too many SOLID walls.\n
1208  *\n
1209  * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used\n
1210  * as a part of the dodge SOLID walls algorithm.\n
1211  *\n
1212  * Type 2 tunnels are made of two straight lines at right angles. When this is used with\n
1213  * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.\n
1214  *\n
1215  * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.\n
1216  * This, when used with longer line segments gives the "catacomb-like" tunnels seen near\n
1217  * the surface.\n
1218  */
1219 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
1220 {
1221         int i;
1222         POSITION x, y;
1223         int length;
1224
1225         /* Check for early exit */
1226         if (!(*fail)) return;
1227
1228         length = distance(x1, y1, x2, y2);
1229
1230         count++;
1231
1232         if ((type == 1) && (length != 0))
1233         {
1234
1235                 for (i = 0; i <= length; i++)
1236                 {
1237                         x = x1 + i * (x2 - x1) / length;
1238                         y = y1 + i * (y2 - y1) / length;
1239                         if (!set_tunnel(&x, &y, TRUE))
1240                         {
1241                                 if (count > 50)
1242                                 {
1243                                         /* This isn't working - probably have an infinite loop */
1244                                         *fail = FALSE;
1245                                         return;
1246                                 }
1247
1248                                 /* solid wall - so try to go around */
1249                                 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
1250                                 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
1251                         }
1252                 }
1253         }
1254         else if ((type == 2) || (type == 3))
1255         {
1256                 if (x1 < x2)
1257                 {
1258                         for (i = x1; i <= x2; i++)
1259                         {
1260                                 x = i;
1261                                 y = y1;
1262                                 if (!set_tunnel(&x, &y, TRUE))
1263                                 {
1264                                         /* solid wall - so try to go around */
1265                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1266                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1267                                 }
1268                                 if ((type == 3) && ((x + y) % 2))
1269                                 {
1270                                         create_cata_tunnel(i, y1);
1271                                 }
1272                         }
1273                 }
1274                 else
1275                 {
1276                         for (i = x2; i <= x1; i++)
1277                         {
1278                                 x = i;
1279                                 y = y1;
1280                                 if (!set_tunnel(&x, &y, TRUE))
1281                                 {
1282                                         /* solid wall - so try to go around */
1283                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1284                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1285                                 }
1286                                 if ((type == 3) && ((x + y) % 2))
1287                                 {
1288                                         create_cata_tunnel(i, y1);
1289                                 }
1290                         }
1291
1292                 }
1293                 if (y1 < y2)
1294                 {
1295                         for (i = y1; i <= y2; i++)
1296                         {
1297                                 x = x2;
1298                                 y = i;
1299                                 if (!set_tunnel(&x, &y, TRUE))
1300                                 {
1301                                         /* solid wall - so try to go around */
1302                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1303                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1304                                 }
1305                                 if ((type == 3) && ((x + y) % 2))
1306                                 {
1307                                         create_cata_tunnel(x2, i);
1308                                 }
1309                         }
1310                 }
1311                 else
1312                 {
1313                         for (i = y2; i <= y1; i++)
1314                         {
1315                                 x = x2;
1316                                 y = i;
1317                                 if (!set_tunnel(&x, &y, TRUE))
1318                                 {
1319                                         /* solid wall - so try to go around */
1320                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1321                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1322                                 }
1323                                 if ((type == 3) && ((x + y) % 2))
1324                                 {
1325                                         create_cata_tunnel(x2, i);
1326                                 }
1327                         }
1328                 }
1329         }
1330 }
1331
1332
1333 /*!
1334  * @brief 特定の壁(永久壁など)を避けながら部屋間の通路を作成する / This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1335  * @return なし
1336  * @todo 詳細要調査
1337  * @details
1338  * Permanent rock is ignored in this path finding- sometimes there is no\n
1339  * path around anyway -so there will be a crash if we try to find one.\n
1340  * This routine is much like the river creation routine in Zangband.\n
1341  * It works by dividing a line segment into two.  The segments are divided\n
1342  * until they are less than "cutoff" - when the corresponding routine from\n
1343  * "short_seg_hack" is called.\n
1344  * Note it is VERY important that the "stop if hit another passage" logic\n
1345  * stays as is.  Without this the dungeon turns into Swiss Cheese...\n
1346  */
1347 bool build_tunnel2(POSITION x1, POSITION y1, POSITION x2, POSITION y2, int type, int cutoff)
1348 {
1349         POSITION x3, y3, dx, dy;
1350         int changex, changey;
1351         int length;
1352         int i;
1353         bool retval, firstsuccede;
1354         cave_type *c_ptr;
1355
1356         length = distance(x1, y1, x2, y2);
1357
1358         if (length > cutoff)
1359         {
1360                 /*
1361                 * Divide path in half and call routine twice.
1362                  */
1363                 dx = (x2 - x1) / 2;
1364                 dy = (y2 - y1) / 2;
1365
1366                 /* perturbation perpendicular to path */
1367                 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1368                 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1369
1370                 /* Work out "mid" ponit */
1371                 x3 = x1 + dx + changex;
1372                 y3 = y1 + dy + changey;
1373
1374                 /* See if in bounds - if not - do not perturb point */
1375                 if (!in_bounds(y3, x3))
1376                 {
1377                         x3 = (x1 + x2) / 2;
1378                         y3 = (y1 + y2) / 2;
1379                 }
1380                 /* cache c_ptr */
1381                 c_ptr = &cave[y3][x3];
1382                 if (is_solid_grid(c_ptr))
1383                 {
1384                         /* move midpoint a bit to avoid problem. */
1385
1386                         i = 50;
1387
1388                         dy = 0;
1389                         dx = 0;
1390                         while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1391                         {
1392                                 dy = randint0(3) - 1;
1393                                 dx = randint0(3) - 1;
1394                                 if (!in_bounds(y3 + dy, x3 + dx))
1395                                 {
1396                                         dx = 0;
1397                                         dy = 0;
1398                                 }
1399                                 i--;
1400                         }
1401
1402                         if (i == 0)
1403                         {
1404                                 /* Failed for some reason: hack - ignore the solidness */
1405                                 place_outer_bold(y3, x3);
1406                                 dx = 0;
1407                                 dy = 0;
1408                         }
1409                         y3 += dy;
1410                         x3 += dx;
1411                         c_ptr = &cave[y3][x3];
1412                 }
1413
1414                 if (is_floor_grid(c_ptr))
1415                 {
1416                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1417                         {
1418                                 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1419                                 {
1420                                         /* do second half only if works + if have hit a room */
1421                                         retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1422                                 }
1423                                 else
1424                                 {
1425                                         /* have hit another tunnel - make a set of doors here */
1426                                         retval = FALSE;
1427
1428                                         /* Save the door location */
1429                                         if (dun->door_n < DOOR_MAX)
1430                                         {
1431                                                 dun->door[dun->door_n].y = (POSITION)y3;
1432                                                 dun->door[dun->door_n].x = (POSITION)x3;
1433                                                 dun->door_n++;
1434                                         }
1435                                         else return FALSE;
1436                                 }
1437                                 firstsuccede = TRUE;
1438                         }
1439                         else
1440                         {
1441                                 /* false- didn't work all the way */
1442                                 retval = FALSE;
1443                                 firstsuccede = FALSE;
1444                         }
1445                 }
1446                 else
1447                 {
1448                         /* tunnel through walls */
1449                         if (build_tunnel2(x1, y1, (POSITION)x3, (POSITION)y3, type, cutoff))
1450                         {
1451                                 retval = build_tunnel2((POSITION)x3, (POSITION)y3, x2, y2, type, cutoff);
1452                                 firstsuccede = TRUE;
1453                         }
1454                         else
1455                         {
1456                                 /* false- didn't work all the way */
1457                                 retval = FALSE;
1458                                 firstsuccede = FALSE;
1459                         }
1460                 }
1461                 if (firstsuccede)
1462                 {
1463                         /* only do this if the first half has worked */
1464                         set_tunnel(&x3, &y3, TRUE);
1465                 }
1466                 /* return value calculated above */
1467                 return retval;
1468         }
1469         else
1470         {
1471                 /* Do a short segment */
1472                 retval = TRUE;
1473                 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1474
1475                 /* Hack - ignore return value so avoid infinite loops */
1476                 return TRUE;
1477         }
1478 }
1479