From d23a63288dec711b93dfa6702233c29287918cd9 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sun, 1 Jul 2018 21:07:07 +0200 Subject: Built bitwise game state from current game state --- src/engine/bitwise_engine.rs | 12 ++++++------ src/engine/expressive_engine.rs | 4 ++-- src/engine/geometry.rs | 28 ++++++++++++++++++++++++++++ src/engine/mod.rs | 4 ++-- src/input/json.rs | 10 ++++------ 5 files changed, 42 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/engine/bitwise_engine.rs b/src/engine/bitwise_engine.rs index bb1dd76..85c4352 100644 --- a/src/engine/bitwise_engine.rs +++ b/src/engine/bitwise_engine.rs @@ -27,12 +27,10 @@ pub struct PlayerBuildings { pub buildings: [u64; DEFENCE_HEALTH], pub energy_towers: u64, - pub missile_towers: [u64; MISSILE_COOLDOWN], + pub missile_towers: [u64; MISSILE_COOLDOWN+1], pub missiles: [(u64, u64); MAP_WIDTH/4], - pub tesla_cooldowns: [TeslaCooldown; MAX_TESLAS], - - pub unoccupied: Vec + pub tesla_cooldowns: [TeslaCooldown; MAX_TESLAS] } #[derive(Debug, Clone, PartialEq, Eq)] @@ -50,6 +48,8 @@ pub struct TeslaCooldown { } +const EMPTY: [Point; 0] = []; + impl GameState for BitwiseGameState { fn simulate(&mut self, _settings: &GameSettings, _player_command: Command, _opponent_command: Command) -> GameStatus { //TODO @@ -61,8 +61,8 @@ impl GameState for BitwiseGameState { fn opponent(&self) -> &Player { &self.opponent } fn player_has_max_teslas(&self) -> bool { self.player_buildings.count_teslas() >= MAX_TESLAS } fn opponent_has_max_teslas(&self) -> bool { self.opponent_buildings.count_teslas() >= MAX_TESLAS } - fn unoccupied_player_cells(&self) -> &Vec { &self.player_buildings.unoccupied } - fn unoccupied_opponent_cells(&self) -> &Vec { &self.opponent_buildings.unoccupied } + fn unoccupied_player_cells(&self) -> &[Point] { &EMPTY } + fn unoccupied_opponent_cells(&self) -> &[Point] { &EMPTY } } impl PlayerBuildings { diff --git a/src/engine/expressive_engine.rs b/src/engine/expressive_engine.rs index f1255c3..0640d58 100644 --- a/src/engine/expressive_engine.rs +++ b/src/engine/expressive_engine.rs @@ -89,8 +89,8 @@ impl GameState for ExpressiveGameState { fn opponent(&self) -> &Player { &self.opponent } fn player_has_max_teslas(&self) -> bool { self.count_player_teslas() >= 2 } fn opponent_has_max_teslas(&self) -> bool { self.count_opponent_teslas() >= 2 } - fn unoccupied_player_cells(&self) -> &Vec { &self.unoccupied_player_cells } - fn unoccupied_opponent_cells(&self) -> &Vec { &self.unoccupied_opponent_cells } + fn unoccupied_player_cells(&self) -> &[Point] { &self.unoccupied_player_cells } + fn unoccupied_opponent_cells(&self) -> &[Point] { &self.unoccupied_opponent_cells } } impl ExpressiveGameState { diff --git a/src/engine/geometry.rs b/src/engine/geometry.rs index 44ce9fe..22b56f0 100644 --- a/src/engine/geometry.rs +++ b/src/engine/geometry.rs @@ -31,6 +31,34 @@ impl Point { pub fn wrapping_move_right(&mut self) { self.x = self.x.wrapping_add(1); } + + pub fn to_bitfield(&self, width: u8) -> (u64, u64) { + if self.x >= width { + let index = self.y * width + self.x - width; + (0, 1 << index) + } else { + let index = self.y * width + self.x; + (1 << index, 0) + } + } + + pub fn to_left_bitfield(&self, width: u8) -> u64 { + if self.x >= width { + 0 + } else { + let index = self.y * width + self.x; + 1 << index + } + } + + pub fn to_right_bitfield(&self, width: u8) -> u64 { + if self.x < width { + 0 + } else { + let index = self.y * width + self.x - width; + 1 << index + } + } } use std::cmp::Ord; diff --git a/src/engine/mod.rs b/src/engine/mod.rs index 6e7c5c9..39a5f26 100644 --- a/src/engine/mod.rs +++ b/src/engine/mod.rs @@ -15,8 +15,8 @@ pub trait GameState: Clone + Sync { fn opponent(&self) -> &Player; fn player_has_max_teslas(&self) -> bool; fn opponent_has_max_teslas(&self) -> bool; - fn unoccupied_player_cells(&self) -> &Vec; - fn unoccupied_opponent_cells(&self) -> &Vec; + fn unoccupied_player_cells(&self) -> &[Point]; + fn unoccupied_opponent_cells(&self) -> &[Point]; } #[derive(Debug, Clone, Copy, PartialEq, Eq)] diff --git a/src/input/json.rs b/src/input/json.rs index fb88bf0..319c77a 100644 --- a/src/input/json.rs +++ b/src/input/json.rs @@ -32,7 +32,7 @@ pub fn read_bitwise_state_from_file(filename: &str) -> Result Result Result