OSDN Git Service

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