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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use rand::Rng;

use super::NpcCtx;

pub trait Predicate: Sized + Clone {
    fn should(&mut self, ctx: &mut NpcCtx) -> bool;

    fn chance(self, chance: f32) -> Chance<Self> {
        Chance {
            predicate: self,
            chance,
        }
    }

    /// Hint for when this will be true.
    fn time_hint(&self) -> Option<f32> { None }
}

#[derive(Clone, Copy)]
pub struct Yes;

impl Predicate for Yes {
    fn should(&mut self, _ctx: &mut NpcCtx) -> bool { true }

    fn time_hint(&self) -> Option<f32> { Some(0.0) }
}

#[derive(Clone)]
pub struct EveryRange {
    next: Option<f32>,
    sample: std::ops::Range<f32>,
}

pub fn every_range(r: std::ops::Range<f32>) -> EveryRange {
    EveryRange {
        next: None,
        sample: r,
    }
}

impl Predicate for EveryRange {
    fn should(&mut self, ctx: &mut NpcCtx) -> bool {
        if let Some(ref mut next) = self.next {
            *next -= ctx.dt;
            if *next <= 0.0 {
                *next += ctx.rng.gen_range(self.sample.clone());
                true
            } else {
                false
            }
        } else {
            self.next = Some(ctx.rng.gen_range(self.sample.clone()));
            false
        }
    }

    fn time_hint(&self) -> Option<f32> { self.next }
}

#[derive(Clone, Copy)]
pub struct Chance<P> {
    predicate: P,
    chance: f32,
}

impl<P: Predicate> Predicate for Chance<P> {
    fn should(&mut self, ctx: &mut NpcCtx) -> bool {
        self.predicate.should(ctx) && ctx.rng.gen_bool(self.chance as f64)
    }

    fn time_hint(&self) -> Option<f32> { self.predicate.time_hint() }
}

impl<F: Fn(&mut NpcCtx) -> bool + Clone> Predicate for F {
    fn should(&mut self, ctx: &mut NpcCtx) -> bool { self(ctx) }
}

// Seconds
pub fn timeout(time: f64) -> Timeout { Timeout { seconds_left: time } }

#[derive(Clone, Copy)]
pub struct Timeout {
    seconds_left: f64,
}

impl Predicate for Timeout {
    fn should(&mut self, ctx: &mut NpcCtx) -> bool {
        self.seconds_left -= ctx.dt as f64;
        self.seconds_left <= 0.0
    }

    fn time_hint(&self) -> Option<f32> { Some(self.seconds_left.max(0.0) as f32) }
}