summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2018-08-09 22:17:02 +0200
committerJustin Worthe <justin@worthe-it.co.za>2018-08-09 22:17:02 +0200
commit856a6a17aa927e95e33cffcbd178efe86ce14262 (patch)
tree09a0438257e09e768e571fdb7ae12dbc95965625 /src
parent52c4dc1dd7bbe6546e0a2b3eeb4b5ec78d3fb582 (diff)
Rearranged random move selection to avoid expensive division
Diffstat (limited to 'src')
-rw-r--r--src/engine/constants.rs2
-rw-r--r--src/strategy/monte_carlo.rs15
2 files changed, 6 insertions, 11 deletions
diff --git a/src/engine/constants.rs b/src/engine/constants.rs
index 2e80988..458251f 100644
--- a/src/engine/constants.rs
+++ b/src/engine/constants.rs
@@ -28,7 +28,7 @@ pub const ENERGY_CONSTRUCTION_TIME: u8 = 1;
pub const DECONSTRUCT_ENERGY: u16 = 5;
-pub const MAX_CONCURRENT_CONSTRUCTION: usize = 5; //2 teslas, and 3 of anything else
+pub const MAX_CONCURRENT_CONSTRUCTION: usize = 6; //2 teslas, and 3 of anything else, 1 extra because it's push here then update construction times
#[cfg(not(feature = "reduced-time"))]
diff --git a/src/strategy/monte_carlo.rs b/src/strategy/monte_carlo.rs
index 25b5a66..0844402 100644
--- a/src/strategy/monte_carlo.rs
+++ b/src/strategy/monte_carlo.rs
@@ -126,21 +126,16 @@ fn random_opponent_move<R: Rng>(state: &BitwiseGameState, rng: &mut R) -> Comman
// TODO: Given enough energy, most opponents won't do nothing
fn random_move<R: Rng, F:Fn(usize)->Point>(all_buildings: &[BuildingType], rng: &mut R, free_positions_count: usize, get_point: F) -> Command {
- let building_command_count = free_positions_count*all_buildings.len();
let nothing_count = 1;
-
- let number_of_commands = building_command_count + nothing_count;
+ let building_choice_index = rng.gen_range(0, nothing_count + all_buildings.len());
- let choice_index = rng.gen_range(0, number_of_commands);
-
- // TODO: Remove the divide here?
-
- if choice_index == number_of_commands - 1 {
+ if building_choice_index == all_buildings.len() {
Command::Nothing
} else {
+ let position_choice = rng.gen_range(0, free_positions_count);
Command::Build(
- get_point(choice_index/all_buildings.len()),
- all_buildings[choice_index%all_buildings.len()]
+ get_point(position_choice),
+ all_buildings[building_choice_index]
)
}
}