OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / cmd-item / cmd-destroy.cpp
1 #include "cmd-item/cmd-destroy.h"
2 #include "autopick/autopick-registry.h"
3 #include "autopick/autopick.h"
4 #include "avatar/avatar.h"
5 #include "core/asking-player.h"
6 #include "core/stuff-handler.h"
7 #include "core/window-redrawer.h"
8 #include "flavor/flavor-describer.h"
9 #include "flavor/object-flavor-types.h"
10 #include "floor/floor-object.h"
11 #include "game-option/input-options.h"
12 #include "inventory/inventory-object.h"
13 #include "inventory/inventory-slot-types.h"
14 #include "io/input-key-acceptor.h"
15 #include "io/input-key-requester.h"
16 #include "main/sound-definitions-table.h"
17 #include "main/sound-of-music.h"
18 #include "object-hook/hook-expendable.h"
19 #include "object-hook/hook-magic.h"
20 #include "object/item-use-flags.h"
21 #include "object/object-stack.h"
22 #include "object/object-value.h"
23 #include "player-base/player-class.h"
24 #include "player-base/player-race.h"
25 #include "player-info/samurai-data-type.h"
26 #include "player-status/player-energy.h"
27 #include "player/attack-defense-types.h"
28 #include "player/special-defense-types.h"
29 #include "racial/racial-android.h"
30 #include "realm/realm-names-table.h"
31 #include "status/action-setter.h"
32 #include "status/experience.h"
33 #include "system/baseitem-info.h"
34 #include "system/item-entity.h"
35 #include "system/player-type-definition.h"
36 #include "system/redrawing-flags-updater.h"
37 #include "term/screen-processor.h"
38 #include "term/z-form.h"
39 #include "util/int-char-converter.h"
40 #include "view/display-messages.h"
41
42 struct destroy_type {
43     OBJECT_IDX item = 0;
44     QUANTITY amt = 0;
45     QUANTITY old_number = 0;
46     bool force = false;
47     ItemEntity *o_ptr = nullptr;
48     ItemEntity *q_ptr = nullptr;
49     std::string item_name = "";
50     char out_val[MAX_NLEN + 40]{};
51 };
52
53 static destroy_type *initialize_destroy_type(destroy_type *destroy_ptr, ItemEntity *o_ptr)
54 {
55     destroy_ptr->amt = 1;
56     destroy_ptr->force = false;
57     destroy_ptr->q_ptr = o_ptr;
58     return destroy_ptr;
59 }
60
61 static bool check_destory_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
62 {
63     if (destroy_ptr->force || (!confirm_destroy && (destroy_ptr->o_ptr->get_price() <= 0))) {
64         return true;
65     }
66
67     destroy_ptr->item_name = describe_flavor(player_ptr, destroy_ptr->o_ptr, OD_OMIT_PREFIX);
68     const auto mes = _("本当に%sを壊しますか? [y/n/Auto]", "Really destroy %s? [y/n/Auto]");
69     strnfmt(destroy_ptr->out_val, sizeof(destroy_ptr->out_val), mes, destroy_ptr->item_name.data());
70     msg_print(nullptr);
71     message_add(destroy_ptr->out_val);
72     RedrawingFlagsUpdater::get_instance().set_flag(SubWindowRedrawingFlag::MESSAGE);
73     handle_stuff(player_ptr);
74     while (true) {
75         prt(destroy_ptr->out_val, 0, 0);
76         char i = inkey();
77         prt("", 0, 0);
78         if (i == 'y' || i == 'Y') {
79             return true;
80         }
81
82         if (i == ESCAPE || i == 'n' || i == 'N') {
83             return false;
84         }
85
86         if (i != 'A') {
87             continue;
88         }
89
90         if (autopick_autoregister(player_ptr, destroy_ptr->o_ptr)) {
91             autopick_alter_item(player_ptr, destroy_ptr->item, true);
92         }
93
94         return false;
95     }
96 }
97
98 static bool select_destroying_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
99 {
100     concptr q = _("どのアイテムを壊しますか? ", "Destroy which item? ");
101     concptr s = _("壊せるアイテムを持っていない。", "You have nothing to destroy.");
102     destroy_ptr->o_ptr = choose_object(player_ptr, &destroy_ptr->item, q, s, USE_INVEN | USE_FLOOR);
103     if (destroy_ptr->o_ptr == nullptr) {
104         return false;
105     }
106
107     if (!check_destory_item(player_ptr, destroy_ptr)) {
108         return false;
109     }
110
111     if (destroy_ptr->o_ptr->number <= 1) {
112         return true;
113     }
114
115     destroy_ptr->amt = input_quantity(destroy_ptr->o_ptr->number);
116     return destroy_ptr->amt > 0;
117 }
118
119 /*!
120  * @brief 一部職業で高位魔法書の破壊による経験値上昇の判定
121  * @param player_ptr プレイヤーへの参照ポインタ
122  * @param destory_ptr アイテム破壊構造体への参照ポインタ
123  * return 魔法書の破壊によって経験値が入るならばTRUE
124  */
125 static bool decide_magic_book_exp(PlayerType *player_ptr, destroy_type *destroy_ptr)
126 {
127     if (PlayerRace(player_ptr).equals(PlayerRaceType::ANDROID)) {
128         return false;
129     }
130
131     PlayerClass pc(player_ptr);
132     const auto tval = destroy_ptr->o_ptr->bi_key.tval();
133     if (pc.equals(PlayerClassType::WARRIOR) || pc.equals(PlayerClassType::BERSERKER)) {
134         return tval != ItemKindType::HISSATSU_BOOK;
135     }
136
137     if (!pc.equals(PlayerClassType::PALADIN)) {
138         return false;
139     }
140
141     auto is_good_magic_realm = (tval == ItemKindType::LIFE_BOOK) || (tval == ItemKindType::CRUSADE_BOOK);
142     if (is_good_realm(player_ptr->realm1)) {
143         return !is_good_magic_realm;
144     } else {
145         return is_good_magic_realm;
146     }
147 }
148
149 static void gain_exp_by_destroying_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
150 {
151     const auto gain_expr = decide_magic_book_exp(player_ptr, destroy_ptr);
152     if (!gain_expr || (player_ptr->exp >= PY_MAX_EXP)) {
153         return;
154     }
155
156     auto tester_exp = player_ptr->max_exp / 20;
157     if (tester_exp > 10000) {
158         tester_exp = 10000;
159     }
160
161     if (destroy_ptr->q_ptr->bi_key.sval() < 3) {
162         tester_exp /= 4;
163     }
164
165     if (tester_exp < 1) {
166         tester_exp = 1;
167     }
168
169     msg_print(_("更に経験を積んだような気がする。", "You feel more experienced."));
170     gain_exp(player_ptr, tester_exp * destroy_ptr->amt);
171 }
172
173 static void process_destroy_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
174 {
175     const auto *q_ptr = destroy_ptr->q_ptr;
176     const BaseitemKey &bi_key = q_ptr->bi_key;
177     if (!bi_key.is_high_level_book()) {
178         return;
179     }
180
181     const auto tval = bi_key.tval();
182     gain_exp_by_destroying_magic_book(player_ptr, destroy_ptr);
183     if (tval == ItemKindType::LIFE_BOOK) {
184         chg_virtue(player_ptr, Virtue::UNLIFE, 1);
185         chg_virtue(player_ptr, Virtue::VITALITY, -1);
186     } else if (tval == ItemKindType::DEATH_BOOK) {
187         chg_virtue(player_ptr, Virtue::UNLIFE, -1);
188         chg_virtue(player_ptr, Virtue::VITALITY, 1);
189     }
190
191     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_h != 0) || (destroy_ptr->q_ptr->to_d != 0)) {
192         chg_virtue(player_ptr, Virtue::ENCHANT, -1);
193     }
194
195     if (object_value_real(destroy_ptr->q_ptr) > 30000) {
196         chg_virtue(player_ptr, Virtue::SACRIFICE, 2);
197     } else if (object_value_real(destroy_ptr->q_ptr) > 10000) {
198         chg_virtue(player_ptr, Virtue::SACRIFICE, 1);
199     }
200 }
201
202 static void exe_destroy_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
203 {
204     destroy_ptr->q_ptr->copy_from(destroy_ptr->o_ptr);
205     msg_format(_("%sを壊した。", "You destroy %s."), destroy_ptr->item_name.data());
206     sound(SOUND_DESTITEM);
207     reduce_charges(destroy_ptr->o_ptr, destroy_ptr->amt);
208     vary_item(player_ptr, destroy_ptr->item, -destroy_ptr->amt);
209     process_destroy_magic_book(player_ptr, destroy_ptr);
210     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_d != 0) || (destroy_ptr->q_ptr->to_h != 0)) {
211         chg_virtue(player_ptr, Virtue::HARMONY, 1);
212     }
213
214     if (destroy_ptr->item >= INVEN_MAIN_HAND) {
215         calc_android_exp(player_ptr);
216     }
217 }
218
219 /*!
220  * @brief アイテムを破壊するコマンドのメインルーチン / Destroy an item
221  * @param player_ptr プレイヤーへの参照ポインタ
222  */
223 void do_cmd_destroy(PlayerType *player_ptr)
224 {
225     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU });
226
227     ItemEntity forge;
228     destroy_type tmp_destroy;
229     destroy_type *destroy_ptr = initialize_destroy_type(&tmp_destroy, &forge);
230     if (command_arg > 0) {
231         destroy_ptr->force = true;
232     }
233
234     if (!select_destroying_item(player_ptr, destroy_ptr)) {
235         return;
236     }
237
238     destroy_ptr->old_number = destroy_ptr->o_ptr->number;
239     destroy_ptr->o_ptr->number = destroy_ptr->amt;
240     destroy_ptr->item_name = describe_flavor(player_ptr, destroy_ptr->o_ptr, 0);
241     destroy_ptr->o_ptr->number = destroy_ptr->old_number;
242     PlayerEnergy energy(player_ptr);
243     energy.set_player_turn_energy(100);
244     if (!can_player_destroy_object(destroy_ptr->o_ptr)) {
245         energy.reset_player_turn();
246         msg_format(_("%sは破壊不可能だ。", "You cannot destroy %s."), destroy_ptr->item_name.data());
247         return;
248     }
249
250     exe_destroy_item(player_ptr, destroy_ptr);
251 }