OSDN Git Service

Merge pull request #2122 from sikabane-works/release/3.0.0Alpha52
[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-race/monster-race.h"
10 #include "monster-race/race-flags1.h"
11 #include "monster/monster-describer.h"
12 #include "monster/monster-status.h"
13 #include "system/floor-type-definition.h"
14 #include "system/monster-race-definition.h"
15 #include "system/monster-type-definition.h"
16 #include "system/player-type-definition.h"
17 #include "target/target-checker.h"
18 #include "target/target-setter.h"
19 #include "target/target-types.h"
20 #include "view/display-messages.h"
21
22 /*
23  * Get an "aiming direction" from the user.
24  *
25  * The "dir" is loaded with 1,2,3,4,6,7,8,9 for "actual direction", and
26  * "0" for "current target", and "-1" for "entry aborted".
27  *
28  * Note that "Force Target", if set, will pre-empt user interaction,
29  * if there is a usable target already set.
30  *
31  * Note that confusion over-rides any (explicit?) user choice.
32  */
33 bool get_aim_dir(PlayerType *player_ptr, DIRECTION *dp)
34 {
35     DIRECTION dir = command_dir;
36     if (use_old_target && target_okay(player_ptr))
37         dir = 5;
38
39     COMMAND_CODE code;
40     if (repeat_pull(&code))
41         if (!(code == 5 && !target_okay(player_ptr)))
42             dir = (DIRECTION)code;
43
44     *dp = (DIRECTION)code;
45     char command;
46     while (!dir) {
47         concptr p;
48         if (!target_okay(player_ptr))
49             p = _("方向 ('*'でターゲット選択, ESCで中断)? ", "Direction ('*' to choose a target, Escape to cancel)? ");
50         else
51             p = _("方向 ('5'でターゲットへ, '*'でターゲット再選択, ESCで中断)? ", "Direction ('5' for target, '*' to re-target, Escape to cancel)? ");
52
53         if (!get_com(p, &command, true))
54             break;
55
56         if (use_menu && (command == '\r'))
57             command = 't';
58
59         switch (command) {
60         case 'T':
61         case 't':
62         case '.':
63         case '5':
64         case '0':
65             dir = 5;
66             break;
67         case '*':
68         case ' ':
69         case '\r':
70             if (target_set(player_ptr, TARGET_KILL))
71                 dir = 5;
72
73             break;
74         default:
75             dir = get_keymap_dir(command);
76             break;
77         }
78
79         if ((dir == 5) && !target_okay(player_ptr))
80             dir = 0;
81
82         if (!dir)
83             bell();
84     }
85
86     if (!dir) {
87         project_length = 0;
88         return false;
89     }
90
91     command_dir = dir;
92     if (player_ptr->confused)
93         dir = ddd[randint0(8)];
94
95     if (command_dir != dir)
96         msg_print(_("あなたは混乱している。", "You are confused."));
97
98     *dp = dir;
99     repeat_push((COMMAND_CODE)command_dir);
100     return true;
101 }
102
103 bool get_direction(PlayerType *player_ptr, DIRECTION *dp, bool allow_under, bool with_steed)
104 {
105     DIRECTION dir = command_dir;
106     COMMAND_CODE code;
107     if (repeat_pull(&code))
108         dir = (DIRECTION)code;
109
110     *dp = (DIRECTION)code;
111     concptr prompt = allow_under ? _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ")
112                                  : _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
113
114     while (!dir) {
115         char ch;
116         if (!get_com(prompt, &ch, true))
117             break;
118
119         if ((allow_under) && ((ch == '5') || (ch == '-') || (ch == '.'))) {
120             dir = 5;
121             continue;
122         }
123
124         dir = get_keymap_dir(ch);
125         if (!dir)
126             bell();
127     }
128
129     if ((dir == 5) && (!allow_under))
130         dir = 0;
131
132     if (!dir)
133         return false;
134
135     command_dir = dir;
136     if (player_ptr->confused) {
137         if (randint0(100) < 75) {
138             dir = ddd[randint0(8)];
139         }
140     } else if (player_ptr->riding && with_steed) {
141         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
142         monster_race *r_ptr = &r_info[m_ptr->r_idx];
143         if (monster_confused_remaining(m_ptr)) {
144             if (randint0(100) < 75)
145                 dir = ddd[randint0(8)];
146         } else if (r_ptr->behavior_flags.has(MonsterBehaviorType::RAND_MOVE_50) && r_ptr->behavior_flags.has(MonsterBehaviorType::RAND_MOVE_25) && (randint0(100) < 50))
147             dir = ddd[randint0(8)];
148         else if (r_ptr->behavior_flags.has(MonsterBehaviorType::RAND_MOVE_50) && (randint0(100) < 25))
149             dir = ddd[randint0(8)];
150     }
151
152     if (command_dir != dir) {
153         if (player_ptr->confused) {
154             msg_print(_("あなたは混乱している。", "You are confused."));
155         } else {
156             GAME_TEXT m_name[MAX_NLEN];
157             monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
158
159             monster_desc(player_ptr, m_name, m_ptr, 0);
160             if (monster_confused_remaining(m_ptr)) {
161                 msg_format(_("%sは混乱している。", "%^s is confused."), m_name);
162             } else {
163                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
164             }
165         }
166     }
167
168     *dp = dir;
169     repeat_push((COMMAND_CODE)command_dir);
170     return true;
171 }
172
173 /*
174  * @brief 進行方向を指定する(騎乗対象の混乱の影響を受ける) / Request a "movement" direction (1,2,3,4,6,7,8,9) from the user,
175  * and place it into "command_dir", unless we already have one.
176  *
177  * This function should be used for all "repeatable" commands, such as
178  * run, walk, open, close, bash, disarm, spike, tunnel, etc, as well
179  * as all commands which must reference a grid adjacent to the player,
180  * and which may not reference the grid under the player.  Note that,
181  * for example, it is no longer possible to "disarm" or "open" chests
182  * in the same grid as the player.
183  *
184  * Direction "5" is illegal and will (cleanly) abort the command.
185  *
186  * This function tracks and uses the "global direction", and uses
187  * that as the "desired direction", to which "confusion" is applied.
188  */
189 bool get_rep_dir(PlayerType *player_ptr, DIRECTION *dp, bool under)
190 {
191     DIRECTION dir = command_dir;
192     COMMAND_CODE code;
193     if (repeat_pull(&code))
194         dir = (DIRECTION)code;
195
196     *dp = (DIRECTION)code;
197     concptr prompt = under ? _("方向 ('.'足元, ESCで中断)? ", "Direction ('.' at feet, Escape to cancel)? ") : _("方向 (ESCで中断)? ", "Direction (Escape to cancel)? ");
198     while (!dir) {
199         char ch;
200         if (!get_com(prompt, &ch, true))
201             break;
202
203         if ((under) && ((ch == '5') || (ch == '-') || (ch == '.'))) {
204             dir = 5;
205             continue;
206         }
207
208         dir = get_keymap_dir(ch);
209         if (!dir)
210             bell();
211     }
212
213     if ((dir == 5) && (!under))
214         dir = 0;
215
216     if (!dir)
217         return false;
218
219     command_dir = dir;
220     if (player_ptr->confused) {
221         if (randint0(100) < 75)
222             dir = ddd[randint0(8)];
223     } else if (player_ptr->riding) {
224         monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
225         monster_race *r_ptr = &r_info[m_ptr->r_idx];
226         if (monster_confused_remaining(m_ptr)) {
227             if (randint0(100) < 75)
228                 dir = ddd[randint0(8)];
229         } else if (r_ptr->behavior_flags.has_all_of({ MonsterBehaviorType::RAND_MOVE_50, MonsterBehaviorType::RAND_MOVE_25 }) && (randint0(100) < 50))
230             dir = ddd[randint0(8)];
231         else if (r_ptr->behavior_flags.has(MonsterBehaviorType::RAND_MOVE_50) && (randint0(100) < 25))
232             dir = ddd[randint0(8)];
233     }
234
235     if (command_dir != dir) {
236         if (player_ptr->confused) {
237             msg_print(_("あなたは混乱している。", "You are confused."));
238         } else {
239             GAME_TEXT m_name[MAX_NLEN];
240             monster_type *m_ptr = &player_ptr->current_floor_ptr->m_list[player_ptr->riding];
241             monster_desc(player_ptr, m_name, m_ptr, 0);
242             if (monster_confused_remaining(m_ptr))
243                 msg_format(_("%sは混乱している。", "%^s is confused."), m_name);
244             else
245                 msg_format(_("%sは思い通りに動いてくれない。", "You cannot control %s."), m_name);
246         }
247     }
248
249     *dp = dir;
250     repeat_push((COMMAND_CODE)command_dir);
251     return true;
252 }