OSDN Git Service

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