diff options
author | Justin Worthe <justin@worthe-it.co.za> | 2018-08-09 20:50:25 +0200 |
---|---|---|
committer | Justin Worthe <justin@worthe-it.co.za> | 2018-08-09 20:50:25 +0200 |
commit | 2c7870073e70ac19a6959c5627b04ad3c62d8173 (patch) | |
tree | fc6905f5acaa349b602aaa615ea49d4258f8feb7 /src | |
parent | ea78e266cff3f57c39442aefc21295a758419e69 (diff) |
Removed unnecessary Player field
Diffstat (limited to 'src')
-rw-r--r-- | src/engine/bitwise_engine.rs | 10 | ||||
-rw-r--r-- | src/input/json.rs | 15 | ||||
-rw-r--r-- | src/strategy/monte_carlo.rs | 14 |
3 files changed, 19 insertions, 20 deletions
diff --git a/src/engine/bitwise_engine.rs b/src/engine/bitwise_engine.rs index 6b9ccab..e1296c8 100644 --- a/src/engine/bitwise_engine.rs +++ b/src/engine/bitwise_engine.rs @@ -9,8 +9,7 @@ const RIGHT_COL_MASK: u64 = 0x8080808080808080; #[derive(Debug, Clone, PartialEq, Eq)] pub struct Player { pub energy: u16, - pub health: u8, - pub energy_generated: u16, + pub health: u8 } #[derive(Debug, Clone, PartialEq, Eq)] @@ -421,8 +420,7 @@ impl BitwiseGameState { fn add_energy(player: &mut Player, player_buildings: &mut PlayerBuildings) { - player.energy_generated = ENERGY_GENERATED_BASE + player_buildings.energy_towers.count_ones() as u16 * ENERGY_GENERATED_TOWER; - player.energy += player.energy_generated; + player.energy += player_buildings.energy_generated(); } fn update_status(&mut self) { @@ -456,6 +454,10 @@ impl PlayerBuildings { tesla_cooldowns: [TeslaCooldown::empty(); TESLA_MAX] } } + + pub fn energy_generated(&self) -> u16 { + ENERGY_GENERATED_BASE + self.energy_towers.count_ones() as u16 * ENERGY_GENERATED_TOWER + } } impl TeslaCooldown { diff --git a/src/input/json.rs b/src/input/json.rs index 9bc0518..d51200e 100644 --- a/src/input/json.rs +++ b/src/input/json.rs @@ -48,7 +48,6 @@ struct BuildingState { health: u8, construction_time_left: i16, weapon_cooldown_time_left: u8, - energy_generated_per_turn: u16, building_type: String, x: u8, y: u8, @@ -64,8 +63,8 @@ struct MissileState { impl State { fn to_bitwise_engine(&self) -> bitwise_engine::BitwiseGameState { - let mut player = self.player().to_bitwise_engine(); - let mut opponent = self.opponent().to_bitwise_engine(); + let player = self.player().to_bitwise_engine(); + let 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 { @@ -74,10 +73,10 @@ impl State { 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()) + let (mut bitwise_buildings, bitfield) = if building.player_type == 'A' { + (&mut player_buildings, point.to_left_bitfield()) } else { - (&mut opponent, &mut opponent_buildings, point.to_right_bitfield()) + (&mut opponent_buildings, point.to_right_bitfield()) }; bitwise_buildings.occupied |= bitfield; @@ -91,7 +90,6 @@ impl State { } if building_type == command::BuildingType::Energy { bitwise_buildings.energy_towers |= bitfield; - engine_player.energy_generated += building.energy_generated_per_turn; } else if building_type == command::BuildingType::Attack { for cooldown_tier in 0..MISSILE_COOLDOWN + 1 { @@ -155,8 +153,7 @@ impl Player { fn to_bitwise_engine(&self) -> engine::bitwise_engine::Player { engine::bitwise_engine::Player { energy: self.energy, - health: self.health, - energy_generated: ENERGY_GENERATED_BASE + health: self.health } } } diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs index cf0d77c..25b5a66 100644 --- a/src/strategy/monte_carlo.rs +++ b/src/strategy/monte_carlo.rs @@ -1,7 +1,7 @@ use engine::command::*; use engine::geometry::*; use engine::status::GameStatus; -use engine::bitwise_engine::{Player, BitwiseGameState}; +use engine::bitwise_engine::{Player, PlayerBuildings, BitwiseGameState}; use engine::constants::*; use rand::{Rng, XorShiftRng, SeedableRng}; @@ -115,12 +115,12 @@ fn simulate_to_endstate<R: Rng>(command_score: &mut CommandScore, state: &Bitwis } fn random_player_move<R: Rng>(state: &BitwiseGameState, rng: &mut R) -> Command { - let all_buildings = sensible_buildings(&state.player(), state.player_has_max_teslas()); + let all_buildings = sensible_buildings(&state.player, &state.player_buildings, state.player_has_max_teslas()); random_move(&all_buildings, rng, state.unoccupied_player_cell_count(), |i| state.location_of_unoccupied_player_cell(i)) } fn random_opponent_move<R: Rng>(state: &BitwiseGameState, rng: &mut R) -> Command { - let all_buildings = sensible_buildings(&state.opponent(), state.opponent_has_max_teslas()); + let all_buildings = sensible_buildings(&state.opponent, &state.opponent_buildings, state.opponent_has_max_teslas()); random_move(&all_buildings, rng, state.unoccupied_opponent_cell_count(), |i| state.location_of_unoccupied_opponent_cell(i)) } @@ -199,7 +199,7 @@ impl CommandScore { //TODO: Devalue nothing so that it doesn't stand and do nothing when it can do things fn init_command_scores(state: &BitwiseGameState) -> Vec<CommandScore> { - let all_buildings = sensible_buildings(&state.player(), state.player_has_max_teslas()); + let all_buildings = sensible_buildings(&state.player, &state.player_buildings, state.player_has_max_teslas()); let unoccupied_cells = (0..state.unoccupied_player_cell_count()).map(|i| state.location_of_unoccupied_player_cell(i)); @@ -220,7 +220,7 @@ impl CommandScore { } #[cfg(not(feature = "energy-cutoff"))] -fn sensible_buildings(player: &Player, has_max_teslas: bool) -> Vec<BuildingType> { +fn sensible_buildings(player: &Player, _player_buildings: &PlayerBuildings, has_max_teslas: bool) -> Vec<BuildingType> { let mut result = Vec::with_capacity(4); if DEFENCE_PRICE <= player.energy { @@ -243,9 +243,9 @@ fn sensible_buildings(player: &Player, has_max_teslas: bool) -> Vec<BuildingType //TODO: Heuristic that avoids building the initial energy towers all in the same row? //TODO: Update cutoff to maybe build iron curtains #[cfg(feature = "energy-cutoff")] -fn sensible_buildings(player: &Player, has_max_teslas: bool) -> Vec<BuildingType> { +fn sensible_buildings(player: &Player, player_buildings: &PlayerBuildings, has_max_teslas: bool) -> Vec<BuildingType> { let mut result = Vec::with_capacity(4); - let needs_energy = player.energy_generated <= ENERGY_PRODUCTION_CUTOFF || + let needs_energy = player_buildings.energy_generated() <= ENERGY_PRODUCTION_CUTOFF || player.energy <= ENERGY_STORAGE_CUTOFF; if DEFENCE_PRICE <= player.energy { |