OSDN Git Service

Merge branch 'master' of https://github.com/hengband/hengband
[hengbandforosx/hengbandosx.git] / src / object-use / read / read-execution.cpp
1 /*!
2  * @brief 巻物を読んだ際の効果処理
3  * @date 2022/02/26
4  * @author Hourier
5  */
6
7 #include "object-use/read/read-execution.h"
8 #include "action/action-limited.h"
9 #include "avatar/avatar.h"
10 #include "core/window-redrawer.h"
11 #include "inventory/inventory-object.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-use/read/read-executor-factory.h"
16 #include "object/object-info.h"
17 #include "perception/object-perception.h"
18 #include "player-base/player-class.h"
19 #include "player-status/player-energy.h"
20 #include "spell-realm/spells-hex.h"
21 #include "spell-realm/spells-song.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 "util/bit-flags-calculator.h"
28 #include "view/display-messages.h"
29
30 /*!
31  * @brief コンストラクタ
32  * @param player_ptr プレイヤーへの参照ポインタ
33  * @param item 読むオブジェクトの所持品ID
34  */
35 ObjectReadEntity::ObjectReadEntity(PlayerType *player_ptr, INVENTORY_IDX item)
36     : player_ptr(player_ptr)
37     , item(item)
38 {
39 }
40
41 /*!
42  * @brief 巻物を読む
43  * @param known 判明済ならばTRUE
44  */
45 void ObjectReadEntity::execute(bool known)
46 {
47     auto *o_ptr = ref_item(this->player_ptr, this->item);
48     PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
49     if (!this->can_read()) {
50         return;
51     }
52
53     if (music_singing_any(this->player_ptr)) {
54         stop_singing(this->player_ptr);
55     }
56
57     SpellHex spell_hex(this->player_ptr);
58     if (spell_hex.is_spelling_any() && ((this->player_ptr->lev < 35) || spell_hex.is_casting_full_capacity())) {
59         (void)SpellHex(this->player_ptr).stop_all_spells();
60     }
61
62     auto executor = ReadExecutorFactory::create(player_ptr, o_ptr, known);
63     auto used_up = executor->read();
64     auto &rfu = RedrawingFlagsUpdater::get_instance();
65     using Srf = StatusRecalculatingFlag;
66     EnumClassFlagGroup<Srf> flags_srf = { Srf::COMBINATION, Srf::REORDER };
67     if (rfu.has(Srf::AUTO_DESTRUCTION)) {
68         flags_srf.set(Srf::AUTO_DESTRUCTION);
69     }
70
71     rfu.reset_flags(flags_srf);
72     this->change_virtue_as_read(*o_ptr);
73     object_tried(o_ptr);
74     this->gain_exp_from_item_use(o_ptr, executor->is_identified());
75     static constexpr auto flags_swrf = {
76         SubWindowRedrawingFlag::INVENTORY,
77         SubWindowRedrawingFlag::EQUIPMENT,
78         SubWindowRedrawingFlag::PLAYER,
79     };
80     rfu.set_flags(flags_swrf);
81     rfu.set_flags(flags_srf);
82     if (!used_up) {
83         return;
84     }
85
86     sound(SOUND_SCROLL);
87     vary_item(this->player_ptr, this->item, -1);
88 }
89
90 bool ObjectReadEntity::can_read() const
91 {
92     if (cmd_limit_time_walk(this->player_ptr)) {
93         return false;
94     }
95
96     if (PlayerClass(this->player_ptr).equals(PlayerClassType::BERSERKER)) {
97         msg_print(_("巻物なんて読めない。", "You cannot read."));
98         return false;
99     }
100
101     return ItemUseChecker(this->player_ptr).check_stun(_("朦朧としていて読めなかった!", "You too stunned to read it!"));
102 }
103
104 void ObjectReadEntity::change_virtue_as_read(ItemEntity &o_ref)
105 {
106     if (o_ref.is_aware()) {
107         return;
108     }
109
110     chg_virtue(this->player_ptr, Virtue::PATIENCE, -1);
111     chg_virtue(this->player_ptr, Virtue::CHANCE, 1);
112     chg_virtue(this->player_ptr, Virtue::KNOWLEDGE, -1);
113 }
114
115 void ObjectReadEntity::gain_exp_from_item_use(ItemEntity *o_ptr, bool is_identified)
116 {
117     if (!is_identified || o_ptr->is_aware()) {
118         return;
119     }
120
121     object_aware(this->player_ptr, o_ptr);
122     auto lev = o_ptr->get_baseitem().level;
123     gain_exp(this->player_ptr, (lev + (this->player_ptr->lev >> 1)) / this->player_ptr->lev);
124 }