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/line-of-sight.h"
6 #include "floor/geometry.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 "spell-kind/spells-world.h"
22 #include "spell/spell-types.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"
32 static void decide_melee_spell_target(player_type *player_ptr, melee_spell_type *ms_ptr)
34 if ((player_ptr->pet_t_m_idx == 0) || !ms_ptr->pet)
37 ms_ptr->target_idx = player_ptr->pet_t_m_idx;
38 ms_ptr->t_ptr = &player_ptr->current_floor_ptr->m_list[ms_ptr->target_idx];
39 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))
40 ms_ptr->target_idx = 0;
43 static void decide_indirection_melee_spell(player_type *player_ptr, melee_spell_type *ms_ptr)
45 if ((ms_ptr->target_idx != 0) || (ms_ptr->m_ptr->target_y == 0))
48 floor_type *floor_ptr = player_ptr->current_floor_ptr;
49 ms_ptr->target_idx = floor_ptr->grid_array[ms_ptr->m_ptr->target_y][ms_ptr->m_ptr->target_x].m_idx;
50 if (ms_ptr->target_idx == 0)
53 ms_ptr->t_ptr = &floor_ptr->m_list[ms_ptr->target_idx];
54 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))) {
55 ms_ptr->target_idx = 0;
59 if (projectable(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
62 ms_ptr->ability_flags &= RF_ABILITY_INDIRECT_MASK;
65 static bool check_melee_spell_projection(player_type *player_ptr, melee_spell_type *ms_ptr)
67 if (ms_ptr->target_idx != 0)
72 floor_type *floor_ptr = player_ptr->current_floor_ptr;
73 if (player_ptr->phase_out) {
74 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
78 start = floor_ptr->m_max + 1;
80 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus) {
81 MONSTER_IDX dummy = (i % floor_ptr->m_max);
85 ms_ptr->target_idx = dummy;
86 ms_ptr->t_ptr = &floor_ptr->m_list[ms_ptr->target_idx];
87 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)
88 || !projectable(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
97 static void check_darkness(player_type *player_ptr, melee_spell_type *ms_ptr)
99 if (ms_ptr->ability_flags.has_not(RF_ABILITY::DARKNESS))
102 bool vs_ninja = (player_ptr->pclass == PlayerClassType::NINJA) && !is_hostile(ms_ptr->t_ptr);
103 bool can_use_lite_area = vs_ninja && !(ms_ptr->r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) && !(ms_ptr->r_ptr->flags7 & RF7_DARK_MASK);
104 if ((ms_ptr->r_ptr->flags2 & RF2_STUPID) != 0)
107 if (d_info[player_ptr->dungeon_idx].flags.has(DF::DARKNESS)) {
108 ms_ptr->ability_flags.reset(RF_ABILITY::DARKNESS);
112 if (vs_ninja && !can_use_lite_area)
113 ms_ptr->ability_flags.reset(RF_ABILITY::DARKNESS);
116 static void check_stupid(melee_spell_type *ms_ptr)
118 if (!ms_ptr->in_no_magic_dungeon || ((ms_ptr->r_ptr->flags2 & RF2_STUPID) != 0))
121 ms_ptr->ability_flags &= RF_ABILITY_NOMAGIC_MASK;
124 static void check_arena(player_type *player_ptr, melee_spell_type *ms_ptr)
126 if (!player_ptr->current_floor_ptr->inside_arena && !player_ptr->phase_out)
129 ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK).reset(RF_ABILITY::TELE_LEVEL);
130 if (ms_ptr->m_ptr->r_idx == MON_ROLENTO)
131 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
134 static void check_melee_spell_distance(player_type *player_ptr, melee_spell_type *ms_ptr)
136 auto ball_mask_except_rocket = RF_ABILITY_BALL_MASK;
137 ball_mask_except_rocket.reset(RF_ABILITY::ROCKET);
138 if (ms_ptr->ability_flags.has_none_of(ball_mask_except_rocket))
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(RF_ABILITY::BA_LITE)
145 && (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)) {
146 ms_ptr->ability_flags.reset(RF_ABILITY::BA_LITE);
151 int dist = distance(real_y, real_x, player_ptr->y, player_ptr->x);
153 ms_ptr->ability_flags.reset(ball_mask_except_rocket);
160 ms_ptr->ability_flags.reset(RF_ABILITY_BIG_BALL_MASK);
163 static void check_melee_spell_rocket(player_type *player_ptr, melee_spell_type *ms_ptr)
165 if (ms_ptr->ability_flags.has_not(RF_ABILITY::ROCKET))
168 POSITION real_y = ms_ptr->y;
169 POSITION real_x = ms_ptr->x;
170 get_project_point(player_ptr, ms_ptr->m_ptr->fy, ms_ptr->m_ptr->fx, &real_y, &real_x, PROJECT_STOP);
171 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))
172 ms_ptr->ability_flags.reset(RF_ABILITY::ROCKET);
175 static void check_melee_spell_beam(player_type *player_ptr, melee_spell_type *ms_ptr)
177 if (ms_ptr->ability_flags.has_none_of(RF_ABILITY_BEAM_MASK)
178 || 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))
181 ms_ptr->ability_flags.reset(RF_ABILITY_BEAM_MASK);
184 static void check_melee_spell_breath(player_type *player_ptr, melee_spell_type *ms_ptr)
186 if (ms_ptr->ability_flags.has_none_of(RF_ABILITY_BREATH_MASK))
189 POSITION rad = (ms_ptr->r_ptr->flags2 & RF2_POWERFUL) ? 3 : 2;
190 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, 0, true)) {
191 ms_ptr->ability_flags.reset(RF_ABILITY_BREATH_MASK);
195 if (ms_ptr->ability_flags.has(RF_ABILITY::BR_LITE)
196 && !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, GF_LITE, true)) {
197 ms_ptr->ability_flags.reset(RF_ABILITY::BR_LITE);
201 if (ms_ptr->ability_flags.has(RF_ABILITY::BR_DISI)
202 && !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, GF_DISINTEGRATE, true)) {
203 ms_ptr->ability_flags.reset(RF_ABILITY::BR_DISI);
207 static void check_melee_spell_special(player_type *player_ptr, melee_spell_type *ms_ptr)
209 if (ms_ptr->ability_flags.has_not(RF_ABILITY::SPECIAL))
212 if (ms_ptr->m_ptr->r_idx == MON_ROLENTO) {
213 if ((player_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_SUMMON_SPELL)) != (PF_ATTACK_SPELL | PF_SUMMON_SPELL))
214 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
219 if (ms_ptr->r_ptr->d_char == 'B') {
220 if ((player_ptr->pet_extra_flags & (PF_ATTACK_SPELL | PF_TELEPORT)) != (PF_ATTACK_SPELL | PF_TELEPORT))
221 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
226 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
229 static void check_riding(player_type *player_ptr, melee_spell_type *ms_ptr)
231 if (ms_ptr->m_idx != player_ptr->riding)
234 ms_ptr->ability_flags.reset(RF_ABILITY_RIDING_MASK);
237 static void check_pet(player_type *player_ptr, melee_spell_type *ms_ptr)
242 ms_ptr->ability_flags.reset({ RF_ABILITY::SHRIEK, RF_ABILITY::DARKNESS, RF_ABILITY::TRAPS });
243 if (!(player_ptr->pet_extra_flags & PF_TELEPORT))
244 ms_ptr->ability_flags.reset({ RF_ABILITY::BLINK, RF_ABILITY::TPORT, RF_ABILITY::TELE_TO, RF_ABILITY::TELE_AWAY, RF_ABILITY::TELE_LEVEL });
246 if (!(player_ptr->pet_extra_flags & PF_ATTACK_SPELL)) {
247 ms_ptr->ability_flags.reset(RF_ABILITY_ATTACK_MASK);
250 if (!(player_ptr->pet_extra_flags & PF_SUMMON_SPELL)) {
251 ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK);
254 if (!(player_ptr->pet_extra_flags & PF_BALL_SPELL) && (ms_ptr->m_idx != player_ptr->riding)) {
255 check_melee_spell_distance(player_ptr, ms_ptr);
256 check_melee_spell_rocket(player_ptr, ms_ptr);
257 check_melee_spell_beam(player_ptr, ms_ptr);
258 check_melee_spell_breath(player_ptr, ms_ptr);
261 check_melee_spell_special(player_ptr, ms_ptr);
264 static void check_non_stupid(player_type *player_ptr, melee_spell_type *ms_ptr)
266 if ((ms_ptr->r_ptr->flags2 & RF2_STUPID) != 0)
269 if (ms_ptr->ability_flags.has_any_of(RF_ABILITY_BOLT_MASK)
270 && !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)) {
271 ms_ptr->ability_flags.reset(RF_ABILITY_BOLT_MASK);
274 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))) {
275 ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK);
278 if (ms_ptr->ability_flags.has(RF_ABILITY::DISPEL) && !dispel_check_monster(player_ptr, ms_ptr->m_idx, ms_ptr->target_idx))
279 ms_ptr->ability_flags.reset(RF_ABILITY::DISPEL);
281 if (ms_ptr->ability_flags.has(RF_ABILITY::RAISE_DEAD) && !raise_possible(player_ptr, ms_ptr->m_ptr))
282 ms_ptr->ability_flags.reset(RF_ABILITY::RAISE_DEAD);
284 if (ms_ptr->ability_flags.has(RF_ABILITY::SPECIAL) && (ms_ptr->m_ptr->r_idx == MON_ROLENTO)
285 && !summon_possible(player_ptr, ms_ptr->t_ptr->fy, ms_ptr->t_ptr->fx))
286 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
289 static void check_smart(player_type *player_ptr, melee_spell_type *ms_ptr)
291 if ((ms_ptr->r_ptr->flags2 & RF2_SMART) == 0)
294 if ((ms_ptr->m_ptr->hp < ms_ptr->m_ptr->maxhp / 10) && (randint0(100) < 50)) {
295 ms_ptr->ability_flags &= RF_ABILITY_INT_MASK;
298 if (ms_ptr->ability_flags.has(RF_ABILITY::TELE_LEVEL)
299 && is_teleport_level_ineffective(player_ptr, (ms_ptr->target_idx == player_ptr->riding) ? 0 : ms_ptr->target_idx))
300 ms_ptr->ability_flags.reset(RF_ABILITY::TELE_LEVEL);
303 static bool set_melee_spell_set(player_type *player_ptr, melee_spell_type *ms_ptr)
305 if (ms_ptr->ability_flags.none())
308 EnumClassFlagGroup<RF_ABILITY>::get_flags(ms_ptr->ability_flags, std::back_inserter(ms_ptr->spells));
310 return !ms_ptr->spells.empty() && player_ptr->playing && !player_ptr->is_dead && !player_ptr->leaving;
313 bool check_melee_spell_set(player_type *player_ptr, melee_spell_type *ms_ptr)
315 if (monster_confused_remaining(ms_ptr->m_ptr))
318 ms_ptr->ability_flags = ms_ptr->r_ptr->ability_flags;
319 decide_melee_spell_target(player_ptr, ms_ptr);
320 decide_indirection_melee_spell(player_ptr, ms_ptr);
321 if (!check_melee_spell_projection(player_ptr, ms_ptr))
324 ms_ptr->y = ms_ptr->t_ptr->fy;
325 ms_ptr->x = ms_ptr->t_ptr->fx;
326 reset_target(ms_ptr->m_ptr);
327 ms_ptr->ability_flags.reset({ RF_ABILITY::WORLD, RF_ABILITY::TRAPS, RF_ABILITY::FORGET });
328 if (ms_ptr->ability_flags.has(RF_ABILITY::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))
329 ms_ptr->ability_flags.reset(RF_ABILITY::BR_LITE);
331 if (ms_ptr->ability_flags.has(RF_ABILITY::SPECIAL) && (ms_ptr->m_ptr->r_idx != MON_ROLENTO) && (ms_ptr->r_ptr->d_char != 'B'))
332 ms_ptr->ability_flags.reset(RF_ABILITY::SPECIAL);
334 check_darkness(player_ptr, ms_ptr);
335 check_stupid(ms_ptr);
336 check_arena(player_ptr, ms_ptr);
337 if (player_ptr->phase_out && !one_in_(3))
338 ms_ptr->ability_flags.reset(RF_ABILITY::HEAL);
340 check_riding(player_ptr, ms_ptr);
341 check_pet(player_ptr, ms_ptr);
342 check_non_stupid(player_ptr, ms_ptr);
343 check_smart(player_ptr, ms_ptr);
344 return set_melee_spell_set(player_ptr, ms_ptr);