OSDN Git Service

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