OSDN Git Service

[Refactor] #40560 Separated select_destroying_item() from do_cmd_destory()
[hengbandforosx/hengbandosx.git] / src / cmd-item / cmd-destroy.c
1 #include "cmd-item/cmd-destroy.h"
2 #include "autopick/autopick-registry.h"
3 #include "autopick/autopick.h"
4 #include "core/asking-player.h"
5 #include "core/stuff-handler.h"
6 #include "core/window-redrawer.h"
7 #include "flavor/flavor-describer.h"
8 #include "flavor/object-flavor-types.h"
9 #include "floor/floor-object.h"
10 #include "game-option/input-options.h"
11 #include "inventory/inventory-object.h"
12 #include "inventory/inventory-slot-types.h"
13 #include "io/input-key-acceptor.h"
14 #include "io/input-key-requester.h"
15 #include "main/sound-definitions-table.h"
16 #include "main/sound-of-music.h"
17 #include "object-hook/hook-expendable.h"
18 #include "object-hook/hook-magic.h"
19 #include "object/item-use-flags.h"
20 #include "object/object-generator.h"
21 #include "object/object-stack.h"
22 #include "object/object-value.h"
23 #include "player/attack-defense-types.h"
24 #include "player/avatar.h"
25 #include "player/special-defense-types.h"
26 #include "racial/racial-android.h"
27 #include "realm/realm-names-table.h"
28 #include "status/action-setter.h"
29 #include "status/experience.h"
30 #include "system/object-type-definition.h"
31 #include "term/screen-processor.h"
32 #include "util/int-char-converter.h"
33 #include "view/display-messages.h"
34
35 typedef struct destroy_type {
36     OBJECT_IDX item;
37     QUANTITY amt;
38     QUANTITY old_number;
39     bool force;
40     object_type *o_ptr;
41     object_type *q_ptr;
42     GAME_TEXT o_name[MAX_NLEN];
43     char out_val[MAX_NLEN + 40];
44 } destroy_type;
45
46 static destroy_type *initialize_destroy_type(destroy_type *destroy_ptr, object_type *o_ptr)
47 {
48     destroy_ptr->amt = 1;
49     destroy_ptr->force = FALSE;
50     destroy_ptr->q_ptr = o_ptr;
51     return destroy_ptr;
52 }
53
54 static bool check_destory_item(player_type *creature_ptr, destroy_type *destroy_ptr)
55 {
56     if (destroy_ptr->force || (!confirm_destroy && (object_value(creature_ptr, destroy_ptr->o_ptr) <= 0)))
57         return TRUE;
58
59     describe_flavor(creature_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, OD_OMIT_PREFIX);
60     sprintf(destroy_ptr->out_val, _("\96{\93\96\82É%s\82ð\89ó\82µ\82Ü\82·\82©? [y/n/Auto]", "Really destroy %s? [y/n/Auto]"), destroy_ptr->o_name);
61     msg_print(NULL);
62     message_add(destroy_ptr->out_val);
63     creature_ptr->window |= PW_MESSAGE;
64     handle_stuff(creature_ptr);
65     while (TRUE) {
66         prt(destroy_ptr->out_val, 0, 0);
67         char i = inkey();
68         prt("", 0, 0);
69         if (i == 'y' || i == 'Y')
70             return TRUE;
71
72         if (i == ESCAPE || i == 'n' || i == 'N')
73             return FALSE;
74
75         if (i != 'A')
76             continue;
77
78         if (autopick_autoregister(creature_ptr, destroy_ptr->o_ptr))
79             autopick_alter_item(creature_ptr, destroy_ptr->item, TRUE);
80
81         return FALSE;
82     }
83 }
84
85 static bool select_destroying_item(player_type *creature_ptr, destroy_type *destroy_ptr)
86 {
87     concptr q = _("\82Ç\82Ì\83A\83C\83e\83\80\82ð\89ó\82µ\82Ü\82·\82©? ", "Destroy which item? ");
88     concptr s = _("\89ó\82¹\82é\83A\83C\83e\83\80\82ð\8e\9d\82Á\82Ä\82¢\82È\82¢\81B", "You have nothing to destroy.");
89     destroy_ptr->o_ptr = choose_object(creature_ptr, &destroy_ptr->item, q, s, USE_INVEN | USE_FLOOR, 0);
90     if (destroy_ptr->o_ptr == NULL)
91         return FALSE;
92
93     if (!check_destory_item(creature_ptr, destroy_ptr))
94         return FALSE;
95
96     if (destroy_ptr->o_ptr->number <= 1)
97         return TRUE;
98
99     destroy_ptr->amt = get_quantity(NULL, destroy_ptr->o_ptr->number);
100     return destroy_ptr->amt > 0;
101 }
102
103 /*!
104  * @brief \83A\83C\83e\83\80\82ð\94j\89ó\82·\82é\83R\83}\83\93\83h\82Ì\83\81\83C\83\93\83\8b\81[\83`\83\93 / Destroy an item
105  * @param creature_ptr \83v\83\8c\81[\83\84\81[\82Ö\82Ì\8eQ\8fÆ\83|\83C\83\93\83^
106  * @return \82È\82µ
107  */
108 void do_cmd_destroy(player_type *creature_ptr)
109 {
110     if (creature_ptr->special_defense & KATA_MUSOU)
111         set_action(creature_ptr, ACTION_NONE);
112
113     object_type forge;
114     destroy_type tmp_destroy;
115     destroy_type *destroy_ptr = initialize_destroy_type(&tmp_destroy, &forge);
116     if (command_arg > 0)
117         destroy_ptr->force = TRUE;
118
119     if (!select_destroying_item(creature_ptr, destroy_ptr))
120         return;
121
122     destroy_ptr->old_number = destroy_ptr->o_ptr->number;
123     destroy_ptr->o_ptr->number = destroy_ptr->amt;
124     describe_flavor(creature_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, 0);
125     destroy_ptr->o_ptr->number = destroy_ptr->old_number;
126     take_turn(creature_ptr, 100);
127     if (!can_player_destroy_object(creature_ptr, destroy_ptr->o_ptr)) {
128         free_turn(creature_ptr);
129         msg_format(_("%s\82Í\94j\89ó\95s\89Â\94\\82¾\81B", "You cannot destroy %s."), destroy_ptr->o_name);
130         return;
131     }
132
133     object_copy(destroy_ptr->q_ptr, destroy_ptr->o_ptr);
134     msg_format(_("%s\82ð\89ó\82µ\82½\81B", "You destroy %s."), destroy_ptr->o_name);
135     sound(SOUND_DESTITEM);
136     reduce_charges(destroy_ptr->o_ptr, destroy_ptr->amt);
137     vary_item(creature_ptr, destroy_ptr->item, -destroy_ptr->amt);
138     if (item_tester_high_level_book(destroy_ptr->q_ptr)) {
139         bool gain_expr = FALSE;
140         if (creature_ptr->prace == RACE_ANDROID) {
141         } else if ((creature_ptr->pclass == CLASS_WARRIOR) || (creature_ptr->pclass == CLASS_BERSERKER)) {
142             gain_expr = TRUE;
143         } else if (creature_ptr->pclass == CLASS_PALADIN) {
144             if (is_good_realm(creature_ptr->realm1)) {
145                 if (!is_good_realm(tval2realm(destroy_ptr->q_ptr->tval)))
146                     gain_expr = TRUE;
147             } else {
148                 if (is_good_realm(tval2realm(destroy_ptr->q_ptr->tval)))
149                     gain_expr = TRUE;
150             }
151         }
152
153         if (gain_expr && (creature_ptr->exp < PY_MAX_EXP)) {
154             s32b tester_exp = creature_ptr->max_exp / 20;
155             if (tester_exp > 10000)
156                 tester_exp = 10000;
157
158             if (destroy_ptr->q_ptr->sval < 3)
159                 tester_exp /= 4;
160
161             if (tester_exp < 1)
162                 tester_exp = 1;
163
164             msg_print(_("\8dX\82É\8co\8c±\82ð\90Ï\82ñ\82¾\82æ\82¤\82È\8bC\82ª\82·\82é\81B", "You feel more experienced."));
165             gain_exp(creature_ptr, tester_exp * destroy_ptr->amt);
166         }
167
168         if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_LIFE_BOOK) {
169             chg_virtue(creature_ptr, V_UNLIFE, 1);
170             chg_virtue(creature_ptr, V_VITALITY, -1);
171         } else if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_DEATH_BOOK) {
172             chg_virtue(creature_ptr, V_UNLIFE, -1);
173             chg_virtue(creature_ptr, V_VITALITY, 1);
174         }
175
176         if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_h != 0) || (destroy_ptr->q_ptr->to_d != 0))
177             chg_virtue(creature_ptr, V_ENCHANT, -1);
178
179         if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 30000)
180             chg_virtue(creature_ptr, V_SACRIFICE, 2);
181         else if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 10000)
182             chg_virtue(creature_ptr, V_SACRIFICE, 1);
183     }
184
185     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_d != 0) || (destroy_ptr->q_ptr->to_h != 0))
186         chg_virtue(creature_ptr, V_HARMONY, 1);
187
188     if (destroy_ptr->item >= INVEN_RARM)
189         calc_android_exp(creature_ptr);
190 }