OSDN Git Service

[Refactor] #2604 describe_flavor() の引数char* を廃止し、戻り値をstd::string に変えた
[hengbandforosx/hengbandosx.git] / src / market / building-craft-fix.cpp
1 #include "market/building-craft-fix.h"
2 #include "artifact/artifact-info.h"
3 #include "artifact/fixed-art-types.h"
4 #include "artifact/random-art-effects.h"
5 #include "core/asking-player.h"
6 #include "core/player-update-types.h"
7 #include "core/stuff-handler.h"
8 #include "flavor/flavor-describer.h"
9 #include "flavor/object-flavor-types.h"
10 #include "floor/floor-object.h"
11 #include "inventory/inventory-object.h"
12 #include "market/building-util.h"
13 #include "object-enchant/object-boost.h"
14 #include "object-enchant/special-object-flags.h"
15 #include "object-enchant/tr-types.h"
16 #include "object-hook/hook-weapon.h"
17 #include "object/item-tester-hooker.h"
18 #include "object/item-use-flags.h"
19 #include "object/object-flags.h"
20 #include "object/object-kind-hook.h"
21 #include "object/object-value.h"
22 #include "racial/racial-android.h"
23 #include "spell-realm/spells-hex.h"
24 #include "sv-definition/sv-other-types.h"
25 #include "sv-definition/sv-weapon-types.h"
26 #include "system/baseitem-info.h"
27 #include "system/item-entity.h"
28 #include "system/player-type-definition.h"
29 #include "term/screen-processor.h"
30 #include "util/bit-flags-calculator.h"
31 #include "view/display-messages.h"
32 #include <utility>
33
34 /*!
35  * @brief 修復材料のオブジェクトから修復対象に特性を移植する。
36  * @param to_ptr 修復対象オブジェクトの構造体の参照ポインタ。
37  * @param from_ptr 修復材料オブジェクトの構造体の参照ポインタ。
38  * @return 修復対象になるならTRUEを返す。
39  */
40 static void give_one_ability_of_object(ItemEntity *to_ptr, ItemEntity *from_ptr)
41 {
42     auto to_flags = object_flags(to_ptr);
43     auto from_flags = object_flags(from_ptr);
44
45     int n = 0;
46     tr_type cand[TR_FLAG_MAX];
47     for (int i = 0; i < TR_FLAG_MAX; i++) {
48         switch (i) {
49         case TR_IGNORE_ACID:
50         case TR_IGNORE_ELEC:
51         case TR_IGNORE_FIRE:
52         case TR_IGNORE_COLD:
53         case TR_ACTIVATE:
54         case TR_RIDING:
55         case TR_THROW:
56         case TR_SHOW_MODS:
57         case TR_HIDE_TYPE:
58         case TR_XXX_93:
59         case TR_XXX_94:
60         case TR_FULL_NAME:
61         case TR_FIXED_FLAVOR:
62             break;
63         default:
64             auto tr_flag = i2enum<tr_type>(i);
65             if (from_flags.has(tr_flag) && to_flags.has_not(tr_flag)) {
66                 if (!(TR_PVAL_FLAG_MASK.has(tr_flag) && (from_ptr->pval < 1))) {
67                     cand[n++] = tr_flag;
68                 }
69             }
70         }
71     }
72
73     if (n <= 0) {
74         return;
75     }
76
77     auto tr_idx = cand[randint0(n)];
78     to_ptr->art_flags.set(tr_idx);
79     if (TR_PVAL_FLAG_MASK.has(tr_idx)) {
80         to_ptr->pval = std::max<short>(to_ptr->pval, 1);
81     }
82     auto bmax = std::min<short>(3, std::max<short>(1, 40 / (to_ptr->dd * to_ptr->ds)));
83     if (tr_idx == TR_BLOWS) {
84         to_ptr->pval = std::min<short>(to_ptr->pval, bmax);
85     }
86     if (tr_idx == TR_SPEED) {
87         to_ptr->pval = std::min<short>(to_ptr->pval, 4);
88     }
89 }
90
91 static std::pair<bool, ItemEntity *> select_repairing_broken_weapon(PlayerType *player_ptr, const int row, short *item)
92 {
93     prt(_("修復には材料となるもう1つの武器が必要です。", "Hand one material weapon to repair a broken weapon."), row, 2);
94     prt(_("材料に使用した武器はなくなります!", "The material weapon will disappear after repairing!!"), row + 1, 2);
95     const auto q = _("どの折れた武器を修復しますか?", "Repair which broken weapon? ");
96     const auto s = _("修復できる折れた武器がありません。", "You have no broken weapon to repair.");
97     auto *o_ptr = choose_object(player_ptr, item, q, s, (USE_INVEN | USE_EQUIP), FuncItemTester(&ItemEntity::is_broken_weapon));
98     if (o_ptr == nullptr) {
99         return { false, nullptr };
100     }
101
102     if (!o_ptr->is_ego() && !o_ptr->is_fixed_or_random_artifact()) {
103         msg_format(_("それは直してもしょうがないぜ。", "It is worthless to repair."));
104         return { false, o_ptr };
105     }
106
107     if (o_ptr->number > 1) {
108         msg_format(_("一度に複数を修復することはできません!", "They are too many to repair at once!"));
109         return { false, o_ptr };
110     }
111
112     return { true, o_ptr };
113 }
114
115 static void display_reparing_weapon(PlayerType *player_ptr, ItemEntity *o_ptr, const int row)
116 {
117     const auto item_name = describe_flavor(player_ptr, o_ptr, OD_NAME_ONLY);
118     prt(format(_("修復する武器 : %s", "Repairing: %s"), item_name.data()), row + 3, 2);
119 }
120
121 static void display_repair_success_message(PlayerType *player_ptr, ItemEntity *o_ptr, const int cost)
122 {
123     const auto item_name = describe_flavor(player_ptr, o_ptr, OD_NAME_ONLY);
124 #ifdef JP
125     msg_format("$%dで%sに修復しました。", cost, item_name.data());
126 #else
127     msg_format("Repaired into %s for %d gold.", item_name.data(), cost);
128 #endif
129     msg_print(nullptr);
130 }
131
132 /*!
133  * @brief アイテム修復処理のメインルーチン / Repair broken weapon
134  * @param player_ptr プレイヤーへの参照ポインタ
135  * @param bcost 基本修復費用
136  * @return 実際にかかった費用
137  */
138 static PRICE repair_broken_weapon_aux(PlayerType *player_ptr, PRICE bcost)
139 {
140     clear_bldg(0, 22);
141     auto row = 7;
142     short item;
143     const auto &[selection, o_ptr] = select_repairing_broken_weapon(player_ptr, row, &item);
144     if (!selection) {
145         return 0;
146     }
147
148     display_reparing_weapon(player_ptr, o_ptr, row);
149     const auto q = _("材料となる武器は?", "Which weapon for material? ");
150     const auto s = _("材料となる武器がありません。", "You have no material for the repair.");
151     short mater;
152     auto *mo_ptr = choose_object(player_ptr, &mater, q, s, (USE_INVEN | USE_EQUIP), FuncItemTester(&ItemEntity::is_orthodox_melee_weapons));
153     if (!mo_ptr) {
154         return 0;
155     }
156
157     if (mater == item) {
158         msg_print(_("クラインの壷じゃない!", "This is not a Klein bottle!"));
159         return 0;
160     }
161
162     const auto item_name = describe_flavor(player_ptr, mo_ptr, OD_NAME_ONLY);
163     prt(format(_("材料とする武器: %s", "Material : %s"), item_name.data()), row + 4, 2);
164     const auto cost = bcost + object_value_real(o_ptr) * 2;
165     if (!get_check(format(_("$%dかかりますがよろしいですか? ", "Costs %d gold, okay? "), cost))) {
166         return 0;
167     }
168
169     if (player_ptr->au < cost) {
170         msg_format(_("%sを修復するだけのゴールドがありません!", "You do not have the gold to repair %s!"), item_name.data());
171         msg_print(nullptr);
172         return 0;
173     }
174
175     short bi_id;
176     if (o_ptr->bi_key.sval() == SV_BROKEN_DAGGER) {
177         auto n = 1;
178         bi_id = 0;
179         for (const auto &baseitem : baseitems_info) {
180             if (baseitem.bi_key.tval() != ItemKindType::SWORD) {
181                 continue;
182             }
183
184             const auto sval = baseitem.bi_key.sval();
185             if ((sval == SV_BROKEN_DAGGER) || (sval == SV_BROKEN_SWORD) || (sval == SV_POISON_NEEDLE)) {
186                 continue;
187             }
188
189             if (baseitem.weight > 99) {
190                 continue;
191             }
192
193             if (one_in_(n)) {
194                 bi_id = baseitem.idx;
195                 n++;
196             }
197         }
198     } else {
199         auto tval = (one_in_(5) ? mo_ptr->bi_key.tval() : ItemKindType::SWORD);
200         while (true) {
201             bi_id = lookup_baseitem_id({ tval });
202             const auto &baseitem = baseitems_info[bi_id];
203             const auto sval = baseitem.bi_key.sval();
204             if (tval == ItemKindType::SWORD) {
205                 if ((sval == SV_BROKEN_DAGGER) || (sval == SV_BROKEN_SWORD) || (sval == SV_DIAMOND_EDGE) || (sval == SV_POISON_NEEDLE)) {
206                     continue;
207                 }
208             }
209
210             if (tval == ItemKindType::POLEARM) {
211                 if ((sval == SV_DEATH_SCYTHE) || (sval == SV_TSURIZAO)) {
212                     continue;
213                 }
214             }
215
216             if (tval == ItemKindType::HAFTED) {
217                 if ((sval == SV_GROND) || (sval == SV_WIZSTAFF) || (sval == SV_NAMAKE_HAMMER)) {
218                     continue;
219                 }
220             }
221
222             break;
223         }
224     }
225
226     auto dd_bonus = o_ptr->dd - baseitems_info[o_ptr->bi_id].dd;
227     auto ds_bonus = o_ptr->ds - baseitems_info[o_ptr->bi_id].ds;
228     dd_bonus += mo_ptr->dd - baseitems_info[mo_ptr->bi_id].dd;
229     ds_bonus += mo_ptr->ds - baseitems_info[mo_ptr->bi_id].ds;
230
231     const auto &baseitem = baseitems_info[bi_id];
232     o_ptr->bi_id = bi_id;
233     o_ptr->weight = baseitem.weight;
234     o_ptr->bi_key = baseitem.bi_key;
235     o_ptr->dd = baseitem.dd;
236     o_ptr->ds = baseitem.ds;
237     o_ptr->art_flags.set(baseitem.flags);
238     if (baseitem.pval) {
239         o_ptr->pval = std::max<short>(o_ptr->pval, randint1(baseitem.pval));
240     }
241
242     if (baseitem.flags.has(TR_ACTIVATE)) {
243         o_ptr->activation_id = baseitem.act_idx;
244     }
245
246     if (dd_bonus > 0) {
247         o_ptr->dd++;
248         for (int i = 1; i < dd_bonus; i++) {
249             if (one_in_(o_ptr->dd + i)) {
250                 o_ptr->dd++;
251             }
252         }
253     }
254
255     if (ds_bonus > 0) {
256         o_ptr->ds++;
257         for (int i = 1; i < ds_bonus; i++) {
258             if (one_in_(o_ptr->ds + i)) {
259                 o_ptr->ds++;
260             }
261         }
262     }
263
264     if (baseitem.flags.has(TR_BLOWS)) {
265         auto bmax = std::min<short>(3, std::max<short>(1, 40 / (o_ptr->dd * o_ptr->ds)));
266         o_ptr->pval = std::min<short>(o_ptr->pval, bmax);
267     }
268
269     give_one_ability_of_object(o_ptr, mo_ptr);
270     o_ptr->to_d += std::max(0, (mo_ptr->to_d / 3));
271     o_ptr->to_h += std::max<short>(0, (mo_ptr->to_h / 3));
272     o_ptr->to_a += std::max<short>(0, (mo_ptr->to_a));
273
274     const auto is_narsil = o_ptr->is_specific_artifact(FixedArtifactId::NARSIL);
275     if (is_narsil || (o_ptr->is_random_artifact() && one_in_(1)) || (o_ptr->is_ego() && one_in_(7))) {
276         if (o_ptr->is_ego()) {
277             o_ptr->art_flags.set(TR_IGNORE_FIRE);
278             o_ptr->art_flags.set(TR_IGNORE_ACID);
279         }
280
281         give_one_ability_of_object(o_ptr, mo_ptr);
282         if (activation_index(o_ptr) == RandomArtActType::NONE) {
283             one_activation(o_ptr);
284         }
285
286         if (is_narsil) {
287             one_high_resistance(o_ptr);
288             one_ability(o_ptr);
289         }
290
291         msg_print(_("これはかなりの業物だったようだ。", "This blade seems to be exceptional."));
292     }
293
294     display_repair_success_message(player_ptr, o_ptr, cost);
295     o_ptr->ident &= ~(IDENT_BROKEN);
296     o_ptr->discount = 99;
297
298     calc_android_exp(player_ptr);
299     inven_item_increase(player_ptr, mater, -1);
300     inven_item_optimize(player_ptr, mater);
301
302     player_ptr->update |= PU_BONUS;
303     handle_stuff(player_ptr);
304     return cost;
305 }
306
307 /*!
308  * @brief アイテム修復処理の過渡ルーチン / Repair broken weapon
309  * @param player_ptr プレイヤーへの参照ポインタ
310  * @param bcost 基本鑑定費用
311  * @return 実際にかかった費用
312  */
313 int repair_broken_weapon(PlayerType *player_ptr, PRICE bcost)
314 {
315     PRICE cost;
316     screen_save();
317     cost = repair_broken_weapon_aux(player_ptr, bcost);
318     screen_load();
319     return cost;
320 }