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).
18 #include "monsterrace-hook.h"
20 #include "projection.h"
21 #include "spells-summon.h"
24 #include "realm-hex.h"
25 #include "object-hook.h"
28 #include "player-move.h"
32 * @brief モンスターが敵に接近するための方向を決める /
33 * Calculate the direction to the next enemy
34 * @param m_idx モンスターの参照ID
35 * @param mm 移動するべき方角IDを返す参照ポインタ
36 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
38 static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
41 POSITION x = 0, y = 0;
46 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
47 monster_race *r_ptr = &r_info[m_ptr->r_idx];
50 if (riding_t_m_idx && player_bold(m_ptr->fy, m_ptr->fx))
52 y = current_floor_ptr->m_list[riding_t_m_idx].fy;
53 x = current_floor_ptr->m_list[riding_t_m_idx].fx;
55 else if (is_pet(m_ptr) && pet_t_m_idx)
57 y = current_floor_ptr->m_list[pet_t_m_idx].fy;
58 x = current_floor_ptr->m_list[pet_t_m_idx].fx;
62 if (p_ptr->inside_battle)
64 start = randint1(m_max-1)+m_max;
65 if(randint0(2)) plus = -1;
67 else start = m_max + 1;
69 /* Scan thru all monsters */
70 for (i = start; ((i < start + m_max) && (i > start - m_max)); i+=plus)
72 IDX dummy = (i % m_max);
77 t_ptr = ¤t_floor_ptr->m_list[t_idx];
79 /* The monster itself isn't a target */
80 if (t_ptr == m_ptr) continue;
82 /* Paranoia -- Skip dead monsters */
83 if (!t_ptr->r_idx) continue;
87 /* Hack -- only fight away from player */
88 if (p_ptr->pet_follow_distance < 0)
90 /* No fighting near player */
91 if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
96 /* Hack -- no fighting away from player */
97 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
102 if (r_ptr->aaf < t_ptr->cdis) continue;
105 /* Monster must be 'an enemy' */
106 if (!are_enemies(m_ptr, t_ptr)) continue;
108 /* Monster must be projectable if we can't pass through walls */
109 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
110 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
112 if (!in_disintegration_range(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
116 if (!projectable(m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
119 /* OK -- we've got a target */
125 if (!x && !y) return FALSE;
128 /* Extract the direction */
133 if ((y < 0) && (x == 0))
140 else if ((y > 0) && (x == 0))
147 else if ((x > 0) && (y == 0))
154 else if ((x < 0) && (y == 0))
161 else if ((y < 0) && (x < 0))
168 else if ((y < 0) && (x > 0))
175 else if ((y > 0) && (x < 0))
182 else if ((y > 0) && (x > 0))
189 /* Found a monster */
195 * @brief モンスターが敵モンスターに行う打撃処理 /
196 * Hack, based on mon_take_hit... perhaps all monster attacks on other monsters should use this?
197 * @param m_idx 目標となるモンスターの参照ID
199 * @param dead 目標となったモンスターの死亡状態を返す参照ポインタ
200 * @param fear 目標となったモンスターの恐慌状態を返す参照ポインタ
201 * @param note 目標モンスターが死亡した場合の特別メッセージ(NULLならば標準表示を行う)
202 * @param who 打撃を行ったモンスターの参照ID
205 void mon_take_hit_mon(MONSTER_IDX m_idx, HIT_POINT dam, bool *dead, bool *fear, concptr note, IDX who)
207 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
208 monster_race *r_ptr = &r_info[m_ptr->r_idx];
209 GAME_TEXT m_name[160];
210 bool seen = is_seen(m_ptr);
212 /* Can the player be aware of this attack? */
213 bool known = (m_ptr->cdis <= MAX_SIGHT);
215 /* Extract monster name */
216 monster_desc(m_name, m_ptr, 0);
218 /* Redraw (later) if needed */
221 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
222 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
225 (void)set_monster_csleep(m_idx, 0);
227 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
229 if (MON_INVULNER(m_ptr) && randint0(PENETRATE_INVULNERABILITY))
233 msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
238 if (r_ptr->flagsr & RFR_RES_ALL)
243 if((dam == 0) && one_in_(3)) dam = 1;
249 msg_format(_("%^sはダメージを受けない。", "%^s is unharmed."), m_name);
258 /* It is dead now... or is it? */
261 if (((r_ptr->flags1 & (RF1_UNIQUE | RF1_QUESTOR)) ||
262 (r_ptr->flags7 & RF7_NAZGUL)) &&
263 !p_ptr->inside_battle)
270 if (!monster_living(m_ptr->r_idx))
283 monster_desc(m_name, m_ptr, MD_TRUE_NAME);
284 /* Unseen death by normal attack */
289 /* Death by special attack */
292 msg_format(_("%^s%s", "%^s%s"), m_name, note);
294 /* Death by normal attack -- nonliving monster */
295 else if (!monster_living(m_ptr->r_idx))
297 msg_format(_("%^sは破壊された。", "%^s is destroyed."), m_name);
299 /* Death by normal attack -- living monster */
302 msg_format(_("%^sは殺された。", "%^s is killed."), m_name);
306 monster_gain_exp(who, m_ptr->r_idx);
307 monster_death(m_idx, FALSE);
308 delete_monster_idx(m_idx);
313 /* Monster is dead */
322 /* Mega-Hack -- Pain cancels fear */
323 if (MON_MONFEAR(m_ptr) && (dam > 0))
326 if (set_monster_monfear(m_idx, MON_MONFEAR(m_ptr) - randint1(dam / 4)))
333 /* Sometimes a monster gets scared by damage */
334 if (!MON_MONFEAR(m_ptr) && !(r_ptr->flags3 & RF3_NO_FEAR))
336 /* Percentage of fully healthy */
337 int percentage = (100L * m_ptr->hp) / m_ptr->maxhp;
340 * Run (sometimes) if at 10% or less of max hit points,
341 * or (usually) when hit for half its current hit points
343 if (((percentage <= 10) && (randint0(10) < percentage)) ||
344 ((dam >= m_ptr->hp) && (randint0(100) < 80)))
346 /* Hack -- note fear */
349 /* Hack -- Add some timed fear */
350 (void)set_monster_monfear(m_idx, (randint1(10) +
351 (((dam >= m_ptr->hp) && (percentage > 7)) ?
352 20 : ((11 - percentage) * 5))));
356 #endif /* ALLOW_FEAR */
358 if ((dam > 0) && !is_pet(m_ptr) && !is_friendly(m_ptr) && (who != m_idx))
360 if (is_pet(¤t_floor_ptr->m_list[who]) && !player_bold(m_ptr->target_y, m_ptr->target_x))
362 set_target(m_ptr, current_floor_ptr->m_list[who].fy, current_floor_ptr->m_list[who].fx);
366 if (p_ptr->riding && (p_ptr->riding == m_idx) && (dam > 0))
368 /* Extract monster name */
369 monster_desc(m_name, m_ptr, 0);
371 if (m_ptr->hp > m_ptr->maxhp/3) dam = (dam + 1) / 2;
372 if (rakuba((dam > 200) ? 200 : dam, FALSE))
374 msg_format(_("%^sに振り落とされた!", "You have thrown off from %s!"), m_name);
384 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
385 * Returns whether a given monster will try to run from the player.
386 * @param m_idx 逃走するモンスターの参照ID
387 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
389 * Monsters will attempt to avoid very powerful players. See below.\n
391 * Because this function is called so often, little details are important\n
392 * for efficiency. Like not using "mod" or "div" when possible. And\n
393 * attempting to check the conditions in an optimal order. Note that\n
394 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
396 * Note that this function is responsible for about one to five percent\n
397 * of the processor use in normal conditions...\n
399 static bool mon_will_run(MONSTER_IDX m_idx)
401 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
405 monster_race *r_ptr = &r_info[m_ptr->r_idx];
409 HIT_POINT p_chp, p_mhp;
410 HIT_POINT m_chp, m_mhp;
415 /* Friends can be commanded to avoid the player */
418 /* Are we trying to avoid the player? */
419 return ((p_ptr->pet_follow_distance < 0) &&
420 (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
423 /* Keep monsters from running too far away */
424 if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
426 /* All "afraid" monsters will run away */
427 if (MON_MONFEAR(m_ptr)) return (TRUE);
431 /* Nearby monsters will not become terrified */
432 if (m_ptr->cdis <= 5) return (FALSE);
434 /* Examine player power (level) */
437 /* Examine monster power (level plus morale) */
438 m_lev = r_ptr->level + (m_idx & 0x08) + 25;
440 /* Optimize extreme cases below */
441 if (m_lev > p_lev + 4) return (FALSE);
442 if (m_lev + 4 <= p_lev) return (TRUE);
444 /* Examine player health */
448 /* Examine monster health */
450 m_mhp = m_ptr->maxhp;
452 /* Prepare to optimize the calculation */
453 p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
454 m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
456 /* Strong players scare strong monsters */
457 if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
461 /* Assume no terror */
467 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
468 * Search spell castable grid
469 * @param m_idx モンスターの参照ID
470 * @param yp 適したマスのY座標を返す参照ポインタ
471 * @param xp 適したマスのX座標を返す参照ポインタ
472 * @return 有効なマスがあった場合TRUEを返す
474 static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
477 POSITION y, x, y1, x1;
480 bool can_open_door = FALSE;
483 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
484 monster_race *r_ptr = &r_info[m_ptr->r_idx];
486 /* Monster location */
490 /* Monster can already cast spell to player */
491 if (projectable(y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
493 /* Set current grid cost */
494 now_cost = current_floor_ptr->grid_array[y1][x1].cost;
495 if (now_cost == 0) now_cost = 999;
497 /* Can monster bash or open doors? */
498 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
500 can_open_door = TRUE;
503 /* Check nearby grids, diagonals first */
504 for (i = 7; i >= 0; i--)
511 /* Ignore locations off of edge */
512 if (!in_bounds2(y, x)) continue;
514 /* Simply move to player */
515 if (player_bold(y, x)) return (FALSE);
517 g_ptr = ¤t_floor_ptr->grid_array[y][x];
521 /* Monster cannot kill or pass walls */
522 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))))
524 if (cost == 0) continue;
525 if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
528 /* Hack -- for kill or pass wall monster.. */
529 if (cost == 0) cost = 998;
531 if (now_cost < cost) continue;
533 if (!projectable(y, x, p_ptr->y, p_ptr->x)) continue;
535 /* Accept louder sounds */
536 if (best < cost) continue;
539 (*yp) = y1 + ddy_ddd[i];
540 (*xp) = x1 + ddx_ddd[i];
543 /* No legal move (?) */
544 if (best == 999) return (FALSE);
552 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
553 * Choose the "best" direction for "flowing"
554 * @param m_idx モンスターの参照ID
555 * @param yp 移動先のマスのY座標を返す参照ポインタ
556 * @param xp 移動先のマスのX座標を返す参照ポインタ
557 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
558 * @return 有効なマスがあった場合TRUEを返す
560 * Note that ghosts and rock-eaters are never allowed to "flow",\n
561 * since they should move directly towards the player.\n
563 * Prefer "non-diagonal" directions, but twiddle them a little\n
564 * to angle slightly towards the player's actual location.\n
566 * Allow very perceptive monsters to track old "spoor" left by\n
567 * previous locations occupied by the player. This will tend\n
568 * to have monsters end up either near the player or on a grid\n
569 * recently occupied by the player (and left via "teleport").\n
571 * Note that if "smell" is turned on, all monsters get vicious.\n
573 * Also note that teleporting away from a location will cause\n
574 * the monsters who were chasing you to converge on that location\n
575 * as long as you are still near enough to "annoy" them without\n
576 * being close enough to chase directly. I have no idea what will\n
577 * happen if you combine "smell" with low "aaf" values.\n
579 static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
582 POSITION y, x, y1, x1;
585 bool use_scent = FALSE;
587 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
588 monster_race *r_ptr = &r_info[m_ptr->r_idx];
590 /* Can monster cast attack spell? */
591 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
592 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
593 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
595 /* Can move spell castable grid? */
596 if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
599 /* Monster can't flow */
600 if (no_flow) return (FALSE);
602 /* Monster can go through rocks */
603 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
604 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
606 /* Monster location */
610 /* Hack -- Player can see us, run towards him */
611 if (player_has_los_bold(y1, x1) && projectable(p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
614 g_ptr = ¤t_floor_ptr->grid_array[y1][x1];
616 /* If we can hear noises, advance towards them */
622 /* Otherwise, try to follow a scent trail */
623 else if (g_ptr->when)
626 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
632 /* Otherwise, advance blindly */
638 /* Check nearby grids, diagonals first */
639 for (i = 7; i >= 0; i--)
644 /* Ignore locations off of edge */
645 if (!in_bounds2(y, x)) continue;
647 g_ptr = ¤t_floor_ptr->grid_array[y][x];
649 /* We're following a scent trail */
652 int when = g_ptr->when;
654 /* Accept younger scent */
655 if (best > when) continue;
659 /* We're using sound */
664 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
666 else cost = g_ptr->cost;
668 /* Accept louder sounds */
669 if ((cost == 0) || (best < cost)) continue;
673 /* Hack -- Save the "twiddled" location */
674 (*yp) = p_ptr->y + 16 * ddy_ddd[i];
675 (*xp) = p_ptr->x + 16 * ddx_ddd[i];
678 /* No legal move (?) */
679 if (best == 999 || best == 0) return (FALSE);
687 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
688 * Provide a location to flee to, but give the player a wide berth.
689 * @param m_idx モンスターの参照ID
690 * @param yp 移動先のマスのY座標を返す参照ポインタ
691 * @param xp 移動先のマスのX座標を返す参照ポインタ
692 * @return 有効なマスがあった場合TRUEを返す
694 * A monster may wish to flee to a location that is behind the player,\n
695 * but instead of heading directly for it, the monster should "swerve"\n
696 * around the player so that he has a smaller chance of getting hit.\n
698 static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
700 POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
704 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
706 /* Monster location */
710 /* Desired destination */
714 /* Check nearby grids, diagonals first */
715 for (i = 7; i >= 0; i--)
722 /* Ignore locations off of edge */
723 if (!in_bounds2(y, x)) continue;
725 /* Don't move toward player */
726 /* if (current_floor_ptr->grid_array[y][x].dist < 3) continue; */ /* Hmm.. Need it? */
728 /* Calculate distance of this grid from our destination */
729 dis = distance(y, x, y1, x1);
731 /* Score this grid */
732 s = 5000 / (dis + 3) - 500 / (current_floor_ptr->grid_array[y][x].dist + 1);
734 /* No negative scores */
737 /* Ignore lower scores */
738 if (s < score) continue;
740 /* Save the score and time */
743 /* Save the location */
748 /* No legal move (?) */
749 if (score == -1) return (FALSE);
760 * Hack -- Precompute a bunch of calls to distance() in find_safety() and
763 * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
764 * offsets of all the locations with a distance of n from a central point,
765 * with an offset of (0,0) indicating no more offsets at this distance.
767 * This is, of course, fairly unreadable, but it eliminates multiple loops
768 * from the previous version.
770 * It is probably better to replace these arrays with code to compute
771 * the relevant arrays, even if the storage is pre-allocated in hard
772 * coded sizes. At the very least, code should be included which is
773 * able to generate and dump these arrays (ala "los()").
775 * Also, the storage needs could be halved by using bytes.
777 * These arrays could be combined into two big arrays, using sub-arrays
778 * to hold the offsets and lengths of each portion of the sub-arrays, and
779 * this could perhaps also be used somehow in the "look" code.
783 static POSITION d_off_y_0[] = { 0 };
784 static POSITION d_off_x_0[] = { 0 };
786 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
787 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
789 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
790 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
792 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
793 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
795 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 };
796 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 };
799 static POSITION d_off_y_5[] =
800 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
801 -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
804 static POSITION d_off_x_5[] =
805 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
806 -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
810 static POSITION d_off_y_6[] =
811 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
812 -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
815 static POSITION d_off_x_6[] =
816 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
817 0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
818 -3, 2, 3, -1, 0, 1, 0 };
821 static POSITION d_off_y_7[] =
822 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
823 -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
824 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
826 static POSITION d_off_x_7[] =
827 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
828 -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
829 6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
833 static POSITION d_off_y_8[] =
834 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
835 -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
836 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
839 static POSITION d_off_x_8[] =
840 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
841 5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
842 7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
846 static POSITION d_off_y_9[] =
847 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
848 -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
849 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
850 7, 8, 8, 8, 8, 9, 9, 9, 0 };
852 static POSITION d_off_x_9[] =
853 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
854 -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
855 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
856 4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
859 static POSITION *dist_offsets_y[10] =
861 d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
862 d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
865 static POSITION *dist_offsets_x[10] =
867 d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
868 d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
872 * @brief モンスターが逃げ込める安全な地点を返す /
873 * Choose a "safe" location near a monster for it to run toward.
874 * @param m_idx モンスターの参照ID
875 * @param yp 移動先のマスのY座標を返す参照ポインタ
876 * @param xp 移動先のマスのX座標を返す参照ポインタ
877 * @return 有効なマスがあった場合TRUEを返す
879 * A location is "safe" if it can be reached quickly and the player\n
880 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
881 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
882 * try to run towards corridor openings if they are in a room.\n
884 * This function may take lots of CPU time if lots of monsters are\n
887 * Return TRUE if a safe location is available.\n
889 static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
891 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
893 POSITION fy = m_ptr->fy;
894 POSITION fx = m_ptr->fx;
896 POSITION y, x, dy, dx, d, dis, i;
897 POSITION gy = 0, gx = 0, gdis = 0;
904 /* Start with adjacent locations, spread further */
905 for (d = 1; d < 10; d++)
907 /* Get the lists of points with a distance d from (fx, fy) */
908 y_offsets = dist_offsets_y[d];
909 x_offsets = dist_offsets_x[d];
911 /* Check the locations */
912 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
914 i++, dx = x_offsets[i], dy = y_offsets[i])
919 /* Skip illegal locations */
920 if (!in_bounds(y, x)) continue;
922 g_ptr = ¤t_floor_ptr->grid_array[y][x];
924 /* Skip locations in a wall */
925 if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
927 /* Check for "availability" (if monsters can flow) */
928 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
930 /* Ignore grids very far from the player */
931 if (g_ptr->dist == 0) continue;
933 /* Ignore too-distant grids */
934 if (g_ptr->dist > current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
937 /* Check for absence of shot (more or less) */
938 if (!projectable(p_ptr->y, p_ptr->x, y, x))
940 /* Calculate distance from player */
941 dis = distance(y, x, p_ptr->y, p_ptr->x);
943 /* Remember if further than previous */
953 /* Check for success */
960 /* Found safe place */
971 * @brief モンスターが隠れ潜める地点を返す /
972 * Choose a good hiding place near a monster for it to run toward.
973 * @param m_idx モンスターの参照ID
974 * @param yp 移動先のマスのY座標を返す参照ポインタ
975 * @param xp 移動先のマスのX座標を返す参照ポインタ
976 * @return 有効なマスがあった場合TRUEを返す
978 * Pack monsters will use this to "ambush" the player and lure him out\n
979 * of corridors into open space so they can swarm him.\n
981 * Return TRUE if a good location is available.\n
983 static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
985 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
986 monster_race *r_ptr = &r_info[m_ptr->r_idx];
988 POSITION fy = m_ptr->fy;
989 POSITION fx = m_ptr->fx;
991 POSITION y, x, dy, dx, d, dis, i;
992 POSITION gy = 0, gx = 0, gdis = 999;
994 POSITION *y_offsets, *x_offsets;
996 /* Start with adjacent locations, spread further */
997 for (d = 1; d < 10; d++)
999 /* Get the lists of points with a distance d from (fx, fy) */
1000 y_offsets = dist_offsets_y[d];
1001 x_offsets = dist_offsets_x[d];
1003 /* Check the locations */
1004 for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
1006 i++, dx = x_offsets[i], dy = y_offsets[i])
1011 /* Skip illegal locations */
1012 if (!in_bounds(y, x)) continue;
1014 /* Skip occupied locations */
1015 if (!monster_can_enter(y, x, r_ptr, 0)) continue;
1017 /* Check for hidden, available grid */
1018 if (!projectable(p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
1020 /* Calculate distance from player */
1021 dis = distance(y, x, p_ptr->y, p_ptr->x);
1023 /* Remember if closer than previous */
1024 if (dis < gdis && dis >= 2)
1033 /* Check for success */
1040 /* Found good place */
1051 * @brief モンスターの移動方向を返す /
1052 * Choose "logical" directions for monster movement
1053 * @param m_idx モンスターの参照ID
1054 * @param mm 移動方向を返す方向IDの参照ポインタ
1055 * @return 有効方向があった場合TRUEを返す
1057 static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
1059 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
1060 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1061 POSITION y = 0, ay, x = 0, ax;
1063 POSITION y2 = p_ptr->y;
1064 POSITION x2 = p_ptr->x;
1066 bool will_run = mon_will_run(m_idx);
1068 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
1069 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
1071 /* Counter attack to an enemy monster */
1072 if (!will_run && m_ptr->target_y)
1074 int t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
1076 /* The monster must be an enemy, and in LOS */
1078 are_enemies(m_ptr, ¤t_floor_ptr->m_list[t_m_idx]) &&
1079 los(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
1080 projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
1082 /* Extract the "pseudo-direction" */
1083 y = m_ptr->fy - m_ptr->target_y;
1084 x = m_ptr->fx - m_ptr->target_x;
1089 if (!done && !will_run && is_hostile(m_ptr) &&
1090 (r_ptr->flags1 & RF1_FRIENDS) &&
1091 ((los(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
1092 (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
1095 * Animal packs try to get the player out of corridors
1096 * (...unless they can move through walls -- TY)
1098 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
1099 !(r_ptr->flags2 & RF2_KILL_WALL))
1103 /* Count room grids next to player */
1104 for (i = 0; i < 8; i++)
1106 int xx = p_ptr->x + ddx_ddd[i];
1107 int yy = p_ptr->y + ddy_ddd[i];
1109 if (!in_bounds2(yy, xx)) continue;
1111 g_ptr = ¤t_floor_ptr->grid_array[yy][xx];
1114 if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
1116 /* One more room grid */
1120 if (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
1121 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
1123 /* Not in a room and strong player */
1124 if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
1125 (p_ptr->mhp + p_ptr->msp))
1127 /* Find hiding place */
1128 if (find_hiding(m_idx, &y, &x)) done = TRUE;
1132 /* Monster groups try to surround the player */
1133 if (!done && (current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
1137 /* Find an empty square near the player to fill */
1138 for (i = 0; i < 8; i++)
1140 /* Pick squares near player (semi-randomly) */
1141 y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
1142 x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
1144 /* Already there? */
1145 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
1147 /* Attack the player */
1154 if (!in_bounds2(y2, x2)) continue;
1156 /* Ignore filled grids */
1157 if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
1159 /* Try to fill this hole */
1163 /* Extract the new "pseudo-direction" */
1173 /* Flow towards the player */
1174 (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
1176 /* Extract the "pseudo-direction" */
1183 /* Apply fear if possible and necessary */
1184 if (is_pet(m_ptr) && will_run)
1186 /* XXX XXX Not very "smart" */
1191 if (!done && will_run)
1196 /* Try to find safe place */
1197 if (find_safety(m_idx, &y, &x))
1199 /* Attempt to avoid the player */
1202 /* Adjust movement */
1203 if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
1209 /* This is not a very "smart" method XXX XXX */
1217 /* Check for no move */
1218 if (!x && !y) return (FALSE);
1221 /* Extract the "absolute distances" */
1225 /* Do something weird */
1226 if (y < 0) move_val += 8;
1227 if (x > 0) move_val += 4;
1229 /* Prevent the diamond maneuvre */
1230 if (ay > (ax << 1)) move_val += 2;
1231 else if (ax > (ay << 1)) move_val++;
1233 /* Extract some directions */
1378 /* Wants to move... */
1384 * @brief モンスターから敵モンスターへの命中判定
1385 * @param power 打撃属性による基本命中値
1386 * @param level 攻撃側モンスターのレベル
1387 * @param ac 目標モンスターのAC
1388 * @param stun 攻撃側モンスターが朦朧状態ならTRUEを返す
1389 * @return 命中ならばTRUEを返す
1391 static int check_hit2(int power, DEPTH level, ARMOUR_CLASS ac, int stun)
1395 /* Percentile dice */
1398 if (stun && one_in_(2)) return FALSE;
1400 /* Hack -- Always miss or hit */
1401 if (k < 10) return (k < 5);
1403 /* Calculate the "attack quality" */
1404 i = (power + (level * 3));
1406 /* Power and Level compete against Armor */
1407 if ((i > 0) && (randint1(i) > ((ac * 3) / 4))) return (TRUE);
1414 #define BLOW_EFFECT_TYPE_NONE 0
1415 #define BLOW_EFFECT_TYPE_FEAR 1
1416 #define BLOW_EFFECT_TYPE_SLEEP 2
1417 #define BLOW_EFFECT_TYPE_HEAL 3
1421 * @brief モンスターから敵モンスターへの打撃攻撃処理
1422 * @param m_idx 攻撃側モンスターの参照ID
1423 * @param t_idx 目標側モンスターの参照ID
1424 * @return 実際に打撃処理が行われた場合TRUEを返す
1426 static bool monst_attack_monst(MONSTER_IDX m_idx, MONSTER_IDX t_idx)
1428 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
1429 monster_type *t_ptr = ¤t_floor_ptr->m_list[t_idx];
1431 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1432 monster_race *tr_ptr = &r_info[t_ptr->r_idx];
1434 ARMOUR_CLASS ap_cnt;
1438 GAME_TEXT m_name[MAX_NLEN], t_name[MAX_NLEN];
1439 char temp[MAX_NLEN];
1441 bool explode = FALSE, touched = FALSE, fear = FALSE, dead = FALSE;
1442 POSITION y_saver = t_ptr->fy;
1443 POSITION x_saver = t_ptr->fx;
1446 bool see_m = is_seen(m_ptr);
1447 bool see_t = is_seen(t_ptr);
1448 bool see_either = see_m || see_t;
1450 /* Can the player be aware of this attack? */
1451 bool known = (m_ptr->cdis <= MAX_SIGHT) || (t_ptr->cdis <= MAX_SIGHT);
1452 bool do_silly_attack = (one_in_(2) && p_ptr->image);
1454 /* Cannot attack self */
1455 if (m_idx == t_idx) return FALSE;
1457 /* Not allowed to attack */
1458 if (r_ptr->flags1 & RF1_NEVER_BLOW) return FALSE;
1460 if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE) return (FALSE);
1465 /* Extract the effective monster level */
1466 rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
1468 monster_desc(m_name, m_ptr, 0);
1469 monster_desc(t_name, t_ptr, 0);
1471 /* Assume no blink */
1474 if (!see_either && known)
1479 if (p_ptr->riding && (m_idx == p_ptr->riding)) disturb(TRUE, TRUE);
1481 /* Scan through all four blows */
1482 for (ap_cnt = 0; ap_cnt < 4; ap_cnt++)
1484 bool obvious = FALSE;
1486 HIT_POINT power = 0;
1487 HIT_POINT damage = 0;
1491 /* Extract the attack infomation */
1492 int effect = r_ptr->blow[ap_cnt].effect;
1493 int method = r_ptr->blow[ap_cnt].method;
1494 int d_dice = r_ptr->blow[ap_cnt].d_dice;
1495 int d_side = r_ptr->blow[ap_cnt].d_side;
1497 if (!m_ptr->r_idx) break;
1499 /* Stop attacking if the target dies! */
1500 if (t_ptr->fx != x_saver || t_ptr->fy != y_saver)
1503 /* Hack -- no more attacks */
1506 if (method == RBM_SHOOT) continue;
1508 /* Extract the attack "power" */
1509 power = mbe_info[effect].power;
1512 if (!effect || check_hit2(power, rlev, ac, MON_STUNNED(m_ptr)))
1514 (void)set_monster_csleep(t_idx, 0);
1518 /* Redraw the health bar */
1519 if (p_ptr->health_who == t_idx) p_ptr->redraw |= (PR_HEALTH);
1520 if (p_ptr->riding == t_idx) p_ptr->redraw |= (PR_UHEALTH);
1523 /* Describe the attack method */
1528 act = _("%sを殴った。", "hits %s.");
1535 act = _("%sを触った。", "touches %s.");
1542 act = _("%sをパンチした。", "punches %s.");
1549 act = _("%sを蹴った。", "kicks %s.");
1556 act = _("%sをひっかいた。", "claws %s.");
1563 act = _("%sを噛んだ。", "bites %s.");
1570 act = _("%sを刺した。", "stings %s.");
1577 act = _("%sを斬った。", "slashes %s.");
1583 act = _("%sを角で突いた。", "butts %s.");
1590 act = _("%sに体当りした。", "crushes %s.");
1597 act = _("%sを飲み込んだ。", "engulfs %s.");
1604 act = _("%sに請求書をよこした。", "charges %s.");
1611 act = _("%sの体の上を這い回った。", "crawls on %s.");
1618 act = _("%sによだれをたらした。", "drools on %s.");
1625 act = _("%sに唾を吐いた。", "spits on %s.");
1632 if (see_either) disturb(TRUE, TRUE);
1633 act = _("爆発した。", "explodes.");
1641 act = _("%sをにらんだ。", "gazes at %s.");
1648 act = _("%sに泣きついた。", "wails at %s.");
1655 act = _("%sに胞子を飛ばした。", "releases spores at %s.");
1662 act = _("%sにXXX4を飛ばした。", "projects XXX4's at %s.");
1669 act = _("%sに金をせがんだ。", "begs %s for money.");
1676 act = _("%sを侮辱した。", "insults %s.");
1683 act = _("%sにむかってうめいた。", "moans at %s.");
1690 act = _("%sにむかって歌った。", "sings to %s.");
1696 if (act && see_either)
1699 if (do_silly_attack) act = silly_attacks2[randint0(MAX_SILLY_ATTACK)];
1700 strfmt(temp, act, t_name);
1701 msg_format("%^sは%s", m_name, temp);
1703 if (do_silly_attack)
1705 act = silly_attacks[randint0(MAX_SILLY_ATTACK)];
1706 strfmt(temp, "%s %s.", act, t_name);
1708 else strfmt(temp, act, t_name);
1709 msg_format("%^s %s", m_name, temp);
1713 /* Hack -- assume all attacks are obvious */
1716 /* Roll out the damage */
1717 damage = damroll(d_dice, d_side);
1719 /* Assume no effect */
1720 effect_type = BLOW_EFFECT_TYPE_NONE;
1724 /* Apply appropriate damage */
1733 if ((randint1(rlev*2+250) > (ac+200)) || one_in_(13))
1735 int tmp_damage = damage - (damage * ((ac < 150) ? ac : 150) / 250);
1736 damage = MAX(damage, tmp_damage * 2);
1743 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1758 if ((p_ptr->riding != m_idx) && one_in_(2)) blinked = TRUE;
1794 effect_type = BLOW_EFFECT_TYPE_FEAR;
1798 effect_type = BLOW_EFFECT_TYPE_SLEEP;
1802 damage -= (damage * ((ac < 150) ? ac : 150) / 250);
1803 if (damage > 23) earthquake_aux(m_ptr->fy, m_ptr->fx, 8, m_idx);
1818 pt = GF_HYPODYNAMIA;
1819 effect_type = BLOW_EFFECT_TYPE_HEAL;
1837 /* Do damage if not exploding */
1840 project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1841 damage, pt, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1844 switch (effect_type)
1846 case BLOW_EFFECT_TYPE_FEAR:
1847 project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1848 damage, GF_TURN_ALL, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1851 case BLOW_EFFECT_TYPE_SLEEP:
1852 project(m_idx, 0, t_ptr->fy, t_ptr->fx,
1853 r_ptr->level, GF_OLD_SLEEP, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1856 case BLOW_EFFECT_TYPE_HEAL:
1857 if ((monster_living(m_idx)) && (damage > 2))
1859 bool did_heal = FALSE;
1861 if (m_ptr->hp < m_ptr->maxhp) did_heal = TRUE;
1864 m_ptr->hp += damroll(4, damage / 6);
1865 if (m_ptr->hp > m_ptr->maxhp) m_ptr->hp = m_ptr->maxhp;
1867 /* Redraw (later) if needed */
1868 if (p_ptr->health_who == m_idx) p_ptr->redraw |= (PR_HEALTH);
1869 if (p_ptr->riding == m_idx) p_ptr->redraw |= (PR_UHEALTH);
1871 /* Special message */
1872 if (see_m && did_heal)
1874 msg_format(_("%sは体力を回復したようだ。", "%^s appears healthier."), m_name);
1883 if ((tr_ptr->flags2 & RF2_AURA_FIRE) && m_ptr->r_idx)
1885 if (!(r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK))
1889 msg_format(_("%^sは突然熱くなった!", "%^s is suddenly very hot!"), m_name);
1891 if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_FIRE;
1892 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1893 damroll (1 + ((tr_ptr->level) / 26),
1894 1 + ((tr_ptr->level) / 17)),
1895 GF_FIRE, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1899 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_FIRE_MASK);
1904 if ((tr_ptr->flags3 & RF3_AURA_COLD) && m_ptr->r_idx)
1906 if (!(r_ptr->flagsr & RFR_EFF_IM_COLD_MASK))
1910 msg_format(_("%^sは突然寒くなった!", "%^s is suddenly very cold!"), m_name);
1912 if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags3 |= RF3_AURA_COLD;
1913 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1914 damroll (1 + ((tr_ptr->level) / 26),
1915 1 + ((tr_ptr->level) / 17)),
1916 GF_COLD, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1920 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_COLD_MASK);
1925 if ((tr_ptr->flags2 & RF2_AURA_ELEC) && m_ptr->r_idx)
1927 if (!(r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK))
1931 msg_format(_("%^sは電撃を食らった!", "%^s gets zapped!"), m_name);
1933 if (m_ptr->ml && is_original_ap_and_seen(t_ptr)) tr_ptr->r_flags2 |= RF2_AURA_ELEC;
1934 project(t_idx, 0, m_ptr->fy, m_ptr->fx,
1935 damroll (1 + ((tr_ptr->level) / 26),
1936 1 + ((tr_ptr->level) / 17)),
1937 GF_ELEC, PROJECT_KILL | PROJECT_STOP | PROJECT_AIMED, -1);
1941 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flagsr |= (r_ptr->flagsr & RFR_EFF_IM_ELEC_MASK);
1948 /* Monster missed player */
1951 /* Analyze failed attacks */
1967 (void)set_monster_csleep(t_idx, 0);
1969 /* Visible monsters */
1973 msg_format("%sは%^sの攻撃をかわした。", t_name, m_name);
1975 msg_format("%^s misses %s.", m_name, t_name);
1985 /* Analyze "visible" monsters only */
1986 if (is_original_ap_and_seen(m_ptr) && !do_silly_attack)
1988 /* Count "obvious" attacks (and ones that cause damage) */
1989 if (obvious || damage || (r_ptr->r_blows[ap_cnt] > 10))
1991 /* Count attacks of this type */
1992 if (r_ptr->r_blows[ap_cnt] < MAX_UCHAR)
1994 r_ptr->r_blows[ap_cnt]++;
2002 sound(SOUND_EXPLODE);
2004 /* Cancel Invulnerability */
2005 (void)set_monster_invulner(m_idx, 0, FALSE);
2006 mon_take_hit_mon(m_idx, m_ptr->hp + 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2011 if (blinked && m_ptr->r_idx)
2013 if (teleport_barrier(m_idx))
2017 msg_print(_("泥棒は笑って逃げ...ようとしたがバリアに防がれた。", "The thief flees laughing...? But magic barrier obstructs it."));
2028 msg_print(_("泥棒は笑って逃げた!", "The thief flees laughing!"));
2035 teleport_away(m_idx, MAX_SIGHT * 2 + 5, 0L);
2043 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
2045 return !have_flag(f_ptr->flags, FF_GLASS) ||
2046 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
2047 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
2052 * @brief モンスター単体の1ターン行動処理メインルーチン /
2054 * @param m_idx 行動モンスターの参照ID
2057 * The monster is known to be within 100 grids of the player\n
2059 * In several cases, we directly update the monster lore\n
2061 * Note that a monster is only allowed to "reproduce" if there\n
2062 * are a limited number of "reproducing" monsters on the current\n
2063 * level. This should prevent the level from being "swamped" by\n
2064 * reproducing monsters. It also allows a large mass of mice to\n
2065 * prevent a louse from multiplying, but this is a small price to\n
2066 * pay for a simple multiplication method.\n
2068 * XXX Monster fear is slightly odd, in particular, monsters will\n
2069 * fixate on opening a door even if they cannot open it. Actually,\n
2070 * the same thing happens to normal monsters when they hit a door\n
2072 * In addition, monsters which *cannot* open or bash\n
2073 * down a door will still stand there trying to open it...\n
2075 * XXX Technically, need to check for monster in the way\n
2076 * combined with that monster being in a wall (or door?)\n
2078 * A "direction" of "5" means "pick a random direction".\n
2080 void process_monster(MONSTER_IDX m_idx)
2082 monster_type *m_ptr = ¤t_floor_ptr->m_list[m_idx];
2083 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2084 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2087 POSITION oy, ox, ny, nx;
2092 feature_type *f_ptr;
2094 monster_type *y_ptr;
2099 bool must_alter_to_move;
2108 bool gets_angry = FALSE;
2113 bool is_riding_mon = (m_idx == p_ptr->riding);
2114 bool see_m = is_seen(m_ptr);
2116 if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
2118 if (rakuba(0, TRUE))
2121 msg_print("地面に落とされた。");
2123 GAME_TEXT m_name[MAX_NLEN];
2124 monster_desc(m_name, ¤t_floor_ptr->m_list[p_ptr->riding], 0);
2125 msg_format("You have fallen from %s.", m_name);
2130 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
2132 choose_new_monster(m_idx, FALSE, 0);
2133 r_ptr = &r_info[m_ptr->r_idx];
2136 /* Players hidden in shadow are almost imperceptable. -LM- */
2137 if (p_ptr->special_defense & NINJA_S_STEALTH)
2139 int tmp = p_ptr->lev*6+(p_ptr->skill_stl+10)*4;
2140 if (p_ptr->monlite) tmp /= 3;
2141 if (p_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
2142 if (r_ptr->level > (p_ptr->lev * p_ptr->lev / 20 + 10)) tmp /= 3;
2143 /* Low-level monsters will find it difficult to locate the player. */
2144 if (randint0(tmp) > (r_ptr->level+20)) aware = FALSE;
2147 /* Are there its parent? */
2148 if (m_ptr->parent_m_idx && !current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2150 /* Its parent have gone, it also goes away. */
2154 GAME_TEXT m_name[MAX_NLEN];
2155 monster_desc(m_name, m_ptr, 0);
2156 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2159 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2161 GAME_TEXT m_name[MAX_NLEN];
2162 monster_desc(m_name, m_ptr, MD_INDEF_VISIBLE);
2163 do_cmd_write_nikki(NIKKI_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2166 delete_monster_idx(m_idx);
2171 /* Quantum monsters are odd */
2172 if (r_ptr->flags2 & (RF2_QUANTUM))
2174 /* Sometimes skip move */
2175 if (!randint0(2)) return;
2178 if (!randint0((m_idx % 100) + 10) && !(r_ptr->flags1 & RF1_QUESTOR))
2182 if (is_pet(m_ptr) && !(m_ptr->ml)) sad = TRUE;
2186 GAME_TEXT m_name[MAX_NLEN];
2187 monster_desc(m_name, m_ptr, 0);
2189 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2192 /* Generate treasure, etc */
2193 monster_death(m_idx, FALSE);
2195 delete_monster_idx(m_idx);
2198 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2205 if (m_ptr->r_idx == MON_SHURYUUDAN)
2207 mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
2211 if ((is_pet(m_ptr) || is_friendly(m_ptr)) && ((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) && !p_ptr->inside_battle)
2213 static int riding_pinch = 0;
2215 if (m_ptr->hp < m_ptr->maxhp/3)
2217 GAME_TEXT m_name[MAX_NLEN];
2218 monster_desc(m_name, m_ptr, 0);
2220 if (is_riding_mon && riding_pinch < 2)
2222 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2223 "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
2225 disturb(TRUE, TRUE);
2231 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2232 if (rakuba(-1, FALSE))
2234 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2240 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) &&
2241 player_has_los_bold(m_ptr->fy, m_ptr->fx) && projectable(m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x))
2243 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2245 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
2246 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2249 if (is_riding_mon && rakuba(-1, FALSE))
2251 msg_print(_("地面に落とされた。", "You have fallen from riding pet."));
2254 /* Check for quest completion */
2255 check_quest_completion(m_ptr);
2257 delete_monster_idx(m_idx);
2264 /* Reset the counter */
2265 if (is_riding_mon) riding_pinch = 0;
2269 /* Handle "sleep" */
2270 if (MON_CSLEEP(m_ptr))
2272 /* Handle non-aggravation - Still sleeping */
2273 if (!(p_ptr->cursed & TRC_AGGRAVATE)) return;
2275 /* Reset sleep counter */
2276 (void)set_monster_csleep(m_idx, 0);
2278 /* Notice the "waking up" */
2281 GAME_TEXT m_name[MAX_NLEN];
2282 monster_desc(m_name, m_ptr, 0);
2283 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2286 /* Hack -- Count the wakings */
2287 if (is_original_ap_and_seen(m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2294 if (MON_STUNNED(m_ptr))
2296 /* Sometimes skip move */
2297 if (one_in_(2)) return;
2302 p_ptr->update |= (PU_BONUS);
2305 /* No one wants to be your friend if you're aggravating */
2306 if (is_friendly(m_ptr) && (p_ptr->cursed & TRC_AGGRAVATE))
2309 /* Paranoia... no pet uniques outside wizard mode -- TY */
2310 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2311 monster_has_hostile_align(NULL, 10, -10, r_ptr))
2312 || (r_ptr->flagsr & RFR_RES_ALL)))
2317 if (p_ptr->inside_battle) gets_angry = FALSE;
2321 if (is_pet(m_ptr) || see_m)
2323 GAME_TEXT m_name[MAX_NLEN];
2324 monster_desc(m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2325 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2331 /* Get the origin */
2335 /* Attempt to "multiply" if able and allowed */
2336 if ((r_ptr->flags2 & RF2_MULTIPLY) && (current_floor_ptr->num_repro < MAX_REPRO))
2341 /* Count the adjacent monsters */
2342 for (k = 0, y = oy - 1; y <= oy + 1; y++)
2344 for (x = ox - 1; x <= ox + 1; x++)
2346 /* Ignore locations off of edge */
2347 if (!in_bounds2(y, x)) continue;
2348 if (current_floor_ptr->grid_array[y][x].m_idx) k++;
2353 if (multiply_barrier(m_idx)) k = 8;
2355 /* Hack -- multiply slower in crowded areas */
2356 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2358 /* Try to multiply */
2359 if (multiply_monster(m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2361 /* Take note if visible */
2362 if (current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(m_ptr))
2364 r_ptr->r_flags2 |= (RF2_MULTIPLY);
2367 /* Multiplying takes energy */
2373 if (r_ptr->a_ability_flags2 & RF6_SPECIAL)
2375 /* Hack -- Ohmu scatters molds! */
2376 if (m_ptr->r_idx == MON_OHMU)
2378 if (!p_ptr->inside_arena && !p_ptr->inside_battle)
2380 if (r_ptr->freq_spell && (randint1(100) <= r_ptr->freq_spell))
2383 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2384 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2386 for (k = 0; k < A_MAX; k++)
2388 if (summon_specific(m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode), '\0'))
2390 if (current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2394 if (count && is_original_ap_and_seen(m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2400 if (!p_ptr->inside_battle)
2402 /* Hack! "Cyber" monster makes noise... */
2403 if (m_ptr->ap_r_idx == MON_CYBER &&
2404 one_in_(CYBERNOISE) &&
2405 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2407 if (disturb_minor) disturb(FALSE, FALSE);
2408 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2411 /* Some monsters can speak */
2412 if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
2413 one_in_(SPEAK_CHANCE) &&
2414 player_has_los_bold(oy, ox) &&
2415 projectable(oy, ox, p_ptr->y, p_ptr->x))
2417 GAME_TEXT m_name[MAX_NLEN];
2418 char monmessage[1024];
2421 /* Acquire the monster name/poss */
2423 monster_desc(m_name, m_ptr, 0);
2425 strcpy(m_name, _("それ", "It"));
2427 /* Select the file for monster quotes */
2428 if (MON_MONFEAR(m_ptr))
2429 filename = _("monfear_j.txt", "monfear.txt");
2430 else if (is_pet(m_ptr))
2431 filename = _("monpet_j.txt", "monpet.txt");
2432 else if (is_friendly(m_ptr))
2433 filename = _("monfrien_j.txt", "monfrien.txt");
2435 filename = _("monspeak_j.txt", "monspeak.txt");
2436 /* Get the monster line */
2437 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2440 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2445 /* Try to cast spell occasionally */
2446 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
2448 bool counterattack = FALSE;
2450 /* Give priority to counter attack? */
2451 if (m_ptr->target_y)
2453 MONSTER_IDX t_m_idx = current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2455 /* The monster must be an enemy, and projectable */
2456 if (t_m_idx && are_enemies(m_ptr, ¤t_floor_ptr->m_list[t_m_idx]) &&
2457 projectable(m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2459 counterattack = TRUE;
2465 /* Attempt to cast a spell */
2466 if (aware && make_attack_spell(m_idx)) return;
2469 * Attempt to cast a spell at an enemy other than the player
2470 * (may slow the game a smidgeon, but I haven't noticed.)
2472 if (monst_spell_monst(m_idx)) return;
2476 /* Attempt to do counter attack at first */
2477 if (monst_spell_monst(m_idx)) return;
2479 if (aware && make_attack_spell(m_idx)) return;
2483 /* Hack -- Assume no movement */
2484 mm[0] = mm[1] = mm[2] = mm[3] = 0;
2485 mm[4] = mm[5] = mm[6] = mm[7] = 0;
2488 /* Confused -- 100% random */
2489 if (MON_CONFUSED(m_ptr) || !aware)
2491 /* Try four "random" directions */
2492 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2495 /* 75% random movement */
2496 else if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) &&
2497 (randint0(100) < 75))
2499 /* Memorize flags */
2500 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2502 /* Try four "random" directions */
2503 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2506 /* 50% random movement */
2507 else if ((r_ptr->flags1 & RF1_RAND_50) &&
2508 (randint0(100) < 50))
2510 /* Memorize flags */
2511 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2513 /* Try four "random" directions */
2514 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2517 /* 25% random movement */
2518 else if ((r_ptr->flags1 & RF1_RAND_25) &&
2519 (randint0(100) < 25))
2521 /* Memorize flags */
2522 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2524 /* Try four "random" directions */
2525 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2528 /* Can't reach player - find something else to hit */
2529 else if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2531 /* Try four "random" directions */
2532 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2534 /* Look for an enemy */
2535 #if 0 /* Hack - Too slow. Mimic pits are horrible with this on. */
2536 get_enemy_dir(m_idx, mm);
2540 /* Pets will follow the player */
2541 else if (is_pet(m_ptr))
2543 /* Are we trying to avoid the player? */
2544 bool avoid = ((p_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
2546 /* Do we want to find the player? */
2547 bool lonely = (!avoid && (m_ptr->cdis > p_ptr->pet_follow_distance));
2549 /* Should we find the player if we can't find a monster? */
2550 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2552 /* by default, move randomly */
2553 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2555 /* Look for an enemy */
2556 if (!get_enemy_dir(m_idx, mm))
2558 /* Find the player if necessary */
2559 if (avoid || lonely || distant)
2561 /* Remember the leash length */
2562 POSITION dis = p_ptr->pet_follow_distance;
2564 /* Hack -- adjust follow distance temporarily */
2565 if (p_ptr->pet_follow_distance > PET_SEEK_DIST)
2567 p_ptr->pet_follow_distance = PET_SEEK_DIST;
2570 /* Find the player */
2571 (void)get_moves(m_idx, mm);
2573 /* Restore the leash */
2574 p_ptr->pet_follow_distance = (s16b)dis;
2579 /* Friendly monster movement */
2580 else if (!is_hostile(m_ptr))
2582 /* by default, move randomly */
2583 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2585 /* Look for an enemy */
2586 get_enemy_dir(m_idx, mm);
2588 /* Normal movement */
2591 /* Logical moves, may do nothing */
2592 if (!get_moves(m_idx, mm)) return;
2595 /* Assume nothing */
2599 must_alter_to_move = FALSE;
2601 /* Assume nothing */
2602 did_open_door = FALSE;
2603 did_bash_door = FALSE;
2604 did_take_item = FALSE;
2605 did_kill_item = FALSE;
2606 did_move_body = FALSE;
2607 did_pass_wall = FALSE;
2608 did_kill_wall = FALSE;
2610 /* Take a zero-terminated array of "directions" */
2611 for (i = 0; mm[i]; i++)
2613 /* Get the direction */
2616 /* Hack -- allow "randomized" motion */
2617 if (d == 5) d = ddd[randint0(8)];
2619 /* Get the destination */
2623 /* Ignore locations off of edge */
2624 if (!in_bounds2(ny, nx)) continue;
2626 /* Access that grid */
2627 g_ptr = ¤t_floor_ptr->grid_array[ny][nx];
2628 f_ptr = &f_info[g_ptr->feat];
2629 can_cross = monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
2631 /* Access that grid's contents */
2632 y_ptr = ¤t_floor_ptr->m_list[g_ptr->m_idx];
2634 /* Hack -- player 'in' wall */
2635 if (player_bold(ny, nx))
2640 /* Possibly a monster to attack */
2641 else if (g_ptr->m_idx)
2646 /* Monster destroys walls (and doors) */
2647 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
2648 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
2649 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
2650 check_hp_for_feat_destruction(f_ptr, m_ptr))
2652 /* Eat through walls/doors/rubble */
2654 if (!can_cross) must_alter_to_move = TRUE;
2656 /* Monster destroyed a wall (later) */
2657 did_kill_wall = TRUE;
2660 /* Floor is open? */
2663 /* Go ahead and move */
2666 /* Monster moves through walls (and doors) */
2667 if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || p_ptr->pass_wall) &&
2668 have_flag(f_ptr->flags, FF_CAN_PASS))
2670 /* Monster went through a wall */
2671 did_pass_wall = TRUE;
2675 /* Handle doors and secret doors */
2676 else if (is_closed_door(g_ptr->feat))
2678 bool may_bash = TRUE;
2680 /* Assume no move allowed */
2683 /* Creature can open doors. */
2684 if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
2685 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2690 /* The door is open */
2691 did_open_door = TRUE;
2693 /* Do not bash the door */
2699 /* Locked doors (not jammed) */
2702 /* Try to unlock it */
2703 if (randint0(m_ptr->hp / 10) > f_ptr->power)
2705 /* Unlock the door */
2706 cave_alter_feat(ny, nx, FF_DISARM);
2708 /* Do not bash the door */
2716 /* Stuck doors -- attempt to bash them down if allowed */
2717 if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
2718 (!is_pet(m_ptr) || (p_ptr->pet_extra_flags & PF_OPEN_DOORS)))
2720 /* Attempt to Bash */
2721 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
2723 if (have_flag(f_ptr->flags, FF_GLASS))
2724 msg_print(_("ガラスが砕ける音がした!", "You hear a glass was crashed!"));
2726 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
2728 /* Disturb (sometimes) */
2729 if (disturb_minor) disturb(FALSE, FALSE);
2731 /* The door was bashed open */
2732 did_bash_door = TRUE;
2734 /* Hack -- fall into doorway */
2736 must_alter_to_move = TRUE;
2741 /* Deal with doors in the way */
2742 if (did_open_door || did_bash_door)
2744 /* Break down the door */
2745 if (did_bash_door && ((randint0(100) < 50) || (feat_state(g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
2747 cave_alter_feat(ny, nx, FF_BASH);
2749 if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2751 /* Update some things */
2752 p_ptr->update |= (PU_FLOW);
2753 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2754 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
2763 cave_alter_feat(ny, nx, FF_OPEN);
2766 f_ptr = &f_info[g_ptr->feat];
2768 /* Handle viewable doors */
2773 /* Hack -- check for Glyph of Warding */
2774 if (do_move && is_glyph_grid(g_ptr) &&
2775 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2777 /* Assume no move allowed */
2780 /* Break the ward */
2781 if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
2783 /* Describe observable breakage */
2784 if (g_ptr->info & CAVE_MARK)
2786 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
2789 /* Forget the rune */
2790 g_ptr->info &= ~(CAVE_MARK);
2792 /* Break the rune */
2793 g_ptr->info &= ~(CAVE_OBJECT);
2796 /* Allow movement */
2802 else if (do_move && is_explosive_rune_grid(g_ptr) &&
2803 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(ny, nx)))
2805 /* Assume no move allowed */
2808 /* Break the ward */
2811 /* Break the ward */
2812 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
2814 /* Describe observable breakage */
2815 if (g_ptr->info & CAVE_MARK)
2817 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
2818 project(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);
2823 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
2826 /* Forget the rune */
2827 g_ptr->info &= ~(CAVE_MARK);
2829 /* Break the rune */
2830 g_ptr->info &= ~(CAVE_OBJECT);
2836 if (!m_ptr->r_idx) return;
2837 /* Allow movement */
2842 /* The player is in the way */
2843 if (do_move && player_bold(ny, nx))
2845 /* Some monsters never attack */
2846 if (r_ptr->flags1 & RF1_NEVER_BLOW)
2848 /* Hack -- memorize lack of attacks */
2849 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
2855 /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2856 if (do_move && (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
2858 if (!MON_CONFUSED(m_ptr))
2860 if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
2863 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2868 /* The player is in the way. Attack him. */
2871 if (!p_ptr->riding || one_in_(2))
2874 (void)make_attack_normal(m_idx);
2879 /* Took a current_world_ptr->game_turn */
2885 /* A monster is in the way */
2886 if (do_move && g_ptr->m_idx)
2888 monster_race *z_ptr = &r_info[y_ptr->r_idx];
2890 /* Assume no movement */
2893 /* Attack 'enemies' */
2894 if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
2895 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
2896 can_cross && (g_ptr->m_idx != p_ptr->riding)) ||
2897 are_enemies(m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
2899 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
2901 if (r_ptr->flags2 & RF2_KILL_BODY)
2903 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
2907 if (y_ptr->r_idx && (y_ptr->hp >= 0))
2909 if (monst_attack_monst(m_idx, g_ptr->m_idx)) return;
2911 /* In anti-melee dungeon, stupid or confused monster takes useless current_world_ptr->game_turn */
2912 else if (d_info[p_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
2914 if (MON_CONFUSED(m_ptr)) return;
2915 else if (r_ptr->flags2 & RF2_STUPID)
2917 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
2925 /* Push past weaker monsters (unless leaving a wall) */
2926 else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
2927 (r_ptr->mexp > z_ptr->mexp) &&
2928 can_cross && (g_ptr->m_idx != p_ptr->riding) &&
2929 monster_can_cross_terrain(current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
2931 /* Allow movement */
2934 /* Monster pushed past another monster */
2935 did_move_body = TRUE;
2937 /* Wake up the moved monster */
2938 (void)set_monster_csleep(g_ptr->m_idx, 0);
2946 if (!p_ptr->riding_ryoute && !MON_MONFEAR(¤t_floor_ptr->m_list[p_ptr->riding])) do_move = FALSE;
2949 if (did_kill_wall && do_move)
2951 if (one_in_(GRINDNOISE))
2953 if (have_flag(f_ptr->flags, FF_GLASS))
2954 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
2956 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
2959 cave_alter_feat(ny, nx, FF_HURT_DISI);
2961 if (!m_ptr->r_idx) /* Killed by shards of glass, etc. */
2963 /* Update some things */
2964 p_ptr->update |= (PU_FLOW);
2965 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
2966 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2971 f_ptr = &f_info[g_ptr->feat];
2973 /* Note changes to viewable region */
2978 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
2980 if (!monster_can_cross_terrain(g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
2982 /* Assume no move allowed */
2988 * Check if monster can cross terrain
2989 * This is checked after the normal attacks
2990 * to allow monsters to attack an enemy,
2991 * even if it can't enter the terrain.
2993 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
2995 /* Assume no move allowed */
2999 /* Some monsters never move */
3000 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
3002 /* Hack -- memorize lack of moves */
3003 if (is_original_ap_and_seen(m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
3009 /* Creature has been allowed move */
3014 if (have_flag(f_ptr->flags, FF_TREE))
3016 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
3018 m_ptr->energy_need += ENERGY_NEED();
3024 /* Hack -- Update the old location */
3025 current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
3027 /* Mega-Hack -- move the old monster, if any */
3030 /* Move the old monster */
3034 /* Update the old monster */
3035 update_monster(g_ptr->m_idx, TRUE);
3038 /* Hack -- Update the new location */
3039 g_ptr->m_idx = m_idx;
3041 /* Move the monster */
3044 update_monster(m_idx, TRUE);
3051 /* sound(SOUND_WALK); */
3052 if (!move_player_effect(ny, nx, MPE_DONT_PICKUP)) break;
3055 /* Possible disturb */
3058 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(p_ptr->y, p_ptr->x, m_ptr->fy, m_ptr->fx)) ||
3059 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= p_ptr->lev)))
3061 if (is_hostile(m_ptr))
3062 disturb(FALSE, TRUE);
3065 /* Take or Kill objects on the floor */
3066 if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
3067 (!is_pet(m_ptr) || ((p_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
3069 OBJECT_IDX this_o_idx, next_o_idx;
3070 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
3072 /* Scan all objects in the grid */
3073 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
3075 BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
3076 GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
3077 object_type *o_ptr = ¤t_floor_ptr->o_list[this_o_idx];
3078 next_o_idx = o_ptr->next_o_idx;
3083 if (o_ptr->tval == TV_GOLD) continue;
3086 * Skip "real" corpses and statues, to avoid extreme
3087 * silliness like a novice rogue pockets full of statues
3090 if ((o_ptr->tval == TV_CORPSE) ||
3091 (o_ptr->tval == TV_STATUE)) continue;
3094 /* Extract some flags */
3095 object_flags(o_ptr, flgs);
3097 /* Acquire the object name */
3098 object_desc(o_name, o_ptr, 0);
3099 monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
3101 /* React to objects that hurt the monster */
3102 if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
3103 if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
3104 if (have_flag(flgs, TR_SLAY_TROLL)) flg3 |= (RF3_TROLL);
3105 if (have_flag(flgs, TR_KILL_TROLL)) flg3 |= (RF3_TROLL);
3106 if (have_flag(flgs, TR_SLAY_GIANT)) flg3 |= (RF3_GIANT);
3107 if (have_flag(flgs, TR_KILL_GIANT)) flg3 |= (RF3_GIANT);
3108 if (have_flag(flgs, TR_SLAY_ORC)) flg3 |= (RF3_ORC);
3109 if (have_flag(flgs, TR_KILL_ORC)) flg3 |= (RF3_ORC);
3110 if (have_flag(flgs, TR_SLAY_DEMON)) flg3 |= (RF3_DEMON);
3111 if (have_flag(flgs, TR_KILL_DEMON)) flg3 |= (RF3_DEMON);
3112 if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
3113 if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
3114 if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
3115 if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
3116 if (have_flag(flgs, TR_SLAY_EVIL)) flg3 |= (RF3_EVIL);
3117 if (have_flag(flgs, TR_KILL_EVIL)) flg3 |= (RF3_EVIL);
3118 if (have_flag(flgs, TR_SLAY_HUMAN)) flg2 |= (RF2_HUMAN);
3119 if (have_flag(flgs, TR_KILL_HUMAN)) flg2 |= (RF2_HUMAN);
3120 if (have_flag(flgs, TR_BRAND_ACID)) flgr |= (RFR_IM_ACID);
3121 if (have_flag(flgs, TR_BRAND_ELEC)) flgr |= (RFR_IM_ELEC);
3122 if (have_flag(flgs, TR_BRAND_FIRE)) flgr |= (RFR_IM_FIRE);
3123 if (have_flag(flgs, TR_BRAND_COLD)) flgr |= (RFR_IM_COLD);
3124 if (have_flag(flgs, TR_BRAND_POIS)) flgr |= (RFR_IM_POIS);
3126 /* The object cannot be picked up by the monster */
3127 if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
3128 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
3130 /* Only give a message for "take_item" */
3131 if (do_take && (r_ptr->flags2 & RF2_STUPID))
3133 did_take_item = TRUE;
3135 /* Describe observable situations */
3136 if (m_ptr->ml && player_can_see_bold(ny, nx))
3138 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
3143 /* Pick up the item */
3146 did_take_item = TRUE;
3148 /* Describe observable situations */
3149 if (player_can_see_bold(ny, nx))
3151 msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
3154 /* Excise the object */
3155 excise_object_idx(this_o_idx);
3158 o_ptr->marked &= OM_TOUCHED;
3160 /* Forget location */
3161 o_ptr->iy = o_ptr->ix = 0;
3163 /* Memorize monster */
3164 o_ptr->held_m_idx = m_idx;
3167 o_ptr->next_o_idx = m_ptr->hold_o_idx;
3170 m_ptr->hold_o_idx = this_o_idx;
3173 /* Destroy the item if not a pet */
3174 else if (!is_pet(m_ptr))
3176 did_kill_item = TRUE;
3178 /* Describe observable situations */
3179 if (player_has_los_bold(ny, nx))
3181 msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
3184 delete_object_idx(this_o_idx);
3190 /* Stop when done */
3195 * Forward movements failed, but now received LOS attack!
3196 * Try to flow by smell.
3198 if (p_ptr->no_flowed && i > 2 && m_ptr->target_y)
3199 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3201 /* If we haven't done anything, try casting a spell again */
3202 if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
3204 /* Try to cast spell again */
3205 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
3207 if (make_attack_spell(m_idx)) return;
3212 /* Notice changes in view */
3215 p_ptr->update |= (PU_FLOW);
3216 p_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
3219 /* Notice changes in view */
3220 if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
3221 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !p_ptr->inside_battle)))
3223 /* Update some things */
3224 p_ptr->update |= (PU_MON_LITE);
3227 /* Learn things from observable monster */
3228 if (is_original_ap_and_seen(m_ptr))
3230 /* Monster opened a door */
3231 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
3233 /* Monster bashed a door */
3234 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
3236 /* Monster tried to pick something up */
3237 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
3239 /* Monster tried to crush something */
3240 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
3242 /* Monster pushed past another monster */
3243 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
3245 /* Monster passed through a wall */
3246 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
3248 /* Monster destroyed a wall */
3249 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
3253 /* Hack -- get "bold" if out of options */
3254 if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
3256 /* No longer afraid */
3257 (void)set_monster_monfear(m_idx, 0);
3259 /* Message if seen */
3262 GAME_TEXT m_name[MAX_NLEN];
3263 monster_desc(m_name, m_ptr, 0);
3264 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
3267 if (m_ptr->ml) chg_virtue(V_COMPASSION, -1);
3269 /* Actually do something now (?) */
3274 * @brief 全モンスターのターン管理メインルーチン /
3275 * Process all the "live" monsters, once per game current_world_ptr->game_turn.
3278 * During each game current_world_ptr->game_turn, we scan through the list of all the "live" monsters,\n
3279 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
3280 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
3282 * Note that monsters can never move in the monster array (except when the\n
3283 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
3285 * This function is responsible for at least half of the processor time\n
3286 * on a normal system with a "normal" amount of monsters and a player doing\n
3289 * When the player is resting, virtually 90% of the processor time is spent\n
3290 * in this function, and its children, "process_monster()" and "make_move()".\n
3292 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
3293 * especially when the player is running.\n
3295 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
3296 * monsters while they are still being "born". A monster is "fresh" only\n
3297 * during the current_world_ptr->game_turn in which it is created, and we use the "hack_m_idx" to\n
3298 * determine if the monster is yet to be processed during the current current_world_ptr->game_turn.\n
3300 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
3301 * move before any "nasty" monsters get to use their spell attacks.\n
3303 * Note that when the "knowledge" about the currently tracked monster\n
3304 * changes (flags, attacks, spells), we induce a redraw of the monster\n
3307 void process_monsters(void)
3314 monster_type *m_ptr;
3315 monster_race *r_ptr;
3317 MONRACE_IDX old_monster_race_idx;
3319 BIT_FLAGS old_r_flags1 = 0L;
3320 BIT_FLAGS old_r_flags2 = 0L;
3321 BIT_FLAGS old_r_flags3 = 0L;
3322 BIT_FLAGS old_r_flags4 = 0L;
3323 BIT_FLAGS old_r_flags5 = 0L;
3324 BIT_FLAGS old_r_flags6 = 0L;
3325 BIT_FLAGS old_r_flagsr = 0L;
3327 byte old_r_blows0 = 0;
3328 byte old_r_blows1 = 0;
3329 byte old_r_blows2 = 0;
3330 byte old_r_blows3 = 0;
3332 byte old_r_cast_spell = 0;
3336 /* Clear monster fighting indicator */
3339 /* Memorize old race */
3340 old_monster_race_idx = p_ptr->monster_race_idx;
3342 /* Acquire knowledge */
3343 if (p_ptr->monster_race_idx)
3345 /* Acquire current monster */
3346 r_ptr = &r_info[p_ptr->monster_race_idx];
3348 /* Memorize flags */
3349 old_r_flags1 = r_ptr->r_flags1;
3350 old_r_flags2 = r_ptr->r_flags2;
3351 old_r_flags3 = r_ptr->r_flags3;
3352 old_r_flags4 = r_ptr->r_flags4;
3353 old_r_flags5 = r_ptr->r_flags5;
3354 old_r_flags6 = r_ptr->r_flags6;
3355 old_r_flagsr = r_ptr->r_flagsr;
3357 /* Memorize blows */
3358 old_r_blows0 = r_ptr->r_blows[0];
3359 old_r_blows1 = r_ptr->r_blows[1];
3360 old_r_blows2 = r_ptr->r_blows[2];
3361 old_r_blows3 = r_ptr->r_blows[3];
3363 /* Memorize castings */
3364 old_r_cast_spell = r_ptr->r_cast_spell;
3368 /* Process the monsters (backwards) */
3369 for (i = m_max - 1; i >= 1; i--)
3371 /* Access the monster */
3372 m_ptr = ¤t_floor_ptr->m_list[i];
3373 r_ptr = &r_info[m_ptr->r_idx];
3375 /* Handle "leaving" */
3376 if (p_ptr->leaving) break;
3378 /* Ignore "dead" monsters */
3379 if (!m_ptr->r_idx) continue;
3381 if (p_ptr->wild_mode) continue;
3384 /* Handle "fresh" monsters */
3385 if (m_ptr->mflag & MFLAG_BORN)
3387 /* No longer "fresh" */
3388 m_ptr->mflag &= ~(MFLAG_BORN);
3394 /* Hack -- Require proximity */
3395 if (m_ptr->cdis >= AAF_LIMIT) continue;
3398 /* Access the location */
3402 /* Flow by smell is allowed */
3403 if (!p_ptr->no_flowed)
3405 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
3408 /* Assume no move */
3411 /* Handle "sensing radius" */
3412 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
3414 /* We can "sense" the player */
3418 /* Handle "sight" and "aggravation" */
3419 else if ((m_ptr->cdis <= MAX_SIGHT || p_ptr->inside_battle) &&
3420 (player_has_los_bold(fy, fx) || (p_ptr->cursed & TRC_AGGRAVATE)))
3422 /* We can "see" or "feel" the player */
3426 #if 0 /* (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) is always FALSE... */
3427 /* Hack -- Monsters can "smell" the player from far away */
3428 /* Note that most monsters have "aaf" of "20" or so */
3429 else if (!(m_ptr->mflag2 & MFLAG2_NOFLOW) &&
3430 cave_have_flag_bold(p_ptr->y, p_ptr->x, FF_MOVE) &&
3431 (current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when == current_floor_ptr->grid_array[fy][fx].when) &&
3432 (current_floor_ptr->grid_array[fy][fx].dist < MONSTER_FLOW_DEPTH) &&
3433 (current_floor_ptr->grid_array[fy][fx].dist < r_ptr->aaf))
3435 /* We can "smell" the player */
3439 else if (m_ptr->target_y) test = TRUE;
3442 if (!test) continue;
3445 if (p_ptr->riding == i)
3446 speed = p_ptr->pspeed;
3449 speed = m_ptr->mspeed;
3451 /* Monsters move quickly in Nightmare mode */
3452 if (ironman_nightmare) speed += 5;
3454 if (MON_FAST(m_ptr)) speed += 10;
3455 if (MON_SLOW(m_ptr)) speed -= 10;
3458 /* Give this monster some energy */
3459 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
3461 /* Not enough energy to move */
3462 if (m_ptr->energy_need > 0) continue;
3464 /* Use up "some" energy */
3465 m_ptr->energy_need += ENERGY_NEED();
3468 /* Save global index */
3471 /* Process the monster */
3474 reset_target(m_ptr);
3476 /* Give up flow_by_smell when it might useless */
3477 if (p_ptr->no_flowed && one_in_(3))
3478 m_ptr->mflag2 |= MFLAG2_NOFLOW;
3480 /* Hack -- notice death or departure */
3481 if (!p_ptr->playing || p_ptr->is_dead) break;
3483 /* Notice leaving */
3484 if (p_ptr->leaving) break;
3487 /* Reset global index */
3491 /* Tracking a monster race (the same one we were before) */
3492 if (p_ptr->monster_race_idx && (p_ptr->monster_race_idx == old_monster_race_idx))
3494 /* Acquire monster race */
3495 r_ptr = &r_info[p_ptr->monster_race_idx];
3497 /* Check for knowledge change */
3498 if ((old_r_flags1 != r_ptr->r_flags1) ||
3499 (old_r_flags2 != r_ptr->r_flags2) ||
3500 (old_r_flags3 != r_ptr->r_flags3) ||
3501 (old_r_flags4 != r_ptr->r_flags4) ||
3502 (old_r_flags5 != r_ptr->r_flags5) ||
3503 (old_r_flags6 != r_ptr->r_flags6) ||
3504 (old_r_flagsr != r_ptr->r_flagsr) ||
3505 (old_r_blows0 != r_ptr->r_blows[0]) ||
3506 (old_r_blows1 != r_ptr->r_blows[1]) ||
3507 (old_r_blows2 != r_ptr->r_blows[2]) ||
3508 (old_r_blows3 != r_ptr->r_blows[3]) ||
3509 (old_r_cast_spell != r_ptr->r_cast_spell))
3511 p_ptr->window |= (PW_MONSTER);