From 27499d0f466e0405dba10f8f5b98533beead1c9e Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Tue, 15 May 2018 23:15:36 +0200 Subject: Additional code and bug fixes to help end to end tests --- src/engine/geometry.rs | 14 ++++++++++++++ src/engine/mod.rs | 31 ++++++++++++++++++++++--------- src/json.rs | 4 ++-- 3 files changed, 38 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/engine/geometry.rs b/src/engine/geometry.rs index a946bf9..23e0433 100644 --- a/src/engine/geometry.rs +++ b/src/engine/geometry.rs @@ -25,3 +25,17 @@ impl Point { } } } + +use std::cmp::Ord; +use std::cmp::Ordering; + +impl PartialOrd for Point { + fn partial_cmp(&self, other: &Point) -> Option { + Some(self.cmp(other)) + } +} +impl Ord for Point { + fn cmp(&self, other: &Point) -> Ordering { + self.x.cmp(&other.x).then(self.y.cmp(&other.y)) + } +} diff --git a/src/engine/mod.rs b/src/engine/mod.rs index f8a456f..6bc200c 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -9,7 +9,7 @@ use self::settings::{GameSettings, BuildingSettings}; use std::ops::Fn; use std::cmp; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct GameState { pub status: GameStatus, pub player: Player, @@ -30,13 +30,13 @@ pub enum GameStatus { Draw } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Player { pub energy: u16, pub health: u16 } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Building { pub pos: Point, pub health: u16, @@ -48,7 +48,7 @@ pub struct Building { pub energy_generated_per_turn: u16 } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub struct Missile { pub pos: Point, pub damage: u16, @@ -76,6 +76,19 @@ impl GameState { } } + /** + * Sorts the various arrays. Generally not necessary, but useful + * for tests that check equality between states. + */ + pub fn sort(&mut self) { + self.player_buildings.sort_by_key(|b| b.pos); + self.unoccupied_player_cells.sort(); + self.opponent_buildings.sort_by_key(|b| b.pos); + self.unoccupied_opponent_cells.sort(); + self.player_missiles.sort_by_key(|b| b.pos); + self.opponent_missiles.sort_by_key(|b| b.pos); + } + pub fn simulate(&self, settings: &GameSettings, player_command: Command, opponent_command: Command) -> GameState { let mut state = self.clone(); state.simulate_mut(settings, player_command, opponent_command); @@ -87,9 +100,6 @@ impl GameState { return; } - GameState::perform_command(&mut self.player_buildings, &mut self.player, &mut self.unoccupied_player_cells, settings, player_command, &settings.size); - GameState::perform_command(&mut self.opponent_buildings, &mut self.opponent, &mut self.unoccupied_opponent_cells, settings, opponent_command, &settings.size); - GameState::update_construction(&mut self.player_buildings); GameState::update_construction(&mut self.opponent_buildings); @@ -106,6 +116,9 @@ impl GameState { GameState::add_energy(&mut self.player, settings, &self.player_buildings); GameState::add_energy(&mut self.opponent, settings, &self.opponent_buildings); + GameState::perform_command(&mut self.player_buildings, &mut self.player, &mut self.unoccupied_player_cells, settings, player_command, &settings.size); + GameState::perform_command(&mut self.opponent_buildings, &mut self.opponent, &mut self.unoccupied_opponent_cells, settings, opponent_command, &settings.size); + GameState::update_status(self); } @@ -161,7 +174,7 @@ impl GameState { }, Some(point) => { missile.pos = point; - for hit in opponent_buildings.iter_mut().filter(|b| b.is_constructed() && b.pos == point/* && b.health > 0*/) { //TODO surely this health>0 belongs? Not what the real game engine is doing unfortunately + for hit in opponent_buildings.iter_mut().filter(|b| b.is_constructed() && b.pos == point) { let damage = cmp::min(missile.damage, hit.health); hit.health -= damage; missile.speed = 0; @@ -187,7 +200,7 @@ impl GameState { fn add_energy(player: &mut Player, settings: &GameSettings, buildings: &Vec) { player.energy += settings.energy_income; - player.energy += buildings.iter().map(|b| b.energy_generated_per_turn).sum::(); + player.energy += buildings.iter().filter(|b| b.is_constructed()).map(|b| b.energy_generated_per_turn).sum::(); } fn update_status(state: &mut GameState) { diff --git a/src/json.rs b/src/json.rs index 4253a19..239a351 100644 --- a/src/json.rs +++ b/src/json.rs @@ -140,7 +140,7 @@ impl State { fn opponent(&self) -> &Player { self.players.iter() - .filter(|p| p.player_type != 'B') + .filter(|p| p.player_type == 'B') .next() .expect("Opponent character did not appear in state.json") } @@ -173,7 +173,7 @@ impl BuildingBlueprint { engine::settings::BuildingSettings { price: self.price, health: self.health, - construction_time: self.construction_time, + construction_time: self.construction_time-2, weapon_damage: self.weapon_damage, weapon_speed: self.weapon_speed, weapon_cooldown_period: self.weapon_cooldown_period, -- cgit v1.2.3