Added an index, to make the missile towers a proper circular buffer
authorJustin Worthe <justin@worthe-it.co.za>
Sat, 21 Jul 2018 10:37:56 +0000 (12:37 +0200)
committerJustin Worthe <justin@worthe-it.co.za>
Sat, 21 Jul 2018 10:37:56 +0000 (12:37 +0200)
src/engine/bitwise_engine.rs
src/engine/constants.rs
tests/expressive_to_bitwise_comparison.rs

index 99e4d92..add7d31 100644 (file)
@@ -23,7 +23,9 @@ pub struct PlayerBuildings {
     pub occupied: u64,
     
     pub energy_towers: u64,
-    pub missile_towers: [u64; MISSILE_COOLDOWN+1],
+
+    pub missile_towers: [u64; MISSILE_COOLDOWN_STATES],
+    pub firing_tower: usize,
     
     pub missiles: [(u64, u64); MISSILE_MAX_SINGLE_CELL],
     pub tesla_cooldowns: [TeslaCooldown; TESLA_MAX]
@@ -178,6 +180,26 @@ impl BitwiseGameState {
 
         self.player_buildings.tesla_cooldowns.sort_by_key(|b| (!b.active, b.pos));
         self.opponent_buildings.tesla_cooldowns.sort_by_key(|b| (!b.active, b.pos));
+
+
+        while self.player_buildings.firing_tower > 0 {
+            self.player_buildings.firing_tower -= 1;
+            let zero = self.player_buildings.missile_towers[0];
+            for i in 1..self.player_buildings.missile_towers.len() {
+                self.player_buildings.missile_towers[i-1] = self.player_buildings.missile_towers[i];
+            }
+            let end = self.player_buildings.missile_towers.len()-1;
+            self.player_buildings.missile_towers[end] = zero;
+        }
+        while self.opponent_buildings.firing_tower > 0 {
+            self.opponent_buildings.firing_tower -= 1;
+            let zero = self.opponent_buildings.missile_towers[0];
+            for i in 1..self.opponent_buildings.missile_towers.len() {
+                self.opponent_buildings.missile_towers[i-1] = self.opponent_buildings.missile_towers[i];
+            }
+            let end = self.opponent_buildings.missile_towers.len()-1;
+            self.opponent_buildings.missile_towers[end] = zero;
+        }
     }
 
     #[cfg(debug_assertions)]
@@ -256,7 +278,7 @@ impl BitwiseGameState {
                     player_buildings.energy_towers |= bitfield;
                 }
                 if building_type == BuildingType::Attack {
-                    player_buildings.missile_towers[0] |= bitfield;
+                    player_buildings.missile_towers[player_buildings.firing_tower] |= bitfield;
                 }
                 if building_type == BuildingType::Tesla {
                     let ref mut tesla_cooldown = if player_buildings.tesla_cooldowns[0].active {
@@ -321,27 +343,15 @@ impl BitwiseGameState {
     }
 
     fn add_missiles(player_buildings: &mut PlayerBuildings) {
-        let mut missiles = player_buildings.missile_towers[0];
+        let mut missiles = player_buildings.missile_towers[player_buildings.firing_tower];
         for mut tier in player_buildings.missiles.iter_mut() {
             let setting = !tier.0 & missiles;
             tier.0 |= setting;
             missiles &= !setting;
         }
-
-        BitwiseGameState::rotate_missile_towers(player_buildings);
-    }
-
-    //TODO: Add a pointer and stop rotating here
-    fn rotate_missile_towers(player_buildings: &mut PlayerBuildings) {
-        let zero = player_buildings.missile_towers[0];
-        for i in 1..player_buildings.missile_towers.len() {
-            player_buildings.missile_towers[i-1] = player_buildings.missile_towers[i];
-        }
-        let end = player_buildings.missile_towers.len()-1;
-        player_buildings.missile_towers[end] = zero;
+        player_buildings.firing_tower = (player_buildings.firing_tower + 1) % MISSILE_COOLDOWN_STATES;
     }
 
-
     fn move_and_collide_missiles(opponent: &mut Player, opponent_buildings: &mut PlayerBuildings, player_missiles: &mut [(u64, u64); MISSILE_MAX_SINGLE_CELL]) {
         for _ in 0..MISSILE_SPEED {
             for i in 0..MISSILE_MAX_SINGLE_CELL {
@@ -417,7 +427,8 @@ impl PlayerBuildings {
             buildings: [0; DEFENCE_HEALTH],
             occupied: 0,
             energy_towers: 0,
-            missile_towers: [0; MISSILE_COOLDOWN+1],
+            missile_towers: [0; MISSILE_COOLDOWN_STATES],
+            firing_tower: 0,
             missiles: [(0,0); MISSILE_MAX_SINGLE_CELL],
             tesla_cooldowns: [TeslaCooldown::empty(); TESLA_MAX]
         }
index e77392e..e321a81 100644 (file)
@@ -3,6 +3,7 @@ pub const SINGLE_MAP_WIDTH: u8 = FULL_MAP_WIDTH/2;
 pub const MAP_HEIGHT: u8 = 8;
 
 pub const MISSILE_COOLDOWN: usize = 3;
+pub const MISSILE_COOLDOWN_STATES: usize = MISSILE_COOLDOWN+1;
 pub const MISSILE_SPEED: usize = 2;
 pub const MISSILE_MAX_SINGLE_CELL: usize = SINGLE_MAP_WIDTH as usize / MISSILE_SPEED;
 pub const MISSILE_DAMAGE: u8 = 5;
index e0c9a30..72b5731 100644 (file)
@@ -146,20 +146,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..MISSILE_COOLDOWN as u8 + 1)
+    let mut player_attack_iter = (0..MISSILE_COOLDOWN_STATES as u8)
         .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..MISSILE_COOLDOWN as u8 + 1)
+    let mut opponent_attack_iter = (0..MISSILE_COOLDOWN_STATES as u8)
         .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);MISSILE_COOLDOWN+1] = [(0,0),(0,0),(0,0),(0,0)];
+    let empty_missiles: [(u64,u64);MISSILE_COOLDOWN_STATES] = [(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();
@@ -219,6 +219,7 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS
             occupied: player_occupied,
             energy_towers: player_energy,
             missile_towers: [player_attack_iter.next().unwrap(), player_attack_iter.next().unwrap(), player_attack_iter.next().unwrap(), player_attack_iter.next().unwrap()],
+            firing_tower: 0,
             missiles: player_missiles,
             tesla_cooldowns: [player_tesla_iter.next().unwrap_or(null_tesla.clone()),
                               player_tesla_iter.next().unwrap_or(null_tesla.clone())]
@@ -229,6 +230,7 @@ fn build_bitwise_from_expressive(expressive: &expressive_engine::ExpressiveGameS
             occupied: opponent_occupied,
             energy_towers: opponent_energy,
             missile_towers: [opponent_attack_iter.next().unwrap(), opponent_attack_iter.next().unwrap(), opponent_attack_iter.next().unwrap(), opponent_attack_iter.next().unwrap()],
+            firing_tower: 0,
             missiles: opponent_missiles,
             tesla_cooldowns: [opponent_tesla_iter.next().unwrap_or(null_tesla.clone()),
                               opponent_tesla_iter.next().unwrap_or(null_tesla.clone())]