summaryrefslogtreecommitdiff
path: root/src/engine
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-08-19 14:57:06 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-08-19 14:57:06 +0200
commit32e3f839c4d8ec0cdee18bff8143eb649fd70ff9 (patch)
tree2f6198e002f52380a94433e775d0a0eb2fd487d1 /src/engine
parent0c1636355918a82df0ec188252c8f90487206c7b (diff)
Tweaked performance for enegy tower limiting
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/bitwise_engine.rs53
-rw-r--r--src/engine/constants.rs2
2 files changed, 45 insertions, 10 deletions
diff --git a/src/engine/bitwise_engine.rs b/src/engine/bitwise_engine.rs
index 27c1bd8..f8af86d 100644
--- a/src/engine/bitwise_engine.rs
+++ b/src/engine/bitwise_engine.rs
@@ -34,6 +34,8 @@ pub struct Player {
pub iron_curtain_available: bool,
pub iron_curtain_remaining: u8,
+
+ pub energy_towers_with_heuristics: u64,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -73,6 +75,9 @@ impl BitwiseGameState {
BitwiseGameState::update_iron_curtain(&mut self.player, self.round);
BitwiseGameState::update_iron_curtain(&mut self.opponent, self.round);
+ self.player.update_energy_towers_with_heuristics();
+ self.opponent.update_energy_towers_with_heuristics();
+
self.round += 1;
self.update_status();
@@ -430,7 +435,8 @@ impl Player {
missiles: [(0,0); MISSILE_MAX_SINGLE_CELL],
tesla_cooldowns: ArrayVec::new(),
iron_curtain_available: false,
- iron_curtain_remaining: 0
+ iron_curtain_remaining: 0,
+ energy_towers_with_heuristics: 0
}
}
@@ -454,28 +460,57 @@ impl Player {
point
}
- pub fn energy_occupied_energy_with_heuristics(&self) -> u64 {
- let mut result = 0;
+ pub fn update_energy_towers_with_heuristics(&mut self) {
+ self.energy_towers_with_heuristics = self.occupied;
for y in 0..MAP_HEIGHT {
- let mask = 255 << y*SINGLE_MAP_WIDTH;
+ let mask = 255u64 << (y*SINGLE_MAP_WIDTH);
let isolated_row = self.energy_towers & mask;
let row_count = isolated_row.count_ones();
- result |= if row_count > ENERGY_MAX_IN_ROW {
+ self.energy_towers_with_heuristics |= if row_count >= ENERGY_MAX_IN_ROW {
mask
} else {
- self.occupied & mask
+ 0
};
}
- result
}
pub fn unoccupied_energy_cell_count(&self) -> usize {
- self.energy_occupied_energy_with_heuristics().count_zeros() as usize
+ self.energy_towers_with_heuristics.count_zeros() as usize
}
pub fn location_of_unoccupied_energy_cell(&self, i: usize) -> Point {
- let bit = find_bit_index_from_rank(self.energy_occupied_energy_with_heuristics(), i as u64);
+ let bit = find_bit_index_from_rank(self.energy_towers_with_heuristics, i as u64);
let point = Point { index: bit };
debug_assert!(point.to_either_bitfield() & self.occupied == 0);
point
}
}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn energy_occupied_marks_empty_rows_appropriately() {
+ let mut player = Player::empty();
+ player.energy_towers = 0;
+ player.occupied = 0;
+ player.update_energy_towers_with_heuristics();
+
+ let expected = 0;
+ let actual = player.energy_towers_with_heuristics;
+
+ assert_eq!(expected, actual);
+ }
+ #[test]
+ fn energy_occupied_marks_full_first_row_appropriately() {
+ let mut player = Player::empty();
+ player.energy_towers = 0x00_07; //3 energy towers in first row, don't build more
+ player.occupied = 0x07_07; //3 more towers in second row, can still build by them
+ player.update_energy_towers_with_heuristics();
+
+ let expected = 0x07_ff;
+ let actual = player.energy_towers_with_heuristics;
+
+ assert_eq!(expected, actual);
+ }
+}
diff --git a/src/engine/constants.rs b/src/engine/constants.rs
index 60e8101..71d937d 100644
--- a/src/engine/constants.rs
+++ b/src/engine/constants.rs
@@ -26,7 +26,7 @@ pub const ENERGY_GENERATED_TOWER: u16 = 3;
pub const ENERGY_PRICE: u16 = 20;
pub const ENERGY_CONSTRUCTION_TIME: u8 = 1;
-pub const ENERGY_MAX_IN_ROW: u32 = 2;
+pub const ENERGY_MAX_IN_ROW: u32 = 3;
pub const IRON_CURTAIN_PRICE: u16 = 100;
pub const IRON_CURTAIN_UNLOCK_INTERVAL: u16 = 30;