OSDN Git Service

[Implement] ウサウサストライカー召喚処理を追加
[hengbandforosx/hengbandosx.git] / src / monster-attack / monster-attack-processor.cpp
1 /*!
2  * @brief モンスターの攻撃に関する処理
3  * @date 2020/03/08
4  * @author Hourier
5  */
6
7 #include "monster-attack/monster-attack-processor.h"
8 #include "dungeon/dungeon-flag-types.h"
9 #include "floor/cave.h"
10 #include "melee/monster-attack-monster.h"
11 #include "monster-attack/monster-attack-player.h"
12 #include "monster-race/monster-race.h"
13 #include "monster-race/race-flags1.h"
14 #include "monster-race/race-flags2.h"
15 #include "monster/monster-info.h"
16 #include "monster/monster-processor-util.h"
17 #include "monster/monster-status-setter.h"
18 #include "monster/monster-status.h"
19 #include "system/dungeon-info.h"
20 #include "system/floor-type-definition.h"
21 #include "system/grid-type-definition.h"
22 #include "system/monster-entity.h"
23 #include "system/monster-race-info.h"
24 #include "system/player-type-definition.h"
25 #include "util/bit-flags-calculator.h"
26
27 /*!
28  * @brief モンスターが移動した結果、そこにプレイヤーがいたら直接攻撃を行う
29  * @param player_ptr プレイヤーへの参照ポインタ
30  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
31  * @param m_idx モンスターID
32  * @param pos モンスターの移動先座標
33  * @details
34  * 反攻撃の洞窟など、直接攻撃ができない場所では処理をスキップする
35  */
36 void exe_monster_attack_to_player(PlayerType *player_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, const Pos2D &pos)
37 {
38     auto &floor = *player_ptr->current_floor_ptr;
39     auto *m_ptr = &floor.m_list[m_idx];
40     auto *r_ptr = &m_ptr->get_monrace();
41     if (!turn_flags_ptr->do_move || !player_ptr->is_located_at(pos)) {
42         return;
43     }
44
45     if (r_ptr->behavior_flags.has(MonsterBehaviorType::NEVER_BLOW)) {
46         if (is_original_ap_and_seen(player_ptr, m_ptr)) {
47             r_ptr->r_behavior_flags.set(MonsterBehaviorType::NEVER_BLOW);
48         }
49
50         turn_flags_ptr->do_move = false;
51     }
52
53     if (turn_flags_ptr->do_move && floor.get_dungeon_definition().flags.has(DungeonFeatureType::NO_MELEE) && !m_ptr->is_confused()) {
54         if (r_ptr->behavior_flags.has_not(MonsterBehaviorType::STUPID)) {
55             turn_flags_ptr->do_move = false;
56         } else if (is_original_ap_and_seen(player_ptr, m_ptr)) {
57             r_ptr->r_behavior_flags.set(MonsterBehaviorType::STUPID);
58         }
59     }
60
61     if (!turn_flags_ptr->do_move) {
62         return;
63     }
64
65     if (!player_ptr->riding || one_in_(2)) {
66         MonsterAttackPlayer(player_ptr, m_idx).make_attack_normal();
67         turn_flags_ptr->do_move = false;
68         turn_flags_ptr->do_turn = true;
69     }
70 }
71
72 /*!
73  * @brief モンスターからモンスターへの直接攻撃を実行する
74  * @param player_ptr プレイヤーへの参照ポインタ
75  * @param m_idx モンスターID
76  * @param g_ptr グリッドへの参照ポインタ
77  */
78 static bool exe_monster_attack_to_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, Grid *g_ptr)
79 {
80     auto &floor = *player_ptr->current_floor_ptr;
81     auto *m_ptr = &floor.m_list[m_idx];
82     auto *r_ptr = &m_ptr->get_monrace();
83     MonsterEntity *y_ptr;
84     y_ptr = &player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
85     if (r_ptr->behavior_flags.has(MonsterBehaviorType::NEVER_BLOW)) {
86         return false;
87     }
88
89     if ((r_ptr->behavior_flags.has_not(MonsterBehaviorType::KILL_BODY)) && is_original_ap_and_seen(player_ptr, m_ptr)) {
90         r_ptr->r_behavior_flags.set(MonsterBehaviorType::KILL_BODY);
91     }
92
93     if (!MonsterRace(y_ptr->r_idx).is_valid() || (y_ptr->hp < 0)) {
94         return false;
95     }
96     if (monst_attack_monst(player_ptr, m_idx, g_ptr->m_idx)) {
97         return true;
98     }
99     if (floor.get_dungeon_definition().flags.has_not(DungeonFeatureType::NO_MELEE)) {
100         return false;
101     }
102     if (m_ptr->is_confused()) {
103         return true;
104     }
105     if (r_ptr->behavior_flags.has_not(MonsterBehaviorType::STUPID)) {
106         return false;
107     }
108
109     if (is_original_ap_and_seen(player_ptr, m_ptr)) {
110         r_ptr->r_behavior_flags.set(MonsterBehaviorType::STUPID);
111     }
112
113     return true;
114 }
115
116 /*!
117  * @brief モンスターからモンスターへの攻撃処理
118  * @param player_ptr プレイヤーへの参照ポインタ
119  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
120  * @param m_idx モンスターID
121  * @param g_ptr グリッドへの参照ポインタ
122  * @param can_cross モンスターが地形を踏破できるならばTRUE
123  * @return ターン消費が発生したらTRUE
124  */
125 bool process_monster_attack_to_monster(PlayerType *player_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, Grid *g_ptr, bool can_cross)
126 {
127     if (!turn_flags_ptr->do_move || (g_ptr->m_idx == 0)) {
128         return false;
129     }
130
131     turn_flags_ptr->do_move = false;
132     const auto &monster_from = player_ptr->current_floor_ptr->m_list[m_idx];
133     const auto &monrace_from = monster_from.get_monrace();
134     const auto &monster_to = player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
135     const auto &monrace_to = monster_to.get_monrace();
136     auto do_kill_body = monrace_from.behavior_flags.has(MonsterBehaviorType::KILL_BODY) && monrace_from.behavior_flags.has_not(MonsterBehaviorType::NEVER_BLOW);
137     do_kill_body &= (monrace_from.mexp * monrace_from.level > monrace_to.mexp * monrace_to.level);
138     do_kill_body &= (g_ptr->m_idx != player_ptr->riding);
139     if (do_kill_body || monster_from.is_hostile_to_melee(monster_to) || monster_from.is_confused()) {
140         return exe_monster_attack_to_monster(player_ptr, m_idx, g_ptr);
141     }
142
143     auto do_move_body = monrace_from.behavior_flags.has(MonsterBehaviorType::MOVE_BODY) && monrace_from.behavior_flags.has_not(MonsterBehaviorType::NEVER_MOVE);
144     do_move_body &= (monrace_from.mexp > monrace_to.mexp);
145     do_move_body &= can_cross;
146     do_move_body &= (g_ptr->m_idx != player_ptr->riding);
147     do_move_body &= monster_can_cross_terrain(player_ptr, player_ptr->current_floor_ptr->grid_array[monster_from.fy][monster_from.fx].feat, &monrace_to, 0);
148     if (do_move_body) {
149         turn_flags_ptr->do_move = true;
150         turn_flags_ptr->did_move_body = true;
151         (void)set_monster_csleep(player_ptr, g_ptr->m_idx, 0);
152     }
153
154     return false;
155 }