OSDN Git Service

Merge branch 'develop' into macos-develop
[hengbandforosx/hengbandosx.git] / src / object-use / use-execution.cpp
1 /*!
2  * @brief 杖を振る処理
3  * @date 2021/09/25
4  * @author Hourier
5  */
6 #include "object-use/use-execution.h"
7 #include "action/action-limited.h"
8 #include "avatar/avatar.h"
9 #include "cmd-item/cmd-usestaff.h"
10 #include "core/window-redrawer.h"
11 #include "floor/floor-object.h"
12 #include "game-option/disturbance-options.h"
13 #include "inventory/inventory-object.h"
14 #include "main/sound-definitions-table.h"
15 #include "main/sound-of-music.h"
16 #include "object-enchant/special-object-flags.h"
17 #include "object-use/item-use-checker.h"
18 #include "object/object-info.h"
19 #include "perception/object-perception.h"
20 #include "player-base/player-class.h"
21 #include "player-status/player-energy.h"
22 #include "status/experience.h"
23 #include "system/baseitem-info.h"
24 #include "system/item-entity.h"
25 #include "system/player-type-definition.h"
26 #include "system/redrawing-flags-updater.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 #include "view/object-describer.h"
33
34 /*!
35  * @brief コンストラクタ
36  * @param player_ptr プレイヤーへの参照ポインタ
37  * @param i_idx 使うオブジェクトの所持品ID
38  */
39 ObjectUseEntity::ObjectUseEntity(PlayerType *player_ptr, INVENTORY_IDX i_idx)
40     : player_ptr(player_ptr)
41     , i_idx(i_idx)
42 {
43 }
44
45 /*!
46  * @brief 杖を使う
47  */
48 void ObjectUseEntity::execute()
49 {
50     auto use_charge = true;
51     auto *o_ptr = ref_item(this->player_ptr, this->i_idx);
52     if ((this->i_idx < 0) && (o_ptr->number > 1)) {
53         msg_print(_("まずは杖を拾わなければ。", "You must first pick up the staffs."));
54         return;
55     }
56
57     PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
58     if (!this->check_can_use()) {
59         return;
60     }
61
62     auto lev = o_ptr->get_baseitem().level;
63     if (lev > 50) {
64         lev = 50 + (lev - 50) / 2;
65     }
66
67     auto chance = this->player_ptr->skill_dev;
68     if (this->player_ptr->effects()->confusion()->is_confused()) {
69         chance = chance / 2;
70     }
71
72     chance = chance - lev;
73     if ((chance < USE_DEVICE) && one_in_(USE_DEVICE - chance + 1)) {
74         chance = USE_DEVICE;
75     }
76
77     if ((chance < USE_DEVICE) || (randint1(chance) < USE_DEVICE) || PlayerClass(this->player_ptr).equals(PlayerClassType::BERSERKER)) {
78         if (flush_failure) {
79             flush();
80         }
81
82         msg_print(_("杖をうまく使えなかった。", "You failed to use the staff properly."));
83         sound(SOUND_FAIL);
84         return;
85     }
86
87     if (o_ptr->pval <= 0) {
88         if (flush_failure) {
89             flush();
90         }
91
92         msg_print(_("この杖にはもう魔力が残っていない。", "The staff has no charges left."));
93         o_ptr->ident |= IDENT_EMPTY;
94         auto &rfu = RedrawingFlagsUpdater::get_instance();
95         static constexpr auto flags = {
96             StatusRecalculatingFlag::COMBINATION,
97             StatusRecalculatingFlag::REORDER,
98         };
99         rfu.set_flags(flags);
100         rfu.set_flag(SubWindowRedrawingFlag::INVENTORY);
101         return;
102     }
103
104     sound(SOUND_ZAP);
105     auto ident = staff_effect(this->player_ptr, *o_ptr->bi_key.sval(), &use_charge, false, false, o_ptr->is_aware());
106     if (!(o_ptr->is_aware())) {
107         chg_virtue(this->player_ptr, Virtue::PATIENCE, -1);
108         chg_virtue(this->player_ptr, Virtue::CHANCE, 1);
109         chg_virtue(this->player_ptr, Virtue::KNOWLEDGE, -1);
110     }
111
112     auto &rfu = RedrawingFlagsUpdater::get_instance();
113     using Srf = StatusRecalculatingFlag;
114     EnumClassFlagGroup<Srf> flags_srf = { Srf::COMBINATION, Srf::REORDER };
115     if (rfu.has(Srf::AUTO_DESTRUCTION)) {
116         flags_srf.set(Srf::AUTO_DESTRUCTION);
117     }
118
119     rfu.reset_flags(flags_srf);
120     o_ptr->mark_as_tried();
121     if (ident && !o_ptr->is_aware()) {
122         object_aware(this->player_ptr, o_ptr);
123         gain_exp(this->player_ptr, (lev + (this->player_ptr->lev >> 1)) / this->player_ptr->lev);
124     }
125
126     static constexpr auto flags_swrf = {
127         SubWindowRedrawingFlag::INVENTORY,
128         SubWindowRedrawingFlag::EQUIPMENT,
129         SubWindowRedrawingFlag::PLAYER,
130         SubWindowRedrawingFlag::FLOOR_ITEMS,
131         SubWindowRedrawingFlag::FOUND_ITEMS,
132     };
133     rfu.set_flags(flags_swrf);
134     rfu.set_flags(flags_srf);
135     if (!use_charge) {
136         return;
137     }
138
139     o_ptr->pval--;
140     if ((this->i_idx >= 0) && (o_ptr->number > 1)) {
141         ItemEntity forge;
142         auto *q_ptr = &forge;
143         q_ptr->copy_from(o_ptr);
144         q_ptr->number = 1;
145         o_ptr->pval++;
146         o_ptr->number--;
147         this->i_idx = store_item_to_inventory(this->player_ptr, q_ptr);
148         msg_print(_("杖をまとめなおした。", "You unstack your staff."));
149     }
150
151     if (this->i_idx >= 0) {
152         inven_item_charges(this->player_ptr->inventory_list[this->i_idx]);
153     } else {
154         floor_item_charges(this->player_ptr->current_floor_ptr, 0 - this->i_idx);
155     }
156 }
157
158 bool ObjectUseEntity::check_can_use()
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 use it!"));
165 }