Simplified duration handling to allow longer profiling
[entelect-challenge-tower-defence.git] / src / main.rs
1 extern crate zombot;
2 extern crate time;
3 use time::{PreciseTime, Duration};
4
5 use zombot::*;
6 use zombot::engine::constants::*;
7 use zombot::engine::command::Command;
8
9 use std::error::Error;
10
11 const STATE_PATH: &str = "state.json";
12
13 const COMMAND_PATH: &str = "command.txt";
14
15 use std::fs::File;
16 use std::io::prelude::*;
17 use std::process;
18
19 fn choose_move<GS:engine::GameState>(settings: &engine::settings::GameSettings, state: &GS, start_time: &PreciseTime) -> Command {
20     let max_time = Duration::milliseconds(MAX_TIME_MILLIS);
21     strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
22 }
23
24
25 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
26     let mut file = File::create(filename)?;
27     write!(file, "{}", command)?;
28     Ok(())
29 }
30
31
32 fn main() {
33     let start_time = PreciseTime::now();
34     
35     let (settings, state) = match input::json::read_bitwise_state_from_file(STATE_PATH) {
36         Ok(ok) => ok,
37         Err(error) => {
38             println!("Error while parsing JSON file: {}", error);
39             process::exit(1);
40         }
41     };
42     let command = choose_move(&settings, &state, &start_time);
43
44     match write_command(COMMAND_PATH, command) {
45         Ok(()) => {}
46         Err(error) => {
47             println!("Error while writing command file: {}", error);
48             process::exit(1);
49         }
50     }
51
52     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
53 }