Calibrated energy cutoff and turned it on by default
[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::command::Command;
7
8 use std::error::Error;
9
10 const STATE_PATH: &str = "state.json";
11
12 const COMMAND_PATH: &str = "command.txt";
13
14 use std::fs::File;
15 use std::io::prelude::*;
16 use std::process;
17
18 fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, start_time: &PreciseTime) -> Command {
19     #[cfg(not(feature = "reduced-time"))]
20     #[cfg(not(feature = "extended-time"))]
21     let max_time = Duration::milliseconds(1950);
22     
23     #[cfg(feature = "reduced-time")]
24     let max_time = Duration::milliseconds(950);
25
26     #[cfg(feature = "extended-time")]
27     let max_time = Duration::milliseconds(19950);
28     
29     strategy::monte_carlo::choose_move(settings, state, start_time, max_time)
30 }
31
32
33 fn write_command(filename: &str, command: Command) -> Result<(), Box<Error> > {
34     let mut file = File::create(filename)?;
35     write!(file, "{}", command)?;
36     Ok(())
37 }
38
39
40 fn main() {
41     let start_time = PreciseTime::now();
42     
43     println!("Reading in state.json file");
44     let (settings, state) = match input::json::read_state_from_file(STATE_PATH) {
45         Ok(ok) => ok,
46         Err(error) => {
47             println!("Error while parsing JSON file: {}", error);
48             process::exit(1);
49         }
50     };
51     let command = choose_move(&settings, &state, &start_time);
52
53     match write_command(COMMAND_PATH, command) {
54         Ok(()) => {}
55         Err(error) => {
56             println!("Error while writing command file: {}", error);
57             process::exit(1);
58         }
59     }
60
61     println!("Elapsed time: {}", start_time.to(PreciseTime::now()));
62 }