OSDN Git Service

[Refactor] #38997 set_monster_*() にplayer_type * 引数を追加 / Added player_type * argument...
[hengband/hengband.git] / src / monster-process.c
index ebd7705..d43d40d 100644 (file)
 /*!
  * @brief モンスターが敵に接近するための方向を決める /
  * Calculate the direction to the next enemy
+ * @param target_ptr プレーヤーへの参照ポインタ
  * @param m_idx モンスターの参照ID
  * @param mm 移動するべき方角IDを返す参照ポインタ
  * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
  */
-static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
+static bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
 {
-       int i;
-       POSITION x = 0, y = 0;
-       MONSTER_IDX t_idx;
-       int start;
-       int plus = 1;
-
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
        monster_race *r_ptr = &r_info[m_ptr->r_idx];
        monster_type *t_ptr;
 
-       if (p_ptr->riding_t_m_idx && player_bold(p_ptr, m_ptr->fy, m_ptr->fx))
+       POSITION x = 0, y = 0;
+       if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
        {
-               y = p_ptr->current_floor_ptr->m_list[p_ptr->riding_t_m_idx].fy;
-               x = p_ptr->current_floor_ptr->m_list[p_ptr->riding_t_m_idx].fx;
+               y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
+               x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
        }
-       else if (is_pet(m_ptr) && p_ptr->pet_t_m_idx)
+       else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
        {
-               y = p_ptr->current_floor_ptr->m_list[p_ptr->pet_t_m_idx].fy;
-               x = p_ptr->current_floor_ptr->m_list[p_ptr->pet_t_m_idx].fx;
+               y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
+               x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
        }
        else
        {
-               if (p_ptr->phase_out)
+               int start;
+               int plus = 1;
+               if (target_ptr->phase_out)
                {
-                       start = randint1(p_ptr->current_floor_ptr->m_max-1)+p_ptr->current_floor_ptr->m_max;
+                       start = randint1(floor_ptr->m_max-1)+floor_ptr->m_max;
                        if(randint0(2)) plus = -1;
                }
-               else start = p_ptr->current_floor_ptr->m_max + 1;
+               else start = floor_ptr->m_max + 1;
 
                /* Scan thru all monsters */
-               for (i = start; ((i < start + p_ptr->current_floor_ptr->m_max) && (i > start - p_ptr->current_floor_ptr->m_max)); i+=plus)
+               for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i+=plus)
                {
-                       MONSTER_IDX dummy = (i % p_ptr->current_floor_ptr->m_max);
+                       MONSTER_IDX dummy = (i % floor_ptr->m_max);
 
                        if (!dummy) continue;
 
-                       t_idx = dummy;
-                       t_ptr = &p_ptr->current_floor_ptr->m_list[t_idx];
+                       MONSTER_IDX t_idx = dummy;
+                       t_ptr = &floor_ptr->m_list[t_idx];
 
                        /* The monster itself isn't a target */
                        if (t_ptr == m_ptr) continue;
@@ -97,16 +96,16 @@ static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
                        if (is_pet(m_ptr))
                        {
                                /* Hack -- only fight away from player */
-                               if (p_ptr->pet_follow_distance < 0)
+                               if (target_ptr->pet_follow_distance < 0)
                                {
                                        /* No fighting near player */
-                                       if (t_ptr->cdis <= (0 - p_ptr->pet_follow_distance))
+                                       if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
                                        {
                                                continue;
                                        }
                                }
                                /* Hack -- no fighting away from player */
-                               else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > p_ptr->pet_follow_distance))
+                               else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
                                {
                                        continue;
                                }
@@ -118,14 +117,14 @@ static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
                        if (!are_enemies(m_ptr, t_ptr)) continue;
 
                        /* Monster must be projectable if we can't pass through walls */
-                       if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) ||
-                           ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)))
+                       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)))
                        {
-                               if (!in_disintegration_range(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
+                               if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
                        }
                        else
                        {
-                               if (!projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
+                               if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
                        }
 
                        /* OK -- we've got a target */
@@ -134,6 +133,7 @@ static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
 
                        break;
                }
+
                if (!x && !y) return FALSE;
        }
 
@@ -198,7 +198,6 @@ static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
                mm[2] = 2;
        }
 
-       /* Found a monster */
        return TRUE;
 }
 
@@ -218,9 +217,9 @@ static bool get_enemy_dir(MONSTER_IDX m_idx, int *mm)
  * Note that this function is responsible for about one to five percent\n
  * of the processor use in normal conditions...\n
  */
-static bool mon_will_run(MONSTER_IDX m_idx)
+static bool mon_will_run(player_type *target_ptr, MONSTER_IDX m_idx)
 {
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
 
 #ifdef ALLOW_TERROR
 
@@ -238,34 +237,34 @@ static bool mon_will_run(MONSTER_IDX m_idx)
        if (is_pet(m_ptr))
        {
                /* Are we trying to avoid the player? */
-               return ((p_ptr->pet_follow_distance < 0) &&
-                                 (m_ptr->cdis <= (0 - p_ptr->pet_follow_distance)));
+               return ((target_ptr->pet_follow_distance < 0) &&
+                                 (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
        }
 
        /* Keep monsters from running too far away */
-       if (m_ptr->cdis > MAX_SIGHT + 5) return (FALSE);
+       if (m_ptr->cdis > MAX_SIGHT + 5) return FALSE;
 
        /* All "afraid" monsters will run away */
-       if (MON_MONFEAR(m_ptr)) return (TRUE);
+       if (MON_MONFEAR(m_ptr)) return TRUE;
 
 #ifdef ALLOW_TERROR
 
        /* Nearby monsters will not become terrified */
-       if (m_ptr->cdis <= 5) return (FALSE);
+       if (m_ptr->cdis <= 5) return FALSE;
 
        /* Examine player power (level) */
-       p_lev = p_ptr->lev;
+       p_lev = target_ptr->lev;
 
        /* Examine monster power (level plus morale) */
        m_lev = r_ptr->level + (m_idx & 0x08) + 25;
 
        /* Optimize extreme cases below */
-       if (m_lev > p_lev + 4) return (FALSE);
-       if (m_lev + 4 <= p_lev) return (TRUE);
+       if (m_lev > p_lev + 4) return FALSE;
+       if (m_lev + 4 <= p_lev) return TRUE;
 
        /* Examine player health */
-       p_chp = p_ptr->chp;
-       p_mhp = p_ptr->mhp;
+       p_chp = target_ptr->chp;
+       p_mhp = target_ptr->mhp;
 
        /* Examine monster health */
        m_chp = m_ptr->hp;
@@ -276,72 +275,69 @@ static bool mon_will_run(MONSTER_IDX m_idx)
        m_val = (m_lev * m_mhp) + (m_chp << 2); /* div m_mhp */
 
        /* Strong players scare strong monsters */
-       if (p_val * m_mhp > m_val * p_mhp) return (TRUE);
+       if (p_val * m_mhp > m_val * p_mhp) return TRUE;
 
 #endif
 
-       /* Assume no terror */
-       return (FALSE);
+       return FALSE;
 }
 
 
 /*!
  * @brief モンスターがプレイヤーに向けて遠距離攻撃を行うことが可能なマスを走査する /
  * Search spell castable grid
+ * @param target_ptr プレーヤーへの参照ポインタ
  * @param m_idx モンスターの参照ID
  * @param yp 適したマスのY座標を返す参照ポインタ
  * @param xp 適したマスのX座標を返す参照ポインタ
  * @return 有効なマスがあった場合TRUEを返す
  */
-static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
+static bool get_moves_aux2(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
 {
-       int i, best = 999;
-       POSITION y, x, y1, x1;
-
-       grid_type *g_ptr;
-       bool can_open_door = FALSE;
-       int now_cost;
-
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
        monster_race *r_ptr = &r_info[m_ptr->r_idx];
 
        /* Monster location */
-       y1 = m_ptr->fy;
-       x1 = m_ptr->fx;
+       POSITION y1 = m_ptr->fy;
+       POSITION x1 = m_ptr->fx;
 
        /* Monster can already cast spell to player */
-       if (projectable(p_ptr->current_floor_ptr, y1, x1, p_ptr->y, p_ptr->x)) return (FALSE);
+       if (projectable(target_ptr, y1, x1, target_ptr->y, target_ptr->x)) return FALSE;
 
        /* Set current grid cost */
-       now_cost = p_ptr->current_floor_ptr->grid_array[y1][x1].cost;
+       int now_cost = floor_ptr->grid_array[y1][x1].cost;
        if (now_cost == 0) now_cost = 999;
 
        /* Can monster bash or open doors? */
+       bool can_open_door = FALSE;
        if (r_ptr->flags2 & (RF2_BASH_DOOR | RF2_OPEN_DOOR))
        {
                can_open_door = TRUE;
        }
 
        /* Check nearby grids, diagonals first */
-       for (i = 7; i >= 0; i--)
+       int best = 999;
+       for (int i = 7; i >= 0; i--)
        {
                int cost;
 
-               y = y1 + ddy_ddd[i];
-               x = x1 + ddx_ddd[i];
+               POSITION y = y1 + ddy_ddd[i];
+               POSITION x = x1 + ddx_ddd[i];
 
                /* Ignore locations off of edge */
-               if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
+               if (!in_bounds2(floor_ptr, y, x)) continue;
 
                /* Simply move to player */
-               if (player_bold(p_ptr, y, x)) return (FALSE);
+               if (player_bold(target_ptr, y, x)) return FALSE;
 
-               g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
+               grid_type *g_ptr;
+               g_ptr = &floor_ptr->grid_array[y][x];
 
                cost = g_ptr->cost;
 
                /* Monster cannot kill or pass walls */
-               if (!(((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) || ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding))))
+               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))))
                {
                        if (cost == 0) continue;
                        if (!can_open_door && is_closed_door(g_ptr->feat)) continue;
@@ -352,7 +348,7 @@ static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
 
                if (now_cost < cost) continue;
 
-               if (!projectable(p_ptr->current_floor_ptr, y, x, p_ptr->y, p_ptr->x)) continue;
+               if (!projectable(target_ptr, y, x, target_ptr->y, target_ptr->x)) continue;
 
                /* Accept louder sounds */
                if (best < cost) continue;
@@ -362,11 +358,9 @@ static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
                (*xp) = x1 + ddx_ddd[i];
        }
 
-       /* No legal move (?) */
-       if (best == 999) return (FALSE);
+       if (best == 999) return FALSE;
 
-       /* Success */
-       return (TRUE);
+       return TRUE;
 }
 
 
@@ -398,15 +392,11 @@ static bool get_moves_aux2(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
  * being close enough to chase directly.  I have no idea what will\n
  * happen if you combine "smell" with low "aaf" values.\n
  */
-static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
+static bool get_moves_aux(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no_flow)
 {
-       int i, best;
-       POSITION y, x, y1, x1;
-
        grid_type *g_ptr;
-       bool use_scent = FALSE;
-
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
        monster_race *r_ptr = &r_info[m_ptr->r_idx];
 
        /* Can monster cast attack spell? */
@@ -415,27 +405,29 @@ static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no
            r_ptr->a_ability_flags2 & (RF6_ATTACK_MASK))
        {
                /* Can move spell castable grid? */
-               if (get_moves_aux2(m_idx, yp, xp)) return (TRUE);
+               if (get_moves_aux2(target_ptr, m_idx, yp, xp)) return TRUE;
        }
 
        /* Monster can't flow */
-       if (no_flow) return (FALSE);
+       if (no_flow) return FALSE;
 
        /* Monster can go through rocks */
-       if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall)) return (FALSE);
-       if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != p_ptr->riding)) return (FALSE);
+       if ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) return FALSE;
+       if ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)) return FALSE;
 
        /* Monster location */
-       y1 = m_ptr->fy;
-       x1 = m_ptr->fx;
+       POSITION y1 = m_ptr->fy;
+       POSITION x1 = m_ptr->fx;
 
        /* Hack -- Player can see us, run towards him */
-       if (player_has_los_bold(p_ptr, y1, x1) && projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y1, x1)) return (FALSE);
+       if (player_has_los_bold(target_ptr, y1, x1) && projectable(target_ptr, target_ptr->y, target_ptr->x, y1, x1)) return FALSE;
 
        /* Monster grid */
-       g_ptr = &p_ptr->current_floor_ptr->grid_array[y1][x1];
+       g_ptr = &floor_ptr->grid_array[y1][x1];
 
        /* If we can hear noises, advance towards them */
+       int best;
+       bool use_scent = FALSE;
        if (g_ptr->cost)
        {
                best = 999;
@@ -445,7 +437,7 @@ static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no
        else if (g_ptr->when)
        {
                /* Too old smell */
-               if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].when - g_ptr->when > 127) return (FALSE);
+               if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].when - g_ptr->when > 127) return FALSE;
 
                use_scent = TRUE;
                best = 0;
@@ -454,19 +446,19 @@ static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no
        /* Otherwise, advance blindly */
        else
        {
-               return (FALSE);
+               return FALSE;
        }
 
        /* Check nearby grids, diagonals first */
-       for (i = 7; i >= 0; i--)
+       for (int i = 7; i >= 0; i--)
        {
-               y = y1 + ddy_ddd[i];
-               x = x1 + ddx_ddd[i];
+               POSITION y = y1 + ddy_ddd[i];
+               POSITION x = x1 + ddx_ddd[i];
 
                /* Ignore locations off of edge */
-               if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
+               if (!in_bounds2(floor_ptr, y, x)) continue;
 
-               g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
+               g_ptr = &floor_ptr->grid_array[y][x];
 
                /* We're following a scent trail */
                if (use_scent)
@@ -493,15 +485,13 @@ static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no
                }
 
                /* Hack -- Save the "twiddled" location */
-               (*yp) = p_ptr->y + 16 * ddy_ddd[i];
-               (*xp) = p_ptr->x + 16 * ddx_ddd[i];
+               (*yp) = target_ptr->y + 16 * ddy_ddd[i];
+               (*xp) = target_ptr->x + 16 * ddx_ddd[i];
        }
 
-       /* No legal move (?) */
-       if (best == 999 || best == 0) return (FALSE);
+       if (best == 999 || best == 0) return FALSE;
 
-       /* Success */
-       return (TRUE);
+       return TRUE;
 }
 
 
@@ -517,38 +507,35 @@ static bool get_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp, bool no
  * but instead of heading directly for it, the monster should "swerve"\n
  * around the player so that he has a smaller chance of getting hit.\n
  */
-static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
+static bool get_fear_moves_aux(floor_type *floor_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
 {
-       POSITION y, x, y1, x1, fy, fx, gy = 0, gx = 0;
-       int score = -1;
-       int i;
-
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       POSITION gy = 0, gx = 0;
 
        /* Monster location */
-       fy = m_ptr->fy;
-       fx = m_ptr->fx;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
+       POSITION fy = m_ptr->fy;
+       POSITION fx = m_ptr->fx;
 
        /* Desired destination */
-       y1 = fy - (*yp);
-       x1 = fx - (*xp);
+       POSITION y1 = fy - (*yp);
+       POSITION x1 = fx - (*xp);
 
        /* Check nearby grids, diagonals first */
-       for (i = 7; i >= 0; i--)
+       int score = -1;
+       for (int i = 7; i >= 0; i--)
        {
                POSITION dis, s;
-
-               y = fy + ddy_ddd[i];
-               x = fx + ddx_ddd[i];
+               POSITION y = fy + ddy_ddd[i];
+               POSITION x = fx + ddx_ddd[i];
 
                /* Ignore locations off of edge */
-               if (!in_bounds2(p_ptr->current_floor_ptr, y, x)) continue;
+               if (!in_bounds2(floor_ptr, y, x)) continue;
 
                /* Calculate distance of this grid from our destination */
                dis = distance(y, x, y1, x1);
 
                /* Score this grid */
-               s = 5000 / (dis + 3) - 500 / (p_ptr->current_floor_ptr->grid_array[y][x].dist + 1);
+               s = 5000 / (dis + 3) - 500 / (floor_ptr->grid_array[y][x].dist + 1);
 
                /* No negative scores */
                if (s < 0) s = 0;
@@ -565,14 +552,14 @@ static bool get_fear_moves_aux(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
        }
 
        /* No legal move (?) */
-       if (score == -1) return (FALSE);
+       if (score == -1) return FALSE;
 
        /* Find deltas */
        (*yp) = fy - gy;
        (*xp) = fx - gx;
 
        /* Success */
-       return (TRUE);
+       return TRUE;
 }
 
 /*
@@ -690,6 +677,7 @@ static POSITION *dist_offsets_x[10] =
 /*!
  * @brief モンスターが逃げ込める安全な地点を返す /
  * Choose a "safe" location near a monster for it to run toward.
+ * @param target_ptr プレーヤーへの参照ポインタ
  * @param m_idx モンスターの参照ID
  * @param yp 移動先のマスのY座標を返す参照ポインタ
  * @param xp 移動先のマスのX座標を返す参照ポインタ
@@ -705,43 +693,42 @@ static POSITION *dist_offsets_x[10] =
  *\n
  * Return TRUE if a safe location is available.\n
  */
-static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
+static bool find_safety(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
 {
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
 
        POSITION fy = m_ptr->fy;
        POSITION fx = m_ptr->fx;
 
-       POSITION y, x, dy, dx, d, dis, i;
        POSITION gy = 0, gx = 0, gdis = 0;
 
-       POSITION *y_offsets;
-       POSITION *x_offsets;
-
-       grid_type *g_ptr;
-
        /* Start with adjacent locations, spread further */
-       for (d = 1; d < 10; d++)
+       for (POSITION d = 1; d < 10; d++)
        {
                /* Get the lists of points with a distance d from (fx, fy) */
+               POSITION *y_offsets;
                y_offsets = dist_offsets_y[d];
+
+               POSITION *x_offsets;
                x_offsets = dist_offsets_x[d];
 
                /* Check the locations */
-               for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
+               for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
                     dx != 0 || dy != 0;
                     i++, dx = x_offsets[i], dy = y_offsets[i])
                {
-                       y = fy + dy;
-                       x = fx + dx;
+                       POSITION y = fy + dy;
+                       POSITION x = fx + dx;
 
                        /* Skip illegal locations */
-                       if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
+                       if (!in_bounds(floor_ptr, y, x)) continue;
 
-                       g_ptr = &p_ptr->current_floor_ptr->grid_array[y][x];
+                       grid_type *g_ptr;
+                       g_ptr = &floor_ptr->grid_array[y][x];
 
                        /* Skip locations in a wall */
-                       if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == p_ptr->riding) ? CEM_RIDING : 0)) continue;
+                       if (!monster_can_cross_terrain(g_ptr->feat, &r_info[m_ptr->r_idx], (m_idx == target_ptr->riding) ? CEM_RIDING : 0)) continue;
 
                        /* Check for "availability" (if monsters can flow) */
                        if (!(m_ptr->mflag2 & MFLAG2_NOFLOW))
@@ -750,45 +737,41 @@ static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
                                if (g_ptr->dist == 0) continue;
 
                                /* Ignore too-distant grids */
-                               if (g_ptr->dist > p_ptr->current_floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
+                               if (g_ptr->dist > floor_ptr->grid_array[fy][fx].dist + 2 * d) continue;
                        }
 
                        /* Check for absence of shot (more or less) */
-                       if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x))
-                       {
-                               /* Calculate distance from player */
-                               dis = distance(y, x, p_ptr->y, p_ptr->x);
+                       if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x)) continue;
 
-                               /* Remember if further than previous */
-                               if (dis > gdis)
-                               {
-                                       gy = y;
-                                       gx = x;
-                                       gdis = dis;
-                               }
-                       }
+                       /* Calculate distance from player */
+                       POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
+
+                       /* Remember if further than previous */
+                       if (dis <= gdis) continue;
+
+                       gy = y;
+                       gx = x;
+                       gdis = dis;
                }
 
                /* Check for success */
-               if (gdis > 0)
-               {
-                       /* Good location */
-                       (*yp) = fy - gy;
-                       (*xp) = fx - gx;
+               if (gdis <= 0) continue;
 
-                       /* Found safe place */
-                       return (TRUE);
-               }
+               /* Good location */
+               (*yp) = fy - gy;
+               (*xp) = fx - gx;
+
+               return TRUE;
        }
 
-       /* No safe place */
-       return (FALSE);
+       return FALSE;
 }
 
 
 /*!
  * @brief モンスターが隠れ潜める地点を返す /
  * Choose a good hiding place near a monster for it to run toward.
+ * @param target_ptr プレーヤーへの参照ポインタ
  * @param m_idx モンスターの参照ID
  * @param yp 移動先のマスのY座標を返す参照ポインタ
  * @param xp 移動先のマスのX座標を返す参照ポインタ
@@ -799,104 +782,102 @@ static bool find_safety(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
  *\n
  * Return TRUE if a good location is available.\n
  */
-static bool find_hiding(MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
+static bool find_hiding(player_type *target_ptr, MONSTER_IDX m_idx, POSITION *yp, POSITION *xp)
 {
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
        monster_race *r_ptr = &r_info[m_ptr->r_idx];
 
        POSITION fy = m_ptr->fy;
        POSITION fx = m_ptr->fx;
 
-       POSITION y, x, dy, dx, d, dis, i;
        POSITION gy = 0, gx = 0, gdis = 999;
 
-       POSITION *y_offsets, *x_offsets;
-
        /* Start with adjacent locations, spread further */
-       for (d = 1; d < 10; d++)
+       for (POSITION d = 1; d < 10; d++)
        {
                /* Get the lists of points with a distance d from (fx, fy) */
+               POSITION *y_offsets;
                y_offsets = dist_offsets_y[d];
+
+               POSITION *x_offsets;
                x_offsets = dist_offsets_x[d];
 
                /* Check the locations */
-               for (i = 0, dx = x_offsets[0], dy = y_offsets[0];
+               for (POSITION i = 0, dx = x_offsets[0], dy = y_offsets[0];
                     dx != 0 || dy != 0;
                     i++, dx = x_offsets[i], dy = y_offsets[i])
                {
-                       y = fy + dy;
-                       x = fx + dx;
+                       POSITION y = fy + dy;
+                       POSITION x = fx + dx;
 
                        /* Skip illegal locations */
-                       if (!in_bounds(p_ptr->current_floor_ptr, y, x)) continue;
+                       if (!in_bounds(floor_ptr, y, x)) continue;
 
                        /* Skip occupied locations */
                        if (!monster_can_enter(y, x, r_ptr, 0)) continue;
 
                        /* Check for hidden, available grid */
-                       if (!projectable(p_ptr->current_floor_ptr, p_ptr->y, p_ptr->x, y, x) && clean_shot(fy, fx, y, x, FALSE))
-                       {
-                               /* Calculate distance from player */
-                               dis = distance(y, x, p_ptr->y, p_ptr->x);
+                       if (projectable(target_ptr, target_ptr->y, target_ptr->x, y, x) && clean_shot(target_ptr, fy, fx, y, x, FALSE))
+                               continue;
 
-                               /* Remember if closer than previous */
-                               if (dis < gdis && dis >= 2)
-                               {
-                                       gy = y;
-                                       gx = x;
-                                       gdis = dis;
-                               }
+                       /* Calculate distance from player */
+                       POSITION dis = distance(y, x, target_ptr->y, target_ptr->x);
+
+                       /* Remember if closer than previous */
+                       if (dis < gdis && dis >= 2)
+                       {
+                               gy = y;
+                               gx = x;
+                               gdis = dis;
                        }
                }
 
-               /* Check for success */
-               if (gdis < 999)
-               {
-                       /* Good location */
-                       (*yp) = fy - gy;
-                       (*xp) = fx - gx;
+               if (gdis >= 999) continue;
 
-                       /* Found good place */
-                       return (TRUE);
-               }
+               *yp = fy - gy;
+               *xp = fx - gx;
+
+               return TRUE;
        }
 
-       /* No good place */
-       return (FALSE);
+       return FALSE;
 }
 
 
 /*!
  * @brief モンスターの移動方向を返す /
  * Choose "logical" directions for monster movement
+ * @param target_ptr プレーヤーへの参照ポインタ
  * @param m_idx モンスターの参照ID
  * @param mm 移動方向を返す方向IDの参照ポインタ
  * @return 有効方向があった場合TRUEを返す
  */
-static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
+static bool get_moves(player_type *target_ptr, MONSTER_IDX m_idx, DIRECTION *mm)
 {
-       monster_type *m_ptr = &p_ptr->current_floor_ptr->m_list[m_idx];
+       floor_type *floor_ptr = target_ptr->current_floor_ptr;
+       monster_type *m_ptr = &floor_ptr->m_list[m_idx];
        monster_race *r_ptr = &r_info[m_ptr->r_idx];
-       POSITION     y = 0, ay, x = 0, ax;
-       int          move_val = 0;
-       POSITION     y2 = p_ptr->y;
-       POSITION     x2 = p_ptr->x;
-       bool         done = FALSE;
-       bool         will_run = mon_will_run(m_idx);
-       grid_type    *g_ptr;
-       bool         no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
-       bool         can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != p_ptr->riding) || p_ptr->pass_wall));
+       POSITION y = 0, ay, x = 0, ax;
+       int move_val = 0;
+       POSITION y2 = target_ptr->y;
+       POSITION x2 = target_ptr->x;
+       bool done = FALSE;
+       bool will_run = mon_will_run(target_ptr, m_idx);
+       grid_type *g_ptr;
+       bool no_flow = ((m_ptr->mflag2 & MFLAG2_NOFLOW) && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].cost > 2));
+       bool can_pass_wall = ((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall));
 
        /* Counter attack to an enemy monster */
        if (!will_run && m_ptr->target_y)
        {
-               int t_m_idx = p_ptr->current_floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
+               int t_m_idx = floor_ptr->grid_array[m_ptr->target_y][m_ptr->target_x].m_idx;
 
                /* The monster must be an enemy, and in LOS */
                if (t_m_idx &&
-                   are_enemies(m_ptr, &p_ptr->current_floor_ptr->m_list[t_m_idx]) &&
-                   los(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
-                   projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
+                   are_enemies(m_ptr, &floor_ptr->m_list[t_m_idx]) &&
+                   los(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x) &&
+                   projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
                {
                        /* Extract the "pseudo-direction" */
                        y = m_ptr->fy - m_ptr->target_y;
@@ -907,8 +888,8 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
 
        if (!done && !will_run && is_hostile(m_ptr) &&
            (r_ptr->flags1 & RF1_FRIENDS) &&
-           ((los(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x) && projectable(p_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, p_ptr->y, p_ptr->x)) ||
-           (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
+           ((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)) ||
+           (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < MAX_SIGHT / 2)))
        {
        /*
         * Animal packs try to get the player out of corridors
@@ -922,12 +903,12 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
                        /* Count room grids next to player */
                        for (i = 0; i < 8; i++)
                        {
-                               int xx = p_ptr->x + ddx_ddd[i];
-                               int yy = p_ptr->y + ddy_ddd[i];
+                               int xx = target_ptr->x + ddx_ddd[i];
+                               int yy = target_ptr->y + ddy_ddd[i];
 
-                               if (!in_bounds2(p_ptr->current_floor_ptr, yy, xx)) continue;
+                               if (!in_bounds2(floor_ptr, yy, xx)) continue;
 
-                               g_ptr = &p_ptr->current_floor_ptr->grid_array[yy][xx];
+                               g_ptr = &floor_ptr->grid_array[yy][xx];
 
                                /* Check grid */
                                if (monster_can_cross_terrain(g_ptr->feat, r_ptr, 0))
@@ -936,20 +917,20 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
                                        room++;
                                }
                        }
-                       if (p_ptr->current_floor_ptr->grid_array[p_ptr->y][p_ptr->x].info & CAVE_ROOM) room -= 2;
+                       if (floor_ptr->grid_array[target_ptr->y][target_ptr->x].info & CAVE_ROOM) room -= 2;
                        if (!r_ptr->flags4 && !r_ptr->a_ability_flags1 && !r_ptr->a_ability_flags2) room -= 2;
 
                        /* Not in a room and strong player */
-                       if (room < (8 * (p_ptr->chp + p_ptr->csp)) /
-                           (p_ptr->mhp + p_ptr->msp))
+                       if (room < (8 * (target_ptr->chp + target_ptr->csp)) /
+                           (target_ptr->mhp + target_ptr->msp))
                        {
                                /* Find hiding place */
-                               if (find_hiding(m_idx, &y, &x)) done = TRUE;
+                               if (find_hiding(target_ptr, m_idx, &y, &x)) done = TRUE;
                        }
                }
 
                /* Monster groups try to surround the player */
-               if (!done && (p_ptr->current_floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
+               if (!done && (floor_ptr->grid_array[m_ptr->fy][m_ptr->fx].dist < 3))
                {
                        int i;
 
@@ -957,20 +938,20 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
                        for (i = 0; i < 8; i++)
                        {
                                /* Pick squares near player (semi-randomly) */
-                               y2 = p_ptr->y + ddy_ddd[(m_idx + i) & 7];
-                               x2 = p_ptr->x + ddx_ddd[(m_idx + i) & 7];
+                               y2 = target_ptr->y + ddy_ddd[(m_idx + i) & 7];
+                               x2 = target_ptr->x + ddx_ddd[(m_idx + i) & 7];
 
                                /* Already there? */
                                if ((m_ptr->fy == y2) && (m_ptr->fx == x2))
                                {
                                        /* Attack the player */
-                                       y2 = p_ptr->y;
-                                       x2 = p_ptr->x;
+                                       y2 = target_ptr->y;
+                                       x2 = target_ptr->x;
 
                                        break;
                                }
 
-                               if (!in_bounds2(p_ptr->current_floor_ptr, y2, x2)) continue;
+                               if (!in_bounds2(floor_ptr, y2, x2)) continue;
 
                                /* Ignore filled grids */
                                if (!monster_can_enter(y2, x2, r_ptr, 0)) continue;
@@ -990,7 +971,7 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
        if (!done)
        {
                /* Flow towards the player */
-               (void)get_moves_aux(m_idx, &y2, &x2, no_flow);
+               (void)get_moves_aux(target_ptr, m_idx, &y2, &x2, no_flow);
 
                /* Extract the "pseudo-direction" */
                y = m_ptr->fy - y2;
@@ -1013,13 +994,13 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
                        int tmp_y = (-y);
 
                        /* Try to find safe place */
-                       if (find_safety(m_idx, &y, &x))
+                       if (find_safety(target_ptr, m_idx, &y, &x))
                        {
                                /* Attempt to avoid the player */
                                if (!no_flow)
                                {
                                        /* Adjust movement */
-                                       if (get_fear_moves_aux(m_idx, &y, &x)) done = TRUE;
+                                       if (get_fear_moves_aux(target_ptr->current_floor_ptr, m_idx, &y, &x)) done = TRUE;
                                }
                        }
 
@@ -1034,7 +1015,7 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
 
 
        /* Check for no move */
-       if (!x && !y) return (FALSE);
+       if (!x && !y) return FALSE;
 
 
        /* Extract the "absolute distances" */
@@ -1195,7 +1176,7 @@ static bool get_moves(MONSTER_IDX m_idx, DIRECTION *mm)
        }
 
        /* Wants to move... */
-       return (TRUE);
+       return TRUE;
 }
 
 
@@ -1253,21 +1234,21 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
 
        monster_type    *y_ptr;
 
-       bool            do_turn;
-       bool            do_move;
-       bool            do_view;
-       bool            must_alter_to_move;
-
-       bool            did_open_door;
-       bool            did_bash_door;
-       bool            did_take_item;
-       bool            did_kill_item;
-       bool            did_move_body;
-       bool            did_pass_wall;
-       bool            did_kill_wall;
-       bool            gets_angry = FALSE;
-       bool            can_cross;
-       bool            aware = TRUE;
+       bool do_turn;
+       bool do_move;
+       bool do_view;
+       bool must_alter_to_move;
+
+       bool did_open_door;
+       bool did_bash_door;
+       bool did_take_item;
+       bool did_kill_item;
+       bool did_move_body;
+       bool did_pass_wall;
+       bool did_kill_wall;
+       bool gets_angry = FALSE;
+       bool can_cross;
+       bool aware = TRUE;
 
        bool fear, dead;
        bool is_riding_mon = (m_idx == target_ptr->riding);
@@ -1364,7 +1345,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
 
        if (m_ptr->r_idx == MON_SHURYUUDAN)
        {
-               mon_take_hit_mon(m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
+               mon_take_hit_mon(target_ptr, m_idx, 1, &dead, &fear, _("は爆発して粉々になった。", " explodes into tiny shreds."), m_idx);
                if(dead) return;
        }
 
@@ -1380,7 +1361,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                        if (is_riding_mon && riding_pinch < 2)
                        {
                                msg_format(_("%sは傷の痛さの余りあなたの束縛から逃れようとしている。",
-                                                        "%^s seems to be in so much pain, and trying to escape from your restriction."), m_name);
+                                                        "%^s seems to be in so much pain and trying to escape from your restriction."), m_name);
                                riding_pinch++;
                                disturb(target_ptr, TRUE, TRUE);
                        }
@@ -1398,11 +1379,11 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                                if (see_m)
                                {
                                        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) &&
-                                           player_has_los_bold(target_ptr, m_ptr->fy, m_ptr->fx) && projectable(target_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, target_ptr->y, target_ptr->x))
+                                           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))
                                        {
                                                msg_format(_("%^s「ピンチだ!退却させてもらう!」", "%^s says 'It is the pinch! I will retreat'."), m_name);
                                        }
-                                       msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s read a scroll of teleport level."), m_name);
+                                       msg_format(_("%^sがテレポート・レベルの巻物を読んだ。", "%^s reads a scroll of teleport level."), m_name);
                                        msg_format(_("%^sが消え去った。", "%^s disappears."), m_name);
                                }
 
@@ -1429,7 +1410,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                /* Handle non-aggravation - Still sleeping */
                if (!(target_ptr->cursed & TRC_AGGRAVATE)) return;
 
-               (void)set_monster_csleep(m_idx, 0);
+               (void)set_monster_csleep(target_ptr, m_idx, 0);
 
                /* Notice the "waking up" */
                if (m_ptr->ml)
@@ -1566,7 +1547,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                if ((ap_r_ptr->flags2 & RF2_CAN_SPEAK) && aware &&
                    one_in_(SPEAK_CHANCE) &&
                    player_has_los_bold(target_ptr, oy, ox) &&
-                   projectable(target_ptr->current_floor_ptr, oy, ox, target_ptr->y, target_ptr->x))
+                   projectable(target_ptr, oy, ox, target_ptr->y, target_ptr->x))
                {
                        GAME_TEXT m_name[MAX_NLEN];
                        char monmessage[1024];
@@ -1608,7 +1589,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
 
                        /* The monster must be an enemy, and projectable */
                        if (t_m_idx && are_enemies(m_ptr, &target_ptr->current_floor_ptr->m_list[t_m_idx]) &&
-                           projectable(target_ptr->current_floor_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
+                           projectable(target_ptr, m_ptr->fy, m_ptr->fx, m_ptr->target_y, m_ptr->target_x))
                        {
                                counterattack = TRUE;
                        }
@@ -1707,7 +1688,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                mm[0] = mm[1] = mm[2] = mm[3] = 5;
 
                /* Look for an enemy */
-               if (!get_enemy_dir(m_idx, mm))
+               if (!get_enemy_dir(target_ptr, m_idx, mm))
                {
                        /* Find the player if necessary */
                        if (avoid || lonely || distant)
@@ -1722,7 +1703,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                                }
 
                                /* Find the player */
-                               (void)get_moves(m_idx, mm);
+                               (void)get_moves(target_ptr, m_idx, mm);
 
                                /* Restore the leash */
                                target_ptr->pet_follow_distance = (s16b)dis;
@@ -1737,13 +1718,13 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                mm[0] = mm[1] = mm[2] = mm[3] = 5;
 
                /* Look for an enemy */
-               get_enemy_dir(m_idx, mm);
+               get_enemy_dir(target_ptr, m_idx, mm);
        }
        /* Normal movement */
        else
        {
                /* Logical moves, may do nothing */
-               if (!get_moves(m_idx, mm)) return;
+               if (!get_moves(target_ptr, m_idx, mm)) return;
        }
 
        /* Assume nothing */
@@ -2088,7 +2069,7 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                                did_move_body = TRUE;
 
                                /* Wake up the moved monster */
-                               (void)set_monster_csleep(g_ptr->m_idx, 0);
+                               (void)set_monster_csleep(target_ptr, g_ptr->m_idx, 0);
 
                                /* Message */
                        }
@@ -2159,187 +2140,194 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                }
 
                /* Creature has been allowed move */
-               if (do_move)
+               if (!do_move)
                {
-                       do_turn = TRUE;
+                       if (do_turn) break;
+                       continue;
+               }
 
-                       if (have_flag(f_ptr->flags, FF_TREE))
+               do_turn = TRUE;
+
+               if (have_flag(f_ptr->flags, FF_TREE))
+               {
+                       if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
                        {
-                               if (!(r_ptr->flags7 & RF7_CAN_FLY) && !(r_ptr->flags8 & RF8_WILD_WOOD))
-                               {
-                                       m_ptr->energy_need += ENERGY_NEED();
-                               }
+                               m_ptr->energy_need += ENERGY_NEED();
                        }
+               }
+
+               if (!is_riding_mon)
+               {
+                       /* Hack -- Update the old location */
+                       target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
 
-                       if (!is_riding_mon)
+                       /* Mega-Hack -- move the old monster, if any */
+                       if (g_ptr->m_idx)
                        {
-                               /* Hack -- Update the old location */
-                               target_ptr->current_floor_ptr->grid_array[oy][ox].m_idx = g_ptr->m_idx;
+                               /* Move the old monster */
+                               y_ptr->fy = oy;
+                               y_ptr->fx = ox;
 
-                               /* Mega-Hack -- move the old monster, if any */
-                               if (g_ptr->m_idx)
-                               {
-                                       /* Move the old monster */
-                                       y_ptr->fy = oy;
-                                       y_ptr->fx = ox;
+                               /* Update the old monster */
+                               update_monster(target_ptr, g_ptr->m_idx, TRUE);
+                       }
 
-                                       /* Update the old monster */
-                                       update_monster(target_ptr, g_ptr->m_idx, TRUE);
-                               }
+                       /* Hack -- Update the new location */
+                       g_ptr->m_idx = m_idx;
+
+                       /* Move the monster */
+                       m_ptr->fy = ny;
+                       m_ptr->fx = nx;
+                       update_monster(target_ptr, m_idx, TRUE);
 
-                               /* Hack -- Update the new location */
-                               g_ptr->m_idx = m_idx;
+                       lite_spot(oy, ox);
+                       lite_spot(ny, nx);
+               }
+               else
+               {
+                       /* sound(SOUND_WALK); */
+                       if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
+               }
 
-                               /* Move the monster */
-                               m_ptr->fy = ny;
-                               m_ptr->fx = nx;
-                               update_monster(target_ptr, m_idx, TRUE);
+               /* Possible disturb */
+               if (m_ptr->ml &&
+                       (disturb_move ||
+                       (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
+                               (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
+               {
+                       if (is_hostile(m_ptr))
+                               disturb(target_ptr, FALSE, TRUE);
+               }
 
-                               lite_spot(oy, ox);
-                               lite_spot(ny, nx);
-                       }
-                       else
-                       {
-                               /* sound(SOUND_WALK); */
-                               if (!move_player_effect(target_ptr, ny, nx, MPE_DONT_PICKUP)) break;
-                       }
+               /* Take or Kill objects on the floor */
+               bool is_takable_or_killable = g_ptr->o_idx > 0;
+               is_takable_or_killable &= (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM));
+               is_takable_or_killable &= !is_pet(m_ptr) || ((target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM));
+               if (!is_takable_or_killable)
+               {
+                       if (do_turn) break;
+                       continue;
+               }
+               
+               OBJECT_IDX this_o_idx, next_o_idx;
+               bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
+
+               /* Scan all objects in the grid */
+               for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
+               {
+                       BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
+                       GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
+                       object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
+                       next_o_idx = o_ptr->next_o_idx;
 
-                       /* Possible disturb */
-                       if (m_ptr->ml &&
-                           (disturb_move ||
-                            (disturb_near && (m_ptr->mflag & MFLAG_VIEW) && projectable(target_ptr->current_floor_ptr, target_ptr->y, target_ptr->x, m_ptr->fy, m_ptr->fx)) ||
-                            (disturb_high && ap_r_ptr->r_tkills && ap_r_ptr->level >= target_ptr->lev)))
+                       if (do_take)
                        {
-                               if (is_hostile(m_ptr))
-                                       disturb(target_ptr, FALSE, TRUE);
+                               /* Skip gold */
+                               if (o_ptr->tval == TV_GOLD) continue;
+
+                               /*
+                                * Skip "real" corpses and statues, to avoid extreme
+                                * silliness like a novice rogue pockets full of statues
+                                * and corpses.
+                                */
+                               if ((o_ptr->tval == TV_CORPSE) ||
+                                       (o_ptr->tval == TV_STATUE)) continue;
                        }
 
-                       /* Take or Kill objects on the floor */
-                       if (g_ptr->o_idx && (r_ptr->flags2 & (RF2_TAKE_ITEM | RF2_KILL_ITEM)) &&
-                           (!is_pet(m_ptr) || ((target_ptr->pet_extra_flags & PF_PICKUP_ITEMS) && (r_ptr->flags2 & RF2_TAKE_ITEM))))
+                       /* Extract some flags */
+                       object_flags(o_ptr, flgs);
+
+                       /* Acquire the object name */
+                       object_desc(o_name, o_ptr, 0);
+                       monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
+
+                       /* React to objects that hurt the monster */
+                       if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
+                       if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
+                       if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
+                       if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
+                       if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
+                       if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
+                       if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
+                       if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
+                       if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
+                       if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
+                       if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
+                       if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
+                       if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
+                       if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
+                       if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
+                       if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
+                       if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
+                       if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
+                       if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
+                       if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
+                       if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
+                       if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
+                       if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
+
+                       /* The object cannot be picked up by the monster */
+                       if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
+                               ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
                        {
-                               OBJECT_IDX this_o_idx, next_o_idx;
-                               bool do_take = (r_ptr->flags2 & RF2_TAKE_ITEM) ? TRUE : FALSE;
-
-                               /* Scan all objects in the grid */
-                               for (this_o_idx = g_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
+                               /* Only give a message for "take_item" */
+                               if (do_take && (r_ptr->flags2 & RF2_STUPID))
                                {
-                                       BIT_FLAGS flgs[TR_FLAG_SIZE], flg2 = 0L, flg3 = 0L, flgr = 0L;
-                                       GAME_TEXT m_name[MAX_NLEN], o_name[MAX_NLEN];
-                                       object_type *o_ptr = &target_ptr->current_floor_ptr->o_list[this_o_idx];
-                                       next_o_idx = o_ptr->next_o_idx;
-
-                                       if (do_take)
-                                       {
-                                               /* Skip gold */
-                                               if (o_ptr->tval == TV_GOLD) continue;
-
-                                               /*
-                                                * Skip "real" corpses and statues, to avoid extreme
-                                                * silliness like a novice rogue pockets full of statues
-                                                * and corpses.
-                                                */
-                                               if ((o_ptr->tval == TV_CORPSE) ||
-                                                   (o_ptr->tval == TV_STATUE)) continue;
-                                       }
+                                       did_take_item = TRUE;
 
-                                       /* Extract some flags */
-                                       object_flags(o_ptr, flgs);
-
-                                       /* Acquire the object name */
-                                       object_desc(o_name, o_ptr, 0);
-                                       monster_desc(m_name, m_ptr, MD_INDEF_HIDDEN);
-
-                                       /* React to objects that hurt the monster */
-                                       if (have_flag(flgs, TR_SLAY_DRAGON)) flg3 |= (RF3_DRAGON);
-                                       if (have_flag(flgs, TR_KILL_DRAGON)) flg3 |= (RF3_DRAGON);
-                                       if (have_flag(flgs, TR_SLAY_TROLL))  flg3 |= (RF3_TROLL);
-                                       if (have_flag(flgs, TR_KILL_TROLL))  flg3 |= (RF3_TROLL);
-                                       if (have_flag(flgs, TR_SLAY_GIANT))  flg3 |= (RF3_GIANT);
-                                       if (have_flag(flgs, TR_KILL_GIANT))  flg3 |= (RF3_GIANT);
-                                       if (have_flag(flgs, TR_SLAY_ORC))    flg3 |= (RF3_ORC);
-                                       if (have_flag(flgs, TR_KILL_ORC))    flg3 |= (RF3_ORC);
-                                       if (have_flag(flgs, TR_SLAY_DEMON))  flg3 |= (RF3_DEMON);
-                                       if (have_flag(flgs, TR_KILL_DEMON))  flg3 |= (RF3_DEMON);
-                                       if (have_flag(flgs, TR_SLAY_UNDEAD)) flg3 |= (RF3_UNDEAD);
-                                       if (have_flag(flgs, TR_KILL_UNDEAD)) flg3 |= (RF3_UNDEAD);
-                                       if (have_flag(flgs, TR_SLAY_ANIMAL)) flg3 |= (RF3_ANIMAL);
-                                       if (have_flag(flgs, TR_KILL_ANIMAL)) flg3 |= (RF3_ANIMAL);
-                                       if (have_flag(flgs, TR_SLAY_EVIL))   flg3 |= (RF3_EVIL);
-                                       if (have_flag(flgs, TR_KILL_EVIL))   flg3 |= (RF3_EVIL);
-                                       if (have_flag(flgs, TR_SLAY_HUMAN))  flg2 |= (RF2_HUMAN);
-                                       if (have_flag(flgs, TR_KILL_HUMAN))  flg2 |= (RF2_HUMAN);
-                                       if (have_flag(flgs, TR_BRAND_ACID))  flgr |= (RFR_IM_ACID);
-                                       if (have_flag(flgs, TR_BRAND_ELEC))  flgr |= (RFR_IM_ELEC);
-                                       if (have_flag(flgs, TR_BRAND_FIRE))  flgr |= (RFR_IM_FIRE);
-                                       if (have_flag(flgs, TR_BRAND_COLD))  flgr |= (RFR_IM_COLD);
-                                       if (have_flag(flgs, TR_BRAND_POIS))  flgr |= (RFR_IM_POIS);
-
-                                       /* The object cannot be picked up by the monster */
-                                       if (object_is_artifact(o_ptr) || (r_ptr->flags3 & flg3) || (r_ptr->flags2 & flg2) ||
-                                           ((~(r_ptr->flagsr) & flgr) && !(r_ptr->flagsr & RFR_RES_ALL)))
+                                       /* Describe observable situations */
+                                       if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
                                        {
-                                               /* Only give a message for "take_item" */
-                                               if (do_take && (r_ptr->flags2 & RF2_STUPID))
-                                               {
-                                                       did_take_item = TRUE;
-
-                                                       /* Describe observable situations */
-                                                       if (m_ptr->ml && player_can_see_bold(target_ptr, ny, nx))
-                                                       {
-                                                               msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
-                                                       }
-                                               }
+                                               msg_format(_("%^sは%sを拾おうとしたが、だめだった。", "%^s tries to pick up %s, but fails."), m_name, o_name);
                                        }
+                               }
+                       }
 
-                                       /* Pick up the item */
-                                       else if (do_take)
-                                       {
-                                               did_take_item = TRUE;
-
-                                               /* Describe observable situations */
-                                               if (player_can_see_bold(target_ptr, ny, nx))
-                                               {
-                                                       msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
-                                               }
+                       /* Pick up the item */
+                       else if (do_take)
+                       {
+                               did_take_item = TRUE;
 
-                                               /* Excise the object */
-                                               excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
+                               /* Describe observable situations */
+                               if (player_can_see_bold(target_ptr, ny, nx))
+                               {
+                                       msg_format(_("%^sが%sを拾った。", "%^s picks up %s."), m_name, o_name);
+                               }
 
-                                               /* Forget mark */
-                                               o_ptr->marked &= OM_TOUCHED;
+                               /* Excise the object */
+                               excise_object_idx(target_ptr->current_floor_ptr, this_o_idx);
 
-                                               /* Forget location */
-                                               o_ptr->iy = o_ptr->ix = 0;
+                               /* Forget mark */
+                               o_ptr->marked &= OM_TOUCHED;
 
-                                               /* Memorize monster */
-                                               o_ptr->held_m_idx = m_idx;
+                               /* Forget location */
+                               o_ptr->iy = o_ptr->ix = 0;
 
-                                               /* Build a stack */
-                                               o_ptr->next_o_idx = m_ptr->hold_o_idx;
+                               /* Memorize monster */
+                               o_ptr->held_m_idx = m_idx;
 
-                                               /* Carry object */
-                                               m_ptr->hold_o_idx = this_o_idx;
-                                       }
+                               /* Build a stack */
+                               o_ptr->next_o_idx = m_ptr->hold_o_idx;
 
-                                       /* Destroy the item if not a pet */
-                                       else if (!is_pet(m_ptr))
-                                       {
-                                               did_kill_item = TRUE;
+                               /* Carry object */
+                               m_ptr->hold_o_idx = this_o_idx;
+                       }
 
-                                               /* Describe observable situations */
-                                               if (player_has_los_bold(target_ptr, ny, nx))
-                                               {
-                                                       msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
-                                               }
+                       /* Destroy the item if not a pet */
+                       else if (!is_pet(m_ptr))
+                       {
+                               did_kill_item = TRUE;
 
-                                               delete_object_idx(target_ptr->current_floor_ptr, this_o_idx);
-                                       }
+                               /* Describe observable situations */
+                               if (player_has_los_bold(target_ptr, ny, nx))
+                               {
+                                       msg_format(_("%^sが%sを破壊した。", "%^s destroys %s."), m_name, o_name);
                                }
+
+                               delete_object_idx(target_ptr->current_floor_ptr, this_o_idx);
                        }
                }
 
-               /* Stop when done */
                if (do_turn) break;
        }
 
@@ -2360,7 +2348,6 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                }
        }
 
-
        /* Notice changes in view */
        if (do_view)
        {
@@ -2400,25 +2387,21 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
                if (did_kill_wall) r_ptr->r_flags2 |= (RF2_KILL_WALL);
        }
 
+       bool is_battle_determined = !do_turn && !do_move && MON_MONFEAR(m_ptr) && aware;
+       if (!is_battle_determined) return;
 
-       /* Hack -- get "bold" if out of options */
-       if (!do_turn && !do_move && MON_MONFEAR(m_ptr) && aware)
-       {
-               /* No longer afraid */
-               (void)set_monster_monfear(m_idx, 0);
+       /* No longer afraid */
+       (void)set_monster_monfear(target_ptr, m_idx, 0);
 
-               /* Message if seen */
-               if (see_m)
-               {
-                       GAME_TEXT m_name[MAX_NLEN];
-                       monster_desc(m_name, m_ptr, 0);
-                       msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
-               }
-
-               if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
-
-               /* Actually do something now (?) */
+       /* Message if seen */
+       if (see_m)
+       {
+               GAME_TEXT m_name[MAX_NLEN];
+               monster_desc(m_name, m_ptr, 0);
+               msg_format(_("%^sは戦いを決意した!", "%^s turns to fight!"), m_name);
        }
+
+       if (m_ptr->ml) chg_virtue(target_ptr, V_COMPASSION, -1);
 }
 
 /*!
@@ -2457,16 +2440,6 @@ void process_monster(player_type *target_ptr, MONSTER_IDX m_idx)
  */
 void process_monsters(player_type *target_ptr)
 {
-       MONSTER_IDX i;
-       POSITION fx, fy;
-
-       bool            test;
-
-       monster_type    *m_ptr;
-       monster_race    *r_ptr;
-
-       MONRACE_IDX old_monster_race_idx;
-
        BIT_FLAGS old_r_flags1 = 0L;
        BIT_FLAGS old_r_flags2 = 0L;
        BIT_FLAGS old_r_flags3 = 0L;
@@ -2480,21 +2453,19 @@ void process_monsters(player_type *target_ptr)
        byte old_r_blows2 = 0;
        byte old_r_blows3 = 0;
 
-       byte old_r_cast_spell = 0;
-
-       SPEED speed;
-
        /* Clear monster fighting indicator */
        floor_type *floor_ptr = target_ptr->current_floor_ptr;
        floor_ptr->monster_noise = FALSE;
 
        /* Memorize old race */
-       old_monster_race_idx = target_ptr->monster_race_idx;
+       MONRACE_IDX old_monster_race_idx = target_ptr->monster_race_idx;
 
        /* Acquire knowledge */
+       byte old_r_cast_spell = 0;
        if (target_ptr->monster_race_idx)
        {
                /* Acquire current monster */
+               monster_race *r_ptr;
                r_ptr = &r_info[target_ptr->monster_race_idx];
 
                /* Memorize flags */
@@ -2516,10 +2487,12 @@ void process_monsters(player_type *target_ptr)
                old_r_cast_spell = r_ptr->r_cast_spell;
        }
 
-
        /* Process the monsters (backwards) */
-       for (i = floor_ptr->m_max - 1; i >= 1; i--)
+       bool test;
+       for (MONSTER_IDX i = floor_ptr->m_max - 1; i >= 1; i--)
        {
+               monster_type *m_ptr;
+               monster_race *r_ptr;
                m_ptr = &floor_ptr->m_list[i];
                r_ptr = &r_info[m_ptr->r_idx];
 
@@ -2545,8 +2518,8 @@ void process_monsters(player_type *target_ptr)
                /* Hack -- Require proximity */
                if (m_ptr->cdis >= AAF_LIMIT) continue;
 
-               fx = m_ptr->fx;
-               fy = m_ptr->fy;
+               POSITION fx = m_ptr->fx;
+               POSITION fy = m_ptr->fy;
 
                /* Flow by smell is allowed */
                if (!target_ptr->no_flowed)
@@ -2590,7 +2563,7 @@ void process_monsters(player_type *target_ptr)
                /* Do nothing */
                if (!test) continue;
 
-
+               SPEED speed;
                if (target_ptr->riding == i)
                        speed = target_ptr->pspeed;
                else
@@ -2641,6 +2614,7 @@ void process_monsters(player_type *target_ptr)
                return;
 
        /* Acquire monster race */
+       monster_race *r_ptr;
        r_ptr = &r_info[target_ptr->monster_race_idx];
 
        /* Check for knowledge change */