diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-07-01 21:07:07 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-07-01 21:07:07 +0200 |
commit | d23a63288dec711b93dfa6702233c29287918cd9 (patch) | |
tree | 2847ed28290efdf1e7eadbb5b98c8d149f7cf847 /src | |
parent | 26aefe70fa11f209726e5b8a15bd05303726396e (diff) |
Built bitwise game state from current game state
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/bitwise_engine.rs | 12 | ||||
-rw-r--r-- | src/engine/expressive_engine.rs | 4 | ||||
-rw-r--r-- | src/engine/geometry.rs | 28 | ||||
-rw-r--r-- | src/engine/mod.rs | 4 | ||||
-rw-r--r-- | src/input/json.rs | 10 |
5 files changed, 42 insertions, 16 deletions
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<Point> + 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<Point> { &self.player_buildings.unoccupied } - fn unoccupied_opponent_cells(&self) -> &Vec<Point> { &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<Point> { &self.unoccupied_player_cells } - fn unoccupied_opponent_cells(&self) -> &Vec<Point> { &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<Point>; - fn unoccupied_opponent_cells(&self) -> &Vec<Point>; + 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<bitwise_engine::Bi unconstructed: Vec::new(), buildings: [0,0,0,0], energy_towers: 0, - missile_towers: [0,0,0], + missile_towers: [0,0,0,0], missiles: [(0,0),(0,0),(0,0),(0,0)], tesla_cooldowns: [bitwise_engine::TeslaCooldown { active: false, @@ -42,14 +42,13 @@ pub fn read_bitwise_state_from_file(filename: &str) -> Result<bitwise_engine::Bi active: false, pos: engine::geometry::Point::new(0,0), cooldown: 0 - }], - unoccupied: Vec::new() + }] }, opponent_buildings: bitwise_engine::PlayerBuildings { unconstructed: Vec::new(), buildings: [0,0,0,0], energy_towers: 0, - missile_towers: [0,0,0], + missile_towers: [0,0,0,0], missiles: [(0,0),(0,0),(0,0),(0,0)], tesla_cooldowns: [bitwise_engine::TeslaCooldown { active: false, @@ -59,8 +58,7 @@ pub fn read_bitwise_state_from_file(filename: &str) -> Result<bitwise_engine::Bi active: false, pos: engine::geometry::Point::new(0,0), cooldown: 0 - }], - unoccupied: Vec::new() + }] } }) } |