diff options
-rw-r--r-- | src/engine/bitwise_engine.rs | 23 | ||||
-rw-r--r-- | src/input/json.rs | 97 | ||||
-rw-r--r-- | tests/expressive_to_bitwise_comparison.rs | 10 |
3 files changed, 86 insertions, 44 deletions
diff --git a/src/engine/bitwise_engine.rs b/src/engine/bitwise_engine.rs index 4fad36f..04e4f85 100644 --- a/src/engine/bitwise_engine.rs +++ b/src/engine/bitwise_engine.rs @@ -40,7 +40,7 @@ pub struct UnconstructedBuilding { pub building_type: BuildingType } -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct TeslaCooldown { pub active: bool, pub pos: Point, @@ -82,4 +82,25 @@ impl PlayerBuildings { pub fn count_teslas(&self) -> usize { self.tesla_cooldowns.iter().filter(|t| t.active).count() } + + pub fn empty() -> PlayerBuildings { + PlayerBuildings { + unconstructed: Vec::with_capacity(4), + buildings: [0; 4], + energy_towers: 0, + missile_towers: [0; 4], + missiles: [(0,0); 4], + tesla_cooldowns: [TeslaCooldown::empty(); 2] + } + } +} + +impl TeslaCooldown { + pub fn empty() -> TeslaCooldown { + TeslaCooldown { + active: false, + pos: Point::new(0,0), + cooldown: 0 + } + } } diff --git a/src/input/json.rs b/src/input/json.rs index 0ed86e7..0c0ed48 100644 --- a/src/input/json.rs +++ b/src/input/json.rs @@ -84,8 +84,8 @@ struct Player { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct GameCell { - //x: u8, - //y: u8, + x: u8, + y: u8, buildings: Vec<BuildingState>, missiles: Vec<MissileState>, //cell_owner: char @@ -150,43 +150,64 @@ impl State { } fn to_bitwise_engine(&self) -> bitwise_engine::BitwiseGameState { - let player_buildings = self.buildings_to_expressive_engine('A'); - let opponent_buildings = self.buildings_to_expressive_engine('B'); - bitwise_engine::BitwiseGameState::new( - self.player().to_bitwise_engine(), - self.opponent().to_bitwise_engine(), - bitwise_engine::PlayerBuildings { - unconstructed: self.unconstructed_buildings_to_bitwise_engine('A'), - buildings: [0,0,0,0], - energy_towers: 0, - missile_towers: [0,0,0,0], - missiles: [(0,0),(0,0),(0,0),(0,0)], - tesla_cooldowns: [bitwise_engine::TeslaCooldown { - active: false, - pos: engine::geometry::Point::new(0,0), - cooldown: 0 - }, bitwise_engine::TeslaCooldown { - active: false, - pos: engine::geometry::Point::new(0,0), - cooldown: 0 - }] - }, - bitwise_engine::PlayerBuildings { - unconstructed: Vec::new(), - buildings: [0,0,0,0], - energy_towers: 0, - missile_towers: [0,0,0,0], - missiles: [(0,0),(0,0),(0,0),(0,0)], - tesla_cooldowns: [bitwise_engine::TeslaCooldown { - active: false, - pos: engine::geometry::Point::new(0,0), - cooldown: 0 - }, bitwise_engine::TeslaCooldown { - active: false, - pos: engine::geometry::Point::new(0,0), - cooldown: 0 - }] + let mut player = self.player().to_bitwise_engine(); + let mut opponent = self.opponent().to_bitwise_engine(); + let mut player_buildings = bitwise_engine::PlayerBuildings::empty(); + let mut opponent_buildings = bitwise_engine::PlayerBuildings::empty(); + for row in &self.game_map { + for cell in row { + let point = engine::geometry::Point::new(cell.x, cell.y); + for building in &cell.buildings { + let building_type = building.convert_building_type(); + + let (mut engine_player, mut bitwise_buildings, bitfield) = if building.player_type == 'A' { + (&mut player, &mut player_buildings, point.to_left_bitfield(8)) + } else { + (&mut opponent, &mut opponent_buildings, point.to_right_bitfield(8)) + }; + + if building.construction_time_left >= 0 { + bitwise_buildings.unconstructed.push(building.to_bitwise_engine_unconstructed()); + } else { + for health_tier in 0..4 { + if building.health > health_tier*5 { + bitwise_buildings.buildings[health_tier as usize] |= bitfield; + } + } + if building_type == command::BuildingType::Energy { + bitwise_buildings.energy_towers |= bitfield; + engine_player.energy_generated += building.energy_generated_per_turn; + } + if building_type == command::BuildingType::Attack { + for cooldown_tier in 0..4 { + if building.weapon_cooldown_time_left == cooldown_tier { + bitwise_buildings.missile_towers[cooldown_tier as usize] |= bitfield; + } + } + } + if building_type == command::BuildingType::Tesla { + let ref mut tesla_cooldown = if bitwise_buildings.tesla_cooldowns[0].active { + bitwise_buildings.tesla_cooldowns[1] + } else { + bitwise_buildings.tesla_cooldowns[0] + }; + tesla_cooldown.active = true; + tesla_cooldown.pos = point; + tesla_cooldown.cooldown = building.weapon_cooldown_time_left; + } + } + } + for missile in &cell.missiles { + if missile.player_type == 'A' { + } else { + } + } } + } + + bitwise_engine::BitwiseGameState::new( + player, opponent, + player_buildings, opponent_buildings ) } diff --git a/tests/expressive_to_bitwise_comparison.rs b/tests/expressive_to_bitwise_comparison.rs index e534fca..9ce5288 100644 --- a/tests/expressive_to_bitwise_comparison.rs +++ b/tests/expressive_to_bitwise_comparison.rs @@ -116,17 +116,17 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS .fold(0, |acc, next| acc | next.pos.to_left_bitfield(8)); let opponent_energy = expressive.opponent_buildings.iter() .filter(|b| identify_building_type(b.weapon_damage, b.energy_generated_per_turn) == BuildingType::Energy) - .fold(0, |acc, next| acc | next.pos.to_left_bitfield(8)); + .fold(0, |acc, next| acc | next.pos.to_right_bitfield(8)); let mut player_buildings_iter = (0..4) .map(|i| expressive.player_buildings.iter() - .filter(|b| b.health >= i*5) + .filter(|b| b.health > i*5) .fold(0, |acc, next| acc | next.pos.to_left_bitfield(8)) ); let mut opponent_buildings_iter = (0..4) .map(|i| expressive.opponent_buildings.iter() - .filter(|b| b.health >= i*5) - .fold(0, |acc, next| acc | next.pos.to_left_bitfield(8)) + .filter(|b| b.health > i*5) + .fold(0, |acc, next| acc | next.pos.to_right_bitfield(8)) ); let mut player_attack_iter = (0..4) @@ -139,7 +139,7 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS .map(|i| expressive.opponent_buildings.iter() .filter(|b| identify_building_type(b.weapon_damage, b.energy_generated_per_turn) == BuildingType::Attack) .filter(|b| b.weapon_cooldown_time_left == i) - .fold(0, |acc, next| acc | next.pos.to_left_bitfield(8)) + .fold(0, |acc, next| acc | next.pos.to_right_bitfield(8)) ); let empty_missiles: [(u64,u64);4] = [(0,0),(0,0),(0,0),(0,0)]; |