OSDN Git Service

[Refactor] C言語時代に「巨大なヘッダー定義」という意味でdefinitionを付けていたが、今はクラス化してメソッドも定義され、当初の意味を失ったbaseit...
[hengbandforosx/hengbandosx.git] / src / market / bounty.cpp
1 #include "market/bounty.h"
2 #include "autopick/autopick.h"
3 #include "avatar/avatar.h"
4 #include "cmd-building/cmd-building.h"
5 #include "core/asking-player.h"
6 #include "core/player-redraw-types.h"
7 #include "core/stuff-handler.h"
8 #include "flavor/flavor-describer.h"
9 #include "game-option/cheat-options.h"
10 #include "inventory/inventory-object.h"
11 #include "inventory/inventory-slot-types.h"
12 #include "io/input-key-acceptor.h"
13 #include "market/bounty-prize-table.h"
14 #include "market/building-util.h"
15 #include "monster-race/monster-race-hook.h"
16 #include "monster-race/monster-race.h"
17 #include "monster-race/race-flags1.h"
18 #include "monster-race/race-flags2.h"
19 #include "monster-race/race-flags7.h"
20 #include "monster-race/race-indice-types.h"
21 #include "monster/monster-list.h"
22 #include "monster/monster-util.h"
23 #include "object-enchant/item-apply-magic.h"
24 #include "object-enchant/item-magic-applier.h"
25 #include "object/object-info.h"
26 #include "object/object-kind-hook.h"
27 #include "perception/object-perception.h"
28 #include "sv-definition/sv-other-types.h"
29 #include "system/baseitem-info.h"
30 #include "system/dungeon-info.h"
31 #include "system/floor-type-definition.h"
32 #include "system/item-entity.h"
33 #include "system/monster-race-info.h"
34 #include "system/player-type-definition.h"
35 #include "term/screen-processor.h"
36 #include "term/term-color-types.h"
37 #include "util/bit-flags-calculator.h"
38 #include "view/display-messages.h"
39 #include "world/world.h"
40 #include <algorithm>
41
42 /*!
43  * @brief 賞金首の引き換え処理 / Get prize
44  * @param player_ptr プレイヤーへの参照ポインタ
45  * @return 各種賞金首のいずれかでも換金が行われたか否か。
46  */
47 bool exchange_cash(PlayerType *player_ptr)
48 {
49     bool change = false;
50     GAME_TEXT o_name[MAX_NLEN];
51     ItemEntity *o_ptr;
52
53     for (INVENTORY_IDX i = 0; i <= INVEN_SUB_HAND; i++) {
54         o_ptr = &player_ptr->inventory_list[i];
55         const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
56
57         if ((o_ptr->tval == ItemKindType::CAPTURE) && (r_idx_of_item == MonsterRaceId::TSUCHINOKO)) {
58             char buf[MAX_NLEN + 32];
59             describe_flavor(player_ptr, o_name, o_ptr, 0);
60             sprintf(buf, _("%s を換金しますか?", "Convert %s into money? "), o_name);
61             if (get_check(buf)) {
62                 msg_format(_("賞金 %ld$を手に入れた。", "You get %ldgp."), (long int)(1000000L * o_ptr->number));
63                 player_ptr->au += 1000000L * o_ptr->number;
64                 player_ptr->redraw |= (PR_GOLD);
65                 vary_item(player_ptr, i, -o_ptr->number);
66             }
67
68             change = true;
69         }
70     }
71
72     for (INVENTORY_IDX i = 0; i < INVEN_PACK; i++) {
73         o_ptr = &player_ptr->inventory_list[i];
74         const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
75
76         if ((o_ptr->tval == ItemKindType::CORPSE) && (o_ptr->sval == SV_CORPSE) && (r_idx_of_item == MonsterRaceId::TSUCHINOKO)) {
77             char buf[MAX_NLEN + 32];
78             describe_flavor(player_ptr, o_name, o_ptr, 0);
79             sprintf(buf, _("%s を換金しますか?", "Convert %s into money? "), o_name);
80             if (get_check(buf)) {
81                 msg_format(_("賞金 %ld$を手に入れた。", "You get %ldgp."), (long int)(200000L * o_ptr->number));
82                 player_ptr->au += 200000L * o_ptr->number;
83                 player_ptr->redraw |= (PR_GOLD);
84                 vary_item(player_ptr, i, -o_ptr->number);
85             }
86
87             change = true;
88         }
89     }
90
91     for (INVENTORY_IDX i = 0; i < INVEN_PACK; i++) {
92         o_ptr = &player_ptr->inventory_list[i];
93         const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
94
95         if ((o_ptr->tval == ItemKindType::CORPSE) && (o_ptr->sval == SV_SKELETON) && (r_idx_of_item == MonsterRaceId::TSUCHINOKO)) {
96             char buf[MAX_NLEN + 32];
97             describe_flavor(player_ptr, o_name, o_ptr, 0);
98             sprintf(buf, _("%s を換金しますか?", "Convert %s into money? "), o_name);
99             if (get_check(buf)) {
100                 msg_format(_("賞金 %ld$を手に入れた。", "You get %ldgp."), (long int)(100000L * o_ptr->number));
101                 player_ptr->au += 100000L * o_ptr->number;
102                 player_ptr->redraw |= (PR_GOLD);
103                 vary_item(player_ptr, i, -o_ptr->number);
104             }
105
106             change = true;
107         }
108     }
109
110     for (INVENTORY_IDX i = 0; i < INVEN_PACK; i++) {
111         o_ptr = &player_ptr->inventory_list[i];
112         const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
113
114         if ((o_ptr->tval == ItemKindType::CORPSE) && (o_ptr->sval == SV_CORPSE) && (streq(monraces_info[r_idx_of_item].name.data(), monraces_info[w_ptr->today_mon].name.data()))) {
115             char buf[MAX_NLEN + 32];
116             describe_flavor(player_ptr, o_name, o_ptr, 0);
117             sprintf(buf, _("%s を換金しますか?", "Convert %s into money? "), o_name);
118             if (get_check(buf)) {
119                 msg_format(
120                     _("賞金 %ld$を手に入れた。", "You get %ldgp."), (long int)((monraces_info[w_ptr->today_mon].level * 50 + 100) * o_ptr->number));
121                 player_ptr->au += (monraces_info[w_ptr->today_mon].level * 50 + 100) * o_ptr->number;
122                 player_ptr->redraw |= (PR_GOLD);
123                 vary_item(player_ptr, i, -o_ptr->number);
124             }
125
126             change = true;
127         }
128     }
129
130     for (INVENTORY_IDX i = 0; i < INVEN_PACK; i++) {
131         o_ptr = &player_ptr->inventory_list[i];
132         const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
133
134         if ((o_ptr->tval == ItemKindType::CORPSE) && (o_ptr->sval == SV_SKELETON) && (streq(monraces_info[r_idx_of_item].name.data(), monraces_info[w_ptr->today_mon].name.data()))) {
135             char buf[MAX_NLEN + 32];
136             describe_flavor(player_ptr, o_name, o_ptr, 0);
137             sprintf(buf, _("%s を換金しますか?", "Convert %s into money? "), o_name);
138             if (get_check(buf)) {
139                 msg_format(_("賞金 %ld$を手に入れた。", "You get %ldgp."), (long int)((monraces_info[w_ptr->today_mon].level * 30 + 60) * o_ptr->number));
140                 player_ptr->au += (monraces_info[w_ptr->today_mon].level * 30 + 60) * o_ptr->number;
141                 player_ptr->redraw |= (PR_GOLD);
142                 vary_item(player_ptr, i, -o_ptr->number);
143             }
144
145             change = true;
146         }
147     }
148
149     for (auto &[r_idx, is_achieved] : w_ptr->bounties) {
150         if (is_achieved) {
151             continue;
152         }
153
154         for (INVENTORY_IDX i = INVEN_PACK - 1; i >= 0; i--) {
155             o_ptr = &player_ptr->inventory_list[i];
156             const auto r_idx_of_item = static_cast<MonsterRaceId>(o_ptr->pval);
157
158             if ((o_ptr->tval != ItemKindType::CORPSE) || (r_idx_of_item != r_idx)) {
159                 continue;
160             }
161
162             char buf[MAX_NLEN + 20];
163             INVENTORY_IDX item_new;
164             ItemEntity forge;
165
166             describe_flavor(player_ptr, o_name, o_ptr, 0);
167             sprintf(buf, _("%sを渡しますか?", "Hand %s over? "), o_name);
168             if (!get_check(buf)) {
169                 continue;
170             }
171
172             vary_item(player_ptr, i, -o_ptr->number);
173             chg_virtue(player_ptr, V_JUSTICE, 5);
174             is_achieved = true;
175
176             auto num = std::count_if(std::begin(w_ptr->bounties), std::end(w_ptr->bounties),
177                 [](const auto &b_ref) { return b_ref.is_achieved; });
178
179             msg_format(_("これで合計 %d ポイント獲得しました。", "You earned %d point%s total."), num, (num > 1 ? "s" : ""));
180
181             (&forge)->prep(lookup_baseitem_id(prize_list[num - 1]));
182             ItemMagicApplier(player_ptr, &forge, player_ptr->current_floor_ptr->object_level, AM_NO_FIXED_ART).execute();
183
184             object_aware(player_ptr, &forge);
185             object_known(&forge);
186
187             /*
188              * Hand it --- Assume there is an empty slot.
189              * Since a corpse is handed at first,
190              * there is at least one empty slot.
191              */
192             item_new = store_item_to_inventory(player_ptr, &forge);
193             describe_flavor(player_ptr, o_name, &forge, 0);
194             msg_format(_("%s(%c)を貰った。", "You get %s (%c). "), o_name, index_to_label(item_new));
195
196             autopick_alter_item(player_ptr, item_new, false);
197             handle_stuff(player_ptr);
198             change = true;
199         }
200     }
201
202     if (change) {
203         return true;
204     }
205
206     msg_print(_("賞金を得られそうなものは持っていなかった。", "You have nothing."));
207     msg_print(nullptr);
208     return false;
209 }
210
211 /*!
212  * @brief 本日の賞金首情報を表示する。
213  * @param player_ptr プレイヤーへの参照ポインタ
214  */
215 void today_target(PlayerType *player_ptr)
216 {
217     char buf[160];
218     auto *r_ptr = &monraces_info[w_ptr->today_mon];
219
220     clear_bldg(4, 18);
221     c_put_str(TERM_YELLOW, _("本日の賞金首", "Wanted monster that changes from day to day"), 5, 10);
222     sprintf(buf, _("ターゲット: %s", "target: %s"), r_ptr->name.data());
223     c_put_str(TERM_YELLOW, buf, 6, 10);
224     sprintf(buf, _("死体 ---- $%d", "corpse   ---- $%d"), (int)r_ptr->level * 50 + 100);
225     prt(buf, 8, 10);
226     sprintf(buf, _("骨   ---- $%d", "skeleton ---- $%d"), (int)r_ptr->level * 30 + 60);
227     prt(buf, 9, 10);
228     player_ptr->knows_daily_bounty = true;
229 }
230
231 /*!
232  * @brief ツチノコの賞金首情報を表示する。
233  */
234 void tsuchinoko(void)
235 {
236     clear_bldg(4, 18);
237     c_put_str(TERM_YELLOW, _("一獲千金の大チャンス!!!", "Big chance for quick money!!!"), 5, 10);
238     c_put_str(TERM_YELLOW, _("ターゲット:幻の珍獣「ツチノコ」", "target: the rarest animal 'Tsuchinoko'"), 6, 10);
239     c_put_str(TERM_WHITE, _("生け捕り ---- $1,000,000", "catch alive ---- $1,000,000"), 8, 10);
240     c_put_str(TERM_WHITE, _("死体     ----   $200,000", "corpse      ----   $200,000"), 9, 10);
241     c_put_str(TERM_WHITE, _("骨       ----   $100,000", "bones       ----   $100,000"), 10, 10);
242 }
243
244 /*!
245  * @brief 通常の賞金首情報を表示する。
246  */
247 void show_bounty(void)
248 {
249     TERM_LEN y = 0;
250
251     clear_bldg(4, 18);
252     prt(_("死体を持ち帰れば報酬を差し上げます。", "Offer a prize when you bring a wanted monster's corpse"), 4, 10);
253     c_put_str(TERM_YELLOW, _("現在の賞金首", "Wanted monsters"), 6, 10);
254
255     for (auto i = 0U; i < std::size(w_ptr->bounties); i++) {
256         const auto &[r_idx, is_achieved] = w_ptr->bounties[i];
257         MonsterRaceInfo *r_ptr = &monraces_info[r_idx];
258
259         auto color = is_achieved ? TERM_RED : TERM_WHITE;
260         auto done_mark = is_achieved ? _("(済)", "(done)") : "";
261
262         c_prt(color, format("%s %s", r_ptr->name.data(), done_mark), y + 7, 10);
263
264         y = (y + 1) % 10;
265         if (!y && (i < std::size(w_ptr->bounties) - 1)) {
266             prt(_("何かキーを押してください", "Hit any key."), 0, 0);
267             (void)inkey();
268             prt("", 0, 0);
269             clear_bldg(7, 18);
270         }
271     }
272 }
273
274 /*!
275  * @brief 今日の賞金首を確定する / Determine today's bounty monster
276  * @param PlayerType プレイヤーへの参照ポインタ
277  * @note conv_old is used if loaded 0.0.3 or older save file
278  */
279 void determine_daily_bounty(PlayerType *player_ptr, bool conv_old)
280 {
281     int max_dl = 3;
282     if (!conv_old) {
283         for (const auto &d_ref : dungeons_info) {
284             if (max_dlv[d_ref.idx] < d_ref.mindepth) {
285                 continue;
286             }
287             if (max_dl < max_dlv[d_ref.idx]) {
288                 max_dl = max_dlv[d_ref.idx];
289             }
290         }
291     } else {
292         max_dl = std::max(max_dlv[DUNGEON_ANGBAND], 3);
293     }
294
295     get_mon_num_prep_bounty(player_ptr);
296
297     while (true) {
298         w_ptr->today_mon = get_mon_num(player_ptr, std::min(max_dl / 2, 40), max_dl, GMN_ARENA);
299         MonsterRaceInfo *r_ptr;
300         r_ptr = &monraces_info[w_ptr->today_mon];
301
302         if (cheat_hear) {
303             msg_format("日替わり候補: %s ", r_ptr->name.data());
304         }
305
306         if (r_ptr->kind_flags.has(MonsterKindType::UNIQUE)) {
307             continue;
308         }
309         if (r_ptr->population_flags.has(MonsterPopulationType::NAZGUL) || any_bits(r_ptr->flags7, RF7_UNIQUE2)) {
310             continue;
311         }
312         if (r_ptr->flags2 & RF2_MULTIPLY) {
313             continue;
314         }
315         if (!r_ptr->drop_flags.has_all_of({ MonsterDropType::DROP_CORPSE, MonsterDropType::DROP_SKELETON })) {
316             continue;
317         }
318         if (r_ptr->rarity > 10) {
319             continue;
320         }
321         break;
322     }
323
324     player_ptr->knows_daily_bounty = false;
325 }
326
327 /*!
328  * @brief 賞金首となるユニークを確定する / Determine bounty uniques
329  * @param player_ptr プレイヤーへの参照ポインタ
330  */
331 void determine_bounty_uniques(PlayerType *player_ptr)
332 {
333     get_mon_num_prep_bounty(player_ptr);
334
335     auto is_suitable_for_bounty = [](MonsterRaceId r_idx) {
336         const auto &r_ref = monraces_info[r_idx];
337         bool is_suitable = r_ref.kind_flags.has(MonsterKindType::UNIQUE);
338         is_suitable &= r_ref.drop_flags.has_any_of({ MonsterDropType::DROP_CORPSE, MonsterDropType::DROP_SKELETON });
339         is_suitable &= r_ref.rarity <= 100;
340         is_suitable &= !no_questor_or_bounty_uniques(r_idx);
341         return is_suitable;
342     };
343
344     // 賞金首とするモンスターの種族IDのリストを生成
345     std::vector<MonsterRaceId> bounty_r_idx_list;
346     while (bounty_r_idx_list.size() < std::size(w_ptr->bounties)) {
347         auto r_idx = get_mon_num(player_ptr, 0, MAX_DEPTH - 1, GMN_ARENA);
348         if (!is_suitable_for_bounty(r_idx)) {
349             continue;
350         }
351
352         auto is_already_selected = std::any_of(bounty_r_idx_list.begin(), bounty_r_idx_list.end(),
353             [r_idx](MonsterRaceId bounty_r_idx) { return r_idx == bounty_r_idx; });
354         if (!is_already_selected) {
355             bounty_r_idx_list.push_back(r_idx);
356         }
357     }
358
359     // モンスターのLVで昇順に並び替える
360     std::sort(bounty_r_idx_list.begin(), bounty_r_idx_list.end(),
361         [](MonsterRaceId r_idx1, MonsterRaceId r_idx2) {
362             return monraces_info[r_idx1].level < monraces_info[r_idx2].level;
363         });
364
365     // 賞金首情報を設定
366     std::transform(bounty_r_idx_list.begin(), bounty_r_idx_list.end(), std::begin(w_ptr->bounties),
367         [](MonsterRaceId r_idx) -> bounty_type {
368             return { r_idx, false };
369         });
370 }