OSDN Git Service

Merge pull request #3532 from sikabane-works/release/3.0.0.87-alpha
[hengbandforosx/hengbandosx.git] / src / mind / mind-archer.cpp
1 #include "mind/mind-archer.h"
2 #include "action/action-limited.h"
3 #include "autopick/autopick.h"
4 #include "core/asking-player.h"
5 #include "flavor/flavor-describer.h"
6 #include "floor/cave.h"
7 #include "floor/floor-object.h"
8 #include "floor/geometry.h"
9 #include "grid/grid.h"
10 #include "inventory/inventory-object.h"
11 #include "io/command-repeater.h"
12 #include "object-enchant/item-apply-magic.h"
13 #include "object-enchant/item-magic-applier.h"
14 #include "object-enchant/object-boost.h"
15 #include "object/item-tester-hooker.h"
16 #include "object/item-use-flags.h"
17 #include "object/object-kind-hook.h"
18 #include "perception/object-perception.h"
19 #include "system/angband.h"
20 #include "system/baseitem-info.h"
21 #include "system/floor-type-definition.h"
22 #include "system/grid-type-definition.h"
23 #include "system/item-entity.h"
24 #include "system/player-type-definition.h"
25 #include "system/redrawing-flags-updater.h"
26 #include "system/terrain-type-definition.h"
27 #include "target/target-getter.h"
28 #include "util/bit-flags-calculator.h"
29 #include "view/display-messages.h"
30 #include <string>
31
32 enum ammo_creation_type {
33     AMMO_NONE = 0,
34     AMMO_SHOT = 1,
35     AMMO_ARROW = 2,
36     AMMO_BOLT = 3,
37 };
38
39 static bool select_ammo_creation_type(ammo_creation_type &type, PLAYER_LEVEL plev)
40 {
41     COMMAND_CODE code;
42     if (repeat_pull(&code)) {
43         type = i2enum<ammo_creation_type>(code);
44         switch (type) {
45         case AMMO_SHOT:
46         case AMMO_ARROW:
47         case AMMO_BOLT:
48             return true;
49         case AMMO_NONE:
50         default:
51             break;
52         }
53     }
54
55     std::string prompt;
56     if (plev >= 20) {
57         prompt = _("[S]弾, [A]矢, [B]クロスボウの矢 :", "Create [S]hots, Create [A]rrow or Create [B]olt ?");
58     } else if (plev >= 10) {
59         prompt = _("[S]弾, [A]矢:", "Create [S]hots or Create [A]rrow ?");
60     } else {
61         prompt = _("[S]弾:", "Create [S]hots ?");
62     }
63
64     while (type == AMMO_NONE) {
65         const auto command = input_command(prompt, true);
66         if (!command.has_value()) {
67             return false;
68         }
69
70         const auto ch = command.value();
71         if (ch == 'S' || ch == 's') {
72             type = AMMO_SHOT;
73             break;
74         }
75
76         if ((ch == 'A' || ch == 'a') && (plev >= 10)) {
77             type = AMMO_ARROW;
78             break;
79         }
80
81         if ((ch == 'B' || ch == 'b') && (plev >= 20)) {
82             type = AMMO_BOLT;
83             break;
84         }
85     }
86
87     repeat_push(static_cast<COMMAND_CODE>(type));
88     return true;
89 }
90
91 /*!
92  * @brief「弾/矢の製造」処理 / do_cmd_cast calls this function if the player's class is 'archer'.
93  * Hook to determine if an object is contertible in an arrow/bolt
94  * @return 製造を実際に行ったらTRUE、キャンセルしたらFALSEを返す
95  */
96 bool create_ammo(PlayerType *player_ptr)
97 {
98     if (cmd_limit_confused(player_ptr) || cmd_limit_blind(player_ptr)) {
99         return false;
100     }
101
102     ammo_creation_type ext = AMMO_NONE;
103
104     if (!select_ammo_creation_type(ext, player_ptr->lev)) {
105         return false;
106     }
107
108     switch (ext) {
109     case AMMO_SHOT: {
110         DIRECTION dir;
111         if (!get_rep_dir(player_ptr, &dir)) {
112             return false;
113         }
114
115         POSITION y = player_ptr->y + ddy[dir];
116         POSITION x = player_ptr->x + ddx[dir];
117         auto *g_ptr = &player_ptr->current_floor_ptr->grid_array[y][x];
118         if (terrains_info[g_ptr->get_feat_mimic()].flags.has_not(TerrainCharacteristics::CAN_DIG)) {
119             msg_print(_("そこには岩石がない。", "You need a pile of rubble."));
120             return false;
121         }
122
123         if (!g_ptr->cave_has_flag(TerrainCharacteristics::CAN_DIG) || !g_ptr->cave_has_flag(TerrainCharacteristics::HURT_ROCK)) {
124             msg_print(_("硬すぎて崩せなかった。", "You failed to make ammo."));
125             return true;
126         }
127
128         ItemEntity forge;
129         auto *q_ptr = &forge;
130         q_ptr->prep(lookup_baseitem_id({ ItemKindType::SHOT, m_bonus(1, player_ptr->lev) + 1 }));
131         q_ptr->number = (byte)rand_range(15, 30);
132         object_aware(player_ptr, q_ptr);
133         object_known(q_ptr);
134         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
135         q_ptr->discount = 99;
136         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
137         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
138         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
139         if (slot >= 0) {
140             autopick_alter_item(player_ptr, slot, false);
141         }
142
143         cave_alter_feat(player_ptr, y, x, TerrainCharacteristics::HURT_ROCK);
144         RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::FLOW);
145         return true;
146     }
147     case AMMO_ARROW: {
148         concptr q = _("どのアイテムから作りますか? ", "Convert which item? ");
149         concptr s = _("材料を持っていない。", "You have no item to convert.");
150         OBJECT_IDX item;
151         auto *q_ptr = choose_object(player_ptr, &item, q, s, USE_INVEN | USE_FLOOR, FuncItemTester(&ItemEntity::is_convertible));
152         if (!q_ptr) {
153             return false;
154         }
155
156         ItemEntity forge;
157         q_ptr = &forge;
158         q_ptr->prep(lookup_baseitem_id({ ItemKindType::ARROW, static_cast<short>(m_bonus(1, player_ptr->lev) + 1) }));
159         q_ptr->number = (byte)rand_range(5, 10);
160         object_aware(player_ptr, q_ptr);
161         object_known(q_ptr);
162         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
163         q_ptr->discount = 99;
164         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
165         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
166         vary_item(player_ptr, item, -1);
167         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
168         if (slot >= 0) {
169             autopick_alter_item(player_ptr, slot, false);
170         }
171
172         return true;
173     }
174     case AMMO_BOLT: {
175         concptr q = _("どのアイテムから作りますか? ", "Convert which item? ");
176         concptr s = _("材料を持っていない。", "You have no item to convert.");
177         OBJECT_IDX item;
178         auto *q_ptr = choose_object(player_ptr, &item, q, s, (USE_INVEN | USE_FLOOR), FuncItemTester(&ItemEntity::is_convertible));
179         if (!q_ptr) {
180             return false;
181         }
182
183         ItemEntity forge;
184         q_ptr = &forge;
185         q_ptr->prep(lookup_baseitem_id({ ItemKindType::BOLT, static_cast<short>(m_bonus(1, player_ptr->lev) + 1) }));
186         q_ptr->number = (byte)rand_range(4, 8);
187         object_aware(player_ptr, q_ptr);
188         object_known(q_ptr);
189         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
190         q_ptr->discount = 99;
191         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
192         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
193         vary_item(player_ptr, item, -1);
194         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
195         if (slot >= 0) {
196             autopick_alter_item(player_ptr, slot, false);
197         }
198
199         return true;
200     }
201     default:
202         return true;
203     }
204 }