OSDN Git Service

3f815bc4058e9a5ed24fb82054728126428fc45c
[hengbandforosx/hengbandosx.git] / src / player / player-skill.cpp
1 #include "player/player-skill.h"
2 #include "core/player-update-types.h"
3 #include "monster-race/monster-race.h"
4 #include "player-info/class-info.h"
5 #include "player/player-realm.h"
6 #include "system/floor-type-definition.h"
7 #include "system/monster-race-definition.h"
8 #include "system/monster-type-definition.h"
9 #include "system/object-type-definition.h"
10 #include "system/player-type-definition.h"
11 #include "util/bit-flags-calculator.h"
12
13 /* Proficiency of weapons and misc. skills (except riding) */
14 constexpr SUB_EXP WEAPON_EXP_UNSKILLED = 0;
15 constexpr SUB_EXP WEAPON_EXP_BEGINNER = 4000;
16 constexpr SUB_EXP WEAPON_EXP_SKILLED = 6000;
17 constexpr SUB_EXP WEAPON_EXP_EXPERT = 7000;
18 constexpr SUB_EXP WEAPON_EXP_MASTER = 8000;
19
20 /* Proficiency of riding */
21 constexpr SUB_EXP RIDING_EXP_UNSKILLED = 0;
22 constexpr SUB_EXP RIDING_EXP_BEGINNER = 500;
23 constexpr SUB_EXP RIDING_EXP_SKILLED = 2000;
24 constexpr SUB_EXP RIDING_EXP_EXPERT = 5000;
25 constexpr SUB_EXP RIDING_EXP_MASTER = 8000;
26
27 /* Proficiency of spells */
28 constexpr SUB_EXP SPELL_EXP_UNSKILLED = 0;
29 constexpr SUB_EXP SPELL_EXP_BEGINNER = 900;
30 constexpr SUB_EXP SPELL_EXP_SKILLED = 1200;
31 constexpr SUB_EXP SPELL_EXP_EXPERT = 1400;
32 constexpr SUB_EXP SPELL_EXP_MASTER = 1600;
33
34 /*
35  * The skill table
36  */
37 std::vector<skill_table> s_info;
38
39 /*!
40  * @brief 技能値到達表記テーブル
41  */
42 const concptr exp_level_str[5] =
43 #ifdef JP
44     { "[初心者]", "[入門者]", "[熟練者]", "[エキスパート]", "[達人]" };
45 #else
46     { "[Unskilled]", "[Beginner]", "[Skilled]", "[Expert]", "[Master]" };
47 #endif
48
49 namespace {
50
51 using GainAmountList = std::array<int, EXP_LEVEL_MASTER>;
52
53 void gain_attack_skill_exp(player_type *player_ptr, short &exp, const GainAmountList &gain_amount_list)
54 {
55     auto gain_amount = 0;
56
57     if (exp < WEAPON_EXP_BEGINNER) {
58         gain_amount = std::min(gain_amount_list[EXP_LEVEL_UNSKILLED], WEAPON_EXP_BEGINNER - exp);
59     } else if (exp < WEAPON_EXP_SKILLED) {
60         gain_amount = std::min(gain_amount_list[EXP_LEVEL_BEGINNER], WEAPON_EXP_SKILLED - exp);
61     } else if ((exp < WEAPON_EXP_EXPERT) && (player_ptr->lev > 19)) {
62         gain_amount = std::min(gain_amount_list[EXP_LEVEL_SKILLED], WEAPON_EXP_EXPERT - exp);
63     } else if ((exp < WEAPON_EXP_MASTER) && (player_ptr->lev > 34)) {
64         gain_amount = std::min(gain_amount_list[EXP_LEVEL_EXPERT], WEAPON_EXP_MASTER - exp);
65     }
66
67     exp += static_cast<short>(gain_amount);
68     set_bits(player_ptr->update, PU_BONUS);
69 }
70
71 void gain_spell_skill_exp_aux(player_type *player_ptr, short &exp, const GainAmountList &gain_amount_list, int spell_level)
72 {
73     const auto dlev = player_ptr->current_floor_ptr->dun_level;
74     const auto plev = player_ptr->lev;
75
76     auto gain_amount = 0;
77     if (exp < SPELL_EXP_BEGINNER) {
78         gain_amount = std::min(gain_amount_list[EXP_LEVEL_UNSKILLED], SPELL_EXP_BEGINNER - exp);
79     } else if (exp < SPELL_EXP_SKILLED) {
80         if ((dlev > 4) && ((dlev + 10) > plev)) {
81             gain_amount = std::min(gain_amount_list[EXP_LEVEL_BEGINNER], SPELL_EXP_SKILLED - exp);
82         }
83     } else if (exp < SPELL_EXP_EXPERT) {
84         if (((dlev + 5) > plev) && ((dlev + 5) > spell_level)) {
85             gain_amount = std::min(gain_amount_list[EXP_LEVEL_SKILLED], SPELL_EXP_EXPERT - exp);
86         }
87     } else if (exp < SPELL_EXP_MASTER) {
88         if (((dlev + 5) > plev) && (dlev > spell_level)) {
89             gain_amount = std::min(gain_amount_list[EXP_LEVEL_EXPERT], SPELL_EXP_MASTER - exp);
90         }
91     }
92
93     exp += static_cast<short>(gain_amount);
94     set_bits(player_ptr->update, PU_BONUS);
95 }
96
97 }
98
99 PlayerSkill::PlayerSkill(player_type *player_ptr)
100     : player_ptr(player_ptr)
101 {
102 }
103
104 SUB_EXP PlayerSkill::weapon_exp_at(int level)
105 {
106     switch (level) {
107     case EXP_LEVEL_UNSKILLED:
108         return WEAPON_EXP_UNSKILLED;
109     case EXP_LEVEL_BEGINNER:
110         return WEAPON_EXP_BEGINNER;
111     case EXP_LEVEL_SKILLED:
112         return WEAPON_EXP_SKILLED;
113     case EXP_LEVEL_EXPERT:
114         return WEAPON_EXP_EXPERT;
115     case EXP_LEVEL_MASTER:
116         return WEAPON_EXP_MASTER;
117     }
118
119     return WEAPON_EXP_UNSKILLED;
120 }
121
122 SUB_EXP PlayerSkill::spell_exp_at(int level)
123 {
124     switch (level) {
125     case EXP_LEVEL_UNSKILLED:
126         return SPELL_EXP_UNSKILLED;
127     case EXP_LEVEL_BEGINNER:
128         return SPELL_EXP_BEGINNER;
129     case EXP_LEVEL_SKILLED:
130         return SPELL_EXP_SKILLED;
131     case EXP_LEVEL_EXPERT:
132         return SPELL_EXP_EXPERT;
133     case EXP_LEVEL_MASTER:
134         return SPELL_EXP_MASTER;
135     }
136
137     return SPELL_EXP_UNSKILLED;
138 }
139 /*!
140  * @brief 武器や各種スキル(騎乗以外)の抽象的表現ランクを返す。 /  Return proficiency level of weapons and misc. skills (except riding)
141  * @param weapon_exp 経験値
142  * @return ランク値
143  */
144 int PlayerSkill::weapon_exp_level(int weapon_exp)
145 {
146     if (weapon_exp < WEAPON_EXP_BEGINNER)
147         return EXP_LEVEL_UNSKILLED;
148     else if (weapon_exp < WEAPON_EXP_SKILLED)
149         return EXP_LEVEL_BEGINNER;
150     else if (weapon_exp < WEAPON_EXP_EXPERT)
151         return EXP_LEVEL_SKILLED;
152     else if (weapon_exp < WEAPON_EXP_MASTER)
153         return EXP_LEVEL_EXPERT;
154     else
155         return EXP_LEVEL_MASTER;
156 }
157
158 bool PlayerSkill::valid_weapon_exp(int weapon_exp)
159 {
160     return (WEAPON_EXP_UNSKILLED <= weapon_exp) && (weapon_exp <= WEAPON_EXP_MASTER);
161 }
162
163 /*!
164  * @brief 騎乗スキルの抽象的ランクを返す。 / Return proficiency level of riding
165  * @param riding_exp 経験値
166  * @return ランク値
167  */
168 int PlayerSkill::riding_exp_level(int riding_exp)
169 {
170     if (riding_exp < RIDING_EXP_BEGINNER)
171         return EXP_LEVEL_UNSKILLED;
172     else if (riding_exp < RIDING_EXP_SKILLED)
173         return EXP_LEVEL_BEGINNER;
174     else if (riding_exp < RIDING_EXP_EXPERT)
175         return EXP_LEVEL_SKILLED;
176     else if (riding_exp < RIDING_EXP_MASTER)
177         return EXP_LEVEL_EXPERT;
178     else
179         return EXP_LEVEL_MASTER;
180 }
181
182 /*!
183  * @brief プレイヤーの呪文レベルの抽象的ランクを返す。 / Return proficiency level of spells
184  * @param spell_exp 経験値
185  * @return ランク値
186  */
187 int PlayerSkill::spell_exp_level(int spell_exp)
188 {
189     if (spell_exp < SPELL_EXP_BEGINNER)
190         return EXP_LEVEL_UNSKILLED;
191     else if (spell_exp < SPELL_EXP_SKILLED)
192         return EXP_LEVEL_BEGINNER;
193     else if (spell_exp < SPELL_EXP_EXPERT)
194         return EXP_LEVEL_SKILLED;
195     else if (spell_exp < SPELL_EXP_MASTER)
196         return EXP_LEVEL_EXPERT;
197     else
198         return EXP_LEVEL_MASTER;
199 }
200
201 concptr PlayerSkill::skill_name(PlayerSkillKindType skill)
202 {
203     switch (skill) {
204     case PlayerSkillKindType::MARTIAL_ARTS:
205         return _("マーシャルアーツ", "Martial Arts");
206     case PlayerSkillKindType::TWO_WEAPON:
207         return _("二刀流", "Dual Wielding");
208     case PlayerSkillKindType::RIDING:
209         return _("乗馬", "Riding");
210     case PlayerSkillKindType::SHIELD:
211         return _("盾", "Shield");
212     case PlayerSkillKindType::MAX:
213         break;
214     }
215
216     return _("不明", "Unknown");
217 }
218
219 void PlayerSkill::gain_melee_weapon_exp(const object_type *o_ptr)
220 {
221     const GainAmountList gain_amount_list{ 80, 10, 1, (one_in_(2) ? 1 : 0) };
222     constexpr GainAmountList others_gain_amount_list{ 8, 1, 0, 0 };
223
224     for (auto sval = 0U; sval < this->player_ptr->weapon_exp[o_ptr->tval].size(); ++sval) {
225         auto &now_exp = this->player_ptr->weapon_exp[o_ptr->tval][sval];
226         if (now_exp < s_info[enum2i(this->player_ptr->pclass)].w_max[o_ptr->tval][sval]) {
227             gain_attack_skill_exp(this->player_ptr, now_exp,
228                 (static_cast<int>(sval) == o_ptr->sval) ? gain_amount_list : others_gain_amount_list);
229         }
230     }
231 }
232
233 void PlayerSkill::gain_range_weapon_exp(const object_type *o_ptr)
234 {
235     constexpr GainAmountList gain_amount_list{ 80, 25, 10, 2 };
236     constexpr GainAmountList others_gain_amount_list{ 8, 2, 0, 0 };
237
238     for (auto sval = 0U; sval < this->player_ptr->weapon_exp[o_ptr->tval].size(); ++sval) {
239         auto &now_exp = this->player_ptr->weapon_exp[o_ptr->tval][sval];
240         if (now_exp < s_info[enum2i(this->player_ptr->pclass)].w_max[o_ptr->tval][sval]) {
241             gain_attack_skill_exp(this->player_ptr, now_exp,
242                 (static_cast<int>(sval) == o_ptr->sval) ? gain_amount_list : others_gain_amount_list);
243         }
244     }
245 }
246
247 void PlayerSkill::gain_martial_arts_skill_exp()
248 {
249     if (this->player_ptr->skill_exp[PlayerSkillKindType::MARTIAL_ARTS] < s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::MARTIAL_ARTS]) {
250         const GainAmountList gain_amount_list{ 40, 5, 1, (one_in_(3) ? 1 : 0) };
251         gain_attack_skill_exp(this->player_ptr, this->player_ptr->skill_exp[PlayerSkillKindType::MARTIAL_ARTS], gain_amount_list);
252     }
253 }
254
255 void PlayerSkill::gain_two_weapon_skill_exp()
256 {
257     if (this->player_ptr->skill_exp[PlayerSkillKindType::TWO_WEAPON] < s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::TWO_WEAPON]) {
258         const GainAmountList gain_amount_list{ 80, 4, 1, (one_in_(3) ? 1 : 0) };
259         gain_attack_skill_exp(this->player_ptr, this->player_ptr->skill_exp[PlayerSkillKindType::TWO_WEAPON], gain_amount_list);
260     }
261 }
262
263 void PlayerSkill::gain_riding_skill_exp_on_melee_attack(const monster_race *r_ptr)
264 {
265     auto now_exp = this->player_ptr->skill_exp[PlayerSkillKindType::RIDING];
266     auto max_exp = s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::RIDING];
267     if (now_exp >= max_exp)
268         return;
269
270     auto riding_level = r_info[this->player_ptr->current_floor_ptr->m_list[this->player_ptr->riding].r_idx].level;
271     int inc = 0;
272
273     if ((now_exp / 200 - 5) < r_ptr->level)
274         inc += 1;
275
276     if ((now_exp / 100) < riding_level) {
277         if ((now_exp / 100 + 15) < riding_level)
278             inc += 1 + (riding_level - (now_exp / 100 + 15));
279         else
280             inc += 1;
281     }
282
283     this->player_ptr->skill_exp[PlayerSkillKindType::RIDING] = std::min<SUB_EXP>(max_exp, now_exp + inc);
284     set_bits(this->player_ptr->update, PU_BONUS);
285 }
286
287 void PlayerSkill::gain_riding_skill_exp_on_range_attack()
288 {
289     auto now_exp = this->player_ptr->skill_exp[PlayerSkillKindType::RIDING];
290     auto max_exp = s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::RIDING];
291     if (now_exp >= max_exp)
292         return;
293
294     if (((this->player_ptr->skill_exp[PlayerSkillKindType::RIDING] - (RIDING_EXP_BEGINNER * 2)) / 200 < r_info[this->player_ptr->current_floor_ptr->m_list[this->player_ptr->riding].r_idx].level) && one_in_(2)) {
295         this->player_ptr->skill_exp[PlayerSkillKindType::RIDING] += 1;
296         set_bits(this->player_ptr->update, PU_BONUS);
297     }
298 }
299
300 void PlayerSkill::gain_riding_skill_exp_on_fall_off_check(HIT_POINT dam)
301 {
302     auto now_exp = this->player_ptr->skill_exp[PlayerSkillKindType::RIDING];
303     auto max_exp = s_info[enum2i(this->player_ptr->pclass)].s_max[PlayerSkillKindType::RIDING];
304     if (now_exp >= max_exp || max_exp <= 1000)
305         return;
306
307     auto riding_level = r_info[this->player_ptr->current_floor_ptr->m_list[this->player_ptr->riding].r_idx].level;
308
309     if ((dam / 2 + riding_level) <= (now_exp / 30 + 10))
310         return;
311
312     int inc = 0;
313     if ((now_exp / 100 + 15) < riding_level)
314         inc += 1 + (riding_level - (now_exp / 100 + 15));
315     else
316         inc += 1;
317
318     this->player_ptr->skill_exp[PlayerSkillKindType::RIDING] = std::min<SUB_EXP>(max_exp, now_exp + inc);
319     set_bits(this->player_ptr->update, PU_BONUS);
320 }
321
322 void PlayerSkill::gain_spell_skill_exp(int realm, int spell_idx)
323 {
324     if (((spell_idx < 0) || (32 <= spell_idx)) ||
325         ((realm < 1) || (static_cast<int>(std::size(mp_ptr->info)) < realm)) ||
326         ((realm != this->player_ptr->realm1) && (realm != this->player_ptr->realm2))) {
327         return;
328     }
329
330     constexpr GainAmountList gain_amount_list_first{ 60, 8, 2, 1 };
331     constexpr GainAmountList gain_amount_list_second{ 60, 8, 2, 0 };
332
333     const auto is_first_realm = (realm == this->player_ptr->realm1);
334     const auto *s_ptr = &mp_ptr->info[realm - 1][spell_idx];
335
336     gain_spell_skill_exp_aux(this->player_ptr, this->player_ptr->spell_exp[spell_idx + (is_first_realm ? 0 : 32)],
337         (is_first_realm ? gain_amount_list_first : gain_amount_list_second), s_ptr->slevel);
338 }
339
340 void PlayerSkill::gain_continuous_spell_skill_exp(int realm, int spell_idx)
341 {
342     if (((spell_idx < 0) || (32 <= spell_idx)) ||
343         ((realm != REALM_MUSIC) && (realm != REALM_HEX))) {
344         return;
345     }
346
347     const auto *s_ptr = &technic_info[realm - MIN_TECHNIC][spell_idx];
348
349     const GainAmountList gain_amount_list{ 5, (one_in_(2) ? 1 : 0), (one_in_(5) ? 1 : 0), (one_in_(5) ? 1 : 0) };
350
351     gain_spell_skill_exp_aux(this->player_ptr, this->player_ptr->spell_exp[spell_idx], gain_amount_list, s_ptr->slevel);
352 }
353
354 int PlayerSkill::gain_spell_skill_exp_over_learning(int spell_idx)
355 {
356     if ((spell_idx < 0) || (static_cast<int>(std::size(this->player_ptr->spell_exp)) <= spell_idx)) {
357         return EXP_LEVEL_UNSKILLED;
358     }
359
360     auto &exp = this->player_ptr->spell_exp[spell_idx];
361
362     if (exp >= SPELL_EXP_EXPERT) {
363         exp = SPELL_EXP_MASTER;
364     } else if (exp >= SPELL_EXP_SKILLED) {
365         if (spell_idx >= 32) {
366             exp = SPELL_EXP_EXPERT;
367         } else {
368             exp += SPELL_EXP_EXPERT - SPELL_EXP_SKILLED;
369         }
370     } else if (exp >= SPELL_EXP_BEGINNER) {
371         exp = SPELL_EXP_SKILLED + (exp - SPELL_EXP_BEGINNER) * 2 / 3;
372     } else {
373         exp = SPELL_EXP_BEGINNER + exp / 3;
374     }
375
376     set_bits(this->player_ptr->update, PU_BONUS);
377
378     return PlayerSkill::spell_exp_level(exp);
379 }
380
381 /*!
382  * @brief 呪文の経験値を返す /
383  * Returns experience of a spell
384  * @param use_realm 魔法領域
385  * @param spell_idx 呪文ID
386  * @return 経験値
387  */
388 EXP PlayerSkill::exp_of_spell(int realm, int spell_idx) const
389 {
390     if (this->player_ptr->pclass == PlayerClassType::SORCERER)
391         return SPELL_EXP_MASTER;
392     else if (this->player_ptr->pclass == PlayerClassType::RED_MAGE)
393         return SPELL_EXP_SKILLED;
394     else if (realm == this->player_ptr->realm1)
395         return this->player_ptr->spell_exp[spell_idx];
396     else if (realm == this->player_ptr->realm2)
397         return this->player_ptr->spell_exp[spell_idx + 32];
398     else
399         return 0;
400 }