OSDN Git Service

Add Doxygen comment to monster1.c.
[hengband/hengband.git] / src / tmp / 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         int     y, x;
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 = rand_range(1, cur_hgt - 2);
37                 x = 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         py = y;
75         px = 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 Ä¹Êý·Á¤Î¶õƶ¤òÀ¸À®¤¹¤ë / Make an empty square floor, for the middle of rooms
272  * @param x1 Ä¹Êý·Á¤Îº¸Ã¼XºÂɸ(-1)
273  * @param x2 Ä¹Êý·Á¤Î±¦Ã¼XºÂɸ(+1)
274  * @param y1 Ä¹Êý·Á¤Î¾åüYºÂɸ(-1)
275  * @param y2 Ä¹Êý·Á¤Î²¼Ã¼YºÂɸ(+1)
276  * @param light ¾ÈÌÀ¤Î̵ͭ
277  * @return ¤Ê¤·
278  */
279 void place_floor(int x1, int x2, int y1, int y2, bool light)
280 {
281         int x, y;
282
283         /* Place a full floor under the room */
284         for (y = y1 - 1; y <= y2 + 1; y++)
285         {
286                 for (x = x1 - 1; x <= x2 + 1; x++)
287                 {
288                         place_floor_bold(y, x);
289                         add_cave_info(y, x, CAVE_ROOM);
290                         if (light) add_cave_info(y, x, CAVE_GLOW);
291                 }
292         }
293 }
294
295
296 /*!
297  * @brief Ä¹Êý·Á¤ÎÉô²°¤òÀ¸À®¤¹¤ë / Make an empty square room, only floor and wall grids
298  * @param x1 Ä¹Êý·Á¤Îº¸Ã¼XºÂɸ(-1)
299  * @param x2 Ä¹Êý·Á¤Î±¦Ã¼XºÂɸ(+1)
300  * @param y1 Ä¹Êý·Á¤Î¾åüYºÂɸ(-1)
301  * @param y2 Ä¹Êý·Á¤Î²¼Ã¼YºÂɸ(+1)
302  * @param light ¾ÈÌÀ¤Î̵ͭ
303  * @return ¤Ê¤·
304  */
305 void place_room(int x1, int x2, int y1, int y2, bool light)
306 {
307         int y, x;
308
309         place_floor(x1, x2, y1, y2, light);
310
311         /* Walls around the room */
312         for (y = y1 - 1; y <= y2 + 1; y++)
313         {
314                 place_outer_bold(y, x1 - 1);
315                 place_outer_bold(y, x2 + 1);
316         }
317         for (x = x1 - 1; x <= x2 + 1; x++)
318         {
319                 place_outer_bold(y1 - 1, x);
320                 place_outer_bold(y2 + 1, x);
321         }
322 }
323
324
325 /*!
326  * @brief Æüì¤ÊÉô²°¸þ¤±¤Ë³Æ¼ï¥¢¥¤¥Æ¥à¤òÇÛÃÖ¤¹¤ë / Create up to "num" objects near the given coordinates
327  * @param y ÇÛÃÖ¤·¤¿¤¤Ãæ¿´¥Þ¥¹¤ÎYºÂɸ
328  * @param x ÇÛÃÖ¤·¤¿¤¤Ãæ¿´¥Þ¥¹¤ÎXºÂɸ
329  * @param num ÇÛÃÖ¤·¤¿¤¤¿ô
330  * @return ¤Ê¤·
331  * @details
332  * Only really called by some of the "vault" routines.
333  */
334 void vault_objects(int y, int x, int num)
335 {
336         int dummy = 0;
337         int i = 0, j = y, k = x;
338
339         cave_type *c_ptr;
340
341
342         /* Attempt to place 'num' objects */
343         for (; num > 0; --num)
344         {
345                 /* Try up to 11 spots looking for empty space */
346                 for (i = 0; i < 11; ++i)
347                 {
348                         /* Pick a random location */
349                         while (dummy < SAFE_MAX_ATTEMPTS)
350                         {
351                                 j = rand_spread(y, 2);
352                                 k = rand_spread(x, 3);
353                                 dummy++;
354                                 if (!in_bounds(j, k)) continue;
355                                 break;
356                         }
357
358
359                         if (dummy >= SAFE_MAX_ATTEMPTS)
360                         {
361                                 if (cheat_room)
362                                 {
363 #ifdef JP
364 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥¢¥¤¥Æ¥à¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
365 #else
366                                         msg_print("Warning! Could not place vault object!");
367 #endif
368
369                                 }
370                         }
371
372
373                         /* Require "clean" floor space */
374                         c_ptr = &cave[j][k];
375                         if (!is_floor_grid(c_ptr) || c_ptr->o_idx) continue;
376
377                         /* Place an item */
378                         if (randint0(100) < 75)
379                         {
380                                 place_object(j, k, 0L);
381                         }
382
383                         /* Place gold */
384                         else
385                         {
386                                 place_gold(j, k);
387                         }
388
389                         /* Placement accomplished */
390                         break;
391                 }
392         }
393 }
394
395 /*!
396  * @brief Æüì¤ÊÉô²°¸þ¤±¤Ë³Æ¼ï¥¢¥¤¥Æ¥à¤òÇÛÃÖ¤¹¤ë(vault_trap¤Î¥µ¥Ö¥»¥Ã¥È) / Place a trap with a given displacement of point
397  * @param y ¥È¥é¥Ã¥×¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´YºÂɸ
398  * @param x ¥È¥é¥Ã¥×¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´XºÂɸ
399  * @param yd YÊý¸þ¤ÎÇÛÃÖʬ»¶¥Þ¥¹¿ô
400  * @param xd XÊý¸þ¤ÎÇÛÃÖʬ»¶¥Þ¥¹¿ô
401  * @return ¤Ê¤·
402  * @details
403  * Only really called by some of the "vault" routines.
404  */
405 void vault_trap_aux(int y, int x, int yd, int xd)
406 {
407         int count = 0, y1 = y, x1 = x;
408         int dummy = 0;
409
410         cave_type *c_ptr;
411
412         /* Place traps */
413         for (count = 0; count <= 5; count++)
414         {
415                 /* Get a location */
416                 while (dummy < SAFE_MAX_ATTEMPTS)
417                 {
418                         y1 = rand_spread(y, yd);
419                         x1 = rand_spread(x, xd);
420                         dummy++;
421                         if (!in_bounds(y1, x1)) continue;
422                         break;
423                 }
424
425                 if (dummy >= SAFE_MAX_ATTEMPTS)
426                 {
427                         if (cheat_room)
428                         {
429 #ifdef JP
430 msg_print("·Ù¹ð¡ªÃϲ¼¼¼¤Î¥È¥é¥Ã¥×¤òÇÛÃ֤Ǥ­¤Þ¤»¤ó¡ª");
431 #else
432                                 msg_print("Warning! Could not place vault trap!");
433 #endif
434
435                         }
436                 }
437
438                 /* Require "naked" floor grids */
439                 c_ptr = &cave[y1][x1];
440                 if (!is_floor_grid(c_ptr) || c_ptr->o_idx || c_ptr->m_idx) continue;
441
442                 /* Place the trap */
443                 place_trap(y1, x1);
444
445                 /* Done */
446                 break;
447         }
448 }
449
450 /*!
451  * @brief Æüì¤ÊÉô²°¸þ¤±¤Ë³Æ¼ï¥¢¥¤¥Æ¥à¤òÇÛÃÖ¤¹¤ë(¥á¥¤¥ó¥ë¡¼¥Á¥ó) / Place some traps with a given displacement of given location
452  * @param y ¥È¥é¥Ã¥×¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´YºÂɸ
453  * @param x ¥È¥é¥Ã¥×¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´XºÂɸ
454  * @param yd YÊý¸þ¤ÎÇÛÃÖʬ»¶¥Þ¥¹¿ô
455  * @param xd XÊý¸þ¤ÎÇÛÃÖʬ»¶¥Þ¥¹¿ô
456  * @param num ÇÛÃÖ¤·¤¿¤¤¥È¥é¥Ã¥×¤Î¿ô
457  * @return ¤Ê¤·
458  * @details
459  * Only really called by some of the "vault" routines.
460  */
461 void vault_traps(int y, int x, int yd, int xd, int num)
462 {
463         int i;
464
465         for (i = 0; i < num; i++)
466         {
467                 vault_trap_aux(y, x, yd, xd);
468         }
469 }
470
471 /*!
472  * @brief Æüì¤ÊÉô²°ÃÏ·Á¸þ¤±¤Ë¥â¥ó¥¹¥¿¡¼¤òÇÛÃÖ¤¹¤ë / Hack -- Place some sleeping monsters near the given location
473  * @param y1 ¥â¥ó¥¹¥¿¡¼¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´YºÂɸ
474  * @param x1 ¥â¥ó¥¹¥¿¡¼¤òÇÛÃÖ¤·¤¿¤¤¥Þ¥¹¤ÎÃæ¿´XºÂɸ
475  * @param num ÇÛÃÖ¤·¤¿¤¤¥â¥ó¥¹¥¿¡¼¤Î¿ô
476  * @return ¤Ê¤·
477  * @details
478  * Only really called by some of the "vault" routines.
479  */
480 void vault_monsters(int y1, int x1, int num)
481 {
482         int k, i, y, x;
483         cave_type *c_ptr;
484
485         /* Try to summon "num" monsters "near" the given location */
486         for (k = 0; k < num; k++)
487         {
488                 /* Try nine locations */
489                 for (i = 0; i < 9; i++)
490                 {
491                         int d = 1;
492
493                         /* Pick a nearby location */
494                         scatter(&y, &x, y1, x1, d, 0);
495
496                         /* Require "empty" floor grids */
497                         c_ptr = &cave[y][x];
498                         if (!cave_empty_grid(c_ptr)) continue;
499
500                         /* Place the monster (allow groups) */
501                         monster_level = base_level + 2;
502                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
503                         monster_level = base_level;
504                 }
505         }
506 }
507
508
509 /*!
510  * @brief build_tunnelÍѤËÄÌÏ©¤ò·¡¤ë¤¿¤á¤ÎÊý¸þ¤ò°ÌÃÖ´Ø·¸Ä̤ê¤Ë·è¤á¤ë / Always picks a correct direction
511  * @param rdir YÊý¸þ¤Ë¼è¤ë¤Ù¤­¥Ù¥¯¥È¥ëÃͤòÊÖ¤¹»²¾È¥Ý¥¤¥ó¥¿
512  * @param cdir XÊý¸þ¤Ë¼è¤ë¤Ù¤­¥Ù¥¯¥È¥ëÃͤòÊÖ¤¹»²¾È¥Ý¥¤¥ó¥¿
513  * @param y1 »ÏÅÀYºÂɸ
514  * @param x1 »ÏÅÀXºÂɸ
515  * @param y2 ½ªÅÀYºÂɸ
516  * @param x2 ½ªÅÀXºÂɸ
517  * @return ¤Ê¤·
518  */
519 void correct_dir(int *rdir, int *cdir, int y1, int x1, int y2, int x2)
520 {
521         /* Extract vertical and horizontal directions */
522         *rdir = (y1 == y2) ? 0 : (y1 < y2) ? 1 : -1;
523         *cdir = (x1 == x2) ? 0 : (x1 < x2) ? 1 : -1;
524
525         /* Never move diagonally */
526         if (*rdir && *cdir)
527         {
528                 if (randint0(100) < 50)
529                         *rdir = 0;
530                 else
531                         *cdir = 0;
532         }
533 }
534
535 /*!
536  * @brief build_tunnelÍѤËÄÌÏ©¤ò·¡¤ë¤¿¤á¤ÎÊý¸þ¤ò¥é¥ó¥À¥à¤Ë·è¤á¤ë / Pick a random direction
537  * @param rdir YÊý¸þ¤Ë¼è¤ë¤Ù¤­¥Ù¥¯¥È¥ëÃͤòÊÖ¤¹»²¾È¥Ý¥¤¥ó¥¿
538  * @param cdir XÊý¸þ¤Ë¼è¤ë¤Ù¤­¥Ù¥¯¥È¥ëÃͤòÊÖ¤¹»²¾È¥Ý¥¤¥ó¥¿
539  * @return ¤Ê¤·
540  */
541 void rand_dir(int *rdir, int *cdir)
542 {
543         /* Pick a random direction */
544         int i = randint0(4);
545
546         /* Extract the dy/dx components */
547         *rdir = ddy_ddd[i];
548         *cdir = ddx_ddd[i];
549 }
550
551 /*!
552  * @brief »ØÄê¤Î¥Þ¥¹¤¬¾²·ÏÃÏ·Á¤Ç¤¢¤ë¤«¤òÊÖ¤¹ / Function that sees if a square is a floor.  (Includes range checking.)
553  * @param x ¥Á¥§¥Ã¥¯¤¹¤ë¥Þ¥¹¤ÎXºÂɸ
554  * @param y ¥Á¥§¥Ã¥¯¤¹¤ë¥Þ¥¹¤ÎYºÂɸ
555  * @return ¾²·ÏÃÏ·Á¤Ê¤é¤ÐTRUE
556  */
557 bool get_is_floor(int x, int y)
558 {
559         if (!in_bounds(y, x))
560         {
561                 /* Out of bounds */
562                 return (FALSE);
563         }
564
565         /* Do the real check */
566         if (is_floor_bold(y, x)) return (TRUE);
567
568         return (FALSE);
569 }
570
571 /*!
572  * @brief »ØÄê¤Î¥Þ¥¹¤ò¾²ÃÏ·Á¤ËÊѤ¨¤ë / Set a square to be floor.  (Includes range checking.)
573  * @param x ÃÏ·Á¤òÊѤ¨¤¿¤¤¥Þ¥¹¤ÎXºÂɸ
574  * @param y ÃÏ·Á¤òÊѤ¨¤¿¤¤¥Þ¥¹¤ÎYºÂɸ
575  * @return ¤Ê¤·
576  */
577 void set_floor(int x, int y)
578 {
579         if (!in_bounds(y, x))
580         {
581                 /* Out of bounds */
582                 return;
583         }
584
585         if (cave[y][x].info & CAVE_ROOM)
586         {
587                 /* A room border don't touch. */
588                 return;
589         }
590
591         /* Set to be floor if is a wall (don't touch lakes). */
592         if (is_extra_bold(y, x))
593                 place_floor_bold(y, x);
594 }
595
596
597 /*!
598  * @brief Éô²°´Ö¤Î¥È¥ó¥Í¥ë¤òÀ¸À®¤¹¤ë / Constructs a tunnel between two points
599  * @param row1 »ÏÅÀYºÂɸ
600  * @param col1 »ÏÅÀXºÂɸ
601  * @param row2 ½ªÅÀYºÂɸ
602  * @param col2 ½ªÅÀXºÂɸ
603  * @return À¸À®¤ËÀ®¸ù¤·¤¿¤éTRUE¤òÊÖ¤¹
604  * @details
605  * This function must be called BEFORE any streamers are created,\n
606  * since we use the special "granite wall" sub-types to keep track\n
607  * of legal places for corridors to pierce rooms.\n
608  *\n
609  * We use "door_flag" to prevent excessive construction of doors\n
610  * along overlapping corridors.\n
611  *\n
612  * We queue the tunnel grids to prevent door creation along a corridor\n
613  * which intersects itself.\n
614  *\n
615  * We queue the wall piercing grids to prevent a corridor from leaving\n
616  * a room and then coming back in through the same entrance.\n
617  *\n
618  * We "pierce" grids which are "outer" walls of rooms, and when we\n
619  * do so, we change all adjacent "outer" walls of rooms into "solid"\n
620  * walls so that no two corridors may use adjacent grids for exits.\n
621  *\n
622  * The "solid" wall check prevents corridors from "chopping" the\n
623  * corners of rooms off, as well as "silly" door placement, and\n
624  * "excessively wide" room entrances.\n
625  *\n
626  * Kind of walls:\n
627  *   extra -- walls\n
628  *   inner -- inner room walls\n
629  *   outer -- outer room walls\n
630  *   solid -- solid room walls\n
631  */
632 bool build_tunnel(int row1, int col1, int row2, int col2)
633 {
634         int y, x;
635         int tmp_row, tmp_col;
636         int row_dir, col_dir;
637         int start_row, start_col;
638         int main_loop_count = 0;
639
640         bool door_flag = FALSE;
641
642         cave_type *c_ptr;
643
644         /* Save the starting location */
645         start_row = row1;
646         start_col = col1;
647
648         /* Start out in the correct direction */
649         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
650
651         /* Keep going until done (or bored) */
652         while ((row1 != row2) || (col1 != col2))
653         {
654                 /* Mega-Hack -- Paranoia -- prevent infinite loops */
655                 if (main_loop_count++ > 2000) return FALSE;
656
657                 /* Allow bends in the tunnel */
658                 if (randint0(100) < dun_tun_chg)
659                 {
660                         /* Acquire the correct direction */
661                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
662
663                         /* Random direction */
664                         if (randint0(100) < dun_tun_rnd)
665                         {
666                                 rand_dir(&row_dir, &col_dir);
667                         }
668                 }
669
670                 /* Get the next location */
671                 tmp_row = row1 + row_dir;
672                 tmp_col = col1 + col_dir;
673
674
675                 /* Extremely Important -- do not leave the dungeon */
676                 while (!in_bounds(tmp_row, tmp_col))
677                 {
678                         /* Acquire the correct direction */
679                         correct_dir(&row_dir, &col_dir, row1, col1, row2, col2);
680
681                         /* Random direction */
682                         if (randint0(100) < dun_tun_rnd)
683                         {
684                                 rand_dir(&row_dir, &col_dir);
685                         }
686
687                         /* Get the next location */
688                         tmp_row = row1 + row_dir;
689                         tmp_col = col1 + col_dir;
690                 }
691
692
693                 /* Access the location */
694                 c_ptr = &cave[tmp_row][tmp_col];
695
696                 /* Avoid "solid" walls */
697                 if (is_solid_grid(c_ptr)) continue;
698
699                 /* Pierce "outer" walls of rooms */
700                 if (is_outer_grid(c_ptr))
701                 {
702                         /* Acquire the "next" location */
703                         y = tmp_row + row_dir;
704                         x = tmp_col + col_dir;
705
706                         /* Hack -- Avoid outer/solid walls */
707                         if (is_outer_bold(y, x)) continue;
708                         if (is_solid_bold(y, x)) continue;
709
710                         /* Accept this location */
711                         row1 = tmp_row;
712                         col1 = tmp_col;
713
714                         /* Save the wall location */
715                         if (dun->wall_n < WALL_MAX)
716                         {
717                                 dun->wall[dun->wall_n].y = row1;
718                                 dun->wall[dun->wall_n].x = col1;
719                                 dun->wall_n++;
720                         }
721                         else return FALSE;
722
723                         /* Forbid re-entry near this piercing */
724                         for (y = row1 - 1; y <= row1 + 1; y++)
725                         {
726                                 for (x = col1 - 1; x <= col1 + 1; x++)
727                                 {
728                                         /* Convert adjacent "outer" walls as "solid" walls */
729                                         if (is_outer_bold(y, x))
730                                         {
731                                                 /* Change the wall to a "solid" wall */
732                                                 place_solid_noperm_bold(y, x);
733                                         }
734                                 }
735                         }
736                 }
737
738                 /* Travel quickly through rooms */
739                 else if (c_ptr->info & (CAVE_ROOM))
740                 {
741                         /* Accept the location */
742                         row1 = tmp_row;
743                         col1 = tmp_col;
744                 }
745
746                 /* Tunnel through all other walls */
747                 else if (is_extra_grid(c_ptr) || is_inner_grid(c_ptr) || is_solid_grid(c_ptr))
748                 {
749                         /* Accept this location */
750                         row1 = tmp_row;
751                         col1 = tmp_col;
752
753                         /* Save the tunnel location */
754                         if (dun->tunn_n < TUNN_MAX)
755                         {
756                                 dun->tunn[dun->tunn_n].y = row1;
757                                 dun->tunn[dun->tunn_n].x = col1;
758                                 dun->tunn_n++;
759                         }
760                         else return FALSE;
761
762                         /* Allow door in next grid */
763                         door_flag = FALSE;
764                 }
765
766                 /* Handle corridor intersections or overlaps */
767                 else
768                 {
769                         /* Accept the location */
770                         row1 = tmp_row;
771                         col1 = tmp_col;
772
773                         /* Collect legal door locations */
774                         if (!door_flag)
775                         {
776                                 /* Save the door location */
777                                 if (dun->door_n < DOOR_MAX)
778                                 {
779                                         dun->door[dun->door_n].y = row1;
780                                         dun->door[dun->door_n].x = col1;
781                                         dun->door_n++;
782                                 }
783                                 else return FALSE;
784
785                                 /* No door in next grid */
786                                 door_flag = TRUE;
787                         }
788
789                         /* Hack -- allow pre-emptive tunnel termination */
790                         if (randint0(100) >= dun_tun_con)
791                         {
792                                 /* Distance between row1 and start_row */
793                                 tmp_row = row1 - start_row;
794                                 if (tmp_row < 0) tmp_row = (-tmp_row);
795
796                                 /* Distance between col1 and start_col */
797                                 tmp_col = col1 - start_col;
798                                 if (tmp_col < 0) tmp_col = (-tmp_col);
799
800                                 /* Terminate the tunnel */
801                                 if ((tmp_row > 10) || (tmp_col > 10)) break;
802                         }
803                 }
804         }
805
806         return TRUE;
807 }
808
809
810 /*!
811  * @brief ¥È¥ó¥Í¥ëÀ¸À®¤Î¤¿¤á¤Î´ð½àÅÀ¤ò»ØÄꤹ¤ë¡£
812  * @param x ´ð½àÅÀ¤ò»ØÄꤹ¤ëXºÂɸ¤Î»²¾È¥Ý¥¤¥ó¥¿¡¢Å¬»þÃͤ¬½¤Àµ¤µ¤ì¤ë¡£
813  * @param y ´ð½àÅÀ¤ò»ØÄꤹ¤ëYºÂɸ¤Î»²¾È¥Ý¥¤¥ó¥¿¡¢Å¬»þÃͤ¬½¤Àµ¤µ¤ì¤ë¡£
814  * @param affectwall (Ä´ººÃæ)
815  * @return ¤Ê¤·
816  * @details
817  * This routine adds the square to the tunnel\n
818  * It also checks for SOLID walls - and returns a nearby\n
819  * non-SOLID square in (x,y) so that a simple avoiding\n
820  * routine can be used. The returned boolean value reflects\n
821  * whether or not this routine hit a SOLID wall.\n
822  *\n
823  * "affectwall" toggles whether or not this new square affects\n
824  * the boundaries of rooms. - This is used by the catacomb\n
825  * routine.\n
826  * @todo Æä˾ܺ٤ʽèÍý¤Î°ÕÌ£¤òÄ´ºº¤¹¤Ù¤·
827  */
828 static bool set_tunnel(int *x, int *y, bool affectwall)
829 {
830         int i, j, dx, dy;
831
832         cave_type *c_ptr = &cave[*y][*x];
833
834         if (!in_bounds(*y, *x)) return TRUE;
835
836         if (is_inner_grid(c_ptr))
837         {
838                 return TRUE;
839         }
840
841         if (is_extra_bold(*y,*x))
842         {
843                 /* Save the tunnel location */
844                 if (dun->tunn_n < TUNN_MAX)
845                 {
846                         dun->tunn[dun->tunn_n].y = *y;
847                         dun->tunn[dun->tunn_n].x = *x;
848                         dun->tunn_n++;
849
850                         return TRUE;
851                 }
852                 else return FALSE;
853         }
854
855         if (is_floor_bold(*y, *x))
856         {
857                 /* Don't do anything */
858                 return TRUE;
859         }
860
861         if (is_outer_grid(c_ptr) && affectwall)
862         {
863                 /* Save the wall location */
864                 if (dun->wall_n < WALL_MAX)
865                 {
866                         dun->wall[dun->wall_n].y = *y;
867                         dun->wall[dun->wall_n].x = *x;
868                         dun->wall_n++;
869                 }
870                 else return FALSE;
871
872                 /* Forbid re-entry near this piercing */
873                 for (j = *y - 1; j <= *y + 1; j++)
874                 {
875                         for (i = *x - 1; i <= *x + 1; i++)
876                         {
877                                 /* Convert adjacent "outer" walls as "solid" walls */
878                                 if (is_outer_bold(j, i))
879                                 {
880                                         /* Change the wall to a "solid" wall */
881                                         place_solid_noperm_bold(j, i);
882                                 }
883                         }
884                 }
885
886                 /* Clear mimic type */
887                 cave[*y][*x].mimic = 0;
888
889                 place_floor_bold(*y, *x);
890
891                 return TRUE;
892         }
893
894         if (is_solid_grid(c_ptr) && affectwall)
895         {
896                 /* cannot place tunnel here - use a square to the side */
897
898                 /* find usable square and return value in (x,y) */
899
900                 i = 50;
901
902                 dy = 0;
903                 dx = 0;
904                 while ((i > 0) && is_solid_bold(*y + dy, *x + dx))
905                 {
906                         dy = randint0(3) - 1;
907                         dx = randint0(3) - 1;
908
909                         if (!in_bounds(*y + dy, *x + dx))
910                         {
911                                 dx = 0;
912                                 dy = 0;
913                         }
914
915                         i--;
916                 }
917
918                 if (i == 0)
919                 {
920                         /* Failed for some reason: hack - ignore the solidness */
921                         place_outer_grid(c_ptr);
922                         dx = 0;
923                         dy = 0;
924                 }
925
926                 /* Give new, acceptable coordinate. */
927                 *x = *x + dx;
928                 *y = *y + dy;
929
930                 return FALSE;
931         }
932
933         return TRUE;
934 }
935
936
937 /*!
938  * @brief ³°Êɤòºï¤Ã¤Æ¡Ö¥«¥¿¥³¥ó¥Ù¾õ¡×¤ÎÄÌÏ©¤òºîÀ®¤¹¤ë / This routine creates the catacomb-like tunnels by removing extra rock.
939  * @param x ´ð½àÅÀ¤ÎXºÂɸ
940  * @param y ´ð½àÅÀ¤ÎYºÂɸ
941  * @return ¤Ê¤·
942  * @details
943  * Note that this routine is only called on "even" squares - so it gives
944  * a natural checkerboard pattern.
945  */
946 static void create_cata_tunnel(int x, int y)
947 {
948         int x1, y1;
949
950         /* Build tunnel */
951         x1 = x - 1;
952         y1 = y;
953         set_tunnel(&x1, &y1, FALSE);
954
955         x1 = x + 1;
956         y1 = y;
957         set_tunnel(&x1, &y1, FALSE);
958
959         x1 = x;
960         y1 = y - 1;
961         set_tunnel(&x1, &y1, FALSE);
962
963         x1 = x;
964         y1 = y + 1;
965         set_tunnel(&x1, &y1, FALSE);
966 }
967
968
969 /*!
970  * @brief ¥È¥ó¥Í¥ëÀ¸À®½èÍý¡Ê¾ÜºÙÄ´ººÃæ¡Ë/ This routine does the bulk of the work in creating the new types of tunnels.
971  * @return ¤Ê¤·
972  * @todo ¾ÜºÙÍÑÄ´ºº
973  * @details
974  * It is designed to use very simple algorithms to go from (x1,y1) to (x2,y2)\n
975  * It doesn't need to add any complexity - straight lines are fine.\n
976  * The SOLID walls are avoided by a recursive algorithm which tries random ways\n
977  * around the obstical until it works.  The number of itterations is counted, and it\n
978  * this gets too large the routine exits. This should stop any crashes - but may leave\n
979  * small gaps in the tunnel where there are too many SOLID walls.\n
980  *\n
981  * Type 1 tunnels are extremely simple - straight line from A to B.  This is only used\n
982  * as a part of the dodge SOLID walls algorithm.\n
983  *\n
984  * Type 2 tunnels are made of two straight lines at right angles. When this is used with\n
985  * short line segments it gives the "cavelike" tunnels seen deeper in the dungeon.\n
986  *\n
987  * Type 3 tunnels are made of two straight lines like type 2, but with extra rock removed.\n
988  * This, when used with longer line segments gives the "catacomb-like" tunnels seen near\n
989  * the surface.\n
990  */
991 static void short_seg_hack(int x1, int y1, int x2, int y2, int type, int count, bool *fail)
992 {
993         int i, x, y;
994         int length;
995
996         /* Check for early exit */
997         if (!(*fail)) return;
998
999         length = distance(x1, y1, x2, y2);
1000
1001         count++;
1002
1003         if ((type == 1) && (length != 0))
1004         {
1005
1006                 for (i = 0; i <= length; i++)
1007                 {
1008                         x = x1 + i * (x2 - x1) / length;
1009                         y = y1 + i * (y2 - y1) / length;
1010                         if (!set_tunnel(&x, &y, TRUE))
1011                         {
1012                                 if (count > 50)
1013                                 {
1014                                         /* This isn't working - probably have an infinite loop */
1015                                         *fail = FALSE;
1016                                         return;
1017                                 }
1018
1019                                 /* solid wall - so try to go around */
1020                                 short_seg_hack(x, y, x1 + (i - 1) * (x2 - x1) / length, y1 + (i - 1) * (y2 - y1) / length, 1, count, fail);
1021                                 short_seg_hack(x, y, x1 + (i + 1) * (x2 - x1) / length, y1 + (i + 1) * (y2 - y1) / length, 1, count, fail);
1022                         }
1023                 }
1024         }
1025         else if ((type == 2) || (type == 3))
1026         {
1027                 if (x1 < x2)
1028                 {
1029                         for (i = x1; i <= x2; i++)
1030                         {
1031                                 x = i;
1032                                 y = y1;
1033                                 if (!set_tunnel(&x, &y, TRUE))
1034                                 {
1035                                         /* solid wall - so try to go around */
1036                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1037                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1038                                 }
1039                                 if ((type == 3) && ((x + y) % 2))
1040                                 {
1041                                         create_cata_tunnel(i, y1);
1042                                 }
1043                         }
1044                 }
1045                 else
1046                 {
1047                         for (i = x2; i <= x1; i++)
1048                         {
1049                                 x = i;
1050                                 y = y1;
1051                                 if (!set_tunnel(&x, &y, TRUE))
1052                                 {
1053                                         /* solid wall - so try to go around */
1054                                         short_seg_hack(x, y, i - 1, y1, 1, count, fail);
1055                                         short_seg_hack(x, y, i + 1, y1, 1, count, fail);
1056                                 }
1057                                 if ((type == 3) && ((x + y) % 2))
1058                                 {
1059                                         create_cata_tunnel(i, y1);
1060                                 }
1061                         }
1062
1063                 }
1064                 if (y1 < y2)
1065                 {
1066                         for (i = y1; i <= y2; i++)
1067                         {
1068                                 x = x2;
1069                                 y = i;
1070                                 if (!set_tunnel(&x, &y, TRUE))
1071                                 {
1072                                         /* solid wall - so try to go around */
1073                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1074                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1075                                 }
1076                                 if ((type == 3) && ((x + y) % 2))
1077                                 {
1078                                         create_cata_tunnel(x2, i);
1079                                 }
1080                         }
1081                 }
1082                 else
1083                 {
1084                         for (i = y2; i <= y1; i++)
1085                         {
1086                                 x = x2;
1087                                 y = i;
1088                                 if (!set_tunnel(&x, &y, TRUE))
1089                                 {
1090                                         /* solid wall - so try to go around */
1091                                         short_seg_hack(x, y, x2, i - 1, 1, count, fail);
1092                                         short_seg_hack(x, y, x2, i + 1, 1, count, fail);
1093                                 }
1094                                 if ((type == 3) && ((x + y) % 2))
1095                                 {
1096                                         create_cata_tunnel(x2, i);
1097                                 }
1098                         }
1099                 }
1100         }
1101 }
1102
1103
1104 /*!
1105  * @brief ÆÃÄê¤ÎÊÉ(±Êµ×ÊɤʤÉ)¤òÈò¤±¤Ê¤¬¤éÉô²°´Ö¤ÎÄÌÏ©¤òºîÀ®¤¹¤ë / This routine maps a path from (x1, y1) to (x2, y2) avoiding SOLID walls.
1106  * @return ¤Ê¤·
1107  * @todo ¾ÜºÙÍÑÄ´ºº
1108  * @details
1109  * Permanent rock is ignored in this path finding- sometimes there is no\n
1110  * path around anyway -so there will be a crash if we try to find one.\n
1111  * This routine is much like the river creation routine in Zangband.\n
1112  * It works by dividing a line segment into two.  The segments are divided\n
1113  * until they are less than "cutoff" - when the corresponding routine from\n
1114  * "short_seg_hack" is called.\n
1115  * Note it is VERY important that the "stop if hit another passage" logic\n
1116  * stays as is.  Without this the dungeon turns into Swiss Cheese...\n
1117  */
1118 bool build_tunnel2(int x1, int y1, int x2, int y2, int type, int cutoff)
1119 {
1120         int x3, y3, dx, dy;
1121         int changex, changey;
1122         int length;
1123         int i;
1124         bool retval, firstsuccede;
1125         cave_type *c_ptr;
1126
1127         length = distance(x1, y1, x2, y2);
1128
1129         if (length > cutoff)
1130         {
1131                 /*
1132                 * Divide path in half and call routine twice.
1133                  */
1134                 dx = (x2 - x1) / 2;
1135                 dy = (y2 - y1) / 2;
1136
1137                 /* perturbation perpendicular to path */
1138                 changex = (randint0(abs(dy) + 2) * 2 - abs(dy) - 1) / 2;
1139
1140                 /* perturbation perpendicular to path */
1141                 changey = (randint0(abs(dx) + 2) * 2 - abs(dx) - 1) / 2;
1142
1143                 /* Work out "mid" ponit */
1144                 x3 = x1 + dx + changex;
1145                 y3 = y1 + dy + changey;
1146
1147                 /* See if in bounds - if not - do not perturb point */
1148                 if (!in_bounds(y3, x3))
1149                 {
1150                         x3 = (x1 + x2) / 2;
1151                         y3 = (y1 + y2) / 2;
1152                 }
1153                 /* cache c_ptr */
1154                 c_ptr = &cave[y3][x3];
1155                 if (is_solid_grid(c_ptr))
1156                 {
1157                         /* move midpoint a bit to avoid problem. */
1158
1159                         i = 50;
1160
1161                         dy = 0;
1162                         dx = 0;
1163                         while ((i > 0) && is_solid_bold(y3 + dy, x3 + dx))
1164                         {
1165                                 dy = randint0(3) - 1;
1166                                 dx = randint0(3) - 1;
1167                                 if (!in_bounds(y3 + dy, x3 + dx))
1168                                 {
1169                                         dx = 0;
1170                                         dy = 0;
1171                                 }
1172                                 i--;
1173                         }
1174
1175                         if (i == 0)
1176                         {
1177                                 /* Failed for some reason: hack - ignore the solidness */
1178                                 place_outer_bold(y3, x3);
1179                                 dx = 0;
1180                                 dy = 0;
1181                         }
1182                         y3 += dy;
1183                         x3 += dx;
1184                         c_ptr = &cave[y3][x3];
1185                 }
1186
1187                 if (is_floor_grid(c_ptr))
1188                 {
1189                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1190                         {
1191                                 if ((cave[y3][x3].info & CAVE_ROOM) || (randint1(100) > 95))
1192                                 {
1193                                         /* do second half only if works + if have hit a room */
1194                                         retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1195                                 }
1196                                 else
1197                                 {
1198                                         /* have hit another tunnel - make a set of doors here */
1199                                         retval = FALSE;
1200
1201                                         /* Save the door location */
1202                                         if (dun->door_n < DOOR_MAX)
1203                                         {
1204                                                 dun->door[dun->door_n].y = y3;
1205                                                 dun->door[dun->door_n].x = x3;
1206                                                 dun->door_n++;
1207                                         }
1208                                         else return FALSE;
1209                                 }
1210                                 firstsuccede = TRUE;
1211                         }
1212                         else
1213                         {
1214                                 /* false- didn't work all the way */
1215                                 retval = FALSE;
1216                                 firstsuccede = FALSE;
1217                         }
1218                 }
1219                 else
1220                 {
1221                         /* tunnel through walls */
1222                         if (build_tunnel2(x1, y1, x3, y3, type, cutoff))
1223                         {
1224                                 retval = build_tunnel2(x3, y3, x2, y2, type, cutoff);
1225                                 firstsuccede = TRUE;
1226                         }
1227                         else
1228                         {
1229                                 /* false- didn't work all the way */
1230                                 retval = FALSE;
1231                                 firstsuccede = FALSE;
1232                         }
1233                 }
1234                 if (firstsuccede)
1235                 {
1236                         /* only do this if the first half has worked */
1237                         set_tunnel(&x3, &y3, TRUE);
1238                 }
1239                 /* return value calculated above */
1240                 return retval;
1241         }
1242         else
1243         {
1244                 /* Do a short segment */
1245                 retval = TRUE;
1246                 short_seg_hack(x1, y1, x2, y2, type, 0, &retval);
1247
1248                 /* Hack - ignore return value so avoid infinite loops */
1249                 return TRUE;
1250         }
1251 }
1252