OSDN Git Service

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