OSDN Git Service

f32bb8b8ec8c290cb9100645c4fa4bb3c4ba5a24
[hengbandforosx/hengbandosx.git] / src / monster / monster-processor.c
1 /*!
2  * @brief モンスターの特殊技能とターン経過処理 (移動等)/ Monster spells and movement for passaging a turn
3  * @date 2014/01/17
4  * @author
5  * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
6  * This software may be copied and distributed for educational, research,\n
7  * and not for profit purposes provided that this copyright and statement\n
8  * are included in all such copies.  Other copyrights may also apply.\n
9  * 2014 Deskull rearranged comment for Doxygen.\n
10  * @details
11  * This file has several additions to it by Keldon Jones (keldon@umr.edu)
12  * to improve the general quality of the AI (version 0.1.1).
13  */
14
15 #include "monster/monster-processor.h"
16 #include "cmd-io/cmd-dump.h"
17 #include "core/player-update-types.h"
18 #include "core/speed-table.h"
19 #include "floor/cave.h"
20 #include "game-option/play-record-options.h"
21 #include "grid/feature.h"
22 #include "grid/grid.h"
23 #include "io/write-diary.h"
24 #include "melee/melee-postprocess.h"
25 #include "melee/melee-spell.h"
26 #include "monster-floor/monster-direction.h"
27 #include "monster-floor/monster-generator.h"
28 #include "monster-floor/monster-move.h"
29 #include "monster-floor/monster-remover.h"
30 #include "monster-floor/monster-runaway.h"
31 #include "monster-floor/monster-summon.h"
32 #include "monster-floor/place-monster-types.h"
33 #include "monster-floor/quantum-effect.h"
34 #include "monster-race/monster-race.h"
35 #include "monster-race/race-flags-ability2.h"
36 #include "monster-race/race-flags-resistance.h"
37 #include "monster-race/race-flags1.h"
38 #include "monster-race/race-flags2.h"
39 #include "monster-race/race-flags7.h"
40 #include "monster-race/race-indice-types.h"
41 #include "monster/monster-describer.h"
42 #include "monster/monster-description-types.h"
43 #include "monster/monster-flag-types.h"
44 #include "monster/monster-info.h"
45 #include "monster/monster-list.h"
46 #include "monster/monster-processor-util.h"
47 #include "monster/monster-status-setter.h"
48 #include "monster/monster-status.h"
49 #include "monster/monster-update.h"
50 #include "monster/monster-util.h"
51 #include "mspell/mspell-attack.h"
52 #include "mspell/mspell-judgement.h"
53 #include "object-enchant/trc-types.h"
54 #include "pet/pet-fall-off.h"
55 #include "player-info/avatar.h"
56 #include "player/player-skill.h"
57 #include "player/player-status-flags.h"
58 #include "player/player-move.h"
59 #include "player/special-defense-types.h"
60 #include "spell-realm/spells-hex.h"
61 #include "spell/summon-types.h"
62 #include "system/floor-type-definition.h"
63 #include "system/monster-type-definition.h"
64 #include "target/projection-path-calculator.h"
65 #include "view/display-messages.h"
66
67 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
68 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
69 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
70 bool awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
71 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
72 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
73 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
74 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
75 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
76
77 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
78
79 void sweep_monster_process(player_type *target_ptr);
80 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
81
82 /*!
83  * @brief モンスター単体の1ターン行動処理メインルーチン /
84  * Process a monster
85  * @param target_ptr プレーヤーへの参照ポインタ
86  * @param m_idx 行動モンスターの参照ID
87  * @return なし
88  * @details
89  * The monster is known to be within 100 grids of the player\n
90  *\n
91  * In several cases, we directly update the monster lore\n
92  *\n
93  * Note that a monster is only allowed to "reproduce" if there\n
94  * are a limited number of "reproducing" monsters on the current\n
95  * level.  This should prevent the level from being "swamped" by\n
96  * reproducing monsters.  It also allows a large mass of mice to\n
97  * prevent a louse from multiplying, but this is a small price to\n
98  * pay for a simple multiplication method.\n
99  *\n
100  * XXX Monster fear is slightly odd, in particular, monsters will\n
101  * fixate on opening a door even if they cannot open it.  Actually,\n
102  * the same thing happens to normal monsters when they hit a door\n
103  *\n
104  * In addition, monsters which *cannot* open or bash\n
105  * down a door will still stand there trying to open it...\n
106  *\n
107  * XXX Technically, need to check for monster in the way\n
108  * combined with that monster being in a wall (or door?)\n
109  *\n
110  * A "direction" of "5" means "pick a random direction".\n
111  */
112 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
113 {
114     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
115     monster_race *r_ptr = &r_info[m_ptr->r_idx];
116     turn_flags tmp_flags;
117     turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
118     turn_flags_ptr->see_m = is_seen(target_ptr, m_ptr);
119
120     decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
121     if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !monster_csleep_remaining(m_ptr)) {
122         choose_new_monster(target_ptr, m_idx, FALSE, 0);
123         r_ptr = &r_info[m_ptr->r_idx];
124     }
125
126     turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
127     if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m) || process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)
128         || explode_grenade(target_ptr, m_idx) || runaway_monster(target_ptr, turn_flags_ptr, m_idx) || !awake_monster(target_ptr, m_idx))
129         return;
130
131     if (monster_stunned_remaining(m_ptr) && one_in_(2))
132         return;
133
134     if (turn_flags_ptr->is_riding_mon)
135         target_ptr->update |= PU_BONUS;
136
137     process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
138
139     POSITION oy = m_ptr->fy;
140     POSITION ox = m_ptr->fx;
141     if (decide_monster_multiplication(target_ptr, m_idx, oy, ox))
142         return;
143
144     process_special(target_ptr, m_idx);
145     process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
146     if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware))
147         return;
148
149     DIRECTION mm[8];
150     mm[0] = mm[1] = mm[2] = mm[3] = 0;
151     mm[4] = mm[5] = mm[6] = mm[7] = 0;
152
153     if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware))
154         return;
155
156     int count = 0;
157     if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count))
158         return;
159
160     /*
161      *  Forward movements failed, but now received LOS attack!
162      *  Try to flow by smell.
163      */
164     if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
165         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
166
167     if (!turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && !monster_fear_remaining(m_ptr) && !turn_flags_ptr->is_riding_mon && turn_flags_ptr->aware) {
168         if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell) {
169             if (make_attack_spell(target_ptr, m_idx))
170                 return;
171         }
172     }
173
174     update_player_type(target_ptr, turn_flags_ptr, r_ptr);
175     update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
176
177     if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx))
178         return;
179
180     if (m_ptr->ml)
181         chg_virtue(target_ptr, V_COMPASSION, -1);
182 }
183
184 /*!
185  * @brief 超隠密処理
186  * @param target_ptr プレーヤーへの参照ポインタ
187  * @param m_idx モンスターID
188  * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
189  */
190 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
191 {
192     if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0)
193         return TRUE;
194
195     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
196     monster_race *r_ptr = &r_info[m_ptr->r_idx];
197     int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
198     if (target_ptr->monlite)
199         tmp /= 3;
200
201     if (has_aggravate(target_ptr))
202         tmp /= 2;
203
204     if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10))
205         tmp /= 3;
206
207     return (randint0(tmp) <= (r_ptr->level + 20));
208 }
209
210 /*!
211  * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
212  * @param target_ptr プレーヤーへの参照ポインタ
213  * @param m_idx モンスターID
214  * @param is_riding_mon 騎乗中であればTRUE
215  * @return なし
216  */
217 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
218 {
219     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
220     monster_race *r_ptr = &r_info[m_ptr->r_idx];
221     if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0))
222         return;
223
224     if (process_fall_off_horse(target_ptr, 0, TRUE)) {
225 #ifdef JP
226         msg_print("地面に落とされた。");
227 #else
228         GAME_TEXT m_name[MAX_NLEN];
229         monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
230         msg_format("You have fallen from %s.", m_name);
231 #endif
232     }
233 }
234
235 /*!
236  * @brief 召喚の親元が消滅した時、子供も消滅させる
237  * @param target_ptr プレーヤーへの参照ポインタ
238  * @param m_idx モンスターID
239  * @param see_m モンスターが視界内にいたらTRUE
240  * @return 召喚モンスターが消滅したらTRUE
241  */
242 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
243 {
244     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
245     if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
246         return FALSE;
247
248     if (see_m) {
249         GAME_TEXT m_name[MAX_NLEN];
250         monster_desc(target_ptr, m_name, m_ptr, 0);
251         msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
252     }
253
254     if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname) {
255         GAME_TEXT m_name[MAX_NLEN];
256         monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
257         exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
258     }
259
260     delete_monster_idx(target_ptr, m_idx);
261     return TRUE;
262 }
263
264 /*!
265  * @brief 寝ているモンスターの起床を判定する
266  * @param target_ptr プレーヤーへの参照ポインタ
267  * @param m_idx モンスターID
268  * @return 寝たままならFALSE、起きているor起きたらTRUE
269  */
270 bool awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
271 {
272     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
273     monster_race *r_ptr = &r_info[m_ptr->r_idx];
274     if (!monster_csleep_remaining(m_ptr))
275         return TRUE;
276
277     if (!has_aggravate(target_ptr))
278         return FALSE;
279
280     (void)set_monster_csleep(target_ptr, m_idx, 0);
281     if (m_ptr->ml) {
282         GAME_TEXT m_name[MAX_NLEN];
283         monster_desc(target_ptr, m_name, m_ptr, 0);
284         msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
285     }
286
287     if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
288         r_ptr->r_wake++;
289
290     return TRUE;
291 }
292
293 /*!
294  * @brief モンスターの怒り状態を判定する (怒っていたら敵に回す)
295  * @param target_ptr プレーヤーへの参照ポインタ
296  * @param m_idx モンスターID
297  * @param see_m モンスターが視界内にいたらTRUE
298  * @return なし
299  */
300 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
301 {
302     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
303     monster_race *r_ptr = &r_info[m_ptr->r_idx];
304     bool gets_angry = FALSE;
305     if (is_friendly(m_ptr) && has_aggravate(target_ptr))
306         gets_angry = TRUE;
307
308     if (is_pet(m_ptr)
309         && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr))
310             || (r_ptr->flagsr & RFR_RES_ALL)))
311         gets_angry = TRUE;
312
313     if (target_ptr->phase_out || !gets_angry)
314         return;
315
316     GAME_TEXT m_name[MAX_NLEN];
317     monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
318
319     /* When riding a hostile align pet */
320     if (target_ptr->riding == m_idx) {
321         if (abs(target_ptr->align / 10) < randint0(target_ptr->skill_exp[GINOU_RIDING]))
322             return;
323
324         msg_format(_("%^sが突然暴れだした!", "%^s suddenly begins unruly!"), m_name);
325         if (!process_fall_off_horse(target_ptr, 1, TRUE))
326             return;
327
328         msg_format(_("あなたは振り落とされた。", "You have fallen."));
329     }
330
331     if (is_pet(m_ptr) || see_m) {
332         msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
333     }
334
335     set_hostile(target_ptr, m_ptr);
336 }
337
338 /*!
339  * @brief 手榴弾の爆発処理
340  * @param target_ptr プレーヤーへの参照ポインタ
341  * @param m_idx モンスターID
342  * @return 爆死したらTRUE
343  */
344 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
345 {
346     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
347     if (m_ptr->r_idx != MON_GRENADE)
348         return FALSE;
349
350     bool fear, dead;
351     mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
352     return dead;
353 }
354
355 /*!
356  * @brief モンスター依存の特別な行動を取らせる
357  * @param target_ptr プレーヤーへの参照ポインタ
358  * @param m_idx モンスターID
359  * @return なし
360  */
361 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
362 {
363     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
364     monster_race *r_ptr = &r_info[m_ptr->r_idx];
365     if (((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) || (m_ptr->r_idx != MON_OHMU) || target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out
366         || (r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
367         return;
368
369     int count = 0;
370     DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
371     BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
372
373     for (int k = 0; k < A_MAX; k++) {
374         if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode))) {
375             if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml)
376                 count++;
377         }
378     }
379
380     if (count && is_original_ap_and_seen(target_ptr, m_ptr))
381         r_ptr->r_flags6 |= (RF6_SPECIAL);
382 }
383
384 /*!
385  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
386  * @param target_ptr プレーヤーへの参照ポインタ
387  * @param m_idx モンスターID
388  * @param oy 分裂元モンスターのY座標
389  * @param ox 分裂元モンスターのX座標
390  * @return 実際に分裂したらTRUEを返す
391  */
392 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
393 {
394     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
395     monster_race *r_ptr = &r_info[m_ptr->r_idx];
396     if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
397         return FALSE;
398
399     int k = 0;
400     for (POSITION y = oy - 1; y <= oy + 1; y++) {
401         for (POSITION x = ox - 1; x <= ox + 1; x++) {
402             if (!in_bounds2(target_ptr->current_floor_ptr, y, x))
403                 continue;
404
405             if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx)
406                 k++;
407         }
408     }
409
410     if (multiply_barrier(target_ptr, m_idx))
411         k = 8;
412
413     if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ))) {
414         if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0))) {
415             if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
416                 r_ptr->r_flags2 |= RF2_MULTIPLY;
417
418             return TRUE;
419         }
420     }
421
422     return FALSE;
423 }
424
425 /*!
426  * @brief モンスターに魔法を試行させる
427  * @param target_ptr プレーヤーへの参照ポインタ
428  * @param m_idx モンスターID
429  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
430  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
431  */
432 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
433 {
434     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
435     monster_race *r_ptr = &r_info[m_ptr->r_idx];
436     if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
437         return FALSE;
438
439     bool counterattack = FALSE;
440     if (m_ptr->target_y) {
441         MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
442         if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx])
443             && projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x)) {
444             counterattack = TRUE;
445         }
446     }
447
448     if (counterattack) {
449         if (monst_spell_monst(target_ptr, m_idx) || (aware && make_attack_spell(target_ptr, m_idx)))
450             return TRUE;
451     } else {
452         if ((aware && make_attack_spell(target_ptr, m_idx)) || monst_spell_monst(target_ptr, m_idx))
453             return TRUE;
454     }
455
456     return FALSE;
457 }
458
459 /*!
460  * @brief モンスターの恐怖状態を処理する
461  * @param target_ptr プレーヤーへの参照ポインタ
462  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
463  * @param m_idx モンスターID
464  * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
465  * @return モンスターが戦いを決意したらTRUE
466  */
467 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
468 {
469     monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
470     bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && monster_fear_remaining(m_ptr) && turn_flags_ptr->aware;
471     if (!is_battle_determined)
472         return FALSE;
473
474     (void)set_monster_monfear(target_ptr, m_idx, 0);
475     if (!turn_flags_ptr->see_m)
476         return TRUE;
477
478     GAME_TEXT m_name[MAX_NLEN];
479     monster_desc(target_ptr, m_name, m_ptr, 0);
480     msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
481     return TRUE;
482 }
483
484 /*!
485  * @brief 全モンスターのターン管理メインルーチン /
486  * Process all the "live" monsters, once per game turn.
487  * @return なし
488  * @details
489  * During each game current game turn, we scan through the list of all the "live" monsters,\n
490  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
491  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
492  *\n
493  * Note that monsters can never move in the monster array (except when the\n
494  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
495  *\n
496  * This function is responsible for at least half of the processor time\n
497  * on a normal system with a "normal" amount of monsters and a player doing\n
498  * normal things.\n
499  *\n
500  * When the player is resting, virtually 90% of the processor time is spent\n
501  * in this function, and its children, "process_monster()" and "make_move()".\n
502  *\n
503  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
504  * especially when the player is running.\n
505  *\n
506  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
507  * monsters while they are still being "born".  A monster is "fresh" only\n
508  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
509  * determine if the monster is yet to be processed during the game turn.\n
510  *\n
511  * Note the special "MFLAG_PREVENT_MAGIC" flag, which allows the player to get one\n
512  * move before any "nasty" monsters get to use their spell attacks.\n
513  *\n
514  * Note that when the "knowledge" about the currently tracked monster\n
515  * changes (flags, attacks, spells), we induce a redraw of the monster\n
516  * recall window.\n
517  */
518 void process_monsters(player_type *target_ptr)
519 {
520     old_race_flags tmp_flags;
521     old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
522     target_ptr->current_floor_ptr->monster_noise = FALSE;
523     MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
524     save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
525     sweep_monster_process(target_ptr);
526     hack_m_idx = 0;
527     if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
528         return;
529
530     update_player_window(target_ptr, old_race_flags_ptr);
531 }
532
533 /*!
534  * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
535  * @param target_ptr プレーヤーへの参照ポインタ
536  */
537 void sweep_monster_process(player_type *target_ptr)
538 {
539     floor_type *floor_ptr = target_ptr->current_floor_ptr;
540     for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--) {
541         monster_type *m_ptr;
542         m_ptr = &floor_ptr->m_list[i];
543
544         if (target_ptr->leaving)
545             return;
546
547         if (!monster_is_valid(m_ptr) || target_ptr->wild_mode)
548             continue;
549
550         if (m_ptr->mflag & MFLAG_BORN) {
551             m_ptr->mflag &= ~(MFLAG_BORN);
552             continue;
553         }
554
555         if ((m_ptr->cdis >= AAF_LIMIT) || !decide_process_continue(target_ptr, m_ptr))
556             continue;
557
558         SPEED speed = (target_ptr->riding == i) ? target_ptr->pspeed : decide_monster_speed(m_ptr);
559         m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
560         if (m_ptr->energy_need > 0)
561             continue;
562
563         m_ptr->energy_need += ENERGY_NEED();
564         hack_m_idx = i;
565         process_monster(target_ptr, i);
566         reset_target(m_ptr);
567         if (target_ptr->no_flowed && one_in_(3))
568             m_ptr->mflag2 |= MFLAG2_NOFLOW;
569
570         if (!target_ptr->playing || target_ptr->is_dead || target_ptr->leaving)
571             return;
572     }
573 }
574
575 /*!
576  * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
577  * @param target_ptr プレーヤーへの参照ポインタ
578  * @param m_ptr モンスターへの参照ポインタ
579  * @return 後続処理が必要ならTRUE
580  */
581 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
582 {
583     monster_race *r_ptr;
584     r_ptr = &r_info[m_ptr->r_idx];
585     if (!target_ptr->no_flowed) {
586         m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
587     }
588
589     if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
590         return TRUE;
591
592     if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) && (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || has_aggravate(target_ptr)))
593         return TRUE;
594
595     if (m_ptr->target_y)
596         return TRUE;
597
598     return FALSE;
599 }