OSDN Git Service

Merge branch 'master' of https://github.com/hengband/hengband
[hengbandforosx/hengbandosx.git] / src / target / target-getter.cpp
1 #include "target/target-getter.h"
2 #include "core/asking-player.h"
3 #include "effect/spells-effect-util.h"
4 #include "floor/geometry.h"
5 #include "game-option/input-options.h"
6 #include "io/command-repeater.h"
7 #include "io/input-key-requester.h"
8 #include "main/sound-of-music.h"
9 #include "monster/monster-describer.h"
10 #include "monster/monster-status.h"
11 #include "system/floor-type-definition.h"
12 #include "system/monster-entity.h"
13 #include "system/monster-race-info.h"
14 #include "system/player-type-definition.h"
15 #include "target/target-checker.h"
16 #include "target/target-setter.h"
17 #include "target/target-types.h"
18 #include "timed-effect/timed-effects.h"
19 #include "util/finalizer.h"
20 #include "view/display-messages.h"
21 #include <string>
22
23 /*
24  * Get an "aiming direction" from the user.
25  *
26  * The "dir" is loaded with 1,2,3,4,6,7,8,9 for "actual direction", and
27  * "0" for "current target", and "-1" for "entry aborted".
28  *
29  * Note that "Force Target", if set, will pre-empt user interaction,
30  * if there is a usable target already set.
31  *
32  * Note that confusion over-rides any (explicit?) user choice.
33  */
34 bool get_aim_dir(PlayerType *player_ptr, int *dp)
35 {
36     auto dir = command_dir;
37     if (use_old_target && target_okay(player_ptr)) {
38         dir = 5;
39     }
40
41     short code;
42     if (repeat_pull(&code)) {
43         if (!(code == 5 && !target_okay(player_ptr))) {
44             dir = code;
45         }
46     }
47
48     *dp = code;
49     while (dir == 0) {
50         std::string prompt;
51         if (!target_okay(player_ptr)) {
52             prompt = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
53         } else {
54             prompt = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
55         }
56
57         const auto command_opt = input_command(prompt, true);
58         if (!command_opt) {
59             break;
60         }
61
62         auto command = *command_opt;
63         if (use_menu && (command == '\r')) {
64             command = 't';
65         }
66
67         switch (command) {
68         case 'T':
69         case 't':
70         case '.':
71         case '5':
72         case '0':
73             dir = 5;
74             break;
75         case '*':
76         case ' ':
77         case '\r':
78             if (target_set(player_ptr, TARGET_KILL)) {
79                 dir = 5;
80             }
81
82             break;
83         default:
84             dir = get_keymap_dir(command);
85             break;
86         }
87
88         if ((dir == 5) && !target_okay(player_ptr)) {
89             dir = 0;
90         }
91
92         if (dir == 0) {
93             bell();
94         }
95     }
96
97     if (dir == 0) {
98         project_length = 0;
99         return false;
100     }
101
102     command_dir = dir;
103     if (player_ptr->effects()->confusion().is_confused()) {
104         dir = ddd[randint0(8)];
105     }
106
107     if (command_dir != dir) {
108         msg_print(_("あなたは混乱している。", "You are confused."));
109     }
110
111     *dp = dir;
112     repeat_push((COMMAND_CODE)command_dir);
113     return true;
114 }
115
116 /*!
117  * @brief 方向を指定する(テンキー配列順)
118  *
119  * 上下左右および斜め方向を指定する。
120  * 指定された方向は整数で返され、それぞれの値方向は以下の通り。
121  *
122  * 7 8 9
123  *  \|/
124  * 4-@-6
125  *  /|\
126  * 1 2 3
127  *
128  * @return 指定した方向。指定をキャンセルした場合はstd::nullopt。
129  */
130 std::optional<int> get_direction(PlayerType *player_ptr)
131 {
132     auto dir = command_dir;
133     short code;
134     if (repeat_pull(&code)) {
135         dir = code;
136     }
137
138     constexpr auto prompt = _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
139     while (dir == 0) {
140         const auto command = input_command(prompt, true);
141         if (!command) {
142             return std::nullopt;
143         }
144
145         dir = get_keymap_dir(*command);
146         if (dir == 0) {
147             bell();
148         }
149     }
150
151     command_dir = dir;
152     const auto finalizer = util::make_finalizer([] {
153         repeat_push(static_cast<short>(command_dir));
154     });
155     const auto is_confused = player_ptr->effects()->confusion().is_confused();
156     if (is_confused && evaluate_percent(75)) {
157         dir = ddd[randint0(8)];
158     }
159
160     if (command_dir == dir) {
161         return dir;
162     }
163
164     if (is_confused) {
165         msg_print(_("あなたは混乱している。", "You are confused."));
166         return dir;
167     }
168
169     const auto &monster = player_ptr->current_floor_ptr->m_list[player_ptr->riding];
170     const auto m_name = monster_desc(player_ptr, &monster, 0);
171     const auto fmt = monster.is_confused()
172                          ? _("%sは混乱している。", "%s^ is confused.")
173                          : _("%sは思い通りに動いてくれない。", "You cannot control %s.");
174     msg_format(fmt, m_name.data());
175     return dir;
176 }
177
178 /*!
179  * @brief 方向を指定する(円周順)
180  *
181  * 上下左右および斜め方向を指定する。
182  * 指定された方向は整数で返され、それぞれの値方向は以下の通り。
183  *
184  * 5 4 3
185  *  \|/
186  * 6-@-2
187  *  /|\
188  * 7 0 1
189  *
190  * @return 指定した方向。指定をキャンセルした場合はstd::nullopt。
191  */
192 std::optional<int> get_direction_as_cdir(PlayerType *player_ptr)
193 {
194     constexpr std::array<int, 10> cdirs = { { 0, 7, 0, 1, 6, 0, 2, 5, 4, 3 } };
195
196     const auto dir = get_direction(player_ptr);
197     if (!dir || (dir <= 0) || (dir == 5) || (dir >= std::ssize(cdirs))) {
198         return std::nullopt;
199     }
200
201     return cdirs[*dir];
202 }
203
204 /*
205  * @brief 進行方向を指定する(騎乗対象の混乱の影響を受ける) / Request a "movement" direction (1,2,3,4,6,7,8,9) from the user,
206  * and place it into "command_dir", unless we already have one.
207  *
208  * This function should be used for all "repeatable" commands, such as
209  * run, walk, open, close, bash, disarm, spike, tunnel, etc, as well
210  * as all commands which must reference a grid adjacent to the player,
211  * and which may not reference the grid under the player.  Note that,
212  * for example, it is no longer possible to "disarm" or "open" chests
213  * in the same grid as the player.
214  *
215  * Direction "5" is illegal and will (cleanly) abort the command.
216  *
217  * This function tracks and uses the "global direction", and uses
218  * that as the "desired direction", to which "confusion" is applied.
219  */
220 bool get_rep_dir(PlayerType *player_ptr, int *dp, bool under)
221 {
222     auto dir = command_dir;
223     short code;
224     if (repeat_pull(&code)) {
225         dir = code;
226     }
227
228     *dp = code;
229     const auto prompt = under ? _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ")
230                               : _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
231     while (dir == 0) {
232         const auto command = input_command(prompt, true);
233         if (!command) {
234             return false;
235         }
236
237         if (under && ((command == '5') || (command == '-') || (command == '.'))) {
238             dir = 5;
239             break;
240         }
241
242         dir = get_keymap_dir(*command);
243         if (dir == 0) {
244             bell();
245         }
246     }
247
248     if ((dir == 5) && !under) {
249         return false;
250     }
251
252     command_dir = dir;
253     auto is_confused = player_ptr->effects()->confusion().is_confused();
254     if (is_confused) {
255         if (evaluate_percent(75)) {
256             dir = ddd[randint0(8)];
257         }
258     } else if (player_ptr->riding) {
259         auto *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
260         auto *r_ptr = &m_ptr->get_monrace();
261         if (m_ptr->is_confused()) {
262             if (evaluate_percent(75)) {
263                 dir = ddd[randint0(8)];
264             }
265         } else if (r_ptr->behavior_flags.has_all_of({ MonsterBehaviorType::RAND_MOVE_50, MonsterBehaviorType::RAND_MOVE_25 }) && one_in_(2)) {
266             dir = ddd[randint0(8)];
267         } else if (r_ptr->behavior_flags.has(MonsterBehaviorType::RAND_MOVE_50) && one_in_(4)) {
268             dir = ddd[randint0(8)];
269         }
270     }
271
272     if (command_dir != dir) {
273         if (is_confused) {
274             msg_print(_("あなたは混乱している。", "You are confused."));
275         } else {
276             auto *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
277             const auto m_name = monster_desc(player_ptr, m_ptr, 0);
278             if (m_ptr->is_confused()) {
279                 msg_format(_("%sは混乱している。", "%s^ is confused."), m_name.data());
280             } else {
281                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name.data());
282             }
283         }
284     }
285
286     *dp = dir;
287     repeat_push(static_cast<short>(command_dir));
288     return true;
289 }