summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin@worthe-it.co.za>2017-07-15 19:52:25 +0200
committerJustin Worthe <justin@worthe-it.co.za>2017-07-15 19:52:25 +0200
commit9255b4040b192aca63c1dd374e8ae5171a73ecb5 (patch)
tree3212705ffef6805d37882deb6bba9e7f840e52dc
parent0cc8c49bce23090099f564d56844691323c705aa (diff)
Updated shot ordering to choose cheeper one if scores are equal
-rw-r--r--src/knowledge.rs7
1 files changed, 6 insertions, 1 deletions
diff --git a/src/knowledge.rs b/src/knowledge.rs
index f8d71eb..1923f41 100644
--- a/src/knowledge.rs
+++ b/src/knowledge.rs
@@ -4,6 +4,7 @@ use state::*;
use math::*;
use std::collections::HashMap;
+use std::cmp::Ordering;
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Knowledge {
@@ -215,7 +216,11 @@ impl Knowledge {
let best: (Weapon, (Vec<Point>, usize)) =
best_shots.iter()
- .max_by_key(|&(_, &(_, score))| score)
+ .max_by(|&(weapon_a, &(_, score_a)), &(weapon_b, &(_, score_b))| {
+ let score = score_a.cmp(&score_b);
+ let cost = weapon_a.energy_cost(self.map_size).cmp(&weapon_b.energy_cost(self.map_size));
+ if score == Ordering::Equal { cost } else { score }
+ })
.and_then(|(&weapon, x)| {
if self.shootable_weapons.contains(&weapon) {
Some((weapon, x.clone()))