OSDN Git Service

362f777f71a80111f64a8fc45567c45be48e5880
[hengbandforosx/hengbandosx.git] / src / pet / pet-util.cpp
1 #include "pet/pet-util.h"
2 #include "core/player-update-types.h"
3 #include "core/stuff-handler.h"
4 #include "grid/grid.h"
5 #include "monster-race/monster-race.h"
6 #include "monster-race/race-flags1.h"
7 #include "monster-race/race-flags7.h"
8 #include "monster/monster-info.h"
9 #include "monster/monster-status.h"
10 #include "player/player-class.h"
11 #include "system/floor-type-definition.h"
12 #include "system/grid-type-definition.h"
13 #include "system/monster-race-definition.h"
14 #include "system/monster-type-definition.h"
15 #include "system/player-type-definition.h"
16 #include "util/bit-flags-calculator.h"
17 #include "world/world.h"
18
19 int total_friends = 0;
20
21 /*!
22  * @brief プレイヤーの騎乗/下馬処理判定
23  * @param g_ptr プレイヤーの移動先マスの構造体参照ポインタ
24  * @param now_riding trueなら下馬処理、falseならば騎乗処理
25  * @return 可能ならばtrueを返す
26  */
27 bool can_player_ride_pet(player_type *creature_ptr, grid_type *g_ptr, bool now_riding)
28 {
29     bool old_character_xtra = current_world_ptr->character_xtra;
30     MONSTER_IDX old_riding = creature_ptr->riding;
31     bool old_riding_two_hands = creature_ptr->riding_ryoute;
32     bool old_old_riding_two_hands = creature_ptr->old_riding_ryoute;
33     bool old_pf_two_hands = (creature_ptr->pet_extra_flags & PF_TWO_HANDS) ? true : false;
34     current_world_ptr->character_xtra = true;
35
36     if (now_riding)
37         creature_ptr->riding = g_ptr->m_idx;
38     else {
39         creature_ptr->riding = 0;
40         creature_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
41         creature_ptr->riding_ryoute = creature_ptr->old_riding_ryoute = false;
42     }
43
44     creature_ptr->update |= PU_BONUS;
45     handle_stuff(creature_ptr);
46
47     bool p_can_enter = player_can_enter(creature_ptr, g_ptr->feat, CEM_P_CAN_ENTER_PATTERN);
48     creature_ptr->riding = old_riding;
49     if (old_pf_two_hands)
50         creature_ptr->pet_extra_flags |= (PF_TWO_HANDS);
51     else
52         creature_ptr->pet_extra_flags &= ~(PF_TWO_HANDS);
53
54     creature_ptr->riding_ryoute = old_riding_two_hands;
55     creature_ptr->old_riding_ryoute = old_old_riding_two_hands;
56     creature_ptr->update |= PU_BONUS;
57     handle_stuff(creature_ptr);
58
59     current_world_ptr->character_xtra = old_character_xtra;
60     return p_can_enter;
61 }
62
63 /*!
64  * @brief ペットの維持コスト計算
65  * @return 維持コスト(%)
66  */
67 PERCENTAGE calculate_upkeep(player_type *creature_ptr)
68 {
69     bool has_a_unique = false;
70     DEPTH total_friend_levels = 0;
71     total_friends = 0;
72     for (auto m_idx = creature_ptr->current_floor_ptr->m_max - 1; m_idx >= 1; m_idx--) {
73         auto *m_ptr = &creature_ptr->current_floor_ptr->m_list[m_idx];
74         if (!monster_is_valid(m_ptr))
75             continue;
76         auto *r_ptr = &r_info[m_ptr->r_idx];
77
78         if (!is_pet(m_ptr)) {
79             continue;
80         }
81
82         total_friends++;
83         if (none_bits(r_ptr->flags1, RF1_UNIQUE)) {
84             total_friend_levels += r_ptr->level;
85             continue;
86         }
87
88         if (creature_ptr->pclass != CLASS_CAVALRY) {
89             total_friend_levels += (r_ptr->level + 5) * 10;
90             continue;
91         }
92
93         if (creature_ptr->riding == m_idx)
94             total_friend_levels += (r_ptr->level + 5) * 2;
95         else if (!has_a_unique && any_bits(r_info[m_ptr->r_idx].flags7, RF7_RIDING))
96             total_friend_levels += (r_ptr->level + 5) * 7 / 2;
97         else
98             total_friend_levels += (r_ptr->level + 5) * 10;
99
100         has_a_unique = true;
101     }
102
103     if (total_friends == 0) {
104         return 0;
105     }
106
107     int upkeep_factor = (total_friend_levels - (creature_ptr->lev * 80 / (cp_ptr->pet_upkeep_div)));
108     if (upkeep_factor < 0) {
109         upkeep_factor = 0;
110     }
111
112     if (upkeep_factor > 1000) {
113         upkeep_factor = 1000;
114     }
115
116     return upkeep_factor;
117 }