OSDN Git Service

[Refactor] #2200 effect-processor::project() 以外の箇所からg_cap_mon_ptrの依存をなくした
[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 "util/quarks.h"
47 #include "util/sort.h"
48 #include "view/display-messages.h"
49 #include "world/world.h"
50
51 static void decide_activation_level(ae_type *ae_ptr)
52 {
53     if (ae_ptr->o_ptr->is_fixed_artifact()) {
54         ae_ptr->lev = a_info[ae_ptr->o_ptr->name1].level;
55         return;
56     }
57
58     if (ae_ptr->o_ptr->is_random_artifact()) {
59         auto act_ptr = find_activation_info(ae_ptr->o_ptr);
60         if (act_ptr.has_value()) {
61             ae_ptr->lev = act_ptr.value()->level;
62         }
63
64         return;
65     }
66
67     if (((ae_ptr->o_ptr->tval == ItemKindType::RING) || (ae_ptr->o_ptr->tval == ItemKindType::AMULET)) && ae_ptr->o_ptr->name2)
68         ae_ptr->lev = e_info[ae_ptr->o_ptr->name2].level;
69 }
70
71 static void decide_chance_fail(PlayerType *player_ptr, ae_type *ae_ptr)
72 {
73     ae_ptr->chance = player_ptr->skill_dev;
74     if (player_ptr->confused)
75         ae_ptr->chance = ae_ptr->chance / 2;
76
77     ae_ptr->fail = ae_ptr->lev + 5;
78     if (ae_ptr->chance > ae_ptr->fail)
79         ae_ptr->fail -= (ae_ptr->chance - ae_ptr->fail) * 2;
80     else
81         ae_ptr->chance -= (ae_ptr->fail - ae_ptr->chance) * 2;
82
83     if (ae_ptr->fail < USE_DEVICE)
84         ae_ptr->fail = USE_DEVICE;
85
86     if (ae_ptr->chance < USE_DEVICE)
87         ae_ptr->chance = USE_DEVICE;
88 }
89
90 static void decide_activation_success(PlayerType *player_ptr, ae_type *ae_ptr)
91 {
92     if (PlayerClass(player_ptr).equals(PlayerClassType::BERSERKER)) {
93         ae_ptr->success = false;
94         return;
95     }
96
97     if (ae_ptr->chance > ae_ptr->fail) {
98         ae_ptr->success = randint0(ae_ptr->chance * 2) >= ae_ptr->fail;
99         return;
100     }
101
102     ae_ptr->success = randint0(ae_ptr->fail * 2) < ae_ptr->chance;
103 }
104
105 static bool check_activation_success(ae_type *ae_ptr)
106 {
107     if (ae_ptr->success)
108         return true;
109
110     if (flush_failure)
111         flush();
112
113     msg_print(_("うまく始動させることができなかった。", "You failed to activate it properly."));
114     sound(SOUND_FAIL);
115     return false;
116 }
117
118 static bool check_activation_conditions(PlayerType *player_ptr, ae_type *ae_ptr)
119 {
120     if (!check_activation_success(ae_ptr))
121         return false;
122
123     if (ae_ptr->o_ptr->timeout) {
124         msg_print(_("それは微かに音を立て、輝き、消えた...", "It whines, glows and fades..."));
125         return false;
126     }
127
128     if (ae_ptr->o_ptr->is_fuel() && (ae_ptr->o_ptr->fuel == 0)) {
129         msg_print(_("燃料がない。", "It has no fuel."));
130         PlayerEnergy(player_ptr).reset_player_turn();
131         return false;
132     }
133
134     return true;
135 }
136
137 /*!
138  * @brief アイテムの発動効果を処理する。
139  * @param player_ptr プレイヤーへの参照ポインタ
140  * @param o_ptr 対象のオブジェクト構造体ポインタ
141  * @return 発動実行の是非を返す。
142  */
143 static bool activate_artifact(PlayerType *player_ptr, ObjectType *o_ptr)
144 {
145     concptr name = k_info[o_ptr->k_idx].name.c_str();
146     auto tmp_act_ptr = find_activation_info(o_ptr);
147     if (!tmp_act_ptr.has_value()) {
148         msg_print("Activation information is not found.");
149         return false;
150     }
151
152     auto *act_ptr = tmp_act_ptr.value();
153     if (!switch_activation(player_ptr, &o_ptr, act_ptr, name)) {
154         return false;
155     }
156
157     if (act_ptr->timeout.constant >= 0) {
158         o_ptr->timeout = (int16_t)act_ptr->timeout.constant;
159         if (act_ptr->timeout.dice > 0)
160             o_ptr->timeout += randint1(act_ptr->timeout.dice);
161
162         return true;
163     }
164
165     switch (act_ptr->index) {
166     case RandomArtActType::BR_FIRE:
167         o_ptr->timeout = ((o_ptr->tval == ItemKindType::RING) && (o_ptr->sval == SV_RING_FLAMES)) ? 200 : 250;
168         return true;
169     case RandomArtActType::BR_COLD:
170         o_ptr->timeout = ((o_ptr->tval == ItemKindType::RING) && (o_ptr->sval == SV_RING_ICE)) ? 200 : 250;
171         return true;
172     case RandomArtActType::TERROR:
173         o_ptr->timeout = 3 * (player_ptr->lev + 10);
174         return true;
175     case RandomArtActType::MURAMASA:
176         return true;
177     default:
178         msg_format("Special timeout is not implemented: %d.", act_ptr->index);
179         return false;
180     }
181 }
182
183 static bool activate_whistle(PlayerType *player_ptr, ae_type *ae_ptr)
184 {
185     if (ae_ptr->o_ptr->tval != ItemKindType::WHISTLE)
186         return false;
187
188     if (music_singing_any(player_ptr))
189         stop_singing(player_ptr);
190
191     if (SpellHex(player_ptr).is_spelling_any()) {
192         (void)SpellHex(player_ptr).stop_all_spells();
193     }
194
195     std::vector<MONSTER_IDX> who;
196     for (MONSTER_IDX pet_ctr = player_ptr->current_floor_ptr->m_max - 1; pet_ctr >= 1; pet_ctr--)
197         if (is_pet(&player_ptr->current_floor_ptr->m_list[pet_ctr]) && (player_ptr->riding != pet_ctr))
198             who.push_back(pet_ctr);
199
200     uint16_t dummy_why;
201     ang_sort(player_ptr, who.data(), &dummy_why, who.size(), ang_sort_comp_pet, ang_sort_swap_hook);
202     for (auto pet_ctr : who) {
203         teleport_monster_to(player_ptr, pet_ctr, player_ptr->y, player_ptr->x, 100, TELEPORT_PASSIVE);
204     }
205
206     ae_ptr->o_ptr->timeout = 100 + randint1(100);
207     return true;
208 }
209
210 /*!
211  * @brief 装備を発動するコマンドのサブルーチン /
212  * Activate a wielded object.  Wielded objects never stack.
213  * And even if they did, activatable objects never stack.
214  * @param item 発動するオブジェクトの所持品ID
215  * @details
216  * <pre>
217  * Currently, only (some) artifacts, and Dragon Scale Mail, can be activated.
218  * But one could, for example, easily make an activatable "Ring of Plasma".
219  * Note that it always takes a turn to activate an artifact, even if
220  * the user hits "escape" at the "direction" prompt.
221  * </pre>
222  */
223 void exe_activate(PlayerType *player_ptr, INVENTORY_IDX item, CapturedMonsterType *cap_mon_ptr)
224 {
225     PlayerEnergy(player_ptr).set_player_turn_energy(100);
226     ae_type tmp_ae;
227     ae_type *ae_ptr = initialize_ae_type(player_ptr, &tmp_ae, item);
228     decide_activation_level(ae_ptr);
229     decide_chance_fail(player_ptr, ae_ptr);
230     if (cmd_limit_time_walk(player_ptr))
231         return;
232
233     decide_activation_success(player_ptr, ae_ptr);
234     if (!check_activation_conditions(player_ptr, ae_ptr))
235         return;
236
237     msg_print(_("始動させた...", "You activate it..."));
238     sound(SOUND_ZAP);
239     if (activation_index(ae_ptr->o_ptr) > RandomArtActType::NONE) {
240         (void)activate_artifact(player_ptr, ae_ptr->o_ptr);
241         player_ptr->window_flags |= PW_INVEN | PW_EQUIP;
242         return;
243     }
244
245     if (activate_whistle(player_ptr, ae_ptr))
246         return;
247
248     if (exe_monster_capture(player_ptr, ae_ptr, cap_mon_ptr))
249         return;
250
251     msg_print(_("おっと、このアイテムは始動できない。", "Oops.  That object cannot be activated."));
252 }