OSDN Git Service

[Refactor] #40477 Separated text-display-options.c/h from cmd-gameoption.c/h
[hengband/hengband.git] / src / floor / geometry.c
1 #include "floor/floor.h"
2 #include "floor/geometry.h"
3 #include "game-option/text-display-options.h"
4
5 /*!
6  * キーパッドの方向を南から反時計回り順に列挙 / Global array for looping through the "keypad directions"
7  */
8 const POSITION ddd[9] =
9 { 2, 8, 6, 4, 3, 1, 9, 7, 5 };
10
11 /*!
12  * dddで定義した順にベクトルのX軸成分を定義 / Global arrays for converting "keypad direction" into offsets
13  */
14 const POSITION ddx[10] =
15 { 0, -1, 0, 1, -1, 0, 1, -1, 0, 1 };
16
17 /*!
18  * dddで定義した順にベクトルのY軸成分を定義 / Global arrays for converting "keypad direction" into offsets
19  */
20 const POSITION ddy[10] =
21 { 0, 1, 1, 1, 0, 0, 0, -1, -1, -1 };
22
23 /*!
24  * ddd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]"
25  */
26 const POSITION ddx_ddd[9] =
27 { 0, 0, 1, -1, 1, -1, 1, -1, 0 };
28
29 /*!
30  * ddd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[ddd[i]]" and "ddy[ddd[i]]"
31  */
32 const POSITION ddy_ddd[9] =
33 { 1, -1, 0, 0, 1, 1, -1, -1, 0 };
34
35 /*!
36  * キーパッドの円環状方向配列 / Circular keypad direction array
37  */
38 const POSITION cdd[8] =
39 { 2, 3, 6, 9, 8, 7, 4, 1 };
40
41 /*!
42  * cdd越しにベクトルのX軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]"
43  */
44 const POSITION ddx_cdd[8] =
45 { 0, 1, 1, 1, 0, -1, -1, -1 };
46
47 /*!
48  * cdd越しにベクトルのY軸成分を定義 / Global arrays for optimizing "ddx[cdd[i]]" and "ddy[cdd[i]]"
49  */
50 const POSITION ddy_cdd[8] =
51 { 1, 1, 0, -1, -1, -1, 0, 1 };
52
53
54 /*!
55  * @brief 2点間の距離をニュートン・ラプソン法で算出する / Distance between two points via Newton-Raphson technique
56  * @param y1 1点目のy座標
57  * @param x1 1点目のx座標
58  * @param y2 2点目のy座標
59  * @param x2 2点目のx座標
60  * @return 2点間の距離
61  */
62 POSITION distance(POSITION y1, POSITION x1, POSITION y2, POSITION x2)
63 {
64         POSITION dy = (y1 > y2) ? (y1 - y2) : (y2 - y1);
65         POSITION dx = (x1 > x2) ? (x1 - x2) : (x2 - x1);
66
67         /* Squared distance */
68         POSITION target = (dy * dy) + (dx * dx);
69
70         /* Approximate distance: hypot(dy,dx) = max(dy,dx) + min(dy,dx) / 2 */
71         POSITION d = (dy > dx) ? (dy + (dx >> 1)) : (dx + (dy >> 1));
72
73         POSITION err;
74
75         /* Simple case */
76         if (!dy || !dx) return d;
77
78         while (TRUE)
79         {
80                 /* Approximate error */
81                 err = (target - d * d) / (2 * d);
82
83                 /* No error - we are done */
84                 if (!err) break;
85
86                 /* Adjust distance */
87                 d += err;
88         }
89
90         return d;
91 }
92
93
94 /*!
95  * @brief プレイヤーから指定の座標がどの方角にあるかを返す /
96  * Convert an adjacent location to a direction.
97  * @param y 方角を確認したY座標
98  * @param x 方角を確認したX座標
99  * @return 方向ID
100  */
101 DIRECTION coords_to_dir(player_type *creature_ptr, POSITION y, POSITION x)
102 {
103         DIRECTION d[3][3] = { {7, 4, 1}, {8, 5, 2}, {9, 6, 3} };
104         POSITION dy, dx;
105
106         dy = y - creature_ptr->y;
107         dx = x - creature_ptr->x;
108         if (ABS(dx) > 1 || ABS(dy) > 1) return 0;
109
110         return d[dx + 1][dy + 1];
111 }
112
113
114 /*!
115  * @brief 指定された座標をプレイヤーが視覚に収められるかを返す。 / Can the player "see" the given grid in detail?
116  * @param y y座標
117  * @param x x座標
118  * @return 視覚に収められる状態ならTRUEを返す
119  * @details
120  * He must have vision, illumination, and line of sight.\n
121  * \n
122  * Note -- "CAVE_LITE" is only set if the "torch" has "los()".\n
123  * So, given "CAVE_LITE", we know that the grid is "fully visible".\n
124  *\n
125  * Note that "CAVE_GLOW" makes little sense for a wall, since it would mean\n
126  * that a wall is visible from any direction.  That would be odd.  Except\n
127  * under wizard light, which might make sense.  Thus, for walls, we require\n
128  * not only that they be "CAVE_GLOW", but also, that they be adjacent to a\n
129  * grid which is not only "CAVE_GLOW", but which is a non-wall, and which is\n
130  * in line of sight of the player.\n
131  *\n
132  * This extra check is expensive, but it provides a more "correct" semantics.\n
133  *\n
134  * Note that we should not run this check on walls which are "outer walls" of\n
135  * the dungeon, or we will induce a memory fault, but actually verifying all\n
136  * of the locations would be extremely expensive.\n
137  *\n
138  * Thus, to speed up the function, we assume that all "perma-walls" which are\n
139  * "CAVE_GLOW" are "illuminated" from all sides.  This is correct for all cases\n
140  * except "vaults" and the "buildings" in town.  But the town is a hack anyway,\n
141  * and the player has more important things on his mind when he is attacking a\n
142  * monster vault.  It is annoying, but an extremely important optimization.\n
143  *\n
144  * Note that "glowing walls" are only considered to be "illuminated" if the\n
145  * grid which is next to the wall in the direction of the player is also a\n
146  * "glowing" grid.  This prevents the player from being able to "see" the\n
147  * walls of illuminated rooms from a corridor outside the room.\n
148  */
149 bool player_can_see_bold(player_type *creature_ptr, POSITION y, POSITION x)
150 {
151         grid_type *g_ptr;
152
153         /* Blind players see nothing */
154         if (creature_ptr->blind) return FALSE;
155
156         g_ptr = &creature_ptr->current_floor_ptr->grid_array[y][x];
157
158         /* Note that "torch-lite" yields "illumination" */
159         if (g_ptr->info & (CAVE_LITE | CAVE_MNLT)) return TRUE;
160
161         /* Require line of sight to the grid */
162         if (!player_has_los_bold(creature_ptr, y, x)) return FALSE;
163
164         /* Noctovision of Ninja */
165         if (creature_ptr->see_nocto) return TRUE;
166
167         /* Require "perma-lite" of the grid */
168         if ((g_ptr->info & (CAVE_GLOW | CAVE_MNDK)) != CAVE_GLOW) return FALSE;
169
170         /* Feature code (applying "mimic" field) */
171         /* Floors are simple */
172         if (feat_supports_los(get_feat_mimic(g_ptr))) return TRUE;
173
174         /* Check for "local" illumination */
175         return check_local_illumination(creature_ptr, y, x);
176 }
177
178
179 /*
180  * Calculate "incremental motion". Used by project() and shoot().
181  * Assumes that (*y,*x) lies on the path from (y1,x1) to (y2,x2).
182  */
183 void mmove2(POSITION *y, POSITION *x, POSITION y1, POSITION x1, POSITION y2, POSITION x2)
184 {
185         POSITION dy, dx, dist, shift;
186
187         /* Extract the distance travelled */
188         dy = (*y < y1) ? y1 - *y : *y - y1;
189         dx = (*x < x1) ? x1 - *x : *x - x1;
190
191         /* Number of steps */
192         dist = (dy > dx) ? dy : dx;
193
194         /* We are calculating the next location */
195         dist++;
196
197         /* Calculate the total distance along each axis */
198         dy = (y2 < y1) ? (y1 - y2) : (y2 - y1);
199         dx = (x2 < x1) ? (x1 - x2) : (x2 - x1);
200
201         /* Paranoia -- Hack -- no motion */
202         if (!dy && !dx) return;
203
204
205         /* Move mostly vertically */
206         if (dy > dx)
207         {
208                 /* Extract a shift factor */
209                 shift = (dist * dx + (dy - 1) / 2) / dy;
210
211                 /* Sometimes move along the minor axis */
212                 (*x) = (x2 < x1) ? (x1 - shift) : (x1 + shift);
213
214                 /* Always move along major axis */
215                 (*y) = (y2 < y1) ? (y1 - dist) : (y1 + dist);
216         }
217
218         /* Move mostly horizontally */
219         else
220         {
221                 /* Extract a shift factor */
222                 shift = (dist * dy + (dx - 1) / 2) / dx;
223
224                 /* Sometimes move along the minor axis */
225                 (*y) = (y2 < y1) ? (y1 - shift) : (y1 + shift);
226
227                 /* Always move along major axis */
228                 (*x) = (x2 < x1) ? (x1 - dist) : (x1 + dist);
229         }
230 }
231
232 /*
233  * todo is_seen() の関数マクロをバラそうとしたがインクルード関係のコンパイルエラーで失敗
234  * Is the monster seen by the player?
235  * @param creature_ptr プレーヤーへの参照ポインタ
236  * @param m_ptr 個々のモンスターへの参照ポインタ
237  * @return 個々のモンスターがプレーヤーが見えたらTRUE
238  */
239 bool is_seen(player_type *creature_ptr, monster_type *m_ptr)
240 {
241     bool is_inside_view = !ignore_unview;
242     is_inside_view |= creature_ptr->phase_out;
243     is_inside_view
244         |= 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);
245     return m_ptr->ml && is_inside_view;
246 }