Make Nature a trait
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
efd1acdfa5
commit
996a35ffa4
|
@ -77,7 +77,7 @@ pub struct Pokemon {
|
||||||
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
|
/// The [effort values](https://bulbapedia.bulbagarden.net/wiki/Effort_values) of the Pokemon.
|
||||||
effort_values: ClampedStatisticSet<u8, 0, 252>,
|
effort_values: ClampedStatisticSet<u8, 0, 252>,
|
||||||
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
||||||
nature: Arc<Nature>,
|
nature: Arc<dyn Nature>,
|
||||||
|
|
||||||
/// An optional nickname of the Pokemon.
|
/// An optional nickname of the Pokemon.
|
||||||
nickname: Option<String>,
|
nickname: Option<String>,
|
||||||
|
@ -443,7 +443,7 @@ impl Pokemon {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
||||||
pub fn nature(&self) -> &Arc<Nature> {
|
pub fn nature(&self) -> &Arc<dyn Nature> {
|
||||||
&self.nature
|
&self.nature
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -272,7 +272,7 @@ extern "C" fn pokemon_allowed_experience_gain(ptr: ExternPointer<Arc<Pokemon>>)
|
||||||
|
|
||||||
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
/// The [nature](https://bulbapedia.bulbagarden.net/wiki/Nature) of the Pokemon.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<Nature>> {
|
extern "C" fn pokemon_nature(ptr: ExternPointer<Arc<Pokemon>>) -> IdentifiablePointer<Arc<dyn Nature>> {
|
||||||
ptr.as_ref().nature().clone().into()
|
ptr.as_ref().nature().clone().into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<NatureLibrary>) {
|
||||||
unsafe extern "C" fn nature_library_load_nature(
|
unsafe extern "C" fn nature_library_load_nature(
|
||||||
mut ptr: ExternPointer<NatureLibrary>,
|
mut ptr: ExternPointer<NatureLibrary>,
|
||||||
name: BorrowedPtr<c_char>,
|
name: BorrowedPtr<c_char>,
|
||||||
nature: OwnedPtr<Arc<Nature>>,
|
nature: OwnedPtr<Arc<dyn Nature>>,
|
||||||
) {
|
) {
|
||||||
ptr.as_mut()
|
ptr.as_mut()
|
||||||
.load_nature(CStr::from_ptr(name).into(), nature.as_ref().unwrap().clone())
|
.load_nature(CStr::from_ptr(name).into(), nature.as_ref().unwrap().clone())
|
||||||
|
@ -32,7 +32,7 @@ unsafe extern "C" fn nature_library_load_nature(
|
||||||
unsafe extern "C" fn nature_library_get_nature(
|
unsafe extern "C" fn nature_library_get_nature(
|
||||||
ptr: ExternPointer<NatureLibrary>,
|
ptr: ExternPointer<NatureLibrary>,
|
||||||
name: BorrowedPtr<c_char>,
|
name: BorrowedPtr<c_char>,
|
||||||
) -> IdentifiablePointer<Arc<Nature>> {
|
) -> IdentifiablePointer<Arc<dyn Nature>> {
|
||||||
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
|
if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) {
|
||||||
nature.clone().into()
|
nature.clone().into()
|
||||||
} else {
|
} else {
|
||||||
|
@ -44,7 +44,7 @@ unsafe extern "C" fn nature_library_get_nature(
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
unsafe extern "C" fn nature_library_get_nature_name(
|
unsafe extern "C" fn nature_library_get_nature_name(
|
||||||
ptr: ExternPointer<NatureLibrary>,
|
ptr: ExternPointer<NatureLibrary>,
|
||||||
nature: BorrowedPtr<Arc<Nature>>,
|
nature: BorrowedPtr<Arc<dyn Nature>>,
|
||||||
) -> OwnedPtr<c_char> {
|
) -> OwnedPtr<c_char> {
|
||||||
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())
|
CString::new(ptr.as_ref().get_nature_name(nature.as_ref().unwrap()).str())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
|
use crate::ffi::{ExternPointer, IdentifiablePointer, OwnedPtr};
|
||||||
use crate::static_data::{Nature, Statistic};
|
use crate::static_data::{Nature, NatureImpl, Statistic};
|
||||||
use std::ptr::drop_in_place;
|
use std::ptr::drop_in_place;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -10,31 +10,31 @@ 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<Nature>> {
|
) -> IdentifiablePointer<Arc<NatureImpl>> {
|
||||||
Nature::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier).into()
|
NatureImpl::new(increase_stat, decrease_stat, increase_modifier, decrease_modifier).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<Nature>>) {
|
unsafe extern "C" fn nature_drop(ptr: OwnedPtr<Arc<NatureImpl>>) {
|
||||||
drop_in_place(ptr)
|
drop_in_place(ptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The stat that should receive the increased modifier.
|
/// The stat that should receive the increased modifier.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn nature_increased_stat(ptr: ExternPointer<Arc<Nature>>) -> Statistic {
|
extern "C" fn nature_increased_stat(ptr: ExternPointer<Arc<dyn Nature>>) -> Statistic {
|
||||||
ptr.as_ref().increased_stat()
|
ptr.as_ref().increased_stat()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The stat that should receive the decreased modifier.
|
/// The stat that should receive the decreased modifier.
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn nature_decreased_stat(ptr: ExternPointer<Arc<Nature>>) -> Statistic {
|
extern "C" fn nature_decreased_stat(ptr: ExternPointer<Arc<dyn Nature>>) -> Statistic {
|
||||||
ptr.as_ref().decreased_stat()
|
ptr.as_ref().decreased_stat()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
||||||
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
extern "C" fn nature_get_stat_modifier(ptr: ExternPointer<Arc<Nature>>, stat: Statistic) -> f32 {
|
extern "C" fn nature_get_stat_modifier(ptr: ExternPointer<Arc<dyn Nature>>, stat: Statistic) -> f32 {
|
||||||
ptr.as_ref().get_stat_modifier(stat)
|
ptr.as_ref().get_stat_modifier(stat)
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ pub struct NatureLibrary {
|
||||||
/// 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 data structure.
|
/// The underlying data structure.
|
||||||
map: HashMap<StringKey, Arc<Nature>>,
|
map: HashMap<StringKey, Arc<dyn Nature>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl NatureLibrary {
|
impl NatureLibrary {
|
||||||
|
@ -22,21 +22,21 @@ impl NatureLibrary {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new nature with name to the library.
|
/// Adds a new nature with name to the library.
|
||||||
pub fn load_nature(&mut self, name: StringKey, nature: Arc<Nature>) {
|
pub fn load_nature(&mut self, name: StringKey, nature: Arc<dyn Nature>) {
|
||||||
self.map.insert(name, nature);
|
self.map.insert(name, nature);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a nature by name.
|
/// Gets a nature by name.
|
||||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
|
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<dyn Nature>> {
|
||||||
self.map.get(key)
|
self.map.get(key)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Finds a nature name by nature.
|
/// Finds a nature name by nature.
|
||||||
pub fn get_nature_name(&self, nature: &Nature) -> StringKey {
|
pub fn get_nature_name(&self, nature: &Arc<dyn Nature>) -> StringKey {
|
||||||
for kv in &self.map {
|
for kv in &self.map {
|
||||||
// As natures can't be copied, and should always be the same reference as the value
|
// As natures can't be copied, and should always be the same reference as the value
|
||||||
// in the map, we just compare by reference.
|
// in the map, we just compare by reference.
|
||||||
if std::ptr::eq(Arc::as_ptr(kv.1), nature) {
|
if kv.1.value_identifier() == nature.value_identifier() {
|
||||||
return kv.0.clone();
|
return kv.0.clone();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,14 +53,14 @@ impl ValueIdentifiable for NatureLibrary {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use crate::static_data::statistics::Statistic;
|
use crate::static_data::statistics::Statistic;
|
||||||
use crate::static_data::{Nature, NatureLibrary};
|
use crate::static_data::{NatureImpl, NatureLibrary};
|
||||||
|
|
||||||
pub fn build() -> NatureLibrary {
|
pub fn build() -> NatureLibrary {
|
||||||
let mut lib = NatureLibrary::new(2);
|
let mut lib = NatureLibrary::new(2);
|
||||||
|
|
||||||
lib.load_nature(
|
lib.load_nature(
|
||||||
"test_nature".into(),
|
"test_nature".into(),
|
||||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||||
);
|
);
|
||||||
|
|
||||||
lib
|
lib
|
||||||
|
@ -69,10 +69,13 @@ pub mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn create_nature_library_insert_and_retrieve() {
|
fn create_nature_library_insert_and_retrieve() {
|
||||||
let mut lib = NatureLibrary::new(2);
|
let mut lib = NatureLibrary::new(2);
|
||||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
lib.load_nature(
|
||||||
|
"foo".into(),
|
||||||
|
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||||
|
);
|
||||||
lib.load_nature(
|
lib.load_nature(
|
||||||
"bar".into(),
|
"bar".into(),
|
||||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
NatureImpl::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||||
);
|
);
|
||||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||||
assert_eq!(n1.increased_stat(), Statistic::HP);
|
assert_eq!(n1.increased_stat(), Statistic::HP);
|
||||||
|
@ -84,10 +87,13 @@ pub mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn create_nature_library_insert_and_get_name() {
|
fn create_nature_library_insert_and_get_name() {
|
||||||
let mut lib = NatureLibrary::new(2);
|
let mut lib = NatureLibrary::new(2);
|
||||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
lib.load_nature(
|
||||||
|
"foo".into(),
|
||||||
|
NatureImpl::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||||
|
);
|
||||||
lib.load_nature(
|
lib.load_nature(
|
||||||
"bar".into(),
|
"bar".into(),
|
||||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
NatureImpl::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||||
);
|
);
|
||||||
|
|
||||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||||
|
|
|
@ -1,11 +1,26 @@
|
||||||
use crate::static_data::Statistic;
|
use crate::static_data::Statistic;
|
||||||
use crate::{ValueIdentifiable, ValueIdentifier};
|
use crate::{ValueIdentifiable, ValueIdentifier};
|
||||||
|
use std::fmt::Debug;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
||||||
|
/// can have an increased statistic and a decreased statistic, or be neutral.
|
||||||
|
pub trait Nature: ValueIdentifiable + Debug {
|
||||||
|
/// The stat that should receive the increased modifier.
|
||||||
|
fn increased_stat(&self) -> Statistic;
|
||||||
|
|
||||||
|
/// The stat that should receive the decreased modifier.
|
||||||
|
fn decreased_stat(&self) -> Statistic;
|
||||||
|
|
||||||
|
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
||||||
|
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
||||||
|
fn get_stat_modifier(&self, stat: Statistic) -> f32;
|
||||||
|
}
|
||||||
|
|
||||||
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
/// A nature is an attribute on a Pokemon that modifies the effective base stats on a Pokemon. They
|
||||||
/// can have an increased statistic and a decreased statistic, or be neutral.
|
/// can have an increased statistic and a decreased statistic, or be neutral.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Nature {
|
pub struct NatureImpl {
|
||||||
/// 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 stat that should receive the increased modifier.
|
/// The stat that should receive the increased modifier.
|
||||||
|
@ -18,7 +33,7 @@ pub struct Nature {
|
||||||
decrease_modifier: f32,
|
decrease_modifier: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Nature {
|
impl NatureImpl {
|
||||||
/// Instantiates a new statistic.
|
/// Instantiates a new statistic.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
increase_stat: Statistic,
|
increase_stat: Statistic,
|
||||||
|
@ -34,20 +49,22 @@ impl Nature {
|
||||||
decrease_modifier,
|
decrease_modifier,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Nature for NatureImpl {
|
||||||
/// The stat that should receive the increased modifier.
|
/// The stat that should receive the increased modifier.
|
||||||
pub fn increased_stat(&self) -> Statistic {
|
fn increased_stat(&self) -> Statistic {
|
||||||
self.increase_stat
|
self.increase_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The stat that should receive the decreased modifier.
|
/// The stat that should receive the decreased modifier.
|
||||||
pub fn decreased_stat(&self) -> Statistic {
|
fn decreased_stat(&self) -> Statistic {
|
||||||
self.decrease_stat
|
self.decrease_stat
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
/// Calculates the modifier for a given stat. If it's the increased stat, returns the increased
|
||||||
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
/// modifier, if it's the decreased stat, returns the decreased modifier. Otherwise returns 1.0
|
||||||
pub fn get_stat_modifier(&self, stat: Statistic) -> f32 {
|
fn get_stat_modifier(&self, stat: Statistic) -> f32 {
|
||||||
if stat == self.increase_stat && stat != self.decrease_stat {
|
if stat == self.increase_stat && stat != self.decrease_stat {
|
||||||
self.increase_modifier
|
self.increase_modifier
|
||||||
} else if stat == self.decrease_stat && stat != self.increase_stat {
|
} else if stat == self.decrease_stat && stat != self.increase_stat {
|
||||||
|
@ -58,7 +75,7 @@ impl Nature {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ValueIdentifiable for Nature {
|
impl ValueIdentifiable for NatureImpl {
|
||||||
fn value_identifier(&self) -> ValueIdentifier {
|
fn value_identifier(&self) -> ValueIdentifier {
|
||||||
self.identifier
|
self.identifier
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ 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,
|
Ability, AbilityLibrary, BattleItemCategory, DataLibrary, EffectParameter, Form, GrowthRateLibrary, ItemImpl,
|
||||||
ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveData, MoveLibrary, Nature, NatureLibrary,
|
ItemLibrary, LearnableMoves, LibrarySettings, LookupGrowthRate, MoveData, MoveLibrary, NatureImpl, NatureLibrary,
|
||||||
SecondaryEffect, Species, StaticData, StaticStatisticSet, Statistic, TypeLibrary,
|
SecondaryEffect, Species, StaticData, StaticStatisticSet, Statistic, TypeLibrary,
|
||||||
};
|
};
|
||||||
use pkmn_lib::StringKey;
|
use pkmn_lib::StringKey;
|
||||||
|
@ -80,13 +80,13 @@ pub fn load_natures(path: &String, nature_library: &mut NatureLibrary) {
|
||||||
let increased_statistic_str = record.get(1).unwrap();
|
let increased_statistic_str = record.get(1).unwrap();
|
||||||
let decreased_statistic_str = record.get(2).unwrap();
|
let decreased_statistic_str = record.get(2).unwrap();
|
||||||
if increased_statistic_str.is_empty() || decreased_statistic_str.is_empty() {
|
if increased_statistic_str.is_empty() || decreased_statistic_str.is_empty() {
|
||||||
nature_library.load_nature(nature_name, Nature::new(Statistic::HP, Statistic::HP, 1.0, 1.0));
|
nature_library.load_nature(nature_name, NatureImpl::new(Statistic::HP, Statistic::HP, 1.0, 1.0));
|
||||||
} else {
|
} else {
|
||||||
let increased_statistic = serde_plain::from_str(increased_statistic_str).unwrap();
|
let increased_statistic = serde_plain::from_str(increased_statistic_str).unwrap();
|
||||||
let decreased_statistic = serde_plain::from_str(decreased_statistic_str).unwrap();
|
let decreased_statistic = serde_plain::from_str(decreased_statistic_str).unwrap();
|
||||||
nature_library.load_nature(
|
nature_library.load_nature(
|
||||||
nature_name,
|
nature_name,
|
||||||
Nature::new(increased_statistic, decreased_statistic, 1.1, 0.9),
|
NatureImpl::new(increased_statistic, decreased_statistic, 1.1, 0.9),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue