2022-06-11 18:51:37 +00:00
|
|
|
use crate::static_data::Form;
|
|
|
|
use crate::static_data::Gender;
|
|
|
|
use crate::Random;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::StringKey;
|
2022-06-06 11:54:59 +00:00
|
|
|
use hashbrown::{HashMap, HashSet};
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-06-06 12:43:41 +00:00
|
|
|
#[derive(Debug)]
|
2021-01-30 21:29:59 +00:00
|
|
|
pub struct Species<'a> {
|
|
|
|
id: u16,
|
2022-06-11 15:22:46 +00:00
|
|
|
name: StringKey,
|
2021-01-30 21:29:59 +00:00
|
|
|
gender_rate: f32,
|
2022-06-11 15:22:46 +00:00
|
|
|
growth_rate: StringKey,
|
2021-01-30 21:29:59 +00:00
|
|
|
capture_rate: u8,
|
2022-06-11 15:22:46 +00:00
|
|
|
forms: HashMap<StringKey, Form<'a>>,
|
|
|
|
flags: HashSet<StringKey>,
|
|
|
|
}
|
|
|
|
lazy_static::lazy_static! {
|
|
|
|
static ref DEFAULT_KEY: StringKey = StringKey::new("default");
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Species<'a> {
|
|
|
|
pub fn new(
|
|
|
|
id: u16,
|
2022-06-11 15:22:46 +00:00
|
|
|
name: &StringKey,
|
2021-01-30 21:29:59 +00:00
|
|
|
gender_rate: f32,
|
2022-06-11 15:22:46 +00:00
|
|
|
growth_rate: &StringKey,
|
2021-01-30 21:29:59 +00:00
|
|
|
capture_rate: u8,
|
|
|
|
default_form: Form<'a>,
|
2022-06-11 15:22:46 +00:00
|
|
|
flags: HashSet<StringKey>,
|
2021-01-30 21:29:59 +00:00
|
|
|
) -> Species<'a> {
|
2022-06-06 11:54:59 +00:00
|
|
|
let mut forms = HashMap::with_capacity(1);
|
2022-06-11 15:22:46 +00:00
|
|
|
forms.insert_unique_unchecked(DEFAULT_KEY.clone(), default_form);
|
2021-01-30 21:29:59 +00:00
|
|
|
Species {
|
|
|
|
id,
|
2022-06-11 15:22:46 +00:00
|
|
|
name: name.clone(),
|
2021-01-30 21:29:59 +00:00
|
|
|
gender_rate,
|
2022-06-11 15:22:46 +00:00
|
|
|
growth_rate: growth_rate.clone(),
|
2021-01-30 21:29:59 +00:00
|
|
|
capture_rate,
|
2022-06-06 11:54:59 +00:00
|
|
|
forms,
|
2021-01-30 21:29:59 +00:00
|
|
|
flags,
|
|
|
|
}
|
|
|
|
}
|
2022-06-06 12:43:41 +00:00
|
|
|
pub fn id(&self) -> u16 {
|
|
|
|
self.id
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn name(&self) -> &StringKey {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
pub fn gender_rate(&self) -> f32 {
|
|
|
|
self.gender_rate
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn growth_rate(&self) -> &StringKey {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.growth_rate
|
|
|
|
}
|
|
|
|
pub fn capture_rate(&self) -> u8 {
|
|
|
|
self.capture_rate
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn forms(&self) -> &HashMap<StringKey, Form<'a>> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.forms
|
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn flags(&self) -> &HashSet<StringKey> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.flags
|
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn add_form(&mut self, id: StringKey, form: Form<'a>) {
|
2021-01-30 21:29:59 +00:00
|
|
|
self.forms.insert(id, form);
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn get_form(&self, id: &StringKey) -> Option<&Form> {
|
2021-01-30 21:29:59 +00:00
|
|
|
self.forms.get(id)
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn get_default_form(&self) -> &Form {
|
|
|
|
self.forms.get(&DEFAULT_KEY).unwrap()
|
|
|
|
}
|
|
|
|
|
2021-01-30 21:29:59 +00:00
|
|
|
pub fn get_random_gender(&self, rand: &mut Random) -> Gender {
|
|
|
|
if self.gender_rate < 0.0 {
|
|
|
|
Gender::Genderless
|
|
|
|
} else if rand.get_float() >= self.gender_rate {
|
|
|
|
Gender::Female
|
|
|
|
} else {
|
|
|
|
Gender::Male
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 15:22:46 +00:00
|
|
|
pub fn has_flag(&self, key: &StringKey) -> bool {
|
2021-01-30 21:29:59 +00:00
|
|
|
self.flags.contains(key)
|
|
|
|
}
|
|
|
|
}
|