4 * @brief グリッドの実装 / low level dungeon routines -BEN-
7 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\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
13 * Support for Adam Bolt's tileset, lighting and transparency effects\n
14 * by Robert Ruehlmann (rr9@angband.org)\n
16 * 2013 Deskull Doxygen向けのコメント整理\n
23 #include "object-flavor.h"
24 #include "object-hook.h"
32 #include "monster-status.h"
33 #include "player-status.h"
35 #include "view-mainwindow.h"
38 * @brief 新規フロアに入りたてのプレイヤーをランダムな場所に配置する / Returns random co-ordinates for player/monster/object
39 * @return 配置に成功したらTRUEを返す
41 bool new_player_spot(void)
43 POSITION y = 0, x = 0;
44 int max_attempts = 10000;
49 /* Place the player */
50 while (max_attempts--)
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);
56 g_ptr = ¤t_floor_ptr->grid_array[y][x];
58 /* Must be a "naked" floor grid */
59 if (g_ptr->m_idx) continue;
60 if (current_floor_ptr->dun_level)
62 f_ptr = &f_info[g_ptr->feat];
64 if (max_attempts > 5000) /* Rule 1 */
66 if (!have_flag(f_ptr->flags, FF_FLOOR)) continue;
70 if (!have_flag(f_ptr->flags, FF_MOVE)) continue;
71 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) continue;
74 /* Refuse to start on anti-teleport grids in dungeon */
75 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) continue;
77 if (!player_can_enter(g_ptr->feat, 0)) continue;
78 if (!in_bounds(y, x)) continue;
80 /* Refuse to start on anti-teleport grids */
81 if (g_ptr->info & (CAVE_ICKY)) continue;
86 if (max_attempts < 1) /* Should be -1, actually if we failed... */
89 /* Save the new player grid */
99 * @brief 所定の位置に上り階段か下り階段を配置する / Place an up/down staircase at given location
100 * @param y 配置を試みたいマスのY座標
101 * @param x 配置を試みたいマスのX座標
104 void place_random_stairs(POSITION y, POSITION x)
106 bool up_stairs = TRUE;
107 bool down_stairs = TRUE;
109 g_ptr = ¤t_floor_ptr->grid_array[y][x];
110 if (!is_floor_grid(g_ptr) || g_ptr->o_idx) return;
113 if (!current_floor_ptr->dun_level) up_stairs = FALSE;
116 if (ironman_downward) up_stairs = FALSE;
119 if (current_floor_ptr->dun_level >= d_info[p_ptr->dungeon_idx].maxdepth) down_stairs = FALSE;
122 if (quest_number(current_floor_ptr->dun_level) && (current_floor_ptr->dun_level > 1)) down_stairs = FALSE;
124 /* We can't place both */
125 if (down_stairs && up_stairs)
127 /* Choose a staircase randomly */
128 if (randint0(100) < 50) up_stairs = FALSE;
129 else down_stairs = FALSE;
132 /* Place the stairs */
133 if (up_stairs) place_up_stairs(y, x);
134 else if (down_stairs) place_down_stairs(y, x);
138 * @brief 所定の位置にさまざまな状態や種類のドアを配置する / Place a random type of door at the given location
139 * @param y ドアの配置を試みたいマスのY座標
140 * @param x ドアの配置を試みたいマスのX座標
141 * @param room 部屋に接している場合向けのドア生成か否か
144 void place_random_door(POSITION y, POSITION x, bool room)
147 FEAT_IDX feat = feat_none;
148 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
150 /* Initialize mimic info */
153 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
155 place_floor_bold(y, x);
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);
163 /* Choose an object */
164 tmp = randint0(1000);
166 /* Open doors (300/1000) */
169 /* Create open door */
170 feat = feat_door[type].open;
173 /* Broken doors (100/1000) */
176 /* Create broken door */
177 feat = feat_door[type].broken;
180 /* Secret doors (200/1000) */
183 /* Create secret door */
184 place_closed_door(y, x, type);
186 if (type != DOOR_CURTAIN)
188 /* Hide. If on the edge of room, use outer wall. */
189 g_ptr->mimic = room ? feat_wall_outer : feat_wall_type[randint0(100)];
191 /* Floor type terrain cannot hide a door */
192 if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
194 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
196 g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
203 /* Closed, locked, or stuck doors (400/1000) */
204 else place_closed_door(y, x, type);
208 if (feat != feat_none)
210 set_cave_feat(y, x, feat);
214 place_floor_bold(y, x);
218 delete_monster(y, x);
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
228 void place_closed_door(POSITION y, POSITION x, int type)
231 FEAT_IDX feat = feat_none;
233 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
235 place_floor_bold(y, x);
239 /* Choose an object */
242 /* Closed doors (300/400) */
245 /* Create closed door */
246 feat = feat_door[type].closed;
249 /* Locked doors (99/400) */
252 /* Create locked door */
253 feat = feat_locked_door_random(type);
256 /* Stuck doors (1/400) */
259 /* Create jammed door */
260 feat = feat_jammed_door_random(type);
263 if (feat != feat_none)
265 cave_set_feat(y, x, feat);
267 /* Now it is not floor */
268 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_MASK);
272 place_floor_bold(y, x);
277 * @brief 鍵のかかったドアを配置する
278 * @param y 配置したいフロアのY座標
279 * @param x 配置したいフロアのX座標
282 void place_locked_door(POSITION y, POSITION x)
284 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
286 place_floor_bold(y, x);
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);
299 * @param y 配置したいフロアのY座標
300 * @param x 配置したいフロアのX座標
301 * @param type DOOR_DEFAULT / DOOR_DOOR / DOOR_GLASS_DOOR / DOOR_CURTAIN のいずれか
304 void place_secret_door(POSITION y, POSITION x, int type)
306 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_DOORS)
308 place_floor_bold(y, x);
312 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
314 if (type == DOOR_DEFAULT)
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);
321 /* Create secret door */
322 place_closed_door(y, x, type);
324 if (type != DOOR_CURTAIN)
326 /* Hide by inner wall because this is used in rooms only */
327 g_ptr->mimic = feat_wall_inner;
329 /* Floor type terrain cannot hide a door */
330 if (feat_supports_los(g_ptr->mimic) && !feat_supports_los(g_ptr->feat))
332 if (have_flag(f_info[g_ptr->mimic].flags, FF_MOVE) || have_flag(f_info[g_ptr->mimic].flags, FF_CAN_FLY))
334 g_ptr->feat = one_in_(2) ? g_ptr->mimic : feat_ground_type[randint0(100)];
340 g_ptr->info &= ~(CAVE_FLOOR);
341 delete_monster(y, x);
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.
349 * The doors must be INSIDE the allocated region.
351 void add_door(POSITION x, POSITION y)
353 /* Need to have a wall in the center square */
354 if (!is_outer_bold(y, x)) return;
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)))
369 place_secret_door(y, x, DOOR_DEFAULT);
371 /* set boundarys so don't get wide doors */
372 place_solid_bold(y, x - 1);
373 place_solid_bold(y, x + 1);
382 * where x = don't care
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))
389 place_secret_door(y, x, DOOR_DEFAULT);
391 /* set boundarys so don't get wide doors */
392 place_solid_bold(y - 1, x);
393 place_solid_bold(y + 1, x);
398 * @brief 隣接4マスに存在する通路の数を返す / Count the number of "corridor" grids adjacent to the given grid.
399 * @param y1 基準となるマスのY座標
400 * @param x1 基準となるマスのX座標
402 * @note Assumes "in_bounds(y1, x1)"
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.
408 static int next_to_corr(POSITION y1, POSITION x1)
415 /* Scan adjacent grids */
416 for (i = 0; i < 4; i++)
418 /* Extract the location */
421 g_ptr = ¤t_floor_ptr->grid_array[y][x];
423 /* Skip non floors */
424 if (cave_have_flag_grid(g_ptr, FF_WALL)) continue;
426 /* Skip non "empty floor" grids */
427 if (!is_floor_grid(g_ptr))
430 /* Skip grids inside rooms */
431 if (g_ptr->info & (CAVE_ROOM)) continue;
433 /* Count these grids */
437 /* Return the number of corridors */
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)"
449 * Assumes "in_bounds(y, x)"\n
451 static bool possible_doorway(POSITION y, POSITION x)
453 /* Count the adjacent corridors */
454 if (next_to_corr(y, x) >= 2)
457 if (cave_have_flag_bold(y - 1, x, FF_WALL) &&
458 cave_have_flag_bold(y + 1, x, FF_WALL))
463 /* Check Horizontal */
464 if (cave_have_flag_bold(y, x - 1, FF_WALL) &&
465 cave_have_flag_bold(y, x + 1, FF_WALL))
476 * @brief ドアの設置を試みる / Places door at y, x position if at least 2 walls found
477 * @param y 設置を行いたいマスのY座標
478 * @param x 設置を行いたいマスのX座標
481 void try_door(POSITION y, POSITION x)
482 { if (!in_bounds(y, x)) return;
485 if (cave_have_flag_bold(y, x, FF_WALL)) return;
487 /* Ignore room grids */
488 if (current_floor_ptr->grid_array[y][x].info & (CAVE_ROOM)) return;
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))
494 place_random_door(y, x, FALSE);
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)
508 void place_floor(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
512 /* Place a full floor under the room */
513 for (y = y1 - 1; y <= y2 + 1; y++)
515 for (x = x1 - 1; x <= x2 + 1; x++)
517 place_floor_bold(y, x);
518 add_cave_info(y, x, CAVE_ROOM);
519 if (light) add_cave_info(y, x, CAVE_GLOW);
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)
534 void place_room(POSITION x1, POSITION x2, POSITION y1, POSITION y2, bool light)
538 place_floor(x1, x2, y1, y2, light);
540 /* Walls around the room */
541 for (y = y1 - 1; y <= y2 + 1; y++)
543 place_outer_bold(y, x1 - 1);
544 place_outer_bold(y, x2 + 1);
546 for (x = x1 - 1; x <= x2 + 1; x++)
548 place_outer_bold(y1 - 1, x);
549 place_outer_bold(y2 + 1, x);
555 * @brief 特殊な部屋向けに各種アイテムを配置する / Create up to "num" objects near the given coordinates
556 * @param y 配置したい中心マスのY座標
557 * @param x 配置したい中心マスのX座標
561 * Only really called by some of the "vault" routines.
563 void vault_objects(POSITION y, POSITION x, int num)
566 int i = 0, j = y, k = x;
571 /* Attempt to place 'num' objects */
572 for (; num > 0; --num)
574 /* Try up to 11 spots looking for empty space */
575 for (i = 0; i < 11; ++i)
577 /* Pick a random location */
578 while (dummy < SAFE_MAX_ATTEMPTS)
580 j = rand_spread(y, 2);
581 k = rand_spread(x, 3);
583 if (!in_bounds(j, k)) continue;
587 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
589 msg_print(_("警告!地下室のアイテムを配置できません!", "Warning! Could not place vault object!"));
592 /* Require "clean" floor space */
593 g_ptr = ¤t_floor_ptr->grid_array[j][k];
594 if (!is_floor_grid(g_ptr) || g_ptr->o_idx) continue;
596 if (randint0(100) < 75)
598 place_object(j, k, 0L);
605 /* Placement accomplished */
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方向の配置分散マス数
619 * Only really called by some of the "vault" routines.
621 void vault_trap_aux(POSITION y, POSITION x, POSITION yd, POSITION xd)
623 int count = 0, y1 = y, x1 = x;
629 for (count = 0; count <= 5; count++)
632 while (dummy < SAFE_MAX_ATTEMPTS)
634 y1 = rand_spread(y, yd);
635 x1 = rand_spread(x, xd);
637 if (!in_bounds(y1, x1)) continue;
641 if (dummy >= SAFE_MAX_ATTEMPTS && cheat_room)
643 msg_print(_("警告!地下室のトラップを配置できません!", "Warning! Could not place vault trap!"));
646 /* Require "naked" floor grids */
647 g_ptr = ¤t_floor_ptr->grid_array[y1][x1];
648 if (!is_floor_grid(g_ptr) || g_ptr->o_idx || g_ptr->m_idx) continue;
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 配置したいトラップの数
666 * Only really called by some of the "vault" routines.
668 void vault_traps(POSITION y, POSITION x, POSITION yd, POSITION xd, int num)
672 for (i = 0; i < num; i++)
674 vault_trap_aux(y, x, yd, xd);
679 * @brief 特殊な部屋地形向けにモンスターを配置する / Hack -- Place some sleeping monsters near the given location
680 * @param y1 モンスターを配置したいマスの中心Y座標
681 * @param x1 モンスターを配置したいマスの中心X座標
682 * @param num 配置したいモンスターの数
685 * Only really called by some of the "vault" routines.
687 void vault_monsters(POSITION y1, POSITION x1, int num)
693 /* Try to summon "num" monsters "near" the given location */
694 for (k = 0; k < num; k++)
696 /* Try nine locations */
697 for (i = 0; i < 9; i++)
701 /* Pick a nearby location */
702 scatter(&y, &x, y1, x1, d, 0);
704 /* Require "empty" floor grids */
705 g_ptr = ¤t_floor_ptr->grid_array[y][x];
706 if (!cave_empty_grid(g_ptr)) continue;
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;
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
722 bool get_is_floor(POSITION x, POSITION y)
724 if (!in_bounds(y, x))
730 /* Do the real check */
731 if (is_floor_bold(y, x)) return (TRUE);
737 * @brief 指定のマスを床地形に変える / Set a square to be floor. (Includes range checking.)
738 * @param x 地形を変えたいマスのX座標
739 * @param y 地形を変えたいマスのY座標
742 void set_floor(POSITION x, POSITION y)
744 if (!in_bounds(y, x))
750 if (current_floor_ptr->grid_array[y][x].info & CAVE_ROOM)
752 /* A room border don't touch. */
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);
762 * @brief マスにフロア端用の永久壁を配置する / Set boundary mimic and add "solid" perma-wall
763 * @param g_ptr 永久壁を配置したいマス構造体の参照ポインタ
766 void place_bound_perm_wall(grid_type *g_ptr)
768 if (bound_walls_perm)
770 /* Clear boundary mimic */
775 feature_type *f_ptr = &f_info[g_ptr->feat];
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);
782 /* Set boundary mimic */
783 g_ptr->mimic = g_ptr->feat;
786 /* Add "solid" perma-wall */
787 place_solid_perm_grid(g_ptr);
791 * @brief マスに看破済みの罠があるかの判定を行う。 / Return TRUE if the given grid is a known trap
792 * @param g_ptr マス構造体の参照ポインタ
793 * @return 看破済みの罠があるならTRUEを返す。
795 bool is_known_trap(grid_type *g_ptr)
797 if (!g_ptr->mimic && !cave_have_flag_grid(g_ptr, FF_SECRET) &&
798 is_trap(g_ptr->feat)) return TRUE;
806 * @brief マスに隠されたドアがあるかの判定を行う。 / Return TRUE if the given grid is a hidden closed door
807 * @param g_ptr マス構造体の参照ポインタ
808 * @return 隠されたドアがあるならTRUEを返す。
810 bool is_hidden_door(grid_type *g_ptr)
812 if ((g_ptr->mimic || cave_have_flag_grid(g_ptr, FF_SECRET)) &&
813 is_closed_door(g_ptr->feat))
819 #define COMPLEX_WALL_ILLUMINATION /*!< 照明状態を壁により影響を受ける、より複雑な判定に切り替えるマクロ */
823 * @brief 指定された座標のマスが現在照らされているかを返す。 / Check for "local" illumination
826 * @return 指定された座標に照明がかかっているならTRUEを返す。。
828 bool check_local_illumination(POSITION y, POSITION x)
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;
834 /* Check for "local" illumination */
836 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
838 /* Check for "complex" illumination */
839 if ((feat_supports_los(get_feat_mimic(¤t_floor_ptr->grid_array[yy][xx])) &&
840 (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW)) ||
841 (feat_supports_los(get_feat_mimic(¤t_floor_ptr->grid_array[y][xx])) &&
842 (current_floor_ptr->grid_array[y][xx].info & CAVE_GLOW)) ||
843 (feat_supports_los(get_feat_mimic(¤t_floor_ptr->grid_array[yy][x])) &&
844 (current_floor_ptr->grid_array[yy][x].info & CAVE_GLOW)))
850 #else /* COMPLEX_WALL_ILLUMINATION */
852 /* Check for "simple" illumination */
853 return (current_floor_ptr->grid_array[yy][xx].info & CAVE_GLOW) ? TRUE : FALSE;
855 #endif /* COMPLEX_WALL_ILLUMINATION */
859 /*! 対象座標のマスの照明状態を更新する際の補助処理マクロ */
860 #define update_local_illumination_aux(Y, X) \
862 if (player_has_los_bold((Y), (X))) \
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); \
867 /* Notice and redraw */ \
868 note_spot((Y), (X)); \
869 lite_spot((Y), (X)); \
874 * @brief 指定された座標の照明状態を更新する / Update "local" illumination
879 void update_local_illumination(POSITION y, POSITION x)
884 if (!in_bounds(y, x)) return;
886 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
888 if ((y != p_ptr->y) && (x != p_ptr->x))
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);
896 else if (x != p_ptr->x) /* y == p_ptr->y */
898 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
899 for (i = -1; i <= 1; i++)
902 update_local_illumination_aux(yy, xx);
905 update_local_illumination_aux(yy, x);
907 update_local_illumination_aux(yy, x);
909 else if (y != p_ptr->y) /* x == p_ptr->x */
911 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
912 for (i = -1; i <= 1; i++)
915 update_local_illumination_aux(yy, xx);
918 update_local_illumination_aux(y, xx);
920 update_local_illumination_aux(y, xx);
922 else /* Player's grid */
924 for (i = 0; i < 8; i++)
928 update_local_illumination_aux(yy, xx);
932 #else /* COMPLEX_WALL_ILLUMINATION */
934 if ((y != p_ptr->y) && (x != p_ptr->x))
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);
940 else if (x != p_ptr->x) /* y == p_ptr->y */
942 xx = (x < p_ptr->x) ? (x - 1) : (x + 1);
943 for (i = -1; i <= 1; i++)
946 update_local_illumination_aux(yy, xx);
949 else if (y != p_ptr->y) /* x == p_ptr->x */
951 yy = (y < p_ptr->y) ? (y - 1) : (y + 1);
952 for (i = -1; i <= 1; i++)
955 update_local_illumination_aux(yy, xx);
958 else /* Player's grid */
960 for (i = 0; i < 8; i++)
964 update_local_illumination_aux(yy, xx);
968 #endif /* COMPLEX_WALL_ILLUMINATION */
973 * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
976 * @return 視覚に収められる状態ならTRUEを返す
978 * He must have vision, illumination, and line of sight.\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
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
990 * This extra check is expensive, but it provides a more "correct" semantics.\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
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
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
1007 bool player_can_see_bold(POSITION y, POSITION x)
1011 /* Blind players see nothing */
1012 if (p_ptr->blind) return FALSE;
1014 g_ptr = ¤t_floor_ptr->grid_array[y][x];
1016 /* Note that "torch-lite" yields "illumination" */
1017 if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) return TRUE;
1019 /* Require line of sight to the grid */
1020 if (!player_has_los_bold(y, x)) return FALSE;
1022 /* Noctovision of Ninja */
1023 if (p_ptr->see_nocto) return TRUE;
1025 /* Require "perma-lite" of the grid */
1026 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) return FALSE;
1028 /* Feature code (applying "mimic" field) */
1029 /* Floors are simple */
1030 if (feat_supports_los(get_feat_mimic(g_ptr))) return TRUE;
1032 /* Check for "local" illumination */
1033 return check_local_illumination(y, x);
1037 * @brief 指定された座標をプレイヤー収められていない状態かどうか / Returns true if the player's grid is dark
1038 * @return 視覚に収められていないならTRUEを返す
1039 * @details player_can_see_bold()関数の返り値の否定を返している。
1043 return (!player_can_see_bold(p_ptr->y, p_ptr->x));
1048 * @brief 指定された座標が地震や階段生成の対象となるマスかを返す。 / Determine if a given location may be "destroyed"
1051 * @return 各種の変更が可能ならTRUEを返す。
1053 * 条件は永久地形でなく、なおかつ該当のマスにアーティファクトが存在しないか、である。英語の旧コメントに反して*破壊*の抑止判定には現在使われていない。
1055 bool cave_valid_bold(POSITION y, POSITION x)
1057 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
1058 OBJECT_IDX this_o_idx, next_o_idx = 0;
1060 /* Forbid perma-grids */
1061 if (cave_perma_grid(g_ptr)) return (FALSE);
1064 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1067 o_ptr = ¤t_floor_ptr->o_list[this_o_idx];
1068 next_o_idx = o_ptr->next_o_idx;
1070 /* Forbid artifact grids */
1071 if (object_is_artifact(o_ptr)) return (FALSE);
1079 * Moves the cursor to a given MAP (y,x) location
1081 void move_cursor_relative(int row, int col)
1083 /* Real co-ords convert to screen positions */
1084 row -= panel_row_prt;
1087 Term_gotoxy(panel_col_of(col), row);
1093 * Place an attr/char pair at the given map coordinate, if legal.
1095 void print_rel(SYMBOL_CODE c, TERM_COLOR a, TERM_LEN y, TERM_LEN x)
1097 /* Only do "legal" locations */
1098 if (panel_contains(y, x))
1100 /* Hack -- fake monochrome */
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;
1108 /* Draw the char using the attr */
1109 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, 0, 0);
1118 * Memorize interesting viewable object/features in the given grid
1120 * This function should only be called on "legal" grids.
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).
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()".
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.
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
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.
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.
1156 void note_spot(POSITION y, POSITION x)
1158 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
1159 OBJECT_IDX this_o_idx, next_o_idx = 0;
1161 /* Blind players see nothing */
1162 if (p_ptr->blind) return;
1164 /* Analyze non-torch-lit grids */
1165 if (!(g_ptr->info & (CAVE_LITE | CAVE_MNLT)))
1167 /* Require line of sight to the grid */
1168 if (!(g_ptr->info & (CAVE_VIEW))) return;
1170 /* Require "perma-lite" of the grid */
1171 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
1174 if (!p_ptr->see_nocto) return;
1179 /* Hack -- memorize objects */
1180 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1182 object_type *o_ptr = ¤t_floor_ptr->o_list[this_o_idx];
1183 next_o_idx = o_ptr->next_o_idx;
1185 /* Memorize objects */
1186 o_ptr->marked |= OM_FOUND;
1190 /* Hack -- memorize grids */
1191 if (!(g_ptr->info & (CAVE_MARK)))
1193 /* Feature code (applying "mimic" field) */
1194 feature_type *f_ptr = &f_info[get_feat_mimic(g_ptr)];
1196 /* Memorize some "boring" grids */
1197 if (!have_flag(f_ptr->flags, FF_REMEMBER))
1199 /* Option -- memorize all torch-lit floors */
1200 if (view_torch_grids &&
1201 ((g_ptr->info & (CAVE_LITE | CAVE_MNLT)) || p_ptr->see_nocto))
1203 g_ptr->info |= (CAVE_MARK);
1206 /* Option -- memorize all perma-lit floors */
1207 else if (view_perma_grids && ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) == CAVE_GLOW))
1209 g_ptr->info |= (CAVE_MARK);
1213 /* Memorize normal grids */
1214 else if (have_flag(f_ptr->flags, FF_LOS))
1216 g_ptr->info |= (CAVE_MARK);
1219 /* Memorize torch-lit walls */
1220 else if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
1222 g_ptr->info |= (CAVE_MARK);
1225 /* Memorize walls seen by noctovision of Ninja */
1226 else if (p_ptr->see_nocto)
1228 g_ptr->info |= (CAVE_MARK);
1231 /* Memorize certain non-torch-lit wall grids */
1232 else if (check_local_illumination(y, x))
1234 g_ptr->info |= (CAVE_MARK);
1238 /* Memorize terrain of the grid */
1239 g_ptr->info |= (CAVE_KNOWN);
1243 void display_dungeon(void)
1250 SYMBOL_CODE tc = '\0';
1252 for (x = p_ptr->x - Term->wid / 2 + 1; x <= p_ptr->x + Term->wid / 2; x++)
1254 for (y = p_ptr->y - Term->hgt / 2 + 1; y <= p_ptr->y + Term->hgt / 2; y++)
1256 if (in_bounds2(y, x))
1258 map_info(y, x, &a, &c, &ta, &tc);
1260 /* Hack -- fake monochrome */
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;
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);
1273 /* Clear out-of-bound tiles */
1275 /* Access darkness */
1276 feature_type *f_ptr = &f_info[feat_none];
1279 a = f_ptr->x_attr[F_LIT_STANDARD];
1282 c = f_ptr->x_char[F_LIT_STANDARD];
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);
1293 * Redraw (on the screen) a given MAP location
1295 * This function should only be called on "legal" grids
1297 void lite_spot(POSITION y, POSITION x)
1299 /* Redraw if on screen */
1300 if (panel_contains(y, x) && in_bounds2(y, x))
1307 map_info(y, x, &a, &c, &ta, &tc);
1309 /* Hack -- fake monochrome */
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;
1317 /* Hack -- Queue it */
1318 Term_queue_bigchar(panel_col_of(x), y - panel_row_prt, a, c, ta, tc);
1320 /* Update sub-windows */
1321 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1327 * print project path
1329 void prt_path(POSITION y, POSITION x)
1334 byte_hack default_color = TERM_SLATE;
1336 if (!display_path) return;
1337 if (-1 == project_length)
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);
1343 p_ptr->redraw |= (PR_MAP);
1347 for (i = 0; i < path_n; i++)
1349 POSITION ny = GRID_Y(path_g[i]);
1350 POSITION nx = GRID_X(path_g[i]);
1351 grid_type *g_ptr = ¤t_floor_ptr->grid_array[ny][nx];
1353 if (panel_contains(ny, nx))
1355 TERM_COLOR a = default_color;
1358 TERM_COLOR ta = default_color;
1361 if (g_ptr->m_idx && current_floor_ptr->m_list[g_ptr->m_idx].ml)
1363 /* Determine what is there */
1364 map_info(ny, nx, &a, &c, &ta, &tc);
1366 if (!is_ascii_graphics(a))
1368 else if (c == '.' && (a == TERM_WHITE || a == TERM_L_WHITE))
1370 else if (a == default_color)
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;
1383 /* Hack -- Queue it */
1384 Term_queue_bigchar(panel_col_of(nx), ny - panel_row_prt, a, c, ta, tc);
1388 if ((g_ptr->info & CAVE_MARK) && !cave_have_flag_grid(g_ptr, FF_PROJECT)) break;
1391 if (nx == x && ny == y) default_color = TERM_L_DARK;
1396 * Some comments on the grid flags. -BEN-
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.
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.
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.
1427 * These new methods are so efficient that the old nasty code was removed.
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.
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.
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.
1449 * The "update_lite()" function maintains the "CAVE_LITE" flag for each
1450 * grid and maintains an array of all "CAVE_LITE" grids.
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.
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".
1461 * The "update_view()" function maintains the "CAVE_VIEW" flag for each
1462 * grid and maintains an array of all "CAVE_VIEW" grids.
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
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.
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".
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.
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".
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.
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.
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.
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.
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.
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.
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.
1528 * Some comments on the "update_view()" algorithm...
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.
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()".
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.
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.
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).
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.
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()".
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()"...).
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.
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.
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.
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.
1603 * For delayed visual update
1605 #define cave_note_and_redraw_later(C,Y,X) \
1607 (C)->info |= CAVE_NOTE; \
1608 cave_redraw_later((C), (Y), (X)); \
1613 * For delayed visual update
1615 #define cave_redraw_later(C,Y,X) \
1617 if (!((C)->info & CAVE_REDRAW)) \
1619 (C)->info |= CAVE_REDRAW; \
1620 current_floor_ptr->redraw_y[current_floor_ptr->redraw_n] = (Y); \
1621 current_floor_ptr->redraw_x[current_floor_ptr->redraw_n++] = (X); \
1627 * This macro allows us to efficiently add a grid to the "lite" array,
1628 * note that we are never called for illegal grids, or for grids which
1629 * have already been placed into the "lite" array, and we are never
1630 * called when the "lite" array is full.
1632 #define cave_lite_hack(Y,X) \
1634 if (!(current_floor_ptr->grid_array[Y][X].info & (CAVE_LITE))) \
1636 current_floor_ptr->grid_array[Y][X].info |= (CAVE_LITE); \
1637 current_floor_ptr->lite_y[current_floor_ptr->lite_n] = (Y); \
1638 current_floor_ptr->lite_x[current_floor_ptr->lite_n++] = (X); \
1644 * Update the set of grids "illuminated" by the player's lite.
1646 * This routine needs to use the results of "update_view()"
1648 * Note that "blindness" does NOT affect "torch lite". Be careful!
1650 * We optimize most lites (all non-artifact lites) by using "obvious"
1651 * facts about the results of "small" lite radius, and we attempt to
1652 * list the "nearby" grids before the more "distant" ones in the
1653 * array of torch-lit grids.
1655 * We assume that "radius zero" lite is in fact no lite at all.
1657 * Torch Lantern Artifacts
1667 void update_lite(void)
1670 POSITION x, y, min_x, max_x, min_y, max_y;
1671 POSITION p = p_ptr->cur_lite;
1674 /*** Special case ***/
1677 /* Hack -- Player has no lite */
1680 /* Forget the old lite */
1681 /* forget_lite(); Perhaps don't need? */
1683 /* Add it to later visual update */
1684 cave_redraw_later(¤t_floor_ptr->grid_array[p_ptr->y][p_ptr->x], p_ptr->y, p_ptr->x);
1688 /*** Save the old "lite" grids for later ***/
1690 /* Clear them all */
1691 for (i = 0; i < current_floor_ptr->lite_n; i++)
1693 y = current_floor_ptr->lite_y[i];
1694 x = current_floor_ptr->lite_x[i];
1696 /* Mark the grid as not "lite" */
1697 current_floor_ptr->grid_array[y][x].info &= ~(CAVE_LITE);
1699 /* Mark the grid as "seen" */
1700 current_floor_ptr->grid_array[y][x].info |= (CAVE_TEMP);
1702 /* Add it to the "seen" set */
1703 tmp_pos.y[tmp_pos.n] = y;
1704 tmp_pos.x[tmp_pos.n] = x;
1709 current_floor_ptr->lite_n = 0;
1712 /*** Collect the new "lite" grids ***/
1714 /* Radius 1 -- torch radius */
1718 cave_lite_hack(p_ptr->y, p_ptr->x);
1721 cave_lite_hack(p_ptr->y + 1, p_ptr->x);
1722 cave_lite_hack(p_ptr->y - 1, p_ptr->x);
1723 cave_lite_hack(p_ptr->y, p_ptr->x + 1);
1724 cave_lite_hack(p_ptr->y, p_ptr->x - 1);
1726 /* Diagonal grids */
1727 cave_lite_hack(p_ptr->y + 1, p_ptr->x + 1);
1728 cave_lite_hack(p_ptr->y + 1, p_ptr->x - 1);
1729 cave_lite_hack(p_ptr->y - 1, p_ptr->x + 1);
1730 cave_lite_hack(p_ptr->y - 1, p_ptr->x - 1);
1733 /* Radius 2 -- lantern radius */
1736 /* South of the player */
1737 if (cave_los_bold(p_ptr->y + 1, p_ptr->x))
1739 cave_lite_hack(p_ptr->y + 2, p_ptr->x);
1740 cave_lite_hack(p_ptr->y + 2, p_ptr->x + 1);
1741 cave_lite_hack(p_ptr->y + 2, p_ptr->x - 1);
1744 /* North of the player */
1745 if (cave_los_bold(p_ptr->y - 1, p_ptr->x))
1747 cave_lite_hack(p_ptr->y - 2, p_ptr->x);
1748 cave_lite_hack(p_ptr->y - 2, p_ptr->x + 1);
1749 cave_lite_hack(p_ptr->y - 2, p_ptr->x - 1);
1752 /* East of the player */
1753 if (cave_los_bold(p_ptr->y, p_ptr->x + 1))
1755 cave_lite_hack(p_ptr->y, p_ptr->x + 2);
1756 cave_lite_hack(p_ptr->y + 1, p_ptr->x + 2);
1757 cave_lite_hack(p_ptr->y - 1, p_ptr->x + 2);
1760 /* West of the player */
1761 if (cave_los_bold(p_ptr->y, p_ptr->x - 1))
1763 cave_lite_hack(p_ptr->y, p_ptr->x - 2);
1764 cave_lite_hack(p_ptr->y + 1, p_ptr->x - 2);
1765 cave_lite_hack(p_ptr->y - 1, p_ptr->x - 2);
1769 /* Radius 3+ -- artifact radius */
1774 /* Paranoia -- see "LITE_MAX" */
1777 /* South-East of the player */
1778 if (cave_los_bold(p_ptr->y + 1, p_ptr->x + 1))
1780 cave_lite_hack(p_ptr->y + 2, p_ptr->x + 2);
1783 /* South-West of the player */
1784 if (cave_los_bold(p_ptr->y + 1, p_ptr->x - 1))
1786 cave_lite_hack(p_ptr->y + 2, p_ptr->x - 2);
1789 /* North-East of the player */
1790 if (cave_los_bold(p_ptr->y - 1, p_ptr->x + 1))
1792 cave_lite_hack(p_ptr->y - 2, p_ptr->x + 2);
1795 /* North-West of the player */
1796 if (cave_los_bold(p_ptr->y - 1, p_ptr->x - 1))
1798 cave_lite_hack(p_ptr->y - 2, p_ptr->x - 2);
1802 min_y = p_ptr->y - p;
1803 if (min_y < 0) min_y = 0;
1806 max_y = p_ptr->y + p;
1807 if (max_y > current_floor_ptr->height - 1) max_y = current_floor_ptr->height - 1;
1810 min_x = p_ptr->x - p;
1811 if (min_x < 0) min_x = 0;
1814 max_x = p_ptr->x + p;
1815 if (max_x > current_floor_ptr->width - 1) max_x = current_floor_ptr->width - 1;
1817 /* Scan the maximal box */
1818 for (y = min_y; y <= max_y; y++)
1820 for (x = min_x; x <= max_x; x++)
1822 int dy = (p_ptr->y > y) ? (p_ptr->y - y) : (y - p_ptr->y);
1823 int dx = (p_ptr->x > x) ? (p_ptr->x - x) : (x - p_ptr->x);
1825 /* Skip the "central" grids (above) */
1826 if ((dy <= 2) && (dx <= 2)) continue;
1828 /* Hack -- approximate the distance */
1829 d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
1831 /* Skip distant grids */
1832 if (d > p) continue;
1834 /* Viewable, nearby, grids get "torch lit" */
1835 if (current_floor_ptr->grid_array[y][x].info & CAVE_VIEW)
1837 /* This grid is "torch lit" */
1838 cave_lite_hack(y, x);
1845 /*** Complete the algorithm ***/
1847 /* Draw the new grids */
1848 for (i = 0; i < current_floor_ptr->lite_n; i++)
1850 y = current_floor_ptr->lite_y[i];
1851 x = current_floor_ptr->lite_x[i];
1853 g_ptr = ¤t_floor_ptr->grid_array[y][x];
1855 /* Update fresh grids */
1856 if (g_ptr->info & (CAVE_TEMP)) continue;
1858 /* Add it to later visual update */
1859 cave_note_and_redraw_later(g_ptr, y, x);
1862 /* Clear them all */
1863 for (i = 0; i < tmp_pos.n; i++)
1868 g_ptr = ¤t_floor_ptr->grid_array[y][x];
1870 /* No longer in the array */
1871 g_ptr->info &= ~(CAVE_TEMP);
1873 /* Update stale grids */
1874 if (g_ptr->info & (CAVE_LITE)) continue;
1876 /* Add it to later visual update */
1877 cave_redraw_later(g_ptr, y, x);
1883 /* Mega-Hack -- Visual update later */
1884 p_ptr->update |= (PU_DELAY_VIS);
1888 static bool mon_invis;
1889 static POSITION mon_fy, mon_fx;
1892 * Add a square to the changes array
1894 static void mon_lite_hack(POSITION y, POSITION x)
1900 /* We trust this grid is in bounds */
1901 /* if (!in_bounds2(y, x)) return; */
1903 g_ptr = ¤t_floor_ptr->grid_array[y][x];
1905 /* Want a unlit square in view of the player */
1906 if ((g_ptr->info & (CAVE_MNLT | CAVE_VIEW)) != CAVE_VIEW) return;
1908 if (!cave_los_grid(g_ptr))
1910 /* Hack -- Prevent monster lite leakage in walls */
1912 /* Horizontal walls between player and a monster */
1913 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
1915 dpf = p_ptr->y - mon_fy;
1917 midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
1919 /* Only first wall viewed from mid-x is lit */
1922 if (!cave_los_bold(y, x + 1)) return;
1924 else if (x > midpoint)
1926 if (!cave_los_bold(y, x - 1)) return;
1929 /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1930 else if (mon_invis) return;
1933 /* Vertical walls between player and a monster */
1934 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
1936 dpf = p_ptr->x - mon_fx;
1938 midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
1940 /* Only first wall viewed from mid-y is lit */
1943 if (!cave_los_bold(y + 1, x)) return;
1945 else if (y > midpoint)
1947 if (!cave_los_bold(y - 1, x)) return;
1950 /* Hack XXX XXX - Is it a wall and monster not in LOS? */
1951 else if (mon_invis) return;
1955 /* We trust tmp_pos.n does not exceed TEMP_MAX */
1958 if (!(g_ptr->info & CAVE_MNDK))
1960 /* Save this square */
1961 tmp_pos.x[tmp_pos.n] = x;
1962 tmp_pos.y[tmp_pos.n] = y;
1969 /* No longer dark */
1970 g_ptr->info &= ~(CAVE_MNDK);
1974 g_ptr->info |= CAVE_MNLT;
1979 * Add a square to the changes array
1981 static void mon_dark_hack(POSITION y, POSITION x)
1984 int midpoint, dpf, d;
1986 /* We trust this grid is in bounds */
1987 /* if (!in_bounds2(y, x)) return; */
1989 g_ptr = ¤t_floor_ptr->grid_array[y][x];
1991 /* Want a unlit and undarkened square in view of the player */
1992 if ((g_ptr->info & (CAVE_LITE | CAVE_MNLT | CAVE_MNDK | CAVE_VIEW)) != CAVE_VIEW) return;
1994 if (!cave_los_grid(g_ptr) && !cave_have_flag_grid(g_ptr, FF_PROJECT))
1996 /* Hack -- Prevent monster dark lite leakage in walls */
1998 /* Horizontal walls between player and a monster */
1999 if (((y < p_ptr->y) && (y > mon_fy)) || ((y > p_ptr->y) && (y < mon_fy)))
2001 dpf = p_ptr->y - mon_fy;
2003 midpoint = mon_fx + ((p_ptr->x - mon_fx) * ABS(d)) / ABS(dpf);
2005 /* Only first wall viewed from mid-x is lit */
2008 if (!cave_los_bold(y, x + 1) && !cave_have_flag_bold(y, x + 1, FF_PROJECT)) return;
2010 else if (x > midpoint)
2012 if (!cave_los_bold(y, x - 1) && !cave_have_flag_bold(y, x - 1, FF_PROJECT)) return;
2015 /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2016 else if (mon_invis) return;
2019 /* Vertical walls between player and a monster */
2020 if (((x < p_ptr->x) && (x > mon_fx)) || ((x > p_ptr->x) && (x < mon_fx)))
2022 dpf = p_ptr->x - mon_fx;
2024 midpoint = mon_fy + ((p_ptr->y - mon_fy) * ABS(d)) / ABS(dpf);
2026 /* Only first wall viewed from mid-y is lit */
2029 if (!cave_los_bold(y + 1, x) && !cave_have_flag_bold(y + 1, x, FF_PROJECT)) return;
2031 else if (y > midpoint)
2033 if (!cave_los_bold(y - 1, x) && !cave_have_flag_bold(y - 1, x, FF_PROJECT)) return;
2036 /* Hack XXX XXX - Is it a wall and monster not in LOS? */
2037 else if (mon_invis) return;
2041 /* We trust tmp_pos.n does not exceed TEMP_MAX */
2043 /* Save this square */
2044 tmp_pos.x[tmp_pos.n] = x;
2045 tmp_pos.y[tmp_pos.n] = y;
2049 g_ptr->info |= CAVE_MNDK;
2054 * Update squares illuminated or darkened by monsters.
2056 * Hack - use the CAVE_ROOM flag (renamed to be CAVE_MNLT) to
2057 * denote squares illuminated by monsters.
2059 * The CAVE_TEMP and CAVE_XTRA flag are used to store the state during the
2060 * updating. Only squares in view of the player, whos state
2061 * changes are drawn via lite_spot().
2063 void update_mon_lite(void)
2069 void(*add_mon_lite)(POSITION, POSITION);
2074 /* Non-Ninja player in the darkness */
2075 int dis_lim = ((d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) && !p_ptr->see_nocto) ?
2076 (MAX_SIGHT / 2 + 1) : (MAX_SIGHT + 3);
2078 /* Clear all monster lit squares */
2079 for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
2082 g_ptr = ¤t_floor_ptr->grid_array[current_floor_ptr->mon_lite_y[i]][current_floor_ptr->mon_lite_x[i]];
2084 /* Set temp or xtra flag */
2085 g_ptr->info |= (g_ptr->info & CAVE_MNLT) ? CAVE_TEMP : CAVE_XTRA;
2087 /* Clear monster illumination flag */
2088 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
2091 /* Empty temp list of new squares to lite up */
2094 /* If a monster stops time, don't process */
2095 if (!current_world_ptr->timewalk_m_idx)
2097 monster_type *m_ptr;
2098 monster_race *r_ptr;
2100 /* Loop through monsters, adding newly lit squares to changes list */
2101 for (i = 1; i < m_max; i++)
2103 m_ptr = ¤t_floor_ptr->m_list[i];
2104 r_ptr = &r_info[m_ptr->r_idx];
2105 if (!monster_is_valid(m_ptr)) continue;
2107 /* Is it too far away? */
2108 if (m_ptr->cdis > dis_lim) continue;
2110 /* Get lite radius */
2113 /* Note the radii are cumulative */
2114 if (r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_SELF_LITE_1)) rad++;
2115 if (r_ptr->flags7 & (RF7_HAS_LITE_2 | RF7_SELF_LITE_2)) rad += 2;
2116 if (r_ptr->flags7 & (RF7_HAS_DARK_1 | RF7_SELF_DARK_1)) rad--;
2117 if (r_ptr->flags7 & (RF7_HAS_DARK_2 | RF7_SELF_DARK_2)) rad -= 2;
2119 /* Exit if has no light */
2123 if (!(r_ptr->flags7 & (RF7_SELF_LITE_1 | RF7_SELF_LITE_2)) && (MON_CSLEEP(m_ptr) || (!current_floor_ptr->dun_level && is_daytime()) || p_ptr->inside_battle)) continue;
2124 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) rad = 1;
2125 add_mon_lite = mon_lite_hack;
2130 if (!(r_ptr->flags7 & (RF7_SELF_DARK_1 | RF7_SELF_DARK_2)) && (MON_CSLEEP(m_ptr) || (!current_floor_ptr->dun_level && !is_daytime()))) continue;
2131 add_mon_lite = mon_dark_hack;
2132 f_flag = FF_PROJECT;
2133 rad = -rad; /* Use absolute value */
2139 /* Is the monster visible? */
2140 mon_invis = !(current_floor_ptr->grid_array[mon_fy][mon_fx].info & CAVE_VIEW);
2142 /* The square it is on */
2143 add_mon_lite(mon_fy, mon_fx);
2145 /* Adjacent squares */
2146 add_mon_lite(mon_fy + 1, mon_fx);
2147 add_mon_lite(mon_fy - 1, mon_fx);
2148 add_mon_lite(mon_fy, mon_fx + 1);
2149 add_mon_lite(mon_fy, mon_fx - 1);
2150 add_mon_lite(mon_fy + 1, mon_fx + 1);
2151 add_mon_lite(mon_fy + 1, mon_fx - 1);
2152 add_mon_lite(mon_fy - 1, mon_fx + 1);
2153 add_mon_lite(mon_fy - 1, mon_fx - 1);
2158 /* South of the monster */
2159 if (cave_have_flag_bold(mon_fy + 1, mon_fx, f_flag))
2161 add_mon_lite(mon_fy + 2, mon_fx + 1);
2162 add_mon_lite(mon_fy + 2, mon_fx);
2163 add_mon_lite(mon_fy + 2, mon_fx - 1);
2165 g_ptr = ¤t_floor_ptr->grid_array[mon_fy + 2][mon_fx];
2168 if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
2170 add_mon_lite(mon_fy + 3, mon_fx + 1);
2171 add_mon_lite(mon_fy + 3, mon_fx);
2172 add_mon_lite(mon_fy + 3, mon_fx - 1);
2176 /* North of the monster */
2177 if (cave_have_flag_bold(mon_fy - 1, mon_fx, f_flag))
2179 add_mon_lite(mon_fy - 2, mon_fx + 1);
2180 add_mon_lite(mon_fy - 2, mon_fx);
2181 add_mon_lite(mon_fy - 2, mon_fx - 1);
2183 g_ptr = ¤t_floor_ptr->grid_array[mon_fy - 2][mon_fx];
2186 if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
2188 add_mon_lite(mon_fy - 3, mon_fx + 1);
2189 add_mon_lite(mon_fy - 3, mon_fx);
2190 add_mon_lite(mon_fy - 3, mon_fx - 1);
2194 /* East of the monster */
2195 if (cave_have_flag_bold(mon_fy, mon_fx + 1, f_flag))
2197 add_mon_lite(mon_fy + 1, mon_fx + 2);
2198 add_mon_lite(mon_fy, mon_fx + 2);
2199 add_mon_lite(mon_fy - 1, mon_fx + 2);
2201 g_ptr = ¤t_floor_ptr->grid_array[mon_fy][mon_fx + 2];
2204 if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
2206 add_mon_lite(mon_fy + 1, mon_fx + 3);
2207 add_mon_lite(mon_fy, mon_fx + 3);
2208 add_mon_lite(mon_fy - 1, mon_fx + 3);
2212 /* West of the monster */
2213 if (cave_have_flag_bold(mon_fy, mon_fx - 1, f_flag))
2215 add_mon_lite(mon_fy + 1, mon_fx - 2);
2216 add_mon_lite(mon_fy, mon_fx - 2);
2217 add_mon_lite(mon_fy - 1, mon_fx - 2);
2219 g_ptr = ¤t_floor_ptr->grid_array[mon_fy][mon_fx - 2];
2222 if ((rad == 3) && cave_have_flag_grid(g_ptr, f_flag))
2224 add_mon_lite(mon_fy + 1, mon_fx - 3);
2225 add_mon_lite(mon_fy, mon_fx - 3);
2226 add_mon_lite(mon_fy - 1, mon_fx - 3);
2234 /* South-East of the monster */
2235 if (cave_have_flag_bold(mon_fy + 1, mon_fx + 1, f_flag))
2237 add_mon_lite(mon_fy + 2, mon_fx + 2);
2240 /* South-West of the monster */
2241 if (cave_have_flag_bold(mon_fy + 1, mon_fx - 1, f_flag))
2243 add_mon_lite(mon_fy + 2, mon_fx - 2);
2246 /* North-East of the monster */
2247 if (cave_have_flag_bold(mon_fy - 1, mon_fx + 1, f_flag))
2249 add_mon_lite(mon_fy - 2, mon_fx + 2);
2252 /* North-West of the monster */
2253 if (cave_have_flag_bold(mon_fy - 1, mon_fx - 1, f_flag))
2255 add_mon_lite(mon_fy - 2, mon_fx - 2);
2261 /* Save end of list of new squares */
2262 end_temp = tmp_pos.n;
2265 * Look at old set flags to see if there are any changes.
2267 for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
2269 fx = current_floor_ptr->mon_lite_x[i];
2270 fy = current_floor_ptr->mon_lite_y[i];
2272 /* We trust this grid is in bounds */
2275 g_ptr = ¤t_floor_ptr->grid_array[fy][fx];
2277 if (g_ptr->info & CAVE_TEMP) /* Pervious lit */
2279 /* It it no longer lit? */
2280 if ((g_ptr->info & (CAVE_VIEW | CAVE_MNLT)) == CAVE_VIEW)
2282 /* It is now unlit */
2283 /* Add it to later visual update */
2284 cave_note_and_redraw_later(g_ptr, fy, fx);
2287 else /* Pervious darkened */
2289 /* It it no longer darken? */
2290 if ((g_ptr->info & (CAVE_VIEW | CAVE_MNDK)) == CAVE_VIEW)
2292 /* It is now undarken */
2293 /* Add it to later visual update */
2294 cave_note_and_redraw_later(g_ptr, fy, fx);
2298 /* Add to end of temp array */
2299 tmp_pos.x[tmp_pos.n] = fx;
2300 tmp_pos.y[tmp_pos.n] = fy;
2304 /* Clear the lite array */
2305 current_floor_ptr->mon_lite_n = 0;
2307 /* Copy the temp array into the lit array lighting the new squares. */
2308 for (i = 0; i < end_temp; i++)
2313 /* We trust this grid is in bounds */
2316 g_ptr = ¤t_floor_ptr->grid_array[fy][fx];
2318 if (g_ptr->info & CAVE_MNLT) /* Lit */
2320 /* The is the square newly lit and visible? */
2321 if ((g_ptr->info & (CAVE_VIEW | CAVE_TEMP)) == CAVE_VIEW)
2324 /* Add it to later visual update */
2325 cave_note_and_redraw_later(g_ptr, fy, fx);
2330 /* The is the square newly darkened and visible? */
2331 if ((g_ptr->info & (CAVE_VIEW | CAVE_XTRA)) == CAVE_VIEW)
2333 /* It is now darkened */
2334 /* Add it to later visual update */
2335 cave_note_and_redraw_later(g_ptr, fy, fx);
2339 /* Save in the monster lit or darkened array */
2340 current_floor_ptr->mon_lite_x[current_floor_ptr->mon_lite_n] = fx;
2341 current_floor_ptr->mon_lite_y[current_floor_ptr->mon_lite_n] = fy;
2342 current_floor_ptr->mon_lite_n++;
2345 /* Clear the temp flag for the old lit or darken grids */
2346 for (i = end_temp; i < tmp_pos.n; i++)
2348 /* We trust this grid is in bounds */
2350 current_floor_ptr->grid_array[tmp_pos.y[i]][tmp_pos.x[i]].info &= ~(CAVE_TEMP | CAVE_XTRA);
2353 /* Finished with tmp_pos.n */
2356 /* Mega-Hack -- Visual update later */
2357 p_ptr->update |= (PU_DELAY_VIS);
2359 p_ptr->monlite = (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_MNLT) ? TRUE : FALSE;
2361 if (p_ptr->special_defense & NINJA_S_STEALTH)
2363 if (p_ptr->old_monlite != p_ptr->monlite)
2367 msg_print(_("影の覆いが薄れた気がする。", "Your mantle of shadow become thin."));
2371 msg_print(_("影の覆いが濃くなった!", "Your mantle of shadow restored its original darkness."));
2375 p_ptr->old_monlite = p_ptr->monlite;
2378 void clear_mon_lite(void)
2383 /* Clear all monster lit squares */
2384 for (i = 0; i < current_floor_ptr->mon_lite_n; i++)
2387 g_ptr = ¤t_floor_ptr->grid_array[current_floor_ptr->mon_lite_y[i]][current_floor_ptr->mon_lite_x[i]];
2389 /* Clear monster illumination flag */
2390 g_ptr->info &= ~(CAVE_MNLT | CAVE_MNDK);
2393 /* Empty the array */
2394 current_floor_ptr->mon_lite_n = 0;
2400 * Clear the viewable space
2402 void forget_view(void)
2408 /* None to forget */
2409 if (!current_floor_ptr->view_n) return;
2411 /* Clear them all */
2412 for (i = 0; i < current_floor_ptr->view_n; i++)
2414 POSITION y = current_floor_ptr->view_y[i];
2415 POSITION x = current_floor_ptr->view_x[i];
2416 g_ptr = ¤t_floor_ptr->grid_array[y][x];
2418 /* Forget that the grid is viewable */
2419 g_ptr->info &= ~(CAVE_VIEW);
2421 /* if (!panel_contains(y, x)) continue; */
2423 /* Update the screen */
2424 /* lite_spot(y, x); Perhaps don't need? */
2428 current_floor_ptr->view_n = 0;
2434 * This macro allows us to efficiently add a grid to the "view" array,
2435 * note that we are never called for illegal grids, or for grids which
2436 * have already been placed into the "view" array, and we are never
2437 * called when the "view" array is full.
2439 #define cave_view_hack(C,Y,X) \
2441 if (!((C)->info & (CAVE_VIEW))){\
2442 (C)->info |= (CAVE_VIEW); \
2443 current_floor_ptr->view_y[current_floor_ptr->view_n] = (Y); \
2444 current_floor_ptr->view_x[current_floor_ptr->view_n] = (X); \
2445 current_floor_ptr->view_n++;}\
2451 * Helper function for "update_view()" below
2453 * We are checking the "viewability" of grid (y,x) by the player.
2455 * This function assumes that (y,x) is legal (i.e. on the map).
2457 * Grid (y1,x1) is on the "diagonal" between (p_ptr->y,p_ptr->x) and (y,x)
2458 * Grid (y2,x2) is "adjacent", also between (p_ptr->y,p_ptr->x) and (y,x).
2460 * Note that we are using the "CAVE_XTRA" field for marking grids as
2461 * "easily viewable". This bit is cleared at the end of "update_view()".
2463 * This function adds (y,x) to the "viewable set" if necessary.
2465 * This function now returns "TRUE" if vision is "blocked" by grid (y,x).
2467 static bool update_view_aux(POSITION y, POSITION x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
2469 bool f1, f2, v1, v2, z1, z2, wall;
2473 grid_type *g1_c_ptr;
2474 grid_type *g2_c_ptr;
2476 /* Access the grids */
2477 g1_c_ptr = ¤t_floor_ptr->grid_array[y1][x1];
2478 g2_c_ptr = ¤t_floor_ptr->grid_array[y2][x2];
2481 /* Check for walls */
2482 f1 = (cave_los_grid(g1_c_ptr));
2483 f2 = (cave_los_grid(g2_c_ptr));
2485 /* Totally blocked by physical walls */
2486 if (!f1 && !f2) return (TRUE);
2489 /* Check for visibility */
2490 v1 = (f1 && (g1_c_ptr->info & (CAVE_VIEW)));
2491 v2 = (f2 && (g2_c_ptr->info & (CAVE_VIEW)));
2493 /* Totally blocked by "unviewable neighbors" */
2494 if (!v1 && !v2) return (TRUE);
2496 g_ptr = ¤t_floor_ptr->grid_array[y][x];
2499 /* Check for walls */
2500 wall = (!cave_los_grid(g_ptr));
2503 /* Check the "ease" of visibility */
2504 z1 = (v1 && (g1_c_ptr->info & (CAVE_XTRA)));
2505 z2 = (v2 && (g2_c_ptr->info & (CAVE_XTRA)));
2507 /* Hack -- "easy" plus "easy" yields "easy" */
2510 g_ptr->info |= (CAVE_XTRA);
2512 cave_view_hack(g_ptr, y, x);
2517 /* Hack -- primary "easy" yields "viewed" */
2520 cave_view_hack(g_ptr, y, x);
2525 /* Hack -- "view" plus "view" yields "view" */
2528 /* g_ptr->info |= (CAVE_XTRA); */
2530 cave_view_hack(g_ptr, y, x);
2536 /* Mega-Hack -- the "los()" function works poorly on walls */
2539 cave_view_hack(g_ptr, y, x);
2545 /* Hack -- check line of sight */
2546 if (los(p_ptr->y, p_ptr->x, y, x))
2548 cave_view_hack(g_ptr, y, x);
2554 /* Assume no line of sight. */
2561 * Calculate the viewable space
2563 * 1: Process the player
2564 * 1a: The player is always (easily) viewable
2565 * 2: Process the diagonals
2566 * 2a: The diagonals are (easily) viewable up to the first wall
2567 * 2b: But never go more than 2/3 of the "full" distance
2568 * 3: Process the main axes
2569 * 3a: The main axes are (easily) viewable up to the first wall
2570 * 3b: But never go more than the "full" distance
2571 * 4: Process sequential "strips" in each of the eight octants
2572 * 4a: Each strip runs along the previous strip
2573 * 4b: The main axes are "previous" to the first strip
2574 * 4c: Process both "sides" of each "direction" of each strip
2575 * 4c1: Each side aborts as soon as possible
2576 * 4c2: Each side tells the next strip how far it has to check
2578 * Note that the octant processing involves some pretty interesting
2579 * observations involving when a grid might possibly be viewable from
2580 * a given grid, and on the order in which the strips are processed.
2582 * Note the use of the mathematical facts shown below, which derive
2583 * from the fact that (1 < sqrt(2) < 1.5), and that the length of the
2584 * hypotenuse of a right triangle is primarily determined by the length
2585 * of the longest side, when one side is small, and is strictly less
2586 * than one-and-a-half times as long as the longest side when both of
2587 * the sides are large.
2589 * if (manhatten(dy,dx) < R) then (hypot(dy,dx) < R)
2590 * if (manhatten(dy,dx) > R*3/2) then (hypot(dy,dx) > R)
2592 * hypot(dy,dx) is approximated by (dx+dy+MAX(dx,dy)) / 2
2594 * These observations are important because the calculation of the actual
2595 * value of "hypot(dx,dy)" is extremely expensive, involving square roots,
2596 * while for small values (up to about 20 or so), the approximations above
2597 * are correct to within an error of at most one grid or so.
2599 * Observe the use of "full" and "over" in the code below, and the use of
2600 * the specialized calculation involving "limit", all of which derive from
2601 * the observations given above. Basically, we note that the "circle" of
2602 * view is completely contained in an "octagon" whose bounds are easy to
2603 * determine, and that only a few steps are needed to derive the actual
2604 * bounds of the circle given the bounds of the octagon.
2606 * Note that by skipping all the grids in the corners of the octagon, we
2607 * place an upper limit on the number of grids in the field of view, given
2608 * that "full" is never more than 20. Of the 1681 grids in the "square" of
2609 * view, only about 1475 of these are in the "octagon" of view, and even
2610 * fewer are in the "circle" of view, so 1500 or 1536 is more than enough
2611 * entries to completely contain the actual field of view.
2613 * Note also the care taken to prevent "running off the map". The use of
2614 * explicit checks on the "validity" of the "diagonal", and the fact that
2615 * the loops are never allowed to "leave" the map, lets "update_view_aux()"
2616 * use the optimized "cave_los_bold()" macro, and to avoid the overhead
2617 * of multiple checks on the validity of grids.
2619 * Note the "optimizations" involving the "se","sw","ne","nw","es","en",
2620 * "ws","wn" variables. They work like this: While travelling down the
2621 * south-bound strip just to the east of the main south axis, as soon as
2622 * we get to a grid which does not "transmit" viewing, if all of the strips
2623 * preceding us (in this case, just the main axis) had terminated at or before
2624 * the same point, then we can stop, and reset the "max distance" to ourself.
2625 * So, each strip (named by major axis plus offset, thus "se" in this case)
2626 * maintains a "blockage" variable, initialized during the main axis step,
2627 * and checks it whenever a blockage is observed. After processing each
2628 * strip as far as the previous strip told us to process, the next strip is
2629 * told not to go farther than the current strip's farthest viewable grid,
2630 * unless open space is still available. This uses the "k" variable.
2632 * Note the use of "inline" macros for efficiency. The "cave_los_grid()"
2633 * macro is a replacement for "cave_los_bold()" which takes a pointer to
2634 * a grid instead of its location. The "cave_view_hack()" macro is a
2635 * chunk of code which adds the given location to the "view" array if it
2636 * is not already there, using both the actual location and a pointer to
2637 * the grid. See above.
2639 * By the way, the purpose of this code is to reduce the dependancy on the
2640 * "los()" function which is slow, and, in some cases, not very accurate.
2642 * It is very possible that I am the only person who fully understands this
2643 * function, and for that I am truly sorry, but efficiency was very important
2644 * and the "simple" version of this function was just not fast enough. I am
2645 * more than willing to replace this function with a simpler one, if it is
2646 * equally efficient, and especially willing if the new function happens to
2647 * derive "reverse-line-of-sight" at the same time, since currently monsters
2648 * just use an optimized hack of "you see me, so I see you", and then use the
2649 * actual "projectable()" function to check spell attacks.
2651 void update_view(void)
2656 int se, sw, ne, nw, es, en, ws, wn;
2660 POSITION y_max = current_floor_ptr->height - 1;
2661 POSITION x_max = current_floor_ptr->width - 1;
2665 /*** Initialize ***/
2668 if (view_reduce_view && !current_floor_ptr->dun_level)
2670 /* Full radius (10) */
2671 full = MAX_SIGHT / 2;
2673 /* Octagon factor (15) */
2674 over = MAX_SIGHT * 3 / 4;
2680 /* Full radius (20) */
2683 /* Octagon factor (30) */
2684 over = MAX_SIGHT * 3 / 2;
2688 /*** Step 0 -- Begin ***/
2690 /* Save the old "view" grids for later */
2691 for (n = 0; n < current_floor_ptr->view_n; n++)
2693 y = current_floor_ptr->view_y[n];
2694 x = current_floor_ptr->view_x[n];
2695 g_ptr = ¤t_floor_ptr->grid_array[y][x];
2697 /* Mark the grid as not in "view" */
2698 g_ptr->info &= ~(CAVE_VIEW);
2700 /* Mark the grid as "seen" */
2701 g_ptr->info |= (CAVE_TEMP);
2703 /* Add it to the "seen" set */
2704 tmp_pos.y[tmp_pos.n] = y;
2705 tmp_pos.x[tmp_pos.n] = x;
2709 /* Start over with the "view" array */
2710 current_floor_ptr->view_n = 0;
2712 /*** Step 1 -- adjacent grids ***/
2714 /* Now start on the player */
2717 g_ptr = ¤t_floor_ptr->grid_array[y][x];
2719 /* Assume the player grid is easily viewable */
2720 g_ptr->info |= (CAVE_XTRA);
2722 /* Assume the player grid is viewable */
2723 cave_view_hack(g_ptr, y, x);
2726 /*** Step 2 -- Major Diagonals ***/
2731 /* Scan south-east */
2732 for (d = 1; d <= z; d++)
2734 g_ptr = ¤t_floor_ptr->grid_array[y + d][x + d];
2735 g_ptr->info |= (CAVE_XTRA);
2736 cave_view_hack(g_ptr, y + d, x + d);
2737 if (!cave_los_grid(g_ptr)) break;
2740 /* Scan south-west */
2741 for (d = 1; d <= z; d++)
2743 g_ptr = ¤t_floor_ptr->grid_array[y + d][x - d];
2744 g_ptr->info |= (CAVE_XTRA);
2745 cave_view_hack(g_ptr, y + d, x - d);
2746 if (!cave_los_grid(g_ptr)) break;
2749 /* Scan north-east */
2750 for (d = 1; d <= z; d++)
2752 g_ptr = ¤t_floor_ptr->grid_array[y - d][x + d];
2753 g_ptr->info |= (CAVE_XTRA);
2754 cave_view_hack(g_ptr, y - d, x + d);
2755 if (!cave_los_grid(g_ptr)) break;
2758 /* Scan north-west */
2759 for (d = 1; d <= z; d++)
2761 g_ptr = ¤t_floor_ptr->grid_array[y - d][x - d];
2762 g_ptr->info |= (CAVE_XTRA);
2763 cave_view_hack(g_ptr, y - d, x - d);
2764 if (!cave_los_grid(g_ptr)) break;
2767 /*** Step 3 -- major axes ***/
2770 for (d = 1; d <= full; d++)
2772 g_ptr = ¤t_floor_ptr->grid_array[y + d][x];
2773 g_ptr->info |= (CAVE_XTRA);
2774 cave_view_hack(g_ptr, y + d, x);
2775 if (!cave_los_grid(g_ptr)) break;
2778 /* Initialize the "south strips" */
2782 for (d = 1; d <= full; d++)
2784 g_ptr = ¤t_floor_ptr->grid_array[y - d][x];
2785 g_ptr->info |= (CAVE_XTRA);
2786 cave_view_hack(g_ptr, y - d, x);
2787 if (!cave_los_grid(g_ptr)) break;
2790 /* Initialize the "north strips" */
2794 for (d = 1; d <= full; d++)
2796 g_ptr = ¤t_floor_ptr->grid_array[y][x + d];
2797 g_ptr->info |= (CAVE_XTRA);
2798 cave_view_hack(g_ptr, y, x + d);
2799 if (!cave_los_grid(g_ptr)) break;
2802 /* Initialize the "east strips" */
2806 for (d = 1; d <= full; d++)
2808 g_ptr = ¤t_floor_ptr->grid_array[y][x - d];
2809 g_ptr->info |= (CAVE_XTRA);
2810 cave_view_hack(g_ptr, y, x - d);
2811 if (!cave_los_grid(g_ptr)) break;
2814 /* Initialize the "west strips" */
2818 /*** Step 4 -- Divide each "octant" into "strips" ***/
2820 /* Now check each "diagonal" (in parallel) */
2821 for (n = 1; n <= over / 2; n++)
2823 POSITION ypn, ymn, xpn, xmn;
2825 /* Acquire the "bounds" of the maximal circle */
2827 if (z > full - n) z = full - n;
2828 while ((z + n + (n >> 1)) > full) z--;
2831 /* Access the four diagonal grids */
2841 /* Maximum distance */
2842 m = MIN(z, y_max - ypn);
2845 if ((xpn <= x_max) && (n < se))
2848 for (k = n, d = 1; d <= m; d++)
2850 /* Check grid "d" in strip "n", notice "blockage" */
2851 if (update_view_aux(ypn + d, xpn, ypn + d - 1, xpn - 1, ypn + d - 1, xpn))
2853 if (n + d >= se) break;
2856 /* Track most distant "non-blockage" */
2863 /* Limit the next strip */
2868 if ((xmn >= 0) && (n < sw))
2871 for (k = n, d = 1; d <= m; d++)
2873 /* Check grid "d" in strip "n", notice "blockage" */
2874 if (update_view_aux(ypn + d, xmn, ypn + d - 1, xmn + 1, ypn + d - 1, xmn))
2876 if (n + d >= sw) break;
2879 /* Track most distant "non-blockage" */
2886 /* Limit the next strip */
2895 /* Maximum distance */
2899 if ((xpn <= x_max) && (n < ne))
2902 for (k = n, d = 1; d <= m; d++)
2904 /* Check grid "d" in strip "n", notice "blockage" */
2905 if (update_view_aux(ymn - d, xpn, ymn - d + 1, xpn - 1, ymn - d + 1, xpn))
2907 if (n + d >= ne) break;
2910 /* Track most distant "non-blockage" */
2917 /* Limit the next strip */
2922 if ((xmn >= 0) && (n < nw))
2925 for (k = n, d = 1; d <= m; d++)
2927 /* Check grid "d" in strip "n", notice "blockage" */
2928 if (update_view_aux(ymn - d, xmn, ymn - d + 1, xmn + 1, ymn - d + 1, xmn))
2930 if (n + d >= nw) break;
2933 /* Track most distant "non-blockage" */
2940 /* Limit the next strip */
2949 /* Maximum distance */
2950 m = MIN(z, x_max - xpn);
2953 if ((ypn <= x_max) && (n < es))
2956 for (k = n, d = 1; d <= m; d++)
2958 /* Check grid "d" in strip "n", notice "blockage" */
2959 if (update_view_aux(ypn, xpn + d, ypn - 1, xpn + d - 1, ypn, xpn + d - 1))
2961 if (n + d >= es) break;
2964 /* Track most distant "non-blockage" */
2971 /* Limit the next strip */
2976 if ((ymn >= 0) && (n < en))
2979 for (k = n, d = 1; d <= m; d++)
2981 /* Check grid "d" in strip "n", notice "blockage" */
2982 if (update_view_aux(ymn, xpn + d, ymn + 1, xpn + d - 1, ymn, xpn + d - 1))
2984 if (n + d >= en) break;
2987 /* Track most distant "non-blockage" */
2994 /* Limit the next strip */
3003 /* Maximum distance */
3007 if ((ypn <= y_max) && (n < ws))
3010 for (k = n, d = 1; d <= m; d++)
3012 /* Check grid "d" in strip "n", notice "blockage" */
3013 if (update_view_aux(ypn, xmn - d, ypn - 1, xmn - d + 1, ypn, xmn - d + 1))
3015 if (n + d >= ws) break;
3018 /* Track most distant "non-blockage" */
3025 /* Limit the next strip */
3030 if ((ymn >= 0) && (n < wn))
3033 for (k = n, d = 1; d <= m; d++)
3035 /* Check grid "d" in strip "n", notice "blockage" */
3036 if (update_view_aux(ymn, xmn - d, ymn + 1, xmn - d + 1, ymn, xmn - d + 1))
3038 if (n + d >= wn) break;
3041 /* Track most distant "non-blockage" */
3048 /* Limit the next strip */
3055 /*** Step 5 -- Complete the algorithm ***/
3057 /* Update all the new grids */
3058 for (n = 0; n < current_floor_ptr->view_n; n++)
3060 y = current_floor_ptr->view_y[n];
3061 x = current_floor_ptr->view_x[n];
3062 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3064 /* Clear the "CAVE_XTRA" flag */
3065 g_ptr->info &= ~(CAVE_XTRA);
3067 /* Update only newly viewed grids */
3068 if (g_ptr->info & (CAVE_TEMP)) continue;
3070 /* Add it to later visual update */
3071 cave_note_and_redraw_later(g_ptr, y, x);
3074 /* Wipe the old grids, update as needed */
3075 for (n = 0; n < tmp_pos.n; n++)
3079 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3081 /* No longer in the array */
3082 g_ptr->info &= ~(CAVE_TEMP);
3084 /* Update only non-viewable grids */
3085 if (g_ptr->info & (CAVE_VIEW)) continue;
3087 /* Add it to later visual update */
3088 cave_redraw_later(g_ptr, y, x);
3094 /* Mega-Hack -- Visual update later */
3095 p_ptr->update |= (PU_DELAY_VIS);
3100 * Mega-Hack -- Delayed visual update
3101 * Only used if update_view(), update_lite() or update_mon_lite() was called
3103 void delayed_visual_update(void)
3109 /* Update needed grids */
3110 for (i = 0; i < current_floor_ptr->redraw_n; i++)
3112 y = current_floor_ptr->redraw_y[i];
3113 x = current_floor_ptr->redraw_x[i];
3114 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3116 /* Update only needed grids (prevent multiple updating) */
3117 if (!(g_ptr->info & CAVE_REDRAW)) continue;
3119 /* If required, note */
3120 if (g_ptr->info & CAVE_NOTE) note_spot(y, x);
3124 /* Hack -- Visual update of monster on this grid */
3125 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
3127 /* No longer in the array */
3128 g_ptr->info &= ~(CAVE_NOTE | CAVE_REDRAW);
3132 current_floor_ptr->redraw_n = 0;
3137 * Hack -- forget the "flow" information
3139 void forget_flow(void)
3143 /* Check the entire dungeon */
3144 for (y = 0; y < current_floor_ptr->height; y++)
3146 for (x = 0; x < current_floor_ptr->width; x++)
3148 /* Forget the old data */
3149 current_floor_ptr->grid_array[y][x].dist = 0;
3150 current_floor_ptr->grid_array[y][x].cost = 0;
3151 current_floor_ptr->grid_array[y][x].when = 0;
3158 * Hack - speed up the update_flow algorithm by only doing
3159 * it everytime the player moves out of LOS of the last
3162 static POSITION flow_x = 0;
3163 static POSITION flow_y = 0;
3168 * Hack -- fill in the "cost" field of every grid that the player
3169 * can "reach" with the number of steps needed to reach that grid.
3170 * This also yields the "distance" of the player from every grid.
3172 * In addition, mark the "when" of the grids that can reach
3173 * the player with the incremented value of "flow_n".
3175 * Hack -- use the "seen" array as a "circular queue".
3177 * We do not need a priority queue because the cost from grid
3178 * to grid is always "one" and we process them in order.
3180 void update_flow(void)
3187 /* Paranoia -- make sure the array is empty */
3188 if (tmp_pos.n) return;
3190 /* The last way-point is on the map */
3191 if (running && in_bounds(flow_y, flow_x))
3193 /* The way point is in sight - do not update. (Speedup) */
3194 if (current_floor_ptr->grid_array[flow_y][flow_x].info & CAVE_VIEW) return;
3197 /* Erase all of the current flow information */
3198 for (y = 0; y < current_floor_ptr->height; y++)
3200 for (x = 0; x < current_floor_ptr->width; x++)
3202 current_floor_ptr->grid_array[y][x].cost = 0;
3203 current_floor_ptr->grid_array[y][x].dist = 0;
3207 /* Save player position */
3211 /* Add the player's grid to the queue */
3212 tmp_pos.y[0] = p_ptr->y;
3213 tmp_pos.x[0] = p_ptr->x;
3215 /* Now process the queue */
3216 while (flow_head != flow_tail)
3220 /* Extract the next entry */
3221 ty = tmp_pos.y[flow_tail];
3222 tx = tmp_pos.x[flow_tail];
3224 /* Forget that entry */
3225 if (++flow_tail == TEMP_MAX) flow_tail = 0;
3227 /* Add the "children" */
3228 for (d = 0; d < 8; d++)
3230 int old_head = flow_head;
3231 byte_hack m = current_floor_ptr->grid_array[ty][tx].cost + 1;
3232 byte_hack n = current_floor_ptr->grid_array[ty][tx].dist + 1;
3235 /* Child location */
3236 y = ty + ddy_ddd[d];
3237 x = tx + ddx_ddd[d];
3239 /* Ignore player's grid */
3240 if (player_bold(y, x)) continue;
3242 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3244 if (is_closed_door(g_ptr->feat)) m += 3;
3246 /* Ignore "pre-stamped" entries */
3247 if (g_ptr->dist != 0 && g_ptr->dist <= n && g_ptr->cost <= m) continue;
3249 /* Ignore "walls" and "rubble" */
3250 if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
3252 /* Save the flow cost */
3253 if (g_ptr->cost == 0 || g_ptr->cost > m) g_ptr->cost = m;
3254 if (g_ptr->dist == 0 || g_ptr->dist > n) g_ptr->dist = n;
3256 /* Hack -- limit flow depth */
3257 if (n == MONSTER_FLOW_DEPTH) continue;
3259 /* Enqueue that entry */
3260 tmp_pos.y[flow_head] = y;
3261 tmp_pos.x[flow_head] = x;
3263 /* Advance the queue */
3264 if (++flow_head == TEMP_MAX) flow_head = 0;
3266 /* Hack -- notice overflow by forgetting new entry */
3267 if (flow_head == flow_tail) flow_head = old_head;
3273 static int scent_when = 0;
3276 * Characters leave scent trails for perceptive monsters to track.
3278 * Smell is rather more limited than sound. Many creatures cannot use
3279 * it at all, it doesn't extend very far outwards from the character's
3280 * current position, and monsters can use it to home in the character,
3281 * but not to run away from him.
3283 * Smell is valued according to age. When a character takes his current_world_ptr->game_turn,
3284 * scent is aged by one, and new scent of the current age is laid down.
3285 * Speedy characters leave more scent, true, but it also ages faster,
3286 * which makes it harder to hunt them down.
3288 * Whenever the age count loops, most of the scent trail is erased and
3289 * the age of the remainder is recalculated.
3291 void update_smell(void)
3296 /* Create a table that controls the spread of scent */
3297 const int scent_adjust[5][5] =
3306 /* Loop the age and adjust scent values when necessary */
3307 if (++scent_when == 254)
3309 /* Scan the entire dungeon */
3310 for (y = 0; y < current_floor_ptr->height; y++)
3312 for (x = 0; x < current_floor_ptr->width; x++)
3314 int w = current_floor_ptr->grid_array[y][x].when;
3315 current_floor_ptr->grid_array[y][x].when = (w > 128) ? (w - 128) : 0;
3324 /* Lay down new scent */
3325 for (i = 0; i < 5; i++)
3327 for (j = 0; j < 5; j++)
3331 /* Translate table to map grids */
3332 y = i + p_ptr->y - 2;
3333 x = j + p_ptr->x - 2;
3336 if (!in_bounds(y, x)) continue;
3338 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3340 /* Walls, water, and lava cannot hold scent. */
3341 if (!cave_have_flag_grid(g_ptr, FF_MOVE) && !is_closed_door(g_ptr->feat)) continue;
3343 /* Grid must not be blocked by walls from the character */
3344 if (!player_has_los_bold(y, x)) continue;
3346 /* Note grids that are too far away */
3347 if (scent_adjust[i][j] == -1) continue;
3349 /* Mark the grid with new scent */
3350 g_ptr->when = scent_when + scent_adjust[i][j];
3357 * Hack -- map the current panel (plus some) ala "magic mapping"
3359 void map_area(POSITION range)
3365 feature_type *f_ptr;
3367 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS) range /= 3;
3369 /* Scan that area */
3370 for (y = 1; y < current_floor_ptr->height - 1; y++)
3372 for (x = 1; x < current_floor_ptr->width - 1; x++)
3374 if (distance(p_ptr->y, p_ptr->x, y, x) > range) continue;
3376 g_ptr = ¤t_floor_ptr->grid_array[y][x];
3378 /* Memorize terrain of the grid */
3379 g_ptr->info |= (CAVE_KNOWN);
3381 /* Feature code (applying "mimic" field) */
3382 feat = get_feat_mimic(g_ptr);
3383 f_ptr = &f_info[feat];
3385 /* All non-walls are "checked" */
3386 if (!have_flag(f_ptr->flags, FF_WALL))
3388 /* Memorize normal features */
3389 if (have_flag(f_ptr->flags, FF_REMEMBER))
3391 /* Memorize the object */
3392 g_ptr->info |= (CAVE_MARK);
3395 /* Memorize known walls */
3396 for (i = 0; i < 8; i++)
3398 g_ptr = ¤t_floor_ptr->grid_array[y + ddy_ddd[i]][x + ddx_ddd[i]];
3400 /* Feature code (applying "mimic" field) */
3401 feat = get_feat_mimic(g_ptr);
3402 f_ptr = &f_info[feat];
3404 /* Memorize walls (etc) */
3405 if (have_flag(f_ptr->flags, FF_REMEMBER))
3407 /* Memorize the walls */
3408 g_ptr->info |= (CAVE_MARK);
3415 p_ptr->redraw |= (PR_MAP);
3417 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3422 * Change the "feat" flag for a grid, and notice/redraw the grid
3424 void cave_set_feat(POSITION y, POSITION x, FEAT_IDX feat)
3426 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
3427 feature_type *f_ptr = &f_info[feat];
3428 bool old_los, old_mirror;
3430 if (!character_dungeon)
3432 /* Clear mimic type */
3435 /* Change the feature */
3438 /* Hack -- glow the GLOW terrain */
3439 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
3444 for (i = 0; i < 9; i++)
3446 yy = y + ddy_ddd[i];
3447 xx = x + ddx_ddd[i];
3448 if (!in_bounds2(yy, xx)) continue;
3449 current_floor_ptr->grid_array[yy][xx].info |= CAVE_GLOW;
3456 old_los = cave_have_flag_bold(y, x, FF_LOS);
3457 old_mirror = is_mirror_grid(g_ptr);
3459 /* Clear mimic type */
3462 /* Change the feature */
3465 /* Remove flag for mirror/glyph */
3466 g_ptr->info &= ~(CAVE_OBJECT);
3468 if (old_mirror && (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
3470 g_ptr->info &= ~(CAVE_GLOW);
3471 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
3473 update_local_illumination(y, x);
3476 /* Check for change to boring grid */
3477 if (!have_flag(f_ptr->flags, FF_REMEMBER)) g_ptr->info &= ~(CAVE_MARK);
3478 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
3483 /* Check if los has changed */
3484 if (old_los ^ have_flag(f_ptr->flags, FF_LOS))
3487 #ifdef COMPLEX_WALL_ILLUMINATION /* COMPLEX_WALL_ILLUMINATION */
3489 update_local_illumination(y, x);
3491 #endif /* COMPLEX_WALL_ILLUMINATION */
3493 /* Update the visuals */
3494 p_ptr->update |= (PU_VIEW | PU_LITE | PU_MON_LITE | PU_MONSTERS);
3497 /* Hack -- glow the GLOW terrain */
3498 if (have_flag(f_ptr->flags, FF_GLOW) && !(d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS))
3504 for (i = 0; i < 9; i++)
3506 yy = y + ddy_ddd[i];
3507 xx = x + ddx_ddd[i];
3508 if (!in_bounds2(yy, xx)) continue;
3509 cc_ptr = ¤t_floor_ptr->grid_array[yy][xx];
3510 cc_ptr->info |= CAVE_GLOW;
3512 if (player_has_los_grid(cc_ptr))
3514 if (cc_ptr->m_idx) update_monster(cc_ptr->m_idx, FALSE);
3521 update_local_illumination(yy, xx);
3524 if (p_ptr->special_defense & NINJA_S_STEALTH)
3526 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_GLOW) set_superstealth(FALSE);
3532 FEAT_IDX conv_dungeon_feat(FEAT_IDX newfeat)
3534 feature_type *f_ptr = &f_info[newfeat];
3536 if (have_flag(f_ptr->flags, FF_CONVERT))
3538 switch (f_ptr->subtype)
3540 case CONVERT_TYPE_FLOOR:
3541 return feat_ground_type[randint0(100)];
3542 case CONVERT_TYPE_WALL:
3543 return feat_wall_type[randint0(100)];
3544 case CONVERT_TYPE_INNER:
3545 return feat_wall_inner;
3546 case CONVERT_TYPE_OUTER:
3547 return feat_wall_outer;
3548 case CONVERT_TYPE_SOLID:
3549 return feat_wall_solid;
3550 case CONVERT_TYPE_STREAM1:
3551 return d_info[p_ptr->dungeon_idx].stream1;
3552 case CONVERT_TYPE_STREAM2:
3553 return d_info[p_ptr->dungeon_idx].stream2;
3558 else return newfeat;
3563 * Take a feature, determine what that feature becomes
3564 * through applying the given action.
3566 FEAT_IDX feat_state(FEAT_IDX feat, int action)
3568 feature_type *f_ptr = &f_info[feat];
3571 /* Get the new feature */
3572 for (i = 0; i < MAX_FEAT_STATES; i++)
3574 if (f_ptr->state[i].action == action) return conv_dungeon_feat(f_ptr->state[i].result);
3577 if (have_flag(f_ptr->flags, FF_PERMANENT)) return feat;
3579 return (feature_action_flags[action] & FAF_DESTROY) ? conv_dungeon_feat(f_ptr->destroyed) : feat;
3583 * Takes a location and action and changes the feature at that
3584 * location through applying the given action.
3586 void cave_alter_feat(POSITION y, POSITION x, int action)
3588 /* Set old feature */
3589 FEAT_IDX oldfeat = current_floor_ptr->grid_array[y][x].feat;
3591 /* Get the new feat */
3592 FEAT_IDX newfeat = feat_state(oldfeat, action);
3595 if (newfeat == oldfeat) return;
3597 /* Set the new feature */
3598 cave_set_feat(y, x, newfeat);
3600 if (!(feature_action_flags[action] & FAF_NO_DROP))
3602 feature_type *old_f_ptr = &f_info[oldfeat];
3603 feature_type *f_ptr = &f_info[newfeat];
3607 if (have_flag(old_f_ptr->flags, FF_HAS_GOLD) && !have_flag(f_ptr->flags, FF_HAS_GOLD))
3609 /* Place some gold */
3615 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)))
3618 place_object(y, x, 0L);
3622 if (found && character_dungeon && player_can_see_bold(y, x))
3624 msg_print(_("何かを発見した!", "You have found something!"));
3628 if (feature_action_flags[action] & FAF_CRASH_GLASS)
3630 feature_type *old_f_ptr = &f_info[oldfeat];
3632 if (have_flag(old_f_ptr->flags, FF_GLASS) && character_dungeon)
3634 project(PROJECT_WHO_GLASS_SHARDS, 1, y, x, MIN(current_floor_ptr->dun_level, 100) / 4, GF_SHARDS,
3635 (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
3641 /* Remove a mirror */
3642 void remove_mirror(POSITION y, POSITION x)
3644 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
3646 /* Remove the mirror */
3647 g_ptr->info &= ~(CAVE_OBJECT);
3650 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_DARKNESS)
3652 g_ptr->info &= ~(CAVE_GLOW);
3653 if (!view_torch_grids) g_ptr->info &= ~(CAVE_MARK);
3654 if (g_ptr->m_idx) update_monster(g_ptr->m_idx, FALSE);
3656 update_local_illumination(y, x);
3666 * Return TRUE if there is a mirror on the grid.
3668 bool is_mirror_grid(grid_type *g_ptr)
3670 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MIRROR))
3678 * Return TRUE if there is a mirror on the grid.
3680 bool is_glyph_grid(grid_type *g_ptr)
3682 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_GLYPH))
3690 * Return TRUE if there is a mirror on the grid.
3692 bool is_explosive_rune_grid(grid_type *g_ptr)
3694 if ((g_ptr->info & CAVE_OBJECT) && have_flag(f_info[g_ptr->mimic].flags, FF_MINOR_GLYPH))
3702 * Track a new monster
3704 void health_track(MONSTER_IDX m_idx)
3706 /* Mount monster is already tracked */
3707 if (m_idx && m_idx == p_ptr->riding) return;
3709 /* Track a new guy */
3710 p_ptr->health_who = m_idx;
3712 /* Redraw (later) */
3713 p_ptr->redraw |= (PR_HEALTH);
3719 * Hack -- track the given monster race
3721 void monster_race_track(MONRACE_IDX r_idx)
3723 /* Save this monster ID */
3724 p_ptr->monster_race_idx = r_idx;
3726 p_ptr->window |= (PW_MONSTER);
3732 * Hack -- track the given object kind
3734 void object_kind_track(KIND_OBJECT_IDX k_idx)
3736 /* Save this monster ID */
3737 p_ptr->object_kind_idx = k_idx;
3739 p_ptr->window |= (PW_OBJECT);
3744 * @brief 指定されたマスがモンスターのテレポート可能先かどうかを判定する。
3745 * @param m_idx モンスターID
3749 * @return テレポート先として妥当ならばtrue
3751 bool cave_monster_teleportable_bold(MONSTER_IDX m_idx, POSITION y, POSITION x, BIT_FLAGS mode)
3753 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
3754 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
3755 feature_type *f_ptr = &f_info[g_ptr->feat];
3757 /* Require "teleportable" space */
3758 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
3760 if (g_ptr->m_idx && (g_ptr->m_idx != m_idx)) return FALSE;
3761 if (player_bold(y, x)) return FALSE;
3763 /* Hack -- no teleport onto glyph of warding */
3764 if (is_glyph_grid(g_ptr)) return FALSE;
3765 if (is_explosive_rune_grid(g_ptr)) return FALSE;
3767 if (!(mode & TELEPORT_PASSIVE))
3769 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], 0)) return FALSE;
3776 * @brief 指定されたマスにプレイヤーがテレポート可能かどうかを判定する。
3780 * @return テレポート先として妥当ならばtrue
3782 bool cave_player_teleportable_bold(POSITION y, POSITION x, BIT_FLAGS mode)
3784 grid_type *g_ptr = ¤t_floor_ptr->grid_array[y][x];
3785 feature_type *f_ptr = &f_info[g_ptr->feat];
3787 /* Require "teleportable" space */
3788 if (!have_flag(f_ptr->flags, FF_TELEPORTABLE)) return FALSE;
3790 /* No magical teleporting into vaults and such */
3791 if (!(mode & TELEPORT_NONMAGICAL) && (g_ptr->info & CAVE_ICKY)) return FALSE;
3793 if (g_ptr->m_idx && (g_ptr->m_idx != p_ptr->riding)) return FALSE;
3795 /* don't teleport on a trap. */
3796 if (have_flag(f_ptr->flags, FF_HIT_TRAP)) return FALSE;
3798 if (!(mode & TELEPORT_PASSIVE))
3800 if (!player_can_enter(g_ptr->feat, 0)) return FALSE;
3802 if (have_flag(f_ptr->flags, FF_WATER) && have_flag(f_ptr->flags, FF_DEEP))
3804 if (!p_ptr->levitation && !p_ptr->can_swim) return FALSE;
3807 if (have_flag(f_ptr->flags, FF_LAVA) && !p_ptr->immune_fire && !IS_INVULN())
3809 /* Always forbid deep lava */
3810 if (have_flag(f_ptr->flags, FF_DEEP)) return FALSE;
3812 /* Forbid shallow lava when the player don't have levitation */
3813 if (!p_ptr->levitation) return FALSE;
3822 * @brief 地形は開くものであって、かつ開かれているかを返す /
3823 * Attempt to open the given chest at the given location
3825 * @return 開いた地形である場合TRUEを返す / Return TRUE if the given feature is an open door
3827 bool is_open(FEAT_IDX feat)
3829 return have_flag(f_info[feat].flags, FF_CLOSE) && (feat != feat_state(feat, FF_CLOSE));
3833 * @brief プレイヤーが地形踏破可能かを返す
3834 * @param feature 判定したい地形ID
3835 * @param mode 移動に関するオプションフラグ
3836 * @return 移動可能ならばTRUEを返す
3838 bool player_can_enter(FEAT_IDX feature, BIT_FLAGS16 mode)
3840 feature_type *f_ptr = &f_info[feature];
3842 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);
3844 if (have_flag(f_ptr->flags, FF_PATTERN))
3846 if (!(mode & CEM_P_CAN_ENTER_PATTERN)) return FALSE;
3849 if (have_flag(f_ptr->flags, FF_CAN_FLY) && p_ptr->levitation) return TRUE;
3850 if (have_flag(f_ptr->flags, FF_CAN_SWIM) && p_ptr->can_swim) return TRUE;
3851 if (have_flag(f_ptr->flags, FF_CAN_PASS) && p_ptr->pass_wall) return TRUE;
3853 if (!have_flag(f_ptr->flags, FF_MOVE)) return FALSE;