2023-04-19 16:44:11 +00:00
|
|
|
use anyhow_ext::{anyhow, Result};
|
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
|
|
|
|
2023-07-29 10:57:52 +00:00
|
|
|
use crate::static_data::species_data::evolution_data::EvolutionData;
|
2022-06-11 18:51:37 +00:00
|
|
|
use crate::static_data::Form;
|
|
|
|
use crate::static_data::Gender;
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::Random;
|
2022-06-11 15:22:46 +00:00
|
|
|
use crate::StringKey;
|
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.
|
2023-06-24 12:44:23 +00:00
|
|
|
pub trait Species: Debug {
|
2022-11-28 20:34:28 +00:00
|
|
|
/// 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;
|
2023-07-29 10:57:52 +00:00
|
|
|
/// The base happiness of the Pokemon.
|
|
|
|
fn base_happiness(&self) -> u8;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// 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>>;
|
2023-01-14 12:25:21 +00:00
|
|
|
/// Gets a form by the hash of its name.
|
|
|
|
fn get_form_by_hash(&self, name_hash: u32) -> Option<Arc<dyn Form>>;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// Gets the form the Pokemon will have by default, if no other form is specified.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_default_form(&self) -> Result<Arc<dyn Form>>;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// 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;
|
2023-01-14 12:25:21 +00:00
|
|
|
/// Check whether the Pokemon has a specific flag set.
|
|
|
|
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
|
2023-07-29 10:57:52 +00:00
|
|
|
/// The data regarding into which Pokemons this species can evolve, and how.
|
|
|
|
fn evolution_data(&self) -> &Vec<EvolutionData>;
|
2022-11-28 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
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-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,
|
2023-07-29 10:57:52 +00:00
|
|
|
/// The base happiness of the Pokemon.
|
|
|
|
base_happiness: 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>,
|
2023-07-29 10:57:52 +00:00
|
|
|
/// The data regarding into which Pokemons this species can evolve, and how.
|
|
|
|
evolution_data: Vec<EvolutionData>,
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
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,
|
2023-07-29 10:57:52 +00:00
|
|
|
base_happiness: 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>,
|
2023-07-29 10:57:52 +00:00
|
|
|
evolution_data: Vec<EvolutionData>,
|
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 {
|
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,
|
2023-07-29 10:57:52 +00:00
|
|
|
base_happiness,
|
2022-10-08 11:15:04 +00:00
|
|
|
forms: RwLock::new(forms),
|
2021-01-30 21:29:59 +00:00
|
|
|
flags,
|
2023-07-29 10:57:52 +00:00
|
|
|
evolution_data,
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
}
|
2023-07-29 10:57:52 +00:00
|
|
|
|
|
|
|
fn base_happiness(&self) -> u8 {
|
|
|
|
self.base_happiness
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-14 12:25:21 +00:00
|
|
|
fn get_form_by_hash(&self, name_hash: u32) -> Option<Arc<dyn Form>> {
|
|
|
|
self.forms.read().get::<u32>(&name_hash).cloned()
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:52:00 +00:00
|
|
|
/// Gets the form the Pokemon will have by default, if no other form is specified.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_default_form(&self) -> Result<Arc<dyn Form>> {
|
|
|
|
Ok(self
|
|
|
|
.forms
|
|
|
|
.read()
|
|
|
|
.get(&get_default_key())
|
|
|
|
.ok_or(anyhow!("No default form for species"))?
|
|
|
|
.clone())
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2023-07-15 12:22:48 +00:00
|
|
|
/// Gets a random gender, returning a value based on the species gender ratio.
|
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)
|
|
|
|
}
|
2023-01-14 12:25:21 +00:00
|
|
|
|
|
|
|
fn has_flag_by_hash(&self, key_hash: u32) -> bool {
|
|
|
|
self.flags.contains::<u32>(&key_hash)
|
|
|
|
}
|
2023-07-29 10:57:52 +00:00
|
|
|
|
|
|
|
fn evolution_data(&self) -> &Vec<EvolutionData> {
|
|
|
|
&self.evolution_data
|
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
2022-10-08 11:15:04 +00:00
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
#[cfg(test)]
|
2023-04-19 16:44:11 +00:00
|
|
|
#[allow(clippy::indexing_slicing)]
|
|
|
|
#[allow(clippy::unwrap_used)]
|
2022-11-28 20:34:28 +00:00
|
|
|
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;
|
2023-07-29 10:57:52 +00:00
|
|
|
fn base_happiness(&self) -> u8;
|
2022-11-28 20:34:28 +00:00
|
|
|
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>>;
|
2023-01-14 12:25:21 +00:00
|
|
|
fn get_form_by_hash(&self, name_hash: u32) -> Option<Arc<dyn Form>>;
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_default_form(&self) -> Result<Arc<dyn Form>>;
|
2022-11-28 20:34:28 +00:00
|
|
|
fn get_random_gender(&self, rand: &mut Random) -> Gender;
|
|
|
|
fn has_flag(&self, key: &StringKey) -> bool;
|
2023-01-14 12:25:21 +00:00
|
|
|
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
|
2023-07-29 10:57:52 +00:00
|
|
|
fn evolution_data(&self) -> &Vec<EvolutionData>;
|
2022-11-28 20:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|