1 #include "floor/geometry.h"
2 #include "floor/cave.h"
3 #include "game-option/text-display-options.h"
4 #include "grid/feature.h"
6 #include "system/floor-type-definition.h"
7 #include "system/monster-type-definition.h"
8 #include "system/player-type-definition.h"
9 #include "target/projection-path-calculator.h"
10 #include "util/bit-flags-calculator.h"
13 * キーパッドの方向を南から反時計回り順に列挙 / Global array for looping through the "keypad directions"
15 const POSITION ddd[9] = { 2, 8, 6, 4, 3, 1, 9, 7, 5 };
18 * dddで定義した順にベクトルのX軸成分を定義 / Global arrays for converting "keypad direction" into offsets
20 const POSITION ddx[10] = { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 };
23 * dddで定義した順にベクトルのY軸成分を定義 / Global arrays for converting "keypad direction" into offsets
25 const POSITION ddy[10] = { 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 };
28 * ddd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]"
30 const POSITION ddx_ddd[9] = { 0, 0, 1, -1, 1, -1, 1, -1, 0 };
33 * ddd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]"
35 const POSITION ddy_ddd[9] = { 1, -1, 0, 0, 1, 1, -1, -1, 0 };
38 * キーパッドの円環状方向配列 / Circular keypad direction array
40 const POSITION cdd[8] = { 2, 3, 6, 9, 8, 7, 4, 1 };
43 * cdd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]"
45 const POSITION ddx_cdd[8] = { 0, 1, 1, 1, 0, -1, -1, -1 };
48 * cdd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]"
50 const POSITION ddy_cdd[8] = { 1, 1, 0, -1, -1, -1, 0, 1 };
53 * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique
60 POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
62 POSITION dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
63 POSITION dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
65 /* Squared distance */
66 POSITION target = (dy * dy) + (dx * dx);
68 /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */
69 POSITION d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
78 /* Approximate error */
79 err = (target - d * d) / (2 * d);
81 /* No error - we are done */
93 * @brief プレイヤーから指定の座標がどの方角にあるかを返す /
94 * Convert an adjacent location to a direction.
99 DIRECTION coords_to_dir(player_type *creature_ptr, POSITION y, POSITION x)
101 DIRECTION d[3][3] = { { 7, 4, 1 }, { 8, 5, 2 }, { 9, 6, 3 } };
104 dy = y - creature_ptr->y;
105 dx = x - creature_ptr->x;
106 if (ABS(dx) > 1 || ABS(dy) > 1)
109 return d[dx + 1][dy + 1];
113 * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
116 * @return 視覚に収められる状態ならTRUEを返す
118 * He must have vision, illumination, and line of sight.\n
120 * Note -- "CAVE_LITE" is only set if the "torch" has "los()".\n
121 * So, given "CAVE_LITE", we know that the grid is "fully visible".\n
123 * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean\n
124 * that a wall is visible from any direction. That would be odd. Except\n
125 * under wizard light, which might make sense. Thus, for walls, we require\n
126 * not only that they be "CAVE_GLOW", but also, that they be adjacent to a\n
127 * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is\n
128 * in line of sight of the player.\n
130 * This extra check is expensive, but it provides a more "correct" semantics.\n
132 * Note that we should not run this check on walls which are "outer walls" of\n
133 * the dungeon, or we will induce a memory fault, but actually verifying all\n
134 * of the locations would be extremely expensive.\n
136 * Thus, to speed up the function, we assume that all "perma-walls" which are\n
137 * "CAVE_GLOW" are "illuminated" from all sides. This is correct for all cases\n
138 * except "vaults" and the "buildings" in town. But the town is a hack anyway,\n
139 * and the player has more important things on his mind when he is attacking a\n
140 * monster vault. It is annoying, but an extremely important optimization.\n
142 * Note that "glowing walls" are only considered to be "illuminated" if the\n
143 * grid which is next to the wall in the direction of the player is also a\n
144 * "glowing" grid. This prevents the player from being able to "see" the\n
145 * walls of illuminated rooms from a corridor outside the room.\n
147 bool player_can_see_bold(player_type *creature_ptr, POSITION y, POSITION x)
151 /* Blind players see nothing */
152 if (creature_ptr->blind)
155 g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
157 /* Note that "torch-lite" yields "illumination" */
158 if (g_ptr->info & (CAVE_LITE | CAVE_MNLT))
161 /* Require line of sight to the grid */
162 if (!player_has_los_bold(creature_ptr, y, x))
165 /* Noctovision of Ninja */
166 if (creature_ptr->see_nocto)
169 /* Require "perma-lite" of the grid */
170 if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW)
173 /* Feature code (applying "mimic" field) */
174 /* Floors are simple */
175 if (feat_supports_los(get_feat_mimic(g_ptr)))
178 /* Check for "local" illumination */
179 return check_local_illumination(creature_ptr, y, x);
183 * Calculate "incremental motion". Used by project() and shoot().
184 * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2).
186 void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
188 POSITION dy, dx, dist, shift;
190 /* Extract the distance travelled */
191 dy = (*y < y1) ? y1 - *y : *y - y1;
192 dx = (*x < x1) ? x1 - *x : *x - x1;
194 /* Number of steps */
195 dist = (dy > dx) ? dy : dx;
197 /* We are calculating the next location */
200 /* Calculate the total distance along each axis */
201 dy = (y2 < y1) ? (y1 - y2) : (y2 - y1);
202 dx = (x2 < x1) ? (x1 - x2) : (x2 - x1);
204 /* Paranoia -- Hack -- no motion */
208 /* Move mostly vertically */
210 /* Extract a shift factor */
211 shift = (dist * dx + (dy - 1) / 2) / dy;
213 /* Sometimes move along the minor axis */
214 (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift);
216 /* Always move along major axis */
217 (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist);
220 /* Move mostly horizontally */
222 /* Extract a shift factor */
223 shift = (dist * dy + (dx - 1) / 2) / dx;
225 /* Sometimes move along the minor axis */
226 (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift);
228 /* Always move along major axis */
229 (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist);
234 * @brief Is the monster seen by the player?
235 * @param creature_ptr プレーヤーへの参照ポインタ
236 * @param m_ptr 個々のモンスターへの参照ポインタ
237 * @return 個々のモンスターがプレーヤーが見えたらTRUE
238 * @todo is_seen() の関数マクロをバラそうとしたがインクルード関係のコンパイルエラーで失敗
240 bool is_seen(player_type *creature_ptr, monster_type *m_ptr)
242 bool is_inside_view = !ignore_unview;
243 is_inside_view |= creature_ptr->phase_out;
245 |= player_can_see_bold(creature_ptr, m_ptr->fy, m_ptr->fx) && projectable(creature_ptr, creature_ptr->y, creature_ptr->x, m_ptr->fy, m_ptr->fx);
246 return m_ptr->ml && is_inside_view;