Make Ability a trait
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2022-11-27 17:47:51 +01:00
parent 996a35ffa4
commit e04f61d9e6
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
9 changed files with 50 additions and 31 deletions

View File

@ -86,7 +86,7 @@ pub struct Pokemon {
ability_index: AbilityIndex, ability_index: AbilityIndex,
/// An ability can be overriden to an arbitrary ability. This is for example used for the Mummy /// An ability can be overriden to an arbitrary ability. This is for example used for the Mummy
/// ability. /// ability.
override_ability: Option<Ability>, override_ability: Option<Arc<dyn Ability>>,
/// If in battle, we have additional data. /// If in battle, we have additional data.
battle_data: RwLock<Option<PokemonBattleData>>, battle_data: RwLock<Option<PokemonBattleData>>,
@ -416,7 +416,7 @@ impl Pokemon {
self.override_ability.is_some() self.override_ability.is_some()
} }
/// Returns the currently active ability. /// Returns the currently active ability.
pub fn active_ability(&self) -> &Ability { pub fn active_ability(&self) -> &Arc<dyn Ability> {
if let Some(v) = &self.override_ability { if let Some(v) = &self.override_ability {
return v; return v;
} }

View File

@ -260,8 +260,8 @@ extern "C" fn pokemon_is_ability_overriden(ptr: ExternPointer<Arc<Pokemon>>) ->
/// Returns the currently active ability. /// Returns the currently active ability.
#[no_mangle] #[no_mangle]
extern "C" fn pokemon_active_ability(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Ability> { extern "C" fn pokemon_active_ability(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Ability>> {
(ptr.as_ref().active_ability() as *const Ability).into() ptr.as_ref().active_ability().clone().into()
} }
/// Whether or not the Pokemon is allowed to gain experience. /// Whether or not the Pokemon is allowed to gain experience.

View File

@ -1,5 +1,5 @@
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr}; use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
use crate::static_data::{Ability, EffectParameter}; use crate::static_data::{Ability, AbilityImpl, EffectParameter};
use crate::StringKey; use crate::StringKey;
use std::ffi::{c_char, CStr, CString}; use std::ffi::{c_char, CStr, CString};
use std::ptr::drop_in_place; use std::ptr::drop_in_place;
@ -12,7 +12,7 @@ unsafe extern "C" fn ability_new(
effect: *const c_char, effect: *const c_char,
parameters: *const OwnedPtr<EffectParameter>, parameters: *const OwnedPtr<EffectParameter>,
parameters_length: usize, parameters_length: usize,
) -> IdentifiablePointer<Arc<Ability>> { ) -> IdentifiablePointer<Arc<dyn Ability>> {
let parameters = std::slice::from_raw_parts(parameters, parameters_length); let parameters = std::slice::from_raw_parts(parameters, parameters_length);
let mut parameters_vec: Vec<EffectParameter> = Vec::with_capacity(parameters_length); let mut parameters_vec: Vec<EffectParameter> = Vec::with_capacity(parameters_length);
for parameter in parameters { for parameter in parameters {
@ -22,37 +22,38 @@ unsafe extern "C" fn ability_new(
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into(); let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
let effect: StringKey = CStr::from_ptr(effect).to_str().unwrap().into(); let effect: StringKey = CStr::from_ptr(effect).to_str().unwrap().into();
Arc::new(Ability::new(&name, &effect, parameters_vec)).into() let arc: Arc<dyn Ability> = Arc::new(AbilityImpl::new(&name, &effect, parameters_vec));
arc.into()
} }
/// Drops a reference counted ability. /// Drops a reference counted ability.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn ability_drop(ptr: OwnedPtr<Arc<Ability>>) { unsafe extern "C" fn ability_drop(ptr: OwnedPtr<Arc<dyn Ability>>) {
drop_in_place(ptr) drop_in_place(ptr)
} }
/// The name of the ability. /// The name of the ability.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn ability_name(ptr: ExternPointer<Arc<Ability>>) -> OwnedPtr<c_char> { unsafe extern "C" fn ability_name(ptr: ExternPointer<Arc<dyn Ability>>) -> OwnedPtr<c_char> {
CString::new(ptr.as_ref().name().str()).unwrap().into_raw() CString::new(ptr.as_ref().name().str()).unwrap().into_raw()
} }
/// The name of the script effect of the ability. /// The name of the script effect of the ability.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn ability_effect(ptr: ExternPointer<Arc<Ability>>) -> OwnedPtr<c_char> { unsafe extern "C" fn ability_effect(ptr: ExternPointer<Arc<dyn Ability>>) -> OwnedPtr<c_char> {
CString::new(ptr.as_ref().effect().str()).unwrap().into_raw() CString::new(ptr.as_ref().effect().str()).unwrap().into_raw()
} }
/// The length of the parameters for the script effect of the ability. /// The length of the parameters for the script effect of the ability.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn ability_parameter_length(ptr: ExternPointer<Arc<Ability>>) -> usize { unsafe extern "C" fn ability_parameter_length(ptr: ExternPointer<Arc<dyn Ability>>) -> usize {
ptr.as_ref().parameters().len() ptr.as_ref().parameters().len()
} }
/// Gets a parameter for the script effect of the ability. /// Gets a parameter for the script effect of the ability.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn ability_parameter_get( unsafe extern "C" fn ability_parameter_get(
ptr: ExternPointer<Arc<Ability>>, ptr: ExternPointer<Arc<dyn Ability>>,
index: usize, index: usize,
) -> IdentifiablePointer<EffectParameter> { ) -> IdentifiablePointer<EffectParameter> {
if let Some(p) = ptr.as_ref().parameters().get(index) { if let Some(p) = ptr.as_ref().parameters().get(index) {

View File

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

View File

@ -10,13 +10,14 @@ extern "C" fn nature_new(
decrease_stat: Statistic, decrease_stat: Statistic,
increase_modifier: f32, increase_modifier: f32,
decrease_modifier: f32, decrease_modifier: f32,
) -> IdentifiablePointer<Arc<NatureImpl>> { ) -> IdentifiablePointer<Arc<dyn Nature>> {
NatureImpl::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier).into() let arc: Arc<dyn Nature> = NatureImpl::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier);
arc.into()
} }
/// Reduce the reference count for a nature. /// Reduce the reference count for a nature.
#[no_mangle] #[no_mangle]
unsafe extern "C" fn nature_drop(ptr: OwnedPtr<Arc<NatureImpl>>) { unsafe extern "C" fn nature_drop(ptr: OwnedPtr<Arc<dyn Nature>>) {
drop_in_place(ptr) drop_in_place(ptr)
} }

View File

@ -12,7 +12,7 @@ pub struct AbilityLibrary {
/// 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 for the library. /// The underlying map for the library.
map: IndexMap<StringKey, Arc<Ability>>, map: IndexMap<StringKey, Arc<dyn Ability>>,
} }
impl AbilityLibrary { impl AbilityLibrary {
@ -25,11 +25,11 @@ impl AbilityLibrary {
} }
} }
impl DataLibrary<Ability> for AbilityLibrary { impl DataLibrary<dyn Ability> for AbilityLibrary {
fn map(&self) -> &IndexMap<StringKey, Arc<Ability>> { fn map(&self) -> &IndexMap<StringKey, Arc<dyn Ability>> {
&self.map &self.map
} }
fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<Ability>> { fn get_modify(&mut self) -> &mut IndexMap<StringKey, Arc<dyn Ability>> {
&mut self.map &mut self.map
} }
} }
@ -42,7 +42,7 @@ impl ValueIdentifiable for AbilityLibrary {
#[cfg(test)] #[cfg(test)]
pub mod tests { pub mod tests {
use crate::static_data::Ability; use crate::static_data::AbilityImpl;
use crate::static_data::AbilityLibrary; use crate::static_data::AbilityLibrary;
use crate::static_data::DataLibrary; use crate::static_data::DataLibrary;
use crate::StringKey; use crate::StringKey;
@ -52,7 +52,11 @@ pub mod tests {
let mut lib = AbilityLibrary::new(1); let mut lib = AbilityLibrary::new(1);
lib.add( lib.add(
&StringKey::new("test_ability"), &StringKey::new("test_ability"),
Arc::new(Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new())), Arc::new(AbilityImpl::new(
&"test_ability".into(),
&"test_ability".into(),
Vec::new(),
)),
); );
lib lib
} }

View File

@ -1,9 +1,20 @@
use crate::static_data::EffectParameter; use crate::static_data::EffectParameter;
use crate::{StringKey, ValueIdentifiable, ValueIdentifier}; use crate::{StringKey, ValueIdentifiable, ValueIdentifier};
use std::fmt::Debug;
/// An ability is a passive effect in battle that is attached to a Pokemon.
pub trait Ability: Debug + ValueIdentifiable {
/// The name of the ability.
fn name(&self) -> &StringKey;
/// The name of the script effect of the ability.
fn effect(&self) -> &StringKey;
/// The parameters for the script effect of the ability.
fn parameters(&self) -> &Vec<EffectParameter>;
}
/// 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.
#[derive(Debug)] #[derive(Debug)]
pub struct Ability { pub struct AbilityImpl {
/// 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 ability. /// The name of the ability.
@ -14,7 +25,7 @@ pub struct Ability {
parameters: Vec<EffectParameter>, parameters: Vec<EffectParameter>,
} }
impl Ability { impl AbilityImpl {
/// Instantiates a new ability. /// Instantiates a new ability.
pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self { pub fn new(name: &StringKey, effect: &StringKey, parameters: Vec<EffectParameter>) -> Self {
Self { Self {
@ -24,22 +35,24 @@ impl Ability {
parameters, parameters,
} }
} }
}
impl Ability for AbilityImpl {
/// The name of the ability. /// The name of the ability.
pub fn name(&self) -> &StringKey { fn name(&self) -> &StringKey {
&self.name &self.name
} }
/// The name of the script effect of the ability. /// The name of the script effect of the ability.
pub fn effect(&self) -> &StringKey { fn effect(&self) -> &StringKey {
&self.effect &self.effect
} }
/// The parameters for the script effect of the ability. /// The parameters for the script effect of the ability.
pub fn parameters(&self) -> &Vec<EffectParameter> { fn parameters(&self) -> &Vec<EffectParameter> {
&self.parameters &self.parameters
} }
} }
impl ValueIdentifiable for Ability { impl ValueIdentifiable for AbilityImpl {
fn value_identifier(&self) -> ValueIdentifier { fn value_identifier(&self) -> ValueIdentifier {
self.identifier self.identifier
} }

View File

@ -116,7 +116,7 @@ impl Form {
} }
/// 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: &Ability) -> Option<AbilityIndex> { pub 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 {

View File

@ -16,7 +16,7 @@ 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::{
Ability, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, ItemImpl, AbilityImpl, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, ItemImpl,
ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveData, MoveLibrary, NatureImpl, NatureLibrary, ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveData, MoveLibrary, NatureImpl, NatureLibrary,
SecondaryEffect, Species, StaticData, StaticStatisticSet, Statistic, TypeLibrary, SecondaryEffect, Species, StaticData, StaticStatisticSet, Statistic, TypeLibrary,
}; };
@ -161,7 +161,7 @@ pub fn load_abilities(path: &String, ability_library: &mut AbilityLibrary) {
} }
} }
ability_library.add(&name, Arc::new(Ability::new(&name, &effect, parameters))); ability_library.add(&name, Arc::new(AbilityImpl::new(&name, &effect, parameters)));
} }
} }