2 * @file monster-process.c
3 * @brief モンスターの特殊技能とターン経過処理 (移動等)/ Monster spells and movement for passaging a turn
6 * Copyright (c) 1997 Ben Harrison, James E. Wilson, Robert A. Koeneke\n
7 * This software may be copied and distributed for educational, research,\n
8 * and not for profit purposes provided that this copyright and statement\n
9 * are included in all such copies. Other copyrights may also apply.\n
10 * 2014 Deskull rearranged comment for Doxygen.\n
12 * This file has several additions to it by Keldon Jones (keldon@umr.edu)
13 * to improve the general quality of the AI (version 0.1.1).
18 #include "monster/monster-attack.h"
19 #include "monster/monster-direction.h"
20 #include "monster/monster-object.h"
21 #include "monster/monster-move.h"
22 #include "monster/monster-runaway.h"
23 #include "monster/monster-safety-hiding.h"
24 #include "monster/monster-util.h"
25 #include "monster/monster-update.h"
26 #include "monster/quantum-effect.h"
33 #include "spells-floor.h"
34 #include "spells-summon.h"
36 #include "realm-hex.h"
39 #include "player-move.h"
40 #include "monster-status.h"
41 #include "monster-spell.h"
42 #include "monster-process.h"
43 #include "monsterrace-hook.h"
46 #include "view-mainwindow.h"
48 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon);
49 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx);
50 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
51 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
52 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
53 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx);
54 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
55 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
56 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
57 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
59 bool process_monster_movement(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, DIRECTION *mm, POSITION oy, POSITION ox, int *count);
60 bool process_post_dig_wall(player_type *target_ptr, turn_flags *turn_flags_ptr, monster_type *m_ptr, POSITION ny, POSITION nx);
61 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx);
63 void sweep_monster_process(player_type *target_ptr);
64 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr);
67 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
68 * Returns whether a given monster will try to run from the player.
69 * @param m_idx 逃走するモンスターの参照ID
70 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
72 * Monsters will attempt to avoid very powerful players. See below.\n
74 * Because this function is called so often, little details are important\n
75 * for efficiency. Like not using "mod" or "div" when possible. And\n
76 * attempting to check the conditions in an optimal order. Note that\n
77 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
79 * Note that this function is responsible for about one to five percent\n
80 * of the processor use in normal conditions...\n
82 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
84 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
85 monster_race *r_ptr = &r_info[m_ptr->r_idx];
89 return ((target_ptr->pet_follow_distance < 0) &&
90 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
93 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
94 if (MON_MONFEAR(m_ptr)) return TRUE;
95 if (m_ptr->cdis <= 5) return FALSE;
97 PLAYER_LEVEL p_lev = target_ptr->lev;
98 DEPTH m_lev = r_ptr->level + (m_idx & 0x08) + 25;
99 if (m_lev > p_lev + 4) return FALSE;
100 if (m_lev + 4 <= p_lev) return TRUE;
102 HIT_POINT p_chp = target_ptr->chp;
103 HIT_POINT p_mhp = target_ptr->mhp;
104 HIT_POINT m_chp = m_ptr->hp;
105 HIT_POINT m_mhp = m_ptr->maxhp;
106 u32b p_val = (p_lev * p_mhp) + (p_chp << 2);
107 u32b m_val = (m_lev * m_mhp) + (m_chp << 2);
108 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
115 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
116 * Search spell castable grid
117 * @param target_ptr プレーヤーへの参照ポインタ
118 * @param m_idx モンスターの参照ID
119 * @param yp 適したマスのY座標を返す参照ポインタ
120 * @param xp 適したマスのX座標を返す参照ポインタ
121 * @return 有効なマスがあった場合TRUEを返す
123 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
125 floor_type *floor_ptr = target_ptr->current_floor_ptr;
126 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
127 monster_race *r_ptr = &r_info[m_ptr->r_idx];
129 POSITION y1 = m_ptr->fy;
130 POSITION x1 = m_ptr->fx;
132 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
134 int now_cost = floor_ptr->grid_array[y1][x1].cost;
135 if (now_cost == 0) now_cost = 999;
137 bool can_open_door = FALSE;
138 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
140 can_open_door = TRUE;
144 for (int i = 7; i >= 0; i--)
146 POSITION y = y1 + ddy_ddd[i];
147 POSITION x = x1 + ddx_ddd[i];
148 if (!in_bounds2(floor_ptr, y, x)) continue;
149 if (player_bold(target_ptr, y, x)) return FALSE;
152 g_ptr = &floor_ptr->grid_array[y][x];
153 int cost = g_ptr->cost;
154 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding))))
156 if (cost == 0) continue;
157 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
160 if (cost == 0) cost = 998;
162 if (now_cost < cost) continue;
163 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
164 if (best < cost) continue;
167 *yp = y1 + ddy_ddd[i];
168 *xp = x1 + ddx_ddd[i];
171 if (best == 999) return FALSE;
178 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
179 * Choose the "best" direction for "flowing"
180 * @param m_idx モンスターの参照ID
181 * @param yp 移動先のマスのY座標を返す参照ポインタ
182 * @param xp 移動先のマスのX座標を返す参照ポインタ
183 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
184 * @return 有効なマスがあった場合TRUEを返す
186 * Note that ghosts and rock-eaters are never allowed to "flow",\n
187 * since they should move directly towards the player.\n
189 * Prefer "non-diagonal" directions, but twiddle them a little\n
190 * to angle slightly towards the player's actual location.\n
192 * Allow very perceptive monsters to track old "spoor" left by\n
193 * previous locations occupied by the player. This will tend\n
194 * to have monsters end up either near the player or on a grid\n
195 * recently occupied by the player (and left via "teleport").\n
197 * Note that if "smell" is turned on, all monsters get vicious.\n
199 * Also note that teleporting away from a location will cause\n
200 * the monsters who were chasing you to converge on that location\n
201 * as long as you are still near enough to "annoy" them without\n
202 * being close enough to chase directly. I have no idea what will\n
203 * happen if you combine "smell" with low "aaf" values.\n
205 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
208 floor_type *floor_ptr = target_ptr->current_floor_ptr;
209 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
210 monster_race *r_ptr = &r_info[m_ptr->r_idx];
212 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
213 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
214 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
216 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
219 if (no_flow) return FALSE;
220 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
221 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
223 POSITION y1 = m_ptr->fy;
224 POSITION x1 = m_ptr->fx;
225 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
227 g_ptr = &floor_ptr->grid_array[y1][x1];
230 bool use_scent = FALSE;
235 else if (g_ptr->when)
237 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
247 for (int i = 7; i >= 0; i--)
249 POSITION y = y1 + ddy_ddd[i];
250 POSITION x = x1 + ddx_ddd[i];
252 if (!in_bounds2(floor_ptr, y, x)) continue;
254 g_ptr = &floor_ptr->grid_array[y][x];
257 int when = g_ptr->when;
258 if (best > when) continue;
265 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
274 if ((cost == 0) || (best < cost)) continue;
279 *yp = target_ptr->y + 16 * ddy_ddd[i];
280 *xp = target_ptr->x + 16 * ddx_ddd[i];
283 if (best == 999 || best == 0) return FALSE;
290 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
291 * Provide a location to flee to, but give the player a wide berth.
292 * @param m_idx モンスターの参照ID
293 * @param yp 移動先のマスのY座標を返す参照ポインタ
294 * @param xp 移動先のマスのX座標を返す参照ポインタ
295 * @return 有効なマスがあった場合TRUEを返す
297 * A monster may wish to flee to a location that is behind the player,\n
298 * but instead of heading directly for it, the monster should "swerve"\n
299 * around the player so that he has a smaller chance of getting hit.\n
301 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
303 POSITION gy = 0, gx = 0;
305 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
306 POSITION fy = m_ptr->fy;
307 POSITION fx = m_ptr->fx;
309 POSITION y1 = fy - (*yp);
310 POSITION x1 = fx - (*xp);
313 for (int i = 7; i >= 0; i--)
315 POSITION y = fy + ddy_ddd[i];
316 POSITION x = fx + ddx_ddd[i];
317 if (!in_bounds2(floor_ptr, y, x)) continue;
319 POSITION dis = distance(y, x, y1, x1);
320 POSITION s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
323 if (s < score) continue;
330 if (score == -1) return FALSE;
340 * todo 分割したいが条件が多すぎて適切な関数名と詳細処理を追いきれない……
341 * @brief モンスターの移動方向を返す /
342 * Choose "logical" directions for monster movement
343 * @param target_ptr プレーヤーへの参照ポインタ
344 * @param m_idx モンスターの参照ID
345 * @param mm 移動方向を返す方向IDの参照ポインタ
346 * @return 有効方向があった場合TRUEを返す
348 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
350 floor_type *floor_ptr = target_ptr->current_floor_ptr;
351 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
352 monster_race *r_ptr = &r_info[m_ptr->r_idx];
353 POSITION y = 0, x = 0;
354 POSITION y2 = target_ptr->y;
355 POSITION x2 = target_ptr->x;
357 bool will_run = mon_will_run(target_ptr, m_idx);
359 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) != 0) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2);
360 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) != 0) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall);
362 if (!will_run && m_ptr->target_y)
364 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
366 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
367 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
368 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
370 y = m_ptr->fy - m_ptr->target_y;
371 x = m_ptr->fx - m_ptr->target_x;
376 if (!done && !will_run && is_hostile(m_ptr) &&
377 (r_ptr->flags1 & RF1_FRIENDS) &&
378 ((los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
379 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
381 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
382 !(r_ptr->flags2 & RF2_KILL_WALL))
385 for (int i = 0; i < 8; i++)
387 int xx = target_ptr->x + ddx_ddd[i];
388 int yy = target_ptr->y + ddy_ddd[i];
390 if (!in_bounds2(floor_ptr, yy, xx)) continue;
392 g_ptr = &floor_ptr->grid_array[yy][xx];
393 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
399 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
400 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
402 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
403 (target_ptr->mhp + target_ptr->msp))
405 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
409 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
411 for (int i = 0; i < 8; i++)
413 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
414 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
415 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
422 if (!in_bounds2(floor_ptr, y2, x2)) continue;
423 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
436 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
441 if (is_pet(m_ptr) && will_run)
447 if (!done && will_run)
451 if (find_safety(target_ptr, m_idx, &y, &x) && !no_flow)
453 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x))
465 if (!x && !y) return FALSE;
467 store_moves_val(mm, y, x);
473 * @brief モンスター単体の1ターン行動処理メインルーチン /
475 * @param target_ptr プレーヤーへの参照ポインタ
476 * @param m_idx 行動モンスターの参照ID
479 * The monster is known to be within 100 grids of the player\n
481 * In several cases, we directly update the monster lore\n
483 * Note that a monster is only allowed to "reproduce" if there\n
484 * are a limited number of "reproducing" monsters on the current\n
485 * level. This should prevent the level from being "swamped" by\n
486 * reproducing monsters. It also allows a large mass of mice to\n
487 * prevent a louse from multiplying, but this is a small price to\n
488 * pay for a simple multiplication method.\n
490 * XXX Monster fear is slightly odd, in particular, monsters will\n
491 * fixate on opening a door even if they cannot open it. Actually,\n
492 * the same thing happens to normal monsters when they hit a door\n
494 * In addition, monsters which *cannot* open or bash\n
495 * down a door will still stand there trying to open it...\n
497 * XXX Technically, need to check for monster in the way\n
498 * combined with that monster being in a wall (or door?)\n
500 * A "direction" of "5" means "pick a random direction".\n
502 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
504 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
505 monster_race *r_ptr = &r_info[m_ptr->r_idx];
506 turn_flags tmp_flags;
507 turn_flags *turn_flags_ptr = init_turn_flags(target_ptr->riding, m_idx, &tmp_flags);
508 turn_flags_ptr->see_m = is_seen(m_ptr);
510 decide_drop_from_monster(target_ptr, m_idx, turn_flags_ptr->is_riding_mon);
511 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
513 choose_new_monster(target_ptr, m_idx, FALSE, 0);
514 r_ptr = &r_info[m_ptr->r_idx];
517 turn_flags_ptr->aware = process_stealth(target_ptr, m_idx);
518 if (vanish_summoned_children(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
519 if (process_quantum_effect(target_ptr, m_idx, turn_flags_ptr->see_m)) return;
520 if (explode_grenade(target_ptr, m_idx)) return;
521 if (runaway_monster(target_ptr, turn_flags_ptr, m_idx)) return;
523 awake_monster(target_ptr, m_idx);
524 if (MON_STUNNED(m_ptr))
526 if (one_in_(2)) return;
529 if (turn_flags_ptr->is_riding_mon)
531 target_ptr->update |= (PU_BONUS);
534 process_angar(target_ptr, m_idx, turn_flags_ptr->see_m);
536 POSITION oy = m_ptr->fy;
537 POSITION ox = m_ptr->fx;
538 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
540 process_special(target_ptr, m_idx);
541 process_speak_sound(target_ptr, m_idx, oy, ox, turn_flags_ptr->aware);
542 if (cast_spell(target_ptr, m_idx, turn_flags_ptr->aware)) return;
545 mm[0] = mm[1] = mm[2] = mm[3] = 0;
546 mm[4] = mm[5] = mm[6] = mm[7] = 0;
548 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, turn_flags_ptr->aware, get_moves)) return;
551 if (!process_monster_movement(target_ptr, turn_flags_ptr, m_idx, mm, oy, ox, &count)) return;
554 * Forward movements failed, but now received LOS attack!
555 * Try to flow by smell.
557 if (target_ptr->no_flowed && count > 2 && m_ptr->target_y)
558 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
560 if (!turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && !MON_MONFEAR(m_ptr) && !turn_flags_ptr->is_riding_mon && turn_flags_ptr->aware)
562 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
564 if (make_attack_spell(m_idx, target_ptr)) return;
568 update_player_type(target_ptr, turn_flags_ptr, r_ptr);
569 update_monster_race_flags(target_ptr, turn_flags_ptr, m_ptr);
571 if (!process_monster_fear(target_ptr, turn_flags_ptr, m_idx)) return;
573 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
579 * @param target_ptr プレーヤーへの参照ポインタ
580 * @param m_idx モンスターID
581 * @return モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
583 bool process_stealth(player_type *target_ptr, MONSTER_IDX m_idx)
585 if ((target_ptr->special_defense & NINJA_S_STEALTH) == 0) return TRUE;
587 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
588 monster_race *r_ptr = &r_info[m_ptr->r_idx];
589 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
590 if (target_ptr->monlite) tmp /= 3;
591 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
592 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
593 return (randint0(tmp) <= (r_ptr->level + 20));
598 * @brief 死亡したモンスターが乗馬中のモンスターだった場合に落馬処理を行う
599 * @param target_ptr プレーヤーへの参照ポインタ
600 * @param m_idx モンスターID
601 * @param is_riding_mon 騎乗中であればTRUE
604 void decide_drop_from_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon)
606 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
607 monster_race *r_ptr = &r_info[m_ptr->r_idx];
608 if (!is_riding_mon || ((r_ptr->flags7 & RF7_RIDING) != 0)) return;
610 if (rakuba(target_ptr, 0, TRUE))
613 msg_print("地面に落とされた。");
615 GAME_TEXT m_name[MAX_NLEN];
616 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
617 msg_format("You have fallen from %s.", m_name);
624 * @brief 召喚の親元が消滅した時、子供も消滅させる
625 * @param target_ptr プレーヤーへの参照ポインタ
626 * @param m_idx モンスターID
627 * @param see_m モンスターが視界内にいたらTRUE
628 * @return 召喚モンスターが消滅したらTRUE
630 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
632 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
633 if ((m_ptr->parent_m_idx == 0) || (target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx > 0))
638 GAME_TEXT m_name[MAX_NLEN];
639 monster_desc(target_ptr, m_name, m_ptr, 0);
640 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
643 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
645 GAME_TEXT m_name[MAX_NLEN];
646 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
647 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
650 delete_monster_idx(target_ptr, m_idx);
656 * @brief 寝ているモンスターの起床を判定する
657 * @param target_ptr プレーヤーへの参照ポインタ
658 * @param m_idx モンスターID
661 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
663 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
664 monster_race *r_ptr = &r_info[m_ptr->r_idx];
665 if (!MON_CSLEEP(m_ptr)) return;
666 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
668 (void)set_monster_csleep(target_ptr, m_idx, 0);
671 GAME_TEXT m_name[MAX_NLEN];
672 monster_desc(target_ptr, m_name, m_ptr, 0);
673 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
676 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
684 * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
685 * @param target_ptr プレーヤーへの参照ポインタ
686 * @param m_idx モンスターID
687 * @param see_m モンスターが視界内にいたらTRUE
690 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
692 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
693 monster_race *r_ptr = &r_info[m_ptr->r_idx];
694 bool gets_angry = FALSE;
695 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
698 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
699 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
704 if (target_ptr->phase_out || !gets_angry) return;
706 if (is_pet(m_ptr) || see_m)
708 GAME_TEXT m_name[MAX_NLEN];
709 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
710 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
713 set_hostile(target_ptr, m_ptr);
719 * @param target_ptr プレーヤーへの参照ポインタ
720 * @param m_idx モンスターID
723 bool explode_grenade(player_type *target_ptr, MONSTER_IDX m_idx)
725 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
726 if (m_ptr->r_idx != MON_GRENADE) return FALSE;
729 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
735 * @brief モンスター依存の特別な行動を取らせる
736 * @param target_ptr プレーヤーへの参照ポインタ
737 * @param m_idx モンスターID
740 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
742 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
743 monster_race *r_ptr = &r_info[m_ptr->r_idx];
744 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
745 if (m_ptr->r_idx != MON_OHMU) return;
746 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
747 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
750 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
751 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
753 for (int k = 0; k < A_MAX; k++)
755 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
757 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
761 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
766 * @brief モンスターを喋らせたり足音を立てたりする
767 * @param target_ptr プレーヤーへの参照ポインタ
768 * @param m_idx モンスターID
769 * @param oy モンスターが元々いたY座標
770 * @param ox モンスターが元々いたX座標
771 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
774 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
776 if (target_ptr->phase_out) return;
778 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
779 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
780 if (m_ptr->ap_r_idx == MON_CYBER &&
781 one_in_(CYBERNOISE) &&
782 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
784 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
785 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
788 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
789 !one_in_(SPEAK_CHANCE) ||
790 !player_has_los_bold(target_ptr, oy, ox) ||
791 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
794 GAME_TEXT m_name[MAX_NLEN];
795 char monmessage[1024];
799 monster_desc(target_ptr, m_name, m_ptr, 0);
801 strcpy(m_name, _("それ", "It"));
803 if (MON_MONFEAR(m_ptr))
804 filename = _("monfear_j.txt", "monfear.txt");
805 else if (is_pet(m_ptr))
806 filename = _("monpet_j.txt", "monpet.txt");
807 else if (is_friendly(m_ptr))
808 filename = _("monfrien_j.txt", "monfrien.txt");
810 filename = _("monspeak_j.txt", "monspeak.txt");
812 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
814 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
820 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
821 * @param target_ptr プレーヤーへの参照ポインタ
822 * @param m_idx モンスターID
823 * @param oy 分裂元モンスターのY座標
824 * @param ox 分裂元モンスターのX座標
825 * @return 実際に分裂したらTRUEを返す
827 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
829 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
830 monster_race *r_ptr = &r_info[m_ptr->r_idx];
831 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) || (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
835 for (POSITION y = oy - 1; y <= oy + 1; y++)
837 for (POSITION x = ox - 1; x <= ox + 1; x++)
839 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
840 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
844 if (multiply_barrier(target_ptr, m_idx)) k = 8;
846 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
848 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
850 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
852 r_ptr->r_flags2 |= (RF2_MULTIPLY);
864 * @brief モンスターに魔法を試行させる
865 * @param target_ptr プレーヤーへの参照ポインタ
866 * @param m_idx モンスターID
867 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
868 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
870 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
872 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
873 monster_race *r_ptr = &r_info[m_ptr->r_idx];
874 if ((r_ptr->freq_spell == 0) || (randint1(100) > r_ptr->freq_spell))
877 bool counterattack = FALSE;
880 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
881 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
882 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
884 counterattack = TRUE;
890 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
891 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
895 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
896 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
904 * todo 少し長いが、これといってブロックとしてまとまった部分もないので暫定でこのままとする
905 * @brief モンスターの移動に関するメインルーチン
906 * @param target_ptr プレーヤーへの参照ポインタ
907 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
908 * @param m_idx モンスターID
909 * @param mm モンスターの移動方向
910 * @param oy 移動前の、モンスターのY座標
911 * @param ox 移動前の、モンスターのX座標
912 * @param count 移動回数 (のはず todo)
913 * @return 移動が阻害される何か (ドア等)があったらFALSE
915 bool process_monster_movement(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx, DIRECTION *mm, POSITION oy, POSITION ox, int *count)
917 for (int i = 0; mm[i]; i++)
920 if (d == 5) d = ddd[randint0(8)];
922 POSITION ny = oy + ddy[d];
923 POSITION nx = ox + ddx[d];
924 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
927 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
928 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
929 monster_race *r_ptr = &r_info[m_ptr->r_idx];
930 bool can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0);
932 if (!process_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx, can_cross))
934 if (!process_door(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
938 if (!process_protection_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
940 if (!process_explosive_rune(target_ptr, turn_flags_ptr, m_ptr, ny, nx))
944 exe_monster_attack_to_player(target_ptr, turn_flags_ptr, m_idx, ny, nx);
945 if (process_monster_attack_to_monster(target_ptr, turn_flags_ptr, m_idx, g_ptr, can_cross)) return FALSE;
947 if (turn_flags_ptr->is_riding_mon)
949 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) turn_flags_ptr->do_move = FALSE;
952 if (!process_post_dig_wall(target_ptr, turn_flags_ptr, m_ptr, ny, nx)) return FALSE;
954 if (turn_flags_ptr->must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
956 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, turn_flags_ptr->is_riding_mon ? CEM_RIDING : 0))
957 turn_flags_ptr->do_move = FALSE;
960 if (turn_flags_ptr->do_move && !can_cross && !turn_flags_ptr->did_kill_wall && !turn_flags_ptr->did_bash_door)
961 turn_flags_ptr->do_move = FALSE;
963 if (turn_flags_ptr->do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
965 if (is_original_ap_and_seen(target_ptr, m_ptr))
966 r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
968 turn_flags_ptr->do_move = FALSE;
971 if (!turn_flags_ptr->do_move)
973 if (turn_flags_ptr->do_turn) break;
978 turn_flags_ptr->do_turn = TRUE;
980 f_ptr = &f_info[g_ptr->feat];
981 if (have_flag(f_ptr->flags, FF_TREE))
983 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
985 m_ptr->energy_need += ENERGY_NEED();
989 if (!update_riding_monster(target_ptr, turn_flags_ptr, m_idx, oy, ox, ny, nx)) break;
991 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
994 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
995 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
997 if (is_hostile(m_ptr))
998 disturb(target_ptr, FALSE, TRUE);
1001 bool is_takable_or_killable = g_ptr->o_idx > 0;
1002 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1004 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1005 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1007 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1008 if (!is_takable_or_killable)
1010 if (turn_flags_ptr->do_turn) break;
1015 update_object_by_monster_movement(target_ptr, turn_flags_ptr, m_idx, ny, nx);
1016 if (turn_flags_ptr->do_turn) break;
1026 * @brief モンスターの恐怖状態を処理する
1027 * @param target_ptr プレーヤーへの参照ポインタ
1028 * @param turn_flags_ptr ターン経過処理フラグへの参照ポインタ
1029 * @param m_idx モンスターID
1030 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
1031 * @return モンスターが戦いを決意したらTRUE
1033 bool process_monster_fear(player_type *target_ptr, turn_flags *turn_flags_ptr, MONSTER_IDX m_idx)
1035 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1036 bool is_battle_determined = !turn_flags_ptr->do_turn && !turn_flags_ptr->do_move && MON_MONFEAR(m_ptr) && turn_flags_ptr->aware;
1037 if (!is_battle_determined) return FALSE;
1039 (void)set_monster_monfear(target_ptr, m_idx, 0);
1040 if (!turn_flags_ptr->see_m) return TRUE;
1042 GAME_TEXT m_name[MAX_NLEN];
1043 monster_desc(target_ptr, m_name, m_ptr, 0);
1044 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
1050 * @brief 全モンスターのターン管理メインルーチン /
1051 * Process all the "live" monsters, once per game turn.
1054 * During each game current game turn, we scan through the list of all the "live" monsters,\n
1055 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
1056 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
1058 * Note that monsters can never move in the monster array (except when the\n
1059 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
1061 * This function is responsible for at least half of the processor time\n
1062 * on a normal system with a "normal" amount of monsters and a player doing\n
1065 * When the player is resting, virtually 90% of the processor time is spent\n
1066 * in this function, and its children, "process_monster()" and "make_move()".\n
1068 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
1069 * especially when the player is running.\n
1071 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
1072 * monsters while they are still being "born". A monster is "fresh" only\n
1073 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
1074 * determine if the monster is yet to be processed during the game turn.\n
1076 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
1077 * move before any "nasty" monsters get to use their spell attacks.\n
1079 * Note that when the "knowledge" about the currently tracked monster\n
1080 * changes (flags, attacks, spells), we induce a redraw of the monster\n
1083 void process_monsters(player_type *target_ptr)
1085 old_race_flags tmp_flags;
1086 old_race_flags *old_race_flags_ptr = init_old_race_flags(&tmp_flags);
1088 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1089 floor_ptr->monster_noise = FALSE;
1091 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
1092 save_old_race_flags(target_ptr->monster_race_idx, old_race_flags_ptr);
1093 sweep_monster_process(target_ptr);
1096 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
1099 update_player_window(target_ptr, old_race_flags_ptr);
1104 * @brief フロア内のモンスターについてターン終了時の処理を繰り返す
1105 * @param target_ptr プレーヤーへの参照ポインタ
1107 void sweep_monster_process(player_type *target_ptr)
1109 floor_type *floor_ptr = target_ptr->current_floor_ptr;
1110 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
1112 monster_type *m_ptr;
1113 m_ptr = &floor_ptr->m_list[i];
1115 if (target_ptr->leaving) return;
1116 if (!monster_is_valid(m_ptr)) continue;
1117 if (target_ptr->wild_mode) continue;
1119 if (m_ptr->mflag & MFLAG_BORN)
1121 m_ptr->mflag &= ~(MFLAG_BORN);
1125 if (m_ptr->cdis >= AAF_LIMIT) continue;
1126 if (!decide_process_continue(target_ptr, m_ptr)) continue;
1128 SPEED speed = (target_ptr->riding == i) ? target_ptr->pspeed : decide_monster_speed(m_ptr);
1129 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
1130 if (m_ptr->energy_need > 0) continue;
1132 m_ptr->energy_need += ENERGY_NEED();
1134 process_monster(target_ptr, i);
1135 reset_target(m_ptr);
1137 if (target_ptr->no_flowed && one_in_(3))
1138 m_ptr->mflag2 |= MFLAG2_NOFLOW;
1140 if (!target_ptr->playing || target_ptr->is_dead) return;
1141 if (target_ptr->leaving) return;
1147 * @brief 後続のモンスター処理が必要かどうか判定する (要調査)
1148 * @param target_ptr プレーヤーへの参照ポインタ
1149 * @param m_ptr モンスターへの参照ポインタ
1150 * @return 後続処理が必要ならTRUE
1152 bool decide_process_continue(player_type *target_ptr, monster_type *m_ptr)
1154 monster_race *r_ptr;
1155 r_ptr = &r_info[m_ptr->r_idx];
1156 if (!target_ptr->no_flowed)
1158 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1161 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
1164 if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
1165 (player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
1168 if (m_ptr->target_y)