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 /* Calculate distance of this grid from our destination */
548 dis = distance(y, x, y1, x1);
550 /* Score this grid */
551 s = 5000 / (dis + 3) - 500 / (p_ptr->current_floor_ptr->grid_array[y][x].dist + 1);
553 /* No negative scores */
556 /* Ignore lower scores */
557 if (s < score) continue;
559 /* Save the score and time */
562 /* Save the location */
567 /* No legal move (?) */
568 if (score == -1) return (FALSE);
579 * Hack -- Precompute a bunch of calls to distance() in find_safety() and
582 * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
583 * offsets of all the locations with a distance of n from a central point,
584 * with an offset of (0,0) indicating no more offsets at this distance.
586 * This is, of course, fairly unreadable, but it eliminates multiple loops
587 * from the previous version.
589 * It is probably better to replace these arrays with code to compute
590 * the relevant arrays, even if the storage is pre-allocated in hard
591 * coded sizes. At the very least, code should be included which is
592 * able to generate and dump these arrays (ala "los()").
594 * Also, the storage needs could be halved by using bytes.
596 * These arrays could be combined into two big arrays, using sub-arrays
597 * to hold the offsets and lengths of each portion of the sub-arrays, and
598 * this could perhaps also be used somehow in the "look" code.
602 static POSITION d_off_y_0[] = { 0 };
603 static POSITION d_off_x_0[] = { 0 };
605 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
606 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
608 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
609 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
611 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
612 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
614 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 };
615 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 };
618 static POSITION d_off_y_5[] =
619 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
620 -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
623 static POSITION d_off_x_5[] =
624 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
625 -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
629 static POSITION d_off_y_6[] =
630 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
631 -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
634 static POSITION d_off_x_6[] =
635 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
636 0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
637 -3, 2, 3, -1, 0, 1, 0 };
640 static POSITION d_off_y_7[] =
641 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
642 -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
643 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
645 static POSITION d_off_x_7[] =
646 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
647 -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
648 6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
652 static POSITION d_off_y_8[] =
653 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
654 -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
655 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
658 static POSITION d_off_x_8[] =
659 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
660 5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
661 7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
665 static POSITION d_off_y_9[] =
666 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
667 -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
668 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
669 7, 8, 8, 8, 8, 9, 9, 9, 0 };
671 static POSITION d_off_x_9[] =
672 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
673 -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
674 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
675 4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
678 static POSITION *dist_offsets_y[10] =
680 d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
681 d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
684 static POSITION *dist_offsets_x[10] =
686 d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
687 d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
691 * @brief モンスターが逃げ込める安全な地点を返す /
692 * Choose a "safe" location near a monster for it to run toward.
693 * @param m_idx モンスターの参照ID
694 * @param yp 移動先のマスのY座標を返す参照ポインタ
695 * @param xp 移動先のマスのX座標を返す参照ポインタ
696 * @return 有効なマスがあった場合TRUEを返す
698 * A location is "safe" if it can be reached quickly and the player\n
699 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
700 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
701 * try to run towards corridor openings if they are in a room.\n
703 * This function may take lots of CPU time if lots of monsters are\n
706 * Return TRUE if a safe location is available.\n
708 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
710 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
712 POSITION fy = m_ptr->fy;
713 POSITION fx = m_ptr->fx;
715 POSITION y, x, dy, dx, d, dis, i;
716 POSITION gy = 0, gx = 0, gdis = 0;
723 /* Start with adjacent locations, spread further */
724 for (d = 1; d < 10; d++)
726 /* Get the lists of points with a distance d from (fx, fy) */
727 y_offsets = dist_offsets_y[d];
728 x_offsets = dist_offsets_x[d];
730 /* Check the locations */
731 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
733 i++, dx = x_offsets[i], dy = y_offsets[i])
738 /* Skip illegal locations */
739 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
741 g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
743 /* Skip locations in a wall */
744 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
746 /* Check for "availability" (if monsters can flow) */
747 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
749 /* Ignore grids very far from the player */
750 if (g_ptr->dist == 0) continue;
752 /* Ignore too-distant grids */
753 if (g_ptr->dist > p_ptr->current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
756 /* Check for absence of shot (more or less) */
757 if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x))
759 /* Calculate distance from player */
760 dis = distance(y, x, p_ptr->y, p_ptr->x);
762 /* Remember if further than previous */
772 /* Check for success */
779 /* Found safe place */
790 * @brief モンスターが隠れ潜める地点を返す /
791 * Choose a good hiding place near a monster for it to run toward.
792 * @param m_idx モンスターの参照ID
793 * @param yp 移動先のマスのY座標を返す参照ポインタ
794 * @param xp 移動先のマスのX座標を返す参照ポインタ
795 * @return 有効なマスがあった場合TRUEを返す
797 * Pack monsters will use this to "ambush" the player and lure him out\n
798 * of corridors into open space so they can swarm him.\n
800 * Return TRUE if a good location is available.\n
802 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
804 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
805 monster_race *r_ptr = &r_info[m_ptr->r_idx];
807 POSITION fy = m_ptr->fy;
808 POSITION fx = m_ptr->fx;
810 POSITION y, x, dy, dx, d, dis, i;
811 POSITION gy = 0, gx = 0, gdis = 999;
813 POSITION *y_offsets, *x_offsets;
815 /* Start with adjacent locations, spread further */
816 for (d = 1; d < 10; d++)
818 /* Get the lists of points with a distance d from (fx, fy) */
819 y_offsets = dist_offsets_y[d];
820 x_offsets = dist_offsets_x[d];
822 /* Check the locations */
823 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
825 i++, dx = x_offsets[i], dy = y_offsets[i])
830 /* Skip illegal locations */
831 if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
833 /* Skip occupied locations */
834 if (!monster_can_enter(y, x, r_ptr, 0)) continue;
836 /* Check for hidden, available grid */
837 if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
839 /* Calculate distance from player */
840 dis = distance(y, x, p_ptr->y, p_ptr->x);
842 /* Remember if closer than previous */
843 if (dis < gdis && dis >= 2)
852 /* Check for success */
859 /* Found good place */
870 * @brief モンスターの移動方向を返す /
871 * Choose "logical" directions for monster movement
872 * @param m_idx モンスターの参照ID
873 * @param mm 移動方向を返す方向IDの参照ポインタ
874 * @return 有効方向があった場合TRUEを返す
876 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
878 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
879 monster_race *r_ptr = &r_info[m_ptr->r_idx];
880 POSITION y = 0, ay, x = 0, ax;
882 POSITION y2 = p_ptr->y;
883 POSITION x2 = p_ptr->x;
885 bool will_run = mon_will_run(m_idx);
887 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
888 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
890 /* Counter attack to an enemy monster */
891 if (!will_run && m_ptr->target_y)
893 int t_m_idx = p_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
895 /* The monster must be an enemy, and in LOS */
897 are_enemies(m_ptr, &p_ptr->current_floor_ptr->m_list[t_m_idx]) &&
898 los(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
899 projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
901 /* Extract the "pseudo-direction" */
902 y = m_ptr->fy - m_ptr->target_y;
903 x = m_ptr->fx - m_ptr->target_x;
908 if (!done && !will_run && is_hostile(m_ptr) &&
909 (r_ptr->flags1 & RF1_FRIENDS) &&
910 ((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)) ||
911 (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
914 * Animal packs try to get the player out of corridors
915 * (...unless they can move through walls -- TY)
917 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
918 !(r_ptr->flags2 & RF2_KILL_WALL))
922 /* Count room grids next to player */
923 for (i = 0; i < 8; i++)
925 int xx = p_ptr->x + ddx_ddd[i];
926 int yy = p_ptr->y + ddy_ddd[i];
928 if (!in_bounds2(p_ptr->current_floor_ptr, yy, xx)) continue;
930 g_ptr = &p_ptr->current_floor_ptr->grid_array[yy][xx];
933 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
935 /* One more room grid */
939 if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
940 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
942 /* Not in a room and strong player */
943 if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
944 (p_ptr->mhp + p_ptr->msp))
946 /* Find hiding place */
947 if (find_hiding(m_idx, &y, &x)) done = TRUE;
951 /* Monster groups try to surround the player */
952 if (!done && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
956 /* Find an empty square near the player to fill */
957 for (i = 0; i < 8; i++)
959 /* Pick squares near player (semi-randomly) */
960 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
961 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
964 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
966 /* Attack the player */
973 if (!in_bounds2(p_ptr->current_floor_ptr, y2, x2)) continue;
975 /* Ignore filled grids */
976 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
978 /* Try to fill this hole */
982 /* Extract the new "pseudo-direction" */
992 /* Flow towards the player */
993 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
995 /* Extract the "pseudo-direction" */
1002 /* Apply fear if possible and necessary */
1003 if (is_pet(m_ptr) && will_run)
1005 /* XXX XXX Not very "smart" */
1010 if (!done && will_run)
1015 /* Try to find safe place */
1016 if (find_safety(m_idx, &y, &x))
1018 /* Attempt to avoid the player */
1021 /* Adjust movement */
1022 if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1028 /* This is not a very "smart" method XXX XXX */
1036 /* Check for no move */
1037 if (!x && !y) return (FALSE);
1040 /* Extract the "absolute distances" */
1044 /* Do something weird */
1045 if (y < 0) move_val += 8;
1046 if (x > 0) move_val += 4;
1048 /* Prevent the diamond maneuvre */
1049 if (ay > (ax << 1)) move_val += 2;
1050 else if (ax > (ay << 1)) move_val++;
1052 /* Extract some directions */
1197 /* Wants to move... */
1202 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1204 return !have_flag(f_ptr->flags, FF_GLASS) ||
1205 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1206 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1211 * @brief モンスター単体の1ターン行動処理メインルーチン /
1213 * @param m_idx 行動モンスターの参照ID
1216 * The monster is known to be within 100 grids of the player\n
1218 * In several cases, we directly update the monster lore\n
1220 * Note that a monster is only allowed to "reproduce" if there\n
1221 * are a limited number of "reproducing" monsters on the current\n
1222 * level. This should prevent the level from being "swamped" by\n
1223 * reproducing monsters. It also allows a large mass of mice to\n
1224 * prevent a louse from multiplying, but this is a small price to\n
1225 * pay for a simple multiplication method.\n
1227 * XXX Monster fear is slightly odd, in particular, monsters will\n
1228 * fixate on opening a door even if they cannot open it. Actually,\n
1229 * the same thing happens to normal monsters when they hit a door\n
1231 * In addition, monsters which *cannot* open or bash\n
1232 * down a door will still stand there trying to open it...\n
1234 * XXX Technically, need to check for monster in the way\n
1235 * combined with that monster being in a wall (or door?)\n
1237 * A "direction" of "5" means "pick a random direction".\n
1239 void process_monster(MONSTER_IDX m_idx)
1241 monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
1242 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1243 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1246 POSITION oy, ox, ny, nx;
1251 feature_type *f_ptr;
1253 monster_type *y_ptr;
1258 bool must_alter_to_move;
1267 bool gets_angry = FALSE;
1272 bool is_riding_mon = (m_idx == p_ptr->riding);
1273 bool see_m = is_seen(m_ptr);
1275 if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1277 if (rakuba(p_ptr, 0, TRUE))
1280 msg_print("地面に落とされた。");
1282 GAME_TEXT m_name[MAX_NLEN];
1283 monster_desc(m_name, &p_ptr->current_floor_ptr->m_list[p_ptr->riding], 0);
1284 msg_format("You have fallen from %s.", m_name);
1289 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1291 choose_new_monster(m_idx, FALSE, 0);
1292 r_ptr = &r_info[m_ptr->r_idx];
1295 /* Players hidden in shadow are almost imperceptable. -LM- */
1296 if (p_ptr->special_defense & NINJA_S_STEALTH)
1298 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
1299 if (p_ptr->monlite) tmp /= 3;
1300 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1301 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
1302 /* Low-level monsters will find it difficult to locate the player. */
1303 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
1306 /* Are there its parent? */
1307 if (m_ptr->parent_m_idx && !p_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
1309 /* Its parent have gone, it also goes away. */
1313 GAME_TEXT m_name[MAX_NLEN];
1314 monster_desc(m_name, m_ptr, 0);
1315 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1318 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
1320 GAME_TEXT m_name[MAX_NLEN];
1321 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
1322 exe_write_diary(p_ptr, NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
1325 delete_monster_idx(m_idx);
1330 /* Quantum monsters are odd */
1331 if (r_ptr->flags2 & (RF2_QUANTUM))
1333 /* Sometimes skip move */
1334 if (!randint0(2)) return;
1337 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
1341 if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
1345 GAME_TEXT m_name[MAX_NLEN];
1346 monster_desc(m_name, m_ptr, 0);
1348 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
1351 /* Generate treasure, etc */
1352 monster_death(m_idx, FALSE);
1354 delete_monster_idx(m_idx);
1357 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
1364 if (m_ptr->r_idx == MON_SHURYUUDAN)
1366 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1370 if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->phase_out)
1372 static int riding_pinch = 0;
1374 if (m_ptr->hp < m_ptr->maxhp/3)
1376 GAME_TEXT m_name[MAX_NLEN];
1377 monster_desc(m_name, m_ptr, 0);
1379 if (is_riding_mon && riding_pinch < 2)
1381 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
1382 "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
1384 disturb(p_ptr, TRUE, TRUE);
1390 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
1391 if (rakuba(p_ptr, -1, FALSE))
1393 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1399 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) &&
1400 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))
1402 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
1404 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
1405 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
1408 if (is_riding_mon && rakuba(p_ptr, -1, FALSE))
1410 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
1413 check_quest_completion(m_ptr);
1414 delete_monster_idx(m_idx);
1420 /* Reset the counter */
1421 if (is_riding_mon) riding_pinch = 0;
1425 /* Handle "sleep" */
1426 if (MON_CSLEEP(m_ptr))
1428 /* Handle non-aggravation - Still sleeping */
1429 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
1431 (void)set_monster_csleep(m_idx, 0);
1433 /* Notice the "waking up" */
1436 GAME_TEXT m_name[MAX_NLEN];
1437 monster_desc(m_name, m_ptr, 0);
1438 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
1441 /* Hack -- Count the wakings */
1442 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
1449 if (MON_STUNNED(m_ptr))
1451 /* Sometimes skip move */
1452 if (one_in_(2)) return;
1457 p_ptr->update |= (PU_BONUS);
1460 /* No one wants to be your friend if you're aggravating */
1461 if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
1464 /* Paranoia... no pet uniques outside wizard mode -- TY */
1465 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1466 monster_has_hostile_align(NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1471 if (p_ptr->phase_out) gets_angry = FALSE;
1475 if (is_pet(m_ptr) || see_m)
1477 GAME_TEXT m_name[MAX_NLEN];
1478 monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1479 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1485 /* Get the origin */
1489 /* Attempt to "multiply" if able and allowed */
1490 if ((r_ptr->flags2 & RF2_MULTIPLY) && (p_ptr->current_floor_ptr->num_repro < MAX_REPRO))
1495 /* Count the adjacent monsters */
1496 for (k = 0, y = oy - 1; y <= oy + 1; y++)
1498 for (x = ox - 1; x <= ox + 1; x++)
1500 /* Ignore locations off of edge */
1501 if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
1502 if (p_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
1506 if (multiply_barrier(m_idx)) k = 8;
1508 /* Hack -- multiply slower in crowded areas */
1509 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
1511 /* Try to multiply */
1512 if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
1514 /* Take note if visible */
1515 if (p_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
1517 r_ptr->r_flags2 |= (RF2_MULTIPLY);
1520 /* Multiplying takes energy */
1526 if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
1528 /* Hack -- Ohmu scatters molds! */
1529 if (m_ptr->r_idx == MON_OHMU)
1531 if (!p_ptr->current_floor_ptr->inside_arena && !p_ptr->phase_out)
1533 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
1536 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1537 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
1539 for (k = 0; k < A_MAX; k++)
1541 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
1543 if (p_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
1547 if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
1553 if (!p_ptr->phase_out)
1555 /* Hack! "Cyber" monster makes noise... */
1556 if (m_ptr->ap_r_idx == MON_CYBER &&
1557 one_in_(CYBERNOISE) &&
1558 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
1560 if (disturb_minor) disturb(p_ptr, FALSE, FALSE);
1561 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
1564 /* Some monsters can speak */
1565 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
1566 one_in_(SPEAK_CHANCE) &&
1567 player_has_los_bold(p_ptr, oy, ox) &&
1568 projectable(p_ptr->current_floor_ptr, oy, ox, p_ptr->y, p_ptr->x))
1570 GAME_TEXT m_name[MAX_NLEN];
1571 char monmessage[1024];
1574 /* Acquire the monster name/poss */
1576 monster_desc(m_name, m_ptr, 0);
1578 strcpy(m_name, _("それ", "It"));
1580 /* Select the file for monster quotes */
1581 if (MON_MONFEAR(m_ptr))
1582 filename = _("monfear_j.txt", "monfear.txt");
1583 else if (is_pet(m_ptr))
1584 filename = _("monpet_j.txt", "monpet.txt");
1585 else if (is_friendly(m_ptr))
1586 filename = _("monfrien_j.txt", "monfrien.txt");
1588 filename = _("monspeak_j.txt", "monspeak.txt");
1589 /* Get the monster line */
1590 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
1593 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
1598 /* Try to cast spell occasionally */
1599 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1601 bool counterattack = FALSE;
1603 /* Give priority to counter attack? */
1604 if (m_ptr->target_y)
1606 MONSTER_IDX t_m_idx = p_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1608 /* The monster must be an enemy, and projectable */
1609 if (t_m_idx && are_enemies(m_ptr, &p_ptr->current_floor_ptr->m_list[t_m_idx]) &&
1610 projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1612 counterattack = TRUE;
1618 /* Attempt to cast a spell */
1619 if (aware && make_attack_spell(m_idx, p_ptr)) return;
1622 * Attempt to cast a spell at an enemy other than the player
1623 * (may slow the game a smidgeon, but I haven't noticed.)
1625 if (monst_spell_monst(m_idx)) return;
1629 /* Attempt to do counter attack at first */
1630 if (monst_spell_monst(m_idx)) return;
1632 if (aware && make_attack_spell(m_idx, p_ptr)) return;
1636 /* Hack -- Assume no movement */
1637 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1638 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1641 /* Confused -- 100% random */
1642 if (MON_CONFUSED(m_ptr) || !aware)
1644 /* Try four "random" directions */
1645 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1648 /* 75% random movement */
1649 else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
1650 (randint0(100) < 75))
1652 /* Memorize flags */
1653 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
1655 /* Try four "random" directions */
1656 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1659 /* 50% random movement */
1660 else if ((r_ptr->flags1 & RF1_RAND_50) &&
1661 (randint0(100) < 50))
1663 /* Memorize flags */
1664 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
1666 /* Try four "random" directions */
1667 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1670 /* 25% random movement */
1671 else if ((r_ptr->flags1 & RF1_RAND_25) &&
1672 (randint0(100) < 25))
1674 /* Memorize flags */
1675 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
1677 /* Try four "random" directions */
1678 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1681 /* Can't reach player - find something else to hit */
1682 else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
1684 /* Try four "random" directions */
1685 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1687 /* Look for an enemy */
1688 #if 0 /* Hack - Too slow. Mimic pits are horrible with this on. */
1689 get_enemy_dir(m_idx, mm);
1693 /* Pets will follow the player */
1694 else if (is_pet(m_ptr))
1696 /* Are we trying to avoid the player? */
1697 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
1699 /* Do we want to find the player? */
1700 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
1702 /* Should we find the player if we can't find a monster? */
1703 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
1705 /* by default, move randomly */
1706 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1708 /* Look for an enemy */
1709 if (!get_enemy_dir(m_idx, mm))
1711 /* Find the player if necessary */
1712 if (avoid || lonely || distant)
1714 /* Remember the leash length */
1715 POSITION dis = p_ptr->pet_follow_distance;
1717 /* Hack -- adjust follow distance temporarily */
1718 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
1720 p_ptr->pet_follow_distance = PET_SEEK_DIST;
1723 /* Find the player */
1724 (void)get_moves(m_idx, mm);
1726 /* Restore the leash */
1727 p_ptr->pet_follow_distance = (s16b)dis;
1732 /* Friendly monster movement */
1733 else if (!is_hostile(m_ptr))
1735 /* by default, move randomly */
1736 mm[0] = mm[1] = mm[2] = mm[3] = 5;
1738 /* Look for an enemy */
1739 get_enemy_dir(m_idx, mm);
1741 /* Normal movement */
1744 /* Logical moves, may do nothing */
1745 if (!get_moves(m_idx, mm)) return;
1748 /* Assume nothing */
1752 must_alter_to_move = FALSE;
1754 /* Assume nothing */
1755 did_open_door = FALSE;
1756 did_bash_door = FALSE;
1757 did_take_item = FALSE;
1758 did_kill_item = FALSE;
1759 did_move_body = FALSE;
1760 did_pass_wall = FALSE;
1761 did_kill_wall = FALSE;
1763 /* Take a zero-terminated array of "directions" */
1764 for (i = 0; mm[i]; i++)
1766 /* Get the direction */
1769 /* Hack -- allow "randomized" motion */
1770 if (d == 5) d = ddd[randint0(8)];
1772 /* Get the destination */
1776 /* Ignore locations off of edge */
1777 if (!in_bounds2(p_ptr->current_floor_ptr, ny, nx)) continue;
1779 /* Access that grid */
1780 g_ptr = &p_ptr->current_floor_ptr->grid_array[ny][nx];
1781 f_ptr = &f_info[g_ptr->feat];
1782 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1784 /* Access that grid's contents */
1785 y_ptr = &p_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1787 /* Hack -- player 'in' wall */
1788 if (player_bold(p_ptr, ny, nx))
1793 /* Possibly a monster to attack */
1794 else if (g_ptr->m_idx)
1799 /* Monster destroys walls (and doors) */
1800 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1801 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1802 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1803 check_hp_for_feat_destruction(f_ptr, m_ptr))
1805 /* Eat through walls/doors/rubble */
1807 if (!can_cross) must_alter_to_move = TRUE;
1809 /* Monster destroyed a wall (later) */
1810 did_kill_wall = TRUE;
1813 /* Floor is open? */
1816 /* Go ahead and move */
1819 /* Monster moves through walls (and doors) */
1820 if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
1821 have_flag(f_ptr->flags, FF_CAN_PASS))
1823 /* Monster went through a wall */
1824 did_pass_wall = TRUE;
1828 /* Handle doors and secret doors */
1829 else if (is_closed_door(g_ptr->feat))
1831 bool may_bash = TRUE;
1833 /* Assume no move allowed */
1836 /* Creature can open doors. */
1837 if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1838 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1843 /* The door is open */
1844 did_open_door = TRUE;
1846 /* Do not bash the door */
1852 /* Locked doors (not jammed) */
1855 /* Try to unlock it */
1856 if (randint0(m_ptr->hp / 10) > f_ptr->power)
1858 /* Unlock the door */
1859 cave_alter_feat(ny, nx, FF_DISARM);
1861 /* Do not bash the door */
1869 /* Stuck doors -- attempt to bash them down if allowed */
1870 if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1871 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1873 /* Attempt to Bash */
1874 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1876 if (have_flag(f_ptr->flags, FF_GLASS))
1877 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
1879 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1881 /* Disturb (sometimes) */
1882 if (disturb_minor) disturb(p_ptr, FALSE, FALSE);
1884 /* The door was bashed open */
1885 did_bash_door = TRUE;
1887 /* Hack -- fall into doorway */
1889 must_alter_to_move = TRUE;
1894 /* Deal with doors in the way */
1895 if (did_open_door || did_bash_door)
1897 /* Break down the door */
1898 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1900 cave_alter_feat(ny, nx, FF_BASH);
1902 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1904 p_ptr->update |= (PU_FLOW);
1905 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1906 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1915 cave_alter_feat(ny, nx, FF_OPEN);
1918 f_ptr = &f_info[g_ptr->feat];
1920 /* Handle viewable doors */
1925 /* Hack -- check for Glyph of Warding */
1926 if (do_move && is_glyph_grid(g_ptr) &&
1927 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(p_ptr, ny, nx)))
1929 /* Assume no move allowed */
1932 /* Break the ward */
1933 if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1935 /* Describe observable breakage */
1936 if (g_ptr->info & CAVE_MARK)
1938 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1941 /* Forget the rune */
1942 g_ptr->info &= ~(CAVE_MARK);
1944 /* Break the rune */
1945 g_ptr->info &= ~(CAVE_OBJECT);
1948 /* Allow movement */
1954 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1955 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(p_ptr, ny, nx)))
1957 /* Assume no move allowed */
1960 /* Break the ward */
1963 /* Break the ward */
1964 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1966 /* Describe observable breakage */
1967 if (g_ptr->info & CAVE_MARK)
1969 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1970 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);
1975 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1978 /* Forget the rune */
1979 g_ptr->info &= ~(CAVE_MARK);
1981 /* Break the rune */
1982 g_ptr->info &= ~(CAVE_OBJECT);
1988 if (!monster_is_valid(m_ptr)) return;
1989 /* Allow movement */
1994 /* The player is in the way */
1995 if (do_move && player_bold(p_ptr, ny, nx))
1997 /* Some monsters never attack */
1998 if (r_ptr->flags1 & RF1_NEVER_BLOW)
2000 /* Hack -- memorize lack of attacks */
2001 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2007 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2008 if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2010 if (!MON_CONFUSED(m_ptr))
2012 if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2015 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2020 /* The player is in the way. Attack him. */
2023 if (!p_ptr->riding || one_in_(2))
2026 (void)make_attack_normal(p_ptr, m_idx);
2037 /* A monster is in the way */
2038 if (do_move && g_ptr->m_idx)
2040 monster_race *z_ptr = &r_info[y_ptr->r_idx];
2042 /* Assume no movement */
2045 /* Attack 'enemies' */
2046 if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2047 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2048 can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2049 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2051 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2053 if (r_ptr->flags2 & RF2_KILL_BODY)
2055 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2059 if (y_ptr->r_idx && (y_ptr->hp >= 0))
2061 if (monst_attack_monst(p_ptr, m_idx, g_ptr->m_idx)) return;
2063 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
2064 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2066 if (MON_CONFUSED(m_ptr)) return;
2067 else if (r_ptr->flags2 & RF2_STUPID)
2069 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2077 /* Push past weaker monsters (unless leaving a wall) */
2078 else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2079 (r_ptr->mexp > z_ptr->mexp) &&
2080 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2081 monster_can_cross_terrain(p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2083 /* Allow movement */
2086 /* Monster pushed past another monster */
2087 did_move_body = TRUE;
2089 /* Wake up the moved monster */
2090 (void)set_monster_csleep(g_ptr->m_idx, 0);
2098 if (!p_ptr->riding_ryoute && !MON_MONFEAR(&p_ptr->current_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2101 if (did_kill_wall && do_move)
2103 if (one_in_(GRINDNOISE))
2105 if (have_flag(f_ptr->flags, FF_GLASS))
2106 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2108 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2111 cave_alter_feat(ny, nx, FF_HURT_DISI);
2113 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
2115 p_ptr->update |= (PU_FLOW);
2116 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2117 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2122 f_ptr = &f_info[g_ptr->feat];
2124 /* Note changes to viewable region */
2129 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2131 if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2133 /* Assume no move allowed */
2139 * Check if monster can cross terrain
2140 * This is checked after the normal attacks
2141 * to allow monsters to attack an enemy,
2142 * even if it can't enter the terrain.
2144 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2146 /* Assume no move allowed */
2150 /* Some monsters never move */
2151 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
2153 /* Hack -- memorize lack of moves */
2154 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
2160 /* Creature has been allowed move */
2165 if (have_flag(f_ptr->flags, FF_TREE))
2167 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
2169 m_ptr->energy_need += ENERGY_NEED();
2175 /* Hack -- Update the old location */
2176 p_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
2178 /* Mega-Hack -- move the old monster, if any */
2181 /* Move the old monster */
2185 /* Update the old monster */
2186 update_monster(p_ptr, g_ptr->m_idx, TRUE);
2189 /* Hack -- Update the new location */
2190 g_ptr->m_idx = m_idx;
2192 /* Move the monster */
2195 update_monster(p_ptr, m_idx, TRUE);
2202 /* sound(SOUND_WALK); */
2203 if (!move_player_effect(p_ptr, ny, nx, MPE_DONT_PICKUP)) break;
2206 /* Possible disturb */
2209 (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)) ||
2210 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
2212 if (is_hostile(m_ptr))
2213 disturb(p_ptr, FALSE, TRUE);
2216 /* Take or Kill objects on the floor */
2217 if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
2218 (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
2220 OBJECT_IDX this_o_idx, next_o_idx;
2221 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
2223 /* Scan all objects in the grid */
2224 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
2226 BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
2227 GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
2228 object_type *o_ptr = &p_ptr->current_floor_ptr->o_list[this_o_idx];
2229 next_o_idx = o_ptr->next_o_idx;
2234 if (o_ptr->tval == TV_GOLD) continue;
2237 * Skip "real" corpses and statues, to avoid extreme
2238 * silliness like a novice rogue pockets full of statues
2241 if ((o_ptr->tval == TV_CORPSE) ||
2242 (o_ptr->tval == TV_STATUE)) continue;
2245 /* Extract some flags */
2246 object_flags(o_ptr, flgs);
2248 /* Acquire the object name */
2249 object_desc(o_name, o_ptr, 0);
2250 monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
2252 /* React to objects that hurt the monster */
2253 if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
2254 if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
2255 if (have_flag(flgs, TR_SLAY_TROLL)) flg3 |= (RF3_TROLL);
2256 if (have_flag(flgs, TR_KILL_TROLL)) flg3 |= (RF3_TROLL);
2257 if (have_flag(flgs, TR_SLAY_GIANT)) flg3 |= (RF3_GIANT);
2258 if (have_flag(flgs, TR_KILL_GIANT)) flg3 |= (RF3_GIANT);
2259 if (have_flag(flgs, TR_SLAY_ORC)) flg3 |= (RF3_ORC);
2260 if (have_flag(flgs, TR_KILL_ORC)) flg3 |= (RF3_ORC);
2261 if (have_flag(flgs, TR_SLAY_DEMON)) flg3 |= (RF3_DEMON);
2262 if (have_flag(flgs, TR_KILL_DEMON)) flg3 |= (RF3_DEMON);
2263 if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
2264 if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
2265 if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
2266 if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
2267 if (have_flag(flgs, TR_SLAY_EVIL)) flg3 |= (RF3_EVIL);
2268 if (have_flag(flgs, TR_KILL_EVIL)) flg3 |= (RF3_EVIL);
2269 if (have_flag(flgs, TR_SLAY_HUMAN)) flg2 |= (RF2_HUMAN);
2270 if (have_flag(flgs, TR_KILL_HUMAN)) flg2 |= (RF2_HUMAN);
2271 if (have_flag(flgs, TR_BRAND_ACID)) flgr |= (RFR_IM_ACID);
2272 if (have_flag(flgs, TR_BRAND_ELEC)) flgr |= (RFR_IM_ELEC);
2273 if (have_flag(flgs, TR_BRAND_FIRE)) flgr |= (RFR_IM_FIRE);
2274 if (have_flag(flgs, TR_BRAND_COLD)) flgr |= (RFR_IM_COLD);
2275 if (have_flag(flgs, TR_BRAND_POIS)) flgr |= (RFR_IM_POIS);
2277 /* The object cannot be picked up by the monster */
2278 if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
2279 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
2281 /* Only give a message for "take_item" */
2282 if (do_take && (r_ptr->flags2 & RF2_STUPID))
2284 did_take_item = TRUE;
2286 /* Describe observable situations */
2287 if (m_ptr->ml && player_can_see_bold(p_ptr, ny, nx))
2289 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
2294 /* Pick up the item */
2297 did_take_item = TRUE;
2299 /* Describe observable situations */
2300 if (player_can_see_bold(p_ptr, ny, nx))
2302 msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
2305 /* Excise the object */
2306 excise_object_idx(this_o_idx);
2309 o_ptr->marked &= OM_TOUCHED;
2311 /* Forget location */
2312 o_ptr->iy = o_ptr->ix = 0;
2314 /* Memorize monster */
2315 o_ptr->held_m_idx = m_idx;
2318 o_ptr->next_o_idx = m_ptr->hold_o_idx;
2321 m_ptr->hold_o_idx = this_o_idx;
2324 /* Destroy the item if not a pet */
2325 else if (!is_pet(m_ptr))
2327 did_kill_item = TRUE;
2329 /* Describe observable situations */
2330 if (player_has_los_bold(p_ptr, ny, nx))
2332 msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
2335 delete_object_idx(this_o_idx);
2341 /* Stop when done */
2346 * Forward movements failed, but now received LOS attack!
2347 * Try to flow by smell.
2349 if (p_ptr->no_flowed && i > 2 && m_ptr->target_y)
2350 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2352 /* If we haven't done anything, try casting a spell again */
2353 if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
2355 /* Try to cast spell again */
2356 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2358 if (make_attack_spell(m_idx, p_ptr)) return;
2363 /* Notice changes in view */
2366 p_ptr->update |= (PU_FLOW);
2367 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2370 /* Notice changes in view */
2371 if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
2372 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->phase_out)))
2374 p_ptr->update |= (PU_MON_LITE);
2377 /* Learn things from observable monster */
2378 if (is_original_ap_and_seen(m_ptr))
2380 /* Monster opened a door */
2381 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
2383 /* Monster bashed a door */
2384 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2386 /* Monster tried to pick something up */
2387 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2389 /* Monster tried to crush something */
2390 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2392 /* Monster pushed past another monster */
2393 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2395 /* Monster passed through a wall */
2396 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2398 /* Monster destroyed a wall */
2399 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2403 /* Hack -- get "bold" if out of options */
2404 if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
2406 /* No longer afraid */
2407 (void)set_monster_monfear(m_idx, 0);
2409 /* Message if seen */
2412 GAME_TEXT m_name[MAX_NLEN];
2413 monster_desc(m_name, m_ptr, 0);
2414 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2417 if (m_ptr->ml) chg_virtue(p_ptr, V_COMPASSION, -1);
2419 /* Actually do something now (?) */
2424 * @brief 全モンスターのターン管理メインルーチン /
2425 * Process all the "live" monsters, once per game turn.
2428 * During each game current game turn, we scan through the list of all the "live" monsters,\n
2429 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2430 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2432 * Note that monsters can never move in the monster array (except when the\n
2433 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2435 * This function is responsible for at least half of the processor time\n
2436 * on a normal system with a "normal" amount of monsters and a player doing\n
2439 * When the player is resting, virtually 90% of the processor time is spent\n
2440 * in this function, and its children, "process_monster()" and "make_move()".\n
2442 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2443 * especially when the player is running.\n
2445 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2446 * monsters while they are still being "born". A monster is "fresh" only\n
2447 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2448 * determine if the monster is yet to be processed during the game turn.\n
2450 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2451 * move before any "nasty" monsters get to use their spell attacks.\n
2453 * Note that when the "knowledge" about the currently tracked monster\n
2454 * changes (flags, attacks, spells), we induce a redraw of the monster\n
2457 void process_monsters(void)
2464 monster_type *m_ptr;
2465 monster_race *r_ptr;
2467 MONRACE_IDX old_monster_race_idx;
2469 BIT_FLAGS old_r_flags1 = 0L;
2470 BIT_FLAGS old_r_flags2 = 0L;
2471 BIT_FLAGS old_r_flags3 = 0L;
2472 BIT_FLAGS old_r_flags4 = 0L;
2473 BIT_FLAGS old_r_flags5 = 0L;
2474 BIT_FLAGS old_r_flags6 = 0L;
2475 BIT_FLAGS old_r_flagsr = 0L;
2477 byte old_r_blows0 = 0;
2478 byte old_r_blows1 = 0;
2479 byte old_r_blows2 = 0;
2480 byte old_r_blows3 = 0;
2482 byte old_r_cast_spell = 0;
2486 /* Clear monster fighting indicator */
2487 p_ptr->current_floor_ptr->monster_noise = FALSE;
2489 /* Memorize old race */
2490 old_monster_race_idx = p_ptr->monster_race_idx;
2492 /* Acquire knowledge */
2493 if (p_ptr->monster_race_idx)
2495 /* Acquire current monster */
2496 r_ptr = &r_info[p_ptr->monster_race_idx];
2498 /* Memorize flags */
2499 old_r_flags1 = r_ptr->r_flags1;
2500 old_r_flags2 = r_ptr->r_flags2;
2501 old_r_flags3 = r_ptr->r_flags3;
2502 old_r_flags4 = r_ptr->r_flags4;
2503 old_r_flags5 = r_ptr->r_flags5;
2504 old_r_flags6 = r_ptr->r_flags6;
2505 old_r_flagsr = r_ptr->r_flagsr;
2507 /* Memorize blows */
2508 old_r_blows0 = r_ptr->r_blows[0];
2509 old_r_blows1 = r_ptr->r_blows[1];
2510 old_r_blows2 = r_ptr->r_blows[2];
2511 old_r_blows3 = r_ptr->r_blows[3];
2513 /* Memorize castings */
2514 old_r_cast_spell = r_ptr->r_cast_spell;
2518 /* Process the monsters (backwards) */
2519 for (i = p_ptr->current_floor_ptr->m_max - 1; i >= 1; i--)
2521 m_ptr = &p_ptr->current_floor_ptr->m_list[i];
2522 r_ptr = &r_info[m_ptr->r_idx];
2524 /* Handle "leaving" */
2525 if (p_ptr->leaving) break;
2527 /* Ignore "dead" monsters */
2528 if (!monster_is_valid(m_ptr)) continue;
2530 if (p_ptr->wild_mode) continue;
2533 /* Handle "fresh" monsters */
2534 if (m_ptr->mflag & MFLAG_BORN)
2536 /* No longer "fresh" */
2537 m_ptr->mflag &= ~(MFLAG_BORN);
2543 /* Hack -- Require proximity */
2544 if (m_ptr->cdis >= AAF_LIMIT) continue;
2549 /* Flow by smell is allowed */
2550 if (!p_ptr->no_flowed)
2552 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2555 /* Assume no move */
2558 /* Handle "sensing radius" */
2559 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2561 /* We can "sense" the player */
2565 /* Handle "sight" and "aggravation" */
2566 else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->phase_out) &&
2567 (player_has_los_bold(p_ptr, fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
2569 /* We can "see" or "feel" the player */
2573 #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... */
2574 /* Hack -- Monsters can "smell" the player from far away */
2575 /* Note that most monsters have "aaf" of "20" or so */
2576 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
2577 cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
2578 (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == p_ptr->current_floor_ptr->grid_array[fy][fx].when) &&
2579 (p_ptr->current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
2580 (p_ptr->current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
2582 /* We can "smell" the player */
2586 else if (m_ptr->target_y) test = TRUE;
2589 if (!test) continue;
2592 if (p_ptr->riding == i)
2593 speed = p_ptr->pspeed;
2596 speed = m_ptr->mspeed;
2598 /* Monsters move quickly in Nightmare mode */
2599 if (ironman_nightmare) speed += 5;
2601 if (MON_FAST(m_ptr)) speed += 10;
2602 if (MON_SLOW(m_ptr)) speed -= 10;
2605 /* Give this monster some energy */
2606 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2608 /* Not enough energy to move */
2609 if (m_ptr->energy_need > 0) continue;
2611 /* Use up "some" energy */
2612 m_ptr->energy_need += ENERGY_NEED();
2614 /* Save global index */
2617 /* Process the monster */
2620 reset_target(m_ptr);
2622 /* Give up flow_by_smell when it might useless */
2623 if (p_ptr->no_flowed && one_in_(3))
2624 m_ptr->mflag2 |= MFLAG2_NOFLOW;
2626 /* Hack -- notice death or departure */
2627 if (!p_ptr->playing || p_ptr->is_dead) break;
2629 /* Notice leaving */
2630 if (p_ptr->leaving) break;
2633 /* Reset global index */
2637 /* Tracking a monster race (the same one we were before) */
2638 if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
2640 /* Acquire monster race */
2641 r_ptr = &r_info[p_ptr->monster_race_idx];
2643 /* Check for knowledge change */
2644 if ((old_r_flags1 != r_ptr->r_flags1) ||
2645 (old_r_flags2 != r_ptr->r_flags2) ||
2646 (old_r_flags3 != r_ptr->r_flags3) ||
2647 (old_r_flags4 != r_ptr->r_flags4) ||
2648 (old_r_flags5 != r_ptr->r_flags5) ||
2649 (old_r_flags6 != r_ptr->r_flags6) ||
2650 (old_r_flagsr != r_ptr->r_flagsr) ||
2651 (old_r_blows0 != r_ptr->r_blows[0]) ||
2652 (old_r_blows1 != r_ptr->r_blows[1]) ||
2653 (old_r_blows2 != r_ptr->r_blows[2]) ||
2654 (old_r_blows3 != r_ptr->r_blows[3]) ||
2655 (old_r_cast_spell != r_ptr->r_cast_spell))
2657 p_ptr->window |= (PW_MONSTER);