OSDN Git Service

[Refactor] #3778 Reshaped bldg_process_command()
[hengbandforosx/hengbandosx.git] / src / cmd-building / cmd-building.cpp
1 /*!
2  * @brief 町の施設処理 / Building commands
3  * @date 2013/12/23
4  * @author
5  * Created by Ken Wigle for Kangband - a variant of Angband 2.8.3
6  * -KMW-
7  *
8  * Rewritten for Kangband 2.8.3i using Kamband's version of
9  * building.c as written by Ivan Tkatchev
10  *
11  * Changed for ZAngband by Robert Ruehlmann
12  */
13
14 #include "cmd-building/cmd-building.h"
15 #include "avatar/avatar.h"
16 #include "cmd-action/cmd-spell.h"
17 #include "cmd-building/cmd-inn.h"
18 #include "cmd-io/cmd-dump.h"
19 #include "core/asking-player.h"
20 #include "core/scores.h"
21 #include "core/show-file.h"
22 #include "core/special-internal-keys.h"
23 #include "core/stuff-handler.h"
24 #include "core/window-redrawer.h"
25 #include "floor/cave.h"
26 #include "floor/floor-events.h"
27 #include "floor/floor-mode-changer.h"
28 #include "floor/wild.h"
29 #include "io/input-key-acceptor.h"
30 #include "io/input-key-requester.h"
31 #include "main/music-definitions-table.h"
32 #include "main/sound-of-music.h"
33 #include "market/arena.h"
34 #include "market/bounty.h"
35 #include "market/building-actions-table.h"
36 #include "market/building-craft-armor.h"
37 #include "market/building-craft-fix.h"
38 #include "market/building-craft-weapon.h"
39 #include "market/building-enchanter.h"
40 #include "market/building-monster.h"
41 #include "market/building-quest.h"
42 #include "market/building-recharger.h"
43 #include "market/building-service.h"
44 #include "market/building-util.h"
45 #include "market/play-gamble.h"
46 #include "market/poker.h"
47 #include "monster-race/monster-race.h"
48 #include "mutation/mutation-flag-types.h"
49 #include "mutation/mutation-investor-remover.h"
50 #include "object-hook/hook-armor.h"
51 #include "object-hook/hook-weapon.h"
52 #include "object/item-tester-hooker.h"
53 #include "player-status/player-energy.h"
54 #include "player/player-personality-types.h"
55 #include "spell-kind/spells-perception.h"
56 #include "spell-kind/spells-world.h"
57 #include "spell/spells-status.h"
58 #include "system/angband-system.h"
59 #include "system/building-type-definition.h"
60 #include "system/floor-type-definition.h"
61 #include "system/grid-type-definition.h"
62 #include "system/item-entity.h"
63 #include "system/player-type-definition.h"
64 #include "system/redrawing-flags-updater.h"
65 #include "system/terrain-type-definition.h"
66 #include "term/gameterm.h"
67 #include "term/screen-processor.h"
68 #include "util/bit-flags-calculator.h"
69 #include "util/int-char-converter.h"
70 #include "view/display-messages.h"
71 #include "world/world.h"
72
73 uint32_t mon_odds[4];
74 int battle_odds;
75 PRICE kakekin;
76 int sel_monster;
77
78 bool reinit_wilderness = false;
79
80 /*!
81  * @brief 町に関するヘルプを表示する / Display town history
82  * @param player_ptr プレイヤーへの参照ポインタ
83  */
84 static void town_history(PlayerType *player_ptr)
85 {
86     screen_save();
87     (void)show_file(player_ptr, true, _("jbldg.txt", "bldg.txt"), 0, 0);
88     screen_load();
89 }
90
91 /*!
92  * @brief 施設の処理実行メインルーチン / Execute a building command
93  * @param player_ptr プレイヤーへの参照ポインタ
94  * @param bldg 施設構造体の参照ポインタ
95  * @param i 実行したい施設のサービステーブルの添字
96  */
97 static void bldg_process_command(PlayerType *player_ptr, building_type *bldg, int i)
98 {
99     msg_flag = false;
100     msg_erase();
101     const auto can_be_owner = is_owner(player_ptr, bldg);
102     const auto building_cost = can_be_owner ? bldg->member_costs[i] : bldg->other_costs[i];
103     if (((bldg->action_restr[i] == 1) && !is_member(player_ptr, bldg)) || ((bldg->action_restr[i] == 2) && !can_be_owner)) {
104         msg_print(_("それを選択する権利はありません!", "You have no right to choose that!"));
105         return;
106     }
107
108     const auto building_action = bldg->actions[i];
109     if ((building_action != BACT_RECHARGE) && (((bldg->member_costs[i] > player_ptr->au) && can_be_owner) || ((bldg->other_costs[i] > player_ptr->au) && !can_be_owner))) {
110         msg_print(_("お金が足りません!", "You do not have the gold!"));
111         return;
112     }
113
114     switch (building_action) {
115     case BACT_NOTHING:
116         /* Do nothing */
117         return;
118     case BACT_RESEARCH_ITEM:
119         if (identify_fully(player_ptr, false)) {
120             player_ptr->au -= building_cost;
121         }
122
123         return;
124     case BACT_TOWN_HISTORY:
125         town_history(player_ptr);
126         return;
127     case BACT_RACE_LEGENDS:
128         race_legends(player_ptr);
129         return;
130     case BACT_QUEST:
131         castle_quest(player_ptr);
132         return;
133     case BACT_KING_LEGENDS:
134     case BACT_ARENA_LEGENDS:
135     case BACT_LEGENDS:
136         show_highclass(player_ptr);
137         return;
138     case BACT_POSTER:
139     case BACT_ARENA_RULES:
140     case BACT_ARENA:
141         arena_comm(player_ptr, building_action);
142         return;
143     case BACT_IN_BETWEEN:
144     case BACT_CRAPS:
145     case BACT_SPIN_WHEEL:
146     case BACT_DICE_SLOTS:
147     case BACT_GAMBLE_RULES:
148     case BACT_POKER:
149         gamble_comm(player_ptr, building_action);
150         return;
151     case BACT_REST:
152     case BACT_RUMORS:
153     case BACT_FOOD:
154         if (inn_comm(player_ptr, building_action)) {
155             player_ptr->au -= building_cost;
156         }
157
158         return;
159     case BACT_RESEARCH_MONSTER:
160         if (research_mon(player_ptr)) {
161             player_ptr->au -= building_cost;
162         }
163
164         return;
165     case BACT_COMPARE_WEAPONS:
166         player_ptr->au -= compare_weapons(player_ptr, building_cost);
167         return;
168     case BACT_ENCHANT_WEAPON:
169         enchant_item(player_ptr, building_cost, 1, 1, 0, FuncItemTester(&ItemEntity::allow_enchant_melee_weapon));
170         return;
171     case BACT_ENCHANT_ARMOR:
172         enchant_item(player_ptr, building_cost, 0, 0, 1, FuncItemTester(&ItemEntity::is_protector));
173         return;
174     case BACT_RECHARGE:
175         building_recharge(player_ptr);
176         return;
177     case BACT_RECHARGE_ALL:
178         building_recharge_all(player_ptr);
179         return;
180     case BACT_IDENTS:
181         if (!input_check(_("持ち物を全て鑑定してよろしいですか?", "Do you pay to identify all your possession? "))) {
182             return;
183         }
184
185         identify_pack(player_ptr);
186         msg_print(_(" 持ち物全てが鑑定されました。", "Your possessions have been identified."));
187         player_ptr->au -= building_cost;
188         return;
189     case BACT_IDENT_ONE:
190         if (ident_spell(player_ptr, false)) {
191             player_ptr->au -= building_cost;
192         }
193
194         return;
195     case BACT_LEARN:
196         do_cmd_study(player_ptr);
197         return;
198     case BACT_HEALING:
199         if (cure_critical_wounds(player_ptr, 200)) {
200             player_ptr->au -= building_cost;
201         }
202
203         return;
204     case BACT_RESTORE:
205         if (restore_all_status(player_ptr)) {
206             player_ptr->au -= building_cost;
207         }
208
209         return;
210     case BACT_ENCHANT_ARROWS:
211         enchant_item(player_ptr, building_cost, 1, 1, 0, FuncItemTester(&ItemEntity::is_ammo));
212         return;
213     case BACT_ENCHANT_BOW:
214         enchant_item(player_ptr, building_cost, 1, 1, 0, TvalItemTester(ItemKindType::BOW));
215         return;
216     case BACT_RECALL:
217         if (recall_player(player_ptr, 1)) {
218             player_ptr->au -= building_cost;
219         }
220
221         return;
222     case BACT_TELEPORT_LEVEL:
223         screen_save();
224         clear_bldg(4, 20);
225         if (free_level_recall(player_ptr)) {
226             player_ptr->au -= building_cost;
227         }
228
229         screen_load();
230         return;
231     case BACT_LOSE_MUTATION: {
232         auto muta = player_ptr->muta;
233         if (player_ptr->ppersonality == PERSONALITY_LUCKY) {
234             // ラッキーマンの白オーラは突然変異治療の対象外
235             muta.reset(PlayerMutationType::GOOD_LUCK);
236         }
237
238         if (muta.any()) {
239             while (!lose_mutation(player_ptr, 0)) {
240                 ;
241             }
242
243             player_ptr->au -= building_cost;
244             return;
245         }
246
247         msg_print(_("治すべき突然変異が無い。", "You have no mutations."));
248         msg_print(nullptr);
249         return;
250     }
251     case BACT_BATTLE:
252         monster_arena_comm(player_ptr);
253         return;
254     case BACT_TSUCHINOKO:
255         tsuchinoko();
256         return;
257     case BACT_BOUNTY:
258         show_bounty();
259         return;
260     case BACT_TARGET:
261         today_target(player_ptr);
262         return;
263     case BACT_KANKIN:
264         exchange_cash(player_ptr);
265         return;
266     case BACT_HEIKOUKA:
267         msg_print(_("平衡化の儀式を行なった。", "You received an equalization ritual."));
268         set_virtue(player_ptr, Virtue::COMPASSION, 0);
269         set_virtue(player_ptr, Virtue::HONOUR, 0);
270         set_virtue(player_ptr, Virtue::JUSTICE, 0);
271         set_virtue(player_ptr, Virtue::SACRIFICE, 0);
272         set_virtue(player_ptr, Virtue::KNOWLEDGE, 0);
273         set_virtue(player_ptr, Virtue::FAITH, 0);
274         set_virtue(player_ptr, Virtue::ENLIGHTEN, 0);
275         set_virtue(player_ptr, Virtue::ENCHANT, 0);
276         set_virtue(player_ptr, Virtue::CHANCE, 0);
277         set_virtue(player_ptr, Virtue::NATURE, 0);
278         set_virtue(player_ptr, Virtue::HARMONY, 0);
279         set_virtue(player_ptr, Virtue::VITALITY, 0);
280         set_virtue(player_ptr, Virtue::UNLIFE, 0);
281         set_virtue(player_ptr, Virtue::PATIENCE, 0);
282         set_virtue(player_ptr, Virtue::TEMPERANCE, 0);
283         set_virtue(player_ptr, Virtue::DILIGENCE, 0);
284         set_virtue(player_ptr, Virtue::VALOUR, 0);
285         set_virtue(player_ptr, Virtue::INDIVIDUALISM, 0);
286         initialize_virtues(player_ptr);
287         player_ptr->au -= building_cost;
288         return;
289     case BACT_TELE_TOWN:
290         if (tele_town(player_ptr)) {
291             player_ptr->au -= building_cost;
292         }
293
294         return;
295     case BACT_EVAL_AC:
296         if (eval_ac(player_ptr->dis_ac + player_ptr->dis_to_a)) {
297             player_ptr->au -= building_cost;
298         }
299
300         return;
301     case BACT_BROKEN_WEAPON:
302         player_ptr->au -= repair_broken_weapon(player_ptr, building_cost);
303         return;
304     }
305 }
306
307 /*!
308  * @brief 施設入り口にプレイヤーが乗った際の処理 / Do building commands
309  * @param プレイヤーへの参照ポインタ
310  */
311 void do_cmd_building(PlayerType *player_ptr)
312 {
313     if (player_ptr->wild_mode) {
314         return;
315     }
316
317     PlayerEnergy energy(player_ptr);
318     energy.set_player_turn_energy(100);
319     const auto p_pos = player_ptr->get_position();
320     if (!cave_has_flag_bold(player_ptr->current_floor_ptr, p_pos.y, p_pos.x, TerrainCharacteristics::BLDG)) {
321         msg_print(_("ここには建物はない。", "You see no building here."));
322         return;
323     }
324
325     int which = player_ptr->current_floor_ptr->get_grid(p_pos).get_terrain().subtype;
326
327     building_type *bldg;
328     bldg = &buildings[which];
329
330     reinit_wilderness = false;
331
332     if ((which == 2) && (player_ptr->arena_number < 0)) {
333         msg_print(_("「敗者に用はない。」", "'There's no place here for a LOSER like you!'"));
334         return;
335     }
336
337     if ((which == 2) && player_ptr->current_floor_ptr->inside_arena) {
338         if (!player_ptr->exit_bldg && player_ptr->current_floor_ptr->m_cnt > 0) {
339             prt(_("ゲートは閉まっている。モンスターがあなたを待っている!", "The gates are closed.  The monster awaits!"), 0, 0);
340         } else {
341             prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_NO_RETURN);
342             player_ptr->current_floor_ptr->inside_arena = false;
343             player_ptr->leaving = true;
344             command_new = SPECIAL_KEY_BUILDING;
345             energy.reset_player_turn();
346         }
347
348         return;
349     }
350
351     TermCenteredOffsetSetter tcos(MAIN_TERM_MIN_COLS, MAIN_TERM_MIN_ROWS);
352
353     auto &system = AngbandSystem::get_instance();
354     if (system.is_phase_out()) {
355         prepare_change_floor_mode(player_ptr, CFM_SAVE_FLOORS | CFM_NO_RETURN);
356         player_ptr->leaving = true;
357         system.set_phase_out(false);
358         command_new = SPECIAL_KEY_BUILDING;
359         energy.reset_player_turn();
360         return;
361     }
362
363     player_ptr->oldpy = player_ptr->y;
364     player_ptr->oldpx = player_ptr->x;
365     forget_lite(player_ptr->current_floor_ptr);
366     forget_view(player_ptr->current_floor_ptr);
367     w_ptr->character_icky_depth++;
368
369     command_arg = 0;
370     command_rep = 0;
371     command_new = 0;
372
373     display_buikding_service(player_ptr, bldg);
374     player_ptr->leave_bldg = false;
375     play_music(TERM_XTRA_MUSIC_BASIC, MUSIC_BASIC_BUILD);
376
377     bool validcmd;
378     while (!player_ptr->leave_bldg) {
379         validcmd = false;
380         prt("", 1, 0);
381
382         building_prt_gold(player_ptr);
383
384         char command = inkey();
385
386         if (command == ESCAPE) {
387             player_ptr->leave_bldg = true;
388             player_ptr->current_floor_ptr->inside_arena = false;
389             system.set_phase_out(false);
390             break;
391         }
392
393         int i;
394         for (i = 0; i < 8; i++) {
395             if (bldg->letters[i] && (bldg->letters[i] == command)) {
396                 validcmd = true;
397                 break;
398             }
399         }
400
401         if (validcmd) {
402             bldg_process_command(player_ptr, bldg, i);
403         }
404
405         handle_stuff(player_ptr);
406     }
407
408     select_floor_music(player_ptr);
409
410     msg_flag = false;
411     msg_erase();
412
413     if (reinit_wilderness) {
414         player_ptr->leaving = true;
415     }
416
417     w_ptr->character_icky_depth--;
418     term_clear();
419
420     auto &rfu = RedrawingFlagsUpdater::get_instance();
421     static constexpr auto flags_srf = {
422         StatusRecalculatingFlag::VIEW,
423         StatusRecalculatingFlag::MONSTER_STATUSES,
424         StatusRecalculatingFlag::BONUS,
425         StatusRecalculatingFlag::LITE,
426         StatusRecalculatingFlag::MONSTER_LITE,
427     };
428     rfu.set_flags(flags_srf);
429     static constexpr auto flags_mwrf = {
430         MainWindowRedrawingFlag::BASIC,
431         MainWindowRedrawingFlag::EXTRA,
432         MainWindowRedrawingFlag::EQUIPPY,
433         MainWindowRedrawingFlag::MAP,
434     };
435     rfu.set_flags(flags_mwrf);
436     static constexpr auto flags_swrf = {
437         SubWindowRedrawingFlag::OVERHEAD,
438         SubWindowRedrawingFlag::DUNGEON,
439     };
440     rfu.set_flags(flags_swrf);
441 }