OSDN Git Service

[Refactor] #3312 StatusRedrawingFlag をStatusRecalculatingFlag に改名した
[hengbandforosx/hengbandosx.git] / src / object-use / zaprod-execution.cpp
1 /*!
2  * @brief ロッドを振る処理
3  * @date 2021/09/25
4  * @author Hourier
5  */
6 #include "object-use/zaprod-execution.h"
7 #include "action/action-limited.h"
8 #include "avatar/avatar.h"
9 #include "cmd-item/cmd-zaprod.h" // 相互依存。暫定的措置、後で何とかする.
10 #include "core/window-redrawer.h"
11 #include "game-option/disturbance-options.h"
12 #include "main/sound-definitions-table.h"
13 #include "main/sound-of-music.h"
14 #include "object-use/item-use-checker.h"
15 #include "object/object-info.h"
16 #include "perception/object-perception.h"
17 #include "player-base/player-class.h"
18 #include "player-status/player-energy.h"
19 #include "status/experience.h"
20 #include "sv-definition/sv-other-types.h"
21 #include "sv-definition/sv-rod-types.h"
22 #include "system/baseitem-info.h"
23 #include "system/item-entity.h"
24 #include "system/player-type-definition.h"
25 #include "system/redrawing-flags-updater.h"
26 #include "target/target-getter.h"
27 #include "term/screen-processor.h"
28 #include "timed-effect/player-confusion.h"
29 #include "timed-effect/timed-effects.h"
30 #include "util/bit-flags-calculator.h"
31 #include "view/display-messages.h"
32
33 /*!
34  * @brief コンストラクタ
35  * @param player_ptr プレイヤーへの参照ポインタ
36  * @param item 使うオブジェクトの所持品ID
37  */
38 ObjectZapRodEntity::ObjectZapRodEntity(PlayerType *player_ptr)
39     : player_ptr(player_ptr)
40 {
41 }
42
43 /*!
44  * @brief ロッドを使う
45  */
46 void ObjectZapRodEntity::execute(INVENTORY_IDX item)
47 {
48     DIRECTION dir = 0;
49     auto use_charge = true;
50     auto *o_ptr = ref_item(this->player_ptr, item);
51     if ((item < 0) && (o_ptr->number > 1)) {
52         msg_print(_("まずはロッドを拾わなければ。", "You must first pick up the rods."));
53         return;
54     }
55
56     if (o_ptr->is_aiming_rod() || !o_ptr->is_aware()) {
57         if (!get_aim_dir(this->player_ptr, &dir)) {
58             return;
59         }
60     }
61
62     PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
63     if (!this->check_can_zap()) {
64         return;
65     }
66
67     auto lev = o_ptr->get_baseitem().level;
68     auto chance = this->player_ptr->skill_dev;
69     if (this->player_ptr->effects()->confusion()->is_confused()) {
70         chance = chance / 2;
71     }
72
73     auto fail = lev + 5;
74     if (chance > fail) {
75         fail -= (chance - fail) * 2;
76     } else {
77         chance -= (fail - chance) * 2;
78     }
79
80     if (fail < USE_DEVICE) {
81         fail = USE_DEVICE;
82     }
83
84     if (chance < USE_DEVICE) {
85         chance = USE_DEVICE;
86     }
87
88     bool success;
89     if (PlayerClass(this->player_ptr).equals(PlayerClassType::BERSERKER)) {
90         success = false;
91     } else if (chance > fail) {
92         success = randint0(chance * 2) >= fail;
93     } else {
94         success = randint0(fail * 2) < chance;
95     }
96
97     if (!success) {
98         if (flush_failure) {
99             flush();
100         }
101
102         msg_print(_("うまくロッドを使えなかった。", "You failed to use the rod properly."));
103         sound(SOUND_FAIL);
104         return;
105     }
106
107     const auto &baseitem = o_ptr->get_baseitem();
108     if ((o_ptr->number == 1) && (o_ptr->timeout)) {
109         if (flush_failure) {
110             flush();
111         }
112
113         msg_print(_("このロッドはまだ魔力を充填している最中だ。", "The rod is still charging."));
114         return;
115     } else if ((o_ptr->number > 1) && (o_ptr->timeout > baseitem.pval * (o_ptr->number - 1))) {
116         if (flush_failure) {
117             flush();
118         }
119
120         msg_print(_("そのロッドはまだ充填中です。", "The rods are all still charging."));
121         return;
122     }
123
124     sound(SOUND_ZAP);
125     auto ident = rod_effect(this->player_ptr, o_ptr->bi_key.sval().value(), dir, &use_charge, false);
126     if (use_charge) {
127         o_ptr->timeout += baseitem.pval;
128     }
129
130     auto &rfu = RedrawingFlagsUpdater::get_instance();
131     static constexpr auto flags_srf = {
132         StatusRecalculatingFlag::COMBINATION,
133         StatusRecalculatingFlag::REORDER,
134     };
135     rfu.set_flags(flags_srf);
136     if (!(o_ptr->is_aware())) {
137         chg_virtue(this->player_ptr, Virtue::PATIENCE, -1);
138         chg_virtue(this->player_ptr, Virtue::CHANCE, 1);
139         chg_virtue(this->player_ptr, Virtue::KNOWLEDGE, -1);
140     }
141
142     object_tried(o_ptr);
143     if ((ident != 0) && !o_ptr->is_aware()) {
144         object_aware(this->player_ptr, o_ptr);
145         gain_exp(this->player_ptr, (lev + (this->player_ptr->lev >> 1)) / this->player_ptr->lev);
146     }
147
148     static constexpr auto flags_swrf = {
149         SubWindowRedrawingFlag::INVENTORY,
150         SubWindowRedrawingFlag::EQUIPMENT,
151         SubWindowRedrawingFlag::PLAYER,
152         SubWindowRedrawingFlag::FLOOR_ITEMS,
153         SubWindowRedrawingFlag::FOUND_ITEMS,
154     };
155     rfu.set_flags(flags_swrf);
156 }
157
158 bool ObjectZapRodEntity::check_can_zap()
159 {
160     if (cmd_limit_time_walk(this->player_ptr)) {
161         return false;
162     }
163
164     return ItemUseChecker(this->player_ptr).check_stun(_("朦朧としていてロッドを振れなかった!", "You are too stunned to zap it!"));
165 }