Fixes for FFI.
This commit is contained in:
@@ -33,6 +33,11 @@ pub trait DataLibrary<'a, T: 'a> {
|
||||
self.map().get::<u32>(&key)
|
||||
}
|
||||
|
||||
/// Gets a value from the library by the index where it is stored.
|
||||
fn get_key_by_index(&'a self, index: usize) -> Option<&StringKey> {
|
||||
self.map().get_index(index).map(|a| a.0)
|
||||
}
|
||||
|
||||
/// Gets the amount of values in the library.
|
||||
fn len(&self) -> usize {
|
||||
self.map().len()
|
||||
|
||||
@@ -11,6 +11,8 @@ pub use library_settings::LibrarySettings;
|
||||
#[doc(inline)]
|
||||
pub use move_library::MoveLibrary;
|
||||
#[doc(inline)]
|
||||
pub use nature_library::*;
|
||||
#[doc(inline)]
|
||||
pub use species_library::SpeciesLibrary;
|
||||
#[doc(inline)]
|
||||
pub use static_data::StaticData;
|
||||
@@ -29,6 +31,8 @@ mod item_library;
|
||||
mod library_settings;
|
||||
/// The library data for moves.
|
||||
mod move_library;
|
||||
/// The library data for natures.
|
||||
mod nature_library;
|
||||
/// The library data for species.
|
||||
mod species_library;
|
||||
/// The combination of all libraries.
|
||||
|
||||
91
src/static_data/libraries/nature_library.rs
Normal file
91
src/static_data/libraries/nature_library.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use crate::static_data::Nature;
|
||||
use crate::StringKey;
|
||||
use hashbrown::HashMap;
|
||||
use std::sync::Arc;
|
||||
|
||||
/// A library of all natures that can be used, stored by their names.
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibrary {
|
||||
/// The underlying data structure.
|
||||
map: HashMap<StringKey, Arc<Nature>>,
|
||||
}
|
||||
|
||||
impl NatureLibrary {
|
||||
/// Creates a new nature library with a given capacity.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
NatureLibrary {
|
||||
map: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new nature with name to the library.
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
|
||||
self.map.insert(name, Arc::new(nature));
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
|
||||
self.map.get(key)
|
||||
}
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
|
||||
for kv in &self.map {
|
||||
// As natures can't be copied, and should always be the same reference as the value
|
||||
// in the map, we just compare by reference.
|
||||
if Arc::ptr_eq(kv.1, nature) {
|
||||
return kv.0.clone();
|
||||
}
|
||||
}
|
||||
panic!("No name was found for the given nature. This should never happen.");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::statistics::Statistic;
|
||||
use crate::static_data::{Nature, NatureLibrary};
|
||||
|
||||
pub fn build() -> NatureLibrary {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
|
||||
lib.load_nature(
|
||||
"test_nature".into(),
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
|
||||
lib
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_retrieve() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
assert_eq!(n1.increased_stat(), Statistic::HP);
|
||||
assert_eq!(n1.decreased_stat(), Statistic::Attack);
|
||||
assert_eq!(n1.get_stat_modifier(n1.increased_stat()), 1.1);
|
||||
assert_eq!(n1.get_stat_modifier(n1.decreased_stat()), 0.9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_get_name() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
let name = lib.get_nature_name(n1);
|
||||
assert_eq!(name, "foo".into());
|
||||
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
|
||||
let name2 = lib.get_nature_name(n2);
|
||||
assert_eq!(name2, "bar".into());
|
||||
}
|
||||
}
|
||||
@@ -111,9 +111,8 @@ pub mod test {
|
||||
use crate::static_data::libraries::library_settings::LibrarySettings;
|
||||
use crate::static_data::libraries::static_data::StaticData;
|
||||
use crate::static_data::libraries::{
|
||||
ability_library, growth_rate_library, item_library, move_library, species_library, type_library,
|
||||
ability_library, growth_rate_library, item_library, move_library, nature_library, species_library, type_library,
|
||||
};
|
||||
use crate::static_data::natures;
|
||||
|
||||
pub fn build() -> StaticData {
|
||||
StaticData {
|
||||
@@ -123,7 +122,7 @@ pub mod test {
|
||||
items: item_library::tests::build(),
|
||||
growth_rates: growth_rate_library::tests::build(),
|
||||
types: type_library::tests::build(),
|
||||
natures: natures::tests::build(),
|
||||
natures: nature_library::tests::build(),
|
||||
abilities: ability_library::tests::build(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,13 +44,23 @@ impl TypeLibrary {
|
||||
}
|
||||
|
||||
/// Gets the type identifier for a type with a name.
|
||||
pub fn get_type_id(&self, key: &StringKey) -> TypeIdentifier {
|
||||
self.types[key]
|
||||
pub fn get_type_id(&self, key: &StringKey) -> Option<TypeIdentifier> {
|
||||
self.types.get(key).cloned()
|
||||
}
|
||||
|
||||
/// Gets the type name from the type identifier.
|
||||
pub fn get_type_name(&self, t: TypeIdentifier) -> Option<StringKey> {
|
||||
for kv in &self.types {
|
||||
if *kv.1 == t {
|
||||
return Some(kv.0.clone());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Gets the effectiveness for a single attacking type against a single defending type.
|
||||
pub fn get_single_effectiveness(&self, attacking: TypeIdentifier, defending: TypeIdentifier) -> f32 {
|
||||
self.effectiveness[attacking.val as usize][defending.val as usize]
|
||||
self.effectiveness[(attacking.val - 1) as usize][(defending.val - 1) as usize]
|
||||
}
|
||||
|
||||
/// Gets the effectiveness for a single attacking type against an amount of defending types.
|
||||
@@ -67,19 +77,19 @@ impl TypeLibrary {
|
||||
/// Registers a new type in the library.
|
||||
pub fn register_type(&mut self, name: &StringKey) -> TypeIdentifier {
|
||||
let id = TypeIdentifier {
|
||||
val: self.types.len() as u8,
|
||||
val: (self.types.len() + 1) as u8,
|
||||
};
|
||||
self.types.insert(name.clone(), id);
|
||||
self.effectiveness.resize((id.val + 1) as usize, vec![]);
|
||||
self.effectiveness.resize((id.val) as usize, vec![]);
|
||||
for effectiveness in &mut self.effectiveness {
|
||||
effectiveness.resize((id.val + 1) as usize, 1.0)
|
||||
effectiveness.resize((id.val) as usize, 1.0)
|
||||
}
|
||||
id
|
||||
}
|
||||
|
||||
/// Sets the effectiveness for an attacking type against a defending type.
|
||||
pub fn set_effectiveness(&mut self, attacking: TypeIdentifier, defending: TypeIdentifier, effectiveness: f32) {
|
||||
self.effectiveness[attacking.val as usize][defending.val as usize] = effectiveness;
|
||||
self.effectiveness[(attacking.val - 1) as usize][(defending.val - 1) as usize] = effectiveness;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,8 +126,8 @@ pub mod tests {
|
||||
|
||||
// Borrow as read so we can read
|
||||
let r = &lib;
|
||||
assert_eq!(r.get_type_id(&"foo".into()), t0);
|
||||
assert_eq!(r.get_type_id(&"bar".into()), t1);
|
||||
assert_eq!(r.get_type_id(&"foo".into()).unwrap(), t0);
|
||||
assert_eq!(r.get_type_id(&"bar".into()).unwrap(), t1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use hashbrown::HashMap;
|
||||
|
||||
use crate::static_data::Statistic;
|
||||
use crate::StringKey;
|
||||
|
||||
/// 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.
|
||||
@@ -57,90 +52,3 @@ impl Nature {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A library of all natures that can be used, stored by their names.
|
||||
#[derive(Debug)]
|
||||
pub struct NatureLibrary {
|
||||
/// The underlying data structure.
|
||||
map: HashMap<StringKey, Arc<Nature>>,
|
||||
}
|
||||
|
||||
impl NatureLibrary {
|
||||
/// Creates a new nature library with a given capacity.
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
NatureLibrary {
|
||||
map: HashMap::with_capacity(capacity),
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a new nature with name to the library.
|
||||
pub fn load_nature(&mut self, name: StringKey, nature: Nature) {
|
||||
self.map.insert(name, Arc::new(nature));
|
||||
}
|
||||
|
||||
/// Gets a nature by name.
|
||||
pub fn get_nature(&self, key: &StringKey) -> Option<&Arc<Nature>> {
|
||||
self.map.get(key)
|
||||
}
|
||||
|
||||
/// Finds a nature name by nature.
|
||||
pub fn get_nature_name(&self, nature: &Arc<Nature>) -> StringKey {
|
||||
for kv in &self.map {
|
||||
// As natures can't be copied, and should always be the same reference as the value
|
||||
// in the map, we just compare by reference.
|
||||
if Arc::ptr_eq(kv.1, nature) {
|
||||
return kv.0.clone();
|
||||
}
|
||||
}
|
||||
panic!("No name was found for the given nature. This should never happen.");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub mod tests {
|
||||
use crate::static_data::natures::{Nature, NatureLibrary};
|
||||
use crate::static_data::statistics::Statistic;
|
||||
|
||||
pub fn build() -> NatureLibrary {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
|
||||
lib.load_nature(
|
||||
"test_nature".into(),
|
||||
Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9),
|
||||
);
|
||||
|
||||
lib
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_retrieve() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
assert_eq!(n1.increase_stat, Statistic::HP);
|
||||
assert_eq!(n1.decrease_stat, Statistic::Attack);
|
||||
assert_eq!(n1.increase_modifier, 1.1);
|
||||
assert_eq!(n1.decrease_modifier, 0.9);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_nature_library_insert_and_get_name() {
|
||||
let mut lib = NatureLibrary::new(2);
|
||||
lib.load_nature("foo".into(), Nature::new(Statistic::HP, Statistic::Attack, 1.1, 0.9));
|
||||
lib.load_nature(
|
||||
"bar".into(),
|
||||
Nature::new(Statistic::Attack, Statistic::Defense, 1.1, 0.9),
|
||||
);
|
||||
|
||||
let n1 = lib.get_nature(&"foo".into()).expect("Nature was not found");
|
||||
let name = lib.get_nature_name(n1);
|
||||
assert_eq!(name, "foo".into());
|
||||
let n2 = lib.get_nature(&"bar".into()).expect("Nature was not found");
|
||||
let name2 = lib.get_nature_name(n2);
|
||||
assert_eq!(name2, "bar".into());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user