Skip to content

Commit

Permalink
Merge pull request #207 from tongohiti/fix-rust-smartguy
Browse files Browse the repository at this point in the history
Bugfixes for the Rust example strategy
  • Loading branch information
BorisKolganov authored Sep 3, 2018
2 parents b42ea1e + dc0ccaf commit 23f6782
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
18 changes: 9 additions & 9 deletions madcars/examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ name = "strategy"
version = "0.0.0"

[dependencies]
chrono = "0.4.1"
fern = "0.5.5"
flexi_logger = "0.8.1"
lazy_static = "1.0.0"
log = "0.4.1"
num-traits = "0.2.2"
rand = "0.4.2"
#chrono = "0.4.1"
#fern = "0.5.5"
#flexi_logger = "0.8.1"
#lazy_static = "1.0.0"
#log = "0.4.1"
#num-traits = "0.2.2"
#rand = "0.4.2"
serde = "1.0.13"
serde_derive = "1.0.13"
serde_json = "1.0.13"
simplelog = "0.5.1"
time = "0.1.39"
#simplelog = "0.5.1"
#time = "0.1.39"
43 changes: 24 additions & 19 deletions madcars/examples/rust/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,13 @@ fn run<T: Strategy>(mut strategy: T) {
// Process the message
let response = handle_message(msg, &mut strategy);

// Check whether there's a response
if response.is_none() {
continue;
}

// Encode response to JSON
let mut output = serde_json::to_string(&response).unwrap();
let mut output = serde_json::to_string(&response.unwrap()).unwrap();
output.push_str("\n");

// Write response JSON to stdout
Expand All @@ -40,9 +45,9 @@ fn run<T: Strategy>(mut strategy: T) {
}

/// Handle input message
fn handle_message<T: Strategy>(msg: Message, strategy: &mut T) -> Response {
// Handle each message type, produce command
let (cmd, debug_msg) = match msg.msg_type {
fn handle_message<T: Strategy>(msg: Message, strategy: &mut T) -> Option<Response> {
// Handle each message type, produce optional response
match msg.msg_type {
MessageType::NewMatch => {
// Extract message parameters
let (my_lives, enemy_lives, proto_map, proto_car) = (
Expand All @@ -55,8 +60,8 @@ fn handle_message<T: Strategy>(msg: Message, strategy: &mut T) -> Response {
// Call strategy's initialization function
strategy.begin_match(my_lives, enemy_lives, proto_map, proto_car);

// Return 'stop' command and a debug message
(Command::Stop, "Starting new match!")
// This message shouldn't be answered
None
}

MessageType::Tick => {
Expand All @@ -71,14 +76,11 @@ fn handle_message<T: Strategy>(msg: Message, strategy: &mut T) -> Response {
let cmd = strategy.on_tick(my_car, enemy_car, deadline_pos);

// Pass strategy's returned command plus a debug message
(cmd, "Drive ahead!!")
Some(Response {
command: cmd,
debug: Some("Drive ahead!!".to_string()),
})
}
};

// Create a response
Response {
command: cmd,
debug: Some(debug_msg.to_string()),
}
}

Expand Down Expand Up @@ -146,8 +148,8 @@ mod model {
pub car_body_friction: f64,
pub car_body_elasticity: f64,
pub max_speed: f64,
pub max_angular_speed: f64,
pub drive: f64,
pub torque: f64,
pub drive: u32, // Drive type: 1=FF, 2=FR, 3=AWD

pub rear_wheel_mass: f64,
pub rear_wheel_position: Point2D,
Expand All @@ -168,6 +170,8 @@ mod model {
pub front_wheel_damp_length: f64,
pub front_wheel_damp_stiffness: f64,
pub front_wheel_damp_damping: f64,

pub squared_wheels: Option<bool>,
}

/// Car state
Expand Down Expand Up @@ -237,13 +241,14 @@ mod strategy {
fn on_tick(&mut self, _my_car: Car, _enemy_car: Car, _deadline_pos: f64) -> Command {
self.tick += 1;

// Print to console (stderr)
eprintln!("Processing tick #{}", self.tick);
let t = self.tick % 9;

if (self.tick % 3) != 0 {
if t < 4 {
Command::Left
} else {
} else if t > 5 {
Command::Right
} else {
Command::Stop
}
}
}
Expand Down

0 comments on commit 23f6782

Please sign in to comment.