From 4755702ef08d70961b5248cb706a592a406d0556 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Thu, 10 May 2018 23:01:22 +0200 Subject: Split to library. Reimplemented sample strategy in new state. --- src/strategy/mod.rs | 1 + src/strategy/sample.rs | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/strategy/mod.rs create mode 100644 src/strategy/sample.rs (limited to 'src/strategy') diff --git a/src/strategy/mod.rs b/src/strategy/mod.rs new file mode 100644 index 0000000..ce8e751 --- /dev/null +++ b/src/strategy/mod.rs @@ -0,0 +1 @@ +pub mod sample; diff --git a/src/strategy/sample.rs b/src/strategy/sample.rs new file mode 100644 index 0000000..bd23916 --- /dev/null +++ b/src/strategy/sample.rs @@ -0,0 +1,38 @@ +use engine; +use engine::command::*; + + +pub fn choose_move(settings: &engine::settings::GameSettings, state: &engine::GameState) -> Command { + if state.player.can_afford_defence_buildings(settings) { + for y in 0..settings.size.y { + if is_under_attack(state, y) { + let p_options = state.unoccupied_player_cells_in_row(settings, y); + if let Some(&p) = p_options.first() { + return Command::Build(p, BuildingType::Defense); + } + } + } + } + + if state.player.can_afford_all_buildings(settings) { + let options = state.unoccupied_player_cells(settings); + let option = options.first(); + let buildings = [BuildingType::Attack, BuildingType::Defense, BuildingType::Energy]; + let building = buildings.first(); + match (option, building) { + (Some(&p), Some(&building)) => Command::Build(p, building), + _ => Command::Nothing + } + } + else { + Command::Nothing + } +} + +fn is_under_attack(state: &engine::GameState, y: u8) -> bool { + let attack = state.opponent_buildings.iter() + .any(|b| b.pos.y == y && b.weapon_damage > 0); + let defences = state.player_buildings.iter() + .any(|b| b.pos.y == y && b.health > 5); + attack && !defences +} -- cgit v1.2.3