PkmnLib_rs/src/static_data/species_data/species.rs

195 lines
6.5 KiB
Rust
Raw Normal View History

use std::fmt::Debug;
2022-09-17 07:38:02 +00:00
use std::sync::{Arc, LazyLock};
use hashbrown::{HashMap, HashSet};
use parking_lot::lock_api::RwLockReadGuard;
use parking_lot::{RawRwLock, RwLock};
2022-06-11 18:51:37 +00:00
use crate::static_data::Form;
use crate::static_data::Gender;
use crate::StringKey;
use crate::{Random, ValueIdentifiable, ValueIdentifier};
/// The data belonging to a Pokemon with certain characteristics.
pub trait Species: ValueIdentifiable + Debug {
/// The national dex identifier of the Pokemon.
fn id(&self) -> u16;
/// The name of the Pokemon.
fn name(&self) -> &StringKey;
/// The chance between 0.0 and 1.0 that a Pokemon is female.
fn gender_rate(&self) -> f32;
/// How much experience is required for a level.
fn growth_rate(&self) -> &StringKey;
/// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is
/// uncatchable.
fn capture_rate(&self) -> u8;
/// The forms that belong to this Pokemon.
fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<dyn Form>>>;
/// The arbitrary flags that can be set on a Pokemon for script use.
fn flags(&self) -> &HashSet<StringKey>;
/// Adds a new form to the species.
fn add_form(&self, id: StringKey, form: Arc<dyn Form>);
/// Gets a form by name.
fn get_form(&self, id: &StringKey) -> Option<Arc<dyn Form>>;
/// Gets the form the Pokemon will have by default, if no other form is specified.
fn get_default_form(&self) -> Arc<dyn Form>;
/// Gets a random gender.
fn get_random_gender(&self, rand: &mut Random) -> Gender;
/// Check whether the Pokemon has a specific flag set.
fn has_flag(&self, key: &StringKey) -> bool;
}
/// The data belonging to a Pokemon with certain characteristics.
#[derive(Debug)]
pub struct SpeciesImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The national dex identifier of the Pokemon.
id: u16,
/// The name of the Pokemon.
name: StringKey,
/// The chance between 0.0 and 1.0 that a Pokemon is female.
gender_rate: f32,
/// How much experience is required for a level.
growth_rate: StringKey,
/// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is
/// uncatchable.
capture_rate: u8,
/// The forms that belong to this Pokemon.
forms: RwLock<HashMap<StringKey, Arc<dyn Form>>>,
/// The arbitrary flags that can be set on a Pokemon for script use.
flags: HashSet<StringKey>,
}
/// A cached String Key to get the default form.
2022-09-17 07:38:02 +00:00
static DEFAULT_KEY: LazyLock<StringKey> = LazyLock::new(|| StringKey::new("default"));
2022-07-18 08:16:47 +00:00
/// Gets the StringKey for "default". Initialises it if it does not exist.
2022-07-18 08:16:47 +00:00
fn get_default_key() -> StringKey {
2022-09-17 07:38:02 +00:00
DEFAULT_KEY.clone()
2022-07-18 08:16:47 +00:00
}
impl SpeciesImpl {
/// Creates a new species.
pub fn new(
id: u16,
name: &StringKey,
gender_rate: f32,
growth_rate: &StringKey,
capture_rate: u8,
default_form: Arc<dyn Form>,
flags: HashSet<StringKey>,
) -> Self {
2022-06-06 11:54:59 +00:00
let mut forms = HashMap::with_capacity(1);
forms.insert_unique_unchecked(get_default_key(), default_form);
Self {
identifier: Default::default(),
id,
name: name.clone(),
gender_rate,
growth_rate: growth_rate.clone(),
capture_rate,
forms: RwLock::new(forms),
flags,
}
}
}
impl Species for SpeciesImpl {
/// The national dex identifier of the Pokemon.
fn id(&self) -> u16 {
self.id
}
/// The name of the Pokemon.
fn name(&self) -> &StringKey {
&self.name
}
/// The chance between 0.0 and 1.0 that a Pokemon is female.
fn gender_rate(&self) -> f32 {
self.gender_rate
}
/// How much experience is required for a level.
fn growth_rate(&self) -> &StringKey {
&self.growth_rate
}
/// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is
/// uncatchable.
fn capture_rate(&self) -> u8 {
self.capture_rate
}
/// The forms that belong to this Pokemon.
fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<dyn Form>>> {
self.forms.read()
}
/// The arbitrary flags that can be set on a Pokemon for script use.
fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
/// Adds a new form to the species.
fn add_form(&self, id: StringKey, form: Arc<dyn Form>) {
self.forms.write().insert(id, form);
}
/// Gets a form by name.
fn get_form(&self, id: &StringKey) -> Option<Arc<dyn Form>> {
self.forms.read().get(id).cloned()
}
/// Gets the form the Pokemon will have by default, if no other form is specified.
fn get_default_form(&self) -> Arc<dyn Form> {
self.forms.read().get(&get_default_key()).unwrap().clone()
}
/// Gets a random gender.
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
}
}
/// Check whether the Pokemon has a specific flag set.
fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}
impl ValueIdentifiable for SpeciesImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub Species {}
impl Species for Species {
fn id(&self) -> u16;
fn name(&self) -> &StringKey;
fn gender_rate(&self) -> f32;
fn growth_rate(&self) -> &StringKey;
fn capture_rate(&self) -> u8;
fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<dyn Form>>>;
fn flags(&self) -> &HashSet<StringKey>;
fn add_form(&self, id: StringKey, form: Arc<dyn Form>);
fn get_form(&self, id: &StringKey) -> Option<Arc<dyn Form>>;
fn get_default_form(&self) -> Arc<dyn Form>;
fn get_random_gender(&self, rand: &mut Random) -> Gender;
fn has_flag(&self, key: &StringKey) -> bool;
}
impl ValueIdentifiable for Species {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}