OSDN Git Service

Merge pull request #36 from taotao54321/fix-wrong-evasion-message
[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-info/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, _("本当に%sを壊しますか? [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_flags |= 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 = _("どのアイテムを壊しますか? ", "Destroy which item? ");
88     concptr s = _("壊せるアイテムを持っていない。", "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 一部職業で高位魔法書の破壊による経験値上昇の判定
105  * @param creature_ptr プレーヤーへの参照ポインタ
106  * @param destory_ptr アイテム破壊構造体への参照ポインタ
107  * return 魔法書の破壊によって経験値が入るならば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 static void gain_exp_by_destroying_magic_book(player_type *creature_ptr, destroy_type *destroy_ptr)
133 {
134     bool gain_expr = decide_magic_book_exp(creature_ptr, destroy_ptr);
135     if (!gain_expr || (creature_ptr->exp >= PY_MAX_EXP))
136         return;
137
138     s32b tester_exp = creature_ptr->max_exp / 20;
139     if (tester_exp > 10000)
140         tester_exp = 10000;
141
142     if (destroy_ptr->q_ptr->sval < 3)
143         tester_exp /= 4;
144
145     if (tester_exp < 1)
146         tester_exp = 1;
147
148     msg_print(_("更に経験を積んだような気がする。", "You feel more experienced."));
149     gain_exp(creature_ptr, tester_exp * destroy_ptr->amt);
150 }
151
152 static void process_destroy_magic_book(player_type *creature_ptr, destroy_type *destroy_ptr)
153 {
154     if (!item_tester_high_level_book(destroy_ptr->q_ptr))
155         return;
156
157     gain_exp_by_destroying_magic_book(creature_ptr, destroy_ptr);
158     if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_LIFE_BOOK) {
159         chg_virtue(creature_ptr, V_UNLIFE, 1);
160         chg_virtue(creature_ptr, V_VITALITY, -1);
161     } else if (item_tester_high_level_book(destroy_ptr->q_ptr) && destroy_ptr->q_ptr->tval == TV_DEATH_BOOK) {
162         chg_virtue(creature_ptr, V_UNLIFE, -1);
163         chg_virtue(creature_ptr, V_VITALITY, 1);
164     }
165
166     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_h != 0) || (destroy_ptr->q_ptr->to_d != 0))
167         chg_virtue(creature_ptr, V_ENCHANT, -1);
168
169     if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 30000)
170         chg_virtue(creature_ptr, V_SACRIFICE, 2);
171     else if (object_value_real(creature_ptr, destroy_ptr->q_ptr) > 10000)
172         chg_virtue(creature_ptr, V_SACRIFICE, 1);
173 }
174
175 static void exe_destroy_item(player_type *creature_ptr, destroy_type *destroy_ptr)
176 {
177     object_copy(destroy_ptr->q_ptr, destroy_ptr->o_ptr);
178     msg_format(_("%sを壊した。", "You destroy %s."), destroy_ptr->o_name);
179     sound(SOUND_DESTITEM);
180     reduce_charges(destroy_ptr->o_ptr, destroy_ptr->amt);
181     vary_item(creature_ptr, destroy_ptr->item, -destroy_ptr->amt);
182     process_destroy_magic_book(creature_ptr, destroy_ptr);
183     if ((destroy_ptr->q_ptr->to_a != 0) || (destroy_ptr->q_ptr->to_d != 0) || (destroy_ptr->q_ptr->to_h != 0))
184         chg_virtue(creature_ptr, V_HARMONY, 1);
185
186     if (destroy_ptr->item >= INVEN_MAIN_HAND)
187         calc_android_exp(creature_ptr);
188 }
189
190 /*!
191  * @brief アイテムを破壊するコマンドのメインルーチン / Destroy an item
192  * @param creature_ptr プレーヤーへの参照ポインタ
193  * @return なし
194  */
195 void do_cmd_destroy(player_type *creature_ptr)
196 {
197     if (creature_ptr->special_defense & KATA_MUSOU)
198         set_action(creature_ptr, ACTION_NONE);
199
200     object_type forge;
201     destroy_type tmp_destroy;
202     destroy_type *destroy_ptr = initialize_destroy_type(&tmp_destroy, &forge);
203     if (command_arg > 0)
204         destroy_ptr->force = TRUE;
205
206     if (!select_destroying_item(creature_ptr, destroy_ptr))
207         return;
208
209     destroy_ptr->old_number = destroy_ptr->o_ptr->number;
210     destroy_ptr->o_ptr->number = destroy_ptr->amt;
211     describe_flavor(creature_ptr, destroy_ptr->o_name, destroy_ptr->o_ptr, 0);
212     destroy_ptr->o_ptr->number = destroy_ptr->old_number;
213     take_turn(creature_ptr, 100);
214     if (!can_player_destroy_object(creature_ptr, destroy_ptr->o_ptr)) {
215         free_turn(creature_ptr);
216         msg_format(_("%sは破壊不可能だ。", "You cannot destroy %s."), destroy_ptr->o_name);
217         return;
218     }
219
220     exe_destroy_item(creature_ptr, destroy_ptr);
221 }