summaryrefslogtreecommitdiff
path: root/src/strategy
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-05-10 23:01:22 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-05-10 23:01:22 +0200
commit4755702ef08d70961b5248cb706a592a406d0556 (patch)
treebfe8ff9e016338a58b6d0d9314d1b67b17cafea9 /src/strategy
parent11c791a59ac60241f253cdfb4e8765039d15edff (diff)
Split to library. Reimplemented sample strategy in new state.
Diffstat (limited to 'src/strategy')
-rw-r--r--src/strategy/mod.rs1
-rw-r--r--src/strategy/sample.rs38
2 files changed, 39 insertions, 0 deletions
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
+}