diff --git a/src/ffi/static_data/libraries/growth_rate_library.rs b/src/ffi/static_data/libraries/growth_rate_library.rs new file mode 100644 index 0000000..8a57eda --- /dev/null +++ b/src/ffi/static_data/libraries/growth_rate_library.rs @@ -0,0 +1,45 @@ +use crate::defines::LevelInt; +use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr}; +use crate::static_data::{GrowthRate, GrowthRateLibrary}; +use std::ffi::{c_char, CStr}; +use std::ptr::drop_in_place; + +#[no_mangle] +extern "C" fn growth_rate_library_new(capacity: usize) -> OwnedPtr { + Box::into_raw(Box::new(GrowthRateLibrary::new(capacity))) +} + +#[no_mangle] +unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr) { + drop_in_place(ptr) +} + +#[no_mangle] +unsafe extern "C" fn growth_rate_library_calculate_level( + ptr: ExternPointer, + growth_rate: BorrowedPtr, + experience: u32, +) -> LevelInt { + ptr.as_ref() + .calculate_level(&CStr::from_ptr(growth_rate).into(), experience) +} + +#[no_mangle] +unsafe extern "C" fn growth_rate_library_calculate_experience( + ptr: ExternPointer, + growth_rate: BorrowedPtr, + level: LevelInt, +) -> u32 { + ptr.as_ref() + .calculate_experience(&CStr::from_ptr(growth_rate).into(), level) +} + +#[no_mangle] +unsafe extern "C" fn growth_rate_library_add_growth_rate( + ptr: ExternPointer, + name: BorrowedPtr, + growth_rate: OwnedPtr>, +) { + ptr.as_mut() + .add_growth_rate(&CStr::from_ptr(name).into(), *Box::from_raw(growth_rate)); +} diff --git a/src/ffi/static_data/libraries/library_settings.rs b/src/ffi/static_data/libraries/library_settings.rs new file mode 100644 index 0000000..5fcd57e --- /dev/null +++ b/src/ffi/static_data/libraries/library_settings.rs @@ -0,0 +1,19 @@ +use crate::defines::LevelInt; +use crate::ffi::{ExternPointer, OwnedPtr}; +use crate::static_data::LibrarySettings; +use std::ptr::drop_in_place; + +#[no_mangle] +extern "C" fn library_settings_new(max_level: LevelInt) -> OwnedPtr { + Box::into_raw(Box::new(LibrarySettings::new(max_level))) +} + +#[no_mangle] +unsafe extern "C" fn library_settings_drop(ptr: OwnedPtr) { + drop_in_place(ptr) +} + +#[no_mangle] +extern "C" fn library_settings_maximum_level(ptr: ExternPointer) -> LevelInt { + ptr.as_ref().maximum_level() +} diff --git a/src/ffi/static_data/libraries/mod.rs b/src/ffi/static_data/libraries/mod.rs new file mode 100644 index 0000000..94b1fe0 --- /dev/null +++ b/src/ffi/static_data/libraries/mod.rs @@ -0,0 +1,72 @@ +mod growth_rate_library; +mod library_settings; +mod nature_library; +mod static_data; +mod type_library; + +use crate::ffi::{BorrowedPtr, OwnedPtr}; +use crate::static_data::*; +use std::ffi::{c_char, CStr}; +use std::ptr::drop_in_place; +use std::sync::Arc; + +macro_rules! library_interface { + ($library_type:ty, $return_type:ty) => { + paste::paste! { + #[no_mangle] + extern "C" fn [< $library_type:snake _new >](capacity: usize) -> OwnedPtr<$library_type> { + Box::into_raw(Box::new($library_type::new(capacity))) + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _drop >](ptr: OwnedPtr<$library_type>) { + drop_in_place(ptr); + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _add >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr, value: OwnedPtr<$return_type>) { + let lib = ptr.as_mut().unwrap(); + lib.add(&CStr::from_ptr(key).into(), *Box::from_raw(value)); + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _remove >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr) { + let lib = ptr.as_mut().unwrap(); + lib.remove(&CStr::from_ptr(key).into()); + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _get >](ptr: OwnedPtr<$library_type>, key: BorrowedPtr) -> BorrowedPtr<$return_type> { + let lib = ptr.as_mut().unwrap(); + let v = lib.get(&CStr::from_ptr(key).into()); + if let Some(value) = v { + Arc::as_ptr(value) + } else { + std::ptr::null() + } + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _get_key_by_index >](ptr: OwnedPtr<$library_type>, index: usize) -> OwnedPtr { + let lib = ptr.as_mut().unwrap(); + let v = lib.get_key_by_index(index); + if let Some(value) = v { + std::ffi::CString::new(value.str()).unwrap().into_raw() + } else { + std::ptr::null_mut() + } + } + + #[no_mangle] + unsafe extern "C" fn [< $library_type:snake _len >](ptr: OwnedPtr<$library_type>) -> usize { + let lib = ptr.as_mut().unwrap(); + lib.len() + } + } + }; +} + +library_interface!(SpeciesLibrary, Species); +library_interface!(MoveLibrary, MoveData); +library_interface!(AbilityLibrary, Ability); +library_interface!(ItemLibrary, Item); diff --git a/src/ffi/static_data/libraries/nature_library.rs b/src/ffi/static_data/libraries/nature_library.rs new file mode 100644 index 0000000..d1ae012 --- /dev/null +++ b/src/ffi/static_data/libraries/nature_library.rs @@ -0,0 +1,48 @@ +use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr}; +use crate::static_data::{Nature, NatureLibrary}; +use std::ffi::{c_char, CStr, CString}; +use std::ptr::drop_in_place; +use std::sync::Arc; + +#[no_mangle] +extern "C" fn nature_library_new(capacity: usize) -> OwnedPtr { + Box::into_raw(Box::new(NatureLibrary::new(capacity))) +} + +#[no_mangle] +unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr) { + drop_in_place(ptr); +} + +#[no_mangle] +unsafe extern "C" fn nature_library_load_nature( + ptr: ExternPointer, + name: BorrowedPtr, + nature: OwnedPtr, +) { + ptr.as_mut() + .load_nature(CStr::from_ptr(name).into(), *Box::from_raw(nature)) +} + +#[no_mangle] +unsafe extern "C" fn nature_library_get_nature( + ptr: ExternPointer, + name: BorrowedPtr, +) -> BorrowedPtr { + if let Some(nature) = ptr.as_ref().get_nature(&CStr::from_ptr(name).into()) { + Arc::into_raw(nature.clone()) + } else { + std::ptr::null() + } +} + +#[no_mangle] +unsafe extern "C" fn nature_library_get_nature_name( + ptr: ExternPointer, + nature: BorrowedPtr, +) -> OwnedPtr { + let arc = Arc::from_raw(nature); + CString::new(ptr.as_ref().get_nature_name(&arc).str()) + .unwrap() + .into_raw() +} diff --git a/src/ffi/static_data/libraries/static_data.rs b/src/ffi/static_data/libraries/static_data.rs new file mode 100644 index 0000000..ecfefd0 --- /dev/null +++ b/src/ffi/static_data/libraries/static_data.rs @@ -0,0 +1,56 @@ +use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr}; +use crate::static_data::{ + AbilityLibrary, GrowthRateLibrary, ItemLibrary, LibrarySettings, MoveLibrary, NatureLibrary, SpeciesLibrary, + StaticData, TypeLibrary, +}; +use std::ptr::drop_in_place; + +#[no_mangle] +unsafe extern "C" fn static_data_new(settings: OwnedPtr) -> OwnedPtr { + Box::into_raw(Box::new(StaticData::new(*Box::from_raw(settings)))) +} + +#[no_mangle] +unsafe extern "C" fn static_data_drop(ptr: OwnedPtr) { + drop_in_place(ptr) +} + +#[no_mangle] +unsafe extern "C" fn static_data_settings(data: ExternPointer) -> BorrowedPtr { + data.as_mut().settings() as *const LibrarySettings +} + +#[no_mangle] +unsafe extern "C" fn static_data_species(data: ExternPointer) -> BorrowedPtr { + data.as_mut().species_mut() as *mut SpeciesLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_moves(data: ExternPointer) -> BorrowedPtr { + data.as_mut().moves_mut() as *mut MoveLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_items(data: ExternPointer) -> BorrowedPtr { + data.as_mut().items_mut() as *mut ItemLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_growth_rates(data: ExternPointer) -> BorrowedPtr { + data.as_mut().growth_rates_mut() as *mut GrowthRateLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_types(data: ExternPointer) -> BorrowedPtr { + data.as_mut().types_mut() as *mut TypeLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_natures(data: ExternPointer) -> BorrowedPtr { + data.as_mut().natures_mut() as *mut NatureLibrary +} + +#[no_mangle] +unsafe extern "C" fn static_data_abilities(data: ExternPointer) -> BorrowedPtr { + data.as_mut().abilities_mut() as *mut AbilityLibrary +} diff --git a/src/ffi/static_data/libraries/type_library.rs b/src/ffi/static_data/libraries/type_library.rs new file mode 100644 index 0000000..9d09b87 --- /dev/null +++ b/src/ffi/static_data/libraries/type_library.rs @@ -0,0 +1,82 @@ +use crate::ffi::{BorrowedPtr, ExternPointer, OwnedPtr}; +use crate::static_data::{TypeIdentifier, TypeLibrary}; +use std::ffi::{c_char, CStr, CString}; +use std::ptr::drop_in_place; + +#[no_mangle] +extern "C" fn type_library_new(capacity: usize) -> OwnedPtr { + Box::into_raw(Box::new(TypeLibrary::new(capacity))) +} + +#[no_mangle] +unsafe extern "C" fn type_library_drop(ptr: OwnedPtr) { + drop_in_place(ptr); +} + +#[no_mangle] +unsafe extern "C" fn type_library_get_type_id( + ptr: ExternPointer, + key: BorrowedPtr, + found: *mut bool, +) -> TypeIdentifier { + if let Some(v) = ptr.as_ref().get_type_id(&CStr::from_ptr(key).into()) { + *found = true; + v + } else { + *found = false; + TypeIdentifier::default() + } +} + +#[no_mangle] +unsafe extern "C" fn type_library_get_type_name( + ptr: ExternPointer, + type_id: TypeIdentifier, + found: *mut bool, +) -> *mut c_char { + if let Some(v) = ptr.as_ref().get_type_name(type_id) { + *found = true; + CString::new(v.str()).unwrap().into_raw() + } else { + *found = false; + std::ptr::null_mut() + } +} + +#[no_mangle] +extern "C" fn type_library_get_single_effectiveness( + ptr: ExternPointer, + attacking: TypeIdentifier, + defending: TypeIdentifier, +) -> f32 { + ptr.as_ref().get_single_effectiveness(attacking, defending) +} + +#[no_mangle] +unsafe extern "C" fn type_library_get_effectiveness( + ptr: ExternPointer, + attacking: TypeIdentifier, + defending: OwnedPtr, + defending_length: usize, +) -> f32 { + let v = std::slice::from_raw_parts(defending, defending_length); + ptr.as_ref().get_effectiveness(attacking, v) +} + +#[no_mangle] +unsafe extern "C" fn type_library_register_type( + ptr: ExternPointer, + name: BorrowedPtr, +) -> TypeIdentifier { + ptr.as_mut().register_type(&CStr::from_ptr(name).into()) +} + +#[no_mangle] +unsafe extern "C" fn type_library_set_effectiveness( + ptr: ExternPointer, + attacking: TypeIdentifier, + defending: TypeIdentifier, + effectiveness: f32, +) { + ptr.as_mut().set_effectiveness(attacking, defending, effectiveness); +} diff --git a/src/ffi/static_data/mod.rs b/src/ffi/static_data/mod.rs index 8e0cd01..607096d 100644 --- a/src/ffi/static_data/mod.rs +++ b/src/ffi/static_data/mod.rs @@ -12,6 +12,7 @@ mod move_data; mod nature; mod species; mod statistic_set; +mod libraries; #[no_mangle] extern "C" fn effect_parameter_new_bool(value: u8) -> OwnedPtr { diff --git a/src/lib.rs b/src/lib.rs index e121ab3..5428696 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,6 @@ #![deny(missing_docs)] #![deny(clippy::missing_docs_in_private_items)] #![feature(test)] -#![feature(bench_black_box)] #![feature(once_cell)] #![feature(const_option)] #![feature(is_some_with)] diff --git a/src/static_data/libraries/data_library.rs b/src/static_data/libraries/data_library.rs index dbc891e..9aab330 100755 --- a/src/static_data/libraries/data_library.rs +++ b/src/static_data/libraries/data_library.rs @@ -33,6 +33,11 @@ pub trait DataLibrary<'a, T: 'a> { self.map().get::(&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() diff --git a/src/static_data/libraries/mod.rs b/src/static_data/libraries/mod.rs index 6bfec6f..8bf6976 100755 --- a/src/static_data/libraries/mod.rs +++ b/src/static_data/libraries/mod.rs @@ -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. diff --git a/src/static_data/libraries/nature_library.rs b/src/static_data/libraries/nature_library.rs new file mode 100644 index 0000000..9244eee --- /dev/null +++ b/src/static_data/libraries/nature_library.rs @@ -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>, +} + +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> { + self.map.get(key) + } + + /// Finds a nature name by nature. + pub fn get_nature_name(&self, nature: &Arc) -> 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()); + } +} diff --git a/src/static_data/libraries/static_data.rs b/src/static_data/libraries/static_data.rs index e2a2af1..9259008 100755 --- a/src/static_data/libraries/static_data.rs +++ b/src/static_data/libraries/static_data.rs @@ -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(), } } diff --git a/src/static_data/libraries/type_library.rs b/src/static_data/libraries/type_library.rs index c5613f6..b0d4af8 100755 --- a/src/static_data/libraries/type_library.rs +++ b/src/static_data/libraries/type_library.rs @@ -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 { + self.types.get(key).cloned() + } + + /// Gets the type name from the type identifier. + pub fn get_type_name(&self, t: TypeIdentifier) -> Option { + 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] diff --git a/src/static_data/natures.rs b/src/static_data/natures.rs index 98a7d13..ed31573 100755 --- a/src/static_data/natures.rs +++ b/src/static_data/natures.rs @@ -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>, -} - -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> { - self.map.get(key) - } - - /// Finds a nature name by nature. - pub fn get_nature_name(&self, nature: &Arc) -> 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()); - } -} diff --git a/tests/common/library_loader.rs b/tests/common/library_loader.rs index a4eee17..b4de2c6 100755 --- a/tests/common/library_loader.rs +++ b/tests/common/library_loader.rs @@ -58,11 +58,13 @@ pub fn load_types(path: &String, type_library: &mut TypeLibrary) { for record in reader.records() { let record = record.unwrap(); let offensive_type = record.get(0).unwrap(); - let offensive_type_id = type_library.get_type_id(&StringKey::new(offensive_type.into())); + let offensive_type_id = type_library + .get_type_id(&StringKey::new(offensive_type.into())) + .unwrap(); for (i, v) in record.iter().skip(1).enumerate() { let effectiveness = v.parse::().unwrap(); - type_library.set_effectiveness(offensive_type_id, (i as u8).into(), effectiveness); + type_library.set_effectiveness(offensive_type_id, ((i + 1) as u8).into(), effectiveness); } } } @@ -171,7 +173,7 @@ pub fn load_moves(path: &String, lib: &mut StaticData) { let move_data = move_data.as_object().unwrap(); let move_name = StringKey::new(move_data.get("name").unwrap().as_str().unwrap().into()); let move_type = StringKey::new(move_data.get("type").unwrap().as_str().unwrap().into()); - let move_type_id = lib.types().get_type_id(&move_type); + let move_type_id = lib.types().get_type_id(&move_type).unwrap(); let move_category = serde_json::from_value(move_data.get("category").unwrap().clone()).unwrap(); let base_power = move_data.get("power").unwrap().as_i64().unwrap() as u8; let accuracy = move_data.get("accuracy").unwrap().as_i64().unwrap() as u8; @@ -300,7 +302,12 @@ fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Form .as_array() .unwrap() .iter() - .map(|a| library.types().get_type_id(&StringKey::new(a.as_str().unwrap().into()))) + .map(|a| { + library + .types() + .get_type_id(&StringKey::new(a.as_str().unwrap().into())) + .unwrap() + }) .collect(); let moves = parse_moves(&value.get("moves").unwrap(), library.moves()); @@ -410,8 +417,8 @@ fn test_type_library_loaded() { assert_eq!( lib.get_effectiveness( - lib.get_type_id(&StringKey::new("fire".into())), - &[lib.get_type_id(&StringKey::new("grass".into()))], + lib.get_type_id(&StringKey::new("fire".into())).unwrap(), + &[lib.get_type_id(&StringKey::new("grass".into())).unwrap()], ), 2.0 );