OSDN Git Service

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