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