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