OSDN Git Service

Merge pull request #3569 from sikabane-works/release/3.0.0.88-alpha
[hengbandforosx/hengbandosx.git] / src / object-use / zapwand-execution.cpp
1 #include "object-use/zapwand-execution.h"
2 #include "action/action-limited.h"
3 #include "avatar/avatar.h"
4 #include "cmd-item/cmd-zapwand.h" // 相互依存。暫定的措置、後で何とかする.
5 #include "core/window-redrawer.h"
6 #include "floor/floor-object.h"
7 #include "game-option/disturbance-options.h"
8 #include "game-option/input-options.h"
9 #include "main/sound-definitions-table.h"
10 #include "main/sound-of-music.h"
11 #include "object-enchant/special-object-flags.h"
12 #include "object-use/item-use-checker.h"
13 #include "object/object-info.h"
14 #include "perception/object-perception.h"
15 #include "player-base/player-class.h"
16 #include "player-status/player-energy.h"
17 #include "status/experience.h"
18 #include "sv-definition/sv-wand-types.h"
19 #include "system/baseitem-info.h"
20 #include "system/item-entity.h"
21 #include "system/player-type-definition.h"
22 #include "system/redrawing-flags-updater.h"
23 #include "target/target-getter.h"
24 #include "term/screen-processor.h"
25 #include "timed-effect/player-confusion.h"
26 #include "timed-effect/timed-effects.h"
27 #include "util/bit-flags-calculator.h"
28 #include "view/display-messages.h"
29 #include "view/object-describer.h"
30
31 ObjectZapWandEntity::ObjectZapWandEntity(PlayerType *player_ptr)
32     : player_ptr(player_ptr)
33 {
34 }
35
36 /*!
37  * @brief 魔法棒を使うコマンドのサブルーチン /
38  * @param item 使うオブジェクトの所持品ID
39  */
40 void ObjectZapWandEntity::execute(INVENTORY_IDX item)
41 {
42     auto old_target_pet = target_pet;
43     auto *o_ptr = ref_item(this->player_ptr, item);
44     if ((item < 0) && (o_ptr->number > 1)) {
45         msg_print(_("まずは魔法棒を拾わなければ。", "You must first pick up the wands."));
46         return;
47     }
48
49     const auto sval = o_ptr->bi_key.sval();
50     if (o_ptr->is_aware() && (sval == SV_WAND_HEAL_MONSTER || sval == SV_WAND_HASTE_MONSTER)) {
51         target_pet = true;
52     }
53
54     DIRECTION dir;
55     if (!get_aim_dir(this->player_ptr, &dir)) {
56         target_pet = old_target_pet;
57         return;
58     }
59
60     target_pet = old_target_pet;
61     PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
62     if (!this->check_can_zap()) {
63         return;
64     }
65
66     auto lev = o_ptr->get_baseitem().level;
67     if (lev > 50) {
68         lev = 50 + (lev - 50) / 2;
69     }
70
71     auto chance = this->player_ptr->skill_dev;
72     if (this->player_ptr->effects()->confusion()->is_confused()) {
73         chance = chance / 2;
74     }
75
76     chance = chance - lev;
77     if ((chance < USE_DEVICE) && one_in_(USE_DEVICE - chance + 1)) {
78         chance = USE_DEVICE;
79     }
80
81     if ((chance < USE_DEVICE) || (randint1(chance) < USE_DEVICE) || PlayerClass(this->player_ptr).equals(PlayerClassType::BERSERKER)) {
82         if (flush_failure) {
83             flush();
84         }
85
86         msg_print(_("魔法棒をうまく使えなかった。", "You failed to use the wand properly."));
87         sound(SOUND_FAIL);
88         return;
89     }
90
91     auto &rfu = RedrawingFlagsUpdater::get_instance();
92     if (o_ptr->pval <= 0) {
93         if (flush_failure) {
94             flush();
95         }
96
97         msg_print(_("この魔法棒にはもう魔力が残っていない。", "The wand has no charges left."));
98         o_ptr->ident |= IDENT_EMPTY;
99         static constexpr auto flags = {
100             StatusRecalculatingFlag::COMBINATION,
101             StatusRecalculatingFlag::REORDER,
102         };
103         rfu.set_flags(flags);
104         rfu.set_flag(SubWindowRedrawingFlag::INVENTORY);
105         return;
106     }
107
108     sound(SOUND_ZAP);
109     auto ident = wand_effect(this->player_ptr, sval.value(), dir, false, false);
110     using Srf = StatusRecalculatingFlag;
111     EnumClassFlagGroup<Srf> flags_srf = { Srf::COMBINATION, Srf::REORDER };
112     if (rfu.has(Srf::AUTO_DESTRUCTION)) {
113         flags_srf.set(Srf::AUTO_DESTRUCTION);
114     }
115
116     rfu.reset_flags(flags_srf);
117     if (!(o_ptr->is_aware())) {
118         chg_virtue(this->player_ptr, Virtue::PATIENCE, -1);
119         chg_virtue(this->player_ptr, Virtue::CHANCE, 1);
120         chg_virtue(this->player_ptr, Virtue::KNOWLEDGE, -1);
121     }
122
123     object_tried(o_ptr);
124     if (ident && !o_ptr->is_aware()) {
125         object_aware(this->player_ptr, o_ptr);
126         gain_exp(this->player_ptr, (lev + (this->player_ptr->lev >> 1)) / this->player_ptr->lev);
127     }
128
129     static constexpr auto flags_swrf = {
130         SubWindowRedrawingFlag::INVENTORY,
131         SubWindowRedrawingFlag::EQUIPMENT,
132         SubWindowRedrawingFlag::PLAYER,
133         SubWindowRedrawingFlag::FLOOR_ITEMS,
134         SubWindowRedrawingFlag::FOUND_ITEMS,
135     };
136     rfu.set_flags(flags_swrf);
137     rfu.set_flags(flags_srf);
138     o_ptr->pval--;
139     if (item >= 0) {
140         inven_item_charges(this->player_ptr->inventory_list[item]);
141         return;
142     }
143
144     floor_item_charges(this->player_ptr->current_floor_ptr, 0 - item);
145 }
146
147 bool ObjectZapWandEntity::check_can_zap() const
148 {
149     if (cmd_limit_time_walk(this->player_ptr)) {
150         return false;
151     }
152
153     return ItemUseChecker(this->player_ptr).check_stun(_("朦朧としていて魔法棒を振れなかった!", "You are too stunned to zap it!"));
154 }