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"
33 static void decide_melee_spell_target(PlayerType *player_ptr, melee_spell_type *ms_ptr)
35 if ((player_ptr->pet_t_m_idx == 0) || !ms_ptr->pet)
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;
44 static void decide_indirection_melee_spell(PlayerType *player_ptr, melee_spell_type *ms_ptr)
46 if ((ms_ptr->target_idx != 0) || (ms_ptr->m_ptr->target_y == 0))
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)
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;
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))
63 ms_ptr->ability_flags &= RF_ABILITY_INDIRECT_MASK;
66 static bool check_melee_spell_projection(PlayerType *player_ptr, melee_spell_type *ms_ptr)
68 if (ms_ptr->target_idx != 0)
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;
79 start = floor_ptr->m_max + 1;
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);
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))
97 static void check_darkness(PlayerType *player_ptr, melee_spell_type *ms_ptr)
99 if (ms_ptr->ability_flags.has_not(MonsterAbilityType::DARKNESS))
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))
107 if (d_info[player_ptr->dungeon_idx].flags.has(DungeonFeatureType::DARKNESS)) {
108 ms_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
112 if (vs_ninja && !can_use_lite_area)
113 ms_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
116 static void check_stupid(melee_spell_type *ms_ptr)
118 if (!ms_ptr->in_no_magic_dungeon || ms_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID))
121 ms_ptr->ability_flags &= RF_ABILITY_NOMAGIC_MASK;
124 static void check_arena(PlayerType *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(MonsterAbilityType::TELE_LEVEL);
130 if (ms_ptr->m_ptr->r_idx == MON_ROLENTO)
131 ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
134 static void check_melee_spell_distance(PlayerType *player_ptr, melee_spell_type *ms_ptr)
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))
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);
150 int dist = distance(real_y, real_x, player_ptr->y, player_ptr->x);
152 ms_ptr->ability_flags.reset(ball_mask_except_rocket);
159 ms_ptr->ability_flags.reset(RF_ABILITY_BIG_BALL_MASK);
162 static void check_melee_spell_rocket(PlayerType *player_ptr, melee_spell_type *ms_ptr)
164 if (ms_ptr->ability_flags.has_not(MonsterAbilityType::ROCKET))
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);
174 static void check_melee_spell_beam(PlayerType *player_ptr, melee_spell_type *ms_ptr)
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))
179 ms_ptr->ability_flags.reset(RF_ABILITY_BEAM_MASK);
182 static void check_melee_spell_breath(PlayerType *player_ptr, melee_spell_type *ms_ptr)
184 if (ms_ptr->ability_flags.has_none_of(RF_ABILITY_BREATH_MASK))
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);
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);
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);
205 static void check_melee_spell_special(PlayerType *player_ptr, melee_spell_type *ms_ptr)
207 if (ms_ptr->ability_flags.has_not(MonsterAbilityType::SPECIAL))
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);
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);
224 ms_ptr->ability_flags.reset(MonsterAbilityType::SPECIAL);
227 static void check_riding(PlayerType *player_ptr, melee_spell_type *ms_ptr)
229 if (ms_ptr->m_idx != player_ptr->riding)
232 ms_ptr->ability_flags.reset(RF_ABILITY_RIDING_MASK);
235 static void check_pet(PlayerType *player_ptr, melee_spell_type *ms_ptr)
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 });
244 if (!(player_ptr->pet_extra_flags & PF_ATTACK_SPELL)) {
245 ms_ptr->ability_flags.reset(RF_ABILITY_ATTACK_MASK);
248 if (!(player_ptr->pet_extra_flags & PF_SUMMON_SPELL)) {
249 ms_ptr->ability_flags.reset(RF_ABILITY_SUMMON_MASK);
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);
259 check_melee_spell_special(player_ptr, ms_ptr);
262 static void check_non_stupid(PlayerType *player_ptr, melee_spell_type *ms_ptr)
264 if (ms_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID))
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);
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);
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);
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);
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);
285 static void check_smart(PlayerType *player_ptr, melee_spell_type *ms_ptr)
287 if (ms_ptr->r_ptr->behavior_flags.has_not(MonsterBehaviorType::SMART))
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;
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);
298 static bool set_melee_spell_set(PlayerType *player_ptr, melee_spell_type *ms_ptr)
300 if (ms_ptr->ability_flags.none())
303 EnumClassFlagGroup<MonsterAbilityType>::get_flags(ms_ptr->ability_flags, std::back_inserter(ms_ptr->spells));
305 return !ms_ptr->spells.empty() && player_ptr->playing && !player_ptr->is_dead && !player_ptr->leaving;
308 bool check_melee_spell_set(PlayerType *player_ptr, melee_spell_type *ms_ptr)
310 if (monster_confused_remaining(ms_ptr->m_ptr))
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))
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);
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);
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);
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);