// Workspace: stats // File: stats/src/stats_structs.rs //! The module `stats_repartition` handles the repartition of stats on creatures. //! It includes structs and methods for managing the fitness of a solution and the //! weights of different affinities 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(super) 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(super) fn lower(&self, min: Option) -> Option { 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(super) fn upper(&self, min: Option) -> Option { 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(super) struct StatWeightList { /// The list of all affinities weights: Vec, } impl StatWeightList { /// Iterate over all affinites to fill, endlessly pub(super) fn iter_cycle(self) -> Cycle> { self.weights.into_iter().cycle() } } impl From 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 } } }