OSDN Git Service

Merge pull request #2241 from sikabane-works/release/3.0.0Alpha53
[hengbandforosx/hengbandosx.git] / src / melee / melee-spell-flags-checker.cpp
1 #include "melee/melee-spell-flags-checker.h"
2 #include "dungeon/dungeon-flag-types.h"
3 #include "dungeon/dungeon.h"
4 #include "effect/effect-characteristics.h"
5 #include "floor/geometry.h"
6 #include "floor/line-of-sight.h"
7 #include "melee/melee-spell-util.h"
8 #include "monster-floor/monster-move.h"
9 #include "monster-race/monster-race.h"
10 #include "monster-race/race-ability-mask.h"
11 #include "monster-race/race-flags2.h"
12 #include "monster-race/race-flags3.h"
13 #include "monster-race/race-flags7.h"
14 #include "monster-race/race-indice-types.h"
15 #include "monster/monster-info.h"
16 #include "monster/monster-status.h"
17 #include "mspell/mspell-checker.h"
18 #include "mspell/mspell-judgement.h"
19 #include "mspell/mspell-util.h"
20 #include "pet/pet-util.h"
21 #include "player-base/player-class.h"
22 #include "spell-kind/spells-world.h"
23 #include "system/floor-type-definition.h"
24 #include "system/grid-type-definition.h"
25 #include "system/monster-race-definition.h"
26 #include "system/monster-type-definition.h"
27 #include "system/player-type-definition.h"
28 #include "target/projection-path-calculator.h"
29 #include "util/bit-flags-calculator.h"
30
31 #include <iterator>
32
33 static void decide_melee_spell_target(PlayerType *player_ptr, melee_spell_type *ms_ptr)
34 {
35     if ((player_ptr->pet_t_m_idx == 0) || !ms_ptr->pet)
36         return;
37
38     ms_ptr->target_idx = player_ptr->pet_t_m_idx;
39     ms_ptr->t_ptr = &player_ptr->current_floor_ptr->m_list[ms_ptr->target_idx];
40     if ((ms_ptr->m_idx == ms_ptr->target_idx) || !projectable(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
41         ms_ptr->target_idx = 0;
42 }
43
44 static void decide_indirection_melee_spell(PlayerType *player_ptr, melee_spell_type *ms_ptr)
45 {
46     if ((ms_ptr->target_idx != 0) || (ms_ptr->m_ptr->target_y == 0))
47         return;
48
49     auto *floor_ptr = player_ptr->current_floor_ptr;
50     ms_ptr->target_idx = floor_ptr->grid_array[ms_ptr->m_ptr->target_y][ms_ptr->m_ptr->target_x].m_idx;
51     if (ms_ptr->target_idx == 0)
52         return;
53
54     ms_ptr->t_ptr = &floor_ptr->m_list[ms_ptr->target_idx];
55     if ((ms_ptr->m_idx == ms_ptr->target_idx) || ((ms_ptr->target_idx != player_ptr->pet_t_m_idx) && !are_enemies(player_ptr, ms_ptr->m_ptr, ms_ptr->t_ptr))) {
56         ms_ptr->target_idx = 0;
57         return;
58     }
59
60     if (projectable(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
61         return;
62
63     ms_ptr->ability_flags &= RF_ABILITY_INDIRECT_MASK;
64 }
65
66 static bool check_melee_spell_projection(PlayerType *player_ptr, melee_spell_type *ms_ptr)
67 {
68     if (ms_ptr->target_idx != 0)
69         return true;
70
71     int start;
72     int plus = 1;
73     auto *floor_ptr = player_ptr->current_floor_ptr;
74     if (player_ptr->phase_out) {
75         start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
76         if (randint0(2))
77             plus = -1;
78     } else
79         start = floor_ptr->m_max + 1;
80
81     for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus) {
82         MONSTER_IDX dummy = (i % floor_ptr->m_max);
83         if (!dummy)
84             continue;
85
86         ms_ptr->target_idx = dummy;
87         ms_ptr->t_ptr = &floor_ptr->m_list[ms_ptr->target_idx];
88         if (!monster_is_valid(ms_ptr->t_ptr) || (ms_ptr->m_idx == ms_ptr->target_idx) || !are_enemies(player_ptr, ms_ptr->m_ptr, ms_ptr->t_ptr) || !projectable(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
89             continue;
90
91         return true;
92     }
93
94     return false;
95 }
96
97 static void check_darkness(PlayerType *player_ptr, melee_spell_type *ms_ptr)
98 {
99     if (ms_ptr->ability_flags.has_not(MonsterAbilityType::DARKNESS))
100         return;
101
102     bool vs_ninja = PlayerClass(player_ptr).equals(PlayerClassType::NINJA) && !is_hostile(ms_ptr->t_ptr);
103     bool can_use_lite_area = vs_ninja && ms_ptr->r_ptr->kind_flags.has_not(MonsterKindType::UNDEAD) && ms_ptr->r_ptr->resistance_flags.has_not(MonsterResistanceType::HURT_LITE) && !(ms_ptr->r_ptr->flags7 & RF7_DARK_MASK);
104     if (ms_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID))
105         return;
106
107     if (d_info[player_ptr->dungeon_idx].flags.has(DungeonFeatureType::DARKNESS)) {
108         ms_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
109         return;
110     }
111
112     if (vs_ninja && !can_use_lite_area)
113         ms_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
114 }
115
116 static void check_stupid(melee_spell_type *ms_ptr)
117 {
118     if (!ms_ptr->in_no_magic_dungeon || ms_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID))
119         return;
120
121     ms_ptr->ability_flags &= RF_ABILITY_NOMAGIC_MASK;
122 }
123
124 static void check_arena(PlayerType *player_ptr, melee_spell_type *ms_ptr)
125 {
126     if (!player_ptr->current_floor_ptr->inside_arena && !player_ptr->phase_out)
127         return;
128
129     ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK).reset(MonsterAbilityType::TELE_LEVEL);
130     if (ms_ptr->m_ptr->r_idx == MON_ROLENTO)
131         ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
132 }
133
134 static void check_melee_spell_distance(PlayerType *player_ptr, melee_spell_type *ms_ptr)
135 {
136     auto ball_mask_except_rocket = RF_ABILITY_BALL_MASK;
137     ball_mask_except_rocket.reset(MonsterAbilityType::ROCKET);
138     if (ms_ptr->ability_flags.has_none_of(ball_mask_except_rocket))
139         return;
140
141     POSITION real_y = ms_ptr->y;
142     POSITION real_x = ms_ptr->x;
143     get_project_point(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, &real_y, &real_x, 0L);
144     if (!projectable(player_ptr, real_y, real_x, player_ptr->y, player_ptr->x) && ms_ptr->ability_flags.has(MonsterAbilityType::BA_LITE) && (distance(real_y, real_x, player_ptr->y, player_ptr->x) <= 4) && los(player_ptr, real_y, real_x, player_ptr->y, player_ptr->x)) {
145         ms_ptr->ability_flags.reset(MonsterAbilityType::BA_LITE);
146
147         return;
148     }
149
150     int dist = distance(real_y, real_x, player_ptr->y, player_ptr->x);
151     if (dist <= 2) {
152         ms_ptr->ability_flags.reset(ball_mask_except_rocket);
153         return;
154     }
155
156     if (dist > 4)
157         return;
158
159     ms_ptr->ability_flags.reset(RF_ABILITY_BIG_BALL_MASK);
160 }
161
162 static void check_melee_spell_rocket(PlayerType *player_ptr, melee_spell_type *ms_ptr)
163 {
164     if (ms_ptr->ability_flags.has_not(MonsterAbilityType::ROCKET))
165         return;
166
167     POSITION real_y = ms_ptr->y;
168     POSITION real_x = ms_ptr->x;
169     get_project_point(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
170     if (projectable(player_ptr, real_y, real_x, player_ptr->y, player_ptr->x) && (distance(real_y, real_x, player_ptr->y, player_ptr->x) <= 2))
171         ms_ptr->ability_flags.reset(MonsterAbilityType::ROCKET);
172 }
173
174 static void check_melee_spell_beam(PlayerType *player_ptr, melee_spell_type *ms_ptr)
175 {
176     if (ms_ptr->ability_flags.has_none_of(RF_ABILITY_BEAM_MASK) || direct_beam(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx, ms_ptr->m_ptr))
177         return;
178
179     ms_ptr->ability_flags.reset(RF_ABILITY_BEAM_MASK);
180 }
181
182 static void check_melee_spell_breath(PlayerType *player_ptr, melee_spell_type *ms_ptr)
183 {
184     if (ms_ptr->ability_flags.has_none_of(RF_ABILITY_BREATH_MASK))
185         return;
186
187     POSITION rad = (ms_ptr->r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
188     if (!breath_direct(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx, rad, AttributeType::NONE, true)) {
189         ms_ptr->ability_flags.reset(RF_ABILITY_BREATH_MASK);
190         return;
191     }
192
193     if (ms_ptr->ability_flags.has(MonsterAbilityType::BR_LITE) && !breath_direct(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx,
194                                                                       ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx, rad, AttributeType::LITE, true)) {
195         ms_ptr->ability_flags.reset(MonsterAbilityType::BR_LITE);
196         return;
197     }
198
199     if (ms_ptr->ability_flags.has(MonsterAbilityType::BR_DISI) && !breath_direct(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx,
200                                                                       ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx, rad, AttributeType::DISINTEGRATE, true)) {
201         ms_ptr->ability_flags.reset(MonsterAbilityType::BR_DISI);
202     }
203 }
204
205 static void check_melee_spell_special(PlayerType *player_ptr, melee_spell_type *ms_ptr)
206 {
207     if (ms_ptr->ability_flags.has_not(MonsterAbilityType::SPECIAL))
208         return;
209
210     if (ms_ptr->m_ptr->r_idx == MON_ROLENTO) {
211         if ((player_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
212             ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
213
214         return;
215     }
216
217     if (ms_ptr->r_ptr->d_char == 'B') {
218         if ((player_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
219             ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
220
221         return;
222     }
223
224     ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
225 }
226
227 static void check_riding(PlayerType *player_ptr, melee_spell_type *ms_ptr)
228 {
229     if (ms_ptr->m_idx != player_ptr->riding)
230         return;
231
232     ms_ptr->ability_flags.reset(RF_ABILITY_RIDING_MASK);
233 }
234
235 static void check_pet(PlayerType *player_ptr, melee_spell_type *ms_ptr)
236 {
237     if (!ms_ptr->pet)
238         return;
239
240     ms_ptr->ability_flags.reset({ MonsterAbilityType::SHRIEK, MonsterAbilityType::DARKNESS, MonsterAbilityType::TRAPS });
241     if (!(player_ptr->pet_extra_flags & PF_TELEPORT))
242         ms_ptr->ability_flags.reset({ MonsterAbilityType::BLINK, MonsterAbilityType::TPORT, MonsterAbilityType::TELE_TO, MonsterAbilityType::TELE_AWAY, MonsterAbilityType::TELE_LEVEL });
243
244     if (!(player_ptr->pet_extra_flags & PF_ATTACK_SPELL)) {
245         ms_ptr->ability_flags.reset(RF_ABILITY_ATTACK_MASK);
246     }
247
248     if (!(player_ptr->pet_extra_flags & PF_SUMMON_SPELL)) {
249         ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK);
250     }
251
252     if (!(player_ptr->pet_extra_flags & PF_BALL_SPELL) && (ms_ptr->m_idx != player_ptr->riding)) {
253         check_melee_spell_distance(player_ptr, ms_ptr);
254         check_melee_spell_rocket(player_ptr, ms_ptr);
255         check_melee_spell_beam(player_ptr, ms_ptr);
256         check_melee_spell_breath(player_ptr, ms_ptr);
257     }
258
259     check_melee_spell_special(player_ptr, ms_ptr);
260 }
261
262 static void check_non_stupid(PlayerType *player_ptr, melee_spell_type *ms_ptr)
263 {
264     if (ms_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID))
265         return;
266
267     if (ms_ptr->ability_flags.has_any_of(RF_ABILITY_BOLT_MASK) && !clean_shot(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx, ms_ptr->pet)) {
268         ms_ptr->ability_flags.reset(RF_ABILITY_BOLT_MASK);
269     }
270
271     if (ms_ptr->ability_flags.has_any_of(RF_ABILITY_SUMMON_MASK) && !(summon_possible(player_ptr, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))) {
272         ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK);
273     }
274
275     if (ms_ptr->ability_flags.has(MonsterAbilityType::DISPEL) && !dispel_check_monster(player_ptr, ms_ptr->m_idx, ms_ptr->target_idx))
276         ms_ptr->ability_flags.reset(MonsterAbilityType::DISPEL);
277
278     if (ms_ptr->ability_flags.has(MonsterAbilityType::RAISE_DEAD) && !raise_possible(player_ptr, ms_ptr->m_ptr))
279         ms_ptr->ability_flags.reset(MonsterAbilityType::RAISE_DEAD);
280
281     if (ms_ptr->ability_flags.has(MonsterAbilityType::SPECIAL) && (ms_ptr->m_ptr->r_idx == MON_ROLENTO) && !summon_possible(player_ptr, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
282         ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
283 }
284
285 static void check_smart(PlayerType *player_ptr, melee_spell_type *ms_ptr)
286 {
287     if (ms_ptr->r_ptr->behavior_flags.has_not(MonsterBehaviorType::SMART))
288         return;
289
290     if ((ms_ptr->m_ptr->hp < ms_ptr->m_ptr->maxhp / 10) && (randint0(100) < 50)) {
291         ms_ptr->ability_flags &= RF_ABILITY_INT_MASK;
292     }
293
294     if (ms_ptr->ability_flags.has(MonsterAbilityType::TELE_LEVEL) && is_teleport_level_ineffective(player_ptr, (ms_ptr->target_idx == player_ptr->riding) ? 0 : ms_ptr->target_idx))
295         ms_ptr->ability_flags.reset(MonsterAbilityType::TELE_LEVEL);
296 }
297
298 static bool set_melee_spell_set(PlayerType *player_ptr, melee_spell_type *ms_ptr)
299 {
300     if (ms_ptr->ability_flags.none())
301         return false;
302
303     EnumClassFlagGroup<MonsterAbilityType>::get_flags(ms_ptr->ability_flags, std::back_inserter(ms_ptr->spells));
304
305     return !ms_ptr->spells.empty() && player_ptr->playing && !player_ptr->is_dead && !player_ptr->leaving;
306 }
307
308 bool check_melee_spell_set(PlayerType *player_ptr, melee_spell_type *ms_ptr)
309 {
310     if (monster_confused_remaining(ms_ptr->m_ptr))
311         return false;
312
313     ms_ptr->ability_flags = ms_ptr->r_ptr->ability_flags;
314     decide_melee_spell_target(player_ptr, ms_ptr);
315     decide_indirection_melee_spell(player_ptr, ms_ptr);
316     if (!check_melee_spell_projection(player_ptr, ms_ptr))
317         return false;
318
319     ms_ptr->y = ms_ptr->t_ptr->fy;
320     ms_ptr->x = ms_ptr->t_ptr->fx;
321     reset_target(ms_ptr->m_ptr);
322     ms_ptr->ability_flags.reset({ MonsterAbilityType::WORLD, MonsterAbilityType::TRAPS, MonsterAbilityType::FORGET });
323     if (ms_ptr->ability_flags.has(MonsterAbilityType::BR_LITE) && !los(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
324         ms_ptr->ability_flags.reset(MonsterAbilityType::BR_LITE);
325
326     if (ms_ptr->ability_flags.has(MonsterAbilityType::SPECIAL) && (ms_ptr->m_ptr->r_idx != MON_ROLENTO) && (ms_ptr->r_ptr->d_char != 'B'))
327         ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
328
329     check_darkness(player_ptr, ms_ptr);
330     check_stupid(ms_ptr);
331     check_arena(player_ptr, ms_ptr);
332     if (player_ptr->phase_out && !one_in_(3))
333         ms_ptr->ability_flags.reset(MonsterAbilityType::HEAL);
334
335     check_riding(player_ptr, ms_ptr);
336     check_pet(player_ptr, ms_ptr);
337     check_non_stupid(player_ptr, ms_ptr);
338     check_smart(player_ptr, ms_ptr);
339     return set_melee_spell_set(player_ptr, ms_ptr);
340 }