2023-04-19 16:44:11 +00:00
|
|
|
use anyhow_ext::{ensure, Result};
|
2022-07-01 15:07:22 +00:00
|
|
|
use hashbrown::HashSet;
|
2022-11-28 20:34:28 +00:00
|
|
|
use std::fmt::Debug;
|
2023-06-22 13:43:41 +00:00
|
|
|
use std::sync::Arc;
|
2022-07-01 15:07:22 +00:00
|
|
|
|
2022-06-11 18:51:37 +00:00
|
|
|
use crate::static_data::Statistic;
|
2022-07-01 16:20:16 +00:00
|
|
|
use crate::static_data::TypeIdentifier;
|
2022-06-18 12:17:29 +00:00
|
|
|
use crate::static_data::{Ability, StaticStatisticSet};
|
2022-07-01 16:20:16 +00:00
|
|
|
use crate::static_data::{AbilityIndex, LearnableMoves};
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::Random;
|
2023-04-19 16:44:11 +00:00
|
|
|
use crate::{StringKey, VecExt};
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
/// A form is a variant of a specific species. A species always has at least one form, but can have
|
|
|
|
/// many more.
|
2023-06-24 12:44:23 +00:00
|
|
|
pub trait Form: Debug {
|
2022-11-28 20:34:28 +00:00
|
|
|
/// The name of the form.
|
|
|
|
fn name(&self) -> &StringKey;
|
|
|
|
/// The height of the form in meters.
|
|
|
|
fn height(&self) -> f32;
|
|
|
|
/// The weight of the form in kilograms.
|
|
|
|
fn weight(&self) -> f32;
|
|
|
|
/// The base amount of experience that is gained when beating a Pokemon with this form.
|
|
|
|
fn base_experience(&self) -> u32;
|
|
|
|
/// The normal types a Pokemon with this form has.
|
|
|
|
fn types(&self) -> &Vec<TypeIdentifier>;
|
|
|
|
/// The inherent values of a form of species that are used for the stats of a Pokemon.
|
2023-06-22 13:43:41 +00:00
|
|
|
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>>;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// The possible abilities a Pokemon with this form can have.
|
|
|
|
fn abilities(&self) -> &Vec<StringKey>;
|
|
|
|
/// The possible hidden abilities a Pokemon with this form can have.
|
|
|
|
fn hidden_abilities(&self) -> &Vec<StringKey>;
|
|
|
|
|
|
|
|
/// The moves a Pokemon with this form can learn.
|
2023-06-24 12:44:23 +00:00
|
|
|
fn moves(&self) -> &Arc<dyn LearnableMoves>;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// Arbitrary flags can be set on a form for scripting use.
|
|
|
|
fn flags(&self) -> &HashSet<StringKey>;
|
|
|
|
|
|
|
|
/// Get a type of the move at a certain index.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_type(&self, index: usize) -> Result<TypeIdentifier>;
|
2022-11-28 20:34:28 +00:00
|
|
|
|
|
|
|
/// Gets a single base stat value.
|
|
|
|
fn get_base_stat(&self, stat: Statistic) -> u16;
|
|
|
|
/// Find the index of an ability that can be on this form.
|
|
|
|
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex>;
|
|
|
|
|
|
|
|
/// Gets an ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_ability(&self, index: AbilityIndex) -> Result<&StringKey>;
|
2022-11-28 20:34:28 +00:00
|
|
|
|
|
|
|
/// Gets a random ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_random_ability(&self, rand: &mut Random) -> Result<&StringKey>;
|
2022-11-28 20:34:28 +00:00
|
|
|
/// Gets a random hidden ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_random_hidden_ability(&self, rand: &mut Random) -> Result<&StringKey>;
|
2022-11-28 20:34:28 +00:00
|
|
|
|
|
|
|
/// Check if the form has a specific flag set.
|
|
|
|
fn has_flag(&self, key: &StringKey) -> bool;
|
2023-01-14 12:25:21 +00:00
|
|
|
|
|
|
|
/// Arbitrary flags that can be applied to the move.
|
|
|
|
fn has_flag_by_hash(&self, key_hash: u32) -> bool;
|
2023-06-24 13:05:58 +00:00
|
|
|
|
|
|
|
/// Check if two forms are equal.
|
|
|
|
fn eq(&self, other: &dyn Form) -> bool;
|
2022-11-28 20:34:28 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// A form is a variant of a specific species. A species always has at least one form, but can have
|
|
|
|
/// many more.
|
2022-06-06 12:43:41 +00:00
|
|
|
#[derive(Debug)]
|
2022-11-28 20:34:28 +00:00
|
|
|
pub struct FormImpl {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The name of the form.
|
2022-06-11 15:22:46 +00:00
|
|
|
name: StringKey,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The height of the form in meters.
|
2021-01-30 21:29:59 +00:00
|
|
|
height: f32,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The weight of the form in kilograms.
|
2021-01-30 21:29:59 +00:00
|
|
|
weight: f32,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The base amount of experience that is gained when beating a Pokemon with this form.
|
2021-01-30 21:29:59 +00:00
|
|
|
base_experience: u32,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The normal types a Pokemon with this form has.
|
|
|
|
types: Vec<TypeIdentifier>,
|
|
|
|
/// The inherent values of a form of species that are used for the stats of a Pokemon.
|
2023-06-22 13:43:41 +00:00
|
|
|
base_stats: Arc<StaticStatisticSet<u16>>,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The possible abilities a Pokemon with this form can have.
|
2022-06-17 17:53:33 +00:00
|
|
|
abilities: Vec<StringKey>,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The possible hidden abilities a Pokemon with this form can have.
|
2022-06-17 17:53:33 +00:00
|
|
|
hidden_abilities: Vec<StringKey>,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The moves a Pokemon with this form can learn.
|
2023-06-24 12:44:23 +00:00
|
|
|
moves: Arc<dyn LearnableMoves>,
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Arbitrary flags can be set on a form for scripting use.
|
2022-06-11 15:22:46 +00:00
|
|
|
flags: HashSet<StringKey>,
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
impl FormImpl {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Instantiates a new form.
|
2021-01-30 21:29:59 +00:00
|
|
|
pub fn new(
|
2022-06-11 15:22:46 +00:00
|
|
|
name: &StringKey,
|
2021-01-30 21:29:59 +00:00
|
|
|
height: f32,
|
|
|
|
weight: f32,
|
|
|
|
base_experience: u32,
|
2022-07-01 15:07:22 +00:00
|
|
|
types: Vec<TypeIdentifier>,
|
2023-06-24 12:44:23 +00:00
|
|
|
base_stats: Arc<StaticStatisticSet<u16>>,
|
2022-06-17 17:53:33 +00:00
|
|
|
abilities: Vec<StringKey>,
|
|
|
|
hidden_abilities: Vec<StringKey>,
|
2023-06-24 12:44:23 +00:00
|
|
|
moves: Arc<dyn LearnableMoves>,
|
2022-06-11 15:22:46 +00:00
|
|
|
flags: HashSet<StringKey>,
|
2022-11-28 20:34:28 +00:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2022-06-11 15:22:46 +00:00
|
|
|
name: name.clone(),
|
2021-01-30 21:29:59 +00:00
|
|
|
height,
|
|
|
|
weight,
|
|
|
|
base_experience,
|
|
|
|
types,
|
2023-06-24 12:44:23 +00:00
|
|
|
base_stats,
|
2021-01-30 21:29:59 +00:00
|
|
|
abilities,
|
|
|
|
hidden_abilities,
|
|
|
|
moves,
|
|
|
|
flags,
|
|
|
|
}
|
|
|
|
}
|
2022-11-28 20:34:28 +00:00
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
|
2022-11-28 20:34:28 +00:00
|
|
|
impl Form for FormImpl {
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The name of the form.
|
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:07:22 +00:00
|
|
|
/// The height of the form in meters.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn height(&self) -> f32 {
|
2022-06-06 12:43:41 +00:00
|
|
|
self.height
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The weight of the form in kilograms.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn weight(&self) -> f32 {
|
2022-06-06 12:43:41 +00:00
|
|
|
self.weight
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The base amount of experience that is gained when beating a Pokemon with this form.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn base_experience(&self) -> u32 {
|
2022-06-06 12:43:41 +00:00
|
|
|
self.base_experience
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The normal types a Pokemon with this form has.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn types(&self) -> &Vec<TypeIdentifier> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.types
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The inherent values of a form of species that are used for the stats of a Pokemon.
|
2023-06-22 13:43:41 +00:00
|
|
|
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>> {
|
2022-06-18 12:17:29 +00:00
|
|
|
&self.base_stats
|
2022-06-06 12:43:41 +00:00
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The possible abilities a Pokemon with this form can have.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn abilities(&self) -> &Vec<StringKey> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.abilities
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The possible hidden abilities a Pokemon with this form can have.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn hidden_abilities(&self) -> &Vec<StringKey> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.hidden_abilities
|
|
|
|
}
|
2022-11-28 19:20:46 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// The moves a Pokemon with this form can learn.
|
2023-06-24 12:44:23 +00:00
|
|
|
fn moves(&self) -> &Arc<dyn LearnableMoves> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.moves
|
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Arbitrary flags can be set on a form for scripting use.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn flags(&self) -> &HashSet<StringKey> {
|
2022-06-06 12:43:41 +00:00
|
|
|
&self.flags
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Get a type of the move at a certain index.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_type(&self, index: usize) -> Result<TypeIdentifier> {
|
|
|
|
Ok(*self.types.get_res(index)?)
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets a single base stat value.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn get_base_stat(&self, stat: Statistic) -> u16 {
|
2021-01-30 21:29:59 +00:00
|
|
|
self.base_stats.get_stat(stat)
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Find the index of an ability that can be on this form.
|
2022-11-28 20:34:28 +00:00
|
|
|
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex> {
|
2021-01-30 21:29:59 +00:00
|
|
|
for (index, a) in self.abilities.iter().enumerate() {
|
2022-06-17 17:53:33 +00:00
|
|
|
if a == ability.name() {
|
2021-01-30 21:29:59 +00:00
|
|
|
return Some(AbilityIndex {
|
|
|
|
hidden: false,
|
|
|
|
index: index as u8,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (index, a) in self.hidden_abilities.iter().enumerate() {
|
2022-06-17 17:53:33 +00:00
|
|
|
if a == ability.name() {
|
2021-01-30 21:29:59 +00:00
|
|
|
return Some(AbilityIndex {
|
|
|
|
hidden: true,
|
|
|
|
index: index as u8,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets an ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_ability(&self, index: AbilityIndex) -> Result<&StringKey> {
|
2022-06-11 15:22:46 +00:00
|
|
|
if index.hidden {
|
2023-04-19 16:44:11 +00:00
|
|
|
Ok(self.hidden_abilities.get_res(index.index as usize)?)
|
2022-06-11 15:22:46 +00:00
|
|
|
} else {
|
2023-04-19 16:44:11 +00:00
|
|
|
Ok(self.abilities.get_res(index.index as usize)?)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
2022-06-11 15:22:46 +00:00
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets a random ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_random_ability(&self, rand: &mut Random) -> Result<&StringKey> {
|
|
|
|
ensure!(!self.abilities.is_empty(), "No abilities on form");
|
|
|
|
self.abilities
|
|
|
|
.get_res(rand.get_between_unsigned(0, self.abilities.len() as u32) as usize)
|
2022-06-11 15:22:46 +00:00
|
|
|
}
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Gets a random hidden ability from the form.
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_random_hidden_ability(&self, rand: &mut Random) -> Result<&StringKey> {
|
|
|
|
ensure!(!self.hidden_abilities.is_empty(), "No hidden abilities on form");
|
|
|
|
self.hidden_abilities
|
|
|
|
.get_res(rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize)
|
2021-01-30 21:29:59 +00:00
|
|
|
}
|
|
|
|
|
2022-07-01 15:07:22 +00:00
|
|
|
/// Check if the form 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-06-24 13:05:58 +00:00
|
|
|
|
|
|
|
fn eq(&self, other: &dyn Form) -> bool {
|
|
|
|
std::ptr::eq(self, other as *const dyn Form as *const Self)
|
|
|
|
}
|
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 Form {}
|
|
|
|
impl Form for Form {
|
|
|
|
fn name(&self) -> &StringKey;
|
|
|
|
fn height(&self) -> f32;
|
|
|
|
fn weight(&self) -> f32;
|
|
|
|
fn base_experience(&self) -> u32;
|
|
|
|
fn types(&self) -> &Vec<TypeIdentifier>;
|
2023-06-22 13:43:41 +00:00
|
|
|
fn base_stats(&self) -> &Arc<StaticStatisticSet<u16>>;
|
2022-11-28 20:34:28 +00:00
|
|
|
fn abilities(&self) -> &Vec<StringKey>;
|
|
|
|
fn hidden_abilities(&self) -> &Vec<StringKey>;
|
2023-06-24 12:44:23 +00:00
|
|
|
fn moves(&self) -> &Arc<dyn LearnableMoves>;
|
2022-11-28 20:34:28 +00:00
|
|
|
fn flags(&self) -> &HashSet<StringKey>;
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_type(&self, index: usize) -> Result<TypeIdentifier>;
|
2022-11-28 20:34:28 +00:00
|
|
|
fn get_base_stat(&self, stat: Statistic) -> u16;
|
|
|
|
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex>;
|
2023-04-19 16:44:11 +00:00
|
|
|
fn get_ability<'a>(&'a self, index: AbilityIndex) -> Result<&'a StringKey>;
|
|
|
|
fn get_random_ability<'a>(&'a self, rand: &mut Random) -> Result<&'a StringKey>;
|
|
|
|
fn get_random_hidden_ability<'a>(&'a self, rand: &mut Random) -> Result<&'a StringKey>;
|
2022-11-28 20:34:28 +00:00
|
|
|
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-06-24 13:05:58 +00:00
|
|
|
fn eq(&self, other: &dyn Form) -> bool;
|
2022-11-28 20:34:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|