2 * @file activation-execution.cpp
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"
56 static void decide_activation_level(ae_type *ae_ptr)
58 if (ae_ptr->o_ptr->is_fixed_artifact()) {
59 ae_ptr->lev = ae_ptr->o_ptr->get_fixed_artifact().level;
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;
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;
78 static void decide_chance_fail(PlayerType *player_ptr, ae_type *ae_ptr)
80 ae_ptr->chance = player_ptr->skill_dev;
81 if (player_ptr->effects()->confusion()->is_confused()) {
82 ae_ptr->chance = ae_ptr->chance / 2;
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;
89 ae_ptr->chance -= (ae_ptr->fail - ae_ptr->chance) * 2;
92 if (ae_ptr->fail < USE_DEVICE) {
93 ae_ptr->fail = USE_DEVICE;
96 if (ae_ptr->chance < USE_DEVICE) {
97 ae_ptr->chance = USE_DEVICE;
101 static void decide_activation_success(PlayerType *player_ptr, ae_type *ae_ptr)
103 if (PlayerClass(player_ptr).equals(PlayerClassType::BERSERKER)) {
104 ae_ptr->success = false;
108 if (ae_ptr->chance > ae_ptr->fail) {
109 ae_ptr->success = randint0(ae_ptr->chance * 2) >= ae_ptr->fail;
113 ae_ptr->success = randint0(ae_ptr->fail * 2) < ae_ptr->chance;
116 static bool check_activation_success(ae_type *ae_ptr)
118 if (ae_ptr->success) {
126 msg_print(_("うまく始動させることができなかった。", "You failed to activate it properly."));
131 static bool check_activation_conditions(PlayerType *player_ptr, ae_type *ae_ptr)
133 if (!check_activation_success(ae_ptr)) {
137 if (ae_ptr->o_ptr->timeout) {
138 msg_print(_("それは微かに音を立て、輝き、消えた...", "It whines, glows and fades..."));
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();
152 * @brief アイテムの発動効果を処理する。
153 * @param player_ptr プレイヤーへの参照ポインタ
154 * @param o_ptr 対象のオブジェクト構造体ポインタ
155 * @return 発動実行の是非を返す。
157 static bool activate_artifact(PlayerType *player_ptr, ItemEntity *o_ptr)
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.");
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())) {
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);
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;
184 case RandomArtActType::BR_COLD:
185 o_ptr->timeout = o_ptr->bi_key == BaseitemKey(ItemKindType::RING, SV_RING_ICE) ? 200 : 250;
187 case RandomArtActType::TERROR:
188 o_ptr->timeout = 3 * (player_ptr->lev + 10);
190 case RandomArtActType::MURAMASA:
193 msg_format("Special timeout is not implemented: %d.", enum2i(act_ptr->index));
198 static bool activate_whistle(PlayerType *player_ptr, ae_type *ae_ptr)
200 if (ae_ptr->o_ptr->bi_key.tval() != ItemKindType::WHISTLE) {
204 if (music_singing_any(player_ptr)) {
205 stop_singing(player_ptr);
208 if (SpellHex(player_ptr).is_spelling_any()) {
209 (void)SpellHex(player_ptr).stop_all_spells();
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);
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);
226 ae_ptr->o_ptr->timeout = 100 + randint1(100);
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
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.
243 void exe_activate(PlayerType *player_ptr, INVENTORY_IDX item)
245 PlayerEnergy(player_ptr).set_player_turn_energy(100);
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)) {
254 decide_activation_success(player_ptr, ae_ptr);
255 if (!check_activation_conditions(player_ptr, ae_ptr)) {
259 msg_print(_("始動させた...", "You activate it..."));
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,
267 RedrawingFlagsUpdater::get_instance().set_flags(flags);
271 if (activate_whistle(player_ptr, ae_ptr)) {
275 if (exe_monster_capture(player_ptr, *ae_ptr->o_ptr)) {
279 msg_print(_("おっと、このアイテムは始動できない。", "Oops. That object cannot be activated."));