diff --git a/src/ffi/static_data/form.rs b/src/ffi/static_data/form.rs index 8b37d37..d9bceb4 100644 --- a/src/ffi/static_data/form.rs +++ b/src/ffi/static_data/form.rs @@ -23,7 +23,7 @@ unsafe extern "C" fn form_new( abilities_length: usize, hidden_abilities: *const BorrowedPtr, hidden_abilities_length: usize, - moves: OwnedPtr, + moves: OwnedPtr>, flags: *const *const c_char, flags_length: usize, ) -> IdentifiablePointer> { @@ -89,7 +89,11 @@ unsafe extern "C" fn form_base_stats(ptr: ExternPointer>) -> Identifia ffi_vec_stringkey_getters!(Form, abilities); ffi_vec_stringkey_getters!(Form, hidden_abilities); -ffi_arc_getter!(Form, moves, BorrowedPtr); +/// The moves a Pokemon with this form can learn. +#[no_mangle] +extern "C" fn form_moves(ptr: ExternPointer>) -> BorrowedPtr> { + ptr.as_ref().moves() as *const Box +} /// Check if the form has a specific flag set. #[no_mangle] diff --git a/src/ffi/static_data/learnable_moves.rs b/src/ffi/static_data/learnable_moves.rs index 7ff25a1..a1dab7b 100644 --- a/src/ffi/static_data/learnable_moves.rs +++ b/src/ffi/static_data/learnable_moves.rs @@ -1,25 +1,25 @@ use crate::defines::LevelInt; use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr}; -use crate::static_data::LearnableMoves; +use crate::static_data::{LearnableMoves, LearnableMovesImpl}; use std::ffi::{c_char, CStr}; use std::ptr::drop_in_place; /// Instantiates a new Learnable Moves. #[no_mangle] -extern "C" fn learnable_moves_new() -> OwnedPtr { - Box::into_raw(Box::new(LearnableMoves::new())) +extern "C" fn learnable_moves_new() -> OwnedPtr> { + Box::into_raw(Box::new(Box::new(LearnableMovesImpl::new()))) } /// drops a learnablemoves struct. #[no_mangle] -unsafe extern "C" fn learnable_moves_drop(ptr: OwnedPtr) { +unsafe extern "C" fn learnable_moves_drop(ptr: OwnedPtr>) { drop_in_place(ptr) } /// Adds a new level move the Pokemon can learn. #[no_mangle] unsafe extern "C" fn learnable_moves_add_level_move( - mut ptr: ExternPointer, + mut ptr: ExternPointer>, level: LevelInt, move_name: BorrowedPtr, ) { diff --git a/src/static_data/libraries/species_library.rs b/src/static_data/libraries/species_library.rs index 1855a2b..c0dbbd6 100755 --- a/src/static_data/libraries/species_library.rs +++ b/src/static_data/libraries/species_library.rs @@ -48,10 +48,9 @@ pub mod tests { use crate::static_data::libraries::data_library::DataLibrary; use crate::static_data::libraries::species_library::SpeciesLibrary; - use crate::static_data::Form; - use crate::static_data::LearnableMoves; use crate::static_data::Species; use crate::static_data::StaticStatisticSet; + use crate::static_data::{Form, LearnableMovesImpl}; fn build_species() -> Species { Species::new( @@ -69,7 +68,7 @@ pub mod tests { StaticStatisticSet::new(10, 10, 10, 10, 10, 10), Vec::new(), Vec::new(), - LearnableMoves::new(), + Box::new(LearnableMovesImpl::new()), HashSet::new(), )), HashSet::new(), diff --git a/src/static_data/species_data/form.rs b/src/static_data/species_data/form.rs index d983240..507a4be 100755 --- a/src/static_data/species_data/form.rs +++ b/src/static_data/species_data/form.rs @@ -30,7 +30,7 @@ pub struct Form { /// The possible hidden abilities a Pokemon with this form can have. hidden_abilities: Vec, /// The moves a Pokemon with this form can learn. - moves: LearnableMoves, + moves: Box, /// Arbitrary flags can be set on a form for scripting use. flags: HashSet, } @@ -46,7 +46,7 @@ impl Form { base_stats: StaticStatisticSet, abilities: Vec, hidden_abilities: Vec, - moves: LearnableMoves, + moves: Box, flags: HashSet, ) -> Form { Form { @@ -96,8 +96,10 @@ impl Form { pub fn hidden_abilities(&self) -> &Vec { &self.hidden_abilities } + + #[allow(clippy::borrowed_box)] /// The moves a Pokemon with this form can learn. - pub fn moves(&self) -> &LearnableMoves { + pub fn moves(&self) -> &Box { &self.moves } /// Arbitrary flags can be set on a form for scripting use. diff --git a/src/static_data/species_data/learnable_moves.rs b/src/static_data/species_data/learnable_moves.rs index e913d69..211fa5c 100755 --- a/src/static_data/species_data/learnable_moves.rs +++ b/src/static_data/species_data/learnable_moves.rs @@ -1,26 +1,39 @@ use hashbrown::hash_map::Entry::{Occupied, Vacant}; use hashbrown::HashMap; +use std::fmt::Debug; use crate::defines::LevelInt; use crate::StringKey; -/// This allows for storage of the moves a Pokemon can learn. +/// The storage of the moves a Pokemon can learn. +pub trait LearnableMoves: Debug { + /// Adds a new level move the Pokemon can learn. + fn add_level_move(&mut self, level: LevelInt, m: &StringKey); + /// Gets all moves a Pokemon can learn when leveling up to a specific level. + fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec>; + /// Gets the distinct moves a Pokemon can learn through leveling up. + fn get_distinct_level_moves(&self) -> &Vec; +} + +/// The storage of the moves a Pokemon can learn. #[derive(Default, PartialEq, Eq, Debug)] -pub struct LearnableMoves { +pub struct LearnableMovesImpl { /// A map of the moves a Pokemon can learn per level. learned_by_level: HashMap>, /// A list of the distinct moves a Pokemon can learn through leveling up. distinct_level_moves: Vec, } -impl LearnableMoves { +impl LearnableMovesImpl { /// Instantiates a new Learnable Moves. - pub fn new() -> LearnableMoves { - LearnableMoves::default() + pub fn new() -> Self { + Self::default() } +} +impl LearnableMoves for LearnableMovesImpl { /// Adds a new level move the Pokemon can learn. - pub fn add_level_move(&mut self, level: LevelInt, m: &StringKey) { + fn add_level_move(&mut self, level: LevelInt, m: &StringKey) { match self.learned_by_level.entry(level) { Occupied(x) => { x.into_mut().push(m.clone()); @@ -35,12 +48,12 @@ impl LearnableMoves { } /// Gets all moves a Pokemon can learn when leveling up to a specific level. - pub fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec> { + fn get_learned_by_level(&self, level: LevelInt) -> Option<&Vec> { self.learned_by_level.get(&level) } /// Gets the distinct moves a Pokemon can learn through leveling up. - pub fn get_distinct_level_moves(&self) -> &Vec { + fn get_distinct_level_moves(&self) -> &Vec { &self.distinct_level_moves } } @@ -48,10 +61,11 @@ impl LearnableMoves { #[cfg(test)] mod tests { use crate::static_data::species_data::learnable_moves::LearnableMoves; + use crate::static_data::LearnableMovesImpl; #[test] fn adds_level_moves() { - let mut moves = LearnableMoves::new(); + let mut moves = LearnableMovesImpl::new(); moves.add_level_move(1, &"foo".into()); moves.add_level_move(1, &"bar".into()); @@ -63,7 +77,7 @@ mod tests { #[test] fn adds_two_same_moves_at_different_level() { - let mut moves = LearnableMoves::new(); + let mut moves = LearnableMovesImpl::new(); moves.add_level_move(1, &"foo".into()); moves.add_level_move(5, &"foo".into()); diff --git a/tests/common/library_loader.rs b/tests/common/library_loader.rs index d711ed6..75d58b3 100755 --- a/tests/common/library_loader.rs +++ b/tests/common/library_loader.rs @@ -17,9 +17,9 @@ use pkmn_lib::dynamic_data::Gen7MiscLibrary; use pkmn_lib::script_implementations::wasm::script_resolver::WebAssemblyScriptResolver; use pkmn_lib::static_data::{ AbilityImpl, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, ItemImpl, - ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveDataImpl, MoveLibrary, NatureImpl, - NatureLibrary, SecondaryEffect, SecondaryEffectImpl, Species, StaticData, StaticStatisticSet, Statistic, - TypeLibrary, + ItemLibrary, LearnableMoves, LearnableMovesImpl, LibrarySettings, LookupGrowthRate, MoveDataImpl, MoveLibrary, + NatureImpl, NatureLibrary, SecondaryEffect, SecondaryEffectImpl, Species, StaticData, StaticStatisticSet, + Statistic, TypeLibrary, }; use pkmn_lib::StringKey; @@ -368,8 +368,8 @@ where ) } -fn parse_moves(value: &Value, move_library: &MoveLibrary) -> LearnableMoves { - let mut moves = LearnableMoves::default(); +fn parse_moves(value: &Value, move_library: &MoveLibrary) -> Box { + let mut moves = LearnableMovesImpl::default(); let level_moves = value.get("levelMoves").unwrap().as_array().unwrap(); for level_move in level_moves { @@ -379,7 +379,7 @@ fn parse_moves(value: &Value, move_library: &MoveLibrary) -> LearnableMoves { moves.add_level_move(level, &name); } - moves + Box::new(moves) } fn parse_effect_parameter(value: &Value) -> EffectParameter {