OSDN Git Service

[Refactor] #37353 dungeon.c/h を追加。
[hengband/hengband.git] / src / grid.c
1 
2  /*!
3   * @file grid.c
4   * @brief グリッドの実装 / low level dungeon routines -BEN-
5   * @date 2013/12/30
6   * @author
7   * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
8   *\n
9   * This software may be copied and distributed for educational, research,\n
10   * and not for profit purposes provided that this copyright and statement\n
11   * are included in all such copies.  Other copyrights may also apply.\n
12   * \n
13   * Support for Adam Bolt's tileset, lighting and transparency effects\n
14   * by Robert Ruehlmann (rr9@angband.org)\n
15   * \n
16   * 2013 Deskull Doxygen向けのコメント整理\n
17   */
18
19
20 #include "angband.h"
21 #include "util.h"
22
23 #include "floor.h"
24 #include "world.h"
25 #include "object-flavor.h"
26 #include "object-hook.h"
27 #include "dungeon.h"
28 #include "floor-generate.h"
29 #include "grid.h"
30 #include "trap.h"
31 #include "rooms.h"
32 #include "monster.h"
33 #include "quest.h"
34 #include "feature.h"
35 #include "monster-status.h"
36 #include "player-status.h"
37 #include "player-effects.h"
38 #include "spells.h"
39 #include "view-mainwindow.h"
40
41 /*!
42  * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
43  * @return 配置に成功したらTRUEを返す
44  */
45 bool new_player_spot(void)
46 {
47         POSITION y = 0, x = 0;
48         int max_attempts = 10000;
49
50         grid_type *g_ptr;
51         feature_type *f_ptr;
52
53         /* Place the player */
54         while (max_attempts--)
55         {
56                 /* Pick a legal spot */
57                 y = (POSITION)rand_range(1, current_floor_ptr->height - 2);
58                 x = (POSITION)rand_range(1, current_floor_ptr->width - 2);
59
60                 g_ptr = &current_floor_ptr->grid_array[y][x];
61
62                 /* Must be a "naked" floor grid */
63                 if (g_ptr->m_idx) continue;
64                 if (current_floor_ptr->dun_level)
65                 {
66                         f_ptr = &f_info[g_ptr->feat];
67
68                         if (max_attempts > 5000) /* Rule 1 */
69                         {
70                                 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
71                         }
72                         else /* Rule 2 */
73                         {
74                                 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
75                                 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
76                         }
77
78                         /* Refuse to start on anti-teleport grids in dungeon */
79                         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
80                 }
81                 if (!player_can_enter(g_ptr->feat, 0)) continue;
82                 if (!in_bounds(y, x)) continue;
83
84                 /* Refuse to start on anti-teleport grids */
85                 if (g_ptr->info & (CAVE_ICKY)) continue;
86
87                 break;
88         }
89
90         if (max_attempts < 1) /* Should be -1, actually if we failed... */
91                 return FALSE;
92
93         /* Save the new player grid */
94         p_ptr->y = y;
95         p_ptr->x = x;
96
97         return TRUE;
98 }
99
100
101
102 /*!
103  * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
104  * @param y 配置を試みたいマスのY座標
105  * @param x 配置を試みたいマスのX座標
106  * @return なし
107  */
108 void place_random_stairs(POSITION y, POSITION x)
109 {
110         bool up_stairs = TRUE;
111         bool down_stairs = TRUE;
112         grid_type *g_ptr;
113         g_ptr = &current_floor_ptr->grid_array[y][x];
114         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) return;
115
116         /* Town */
117         if (!current_floor_ptr->dun_level) up_stairs = FALSE;
118
119         /* Ironman */
120         if (ironman_downward) up_stairs = FALSE;
121
122         /* Bottom */
123         if (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) down_stairs = FALSE;
124
125         /* Quest-level */
126         if (quest_number(current_floor_ptr->dun_level) && (current_floor_ptr->dun_level > 1)) down_stairs = FALSE;
127
128         /* We can't place both */
129         if (down_stairs && up_stairs)
130         {
131                 /* Choose a staircase randomly */
132                 if (randint0(100) < 50) up_stairs = FALSE;
133                 else down_stairs = FALSE;
134         }
135
136         /* Place the stairs */
137         if (up_stairs) place_up_stairs(y, x);
138         else if (down_stairs) place_down_stairs(y, x);
139 }
140
141 /*!
142  * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
143  * @param y ドアの配置を試みたいマスのY座標
144  * @param x ドアの配置を試みたいマスのX座標
145  * @param room 部屋に接している場合向けのドア生成か否か
146  * @return なし
147  */
148 void place_random_door(POSITION y, POSITION x, bool room)
149 {
150         int tmp, type;
151         FEAT_IDX feat = feat_none;
152         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
153
154         /* Initialize mimic info */
155         g_ptr->mimic = 0;
156
157         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
158         {
159                 place_floor_bold(y, x);
160                 return;
161         }
162
163         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
164                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
165                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
166
167         /* Choose an object */
168         tmp = randint0(1000);
169
170         /* Open doors (300/1000) */
171         if (tmp < 300)
172         {
173                 /* Create open door */
174                 feat = feat_door[type].open;
175         }
176
177         /* Broken doors (100/1000) */
178         else if (tmp < 400)
179         {
180                 /* Create broken door */
181                 feat = feat_door[type].broken;
182         }
183
184         /* Secret doors (200/1000) */
185         else if (tmp < 600)
186         {
187                 /* Create secret door */
188                 place_closed_door(y, x, type);
189
190                 if (type != DOOR_CURTAIN)
191                 {
192                         /* Hide. If on the edge of room, use outer wall. */
193                         g_ptr->mimic = room ? feat_wall_outer : feat_wall_type[randint0(100)];
194
195                         /* Floor type terrain cannot hide a door */
196                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
197                         {
198                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
199                                 {
200                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
201                                 }
202                                 g_ptr->mimic = 0;
203                         }
204                 }
205         }
206
207         /* Closed, locked, or stuck doors (400/1000) */
208         else place_closed_door(y, x, type);
209
210         if (tmp < 400)
211         {
212                 if (feat != feat_none)
213                 {
214                         set_cave_feat(y, x, feat);
215                 }
216                 else
217                 {
218                         place_floor_bold(y, x);
219                 }
220         }
221
222         delete_monster(y, x);
223 }
224
225 /*!
226  * @brief 所定の位置に各種の閉じたドアを配置する / Place a random type of normal door at the given location.
227  * @param y ドアの配置を試みたいマスのY座標
228  * @param x ドアの配置を試みたいマスのX座標
229  * @param type ドアの地形ID
230  * @return なし
231  */
232 void place_closed_door(POSITION y, POSITION x, int type)
233 {
234         int tmp;
235         FEAT_IDX feat = feat_none;
236
237         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
238         {
239                 place_floor_bold(y, x);
240                 return;
241         }
242
243         /* Choose an object */
244         tmp = randint0(400);
245
246         /* Closed doors (300/400) */
247         if (tmp < 300)
248         {
249                 /* Create closed door */
250                 feat = feat_door[type].closed;
251         }
252
253         /* Locked doors (99/400) */
254         else if (tmp < 399)
255         {
256                 /* Create locked door */
257                 feat = feat_locked_door_random(type);
258         }
259
260         /* Stuck doors (1/400) */
261         else
262         {
263                 /* Create jammed door */
264                 feat = feat_jammed_door_random(type);
265         }
266
267         if (feat != feat_none)
268         {
269                 cave_set_feat(y, x, feat);
270
271                 /* Now it is not floor */
272                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
273         }
274         else
275         {
276                 place_floor_bold(y, x);
277         }
278 }
279
280 /*!
281 * @brief 鍵のかかったドアを配置する
282 * @param y 配置したいフロアのY座標
283 * @param x 配置したいフロアのX座標
284 * @return なし
285 */
286 void place_locked_door(POSITION y, POSITION x)
287 {
288         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
289         {
290                 place_floor_bold(y, x);
291         }
292         else
293         {
294                 set_cave_feat(y, x, feat_locked_door_random((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR));
295                 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_FLOOR);
296                 delete_monster(y, x);
297         }
298 }
299
300
301 /*!
302 * @brief 隠しドアを配置する
303 * @param y 配置したいフロアのY座標
304 * @param x 配置したいフロアのX座標
305 * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
306 * @return なし
307 */
308 void place_secret_door(POSITION y, POSITION x, int type)
309 {
310         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
311         {
312                 place_floor_bold(y, x);
313         }
314         else
315         {
316                 grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
317
318                 if (type == DOOR_DEFAULT)
319                 {
320                         type = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_CURTAIN) &&
321                                 one_in_((d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_CAVE) ? 16 : 256)) ? DOOR_CURTAIN :
322                                 ((d_info[p_ptr->dungeon_idx].flags1 & DF1_GLASS_DOOR) ? DOOR_GLASS_DOOR : DOOR_DOOR);
323                 }
324
325                 /* Create secret door */
326                 place_closed_door(y, x, type);
327
328                 if (type != DOOR_CURTAIN)
329                 {
330                         /* Hide by inner wall because this is used in rooms only */
331                         g_ptr->mimic = feat_wall_inner;
332
333                         /* Floor type terrain cannot hide a door */
334                         if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
335                         {
336                                 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
337                                 {
338                                         g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
339                                 }
340                                 g_ptr->mimic = 0;
341                         }
342                 }
343
344                 g_ptr->info &= ~(CAVE_FLOOR);
345                 delete_monster(y, x);
346         }
347 }
348
349 /*
350  * Routine used by the random vault creators to add a door to a location
351  * Note that range checking has to be done in the calling routine.
352  *
353  * The doors must be INSIDE the allocated region.
354  */
355 void add_door(POSITION x, POSITION y)
356 {
357         /* Need to have a wall in the center square */
358         if (!is_outer_bold(y, x)) return;
359
360         /* look at:
361         *  x#x
362         *  .#.
363         *  x#x
364         *
365         *  where x=don't care
366         *  .=floor, #=wall
367         */
368
369         if (is_floor_bold(y - 1, x) && is_floor_bold(y + 1, x) &&
370                 (is_outer_bold(y, x - 1) && is_outer_bold(y, x + 1)))
371         {
372                 /* secret door */
373                 place_secret_door(y, x, DOOR_DEFAULT);
374
375                 /* set boundarys so don't get wide doors */
376                 place_solid_bold(y, x - 1);
377                 place_solid_bold(y, x + 1);
378         }
379
380
381         /* look at:
382         *  x#x
383         *  .#.
384         *  x#x
385         *
386         *  where x = don't care
387         *  .=floor, #=wall
388         */
389         if (is_outer_bold(y - 1, x) && is_outer_bold(y + 1, x) &&
390                 is_floor_bold(y, x - 1) && is_floor_bold(y, x + 1))
391         {
392                 /* secret door */
393                 place_secret_door(y, x, DOOR_DEFAULT);
394
395                 /* set boundarys so don't get wide doors */
396                 place_solid_bold(y - 1, x);
397                 place_solid_bold(y + 1, x);
398         }
399 }
400
401 /*!
402 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
403 * @param y1 基準となるマスのY座標
404 * @param x1 基準となるマスのX座標
405 * @return 通路の数
406 * @note Assumes "in_bounds(y1, x1)"
407 * @details
408 * XXX XXX This routine currently only counts actual "empty floor"\n
409 * grids which are not in rooms.  We might want to also count stairs,\n
410 * open doors, closed doors, etc.
411 */
412 static int next_to_corr(POSITION y1, POSITION x1)
413 {
414         int i, k = 0;
415         POSITION y, x;
416
417         grid_type *g_ptr;
418
419         /* Scan adjacent grids */
420         for (i = 0; i < 4; i++)
421         {
422                 /* Extract the location */
423                 y = y1 + ddy_ddd[i];
424                 x = x1 + ddx_ddd[i];
425                 g_ptr = &current_floor_ptr->grid_array[y][x];
426
427                 /* Skip non floors */
428                 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
429
430                 /* Skip non "empty floor" grids */
431                 if (!is_floor_grid(g_ptr))
432                         continue;
433
434                 /* Skip grids inside rooms */
435                 if (g_ptr->info & (CAVE_ROOM)) continue;
436
437                 /* Count these grids */
438                 k++;
439         }
440
441         /* Return the number of corridors */
442         return (k);
443 }
444
445 /*!
446 * @brief ドアを設置可能な地形かを返す / Determine if the given location is "between" two walls, and "next to" two corridor spaces.
447 * @param y 判定を行いたいマスのY座標
448 * @param x 判定を行いたいマスのX座標
449 * @return ドアを設置可能ならばTRUEを返す
450 * @note Assumes "in_bounds(y1, x1)"
451 * @details
452 * \n
453 * Assumes "in_bounds(y, x)"\n
454 */
455 static bool possible_doorway(POSITION y, POSITION x)
456 {
457         /* Count the adjacent corridors */
458         if (next_to_corr(y, x) >= 2)
459         {
460                 /* Check Vertical */
461                 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
462                         cave_have_flag_bold(y + 1, x, FF_WALL))
463                 {
464                         return (TRUE);
465                 }
466
467                 /* Check Horizontal */
468                 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
469                         cave_have_flag_bold(y, x + 1, FF_WALL))
470                 {
471                         return (TRUE);
472                 }
473         }
474
475         /* No doorway */
476         return (FALSE);
477 }
478
479 /*!
480 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
481 * @param y 設置を行いたいマスのY座標
482 * @param x 設置を行いたいマスのX座標
483 * @return なし
484 */
485 void try_door(POSITION y, POSITION x)
486 {       if (!in_bounds(y, x)) return;
487
488         /* Ignore walls */
489         if (cave_have_flag_bold(y, x, FF_WALL)) return;
490
491         /* Ignore room grids */
492         if (current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
493
494         /* Occasional door (if allowed) */
495         if ((randint0(100) < dun_tun_jct) && possible_doorway(y, x) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS))
496         {
497                 /* Place a door */
498                 place_random_door(y, x, FALSE);
499         }
500 }
501
502
503 /*!
504  * @brief 長方形の空洞を生成する / Make an empty square floor, for the middle of rooms
505  * @param x1 長方形の左端X座標(-1)
506  * @param x2 長方形の右端X座標(+1)
507  * @param y1 長方形の上端Y座標(-1)
508  * @param y2 長方形の下端Y座標(+1)
509  * @param light 照明の有無
510  * @return なし
511  */
512 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
513 {
514         POSITION x, y;
515
516         /* Place a full floor under the room */
517         for (y = y1 - 1; y <= y2 + 1; y++)
518         {
519                 for (x = x1 - 1; x <= x2 + 1; x++)
520                 {
521                         place_floor_bold(y, x);
522                         add_cave_info(y, x, CAVE_ROOM);
523                         if (light) add_cave_info(y, x, CAVE_GLOW);
524                 }
525         }
526 }
527
528
529 /*!
530  * @brief 長方形の部屋を生成する / Make an empty square room, only floor and wall grids
531  * @param x1 長方形の左端X座標(-1)
532  * @param x2 長方形の右端X座標(+1)
533  * @param y1 長方形の上端Y座標(-1)
534  * @param y2 長方形の下端Y座標(+1)
535  * @param light 照明の有無
536  * @return なし
537  */
538 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
539 {
540         POSITION y, x;
541
542         place_floor(x1, x2, y1, y2, light);
543
544         /* Walls around the room */
545         for (y = y1 - 1; y <= y2 + 1; y++)
546         {
547                 place_outer_bold(y, x1 - 1);
548                 place_outer_bold(y, x2 + 1);
549         }
550         for (x = x1 - 1; x <= x2 + 1; x++)
551         {
552                 place_outer_bold(y1 - 1, x);
553                 place_outer_bold(y2 + 1, x);
554         }
555 }
556
557
558 /*!
559  * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
560  * @param y 配置したい中心マスのY座標
561  * @param x 配置したい中心マスのX座標
562  * @param num 配置したい数
563  * @return なし
564  * @details
565  * Only really called by some of the "vault" routines.
566  */
567 void vault_objects(POSITION y, POSITION x, int num)
568 {
569         int dummy = 0;
570         int i = 0, j = y, k = x;
571
572         grid_type *g_ptr;
573
574
575         /* Attempt to place 'num' objects */
576         for (; num > 0; --num)
577         {
578                 /* Try up to 11 spots looking for empty space */
579                 for (i = 0; i < 11; ++i)
580                 {
581                         /* Pick a random location */
582                         while (dummy < SAFE_MAX_ATTEMPTS)
583                         {
584                                 j = rand_spread(y, 2);
585                                 k = rand_spread(x, 3);
586                                 dummy++;
587                                 if (!in_bounds(j, k)) continue;
588                                 break;
589                         }
590
591                         if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
592                         {
593                                 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
594                         }
595
596                         /* Require "clean" floor space */
597                         g_ptr = &current_floor_ptr->grid_array[j][k];
598                         if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
599
600                         if (randint0(100) < 75)
601                         {
602                                 place_object(j, k, 0L);
603                         }
604                         else
605                         {
606                                 place_gold(j, k);
607                         }
608
609                         /* Placement accomplished */
610                         break;
611                 }
612         }
613 }
614
615 /*!
616  * @brief 特殊な部屋向けに各種アイテムを配置する(vault_trapのサブセット) / Place a trap with a given displacement of point
617  * @param y トラップを配置したいマスの中心Y座標
618  * @param x トラップを配置したいマスの中心X座標
619  * @param yd Y方向の配置分散マス数
620  * @param xd X方向の配置分散マス数
621  * @return なし
622  * @details
623  * Only really called by some of the "vault" routines.
624  */
625 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
626 {
627         int count = 0, y1 = y, x1 = x;
628         int dummy = 0;
629
630         grid_type *g_ptr;
631
632         /* Place traps */
633         for (count = 0; count <= 5; count++)
634         {
635                 /* Get a location */
636                 while (dummy < SAFE_MAX_ATTEMPTS)
637                 {
638                         y1 = rand_spread(y, yd);
639                         x1 = rand_spread(x, xd);
640                         dummy++;
641                         if (!in_bounds(y1, x1)) continue;
642                         break;
643                 }
644
645                 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
646                 {
647                         msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
648                 }
649
650                 /* Require "naked" floor grids */
651                 g_ptr = &current_floor_ptr->grid_array[y1][x1];
652                 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
653
654                 /* Place the trap */
655                 place_trap(y1, x1);
656
657                 break;
658         }
659 }
660
661 /*!
662  * @brief 特殊な部屋向けに各種アイテムを配置する(メインルーチン) / Place some traps with a given displacement of given location
663  * @param y トラップを配置したいマスの中心Y座標
664  * @param x トラップを配置したいマスの中心X座標
665  * @param yd Y方向の配置分散マス数
666  * @param xd X方向の配置分散マス数
667  * @param num 配置したいトラップの数
668  * @return なし
669  * @details
670  * Only really called by some of the "vault" routines.
671  */
672 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
673 {
674         int i;
675
676         for (i = 0; i < num; i++)
677         {
678                 vault_trap_aux(y, x, yd, xd);
679         }
680 }
681
682 /*!
683  * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
684  * @param y1 モンスターを配置したいマスの中心Y座標
685  * @param x1 モンスターを配置したいマスの中心X座標
686  * @param num 配置したいモンスターの数
687  * @return なし
688  * @details
689  * Only really called by some of the "vault" routines.
690  */
691 void vault_monsters(POSITION y1, POSITION x1, int num)
692 {
693         int k, i;
694         POSITION y, x;
695         grid_type *g_ptr;
696
697         /* Try to summon "num" monsters "near" the given location */
698         for (k = 0; k < num; k++)
699         {
700                 /* Try nine locations */
701                 for (i = 0; i < 9; i++)
702                 {
703                         int d = 1;
704
705                         /* Pick a nearby location */
706                         scatter(&y, &x, y1, x1, d, 0);
707
708                         /* Require "empty" floor grids */
709                         g_ptr = &current_floor_ptr->grid_array[y][x];
710                         if (!cave_empty_grid(g_ptr)) continue;
711
712                         /* Place the monster (allow groups) */
713                         current_floor_ptr->monster_level = current_floor_ptr->base_level + 2;
714                         (void)place_monster(y, x, (PM_ALLOW_SLEEP | PM_ALLOW_GROUP));
715                         current_floor_ptr->monster_level = current_floor_ptr->base_level;
716                 }
717         }
718 }
719
720 /*!
721  * @brief 指定のマスが床系地形であるかを返す / Function that sees if a square is a floor.  (Includes range checking.)
722  * @param x チェックするマスのX座標
723  * @param y チェックするマスのY座標
724  * @return 床系地形ならばTRUE
725  */
726 bool get_is_floor(POSITION x, POSITION y)
727 {
728         if (!in_bounds(y, x))
729         {
730                 /* Out of bounds */
731                 return (FALSE);
732         }
733
734         /* Do the real check */
735         if (is_floor_bold(y, x)) return (TRUE);
736
737         return (FALSE);
738 }
739
740 /*!
741  * @brief 指定のマスを床地形に変える / Set a square to be floor.  (Includes range checking.)
742  * @param x 地形を変えたいマスのX座標
743  * @param y 地形を変えたいマスのY座標
744  * @return なし
745  */
746 void set_floor(POSITION x, POSITION y)
747 {
748         if (!in_bounds(y, x))
749         {
750                 /* Out of bounds */
751                 return;
752         }
753
754         if (current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
755         {
756                 /* A room border don't touch. */
757                 return;
758         }
759
760         /* Set to be floor if is a wall (don't touch lakes). */
761         if (is_extra_bold(y, x))
762                 place_floor_bold(y, x);
763 }
764
765 /*!
766  * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
767  * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
768  * @return なし
769  */
770 void place_bound_perm_wall(grid_type *g_ptr)
771 {
772         if (bound_walls_perm)
773         {
774                 /* Clear boundary mimic */
775                 g_ptr->mimic = 0;
776         }
777         else
778         {
779                 feature_type *f_ptr = &f_info[g_ptr->feat];
780
781                 /* Hack -- Decline boundary walls with known treasure  */
782                 if ((have_flag(f_ptr->flags, FF_HAS_GOLD) || have_flag(f_ptr->flags, FF_HAS_ITEM)) &&
783                         !have_flag(f_ptr->flags, FF_SECRET))
784                         g_ptr->feat = feat_state(g_ptr->feat, FF_ENSECRET);
785
786                 /* Set boundary mimic */
787                 g_ptr->mimic = g_ptr->feat;
788         }
789
790         /* Add "solid" perma-wall */
791         place_solid_perm_grid(g_ptr);
792 }
793
794 /*!
795  * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
796  * @param g_ptr マス構造体の参照ポインタ
797  * @return 看破済みの罠があるならTRUEを返す。
798  */
799 bool is_known_trap(grid_type *g_ptr)
800 {
801         if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
802                 is_trap(g_ptr->feat)) return TRUE;
803         else
804                 return FALSE;
805 }
806
807
808
809 /*!
810  * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
811  * @param g_ptr マス構造体の参照ポインタ
812  * @return 隠されたドアがあるならTRUEを返す。
813  */
814 bool is_hidden_door(grid_type *g_ptr)
815 {
816         if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
817                 is_closed_door(g_ptr->feat))
818                 return TRUE;
819         else
820                 return FALSE;
821 }
822
823 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
824
825
826 /*!
827  * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
828  * @param y y座標
829  * @param x x座標
830  * @return 指定された座標に照明がかかっているならTRUEを返す。。
831  */
832 bool check_local_illumination(POSITION y, POSITION x)
833 {
834         /* Hack -- move towards player */
835         POSITION yy = (y < p_ptr->y) ? (y + 1) : (y > p_ptr->y) ? (y - 1) : y;
836         POSITION xx = (x < p_ptr->x) ? (x + 1) : (x > p_ptr->x) ? (x - 1) : x;
837
838         /* Check for "local" illumination */
839
840 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
841
842         /* Check for "complex" illumination */
843         if ((feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][xx])) &&
844                 (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
845                 (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[y][xx])) &&
846                 (current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
847                         (feat_supports_los(get_feat_mimic(&current_floor_ptr->grid_array[yy][x])) &&
848                 (current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
849         {
850                 return TRUE;
851         }
852         else return FALSE;
853
854 #else /* COMPLEX_WALL_ILLUMINATION */
855
856         /* Check for "simple" illumination */
857         return (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
858
859 #endif /* COMPLEX_WALL_ILLUMINATION */
860 }
861
862
863 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
864 #define update_local_illumination_aux(Y, X) \
865 { \
866         if (player_has_los_bold((Y), (X))) \
867         { \
868                 /* Update the monster */ \
869                 if (current_floor_ptr->grid_array[(Y)][(X)].m_idx) update_monster(current_floor_ptr->grid_array[(Y)][(X)].m_idx, FALSE); \
870 \
871                 /* Notice and redraw */ \
872                 note_spot((Y), (X)); \
873                 lite_spot((Y), (X)); \
874         } \
875 }
876
877 /*!
878  * @brief 指定された座標の照明状態を更新する / Update "local" illumination
879  * @param y y座標
880  * @param x x座標
881  * @return なし
882  */
883 void update_local_illumination(POSITION y, POSITION x)
884 {
885         int i;
886         POSITION yy, xx;
887
888         if (!in_bounds(y, x)) return;
889
890 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
891
892         if ((y != p_ptr->y) && (x != p_ptr->x))
893         {
894                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
895                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
896                 update_local_illumination_aux(yy, xx);
897                 update_local_illumination_aux(y, xx);
898                 update_local_illumination_aux(yy, x);
899         }
900         else if (x != p_ptr->x) /* y == p_ptr->y */
901         {
902                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
903                 for (i = -1; i <= 1; i++)
904                 {
905                         yy = y + i;
906                         update_local_illumination_aux(yy, xx);
907                 }
908                 yy = y - 1;
909                 update_local_illumination_aux(yy, x);
910                 yy = y + 1;
911                 update_local_illumination_aux(yy, x);
912         }
913         else if (y != p_ptr->y) /* x == p_ptr->x */
914         {
915                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
916                 for (i = -1; i <= 1; i++)
917                 {
918                         xx = x + i;
919                         update_local_illumination_aux(yy, xx);
920                 }
921                 xx = x - 1;
922                 update_local_illumination_aux(y, xx);
923                 xx = x + 1;
924                 update_local_illumination_aux(y, xx);
925         }
926         else /* Player's grid */
927         {
928                 for (i = 0; i < 8; i++)
929                 {
930                         yy = y + ddy_cdd[i];
931                         xx = x + ddx_cdd[i];
932                         update_local_illumination_aux(yy, xx);
933                 }
934         }
935
936 #else /* COMPLEX_WALL_ILLUMINATION */
937
938         if ((y != p_ptr->y) && (x != p_ptr->x))
939         {
940                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
941                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
942                 update_local_illumination_aux(yy, xx);
943         }
944         else if (x != p_ptr->x) /* y == p_ptr->y */
945         {
946                 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
947                 for (i = -1; i <= 1; i++)
948                 {
949                         yy = y + i;
950                         update_local_illumination_aux(yy, xx);
951                 }
952         }
953         else if (y != p_ptr->y) /* x == p_ptr->x */
954         {
955                 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
956                 for (i = -1; i <= 1; i++)
957                 {
958                         xx = x + i;
959                         update_local_illumination_aux(yy, xx);
960                 }
961         }
962         else /* Player's grid */
963         {
964                 for (i = 0; i < 8; i++)
965                 {
966                         yy = y + ddy_cdd[i];
967                         xx = x + ddx_cdd[i];
968                         update_local_illumination_aux(yy, xx);
969                 }
970         }
971
972 #endif /* COMPLEX_WALL_ILLUMINATION */
973 }
974
975
976 /*!
977  * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
978  * @param y y座標
979  * @param x x座標
980  * @return 視覚に収められる状態ならTRUEを返す
981  * @details
982  * He must have vision, illumination, and line of sight.\n
983  * \n
984  * Note -- "CAVE_LITE" is only set if the "torch" has "los()".\n
985  * So, given "CAVE_LITE", we know that the grid is "fully visible".\n
986  *\n
987  * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean\n
988  * that a wall is visible from any direction.  That would be odd.  Except\n
989  * under wizard light, which might make sense.  Thus, for walls, we require\n
990  * not only that they be "CAVE_GLOW", but also, that they be adjacent to a\n
991  * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is\n
992  * in line of sight of the player.\n
993  *\n
994  * This extra check is expensive, but it provides a more "correct" semantics.\n
995  *\n
996  * Note that we should not run this check on walls which are "outer walls" of\n
997  * the dungeon, or we will induce a memory fault, but actually verifying all\n
998  * of the locations would be extremely expensive.\n
999  *\n
1000  * Thus, to speed up the function, we assume that all "perma-walls" which are\n
1001  * "CAVE_GLOW" are "illuminated" from all sides.  This is correct for all cases\n
1002  * except "vaults" and the "buildings" in town.  But the town is a hack anyway,\n
1003  * and the player has more important things on his mind when he is attacking a\n
1004  * monster vault.  It is annoying, but an extremely important optimization.\n
1005  *\n
1006  * Note that "glowing walls" are only considered to be "illuminated" if the\n
1007  * grid which is next to the wall in the direction of the player is also a\n
1008  * "glowing" grid.  This prevents the player from being able to "see" the\n
1009  * walls of illuminated rooms from a corridor outside the room.\n
1010  */
1011 bool player_can_see_bold(POSITION y, POSITION x)
1012 {
1013         grid_type *g_ptr;
1014
1015         /* Blind players see nothing */
1016         if (p_ptr->blind) return FALSE;
1017
1018         g_ptr = &current_floor_ptr->grid_array[y][x];
1019
1020         /* Note that "torch-lite" yields "illumination" */
1021         if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) return TRUE;
1022
1023         /* Require line of sight to the grid */
1024         if (!player_has_los_bold(y, x)) return FALSE;
1025
1026         /* Noctovision of Ninja */
1027         if (p_ptr->see_nocto) return TRUE;
1028
1029         /* Require "perma-lite" of the grid */
1030         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) return FALSE;
1031
1032         /* Feature code (applying "mimic" field) */
1033         /* Floors are simple */
1034         if (feat_supports_los(get_feat_mimic(g_ptr))) return TRUE;
1035
1036         /* Check for "local" illumination */
1037         return check_local_illumination(y, x);
1038 }
1039
1040 /*!
1041  * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
1042  * @return 視覚に収められていないならTRUEを返す
1043  * @details player_can_see_bold()関数の返り値の否定を返している。
1044  */
1045 bool no_lite(void)
1046 {
1047         return (!player_can_see_bold(p_ptr->y, p_ptr->x));
1048 }
1049
1050
1051 /*!
1052  * @brief 指定された座標が地震や階段生成の対象となるマスかを返す。 / Determine if a given location may be "destroyed"
1053  * @param y y座標
1054  * @param x x座標
1055  * @return 各種の変更が可能ならTRUEを返す。
1056  * @details
1057  * 条件は永久地形でなく、なおかつ該当のマスにアーティファクトが存在しないか、である。英語の旧コメントに反して*破壊*の抑止判定には現在使われていない。
1058  */
1059 bool cave_valid_bold(POSITION y, POSITION x)
1060 {
1061         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1062         OBJECT_IDX this_o_idx, next_o_idx = 0;
1063
1064         /* Forbid perma-grids */
1065         if (cave_perma_grid(g_ptr)) return (FALSE);
1066
1067         /* Check objects */
1068         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1069         {
1070                 object_type *o_ptr;
1071                 o_ptr = &current_floor_ptr->o_list[this_o_idx];
1072                 next_o_idx = o_ptr->next_o_idx;
1073
1074                 /* Forbid artifact grids */
1075                 if (object_is_artifact(o_ptr)) return (FALSE);
1076         }
1077
1078         /* Accept */
1079         return (TRUE);
1080 }
1081
1082
1083
1084 /*
1085  * Place an attr/char pair at the given map coordinate, if legal.
1086  */
1087 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
1088 {
1089         /* Only do "legal" locations */
1090         if (panel_contains(y, x))
1091         {
1092                 /* Hack -- fake monochrome */
1093                 if (!use_graphics)
1094                 {
1095                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1096                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1097                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1098                 }
1099
1100                 /* Draw the char using the attr */
1101                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
1102         }
1103 }
1104
1105
1106
1107
1108
1109 /*
1110  * Memorize interesting viewable object/features in the given grid
1111  *
1112  * This function should only be called on "legal" grids.
1113  *
1114  * This function will memorize the object and/or feature in the given
1115  * grid, if they are (1) viewable and (2) interesting.  Note that all
1116  * objects are interesting, all terrain features except floors (and
1117  * invisible traps) are interesting, and floors (and invisible traps)
1118  * are interesting sometimes (depending on various options involving
1119  * the illumination of floor grids).
1120  *
1121  * The automatic memorization of all objects and non-floor terrain
1122  * features as soon as they are displayed allows incredible amounts
1123  * of optimization in various places, especially "map_info()".
1124  *
1125  * Note that the memorization of objects is completely separate from
1126  * the memorization of terrain features, preventing annoying floor
1127  * memorization when a detected object is picked up from a dark floor,
1128  * and object memorization when an object is dropped into a floor grid
1129  * which is memorized but out-of-sight.
1130  *
1131  * This function should be called every time the "memorization" of
1132  * a grid (or the object in a grid) is called into question, such
1133  * as when an object is created in a grid, when a terrain feature
1134  * "changes" from "floor" to "non-floor", when any grid becomes
1135  * "illuminated" or "viewable", and when a "floor" grid becomes
1136  * "torch-lit".
1137  *
1138  * Note the relatively efficient use of this function by the various
1139  * "update_view()" and "update_lite()" calls, to allow objects and
1140  * terrain features to be memorized (and drawn) whenever they become
1141  * viewable or illuminated in any way, but not when they "maintain"
1142  * or "lose" their previous viewability or illumination.
1143  *
1144  * Note the butchered "internal" version of "player_can_see_bold()",
1145  * optimized primarily for the most common cases, that is, for the
1146  * non-marked floor grids.
1147  */
1148 void note_spot(POSITION y, POSITION x)
1149 {
1150         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1151         OBJECT_IDX this_o_idx, next_o_idx = 0;
1152
1153         /* Blind players see nothing */
1154         if (p_ptr->blind) return;
1155
1156         /* Analyze non-torch-lit grids */
1157         if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
1158         {
1159                 /* Require line of sight to the grid */
1160                 if (!(g_ptr->info & (CAVE_VIEW))) return;
1161
1162                 /* Require "perma-lite" of the grid */
1163                 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1164                 {
1165                         /* Not Ninja */
1166                         if (!p_ptr->see_nocto) return;
1167                 }
1168         }
1169
1170
1171         /* Hack -- memorize objects */
1172         for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1173         {
1174                 object_type *o_ptr = &current_floor_ptr->o_list[this_o_idx];
1175                 next_o_idx = o_ptr->next_o_idx;
1176
1177                 /* Memorize objects */
1178                 o_ptr->marked |= OM_FOUND;
1179         }
1180
1181
1182         /* Hack -- memorize grids */
1183         if (!(g_ptr->info & (CAVE_MARK)))
1184         {
1185                 /* Feature code (applying "mimic" field) */
1186                 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
1187
1188                 /* Memorize some "boring" grids */
1189                 if (!have_flag(f_ptr->flags, FF_REMEMBER))
1190                 {
1191                         /* Option -- memorize all torch-lit floors */
1192                         if (view_torch_grids &&
1193                                 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
1194                         {
1195                                 g_ptr->info |= (CAVE_MARK);
1196                         }
1197
1198                         /* Option -- memorize all perma-lit floors */
1199                         else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
1200                         {
1201                                 g_ptr->info |= (CAVE_MARK);
1202                         }
1203                 }
1204
1205                 /* Memorize normal grids */
1206                 else if (have_flag(f_ptr->flags, FF_LOS))
1207                 {
1208                         g_ptr->info |= (CAVE_MARK);
1209                 }
1210
1211                 /* Memorize torch-lit walls */
1212                 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1213                 {
1214                         g_ptr->info |= (CAVE_MARK);
1215                 }
1216
1217                 /* Memorize walls seen by noctovision of Ninja */
1218                 else if (p_ptr->see_nocto)
1219                 {
1220                         g_ptr->info |= (CAVE_MARK);
1221                 }
1222
1223                 /* Memorize certain non-torch-lit wall grids */
1224                 else if (check_local_illumination(y, x))
1225                 {
1226                         g_ptr->info |= (CAVE_MARK);
1227                 }
1228         }
1229
1230         /* Memorize terrain of the grid */
1231         g_ptr->info |= (CAVE_KNOWN);
1232 }
1233
1234 /*
1235  * Redraw (on the screen) a given MAP location
1236  *
1237  * This function should only be called on "legal" grids
1238  */
1239 void lite_spot(POSITION y, POSITION x)
1240 {
1241         /* Redraw if on screen */
1242         if (panel_contains(y, x) && in_bounds2(y, x))
1243         {
1244                 TERM_COLOR a;
1245                 SYMBOL_CODE c;
1246                 TERM_COLOR ta;
1247                 SYMBOL_CODE tc;
1248
1249                 map_info(y, x, &a, &c, &ta, &tc);
1250
1251                 /* Hack -- fake monochrome */
1252                 if (!use_graphics)
1253                 {
1254                         if (current_world_ptr->timewalk_m_idx) a = TERM_DARK;
1255                         else if (IS_INVULN() || p_ptr->timewalk) a = TERM_WHITE;
1256                         else if (p_ptr->wraith_form) a = TERM_L_DARK;
1257                 }
1258
1259                 /* Hack -- Queue it */
1260                 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
1261
1262                 /* Update sub-windows */
1263                 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1264         }
1265 }
1266
1267 /*
1268  * Some comments on the grid flags.  -BEN-
1269  *
1270  *
1271  * One of the major bottlenecks in previous versions of Angband was in
1272  * the calculation of "line of sight" from the player to various grids,
1273  * such as monsters.  This was such a nasty bottleneck that a lot of
1274  * silly things were done to reduce the dependancy on "line of sight",
1275  * for example, you could not "see" any grids in a lit room until you
1276  * actually entered the room, and there were all kinds of bizarre grid
1277  * flags to enable this behavior.  This is also why the "call light"
1278  * spells always lit an entire room.
1279  *
1280  * The code below provides functions to calculate the "field of view"
1281  * for the player, which, once calculated, provides extremely fast
1282  * calculation of "line of sight from the player", and to calculate
1283  * the "field of torch lite", which, again, once calculated, provides
1284  * extremely fast calculation of "which grids are lit by the player's
1285  * lite source".  In addition to marking grids as "GRID_VIEW" and/or
1286  * "GRID_LITE", as appropriate, these functions maintain an array for
1287  * each of these two flags, each array containing the locations of all
1288  * of the grids marked with the appropriate flag, which can be used to
1289  * very quickly scan through all of the grids in a given set.
1290  *
1291  * To allow more "semantically valid" field of view semantics, whenever
1292  * the field of view (or the set of torch lit grids) changes, all of the
1293  * grids in the field of view (or the set of torch lit grids) are "drawn"
1294  * so that changes in the world will become apparent as soon as possible.
1295  * This has been optimized so that only grids which actually "change" are
1296  * redrawn, using the "temp" array and the "GRID_TEMP" flag to keep track
1297  * of the grids which are entering or leaving the relevent set of grids.
1298  *
1299  * These new methods are so efficient that the old nasty code was removed.
1300  *
1301  * Note that there is no reason to "update" the "viewable space" unless
1302  * the player "moves", or walls/doors are created/destroyed, and there
1303  * is no reason to "update" the "torch lit grids" unless the field of
1304  * view changes, or the "light radius" changes.  This means that when
1305  * the player is resting, or digging, or doing anything that does not
1306  * involve movement or changing the state of the dungeon, there is no
1307  * need to update the "view" or the "lite" regions, which is nice.
1308  *
1309  * Note that the calls to the nasty "los()" function have been reduced
1310  * to a bare minimum by the use of the new "field of view" calculations.
1311  *
1312  * I wouldn't be surprised if slight modifications to the "update_view()"
1313  * function would allow us to determine "reverse line-of-sight" as well
1314  * as "normal line-of-sight", which would allow monsters to use a more
1315  * "correct" calculation to determine if they can "see" the player.  For
1316  * now, monsters simply "cheat" somewhat and assume that if the player
1317  * has "line of sight" to the monster, then the monster can "pretend"
1318  * that it has "line of sight" to the player.
1319  *
1320  *
1321  * The "update_lite()" function maintains the "CAVE_LITE" flag for each
1322  * grid and maintains an array of all "CAVE_LITE" grids.
1323  *
1324  * This set of grids is the complete set of all grids which are lit by
1325  * the players light source, which allows the "player_can_see_bold()"
1326  * function to work very quickly.
1327  *
1328  * Note that every "CAVE_LITE" grid is also a "CAVE_VIEW" grid, and in
1329  * fact, the player (unless blind) can always "see" all grids which are
1330  * marked as "CAVE_LITE", unless they are "off screen".
1331  *
1332  *
1333  * The "update_view()" function maintains the "CAVE_VIEW" flag for each
1334  * grid and maintains an array of all "CAVE_VIEW" grids.
1335  *
1336  * This set of grids is the complete set of all grids within line of sight
1337  * of the player, allowing the "player_has_los_bold()" macro to work very
1338  * quickly.
1339  *
1340  *
1341  * The current "update_view()" algorithm uses the "CAVE_XTRA" flag as a
1342  * temporary internal flag to mark those grids which are not only in view,
1343  * but which are also "easily" in line of sight of the player.  This flag
1344  * is always cleared when we are done.
1345  *
1346  *
1347  * The current "update_lite()" and "update_view()" algorithms use the
1348  * "CAVE_TEMP" flag, and the array of grids which are marked as "CAVE_TEMP",
1349  * to keep track of which grids were previously marked as "CAVE_LITE" or
1350  * "CAVE_VIEW", which allows us to optimize the "screen updates".
1351  *
1352  * The "CAVE_TEMP" flag, and the array of "CAVE_TEMP" grids, is also used
1353  * for various other purposes, such as spreading lite or darkness during
1354  * "lite_room()" / "unlite_room()", and for calculating monster flow.
1355  *
1356  *
1357  * Any grid can be marked as "CAVE_GLOW" which means that the grid itself is
1358  * in some way permanently lit.  However, for the player to "see" anything
1359  * in the grid, as determined by "player_can_see()", the player must not be
1360  * blind, the grid must be marked as "CAVE_VIEW", and, in addition, "wall"
1361  * grids, even if marked as "perma lit", are only illuminated if they touch
1362  * a grid which is not a wall and is marked both "CAVE_GLOW" and "CAVE_VIEW".
1363  *
1364  *
1365  * To simplify various things, a grid may be marked as "CAVE_MARK", meaning
1366  * that even if the player cannot "see" the grid, he "knows" the terrain in
1367  * that grid.  This is used to "remember" walls/doors/stairs/floors when they
1368  * are "seen" or "detected", and also to "memorize" floors, after "wiz_lite()",
1369  * or when one of the "memorize floor grids" options induces memorization.
1370  *
1371  * Objects are "memorized" in a different way, using a special "marked" flag
1372  * on the object itself, which is set when an object is observed or detected.
1373  *
1374  *
1375  * A grid may be marked as "CAVE_ROOM" which means that it is part of a "room",
1376  * and should be illuminated by "lite room" and "darkness" spells.
1377  *
1378  *
1379  * A grid may be marked as "CAVE_ICKY" which means it is part of a "vault",
1380  * and should be unavailable for "teleportation" destinations.
1381  *
1382  *
1383  * The "view_perma_grids" allows the player to "memorize" every perma-lit grid
1384  * which is observed, and the "view_torch_grids" allows the player to memorize
1385  * every torch-lit grid.  The player will always memorize important walls,
1386  * doors, stairs, and other terrain features, as well as any "detected" grids.
1387  *
1388  * Note that the new "update_view()" method allows, among other things, a room
1389  * to be "partially" seen as the player approaches it, with a growing cone of
1390  * floor appearing as the player gets closer to the door.  Also, by not turning
1391  * on the "memorize perma-lit grids" option, the player will only "see" those
1392  * floor grids which are actually in line of sight.
1393  *
1394  * And my favorite "plus" is that you can now use a special option to draw the
1395  * "floors" in the "viewable region" brightly (actually, to draw the *other*
1396  * grids dimly), providing a "pretty" effect as the player runs around, and
1397  * to efficiently display the "torch lite" in a special color.
1398  *
1399  *
1400  * Some comments on the "update_view()" algorithm...
1401  *
1402  * The algorithm is very fast, since it spreads "obvious" grids very quickly,
1403  * and only has to call "los()" on the borderline cases.  The major axes/diags
1404  * even terminate early when they hit walls.  I need to find a quick way
1405  * to "terminate" the other scans.
1406  *
1407  * Note that in the worst case (a big empty area with say 5% scattered walls),
1408  * each of the 1500 or so nearby grids is checked once, most of them getting
1409  * an "instant" rating, and only a small portion requiring a call to "los()".
1410  *
1411  * The only time that the algorithm appears to be "noticeably" too slow is
1412  * when running, and this is usually only important in town, since the town
1413  * provides about the worst scenario possible, with large open regions and
1414  * a few scattered obstructions.  There is a special "efficiency" option to
1415  * allow the player to reduce his field of view in town, if needed.
1416  *
1417  * In the "best" case (say, a normal stretch of corridor), the algorithm
1418  * makes one check for each viewable grid, and makes no calls to "los()".
1419  * So running in corridors is very fast, and if a lot of monsters are
1420  * nearby, it is much faster than the old methods.
1421  *
1422  * Note that resting, most normal commands, and several forms of running,
1423  * plus all commands executed near large groups of monsters, are strictly
1424  * more efficient with "update_view()" that with the old "compute los() on
1425  * demand" method, primarily because once the "field of view" has been
1426  * calculated, it does not have to be recalculated until the player moves
1427  * (or a wall or door is created or destroyed).
1428  *
1429  * Note that we no longer have to do as many "los()" checks, since once the
1430  * "view" region has been built, very few things cause it to be "changed"
1431  * (player movement, and the opening/closing of doors, changes in wall status).
1432  * Note that door/wall changes are only relevant when the door/wall itself is
1433  * in the "view" region.
1434  *
1435  * The algorithm seems to only call "los()" from zero to ten times, usually
1436  * only when coming down a corridor into a room, or standing in a room, just
1437  * misaligned with a corridor.  So if, say, there are five "nearby" monsters,
1438  * we will be reducing the calls to "los()".
1439  *
1440  * I am thinking in terms of an algorithm that "walks" from the central point
1441  * out to the maximal "distance", at each point, determining the "view" code
1442  * (above).  For each grid not on a major axis or diagonal, the "view" code
1443  * depends on the "cave_los_bold()" and "view" of exactly two other grids
1444  * (the one along the nearest diagonal, and the one next to that one, see
1445  * "update_view_aux()"...).
1446  *
1447  * We "memorize" the viewable space array, so that at the cost of under 3000
1448  * bytes, we reduce the time taken by "forget_view()" to one assignment for
1449  * each grid actually in the "viewable space".  And for another 3000 bytes,
1450  * we prevent "erase + redraw" ineffiencies via the "seen" set.  These bytes
1451  * are also used by other routines, thus reducing the cost to almost nothing.
1452  *
1453  * A similar thing is done for "forget_lite()" in which case the savings are
1454  * much less, but save us from doing bizarre maintenance checking.
1455  *
1456  * In the worst "normal" case (in the middle of the town), the reachable space
1457  * actually reaches to more than half of the largest possible "circle" of view,
1458  * or about 800 grids, and in the worse case (in the middle of a dungeon level
1459  * where all the walls have been removed), the reachable space actually reaches
1460  * the theoretical maximum size of just under 1500 grids.
1461  *
1462  * Each grid G examines the "state" of two (?) other (adjacent) grids, G1 & G2.
1463  * If G1 is lite, G is lite.  Else if G2 is lite, G is half.  Else if G1 and G2
1464  * are both half, G is half.  Else G is dark.  It only takes 2 (or 4) bits to
1465  * "name" a grid, so (for MAX_RAD of 20) we could use 1600 bytes, and scan the
1466  * entire possible space (including initialization) in one step per grid.  If
1467  * we do the "clearing" as a separate step (and use an array of "view" grids),
1468  * then the clearing will take as many steps as grids that were viewed, and the
1469  * algorithm will be able to "stop" scanning at various points.
1470  * Oh, and outside of the "torch radius", only "lite" grids need to be scanned.
1471  */
1472
1473 /*
1474  * Mega-Hack -- Delayed visual update
1475  * Only used if update_view(), update_lite() or update_mon_lite() was called
1476  */
1477 void delayed_visual_update(void)
1478 {
1479         int i;
1480         POSITION y, x;
1481         grid_type *g_ptr;
1482
1483         /* Update needed grids */
1484         for (i = 0; i < current_floor_ptr->redraw_n; i++)
1485         {
1486                 y = current_floor_ptr->redraw_y[i];
1487                 x = current_floor_ptr->redraw_x[i];
1488                 g_ptr = &current_floor_ptr->grid_array[y][x];
1489
1490                 /* Update only needed grids (prevent multiple updating) */
1491                 if (!(g_ptr->info & CAVE_REDRAW)) continue;
1492
1493                 /* If required, note */
1494                 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
1495
1496                 lite_spot(y, x);
1497
1498                 /* Hack -- Visual update of monster on this grid */
1499                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1500
1501                 /* No longer in the array */
1502                 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
1503         }
1504
1505         /* None left */
1506         current_floor_ptr->redraw_n = 0;
1507 }
1508
1509
1510 /*
1511  * Hack -- forget the "flow" information
1512  */
1513 void forget_flow(void)
1514 {
1515         POSITION x, y;
1516
1517         /* Check the entire dungeon */
1518         for (y = 0; y < current_floor_ptr->height; y++)
1519         {
1520                 for (x = 0; x < current_floor_ptr->width; x++)
1521                 {
1522                         /* Forget the old data */
1523                         current_floor_ptr->grid_array[y][x].dist = 0;
1524                         current_floor_ptr->grid_array[y][x].cost = 0;
1525                         current_floor_ptr->grid_array[y][x].when = 0;
1526                 }
1527         }
1528 }
1529
1530
1531 /*
1532  * Hack - speed up the update_flow algorithm by only doing
1533  * it everytime the player moves out of LOS of the last
1534  * "way-point".
1535  */
1536 static POSITION flow_x = 0;
1537 static POSITION flow_y = 0;
1538
1539
1540
1541 /*
1542  * Hack -- fill in the "cost" field of every grid that the player
1543  * can "reach" with the number of steps needed to reach that grid.
1544  * This also yields the "distance" of the player from every grid.
1545  *
1546  * In addition, mark the "when" of the grids that can reach
1547  * the player with the incremented value of "flow_n".
1548  *
1549  * Hack -- use the "seen" array as a "circular queue".
1550  *
1551  * We do not need a priority queue because the cost from grid
1552  * to grid is always "one" and we process them in order.
1553  */
1554 void update_flow(void)
1555 {
1556         POSITION x, y;
1557         DIRECTION d;
1558         int flow_head = 1;
1559         int flow_tail = 0;
1560
1561         /* Paranoia -- make sure the array is empty */
1562         if (tmp_pos.n) return;
1563
1564         /* The last way-point is on the map */
1565         if (running && in_bounds(flow_y, flow_x))
1566         {
1567                 /* The way point is in sight - do not update.  (Speedup) */
1568                 if (current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
1569         }
1570
1571         /* Erase all of the current flow information */
1572         for (y = 0; y < current_floor_ptr->height; y++)
1573         {
1574                 for (x = 0; x < current_floor_ptr->width; x++)
1575                 {
1576                         current_floor_ptr->grid_array[y][x].cost = 0;
1577                         current_floor_ptr->grid_array[y][x].dist = 0;
1578                 }
1579         }
1580
1581         /* Save player position */
1582         flow_y = p_ptr->y;
1583         flow_x = p_ptr->x;
1584
1585         /* Add the player's grid to the queue */
1586         tmp_pos.y[0] = p_ptr->y;
1587         tmp_pos.x[0] = p_ptr->x;
1588
1589         /* Now process the queue */
1590         while (flow_head != flow_tail)
1591         {
1592                 int ty, tx;
1593
1594                 /* Extract the next entry */
1595                 ty = tmp_pos.y[flow_tail];
1596                 tx = tmp_pos.x[flow_tail];
1597
1598                 /* Forget that entry */
1599                 if (++flow_tail == TEMP_MAX) flow_tail = 0;
1600
1601                 /* Add the "children" */
1602                 for (d = 0; d < 8; d++)
1603                 {
1604                         int old_head = flow_head;
1605                         byte_hack m = current_floor_ptr->grid_array[ty][tx].cost + 1;
1606                         byte_hack n = current_floor_ptr->grid_array[ty][tx].dist + 1;
1607                         grid_type *g_ptr;
1608
1609                         /* Child location */
1610                         y = ty + ddy_ddd[d];
1611                         x = tx + ddx_ddd[d];
1612
1613                         /* Ignore player's grid */
1614                         if (player_bold(y, x)) continue;
1615
1616                         g_ptr = &current_floor_ptr->grid_array[y][x];
1617
1618                         if (is_closed_door(g_ptr->feat)) m += 3;
1619
1620                         /* Ignore "pre-stamped" entries */
1621                         if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
1622
1623                         /* Ignore "walls" and "rubble" */
1624                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1625
1626                         /* Save the flow cost */
1627                         if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
1628                         if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
1629
1630                         /* Hack -- limit flow depth */
1631                         if (n == MONSTER_FLOW_DEPTH) continue;
1632
1633                         /* Enqueue that entry */
1634                         tmp_pos.y[flow_head] = y;
1635                         tmp_pos.x[flow_head] = x;
1636
1637                         /* Advance the queue */
1638                         if (++flow_head == TEMP_MAX) flow_head = 0;
1639
1640                         /* Hack -- notice overflow by forgetting new entry */
1641                         if (flow_head == flow_tail) flow_head = old_head;
1642                 }
1643         }
1644 }
1645
1646
1647 static int scent_when = 0;
1648
1649 /*
1650  * Characters leave scent trails for perceptive monsters to track.
1651  *
1652  * Smell is rather more limited than sound.  Many creatures cannot use
1653  * it at all, it doesn't extend very far outwards from the character's
1654  * current position, and monsters can use it to home in the character,
1655  * but not to run away from him.
1656  *
1657  * Smell is valued according to age.  When a character takes his current_world_ptr->game_turn,
1658  * scent is aged by one, and new scent of the current age is laid down.
1659  * Speedy characters leave more scent, true, but it also ages faster,
1660  * which makes it harder to hunt them down.
1661  *
1662  * Whenever the age count loops, most of the scent trail is erased and
1663  * the age of the remainder is recalculated.
1664  */
1665 void update_smell(void)
1666 {
1667         POSITION i, j;
1668         POSITION y, x;
1669
1670         /* Create a table that controls the spread of scent */
1671         const int scent_adjust[5][5] =
1672         {
1673                 { -1, 0, 0, 0,-1 },
1674                 {  0, 1, 1, 1, 0 },
1675                 {  0, 1, 2, 1, 0 },
1676                 {  0, 1, 1, 1, 0 },
1677                 { -1, 0, 0, 0,-1 },
1678         };
1679
1680         /* Loop the age and adjust scent values when necessary */
1681         if (++scent_when == 254)
1682         {
1683                 /* Scan the entire dungeon */
1684                 for (y = 0; y < current_floor_ptr->height; y++)
1685                 {
1686                         for (x = 0; x < current_floor_ptr->width; x++)
1687                         {
1688                                 int w = current_floor_ptr->grid_array[y][x].when;
1689                                 current_floor_ptr->grid_array[y][x].when = (w > 128) ? (w - 128) : 0;
1690                         }
1691                 }
1692
1693                 /* Restart */
1694                 scent_when = 126;
1695         }
1696
1697
1698         /* Lay down new scent */
1699         for (i = 0; i < 5; i++)
1700         {
1701                 for (j = 0; j < 5; j++)
1702                 {
1703                         grid_type *g_ptr;
1704
1705                         /* Translate table to map grids */
1706                         y = i + p_ptr->y - 2;
1707                         x = j + p_ptr->x - 2;
1708
1709                         /* Check Bounds */
1710                         if (!in_bounds(y, x)) continue;
1711
1712                         g_ptr = &current_floor_ptr->grid_array[y][x];
1713
1714                         /* Walls, water, and lava cannot hold scent. */
1715                         if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
1716
1717                         /* Grid must not be blocked by walls from the character */
1718                         if (!player_has_los_bold(y, x)) continue;
1719
1720                         /* Note grids that are too far away */
1721                         if (scent_adjust[i][j] == -1) continue;
1722
1723                         /* Mark the grid with new scent */
1724                         g_ptr->when = scent_when + scent_adjust[i][j];
1725                 }
1726         }
1727 }
1728
1729
1730
1731 /*
1732  * Change the "feat" flag for a grid, and notice/redraw the grid
1733  */
1734 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
1735 {
1736         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1737         feature_type *f_ptr = &f_info[feat];
1738         bool old_los, old_mirror;
1739
1740         if (!character_dungeon)
1741         {
1742                 /* Clear mimic type */
1743                 g_ptr->mimic = 0;
1744
1745                 /* Change the feature */
1746                 g_ptr->feat = feat;
1747
1748                 /* Hack -- glow the GLOW terrain */
1749                 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1750                 {
1751                         DIRECTION i;
1752                         POSITION yy, xx;
1753
1754                         for (i = 0; i < 9; i++)
1755                         {
1756                                 yy = y + ddy_ddd[i];
1757                                 xx = x + ddx_ddd[i];
1758                                 if (!in_bounds2(yy, xx)) continue;
1759                                 current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
1760                         }
1761                 }
1762
1763                 return;
1764         }
1765
1766         old_los = cave_have_flag_bold(y, x, FF_LOS);
1767         old_mirror = is_mirror_grid(g_ptr);
1768
1769         /* Clear mimic type */
1770         g_ptr->mimic = 0;
1771
1772         /* Change the feature */
1773         g_ptr->feat = feat;
1774
1775         /* Remove flag for mirror/glyph */
1776         g_ptr->info &= ~(CAVE_OBJECT);
1777
1778         if (old_mirror && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1779         {
1780                 g_ptr->info &= ~(CAVE_GLOW);
1781                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1782
1783                 update_local_illumination(y, x);
1784         }
1785
1786         /* Check for change to boring grid */
1787         if (!have_flag(f_ptr->flags, FF_REMEMBER)) g_ptr->info &= ~(CAVE_MARK);
1788         if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1789
1790         note_spot(y, x);
1791         lite_spot(y, x);
1792
1793         /* Check if los has changed */
1794         if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
1795         {
1796
1797 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
1798
1799                 update_local_illumination(y, x);
1800
1801 #endif /* COMPLEX_WALL_ILLUMINATION */
1802
1803                 /* Update the visuals */
1804                 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
1805         }
1806
1807         /* Hack -- glow the GLOW terrain */
1808         if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
1809         {
1810                 DIRECTION i;
1811                 POSITION yy, xx;
1812                 grid_type *cc_ptr;
1813
1814                 for (i = 0; i < 9; i++)
1815                 {
1816                         yy = y + ddy_ddd[i];
1817                         xx = x + ddx_ddd[i];
1818                         if (!in_bounds2(yy, xx)) continue;
1819                         cc_ptr = &current_floor_ptr->grid_array[yy][xx];
1820                         cc_ptr->info |= CAVE_GLOW;
1821
1822                         if (player_has_los_grid(cc_ptr))
1823                         {
1824                                 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
1825
1826                                 note_spot(yy, xx);
1827
1828                                 lite_spot(yy, xx);
1829                         }
1830
1831                         update_local_illumination(yy, xx);
1832                 }
1833
1834                 if (p_ptr->special_defense & NINJA_S_STEALTH)
1835                 {
1836                         if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
1837                 }
1838         }
1839 }
1840
1841
1842 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
1843 {
1844         feature_type *f_ptr = &f_info[newfeat];
1845
1846         if (have_flag(f_ptr->flags, FF_CONVERT))
1847         {
1848                 switch (f_ptr->subtype)
1849                 {
1850                 case CONVERT_TYPE_FLOOR:
1851                         return feat_ground_type[randint0(100)];
1852                 case CONVERT_TYPE_WALL:
1853                         return feat_wall_type[randint0(100)];
1854                 case CONVERT_TYPE_INNER:
1855                         return feat_wall_inner;
1856                 case CONVERT_TYPE_OUTER:
1857                         return feat_wall_outer;
1858                 case CONVERT_TYPE_SOLID:
1859                         return feat_wall_solid;
1860                 case CONVERT_TYPE_STREAM1:
1861                         return d_info[p_ptr->dungeon_idx].stream1;
1862                 case CONVERT_TYPE_STREAM2:
1863                         return d_info[p_ptr->dungeon_idx].stream2;
1864                 default:
1865                         return newfeat;
1866                 }
1867         }
1868         else return newfeat;
1869 }
1870
1871
1872 /*
1873  * Take a feature, determine what that feature becomes
1874  * through applying the given action.
1875  */
1876 FEAT_IDX feat_state(FEAT_IDX feat, int action)
1877 {
1878         feature_type *f_ptr = &f_info[feat];
1879         int i;
1880
1881         /* Get the new feature */
1882         for (i = 0; i < MAX_FEAT_STATES; i++)
1883         {
1884                 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
1885         }
1886
1887         if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
1888
1889         return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
1890 }
1891
1892 /*
1893  * Takes a location and action and changes the feature at that
1894  * location through applying the given action.
1895  */
1896 void cave_alter_feat(POSITION y, POSITION x, int action)
1897 {
1898         /* Set old feature */
1899         FEAT_IDX oldfeat = current_floor_ptr->grid_array[y][x].feat;
1900
1901         /* Get the new feat */
1902         FEAT_IDX newfeat = feat_state(oldfeat, action);
1903
1904         /* No change */
1905         if (newfeat == oldfeat) return;
1906
1907         /* Set the new feature */
1908         cave_set_feat(y, x, newfeat);
1909
1910         if (!(feature_action_flags[action] & FAF_NO_DROP))
1911         {
1912                 feature_type *old_f_ptr = &f_info[oldfeat];
1913                 feature_type *f_ptr = &f_info[newfeat];
1914                 bool found = FALSE;
1915
1916                 /* Handle gold */
1917                 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
1918                 {
1919                         /* Place some gold */
1920                         place_gold(y, x);
1921                         found = TRUE;
1922                 }
1923
1924                 /* Handle item */
1925                 if (have_flag(old_f_ptr->flags, FF_HAS_ITEM) && !have_flag(f_ptr->flags, FF_HAS_ITEM) && (randint0(100) < (15 - current_floor_ptr->dun_level / 2)))
1926                 {
1927                         /* Place object */
1928                         place_object(y, x, 0L);
1929                         found = TRUE;
1930                 }
1931
1932                 if (found && character_dungeon && player_can_see_bold(y, x))
1933                 {
1934                         msg_print(_("何かを発見した!", "You have found something!"));
1935                 }
1936         }
1937
1938         if (feature_action_flags[action] & FAF_CRASH_GLASS)
1939         {
1940                 feature_type *old_f_ptr = &f_info[oldfeat];
1941
1942                 if (have_flag(old_f_ptr->flags, FF_GLASS) && character_dungeon)
1943                 {
1944                         project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
1945                                 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1946                 }
1947         }
1948 }
1949
1950
1951 /* Remove a mirror */
1952 void remove_mirror(POSITION y, POSITION x)
1953 {
1954         grid_type *g_ptr = &current_floor_ptr->grid_array[y][x];
1955
1956         /* Remove the mirror */
1957         g_ptr->info &= ~(CAVE_OBJECT);
1958         g_ptr->mimic = 0;
1959
1960         if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
1961         {
1962                 g_ptr->info &= ~(CAVE_GLOW);
1963                 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
1964                 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
1965
1966                 update_local_illumination(y, x);
1967         }
1968
1969         note_spot(y, x);
1970
1971         lite_spot(y, x);
1972 }
1973
1974
1975 /*
1976  *  Return TRUE if there is a mirror on the grid.
1977  */
1978 bool is_mirror_grid(grid_type *g_ptr)
1979 {
1980         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
1981                 return TRUE;
1982         else
1983                 return FALSE;
1984 }
1985
1986
1987 /*
1988  *  Return TRUE if there is a mirror on the grid.
1989  */
1990 bool is_glyph_grid(grid_type *g_ptr)
1991 {
1992         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
1993                 return TRUE;
1994         else
1995                 return FALSE;
1996 }
1997
1998
1999 /*
2000  *  Return TRUE if there is a mirror on the grid.
2001  */
2002 bool is_explosive_rune_grid(grid_type *g_ptr)
2003 {
2004         if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
2005                 return TRUE;
2006         else
2007                 return FALSE;
2008 }
2009
2010 /*!
2011 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
2012 * @param m_idx モンスターID
2013 * @param y 移動先Y座標
2014 * @param x 移動先X座標
2015 * @param mode オプション
2016 * @return テレポート先として妥当ならばtrue
2017 */
2018 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
2019 {
2020         monster_type *m_ptr = &current_floor_ptr->m_list[m_idx];
2021         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
2022         feature_type *f_ptr = &f_info[g_ptr->feat];
2023
2024         /* Require "teleportable" space */
2025         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
2026
2027         if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
2028         if (player_bold(y, x)) return FALSE;
2029
2030         /* Hack -- no teleport onto glyph of warding */
2031         if (is_glyph_grid(g_ptr)) return FALSE;
2032         if (is_explosive_rune_grid(g_ptr)) return FALSE;
2033
2034         if (!(mode & TELEPORT_PASSIVE))
2035         {
2036                 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
2037         }
2038
2039         return TRUE;
2040 }
2041
2042 /*!
2043 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
2044 * @param y 移動先Y座標
2045 * @param x 移動先X座標
2046 * @param mode オプション
2047 * @return テレポート先として妥当ならばtrue
2048 */
2049 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
2050 {
2051         grid_type    *g_ptr = &current_floor_ptr->grid_array[y][x];
2052         feature_type *f_ptr = &f_info[g_ptr->feat];
2053
2054         /* Require "teleportable" space */
2055         if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
2056
2057         /* No magical teleporting into vaults and such */
2058         if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
2059
2060         if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
2061
2062         /* don't teleport on a trap. */
2063         if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
2064
2065         if (!(mode & TELEPORT_PASSIVE))
2066         {
2067                 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
2068
2069                 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
2070                 {
2071                         if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
2072                 }
2073
2074                 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
2075                 {
2076                         /* Always forbid deep lava */
2077                         if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
2078
2079                         /* Forbid shallow lava when the player don't have levitation */
2080                         if (!p_ptr->levitation) return FALSE;
2081                 }
2082
2083         }
2084
2085         return TRUE;
2086 }
2087
2088 /*!
2089  * @brief 地形は開くものであって、かつ開かれているかを返す /
2090  * Attempt to open the given chest at the given location
2091  * @param feat 地形ID
2092  * @return 開いた地形である場合TRUEを返す /  Return TRUE if the given feature is an open door
2093  */
2094 bool is_open(FEAT_IDX feat)
2095 {
2096         return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
2097 }
2098
2099 /*!
2100  * @brief プレイヤーが地形踏破可能かを返す
2101  * @param feature 判定したい地形ID
2102  * @param mode 移動に関するオプションフラグ
2103  * @return 移動可能ならばTRUEを返す
2104  */
2105 bool player_can_enter(FEAT_IDX feature, BIT_FLAGS16 mode)
2106 {
2107         feature_type *f_ptr = &f_info[feature];
2108
2109         if (p_ptr->riding) return monster_can_cross_terrain(feature, &r_info[current_floor_ptr->m_list[p_ptr->riding].r_idx], mode | CEM_RIDING);
2110
2111         if (have_flag(f_ptr->flags, FF_PATTERN))
2112         {
2113                 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
2114         }
2115
2116         if (have_flag(f_ptr->flags, FF_CAN_FLY) && p_ptr->levitation) return TRUE;
2117         if (have_flag(f_ptr->flags, FF_CAN_SWIM) && p_ptr->can_swim) return TRUE;
2118         if (have_flag(f_ptr->flags, FF_CAN_PASS) && p_ptr->pass_wall) return TRUE;
2119
2120         if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;
2121
2122         return TRUE;
2123 }
2124