2 * @file monster-process.c
3 * @brief モンスターの特殊技能と移動処理/ Monster spells and movement
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).
24 #include "spells-floor.h"
25 #include "spells-summon.h"
28 #include "realm-hex.h"
29 #include "object-flavor.h"
30 #include "object-hook.h"
33 #include "player-move.h"
34 #include "monster-status.h"
35 #include "monster-spell.h"
36 #include "monster-process.h"
37 #include "monsterrace-hook.h"
41 #include "view-mainwindow.h"
45 * @brief モンスターが敵に接近するための方向を決める /
46 * Calculate the direction to the next enemy
47 * @param m_idx モンスターの参照ID
48 * @param mm 移動するべき方角IDを返す参照ポインタ
49 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
51 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
54 POSITION x = 0, y = 0;
59 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
60 monster_race *r_ptr = &r_info[m_ptr->r_idx];
63 if (p_ptr->riding_t_m_idx && player_bold(p_ptr, m_ptr->fy, m_ptr->fx))
65 y = p_ptr->current_floor_ptr->m_list[p_ptr->riding_t_m_idx].fy;
66 x = p_ptr->current_floor_ptr->m_list[p_ptr->riding_t_m_idx].fx;
68 else if (is_pet(m_ptr) && p_ptr->pet_t_m_idx)
70 y = p_ptr->current_floor_ptr->m_list[p_ptr->pet_t_m_idx].fy;
71 x = p_ptr->current_floor_ptr->m_list[p_ptr->pet_t_m_idx].fx;
77 start = randint1(p_ptr->current_floor_ptr->m_max-1)+p_ptr->current_floor_ptr->m_max;
78 if(randint0(2)) plus = -1;
80 else start = p_ptr->current_floor_ptr->m_max + 1;
82 /* Scan thru all monsters */
83 for (i = start; ((i < start + p_ptr->current_floor_ptr->m_max) && (i > start - p_ptr->current_floor_ptr->m_max)); i+=plus)
85 MONSTER_IDX dummy = (i % p_ptr->current_floor_ptr->m_max);
90 t_ptr = &p_ptr->current_floor_ptr->m_list[t_idx];
92 /* The monster itself isn't a target */
93 if (t_ptr == m_ptr) continue;
95 if (!monster_is_valid(t_ptr)) continue;
99 /* Hack -- only fight away from player */
100 if (p_ptr->pet_follow_distance < 0)
102 /* No fighting near player */
103 if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
108 /* Hack -- no fighting away from player */
109 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
114 if (r_ptr->aaf < t_ptr->cdis) continue;
117 /* Monster must be 'an enemy' */
118 if (!are_enemies(m_ptr, t_ptr)) continue;
120 /* Monster must be projectable if we can't pass through walls */
121 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
122 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
124 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
128 if (!projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
131 /* OK -- we've got a target */
137 if (!x && !y) return FALSE;
140 /* Extract the direction */
145 if ((y < 0) && (x == 0))
152 else if ((y > 0) && (x == 0))
159 else if ((x > 0) && (y == 0))
166 else if ((x < 0) && (y == 0))
173 else if ((y < 0) && (x < 0))
180 else if ((y < 0) && (x > 0))
187 else if ((y > 0) && (x < 0))
194 else if ((y > 0) && (x > 0))
201 /* Found a monster */
206 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
207 * Returns whether a given monster will try to run from the player.
208 * @param m_idx 逃走するモンスターの参照ID
209 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
211 * Monsters will attempt to avoid very powerful players. See below.\n
213 * Because this function is called so often, little details are important\n
214 * for efficiency. Like not using "mod" or "div" when possible. And\n
215 * attempting to check the conditions in an optimal order. Note that\n
216 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
218 * Note that this function is responsible for about one to five percent\n
219 * of the processor use in normal conditions...\n
221 static bool mon_will_run(MONSTER_IDX m_idx)
223 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
227 monster_race *r_ptr = &r_info[m_ptr->r_idx];
231 HIT_POINT p_chp, p_mhp;
232 HIT_POINT m_chp, m_mhp;
237 /* Friends can be commanded to avoid the player */
240 /* Are we trying to avoid the player? */
241 return ((p_ptr->pet_follow_distance < 0) &&
242 (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
245 /* Keep monsters from running too far away */
246 if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
248 /* All "afraid" monsters will run away */
249 if (MON_MONFEAR(m_ptr)) return (TRUE);
253 /* Nearby monsters will not become terrified */
254 if (m_ptr->cdis <= 5) return (FALSE);
256 /* Examine player power (level) */
259 /* Examine monster power (level plus morale) */
260 m_lev = r_ptr->level + (m_idx & 0x08) + 25;
262 /* Optimize extreme cases below */
263 if (m_lev > p_lev + 4) return (FALSE);
264 if (m_lev + 4 <= p_lev) return (TRUE);
266 /* Examine player health */
270 /* Examine monster health */
272 m_mhp = m_ptr->maxhp;
274 /* Prepare to optimize the calculation */
275 p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
276 m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
278 /* Strong players scare strong monsters */
279 if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
283 /* Assume no terror */
289 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
290 * Search spell castable grid
291 * @param m_idx モンスターの参照ID
292 * @param yp 適したマスのY座標を返す参照ポインタ
293 * @param xp 適したマスのX座標を返す参照ポインタ
294 * @return 有効なマスがあった場合TRUEを返す
296 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
299 POSITION y, x, y1, x1;
302 bool can_open_door = FALSE;
305 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
306 monster_race *r_ptr = &r_info[m_ptr->r_idx];
308 /* Monster location */
312 /* Monster can already cast spell to player */
313 if (projectable(p_ptr->current_floor_ptr, y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
315 /* Set current grid cost */
316 now_cost = p_ptr->current_floor_ptr->grid_array[y1][x1].cost;
317 if (now_cost == 0) now_cost = 999;
319 /* Can monster bash or open doors? */
320 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
322 can_open_door = TRUE;
325 /* Check nearby grids, diagonals first */
326 for (i = 7; i >= 0; i--)
333 /* Ignore locations off of edge */
334 if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
336 /* Simply move to player */
337 if (player_bold(p_ptr, y, x)) return (FALSE);
339 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
343 /* Monster cannot kill or pass walls */
344 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding))))
346 if (cost == 0) continue;
347 if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
350 /* Hack -- for kill or pass wall monster.. */
351 if (cost == 0) cost = 998;
353 if (now_cost < cost) continue;
355 if (!projectable(p_ptr->current_floor_ptr, y, x, p_ptr->y, p_ptr->x)) continue;
357 /* Accept louder sounds */
358 if (best < cost) continue;
361 (*yp) = y1 + ddy_ddd[i];
362 (*xp) = x1 + ddx_ddd[i];
365 /* No legal move (?) */
366 if (best == 999) return (FALSE);
374 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
375 * Choose the "best" direction for "flowing"
376 * @param m_idx モンスターの参照ID
377 * @param yp 移動先のマスのY座標を返す参照ポインタ
378 * @param xp 移動先のマスのX座標を返す参照ポインタ
379 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
380 * @return 有効なマスがあった場合TRUEを返す
382 * Note that ghosts and rock-eaters are never allowed to "flow",\n
383 * since they should move directly towards the player.\n
385 * Prefer "non-diagonal" directions, but twiddle them a little\n
386 * to angle slightly towards the player's actual location.\n
388 * Allow very perceptive monsters to track old "spoor" left by\n
389 * previous locations occupied by the player. This will tend\n
390 * to have monsters end up either near the player or on a grid\n
391 * recently occupied by the player (and left via "teleport").\n
393 * Note that if "smell" is turned on, all monsters get vicious.\n
395 * Also note that teleporting away from a location will cause\n
396 * the monsters who were chasing you to converge on that location\n
397 * as long as you are still near enough to "annoy" them without\n
398 * being close enough to chase directly. I have no idea what will\n
399 * happen if you combine "smell" with low "aaf" values.\n
401 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
404 POSITION y, x, y1, x1;
407 bool use_scent = FALSE;
409 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
410 monster_race *r_ptr = &r_info[m_ptr->r_idx];
412 /* Can monster cast attack spell? */
413 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
414 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
415 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
417 /* Can move spell castable grid? */
418 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
421 /* Monster can't flow */
422 if (no_flow) return (FALSE);
424 /* Monster can go through rocks */
425 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
426 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
428 /* Monster location */
432 /* Hack -- Player can see us, run towards him */
433 if (player_has_los_bold(p_ptr, y1, x1) && projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
436 g_ptr = &p_ptr->current_floor_ptr->grid_array[y1][x1];
438 /* If we can hear noises, advance towards them */
444 /* Otherwise, try to follow a scent trail */
445 else if (g_ptr->when)
448 if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
454 /* Otherwise, advance blindly */
460 /* Check nearby grids, diagonals first */
461 for (i = 7; i >= 0; i--)
466 /* Ignore locations off of edge */
467 if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
469 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
471 /* We're following a scent trail */
474 int when = g_ptr->when;
476 /* Accept younger scent */
477 if (best > when) continue;
481 /* We're using sound */
486 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
488 else cost = g_ptr->cost;
490 /* Accept louder sounds */
491 if ((cost == 0) || (best < cost)) continue;
495 /* Hack -- Save the "twiddled" location */
496 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
497 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
500 /* No legal move (?) */
501 if (best == 999 || best == 0) return (FALSE);
509 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
510 * Provide a location to flee to, but give the player a wide berth.
511 * @param m_idx モンスターの参照ID
512 * @param yp 移動先のマスのY座標を返す参照ポインタ
513 * @param xp 移動先のマスのX座標を返す参照ポインタ
514 * @return 有効なマスがあった場合TRUEを返す
516 * A monster may wish to flee to a location that is behind the player,\n
517 * but instead of heading directly for it, the monster should "swerve"\n
518 * around the player so that he has a smaller chance of getting hit.\n
520 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
522 POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
526 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
528 /* Monster location */
532 /* Desired destination */
536 /* Check nearby grids, diagonals first */
537 for (i = 7; i >= 0; i--)
544 /* Ignore locations off of edge */
545 if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
547 /* Don't move toward player */
548 /* if (p_ptr->current_floor_ptr->grid_array[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
550 /* Calculate distance of this grid from our destination */
551 dis = distance(y, x, y1, x1);
553 /* Score this grid */
554 s = 5000 / (dis + 3) - 500 / (p_ptr->current_floor_ptr->grid_array[y][x].dist + 1);
556 /* No negative scores */
559 /* Ignore lower scores */
560 if (s < score) continue;
562 /* Save the score and time */
565 /* Save the location */
570 /* No legal move (?) */
571 if (score == -1) return (FALSE);
582 * Hack -- Precompute a bunch of calls to distance() in find_safety() and
585 * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
586 * offsets of all the locations with a distance of n from a central point,
587 * with an offset of (0,0) indicating no more offsets at this distance.
589 * This is, of course, fairly unreadable, but it eliminates multiple loops
590 * from the previous version.
592 * It is probably better to replace these arrays with code to compute
593 * the relevant arrays, even if the storage is pre-allocated in hard
594 * coded sizes. At the very least, code should be included which is
595 * able to generate and dump these arrays (ala "los()").
597 * Also, the storage needs could be halved by using bytes.
599 * These arrays could be combined into two big arrays, using sub-arrays
600 * to hold the offsets and lengths of each portion of the sub-arrays, and
601 * this could perhaps also be used somehow in the "look" code.
605 static POSITION d_off_y_0[] = { 0 };
606 static POSITION d_off_x_0[] = { 0 };
608 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
609 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
611 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
612 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
614 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
615 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
617 static POSITION d_off_y_4[] = { -1, -1, -2, -2, -3, -3, -3, -3, -4, -4, -4, 0, 0, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 0 };
618 static POSITION d_off_x_4[] = { -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, -4, 4, -4, 4, -3, 3, -2, -3, 2, 3, -1, 0, 1, 0 };
621 static POSITION d_off_y_5[] =
622 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
623 -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
626 static POSITION d_off_x_5[] =
627 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
628 -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
632 static POSITION d_off_y_6[] =
633 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
634 -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
637 static POSITION d_off_x_6[] =
638 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
639 0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
640 -3, 2, 3, -1, 0, 1, 0 };
643 static POSITION d_off_y_7[] =
644 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
645 -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
646 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
648 static POSITION d_off_x_7[] =
649 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
650 -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
651 6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
655 static POSITION d_off_y_8[] =
656 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
657 -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
658 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
661 static POSITION d_off_x_8[] =
662 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
663 5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
664 7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
668 static POSITION d_off_y_9[] =
669 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
670 -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
671 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
672 7, 8, 8, 8, 8, 9, 9, 9, 0 };
674 static POSITION d_off_x_9[] =
675 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
676 -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
677 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
678 4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
681 static POSITION *dist_offsets_y[10] =
683 d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
684 d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
687 static POSITION *dist_offsets_x[10] =
689 d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
690 d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
694 * @brief モンスターが逃げ込める安全な地点を返す /
695 * Choose a "safe" location near a monster for it to run toward.
696 * @param m_idx モンスターの参照ID
697 * @param yp 移動先のマスのY座標を返す参照ポインタ
698 * @param xp 移動先のマスのX座標を返す参照ポインタ
699 * @return 有効なマスがあった場合TRUEを返す
701 * A location is "safe" if it can be reached quickly and the player\n
702 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
703 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
704 * try to run towards corridor openings if they are in a room.\n
706 * This function may take lots of CPU time if lots of monsters are\n
709 * Return TRUE if a safe location is available.\n
711 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
713 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
715 POSITION fy = m_ptr->fy;
716 POSITION fx = m_ptr->fx;
718 POSITION y, x, dy, dx, d, dis, i;
719 POSITION gy = 0, gx = 0, gdis = 0;
726 /* Start with adjacent locations, spread further */
727 for (d = 1; d < 10; d++)
729 /* Get the lists of points with a distance d from (fx, fy) */
730 y_offsets = dist_offsets_y[d];
731 x_offsets = dist_offsets_x[d];
733 /* Check the locations */
734 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
736 i++, dx = x_offsets[i], dy = y_offsets[i])
741 /* Skip illegal locations */
742 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
744 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
746 /* Skip locations in a wall */
747 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
749 /* Check for "availability" (if monsters can flow) */
750 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
752 /* Ignore grids very far from the player */
753 if (g_ptr->dist == 0) continue;
755 /* Ignore too-distant grids */
756 if (g_ptr->dist > p_ptr->current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
759 /* Check for absence of shot (more or less) */
760 if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x))
762 /* Calculate distance from player */
763 dis = distance(y, x, p_ptr->y, p_ptr->x);
765 /* Remember if further than previous */
775 /* Check for success */
782 /* Found safe place */
793 * @brief モンスターが隠れ潜める地点を返す /
794 * Choose a good hiding place near a monster for it to run toward.
795 * @param m_idx モンスターの参照ID
796 * @param yp 移動先のマスのY座標を返す参照ポインタ
797 * @param xp 移動先のマスのX座標を返す参照ポインタ
798 * @return 有効なマスがあった場合TRUEを返す
800 * Pack monsters will use this to "ambush" the player and lure him out\n
801 * of corridors into open space so they can swarm him.\n
803 * Return TRUE if a good location is available.\n
805 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
807 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
808 monster_race *r_ptr = &r_info[m_ptr->r_idx];
810 POSITION fy = m_ptr->fy;
811 POSITION fx = m_ptr->fx;
813 POSITION y, x, dy, dx, d, dis, i;
814 POSITION gy = 0, gx = 0, gdis = 999;
816 POSITION *y_offsets, *x_offsets;
818 /* Start with adjacent locations, spread further */
819 for (d = 1; d < 10; d++)
821 /* Get the lists of points with a distance d from (fx, fy) */
822 y_offsets = dist_offsets_y[d];
823 x_offsets = dist_offsets_x[d];
825 /* Check the locations */
826 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
828 i++, dx = x_offsets[i], dy = y_offsets[i])
833 /* Skip illegal locations */
834 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
836 /* Skip occupied locations */
837 if (!monster_can_enter(y, x, r_ptr, 0)) continue;
839 /* Check for hidden, available grid */
840 if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
842 /* Calculate distance from player */
843 dis = distance(y, x, p_ptr->y, p_ptr->x);
845 /* Remember if closer than previous */
846 if (dis < gdis && dis >= 2)
855 /* Check for success */
862 /* Found good place */
873 * @brief モンスターの移動方向を返す /
874 * Choose "logical" directions for monster movement
875 * @param m_idx モンスターの参照ID
876 * @param mm 移動方向を返す方向IDの参照ポインタ
877 * @return 有効方向があった場合TRUEを返す
879 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
881 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
882 monster_race *r_ptr = &r_info[m_ptr->r_idx];
883 POSITION y = 0, ay, x = 0, ax;
885 POSITION y2 = p_ptr->y;
886 POSITION x2 = p_ptr->x;
888 bool will_run = mon_will_run(m_idx);
890 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
891 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
893 /* Counter attack to an enemy monster */
894 if (!will_run && m_ptr->target_y)
896 int t_m_idx = p_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
898 /* The monster must be an enemy, and in LOS */
900 are_enemies(m_ptr, &p_ptr->current_floor_ptr->m_list[t_m_idx]) &&
901 los(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
902 projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
904 /* Extract the "pseudo-direction" */
905 y = m_ptr->fy - m_ptr->target_y;
906 x = m_ptr->fx - m_ptr->target_x;
911 if (!done && !will_run && is_hostile(m_ptr) &&
912 (r_ptr->flags1 & RF1_FRIENDS) &&
913 ((los(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
914 (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
917 * Animal packs try to get the player out of corridors
918 * (...unless they can move through walls -- TY)
920 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
921 !(r_ptr->flags2 & RF2_KILL_WALL))
925 /* Count room grids next to player */
926 for (i = 0; i < 8; i++)
928 int xx = p_ptr->x + ddx_ddd[i];
929 int yy = p_ptr->y + ddy_ddd[i];
931 if (!in_bounds2(p_ptr->current_floor_ptr, yy, xx)) continue;
933 g_ptr = &p_ptr->current_floor_ptr->grid_array[yy][xx];
936 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
938 /* One more room grid */
942 if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
943 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
945 /* Not in a room and strong player */
946 if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
947 (p_ptr->mhp + p_ptr->msp))
949 /* Find hiding place */
950 if (find_hiding(m_idx, &y, &x)) done = TRUE;
954 /* Monster groups try to surround the player */
955 if (!done && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
959 /* Find an empty square near the player to fill */
960 for (i = 0; i < 8; i++)
962 /* Pick squares near player (semi-randomly) */
963 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
964 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
967 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
969 /* Attack the player */
976 if (!in_bounds2(p_ptr->current_floor_ptr, y2, x2)) continue;
978 /* Ignore filled grids */
979 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
981 /* Try to fill this hole */
985 /* Extract the new "pseudo-direction" */
995 /* Flow towards the player */
996 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
998 /* Extract the "pseudo-direction" */
1005 /* Apply fear if possible and necessary */
1006 if (is_pet(m_ptr) && will_run)
1008 /* XXX XXX Not very "smart" */
1013 if (!done && will_run)
1018 /* Try to find safe place */
1019 if (find_safety(m_idx, &y, &x))
1021 /* Attempt to avoid the player */
1024 /* Adjust movement */
1025 if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1031 /* This is not a very "smart" method XXX XXX */
1039 /* Check for no move */
1040 if (!x && !y) return (FALSE);
1043 /* Extract the "absolute distances" */
1047 /* Do something weird */
1048 if (y < 0) move_val += 8;
1049 if (x > 0) move_val += 4;
1051 /* Prevent the diamond maneuvre */
1052 if (ay > (ax << 1)) move_val += 2;
1053 else if (ax > (ay << 1)) move_val++;
1055 /* Extract some directions */
1200 /* Wants to move... */
1205 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1207 return !have_flag(f_ptr->flags, FF_GLASS) ||
1208 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1209 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1214 * @brief モンスター単体の1ターン行動処理メインルーチン /
1216 * @param m_idx 行動モンスターの参照ID
1219 * The monster is known to be within 100 grids of the player\n
1221 * In several cases, we directly update the monster lore\n
1223 * Note that a monster is only allowed to "reproduce" if there\n
1224 * are a limited number of "reproducing" monsters on the current\n
1225 * level. This should prevent the level from being "swamped" by\n
1226 * reproducing monsters. It also allows a large mass of mice to\n
1227 * prevent a louse from multiplying, but this is a small price to\n
1228 * pay for a simple multiplication method.\n
1230 * XXX Monster fear is slightly odd, in particular, monsters will\n
1231 * fixate on opening a door even if they cannot open it. Actually,\n
1232 * the same thing happens to normal monsters when they hit a door\n
1234 * In addition, monsters which *cannot* open or bash\n
1235 * down a door will still stand there trying to open it...\n
1237 * XXX Technically, need to check for monster in the way\n
1238 * combined with that monster being in a wall (or door?)\n
1240 * A "direction" of "5" means "pick a random direction".\n
1242 void process_monster(MONSTER_IDX m_idx)
1244 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1245 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1246 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1249 POSITION oy, ox, ny, nx;
1254 feature_type *f_ptr;
1256 monster_type *y_ptr;
1261 bool must_alter_to_move;
1270 bool gets_angry = FALSE;
1275 bool is_riding_mon = (m_idx == p_ptr->riding);
1276 bool see_m = is_seen(m_ptr);
1278 if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1280 if (rakuba(p_ptr, 0, TRUE))
1283 msg_print("地面に落とされた。");
1285 GAME_TEXT m_name[MAX_NLEN];
1286 monster_desc(m_name, &p_ptr->current_floor_ptr->m_list[p_ptr->riding], 0);
1287 msg_format("You have fallen from %s.", m_name);
1292 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1294 choose_new_monster(m_idx, FALSE, 0);
1295 r_ptr = &r_info[m_ptr->r_idx];
1298 /* Players hidden in shadow are almost imperceptable. -LM- */
1299 if (p_ptr->special_defense & NINJA_S_STEALTH)
1301 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
1302 if (p_ptr->monlite) tmp /= 3;
1303 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1304 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
1305 /* Low-level monsters will find it difficult to locate the player. */
1306 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
1309 /* Are there its parent? */
1310 if (m_ptr->parent_m_idx && !p_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
1312 /* Its parent have gone, it also goes away. */
1316 GAME_TEXT m_name[MAX_NLEN];
1317 monster_desc(m_name, m_ptr, 0);
1318 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1321 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1323 GAME_TEXT m_name[MAX_NLEN];
1324 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1325 exe_write_diary(p_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1328 delete_monster_idx(m_idx);
1333 /* Quantum monsters are odd */
1334 if (r_ptr->flags2 & (RF2_QUANTUM))
1336 /* Sometimes skip move */
1337 if (!randint0(2)) return;
1340 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
1344 if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
1348 GAME_TEXT m_name[MAX_NLEN];
1349 monster_desc(m_name, m_ptr, 0);
1351 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1354 /* Generate treasure, etc */
1355 monster_death(m_idx, FALSE);
1357 delete_monster_idx(m_idx);
1360 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
1367 if (m_ptr->r_idx == MON_SHURYUUDAN)
1369 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1373 if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->phase_out)
1375 static int riding_pinch = 0;
1377 if (m_ptr->hp < m_ptr->maxhp/3)
1379 GAME_TEXT m_name[MAX_NLEN];
1380 monster_desc(m_name, m_ptr, 0);
1382 if (is_riding_mon && riding_pinch < 2)
1384 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1385 "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
1387 disturb(p_ptr, TRUE, TRUE);
1393 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1394 if (rakuba(p_ptr, -1, FALSE))
1396 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1402 if ((r_ptr->flags2 & RF2_CAN_SPEAK) && (m_ptr->r_idx != MON_GRIP) && (m_ptr->r_idx != MON_WOLF) && (m_ptr->r_idx != MON_FANG) &&
1403 player_has_los_bold(p_ptr, m_ptr->fy, m_ptr->fx) && projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
1405 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1407 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
1408 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1411 if (is_riding_mon && rakuba(p_ptr, -1, FALSE))
1413 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1416 check_quest_completion(m_ptr);
1417 delete_monster_idx(m_idx);
1423 /* Reset the counter */
1424 if (is_riding_mon) riding_pinch = 0;
1428 /* Handle "sleep" */
1429 if (MON_CSLEEP(m_ptr))
1431 /* Handle non-aggravation - Still sleeping */
1432 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
1434 (void)set_monster_csleep(m_idx, 0);
1436 /* Notice the "waking up" */
1439 GAME_TEXT m_name[MAX_NLEN];
1440 monster_desc(m_name, m_ptr, 0);
1441 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1444 /* Hack -- Count the wakings */
1445 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1452 if (MON_STUNNED(m_ptr))
1454 /* Sometimes skip move */
1455 if (one_in_(2)) return;
1460 p_ptr->update |= (PU_BONUS);
1463 /* No one wants to be your friend if you're aggravating */
1464 if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
1467 /* Paranoia... no pet uniques outside wizard mode -- TY */
1468 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1469 monster_has_hostile_align(NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1474 if (p_ptr->phase_out) gets_angry = FALSE;
1478 if (is_pet(m_ptr) || see_m)
1480 GAME_TEXT m_name[MAX_NLEN];
1481 monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1482 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1488 /* Get the origin */
1492 /* Attempt to "multiply" if able and allowed */
1493 if ((r_ptr->flags2 & RF2_MULTIPLY) && (p_ptr->current_floor_ptr->num_repro < MAX_REPRO))
1498 /* Count the adjacent monsters */
1499 for (k = 0, y = oy - 1; y <= oy + 1; y++)
1501 for (x = ox - 1; x <= ox + 1; x++)
1503 /* Ignore locations off of edge */
1504 if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
1505 if (p_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1509 if (multiply_barrier(m_idx)) k = 8;
1511 /* Hack -- multiply slower in crowded areas */
1512 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1514 /* Try to multiply */
1515 if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1517 /* Take note if visible */
1518 if (p_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
1520 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1523 /* Multiplying takes energy */
1529 if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
1531 /* Hack -- Ohmu scatters molds! */
1532 if (m_ptr->r_idx == MON_OHMU)
1534 if (!p_ptr->current_floor_ptr->inside_arena && !p_ptr->phase_out)
1536 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
1539 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1540 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1542 for (k = 0; k < A_MAX; k++)
1544 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1546 if (p_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1550 if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1556 if (!p_ptr->phase_out)
1558 /* Hack! "Cyber" monster makes noise... */
1559 if (m_ptr->ap_r_idx == MON_CYBER &&
1560 one_in_(CYBERNOISE) &&
1561 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1563 if (disturb_minor) disturb(p_ptr, FALSE, FALSE);
1564 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1567 /* Some monsters can speak */
1568 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
1569 one_in_(SPEAK_CHANCE) &&
1570 player_has_los_bold(p_ptr, oy, ox) &&
1571 projectable(p_ptr->current_floor_ptr, oy, ox, p_ptr->y, p_ptr->x))
1573 GAME_TEXT m_name[MAX_NLEN];
1574 char monmessage[1024];
1577 /* Acquire the monster name/poss */
1579 monster_desc(m_name, m_ptr, 0);
1581 strcpy(m_name, _("それ", "It"));
1583 /* Select the file for monster quotes */
1584 if (MON_MONFEAR(m_ptr))
1585 filename = _("monfear_j.txt", "monfear.txt");
1586 else if (is_pet(m_ptr))
1587 filename = _("monpet_j.txt", "monpet.txt");
1588 else if (is_friendly(m_ptr))
1589 filename = _("monfrien_j.txt", "monfrien.txt");
1591 filename = _("monspeak_j.txt", "monspeak.txt");
1592 /* Get the monster line */
1593 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1596 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1601 /* Try to cast spell occasionally */
1602 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1604 bool counterattack = FALSE;
1606 /* Give priority to counter attack? */
1607 if (m_ptr->target_y)
1609 MONSTER_IDX t_m_idx = p_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1611 /* The monster must be an enemy, and projectable */
1612 if (t_m_idx && are_enemies(m_ptr, &p_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1613 projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1615 counterattack = TRUE;
1621 /* Attempt to cast a spell */
1622 if (aware && make_attack_spell(m_idx, p_ptr)) return;
1625 * Attempt to cast a spell at an enemy other than the player
1626 * (may slow the game a smidgeon, but I haven't noticed.)
1628 if (monst_spell_monst(m_idx)) return;
1632 /* Attempt to do counter attack at first */
1633 if (monst_spell_monst(m_idx)) return;
1635 if (aware && make_attack_spell(m_idx, p_ptr)) return;
1639 /* Hack -- Assume no movement */
1640 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1641 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1644 /* Confused -- 100% random */
1645 if (MON_CONFUSED(m_ptr) || !aware)
1647 /* Try four "random" directions */
1648 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1651 /* 75% random movement */
1652 else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
1653 (randint0(100) < 75))
1655 /* Memorize flags */
1656 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1658 /* Try four "random" directions */
1659 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1662 /* 50% random movement */
1663 else if ((r_ptr->flags1 & RF1_RAND_50) &&
1664 (randint0(100) < 50))
1666 /* Memorize flags */
1667 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
1669 /* Try four "random" directions */
1670 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1673 /* 25% random movement */
1674 else if ((r_ptr->flags1 & RF1_RAND_25) &&
1675 (randint0(100) < 25))
1677 /* Memorize flags */
1678 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1680 /* Try four "random" directions */
1681 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1684 /* Can't reach player - find something else to hit */
1685 else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1687 /* Try four "random" directions */
1688 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1690 /* Look for an enemy */
1691 #if 0 /* Hack - Too slow. Mimic pits are horrible with this on. */
1692 get_enemy_dir(m_idx, mm);
1696 /* Pets will follow the player */
1697 else if (is_pet(m_ptr))
1699 /* Are we trying to avoid the player? */
1700 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
1702 /* Do we want to find the player? */
1703 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
1705 /* Should we find the player if we can't find a monster? */
1706 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1708 /* by default, move randomly */
1709 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1711 /* Look for an enemy */
1712 if (!get_enemy_dir(m_idx, mm))
1714 /* Find the player if necessary */
1715 if (avoid || lonely || distant)
1717 /* Remember the leash length */
1718 POSITION dis = p_ptr->pet_follow_distance;
1720 /* Hack -- adjust follow distance temporarily */
1721 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
1723 p_ptr->pet_follow_distance = PET_SEEK_DIST;
1726 /* Find the player */
1727 (void)get_moves(m_idx, mm);
1729 /* Restore the leash */
1730 p_ptr->pet_follow_distance = (s16b)dis;
1735 /* Friendly monster movement */
1736 else if (!is_hostile(m_ptr))
1738 /* by default, move randomly */
1739 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1741 /* Look for an enemy */
1742 get_enemy_dir(m_idx, mm);
1744 /* Normal movement */
1747 /* Logical moves, may do nothing */
1748 if (!get_moves(m_idx, mm)) return;
1751 /* Assume nothing */
1755 must_alter_to_move = FALSE;
1757 /* Assume nothing */
1758 did_open_door = FALSE;
1759 did_bash_door = FALSE;
1760 did_take_item = FALSE;
1761 did_kill_item = FALSE;
1762 did_move_body = FALSE;
1763 did_pass_wall = FALSE;
1764 did_kill_wall = FALSE;
1766 /* Take a zero-terminated array of "directions" */
1767 for (i = 0; mm[i]; i++)
1769 /* Get the direction */
1772 /* Hack -- allow "randomized" motion */
1773 if (d == 5) d = ddd[randint0(8)];
1775 /* Get the destination */
1779 /* Ignore locations off of edge */
1780 if (!in_bounds2(p_ptr->current_floor_ptr, ny, nx)) continue;
1782 /* Access that grid */
1783 g_ptr = &p_ptr->current_floor_ptr->grid_array[ny][nx];
1784 f_ptr = &f_info[g_ptr->feat];
1785 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1787 /* Access that grid's contents */
1788 y_ptr = &p_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1790 /* Hack -- player 'in' wall */
1791 if (player_bold(p_ptr, ny, nx))
1796 /* Possibly a monster to attack */
1797 else if (g_ptr->m_idx)
1802 /* Monster destroys walls (and doors) */
1803 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1804 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1805 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1806 check_hp_for_feat_destruction(f_ptr, m_ptr))
1808 /* Eat through walls/doors/rubble */
1810 if (!can_cross) must_alter_to_move = TRUE;
1812 /* Monster destroyed a wall (later) */
1813 did_kill_wall = TRUE;
1816 /* Floor is open? */
1819 /* Go ahead and move */
1822 /* Monster moves through walls (and doors) */
1823 if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
1824 have_flag(f_ptr->flags, FF_CAN_PASS))
1826 /* Monster went through a wall */
1827 did_pass_wall = TRUE;
1831 /* Handle doors and secret doors */
1832 else if (is_closed_door(g_ptr->feat))
1834 bool may_bash = TRUE;
1836 /* Assume no move allowed */
1839 /* Creature can open doors. */
1840 if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1841 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1846 /* The door is open */
1847 did_open_door = TRUE;
1849 /* Do not bash the door */
1855 /* Locked doors (not jammed) */
1858 /* Try to unlock it */
1859 if (randint0(m_ptr->hp / 10) > f_ptr->power)
1861 /* Unlock the door */
1862 cave_alter_feat(ny, nx, FF_DISARM);
1864 /* Do not bash the door */
1872 /* Stuck doors -- attempt to bash them down if allowed */
1873 if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1874 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1876 /* Attempt to Bash */
1877 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1879 if (have_flag(f_ptr->flags, FF_GLASS))
1880 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
1882 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1884 /* Disturb (sometimes) */
1885 if (disturb_minor) disturb(p_ptr, FALSE, FALSE);
1887 /* The door was bashed open */
1888 did_bash_door = TRUE;
1890 /* Hack -- fall into doorway */
1892 must_alter_to_move = TRUE;
1897 /* Deal with doors in the way */
1898 if (did_open_door || did_bash_door)
1900 /* Break down the door */
1901 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1903 cave_alter_feat(ny, nx, FF_BASH);
1905 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1907 p_ptr->update |= (PU_FLOW);
1908 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1909 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1918 cave_alter_feat(ny, nx, FF_OPEN);
1921 f_ptr = &f_info[g_ptr->feat];
1923 /* Handle viewable doors */
1928 /* Hack -- check for Glyph of Warding */
1929 if (do_move && is_glyph_grid(g_ptr) &&
1930 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(p_ptr, ny, nx)))
1932 /* Assume no move allowed */
1935 /* Break the ward */
1936 if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1938 /* Describe observable breakage */
1939 if (g_ptr->info & CAVE_MARK)
1941 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1944 /* Forget the rune */
1945 g_ptr->info &= ~(CAVE_MARK);
1947 /* Break the rune */
1948 g_ptr->info &= ~(CAVE_OBJECT);
1951 /* Allow movement */
1957 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1958 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(p_ptr, ny, nx)))
1960 /* Assume no move allowed */
1963 /* Break the ward */
1966 /* Break the ward */
1967 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1969 /* Describe observable breakage */
1970 if (g_ptr->info & CAVE_MARK)
1972 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1973 project(p_ptr, 0, 2, ny, nx, 2 * (p_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1978 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1981 /* Forget the rune */
1982 g_ptr->info &= ~(CAVE_MARK);
1984 /* Break the rune */
1985 g_ptr->info &= ~(CAVE_OBJECT);
1991 if (!monster_is_valid(m_ptr)) return;
1992 /* Allow movement */
1997 /* The player is in the way */
1998 if (do_move && player_bold(p_ptr, ny, nx))
2000 /* Some monsters never attack */
2001 if (r_ptr->flags1 & RF1_NEVER_BLOW)
2003 /* Hack -- memorize lack of attacks */
2004 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2010 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2011 if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2013 if (!MON_CONFUSED(m_ptr))
2015 if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2018 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2023 /* The player is in the way. Attack him. */
2026 if (!p_ptr->riding || one_in_(2))
2029 (void)make_attack_normal(p_ptr, m_idx);
2040 /* A monster is in the way */
2041 if (do_move && g_ptr->m_idx)
2043 monster_race *z_ptr = &r_info[y_ptr->r_idx];
2045 /* Assume no movement */
2048 /* Attack 'enemies' */
2049 if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2050 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2051 can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2052 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2054 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2056 if (r_ptr->flags2 & RF2_KILL_BODY)
2058 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2062 if (y_ptr->r_idx && (y_ptr->hp >= 0))
2064 if (monst_attack_monst(p_ptr, m_idx, g_ptr->m_idx)) return;
2066 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2067 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2069 if (MON_CONFUSED(m_ptr)) return;
2070 else if (r_ptr->flags2 & RF2_STUPID)
2072 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2080 /* Push past weaker monsters (unless leaving a wall) */
2081 else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2082 (r_ptr->mexp > z_ptr->mexp) &&
2083 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2084 monster_can_cross_terrain(p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2086 /* Allow movement */
2089 /* Monster pushed past another monster */
2090 did_move_body = TRUE;
2092 /* Wake up the moved monster */
2093 (void)set_monster_csleep(g_ptr->m_idx, 0);
2101 if (!p_ptr->riding_ryoute && !MON_MONFEAR(&p_ptr->current_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2104 if (did_kill_wall && do_move)
2106 if (one_in_(GRINDNOISE))
2108 if (have_flag(f_ptr->flags, FF_GLASS))
2109 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2111 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2114 cave_alter_feat(ny, nx, FF_HURT_DISI);
2116 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2118 p_ptr->update |= (PU_FLOW);
2119 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2120 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2125 f_ptr = &f_info[g_ptr->feat];
2127 /* Note changes to viewable region */
2132 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2134 if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2136 /* Assume no move allowed */
2142 * Check if monster can cross terrain
2143 * This is checked after the normal attacks
2144 * to allow monsters to attack an enemy,
2145 * even if it can't enter the terrain.
2147 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2149 /* Assume no move allowed */
2153 /* Some monsters never move */
2154 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2156 /* Hack -- memorize lack of moves */
2157 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2163 /* Creature has been allowed move */
2168 if (have_flag(f_ptr->flags, FF_TREE))
2170 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
2172 m_ptr->energy_need += ENERGY_NEED();
2178 /* Hack -- Update the old location */
2179 p_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2181 /* Mega-Hack -- move the old monster, if any */
2184 /* Move the old monster */
2188 /* Update the old monster */
2189 update_monster(p_ptr, g_ptr->m_idx, TRUE);
2192 /* Hack -- Update the new location */
2193 g_ptr->m_idx = m_idx;
2195 /* Move the monster */
2198 update_monster(p_ptr, m_idx, TRUE);
2205 /* sound(SOUND_WALK); */
2206 if (!move_player_effect(p_ptr, ny, nx, MPE_DONT_PICKUP)) break;
2209 /* Possible disturb */
2212 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
2213 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
2215 if (is_hostile(m_ptr))
2216 disturb(p_ptr, FALSE, TRUE);
2219 /* Take or Kill objects on the floor */
2220 if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
2221 (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
2223 OBJECT_IDX this_o_idx, next_o_idx;
2224 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
2226 /* Scan all objects in the grid */
2227 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2229 BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
2230 GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
2231 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
2232 next_o_idx = o_ptr->next_o_idx;
2237 if (o_ptr->tval == TV_GOLD) continue;
2240 * Skip "real" corpses and statues, to avoid extreme
2241 * silliness like a novice rogue pockets full of statues
2244 if ((o_ptr->tval == TV_CORPSE) ||
2245 (o_ptr->tval == TV_STATUE)) continue;
2248 /* Extract some flags */
2249 object_flags(o_ptr, flgs);
2251 /* Acquire the object name */
2252 object_desc(o_name, o_ptr, 0);
2253 monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
2255 /* React to objects that hurt the monster */
2256 if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
2257 if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
2258 if (have_flag(flgs, TR_SLAY_TROLL)) flg3 |= (RF3_TROLL);
2259 if (have_flag(flgs, TR_KILL_TROLL)) flg3 |= (RF3_TROLL);
2260 if (have_flag(flgs, TR_SLAY_GIANT)) flg3 |= (RF3_GIANT);
2261 if (have_flag(flgs, TR_KILL_GIANT)) flg3 |= (RF3_GIANT);
2262 if (have_flag(flgs, TR_SLAY_ORC)) flg3 |= (RF3_ORC);
2263 if (have_flag(flgs, TR_KILL_ORC)) flg3 |= (RF3_ORC);
2264 if (have_flag(flgs, TR_SLAY_DEMON)) flg3 |= (RF3_DEMON);
2265 if (have_flag(flgs, TR_KILL_DEMON)) flg3 |= (RF3_DEMON);
2266 if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
2267 if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
2268 if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
2269 if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
2270 if (have_flag(flgs, TR_SLAY_EVIL)) flg3 |= (RF3_EVIL);
2271 if (have_flag(flgs, TR_KILL_EVIL)) flg3 |= (RF3_EVIL);
2272 if (have_flag(flgs, TR_SLAY_HUMAN)) flg2 |= (RF2_HUMAN);
2273 if (have_flag(flgs, TR_KILL_HUMAN)) flg2 |= (RF2_HUMAN);
2274 if (have_flag(flgs, TR_BRAND_ACID)) flgr |= (RFR_IM_ACID);
2275 if (have_flag(flgs, TR_BRAND_ELEC)) flgr |= (RFR_IM_ELEC);
2276 if (have_flag(flgs, TR_BRAND_FIRE)) flgr |= (RFR_IM_FIRE);
2277 if (have_flag(flgs, TR_BRAND_COLD)) flgr |= (RFR_IM_COLD);
2278 if (have_flag(flgs, TR_BRAND_POIS)) flgr |= (RFR_IM_POIS);
2280 /* The object cannot be picked up by the monster */
2281 if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
2282 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
2284 /* Only give a message for "take_item" */
2285 if (do_take && (r_ptr->flags2 & RF2_STUPID))
2287 did_take_item = TRUE;
2289 /* Describe observable situations */
2290 if (m_ptr->ml && player_can_see_bold(ny, nx))
2292 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
2297 /* Pick up the item */
2300 did_take_item = TRUE;
2302 /* Describe observable situations */
2303 if (player_can_see_bold(ny, nx))
2305 msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
2308 /* Excise the object */
2309 excise_object_idx(this_o_idx);
2312 o_ptr->marked &= OM_TOUCHED;
2314 /* Forget location */
2315 o_ptr->iy = o_ptr->ix = 0;
2317 /* Memorize monster */
2318 o_ptr->held_m_idx = m_idx;
2321 o_ptr->next_o_idx = m_ptr->hold_o_idx;
2324 m_ptr->hold_o_idx = this_o_idx;
2327 /* Destroy the item if not a pet */
2328 else if (!is_pet(m_ptr))
2330 did_kill_item = TRUE;
2332 /* Describe observable situations */
2333 if (player_has_los_bold(p_ptr, ny, nx))
2335 msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
2338 delete_object_idx(this_o_idx);
2344 /* Stop when done */
2349 * Forward movements failed, but now received LOS attack!
2350 * Try to flow by smell.
2352 if (p_ptr->no_flowed && i > 2 && m_ptr->target_y)
2353 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2355 /* If we haven't done anything, try casting a spell again */
2356 if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
2358 /* Try to cast spell again */
2359 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2361 if (make_attack_spell(m_idx, p_ptr)) return;
2366 /* Notice changes in view */
2369 p_ptr->update |= (PU_FLOW);
2370 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2373 /* Notice changes in view */
2374 if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
2375 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->phase_out)))
2377 p_ptr->update |= (PU_MON_LITE);
2380 /* Learn things from observable monster */
2381 if (is_original_ap_and_seen(m_ptr))
2383 /* Monster opened a door */
2384 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
2386 /* Monster bashed a door */
2387 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2389 /* Monster tried to pick something up */
2390 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2392 /* Monster tried to crush something */
2393 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2395 /* Monster pushed past another monster */
2396 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2398 /* Monster passed through a wall */
2399 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2401 /* Monster destroyed a wall */
2402 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2406 /* Hack -- get "bold" if out of options */
2407 if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
2409 /* No longer afraid */
2410 (void)set_monster_monfear(m_idx, 0);
2412 /* Message if seen */
2415 GAME_TEXT m_name[MAX_NLEN];
2416 monster_desc(m_name, m_ptr, 0);
2417 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2420 if (m_ptr->ml) chg_virtue(p_ptr, V_COMPASSION, -1);
2422 /* Actually do something now (?) */
2427 * @brief 全モンスターのターン管理メインルーチン /
2428 * Process all the "live" monsters, once per game turn.
2431 * During each game current game turn, we scan through the list of all the "live" monsters,\n
2432 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2433 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2435 * Note that monsters can never move in the monster array (except when the\n
2436 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2438 * This function is responsible for at least half of the processor time\n
2439 * on a normal system with a "normal" amount of monsters and a player doing\n
2442 * When the player is resting, virtually 90% of the processor time is spent\n
2443 * in this function, and its children, "process_monster()" and "make_move()".\n
2445 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2446 * especially when the player is running.\n
2448 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2449 * monsters while they are still being "born". A monster is "fresh" only\n
2450 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2451 * determine if the monster is yet to be processed during the game turn.\n
2453 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2454 * move before any "nasty" monsters get to use their spell attacks.\n
2456 * Note that when the "knowledge" about the currently tracked monster\n
2457 * changes (flags, attacks, spells), we induce a redraw of the monster\n
2460 void process_monsters(void)
2467 monster_type *m_ptr;
2468 monster_race *r_ptr;
2470 MONRACE_IDX old_monster_race_idx;
2472 BIT_FLAGS old_r_flags1 = 0L;
2473 BIT_FLAGS old_r_flags2 = 0L;
2474 BIT_FLAGS old_r_flags3 = 0L;
2475 BIT_FLAGS old_r_flags4 = 0L;
2476 BIT_FLAGS old_r_flags5 = 0L;
2477 BIT_FLAGS old_r_flags6 = 0L;
2478 BIT_FLAGS old_r_flagsr = 0L;
2480 byte old_r_blows0 = 0;
2481 byte old_r_blows1 = 0;
2482 byte old_r_blows2 = 0;
2483 byte old_r_blows3 = 0;
2485 byte old_r_cast_spell = 0;
2489 /* Clear monster fighting indicator */
2490 p_ptr->current_floor_ptr->monster_noise = FALSE;
2492 /* Memorize old race */
2493 old_monster_race_idx = p_ptr->monster_race_idx;
2495 /* Acquire knowledge */
2496 if (p_ptr->monster_race_idx)
2498 /* Acquire current monster */
2499 r_ptr = &r_info[p_ptr->monster_race_idx];
2501 /* Memorize flags */
2502 old_r_flags1 = r_ptr->r_flags1;
2503 old_r_flags2 = r_ptr->r_flags2;
2504 old_r_flags3 = r_ptr->r_flags3;
2505 old_r_flags4 = r_ptr->r_flags4;
2506 old_r_flags5 = r_ptr->r_flags5;
2507 old_r_flags6 = r_ptr->r_flags6;
2508 old_r_flagsr = r_ptr->r_flagsr;
2510 /* Memorize blows */
2511 old_r_blows0 = r_ptr->r_blows[0];
2512 old_r_blows1 = r_ptr->r_blows[1];
2513 old_r_blows2 = r_ptr->r_blows[2];
2514 old_r_blows3 = r_ptr->r_blows[3];
2516 /* Memorize castings */
2517 old_r_cast_spell = r_ptr->r_cast_spell;
2521 /* Process the monsters (backwards) */
2522 for (i = p_ptr->current_floor_ptr->m_max - 1; i >= 1; i--)
2524 m_ptr = &p_ptr->current_floor_ptr->m_list[i];
2525 r_ptr = &r_info[m_ptr->r_idx];
2527 /* Handle "leaving" */
2528 if (p_ptr->leaving) break;
2530 /* Ignore "dead" monsters */
2531 if (!monster_is_valid(m_ptr)) continue;
2533 if (p_ptr->wild_mode) continue;
2536 /* Handle "fresh" monsters */
2537 if (m_ptr->mflag & MFLAG_BORN)
2539 /* No longer "fresh" */
2540 m_ptr->mflag &= ~(MFLAG_BORN);
2546 /* Hack -- Require proximity */
2547 if (m_ptr->cdis >= AAF_LIMIT) continue;
2552 /* Flow by smell is allowed */
2553 if (!p_ptr->no_flowed)
2555 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2558 /* Assume no move */
2561 /* Handle "sensing radius" */
2562 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2564 /* We can "sense" the player */
2568 /* Handle "sight" and "aggravation" */
2569 else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->phase_out) &&
2570 (player_has_los_bold(p_ptr, fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
2572 /* We can "see" or "feel" the player */
2576 #if 0 /* (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == p_ptr->current_floor_ptr->grid_array[fy][fx].when) is always FALSE... */
2577 /* Hack -- Monsters can "smell" the player from far away */
2578 /* Note that most monsters have "aaf" of "20" or so */
2579 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
2580 cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
2581 (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == p_ptr->current_floor_ptr->grid_array[fy][fx].when) &&
2582 (p_ptr->current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
2583 (p_ptr->current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
2585 /* We can "smell" the player */
2589 else if (m_ptr->target_y) test = TRUE;
2592 if (!test) continue;
2595 if (p_ptr->riding == i)
2596 speed = p_ptr->pspeed;
2599 speed = m_ptr->mspeed;
2601 /* Monsters move quickly in Nightmare mode */
2602 if (ironman_nightmare) speed += 5;
2604 if (MON_FAST(m_ptr)) speed += 10;
2605 if (MON_SLOW(m_ptr)) speed -= 10;
2608 /* Give this monster some energy */
2609 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2611 /* Not enough energy to move */
2612 if (m_ptr->energy_need > 0) continue;
2614 /* Use up "some" energy */
2615 m_ptr->energy_need += ENERGY_NEED();
2617 /* Save global index */
2620 /* Process the monster */
2623 reset_target(m_ptr);
2625 /* Give up flow_by_smell when it might useless */
2626 if (p_ptr->no_flowed && one_in_(3))
2627 m_ptr->mflag2 |= MFLAG2_NOFLOW;
2629 /* Hack -- notice death or departure */
2630 if (!p_ptr->playing || p_ptr->is_dead) break;
2632 /* Notice leaving */
2633 if (p_ptr->leaving) break;
2636 /* Reset global index */
2640 /* Tracking a monster race (the same one we were before) */
2641 if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
2643 /* Acquire monster race */
2644 r_ptr = &r_info[p_ptr->monster_race_idx];
2646 /* Check for knowledge change */
2647 if ((old_r_flags1 != r_ptr->r_flags1) ||
2648 (old_r_flags2 != r_ptr->r_flags2) ||
2649 (old_r_flags3 != r_ptr->r_flags3) ||
2650 (old_r_flags4 != r_ptr->r_flags4) ||
2651 (old_r_flags5 != r_ptr->r_flags5) ||
2652 (old_r_flags6 != r_ptr->r_flags6) ||
2653 (old_r_flagsr != r_ptr->r_flagsr) ||
2654 (old_r_blows0 != r_ptr->r_blows[0]) ||
2655 (old_r_blows1 != r_ptr->r_blows[1]) ||
2656 (old_r_blows2 != r_ptr->r_blows[2]) ||
2657 (old_r_blows3 != r_ptr->r_blows[3]) ||
2658 (old_r_cast_spell != r_ptr->r_cast_spell))
2660 p_ptr->window |= (PW_MONSTER);