OSDN Git Service

Merge pull request #3915 from Hourier/Replace-GetNeighbor
[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 ch = input_command(prompt, true);
66         if (!ch) {
67             return false;
68         }
69
70         if (ch == 'S' || ch == 's') {
71             type = AMMO_SHOT;
72             break;
73         }
74
75         if ((ch == 'A' || ch == 'a') && (plev >= 10)) {
76             type = AMMO_ARROW;
77             break;
78         }
79
80         if ((ch == 'B' || ch == 'b') && (plev >= 20)) {
81             type = AMMO_BOLT;
82             break;
83         }
84     }
85
86     repeat_push(static_cast<COMMAND_CODE>(type));
87     return true;
88 }
89
90 /*!
91  * @brief「弾/矢の製造」処理 / do_cmd_cast calls this function if the player's class is 'archer'.
92  * Hook to determine if an object is contertible in an arrow/bolt
93  * @return 製造を実際に行ったらTRUE、キャンセルしたらFALSEを返す
94  */
95 bool create_ammo(PlayerType *player_ptr)
96 {
97     if (cmd_limit_confused(player_ptr) || cmd_limit_blind(player_ptr)) {
98         return false;
99     }
100
101     ammo_creation_type ext = AMMO_NONE;
102
103     if (!select_ammo_creation_type(ext, player_ptr->lev)) {
104         return false;
105     }
106
107     switch (ext) {
108     case AMMO_SHOT: {
109         int dir;
110         if (!get_rep_dir(player_ptr, &dir)) {
111             return false;
112         }
113
114         const auto pos = player_ptr->get_neighbor(dir);
115         const auto &grid = player_ptr->current_floor_ptr->get_grid(pos);
116         if (grid.get_terrain_mimic().flags.has_not(TerrainCharacteristics::CAN_DIG)) {
117             msg_print(_("そこには岩石がない。", "You need a pile of rubble."));
118             return false;
119         }
120
121         if (!grid.cave_has_flag(TerrainCharacteristics::CAN_DIG) || !grid.cave_has_flag(TerrainCharacteristics::HURT_ROCK)) {
122             msg_print(_("硬すぎて崩せなかった。", "You failed to make ammo."));
123             return true;
124         }
125
126         ItemEntity forge;
127         auto *q_ptr = &forge;
128         q_ptr->prep(lookup_baseitem_id({ ItemKindType::SHOT, m_bonus(1, player_ptr->lev) + 1 }));
129         q_ptr->number = (byte)rand_range(15, 30);
130         object_aware(player_ptr, q_ptr);
131         q_ptr->mark_as_known();
132         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
133         q_ptr->discount = 99;
134         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
135         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
136         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
137         if (slot >= 0) {
138             autopick_alter_item(player_ptr, slot, false);
139         }
140
141         cave_alter_feat(player_ptr, pos.y, pos.x, TerrainCharacteristics::HURT_ROCK);
142         RedrawingFlagsUpdater::get_instance().set_flag(StatusRecalculatingFlag::FLOW);
143         return true;
144     }
145     case AMMO_ARROW: {
146         constexpr auto q = _("どのアイテムから作りますか? ", "Convert which item? ");
147         constexpr auto s = _("材料を持っていない。", "You have no item to convert.");
148         short i_idx;
149         auto *q_ptr = choose_object(player_ptr, &i_idx, q, s, USE_INVEN | USE_FLOOR, FuncItemTester(&ItemEntity::is_convertible));
150         if (!q_ptr) {
151             return false;
152         }
153
154         ItemEntity forge;
155         q_ptr = &forge;
156         q_ptr->prep(lookup_baseitem_id({ ItemKindType::ARROW, static_cast<short>(m_bonus(1, player_ptr->lev) + 1) }));
157         q_ptr->number = (byte)rand_range(5, 10);
158         object_aware(player_ptr, q_ptr);
159         q_ptr->mark_as_known();
160         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
161         q_ptr->discount = 99;
162         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
163         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
164         vary_item(player_ptr, i_idx, -1);
165         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
166         if (slot >= 0) {
167             autopick_alter_item(player_ptr, slot, false);
168         }
169
170         return true;
171     }
172     case AMMO_BOLT: {
173         constexpr auto q = _("どのアイテムから作りますか? ", "Convert which item? ");
174         constexpr auto s = _("材料を持っていない。", "You have no item to convert.");
175         short i_idx;
176         auto *q_ptr = choose_object(player_ptr, &i_idx, q, s, (USE_INVEN | USE_FLOOR), FuncItemTester(&ItemEntity::is_convertible));
177         if (!q_ptr) {
178             return false;
179         }
180
181         ItemEntity forge;
182         q_ptr = &forge;
183         q_ptr->prep(lookup_baseitem_id({ ItemKindType::BOLT, static_cast<short>(m_bonus(1, player_ptr->lev) + 1) }));
184         q_ptr->number = (byte)rand_range(4, 8);
185         object_aware(player_ptr, q_ptr);
186         q_ptr->mark_as_known();
187         ItemMagicApplier(player_ptr, q_ptr, player_ptr->lev, AM_NO_FIXED_ART).execute();
188         q_ptr->discount = 99;
189         const auto item_name = describe_flavor(player_ptr, q_ptr, 0);
190         msg_print(_(format("%sを作った。", item_name.data()), "You make some ammo."));
191         vary_item(player_ptr, i_idx, -1);
192         int16_t slot = store_item_to_inventory(player_ptr, q_ptr);
193         if (slot >= 0) {
194             autopick_alter_item(player_ptr, slot, false);
195         }
196
197         return true;
198     }
199     default:
200         return true;
201     }
202 }