OSDN Git Service

[Refactor] #3286 Removed player-redraw-types.h
[hengbandforosx/hengbandosx.git] / src / mspell / mspell-checker.cpp
1 /*!
2  * @brief モンスター魔法の実装 / Monster spells (attack player)
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-checker.h"
13 #include "blue-magic/blue-magic-checker.h"
14 #include "core/disturbance.h"
15 #include "dungeon/dungeon-flag-types.h"
16 #include "dungeon/quest.h"
17 #include "effect/attribute-types.h"
18 #include "effect/effect-characteristics.h"
19 #include "effect/effect-processor.h"
20 #include "floor/cave.h"
21 #include "floor/geometry.h"
22 #include "floor/line-of-sight.h"
23 #include "monster-floor/monster-move.h"
24 #include "monster-race/monster-race.h"
25 #include "monster-race/race-ability-mask.h"
26 #include "monster-race/race-flags2.h"
27 #include "monster-race/race-flags3.h"
28 #include "monster-race/race-flags7.h"
29 #include "monster-race/race-indice-types.h"
30 #include "monster/monster-describer.h"
31 #include "monster/monster-description-types.h"
32 #include "monster/monster-flag-types.h"
33 #include "monster/monster-info.h"
34 #include "monster/monster-status.h"
35 #include "mspell/assign-monster-spell.h"
36 #include "mspell/improper-mspell-remover.h"
37 #include "mspell/mspell-judgement.h"
38 #include "mspell/mspell-learn-checker.h"
39 #include "mspell/mspell-selector.h"
40 #include "mspell/mspell-util.h"
41 #include "object-enchant/object-curse.h"
42 #include "player-info/class-info.h"
43 #include "player-info/race-types.h"
44 #include "player/attack-defense-types.h"
45 #include "spell-kind/spells-world.h"
46 #include "spell-realm/spells-hex.h"
47 #include "spell/range-calc.h"
48 #include "system/dungeon-info.h"
49 #include "system/floor-type-definition.h"
50 #include "system/grid-type-definition.h"
51 #include "system/item-entity.h"
52 #include "system/monster-entity.h"
53 #include "system/monster-race-info.h"
54 #include "system/player-type-definition.h"
55 #include "target/projection-path-calculator.h"
56 #include "util/bit-flags-calculator.h"
57 #include "view/display-messages.h"
58 #include "world/world.h"
59
60 /*!
61  * @brief モンスターにとって所定の地点が召還に相応しい地点かどうかを返す。 /
62  * Determine if there is a space near the player in which a summoned creature can appear
63  * @param player_ptr プレイヤーへの参照ポインタ
64  * @param y1 判定を行いたいマスのY座標
65  * @param x1 判定を行いたいマスのX座標
66  * @return 召還に相応しいならばTRUEを返す
67  */
68 bool summon_possible(PlayerType *player_ptr, POSITION y1, POSITION x1)
69 {
70     auto *floor_ptr = player_ptr->current_floor_ptr;
71     for (POSITION y = y1 - 2; y <= y1 + 2; y++) {
72         for (POSITION x = x1 - 2; x <= x1 + 2; x++) {
73             if (!in_bounds(floor_ptr, y, x)) {
74                 continue;
75             }
76
77             if (distance(y1, x1, y, x) > 2) {
78                 continue;
79             }
80
81             if (pattern_tile(floor_ptr, y, x)) {
82                 continue;
83             }
84
85             if (is_cave_empty_bold(player_ptr, y, x) && projectable(player_ptr, y1, x1, y, x) && projectable(player_ptr, y, x, y1, x1)) {
86                 return true;
87             }
88         }
89     }
90
91     return false;
92 }
93
94 /*!
95  * @brief モンスターにとって死者復活を行うべき状態かどうかを返す /
96  * Determine if there is a space near the player in which a summoned creature can appear
97  * @param player_ptr プレイヤーへの参照ポインタ
98  * @param m_ptr 判定を行いたいモンスターの構造体参照ポインタ
99  * @return 死者復活が有効な状態ならばTRUEを返す。
100  */
101 bool raise_possible(PlayerType *player_ptr, MonsterEntity *m_ptr)
102 {
103     POSITION y = m_ptr->fy;
104     POSITION x = m_ptr->fx;
105     auto *floor_ptr = player_ptr->current_floor_ptr;
106     for (POSITION xx = x - 5; xx <= x + 5; xx++) {
107         grid_type *g_ptr;
108         for (POSITION yy = y - 5; yy <= y + 5; yy++) {
109             if (distance(y, x, yy, xx) > 5) {
110                 continue;
111             }
112             if (!los(player_ptr, y, x, yy, xx)) {
113                 continue;
114             }
115             if (!projectable(player_ptr, y, x, yy, xx)) {
116                 continue;
117             }
118
119             g_ptr = &floor_ptr->grid_array[yy][xx];
120             for (const auto this_o_idx : g_ptr->o_idx_list) {
121                 const auto &item = floor_ptr->o_list[this_o_idx];
122                 if (item.bi_key.tval() == ItemKindType::CORPSE) {
123                     auto corpse_r_idx = i2enum<MonsterRaceId>(item.pval);
124                     if (!monster_has_hostile_align(player_ptr, m_ptr, 0, 0, &monraces_info[corpse_r_idx])) {
125                         return true;
126                     }
127                 }
128             }
129         }
130     }
131
132     return false;
133 }
134
135 /*!
136  * @brief モンスターにとってボルト型魔法が有効な状態かを返す /
137  * Determine if a bolt spell will hit the player.
138  * @param player_ptr プレイヤーへの参照ポインタ
139  * @param y1 ボルト魔法発射地点のY座標
140  * @param x1 ボルト魔法発射地点のX座標
141  * @param y2 ボルト魔法目標地点のY座標
142  * @param x2 ボルト魔法目標地点のX座標
143  * @param is_friend モンスターがプレイヤーに害意を持たない(ペットか友好的)ならばTRUEをつける
144  * @return ボルト型魔法が有効ならばTRUEを返す。
145  * @details
146  * Originally, it was possible for a friendly to shoot another friendly.\n
147  * Change it so a "clean shot" means no equally friendly monster is\n
148  * between the attacker and target.\n
149  *\n
150  * This is exactly like "projectable", but it will\n
151  * return FALSE if a monster is in the way.\n
152  * no equally friendly monster is\n
153  * between the attacker and target.\n
154  */
155 bool clean_shot(PlayerType *player_ptr, POSITION y1, POSITION x1, POSITION y2, POSITION x2, bool is_friend)
156 {
157     auto *floor_ptr = player_ptr->current_floor_ptr;
158     projection_path grid_g(player_ptr, get_max_range(player_ptr), y1, x1, y2, x2, 0);
159     if (grid_g.path_num() == 0) {
160         return false;
161     }
162
163     const auto [last_y, last_x] = grid_g.back();
164     if ((last_y != y2) || (last_x != x2)) {
165         return false;
166     }
167
168     for (const auto &[y, x] : grid_g) {
169         if ((floor_ptr->grid_array[y][x].m_idx > 0) && (y != y2 || x != x2)) {
170             auto *m_ptr = &floor_ptr->m_list[floor_ptr->grid_array[y][x].m_idx];
171             if (is_friend == m_ptr->is_pet()) {
172                 return false;
173             }
174         }
175
176         if (player_bold(player_ptr, y, x) && is_friend) {
177             return false;
178         }
179     }
180
181     return true;
182 }
183
184 /*!
185  * @brief モンスターのボルト型魔法処理 /
186  * Cast a bolt at the player Stop if we hit a monster Affect monsters and the player
187  * @param player_ptr プレイヤーへの参照ポインタ
188  * @param m_idx モンスターのID
189  * @param y 目標のY座標
190  * @param x 目標のX座標
191  * @param typ 効果属性ID
192  * @param dam_hp 威力
193  * @param monspell モンスター魔法のID
194  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
195  */
196 ProjectResult bolt(PlayerType *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, AttributeType typ, int dam_hp, int target_type)
197 {
198     BIT_FLAGS flg = 0;
199     switch (target_type) {
200     case MONSTER_TO_MONSTER:
201         flg = PROJECT_STOP | PROJECT_KILL;
202         break;
203     case MONSTER_TO_PLAYER:
204         flg = PROJECT_STOP | PROJECT_KILL | PROJECT_PLAYER;
205         break;
206     }
207
208     if (typ != AttributeType::MONSTER_SHOOT) {
209         flg |= PROJECT_REFLECTABLE;
210     }
211
212     return project(player_ptr, m_idx, 0, y, x, dam_hp, typ, flg);
213 }
214
215 /*!
216  * @brief モンスターのビーム型魔法処理 /
217  * @param player_ptr プレイヤーへの参照ポインタ
218  * @param m_idx モンスターのID
219  * @param y 目標のY座標
220  * @param x 目標のX座標
221  * @param typ 効果属性ID
222  * @param dam_hp 威力
223  * @param monspell モンスター魔法のID
224  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
225  */
226 ProjectResult beam(PlayerType *player_ptr, MONSTER_IDX m_idx, POSITION y, POSITION x, AttributeType typ, int dam_hp, int target_type)
227 {
228     BIT_FLAGS flg = 0;
229     switch (target_type) {
230     case MONSTER_TO_MONSTER:
231         flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU;
232         break;
233     case MONSTER_TO_PLAYER:
234         flg = PROJECT_BEAM | PROJECT_KILL | PROJECT_THRU | PROJECT_PLAYER;
235         break;
236     }
237
238     return project(player_ptr, m_idx, 0, y, x, dam_hp, typ, flg);
239 }
240
241 ProjectResult ball(PlayerType *player_ptr, POSITION y, POSITION x, MONSTER_IDX m_idx, AttributeType typ, int dam_hp, POSITION rad, int target_type)
242 {
243     BIT_FLAGS flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL;
244     if (target_type == MONSTER_TO_PLAYER) {
245         flg |= PROJECT_PLAYER;
246     }
247
248     return project(player_ptr, m_idx, rad, y, x, dam_hp, typ, flg);
249 }
250
251 /*!
252  * @brief モンスターのボール型&ブレス型魔法処理 /
253  * Cast a breath (or ball) attack at the player Pass over any monsters that may be in the way Affect grids, objects, monsters, and the player
254  * @param player_ptr プレイヤーへの参照ポインタ
255  * @param y 目標地点のY座標
256  * @param x 目標地点のX座標
257  * @param m_idx モンスターのID
258  * @param typ 効果属性ID
259  * @param dam_hp 威力
260  * @param rad 半径
261  * @param monspell モンスター魔法のID
262  * @param target_type モンスターからモンスターへ撃つならMONSTER_TO_MONSTER、モンスターからプレイヤーならMONSTER_TO_PLAYER
263  */
264 ProjectResult breath(PlayerType *player_ptr, POSITION y, POSITION x, MONSTER_IDX m_idx, AttributeType typ, int dam_hp, POSITION rad, int target_type)
265 {
266     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
267     auto *r_ptr = &monraces_info[m_ptr->r_idx];
268     BIT_FLAGS flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_BREATH;
269     if (target_type == MONSTER_TO_PLAYER) {
270         flg |= PROJECT_PLAYER;
271     }
272
273     if (rad < 1) {
274         rad = (r_ptr->flags2 & (RF2_POWERFUL)) ? 3 : 2;
275     }
276
277     return project(player_ptr, m_idx, rad, y, x, dam_hp, typ, flg);
278 }
279
280 ProjectResult pointed(PlayerType *player_ptr, POSITION y, POSITION x, MONSTER_IDX m_idx, AttributeType typ, int dam_hp, int target_type)
281 {
282     BIT_FLAGS flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_HIDE | PROJECT_AIMED;
283     if (target_type == MONSTER_TO_PLAYER) {
284         flg |= PROJECT_PLAYER;
285     }
286
287     return project(player_ptr, m_idx, 0, y, x, dam_hp, typ, flg);
288 }
289
290 ProjectResult rocket(PlayerType *player_ptr, POSITION y, POSITION x, MONSTER_IDX m_idx, AttributeType typ, int dam_hp, POSITION rad, int target_type)
291 {
292     BIT_FLAGS flg = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_STOP;
293     if (target_type == MONSTER_TO_PLAYER) {
294         flg |= PROJECT_PLAYER;
295     }
296
297     return project(player_ptr, m_idx, rad, y, x, dam_hp, typ, flg);
298 }
299
300 /*!
301  * @brief ID値が非魔術的な特殊技能かどうかを返す /
302  * Return TRUE if a spell is inate spell.
303  * @param spell 判定対象のID
304  * @return 非魔術的な特殊技能ならばTRUEを返す。
305  */
306 bool spell_is_inate(MonsterAbilityType spell)
307 {
308     return RF_ABILITY_NOMAGIC_MASK.has(spell);
309 }