1
Fork 0

unified teams

This commit is contained in:
Andy Killorin 2026-11-09 14:18:53 -05:00
parent 1319968095
commit 6ebb6753fd
Signed by: ank
GPG key ID: 23F9463ECB67FE8C

View file

@ -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}
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<f32> = 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(color);
}
robot.draw();
}
draw_text("HELLO", 20.0, 20.0, 30.0, DARKGRAY);
loo += 1;