OSDN Git Service

[Refactor] #40571 Moved get_max_range() from target-preparation.c/h to floor.c/h
[hengbandforosx/hengbandosx.git] / src / mspell / mspell-lite.c
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 "dungeon/dungeon.h"
10 #include "floor/cave.h"
11 #include "floor/floor.h"
12 #include "grid/feature.h"
13 #include "grid/grid.h"
14 #include "monster-race/monster-race.h"
15 #include "monster-race/race-flags-ability1.h"
16 #include "monster-race/race-flags-ability2.h"
17 #include "monster-race/race-flags2.h"
18 #include "monster-race/race-flags3.h"
19 #include "monster-race/race-flags4.h"
20 #include "monster-race/race-flags7.h"
21 #include "mspell/mspell-attack-util.h"
22 #include "mspell/mspell-judgement.h"
23 #include "mspell/mspell-mask-definitions.h"
24 #include "spell/range-calc.h"
25 #include "system/floor-type-definition.h"
26 #include "system/monster-type-definition.h"
27 #include "util/bit-flags-calculator.h"
28
29 /*!
30  * @brief モンスターがプレイヤーにダメージを与えるための最適な座標を算出する /
31  * @param target_ptr プレーヤーへの参照ポインタ
32  * @param m_ptr 技能を使用するモンスター構造体の参照ポインタ
33  * @param yp 最適な目標地点のY座標を返す参照ポインタ
34  * @param xp 最適な目標地点のX座標を返す参照ポインタ
35  * @param f_flag 射線に入れるのを避ける地形の所持フラグ
36  * @param path_check 射線を判定するための関数ポインタ
37  * @return 有効な座標があった場合TRUEを返す
38  */
39 bool adjacent_grid_check(player_type *target_ptr, monster_type *m_ptr, POSITION *yp, POSITION *xp, int f_flag, path_check_pf path_check)
40 {
41     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 } };
42     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 } };
43
44     int next;
45     if (m_ptr->fy < target_ptr->y && m_ptr->fx < target_ptr->x)
46         next = 0;
47     else if (m_ptr->fy < target_ptr->y)
48         next = 1;
49     else if (m_ptr->fx < target_ptr->x)
50         next = 2;
51     else
52         next = 3;
53
54     for (int i = 0; i < 8; i++) {
55         int next_x = *xp + tonari_x[next][i];
56         int next_y = *yp + tonari_y[next][i];
57         grid_type *g_ptr;
58         g_ptr = &target_ptr->current_floor_ptr->grid_array[next_y][next_x];
59         if (!cave_have_flag_grid(g_ptr, f_flag))
60             continue;
61
62         if (path_check(target_ptr, m_ptr->fy, m_ptr->fx, next_y, next_x)) {
63             *yp = next_y;
64             *xp = next_x;
65             return TRUE;
66         }
67     }
68
69     return FALSE;
70 }
71
72 void decide_lite_range(player_type *target_ptr, msa_type *msa_ptr)
73 {
74     if ((msa_ptr->f4 & RF4_BR_LITE) == 0)
75         return;
76
77     msa_ptr->y_br_lite = msa_ptr->y;
78     msa_ptr->x_br_lite = msa_ptr->x;
79     if (los(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y_br_lite, msa_ptr->x_br_lite)) {
80         feature_type *f_ptr = &f_info[target_ptr->current_floor_ptr->grid_array[msa_ptr->y_br_lite][msa_ptr->x_br_lite].feat];
81         if (!have_flag(f_ptr->flags, FF_LOS) && have_flag(f_ptr->flags, FF_PROJECT) && one_in_(2))
82             msa_ptr->f4 &= ~(RF4_BR_LITE);
83     } else if (!adjacent_grid_check(target_ptr, msa_ptr->m_ptr, &msa_ptr->y_br_lite, &msa_ptr->x_br_lite, FF_LOS, los))
84         msa_ptr->f4 &= ~(RF4_BR_LITE);
85
86     if ((msa_ptr->f4 & RF4_BR_LITE) != 0)
87         return;
88
89     msa_ptr->y_br_lite = 0;
90     msa_ptr->x_br_lite = 0;
91 }
92
93 static void feature_projection(floor_type *floor_ptr, msa_type *msa_ptr)
94 {
95     feature_type *f_ptr = &f_info[floor_ptr->grid_array[msa_ptr->y][msa_ptr->x].feat];
96     if (have_flag(f_ptr->flags, FF_PROJECT))
97         return;
98
99     if ((msa_ptr->f4 & RF4_BR_DISI) && have_flag(f_ptr->flags, FF_HURT_DISI) && one_in_(2)) {
100         msa_ptr->do_spell = DO_SPELL_BR_DISI;
101         return;
102     }
103
104     if ((msa_ptr->f4 & RF4_BR_LITE) && have_flag(f_ptr->flags, FF_LOS) && one_in_(2))
105         msa_ptr->do_spell = DO_SPELL_BR_LITE;
106 }
107
108 static void check_lite_area_by_mspell(player_type *target_ptr, msa_type *msa_ptr)
109 {
110     if ((msa_ptr->f4 & RF4_BR_DISI) && (msa_ptr->m_ptr->cdis < get_max_range(target_ptr) / 2)
111         && in_disintegration_range(target_ptr->current_floor_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x)
112         && (one_in_(10) || (projectable(target_ptr, msa_ptr->y, msa_ptr->x, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx) && one_in_(2)))) {
113         msa_ptr->do_spell = DO_SPELL_BR_DISI;
114         msa_ptr->success = TRUE;
115         return;
116     }
117
118     if ((msa_ptr->f4 & RF4_BR_LITE) && (msa_ptr->m_ptr->cdis < get_max_range(target_ptr) / 2)
119         && los(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x) && one_in_(5)) {
120         msa_ptr->do_spell = DO_SPELL_BR_LITE;
121         msa_ptr->success = TRUE;
122         return;
123     }
124
125     if (((msa_ptr->f5 & RF5_BA_LITE) == 0) || (msa_ptr->m_ptr->cdis > get_max_range(target_ptr)))
126         return;
127
128     POSITION by = msa_ptr->y, bx = msa_ptr->x;
129     get_project_point(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, &by, &bx, 0L);
130     if ((distance(by, bx, msa_ptr->y, msa_ptr->x) <= 3) && los(target_ptr, by, bx, msa_ptr->y, msa_ptr->x) && one_in_(5)) {
131         msa_ptr->do_spell = DO_SPELL_BA_LITE;
132         msa_ptr->success = TRUE;
133     }
134 }
135
136 static void decide_lite_breath(player_type *target_ptr, msa_type *msa_ptr)
137 {
138     if (msa_ptr->success)
139         return;
140
141     if (msa_ptr->m_ptr->target_y && msa_ptr->m_ptr->target_x) {
142         msa_ptr->y = msa_ptr->m_ptr->target_y;
143         msa_ptr->x = msa_ptr->m_ptr->target_x;
144         msa_ptr->f4 &= RF4_INDIRECT_MASK;
145         msa_ptr->f5 &= RF5_INDIRECT_MASK;
146         msa_ptr->f6 &= RF6_INDIRECT_MASK;
147         msa_ptr->success = TRUE;
148     }
149
150     if ((msa_ptr->y_br_lite == 0) || (msa_ptr->x_br_lite == 0) || (msa_ptr->m_ptr->cdis > get_max_range(target_ptr) / 2) || !one_in_(5))
151         return;
152
153     if (msa_ptr->success) {
154         msa_ptr->f4 |= RF4_BR_LITE;
155         return;
156     }
157
158     msa_ptr->y = msa_ptr->y_br_lite;
159     msa_ptr->x = msa_ptr->x_br_lite;
160     msa_ptr->do_spell = DO_SPELL_BR_LITE;
161     msa_ptr->success = TRUE;
162 }
163
164 bool decide_lite_projection(player_type *target_ptr, msa_type *msa_ptr)
165 {
166     if (projectable(target_ptr, msa_ptr->m_ptr->fy, msa_ptr->m_ptr->fx, msa_ptr->y, msa_ptr->x)) {
167         feature_projection(target_ptr->current_floor_ptr, msa_ptr);
168         return TRUE;
169     }
170
171     msa_ptr->success = FALSE;
172     check_lite_area_by_mspell(target_ptr, msa_ptr);
173     if (!msa_ptr->success)
174         msa_ptr->success = adjacent_grid_check(target_ptr, msa_ptr->m_ptr, &msa_ptr->y, &msa_ptr->x, FF_PROJECT, projectable);
175
176     decide_lite_breath(target_ptr, msa_ptr);
177     return msa_ptr->success;
178 }
179
180 void decide_lite_area(player_type *target_ptr, msa_type *msa_ptr)
181 {
182     if ((msa_ptr->f6 & RF6_DARKNESS) == 0)
183         return;
184
185     bool can_use_lite_area = (target_ptr->pclass == CLASS_NINJA) && ((msa_ptr->r_ptr->flags3 & (RF3_UNDEAD | RF3_HURT_LITE)) == 0)
186         && ((msa_ptr->r_ptr->flags7 & RF7_DARK_MASK) == 0);
187
188     if ((msa_ptr->r_ptr->flags2 & RF2_STUPID) != 0)
189         return;
190
191     if (d_info[target_ptr->dungeon_idx].flags1 & DF1_DARKNESS) {
192         msa_ptr->f6 &= ~(RF6_DARKNESS);
193         return;
194     }
195
196     if ((target_ptr->pclass == CLASS_NINJA) && !can_use_lite_area)
197         msa_ptr->f6 &= ~(RF6_DARKNESS);
198 }