diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 7e96dfe..378d823 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -17,6 +17,7 @@ use parry2d::shape::Shape; const VIEW_DIST: f32 = 180.0; const BOT_RADIUS: f32 = 16.0; +const KILL_RADIUS: f32 = 140.0; trait Mortal { fn collider(&self) -> (&Pose, &dyn Shape); @@ -85,11 +86,11 @@ impl Robot { fn draw_weapon(&self) { let tl = self.pose.translation; - draw_circle(tl.x, tl.y, 80.0, ORANGE); + draw_circle(tl.x, tl.y, KILL_RADIUS, ORANGE); } fn weapon<'a>(&self, peers: impl Iterator) { - let weapon = Ball::new(80.0); + let weapon = Ball::new(KILL_RADIUS); for peer in peers { let (p_p, p_c) = peer.collider(); @@ -105,7 +106,7 @@ impl Robot { match sensor { Sensor::Food => self.move_step(1.0, 0.0), Sensor::Kin => self.move_step(-0.3, 0.02), - Sensor::NonKin => self.move_step(-0.3, 0.02), + Sensor::NonKin => self.move_step(0.3, -0.02), Sensor::None => self.move_step(0.3, 0.01), } @@ -162,13 +163,14 @@ impl Food { } async fn run_simulation() -> (i32, i32) { + let mut watchdog = 0; // iterations since something interesting let mut robots = Vec::new(); let mut score = HashMap::new(); for i in 5..20 { - robots.push(Robot::new(Vec2 { x: 100.0, y: i as f32*50.0 }, RED)); - robots.push(Robot::new(Vec2 { x: 1200.0, y: i as f32*50.0 }, BLUE)); + robots.push(Robot::new(Vec2 { x: 100.0, y: i as f32*50.0 }, BLUE)); + robots.push(Robot::new(Vec2 { x: 1200.0, y: i as f32*50.0 }, RED)); } let mut food = Vec::new(); @@ -192,7 +194,8 @@ async fn run_simulation() -> (i32, i32) { let mut loo = 0; loop { - let rendered = loo %1 == 0; + let rendered = loo %90 == 0; + //let rendered = false; clear_background(LIGHTGRAY); for food in &mut food{ @@ -200,6 +203,7 @@ async fn run_simulation() -> (i32, i32) { if food.living() { score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1); food.kill(); + watchdog = 0; } } if rendered { food.draw(); } @@ -239,9 +243,12 @@ async fn run_simulation() -> (i32, i32) { let robots = robots.map(|r| r as &dyn Mortal); let peers = robots.chain(food.iter().map(|f| f as &dyn Mortal)); - let death = robot.policy(&sensors[idx]); + let mut death = robot.policy(&sensors[idx]); + if robot.team == BLUE { + death = false; // pacifists + } if death { - if rendered {robot.draw_weapon();} + if true {robot.draw_weapon();} robot.weapon(peers); } if rendered { robot.draw();} @@ -255,7 +262,8 @@ async fn run_simulation() -> (i32, i32) { next_frame().await // TODO: framerate cap } - if food.is_empty() { + watchdog += 1; + if food.is_empty() || watchdog > 1000 { let red = score.get(&color_hash(RED)).unwrap_or(&0); let blu = score.get(&color_hash(BLUE)).unwrap_or(&0); return (*red, *blu);