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/engine/command.rs | 6 +-- src/engine/mod.rs | 147 ++++++++++++++++++++++++++++++++------------------ 2 files changed, 98 insertions(+), 55 deletions(-) (limited to 'src/engine') diff --git a/src/engine/command.rs b/src/engine/command.rs index 603ee42..eab98c1 100644 --- a/src/engine/command.rs +++ b/src/engine/command.rs @@ -9,11 +9,9 @@ pub enum Command { impl fmt::Display for Command { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use Command::*; - match self { - &Nothing => write!(f, ""), - &Build(p, b) => write!(f, "{},{},{}", p.x, p.y, b as u8), + &Command::Nothing => write!(f, ""), + &Command::Build(p, b) => write!(f, "{},{},{}", p.x, p.y, b as u8), } } } diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 4ea63a3..be95c03 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -29,12 +29,6 @@ pub enum GameStatus { InvalidMove } -impl GameStatus { - fn is_complete(&self) -> bool { - *self != GameStatus::Continue - } -} - #[derive(Debug, Clone)] pub struct Player { pub energy: u16, @@ -53,51 +47,6 @@ pub struct Building { pub energy_generated_per_turn: u16 } -impl Building { - fn new(pos: Point, building: BuildingType) -> Building { - match building { - BuildingType::Defense => Building { - pos: pos, - health: 20, - construction_time_left: 3, - weapon_damage: 0, - weapon_speed: 0, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 0, - energy_generated_per_turn: 0 - }, - BuildingType::Attack => Building { - pos: pos, - health: 5, - construction_time_left: 1, - weapon_damage: 5, - weapon_speed: 1, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 3, - energy_generated_per_turn: 0 - }, - BuildingType::Energy => Building { - pos: pos, - health: 5, - construction_time_left: 1, - weapon_damage: 0, - weapon_speed: 0, - weapon_cooldown_time_left: 0, - weapon_cooldown_period: 0, - energy_generated_per_turn: 3 - } - } - } - - fn is_constructed(&self) -> bool { - self.construction_time_left == 0 - } - - fn is_shooty(&self) -> bool { - self.is_constructed() && self.weapon_damage > 0 - } -} - #[derive(Debug, Clone)] pub struct Missile { pub pos: Point, @@ -215,4 +164,100 @@ impl GameState { (false, false) => GameStatus::Continue, }; } + + pub fn unoccupied_player_cells_in_row(&self, settings: &GameSettings, y: u8) -> Vec { + (0..settings.size.x/2) + .map(|x| Point::new(x, y)) + .filter(|&p| !self.player_buildings.iter().any(|b| b.pos == p)) + .collect() + } + + pub fn unoccupied_player_cells(&self, settings: &GameSettings) -> Vec { + (0..settings.size.y) + .flat_map(|y| (0..settings.size.x/2).map(|x| Point::new(x, y)).collect::>()) + .filter(|&p| !self.player_buildings.iter().any(|b| b.pos == p)) + .collect() + } +} + +impl GameStatus { + fn is_complete(&self) -> bool { + *self != GameStatus::Continue + } +} + +impl Player { + pub fn can_afford_all_buildings(&self, settings: &GameSettings) -> bool { + self.can_afford_attack_buildings(settings) && + self.can_afford_defence_buildings(settings) && + self.can_afford_energy_buildings(settings) + } + + pub fn can_afford_attack_buildings(&self, settings: &GameSettings) -> bool { + self.energy >= settings.attack_price + } + pub fn can_afford_defence_buildings(&self, settings: &GameSettings) -> bool { + self.energy >= settings.defence_price + } + pub fn can_afford_energy_buildings(&self, settings: &GameSettings) -> bool { + self.energy >= settings.energy_price + } + +} + +impl Building { + fn new(pos: Point, building: BuildingType) -> Building { + match building { + BuildingType::Defense => Building { + pos: pos, + health: 20, + construction_time_left: 3, + weapon_damage: 0, + weapon_speed: 0, + weapon_cooldown_time_left: 0, + weapon_cooldown_period: 0, + energy_generated_per_turn: 0 + }, + BuildingType::Attack => Building { + pos: pos, + health: 5, + construction_time_left: 1, + weapon_damage: 5, + weapon_speed: 1, + weapon_cooldown_time_left: 0, + weapon_cooldown_period: 3, + energy_generated_per_turn: 0 + }, + BuildingType::Energy => Building { + pos: pos, + health: 5, + construction_time_left: 1, + weapon_damage: 0, + weapon_speed: 0, + weapon_cooldown_time_left: 0, + weapon_cooldown_period: 0, + energy_generated_per_turn: 3 + } + } + } + + fn is_constructed(&self) -> bool { + self.construction_time_left == 0 + } + + fn is_shooty(&self) -> bool { + self.is_constructed() && self.weapon_damage > 0 + } +} + +#[test] +fn how_big() { + use std::mem; + assert_eq!(4, mem::size_of::()); + assert_eq!(12, mem::size_of::()); + assert_eq!(6, mem::size_of::()); + assert_eq!(112, mem::size_of::()); + assert_eq!(24, mem::size_of::>()); + assert_eq!(24, mem::size_of::>()); + } -- cgit v1.2.3