2 * @brief モンスターの移動方向を決定する処理
7 #include "monster/monster-direction.h"
8 #include "monster/monster-util.h"
9 #include "monster-status.h"
10 #include "cmd/cmd-pet.h"
13 * @brief ペットが敵に接近するための方向を決定する
14 * @param target_ptr プレーヤーへの参照ポインタ
15 * @param m_ptr 移動を試みているモンスターへの参照ポインタ
16 * @param t_ptr 移動先モンスターへの参照ポインタ
17 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
18 * @return ペットがモンスターに近づくならばTRUE
20 static bool decide_pet_approch_direction(player_type *target_ptr, monster_type *m_ptr, monster_type *t_ptr)
22 monster_race *r_ptr = &r_info[m_ptr->r_idx];
23 if (!is_pet(m_ptr)) return FALSE;
25 if (target_ptr->pet_follow_distance < 0)
27 if (t_ptr->cdis <= (0 - target_ptr->pet_follow_distance))
32 else if ((m_ptr->cdis < t_ptr->cdis) && (t_ptr->cdis > target_ptr->pet_follow_distance))
37 return (r_ptr->aaf < t_ptr->cdis);
42 * @brief モンスターが敵に接近するための方向を決定する
43 * @param target_ptr プレーヤーへの参照ポインタ
44 * @param m_idx モンスターID
45 * @param start モンスターIDの開始
46 * @param plus モンスターIDの増減 (1/2 の確率で+1、1/2の確率で-1)
47 * @param y モンスターの移動方向Y
48 * @param x モンスターの移動方向X
51 static void decide_enemy_approch_direction(player_type *target_ptr, MONSTER_IDX m_idx, int start, int plus, POSITION *y, POSITION *x)
53 floor_type *floor_ptr = target_ptr->current_floor_ptr;
54 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
55 monster_race *r_ptr = &r_info[m_ptr->r_idx];
56 for (int i = start; ((i < start + floor_ptr->m_max) && (i > start - floor_ptr->m_max)); i += plus)
58 MONSTER_IDX dummy = (i % floor_ptr->m_max);
59 if (dummy == 0) continue;
61 MONSTER_IDX t_idx = dummy;
63 t_ptr = &floor_ptr->m_list[t_idx];
64 if (t_ptr == m_ptr) continue;
65 if (!monster_is_valid(t_ptr)) continue;
66 if (decide_pet_approch_direction(target_ptr, m_ptr, t_ptr)) continue;
67 if (!are_enemies(target_ptr, m_ptr, t_ptr)) continue;
69 if (((r_ptr->flags2 & RF2_PASS_WALL) && ((m_idx != target_ptr->riding) || target_ptr->pass_wall)) ||
70 ((r_ptr->flags2 & RF2_KILL_WALL) && (m_idx != target_ptr->riding)))
72 if (!in_disintegration_range(floor_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
76 if (!projectable(target_ptr, m_ptr->fy, m_ptr->fx, t_ptr->fy, t_ptr->fx)) continue;
87 * @brief モンスターが敵に接近するための方向を計算するメインルーチン
88 * Calculate the direction to the next enemy
89 * @param target_ptr プレーヤーへの参照ポインタ
90 * @param m_idx モンスターの参照ID
91 * @param mm 移動するべき方角IDを返す参照ポインタ
92 * @return 方向が確定した場合TRUE、接近する敵がそもそもいない場合FALSEを返す
94 bool get_enemy_dir(player_type *target_ptr, MONSTER_IDX m_idx, int *mm)
96 floor_type *floor_ptr = target_ptr->current_floor_ptr;
97 monster_type *m_ptr = &floor_ptr->m_list[m_idx];
99 POSITION x = 0, y = 0;
100 if (target_ptr->riding_t_m_idx && player_bold(target_ptr, m_ptr->fy, m_ptr->fx))
102 y = floor_ptr->m_list[target_ptr->riding_t_m_idx].fy;
103 x = floor_ptr->m_list[target_ptr->riding_t_m_idx].fx;
105 else if (is_pet(m_ptr) && target_ptr->pet_t_m_idx)
107 y = floor_ptr->m_list[target_ptr->pet_t_m_idx].fy;
108 x = floor_ptr->m_list[target_ptr->pet_t_m_idx].fx;
114 if (target_ptr->phase_out)
116 start = randint1(floor_ptr->m_max - 1) + floor_ptr->m_max;
117 if (randint0(2)) plus = -1;
121 start = floor_ptr->m_max + 1;
124 decide_enemy_approch_direction(target_ptr, m_idx, start, plus, &y, &x);
126 if ((x == 0) && (y == 0)) return FALSE;
132 store_enemy_approch_direction(mm, y, x);
138 * todo ↓のように書いたが、"5"とはもしかして「その場に留まる」という意味か?
139 * @brief 不規則歩行フラグを持つモンスターの移動方向をその確率に基づいて決定する
140 * @param target_ptr プレーヤーへの参照ポインタ
142 * @param m_ptr モンスターへの参照ポインタ
143 * @return 不規則な方向へ歩くことになったらTRUE
145 static bool random_walk(player_type *target_ptr, DIRECTION *mm, monster_type *m_ptr)
147 monster_race *r_ptr = &r_info[m_ptr->r_idx];
148 if (((r_ptr->flags1 & (RF1_RAND_50 | RF1_RAND_25)) == (RF1_RAND_50 | RF1_RAND_25)) && (randint0(100) < 75))
150 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= (RF1_RAND_50 | RF1_RAND_25);
152 mm[0] = mm[1] = mm[2] = mm[3] = 5;
156 if ((r_ptr->flags1 & RF1_RAND_50) && (randint0(100) < 50))
158 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_50;
160 mm[0] = mm[1] = mm[2] = mm[3] = 5;
164 if ((r_ptr->flags1 & RF1_RAND_25) && (randint0(100) < 25))
166 if (is_original_ap_and_seen(target_ptr, m_ptr)) r_ptr->r_flags1 |= RF1_RAND_25;
168 mm[0] = mm[1] = mm[2] = mm[3] = 5;
177 * @brief ペットや友好的なモンスターがフロアから逃げる処理を行う
178 * @param target_ptr プレーヤーへの参照ポインタ
180 * @param m_idx モンスターID
181 * @return モンスターがペットであればTRUE
183 static bool decide_pet_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, get_moves_pf get_movable_grid)
185 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
186 if (!is_pet(m_ptr)) return FALSE;
188 bool avoid = ((target_ptr->pet_follow_distance < 0) && (m_ptr->cdis <= (0 - target_ptr->pet_follow_distance)));
189 bool lonely = (!avoid && (m_ptr->cdis > target_ptr->pet_follow_distance));
190 bool distant = (m_ptr->cdis > PET_SEEK_DIST);
191 mm[0] = mm[1] = mm[2] = mm[3] = 5;
192 if (get_enemy_dir(target_ptr, m_idx, mm)) return TRUE;
193 if (!avoid && !lonely && !distant) return TRUE;
195 POSITION dis = target_ptr->pet_follow_distance;
196 if (target_ptr->pet_follow_distance > PET_SEEK_DIST)
198 target_ptr->pet_follow_distance = PET_SEEK_DIST;
201 (void)get_movable_grid(target_ptr, m_idx, mm);
202 target_ptr->pet_follow_distance = (s16b)dis;
208 * @brief モンスターの移動パターンを決定する
209 * @param target_ptr プレーヤーへの参照ポインタ
211 * @param m_idx モンスターID
212 * @param aware モンスターがプレーヤーに気付いているならばTRUE、超隠密状態ならばFALSE
213 * @return 移動先が存在すればTRUE
215 bool decide_monster_movement_direction(player_type *target_ptr, DIRECTION *mm, MONSTER_IDX m_idx, bool aware, get_moves_pf get_movable_grid)
217 monster_type *m_ptr = &target_ptr->current_floor_ptr->m_list[m_idx];
218 monster_race *r_ptr = &r_info[m_ptr->r_idx];
220 if (MON_CONFUSED(m_ptr) || !aware)
222 mm[0] = mm[1] = mm[2] = mm[3] = 5;
226 if (random_walk(target_ptr, mm, m_ptr)) return TRUE;
228 if ((r_ptr->flags1 & RF1_NEVER_MOVE) && (m_ptr->cdis > 1))
230 mm[0] = mm[1] = mm[2] = mm[3] = 5;
234 if (decide_pet_movement_direction(target_ptr, mm, m_idx, get_movable_grid)) return TRUE;
236 if (!is_hostile(m_ptr))
238 mm[0] = mm[1] = mm[2] = mm[3] = 5;
239 get_enemy_dir(target_ptr, m_idx, mm);
243 if (!get_movable_grid(target_ptr, m_idx, mm)) return FALSE;