OSDN Git Service

[Refactor] #2122 PlayerType::confused を削除し、TimedEffects::Confusion 側に処理を全て移した
[hengbandforosx/hengbandosx.git] / src / action / activation-execution.cpp
1 /*!
2  * @file activation-execution.cpp
3  * @brief アイテムの発動実行定義
4  */
5
6 #include "action/activation-execution.h"
7 #include "action/action-limited.h"
8 #include "artifact/artifact-info.h"
9 #include "artifact/random-art-effects.h"
10 #include "core/window-redrawer.h"
11 #include "effect/attribute-types.h"
12 #include "effect/spells-effect-util.h"
13 #include "floor/geometry.h"
14 #include "game-option/disturbance-options.h"
15 #include "game-option/input-options.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "monster-floor/monster-generator.h"
19 #include "monster-floor/place-monster-types.h"
20 #include "monster-race/monster-race.h"
21 #include "monster/monster-info.h"
22 #include "monster/monster-util.h"
23 #include "object-activation/activation-switcher.h"
24 #include "object-activation/activation-util.h"
25 #include "object-enchant/activation-info-table.h"
26 #include "object-enchant/object-ego.h"
27 #include "object/object-info.h"
28 #include "object/object-kind.h"
29 #include "player-base/player-class.h"
30 #include "player-status/player-energy.h"
31 #include "racial/racial-android.h"
32 #include "specific-object/monster-ball.h"
33 #include "spell-kind/spells-launcher.h"
34 #include "spell-kind/spells-teleport.h"
35 #include "spell-realm/spells-hex.h"
36 #include "spell-realm/spells-song.h"
37 #include "sv-definition/sv-lite-types.h"
38 #include "sv-definition/sv-ring-types.h"
39 #include "system/artifact-type-definition.h"
40 #include "system/floor-type-definition.h"
41 #include "system/monster-type-definition.h"
42 #include "system/object-type-definition.h"
43 #include "system/player-type-definition.h"
44 #include "target/target-getter.h"
45 #include "term/screen-processor.h"
46 #include "timed-effect/player-confusion.h"
47 #include "timed-effect/timed-effects.h"
48 #include "util/quarks.h"
49 #include "util/sort.h"
50 #include "view/display-messages.h"
51 #include "world/world.h"
52
53 static void decide_activation_level(ae_type *ae_ptr)
54 {
55     if (ae_ptr->o_ptr->is_fixed_artifact()) {
56         ae_ptr->lev = a_info[ae_ptr->o_ptr->fixed_artifact_idx].level;
57         return;
58     }
59
60     if (ae_ptr->o_ptr->is_random_artifact()) {
61         auto act_ptr = find_activation_info(ae_ptr->o_ptr);
62         if (act_ptr.has_value()) {
63             ae_ptr->lev = act_ptr.value()->level;
64         }
65
66         return;
67     }
68
69     if (((ae_ptr->o_ptr->tval == ItemKindType::RING) || (ae_ptr->o_ptr->tval == ItemKindType::AMULET)) && ae_ptr->o_ptr->is_ego()) {
70         ae_ptr->lev = e_info[ae_ptr->o_ptr->ego_idx].level;
71     }
72 }
73
74 static void decide_chance_fail(PlayerType *player_ptr, ae_type *ae_ptr)
75 {
76     ae_ptr->chance = player_ptr->skill_dev;
77     if (player_ptr->effects()->confusion()->is_confused()) {
78         ae_ptr->chance = ae_ptr->chance / 2;
79     }
80
81     ae_ptr->fail = ae_ptr->lev + 5;
82     if (ae_ptr->chance > ae_ptr->fail) {
83         ae_ptr->fail -= (ae_ptr->chance - ae_ptr->fail) * 2;
84     } else {
85         ae_ptr->chance -= (ae_ptr->fail - ae_ptr->chance) * 2;
86     }
87
88     if (ae_ptr->fail < USE_DEVICE) {
89         ae_ptr->fail = USE_DEVICE;
90     }
91
92     if (ae_ptr->chance < USE_DEVICE) {
93         ae_ptr->chance = USE_DEVICE;
94     }
95 }
96
97 static void decide_activation_success(PlayerType *player_ptr, ae_type *ae_ptr)
98 {
99     if (PlayerClass(player_ptr).equals(PlayerClassType::BERSERKER)) {
100         ae_ptr->success = false;
101         return;
102     }
103
104     if (ae_ptr->chance > ae_ptr->fail) {
105         ae_ptr->success = randint0(ae_ptr->chance * 2) >= ae_ptr->fail;
106         return;
107     }
108
109     ae_ptr->success = randint0(ae_ptr->fail * 2) < ae_ptr->chance;
110 }
111
112 static bool check_activation_success(ae_type *ae_ptr)
113 {
114     if (ae_ptr->success) {
115         return true;
116     }
117
118     if (flush_failure) {
119         flush();
120     }
121
122     msg_print(_("うまく始動させることができなかった。", "You failed to activate it properly."));
123     sound(SOUND_FAIL);
124     return false;
125 }
126
127 static bool check_activation_conditions(PlayerType *player_ptr, ae_type *ae_ptr)
128 {
129     if (!check_activation_success(ae_ptr)) {
130         return false;
131     }
132
133     if (ae_ptr->o_ptr->timeout) {
134         msg_print(_("それは微かに音を立て、輝き、消えた...", "It whines, glows and fades..."));
135         return false;
136     }
137
138     if (ae_ptr->o_ptr->is_fuel() && (ae_ptr->o_ptr->fuel == 0)) {
139         msg_print(_("燃料がない。", "It has no fuel."));
140         PlayerEnergy(player_ptr).reset_player_turn();
141         return false;
142     }
143
144     return true;
145 }
146
147 /*!
148  * @brief アイテムの発動効果を処理する。
149  * @param player_ptr プレイヤーへの参照ポインタ
150  * @param o_ptr 対象のオブジェクト構造体ポインタ
151  * @return 発動実行の是非を返す。
152  */
153 static bool activate_artifact(PlayerType *player_ptr, ObjectType *o_ptr)
154 {
155     concptr name = k_info[o_ptr->k_idx].name.c_str();
156     auto tmp_act_ptr = find_activation_info(o_ptr);
157     if (!tmp_act_ptr.has_value()) {
158         msg_print("Activation information is not found.");
159         return false;
160     }
161
162     auto *act_ptr = tmp_act_ptr.value();
163     if (!switch_activation(player_ptr, &o_ptr, act_ptr, name)) {
164         return false;
165     }
166
167     if (act_ptr->timeout.constant >= 0) {
168         o_ptr->timeout = (int16_t)act_ptr->timeout.constant;
169         if (act_ptr->timeout.dice > 0) {
170             o_ptr->timeout += randint1(act_ptr->timeout.dice);
171         }
172
173         return true;
174     }
175
176     switch (act_ptr->index) {
177     case RandomArtActType::BR_FIRE:
178         o_ptr->timeout = ((o_ptr->tval == ItemKindType::RING) && (o_ptr->sval == SV_RING_FLAMES)) ? 200 : 250;
179         return true;
180     case RandomArtActType::BR_COLD:
181         o_ptr->timeout = ((o_ptr->tval == ItemKindType::RING) && (o_ptr->sval == SV_RING_ICE)) ? 200 : 250;
182         return true;
183     case RandomArtActType::TERROR:
184         o_ptr->timeout = 3 * (player_ptr->lev + 10);
185         return true;
186     case RandomArtActType::MURAMASA:
187         return true;
188     default:
189         msg_format("Special timeout is not implemented: %d.", act_ptr->index);
190         return false;
191     }
192 }
193
194 static bool activate_whistle(PlayerType *player_ptr, ae_type *ae_ptr)
195 {
196     if (ae_ptr->o_ptr->tval != ItemKindType::WHISTLE) {
197         return false;
198     }
199
200     if (music_singing_any(player_ptr)) {
201         stop_singing(player_ptr);
202     }
203
204     if (SpellHex(player_ptr).is_spelling_any()) {
205         (void)SpellHex(player_ptr).stop_all_spells();
206     }
207
208     std::vector<MONSTER_IDX> who;
209     for (MONSTER_IDX pet_ctr = player_ptr->current_floor_ptr->m_max - 1; pet_ctr >= 1; pet_ctr--) {
210         if (is_pet(&player_ptr->current_floor_ptr->m_list[pet_ctr]) && (player_ptr->riding != pet_ctr)) {
211             who.push_back(pet_ctr);
212         }
213     }
214
215     uint16_t dummy_why;
216     ang_sort(player_ptr, who.data(), &dummy_why, who.size(), ang_sort_comp_pet, ang_sort_swap_hook);
217     for (auto pet_ctr : who) {
218         teleport_monster_to(player_ptr, pet_ctr, player_ptr->y, player_ptr->x, 100, TELEPORT_PASSIVE);
219     }
220
221     ae_ptr->o_ptr->timeout = 100 + randint1(100);
222     return true;
223 }
224
225 /*!
226  * @brief 装備を発動するコマンドのサブルーチン /
227  * Activate a wielded object.  Wielded objects never stack.
228  * And even if they did, activatable objects never stack.
229  * @param item 発動するオブジェクトの所持品ID
230  * @details
231  * <pre>
232  * Currently, only (some) artifacts, and Dragon Scale Mail, can be activated.
233  * But one could, for example, easily make an activatable "Ring of Plasma".
234  * Note that it always takes a turn to activate an artifact, even if
235  * the user hits "escape" at the "direction" prompt.
236  * </pre>
237  */
238 void exe_activate(PlayerType *player_ptr, INVENTORY_IDX item)
239 {
240     PlayerEnergy(player_ptr).set_player_turn_energy(100);
241     ae_type tmp_ae;
242     ae_type *ae_ptr = initialize_ae_type(player_ptr, &tmp_ae, item);
243     decide_activation_level(ae_ptr);
244     decide_chance_fail(player_ptr, ae_ptr);
245     if (cmd_limit_time_walk(player_ptr)) {
246         return;
247     }
248
249     decide_activation_success(player_ptr, ae_ptr);
250     if (!check_activation_conditions(player_ptr, ae_ptr)) {
251         return;
252     }
253
254     msg_print(_("始動させた...", "You activate it..."));
255     sound(SOUND_ZAP);
256     if (activation_index(ae_ptr->o_ptr) > RandomArtActType::NONE) {
257         (void)activate_artifact(player_ptr, ae_ptr->o_ptr);
258         player_ptr->window_flags |= PW_INVEN | PW_EQUIP;
259         return;
260     }
261
262     if (activate_whistle(player_ptr, ae_ptr)) {
263         return;
264     }
265
266     if (exe_monster_capture(player_ptr, ae_ptr)) {
267         return;
268     }
269
270     msg_print(_("おっと、このアイテムは始動できない。", "Oops.  That object cannot be activated."));
271 }