OSDN Git Service

Replace sprintf() simply. Does part of the work to resolve https://github.com/hengba...
[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 "term/screen-processor.h"
37 #include "term/z-form.h"
38 #include "util/int-char-converter.h"
39 #include "view/display-messages.h"
40
41 struct destroy_type {
42     OBJECT_IDX item;
43     QUANTITY amt;
44     QUANTITY old_number;
45     bool force;
46     ItemEntity *o_ptr;
47     ItemEntity *q_ptr;
48     GAME_TEXT o_name[MAX_NLEN];
49     char out_val[MAX_NLEN + 40];
50 };
51
52 static destroy_type *initialize_destroy_type(destroy_type *destroy_ptr, ItemEntity *o_ptr)
53 {
54     destroy_ptr->amt = 1;
55     destroy_ptr->force = false;
56     destroy_ptr->q_ptr = o_ptr;
57     return destroy_ptr;
58 }
59
60 static bool check_destory_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
61 {
62     if (destroy_ptr->force || (!confirm_destroy && (destroy_ptr->o_ptr->get_price() <= 0))) {
63         return true;
64     }
65
66     describe_flavor(player_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, OD_OMIT_PREFIX);
67     strnfmt(destroy_ptr->out_val, sizeof(destroy_ptr->out_val), _("本当に%sを壊しますか? [y/n/Auto]", "Really destroy %s? [y/n/Auto]"), destroy_ptr->o_name);
68     msg_print(nullptr);
69     message_add(destroy_ptr->out_val);
70     player_ptr->window_flags |= PW_MESSAGE;
71     handle_stuff(player_ptr);
72     while (true) {
73         prt(destroy_ptr->out_val, 0, 0);
74         char i = inkey();
75         prt("", 0, 0);
76         if (i == 'y' || i == 'Y') {
77             return true;
78         }
79
80         if (i == ESCAPE || i == 'n' || i == 'N') {
81             return false;
82         }
83
84         if (i != 'A') {
85             continue;
86         }
87
88         if (autopick_autoregister(player_ptr, destroy_ptr->o_ptr)) {
89             autopick_alter_item(player_ptr, destroy_ptr->item, true);
90         }
91
92         return false;
93     }
94 }
95
96 static bool select_destroying_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
97 {
98     concptr q = _("どのアイテムを壊しますか? ", "Destroy which item? ");
99     concptr s = _("壊せるアイテムを持っていない。", "You have nothing to destroy.");
100     destroy_ptr->o_ptr = choose_object(player_ptr, &destroy_ptr->item, q, s, USE_INVEN | USE_FLOOR);
101     if (destroy_ptr->o_ptr == nullptr) {
102         return false;
103     }
104
105     if (!check_destory_item(player_ptr, destroy_ptr)) {
106         return false;
107     }
108
109     if (destroy_ptr->o_ptr->number <= 1) {
110         return true;
111     }
112
113     destroy_ptr->amt = get_quantity(nullptr, destroy_ptr->o_ptr->number);
114     return destroy_ptr->amt > 0;
115 }
116
117 /*!
118  * @brief 一部職業で高位魔法書の破壊による経験値上昇の判定
119  * @param player_ptr プレイヤーへの参照ポインタ
120  * @param destory_ptr アイテム破壊構造体への参照ポインタ
121  * return 魔法書の破壊によって経験値が入るならばTRUE
122  */
123 static bool decide_magic_book_exp(PlayerType *player_ptr, destroy_type *destroy_ptr)
124 {
125     if (PlayerRace(player_ptr).equals(PlayerRaceType::ANDROID)) {
126         return false;
127     }
128
129     PlayerClass pc(player_ptr);
130     const auto tval = destroy_ptr->o_ptr->bi_key.tval();
131     if (pc.equals(PlayerClassType::WARRIOR) || pc.equals(PlayerClassType::BERSERKER)) {
132         return tval != ItemKindType::HISSATSU_BOOK;
133     }
134
135     if (!pc.equals(PlayerClassType::PALADIN)) {
136         return false;
137     }
138
139     auto is_good_magic_realm = (tval == ItemKindType::LIFE_BOOK) || (tval == ItemKindType::CRUSADE_BOOK);
140     if (is_good_realm(player_ptr->realm1)) {
141         return !is_good_magic_realm;
142     } else {
143         return is_good_magic_realm;
144     }
145 }
146
147 static void gain_exp_by_destroying_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
148 {
149     const auto gain_expr = decide_magic_book_exp(player_ptr, destroy_ptr);
150     if (!gain_expr || (player_ptr->exp >= PY_MAX_EXP)) {
151         return;
152     }
153
154     auto tester_exp = player_ptr->max_exp / 20;
155     if (tester_exp > 10000) {
156         tester_exp = 10000;
157     }
158
159     if (destroy_ptr->q_ptr->bi_key.sval() < 3) {
160         tester_exp /= 4;
161     }
162
163     if (tester_exp < 1) {
164         tester_exp = 1;
165     }
166
167     msg_print(_("更に経験を積んだような気がする。", "You feel more experienced."));
168     gain_exp(player_ptr, tester_exp * destroy_ptr->amt);
169 }
170
171 static void process_destroy_magic_book(PlayerType *player_ptr, destroy_type *destroy_ptr)
172 {
173     const auto *q_ptr = destroy_ptr->q_ptr;
174     const BaseitemKey &bi_key = q_ptr->bi_key;
175     if (!bi_key.is_high_level_book()) {
176         return;
177     }
178
179     const auto tval = bi_key.tval();
180     gain_exp_by_destroying_magic_book(player_ptr, destroy_ptr);
181     if (tval == ItemKindType::LIFE_BOOK) {
182         chg_virtue(player_ptr, V_UNLIFE, 1);
183         chg_virtue(player_ptr, V_VITALITY, -1);
184     } else if (tval == ItemKindType::DEATH_BOOK) {
185         chg_virtue(player_ptr, V_UNLIFE, -1);
186         chg_virtue(player_ptr, V_VITALITY, 1);
187     }
188
189     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_h != 0) || (destroy_ptr->q_ptr->to_d != 0)) {
190         chg_virtue(player_ptr, V_ENCHANT, -1);
191     }
192
193     if (object_value_real(destroy_ptr->q_ptr) > 30000) {
194         chg_virtue(player_ptr, V_SACRIFICE, 2);
195     } else if (object_value_real(destroy_ptr->q_ptr) > 10000) {
196         chg_virtue(player_ptr, V_SACRIFICE, 1);
197     }
198 }
199
200 static void exe_destroy_item(PlayerType *player_ptr, destroy_type *destroy_ptr)
201 {
202     destroy_ptr->q_ptr->copy_from(destroy_ptr->o_ptr);
203     msg_format(_("%sを壊した。", "You destroy %s."), destroy_ptr->o_name);
204     sound(SOUND_DESTITEM);
205     reduce_charges(destroy_ptr->o_ptr, destroy_ptr->amt);
206     vary_item(player_ptr, destroy_ptr->item, -destroy_ptr->amt);
207     process_destroy_magic_book(player_ptr, destroy_ptr);
208     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_d != 0) || (destroy_ptr->q_ptr->to_h != 0)) {
209         chg_virtue(player_ptr, V_HARMONY, 1);
210     }
211
212     if (destroy_ptr->item >= INVEN_MAIN_HAND) {
213         calc_android_exp(player_ptr);
214     }
215 }
216
217 /*!
218  * @brief アイテムを破壊するコマンドのメインルーチン / Destroy an item
219  * @param player_ptr プレイヤーへの参照ポインタ
220  */
221 void do_cmd_destroy(PlayerType *player_ptr)
222 {
223     PlayerClass(player_ptr).break_samurai_stance({ SamuraiStanceType::MUSOU });
224
225     ItemEntity forge;
226     destroy_type tmp_destroy;
227     destroy_type *destroy_ptr = initialize_destroy_type(&tmp_destroy, &forge);
228     if (command_arg > 0) {
229         destroy_ptr->force = true;
230     }
231
232     if (!select_destroying_item(player_ptr, destroy_ptr)) {
233         return;
234     }
235
236     destroy_ptr->old_number = destroy_ptr->o_ptr->number;
237     destroy_ptr->o_ptr->number = destroy_ptr->amt;
238     describe_flavor(player_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, 0);
239     destroy_ptr->o_ptr->number = destroy_ptr->old_number;
240     PlayerEnergy energy(player_ptr);
241     energy.set_player_turn_energy(100);
242     if (!can_player_destroy_object(player_ptr, destroy_ptr->o_ptr)) {
243         energy.reset_player_turn();
244         msg_format(_("%sは破壊不可能だ。", "You cannot destroy %s."), destroy_ptr->o_name);
245         return;
246     }
247
248     exe_destroy_item(player_ptr, destroy_ptr);
249 }