Limited bot to run within the 2 second window
[entelect-challenge-tower-defence.git] / src / strategy / sample.rs
1 use engine;
2 use engine::command::*;
3
4 use time::PreciseTime;
5
6 use rand::{thread_rng, Rng};
7
8 pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState, _start_time: &PreciseTime) -> Command {
9     let mut rng = thread_rng();
10     
11     if state.player.can_afford_defence_buildings(settings) {
12         for y in 0..settings.size.y {
13             if is_under_attack(state, y) {
14                 let p_options = state.unoccupied_player_cells_in_row(settings, y);
15                 if let Some(&p) = rng.choose(&p_options) {
16                     return Command::Build(p, BuildingType::Defence);
17                 }
18             }
19         }
20     }
21
22     if state.player.can_afford_all_buildings(settings) {
23         let options = state.unoccupied_player_cells(settings);
24         let option = rng.choose(&options);
25         let buildings = [BuildingType::Attack, BuildingType::Defence, BuildingType::Energy];
26         let building = rng.choose(&buildings);
27         match (option, building) {
28             (Some(&p), Some(&building)) => Command::Build(p, building),
29             _ => Command::Nothing
30         }
31     }
32     else {
33         Command::Nothing
34     }
35 }
36
37 fn is_under_attack(state: &engine::GameState, y: u8) -> bool {
38     let attack = state.opponent_buildings.iter()
39         .any(|b| b.pos.y == y && b.weapon_damage > 0);
40     let defences = state.player_buildings.iter()
41         .any(|b| b.pos.y == y && b.health > 5);
42     attack && !defences
43 }