OSDN Git Service

[Refactor] #2124 Changed struct object_type to class ObjectType
[hengbandforosx/hengbandosx.git] / src / player-status / player-status-base.cpp
1 #include "player-status/player-status-base.h"
2 #include "inventory/inventory-slot-types.h"
3 #include "object/object-flags.h"
4 #include "player/player-status.h"
5 #include "system/object-type-definition.h"
6 #include "system/player-type-definition.h"
7 #include "util/bit-flags-calculator.h"
8
9 /*!
10  * @brief プレイヤーの各ステータス計算用のクラス
11  * @param player_ptr プレイヤーの参照ポインタ
12  * @details
13  * * コンストラクタでplayer_ptrをセット。メンバ変数を0クリア。
14  */
15 PlayerStatusBase::PlayerStatusBase(PlayerType *player_ptr)
16     : player_ptr(player_ptr)
17 {
18     this->set_locals(); /* 初期化。基底クラスの0クリアが呼ばれる。*/
19 }
20
21 /*!
22  * @brief 該当する値を計算して取得する。
23  * @details
24  * * 派生クラスからset_locals()をコールして初期値、上限、下限をセット。
25  * * 各要素毎に計算した値を初期値に単純に加算し、上限と下限で丸める。
26  */
27 int16_t PlayerStatusBase::get_value()
28 {
29     this->set_locals(); /* 計算前に値のセット。派生クラスの値がセットされる。*/
30     int16_t pow = this->default_value;
31
32     pow += this->action_value();
33     pow += this->stance_value();
34     pow += this->class_base_value();
35     pow += this->class_value();
36     pow += this->equipments_value();
37     pow += this->inventory_weight_value();
38     pow += this->mutation_value();
39     pow += this->personality_value();
40     pow += this->race_value();
41     pow += this->riding_value();
42     pow += this->time_effect_value();
43     pow = this->set_exception_value(pow);
44
45     if ((pow > this->max_value)) {
46         pow = this->max_value;
47     }
48
49     if (pow < this->min_value)
50         pow = this->min_value;
51
52     return pow;
53 }
54
55 /*!
56  * @brief 修正値が0でないところにビットを立てて返す。
57  * @return 判定結果のBIT_FLAGS
58  */
59 BIT_FLAGS PlayerStatusBase::get_all_flags()
60 {
61     this->set_locals(); /* 計算前に値のセット。派生クラスの値がセットされる。*/
62     BIT_FLAGS result = equipments_flags(this->tr_flag);
63
64     if (this->class_value() != 0)
65         set_bits(result, FLAG_CAUSE_CLASS);
66
67     if (this->race_value() != 0)
68         set_bits(result, FLAG_CAUSE_RACE);
69
70     if (this->stance_value() != 0)
71         set_bits(result, FLAG_CAUSE_STANCE);
72
73     if (this->mutation_value() != 0)
74         set_bits(result, FLAG_CAUSE_MUTATION);
75
76     if (this->time_effect_value() != 0)
77         set_bits(result, FLAG_CAUSE_MAGIC_TIME_EFFECT);
78
79     if (this->personality_value() != 0)
80         set_bits(result, FLAG_CAUSE_PERSONALITY);
81
82     if (this->riding_value() != 0)
83         set_bits(result, FLAG_CAUSE_RIDING);
84
85     if (this->inventory_weight_value() != 0)
86         set_bits(result, FLAG_CAUSE_INVEN_PACK);
87
88     if (this->action_value() != 0)
89         set_bits(result, FLAG_CAUSE_ACTION);
90
91     return result;
92 }
93
94 /*!
95  * @brief 修正値が1以上のところにビットを立てて返す。
96  * @return 判定結果のBIT_FLAGS
97  */
98 BIT_FLAGS PlayerStatusBase::get_good_flags()
99 {
100     this->set_locals(); /* 計算前に値のセット。派生クラスの値がセットされる。*/
101     BIT_FLAGS result = equipments_flags(this->tr_flag);
102
103     if (this->class_value() > 0)
104         set_bits(result, FLAG_CAUSE_CLASS);
105
106     if (this->race_value() > 0)
107         set_bits(result, FLAG_CAUSE_RACE);
108
109     if (this->stance_value() > 0)
110         set_bits(result, FLAG_CAUSE_STANCE);
111
112     if (this->mutation_value() > 0)
113         set_bits(result, FLAG_CAUSE_MUTATION);
114
115     if (this->time_effect_value() > 0)
116         set_bits(result, FLAG_CAUSE_MAGIC_TIME_EFFECT);
117
118     if (this->personality_value() > 0)
119         set_bits(result, FLAG_CAUSE_PERSONALITY);
120
121     if (this->riding_value() > 0)
122         set_bits(result, FLAG_CAUSE_RIDING);
123
124     if (this->inventory_weight_value() > 0)
125         set_bits(result, FLAG_CAUSE_INVEN_PACK);
126
127     if (this->action_value() > 0)
128         set_bits(result, FLAG_CAUSE_ACTION);
129
130     return result;
131 }
132
133 /*!
134  * @brief 修正値が-1以下のところにビットを立てて返す。
135  * @return 判定結果のBIT_FLAGS
136  */
137 BIT_FLAGS PlayerStatusBase::get_bad_flags()
138 {
139     this->set_locals(); /* 計算前に値のセット。派生クラスの値がセットされる。*/
140     BIT_FLAGS result = equipments_bad_flags(this->tr_bad_flag);
141
142     if (this->class_value() < 0)
143         set_bits(result, FLAG_CAUSE_CLASS);
144
145     if (this->race_value() < 0)
146         set_bits(result, FLAG_CAUSE_RACE);
147
148     if (this->stance_value() < 0)
149         set_bits(result, FLAG_CAUSE_STANCE);
150
151     if (this->mutation_value() < 0)
152         set_bits(result, FLAG_CAUSE_MUTATION);
153
154     if (this->time_effect_value() < 0)
155         set_bits(result, FLAG_CAUSE_MAGIC_TIME_EFFECT);
156
157     if (this->personality_value() < 0)
158         set_bits(result, FLAG_CAUSE_PERSONALITY);
159
160     if (this->riding_value() < 0)
161         set_bits(result, FLAG_CAUSE_RIDING);
162
163     if (this->inventory_weight_value() < 0)
164         set_bits(result, FLAG_CAUSE_INVEN_PACK);
165
166     if (this->action_value() < 0)
167         set_bits(result, FLAG_CAUSE_ACTION);
168
169     return result;
170 }
171
172 void PlayerStatusBase::set_locals()
173 {
174     this->default_value = 0;
175     this->min_value = 0;
176     this->max_value = 0;
177     this->tr_flag = TR_FLAG_MAX;
178     this->tr_bad_flag = TR_FLAG_MAX;
179 }
180
181 /*!
182  * @brief 判定するflagを持つ装備品に対応するBIT_FLAGSを返す
183  * @param check_flag 判定するtr_type
184  * @return 判定結果のBIT_FLAGS
185  */
186 BIT_FLAGS PlayerStatusBase::equipments_flags(tr_type check_flag)
187 {
188     ObjectType *o_ptr;
189     BIT_FLAGS result = 0L;
190     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
191         o_ptr = &player_ptr->inventory_list[i];
192         if (!o_ptr->k_idx)
193             continue;
194
195         auto flgs = object_flags(o_ptr);
196
197         if (flgs.has(check_flag))
198             set_bits(result, convert_inventory_slot_type_to_flag_cause(i2enum<inventory_slot_type>(i)));
199     }
200     return result;
201 }
202
203 /*!
204  * @brief 判定するflagを持ち、pvalが負の装備品に対応するBIT_FLAGSを返す
205  * @param check_flag 判定するtr_type
206  * @return 判定結果のBIT_FLAGS
207  */
208 BIT_FLAGS PlayerStatusBase::equipments_bad_flags(tr_type check_flag)
209 {
210     ObjectType *o_ptr;
211     BIT_FLAGS result = 0L;
212     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
213         o_ptr = &player_ptr->inventory_list[i];
214         if (!o_ptr->k_idx)
215             continue;
216
217         auto flgs = object_flags(o_ptr);
218
219         if (flgs.has(check_flag)) {
220             if (o_ptr->pval < 0) {
221                 set_bits(result, convert_inventory_slot_type_to_flag_cause(i2enum<inventory_slot_type>(i)));
222             }
223         }
224     }
225     return result;
226 }
227
228 /*!
229  * @brief this->tr_flagを持つ装備品のpval合計値を返す
230  * @return 該当するfalgを持つ全装備のpvalの合計値
231  */
232 int16_t PlayerStatusBase::equipments_value()
233 {
234     this->set_locals(); /* 計算前に値のセット。派生クラスの値がセットされる。*/
235     int16_t result = 0;
236     for (int i = INVEN_MAIN_HAND; i < INVEN_TOTAL; i++) {
237         ObjectType *o_ptr = &player_ptr->inventory_list[i];
238         auto flgs = object_flags(o_ptr);
239
240         if (!o_ptr->k_idx)
241             continue;
242         if (flgs.has(this->tr_flag))
243             result += o_ptr->pval;
244     }
245     return result;
246 }
247
248 int16_t PlayerStatusBase::race_value()
249 {
250     return 0;
251 }
252 int16_t PlayerStatusBase::class_value()
253 {
254     return 0;
255 }
256 int16_t PlayerStatusBase::class_base_value()
257 {
258     return 0;
259 }
260 int16_t PlayerStatusBase::personality_value()
261 {
262     return 0;
263 }
264 int16_t PlayerStatusBase::time_effect_value()
265 {
266     return 0;
267 }
268 int16_t PlayerStatusBase::stance_value()
269 {
270     return 0;
271 }
272 int16_t PlayerStatusBase::mutation_value()
273 {
274     return 0;
275 }
276 int16_t PlayerStatusBase::riding_value()
277 {
278     return 0;
279 }
280 int16_t PlayerStatusBase::inventory_weight_value()
281 {
282     return 0;
283 }
284 int16_t PlayerStatusBase::action_value()
285 {
286     return 0;
287 }
288
289 /*!
290  * @brief 値を直接変更する例外処理。
291  * @param value 単純加算された修正値の合計
292  * @details
293  * * 派生クラスで必要とされる例外処理でoverrideされる
294  * @return 直接変更された値。このままmin-max処理され最終的なvalueになる。
295  */
296 int16_t PlayerStatusBase::set_exception_value(int16_t value)
297 {
298     return value;
299 }