Changed other bitwise stuff to use constants
authorJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Jul 2018 21:41:58 +0000 (23:41 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Thu, 12 Jul 2018 21:41:58 +0000 (23:41 +0200)
src/engine/bitwise_engine.rs
src/input/json.rs
tests/expressive_to_bitwise_comparison.rs
tests/live_comparison.rs

index db4ad9a..87b7f0f 100644 (file)
@@ -306,6 +306,7 @@ impl BitwiseGameState {
                 for _ in 0..(if tesla.pos.y == 0 || tesla.pos.y == MAP_HEIGHT-1 { 2 } else { 3 }) {
                     let hits = destroy_mask & opponent_buildings.buildings[0];
                     destroy_mask &= !hits;
+                    //TODO: This can probably be pulled out of the loop
                     BitwiseGameState::destroy_buildings(opponent_buildings, hits);
                     destroy_mask = destroy_mask << SINGLE_MAP_WIDTH;
                 }
index c032f48..72c964d 100644 (file)
@@ -7,6 +7,7 @@ use engine;
 use engine::command;
 use engine::expressive_engine;
 use engine::bitwise_engine;
+use engine::constants::*;
 
 pub fn read_expressive_state_from_file(filename: &str) -> Result<(engine::settings::GameSettings, expressive_engine::ExpressiveGameState), Box<Error>> {
     let mut file = File::open(filename)?;
@@ -171,9 +172,9 @@ impl State {
                     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;
+                        for health_tier in 0..DEFENCE_HEALTH {
+                            if building.health > health_tier as u8 * MISSILE_DAMAGE {
+                                bitwise_buildings.buildings[health_tier] |= bitfield;
                             }
                         }
                         if building_type == command::BuildingType::Energy {
@@ -181,9 +182,9 @@ impl State {
                             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;
+                            for cooldown_tier in 0..MISSILE_COOLDOWN + 1 {
+                                if building.weapon_cooldown_time_left == cooldown_tier as u8 {
+                                    bitwise_buildings.missile_towers[cooldown_tier] |= bitfield;
                                 }
                             }
                         }
@@ -296,7 +297,7 @@ impl Player {
         engine::Player {
             energy: self.energy,
             health: self.health,
-            energy_generated: 5
+            energy_generated: ENERGY_GENERATED_BASE
         }
     }
 }
index bdc4a19..ac2cbf0 100644 (file)
@@ -11,6 +11,7 @@ use zombot::engine::{GameState, GameStatus, Player};
 
 use zombot::engine::expressive_engine;
 use zombot::engine::bitwise_engine;
+use zombot::engine::constants::*;
 
 use proptest::prelude::*;
 
@@ -123,14 +124,14 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS
         .filter(|b| identify_building_type(b.weapon_damage, b.energy_generated_per_turn) == BuildingType::Energy)
         .fold(0, |acc, next| acc | next.pos.to_right_bitfield());
 
-    let mut player_buildings_iter = (0..4)
+    let mut player_buildings_iter = (0..DEFENCE_HEALTH as u8)
         .map(|i| expressive.player_buildings.iter()
-             .filter(|b| b.health > i*5)
+             .filter(|b| b.health > i*MISSILE_DAMAGE)
              .fold(0, |acc, next| acc | next.pos.to_left_bitfield())
         );
-    let mut opponent_buildings_iter = (0..4)
+    let mut opponent_buildings_iter = (0..DEFENCE_HEALTH as u8)
         .map(|i| expressive.opponent_buildings.iter()
-             .filter(|b| b.health > i*5)
+             .filter(|b| b.health > i*MISSILE_DAMAGE)
              .fold(0, |acc, next| acc | next.pos.to_right_bitfield())
         );
 
@@ -143,20 +144,20 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS
     expressive.opponent_unconstructed_buildings.iter()
         .fold(0, |acc, next| acc | next.pos.to_right_bitfield());
 
-    let mut player_attack_iter = (0..4)
+    let mut player_attack_iter = (0..MISSILE_COOLDOWN as u8 + 1)
         .map(|i| expressive.player_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())
         );
-    let mut opponent_attack_iter = (0..4)
+    let mut opponent_attack_iter = (0..MISSILE_COOLDOWN as u8 + 1)
         .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_right_bitfield())
         );
 
-    let empty_missiles: [(u64,u64);4] = [(0,0),(0,0),(0,0),(0,0)];
+    let empty_missiles: [(u64,u64);MISSILE_COOLDOWN+1] = [(0,0),(0,0),(0,0),(0,0)];
     let player_missiles = expressive.player_missiles.iter()
         .fold(empty_missiles, |acc, m| {
             let (mut left, mut right) = m.pos.to_bitfield();
@@ -240,9 +241,9 @@ fn build_bitwise_unconstructed_from_expressive(b: &expressive_engine::Unconstruc
 
 fn identify_building_type(weapon_damage: u8, energy_generated_per_turn: u16) -> BuildingType {
     match (weapon_damage, energy_generated_per_turn) {
-        (5, _) => BuildingType::Attack,
-        (20, _) => BuildingType::Tesla,
-        (_, 3) => BuildingType::Energy,
+        (MISSILE_DAMAGE, _) => BuildingType::Attack,
+        (TESLA_DAMAGE, _) => BuildingType::Tesla,
+        (_, ENERGY_GENERATED_TOWER) => BuildingType::Energy,
         _ => BuildingType::Defence
     }
 }
index 91d3530..2fcc8b5 100644 (file)
@@ -20,12 +20,12 @@ fn it_successfully_simulates_replay_with_teslas() {
 }
 
 fn test_from_replay(replay_folder: &str, length: usize) {
-    let (settings, mut state) = json::read_expressive_state_from_file(&format!("{}/Round 000/state.json", replay_folder)).unwrap();
+    let (settings, mut state) = json::read_bitwise_state_from_file(&format!("{}/Round 000/state.json", replay_folder)).unwrap();
     
     for i in 0..length {
         let player = read_player_command(&format!("{}/Round {:03}/PlayerCommand.txt", replay_folder, i));
         let opponent = read_opponent_command(&format!("{}/Round {:03}/OpponentCommand.txt", replay_folder, i), &settings);
-        let (_, mut expected_state) = json::read_expressive_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder, i+1)).unwrap();
+        let (_, mut expected_state) = json::read_bitwise_state_from_file(&format!("{}/Round {:03}/state.json", replay_folder, i+1)).unwrap();
         
         state.simulate(&settings, player, opponent);
         state.sort();