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