diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-07-01 22:24:49 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-07-01 22:24:49 +0200 |
commit | c4fe897b41f90a53e0629e2444d3e7f2121cddde (patch) | |
tree | d98ef1163aa8ac6c18e9607a4808661449a818be /src | |
parent | 945ae96707f6b4065c89463047979a18ad8181aa (diff) |
JSON parsing of bitwise buildings
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/bitwise_engine.rs | 23 | ||||
-rw-r--r-- | src/input/json.rs | 97 |
2 files changed, 81 insertions, 39 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 ) } |