OSDN Git Service

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