diff --git a/src/agent.rs b/src/agent.rs index 443ce16..2fdc50b 100644 --- a/src/agent.rs +++ b/src/agent.rs @@ -119,12 +119,33 @@ fn random_move_within( impl Agent for SimpleAgent { fn next_move(&self, view: WorldView) -> Move { if view.self_id == view.tagged { - match view - .other_agents - .iter() - .find(|(dist, id)| dist.x.abs() <= 1 && dist.y.abs() <= 1 && Some(*id) != view.tagged_by) - { - Some((_, other)) => Move::TryTag(*other), + let mut cur = None; + for (dist, id) in view.other_agents { + if Some(id) == view.tagged_by { + continue; + } + if dist.x.abs() <= 1 && dist.y.abs() <= 1 { + cur = Some((id, 0, dist)); + break; + } + let total_dist = dist.x.abs() + dist.y.abs(); + let cur_dist = if let Some((_, dist, _)) = cur { + dist + } else { + isize::MAX + }; + if total_dist < cur_dist { + cur = Some((id, total_dist, dist)); + } + } + match cur { + Some((other, total_dist, dist)) => { + if total_dist == 0 { + Move::TryTag(other) + } else { + Move::TryMove((dist.x.signum(), dist.y.signum()).into()) + } + } None => random_move_within(view.bounds_distance), } } else { diff --git a/src/main.rs b/src/main.rs index eb212b3..b21a9c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,7 @@ fn main() { }; world.do_step(); gen += 1; + //std::thread::sleep(std::time::Duration::from_millis(500)); } } }