Make SimpleAgent actually hunt others

This commit is contained in:
Adrian Heine 2021-07-22 16:14:34 +02:00
parent 5e6177900e
commit 6294d72190
2 changed files with 28 additions and 6 deletions

View file

@ -119,12 +119,33 @@ fn random_move_within(
impl Agent for SimpleAgent { impl Agent for SimpleAgent {
fn next_move(&self, view: WorldView) -> Move { fn next_move(&self, view: WorldView) -> Move {
if view.self_id == view.tagged { if view.self_id == view.tagged {
match view let mut cur = None;
.other_agents for (dist, id) in view.other_agents {
.iter() if Some(id) == view.tagged_by {
.find(|(dist, id)| dist.x.abs() <= 1 && dist.y.abs() <= 1 && Some(*id) != view.tagged_by) continue;
{ }
Some((_, other)) => Move::TryTag(*other), 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), None => random_move_within(view.bounds_distance),
} }
} else { } else {

View file

@ -63,6 +63,7 @@ fn main() {
}; };
world.do_step(); world.do_step();
gen += 1; gen += 1;
//std::thread::sleep(std::time::Duration::from_millis(500));
} }
} }
} }