From 1d36c028cddc5365722467cc8e6b8e7c04f6243e Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Tue, 10 Nov 2026 00:07:07 -0500 Subject: [PATCH] define weapon in toml --- simulator/conf.toml | 9 +++++--- simulator/src/main.rs | 52 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/simulator/conf.toml b/simulator/conf.toml index 58fcffe..7367e98 100644 --- a/simulator/conf.toml +++ b/simulator/conf.toml @@ -1,14 +1,17 @@ food_amount = 120 red_count = 10 blu_count = 10 -kill_radius = 140 -kill_self = false + +[weapon] +kills_user = false +model.type = "lazer" +model.range = 120 [red_policy] aggressive = true food = [1,0] kin = [-0.3,0.02] -nonkin = [0.3,-0.02] +nonkin = [0.3,0.02] none= [0.3, 0.01] [blu_policy] diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 019754b..6d22724 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -47,6 +47,7 @@ struct Robot { collider: Ball, team: Color, policy: Policy, + weapon: Weapon, } impl Mortal for Robot { @@ -71,8 +72,8 @@ impl Mortal for Robot { } impl Robot { - fn new(pose: Pose, team: Color, policy: Policy) -> Self { - Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), team, policy} + fn new(pose: Pose, team: Color, policy: Policy, weapon: Weapon) -> Self { + Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), team, policy, weapon} } fn move_step(&mut self, forward_right: [f32;2]) { @@ -108,13 +109,16 @@ impl Robot { } fn weapon<'a>(&self, peers: impl Iterator) { - let weapon = Ball::new(KILL_RADIUS); + let (pose, weapon) = self.weapon.model.collider(&self); 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() { + if let Some(_) = contact(&pose, &*weapon, p_p , p_c, 0.0).unwrap() { peer.kill(); + if self.weapon.kills_user { + self.kill(); // o7 + } } } @@ -189,6 +193,39 @@ struct Policy { none: [f32;2], } +#[derive(Deserialize, Clone)] +#[serde(rename_all="lowercase", tag="type")] +enum WeaponType { + Ball { radius: f32 }, // radius + Lazer { range: f32 }, // range + /// Ball centered at end of detection range + ForwardBall {radius: f32}, // radius +} + +impl WeaponType { + pub fn collider(&self, robot: &Robot) -> (Pose, Box) { + match self { + WeaponType::Ball{radius} => { + (robot.pose, Box::new(Ball::new(*radius))) + }, + WeaponType::Lazer{range} => { + let end = robot.pose.translation + robot.ahead() * *range; + (Pose::identity(), + Box::new(Segment::new(robot.pose.translation, end))) + }, + WeaponType::ForwardBall{radius} => { + (Pose::from_translation(robot.view_end()), Box::new(Ball::new(*radius))) + }, + } + } +} + +#[derive(Deserialize, Clone)] +struct Weapon { + kills_user: bool, + model: WeaponType, +} + #[derive(Deserialize)] struct SimConfig { food_amount: usize, @@ -196,8 +233,7 @@ struct SimConfig { red_policy: Policy, blu_count: usize, blu_policy: Policy, - kill_radius: f32, - kill_self: bool, + weapon: Weapon, } struct Simulation { @@ -225,14 +261,14 @@ impl Simulation { for i in 0..config.blu_count { let rand_angle = angle.sample(&mut rng); let pose:Pose = Pose::from_parts(Vec2 { x: 100.0, y: i as f32*50.0 + 250.0}, Rot2::from_angle(rand_angle)); - robots.push(Robot::new(pose, BLUE, config.blu_policy.clone())); + robots.push(Robot::new(pose, BLUE, config.blu_policy.clone(), config.weapon.clone())); } //red robots for i in 0..config.red_count { let rand_angle = angle.sample(&mut rng); let pose:Pose = Pose::from_parts(Vec2 { x: 1200.0, y: i as f32*50.0 + 250.0}, Rot2::from_angle(rand_angle)); - robots.push(Robot::new(pose, RED, config.red_policy.clone())); + robots.push(Robot::new(pose, RED, config.red_policy.clone(), config.weapon.clone())); } let mut food = Vec::new();