1
Fork 0

define weapon in toml

This commit is contained in:
Andy Killorin 2026-11-10 00:07:07 -05:00
parent 8c2e9f37eb
commit 1d36c028cd
Signed by: ank
GPG key ID: 23F9463ECB67FE8C
2 changed files with 50 additions and 11 deletions

View file

@ -1,14 +1,17 @@
food_amount = 120 food_amount = 120
red_count = 10 red_count = 10
blu_count = 10 blu_count = 10
kill_radius = 140
kill_self = false [weapon]
kills_user = false
model.type = "lazer"
model.range = 120
[red_policy] [red_policy]
aggressive = true aggressive = true
food = [1,0] food = [1,0]
kin = [-0.3,0.02] kin = [-0.3,0.02]
nonkin = [0.3,-0.02] nonkin = [0.3,0.02]
none= [0.3, 0.01] none= [0.3, 0.01]
[blu_policy] [blu_policy]

View file

@ -47,6 +47,7 @@ struct Robot {
collider: Ball, collider: Ball,
team: Color, team: Color,
policy: Policy, policy: Policy,
weapon: Weapon,
} }
impl Mortal for Robot { impl Mortal for Robot {
@ -71,8 +72,8 @@ impl Mortal for Robot {
} }
impl Robot { impl Robot {
fn new(pose: Pose, team: Color, policy: Policy) -> Self { fn new(pose: Pose, team: Color, policy: Policy, weapon: Weapon) -> Self {
Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), team, policy} Self { pose, alive: AtomicBool::new(true), collider: Ball::new(BOT_RADIUS), team, policy, weapon}
} }
fn move_step(&mut self, forward_right: [f32;2]) { fn move_step(&mut self, forward_right: [f32;2]) {
@ -108,13 +109,16 @@ impl Robot {
} }
fn weapon<'a>(&self, peers: impl Iterator<Item = &'a dyn Mortal>) { fn weapon<'a>(&self, peers: impl Iterator<Item = &'a dyn Mortal>) {
let weapon = Ball::new(KILL_RADIUS); let (pose, weapon) = self.weapon.model.collider(&self);
for peer in peers { for peer in peers {
let (p_p, p_c) = peer.collider(); 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(); peer.kill();
if self.weapon.kills_user {
self.kill(); // o7
}
} }
} }
@ -189,6 +193,39 @@ struct Policy {
none: [f32;2], 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<dyn Shape>) {
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)] #[derive(Deserialize)]
struct SimConfig { struct SimConfig {
food_amount: usize, food_amount: usize,
@ -196,8 +233,7 @@ struct SimConfig {
red_policy: Policy, red_policy: Policy,
blu_count: usize, blu_count: usize,
blu_policy: Policy, blu_policy: Policy,
kill_radius: f32, weapon: Weapon,
kill_self: bool,
} }
struct Simulation { struct Simulation {
@ -225,14 +261,14 @@ impl Simulation {
for i in 0..config.blu_count { for i in 0..config.blu_count {
let rand_angle = angle.sample(&mut rng); 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)); 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 //red robots
for i in 0..config.red_count { for i in 0..config.red_count {
let rand_angle = angle.sample(&mut rng); 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)); 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(); let mut food = Vec::new();