OSDN Git Service

[Refactor] #3230 PlayerType::update に関わる処理を、RedrawingFlagsUpdaterに集約した
[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/player-update-types.h"
11 #include "core/window-redrawer.h"
12 #include "inventory/inventory-object.h"
13 #include "main/sound-definitions-table.h"
14 #include "main/sound-of-music.h"
15 #include "object-use/item-use-checker.h"
16 #include "object-use/read/read-executor-factory.h"
17 #include "object/object-info.h"
18 #include "perception/object-perception.h"
19 #include "player-base/player-class.h"
20 #include "player-status/player-energy.h"
21 #include "spell-realm/spells-hex.h"
22 #include "spell-realm/spells-song.h"
23 #include "status/experience.h"
24 #include "system/baseitem-info.h"
25 #include "system/item-entity.h"
26 #include "system/player-type-definition.h"
27 #include "system/redrawing-flags-updater.h"
28 #include "util/bit-flags-calculator.h"
29 #include "view/display-messages.h"
30
31 /*!
32  * @brief コンストラクタ
33  * @param player_ptr プレイヤーへの参照ポインタ
34  * @param item 読むオブジェクトの所持品ID
35  */
36 ObjectReadEntity::ObjectReadEntity(PlayerType *player_ptr, INVENTORY_IDX item)
37     : player_ptr(player_ptr)
38     , item(item)
39 {
40 }
41
42 /*!
43  * @brief 巻物を読む
44  * @param known 判明済ならばTRUE
45  */
46 void ObjectReadEntity::execute(bool known)
47 {
48     auto *o_ptr = ref_item(this->player_ptr, this->item);
49     PlayerEnergy(this->player_ptr).set_player_turn_energy(100);
50     if (!this->can_read()) {
51         return;
52     }
53
54     if (music_singing_any(this->player_ptr)) {
55         stop_singing(this->player_ptr);
56     }
57
58     SpellHex spell_hex(this->player_ptr);
59     if (spell_hex.is_spelling_any() && ((this->player_ptr->lev < 35) || spell_hex.is_casting_full_capacity())) {
60         (void)SpellHex(this->player_ptr).stop_all_spells();
61     }
62
63     auto executor = ReadExecutorFactory::create(player_ptr, o_ptr, known);
64     auto used_up = executor->read();
65     auto &rfu = RedrawingFlagsUpdater::get_instance();
66     using Srf = StatusRedrawingFlag;
67     EnumClassFlagGroup<Srf> flags_srf = { Srf::COMBINATION, Srf::REORDER };
68     if (rfu.has(Srf::AUTO_DESTRUCTION)) {
69         flags_srf.set(Srf::AUTO_DESTRUCTION);
70     }
71
72     rfu.reset_flags(flags_srf);
73     this->change_virtue_as_read(*o_ptr);
74     object_tried(o_ptr);
75     this->gain_exp_from_item_use(o_ptr, executor->is_identified());
76     this->player_ptr->window_flags |= PW_INVENTORY | PW_EQUIPMENT | PW_PLAYER;
77     rfu.set_flags(flags_srf);
78     if (!used_up) {
79         return;
80     }
81
82     sound(SOUND_SCROLL);
83     vary_item(this->player_ptr, this->item, -1);
84 }
85
86 bool ObjectReadEntity::can_read() const
87 {
88     if (cmd_limit_time_walk(this->player_ptr)) {
89         return false;
90     }
91
92     if (PlayerClass(this->player_ptr).equals(PlayerClassType::BERSERKER)) {
93         msg_print(_("巻物なんて読めない。", "You cannot read."));
94         return false;
95     }
96
97     return ItemUseChecker(this->player_ptr).check_stun(_("朦朧としていて読めなかった!", "You too stunned to read it!"));
98 }
99
100 void ObjectReadEntity::change_virtue_as_read(ItemEntity &o_ref)
101 {
102     if (o_ref.is_aware()) {
103         return;
104     }
105
106     chg_virtue(this->player_ptr, Virtue::PATIENCE, -1);
107     chg_virtue(this->player_ptr, Virtue::CHANCE, 1);
108     chg_virtue(this->player_ptr, Virtue::KNOWLEDGE, -1);
109 }
110
111 void ObjectReadEntity::gain_exp_from_item_use(ItemEntity *o_ptr, bool is_identified)
112 {
113     if (!is_identified || o_ptr->is_aware()) {
114         return;
115     }
116
117     object_aware(this->player_ptr, o_ptr);
118     auto lev = o_ptr->get_baseitem().level;
119     gain_exp(this->player_ptr, (lev + (this->player_ptr->lev >> 1)) / this->player_ptr->lev);
120 }