OSDN Git Service

[Refactor] MULTIPLY を新定義に合わせた
[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/speed-table.h"
19 #include "floor/cave.h"
20 #include "floor/geometry.h"
21 #include "game-option/play-record-options.h"
22 #include "grid/feature.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-resistance.h"
36 #include "monster-race/race-flags1.h"
37 #include "monster-race/race-flags2.h"
38 #include "monster-race/race-flags7.h"
39 #include "monster-race/race-indice-types.h"
40 #include "monster/monster-describer.h"
41 #include "monster/monster-description-types.h"
42 #include "monster/monster-flag-types.h"
43 #include "monster/monster-info.h"
44 #include "monster/monster-list.h"
45 #include "monster/monster-processor-util.h"
46 #include "monster/monster-status-setter.h"
47 #include "monster/monster-status.h"
48 #include "monster/monster-update.h"
49 #include "monster/monster-util.h"
50 #include "mspell/mspell-attack.h"
51 #include "mspell/mspell-judgement.h"
52 #include "object-enchant/trc-types.h"
53 #include "pet/pet-fall-off.h"
54 #include "player-base/player-class.h"
55 #include "player-info/ninja-data-type.h"
56 #include "player/player-move.h"
57 #include "player/player-skill.h"
58 #include "player/player-status-flags.h"
59 #include "player/special-defense-types.h"
60 #include "spell-realm/spells-hex.h"
61 #include "spell/summon-types.h"
62 #include "system/angband-system.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 = &m_ptr->get_monrace();
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 = &m_ptr->get_monrace();
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(StatusRecalculatingFlag::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_map_flags(turn_flags_ptr);
203     update_lite_flags(turn_flags_ptr, r_ptr);
204     update_monster_race_flags(player_ptr, turn_flags_ptr, m_ptr);
205
206     if (!process_monster_fear(player_ptr, turn_flags_ptr, m_idx)) {
207         return;
208     }
209
210     if (m_ptr->ml) {
211         chg_virtue(player_ptr, Virtue::COMPASSION, -1);
212     }
213 }
214
215 /*!
216  * @brief 超隠密処理
217  * @param player_ptr プレイヤーへの参照ポインタ
218  * @param m_idx モンスターID
219  * @return モンスターがプレイヤーに気付いているならばTRUE、超隠密状態ならばFALSE
220  */
221 bool process_stealth(PlayerType *player_ptr, MONSTER_IDX m_idx)
222 {
223     auto ninja_data = PlayerClass(player_ptr).get_specific_data<ninja_data_type>();
224     if (!ninja_data || !ninja_data->s_stealth) {
225         return true;
226     }
227
228     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
229     auto *r_ptr = &m_ptr->get_monrace();
230     int tmp = player_ptr->lev * 6 + (player_ptr->skill_stl + 10) * 4;
231     if (player_ptr->monlite) {
232         tmp /= 3;
233     }
234
235     if (has_aggravate(player_ptr)) {
236         tmp /= 2;
237     }
238
239     if (r_ptr->level > (player_ptr->lev * player_ptr->lev / 20 + 10)) {
240         tmp /= 3;
241     }
242
243     return randint0(tmp) <= (r_ptr->level + 20);
244 }
245
246 /*!
247  * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
248  * @param player_ptr プレイヤーへの参照ポインタ
249  * @param m_idx モンスターID
250  * @param is_riding_mon 騎乗中であればTRUE
251  */
252 void decide_drop_from_monster(PlayerType *player_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
253 {
254     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
255     auto *r_ptr = &m_ptr->get_monrace();
256     if (!is_riding_mon || r_ptr->misc_flags.has(MonsterMiscType::RIDING)) {
257         return;
258     }
259
260     if (process_fall_off_horse(player_ptr, 0, true)) {
261 #ifdef JP
262         msg_print("地面に落とされた。");
263 #else
264         const auto m_name = monster_desc(player_ptr, &player_ptr->current_floor_ptr->m_list[player_ptr->riding], 0);
265         msg_format("You have fallen from %s.", m_name.data());
266 #endif
267     }
268 }
269
270 /*!
271  * @brief 召喚の親元が消滅した時、子供も消滅させる
272  * @param player_ptr プレイヤーへの参照ポインタ
273  * @param m_idx モンスターID
274  * @param see_m モンスターが視界内にいたらTRUE
275  * @return 召喚モンスターが消滅したらTRUE
276  */
277 bool vanish_summoned_children(PlayerType *player_ptr, MONSTER_IDX m_idx, bool see_m)
278 {
279     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
280
281     if (m_ptr->parent_m_idx == 0) {
282         return false;
283     }
284
285     // parent_m_idxが自分自身を指している場合は召喚主は消滅している
286     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()) {
287         return false;
288     }
289
290     if (see_m) {
291         const auto m_name = monster_desc(player_ptr, m_ptr, 0);
292         msg_format(_("%sは消え去った!", "%s^ disappears!"), m_name.data());
293     }
294
295     if (record_named_pet && m_ptr->is_named_pet()) {
296         const auto m_name = monster_desc(player_ptr, m_ptr, MD_INDEF_VISIBLE);
297         exe_write_diary(player_ptr, DiaryKind::NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
298     }
299
300     delete_monster_idx(player_ptr, m_idx);
301     return true;
302 }
303
304 /*!
305  * @brief 寝ているモンスターの起床を判定する
306  * @param player_ptr プレイヤーへの参照ポインタ
307  * @param m_idx モンスターID
308  * @return 寝たままならFALSE、起きているor起きたらTRUE
309  */
310 bool awake_monster(PlayerType *player_ptr, MONSTER_IDX m_idx)
311 {
312     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
313     auto *r_ptr = &m_ptr->get_monrace();
314     if (!m_ptr->is_asleep()) {
315         return true;
316     }
317
318     if (!has_aggravate(player_ptr)) {
319         return false;
320     }
321
322     (void)set_monster_csleep(player_ptr, m_idx, 0);
323     if (m_ptr->ml) {
324         const auto m_name = monster_desc(player_ptr, m_ptr, 0);
325         msg_format(_("%s^が目を覚ました。", "%s^ wakes up."), m_name.data());
326     }
327
328     if (is_original_ap_and_seen(player_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR)) {
329         r_ptr->r_wake++;
330     }
331
332     return true;
333 }
334
335 /*!
336  * @brief モンスターの怒り状態を判定する (怒っていたら敵に回す)
337  * @param player_ptr プレイヤーへの参照ポインタ
338  * @param m_idx モンスターID
339  * @param see_m モンスターが視界内にいたらTRUE
340  */
341 void process_angar(PlayerType *player_ptr, MONSTER_IDX m_idx, bool see_m)
342 {
343     auto &monster = player_ptr->current_floor_ptr->m_list[m_idx];
344     const auto &monrace = monster.get_monrace();
345     auto gets_angry = monster.is_friendly() && has_aggravate(player_ptr);
346     const auto should_aggravate = monster.is_pet();
347     auto has_hostile = monrace.kind_flags.has(MonsterKindType::UNIQUE) || (monrace.population_flags.has(MonsterPopulationType::NAZGUL));
348     has_hostile &= monster_has_hostile_align(player_ptr, nullptr, 10, -10, &monrace);
349     const auto has_resist_all = monrace.resistance_flags.has(MonsterResistanceType::RESIST_ALL);
350     if (should_aggravate && (has_hostile || has_resist_all)) {
351         gets_angry = true;
352     }
353
354     if (AngbandSystem::get_instance().is_phase_out() || !gets_angry) {
355         return;
356     }
357
358     const auto m_name = monster_desc(player_ptr, &monster, monster.is_pet() ? MD_ASSUME_VISIBLE : 0);
359
360     /* When riding a hostile alignment pet */
361     if (player_ptr->riding == m_idx) {
362         if (abs(player_ptr->alignment / 10) < randint0(player_ptr->skill_exp[PlayerSkillKindType::RIDING])) {
363             return;
364         }
365
366         msg_format(_("%s^が突然暴れだした!", "%s^ suddenly begins unruly!"), m_name.data());
367         if (!process_fall_off_horse(player_ptr, 1, true)) {
368             return;
369         }
370
371         msg_format(_("あなたは振り落とされた。", "You have fallen."));
372     }
373
374     if (monster.is_pet() || see_m) {
375         msg_format(_("%s^は突然敵にまわった!", "%s^ suddenly becomes hostile!"), m_name.data());
376     }
377
378     monster.set_hostile();
379 }
380
381 /*!
382  * @brief 手榴弾の爆発処理
383  * @param player_ptr プレイヤーへの参照ポインタ
384  * @param m_idx モンスターID
385  * @return 爆死したらTRUE
386  */
387 bool explode_grenade(PlayerType *player_ptr, MONSTER_IDX m_idx)
388 {
389     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
390     if (m_ptr->r_idx != MonsterRaceId::GRENADE) {
391         return false;
392     }
393
394     bool fear, dead;
395     mon_take_hit_mon(player_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
396     return dead;
397 }
398
399 /*!
400  * @brief モンスター依存の特別な行動を取らせる
401  * @param player_ptr プレイヤーへの参照ポインタ
402  * @param m_idx モンスターID
403  */
404 void process_special(PlayerType *player_ptr, MONSTER_IDX m_idx)
405 {
406     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
407     auto *r_ptr = &m_ptr->get_monrace();
408     auto can_do_special = r_ptr->ability_flags.has(MonsterAbilityType::SPECIAL);
409     can_do_special &= m_ptr->r_idx == MonsterRaceId::OHMU;
410     can_do_special &= !player_ptr->current_floor_ptr->inside_arena;
411     can_do_special &= !AngbandSystem::get_instance().is_phase_out();
412     can_do_special &= r_ptr->freq_spell != 0;
413     can_do_special &= randint1(100) <= r_ptr->freq_spell;
414     if (!can_do_special) {
415         return;
416     }
417
418     int count = 0;
419     DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
420     BIT_FLAGS p_mode = m_ptr->is_pet() ? PM_FORCE_PET : PM_NONE;
421
422     for (int k = 0; k < A_MAX; k++) {
423         if (summon_specific(player_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode))) {
424             if (player_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) {
425                 count++;
426             }
427         }
428     }
429
430     if (count && is_original_ap_and_seen(player_ptr, m_ptr)) {
431         r_ptr->r_ability_flags.set(MonsterAbilityType::SPECIAL);
432     }
433 }
434
435 /*!
436  * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
437  * @param player_ptr プレイヤーへの参照ポインタ
438  * @param m_idx モンスターID
439  * @param oy 分裂元モンスターのY座標
440  * @param ox 分裂元モンスターのX座標
441  * @return 実際に分裂したらTRUEを返す
442  */
443 bool decide_monster_multiplication(PlayerType *player_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
444 {
445     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
446     auto *r_ptr = &m_ptr->get_monrace();
447     if (r_ptr->misc_flags.has_not(MonsterMiscType::MULTIPLY) || (player_ptr->current_floor_ptr->num_repro >= MAX_REPRODUCTION)) {
448         return false;
449     }
450
451     int k = 0;
452     for (POSITION y = oy - 1; y <= oy + 1; y++) {
453         for (POSITION x = ox - 1; x <= ox + 1; x++) {
454             if (!in_bounds2(player_ptr->current_floor_ptr, y, x)) {
455                 continue;
456             }
457
458             if (player_ptr->current_floor_ptr->grid_array[y][x].m_idx) {
459                 k++;
460             }
461         }
462     }
463
464     if (SpellHex(player_ptr).check_hex_barrier(m_idx, HEX_ANTI_MULTI)) {
465         k = 8;
466     }
467
468     constexpr auto chance_reproduction = 8;
469     if ((k < 4) && (!k || !randint0(k * chance_reproduction))) {
470         if (multiply_monster(player_ptr, m_idx, false, (m_ptr->is_pet() ? PM_FORCE_PET : 0))) {
471             if (player_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(player_ptr, m_ptr)) {
472                 r_ptr->r_misc_flags.set(MonsterMiscType::MULTIPLY);
473             }
474
475             return true;
476         }
477     }
478
479     return false;
480 }
481
482 /*!
483  * @brief モンスターに魔法を試行させる
484  * @param player_ptr プレイヤーへの参照ポインタ
485  * @param m_idx モンスターID
486  * @param aware モンスターがプレイヤーに気付いているならばTRUE、超隠密状態ならばFALSE
487  * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
488  */
489 bool cast_spell(PlayerType *player_ptr, MONSTER_IDX m_idx, bool aware)
490 {
491     auto &floor = *player_ptr->current_floor_ptr;
492     const auto &monster_from = floor.m_list[m_idx];
493     const auto &monrace = monster_from.get_monrace();
494     if ((monrace.freq_spell == 0) || (randint1(100) > monrace.freq_spell)) {
495         return false;
496     }
497
498     bool counterattack = false;
499     if (monster_from.target_y) {
500         Pos2D pos(monster_from.target_y, monster_from.target_x);
501         const auto t_m_idx = floor.get_grid(pos).m_idx;
502         const auto &monster_to = floor.m_list[t_m_idx];
503         const auto is_projectable = projectable(player_ptr, monster_from.fy, monster_from.fx, monster_from.target_y, monster_from.target_x);
504         if (t_m_idx && monster_from.is_hostile_to_melee(monster_to) && is_projectable) {
505             counterattack = true;
506         }
507     }
508
509     if (counterattack) {
510         if (monst_spell_monst(player_ptr, m_idx) || (aware && make_attack_spell(player_ptr, m_idx))) {
511             return true;
512         }
513     } else {
514         if ((aware && make_attack_spell(player_ptr, m_idx)) || monst_spell_monst(player_ptr, m_idx)) {
515             return true;
516         }
517     }
518
519     return false;
520 }
521
522 /*!
523  * @brief モンスターの恐怖状態を処理する
524  * @param player_ptr プレイヤーへの参照ポインタ
525  * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
526  * @param m_idx モンスターID
527  * @param aware モンスターがプレイヤーに気付いているならばTRUE、超隠密状態ならばFALSE
528  * @return モンスターが戦いを決意したらTRUE
529  */
530 bool process_monster_fear(PlayerType *player_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
531 {
532     auto *m_ptr = &player_ptr->current_floor_ptr->m_list[m_idx];
533     bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && m_ptr->is_fearful() && turn_flags_ptr->aware;
534     if (!is_battle_determined) {
535         return false;
536     }
537
538     (void)set_monster_monfear(player_ptr, m_idx, 0);
539     if (!turn_flags_ptr->see_m) {
540         return true;
541     }
542
543     const auto m_name = monster_desc(player_ptr, m_ptr, 0);
544     msg_format(_("%s^は戦いを決意した!", "%s^ turns to fight!"), m_name.data());
545     return true;
546 }
547
548 /*!
549  * @brief 全モンスターのターン管理メインルーチン /
550  * Process all the "live" monsters, once per game turn.
551  * @details
552  * During each game current game turn, we scan through the list of all the "live" monsters,\n
553  * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
554  * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
555  *\n
556  * Note that monsters can never move in the monster array (except when the\n
557  * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
558  *\n
559  * This function is responsible for at least half of the processor time\n
560  * on a normal system with a "normal" amount of monsters and a player doing\n
561  * normal things.\n
562  *\n
563  * When the player is resting, virtually 90% of the processor time is spent\n
564  * in this function, and its children, "process_monster()" and "make_move()".\n
565  *\n
566  * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
567  * especially when the player is running.\n
568  *\n
569  * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
570  * monsters while they are still being "born".  A monster is "fresh" only\n
571  * during the game turn in which it is created, and we use the "hack_m_idx" to\n
572  * determine if the monster is yet to be processed during the game turn.\n
573  *\n
574  * Note the special "MFLAG_PREVENT_MAGIC" flag, which allows the player to get one\n
575  * move before any "nasty" monsters get to use their spell attacks.\n
576  *\n
577  * Note that when the "knowledge" about the currently tracked monster\n
578  * changes (flags, attacks, spells), we induce a redraw of the monster\n
579  * recall window.\n
580  */
581 void process_monsters(PlayerType *player_ptr)
582 {
583     const auto old_monrace_id = player_ptr->monster_race_idx;
584     old_race_flags tmp_flags(old_monrace_id);
585     old_race_flags *old_race_flags_ptr = &tmp_flags;
586     player_ptr->current_floor_ptr->monster_noise = false;
587     sweep_monster_process(player_ptr);
588     hack_m_idx = 0;
589     if (!MonsterRace(player_ptr->monster_race_idx).is_valid() || (player_ptr->monster_race_idx != old_monrace_id)) {
590         return;
591     }
592
593     update_player_window(player_ptr, old_race_flags_ptr);
594 }
595
596 /*!
597  * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
598  * @param player_ptr プレイヤーへの参照ポインタ
599  */
600 void sweep_monster_process(PlayerType *player_ptr)
601 {
602     auto *floor_ptr = player_ptr->current_floor_ptr;
603     for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--) {
604         MonsterEntity *m_ptr;
605         m_ptr = &floor_ptr->m_list[i];
606
607         if (player_ptr->leaving) {
608             return;
609         }
610
611         if (!m_ptr->is_valid() || player_ptr->wild_mode) {
612             continue;
613         }
614
615         if (m_ptr->mflag.has(MonsterTemporaryFlagType::BORN)) {
616             m_ptr->mflag.reset(MonsterTemporaryFlagType::BORN);
617             continue;
618         }
619
620         if ((m_ptr->cdis >= MAX_MONSTER_SENSING) || !decide_process_continue(player_ptr, m_ptr)) {
621             continue;
622         }
623
624         byte speed = (player_ptr->riding == i) ? player_ptr->pspeed : m_ptr->get_temporary_speed();
625         m_ptr->energy_need -= speed_to_energy(speed);
626         if (m_ptr->energy_need > 0) {
627             continue;
628         }
629
630         m_ptr->energy_need += ENERGY_NEED();
631         hack_m_idx = i;
632         process_monster(player_ptr, i);
633         reset_target(m_ptr);
634         if (player_ptr->no_flowed && one_in_(3)) {
635             m_ptr->mflag2.set(MonsterConstantFlagType::NOFLOW);
636         }
637
638         if (!player_ptr->playing || player_ptr->is_dead || player_ptr->leaving) {
639             return;
640         }
641     }
642 }
643
644 /*!
645  * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
646  * @param player_ptr プレイヤーへの参照ポインタ
647  * @param m_ptr モンスターへの参照ポインタ
648  * @return 後続処理が必要ならTRUE
649  */
650 bool decide_process_continue(PlayerType *player_ptr, MonsterEntity *m_ptr)
651 {
652     const auto &monrace = m_ptr->get_monrace();
653     if (!player_ptr->no_flowed) {
654         m_ptr->mflag2.reset(MonsterConstantFlagType::NOFLOW);
655     }
656
657     if (m_ptr->cdis <= (m_ptr->is_pet() ? (monrace.aaf > MAX_PLAYER_SIGHT ? MAX_PLAYER_SIGHT : monrace.aaf) : monrace.aaf)) {
658         return true;
659     }
660
661     auto should_continue = (m_ptr->cdis <= MAX_PLAYER_SIGHT) || AngbandSystem::get_instance().is_phase_out();
662     should_continue &= player_ptr->current_floor_ptr->has_los({ m_ptr->fy, m_ptr->fx }) || has_aggravate(player_ptr);
663     if (should_continue) {
664         return true;
665     }
666
667     if (m_ptr->target_y) {
668         return true;
669     }
670
671     return false;
672 }