From 4bc18b26ffa419718c289584cd2cbfaf7fa46128 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 01:01:26 -0500 Subject: [PATCH 01/10] kin visuals --- simulator/src/main.rs | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index e9f1f70..10af53a 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -1,4 +1,5 @@ use std::cmp::min; +use std::ptr; use macroquad::prelude::*; use parry2d::math::Pose; @@ -16,12 +17,14 @@ const BOT_RADIUS: f32 = 16.0; struct Robot { pose: Pose, alive: bool, + collider: Ball, + sensor: Sensor, } impl Robot { fn new(position: Vec2) -> Self { let pose:Pose = Pose::from_translation(position.to_array().into()); - Self { pose, alive: true } + Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None} } fn move_step(&mut self, forward: f32, right: f32) { @@ -45,6 +48,10 @@ impl Robot { self.pose.translation + self.ahead() * VIEW_DIST } + pub fn collider(&self) -> (&Pose, &dyn Shape) { + (&self.pose, &self.collider) + } + fn detect<'a>(&self, things: impl Iterator) -> Option { let collider = Segment::new(self.pose.translation, self.view_end()); things @@ -52,10 +59,11 @@ impl Robot { .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) } - pub fn policy(&mut self, sensor: Sensor){ + pub fn policy(&mut self, sensor: &Sensor){ match sensor { Sensor::Food => self.move_step(1.0, 0.0), - Sensor::None => self.move_step(1.0, 0.01), + Sensor::Kin => self.move_step(-0.3, -0.02), + Sensor::None => self.move_step(0.3, 0.01), } } } @@ -68,6 +76,7 @@ struct Food { enum Sensor { Food, + Kin, None } @@ -110,12 +119,19 @@ async fn main() { food.draw(); } - for robot in &mut robots { + let mut sensors = Vec::new(); + for robot in &robots { println!("loop"); - let detect = robot.detect(food.iter().map(Food::collider)); - let sensor = if detect.is_some() {Sensor::Food} else {Sensor::None}; - robot.policy(sensor); - println!("Food detection: {:?}",detect); + let detect_food = robot.detect(food.iter().map(Food::collider)); + let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); + let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None}; + if detect_kin.is_some() {sensor = Sensor::Kin} + println!("Food detection: {:?}",detect_food); + println!("Kin detection: {:?}",detect_kin); + sensors.push(sensor); + } + for (idx, robot) in robots.iter_mut().enumerate() { + robot.policy(&sensors[idx]); robot.draw(); } draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); From b279cc91d7d28055994cb722d6c1b110fe88c0c8 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 11:42:22 -0500 Subject: [PATCH 02/10] food collection --- simulator/src/main.rs | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 10af53a..ed9c376 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -62,7 +62,7 @@ impl Robot { pub fn policy(&mut self, sensor: &Sensor){ match sensor { Sensor::Food => self.move_step(1.0, 0.0), - Sensor::Kin => self.move_step(-0.3, -0.02), + Sensor::Kin => self.move_step(-0.3, 0.02), Sensor::None => self.move_step(0.3, 0.01), } } @@ -95,6 +95,12 @@ impl Food { pub fn collider(&self) -> (&Pose, &dyn Shape) { (&self.pose, &self.collider) } + + fn detect<'a>(&self, things: impl Iterator) -> Option { + things + .map(|thing| contact(&self.pose, &self.collider, thing.0, thing.1, 0.0).unwrap()).flatten() + .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) + } } @@ -115,10 +121,17 @@ async fn main() { loop { clear_background(LIGHTGRAY); - for food in &food{ + for food in &mut food{ + if let Some(detect_robot) = food.detect(robots.iter().map(Robot::collider)) { + println!("robot detected in contact"); + food.alive = false; + } food.draw(); + // TODO: die upon collision with robot } + food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food + let mut sensors = Vec::new(); for robot in &robots { println!("loop"); From 1319968095c6fbd96465d4a241b6a422f15728e6 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:06:27 -0500 Subject: [PATCH 03/10] red vs blue --- simulator/src/main.rs | 61 +++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index ed9c376..3de3acb 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -36,12 +36,12 @@ impl Robot { Vec2::new(self.pose.rotation.re, self.pose.rotation.im) } - pub fn draw(&self) { + pub fn draw(&self, color: Color) { let tl = self.pose.translation; let tl2 = self.view_end(); draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); - draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, RED); + draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, color); } fn view_end(&self) -> Vec2 { @@ -108,16 +108,32 @@ impl Food { async fn main() { let mut robots = Vec::new(); - robots.push(Robot::new(Vec2 { x: 100.0, y: 100.0 })); - robots.push(Robot::new(Vec2 { x: 140.0, y: 100.0 })); - robots.push(Robot::new(Vec2 { x: 140.0, y: 200.0 })); - robots.push(Robot::new(Vec2 { x: 180.0, y: 150.0 })); + let mut blu_robots = Vec::new(); + + for i in 5..20 { + robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 })); + } + + for i in 5..20 { + blu_robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 })); + } let mut food = Vec::new(); food.push(Food::new(Vec2 { x: 160.0, y: 100.0 })); food.push(Food::new(Vec2 { x: 360.0, y: 100.0 })); food.push(Food::new(Vec2 { x: 360.0, y: 300.0 })); + food.push(Food::new(Vec2 { x: 360.0, y: 400.0 })); + food.push(Food::new(Vec2 { x: 460.0, y: 400.0 })); + food.push(Food::new(Vec2 { x: 260.0, y: 400.0 })); + for j in 10..30 { + for i in 5..20 { + food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); + } + } + + + let mut loo = 0; loop { clear_background(LIGHTGRAY); @@ -133,21 +149,26 @@ async fn main() { food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food let mut sensors = Vec::new(); - for robot in &robots { - println!("loop"); - let detect_food = robot.detect(food.iter().map(Food::collider)); - let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); - let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None}; - if detect_kin.is_some() {sensor = Sensor::Kin} - println!("Food detection: {:?}",detect_food); - println!("Kin detection: {:?}",detect_kin); - sensors.push(sensor); - } - for (idx, robot) in robots.iter_mut().enumerate() { - robot.policy(&sensors[idx]); - robot.draw(); + for (robots, color) in [(&mut robots, RED), (&mut blu_robots, BLUE)] { + for robot in &*robots { + println!("loop"); + let detect_food = robot.detect(food.iter().map(Food::collider)); + let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); + let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None}; + if detect_kin.is_some() {sensor = Sensor::Kin} + println!("Food detection: {:?}",detect_food); + println!("Kin detection: {:?}",detect_kin); + sensors.push(sensor); + } + for (idx, robot) in robots.iter_mut().enumerate() { + robot.policy(&sensors[idx]); + robot.draw(color); + } } draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); - next_frame().await // TODO: framerate cap + loo += 1; + if loo %2 == 0 { + next_frame().await // TODO: framerate cap + } } } From 6ebb6753fdaba3ec5cb473d6ffe5d26c1a882c70 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:18:53 -0500 Subject: [PATCH 04/10] unified teams --- simulator/src/main.rs | 61 ++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 3de3acb..3b49216 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -19,12 +19,13 @@ struct Robot { alive: bool, collider: Ball, sensor: Sensor, + team: Color, } impl Robot { - fn new(position: Vec2) -> Self { + fn new(position: Vec2, team: Color) -> Self { let pose:Pose = Pose::from_translation(position.to_array().into()); - Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None} + Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team} } fn move_step(&mut self, forward: f32, right: f32) { @@ -36,12 +37,12 @@ impl Robot { Vec2::new(self.pose.rotation.re, self.pose.rotation.im) } - pub fn draw(&self, color: Color) { + pub fn draw(&self) { let tl = self.pose.translation; let tl2 = self.view_end(); draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); - draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, color); + draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, self.team); } fn view_end(&self) -> Vec2 { @@ -63,6 +64,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, 1.02), Sensor::None => self.move_step(0.3, 0.01), } } @@ -77,6 +79,7 @@ struct Food { enum Sensor { Food, Kin, + NonKin, None } @@ -108,14 +111,10 @@ impl Food { async fn main() { let mut robots = Vec::new(); - let mut blu_robots = Vec::new(); for i in 5..20 { - robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 })); - } - - for i in 5..20 { - blu_robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 })); + robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 }, RED)); + robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 }, BLUE)); } let mut food = Vec::new(); @@ -149,21 +148,35 @@ async fn main() { food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food let mut sensors = Vec::new(); - for (robots, color) in [(&mut robots, RED), (&mut blu_robots, BLUE)] { - for robot in &*robots { - println!("loop"); - let detect_food = robot.detect(food.iter().map(Food::collider)); - let detect_kin = robot.detect(robots.iter().filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); - let mut sensor = if detect_food.is_some() {Sensor::Food} else {Sensor::None}; - if detect_kin.is_some() {sensor = Sensor::Kin} - println!("Food detection: {:?}",detect_food); - println!("Kin detection: {:?}",detect_kin); - sensors.push(sensor); - } - for (idx, robot) in robots.iter_mut().enumerate() { - robot.policy(&sensors[idx]); - robot.draw(color); + for robot in &*robots { + println!("loop"); + let detect_food = robot.detect(food.iter().map(Food::collider)); + let detect_kin = robot.detect(robots.iter().filter(|o| o.team == robot.team).filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); + let detect_non_kin = robot.detect(robots.iter().filter(|o| o.team != robot.team).filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); + + let mut sensor = Sensor::None; + let mut closest: Option = None; + + for (dist, detection) in [(detect_food, Sensor::Food), (detect_kin, Sensor::Kin), (detect_non_kin, Sensor::NonKin)] { + if let (Some(close), Some(dist)) = (closest, dist) { + if close < dist {continue;} + } + + if let Some(_) = dist { + sensor = detection; + closest = dist; + } + } + + println!("Food detection: {:?}",detect_food); + println!("Kin detection: {:?}",detect_kin); + println!("NonKin detection: {:?}",detect_non_kin); + sensors.push(sensor); + } + for (idx, robot) in robots.iter_mut().enumerate() { + robot.policy(&sensors[idx]); + robot.draw(); } draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); loo += 1; From 5e72d94080bccf0d86ac1b7c16246de6e594ce3f Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:42:01 -0500 Subject: [PATCH 05/10] keep score --- simulator/src/main.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 3b49216..ca24220 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -1,4 +1,5 @@ use std::cmp::min; +use std::collections::HashMap; use std::ptr; use macroquad::prelude::*; @@ -99,10 +100,10 @@ impl Food { (&self.pose, &self.collider) } - fn detect<'a>(&self, things: impl Iterator) -> Option { + fn detect<'a>(&self, things: impl Iterator) -> Option { things - .map(|thing| contact(&self.pose, &self.collider, thing.0, thing.1, 0.0).unwrap()).flatten() - .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) + .map(|robot| contact(&self.pose, &self.collider, &robot.pose, &robot.collider, 0.0).unwrap().map(|a| (a,robot.team))).flatten() + .map(|(contact,team)| (contact.point1.distance(self.pose.translation),team)).reduce(|x ,y| if x.0 < y.0 {x} else{y} ).map(|(_, team)| team) } } @@ -112,9 +113,11 @@ async fn main() { let mut robots = Vec::new(); + let mut score = HashMap::new(); + for i in 5..20 { robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 }, RED)); - robots.push(Robot::new(Vec2 { x: 850.0, y: i as f32*50.0 }, BLUE)); + robots.push(Robot::new(Vec2 { x: 1150.0, y: i as f32*50.0 }, BLUE)); } let mut food = Vec::new(); @@ -125,21 +128,28 @@ async fn main() { food.push(Food::new(Vec2 { x: 460.0, y: 400.0 })); food.push(Food::new(Vec2 { x: 260.0, y: 400.0 })); - for j in 10..30 { + for j in 5..50 { for i in 5..20 { food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); } } + fn color_hash(color: Color) -> [u32; 4] { + color.to_vec().to_array().map(|f| f.to_bits()) + } + let mut loo = 0; loop { clear_background(LIGHTGRAY); for food in &mut food{ - if let Some(detect_robot) = food.detect(robots.iter().map(Robot::collider)) { - println!("robot detected in contact"); - food.alive = false; + if let Some(team) = food.detect(robots.iter()) { + if food.alive { + score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1); + println!("robot detected in contact"); + food.alive = false; + } } food.draw(); // TODO: die upon collision with robot @@ -178,7 +188,8 @@ async fn main() { robot.policy(&sensors[idx]); robot.draw(); } - draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY); + draw_text(format!("RED: {}", score.get(&color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY); + draw_text(format!("BLUE: {}", score.get(&color_hash(BLUE)).unwrap_or(&0)), 20.0, 90.0, 30.0, DARKGRAY); loo += 1; if loo %2 == 0 { next_frame().await // TODO: framerate cap From bd9412e54a973a0d7a5abffc207de1f542128180 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 14:55:30 -0500 Subject: [PATCH 06/10] randomize playing field --- simulator/src/main.rs | 48 ++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index ca24220..e9f27d2 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -1,5 +1,6 @@ use std::cmp::min; use std::collections::HashMap; +use std::f32::consts::TAU; use std::ptr; use macroquad::prelude::*; @@ -25,7 +26,8 @@ struct Robot { impl Robot { fn new(position: Vec2, team: Color) -> Self { - let pose:Pose = Pose::from_translation(position.to_array().into()); + let rand_angle = rand::gen_range(0, 1000) as f32 * TAU / 1000.0; + let pose:Pose = Pose::from_parts(position.to_array().into(), Rot2::from_angle(rand_angle)); Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team} } @@ -111,29 +113,31 @@ impl Food { #[macroquad::main("Adversarial Foraging Simulator")] async fn main() { + rand::srand(4); + let mut robots = Vec::new(); let mut score = HashMap::new(); for i in 5..20 { - robots.push(Robot::new(Vec2 { x: 150.0, y: i as f32*50.0 }, RED)); - robots.push(Robot::new(Vec2 { x: 1150.0, y: i as f32*50.0 }, BLUE)); + 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)); } let mut food = Vec::new(); - food.push(Food::new(Vec2 { x: 160.0, y: 100.0 })); - food.push(Food::new(Vec2 { x: 360.0, y: 100.0 })); - food.push(Food::new(Vec2 { x: 360.0, y: 300.0 })); - food.push(Food::new(Vec2 { x: 360.0, y: 400.0 })); - food.push(Food::new(Vec2 { x: 460.0, y: 400.0 })); - food.push(Food::new(Vec2 { x: 260.0, y: 400.0 })); - for j in 5..50 { - for i in 5..20 { - food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); - } + for i in 0..120 { + let x = rand::gen_range(200, 1100) as f32; + let y = rand::gen_range(200, 900) as f32; + food.push(Food::new(Vec2 { x, y })); } + //for j in 5..20 { + // for i in 5..20 { + // food.push(Food::new(Vec2 { x: i as f32*60.0, y: j as f32*50.0 })); + // } + //} + fn color_hash(color: Color) -> [u32; 4] { color.to_vec().to_array().map(|f| f.to_bits()) } @@ -141,17 +145,17 @@ async fn main() { let mut loo = 0; loop { + let rendered = loo %30 == 0; clear_background(LIGHTGRAY); for food in &mut food{ if let Some(team) = food.detect(robots.iter()) { if food.alive { score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1); - println!("robot detected in contact"); food.alive = false; } } - food.draw(); + if rendered { food.draw(); } // TODO: die upon collision with robot } @@ -159,7 +163,6 @@ async fn main() { let mut sensors = Vec::new(); for robot in &*robots { - println!("loop"); let detect_food = robot.detect(food.iter().map(Food::collider)); let detect_kin = robot.detect(robots.iter().filter(|o| o.team == robot.team).filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); let detect_non_kin = robot.detect(robots.iter().filter(|o| o.team != robot.team).filter(|r| !ptr::eq(&**r , &**&robot)).map(Robot::collider)); @@ -179,19 +182,18 @@ async fn main() { } - println!("Food detection: {:?}",detect_food); - println!("Kin detection: {:?}",detect_kin); - println!("NonKin detection: {:?}",detect_non_kin); sensors.push(sensor); } for (idx, robot) in robots.iter_mut().enumerate() { robot.policy(&sensors[idx]); - robot.draw(); + if rendered { robot.draw();} + } + if rendered { + draw_text(format!("RED: {}", score.get(&color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY); + draw_text(format!("BLUE: {}", score.get(&color_hash(BLUE)).unwrap_or(&0)), 20.0, 40.0, 30.0, DARKGRAY); } - draw_text(format!("RED: {}", score.get(&color_hash(RED)).unwrap_or(&0)), 20.0, 20.0, 30.0, DARKGRAY); - draw_text(format!("BLUE: {}", score.get(&color_hash(BLUE)).unwrap_or(&0)), 20.0, 90.0, 30.0, DARKGRAY); loo += 1; - if loo %2 == 0 { + if rendered { next_frame().await // TODO: framerate cap } } From 3d9f957c4f9abe5c070441bd1bbafa3e733c954c Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:04:16 -0500 Subject: [PATCH 07/10] multi-run --- simulator/src/main.rs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index e9f27d2..8af789f 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -109,12 +109,7 @@ impl Food { } } - -#[macroquad::main("Adversarial Foraging Simulator")] -async fn main() { - - rand::srand(4); - +async fn run_simulation() -> (i32, i32) { let mut robots = Vec::new(); let mut score = HashMap::new(); @@ -196,5 +191,34 @@ async fn main() { if rendered { next_frame().await // TODO: framerate cap } + + if food.is_empty() { + let red = score.get(&color_hash(RED)).unwrap_or(&0); + let blu = score.get(&color_hash(BLUE)).unwrap_or(&0); + return (*red, *blu); + } } } + + +#[macroquad::main("Adversarial Foraging Simulator")] +async fn main() { + + let mut rtot = 0; + let mut btot = 0; + for i in 0..100 { + rand::srand(i); + + let (red, blu) = run_simulation().await; + + rtot += red; + btot += blu; + + println!("{} wins! RED: {red}, BLU: {blu}", if red > blu {"red"} else {"blue"}); + } + + let red = rtot / 100; + let blu = btot / 100; + println!("{} wins on average! RED: {red}, BLU: {blu}", if red > blu {"red"} else {"blue"}); + +} From 596db89430711608e3841e9b807271071255bc23 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:50:07 -0500 Subject: [PATCH 08/10] define mortality --- simulator/src/main.rs | 83 +++++++++++++++++++++++++++++-------------- 1 file changed, 57 insertions(+), 26 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 8af789f..34d4a08 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -2,6 +2,8 @@ use std::cmp::min; use std::collections::HashMap; use std::f32::consts::TAU; use std::ptr; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::Ordering; use macroquad::prelude::*; use parry2d::math::Pose; @@ -16,19 +18,48 @@ use parry2d::shape::Shape; const VIEW_DIST: f32 = 180.0; const BOT_RADIUS: f32 = 16.0; +trait Mortal { + fn collider(&self) -> (&Pose, &dyn Shape); + fn kill(&self); + fn living(&self) -> bool; + + fn draw(&self); +} + struct Robot { pose: Pose, - alive: bool, + alive: AtomicBool, collider: Ball, sensor: Sensor, team: Color, } +impl Mortal for Robot { + fn draw(&self) { + let tl = self.pose.translation; + let tl2 = self.view_end(); + draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); + draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); + draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, self.team); + } + + fn collider(&self) -> (&Pose, &dyn Shape) { + (&self.pose, &self.collider) + } + + fn kill (&self) { + self.alive.store(false, Ordering::Relaxed); + } + fn living(&self) -> bool { + self.alive.load(Ordering::Relaxed) + } +} + impl Robot { fn new(position: Vec2, team: Color) -> Self { let rand_angle = rand::gen_range(0, 1000) as f32 * TAU / 1000.0; let pose:Pose = Pose::from_parts(position.to_array().into(), Rot2::from_angle(rand_angle)); - Self { pose, alive: true, collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team} + Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), sensor: Sensor::None, team} } fn move_step(&mut self, forward: f32, right: f32) { @@ -40,21 +71,10 @@ impl Robot { Vec2::new(self.pose.rotation.re, self.pose.rotation.im) } - pub fn draw(&self) { - let tl = self.pose.translation; - let tl2 = self.view_end(); - draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); - draw_line(tl.x, tl.y, tl2.x, tl2.y, 2.0, BLUE); - draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, self.team); - } - fn view_end(&self) -> Vec2 { self.pose.translation + self.ahead() * VIEW_DIST } - pub fn collider(&self) -> (&Pose, &dyn Shape) { - (&self.pose, &self.collider) - } fn detect<'a>(&self, things: impl Iterator) -> Option { let collider = Segment::new(self.pose.translation, self.view_end()); @@ -76,7 +96,7 @@ impl Robot { struct Food { pose: Pose, collider: Ball, - alive: bool, + alive: AtomicBool, } enum Sensor { @@ -86,22 +106,33 @@ enum Sensor { None } -impl Food { - fn new(position: Vec2) -> Self { - let pose:Pose = Pose::from_translation(position.to_array().into()); - Self { pose, alive: true, collider: Ball::new(BOT_RADIUS) } - } - - pub fn draw(&self) { +impl Mortal for Food { + fn draw(&self) { let tl = self.pose.translation; draw_circle(tl.x, tl.y, BOT_RADIUS, BLACK); draw_circle(tl.x, tl.y, BOT_RADIUS-1.0, GREEN); } - pub fn collider(&self) -> (&Pose, &dyn Shape) { + fn collider(&self) -> (&Pose, &dyn Shape) { (&self.pose, &self.collider) } + fn kill (&self) { + self.alive.store(false, Ordering::Relaxed); + } + + fn living(&self) -> bool { + self.alive.load(Ordering::Relaxed) + } +} + +impl Food { + fn new(position: Vec2) -> Self { + let pose:Pose = Pose::from_translation(position.to_array().into()); + Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS) } + } + + fn detect<'a>(&self, things: impl Iterator) -> Option { things .map(|robot| contact(&self.pose, &self.collider, &robot.pose, &robot.collider, 0.0).unwrap().map(|a| (a,robot.team))).flatten() @@ -140,21 +171,21 @@ async fn run_simulation() -> (i32, i32) { let mut loo = 0; loop { - let rendered = loo %30 == 0; + let rendered = loo %100 == 0; clear_background(LIGHTGRAY); for food in &mut food{ if let Some(team) = food.detect(robots.iter()) { - if food.alive { + if food.living() { score.entry(color_hash(team)).and_modify(|s| *s +=1 ).or_insert(1); - food.alive = false; + food.kill(); } } if rendered { food.draw(); } // TODO: die upon collision with robot } - food = food.into_iter().filter(|f| f.alive).collect(); // remove dead food + food = food.into_iter().filter(Mortal::living).collect(); // remove dead food let mut sensors = Vec::new(); for robot in &*robots { From 17551f2ae293b29d604f23ba918d76d71a5f3e4c Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:14:41 -0500 Subject: [PATCH 09/10] kill beam --- simulator/src/main.rs | 44 +++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 34d4a08..7e96dfe 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -83,13 +83,33 @@ impl Robot { .map(|contact| contact.point1.distance(self.pose.translation)).reduce(|x: f32,y: f32| x.min(y)) } - pub fn policy(&mut self, sensor: &Sensor){ + fn draw_weapon(&self) { + let tl = self.pose.translation; + draw_circle(tl.x, tl.y, 80.0, ORANGE); + } + + fn weapon<'a>(&self, peers: impl Iterator) { + let weapon = Ball::new(80.0); + + for peer in peers { + let (p_p, p_c) = peer.collider(); + + if let Some(_) = contact(&self.pose, &weapon, p_p , p_c, 0.0).unwrap() { + peer.kill(); + } + + } + } + + pub fn policy(&mut self, sensor: &Sensor) -> bool{ 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, 1.02), + Sensor::NonKin => self.move_step(-0.3, 0.02), Sensor::None => self.move_step(0.3, 0.01), } + + return *sensor == Sensor::NonKin; } } @@ -99,6 +119,7 @@ struct Food { alive: AtomicBool, } +#[derive(PartialEq, Eq)] enum Sensor { Food, Kin, @@ -171,7 +192,7 @@ async fn run_simulation() -> (i32, i32) { let mut loo = 0; loop { - let rendered = loo %100 == 0; + let rendered = loo %1 == 0; clear_background(LIGHTGRAY); for food in &mut food{ @@ -182,10 +203,10 @@ async fn run_simulation() -> (i32, i32) { } } if rendered { food.draw(); } - // TODO: die upon collision with robot } food = food.into_iter().filter(Mortal::living).collect(); // remove dead food + robots = robots.into_iter().filter(Mortal::living).collect(); // remove dead robots let mut sensors = Vec::new(); for robot in &*robots { @@ -210,8 +231,19 @@ async fn run_simulation() -> (i32, i32) { sensors.push(sensor); } - for (idx, robot) in robots.iter_mut().enumerate() { - robot.policy(&sensors[idx]); + for idx in 0..robots.len() { + let (before, after) = robots.split_at_mut(idx); + let (robot, after) = after.split_first_mut().unwrap(); + let robots = before.iter().chain(after.iter()); + + 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]); + if death { + if rendered {robot.draw_weapon();} + robot.weapon(peers); + } if rendered { robot.draw();} } if rendered { From aef4cffd43f6206024b7791bd20f88fd706b9772 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Mon, 9 Nov 2026 16:28:58 -0500 Subject: [PATCH 10/10] enlarged kill radius --- simulator/src/main.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) 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);