OSDN Git Service

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