OSDN Git Service

Merge pull request #3569 from sikabane-works/release/3.0.0.88-alpha
[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 ny 移動後の、モンスターのY座標
33  * @param nx 移動後の、モンスターのX座標
34  * @details
35  * 反攻撃の洞窟など、直接攻撃ができない場所では処理をスキップする
36  */
37 void exe_monster_attack_to_player(PlayerType *player_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, POSITION ny, POSITION nx)
38 {
39     auto &floor = *player_ptr->current_floor_ptr;
40     auto *m_ptr = &floor.m_list[m_idx];
41     auto *r_ptr = &monraces_info[m_ptr->r_idx];
42     if (!turn_flags_ptr->do_move || !player_bold(player_ptr, ny, nx)) {
43         return;
44     }
45
46     if (r_ptr->behavior_flags.has(MonsterBehaviorType::NEVER_BLOW)) {
47         if (is_original_ap_and_seen(player_ptr, m_ptr)) {
48             r_ptr->r_behavior_flags.set(MonsterBehaviorType::NEVER_BLOW);
49         }
50
51         turn_flags_ptr->do_move = false;
52     }
53
54     if (turn_flags_ptr->do_move && floor.get_dungeon_definition().flags.has(DungeonFeatureType::NO_MELEE) && !m_ptr->is_confused()) {
55         if (r_ptr->behavior_flags.has_not(MonsterBehaviorType::STUPID)) {
56             turn_flags_ptr->do_move = false;
57         } else if (is_original_ap_and_seen(player_ptr, m_ptr)) {
58             r_ptr->r_behavior_flags.set(MonsterBehaviorType::STUPID);
59         }
60     }
61
62     if (!turn_flags_ptr->do_move) {
63         return;
64     }
65
66     if (!player_ptr->riding || one_in_(2)) {
67         MonsterAttackPlayer(player_ptr, m_idx).make_attack_normal();
68         turn_flags_ptr->do_move = false;
69         turn_flags_ptr->do_turn = true;
70     }
71 }
72
73 /*!
74  * @brief モンスターからモンスターへの直接攻撃を実行する
75  * @param player_ptr プレイヤーへの参照ポインタ
76  * @param m_idx モンスターID
77  * @param g_ptr グリッドへの参照ポインタ
78  */
79 static bool exe_monster_attack_to_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, grid_type *g_ptr)
80 {
81     auto &floor = *player_ptr->current_floor_ptr;
82     auto *m_ptr = &floor.m_list[m_idx];
83     auto *r_ptr = &monraces_info[m_ptr->r_idx];
84     MonsterEntity *y_ptr;
85     y_ptr = &player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
86     if (r_ptr->behavior_flags.has(MonsterBehaviorType::NEVER_BLOW)) {
87         return false;
88     }
89
90     if ((r_ptr->behavior_flags.has_not(MonsterBehaviorType::KILL_BODY)) && is_original_ap_and_seen(player_ptr, m_ptr)) {
91         r_ptr->r_behavior_flags.set(MonsterBehaviorType::KILL_BODY);
92     }
93
94     if (!MonsterRace(y_ptr->r_idx).is_valid() || (y_ptr->hp < 0)) {
95         return false;
96     }
97     if (monst_attack_monst(player_ptr, m_idx, g_ptr->m_idx)) {
98         return true;
99     }
100     if (floor.get_dungeon_definition().flags.has_not(DungeonFeatureType::NO_MELEE)) {
101         return false;
102     }
103     if (m_ptr->is_confused()) {
104         return true;
105     }
106     if (r_ptr->behavior_flags.has_not(MonsterBehaviorType::STUPID)) {
107         return false;
108     }
109
110     if (is_original_ap_and_seen(player_ptr, m_ptr)) {
111         r_ptr->r_behavior_flags.set(MonsterBehaviorType::STUPID);
112     }
113
114     return true;
115 }
116
117 /*!
118  * @brief モンスターからモンスターへの攻撃処理
119  * @param player_ptr プレイヤーへの参照ポインタ
120  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
121  * @param m_idx モンスターID
122  * @param g_ptr グリッドへの参照ポインタ
123  * @param can_cross モンスターが地形を踏破できるならばTRUE
124  * @return ターン消費が発生したらTRUE
125  */
126 bool process_monster_attack_to_monster(PlayerType *player_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, grid_type *g_ptr, bool can_cross)
127 {
128     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
129     auto *r_ptr = &monraces_info[m_ptr->r_idx];
130     MonsterEntity *y_ptr;
131     y_ptr = &player_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
132     if (!turn_flags_ptr->do_move || (g_ptr->m_idx == 0)) {
133         return false;
134     }
135
136     MonsterRaceInfo *z_ptr = &monraces_info[y_ptr->r_idx];
137     turn_flags_ptr->do_move = false;
138
139     bool do_kill_body = r_ptr->behavior_flags.has(MonsterBehaviorType::KILL_BODY) && r_ptr->behavior_flags.has_not(MonsterBehaviorType::NEVER_BLOW);
140     do_kill_body &= (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level);
141     do_kill_body &= (g_ptr->m_idx != player_ptr->riding);
142
143     if (do_kill_body || are_enemies(player_ptr, *m_ptr, *y_ptr) || m_ptr->is_confused()) {
144         return exe_monster_attack_to_monster(player_ptr, m_idx, g_ptr);
145     }
146
147     bool do_move_body = r_ptr->behavior_flags.has(MonsterBehaviorType::MOVE_BODY) && r_ptr->behavior_flags.has_not(MonsterBehaviorType::NEVER_MOVE);
148     do_move_body &= (r_ptr->mexp > z_ptr->mexp);
149     do_move_body &= can_cross;
150     do_move_body &= (g_ptr->m_idx != player_ptr->riding);
151     do_move_body &= monster_can_cross_terrain(player_ptr, player_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0);
152
153     if (do_move_body) {
154         turn_flags_ptr->do_move = true;
155         turn_flags_ptr->did_move_body = true;
156         (void)set_monster_csleep(player_ptr, g_ptr->m_idx, 0);
157     }
158
159     return false;
160 }