define weapon in toml
This commit is contained in:
parent
8c2e9f37eb
commit
1d36c028cd
2 changed files with 50 additions and 11 deletions
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -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<Item = &'a dyn Mortal>) {
|
||||
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<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)]
|
||||
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();
|
||||
|
|
|
|||
Loading…
Reference in a new issue