ravenous-tools/src/stat_structs.rs

84 lines
2.3 KiB
Rust

//! Stats repartition structs
use std::{iter::Cycle, vec::IntoIter};
use crate::stat_io::AffName;
/// How much a solution fits the algorithm, or how much a solution fits a creature
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
pub(crate) enum Fit {
/// Weakest fit, can't be used
VeryWeak,
/// Weak fit, shouldn't be used
Weak,
/// Average fit, can be used
Medium,
/// Strong fit, will always be used
Strong,
/// Best fit, must use
VeryStrong,
}
impl Fit {
/// Lower the fitness by one, with an optionnal maximum
pub(crate) fn lower(&self, min: Option<Fit>) -> Option<Fit> {
let f = match self {
Fit::VeryWeak => None,
Fit::Weak => Some(Fit::VeryWeak),
Fit::Medium => Some(Fit::Weak),
Fit::Strong => Some(Fit::Medium),
Fit::VeryStrong => Some(Fit::Strong),
};
if f <= min {
return None;
}
f
}
/// Raise the fitness level by one, with an optionnal maximum
pub(crate) fn upper(&self, min: Option<Fit>) -> Option<Fit> {
let f = match self {
Fit::VeryWeak => Some(Fit::Weak),
Fit::Weak => Some(Fit::Medium),
Fit::Medium => Some(Fit::Strong),
Fit::Strong => Some(Fit::VeryStrong),
Fit::VeryStrong => None,
};
if f <= min {
return None;
}
f
}
}
/// A stat weight level (Affinity name and weight)
type StatWeightsTuple = Vec<(AffName, usize)>;
/// The statweights, holding looping methods
#[derive(Debug)]
pub(crate) struct StatWeightList {
/// The list of all affinities
weights: Vec<AffName>,
}
impl StatWeightList {
/// Iterate over all affinites to fill, endlessly
pub(crate) fn iter_cycle(self) -> Cycle<IntoIter<AffName>> {
self.weights.into_iter().cycle()
}
}
impl From<StatWeightsTuple> for StatWeightList {
fn from(mut value: StatWeightsTuple) -> Self {
let ln = value.iter().rfold(0, |acc, (_, w)| acc + w);
let mut weights = Vec::with_capacity(ln);
let mut i = 0;
while i < ln {
for (w, nb) in value.iter_mut() {
if nb != &0 {
*nb -= 1;
weights.push(*w);
i += 1;
}
}
}
Self { weights }
}
}