diff --git a/src/npc/misc.rs b/src/npc/misc.rs index ba380d8..6efd3fb 100644 --- a/src/npc/misc.rs +++ b/src/npc/misc.rs @@ -692,8 +692,8 @@ impl NPC { } { - let player = self.get_closest_player_mut(players); - if abs(player.x - self.x) < 480 * 0x200 && abs(player.y - self.y) < 240 * 0x200 + let i = self.get_closest_player_idx_mut(&players); + if abs(players[i].x - self.x) < 480 * 0x200 && abs(players[i].y - self.y) < 240 * 0x200 && state.game_rng.range(0..5) == 1 { let mut particle = NPCMap::create_npc(199, &state.npc_table); particle.cond.set_alive(true); @@ -745,8 +745,8 @@ impl NPC { } { - let player = self.get_closest_player_mut(players); - if abs(player.x - self.x) < 480 * 0x200 && abs(player.y - self.y) < 240 * 0x200 + let i = self.get_closest_player_idx_mut(&players); + if abs(players[i].x - self.x) < 480 * 0x200 && abs(players[i].y - self.y) < 240 * 0x200 && state.game_rng.range(0..5) == 1 { let mut particle = NPCMap::create_npc(199, &state.npc_table); particle.cond.set_alive(true); @@ -797,8 +797,8 @@ impl NPC { } { - let player = self.get_closest_player_mut(players); - if abs(player.x - self.x) < 480 * 0x200 && abs(player.y - self.y) < 240 * 0x200 + let i = self.get_closest_player_idx_mut(&players); + if abs(players[i].x - self.x) < 480 * 0x200 && abs(players[i].y - self.y) < 240 * 0x200 && state.game_rng.range(0..5) == 1 { let mut particle = NPCMap::create_npc(199, &state.npc_table); particle.cond.set_alive(true); @@ -846,8 +846,8 @@ impl NPC { } { - let player = self.get_closest_player_mut(players); - if abs(player.x - self.x) < 480 * 0x200 && abs(player.y - self.y) < 240 * 0x200 + let i = self.get_closest_player_idx_mut(&players); + if abs(players[i].x - self.x) < 480 * 0x200 && abs(players[i].y - self.y) < 240 * 0x200 && state.game_rng.range(0..5) == 1 { let mut particle = NPCMap::create_npc(199, &state.npc_table); particle.cond.set_alive(true); diff --git a/src/npc/npc_utils.rs b/src/npc/npc_utils.rs index 9aea4a0..83b003a 100644 --- a/src/npc/npc_utils.rs +++ b/src/npc/npc_utils.rs @@ -4,6 +4,28 @@ use crate::npc::NPC; use crate::player::Player; impl NPC { + pub fn get_closest_player_idx_mut<'a>(&self, players: &[&'a mut Player; 2]) -> usize { + let mut max_dist = f64::MAX; + let mut player_idx = 0; + + for (idx, player) in players.iter().enumerate() { + if !player.cond.alive() || player.cond.hidden() { + continue; + } + + let dist_x = abs(self.x - player.x) as f64; + let dist_y = abs(self.y - player.y) as f64; + let dist = (dist_x * dist_x + dist_y * dist_y).sqrt(); + + if dist < max_dist { + max_dist = dist; + player_idx = idx; + } + } + + player_idx + } + pub fn get_closest_player_mut<'a>(&self, players: [&'a mut Player; 2]) -> &'a mut Player { let mut max_dist = f64::MAX; let mut player_idx = 0;