OSDN Git Service

Merge pull request #2725 from Hourier/Move-Feature-to-System
[hengbandforosx/hengbandosx.git] / src / store / cmd-store.cpp
1 #include "store/cmd-store.h"
2 #include "cmd-io/macro-util.h"
3 #include "core/player-redraw-types.h"
4 #include "core/player-update-types.h"
5 #include "core/stuff-handler.h"
6 #include "core/window-redrawer.h"
7 #include "flavor/flavor-describer.h"
8 #include "floor/cave.h"
9 #include "floor/floor-events.h"
10 #include "floor/floor-town.h"
11 #include "floor/wild.h"
12 #include "game-option/birth-options.h"
13 #include "game-option/input-options.h"
14 #include "inventory/inventory-object.h"
15 #include "inventory/inventory-slot-types.h"
16 #include "io/input-key-requester.h"
17 #include "main/music-definitions-table.h"
18 #include "main/sound-of-music.h"
19 #include "object/object-info.h"
20 #include "player-status/player-energy.h"
21 #include "store/home.h"
22 #include "store/store-key-processor.h"
23 #include "store/store-owners.h"
24 #include "store/store-util.h"
25 #include "store/store.h"
26 #include "system/dungeon-info.h"
27 #include "system/floor-type-definition.h"
28 #include "system/grid-type-definition.h"
29 #include "system/object-type-definition.h"
30 #include "system/player-type-definition.h"
31 #include "system/terrain-type-definition.h"
32 #include "term/screen-processor.h"
33 #include "util/bit-flags-calculator.h"
34 #include "view/display-messages.h"
35 #include "view/display-store.h"
36 #include "world/world.h"
37
38 #define MIN_STOCK 12
39
40 /*!
41  * @brief 店舗処理全体のメインルーチン /
42  * Enter a store, and interact with it. *
43  * @param player_ptr プレイヤーへの参照ポインタ
44  * @note
45  * <pre>
46  * Note that we use the standard "request_command()" function
47  * to get a command, allowing us to use "command_arg" and all
48  * command macros and other nifty stuff, but we use the special
49  * "shopping" argument, to force certain commands to be converted
50  * into other commands, normally, we convert "p" (pray) and "m"
51  * (cast magic) into "g" (get), and "s" (search) into "d" (drop).
52  * </pre>
53  */
54 void do_cmd_store(PlayerType *player_ptr)
55 {
56     if (player_ptr->wild_mode) {
57         return;
58     }
59     TERM_LEN w, h;
60     term_get_size(&w, &h);
61
62     xtra_stock = std::min(14 + 26, ((h > 24) ? (h - 24) : 0));
63     store_bottom = MIN_STOCK + xtra_stock;
64
65     auto *floor_ptr = player_ptr->current_floor_ptr;
66     const auto *g_ptr = &floor_ptr->grid_array[player_ptr->y][player_ptr->x];
67     if (!g_ptr->cave_has_flag(TerrainCharacteristics::STORE)) {
68         msg_print(_("ここには店がありません。", "You see no store here."));
69         return;
70     }
71
72     // TODO:
73     //   施設の種類により、一時的に現在地 (player_ptr->town_num) を違う値に偽装して処理している。
74     //   我が家および博物館は全ての町で内容を共有するため、現在地を辺境の地 (1) にしている。
75     //   ダンジョン内の店の場合、現在地を NO_TOWN にしている。
76     //   inner_town_num は、施設内で C コマンドなどを使ったときにそのままでは現在地の偽装がバレる
77     //   ため、それを糊塗するためのグローバル変数。
78     //   この辺はリファクタしたい。
79     StoreSaleType store_num = i2enum<StoreSaleType>(terrains_info[g_ptr->feat].subtype);
80     old_town_num = player_ptr->town_num;
81     if ((store_num == StoreSaleType::HOME) || (store_num == StoreSaleType::MUSEUM)) {
82         player_ptr->town_num = 1;
83     }
84
85     if (floor_ptr->is_in_dungeon()) {
86         player_ptr->town_num = NO_TOWN;
87     }
88
89     inner_town_num = player_ptr->town_num;
90     if ((town_info[player_ptr->town_num].store[enum2i(store_num)].store_open >= w_ptr->game_turn) || ironman_shops) {
91         msg_print(_("ドアに鍵がかかっている。", "The doors are locked."));
92         player_ptr->town_num = old_town_num;
93         return;
94     }
95
96     int maintain_num = (w_ptr->game_turn - town_info[player_ptr->town_num].store[enum2i(store_num)].last_visit) / (TURNS_PER_TICK * STORE_TICKS);
97     if (maintain_num > 10) {
98         maintain_num = 10;
99     }
100     if (maintain_num) {
101         store_maintenance(player_ptr, player_ptr->town_num, store_num, maintain_num);
102
103         town_info[player_ptr->town_num].store[enum2i(store_num)].last_visit = w_ptr->game_turn;
104     }
105
106     forget_lite(floor_ptr);
107     forget_view(floor_ptr);
108     w_ptr->character_icky_depth = 1;
109     command_arg = 0;
110     command_rep = 0;
111     command_new = 0;
112     get_com_no_macros = true;
113     cur_store_feat = g_ptr->feat;
114     st_ptr = &town_info[player_ptr->town_num].store[enum2i(store_num)];
115     ot_ptr = &owners.at(store_num)[st_ptr->owner];
116     store_top = 0;
117     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_BUILD);
118     display_store(player_ptr, store_num);
119     leave_store = false;
120
121     while (!leave_store) {
122         prt("", 1, 0);
123         clear_from(20 + xtra_stock);
124         prt(_(" ESC) 建物から出る", " ESC) Exit from Building."), 21 + xtra_stock, 0);
125         if (st_ptr->stock_num > store_bottom) {
126             prt(_(" -)前ページ", " -) Previous page"), 22 + xtra_stock, 0);
127             prt(_(" スペース) 次ページ", " SPACE) Next page"), 23 + xtra_stock, 0);
128         }
129
130         if (store_num == StoreSaleType::HOME) {
131             prt(_("g) アイテムを取る", "g) Get an item."), 21 + xtra_stock, 27);
132             prt(_("d) アイテムを置く", "d) Drop an item."), 22 + xtra_stock, 27);
133             prt(_("x) 家のアイテムを調べる", "x) eXamine an item in the home."), 23 + xtra_stock, 27);
134         } else if (store_num == StoreSaleType::MUSEUM) {
135             prt(_("d) アイテムを置く", "d) Drop an item."), 21 + xtra_stock, 27);
136             prt(_("r) アイテムの展示をやめる", "r) order to Remove an item."), 22 + xtra_stock, 27);
137             prt(_("x) 博物館のアイテムを調べる", "x) eXamine an item in the museum."), 23 + xtra_stock, 27);
138         } else {
139             prt(_("p) 商品を買う", "p) Purchase an item."), 21 + xtra_stock, 30);
140             prt(_("s) アイテムを売る", "s) Sell an item."), 22 + xtra_stock, 30);
141             prt(_("x) 商品を調べる", "x) eXamine an item in the shop"), 23 + xtra_stock, 30);
142         }
143
144         prt(_("i/e) 持ち物/装備の一覧", "i/e) Inventry/Equipment list"), 21 + xtra_stock, 56);
145         if (rogue_like_commands) {
146             prt(_("w/T) 装備する/はずす", "w/T) Wear/Take off equipment"), 22 + xtra_stock, 56);
147         } else {
148             prt(_("w/t) 装備する/はずす", "w/t) Wear/Take off equipment"), 22 + xtra_stock, 56);
149         }
150
151         prt(_("コマンド:", "You may: "), 20 + xtra_stock, 0);
152         InputKeyRequestor(player_ptr, true).request_command();
153         store_process_command(player_ptr, store_num);
154
155         bool need_redraw_store_inv = any_bits(player_ptr->update, PU_BONUS);
156         w_ptr->character_icky_depth = 1;
157         handle_stuff(player_ptr);
158         if (player_ptr->inventory_list[INVEN_PACK].k_idx) {
159             INVENTORY_IDX item = INVEN_PACK;
160             auto *o_ptr = &player_ptr->inventory_list[item];
161             if (store_num != StoreSaleType::HOME) {
162                 if (store_num == StoreSaleType::MUSEUM) {
163                     msg_print(_("ザックからアイテムがあふれそうなので、あわてて博物館から出た...", "Your pack is so full that you flee the Museum..."));
164                 } else {
165                     msg_print(_("ザックからアイテムがあふれそうなので、あわてて店から出た...", "Your pack is so full that you flee the store..."));
166                 }
167
168                 leave_store = true;
169             } else if (!store_check_num(o_ptr, store_num)) {
170                 msg_print(_("ザックからアイテムがあふれそうなので、あわてて家から出た...", "Your pack is so full that you flee your home..."));
171                 leave_store = true;
172             } else {
173                 int item_pos;
174                 ObjectType forge;
175                 ObjectType *q_ptr;
176                 GAME_TEXT o_name[MAX_NLEN];
177                 msg_print(_("ザックからアイテムがあふれてしまった!", "Your pack overflows!"));
178                 q_ptr = &forge;
179                 q_ptr->copy_from(o_ptr);
180                 describe_flavor(player_ptr, o_name, q_ptr, 0);
181                 msg_format(_("%sが落ちた。(%c)", "You drop %s (%c)."), o_name, index_to_label(item));
182                 vary_item(player_ptr, item, -255);
183                 handle_stuff(player_ptr);
184
185                 item_pos = home_carry(player_ptr, q_ptr, store_num);
186                 if (item_pos >= 0) {
187                     store_top = (item_pos / store_bottom) * store_bottom;
188                     display_store_inventory(player_ptr, store_num);
189                 }
190             }
191         }
192
193         if (need_redraw_store_inv) {
194             display_store_inventory(player_ptr, store_num);
195         }
196
197         if (st_ptr->store_open >= w_ptr->game_turn) {
198             leave_store = true;
199         }
200     }
201
202     // 現在地の偽装を解除。
203     player_ptr->town_num = old_town_num;
204
205     select_floor_music(player_ptr);
206     PlayerEnergy(player_ptr).set_player_turn_energy(100);
207     w_ptr->character_icky_depth = 0;
208     command_new = 0;
209     command_see = false;
210     get_com_no_macros = false;
211
212     msg_erase();
213     term_clear();
214
215     player_ptr->update |= PU_VIEW | PU_LITE | PU_MON_LITE;
216     player_ptr->update |= PU_MONSTERS;
217     player_ptr->redraw |= PR_BASIC | PR_EXTRA | PR_EQUIPPY;
218     player_ptr->redraw |= PR_MAP;
219     player_ptr->window_flags |= PW_OVERHEAD | PW_DUNGEON;
220 }