OSDN Git Service

[Refactor] race-flags#.h のインクルードを削除
[hengbandforosx/hengbandosx.git] / src / mspell / mspell-lite.cpp
1 /*!
2  * @brief モンスターの魔法によってフロアを明るくする処理及びその判定
3  * @date 2020/07/23
4  * @author Hourier
5  */
6
7 #include "mspell/mspell-lite.h"
8 #include "dungeon/dungeon-flag-types.h"
9 #include "floor/cave.h"
10 #include "floor/geometry.h"
11 #include "floor/line-of-sight.h"
12 #include "monster-race/monster-race.h"
13 #include "monster-race/race-ability-mask.h"
14 #include "monster-race/race-brightness-mask.h"
15 #include "mspell/mspell-attack-util.h"
16 #include "mspell/mspell-judgement.h"
17 #include "player-base/player-class.h"
18 #include "spell/range-calc.h"
19 #include "system/angband-system.h"
20 #include "system/dungeon-info.h"
21 #include "system/floor-type-definition.h"
22 #include "system/grid-type-definition.h"
23 #include "system/monster-entity.h"
24 #include "system/monster-race-info.h"
25 #include "system/player-type-definition.h"
26 #include "system/terrain-type-definition.h"
27 #include "target/projection-path-calculator.h"
28 #include "util/bit-flags-calculator.h"
29
30 /*!
31  * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
32  * @param player_ptr プレイヤーへの参照ポインタ
33  * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
34  * @param yp 最適な目標地点のY座標を返す参照ポインタ
35  * @param xp 最適な目標地点のX座標を返す参照ポインタ
36  * @param f_flag 射線に入れるのを避ける地形の所持フラグ
37  * @param path_check 射線を判定するための関数ポインタ
38  * @return 有効な座標があった場合TRUEを返す
39  */
40 bool adjacent_grid_check(PlayerType *player_ptr, MonsterEntity *m_ptr, POSITION *yp, POSITION *xp, TerrainCharacteristics f_flag, path_check_pf path_check)
41 {
42     static int tonari_y[4][8] = { { -1, -1, -1, 0, 0, 1, 1, 1 }, { -1, -1, -1, 0, 0, 1, 1, 1 }, { 1, 1, 1, 0, 0, -1, -1, -1 }, { 1, 1, 1, 0, 0, -1, -1, -1 } };
43     static int tonari_x[4][8] = { { -1, 0, 1, -1, 1, -1, 0, 1 }, { 1, 0, -1, 1, -1, 1, 0, -1 }, { -1, 0, 1, -1, 1, -1, 0, 1 }, { 1, 0, -1, 1, -1, 1, 0, -1 } };
44
45     int next;
46     if (m_ptr->fy < player_ptr->y && m_ptr->fx < player_ptr->x) {
47         next = 0;
48     } else if (m_ptr->fy < player_ptr->y) {
49         next = 1;
50     } else if (m_ptr->fx < player_ptr->x) {
51         next = 2;
52     } else {
53         next = 3;
54     }
55
56     for (int i = 0; i < 8; i++) {
57         int next_x = *xp + tonari_x[next][i];
58         int next_y = *yp + tonari_y[next][i];
59         Grid *g_ptr;
60         g_ptr = &player_ptr->current_floor_ptr->grid_array[next_y][next_x];
61         if (!g_ptr->cave_has_flag(f_flag)) {
62             continue;
63         }
64
65         if (path_check(player_ptr, m_ptr->fy, m_ptr->fx, next_y, next_x)) {
66             *yp = next_y;
67             *xp = next_x;
68             return true;
69         }
70     }
71
72     return false;
73 }
74
75 void decide_lite_range(PlayerType *player_ptr, msa_type *msa_ptr)
76 {
77     if (msa_ptr->ability_flags.has_not(MonsterAbilityType::BR_LITE)) {
78         return;
79     }
80
81     msa_ptr->y_br_lite = msa_ptr->y;
82     msa_ptr->x_br_lite = msa_ptr->x;
83     if (los(player_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y_br_lite, msa_ptr->x_br_lite)) {
84         const Pos2D pos(msa_ptr->y_br_lite, msa_ptr->x_br_lite);
85         const auto &terrain = player_ptr->current_floor_ptr->get_grid(pos).get_terrain();
86         if (terrain.flags.has_not(TerrainCharacteristics::LOS) && terrain.flags.has(TerrainCharacteristics::PROJECT) && one_in_(2)) {
87             msa_ptr->ability_flags.reset(MonsterAbilityType::BR_LITE);
88         }
89     } else if (!adjacent_grid_check(player_ptr, msa_ptr->m_ptr, &msa_ptr->y_br_lite, &msa_ptr->x_br_lite, TerrainCharacteristics::LOS, los)) {
90         msa_ptr->ability_flags.reset(MonsterAbilityType::BR_LITE);
91     }
92
93     if (msa_ptr->ability_flags.has(MonsterAbilityType::BR_LITE)) {
94         return;
95     }
96
97     msa_ptr->y_br_lite = 0;
98     msa_ptr->x_br_lite = 0;
99 }
100
101 static void feature_projection(const FloorType &floor, msa_type *msa_ptr)
102 {
103     const Pos2D pos(msa_ptr->y, msa_ptr->x);
104     const auto &terrain = floor.get_grid(pos).get_terrain();
105     if (terrain.flags.has(TerrainCharacteristics::PROJECT)) {
106         return;
107     }
108
109     if (msa_ptr->ability_flags.has(MonsterAbilityType::BR_DISI) && terrain.flags.has(TerrainCharacteristics::HURT_DISI) && one_in_(2)) {
110         msa_ptr->do_spell = DO_SPELL_BR_DISI;
111         return;
112     }
113
114     if (msa_ptr->ability_flags.has(MonsterAbilityType::BR_LITE) && terrain.flags.has(TerrainCharacteristics::LOS) && one_in_(2)) {
115         msa_ptr->do_spell = DO_SPELL_BR_LITE;
116     }
117 }
118
119 static void check_lite_area_by_mspell(PlayerType *player_ptr, msa_type *msa_ptr)
120 {
121     const auto &system = AngbandSystem::get_instance();
122     auto light_by_disintegration = msa_ptr->ability_flags.has(MonsterAbilityType::BR_DISI);
123     light_by_disintegration &= msa_ptr->m_ptr->cdis < system.get_max_range() / 2;
124     light_by_disintegration &= in_disintegration_range(player_ptr->current_floor_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x);
125     light_by_disintegration &= one_in_(10) || (projectable(player_ptr, msa_ptr->y, msa_ptr->x, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx) && one_in_(2));
126     if (light_by_disintegration) {
127         msa_ptr->do_spell = DO_SPELL_BR_DISI;
128         msa_ptr->success = true;
129         return;
130     }
131
132     auto light_by_lite = msa_ptr->ability_flags.has(MonsterAbilityType::BR_LITE);
133     light_by_lite &= msa_ptr->m_ptr->cdis < system.get_max_range() / 2;
134     light_by_lite &= los(player_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x);
135     light_by_lite &= one_in_(5);
136     if (light_by_lite) {
137         msa_ptr->do_spell = DO_SPELL_BR_LITE;
138         msa_ptr->success = true;
139         return;
140     }
141
142     if (msa_ptr->ability_flags.has_not(MonsterAbilityType::BA_LITE) || (msa_ptr->m_ptr->cdis > system.get_max_range())) {
143         return;
144     }
145
146     auto by = msa_ptr->y;
147     auto bx = msa_ptr->x;
148     get_project_point(player_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, &by, &bx, 0L);
149     if ((distance(by, bx, msa_ptr->y, msa_ptr->x) <= 3) && los(player_ptr, by, bx, msa_ptr->y, msa_ptr->x) && one_in_(5)) {
150         msa_ptr->do_spell = DO_SPELL_BA_LITE;
151         msa_ptr->success = true;
152     }
153 }
154
155 static void decide_lite_breath(msa_type *msa_ptr)
156 {
157     if (msa_ptr->success) {
158         return;
159     }
160
161     if (msa_ptr->m_ptr->target_y && msa_ptr->m_ptr->target_x) {
162         msa_ptr->y = msa_ptr->m_ptr->target_y;
163         msa_ptr->x = msa_ptr->m_ptr->target_x;
164         msa_ptr->ability_flags &= RF_ABILITY_INDIRECT_MASK;
165         msa_ptr->success = true;
166     }
167
168     auto should_set = msa_ptr->y_br_lite == 0;
169     should_set |= msa_ptr->x_br_lite == 0;
170     should_set |= msa_ptr->m_ptr->cdis > AngbandSystem::get_instance().get_max_range() / 2;
171     should_set |= !one_in_(5);
172     if (should_set) {
173         return;
174     }
175
176     if (msa_ptr->success) {
177         msa_ptr->ability_flags.set(MonsterAbilityType::BR_LITE);
178         return;
179     }
180
181     msa_ptr->y = msa_ptr->y_br_lite;
182     msa_ptr->x = msa_ptr->x_br_lite;
183     msa_ptr->do_spell = DO_SPELL_BR_LITE;
184     msa_ptr->success = true;
185 }
186
187 bool decide_lite_projection(PlayerType *player_ptr, msa_type *msa_ptr)
188 {
189     if (projectable(player_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x)) {
190         feature_projection(*player_ptr->current_floor_ptr, msa_ptr);
191         return true;
192     }
193
194     msa_ptr->success = false;
195     check_lite_area_by_mspell(player_ptr, msa_ptr);
196     if (!msa_ptr->success) {
197         msa_ptr->success = adjacent_grid_check(player_ptr, msa_ptr->m_ptr, &msa_ptr->y, &msa_ptr->x, TerrainCharacteristics::PROJECT, projectable);
198     }
199
200     decide_lite_breath(msa_ptr);
201     return msa_ptr->success;
202 }
203
204 void decide_lite_area(PlayerType *player_ptr, msa_type *msa_ptr)
205 {
206     if (msa_ptr->ability_flags.has_not(MonsterAbilityType::DARKNESS)) {
207         return;
208     }
209
210     PlayerClass pc(player_ptr);
211     auto can_use_lite_area = pc.equals(PlayerClassType::NINJA);
212     can_use_lite_area &= msa_ptr->r_ptr->kind_flags.has_not(MonsterKindType::UNDEAD);
213     can_use_lite_area &= msa_ptr->r_ptr->resistance_flags.has_not(MonsterResistanceType::HURT_LITE);
214     can_use_lite_area &= (msa_ptr->r_ptr->brightness_flags.has_none_of(dark_mask));
215
216     if (msa_ptr->r_ptr->behavior_flags.has(MonsterBehaviorType::STUPID)) {
217         return;
218     }
219
220     if (player_ptr->current_floor_ptr->get_dungeon_definition().flags.has(DungeonFeatureType::DARKNESS)) {
221         msa_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
222         return;
223     }
224
225     if (pc.equals(PlayerClassType::NINJA) && !can_use_lite_area) {
226         msa_ptr->ability_flags.reset(MonsterAbilityType::DARKNESS);
227     }
228 }