OSDN Git Service

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