OSDN Git Service

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