Skip to content

Commit

Permalink
feat(scorers): Added EvaluatingScorer (#24)
Browse files Browse the repository at this point in the history
* Added EvaluatingScorer

* fixed doc issue

Co-authored-by: doomy <[email protected]>
  • Loading branch information
piedoom and doomy authored Apr 29, 2021
1 parent 92034cd commit 1a1d5b3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,5 +201,6 @@ impl Plugin for BigBrainPlugin {
app.add_system(scorers::all_or_nothing_system.system());
app.add_system(scorers::sum_of_scorers_system.system());
app.add_system(scorers::winning_scorer_system.system());
app.add_system(scorers::evaluating_scorer_system.system());
}
}
76 changes: 75 additions & 1 deletion src/scorers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use std::{cmp::Ordering, sync::Arc};

use bevy::prelude::*;

use crate::thinker::{Actor, ScorerEnt};
use crate::{
evaluators::Evaluator,
thinker::{Actor, ScorerEnt},
};

/**
Score value between `0.0..=1.0` associated with a Scorer.
Expand Down Expand Up @@ -357,3 +360,74 @@ impl ScorerBuilder for WinningScorerBuilder {
});
}
}

/**
Composite scorer that takes a `ScorerBuilder` and applies an `Evaluator`. Note that
unlike other composite scorers, `EvaluatingScorer` only takes one scorer upon building.
### Example
```ignore
Thinker::build()
.when(
EvaluatingScorer::build(MyScorer, MyEvaluator),
MyAction::build());
```
*/
#[derive(Debug, Clone)]
pub struct EvaluatingScorer {
scorer: ScorerEnt,
evaluator: Arc<dyn Evaluator>,
}

impl EvaluatingScorer {
pub fn build(
scorer: impl ScorerBuilder + 'static,
evaluator: impl Evaluator + 'static,
) -> EvaluatingScorerBuilder {
EvaluatingScorerBuilder {
evaluator: Arc::new(evaluator),
scorer: Arc::new(scorer),
}
}
}

pub fn evaluating_scorer_system(
query: Query<(Entity, &EvaluatingScorer)>,
mut scores: QuerySet<(Query<&Score>, Query<&mut Score>)>,
) {
for (sos_ent, eval_scorer) in query.iter() {
// Get the inner score
let inner_score = scores
.q0()
.get(eval_scorer.scorer.0)
.expect("where did it go?")
.get();
// Get composite score
let mut score = scores.q1_mut().get_mut(sos_ent).expect("where did it go?");
score.set(crate::evaluators::clamp(
eval_scorer.evaluator.evaluate(inner_score),
0.0,
1.0,
));
}
}

#[derive(Debug, Clone)]
pub struct EvaluatingScorerBuilder {
pub scorer: Arc<dyn ScorerBuilder>,
pub evaluator: Arc<dyn Evaluator>,
}

impl ScorerBuilder for EvaluatingScorerBuilder {
fn build(&self, cmd: &mut Commands, scorer: Entity, actor: Entity) {
let inner_scorer = self.scorer.attach(cmd, actor);
cmd.entity(scorer)
.insert(Transform::default())
.insert(GlobalTransform::default())
.insert(EvaluatingScorer {
evaluator: self.evaluator.clone(),
scorer: ScorerEnt(inner_scorer),
});
}
}

0 comments on commit 1a1d5b3

Please sign in to comment.