OSDN Git Service

Merge branch 'master' of git.osdn.net:/gitroot/hengband/hengband
[hengband/hengband.git] / src / mspell / mspell-judgement.c
1 /*!
2  * @brief モンスター魔法の実装(対モンスター処理) / Monster spells (attack monster)
3  * @date 2014/01/17
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke
6  * This software may be copied and distributed for educational, research,
7  * and not for profit purposes provided that this copyright and statement
8  * are included in all such copies.  Other copyrights may also apply.
9  * 2014 Deskull rearranged comment for Doxygen.
10  */
11
12 #include "mspell/mspell-judgement.h"
13 #include "dungeon/dungeon.h"
14 #include "effect/effect-characteristics.h"
15 #include "floor/cave.h"
16 #include "floor/line-of-sight.h"
17 #include "grid/grid.h"
18 #include "main/sound-definitions-table.h"
19 #include "monster-race/monster-race.h"
20 #include "monster-race/race-flags-resistance.h"
21 #include "monster-race/race-flags4.h"
22 #include "monster/monster-info.h"
23 #include "monster/monster-status.h"
24 #include "player/attack-defense-types.h"
25 #include "player/player-race.h"
26 #include "player/special-defense-types.h"
27 #include "realm/realm-song-numbers.h"
28 #include "spell/range-calc.h"
29 #include "spell/spell-types.h"
30 #include "system/floor-type-definition.h"
31 #include "target/projection-path-calculator.h"
32
33 /*!
34  * @brief モンスターが敵対モンスターにビームを当てること可能かを判定する /
35  * Determine if a beam spell will hit the target.
36  * @param target_ptr プレーヤーへの参照ポインタ
37  * @param y1 始点のY座標
38  * @param x1 始点のX座標
39  * @param y2 目標のY座標
40  * @param x2 目標のX座標
41  * @param m_ptr 使用するモンスターの構造体参照ポインタ
42  * @return ビームが到達可能ならばTRUEを返す
43  */
44 bool direct_beam(player_type *target_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, monster_type *m_ptr)
45 {
46     floor_type *floor_ptr = target_ptr->current_floor_ptr;
47     u16b grid_g[512];
48     int grid_n = projection_path(target_ptr, grid_g, get_max_range(target_ptr), y1, x1, y2, x2, PROJECT_THRU);
49     if (!grid_n)
50         return FALSE;
51
52     bool hit2 = FALSE;
53     POSITION y, x;
54     bool is_friend = is_pet(m_ptr);
55     for (int i = 0; i < grid_n; i++) {
56         y = get_grid_y(grid_g[i]);
57         x = get_grid_x(grid_g[i]);
58
59         if (y == y2 && x == x2)
60             hit2 = TRUE;
61         else if (is_friend && floor_ptr->grid_array[y][x].m_idx > 0 && !are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[floor_ptr->grid_array[y][x].m_idx])) {
62             return FALSE;
63         }
64
65         if (is_friend && player_bold(target_ptr, y, x))
66             return FALSE;
67     }
68
69     if (!hit2)
70         return FALSE;
71     return TRUE;
72 }
73
74 /*!
75  * @brief モンスターが敵対モンスターに直接ブレスを当てることが可能かを判定する /
76  * Determine if a breath will hit the target.
77  * @param y1 始点のY座標
78  * @param x1 始点のX座標
79  * @param y2 目標のY座標
80  * @param x2 目標のX座標
81  * @param rad 半径
82  * @param typ 効果属性ID
83  * @param is_friend TRUEならば、プレイヤーを巻き込む時にブレスの判定をFALSEにする。
84  * @return ブレスを直接当てられるならばTRUEを返す
85  */
86 bool breath_direct(player_type *master_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, POSITION rad, EFFECT_ID typ, bool is_friend)
87 {
88     BIT_FLAGS flg;
89     switch (typ) {
90     case GF_LITE:
91     case GF_LITE_WEAK:
92         flg = PROJECT_LOS;
93         break;
94     case GF_DISINTEGRATE:
95         flg = PROJECT_DISI;
96         break;
97     default:
98         flg = 0;
99         break;
100     }
101
102     u16b grid_g[512];
103     int grid_n = projection_path(master_ptr, grid_g, get_max_range(master_ptr), y1, x1, y2, x2, flg);
104     int i;
105     POSITION y = y1;
106     POSITION x = x1;
107     for (i = 0; i < grid_n; ++i) {
108         int ny = get_grid_y(grid_g[i]);
109         int nx = get_grid_x(grid_g[i]);
110
111         if (flg & PROJECT_DISI) {
112             if (cave_stop_disintegration(master_ptr->current_floor_ptr, ny, nx))
113                 break;
114         } else if (flg & PROJECT_LOS) {
115             if (!cave_los_bold(master_ptr->current_floor_ptr, ny, nx))
116                 break;
117         } else {
118             if (!cave_have_flag_bold(master_ptr->current_floor_ptr, ny, nx, FF_PROJECT))
119                 break;
120         }
121
122         y = ny;
123         x = nx;
124     }
125
126     grid_n = i;
127     bool hit2 = FALSE;
128     bool hityou = FALSE;
129     if (!grid_n) {
130         if (flg & PROJECT_DISI) {
131             if (in_disintegration_range(master_ptr->current_floor_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad))
132                 hit2 = TRUE;
133             if (in_disintegration_range(master_ptr->current_floor_ptr, y1, x1, master_ptr->y, master_ptr->x)
134                 && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad))
135                 hityou = TRUE;
136         } else if (flg & PROJECT_LOS) {
137             if (los(master_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad))
138                 hit2 = TRUE;
139             if (los(master_ptr, y1, x1, master_ptr->y, master_ptr->x) && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad))
140                 hityou = TRUE;
141         } else {
142             if (projectable(master_ptr, y1, x1, y2, x2) && (distance(y1, x1, y2, x2) <= rad))
143                 hit2 = TRUE;
144             if (projectable(master_ptr, y1, x1, master_ptr->y, master_ptr->x) && (distance(y1, x1, master_ptr->y, master_ptr->x) <= rad))
145                 hityou = TRUE;
146         }
147     } else {
148         int grids = 0;
149         POSITION gx[1024], gy[1024];
150         POSITION gm[32];
151         POSITION gm_rad = rad;
152         breath_shape(master_ptr, grid_g, grid_n, &grids, gx, gy, gm, &gm_rad, rad, y1, x1, y, x, typ);
153         for (i = 0; i < grids; i++) {
154             y = gy[i];
155             x = gx[i];
156             if ((y == y2) && (x == x2))
157                 hit2 = TRUE;
158             if (player_bold(master_ptr, y, x))
159                 hityou = TRUE;
160         }
161     }
162
163     if (!hit2)
164         return FALSE;
165     if (is_friend && hityou)
166         return FALSE;
167
168     return TRUE;
169 }
170
171 /*!
172  * @brief モンスターが特殊能力の目標地点を決める処理 /
173  * Get the actual center point of ball spells (rad > 1) (originally from TOband)
174  * @param target_ptr プレーヤーへの参照ポインタ
175  * @param sy 始点のY座標
176  * @param sx 始点のX座標
177  * @param ty 目標Y座標を返す参照ポインタ
178  * @param tx 目標X座標を返す参照ポインタ
179  * @param flg 判定のフラグ配列
180  * @return なし
181  */
182 void get_project_point(player_type *target_ptr, POSITION sy, POSITION sx, POSITION *ty, POSITION *tx, BIT_FLAGS flg)
183 {
184     u16b path_g[128];
185     int path_n = projection_path(target_ptr, path_g, get_max_range(target_ptr), sy, sx, *ty, *tx, flg);
186     *ty = sy;
187     *tx = sx;
188     for (int i = 0; i < path_n; i++) {
189         sy = get_grid_y(path_g[i]);
190         sx = get_grid_x(path_g[i]);
191         if (!cave_have_flag_bold(target_ptr->current_floor_ptr, sy, sx, FF_PROJECT))
192             break;
193
194         *ty = sy;
195         *tx = sx;
196     }
197 }
198
199 /*!
200  * @brief モンスターが敵モンスターに魔力消去を使うかどうかを返す /
201  * Check should monster cast dispel spell at other monster.
202  * @param target_ptr プレーヤーへの参照ポインタ
203  * @param m_idx 術者のモンスターID
204  * @param t_idx 目標のモンスターID
205  * @return 魔力消去を使うべきならばTRUEを変えす。
206  */
207 bool dispel_check_monster(player_type *target_ptr, MONSTER_IDX m_idx, MONSTER_IDX t_idx)
208 {
209     monster_type *t_ptr = &target_ptr->current_floor_ptr->m_list[t_idx];
210     if (monster_invulner_remaining(t_ptr))
211         return TRUE;
212
213     if ((t_ptr->mspeed < 135) && monster_fast_remaining(t_ptr))
214         return TRUE;
215
216     if ((t_idx == target_ptr->riding) && dispel_check(target_ptr, m_idx))
217         return TRUE;
218
219     return FALSE;
220 }
221
222 /*!
223  * @brief モンスターがプレイヤーに魔力消去を与えるべきかを判定するルーチン
224  * Check should monster cast dispel spell.
225  * @param m_idx モンスターの構造体配列ID
226  * @return 魔力消去をかけるべきならTRUEを返す。
227  */
228 bool dispel_check(player_type *creature_ptr, MONSTER_IDX m_idx)
229 {
230     if (is_invuln(creature_ptr))
231         return TRUE;
232
233     if (creature_ptr->wraith_form)
234         return TRUE;
235
236     if (creature_ptr->shield)
237         return TRUE;
238
239     if (creature_ptr->magicdef)
240         return TRUE;
241
242     if (creature_ptr->multishadow)
243         return TRUE;
244
245     if (creature_ptr->dustrobe)
246         return TRUE;
247
248     if (creature_ptr->shero && (creature_ptr->pclass != CLASS_BERSERKER))
249         return TRUE;
250
251     if (creature_ptr->mimic_form == MIMIC_DEMON_LORD)
252         return TRUE;
253
254     monster_type *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
255     monster_race *r_ptr = &r_info[m_ptr->r_idx];
256     if (r_ptr->flags4 & RF4_BR_ACID) {
257         if (!creature_ptr->immune_acid && (creature_ptr->oppose_acid || music_singing(creature_ptr, MUSIC_RESIST)))
258             return TRUE;
259
260         if (creature_ptr->special_defense & DEFENSE_ACID)
261             return TRUE;
262     }
263
264     if (r_ptr->flags4 & RF4_BR_FIRE) {
265         if (!((creature_ptr->prace == RACE_BALROG) && creature_ptr->lev > 44)) {
266             if (!creature_ptr->immune_fire && (creature_ptr->oppose_fire || music_singing(creature_ptr, MUSIC_RESIST)))
267                 return TRUE;
268
269             if (creature_ptr->special_defense & DEFENSE_FIRE)
270                 return TRUE;
271         }
272     }
273
274     if (r_ptr->flags4 & RF4_BR_ELEC) {
275         if (!creature_ptr->immune_elec && (creature_ptr->oppose_elec || music_singing(creature_ptr, MUSIC_RESIST)))
276             return TRUE;
277
278         if (creature_ptr->special_defense & DEFENSE_ELEC)
279             return TRUE;
280     }
281
282     if (r_ptr->flags4 & RF4_BR_COLD) {
283         if (!creature_ptr->immune_cold && (creature_ptr->oppose_cold || music_singing(creature_ptr, MUSIC_RESIST)))
284             return TRUE;
285
286         if (creature_ptr->special_defense & DEFENSE_COLD)
287             return TRUE;
288     }
289
290     if (((r_ptr->flags4 & (RF4_BR_POIS | RF4_BR_NUKE)) != 0) && !((creature_ptr->pclass == CLASS_NINJA) && (creature_ptr->lev > 44))) {
291         if (creature_ptr->oppose_pois || music_singing(creature_ptr, MUSIC_RESIST))
292             return TRUE;
293
294         if (creature_ptr->special_defense & DEFENSE_POIS)
295             return TRUE;
296     }
297
298     if (creature_ptr->ult_res)
299         return TRUE;
300
301     if (creature_ptr->tsuyoshi)
302         return TRUE;
303
304     if ((creature_ptr->special_attack & ATTACK_ACID) && !(r_ptr->flagsr & RFR_EFF_IM_ACID_MASK))
305         return TRUE;
306
307     if ((creature_ptr->special_attack & ATTACK_FIRE) && !(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
308         return TRUE;
309
310     if ((creature_ptr->special_attack & ATTACK_ELEC) && !(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
311         return TRUE;
312
313     if ((creature_ptr->special_attack & ATTACK_COLD) && !(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
314         return TRUE;
315
316     if ((creature_ptr->special_attack & ATTACK_POIS) && !(r_ptr->flagsr & RFR_EFF_IM_POIS_MASK))
317         return TRUE;
318
319     if ((creature_ptr->pspeed < 145) && is_fast(creature_ptr))
320         return TRUE;
321
322     if (creature_ptr->lightspeed && (m_ptr->mspeed < 136))
323         return TRUE;
324
325     if (creature_ptr->riding && (creature_ptr->current_floor_ptr->m_list[creature_ptr->riding].mspeed < 135)
326         && monster_fast_remaining(&creature_ptr->current_floor_ptr->m_list[creature_ptr->riding]))
327         return TRUE;
328
329     return FALSE;
330 }