1 #include "store/purchase-order.h"
2 #include "autopick/autopick.h"
3 #include "core/asking-player.h"
4 #include "core/stuff-handler.h"
5 #include "flavor/flavor-describer.h"
6 #include "flavor/object-flavor-types.h"
7 #include "game-option/birth-options.h"
8 #include "game-option/play-record-options.h"
9 #include "grid/feature.h"
10 #include "inventory/inventory-object.h"
11 #include "io/write-diary.h"
12 #include "main/sound-definitions-table.h"
13 #include "main/sound-of-music.h"
14 #include "object-enchant/item-feeling.h"
15 #include "object-enchant/special-object-flags.h"
16 #include "object/object-generator.h"
17 #include "object/object-info.h"
18 #include "object/object-stack.h"
19 #include "object/object-value.h"
20 #include "perception/object-perception.h"
21 #include "player-info/avatar.h"
22 #include "player/race-info-table.h"
23 #include "store/home.h"
24 #include "store/owner-insults.h"
25 #include "store/pricing.h"
26 #include "store/say-comments.h"
27 #include "store/store-util.h"
28 #include "store/store.h"
29 #include "term/screen-processor.h"
30 #include "util/int-char-converter.h"
31 #include "view/display-messages.h"
32 #include "view/display-store.h"
33 #include "world/world.h"
35 typedef struct haggle_type {
52 static haggle_type *initialize_haggle_type(player_type *player_ptr, haggle_type *haggle_ptr, object_type *o_ptr, s32b *price)
54 haggle_ptr->o_ptr = o_ptr;
55 haggle_ptr->price = price;
56 haggle_ptr->cur_ask = price_item(player_ptr, o_ptr, ot_ptr->max_inflate, FALSE);
57 haggle_ptr->final_ask = price_item(player_ptr, o_ptr, ot_ptr->min_inflate, FALSE);
58 haggle_ptr->noneed = noneedtobargain(haggle_ptr->final_ask);
59 haggle_ptr->final = FALSE;
60 haggle_ptr->pmt = _("提示価格", "Asking");
64 static void settle_haggle_result(haggle_type *haggle_ptr)
66 if (!haggle_ptr->noneed && manual_haggle)
69 if (haggle_ptr->noneed) {
70 msg_print(_("結局この金額にまとまった。", "You eventually agree upon the price."));
73 msg_print(_("すんなりとこの金額にまとまった。", "You quickly agree upon the price."));
75 haggle_ptr->final_ask += haggle_ptr->final_ask / 10;
78 haggle_ptr->cur_ask = haggle_ptr->final_ask;
79 haggle_ptr->pmt = _("最終提示価格", "Final Offer");
80 haggle_ptr->final = TRUE;
83 static void decide_haggle_offer(player_type *player_ptr, haggle_type *haggle_ptr)
85 haggle_ptr->cur_ask *= haggle_ptr->o_ptr->number;
86 haggle_ptr->final_ask *= haggle_ptr->o_ptr->number;
87 haggle_ptr->min_per = ot_ptr->haggle_per;
88 haggle_ptr->max_per = haggle_ptr->min_per * 3;
89 haggle_ptr->last_offer = object_value(player_ptr, haggle_ptr->o_ptr) * haggle_ptr->o_ptr->number;
90 haggle_ptr->last_offer = haggle_ptr->last_offer * (200 - (int)(ot_ptr->max_inflate)) / 100L;
91 if (haggle_ptr->last_offer <= 0)
92 haggle_ptr->last_offer = 1;
95 static void initialize_haggle_offer(haggle_type *haggle_ptr)
97 haggle_ptr->offer = 0;
99 haggle_ptr->flag = FALSE;
100 haggle_ptr->annoyed = 0;
101 haggle_ptr->cancel = FALSE;
102 *haggle_ptr->price = 0;
105 static void suggest_haggle_offer(haggle_type *haggle_ptr)
107 bool loop_flag = TRUE;
108 while (!haggle_ptr->flag && loop_flag) {
110 (void)sprintf(out_val, "%s : %ld", haggle_ptr->pmt, (long)haggle_ptr->cur_ask);
111 put_str(out_val, 1, 0);
113 = receive_offer(_("提示する金額? ", "What do you offer? "), &haggle_ptr->offer, haggle_ptr->last_offer, 1, haggle_ptr->cur_ask, haggle_ptr->final);
114 if (haggle_ptr->cancel) {
115 haggle_ptr->flag = TRUE;
116 } else if (haggle_ptr->offer > haggle_ptr->cur_ask) {
118 haggle_ptr->offer = haggle_ptr->last_offer;
119 } else if (haggle_ptr->offer == haggle_ptr->cur_ask) {
120 haggle_ptr->flag = TRUE;
121 *haggle_ptr->price = haggle_ptr->offer;
128 static void final_haggle_offer(haggle_type *haggle_ptr)
130 haggle_ptr->final = TRUE;
131 haggle_ptr->cur_ask = haggle_ptr->final_ask;
132 haggle_ptr->pmt = _("最終提示価格", "What do you offer? ");
133 haggle_ptr->annoyed++;
134 if (haggle_ptr->annoyed <= 3)
137 (void)increase_insults();
138 haggle_ptr->cancel = TRUE;
139 haggle_ptr->flag = TRUE;
142 static void show_last_haggle_offer(haggle_type *haggle_ptr)
144 haggle_ptr->last_offer = haggle_ptr->offer;
148 (void)sprintf(out_val, _("前回の提示金額: $%ld", "Your last offer: %ld"), (long)haggle_ptr->last_offer);
149 put_str(out_val, 1, 39);
150 say_comment_2(haggle_ptr->cur_ask, haggle_ptr->annoyed);
154 * @brief プレイヤーが購入する時の値切り処理メインルーチン /
155 * Haggling routine -RAK-
156 * @param player_ptr プレーヤーへの参照ポインタ
157 * @param o_ptr オブジェクトの構造体参照ポインタ
158 * @param price 最終価格を返す参照ポインタ
159 * @return プレイヤーの価格に対して店主が不服ならばTRUEを返す /
160 * Return TRUE if purchase is NOT successful
162 static bool purchase_haggle(player_type *player_ptr, object_type *o_ptr, s32b *price)
164 haggle_type tmp_haggle;
165 haggle_type *haggle_ptr = initialize_haggle_type(player_ptr, &tmp_haggle, o_ptr, price);
166 settle_haggle_result(haggle_ptr);
167 decide_haggle_offer(player_ptr, haggle_ptr);
168 initialize_haggle_offer(haggle_ptr);
169 while (!haggle_ptr->flag) {
170 suggest_haggle_offer(haggle_ptr);
171 if (haggle_ptr->flag)
174 s32b x1 = 100 * (haggle_ptr->offer - haggle_ptr->last_offer) / (haggle_ptr->cur_ask - haggle_ptr->last_offer);
175 if (x1 < haggle_ptr->min_per) {
176 if (haggle_insults()) {
177 haggle_ptr->flag = TRUE;
178 haggle_ptr->cancel = TRUE;
180 } else if (x1 > haggle_ptr->max_per) {
182 if (x1 < haggle_ptr->max_per)
183 x1 = haggle_ptr->max_per;
186 s32b x2 = rand_range(x1 - 2, x1 + 2);
187 s32b x3 = ((haggle_ptr->cur_ask - haggle_ptr->offer) * x2 / 100L) + 1;
190 haggle_ptr->cur_ask -= x3;
192 if (haggle_ptr->cur_ask < haggle_ptr->final_ask)
193 final_haggle_offer(haggle_ptr);
194 else if (haggle_ptr->offer >= haggle_ptr->cur_ask) {
195 haggle_ptr->flag = TRUE;
196 *haggle_ptr->price = haggle_ptr->offer;
199 if (haggle_ptr->flag)
202 show_last_haggle_offer(haggle_ptr);
205 if (haggle_ptr->cancel)
208 updatebargain(*price, haggle_ptr->final_ask, o_ptr->number);
212 static bool show_store_select_item(COMMAND_CODE *item, const int i)
216 /* ブラックマーケットの時は別のメッセージ */
217 switch (cur_store_num) {
219 sprintf(out_val, "どのアイテムを取りますか? ");
222 sprintf(out_val, "どれ? ");
225 sprintf(out_val, "どの品物が欲しいんだい? ");
229 if (cur_store_num == STORE_HOME) {
230 sprintf(out_val, "Which item do you want to take? ");
232 sprintf(out_val, "Which item are you interested in? ");
236 return get_stock(item, out_val, 0, i - 1) != 0;
239 static bool process_purchase_result(player_type *player_ptr, object_type *o_ptr, object_type *j_ptr, COMMAND_CODE *item_new,const int amt, int *i, const COMMAND_CODE item)
241 if (cur_store_num != STORE_HOME)
244 bool combined_or_reordered;
245 distribute_charges(o_ptr, j_ptr, amt);
246 *item_new = store_item_to_inventory(player_ptr, j_ptr);
247 GAME_TEXT o_name[MAX_NLEN];
248 describe_flavor(player_ptr, o_name, &player_ptr->inventory_list[*item_new], 0);
249 msg_format(_("%s(%c)を取った。", "You have %s (%c)."), o_name, index_to_label(*item_new));
250 handle_stuff(player_ptr);
251 *i = st_ptr->stock_num;
252 store_item_increase(item, -amt);
253 store_item_optimize(item);
254 combined_or_reordered = combine_and_reorder_home(player_ptr, STORE_HOME);
255 if (*i == st_ptr->stock_num) {
256 if (combined_or_reordered)
257 display_store_inventory(player_ptr);
259 display_entry(player_ptr, item);
264 if (st_ptr->stock_num == 0)
266 else if (store_top >= st_ptr->stock_num)
267 store_top -= store_bottom;
269 display_store_inventory(player_ptr);
270 chg_virtue(player_ptr, V_SACRIFICE, 1);
274 static void shuffle_store(player_type *player_ptr)
276 if (!one_in_(STORE_SHUFFLE)) {
277 msg_print(_("店主は新たな在庫を取り出した。", "The shopkeeper brings out some new stock."));
282 msg_print(_("店主は引退した。", "The shopkeeper retires."));
283 store_shuffle(player_ptr, cur_store_num);
285 sprintf(buf, "%s (%s)", ot_ptr->owner_name, race_info[ot_ptr->owner_race].title);
287 sprintf(buf, "%s (%ld)", (f_name + f_info[cur_store_feat].name), (long)(ot_ptr->max_cost));
291 static void switch_store_stock(player_type *player_ptr, const int i, const COMMAND_CODE item)
293 if (st_ptr->stock_num == 0) {
294 shuffle_store(player_ptr);
295 for (int j = 0; j < 10; j++)
296 store_maintenance(player_ptr, player_ptr->town_num, cur_store_num);
299 display_store_inventory(player_ptr);
303 if (st_ptr->stock_num != i) {
304 if (store_top >= st_ptr->stock_num)
305 store_top -= store_bottom;
307 display_store_inventory(player_ptr);
311 display_entry(player_ptr, item);
315 * @brief 店からの購入処理のメインルーチン /
316 * Buy an item from a store -RAK-
317 * @param player_ptr プレーヤーへの参照ポインタ
320 void store_purchase(player_type *player_ptr)
322 if (cur_store_num == STORE_MUSEUM) {
323 msg_print(_("博物館から取り出すことはできません。", "Museum."));
327 if (st_ptr->stock_num <= 0) {
328 if (cur_store_num == STORE_HOME)
329 msg_print(_("我が家には何も置いてありません。", "Your home is empty."));
331 msg_print(_("現在商品の在庫を切らしています。", "I am currently out of stock."));
335 int i = (st_ptr->stock_num - store_top);
336 if (i > store_bottom)
340 if (!show_store_select_item(&item, i))
343 item = item + store_top;
345 o_ptr = &st_ptr->stock[item];
350 object_copy(j_ptr, o_ptr);
353 * If a rod or wand, allocate total maximum timeouts or charges
354 * between those purchased and left on the shelf.
356 reduce_charges(j_ptr, o_ptr->number - amt);
358 if (!check_store_item_to_inventory(player_ptr, j_ptr)) {
359 msg_print(_("そんなにアイテムを持てない。", "You cannot carry that many different items."));
363 PRICE best = price_item(player_ptr, j_ptr, ot_ptr->min_inflate, FALSE);
364 if (o_ptr->number > 1) {
365 if ((cur_store_num != STORE_HOME) && (o_ptr->ident & IDENT_FIXED)) {
366 msg_format(_("一つにつき $%ldです。", "That costs %ld gold per item."), (long)(best));
369 amt = get_quantity(NULL, o_ptr->number);
375 object_copy(j_ptr, o_ptr);
378 * If a rod or wand, allocate total maximum timeouts or charges
379 * between those purchased and left on the shelf.
381 reduce_charges(j_ptr, o_ptr->number - amt);
383 if (!check_store_item_to_inventory(player_ptr, j_ptr)) {
384 msg_print(_("ザックにそのアイテムを入れる隙間がない。", "You cannot carry that many items."));
388 COMMAND_CODE item_new;
389 if (process_purchase_result(player_ptr, o_ptr, j_ptr, &item_new, amt, &i, item))
394 if (o_ptr->ident & (IDENT_FIXED)) {
396 price = (best * j_ptr->number);
398 GAME_TEXT o_name[MAX_NLEN];
399 describe_flavor(player_ptr, o_name, j_ptr, 0);
400 msg_format(_("%s(%c)を購入する。", "Buying %s (%c)."), o_name, I2A(item));
402 choice = purchase_haggle(player_ptr, j_ptr, &price);
403 if (st_ptr->store_open >= current_world_ptr->game_turn)
410 if (price == (best * j_ptr->number))
411 o_ptr->ident |= (IDENT_FIXED);
413 if (player_ptr->au < price) {
414 msg_print(_("お金が足りません。", "You do not have enough gold."));
418 say_comment_1(player_ptr);
419 if (cur_store_num == STORE_BLACK)
420 chg_virtue(player_ptr, V_JUSTICE, -1);
421 if ((o_ptr->tval == TV_BOTTLE) && (cur_store_num != STORE_HOME))
422 chg_virtue(player_ptr, V_NATURE, -1);
426 player_ptr->au -= price;
427 store_prt_gold(player_ptr);
428 object_aware(player_ptr, j_ptr);
429 j_ptr->ident &= ~(IDENT_FIXED);
430 GAME_TEXT o_name[MAX_NLEN];
431 describe_flavor(player_ptr, o_name, j_ptr, 0);
433 msg_format(_("%sを $%ldで購入しました。", "You bought %s for %ld gold."), o_name, (long)price);
435 strcpy(record_o_name, o_name);
436 record_turn = current_world_ptr->game_turn;
439 exe_write_diary(player_ptr, DIARY_BUY, 0, o_name);
440 describe_flavor(player_ptr, o_name, o_ptr, OD_NAME_ONLY);
441 if (record_rand_art && o_ptr->art_name)
442 exe_write_diary(player_ptr, DIARY_ART, 0, o_name);
444 j_ptr->inscription = 0;
445 j_ptr->feeling = FEEL_NONE;
446 j_ptr->ident &= ~(IDENT_STORE);
447 item_new = store_item_to_inventory(player_ptr, j_ptr);
449 describe_flavor(player_ptr, o_name, &player_ptr->inventory_list[item_new], 0);
450 msg_format(_("%s(%c)を手に入れた。", "You have %s (%c)."), o_name, index_to_label(item_new));
451 autopick_alter_item(player_ptr, item_new, FALSE);
452 if ((o_ptr->tval == TV_ROD) || (o_ptr->tval == TV_WAND))
453 o_ptr->pval -= j_ptr->pval;
455 handle_stuff(player_ptr);
456 i = st_ptr->stock_num;
457 store_item_increase(item, -amt);
458 store_item_optimize(item);
459 switch_store_stock(player_ptr, i, item);