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 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
46 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
47 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
48 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m);
49 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox);
50 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware);
51 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m);
52 void process_special(player_type *target_ptr, MONSTER_IDX m_idx);
53 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware);
54 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware);
57 * @brief モンスターが敵に接近するための方向を決める /
58 * Calculate the direction to the next enemy
59 * @param target_ptr プレーヤーへの参照ポインタ
60 * @param m_idx モンスターの参照ID
61 * @param mm 移動するべき方角IDを返す参照ポインタ
62 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
64 static bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
66 floor_type *floor_ptr = target_ptr->current_floor_ptr;
67 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
68 monster_race *r_ptr = &r_info[m_ptr->r_idx];
71 POSITION x = 0, y = 0;
72 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
74 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
75 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
77 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
79 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
80 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
86 if (target_ptr->phase_out)
88 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
89 if (randint0(2)) plus = -1;
91 else start = floor_ptr->m_max + 1;
93 /* Scan thru all monsters */
94 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
96 MONSTER_IDX dummy = (i % floor_ptr->m_max);
100 MONSTER_IDX t_idx = dummy;
101 t_ptr = &floor_ptr->m_list[t_idx];
103 /* The monster itself isn't a target */
104 if (t_ptr == m_ptr) continue;
106 if (!monster_is_valid(t_ptr)) continue;
110 /* Hack -- only fight away from player */
111 if (target_ptr->pet_follow_distance < 0)
113 /* No fighting near player */
114 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
119 /* Hack -- no fighting away from player */
120 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
125 if (r_ptr->aaf < t_ptr->cdis) continue;
128 /* Monster must be 'an enemy' */
129 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
131 /* Monster must be projectable if we can't pass through walls */
132 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
133 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
135 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
139 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
142 /* OK -- we've got a target */
149 if (!x && !y) return FALSE;
152 /* Extract the direction */
157 if ((y < 0) && (x == 0))
164 else if ((y > 0) && (x == 0))
171 else if ((x > 0) && (y == 0))
178 else if ((x < 0) && (y == 0))
185 else if ((y < 0) && (x < 0))
192 else if ((y < 0) && (x > 0))
199 else if ((y > 0) && (x < 0))
206 else if ((y > 0) && (x > 0))
217 * @brief モンスターがプレイヤーから逃走するかどうかを返す /
218 * Returns whether a given monster will try to run from the player.
219 * @param m_idx 逃走するモンスターの参照ID
220 * @return モンスターがプレイヤーから逃走するならばTRUEを返す。
222 * Monsters will attempt to avoid very powerful players. See below.\n
224 * Because this function is called so often, little details are important\n
225 * for efficiency. Like not using "mod" or "div" when possible. And\n
226 * attempting to check the conditions in an optimal order. Note that\n
227 * "(x << 2) == (x * 4)" if "x" has enough bits to hold the result.\n
229 * Note that this function is responsible for about one to five percent\n
230 * of the processor use in normal conditions...\n
232 static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
234 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
235 monster_race *r_ptr = &r_info[m_ptr->r_idx];
239 HIT_POINT p_chp, p_mhp;
240 HIT_POINT m_chp, m_mhp;
243 /* Friends can be commanded to avoid the player */
246 /* Are we trying to avoid the player? */
247 return ((target_ptr->pet_follow_distance < 0) &&
248 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
251 /* Keep monsters from running too far away */
252 if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
254 /* All "afraid" monsters will run away */
255 if (MON_MONFEAR(m_ptr)) return TRUE;
257 /* Nearby monsters will not become terrified */
258 if (m_ptr->cdis <= 5) return FALSE;
260 /* Examine player power (level) */
261 p_lev = target_ptr->lev;
263 /* Examine monster power (level plus morale) */
264 m_lev = r_ptr->level + (m_idx & 0x08) + 25;
266 /* Optimize extreme cases below */
267 if (m_lev > p_lev + 4) return FALSE;
268 if (m_lev + 4 <= p_lev) return TRUE;
270 /* Examine player health */
271 p_chp = target_ptr->chp;
272 p_mhp = target_ptr->mhp;
274 /* Examine monster health */
276 m_mhp = m_ptr->maxhp;
278 /* Prepare to optimize the calculation */
279 p_val = (p_lev * p_mhp) + (p_chp << 2); /* div p_mhp */
280 m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
282 /* Strong players scare strong monsters */
283 if (p_val * m_mhp > m_val * p_mhp) return TRUE;
290 * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
291 * Search spell castable grid
292 * @param target_ptr プレーヤーへの参照ポインタ
293 * @param m_idx モンスターの参照ID
294 * @param yp 適したマスのY座標を返す参照ポインタ
295 * @param xp 適したマスのX座標を返す参照ポインタ
296 * @return 有効なマスがあった場合TRUEを返す
298 static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
300 floor_type *floor_ptr = target_ptr->current_floor_ptr;
301 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
302 monster_race *r_ptr = &r_info[m_ptr->r_idx];
304 /* Monster location */
305 POSITION y1 = m_ptr->fy;
306 POSITION x1 = m_ptr->fx;
308 /* Monster can already cast spell to player */
309 if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
311 /* Set current grid cost */
312 int now_cost = floor_ptr->grid_array[y1][x1].cost;
313 if (now_cost == 0) now_cost = 999;
315 /* Can monster bash or open doors? */
316 bool can_open_door = FALSE;
317 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
319 can_open_door = TRUE;
322 /* Check nearby grids, diagonals first */
324 for (int i = 7; i >= 0; i--)
328 POSITION y = y1 + ddy_ddd[i];
329 POSITION x = x1 + ddx_ddd[i];
331 /* Ignore locations off of edge */
332 if (!in_bounds2(floor_ptr, y, x)) continue;
334 /* Simply move to player */
335 if (player_bold(target_ptr, y, x)) return FALSE;
338 g_ptr = &floor_ptr->grid_array[y][x];
342 /* Monster cannot kill or pass walls */
343 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))))
345 if (cost == 0) continue;
346 if (!can_open_door && is_closed_door(target_ptr, g_ptr->feat)) continue;
349 /* Hack -- for kill or pass wall monster.. */
350 if (cost == 0) cost = 998;
352 if (now_cost < cost) continue;
354 if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
356 /* Accept louder sounds */
357 if (best < cost) continue;
360 (*yp) = y1 + ddy_ddd[i];
361 (*xp) = x1 + ddx_ddd[i];
364 if (best == 999) return FALSE;
371 * @brief モンスターがプレイヤーに向けて接近することが可能なマスを走査する /
372 * Choose the "best" direction for "flowing"
373 * @param m_idx モンスターの参照ID
374 * @param yp 移動先のマスのY座標を返す参照ポインタ
375 * @param xp 移動先のマスのX座標を返す参照ポインタ
376 * @param no_flow モンスターにFLOWフラグが経っていない状態でTRUE
377 * @return 有効なマスがあった場合TRUEを返す
379 * Note that ghosts and rock-eaters are never allowed to "flow",\n
380 * since they should move directly towards the player.\n
382 * Prefer "non-diagonal" directions, but twiddle them a little\n
383 * to angle slightly towards the player's actual location.\n
385 * Allow very perceptive monsters to track old "spoor" left by\n
386 * previous locations occupied by the player. This will tend\n
387 * to have monsters end up either near the player or on a grid\n
388 * recently occupied by the player (and left via "teleport").\n
390 * Note that if "smell" is turned on, all monsters get vicious.\n
392 * Also note that teleporting away from a location will cause\n
393 * the monsters who were chasing you to converge on that location\n
394 * as long as you are still near enough to "annoy" them without\n
395 * being close enough to chase directly. I have no idea what will\n
396 * happen if you combine "smell" with low "aaf" values.\n
398 static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
401 floor_type *floor_ptr = target_ptr->current_floor_ptr;
402 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
403 monster_race *r_ptr = &r_info[m_ptr->r_idx];
405 /* Can monster cast attack spell? */
406 if (r_ptr->flags4 & (RF4_ATTACK_MASK) ||
407 r_ptr->a_ability_flags1 & (RF5_ATTACK_MASK) ||
408 r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
410 /* Can move spell castable grid? */
411 if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
414 /* Monster can't flow */
415 if (no_flow) return FALSE;
417 /* Monster can go through rocks */
418 if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
419 if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
421 /* Monster location */
422 POSITION y1 = m_ptr->fy;
423 POSITION x1 = m_ptr->fx;
425 /* Hack -- Player can see us, run towards him */
426 if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
429 g_ptr = &floor_ptr->grid_array[y1][x1];
431 /* If we can hear noises, advance towards them */
433 bool use_scent = FALSE;
439 /* Otherwise, try to follow a scent trail */
440 else if (g_ptr->when)
443 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
449 /* Otherwise, advance blindly */
455 /* Check nearby grids, diagonals first */
456 for (int i = 7; i >= 0; i--)
458 POSITION y = y1 + ddy_ddd[i];
459 POSITION x = x1 + ddx_ddd[i];
461 /* Ignore locations off of edge */
462 if (!in_bounds2(floor_ptr, y, x)) continue;
464 g_ptr = &floor_ptr->grid_array[y][x];
466 /* We're following a scent trail */
469 int when = g_ptr->when;
471 /* Accept younger scent */
472 if (best > when) continue;
476 /* We're using sound */
481 if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
483 else cost = g_ptr->cost;
485 /* Accept louder sounds */
486 if ((cost == 0) || (best < cost)) continue;
490 /* Hack -- Save the "twiddled" location */
491 (*yp) = target_ptr->y + 16 * ddy_ddd[i];
492 (*xp) = target_ptr->x + 16 * ddx_ddd[i];
495 if (best == 999 || best == 0) return FALSE;
502 * @brief モンスターがプレイヤーから逃走することが可能なマスを走査する /
503 * Provide a location to flee to, but give the player a wide berth.
504 * @param m_idx モンスターの参照ID
505 * @param yp 移動先のマスのY座標を返す参照ポインタ
506 * @param xp 移動先のマスのX座標を返す参照ポインタ
507 * @return 有効なマスがあった場合TRUEを返す
509 * A monster may wish to flee to a location that is behind the player,\n
510 * but instead of heading directly for it, the monster should "swerve"\n
511 * around the player so that he has a smaller chance of getting hit.\n
513 static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
515 POSITION gy = 0, gx = 0;
517 /* Monster location */
518 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
519 POSITION fy = m_ptr->fy;
520 POSITION fx = m_ptr->fx;
522 /* Desired destination */
523 POSITION y1 = fy - (*yp);
524 POSITION x1 = fx - (*xp);
526 /* Check nearby grids, diagonals first */
528 for (int i = 7; i >= 0; i--)
531 POSITION y = fy + ddy_ddd[i];
532 POSITION x = fx + ddx_ddd[i];
534 /* Ignore locations off of edge */
535 if (!in_bounds2(floor_ptr, y, x)) continue;
537 /* Calculate distance of this grid from our destination */
538 dis = distance(y, x, y1, x1);
540 /* Score this grid */
541 s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
543 /* No negative scores */
546 /* Ignore lower scores */
547 if (s < score) continue;
549 /* Save the score and time */
552 /* Save the location */
557 /* No legal move (?) */
558 if (score == -1) return FALSE;
569 * Hack -- Precompute a bunch of calls to distance() in find_safety() and
572 * The pair of arrays dist_offsets_y[n] and dist_offsets_x[n] contain the
573 * offsets of all the locations with a distance of n from a central point,
574 * with an offset of (0,0) indicating no more offsets at this distance.
576 * This is, of course, fairly unreadable, but it eliminates multiple loops
577 * from the previous version.
579 * It is probably better to replace these arrays with code to compute
580 * the relevant arrays, even if the storage is pre-allocated in hard
581 * coded sizes. At the very least, code should be included which is
582 * able to generate and dump these arrays (ala "los()").
584 * Also, the storage needs could be halved by using bytes.
586 * These arrays could be combined into two big arrays, using sub-arrays
587 * to hold the offsets and lengths of each portion of the sub-arrays, and
588 * this could perhaps also be used somehow in the "look" code.
592 static POSITION d_off_y_0[] = { 0 };
593 static POSITION d_off_x_0[] = { 0 };
595 static POSITION d_off_y_1[] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
596 static POSITION d_off_x_1[] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
598 static POSITION d_off_y_2[] = { -1, -1, -2, -2, -2, 0, 0, 1, 1, 2, 2, 2, 0 };
599 static POSITION d_off_x_2[] = { -2, 2, -1, 0, 1, -2, 2, -2, 2, -1, 0, 1, 0 };
601 static POSITION d_off_y_3[] = { -1, -1, -2, -2, -3, -3, -3, 0, 0, 1, 1, 2, 2, 3, 3, 3, 0 };
602 static POSITION d_off_x_3[] = { -3, 3, -2, 2, -1, 0, 1, -3, 3, -3, 3, -2, 2, -1, 0, 1, 0 };
604 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 };
605 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 };
608 static POSITION d_off_y_5[] =
609 { -1, -1, -2, -2, -3, -3, -4, -4, -4, -4, -5, -5,
610 -5, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5,
613 static POSITION d_off_x_5[] =
614 { -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1, 0, 1,
615 -5, 5, -5, 5, -4, 4, -4, 4, -2, -3, 2, 3, -1,
619 static POSITION d_off_y_6[] =
620 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
621 -6, -6, -6, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5,
624 static POSITION d_off_x_6[] =
625 { -6, 6, -5, 5, -5, 5, -4, 4, -2, -3, 2, 3, -1,
626 0, 1, -6, 6, -6, 6, -5, 5, -5, 5, -4, 4, -2,
627 -3, 2, 3, -1, 0, 1, 0 };
630 static POSITION d_off_y_7[] =
631 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -5, -5,
632 -6, -6, -6, -6, -7, -7, -7, 0, 0, 1, 1, 2, 2, 3,
633 3, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 0 };
635 static POSITION d_off_x_7[] =
636 { -7, 7, -6, 6, -6, 6, -5, 5, -4, -5, 4, 5, -2,
637 -3, 2, 3, -1, 0, 1, -7, 7, -7, 7, -6, 6, -6,
638 6, -5, 5, -4, -5, 4, 5, -2, -3, 2, 3, -1, 0,
642 static POSITION d_off_y_8[] =
643 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
644 -6, -6, -7, -7, -7, -7, -8, -8, -8, 0, 0, 1, 1,
645 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7,
648 static POSITION d_off_x_8[] =
649 { -8, 8, -7, 7, -7, 7, -6, 6, -6, 6, -4, -5, 4,
650 5, -2, -3, 2, 3, -1, 0, 1, -8, 8, -8, 8, -7,
651 7, -7, 7, -6, 6, -6, 6, -4, -5, 4, 5, -2, -3,
655 static POSITION d_off_y_9[] =
656 { -1, -1, -2, -2, -3, -3, -4, -4, -5, -5, -6, -6,
657 -7, -7, -7, -7, -8, -8, -8, -8, -9, -9, -9, 0,
658 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 7,
659 7, 8, 8, 8, 8, 9, 9, 9, 0 };
661 static POSITION d_off_x_9[] =
662 { -9, 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4,
663 -5, 4, 5, -2, -3, 2, 3, -1, 0, 1, -9, 9, -9,
664 9, -8, 8, -8, 8, -7, 7, -7, 7, -6, 6, -4, -5,
665 4, 5, -2, -3, 2, 3, -1, 0, 1, 0 };
668 static POSITION *dist_offsets_y[10] =
670 d_off_y_0, d_off_y_1, d_off_y_2, d_off_y_3, d_off_y_4,
671 d_off_y_5, d_off_y_6, d_off_y_7, d_off_y_8, d_off_y_9
674 static POSITION *dist_offsets_x[10] =
676 d_off_x_0, d_off_x_1, d_off_x_2, d_off_x_3, d_off_x_4,
677 d_off_x_5, d_off_x_6, d_off_x_7, d_off_x_8, d_off_x_9
681 * @brief モンスターが逃げ込める安全な地点を返す /
682 * Choose a "safe" location near a monster for it to run toward.
683 * @param target_ptr プレーヤーへの参照ポインタ
684 * @param m_idx モンスターの参照ID
685 * @param yp 移動先のマスのY座標を返す参照ポインタ
686 * @param xp 移動先のマスのX座標を返す参照ポインタ
687 * @return 有効なマスがあった場合TRUEを返す
689 * A location is "safe" if it can be reached quickly and the player\n
690 * is not able to fire into it (it isn't a "clean shot"). So, this will\n
691 * cause monsters to "duck" behind walls. Hopefully, monsters will also\n
692 * try to run towards corridor openings if they are in a room.\n
694 * This function may take lots of CPU time if lots of monsters are\n
697 * Return TRUE if a safe location is available.\n
699 static bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
701 floor_type *floor_ptr = target_ptr->current_floor_ptr;
702 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
704 POSITION fy = m_ptr->fy;
705 POSITION fx = m_ptr->fx;
707 POSITION gy = 0, gx = 0, gdis = 0;
709 /* Start with adjacent locations, spread further */
710 for (POSITION d = 1; d < 10; d++)
712 /* Get the lists of points with a distance d from (fx, fy) */
714 y_offsets = dist_offsets_y[d];
717 x_offsets = dist_offsets_x[d];
719 /* Check the locations */
720 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
722 i++, dx = x_offsets[i], dy = y_offsets[i])
724 POSITION y = fy + dy;
725 POSITION x = fx + dx;
727 /* Skip illegal locations */
728 if (!in_bounds(floor_ptr, y, x)) continue;
731 g_ptr = &floor_ptr->grid_array[y][x];
733 /* Skip locations in a wall */
734 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;
736 /* Check for "availability" (if monsters can flow) */
737 if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
739 /* Ignore grids very far from the player */
740 if (g_ptr->dist == 0) continue;
742 /* Ignore too-distant grids */
743 if (g_ptr->dist > floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
746 /* Check for absence of shot (more or less) */
747 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
749 /* Calculate distance from player */
750 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
752 /* Remember if further than previous */
753 if (dis <= gdis) continue;
760 /* Check for success */
761 if (gdis <= 0) continue;
775 * @brief モンスターが隠れ潜める地点を返す /
776 * Choose a good hiding place near a monster for it to run toward.
777 * @param target_ptr プレーヤーへの参照ポインタ
778 * @param m_idx モンスターの参照ID
779 * @param yp 移動先のマスのY座標を返す参照ポインタ
780 * @param xp 移動先のマスのX座標を返す参照ポインタ
781 * @return 有効なマスがあった場合TRUEを返す
783 * Pack monsters will use this to "ambush" the player and lure him out\n
784 * of corridors into open space so they can swarm him.\n
786 * Return TRUE if a good location is available.\n
788 static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
790 floor_type *floor_ptr = target_ptr->current_floor_ptr;
791 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
792 monster_race *r_ptr = &r_info[m_ptr->r_idx];
794 POSITION fy = m_ptr->fy;
795 POSITION fx = m_ptr->fx;
797 POSITION gy = 0, gx = 0, gdis = 999;
799 /* Start with adjacent locations, spread further */
800 for (POSITION d = 1; d < 10; d++)
802 /* Get the lists of points with a distance d from (fx, fy) */
804 y_offsets = dist_offsets_y[d];
807 x_offsets = dist_offsets_x[d];
809 /* Check the locations */
810 for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
812 i++, dx = x_offsets[i], dy = y_offsets[i])
814 POSITION y = fy + dy;
815 POSITION x = fx + dx;
817 /* Skip illegal locations */
818 if (!in_bounds(floor_ptr, y, x)) continue;
820 /* Skip occupied locations */
821 if (!monster_can_enter(target_ptr, y, x, r_ptr, 0)) continue;
823 /* Check for hidden, available grid */
824 if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
827 /* Calculate distance from player */
828 POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
830 /* Remember if closer than previous */
831 if (dis < gdis && dis >= 2)
839 if (gdis >= 999) continue;
852 * @brief モンスターの移動方向を返す /
853 * Choose "logical" directions for monster movement
854 * @param target_ptr プレーヤーへの参照ポインタ
855 * @param m_idx モンスターの参照ID
856 * @param mm 移動方向を返す方向IDの参照ポインタ
857 * @return 有効方向があった場合TRUEを返す
859 static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
861 floor_type *floor_ptr = target_ptr->current_floor_ptr;
862 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
863 monster_race *r_ptr = &r_info[m_ptr->r_idx];
864 POSITION y = 0, ay, x = 0, ax;
866 POSITION y2 = target_ptr->y;
867 POSITION x2 = target_ptr->x;
869 bool will_run = mon_will_run(target_ptr, m_idx);
871 bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
872 bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
874 /* Counter attack to an enemy monster */
875 if (!will_run && m_ptr->target_y)
877 int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
879 /* The monster must be an enemy, and in LOS */
881 are_enemies(target_ptr, m_ptr, &floor_ptr->m_list[t_m_idx]) &&
882 los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
883 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
885 /* Extract the "pseudo-direction" */
886 y = m_ptr->fy - m_ptr->target_y;
887 x = m_ptr->fx - m_ptr->target_x;
892 if (!done && !will_run && is_hostile(m_ptr) &&
893 (r_ptr->flags1 & RF1_FRIENDS) &&
894 ((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)) ||
895 (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
898 * Animal packs try to get the player out of corridors
899 * (...unless they can move through walls -- TY)
901 if ((r_ptr->flags3 & RF3_ANIMAL) && !can_pass_wall &&
902 !(r_ptr->flags2 & RF2_KILL_WALL))
906 /* Count room grids next to player */
907 for (i = 0; i < 8; i++)
909 int xx = target_ptr->x + ddx_ddd[i];
910 int yy = target_ptr->y + ddy_ddd[i];
912 if (!in_bounds2(floor_ptr, yy, xx)) continue;
914 g_ptr = &floor_ptr->grid_array[yy][xx];
917 if (monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, 0))
919 /* One more room grid */
923 if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
924 if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
926 /* Not in a room and strong player */
927 if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
928 (target_ptr->mhp + target_ptr->msp))
930 /* Find hiding place */
931 if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
935 /* Monster groups try to surround the player */
936 if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
940 /* Find an empty square near the player to fill */
941 for (i = 0; i < 8; i++)
943 /* Pick squares near player (semi-randomly) */
944 y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
945 x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
948 if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
950 /* Attack the player */
957 if (!in_bounds2(floor_ptr, y2, x2)) continue;
959 /* Ignore filled grids */
960 if (!monster_can_enter(target_ptr, y2, x2, r_ptr, 0)) continue;
962 /* Try to fill this hole */
966 /* Extract the new "pseudo-direction" */
976 /* Flow towards the player */
977 (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
979 /* Extract the "pseudo-direction" */
986 /* Apply fear if possible and necessary */
987 if (is_pet(m_ptr) && will_run)
989 /* XXX XXX Not very "smart" */
994 if (!done && will_run)
999 /* Try to find safe place */
1000 if (find_safety(target_ptr, m_idx, &y, &x))
1002 /* Attempt to avoid the player */
1005 /* Adjust movement */
1006 if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x)) done = TRUE;
1012 /* This is not a very "smart" method XXX XXX */
1020 /* Check for no move */
1021 if (!x && !y) return FALSE;
1024 /* Extract the "absolute distances" */
1028 /* Do something weird */
1029 if (y < 0) move_val += 8;
1030 if (x > 0) move_val += 4;
1032 /* Prevent the diamond maneuvre */
1033 if (ay > (ax << 1)) move_val += 2;
1034 else if (ax > (ay << 1)) move_val++;
1036 /* Extract some directions */
1181 /* Wants to move... */
1186 static bool check_hp_for_feat_destruction(feature_type *f_ptr, monster_type *m_ptr)
1188 return !have_flag(f_ptr->flags, FF_GLASS) ||
1189 (r_info[m_ptr->r_idx].flags2 & RF2_STUPID) ||
1190 (m_ptr->hp >= MAX(m_ptr->maxhp / 3, 200));
1195 * @brief モンスター単体の1ターン行動処理メインルーチン /
1197 * @param target_ptr プレーヤーへの参照ポインタ
1198 * @param m_idx 行動モンスターの参照ID
1201 * The monster is known to be within 100 grids of the player\n
1203 * In several cases, we directly update the monster lore\n
1205 * Note that a monster is only allowed to "reproduce" if there\n
1206 * are a limited number of "reproducing" monsters on the current\n
1207 * level. This should prevent the level from being "swamped" by\n
1208 * reproducing monsters. It also allows a large mass of mice to\n
1209 * prevent a louse from multiplying, but this is a small price to\n
1210 * pay for a simple multiplication method.\n
1212 * XXX Monster fear is slightly odd, in particular, monsters will\n
1213 * fixate on opening a door even if they cannot open it. Actually,\n
1214 * the same thing happens to normal monsters when they hit a door\n
1216 * In addition, monsters which *cannot* open or bash\n
1217 * down a door will still stand there trying to open it...\n
1219 * XXX Technically, need to check for monster in the way\n
1220 * combined with that monster being in a wall (or door?)\n
1222 * A "direction" of "5" means "pick a random direction".\n
1224 void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
1226 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
1227 monster_race *r_ptr = &r_info[m_ptr->r_idx];
1228 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
1234 feature_type *f_ptr;
1235 monster_type *y_ptr;
1240 bool must_alter_to_move;
1253 bool is_riding_mon = (m_idx == target_ptr->riding);
1254 bool see_m = is_seen(m_ptr);
1256 if (is_riding_mon && !(r_ptr->flags7 & RF7_RIDING))
1258 if (rakuba(target_ptr, 0, TRUE))
1261 msg_print("地面に落とされた。");
1263 GAME_TEXT m_name[MAX_NLEN];
1264 monster_desc(target_ptr, m_name, &target_ptr->current_floor_ptr->m_list[target_ptr->riding], 0);
1265 msg_format("You have fallen from %s.", m_name);
1270 if ((m_ptr->mflag2 & MFLAG2_CHAMELEON) && one_in_(13) && !MON_CSLEEP(m_ptr))
1272 choose_new_monster(target_ptr, m_idx, FALSE, 0);
1273 r_ptr = &r_info[m_ptr->r_idx];
1276 /* Players hidden in shadow are almost imperceptable. -LM- */
1277 if (target_ptr->special_defense & NINJA_S_STEALTH)
1279 int tmp = target_ptr->lev * 6 + (target_ptr->skill_stl + 10) * 4;
1280 if (target_ptr->monlite) tmp /= 3;
1281 if (target_ptr->cursed & TRC_AGGRAVATE) tmp /= 2;
1282 if (r_ptr->level > (target_ptr->lev * target_ptr->lev / 20 + 10)) tmp /= 3;
1283 /* Low-level monsters will find it difficult to locate the player. */
1284 if (randint0(tmp) > (r_ptr->level + 20)) aware = FALSE;
1287 if (vanish_summoned_children(target_ptr, m_idx, see_m)) return;
1288 if (process_quantum_effect(target_ptr,m_idx, see_m)) return;
1290 if (m_ptr->r_idx == MON_SHURYUUDAN)
1292 mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
1296 if (runaway_monster(target_ptr, m_idx, is_riding_mon, see_m)) return;
1298 awake_monster(target_ptr, m_idx);
1301 if (MON_STUNNED(m_ptr))
1303 /* Sometimes skip move */
1304 if (one_in_(2)) return;
1309 target_ptr->update |= (PU_BONUS);
1312 process_angar(target_ptr, m_idx, see_m);
1314 /* Get the origin */
1315 POSITION oy = m_ptr->fy;
1316 POSITION ox = m_ptr->fx;
1318 if (decide_monster_multiplication(target_ptr, m_idx, oy, ox)) return;
1320 process_special(target_ptr, m_idx);
1321 process_speak_sound(target_ptr, m_idx, oy, ox, aware);
1322 cast_spell(target_ptr, m_idx, aware);
1324 /* Hack -- Assume no movement */
1325 mm[0] = mm[1] = mm[2] = mm[3] = 0;
1326 mm[4] = mm[5] = mm[6] = mm[7] = 0;
1328 if (!decide_monster_movement_direction(target_ptr, mm, m_idx, aware)) return;
1330 /* Assume nothing */
1334 must_alter_to_move = FALSE;
1336 /* Assume nothing */
1337 did_open_door = FALSE;
1338 did_bash_door = FALSE;
1339 did_take_item = FALSE;
1340 did_kill_item = FALSE;
1341 did_move_body = FALSE;
1342 did_pass_wall = FALSE;
1343 did_kill_wall = FALSE;
1345 /* Take a zero-terminated array of "directions" */
1346 for (i = 0; mm[i]; i++)
1348 /* Get the direction */
1351 /* Hack -- allow "randomized" motion */
1352 if (d == 5) d = ddd[randint0(8)];
1354 /* Get the destination */
1355 POSITION ny = oy + ddy[d];
1356 POSITION nx = ox + ddx[d];
1358 /* Ignore locations off of edge */
1359 if (!in_bounds2(target_ptr->current_floor_ptr, ny, nx)) continue;
1361 /* Access that grid */
1362 g_ptr = &target_ptr->current_floor_ptr->grid_array[ny][nx];
1363 f_ptr = &f_info[g_ptr->feat];
1364 can_cross = monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0);
1366 /* Access that grid's contents */
1367 y_ptr = &target_ptr->current_floor_ptr->m_list[g_ptr->m_idx];
1369 /* Hack -- player 'in' wall */
1370 if (player_bold(target_ptr, ny, nx))
1375 /* Possibly a monster to attack */
1376 else if (g_ptr->m_idx)
1381 /* Monster destroys walls (and doors) */
1382 else if ((r_ptr->flags2 & RF2_KILL_WALL) &&
1383 (can_cross ? !have_flag(f_ptr->flags, FF_LOS) : !is_riding_mon) &&
1384 have_flag(f_ptr->flags, FF_HURT_DISI) && !have_flag(f_ptr->flags, FF_PERMANENT) &&
1385 check_hp_for_feat_destruction(f_ptr, m_ptr))
1387 /* Eat through walls/doors/rubble */
1389 if (!can_cross) must_alter_to_move = TRUE;
1391 /* Monster destroyed a wall (later) */
1392 did_kill_wall = TRUE;
1395 /* Floor is open? */
1398 /* Go ahead and move */
1401 /* Monster moves through walls (and doors) */
1402 if ((r_ptr->flags2 & RF2_PASS_WALL) && (!is_riding_mon || target_ptr->pass_wall) &&
1403 have_flag(f_ptr->flags, FF_CAN_PASS))
1405 /* Monster went through a wall */
1406 did_pass_wall = TRUE;
1410 /* Handle doors and secret doors */
1411 else if (is_closed_door(target_ptr, g_ptr->feat))
1413 bool may_bash = TRUE;
1415 /* Assume no move allowed */
1418 /* Creature can open doors. */
1419 if ((r_ptr->flags2 & RF2_OPEN_DOOR) && have_flag(f_ptr->flags, FF_OPEN) &&
1420 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1425 /* The door is open */
1426 did_open_door = TRUE;
1428 /* Do not bash the door */
1434 /* Locked doors (not jammed) */
1437 /* Try to unlock it */
1438 if (randint0(m_ptr->hp / 10) > f_ptr->power)
1440 /* Unlock the door */
1441 cave_alter_feat(target_ptr, ny, nx, FF_DISARM);
1443 /* Do not bash the door */
1451 /* Stuck doors -- attempt to bash them down if allowed */
1452 if (may_bash && (r_ptr->flags2 & RF2_BASH_DOOR) && have_flag(f_ptr->flags, FF_BASH) &&
1453 (!is_pet(m_ptr) || (target_ptr->pet_extra_flags & PF_OPEN_DOORS)))
1455 /* Attempt to Bash */
1456 if (check_hp_for_feat_destruction(f_ptr, m_ptr) && (randint0(m_ptr->hp / 10) > f_ptr->power))
1458 if (have_flag(f_ptr->flags, FF_GLASS))
1459 msg_print(_("ガラスが砕ける音がした!", "You hear glass breaking!"));
1461 msg_print(_("ドアを叩き開ける音がした!", "You hear a door burst open!"));
1463 /* Disturb (sometimes) */
1464 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
1466 /* The door was bashed open */
1467 did_bash_door = TRUE;
1469 /* Hack -- fall into doorway */
1471 must_alter_to_move = TRUE;
1476 /* Deal with doors in the way */
1477 if (did_open_door || did_bash_door)
1479 /* Break down the door */
1480 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)))
1482 cave_alter_feat(target_ptr, ny, nx, FF_BASH);
1484 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1486 target_ptr->update |= (PU_FLOW);
1487 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1488 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1497 cave_alter_feat(target_ptr, ny, nx, FF_OPEN);
1500 f_ptr = &f_info[g_ptr->feat];
1502 /* Handle viewable doors */
1507 /* Hack -- check for Glyph of Warding */
1508 if (do_move && is_glyph_grid(g_ptr) &&
1509 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1511 /* Assume no move allowed */
1514 /* Break the ward */
1515 if (!is_pet(m_ptr) && (randint1(BREAK_GLYPH) < r_ptr->level))
1517 /* Describe observable breakage */
1518 if (g_ptr->info & CAVE_MARK)
1520 msg_print(_("守りのルーンが壊れた!", "The rune of protection is broken!"));
1523 /* Forget the rune */
1524 g_ptr->info &= ~(CAVE_MARK);
1526 /* Break the rune */
1527 g_ptr->info &= ~(CAVE_OBJECT);
1530 /* Allow movement */
1533 note_spot(target_ptr, ny, nx);
1536 else if (do_move && is_explosive_rune_grid(g_ptr) &&
1537 !((r_ptr->flags1 & RF1_NEVER_BLOW) && player_bold(target_ptr, ny, nx)))
1539 /* Assume no move allowed */
1542 /* Break the ward */
1545 /* Break the ward */
1546 if (randint1(BREAK_MINOR_GLYPH) > r_ptr->level)
1548 /* Describe observable breakage */
1549 if (g_ptr->info & CAVE_MARK)
1551 msg_print(_("ルーンが爆発した!", "The rune explodes!"));
1552 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);
1557 msg_print(_("爆発のルーンは解除された。", "An explosive rune was disarmed."));
1560 /* Forget the rune */
1561 g_ptr->info &= ~(CAVE_MARK);
1563 /* Break the rune */
1564 g_ptr->info &= ~(CAVE_OBJECT);
1567 note_spot(target_ptr, ny, nx);
1568 lite_spot(target_ptr, ny, nx);
1570 if (!monster_is_valid(m_ptr)) return;
1571 /* Allow movement */
1576 /* The player is in the way */
1577 if (do_move && player_bold(target_ptr, ny, nx))
1579 /* Some monsters never attack */
1580 if (r_ptr->flags1 & RF1_NEVER_BLOW)
1582 /* Hack -- memorize lack of attacks */
1583 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_BLOW);
1589 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1590 if (do_move && (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE))
1592 if (!MON_CONFUSED(m_ptr))
1594 if (!(r_ptr->flags2 & RF2_STUPID)) do_move = FALSE;
1597 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1602 /* The player is in the way. Attack him. */
1605 if (!target_ptr->riding || one_in_(2))
1608 (void)make_attack_normal(target_ptr, m_idx);
1619 /* A monster is in the way */
1620 if (do_move && g_ptr->m_idx)
1622 monster_race *z_ptr = &r_info[y_ptr->r_idx];
1624 /* Assume no movement */
1627 /* Attack 'enemies' */
1628 if (((r_ptr->flags2 & RF2_KILL_BODY) && !(r_ptr->flags1 & RF1_NEVER_BLOW) &&
1629 (r_ptr->mexp * r_ptr->level > z_ptr->mexp * z_ptr->level) &&
1630 can_cross && (g_ptr->m_idx != target_ptr->riding)) ||
1631 are_enemies(target_ptr, m_ptr, y_ptr) || MON_CONFUSED(m_ptr))
1633 if (!(r_ptr->flags1 & RF1_NEVER_BLOW))
1635 if (r_ptr->flags2 & RF2_KILL_BODY)
1637 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_BODY);
1641 if (y_ptr->r_idx && (y_ptr->hp >= 0))
1643 if (monst_attack_monst(target_ptr, m_idx, g_ptr->m_idx)) return;
1645 /* In anti-melee dungeon, stupid or confused monster takes useless turn */
1646 else if (d_info[target_ptr->dungeon_idx].flags1 & DF1_NO_MELEE)
1648 if (MON_CONFUSED(m_ptr)) return;
1649 else if (r_ptr->flags2 & RF2_STUPID)
1651 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_STUPID);
1659 /* Push past weaker monsters (unless leaving a wall) */
1660 else if ((r_ptr->flags2 & RF2_MOVE_BODY) && !(r_ptr->flags1 & RF1_NEVER_MOVE) &&
1661 (r_ptr->mexp > z_ptr->mexp) &&
1662 can_cross && (g_ptr->m_idx != target_ptr->riding) &&
1663 monster_can_cross_terrain(target_ptr, target_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].feat, z_ptr, 0))
1665 /* Allow movement */
1668 /* Monster pushed past another monster */
1669 did_move_body = TRUE;
1671 /* Wake up the moved monster */
1672 (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
1680 if (!target_ptr->riding_ryoute && !MON_MONFEAR(&target_ptr->current_floor_ptr->m_list[target_ptr->riding])) do_move = FALSE;
1683 if (did_kill_wall && do_move)
1685 if (one_in_(GRINDNOISE))
1687 if (have_flag(f_ptr->flags, FF_GLASS))
1688 msg_print(_("何かの砕ける音が聞こえる。", "There is a crashing sound."));
1690 msg_print(_("ギシギシいう音が聞こえる。", "There is a grinding sound."));
1693 cave_alter_feat(target_ptr, ny, nx, FF_HURT_DISI);
1695 if (!monster_is_valid(m_ptr)) /* Killed by shards of glass, etc. */
1697 target_ptr->update |= (PU_FLOW);
1698 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1699 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1704 f_ptr = &f_info[g_ptr->feat];
1706 /* Note changes to viewable region */
1711 if (must_alter_to_move && (r_ptr->flags7 & RF7_AQUATIC))
1713 if (!monster_can_cross_terrain(target_ptr, g_ptr->feat, r_ptr, is_riding_mon ? CEM_RIDING : 0))
1715 /* Assume no move allowed */
1721 * Check if monster can cross terrain
1722 * This is checked after the normal attacks
1723 * to allow monsters to attack an enemy,
1724 * even if it can't enter the terrain.
1726 if (do_move && !can_cross && !did_kill_wall && !did_bash_door)
1728 /* Assume no move allowed */
1732 /* Some monsters never move */
1733 if (do_move && (r_ptr->flags1 & RF1_NEVER_MOVE))
1735 /* Hack -- memorize lack of moves */
1736 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_NEVER_MOVE);
1742 /* Creature has been allowed move */
1751 if (have_flag(f_ptr->flags, FF_TREE))
1753 if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
1755 m_ptr->energy_need += ENERGY_NEED();
1761 /* Hack -- Update the old location */
1762 target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
1764 /* Mega-Hack -- move the old monster, if any */
1767 /* Move the old monster */
1771 /* Update the old monster */
1772 update_monster(target_ptr, g_ptr->m_idx, TRUE);
1775 /* Hack -- Update the new location */
1776 g_ptr->m_idx = m_idx;
1778 /* Move the monster */
1781 update_monster(target_ptr, m_idx, TRUE);
1783 lite_spot(target_ptr, oy, ox);
1784 lite_spot(target_ptr, ny, nx);
1788 /* sound(SOUND_WALK); */
1789 if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
1792 /* Possible disturb */
1795 (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
1796 (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
1798 if (is_hostile(m_ptr))
1799 disturb(target_ptr, FALSE, TRUE);
1802 /* Take or Kill objects on the floor */
1803 bool is_takable_or_killable = g_ptr->o_idx > 0;
1804 is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) != 0;
1805 bool is_pickup_items = (target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) != 0;
1806 is_pickup_items &= (r_ptr->flags2 & RF2_TAKE_ITEM) != 0;
1807 is_takable_or_killable &= !is_pet(m_ptr) || is_pickup_items;
1808 if (!is_takable_or_killable)
1814 OBJECT_IDX this_o_idx, next_o_idx;
1815 bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
1817 /* Scan all objects in the grid */
1818 for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1820 BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
1821 GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
1822 object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
1823 next_o_idx = o_ptr->next_o_idx;
1828 if (o_ptr->tval == TV_GOLD) continue;
1831 * Skip "real" corpses and statues, to avoid extreme
1832 * silliness like a novice rogue pockets full of statues
1835 if ((o_ptr->tval == TV_CORPSE) ||
1836 (o_ptr->tval == TV_STATUE)) continue;
1839 /* Extract some flags */
1840 object_flags(o_ptr, flgs);
1842 /* Acquire the object name */
1843 object_desc(target_ptr, o_name, o_ptr, 0);
1844 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_HIDDEN);
1846 /* React to objects that hurt the monster */
1847 if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
1848 if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
1849 if (have_flag(flgs, TR_SLAY_TROLL)) flg3 |= (RF3_TROLL);
1850 if (have_flag(flgs, TR_KILL_TROLL)) flg3 |= (RF3_TROLL);
1851 if (have_flag(flgs, TR_SLAY_GIANT)) flg3 |= (RF3_GIANT);
1852 if (have_flag(flgs, TR_KILL_GIANT)) flg3 |= (RF3_GIANT);
1853 if (have_flag(flgs, TR_SLAY_ORC)) flg3 |= (RF3_ORC);
1854 if (have_flag(flgs, TR_KILL_ORC)) flg3 |= (RF3_ORC);
1855 if (have_flag(flgs, TR_SLAY_DEMON)) flg3 |= (RF3_DEMON);
1856 if (have_flag(flgs, TR_KILL_DEMON)) flg3 |= (RF3_DEMON);
1857 if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
1858 if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
1859 if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
1860 if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
1861 if (have_flag(flgs, TR_SLAY_EVIL)) flg3 |= (RF3_EVIL);
1862 if (have_flag(flgs, TR_KILL_EVIL)) flg3 |= (RF3_EVIL);
1863 if (have_flag(flgs, TR_SLAY_HUMAN)) flg2 |= (RF2_HUMAN);
1864 if (have_flag(flgs, TR_KILL_HUMAN)) flg2 |= (RF2_HUMAN);
1865 if (have_flag(flgs, TR_BRAND_ACID)) flgr |= (RFR_IM_ACID);
1866 if (have_flag(flgs, TR_BRAND_ELEC)) flgr |= (RFR_IM_ELEC);
1867 if (have_flag(flgs, TR_BRAND_FIRE)) flgr |= (RFR_IM_FIRE);
1868 if (have_flag(flgs, TR_BRAND_COLD)) flgr |= (RFR_IM_COLD);
1869 if (have_flag(flgs, TR_BRAND_POIS)) flgr |= (RFR_IM_POIS);
1871 /* The object cannot be picked up by the monster */
1872 if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
1873 ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
1875 /* Only give a message for "take_item" */
1876 if (do_take && (r_ptr->flags2 & RF2_STUPID))
1878 did_take_item = TRUE;
1880 /* Describe observable situations */
1881 if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
1883 msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
1888 /* Pick up the item */
1891 did_take_item = TRUE;
1893 /* Describe observable situations */
1894 if (player_can_see_bold(target_ptr, ny, nx))
1896 msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
1899 /* Excise the object */
1900 excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
1903 o_ptr->marked &= OM_TOUCHED;
1905 /* Forget location */
1906 o_ptr->iy = o_ptr->ix = 0;
1908 /* Memorize monster */
1909 o_ptr->held_m_idx = m_idx;
1912 o_ptr->next_o_idx = m_ptr->hold_o_idx;
1915 m_ptr->hold_o_idx = this_o_idx;
1918 /* Destroy the item if not a pet */
1919 else if (!is_pet(m_ptr))
1921 did_kill_item = TRUE;
1923 /* Describe observable situations */
1924 if (player_has_los_bold(target_ptr, ny, nx))
1926 msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
1929 delete_object_idx(target_ptr, this_o_idx);
1937 * Forward movements failed, but now received LOS attack!
1938 * Try to flow by smell.
1940 if (target_ptr->no_flowed && i > 2 && m_ptr->target_y)
1941 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
1943 /* If we haven't done anything, try casting a spell again */
1944 if (!do_turn && !do_move && !MON_MONFEAR(m_ptr) && !is_riding_mon && aware)
1946 /* Try to cast spell again */
1947 if (r_ptr->freq_spell && randint1(100) <= r_ptr->freq_spell)
1949 if (make_attack_spell(m_idx, target_ptr)) return;
1953 /* Notice changes in view */
1956 target_ptr->update |= (PU_FLOW);
1957 target_ptr->window |= (PW_OVERHEAD | PW_DUNGEON);
1960 /* Notice changes in view */
1961 if (do_move && ((r_ptr->flags7 & (RF7_SELF_LD_MASK | RF7_HAS_DARK_1 | RF7_HAS_DARK_2))
1962 || ((r_ptr->flags7 & (RF7_HAS_LITE_1 | RF7_HAS_LITE_2)) && !target_ptr->phase_out)))
1964 target_ptr->update |= (PU_MON_LITE);
1967 /* Learn things from observable monster */
1968 if (is_original_ap_and_seen(target_ptr, m_ptr))
1970 /* Monster opened a door */
1971 if (did_open_door) r_ptr->r_flags2 |= (RF2_OPEN_DOOR);
1973 /* Monster bashed a door */
1974 if (did_bash_door) r_ptr->r_flags2 |= (RF2_BASH_DOOR);
1976 /* Monster tried to pick something up */
1977 if (did_take_item) r_ptr->r_flags2 |= (RF2_TAKE_ITEM);
1979 /* Monster tried to crush something */
1980 if (did_kill_item) r_ptr->r_flags2 |= (RF2_KILL_ITEM);
1982 /* Monster pushed past another monster */
1983 if (did_move_body) r_ptr->r_flags2 |= (RF2_MOVE_BODY);
1985 /* Monster passed through a wall */
1986 if (did_pass_wall) r_ptr->r_flags2 |= (RF2_PASS_WALL);
1988 /* Monster destroyed a wall */
1989 if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
1992 bool is_battle_determined = !do_turn && !do_move && MON_MONFEAR(m_ptr) && aware;
1993 if (!is_battle_determined) return;
1995 /* No longer afraid */
1996 (void)set_monster_monfear(target_ptr, m_idx, 0);
1998 /* Message if seen */
2001 GAME_TEXT m_name[MAX_NLEN];
2002 monster_desc(target_ptr, m_name, m_ptr, 0);
2003 msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
2006 if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
2011 * @brief 召喚の親元が消滅した時、子供も消滅させる
2012 * @param target_ptr プレーヤーへの参照ポインタ
2013 * @param m_idx モンスターID
2014 * @param see_m モンスターが視界内にいたらTRUE
2015 * @return 召喚モンスターが消滅したらTRUE
2017 bool vanish_summoned_children(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2019 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2020 if ((m_ptr->parent_m_idx == 0) || target_ptr->current_floor_ptr->m_list[m_ptr->parent_m_idx].r_idx)
2025 GAME_TEXT m_name[MAX_NLEN];
2026 monster_desc(target_ptr, m_name, m_ptr, 0);
2027 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2030 if (record_named_pet && is_pet(m_ptr) && m_ptr->nickname)
2032 GAME_TEXT m_name[MAX_NLEN];
2033 monster_desc(target_ptr, m_name, m_ptr, MD_INDEF_VISIBLE);
2034 exe_write_diary(target_ptr, DIARY_NAMED_PET, RECORD_NAMED_PET_LOSE_PARENT, m_name);
2037 delete_monster_idx(target_ptr, m_idx);
2043 * @brief 寝ているモンスターの起床を判定する
2044 * @param target_ptr プレーヤーへの参照ポインタ
2045 * @param m_idx モンスターID
2048 void awake_monster(player_type *target_ptr, MONSTER_IDX m_idx)
2050 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2051 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2052 if (!MON_CSLEEP(m_ptr)) return;
2053 if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
2055 (void)set_monster_csleep(target_ptr, m_idx, 0);
2058 GAME_TEXT m_name[MAX_NLEN];
2059 monster_desc(target_ptr, m_name, m_ptr, 0);
2060 msg_format(_("%^sが目を覚ました。", "%^s wakes up."), m_name);
2063 if (is_original_ap_and_seen(target_ptr, m_ptr) && (r_ptr->r_wake < MAX_UCHAR))
2071 * @brief モンスターの怒り状態を判定する (起こっていたら敵に回す)
2072 * @param target_ptr プレーヤーへの参照ポインタ
2073 * @param m_idx モンスターID
2074 * @param see_m モンスターが視界内にいたらTRUE
2077 void process_angar(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2079 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2080 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2081 bool gets_angry = FALSE;
2082 if (is_friendly(m_ptr) && (target_ptr->cursed & TRC_AGGRAVATE))
2085 if (is_pet(m_ptr) && ((((r_ptr->flags1 & RF1_UNIQUE) || (r_ptr->flags7 & RF7_NAZGUL)) &&
2086 monster_has_hostile_align(target_ptr, NULL, 10, -10, r_ptr)) || (r_ptr->flagsr & RFR_RES_ALL)))
2091 if (target_ptr->phase_out || !gets_angry) return;
2093 if (is_pet(m_ptr) || see_m)
2095 GAME_TEXT m_name[MAX_NLEN];
2096 monster_desc(target_ptr, m_name, m_ptr, is_pet(m_ptr) ? MD_ASSUME_VISIBLE : 0);
2097 msg_format(_("%^sは突然敵にまわった!", "%^s suddenly becomes hostile!"), m_name);
2100 set_hostile(target_ptr, m_ptr);
2105 * @brief 量子生物の量子的効果を実行する
2106 * @param target_ptr プレーヤーへの参照ポインタ
2107 * @param m_idx モンスターID
2108 * @param see_m モンスターが視界内にいたらTRUE
2109 * @return モンスターが量子的効果により消滅したらTRUE
2111 bool process_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2113 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2114 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2115 if ((r_ptr->flags2 & RF2_QUANTUM) == 0) return FALSE;
2116 if (!randint0(2)) return FALSE;
2117 if (randint0((m_idx % 100) + 10)) return FALSE;
2119 bool can_disappear = (r_ptr->flags1 & RF1_UNIQUE) == 0;
2120 can_disappear &= (r_ptr->flags1 & RF1_QUESTOR) == 0;
2123 vanish_nonunique(target_ptr, m_idx, see_m);
2127 produce_quantum_effect(target_ptr, m_idx, see_m);
2133 * @brief ユニークでない量子生物を消滅させる
2134 * @param target_ptr プレーヤーへの参照ポインタ
2135 * @param m_idx モンスターID
2136 * @param see_m モンスターが視界内にいたらTRUE
2139 void vanish_nonunique(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2141 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2144 GAME_TEXT m_name[MAX_NLEN];
2145 monster_desc(target_ptr, m_name, m_ptr, 0);
2146 msg_format(_("%sは消え去った!", "%^s disappears!"), m_name);
2149 monster_death(target_ptr, m_idx, FALSE);
2150 delete_monster_idx(target_ptr, m_idx);
2151 if (is_pet(m_ptr) && !(m_ptr->ml))
2153 msg_print(_("少しの間悲しい気分になった。", "You feel sad for a moment."));
2159 * @brief 量子生物ユニークの量子的効果 (ショート・テレポートまたは距離10のテレポート・アウェイ)を実行する
2160 * @param target_ptr プレーヤーへの参照ポインタ
2161 * @param m_idx モンスターID
2162 * @param see_m モンスターが視界内にいたらTRUE
2165 * プレーヤーが量子生物を観測しているか、量子生物がプレーヤーを観測している場合、互いの相対的な位置を確定させる
2166 * 波動関数の収縮はテレポートではないので反テレポート無効
2167 * todo パターンは収縮どころか拡散しているが、この際気にしてはいけない
2169 void produce_quantum_effect(player_type *target_ptr, MONSTER_IDX m_idx, bool see_m)
2171 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2172 bool coherent = los(target_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x);
2173 if (!see_m && !coherent) return;
2177 GAME_TEXT m_name[MAX_NLEN];
2178 monster_desc(target_ptr, m_name, m_ptr, 0);
2179 msg_format(_("%sは量子的効果を起こした!", "%^s produced a decoherence!"), m_name);
2183 msg_print(_("量子的効果が起こった!", "A decoherence was produced!"));
2186 bool target = one_in_(2);
2187 const int blink = 32 * 5 + 4;
2190 (void)monspell_to_monster(target_ptr, blink, m_ptr->fy, m_ptr->fx, m_idx, m_idx);
2195 teleport_player_away(m_idx, target_ptr, 10, TRUE);
2200 void process_special(player_type *target_ptr, MONSTER_IDX m_idx)
2202 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2203 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2204 if ((r_ptr->a_ability_flags2 & RF6_SPECIAL) == 0) return;
2205 if (m_ptr->r_idx != MON_OHMU) return;
2206 if (target_ptr->current_floor_ptr->inside_arena || target_ptr->phase_out) return;
2207 if ((r_ptr->freq_spell == 0) || !(randint1(100) <= r_ptr->freq_spell)) return;
2210 DEPTH rlev = ((r_ptr->level >= 1) ? r_ptr->level : 1);
2211 BIT_FLAGS p_mode = is_pet(m_ptr) ? PM_FORCE_PET : 0L;
2213 for (int k = 0; k < A_MAX; k++)
2215 if (summon_specific(target_ptr, m_idx, m_ptr->fy, m_ptr->fx, rlev, SUMMON_MOLD, (PM_ALLOW_GROUP | p_mode)))
2217 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml) count++;
2221 if (count && is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags6 |= (RF6_SPECIAL);
2226 * @brief モンスターを喋らせたり足音を立てたりする
2227 * @param target_ptr プレーヤーへの参照ポインタ
2231 * @param aware 起きていればTRUE
2234 void process_speak_sound(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox, bool aware)
2236 if (target_ptr->phase_out) return;
2238 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2239 monster_race *ap_r_ptr = &r_info[m_ptr->ap_r_idx];
2240 if (m_ptr->ap_r_idx == MON_CYBER &&
2241 one_in_(CYBERNOISE) &&
2242 !m_ptr->ml && (m_ptr->cdis <= MAX_SIGHT))
2244 if (disturb_minor) disturb(target_ptr, FALSE, FALSE);
2245 msg_print(_("重厚な足音が聞こえた。", "You hear heavy steps."));
2248 if (((ap_r_ptr->flags2 & RF2_CAN_SPEAK) == 0) || !aware ||
2249 !one_in_(SPEAK_CHANCE) ||
2250 !player_has_los_bold(target_ptr, oy, ox) ||
2251 !projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
2254 GAME_TEXT m_name[MAX_NLEN];
2255 char monmessage[1024];
2259 monster_desc(target_ptr, m_name, m_ptr, 0);
2261 strcpy(m_name, _("それ", "It"));
2263 if (MON_MONFEAR(m_ptr))
2264 filename = _("monfear_j.txt", "monfear.txt");
2265 else if (is_pet(m_ptr))
2266 filename = _("monpet_j.txt", "monpet.txt");
2267 else if (is_friendly(m_ptr))
2268 filename = _("monfrien_j.txt", "monfrien.txt");
2270 filename = _("monspeak_j.txt", "monspeak.txt");
2272 if (get_rnd_line(filename, m_ptr->ap_r_idx, monmessage) == 0)
2274 msg_format(_("%^s%s", "%^s %s"), m_name, monmessage);
2280 * @brief モンスターを分裂させるかどうかを決定する (分裂もさせる)
2281 * @param target_ptr プレーヤーへの参照ポインタ
2282 * @param m_idx モンスターID
2283 * @param oy 分裂元モンスターのY座標
2284 * @param ox 分裂元モンスターのX座標
2285 * @return 実際に分裂したらTRUEを返す
2287 bool decide_monster_multiplication(player_type *target_ptr, MONSTER_IDX m_idx, POSITION oy, POSITION ox)
2289 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2290 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2291 if (((r_ptr->flags2 & RF2_MULTIPLY) == 0) && (target_ptr->current_floor_ptr->num_repro >= MAX_REPRO))
2295 for (POSITION y = oy - 1; y <= oy + 1; y++)
2297 for (POSITION x = ox - 1; x <= ox + 1; x++)
2299 if (!in_bounds2(target_ptr->current_floor_ptr, y, x)) continue;
2300 if (target_ptr->current_floor_ptr->grid_array[y][x].m_idx) k++;
2304 if (multiply_barrier(target_ptr, m_idx)) k = 8;
2306 if ((k < 4) && (!k || !randint0(k * MON_MULT_ADJ)))
2308 if (multiply_monster(target_ptr, m_idx, FALSE, (is_pet(m_ptr) ? PM_FORCE_PET : 0)))
2310 if (target_ptr->current_floor_ptr->m_list[hack_m_idx_ii].ml && is_original_ap_and_seen(target_ptr, m_ptr))
2312 r_ptr->r_flags2 |= (RF2_MULTIPLY);
2324 * @brief モンスターの移動パターンを決定する
2325 * @param target_ptr プレーヤーへの参照ポインタ
2327 * @param m_idx モンスターID
2328 * @param aware 起きていればTRUE
2329 * @return 移動先が存在すればTRUE
2331 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware)
2333 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2334 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2336 if (MON_CONFUSED(m_ptr) || !aware)
2338 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2342 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
2344 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
2346 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2350 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
2352 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50);
2354 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2358 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
2360 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
2362 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2366 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
2368 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2374 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
2375 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
2376 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
2377 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2378 if (!get_enemy_dir(target_ptr, m_idx, mm))
2380 if (avoid || lonely || distant)
2382 POSITION dis = target_ptr->pet_follow_distance;
2383 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
2385 target_ptr->pet_follow_distance = PET_SEEK_DIST;
2388 (void)get_moves(target_ptr, m_idx, mm);
2389 target_ptr->pet_follow_distance = (s16b)dis;
2396 if (!is_hostile(m_ptr))
2398 mm[0] = mm[1] = mm[2] = mm[3] = 5;
2399 get_enemy_dir(target_ptr, m_idx, mm);
2403 if (!get_moves(target_ptr, m_idx, mm)) return FALSE;
2410 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
2411 * @param target_ptr プレーヤーへの参照ポインタ
2412 * @param m_idx モンスターID
2413 * @param is_riding_mon 騎乗状態ならばTRUE
2414 * @param see_m モンスターが視界内にいたらTRUE
2415 * @return モンスターがフロアから消えたらTRUE
2417 bool runaway_monster(player_type *target_ptr, MONSTER_IDX m_idx, bool is_riding_mon, bool see_m)
2419 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2420 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2421 bool can_runaway = is_pet(m_ptr) || is_friendly(m_ptr);
2422 can_runaway &= ((r_ptr->flags1 & RF1_UNIQUE) != 0) || ((r_ptr->flags7 & RF7_NAZGUL) != 0);
2423 can_runaway &= !target_ptr->phase_out;
2424 if (!can_runaway) return FALSE;
2426 static int riding_pinch = 0;
2428 if (m_ptr->hp >= m_ptr->maxhp / 3)
2430 /* Reset the counter */
2431 if (is_riding_mon) riding_pinch = 0;
2436 GAME_TEXT m_name[MAX_NLEN];
2437 monster_desc(target_ptr, m_name, m_ptr, 0);
2438 if (is_riding_mon && riding_pinch < 2)
2440 msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
2441 "%^s seems to be in so much pain and tries to escape from your restriction."), m_name);
2443 disturb(target_ptr, TRUE, TRUE);
2449 msg_format(_("%sはあなたの束縛から脱出した。", "%^s succeeded to escape from your restriction!"), m_name);
2450 if (rakuba(target_ptr, -1, FALSE))
2452 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2458 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) &&
2459 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))
2461 msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
2464 msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
2465 msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
2468 if (is_riding_mon && rakuba(target_ptr, -1, FALSE))
2470 msg_print(_("地面に落とされた。", "You have fallen from the pet you were riding."));
2473 check_quest_completion(target_ptr, m_ptr);
2474 delete_monster_idx(target_ptr, m_idx);
2480 * @brief モンスターに魔法を試行させる
2481 * @param target_ptr プレーヤーへの参照ポインタ
2482 * @param m_idx モンスターID
2483 * @param aware 起きていればTRUE
2484 * @return 魔法を唱えられなければ強制的にFALSE、その後モンスターが実際に魔法を唱えればTRUE
2486 bool cast_spell(player_type *target_ptr, MONSTER_IDX m_idx, bool aware)
2488 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
2489 monster_race *r_ptr = &r_info[m_ptr->r_idx];
2490 if ((r_ptr->freq_spell == 0) || !randint1(100) <= r_ptr->freq_spell)
2493 bool counterattack = FALSE;
2494 if (m_ptr->target_y)
2496 MONSTER_IDX t_m_idx = target_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
2497 if (t_m_idx && are_enemies(target_ptr, m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
2498 projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
2500 counterattack = TRUE;
2506 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2507 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2511 if (aware && make_attack_spell(m_idx, target_ptr)) return TRUE;
2512 if (monst_spell_monst(target_ptr, m_idx)) return TRUE;
2520 * @brief 全モンスターのターン管理メインルーチン /
2521 * Process all the "live" monsters, once per game turn.
2524 * During each game current game turn, we scan through the list of all the "live" monsters,\n
2525 * (backwards, so we can excise any "freshly dead" monsters), energizing each\n
2526 * monster, and allowing fully energized monsters to move, attack, pass, etc.\n
2528 * Note that monsters can never move in the monster array (except when the\n
2529 * "compact_monsters()" function is called by "dungeon()" or "save_player()").\n
2531 * This function is responsible for at least half of the processor time\n
2532 * on a normal system with a "normal" amount of monsters and a player doing\n
2535 * When the player is resting, virtually 90% of the processor time is spent\n
2536 * in this function, and its children, "process_monster()" and "make_move()".\n
2538 * Most of the rest of the time is spent in "update_view()" and "lite_spot()",\n
2539 * especially when the player is running.\n
2541 * Note the special "MFLAG_BORN" flag, which allows us to ignore "fresh"\n
2542 * monsters while they are still being "born". A monster is "fresh" only\n
2543 * during the game turn in which it is created, and we use the "hack_m_idx" to\n
2544 * determine if the monster is yet to be processed during the game turn.\n
2546 * Note the special "MFLAG_NICE" flag, which allows the player to get one\n
2547 * move before any "nasty" monsters get to use their spell attacks.\n
2549 * Note that when the "knowledge" about the currently tracked monster\n
2550 * changes (flags, attacks, spells), we induce a redraw of the monster\n
2553 void process_monsters(player_type *target_ptr)
2555 BIT_FLAGS old_r_flags1 = 0L;
2556 BIT_FLAGS old_r_flags2 = 0L;
2557 BIT_FLAGS old_r_flags3 = 0L;
2558 BIT_FLAGS old_r_flags4 = 0L;
2559 BIT_FLAGS old_r_flags5 = 0L;
2560 BIT_FLAGS old_r_flags6 = 0L;
2561 BIT_FLAGS old_r_flagsr = 0L;
2563 byte old_r_blows0 = 0;
2564 byte old_r_blows1 = 0;
2565 byte old_r_blows2 = 0;
2566 byte old_r_blows3 = 0;
2568 /* Clear monster fighting indicator */
2569 floor_type *floor_ptr = target_ptr->current_floor_ptr;
2570 floor_ptr->monster_noise = FALSE;
2572 /* Memorize old race */
2573 MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
2575 /* Acquire knowledge */
2576 byte old_r_cast_spell = 0;
2577 if (target_ptr->monster_race_idx)
2579 /* Acquire current monster */
2580 monster_race *r_ptr;
2581 r_ptr = &r_info[target_ptr->monster_race_idx];
2583 /* Memorize flags */
2584 old_r_flags1 = r_ptr->r_flags1;
2585 old_r_flags2 = r_ptr->r_flags2;
2586 old_r_flags3 = r_ptr->r_flags3;
2587 old_r_flags4 = r_ptr->r_flags4;
2588 old_r_flags5 = r_ptr->r_flags5;
2589 old_r_flags6 = r_ptr->r_flags6;
2590 old_r_flagsr = r_ptr->r_flagsr;
2592 /* Memorize blows */
2593 old_r_blows0 = r_ptr->r_blows[0];
2594 old_r_blows1 = r_ptr->r_blows[1];
2595 old_r_blows2 = r_ptr->r_blows[2];
2596 old_r_blows3 = r_ptr->r_blows[3];
2598 /* Memorize castings */
2599 old_r_cast_spell = r_ptr->r_cast_spell;
2602 /* Process the monsters (backwards) */
2604 for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
2606 monster_type *m_ptr;
2607 monster_race *r_ptr;
2608 m_ptr = &floor_ptr->m_list[i];
2609 r_ptr = &r_info[m_ptr->r_idx];
2611 /* Handle "leaving" */
2612 if (target_ptr->leaving) break;
2614 /* Ignore "dead" monsters */
2615 if (!monster_is_valid(m_ptr)) continue;
2617 if (target_ptr->wild_mode) continue;
2620 /* Handle "fresh" monsters */
2621 if (m_ptr->mflag & MFLAG_BORN)
2623 /* No longer "fresh" */
2624 m_ptr->mflag &= ~(MFLAG_BORN);
2630 /* Hack -- Require proximity */
2631 if (m_ptr->cdis >= AAF_LIMIT) continue;
2633 POSITION fx = m_ptr->fx;
2634 POSITION fy = m_ptr->fy;
2636 /* Flow by smell is allowed */
2637 if (!target_ptr->no_flowed)
2639 m_ptr->mflag2 &= ~MFLAG2_NOFLOW;
2642 /* Assume no move */
2645 /* Handle "sensing radius" */
2646 if (m_ptr->cdis <= (is_pet(m_ptr) ? (r_ptr->aaf > MAX_SIGHT ? MAX_SIGHT : r_ptr->aaf) : r_ptr->aaf))
2648 /* We can "sense" the player */
2652 /* Handle "sight" and "aggravation" */
2653 else if ((m_ptr->cdis <= MAX_SIGHT || target_ptr->phase_out) &&
2654 (player_has_los_bold(target_ptr, fy, fx) || (target_ptr->cursed & TRC_AGGRAVATE)))
2656 /* We can "see" or "feel" the player */
2660 else if (m_ptr->target_y) test = TRUE;
2663 if (!test) continue;
2666 if (target_ptr->riding == i)
2667 speed = target_ptr->pspeed;
2670 speed = m_ptr->mspeed;
2672 /* Monsters move quickly in Nightmare mode */
2673 if (ironman_nightmare) speed += 5;
2675 if (MON_FAST(m_ptr)) speed += 10;
2676 if (MON_SLOW(m_ptr)) speed -= 10;
2679 /* Give this monster some energy */
2680 m_ptr->energy_need -= SPEED_TO_ENERGY(speed);
2682 /* Not enough energy to move */
2683 if (m_ptr->energy_need > 0) continue;
2685 /* Use up "some" energy */
2686 m_ptr->energy_need += ENERGY_NEED();
2688 /* Save global index */
2691 /* Process the monster */
2692 process_monster(target_ptr, i);
2694 reset_target(m_ptr);
2696 /* Give up flow_by_smell when it might useless */
2697 if (target_ptr->no_flowed && one_in_(3))
2698 m_ptr->mflag2 |= MFLAG2_NOFLOW;
2700 /* Hack -- notice death or departure */
2701 if (!target_ptr->playing || target_ptr->is_dead) break;
2703 /* Notice leaving */
2704 if (target_ptr->leaving) break;
2707 /* Reset global index */
2711 /* Tracking a monster race (the same one we were before) */
2712 if (!target_ptr->monster_race_idx || (target_ptr->monster_race_idx != old_monster_race_idx))
2715 /* Acquire monster race */
2716 monster_race *r_ptr;
2717 r_ptr = &r_info[target_ptr->monster_race_idx];
2719 /* Check for knowledge change */
2720 if ((old_r_flags1 != r_ptr->r_flags1) ||
2721 (old_r_flags2 != r_ptr->r_flags2) ||
2722 (old_r_flags3 != r_ptr->r_flags3) ||
2723 (old_r_flags4 != r_ptr->r_flags4) ||
2724 (old_r_flags5 != r_ptr->r_flags5) ||
2725 (old_r_flags6 != r_ptr->r_flags6) ||
2726 (old_r_flagsr != r_ptr->r_flagsr) ||
2727 (old_r_blows0 != r_ptr->r_blows[0]) ||
2728 (old_r_blows1 != r_ptr->r_blows[1]) ||
2729 (old_r_blows2 != r_ptr->r_blows[2]) ||
2730 (old_r_blows3 != r_ptr->r_blows[3]) ||
2731 (old_r_cast_spell != r_ptr->r_cast_spell))
2733 target_ptr->window |= (PW_MONSTER);