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

This commit is contained in:
Deukhoofd 2022-11-28 21:34:28 +01:00
parent c1e09c654b
commit bce636b97e
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
25 changed files with 447 additions and 155 deletions

View File

@ -76,16 +76,16 @@ pub enum Event<'own> {
/// The pokemon that changed species. /// The pokemon that changed species.
pokemon: &'own Pokemon, pokemon: &'own Pokemon,
/// The new species of the Pokemon. /// The new species of the Pokemon.
species: Arc<Species>, species: Arc<dyn Species>,
/// The form of the species the Pokemon will have. /// The form of the species the Pokemon will have.
form: Arc<Form>, form: Arc<dyn Form>,
}, },
/// This event happens when a Pokemon changes form during battle. This is rather common. /// This event happens when a Pokemon changes form during battle. This is rather common.
FormChange { FormChange {
/// The pokemon that changed forms. /// The pokemon that changed forms.
pokemon: &'own Pokemon, pokemon: &'own Pokemon,
/// The new form of the Pokemon. /// The new form of the Pokemon.
form: &'own Form, form: Arc<dyn Form>,
}, },
/// This event happens when a Pokemon takes damage. /// This event happens when a Pokemon takes damage.
Damage { Damage {

View File

@ -100,7 +100,7 @@ impl ValueIdentifiable for LearnedMove {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::static_data::MockMoveData; use crate::static_data::tests::MockMoveData;
#[test] #[test]
fn create_learned_move_restore_uses() { fn create_learned_move_restore_uses() {

View File

@ -32,16 +32,16 @@ pub struct Pokemon {
/// The library data of the Pokemon. /// The library data of the Pokemon.
library: Arc<DynamicLibrary>, library: Arc<DynamicLibrary>,
/// The species of the Pokemon. /// The species of the Pokemon.
species: RwLock<Arc<Species>>, species: RwLock<Arc<dyn Species>>,
/// The form of the Pokemon. /// The form of the Pokemon.
form: RwLock<Arc<Form>>, form: RwLock<Arc<dyn Form>>,
/// An optional display species of the Pokemon. If this is set, the client should display this /// An optional display species of the Pokemon. If this is set, the client should display this
/// species. An example of usage for this is the Illusion ability. /// species. An example of usage for this is the Illusion ability.
display_species: Option<Arc<Species>>, display_species: Option<Arc<dyn Species>>,
/// An optional display form of the Pokemon. If this is set, the client should display this /// An optional display form of the Pokemon. If this is set, the client should display this
// species. An example of usage for this is the Illusion ability. // species. An example of usage for this is the Illusion ability.
display_form: Option<Arc<Form>>, display_form: Option<Arc<dyn Form>>,
/// The current level of the Pokemon. /// The current level of the Pokemon.
level: Atomic<LevelInt>, level: Atomic<LevelInt>,
@ -121,8 +121,8 @@ impl Pokemon {
/// Instantiates a new Pokemon. /// Instantiates a new Pokemon.
pub fn new( pub fn new(
library: Arc<DynamicLibrary>, library: Arc<DynamicLibrary>,
species: Arc<Species>, species: Arc<dyn Species>,
form: &Arc<Form>, form: &Arc<dyn Form>,
ability: AbilityIndex, ability: AbilityIndex,
level: LevelInt, level: LevelInt,
unique_identifier: u32, unique_identifier: u32,
@ -192,15 +192,15 @@ impl Pokemon {
&self.library &self.library
} }
/// The species of the Pokemon. /// The species of the Pokemon.
pub fn species(&self) -> Arc<Species> { pub fn species(&self) -> Arc<dyn Species> {
self.species.read().clone() self.species.read().clone()
} }
/// The form of the Pokemon. /// The form of the Pokemon.
pub fn form(&self) -> Arc<Form> { pub fn form(&self) -> Arc<dyn Form> {
self.form.read().clone() self.form.read().clone()
} }
/// The species that should be displayed to the user. This handles stuff like the Illusion ability. /// The species that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_species(&self) -> Arc<Species> { pub fn display_species(&self) -> Arc<dyn Species> {
if let Some(v) = &self.display_species { if let Some(v) = &self.display_species {
v.clone() v.clone()
} else { } else {
@ -208,7 +208,7 @@ impl Pokemon {
} }
} }
/// The form that should be displayed to the user. This handles stuff like the Illusion ability. /// The form that should be displayed to the user. This handles stuff like the Illusion ability.
pub fn display_form(&self) -> Arc<Form> { pub fn display_form(&self) -> Arc<dyn Form> {
if let Some(v) = &self.display_form { if let Some(v) = &self.display_form {
v.clone() v.clone()
} else { } else {
@ -465,7 +465,7 @@ impl Pokemon {
} }
/// Change the species of the Pokemon. /// Change the species of the Pokemon.
pub fn change_species(&self, species: Arc<Species>, form: Arc<Form>) { pub fn change_species(&self, species: Arc<dyn Species>, form: Arc<dyn Form>) {
*self.species.write() = species.clone(); *self.species.write() = species.clone();
*self.form.write() = form.clone(); *self.form.write() = form.clone();
@ -498,7 +498,7 @@ impl Pokemon {
} }
/// Change the form of the Pokemon. /// Change the form of the Pokemon.
pub fn change_form(&self, form: &Arc<Form>) { pub fn change_form(&self, form: &Arc<dyn Form>) {
if self.form().value_identifier() == form.value_identifier() { if self.form().value_identifier() == form.value_identifier() {
return; return;
} }
@ -542,7 +542,10 @@ impl Pokemon {
let r = self.battle_data.read(); let r = self.battle_data.read();
if let Some(battle_data) = r.deref() { if let Some(battle_data) = r.deref() {
if let Some(battle) = battle_data.battle() { if let Some(battle) = battle_data.battle() {
battle.event_hook().trigger(Event::FormChange { pokemon: self, form }) battle.event_hook().trigger(Event::FormChange {
pokemon: self,
form: form.clone(),
})
} }
} }
} }

View File

@ -12,8 +12,8 @@ use std::sync::Arc;
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_new( extern "C" fn pokemon_new(
library: ExternPointer<Arc<DynamicLibrary>>, library: ExternPointer<Arc<DynamicLibrary>>,
species: ExternPointer<Arc<Species>>, species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<Form>>, form: ExternPointer<Arc<dyn Form>>,
hidden_ability: bool, hidden_ability: bool,
ability_index: u8, ability_index: u8,
level: LevelInt, level: LevelInt,
@ -54,25 +54,25 @@ extern "C" fn pokemon_library(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiableP
/// The species of the Pokemon. /// The species of the Pokemon.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> { extern "C" fn pokemon_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Species>> {
ptr.as_ref().species().into() ptr.as_ref().species().into()
} }
/// The form of the Pokemon. /// The form of the Pokemon.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> { extern "C" fn pokemon_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Form>> {
ptr.as_ref().form().into() ptr.as_ref().form().into()
} }
/// The species that should be displayed to the user. This handles stuff like the Illusion ability. /// The species that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_display_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Species>> { extern "C" fn pokemon_display_species(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Species>> {
ptr.as_ref().display_species().into() ptr.as_ref().display_species().into()
} }
/// The form that should be displayed to the user. This handles stuff like the Illusion ability. /// The form that should be displayed to the user. This handles stuff like the Illusion ability.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_display_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Form>> { extern "C" fn pokemon_display_form(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Form>> {
ptr.as_ref().display_form().into() ptr.as_ref().display_form().into()
} }
@ -294,8 +294,8 @@ extern "C" fn pokemon_recalculate_boosted_stats(ptr: ExternPointer<Arc<Pokemon>>
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_change_species( extern "C" fn pokemon_change_species(
ptr: ExternPointer<Arc<Pokemon>>, ptr: ExternPointer<Arc<Pokemon>>,
species: ExternPointer<Arc<Species>>, species: ExternPointer<Arc<dyn Species>>,
form: ExternPointer<Arc<Form>>, form: ExternPointer<Arc<dyn Form>>,
) { ) {
ptr.as_ref() ptr.as_ref()
.change_species(species.as_ref().clone(), form.as_ref().clone()) .change_species(species.as_ref().clone(), form.as_ref().clone())
@ -303,7 +303,7 @@ extern "C" fn pokemon_change_species(
/// Change the form of the Pokemon. /// Change the form of the Pokemon.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<Form>>) { extern "C" fn pokemon_change_form(ptr: ExternPointer<Arc<Pokemon>>, form: ExternPointer<Arc<dyn Form>>) {
ptr.as_ref().change_form(form.as_ref()) ptr.as_ref().change_form(form.as_ref())
} }

View File

@ -58,7 +58,7 @@ macro_rules! ffi_vec_value_getters {
/// length function, and a getter. /// length function, and a getter.
macro_rules! ffi_vec_stringkey_getters { macro_rules! ffi_vec_stringkey_getters {
( (
$type:ident, $func:ident $type:ty, $func:ident
) => { ) => {
paste::paste! { paste::paste! {
#[no_mangle] #[no_mangle]

View File

@ -2,7 +2,7 @@ use crate::ffi::{
ffi_arc_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer, IdentifiablePointer, ffi_arc_getter, ffi_vec_stringkey_getters, ffi_vec_value_getters, BorrowedPtr, ExternPointer, IdentifiablePointer,
OwnedPtr, OwnedPtr,
}; };
use crate::static_data::{Form, LearnableMoves, StaticStatisticSet, TypeIdentifier}; use crate::static_data::{Form, FormImpl, LearnableMoves, StaticStatisticSet, TypeIdentifier};
use crate::StringKey; use crate::StringKey;
use hashbrown::HashSet; use hashbrown::HashSet;
use std::ffi::{c_char, CStr, CString}; use std::ffi::{c_char, CStr, CString};
@ -26,7 +26,7 @@ unsafe extern "C" fn form_new(
moves: OwnedPtr<Box<dyn LearnableMoves>>, moves: OwnedPtr<Box<dyn LearnableMoves>>,
flags: *const *const c_char, flags: *const *const c_char,
flags_length: usize, flags_length: usize,
) -> IdentifiablePointer<Arc<Form>> { ) -> IdentifiablePointer<Arc<dyn Form>> {
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into(); let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let abilities = std::slice::from_raw_parts(abilities, abilities_length); let abilities = std::slice::from_raw_parts(abilities, abilities_length);
@ -46,7 +46,7 @@ unsafe extern "C" fn form_new(
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into()); flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
} }
Arc::new(Form::new( let a: Arc<dyn Form> = Arc::new(FormImpl::new(
&name, &name,
height, height,
weight, weight,
@ -57,47 +57,49 @@ unsafe extern "C" fn form_new(
hidden_abilities_vec, hidden_abilities_vec,
*Box::from_raw(moves), *Box::from_raw(moves),
flags_set, flags_set,
)) ));
.into() a.into()
} }
/// Drops a reference count for a form. /// Drops a reference count for a form.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn form_drop(ptr: OwnedPtr<Arc<Form>>) { unsafe extern "C" fn form_drop(ptr: OwnedPtr<Arc<dyn Form>>) {
drop_in_place(ptr) drop_in_place(ptr)
} }
/// The name of the form. /// The name of the form.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<Form>>) -> OwnedPtr<c_char> { unsafe extern "C" fn form_name(ptr: ExternPointer<Arc<dyn Form>>) -> OwnedPtr<c_char> {
let name = ptr.as_ref().name(); let name = ptr.as_ref().name();
CString::new(name.str()).unwrap().into_raw() CString::new(name.str()).unwrap().into_raw()
} }
ffi_arc_getter!(Form, height, f32); ffi_arc_getter!(dyn Form, height, f32);
ffi_arc_getter!(Form, weight, f32); ffi_arc_getter!(dyn Form, weight, f32);
ffi_arc_getter!(Form, base_experience, u32); ffi_arc_getter!(dyn Form, base_experience, u32);
ffi_vec_value_getters!(Form, types, TypeIdentifier); ffi_vec_value_getters!(dyn Form, types, TypeIdentifier);
/// The inherent values of a form of species that are used for the stats of a Pokemon. /// The inherent values of a form of species that are used for the stats of a Pokemon.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn form_base_stats(ptr: ExternPointer<Arc<Form>>) -> IdentifiablePointer<StaticStatisticSet<u16>> { unsafe extern "C" fn form_base_stats(
ptr: ExternPointer<Arc<dyn Form>>,
) -> IdentifiablePointer<StaticStatisticSet<u16>> {
(ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into() (ptr.as_ref().base_stats() as *const StaticStatisticSet<u16>).into()
} }
ffi_vec_stringkey_getters!(Form, abilities); ffi_vec_stringkey_getters!(dyn Form, abilities);
ffi_vec_stringkey_getters!(Form, hidden_abilities); ffi_vec_stringkey_getters!(dyn Form, hidden_abilities);
/// The moves a Pokemon with this form can learn. /// The moves a Pokemon with this form can learn.
#[no_mangle] #[no_mangle]
extern "C" fn form_moves(ptr: ExternPointer<Arc<Form>>) -> BorrowedPtr<Box<dyn LearnableMoves>> { extern "C" fn form_moves(ptr: ExternPointer<Arc<dyn Form>>) -> BorrowedPtr<Box<dyn LearnableMoves>> {
ptr.as_ref().moves() as *const Box<dyn LearnableMoves> ptr.as_ref().moves() as *const Box<dyn LearnableMoves>
} }
/// Check if the form has a specific flag set. /// Check if the form has a specific flag set.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn form_has_flag(ptr: ExternPointer<Arc<Form>>, flag: *const c_char) -> u8 { unsafe extern "C" fn form_has_flag(ptr: ExternPointer<Arc<dyn Form>>, flag: *const c_char) -> u8 {
let flag = CStr::from_ptr(flag).into(); let flag = CStr::from_ptr(flag).into();
u8::from(ptr.as_ref().has_flag(&flag)) u8::from(ptr.as_ref().has_flag(&flag))
} }

View File

@ -72,7 +72,7 @@ macro_rules! library_interface {
}; };
} }
library_interface!(SpeciesLibrary, Species); library_interface!(SpeciesLibrary, dyn Species);
library_interface!(MoveLibrary, dyn MoveData); library_interface!(MoveLibrary, dyn MoveData);
library_interface!(AbilityLibrary, dyn Ability); library_interface!(AbilityLibrary, dyn Ability);
library_interface!(ItemLibrary, dyn Item); library_interface!(ItemLibrary, dyn Item);

View File

@ -1,5 +1,5 @@
use crate::ffi::{ffi_arc_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr}; use crate::ffi::{ffi_arc_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Form, Species}; use crate::static_data::{Form, Species, SpeciesImpl};
use crate::StringKey; use crate::StringKey;
use hashbrown::HashSet; use hashbrown::HashSet;
use std::ffi::{c_char, CStr}; use std::ffi::{c_char, CStr};
@ -14,10 +14,10 @@ unsafe extern "C" fn species_new(
gender_rate: f32, gender_rate: f32,
growth_rate: BorrowedPtr<c_char>, growth_rate: BorrowedPtr<c_char>,
capture_rate: u8, capture_rate: u8,
default_form: OwnedPtr<Arc<Form>>, default_form: OwnedPtr<Arc<dyn Form>>,
flags: *const *const c_char, flags: *const *const c_char,
flags_length: usize, flags_length: usize,
) -> IdentifiablePointer<Arc<Species>> { ) -> IdentifiablePointer<Arc<dyn Species>> {
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into(); let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let growth_rate: StringKey = CStr::from_ptr(growth_rate).to_str().unwrap().into(); let growth_rate: StringKey = CStr::from_ptr(growth_rate).to_str().unwrap().into();
@ -26,7 +26,7 @@ unsafe extern "C" fn species_new(
for flag in flags { for flag in flags {
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into()); flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
} }
Arc::new(Species::new( let a: Arc<dyn Species> = Arc::new(SpeciesImpl::new(
id, id,
&name, &name,
gender_rate, gender_rate,
@ -34,28 +34,28 @@ unsafe extern "C" fn species_new(
capture_rate, capture_rate,
default_form.as_ref().unwrap().clone(), default_form.as_ref().unwrap().clone(),
flags_set, flags_set,
)) ));
.into() a.into()
} }
/// Drop a reference to the species. /// Drop a reference to the species.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<Species>>) { unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<dyn Species>>) {
drop_in_place(ptr); drop_in_place(ptr);
} }
ffi_arc_getter!(Species, id, u16); ffi_arc_getter!(dyn Species, id, u16);
ffi_arc_stringkey_getter!(Species, name); ffi_arc_stringkey_getter!(dyn Species, name);
ffi_arc_getter!(Species, gender_rate, f32); ffi_arc_getter!(dyn Species, gender_rate, f32);
ffi_arc_stringkey_getter!(Species, growth_rate); ffi_arc_stringkey_getter!(dyn Species, growth_rate);
ffi_arc_getter!(Species, capture_rate, u8); ffi_arc_getter!(dyn Species, capture_rate, u8);
/// Adds a new form to the species. /// Adds a new form to the species.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn species_add_form( unsafe extern "C" fn species_add_form(
mut species: ExternPointer<Arc<Species>>, mut species: ExternPointer<Arc<dyn Species>>,
name: BorrowedPtr<c_char>, name: BorrowedPtr<c_char>,
form: OwnedPtr<Arc<Form>>, form: OwnedPtr<Arc<dyn Form>>,
) { ) {
let form = form.as_ref().unwrap().clone(); let form = form.as_ref().unwrap().clone();
species.as_mut().add_form(CStr::from_ptr(name).into(), form) species.as_mut().add_form(CStr::from_ptr(name).into(), form)
@ -64,9 +64,9 @@ unsafe extern "C" fn species_add_form(
/// Gets a form by name. /// Gets a form by name.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn species_get_form( unsafe extern "C" fn species_get_form(
species: ExternPointer<Arc<Species>>, species: ExternPointer<Arc<dyn Species>>,
name: BorrowedPtr<c_char>, name: BorrowedPtr<c_char>,
) -> IdentifiablePointer<Arc<Form>> { ) -> IdentifiablePointer<Arc<dyn Form>> {
let form = species.as_ref().get_form(&CStr::from_ptr(name).into()); let form = species.as_ref().get_form(&CStr::from_ptr(name).into());
if let Some(form) = form { if let Some(form) = form {
form.into() form.into()

View File

@ -63,7 +63,7 @@ register! {
fn pokemon_get_species( fn pokemon_get_species(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
) -> ExternRef<Species> { ) -> ExternRef<dyn Species> {
let species = pokemon.value_func(&env).unwrap().species(); let species = pokemon.value_func(&env).unwrap().species();
ExternRef::func_new(&env, &species) ExternRef::func_new(&env, &species)
} }

View File

@ -11,7 +11,7 @@ fn species_library_get_species(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
lib: ExternRef<SpeciesLibrary>, lib: ExternRef<SpeciesLibrary>,
string_key: ExternRef<StringKey>, string_key: ExternRef<StringKey>,
) -> ExternRef<Species> { ) -> ExternRef<dyn Species> {
let lib = lib.value_func(&env).unwrap(); let lib = lib.value_func(&env).unwrap();
let m = lib.get(string_key.value_func(&env).unwrap()); let m = lib.get(string_key.value_func(&env).unwrap());
if let Some(v) = m { if let Some(v) = m {
@ -23,39 +23,39 @@ fn species_library_get_species(
fn species_get_capture_rate( fn species_get_capture_rate(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
species: ExternRef<Species>, species: ExternRef<dyn Species>,
) -> u8 { ) -> u8 {
species.value_func(&env).unwrap().capture_rate() species.value_func_arc(&env).unwrap().capture_rate()
} }
fn species_get_growth_rate( fn species_get_growth_rate(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
species: ExternRef<Species>, species: ExternRef<dyn Species>,
) -> ExternRef<StringKey> { ) -> ExternRef<StringKey> {
let species = species.value_func(&env).unwrap(); let species = species.value_func_arc(&env).unwrap();
ExternRef::func_new(&env, species.growth_rate()) ExternRef::func_new(&env, species.growth_rate())
} }
fn species_get_gender_rate( fn species_get_gender_rate(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
species: ExternRef<Species>, species: ExternRef<dyn Species>,
) -> f32 { ) -> f32 {
species.value_func(&env).unwrap().gender_rate() species.value_func_arc(&env).unwrap().gender_rate()
} }
fn species_get_name( fn species_get_name(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
species: ExternRef<Species>, species: ExternRef<dyn Species>,
) -> ExternRef<StringKey> { ) -> ExternRef<StringKey> {
let species = species.value_func(&env).unwrap(); let species = species.value_func_arc(&env).unwrap();
ExternRef::func_new(&env, species.name()) ExternRef::func_new(&env, species.name())
} }
fn species_get_id( fn species_get_id(
env: FunctionEnvMut<WebAssemblyEnv>, env: FunctionEnvMut<WebAssemblyEnv>,
species: ExternRef<Species>, species: ExternRef<dyn Species>,
) -> u16 { ) -> u16 {
species.value_func(&env).unwrap().id() species.value_func_arc(&env).unwrap().id()
} }

View File

@ -40,3 +40,17 @@ impl GrowthRate for LookupGrowthRate {
} }
} }
} }
#[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub GrowthRate {}
impl GrowthRate for GrowthRate {
fn calculate_level(&self, experience: u32) -> LevelInt;
fn calculate_experience(&self, level: LevelInt) -> u32;
}
}
}

View File

@ -133,3 +133,26 @@ impl ValueIdentifiable for ItemImpl {
self.identifier self.identifier
} }
} }
#[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub Item {}
impl Item for Item {
fn name(&self) -> &StringKey;
fn category(&self) -> ItemCategory;
fn battle_category(&self) -> BattleItemCategory;
fn price(&self) -> i32;
fn flags(&self) -> &HashSet<StringKey>;
fn has_flag(&self, key: &StringKey) -> bool;
}
impl ValueIdentifiable for Item {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}

View File

@ -19,6 +19,9 @@ pub use static_data::StaticData;
#[doc(inline)] #[doc(inline)]
pub use type_library::*; pub use type_library::*;
#[cfg(test)]
pub(crate) mod tests {}
/// The library data for abilities. /// The library data for abilities.
mod ability_library; mod ability_library;
/// Basic helper trait for libraries. /// Basic helper trait for libraries.

View File

@ -13,7 +13,7 @@ pub struct SpeciesLibrary {
/// A unique identifier so we know what value this is. /// A unique identifier so we know what value this is.
identifier: ValueIdentifier, identifier: ValueIdentifier,
/// The underlying map. /// The underlying map.
map: IndexMap<StringKey, Arc<Species>>, map: IndexMap<StringKey, Arc<dyn Species>>,
} }
impl SpeciesLibrary { impl SpeciesLibrary {
@ -26,11 +26,11 @@ impl SpeciesLibrary {
} }
} }
impl DataLibrary<Species> for SpeciesLibrary { impl DataLibrary<dyn Species> for SpeciesLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<Species>> { fn map(&self) -> &IndexMap<StringKey, Arc<dyn Species>> {
&self.map &self.map
} }
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Species>> { fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Species>> {
&mut self.map &mut self.map
} }
} }
@ -48,18 +48,18 @@ pub mod tests {
use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::data_library::DataLibrary;
use crate::static_data::libraries::species_library::SpeciesLibrary; use crate::static_data::libraries::species_library::SpeciesLibrary;
use crate::static_data::Species; use crate::static_data::LearnableMovesImpl;
use crate::static_data::StaticStatisticSet; use crate::static_data::{FormImpl, StaticStatisticSet};
use crate::static_data::{Form, LearnableMovesImpl}; use crate::static_data::{Species, SpeciesImpl};
fn build_species() -> Species { fn build_species() -> Arc<dyn Species> {
Species::new( Arc::new(SpeciesImpl::new(
0, 0,
&"foo".into(), &"foo".into(),
0.5, 0.5,
&"test_growthrate".into(), &"test_growthrate".into(),
0, 0,
Arc::new(Form::new( Arc::new(FormImpl::new(
&"default".into(), &"default".into(),
0.0, 0.0,
0.0, 0.0,
@ -72,7 +72,7 @@ pub mod tests {
HashSet::new(), HashSet::new(),
)), )),
HashSet::new(), HashSet::new(),
) ))
} }
pub fn build() -> SpeciesLibrary { pub fn build() -> SpeciesLibrary {
@ -80,7 +80,7 @@ pub mod tests {
let species = build_species(); let species = build_species();
// Borrow as mut so we can insert // Borrow as mut so we can insert
let w = &mut lib; let w = &mut lib;
w.add(&"foo".into(), Arc::new(species)); w.add(&"foo".into(), species);
// Drops borrow as mut // Drops borrow as mut
lib lib

View File

@ -17,6 +17,23 @@ pub use statistic_set::*;
pub use statistics::*; pub use statistics::*;
use std::fmt::{Display, Formatter}; use std::fmt::{Display, Formatter};
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[doc(inline)]
pub use growth_rates::tests::*;
#[doc(inline)]
pub use items::tests::*;
#[doc(inline)]
pub use libraries::tests::*;
#[doc(inline)]
pub use moves::tests::*;
#[doc(inline)]
pub use natures::tests::*;
#[doc(inline)]
pub use species_data::tests::*;
}
/// Growth rates define how fast a Pokemon can level up. /// Growth rates define how fast a Pokemon can level up.
mod growth_rates; mod growth_rates;
/// Items are objects which the player can pick up, keep in their Bag, and use in some manner /// Items are objects which the player can pick up, keep in their Bag, and use in some manner

View File

@ -3,6 +3,16 @@ pub use move_data::*;
#[doc(inline)] #[doc(inline)]
pub use secondary_effect::*; pub use secondary_effect::*;
#[cfg(test)]
pub(crate) mod tests {
use super::*;
#[doc(inline)]
pub use move_data::tests::*;
#[doc(inline)]
pub use secondary_effect::tests::*;
}
/// The data belonging to a certain move. /// The data belonging to a certain move.
mod move_data; mod move_data;
/// A secondary effect is an effect on a move that happens after it hits. /// A secondary effect is an effect on a move that happens after it hits.

View File

@ -208,6 +208,9 @@ impl ValueIdentifiable for MoveDataImpl {
} }
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! { mockall::mock! {
#[derive(Debug)] #[derive(Debug)]
pub MoveData{} pub MoveData{}
@ -230,3 +233,4 @@ mockall::mock! {
} }
} }
} }
}

View File

@ -59,12 +59,28 @@ impl ValueIdentifiable for SecondaryEffectImpl {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub(crate) mod tests {
use super::*;
use assert_approx_eq::assert_approx_eq; use assert_approx_eq::assert_approx_eq;
use crate::static_data::moves::secondary_effect::SecondaryEffect; use crate::static_data::moves::secondary_effect::SecondaryEffect;
use crate::static_data::SecondaryEffectImpl; use crate::static_data::SecondaryEffectImpl;
mockall::mock! {
#[derive(Debug)]
pub SecondaryEffect{}
impl SecondaryEffect for SecondaryEffect {
fn chance(&self) -> f32;
fn effect_name(&self) -> &StringKey;
fn parameters(&self) -> &Vec<EffectParameter>;
}
impl ValueIdentifiable for SecondaryEffect{
fn value_identifier(&self) -> ValueIdentifier{
ValueIdentifier::new(0)
}
}
}
#[test] #[test]
fn create_secondary_effect() { fn create_secondary_effect() {
let empty = SecondaryEffectImpl::new(0.0, "".into(), vec![]); let empty = SecondaryEffectImpl::new(0.0, "".into(), vec![]);

View File

@ -80,3 +80,23 @@ impl ValueIdentifiable for NatureImpl {
self.identifier self.identifier
} }
} }
#[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub Nature {}
impl Nature for Nature {
fn increased_stat(&self) -> Statistic;
fn decreased_stat(&self) -> Statistic;
fn get_stat_modifier(&self, stat: Statistic) -> f32;
}
impl ValueIdentifiable for Nature {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}

View File

@ -68,3 +68,23 @@ pub struct AbilityIndex {
/// The index of the ability. /// The index of the ability.
pub index: u8, pub index: u8,
} }
#[cfg(test)]
pub(crate) mod tests {
use super::*;
mockall::mock! {
#[derive(Debug)]
pub Ability{}
impl Ability for Ability {
fn name(&self) -> &StringKey;
fn effect(&self) -> &StringKey;
fn parameters(&self) -> &Vec<EffectParameter>;
}
impl ValueIdentifiable for Ability {
fn value_identifier(&self) -> ValueIdentifier {
ValueIdentifier::new(0)
}
}
}
}

View File

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

View File

@ -59,9 +59,8 @@ impl LearnableMoves for LearnableMovesImpl {
} }
#[cfg(test)] #[cfg(test)]
mod tests { pub(crate) mod tests {
use crate::static_data::species_data::learnable_moves::LearnableMoves; use super::*;
use crate::static_data::LearnableMovesImpl;
#[test] #[test]
fn adds_level_moves() { fn adds_level_moves() {
@ -92,4 +91,12 @@ mod tests {
assert_eq!(distinct.len(), 1); assert_eq!(distinct.len(), 1);
assert_eq!(distinct[0], "foo".into()); assert_eq!(distinct[0], "foo".into());
} }
mockall::mock! {
pub LearnableMoves {
fn add_level_move(&mut self, level: LevelInt, m: &StringKey);
fn get_learned_by_level<'a>(&'a self, level: LevelInt) -> Option<&'a Vec<StringKey>>;
fn get_distinct_level_moves(&self) -> &Vec<StringKey>;
}
}
} }

View File

@ -9,6 +9,14 @@ pub use learnable_moves::*;
#[doc(inline)] #[doc(inline)]
pub use species::*; pub use species::*;
#[cfg(test)]
pub(crate) mod tests {
pub use super::ability::tests::*;
pub use super::form::tests::*;
pub use super::learnable_moves::tests::*;
pub use super::species::tests::*;
}
/// An ability is a passive effect in battle that is attached to a Pokemon. /// An ability is a passive effect in battle that is attached to a Pokemon.
mod ability; mod ability;
/// A form is a variant of a specific species. A species always has at least one form, but can have /// A form is a variant of a specific species. A species always has at least one form, but can have

View File

@ -1,3 +1,4 @@
use std::fmt::Debug;
use std::sync::{Arc, LazyLock}; use std::sync::{Arc, LazyLock};
use hashbrown::{HashMap, HashSet}; use hashbrown::{HashMap, HashSet};
@ -9,9 +10,38 @@ use crate::static_data::Gender;
use crate::StringKey; use crate::StringKey;
use crate::{Random, ValueIdentifiable, ValueIdentifier}; 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. /// The data belonging to a Pokemon with certain characteristics.
#[derive(Debug)] #[derive(Debug)]
pub struct Species { pub struct SpeciesImpl {
/// A unique identifier so we know what value this is. /// A unique identifier so we know what value this is.
identifier: ValueIdentifier, identifier: ValueIdentifier,
/// The national dex identifier of the Pokemon. /// The national dex identifier of the Pokemon.
@ -26,7 +56,7 @@ pub struct Species {
/// uncatchable. /// uncatchable.
capture_rate: u8, capture_rate: u8,
/// The forms that belong to this Pokemon. /// The forms that belong to this Pokemon.
forms: RwLock<HashMap<StringKey, Arc<Form>>>, forms: RwLock<HashMap<StringKey, Arc<dyn Form>>>,
/// The arbitrary flags that can be set on a Pokemon for script use. /// The arbitrary flags that can be set on a Pokemon for script use.
flags: HashSet<StringKey>, flags: HashSet<StringKey>,
} }
@ -39,7 +69,7 @@ fn get_default_key() -> StringKey {
DEFAULT_KEY.clone() DEFAULT_KEY.clone()
} }
impl Species { impl SpeciesImpl {
/// Creates a new species. /// Creates a new species.
pub fn new( pub fn new(
id: u16, id: u16,
@ -47,12 +77,12 @@ impl Species {
gender_rate: f32, gender_rate: f32,
growth_rate: &StringKey, growth_rate: &StringKey,
capture_rate: u8, capture_rate: u8,
default_form: Arc<Form>, default_form: Arc<dyn Form>,
flags: HashSet<StringKey>, flags: HashSet<StringKey>,
) -> Species { ) -> Self {
let mut forms = HashMap::with_capacity(1); let mut forms = HashMap::with_capacity(1);
forms.insert_unique_unchecked(get_default_key(), default_form); forms.insert_unique_unchecked(get_default_key(), default_form);
Species { Self {
identifier: Default::default(), identifier: Default::default(),
id, id,
name: name.clone(), name: name.clone(),
@ -63,54 +93,56 @@ impl Species {
flags, flags,
} }
} }
}
impl Species for SpeciesImpl {
/// The national dex identifier of the Pokemon. /// The national dex identifier of the Pokemon.
pub fn id(&self) -> u16 { fn id(&self) -> u16 {
self.id self.id
} }
/// The name of the Pokemon. /// The name of the Pokemon.
pub fn name(&self) -> &StringKey { fn name(&self) -> &StringKey {
&self.name &self.name
} }
/// The chance between 0.0 and 1.0 that a Pokemon is female. /// The chance between 0.0 and 1.0 that a Pokemon is female.
pub fn gender_rate(&self) -> f32 { fn gender_rate(&self) -> f32 {
self.gender_rate self.gender_rate
} }
/// How much experience is required for a level. /// How much experience is required for a level.
pub fn growth_rate(&self) -> &StringKey { fn growth_rate(&self) -> &StringKey {
&self.growth_rate &self.growth_rate
} }
/// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is /// How hard it is to capture a Pokemon. 255 means this will be always caught, 0 means this is
/// uncatchable. /// uncatchable.
pub fn capture_rate(&self) -> u8 { fn capture_rate(&self) -> u8 {
self.capture_rate self.capture_rate
} }
/// The forms that belong to this Pokemon. /// The forms that belong to this Pokemon.
pub fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<Form>>> { fn forms(&self) -> RwLockReadGuard<'_, RawRwLock, HashMap<StringKey, Arc<dyn Form>>> {
self.forms.read() self.forms.read()
} }
/// The arbitrary flags that can be set on a Pokemon for script use. /// The arbitrary flags that can be set on a Pokemon for script use.
pub fn flags(&self) -> &HashSet<StringKey> { fn flags(&self) -> &HashSet<StringKey> {
&self.flags &self.flags
} }
/// Adds a new form to the species. /// Adds a new form to the species.
pub fn add_form(&self, id: StringKey, form: Arc<Form>) { fn add_form(&self, id: StringKey, form: Arc<dyn Form>) {
self.forms.write().insert(id, form); self.forms.write().insert(id, form);
} }
/// Gets a form by name. /// Gets a form by name.
pub fn get_form(&self, id: &StringKey) -> Option<Arc<Form>> { fn get_form(&self, id: &StringKey) -> Option<Arc<dyn Form>> {
self.forms.read().get(id).cloned() self.forms.read().get(id).cloned()
} }
/// Gets the form the Pokemon will have by default, if no other form is specified. /// Gets the form the Pokemon will have by default, if no other form is specified.
pub fn get_default_form(&self) -> Arc<Form> { fn get_default_form(&self) -> Arc<dyn Form> {
self.forms.read().get(&get_default_key()).unwrap().clone() self.forms.read().get(&get_default_key()).unwrap().clone()
} }
/// Gets a random gender. /// Gets a random gender.
pub fn get_random_gender(&self, rand: &mut Random) -> Gender { fn get_random_gender(&self, rand: &mut Random) -> Gender {
if self.gender_rate < 0.0 { if self.gender_rate < 0.0 {
Gender::Genderless Gender::Genderless
} else if rand.get_float() >= self.gender_rate { } else if rand.get_float() >= self.gender_rate {
@ -121,13 +153,42 @@ impl Species {
} }
/// Check whether the Pokemon has a specific flag set. /// Check whether the Pokemon has a specific flag set.
pub fn has_flag(&self, key: &StringKey) -> bool { fn has_flag(&self, key: &StringKey) -> bool {
self.flags.contains(key) self.flags.contains(key)
} }
} }
impl ValueIdentifiable for Species { impl ValueIdentifiable for SpeciesImpl {
fn value_identifier(&self) -> ValueIdentifier { fn value_identifier(&self) -> ValueIdentifier {
self.identifier 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)
}
}
}
}

View File

@ -16,10 +16,10 @@ use pkmn_lib::dynamic_data::Gen7DamageLibrary;
use pkmn_lib::dynamic_data::Gen7MiscLibrary; use pkmn_lib::dynamic_data::Gen7MiscLibrary;
use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver; use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver;
use pkmn_lib::static_data::{ use pkmn_lib::static_data::{
AbilityImpl, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, ItemImpl, AbilityImpl, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, FormImpl, GrowthRateLibrary,
ItemLibrary, LearnableMoves, LearnableMovesImpl, LibrarySettings, LookupGrowthRate, MoveDataImpl, MoveLibrary, ItemImpl, ItemLibrary, LearnableMoves, LearnableMovesImpl, LibrarySettings, LookupGrowthRate, MoveDataImpl,
NatureImpl, NatureLibrary, SecondaryEffect, SecondaryEffectImpl, Species, StaticData, StaticStatisticSet, MoveLibrary, NatureImpl, NatureLibrary, SecondaryEffect, SecondaryEffectImpl, SpeciesImpl, StaticData,
Statistic, TypeLibrary, StaticStatisticSet, Statistic, TypeLibrary,
}; };
use pkmn_lib::StringKey; use pkmn_lib::StringKey;
@ -263,7 +263,7 @@ pub fn load_species(path: &String, library: &mut StaticData) {
let default_form_value = forms.get("default").unwrap(); let default_form_value = forms.get("default").unwrap();
let default_form = parse_form("default".into(), default_form_value, library); let default_form = parse_form("default".into(), default_form_value, library);
let species = Species::new( let species = SpeciesImpl::new(
id as u16, id as u16,
&name, &name,
gender_rate as f32, gender_rate as f32,
@ -285,7 +285,7 @@ fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) {
library.finalize(); library.finalize();
} }
fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Arc<Form> { fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Arc<dyn Form> {
let mut abilities = Vec::new(); let mut abilities = Vec::new();
for a in value.get("abilities").unwrap().as_array().unwrap() { for a in value.get("abilities").unwrap().as_array().unwrap() {
abilities.push(a.as_str().unwrap().into()); abilities.push(a.as_str().unwrap().into());
@ -310,7 +310,7 @@ fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Arc<F
let moves = parse_moves(value.get("moves").unwrap(), library.moves()); let moves = parse_moves(value.get("moves").unwrap(), library.moves());
Arc::new(Form::new( Arc::new(FormImpl::new(
&name, &name,
height as f32, height as f32,
weight as f32, weight as f32,