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"
43 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
44 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx);
45 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
46 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
47 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
48 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
49 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
50 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m);
51 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
52 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
53 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
56 * @brief モンスターが敵に接近するための方向を決める /
57 * Calculate the direction to the next enemy
58 * @param target_ptr プレーヤーへの参照ポインタ
59 * @param m_idx モンスターの参照ID
60 * @param mm 移動するべき方角IDを返す参照ポインタ
61 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
63 static bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
65 floor_type *floor_ptr = target_ptr->current_floor_ptr;
66 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
67 monster_race *r_ptr = &r_info[m_ptr->r_idx];
70 POSITION x = 0, y = 0;
71 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
73 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
74 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
76 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
78 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
79 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
85 if (target_ptr->phase_out)
87 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
88 if (randint0(2)) plus = -1;
90 else start = floor_ptr->m_max + 1;
92 /* Scan thru all monsters */
93 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
95 MONSTER_IDX dummy = (i % floor_ptr->m_max);
99 MONSTER_IDX t_idx = dummy;
100 t_ptr = &floor_ptr->m_list[t_idx];
102 /* The monster itself isn't a target */
103 if (t_ptr == m_ptr) continue;
105 if (!monster_is_valid(t_ptr)) continue;
109 /* Hack -- only fight away from player */
110 if (target_ptr->pet_follow_distance < 0)
112 /* No fighting near player */
113 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
118 /* Hack -- no fighting away from player */
119 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
124 if (r_ptr->aaf < t_ptr->cdis) continue;
127 /* Monster must be 'an enemy' */
128 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
130 /* Monster must be projectable if we can't pass through walls */
131 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
132 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
134 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
138 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
141 /* OK -- we've got a target */
148 if (!x && !y) return FALSE;
151 /* Extract the direction */
156 if ((y < 0) && (x == 0))
163 else if ((y > 0) && (x == 0))
170 else if ((x > 0) && (y == 0))
177 else if ((x < 0) && (y == 0))
184 else if ((y < 0) && (x < 0))
191 else if ((y < 0) && (x > 0))
198 else if ((y > 0) && (x < 0))
205 else if ((y > 0) && (x > 0))
216 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
217 * Returns whether a given monster will try to run from the player.
218 * @param m_idx 逃走するモンスターの参照ID
219 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
221 * Monsters will attempt to avoid very powerful players. See below.\n
223 * Because this function is called so often, little details are important\n
224 * for efficiency. Like not using "mod" or "div" when possible. And\n
225 * attempting to check the conditions in an optimal order. Note that\n
226 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
228 * Note that this function is responsible for about one to five percent\n
229 * of the processor use in normal conditions...\n
231 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
233 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
234 monster_race *r_ptr = &r_info[m_ptr->r_idx];
238 HIT_POINT p_chp, p_mhp;
239 HIT_POINT m_chp, m_mhp;
242 /* Friends can be commanded to avoid the player */
245 /* Are we trying to avoid the player? */
246 return ((target_ptr->pet_follow_distance < 0) &&
247 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
250 /* Keep monsters from running too far away */
251 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
253 /* All "afraid" monsters will run away */
254 if (MON_MONFEAR(m_ptr)) return TRUE;
256 /* Nearby monsters will not become terrified */
257 if (m_ptr->cdis <= 5) return FALSE;
259 /* Examine player power (level) */
260 p_lev = target_ptr->lev;
262 /* Examine monster power (level plus morale) */
263 m_lev = r_ptr->level + (m_idx & 0x08) + 25;
265 /* Optimize extreme cases below */
266 if (m_lev > p_lev + 4) return FALSE;
267 if (m_lev + 4 <= p_lev) return TRUE;
269 /* Examine player health */
270 p_chp = target_ptr->chp;
271 p_mhp = target_ptr->mhp;
273 /* Examine monster health */
275 m_mhp = m_ptr->maxhp;
277 /* Prepare to optimize the calculation */
278 p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
279 m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
281 /* Strong players scare strong monsters */
282 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
289 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
290 * Search spell castable grid
291 * @param target_ptr プレーヤーへの参照ポインタ
292 * @param m_idx モンスターの参照ID
293 * @param yp 適したマスのY座標を返す参照ポインタ
294 * @param xp 適したマスのX座標を返す参照ポインタ
295 * @return 有効なマスがあった場合TRUEを返す
297 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
299 floor_type *floor_ptr = target_ptr->current_floor_ptr;
300 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
301 monster_race *r_ptr = &r_info[m_ptr->r_idx];
303 /* Monster location */
304 POSITION y1 = m_ptr->fy;
305 POSITION x1 = m_ptr->fx;
307 /* Monster can already cast spell to player */
308 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
310 /* Set current grid cost */
311 int now_cost = floor_ptr->grid_array[y1][x1].cost;
312 if (now_cost == 0) now_cost = 999;
314 /* Can monster bash or open doors? */
315 bool can_open_door = FALSE;
316 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
318 can_open_door = TRUE;
321 /* Check nearby grids, diagonals first */
323 for (int i = 7; i >= 0; i--)
327 POSITION y = y1 + ddy_ddd[i];
328 POSITION x = x1 + ddx_ddd[i];
330 /* Ignore locations off of edge */
331 if (!in_bounds2(floor_ptr, y, x)) continue;
333 /* Simply move to player */
334 if (player_bold(target_ptr, y, x)) return FALSE;
337 g_ptr = &floor_ptr->grid_array[y][x];
341 /* Monster cannot kill or pass walls */
342 if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding))))
344 if (cost == 0) continue;
345 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
348 /* Hack -- for kill or pass wall monster.. */
349 if (cost == 0) cost = 998;
351 if (now_cost < cost) continue;
353 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
355 /* Accept louder sounds */
356 if (best < cost) continue;
359 (*yp) = y1 + ddy_ddd[i];
360 (*xp) = x1 + ddx_ddd[i];
363 if (best == 999) return FALSE;
370 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
371 * Choose the "best" direction for "flowing"
372 * @param m_idx モンスターの参照ID
373 * @param yp 移動先のマスのY座標を返す参照ポインタ
374 * @param xp 移動先のマスのX座標を返す参照ポインタ
375 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
376 * @return 有効なマスがあった場合TRUEを返す
378 * Note that ghosts and rock-eaters are never allowed to "flow",\n
379 * since they should move directly towards the player.\n
381 * Prefer "non-diagonal" directions, but twiddle them a little\n
382 * to angle slightly towards the player's actual location.\n
384 * Allow very perceptive monsters to track old "spoor" left by\n
385 * previous locations occupied by the player. This will tend\n
386 * to have monsters end up either near the player or on a grid\n
387 * recently occupied by the player (and left via "teleport").\n
389 * Note that if "smell" is turned on, all monsters get vicious.\n
391 * Also note that teleporting away from a location will cause\n
392 * the monsters who were chasing you to converge on that location\n
393 * as long as you are still near enough to "annoy" them without\n
394 * being close enough to chase directly. I have no idea what will\n
395 * happen if you combine "smell" with low "aaf" values.\n
397 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
400 floor_type *floor_ptr = target_ptr->current_floor_ptr;
401 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
402 monster_race *r_ptr = &r_info[m_ptr->r_idx];
404 /* Can monster cast attack spell? */
405 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
406 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
407 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
409 /* Can move spell castable grid? */
410 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
413 /* Monster can't flow */
414 if (no_flow) return FALSE;
416 /* Monster can go through rocks */
417 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
418 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
420 /* Monster location */
421 POSITION y1 = m_ptr->fy;
422 POSITION x1 = m_ptr->fx;
424 /* Hack -- Player can see us, run towards him */
425 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
428 g_ptr = &floor_ptr->grid_array[y1][x1];
430 /* If we can hear noises, advance towards them */
432 bool use_scent = FALSE;
438 /* Otherwise, try to follow a scent trail */
439 else if (g_ptr->when)
442 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
448 /* Otherwise, advance blindly */
454 /* Check nearby grids, diagonals first */
455 for (int i = 7; i >= 0; i--)
457 POSITION y = y1 + ddy_ddd[i];
458 POSITION x = x1 + ddx_ddd[i];
460 /* Ignore locations off of edge */
461 if (!in_bounds2(floor_ptr, y, x)) continue;
463 g_ptr = &floor_ptr->grid_array[y][x];
465 /* We're following a scent trail */
468 int when = g_ptr->when;
470 /* Accept younger scent */
471 if (best > when) continue;
475 /* We're using sound */
480 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
482 else cost = g_ptr->cost;
484 /* Accept louder sounds */
485 if ((cost == 0) || (best < cost)) continue;
489 /* Hack -- Save the "twiddled" location */
490 (*yp) = target_ptr->y + 16 * ddy_ddd[i];
491 (*xp) = target_ptr->x + 16 * ddx_ddd[i];
494 if (best == 999 || best == 0) return FALSE;
501 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
502 * Provide a location to flee to, but give the player a wide berth.
503 * @param m_idx モンスターの参照ID
504 * @param yp 移動先のマスのY座標を返す参照ポインタ
505 * @param xp 移動先のマスのX座標を返す参照ポインタ
506 * @return 有効なマスがあった場合TRUEを返す
508 * A monster may wish to flee to a location that is behind the player,\n
509 * but instead of heading directly for it, the monster should "swerve"\n
510 * around the player so that he has a smaller chance of getting hit.\n
512 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
514 POSITION gy = 0, gx = 0;
516 /* Monster location */
517 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
518 POSITION fy = m_ptr->fy;
519 POSITION fx = m_ptr->fx;
521 /* Desired destination */
522 POSITION y1 = fy - (*yp);
523 POSITION x1 = fx - (*xp);
525 /* Check nearby grids, diagonals first */
527 for (int i = 7; i >= 0; i--)
530 POSITION y = fy + ddy_ddd[i];
531 POSITION x = fx + ddx_ddd[i];
533 /* Ignore locations off of edge */
534 if (!in_bounds2(floor_ptr, y, x)) continue;
536 /* Calculate distance of this grid from our destination */
537 dis = distance(y, x, y1, x1);
539 /* Score this grid */
540 s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
542 /* No negative scores */
545 /* Ignore lower scores */
546 if (s < score) continue;
548 /* Save the score and time */
551 /* Save the location */
556 /* No legal move (?) */
557 if (score == -1) return FALSE;
568 * Hack -- Precompute a bunch of calls to distance() in find_safety() and
571 * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
572 * offsets of all the locations with a distance of n from a central point,
573 * with an offset of (0,0) indicating no more offsets at this distance.
575 * This is, of course, fairly unreadable, but it eliminates multiple loops
576 * from the previous version.
578 * It is probably better to replace these arrays with code to compute
579 * the relevant arrays, even if the storage is pre-allocated in hard
580 * coded sizes. At the very least, code should be included which is
581 * able to generate and dump these arrays (ala "los()").
583 * Also, the storage needs could be halved by using bytes.
585 * These arrays could be combined into two big arrays, using sub-arrays
586 * to hold the offsets and lengths of each portion of the sub-arrays, and
587 * this could perhaps also be used somehow in the "look" code.
591 static POSITION d_off_y_0[] = { 0 };
592 static POSITION d_off_x_0[] = { 0 };
594 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
595 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
597 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
598 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
600 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
601 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
603 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 };
604 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 };
607 static POSITION d_off_y_5[] =
608 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
609 -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
612 static POSITION d_off_x_5[] =
613 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
614 -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
618 static POSITION d_off_y_6[] =
619 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
620 -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
623 static POSITION d_off_x_6[] =
624 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
625 0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
626 -3, 2, 3, -1, 0, 1, 0 };
629 static POSITION d_off_y_7[] =
630 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
631 -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
632 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
634 static POSITION d_off_x_7[] =
635 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
636 -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
637 6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
641 static POSITION d_off_y_8[] =
642 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
643 -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
644 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
647 static POSITION d_off_x_8[] =
648 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
649 5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
650 7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
654 static POSITION d_off_y_9[] =
655 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
656 -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
657 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
658 7, 8, 8, 8, 8, 9, 9, 9, 0 };
660 static POSITION d_off_x_9[] =
661 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
662 -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
663 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
664 4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
667 static POSITION *dist_offsets_y[10] =
669 d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
670 d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
673 static POSITION *dist_offsets_x[10] =
675 d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
676 d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
680 * @brief モンスターが逃げ込める安全な地点を返す /
681 * Choose a "safe" location near a monster for it to run toward.
682 * @param target_ptr プレーヤーへの参照ポインタ
683 * @param m_idx モンスターの参照ID
684 * @param yp 移動先のマスのY座標を返す参照ポインタ
685 * @param xp 移動先のマスのX座標を返す参照ポインタ
686 * @return 有効なマスがあった場合TRUEを返す
688 * A location is "safe" if it can be reached quickly and the player\n
689 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
690 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
691 * try to run towards corridor openings if they are in a room.\n
693 * This function may take lots of CPU time if lots of monsters are\n
696 * Return TRUE if a safe location is available.\n
698 static bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
700 floor_type *floor_ptr = target_ptr->current_floor_ptr;
701 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
703 POSITION fy = m_ptr->fy;
704 POSITION fx = m_ptr->fx;
706 POSITION gy = 0, gx = 0, gdis = 0;
708 /* Start with adjacent locations, spread further */
709 for (POSITION d = 1; d < 10; d++)
711 /* Get the lists of points with a distance d from (fx, fy) */
713 y_offsets = dist_offsets_y[d];
716 x_offsets = dist_offsets_x[d];
718 /* Check the locations */
719 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
721 i++, dx = x_offsets[i], dy = y_offsets[i])
723 POSITION y = fy + dy;
724 POSITION x = fx + dx;
726 /* Skip illegal locations */
727 if (!in_bounds(floor_ptr, y, x)) continue;
730 g_ptr = &floor_ptr->grid_array[y][x];
732 /* Skip locations in a wall */
733 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == target_ptr->riding) ? CEM_RIDING : 0)) continue;
735 /* Check for "availability" (if monsters can flow) */
736 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
738 /* Ignore grids very far from the player */
739 if (g_ptr->dist == 0) continue;
741 /* Ignore too-distant grids */
742 if (g_ptr->dist > floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
745 /* Check for absence of shot (more or less) */
746 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
748 /* Calculate distance from player */
749 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
751 /* Remember if further than previous */
752 if (dis <= gdis) continue;
759 /* Check for success */
760 if (gdis <= 0) continue;
774 * @brief モンスターが隠れ潜める地点を返す /
775 * Choose a good hiding place near a monster for it to run toward.
776 * @param target_ptr プレーヤーへの参照ポインタ
777 * @param m_idx モンスターの参照ID
778 * @param yp 移動先のマスのY座標を返す参照ポインタ
779 * @param xp 移動先のマスのX座標を返す参照ポインタ
780 * @return 有効なマスがあった場合TRUEを返す
782 * Pack monsters will use this to "ambush" the player and lure him out\n
783 * of corridors into open space so they can swarm him.\n
785 * Return TRUE if a good location is available.\n
787 static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
789 floor_type *floor_ptr = target_ptr->current_floor_ptr;
790 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
791 monster_race *r_ptr = &r_info[m_ptr->r_idx];
793 POSITION fy = m_ptr->fy;
794 POSITION fx = m_ptr->fx;
796 POSITION gy = 0, gx = 0, gdis = 999;
798 /* Start with adjacent locations, spread further */
799 for (POSITION d = 1; d < 10; d++)
801 /* Get the lists of points with a distance d from (fx, fy) */
803 y_offsets = dist_offsets_y[d];
806 x_offsets = dist_offsets_x[d];
808 /* Check the locations */
809 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
811 i++, dx = x_offsets[i], dy = y_offsets[i])
813 POSITION y = fy + dy;
814 POSITION x = fx + dx;
816 /* Skip illegal locations */
817 if (!in_bounds(floor_ptr, y, x)) continue;
819 /* Skip occupied locations */
820 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
822 /* Check for hidden, available grid */
823 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
826 /* Calculate distance from player */
827 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
829 /* Remember if closer than previous */
830 if (dis < gdis && dis >= 2)
838 if (gdis >= 999) continue;
851 * @brief モンスターの移動方向を返す /
852 * Choose "logical" directions for monster movement
853 * @param target_ptr プレーヤーへの参照ポインタ
854 * @param m_idx モンスターの参照ID
855 * @param mm 移動方向を返す方向IDの参照ポインタ
856 * @return 有効方向があった場合TRUEを返す
858 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
860 floor_type *floor_ptr = target_ptr->current_floor_ptr;
861 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
862 monster_race *r_ptr = &r_info[m_ptr->r_idx];
863 POSITION y = 0, ay, x = 0, ax;
865 POSITION y2 = target_ptr->y;
866 POSITION x2 = target_ptr->x;
868 bool will_run = mon_will_run(target_ptr, m_idx);
870 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
871 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
873 /* Counter attack to an enemy monster */
874 if (!will_run && m_ptr->target_y)
876 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
878 /* The monster must be an enemy, and in LOS */
880 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
881 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
882 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
884 /* Extract the "pseudo-direction" */
885 y = m_ptr->fy - m_ptr->target_y;
886 x = m_ptr->fx - m_ptr->target_x;
891 if (!done && !will_run && is_hostile(m_ptr) &&
892 (r_ptr->flags1 & RF1_FRIENDS) &&
893 ((los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x)) ||
894 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
897 * Animal packs try to get the player out of corridors
898 * (...unless they can move through walls -- TY)
900 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
901 !(r_ptr->flags2 & RF2_KILL_WALL))
905 /* Count room grids next to player */
906 for (i = 0; i < 8; i++)
908 int xx = target_ptr->x + ddx_ddd[i];
909 int yy = target_ptr->y + ddy_ddd[i];
911 if (!in_bounds2(floor_ptr, yy, xx)) continue;
913 g_ptr = &floor_ptr->grid_array[yy][xx];
916 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
918 /* One more room grid */
922 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
923 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
925 /* Not in a room and strong player */
926 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
927 (target_ptr->mhp + target_ptr->msp))
929 /* Find hiding place */
930 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
934 /* Monster groups try to surround the player */
935 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
939 /* Find an empty square near the player to fill */
940 for (i = 0; i < 8; i++)
942 /* Pick squares near player (semi-randomly) */
943 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
944 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
947 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
949 /* Attack the player */
956 if (!in_bounds2(floor_ptr, y2, x2)) continue;
958 /* Ignore filled grids */
959 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
961 /* Try to fill this hole */
965 /* Extract the new "pseudo-direction" */
975 /* Flow towards the player */
976 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
978 /* Extract the "pseudo-direction" */
985 /* Apply fear if possible and necessary */
986 if (is_pet(m_ptr) && will_run)
988 /* XXX XXX Not very "smart" */
993 if (!done && will_run)
998 /* Try to find safe place */
999 if (find_safety(target_ptr, m_idx, &y, &x))
1001 /* Attempt to avoid the player */
1004 /* Adjust movement */
1005 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x)) done = TRUE;
1011 /* This is not a very "smart" method XXX XXX */
1019 /* Check for no move */
1020 if (!x && !y) return FALSE;
1023 /* Extract the "absolute distances" */
1027 /* Do something weird */
1028 if (y < 0) move_val += 8;
1029 if (x > 0) move_val += 4;
1031 /* Prevent the diamond maneuvre */
1032 if (ay > (ax << 1)) move_val += 2;
1033 else if (ax > (ay << 1)) move_val++;
1035 /* Extract some directions */
1180 /* Wants to move... */
1185 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1187 return !have_flag(f_ptr->flags, FF_GLASS) ||
1188 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1189 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1194 * @brief モンスター単体の1ターン行動処理メインルーチン /
1196 * @param target_ptr プレーヤーへの参照ポインタ
1197 * @param m_idx 行動モンスターの参照ID
1200 * The monster is known to be within 100 grids of the player\n
1202 * In several cases, we directly update the monster lore\n
1204 * Note that a monster is only allowed to "reproduce" if there\n
1205 * are a limited number of "reproducing" monsters on the current\n
1206 * level. This should prevent the level from being "swamped" by\n
1207 * reproducing monsters. It also allows a large mass of mice to\n
1208 * prevent a louse from multiplying, but this is a small price to\n
1209 * pay for a simple multiplication method.\n
1211 * XXX Monster fear is slightly odd, in particular, monsters will\n
1212 * fixate on opening a door even if they cannot open it. Actually,\n
1213 * the same thing happens to normal monsters when they hit a door\n
1215 * In addition, monsters which *cannot* open or bash\n
1216 * down a door will still stand there trying to open it...\n
1218 * XXX Technically, need to check for monster in the way\n
1219 * combined with that monster being in a wall (or door?)\n
1221 * A "direction" of "5" means "pick a random direction".\n
1223 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1225 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1226 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1227 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1233 feature_type *f_ptr;
1234 monster_type *y_ptr;
1239 bool must_alter_to_move;
1252 bool is_riding_mon = (m_idx == target_ptr->riding);
1253 bool see_m = is_seen(m_ptr);
1255 if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1257 if (rakuba(target_ptr, 0, TRUE))
1260 msg_print("地面に落とされた。");
1262 GAME_TEXT m_name[MAX_NLEN];
1263 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1264 msg_format("You have fallen from %s.", m_name);
1269 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1271 choose_new_monster(target_ptr, m_idx, FALSE, 0);
1272 r_ptr = &r_info[m_ptr->r_idx];
1275 /* Players hidden in shadow are almost imperceptable. -LM- */
1276 if (target_ptr->special_defense & NINJA_S_STEALTH)
1278 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1279 if (target_ptr->monlite) tmp /= 3;
1280 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1281 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1282 /* Low-level monsters will find it difficult to locate the player. */
1283 if (randint0(tmp) > (r_ptr->level + 20)) aware = FALSE;
1286 if (vanish_summoned_children(target_ptr, m_idx, see_m)) return;
1287 if (process_quantum_effect(target_ptr,m_idx, see_m)) return;
1289 if (m_ptr->r_idx == MON_SHURYUUDAN)
1291 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1295 if (runaway_monster(target_ptr, m_idx, is_riding_mon, see_m)) return;
1297 awake_monster(target_ptr, m_idx);
1300 if (MON_STUNNED(m_ptr))
1302 /* Sometimes skip move */
1303 if (one_in_(2)) return;
1308 target_ptr->update |= (PU_BONUS);
1311 /* No one wants to be your friend if you're aggravating */
1312 bool gets_angry = FALSE;
1313 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
1316 /* Paranoia... no pet uniques outside wizard mode -- TY */
1317 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
1318 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
1323 if (target_ptr->phase_out) gets_angry = FALSE;
1327 if (is_pet(m_ptr) || see_m)
1329 GAME_TEXT m_name[MAX_NLEN];
1330 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
1331 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
1334 set_hostile(target_ptr, m_ptr);
1337 /* Get the origin */
1338 POSITION oy = m_ptr->fy;
1339 POSITION ox = m_ptr->fx;
1341 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1343 process_special(target_ptr, m_idx);
1344 process_speak_sound(target_ptr, m_idx, oy, ox, aware);
1345 cast_spell(target_ptr, m_idx, aware);
1347 /* Hack -- Assume no movement */
1348 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1349 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1351 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, aware)) return;
1353 /* Assume nothing */
1357 must_alter_to_move = FALSE;
1359 /* Assume nothing */
1360 did_open_door = FALSE;
1361 did_bash_door = FALSE;
1362 did_take_item = FALSE;
1363 did_kill_item = FALSE;
1364 did_move_body = FALSE;
1365 did_pass_wall = FALSE;
1366 did_kill_wall = FALSE;
1368 /* Take a zero-terminated array of "directions" */
1369 for (i = 0; mm[i]; i++)
1371 /* Get the direction */
1374 /* Hack -- allow "randomized" motion */
1375 if (d == 5) d = ddd[randint0(8)];
1377 /* Get the destination */
1378 POSITION ny = oy + ddy[d];
1379 POSITION nx = ox + ddx[d];
1381 /* Ignore locations off of edge */
1382 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1384 /* Access that grid */
1385 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1386 f_ptr = &f_info[g_ptr->feat];
1387 can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1389 /* Access that grid's contents */
1390 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1392 /* Hack -- player 'in' wall */
1393 if (player_bold(target_ptr, ny, nx))
1398 /* Possibly a monster to attack */
1399 else if (g_ptr->m_idx)
1404 /* Monster destroys walls (and doors) */
1405 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1406 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1407 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1408 check_hp_for_feat_destruction(f_ptr, m_ptr))
1410 /* Eat through walls/doors/rubble */
1412 if (!can_cross) must_alter_to_move = TRUE;
1414 /* Monster destroyed a wall (later) */
1415 did_kill_wall = TRUE;
1418 /* Floor is open? */
1421 /* Go ahead and move */
1424 /* Monster moves through walls (and doors) */
1425 if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || target_ptr->pass_wall) &&
1426 have_flag(f_ptr->flags, FF_CAN_PASS))
1428 /* Monster went through a wall */
1429 did_pass_wall = TRUE;
1433 /* Handle doors and secret doors */
1434 else if (is_closed_door(target_ptr, g_ptr->feat))
1436 bool may_bash = TRUE;
1438 /* Assume no move allowed */
1441 /* Creature can open doors. */
1442 if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1443 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1448 /* The door is open */
1449 did_open_door = TRUE;
1451 /* Do not bash the door */
1457 /* Locked doors (not jammed) */
1460 /* Try to unlock it */
1461 if (randint0(m_ptr->hp / 10) > f_ptr->power)
1463 /* Unlock the door */
1464 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1466 /* Do not bash the door */
1474 /* Stuck doors -- attempt to bash them down if allowed */
1475 if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1476 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1478 /* Attempt to Bash */
1479 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1481 if (have_flag(f_ptr->flags, FF_GLASS))
1482 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1484 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1486 /* Disturb (sometimes) */
1487 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1489 /* The door was bashed open */
1490 did_bash_door = TRUE;
1492 /* Hack -- fall into doorway */
1494 must_alter_to_move = TRUE;
1499 /* Deal with doors in the way */
1500 if (did_open_door || did_bash_door)
1502 /* Break down the door */
1503 if (did_bash_door && ((randint0(100) < 50) || (feat_state(target_ptr, g_ptr->feat, FF_OPEN) == g_ptr->feat) || have_flag(f_ptr->flags, FF_GLASS)))
1505 cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1507 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1509 target_ptr->update |= (PU_FLOW);
1510 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1511 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1520 cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1523 f_ptr = &f_info[g_ptr->feat];
1525 /* Handle viewable doors */
1530 /* Hack -- check for Glyph of Warding */
1531 if (do_move && is_glyph_grid(g_ptr) &&
1532 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1534 /* Assume no move allowed */
1537 /* Break the ward */
1538 if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1540 /* Describe observable breakage */
1541 if (g_ptr->info & CAVE_MARK)
1543 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1546 /* Forget the rune */
1547 g_ptr->info &= ~(CAVE_MARK);
1549 /* Break the rune */
1550 g_ptr->info &= ~(CAVE_OBJECT);
1553 /* Allow movement */
1556 note_spot(target_ptr, ny, nx);
1559 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1560 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1562 /* Assume no move allowed */
1565 /* Break the ward */
1568 /* Break the ward */
1569 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1571 /* Describe observable breakage */
1572 if (g_ptr->info & CAVE_MARK)
1574 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1575 project(target_ptr, 0, 2, ny, nx, 2 * (target_ptr->lev + damroll(7, 7)), GF_MANA, (PROJECT_GRID | PROJECT_ITEM | PROJECT_KILL | PROJECT_JUMP | PROJECT_NO_HANGEKI), -1);
1580 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1583 /* Forget the rune */
1584 g_ptr->info &= ~(CAVE_MARK);
1586 /* Break the rune */
1587 g_ptr->info &= ~(CAVE_OBJECT);
1590 note_spot(target_ptr, ny, nx);
1591 lite_spot(target_ptr, ny, nx);
1593 if (!monster_is_valid(m_ptr)) return;
1594 /* Allow movement */
1599 /* The player is in the way */
1600 if (do_move && player_bold(target_ptr, ny, nx))
1602 /* Some monsters never attack */
1603 if (r_ptr->flags1 & RF1_NEVER_BLOW)
1605 /* Hack -- memorize lack of attacks */
1606 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
1612 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1613 if (do_move && (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
1615 if (!MON_CONFUSED(m_ptr))
1617 if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
1620 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1625 /* The player is in the way. Attack him. */
1628 if (!target_ptr->riding || one_in_(2))
1631 (void)make_attack_normal(target_ptr, m_idx);
1642 /* A monster is in the way */
1643 if (do_move && g_ptr->m_idx)
1645 monster_race *z_ptr = &r_info[y_ptr->r_idx];
1647 /* Assume no movement */
1650 /* Attack 'enemies' */
1651 if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
1652 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
1653 can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
1654 are_enemies(target_ptr, m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
1656 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
1658 if (r_ptr->flags2 & RF2_KILL_BODY)
1660 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
1664 if (y_ptr->r_idx && (y_ptr->hp >= 0))
1666 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return;
1668 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1669 else if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
1671 if (MON_CONFUSED(m_ptr)) return;
1672 else if (r_ptr->flags2 & RF2_STUPID)
1674 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1682 /* Push past weaker monsters (unless leaving a wall) */
1683 else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
1684 (r_ptr->mexp > z_ptr->mexp) &&
1685 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
1686 monster_can_cross_terrain(target_ptr, target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
1688 /* Allow movement */
1691 /* Monster pushed past another monster */
1692 did_move_body = TRUE;
1694 /* Wake up the moved monster */
1695 (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
1703 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) do_move = FALSE;
1706 if (did_kill_wall && do_move)
1708 if (one_in_(GRINDNOISE))
1710 if (have_flag(f_ptr->flags, FF_GLASS))
1711 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
1713 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
1716 cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
1718 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1720 target_ptr->update |= (PU_FLOW);
1721 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1722 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1727 f_ptr = &f_info[g_ptr->feat];
1729 /* Note changes to viewable region */
1734 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1736 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
1738 /* Assume no move allowed */
1744 * Check if monster can cross terrain
1745 * This is checked after the normal attacks
1746 * to allow monsters to attack an enemy,
1747 * even if it can't enter the terrain.
1749 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
1751 /* Assume no move allowed */
1755 /* Some monsters never move */
1756 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1758 /* Hack -- memorize lack of moves */
1759 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1765 /* Creature has been allowed move */
1774 if (have_flag(f_ptr->flags, FF_TREE))
1776 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1778 m_ptr->energy_need += ENERGY_NEED();
1784 /* Hack -- Update the old location */
1785 target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
1787 /* Mega-Hack -- move the old monster, if any */
1790 /* Move the old monster */
1794 /* Update the old monster */
1795 update_monster(target_ptr, g_ptr->m_idx, TRUE);
1798 /* Hack -- Update the new location */
1799 g_ptr->m_idx = m_idx;
1801 /* Move the monster */
1804 update_monster(target_ptr, m_idx, TRUE);
1806 lite_spot(target_ptr, oy, ox);
1807 lite_spot(target_ptr, ny, nx);
1811 /* sound(SOUND_WALK); */
1812 if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
1815 /* Possible disturb */
1818 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1819 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1821 if (is_hostile(m_ptr))
1822 disturb(target_ptr, FALSE, TRUE);
1825 /* Take or Kill objects on the floor */
1826 bool is_takable_or_killable = g_ptr->o_idx > 0;
1827 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1828 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1829 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1830 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1831 if (!is_takable_or_killable)
1837 OBJECT_IDX this_o_idx, next_o_idx;
1838 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
1840 /* Scan all objects in the grid */
1841 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1843 BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
1844 GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
1845 object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
1846 next_o_idx = o_ptr->next_o_idx;
1851 if (o_ptr->tval == TV_GOLD) continue;
1854 * Skip "real" corpses and statues, to avoid extreme
1855 * silliness like a novice rogue pockets full of statues
1858 if ((o_ptr->tval == TV_CORPSE) ||
1859 (o_ptr->tval == TV_STATUE)) continue;
1862 /* Extract some flags */
1863 object_flags(o_ptr, flgs);
1865 /* Acquire the object name */
1866 object_desc(target_ptr, o_name, o_ptr, 0);
1867 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_HIDDEN);
1869 /* React to objects that hurt the monster */
1870 if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
1871 if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
1872 if (have_flag(flgs, TR_SLAY_TROLL)) flg3 |= (RF3_TROLL);
1873 if (have_flag(flgs, TR_KILL_TROLL)) flg3 |= (RF3_TROLL);
1874 if (have_flag(flgs, TR_SLAY_GIANT)) flg3 |= (RF3_GIANT);
1875 if (have_flag(flgs, TR_KILL_GIANT)) flg3 |= (RF3_GIANT);
1876 if (have_flag(flgs, TR_SLAY_ORC)) flg3 |= (RF3_ORC);
1877 if (have_flag(flgs, TR_KILL_ORC)) flg3 |= (RF3_ORC);
1878 if (have_flag(flgs, TR_SLAY_DEMON)) flg3 |= (RF3_DEMON);
1879 if (have_flag(flgs, TR_KILL_DEMON)) flg3 |= (RF3_DEMON);
1880 if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
1881 if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
1882 if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
1883 if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
1884 if (have_flag(flgs, TR_SLAY_EVIL)) flg3 |= (RF3_EVIL);
1885 if (have_flag(flgs, TR_KILL_EVIL)) flg3 |= (RF3_EVIL);
1886 if (have_flag(flgs, TR_SLAY_HUMAN)) flg2 |= (RF2_HUMAN);
1887 if (have_flag(flgs, TR_KILL_HUMAN)) flg2 |= (RF2_HUMAN);
1888 if (have_flag(flgs, TR_BRAND_ACID)) flgr |= (RFR_IM_ACID);
1889 if (have_flag(flgs, TR_BRAND_ELEC)) flgr |= (RFR_IM_ELEC);
1890 if (have_flag(flgs, TR_BRAND_FIRE)) flgr |= (RFR_IM_FIRE);
1891 if (have_flag(flgs, TR_BRAND_COLD)) flgr |= (RFR_IM_COLD);
1892 if (have_flag(flgs, TR_BRAND_POIS)) flgr |= (RFR_IM_POIS);
1894 /* The object cannot be picked up by the monster */
1895 if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
1896 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
1898 /* Only give a message for "take_item" */
1899 if (do_take && (r_ptr->flags2 & RF2_STUPID))
1901 did_take_item = TRUE;
1903 /* Describe observable situations */
1904 if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
1906 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
1911 /* Pick up the item */
1914 did_take_item = TRUE;
1916 /* Describe observable situations */
1917 if (player_can_see_bold(target_ptr, ny, nx))
1919 msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
1922 /* Excise the object */
1923 excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
1926 o_ptr->marked &= OM_TOUCHED;
1928 /* Forget location */
1929 o_ptr->iy = o_ptr->ix = 0;
1931 /* Memorize monster */
1932 o_ptr->held_m_idx = m_idx;
1935 o_ptr->next_o_idx = m_ptr->hold_o_idx;
1938 m_ptr->hold_o_idx = this_o_idx;
1941 /* Destroy the item if not a pet */
1942 else if (!is_pet(m_ptr))
1944 did_kill_item = TRUE;
1946 /* Describe observable situations */
1947 if (player_has_los_bold(target_ptr, ny, nx))
1949 msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
1952 delete_object_idx(target_ptr, this_o_idx);
1960 * Forward movements failed, but now received LOS attack!
1961 * Try to flow by smell.
1963 if (target_ptr->no_flowed && i > 2 && m_ptr->target_y)
1964 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1966 /* If we haven't done anything, try casting a spell again */
1967 if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
1969 /* Try to cast spell again */
1970 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1972 if (make_attack_spell(m_idx, target_ptr)) return;
1976 /* Notice changes in view */
1979 target_ptr->update |= (PU_FLOW);
1980 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1983 /* Notice changes in view */
1984 if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
1985 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
1987 target_ptr->update |= (PU_MON_LITE);
1990 /* Learn things from observable monster */
1991 if (is_original_ap_and_seen(target_ptr, m_ptr))
1993 /* Monster opened a door */
1994 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
1996 /* Monster bashed a door */
1997 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1999 /* Monster tried to pick something up */
2000 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
2002 /* Monster tried to crush something */
2003 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
2005 /* Monster pushed past another monster */
2006 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
2008 /* Monster passed through a wall */
2009 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
2011 /* Monster destroyed a wall */
2012 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
2015 bool is_battle_determined = !do_turn && !do_move && MON_MONFEAR(m_ptr) && aware;
2016 if (!is_battle_determined) return;
2018 /* No longer afraid */
2019 (void)set_monster_monfear(target_ptr, m_idx, 0);
2021 /* Message if seen */
2024 GAME_TEXT m_name[MAX_NLEN];
2025 monster_desc(target_ptr, m_name, m_ptr, 0);
2026 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2029 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
2034 * @brief 召喚の親元が消滅した時、子供も消滅させる
2035 * @param target_ptr プレーヤーへの参照ポインタ
2036 * @param m_idx モンスターID
2037 * @param see_m モンスターが視界内にいたらTRUE
2038 * @return 召喚モンスターが消滅したらTRUE
2040 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2042 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2043 if ((m_ptr->parent_m_idx == 0) || target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2048 GAME_TEXT m_name[MAX_NLEN];
2049 monster_desc(target_ptr, m_name, m_ptr, 0);
2050 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2053 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2055 GAME_TEXT m_name[MAX_NLEN];
2056 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
2057 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2060 delete_monster_idx(target_ptr, m_idx);
2066 * @brief 寝ているモンスターの起床を判定する
2067 * @param target_ptr プレーヤーへの参照ポインタ
2068 * @param m_idx モンスターID
2071 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
2073 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2074 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2075 if (!MON_CSLEEP(m_ptr)) return;
2076 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
2078 (void)set_monster_csleep(target_ptr, m_idx, 0);
2081 GAME_TEXT m_name[MAX_NLEN];
2082 monster_desc(target_ptr, m_name, m_ptr, 0);
2083 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2086 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2094 * @brief 量子生物の量子的効果を実行する
2095 * @param target_ptr プレーヤーへの参照ポインタ
2096 * @param m_idx モンスターID
2097 * @param see_m モンスターが視界内にいたらTRUE
2098 * @return モンスターが量子的効果により消滅したらTRUE
2100 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2102 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2103 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2104 if ((r_ptr->flags2 & RF2_QUANTUM) == 0) return FALSE;
2105 if (!randint0(2)) return FALSE;
2106 if (randint0((m_idx % 100) + 10)) return FALSE;
2108 bool can_disappear = (r_ptr->flags1 & RF1_UNIQUE) == 0;
2109 can_disappear &= (r_ptr->flags1 & RF1_QUESTOR) == 0;
2112 vanish_nonunique(target_ptr, m_idx, see_m);
2116 produce_quantum_effect(target_ptr, m_idx, see_m);
2122 * @brief ユニークでない量子生物を消滅させる
2123 * @param target_ptr プレーヤーへの参照ポインタ
2124 * @param m_idx モンスターID
2125 * @param see_m モンスターが視界内にいたらTRUE
2128 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2130 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2133 GAME_TEXT m_name[MAX_NLEN];
2134 monster_desc(target_ptr, m_name, m_ptr, 0);
2135 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2138 monster_death(target_ptr, m_idx, FALSE);
2139 delete_monster_idx(target_ptr, m_idx);
2140 if (is_pet(m_ptr) && !(m_ptr->ml))
2142 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2148 * @brief 量子生物ユニークの量子的効果 (ショート・テレポートまたは距離10のテレポート・アウェイ)を実行する
2149 * @param target_ptr プレーヤーへの参照ポインタ
2150 * @param m_idx モンスターID
2151 * @param see_m モンスターが視界内にいたらTRUE
2154 * プレーヤーが量子生物を観測しているか、量子生物がプレーヤーを観測している場合、互いの相対的な位置を確定させる
2155 * 波動関数の収縮はテレポートではないので反テレポート無効
2156 * todo パターンは収縮どころか拡散しているが、この際気にしてはいけない
2158 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2160 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2161 bool coherent = los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x);
2162 if (!see_m && !coherent) return;
2166 GAME_TEXT m_name[MAX_NLEN];
2167 monster_desc(target_ptr, m_name, m_ptr, 0);
2168 msg_format(_("%sは量子的効果を起こした!", "%^s produced a decoherence!"), m_name);
2172 msg_print(_("量子的効果が起こった!", "A decoherence was produced!"));
2175 bool target = one_in_(2);
2176 const int blink = 32 * 5 + 4;
2179 (void)monspell_to_monster(target_ptr, blink, m_ptr->fy, m_ptr->fx, m_idx, m_idx);
2184 teleport_player_away(m_idx, target_ptr, 10, TRUE);
2189 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
2191 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2192 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2193 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
2194 if (m_ptr->r_idx != MON_OHMU) return;
2195 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
2196 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
2199 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2200 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2202 for (int k = 0; k < A_MAX; k++)
2204 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
2206 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2210 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2215 * @brief モンスターを喋らせたり足音を立てたりする
2216 * @param target_ptr プレーヤーへの参照ポインタ
2220 * @param aware 起きていればTRUE
2223 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
2225 if (target_ptr->phase_out) return;
2227 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2228 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2229 if (m_ptr->ap_r_idx == MON_CYBER &&
2230 one_in_(CYBERNOISE) &&
2231 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2233 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
2234 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2237 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
2238 !one_in_(SPEAK_CHANCE) ||
2239 !player_has_los_bold(target_ptr, oy, ox) ||
2240 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
2243 GAME_TEXT m_name[MAX_NLEN];
2244 char monmessage[1024];
2248 monster_desc(target_ptr, m_name, m_ptr, 0);
2250 strcpy(m_name, _("それ", "It"));
2252 if (MON_MONFEAR(m_ptr))
2253 filename = _("monfear_j.txt", "monfear.txt");
2254 else if (is_pet(m_ptr))
2255 filename = _("monpet_j.txt", "monpet.txt");
2256 else if (is_friendly(m_ptr))
2257 filename = _("monfrien_j.txt", "monfrien.txt");
2259 filename = _("monspeak_j.txt", "monspeak.txt");
2261 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2263 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2269 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
2270 * @param target_ptr プレーヤーへの参照ポインタ
2271 * @param m_idx モンスターID
2272 * @param oy 分裂元モンスターのY座標
2273 * @param ox 分裂元モンスターのX座標
2274 * @return 実際に分裂したらTRUEを返す
2276 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
2278 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2279 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2280 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) && (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
2284 for (POSITION y = oy - 1; y <= oy + 1; y++)
2286 for (POSITION x = ox - 1; x <= ox + 1; x++)
2288 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
2289 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
2293 if (multiply_barrier(target_ptr, m_idx)) k = 8;
2295 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2297 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2299 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
2301 r_ptr->r_flags2 |= (RF2_MULTIPLY);
2313 * @brief モンスターの移動パターンを決定する
2314 * @param target_ptr プレーヤーへの参照ポインタ
2316 * @param m_idx モンスターID
2317 * @param aware 起きていればTRUE
2318 * @return 移動先が存在すればTRUE
2320 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
2322 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2323 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2325 if (MON_CONFUSED(m_ptr) || !aware)
2327 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2331 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
2333 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2335 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2339 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
2341 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2343 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2347 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
2349 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2351 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2355 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2357 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2363 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
2364 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
2365 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2366 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2367 if (!get_enemy_dir(target_ptr, m_idx, mm))
2369 if (avoid || lonely || distant)
2371 POSITION dis = target_ptr->pet_follow_distance;
2372 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
2374 target_ptr->pet_follow_distance = PET_SEEK_DIST;
2377 (void)get_moves(target_ptr, m_idx, mm);
2378 target_ptr->pet_follow_distance = (s16b)dis;
2385 if (!is_hostile(m_ptr))
2387 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2388 get_enemy_dir(target_ptr, m_idx, mm);
2392 if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
2399 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
2400 * @param target_ptr プレーヤーへの参照ポインタ
2401 * @param m_idx モンスターID
2402 * @param is_riding_mon 騎乗状態ならばTRUE
2403 * @param see_m モンスターが視界内にいたらTRUE
2404 * @return モンスターがフロアから消えたらTRUE
2406 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m)
2408 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2409 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2410 bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
2411 can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
2412 can_runaway &= !target_ptr->phase_out;
2413 if (!can_runaway) return FALSE;
2415 static int riding_pinch = 0;
2417 if (m_ptr->hp >= m_ptr->maxhp / 3)
2419 /* Reset the counter */
2420 if (is_riding_mon) riding_pinch = 0;
2425 GAME_TEXT m_name[MAX_NLEN];
2426 monster_desc(target_ptr, m_name, m_ptr, 0);
2427 if (is_riding_mon && riding_pinch < 2)
2429 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2430 "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
2432 disturb(target_ptr, TRUE, TRUE);
2438 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2439 if (rakuba(target_ptr, -1, FALSE))
2441 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2447 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) &&
2448 player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) && projectable(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x))
2450 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2453 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
2454 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2457 if (is_riding_mon && rakuba(target_ptr, -1, FALSE))
2459 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2462 check_quest_completion(target_ptr, m_ptr);
2463 delete_monster_idx(target_ptr, m_idx);
2469 * @brief モンスターに魔法を試行させる
2470 * @param target_ptr プレーヤーへの参照ポインタ
2471 * @param m_idx モンスターID
2472 * @param aware 起きていればTRUE
2473 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
2475 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
2477 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2478 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2479 if ((r_ptr->freq_spell == 0) || !randint1(100) <= r_ptr->freq_spell)
2482 bool counterattack = FALSE;
2483 if (m_ptr->target_y)
2485 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2486 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
2487 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2489 counterattack = TRUE;
2495 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2496 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2500 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2501 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2509 * @brief 全モンスターのターン管理メインルーチン /
2510 * Process all the "live" monsters, once per game turn.
2513 * During each game current game turn, we scan through the list of all the "live" monsters,\n
2514 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2515 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2517 * Note that monsters can never move in the monster array (except when the\n
2518 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2520 * This function is responsible for at least half of the processor time\n
2521 * on a normal system with a "normal" amount of monsters and a player doing\n
2524 * When the player is resting, virtually 90% of the processor time is spent\n
2525 * in this function, and its children, "process_monster()" and "make_move()".\n
2527 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2528 * especially when the player is running.\n
2530 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2531 * monsters while they are still being "born". A monster is "fresh" only\n
2532 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2533 * determine if the monster is yet to be processed during the game turn.\n
2535 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2536 * move before any "nasty" monsters get to use their spell attacks.\n
2538 * Note that when the "knowledge" about the currently tracked monster\n
2539 * changes (flags, attacks, spells), we induce a redraw of the monster\n
2542 void process_monsters(player_type *target_ptr)
2544 BIT_FLAGS old_r_flags1 = 0L;
2545 BIT_FLAGS old_r_flags2 = 0L;
2546 BIT_FLAGS old_r_flags3 = 0L;
2547 BIT_FLAGS old_r_flags4 = 0L;
2548 BIT_FLAGS old_r_flags5 = 0L;
2549 BIT_FLAGS old_r_flags6 = 0L;
2550 BIT_FLAGS old_r_flagsr = 0L;
2552 byte old_r_blows0 = 0;
2553 byte old_r_blows1 = 0;
2554 byte old_r_blows2 = 0;
2555 byte old_r_blows3 = 0;
2557 /* Clear monster fighting indicator */
2558 floor_type *floor_ptr = target_ptr->current_floor_ptr;
2559 floor_ptr->monster_noise = FALSE;
2561 /* Memorize old race */
2562 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2564 /* Acquire knowledge */
2565 byte old_r_cast_spell = 0;
2566 if (target_ptr->monster_race_idx)
2568 /* Acquire current monster */
2569 monster_race *r_ptr;
2570 r_ptr = &r_info[target_ptr->monster_race_idx];
2572 /* Memorize flags */
2573 old_r_flags1 = r_ptr->r_flags1;
2574 old_r_flags2 = r_ptr->r_flags2;
2575 old_r_flags3 = r_ptr->r_flags3;
2576 old_r_flags4 = r_ptr->r_flags4;
2577 old_r_flags5 = r_ptr->r_flags5;
2578 old_r_flags6 = r_ptr->r_flags6;
2579 old_r_flagsr = r_ptr->r_flagsr;
2581 /* Memorize blows */
2582 old_r_blows0 = r_ptr->r_blows[0];
2583 old_r_blows1 = r_ptr->r_blows[1];
2584 old_r_blows2 = r_ptr->r_blows[2];
2585 old_r_blows3 = r_ptr->r_blows[3];
2587 /* Memorize castings */
2588 old_r_cast_spell = r_ptr->r_cast_spell;
2591 /* Process the monsters (backwards) */
2593 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2595 monster_type *m_ptr;
2596 monster_race *r_ptr;
2597 m_ptr = &floor_ptr->m_list[i];
2598 r_ptr = &r_info[m_ptr->r_idx];
2600 /* Handle "leaving" */
2601 if (target_ptr->leaving) break;
2603 /* Ignore "dead" monsters */
2604 if (!monster_is_valid(m_ptr)) continue;
2606 if (target_ptr->wild_mode) continue;
2609 /* Handle "fresh" monsters */
2610 if (m_ptr->mflag & MFLAG_BORN)
2612 /* No longer "fresh" */
2613 m_ptr->mflag &= ~(MFLAG_BORN);
2619 /* Hack -- Require proximity */
2620 if (m_ptr->cdis >= AAF_LIMIT) continue;
2622 POSITION fx = m_ptr->fx;
2623 POSITION fy = m_ptr->fy;
2625 /* Flow by smell is allowed */
2626 if (!target_ptr->no_flowed)
2628 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2631 /* Assume no move */
2634 /* Handle "sensing radius" */
2635 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2637 /* We can "sense" the player */
2641 /* Handle "sight" and "aggravation" */
2642 else if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2643 (player_has_los_bold(target_ptr, fy, fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2645 /* We can "see" or "feel" the player */
2649 else if (m_ptr->target_y) test = TRUE;
2652 if (!test) continue;
2655 if (target_ptr->riding == i)
2656 speed = target_ptr->pspeed;
2659 speed = m_ptr->mspeed;
2661 /* Monsters move quickly in Nightmare mode */
2662 if (ironman_nightmare) speed += 5;
2664 if (MON_FAST(m_ptr)) speed += 10;
2665 if (MON_SLOW(m_ptr)) speed -= 10;
2668 /* Give this monster some energy */
2669 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2671 /* Not enough energy to move */
2672 if (m_ptr->energy_need > 0) continue;
2674 /* Use up "some" energy */
2675 m_ptr->energy_need += ENERGY_NEED();
2677 /* Save global index */
2680 /* Process the monster */
2681 process_monster(target_ptr, i);
2683 reset_target(m_ptr);
2685 /* Give up flow_by_smell when it might useless */
2686 if (target_ptr->no_flowed && one_in_(3))
2687 m_ptr->mflag2 |= MFLAG2_NOFLOW;
2689 /* Hack -- notice death or departure */
2690 if (!target_ptr->playing || target_ptr->is_dead) break;
2692 /* Notice leaving */
2693 if (target_ptr->leaving) break;
2696 /* Reset global index */
2700 /* Tracking a monster race (the same one we were before) */
2701 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2704 /* Acquire monster race */
2705 monster_race *r_ptr;
2706 r_ptr = &r_info[target_ptr->monster_race_idx];
2708 /* Check for knowledge change */
2709 if ((old_r_flags1 != r_ptr->r_flags1) ||
2710 (old_r_flags2 != r_ptr->r_flags2) ||
2711 (old_r_flags3 != r_ptr->r_flags3) ||
2712 (old_r_flags4 != r_ptr->r_flags4) ||
2713 (old_r_flags5 != r_ptr->r_flags5) ||
2714 (old_r_flags6 != r_ptr->r_flags6) ||
2715 (old_r_flagsr != r_ptr->r_flagsr) ||
2716 (old_r_blows0 != r_ptr->r_blows[0]) ||
2717 (old_r_blows1 != r_ptr->r_blows[1]) ||
2718 (old_r_blows2 != r_ptr->r_blows[2]) ||
2719 (old_r_blows3 != r_ptr->r_blows[3]) ||
2720 (old_r_cast_spell != r_ptr->r_cast_spell))
2722 target_ptr->window |= (PW_MONSTER);