OSDN Git Service

[Refactor] #40560 Separated decide_magic_book_exp() 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 \88ê\95\94\90E\8bÆ\82Å\8d\82\88Ê\96\82\96@\8f\91\82Ì\94j\89ó\82É\82æ\82é\8co\8c±\92l\8fã\8f¸\82Ì\94»\92è
105  * @param creature_ptr \83v\83\8c\81[\83\84\81[\82Ö\82Ì\8eQ\8fÆ\83|\83C\83\93\83^
106  * @param destory_ptr \83A\83C\83e\83\80\94j\89ó\8d\\91¢\91Ì\82Ö\82Ì\8eQ\8fÆ\83|\83C\83\93\83^
107  * return \96\82\96@\8f\91\82Ì\94j\89ó\82É\82æ\82Á\82Ä\8co\8c±\92l\82ª\93ü\82é\82È\82ç\82ÎTRUE
108  */
109 static bool decide_magic_book_exp(player_type *creature_ptr, destroy_type *destroy_ptr)
110 {
111     if (creature_ptr->prace == RACE_ANDROID)
112         return FALSE;
113     
114     if ((creature_ptr->pclass == CLASS_WARRIOR) || (creature_ptr->pclass == CLASS_BERSERKER))
115         return TRUE;
116     
117     if (creature_ptr->pclass != CLASS_PALADIN)
118         return FALSE;
119
120     bool gain_expr = FALSE;
121     if (is_good_realm(creature_ptr->realm1)) {
122         if (!is_good_realm(tval2realm(destroy_ptr->q_ptr->tval)))
123             gain_expr = TRUE;
124     } else {
125         if (is_good_realm(tval2realm(destroy_ptr->q_ptr->tval)))
126             gain_expr = TRUE;
127     }
128
129     return gain_expr;
130 }
131
132 /*!
133  * @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
134  * @param creature_ptr \83v\83\8c\81[\83\84\81[\82Ö\82Ì\8eQ\8fÆ\83|\83C\83\93\83^
135  * @return \82È\82µ
136  */
137 void do_cmd_destroy(player_type *creature_ptr)
138 {
139     if (creature_ptr->special_defense & KATA_MUSOU)
140         set_action(creature_ptr, ACTION_NONE);
141
142     object_type forge;
143     destroy_type tmp_destroy;
144     destroy_type *destroy_ptr = initialize_destroy_type(&tmp_destroy, &forge);
145     if (command_arg > 0)
146         destroy_ptr->force = TRUE;
147
148     if (!select_destroying_item(creature_ptr, destroy_ptr))
149         return;
150
151     destroy_ptr->old_number = destroy_ptr->o_ptr->number;
152     destroy_ptr->o_ptr->number = destroy_ptr->amt;
153     describe_flavor(creature_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, 0);
154     destroy_ptr->o_ptr->number = destroy_ptr->old_number;
155     take_turn(creature_ptr, 100);
156     if (!can_player_destroy_object(creature_ptr, destroy_ptr->o_ptr)) {
157         free_turn(creature_ptr);
158         msg_format(_("%s\82Í\94j\89ó\95s\89Â\94\\82¾\81B", "You cannot destroy %s."), destroy_ptr->o_name);
159         return;
160     }
161
162     object_copy(destroy_ptr->q_ptr, destroy_ptr->o_ptr);
163     msg_format(_("%s\82ð\89ó\82µ\82½\81B", "You destroy %s."), destroy_ptr->o_name);
164     sound(SOUND_DESTITEM);
165     reduce_charges(destroy_ptr->o_ptr, destroy_ptr->amt);
166     vary_item(creature_ptr, destroy_ptr->item, -destroy_ptr->amt);
167     if (item_tester_high_level_book(destroy_ptr->q_ptr)) {
168         bool gain_expr = decide_magic_book_exp(creature_ptr, destroy_ptr);
169         if (gain_expr && (creature_ptr->exp < PY_MAX_EXP)) {
170             s32b tester_exp = creature_ptr->max_exp / 20;
171             if (tester_exp > 10000)
172                 tester_exp = 10000;
173
174             if (destroy_ptr->q_ptr->sval < 3)
175                 tester_exp /= 4;
176
177             if (tester_exp < 1)
178                 tester_exp = 1;
179
180             msg_print(_("\8dX\82É\8co\8c±\82ð\90Ï\82ñ\82¾\82æ\82¤\82È\8bC\82ª\82·\82é\81B", "You feel more experienced."));
181             gain_exp(creature_ptr, tester_exp * destroy_ptr->amt);
182         }
183
184         if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_LIFE_BOOK) {
185             chg_virtue(creature_ptr, V_UNLIFE, 1);
186             chg_virtue(creature_ptr, V_VITALITY, -1);
187         } else if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_DEATH_BOOK) {
188             chg_virtue(creature_ptr, V_UNLIFE, -1);
189             chg_virtue(creature_ptr, V_VITALITY, 1);
190         }
191
192         if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_h != 0) || (destroy_ptr->q_ptr->to_d != 0))
193             chg_virtue(creature_ptr, V_ENCHANT, -1);
194
195         if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 30000)
196             chg_virtue(creature_ptr, V_SACRIFICE, 2);
197         else if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 10000)
198             chg_virtue(creature_ptr, V_SACRIFICE, 1);
199     }
200
201     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_d != 0) || (destroy_ptr->q_ptr->to_h != 0))
202         chg_virtue(creature_ptr, V_HARMONY, 1);
203
204     if (destroy_ptr->item >= INVEN_RARM)
205         calc_android_exp(creature_ptr);
206 }