OSDN Git Service

Merge remote-tracking branch 'remotes/origin/feature/Fix-Compile-on-Linux' into develop
[hengband/hengband.git] / src / monster-floor / monster-move.c
1 /*!
2  * @brief モンスターの移動に関する処理
3  * @date 2020/03/08
4  * @author Hourier
5  */
6
7 #include "monster-floor/monster-move.h"
8 #include "core/disturbance.h"
9 #include "core/player-update-types.h"
10 #include "core/speed-table.h"
11 #include "core/window-redrawer.h"
12 #include "effect/effect-characteristics.h"
13 #include "effect/effect-processor.h"
14 #include "floor/cave.h"
15 #include "game-option/disturbance-options.h"
16 #include "grid/feature.h"
17 #include "io/files-util.h"
18 #include "monster-attack/monster-attack-processor.h"
19 #include "monster-floor/monster-object.h"
20 #include "monster-race/monster-race.h"
21 #include "monster-race/race-flags1.h"
22 #include "monster-race/race-flags2.h"
23 #include "monster-race/race-flags7.h"
24 #include "monster-race/race-flags8.h"
25 #include "monster-race/race-indice-types.h"
26 #include "monster/monster-describer.h"
27 #include "monster/monster-flag-types.h"
28 #include "monster/monster-info.h"
29 #include "monster/monster-status.h"
30 #include "monster/monster-update.h"
31 #include "pet/pet-util.h"
32 #include "player/player-status-flags.h"
33 #include "spell/spell-types.h"
34 #include "system/floor-type-definition.h"
35 #include "target/projection-path-calculator.h"
36 #include "util/bit-flags-calculator.h"
37 #include "view/display-messages.h"
38
39 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
40 {
41     return !has_flag(f_ptr->flags, FF_GLASS) || (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) || (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
42 }
43
44 /*!
45  * @brief モンスターによる壁の透過・破壊を行う
46  * @param target_ptr プレーヤーへの参照ポインタ
47  * @param m_ptr モンスターへの参照ポインタ
48  * @param ny モンスターのY座標
49  * @param nx モンスターのX座標
50  * @param can_cross モンスターが地形を踏破できるならばTRUE
51  * @return 透過も破壊もしなかった場合はFALSE、それ以外はTRUE
52  */
53 static bool process_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx, bool can_cross)
54 {
55     monster_race *r_ptr = &r_info[m_ptr->r_idx];
56     grid_type *g_ptr;
57     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
58     feature_type *f_ptr;
59     f_ptr = &f_info[g_ptr->feat];
60     if (player_bold(target_ptr, ny, nx)) {
61         turn_flags_ptr->do_move = TRUE;
62         return TRUE;
63     }
64
65     if (g_ptr->m_idx > 0) {
66         turn_flags_ptr->do_move = TRUE;
67         return TRUE;
68     }
69
70     if (((r_ptr->flags2 & RF2_KILL_WALL) != 0) && (can_cross ? !has_flag(f_ptr->flags, FF_LOS) : !turn_flags_ptr->is_riding_mon)
71         && has_flag(f_ptr->flags, FF_HURT_DISI) && !has_flag(f_ptr->flags, FF_PERMANENT) && check_hp_for_feat_destruction(f_ptr, m_ptr)) {
72         turn_flags_ptr->do_move = TRUE;
73         if (!can_cross)
74             turn_flags_ptr->must_alter_to_move = TRUE;
75
76         turn_flags_ptr->did_kill_wall = TRUE;
77         return TRUE;
78     }
79
80     if (!can_cross)
81         return FALSE;
82
83     turn_flags_ptr->do_move = TRUE;
84     if (((r_ptr->flags2 & RF2_PASS_WALL) != 0) && (!turn_flags_ptr->is_riding_mon || has_pass_wall(target_ptr)) && has_flag(f_ptr->flags, FF_CAN_PASS)) {
85         turn_flags_ptr->did_pass_wall = TRUE;
86     }
87
88     return TRUE;
89 }
90
91 /*!
92  * @brief モンスターが普通のドアを開ける処理
93  * @param target_ptr プレーヤーへの参照ポインタ
94  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
95  * @param m_ptr モンスターへの参照ポインタ
96  * @param ny モンスターのY座標
97  * @param nx モンスターのX座標
98  * @return ここではドアを開けず、ガラスのドアを開ける可能性があるならTRUE
99  */
100 static bool bash_normal_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
101 {
102     monster_race *r_ptr = &r_info[m_ptr->r_idx];
103     grid_type *g_ptr;
104     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
105     feature_type *f_ptr;
106     f_ptr = &f_info[g_ptr->feat];
107     turn_flags_ptr->do_move = FALSE;
108     if (((r_ptr->flags2 & RF2_OPEN_DOOR) == 0) || !has_flag(f_ptr->flags, FF_OPEN) || (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
109         return TRUE;
110
111     if (f_ptr->power == 0) {
112         turn_flags_ptr->did_open_door = TRUE;
113         turn_flags_ptr->do_turn = TRUE;
114         return FALSE;
115     }
116
117     if (randint0(m_ptr->hp / 10) > f_ptr->power) {
118         cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
119         turn_flags_ptr->do_turn = TRUE;
120         return FALSE;
121     }
122
123     return TRUE;
124 }
125
126 /*!
127  * @brief モンスターがガラスのドアを開ける処理
128  * @param target_ptr プレーヤーへの参照ポインタ
129  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
130  * @param m_ptr モンスターへの参照ポインタ
131  * @param g_ptr グリッドへの参照ポインタ
132  * @param f_ptr 地形への参照ポインタ
133  * @return なし
134  */
135 static void bash_glass_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, feature_type *f_ptr, bool may_bash)
136 {
137     monster_race *r_ptr = &r_info[m_ptr->r_idx];
138     if (!may_bash || ((r_ptr->flags2 & RF2_BASH_DOOR) == 0) || !has_flag(f_ptr->flags, FF_BASH)
139         || (is_pet(m_ptr) && ((target_ptr->pet_extra_flags & PF_OPEN_DOORS) == 0)))
140         return;
141
142     if (!check_hp_for_feat_destruction(f_ptr, m_ptr) || (randint0(m_ptr->hp / 10) <= f_ptr->power))
143         return;
144
145     if (has_flag(f_ptr->flags, FF_GLASS))
146         msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
147     else
148         msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
149
150     if (disturb_minor)
151         disturb(target_ptr, FALSE, FALSE);
152
153     turn_flags_ptr->did_bash_door = TRUE;
154     turn_flags_ptr->do_move = TRUE;
155     turn_flags_ptr->must_alter_to_move = TRUE;
156 }
157
158 /*!
159  * @brief モンスターによるドアの開放・破壊を行う
160  * @param target_ptr プレーヤーへの参照ポインタ
161  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
162  * @param m_ptr モンスターへの参照ポインタ
163  * @param ny モンスターのY座標
164  * @param nx モンスターのX座標
165  * @return モンスターが死亡した場合のみFALSE
166  */
167 static bool process_door(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
168 {
169     monster_race *r_ptr = &r_info[m_ptr->r_idx];
170     grid_type *g_ptr;
171     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
172     if (!is_closed_door(target_ptr, g_ptr->feat))
173         return TRUE;
174
175     feature_type *f_ptr;
176     f_ptr = &f_info[g_ptr->feat];
177     bool may_bash = bash_normal_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx);
178     bash_glass_door(target_ptr, turn_flags_ptr, m_ptr, f_ptr, may_bash);
179
180     if (!turn_flags_ptr->did_open_door && !turn_flags_ptr->did_bash_door)
181         return TRUE;
182
183     if (turn_flags_ptr->did_bash_door
184         && ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || has_flag(f_ptr->flags, FF_GLASS))) {
185         cave_alter_feat(target_ptr, ny, nx, FF_BASH);
186         if (!monster_is_valid(m_ptr)) {
187             target_ptr->update |= (PU_FLOW);
188             target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
189             if (is_original_ap_and_seen(target_ptr, m_ptr))
190                 r_ptr->r_flags2 |= (RF2_BASH_DOOR);
191
192             return FALSE;
193         }
194     } else {
195         cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
196     }
197
198     f_ptr = &f_info[g_ptr->feat];
199     turn_flags_ptr->do_view = TRUE;
200     return TRUE;
201 }
202
203 /*!
204  * @brief 守りのルーンによるモンスターの移動制限を処理する
205  * @param target_ptr プレーヤーへの参照ポインタ
206  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
207  * @param m_ptr モンスターへの参照ポインタ
208  * @param ny モンスターのY座標
209  * @param nx モンスターのX座標
210  * @return ルーンのある/なし
211  */
212 static bool process_protection_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
213 {
214     grid_type *g_ptr;
215     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
216     monster_race *r_ptr = &r_info[m_ptr->r_idx];
217     if (!turn_flags_ptr->do_move || !is_glyph_grid(g_ptr) || (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
218         return FALSE;
219
220     turn_flags_ptr->do_move = FALSE;
221     if (is_pet(m_ptr) || (randint1(BREAK_GLYPH) >= r_ptr->level))
222         return TRUE;
223
224     if (g_ptr->info & CAVE_MARK) {
225         msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
226     }
227
228     g_ptr->info &= ~(CAVE_MARK);
229     g_ptr->info &= ~(CAVE_OBJECT);
230     g_ptr->mimic = 0;
231     turn_flags_ptr->do_move = TRUE;
232     note_spot(target_ptr, ny, nx);
233     return TRUE;
234 }
235
236 /*!
237  * @brief 爆発のルーンを処理する
238  * @param target_ptr プレーヤーへの参照ポインタ
239  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
240  * @param m_ptr モンスターへの参照ポインタ
241  * @param ny モンスターのY座標
242  * @param nx モンスターのX座標
243  * @return モンスターが死亡した場合のみFALSE
244  */
245 static bool process_explosive_rune(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
246 {
247     grid_type *g_ptr;
248     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
249     monster_race *r_ptr = &r_info[m_ptr->r_idx];
250     if (!turn_flags_ptr->do_move || !is_explosive_rune_grid(g_ptr) || (((r_ptr->flags1 & RF1_NEVER_BLOW) != 0) && player_bold(target_ptr, ny, nx)))
251         return TRUE;
252
253     turn_flags_ptr->do_move = FALSE;
254     if (is_pet(m_ptr))
255         return TRUE;
256
257     if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level) {
258         if (g_ptr->info & CAVE_MARK) {
259             msg_print(_("ルーンが爆発した!", "The rune explodes!"));
260             BIT_FLAGS project_flags = PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI;
261             project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, project_flags, -1);
262         }
263     } else {
264         msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
265     }
266
267     g_ptr->info &= ~(CAVE_MARK);
268     g_ptr->info &= ~(CAVE_OBJECT);
269     g_ptr->mimic = 0;
270
271     note_spot(target_ptr, ny, nx);
272     lite_spot(target_ptr, ny, nx);
273
274     if (!monster_is_valid(m_ptr))
275         return FALSE;
276
277     turn_flags_ptr->do_move = TRUE;
278     return TRUE;
279 }
280
281 /*!
282  * @brief モンスターが壁を掘った後続処理を実行する
283  * @param target_ptr プレーヤーへの参照ポインタ
284  * @turn_flags_ptr ターン経過処理フラグへの参照ポインタ
285  * @param m_ptr モンスターへの参照ポインタ
286  * @param ny モンスターのY座標
287  * @param nx モンスターのX座標
288  * @return モンスターが死亡した場合のみFALSE
289  */
290 static bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx)
291 {
292     monster_race *r_ptr = &r_info[m_ptr->r_idx];
293     grid_type *g_ptr;
294     g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
295     feature_type *f_ptr;
296     f_ptr = &f_info[g_ptr->feat];
297     if (!turn_flags_ptr->did_kill_wall || !turn_flags_ptr->do_move)
298         return TRUE;
299
300     if (one_in_(GRINDNOISE)) {
301         if (has_flag(f_ptr->flags, FF_GLASS))
302             msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
303         else
304             msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
305     }
306
307     cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
308
309     if (!monster_is_valid(m_ptr)) {
310         target_ptr->update |= (PU_FLOW);
311         target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
312         if (is_original_ap_and_seen(target_ptr, m_ptr))
313             r_ptr->r_flags2 |= (RF2_KILL_WALL);
314
315         return FALSE;
316     }
317
318     f_ptr = &f_info[g_ptr->feat];
319     turn_flags_ptr->do_view = TRUE;
320     turn_flags_ptr->do_turn = TRUE;
321     return TRUE;
322 }
323
324 /*!
325  * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
326  * @brief モンスターの移動に関するメインルーチン
327  * @param target_ptr プレーヤーへの参照ポインタ
328  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
329  * @param m_idx モンスターID
330  * @param mm モンスターの移動方向
331  * @param oy 移動前の、モンスターのY座標
332  * @param ox 移動前の、モンスターのX座標
333  * @param count 移動回数 (のはず todo)
334  * @return 移動が阻害される何か (ドア等)があったらFALSE
335  */
336 bool process_monster_movement(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, DIRECTION *mm, POSITION oy, POSITION ox, int *count)
337 {
338     for (int i = 0; mm[i]; i++) {
339         int d = mm[i];
340         if (d == 5)
341             d = ddd[randint0(8)];
342
343         POSITION ny = oy + ddy[d];
344         POSITION nx = ox + ddx[d];
345         if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx))
346             continue;
347
348         grid_type *g_ptr;
349         g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
350         monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
351         monster_race *r_ptr = &r_info[m_ptr->r_idx];
352         bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
353
354         if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross)) {
355             if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
356                 return FALSE;
357         }
358
359         if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) {
360             if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
361                 return FALSE;
362         }
363
364         exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
365         if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross))
366             return FALSE;
367
368         if (turn_flags_ptr->is_riding_mon) {
369             if (!target_ptr->riding_ryoute && !monster_fear_remaining(&target_ptr->current_floor_ptr->m_list[target_ptr->riding]))
370                 turn_flags_ptr->do_move = FALSE;
371         }
372
373         if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
374             return FALSE;
375
376         if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC)) {
377             if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
378                 turn_flags_ptr->do_move = FALSE;
379         }
380
381         if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
382             turn_flags_ptr->do_move = FALSE;
383
384         if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE)) {
385             if (is_original_ap_and_seen(target_ptr, m_ptr))
386                 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
387
388             turn_flags_ptr->do_move = FALSE;
389         }
390
391         if (!turn_flags_ptr->do_move) {
392             if (turn_flags_ptr->do_turn)
393                 break;
394
395             continue;
396         }
397
398         turn_flags_ptr->do_turn = TRUE;
399         feature_type *f_ptr;
400         f_ptr = &f_info[g_ptr->feat];
401         if (has_flag(f_ptr->flags, FF_TREE) && ((r_ptr->flags7 & RF7_CAN_FLY) == 0) && ((r_ptr->flags8 & RF8_WILD_WOOD) == 0))
402             m_ptr->energy_need += ENERGY_NEED();
403
404         if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx))
405             break;
406
407         monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
408         if (m_ptr->ml
409             && (disturb_move || (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx))
410                 || (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev))) {
411             if (is_hostile(m_ptr))
412                 disturb(target_ptr, FALSE, TRUE);
413         }
414
415         bool is_takable_or_killable = g_ptr->o_idx > 0;
416         is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
417
418         bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
419         is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
420
421         is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
422         if (!is_takable_or_killable) {
423             if (turn_flags_ptr->do_turn)
424                 break;
425
426             continue;
427         }
428
429         update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
430         if (turn_flags_ptr->do_turn)
431             break;
432
433         (*count)++;
434     }
435
436     return TRUE;
437 }
438
439 /*!
440  * @brief モンスターを喋らせたり足音を立てたりする
441  * @param target_ptr プレーヤーへの参照ポインタ
442  * @param m_idx モンスターID
443  * @param oy モンスターが元々いたY座標
444  * @param ox モンスターが元々いたX座標
445  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
446  * @return なし
447  */
448 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
449 {
450     if (target_ptr->phase_out)
451         return;
452
453     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
454     monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
455     if (m_ptr->ap_r_idx == MON_CYBER && one_in_(CYBERNOISE) && !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT)) {
456         if (disturb_minor)
457             disturb(target_ptr, FALSE, FALSE);
458         msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
459     }
460
461     if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware || !one_in_(SPEAK_CHANCE) || !player_has_los_bold(target_ptr, oy, ox)
462         || !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
463         return;
464
465     GAME_TEXT m_name[MAX_NLEN];
466     char monmessage[1024];
467     concptr filename;
468
469     if (m_ptr->ml)
470         monster_desc(target_ptr, m_name, m_ptr, 0);
471     else
472         strcpy(m_name, _("それ", "It"));
473
474     if (monster_fear_remaining(m_ptr))
475         filename = _("monfear_j.txt", "monfear.txt");
476     else if (is_pet(m_ptr))
477         filename = _("monpet_j.txt", "monpet.txt");
478     else if (is_friendly(m_ptr))
479         filename = _("monfrien_j.txt", "monfrien.txt");
480     else
481         filename = _("monspeak_j.txt", "monspeak.txt");
482
483     if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0) {
484         msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
485     }
486 }
487
488 /*!
489  * @brief モンスターの目標地点をセットする / Set the target of counter attack
490  * @param m_ptr モンスターの参照ポインタ
491  * @param y 目標y座標
492  * @param x 目標x座標
493  * @return なし
494  */
495 void set_target(monster_type *m_ptr, POSITION y, POSITION x)
496 {
497     m_ptr->target_y = y;
498     m_ptr->target_x = x;
499 }
500
501 /*!
502  * @brief モンスターの目標地点をリセットする / Reset the target of counter attack
503  * @param m_ptr モンスターの参照ポインタ
504  * @return なし
505  */
506 void reset_target(monster_type *m_ptr) { set_target(m_ptr, 0, 0); }