1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use specs::ReadStorage;
use serde::{Deserialize, Serialize};
use typetag;

use crate::{
    considerations::Consideration,
    thinker::{ActionEnt, Choice},
};

#[typetag::serde]
pub trait Picker: std::fmt::Debug + Sync + Send {
    fn pick<'a>(
        &mut self,
        _choices: &Vec<Choice>,
        _considerations: &ReadStorage<'a, Consideration>,
    ) -> Option<ActionEnt>;
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FirstToScore {
    pub threshold: f32,
}
impl FirstToScore {
    pub fn new(threshold: f32) -> Self {
        FirstToScore { threshold }
    }
}

#[typetag::serde]
impl Picker for FirstToScore {
    fn pick<'a>(
        &mut self,
        choices: &Vec<Choice>,
        considerations: &ReadStorage<'a, Consideration>,
    ) -> Option<ActionEnt> {
        for choice in choices {
            let value = choice.calculate(considerations);
            if value >= self.threshold {
                return Some(choice.action.clone());
            }
        }
        None
    }
}