Move Form and Species to traits, implement a bunch of mocks
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-11-28 21:34:28 +01:00
parent c1e09c654b
commit bce636b97e
25 changed files with 447 additions and 155 deletions

View File

@@ -1,4 +1,5 @@
use hashbrown::HashSet;
use std::fmt::Debug;
use crate::static_data::Statistic;
use crate::static_data::TypeIdentifier;
@@ -7,10 +8,56 @@ use crate::static_data::{AbilityIndex, LearnableMoves};
use crate::StringKey;
use crate::{Random, ValueIdentifiable, ValueIdentifier};
/// A form is a variant of a specific species. A species always has at least one form, but can have
/// many more.
pub trait Form: ValueIdentifiable + Debug {
/// 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.
fn base_stats(&self) -> &StaticStatisticSet<u16>;
/// 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>;
#[allow(clippy::borrowed_box)]
/// The moves a Pokemon with this form can learn.
fn moves(&self) -> &Box<dyn LearnableMoves>;
/// 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.
fn get_type(&self, index: usize) -> TypeIdentifier;
/// 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.
fn get_ability(&self, index: AbilityIndex) -> &StringKey;
/// Gets a random ability from the form.
fn get_random_ability(&self, rand: &mut Random) -> &StringKey;
/// Gets a random hidden ability from the form.
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey;
/// Check if the form has a specific flag set.
fn has_flag(&self, key: &StringKey) -> bool;
}
/// A form is a variant of a specific species. A species always has at least one form, but can have
/// many more.
#[derive(Debug)]
pub struct Form {
pub struct FormImpl {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The name of the form.
@@ -35,7 +82,7 @@ pub struct Form {
flags: HashSet<StringKey>,
}
impl Form {
impl FormImpl {
/// Instantiates a new form.
pub fn new(
name: &StringKey,
@@ -48,8 +95,8 @@ impl Form {
hidden_abilities: Vec<StringKey>,
moves: Box<dyn LearnableMoves>,
flags: HashSet<StringKey>,
) -> Form {
Form {
) -> Self {
Self {
identifier: Default::default(),
name: name.clone(),
height,
@@ -63,62 +110,64 @@ impl Form {
flags,
}
}
}
impl Form for FormImpl {
/// The name of the form.
pub fn name(&self) -> &StringKey {
fn name(&self) -> &StringKey {
&self.name
}
/// The height of the form in meters.
pub fn height(&self) -> f32 {
fn height(&self) -> f32 {
self.height
}
/// The weight of the form in kilograms.
pub fn weight(&self) -> f32 {
fn weight(&self) -> f32 {
self.weight
}
/// The base amount of experience that is gained when beating a Pokemon with this form.
pub fn base_experience(&self) -> u32 {
fn base_experience(&self) -> u32 {
self.base_experience
}
/// The normal types a Pokemon with this form has.
pub fn types(&self) -> &Vec<TypeIdentifier> {
fn types(&self) -> &Vec<TypeIdentifier> {
&self.types
}
/// The inherent values of a form of species that are used for the stats of a Pokemon.
pub fn base_stats(&self) -> &StaticStatisticSet<u16> {
fn base_stats(&self) -> &StaticStatisticSet<u16> {
&self.base_stats
}
/// The possible abilities a Pokemon with this form can have.
pub fn abilities(&self) -> &Vec<StringKey> {
fn abilities(&self) -> &Vec<StringKey> {
&self.abilities
}
/// The possible hidden abilities a Pokemon with this form can have.
pub fn hidden_abilities(&self) -> &Vec<StringKey> {
fn hidden_abilities(&self) -> &Vec<StringKey> {
&self.hidden_abilities
}
#[allow(clippy::borrowed_box)]
/// The moves a Pokemon with this form can learn.
pub fn moves(&self) -> &Box<dyn LearnableMoves> {
fn moves(&self) -> &Box<dyn LearnableMoves> {
&self.moves
}
/// Arbitrary flags can be set on a form for scripting use.
pub fn flags(&self) -> &HashSet<StringKey> {
fn flags(&self) -> &HashSet<StringKey> {
&self.flags
}
/// Get a type of the move at a certain index.
pub fn get_type(&self, index: usize) -> TypeIdentifier {
fn get_type(&self, index: usize) -> TypeIdentifier {
self.types[index]
}
/// Gets a single base stat value.
pub fn get_base_stat(&self, stat: Statistic) -> u16 {
fn get_base_stat(&self, stat: Statistic) -> u16 {
self.base_stats.get_stat(stat)
}
/// Find the index of an ability that can be on this form.
pub fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex> {
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex> {
for (index, a) in self.abilities.iter().enumerate() {
if a == ability.name() {
return Some(AbilityIndex {
@@ -139,7 +188,7 @@ impl Form {
}
/// Gets an ability from the form.
pub fn get_ability(&self, index: AbilityIndex) -> &StringKey {
fn get_ability(&self, index: AbilityIndex) -> &StringKey {
if index.hidden {
&self.hidden_abilities[index.index as usize]
} else {
@@ -148,22 +197,57 @@ impl Form {
}
/// Gets a random ability from the form.
pub fn get_random_ability(&self, rand: &mut Random) -> &StringKey {
fn get_random_ability(&self, rand: &mut Random) -> &StringKey {
&self.abilities[rand.get_between_unsigned(0, self.abilities.len() as u32) as usize]
}
/// Gets a random hidden ability from the form.
pub fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey {
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey {
&self.hidden_abilities[rand.get_between_unsigned(0, self.hidden_abilities.len() as u32) as usize]
}
/// Check if the form has a specific flag set.
pub fn has_flag(&self, key: &StringKey) -> bool {
fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key)
}
}
impl ValueIdentifiable for Form {
impl ValueIdentifiable for FormImpl {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}
#[cfg(test)]
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>;
fn base_stats(&self) -> &StaticStatisticSet<u16>;
fn abilities(&self) -> &Vec<StringKey>;
fn hidden_abilities(&self) -> &Vec<StringKey>;
#[allow(clippy::borrowed_box)]
fn moves(&self) -> &Box<dyn LearnableMoves>;
fn flags(&self) -> &HashSet<StringKey>;
fn get_type(&self, index: usize) -> TypeIdentifier;
fn get_base_stat(&self, stat: Statistic) -> u16;
fn find_ability_index(&self, ability: &dyn Ability) -> Option<AbilityIndex>;
fn get_ability(&self, index: AbilityIndex) -> &StringKey;
fn get_random_ability(&self, rand: &mut Random) -> &StringKey;
fn get_random_hidden_ability(&self, rand: &mut Random) -> &StringKey;
fn has_flag(&self, key: &StringKey) -> bool;
}
impl ValueIdentifiable for Form {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}