Fixes for FFI.
continuous-integration/drone/push Build is failing Details
continuous-integration/drone Build is passing Details

This commit is contained in:
Deukhoofd 2022-10-01 15:40:15 +02:00
parent 78fde698ca
commit 84ddf0307d
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
15 changed files with 457 additions and 111 deletions

View File

@ -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<GrowthRateLibrary> {
Box::into_raw(Box::new(GrowthRateLibrary::new(capacity)))
}
#[no_mangle]
unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr<GrowthRateLibrary>) {
drop_in_place(ptr)
}
#[no_mangle]
unsafe extern "C" fn growth_rate_library_calculate_level(
ptr: ExternPointer<GrowthRateLibrary>,
growth_rate: BorrowedPtr<c_char>,
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<GrowthRateLibrary>,
growth_rate: BorrowedPtr<c_char>,
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<GrowthRateLibrary>,
name: BorrowedPtr<c_char>,
growth_rate: OwnedPtr<Box<dyn GrowthRate>>,
) {
ptr.as_mut()
.add_growth_rate(&CStr::from_ptr(name).into(), *Box::from_raw(growth_rate));
}

View File

@ -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<LibrarySettings> {
Box::into_raw(Box::new(LibrarySettings::new(max_level)))
}
#[no_mangle]
unsafe extern "C" fn library_settings_drop(ptr: OwnedPtr<LibrarySettings>) {
drop_in_place(ptr)
}
#[no_mangle]
extern "C" fn library_settings_maximum_level(ptr: ExternPointer<LibrarySettings>) -> LevelInt {
ptr.as_ref().maximum_level()
}

View File

@ -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<c_char>, 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<c_char>) {
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<c_char>) -> 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<c_char> {
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);

View File

@ -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<NatureLibrary> {
Box::into_raw(Box::new(NatureLibrary::new(capacity)))
}
#[no_mangle]
unsafe extern "C" fn nature_library_drop(ptr: OwnedPtr<NatureLibrary>) {
drop_in_place(ptr);
}
#[no_mangle]
unsafe extern "C" fn nature_library_load_nature(
ptr: ExternPointer<NatureLibrary>,
name: BorrowedPtr<c_char>,
nature: OwnedPtr<Nature>,
) {
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<NatureLibrary>,
name: BorrowedPtr<c_char>,
) -> BorrowedPtr<Nature> {
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<NatureLibrary>,
nature: BorrowedPtr<Nature>,
) -> OwnedPtr<c_char> {
let arc = Arc::from_raw(nature);
CString::new(ptr.as_ref().get_nature_name(&arc).str())
.unwrap()
.into_raw()
}

View File

@ -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<LibrarySettings>) -> OwnedPtr<StaticData> {
Box::into_raw(Box::new(StaticData::new(*Box::from_raw(settings))))
}
#[no_mangle]
unsafe extern "C" fn static_data_drop(ptr: OwnedPtr<StaticData>) {
drop_in_place(ptr)
}
#[no_mangle]
unsafe extern "C" fn static_data_settings(data: ExternPointer<StaticData>) -> BorrowedPtr<LibrarySettings> {
data.as_mut().settings() as *const LibrarySettings
}
#[no_mangle]
unsafe extern "C" fn static_data_species(data: ExternPointer<StaticData>) -> BorrowedPtr<SpeciesLibrary> {
data.as_mut().species_mut() as *mut SpeciesLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_moves(data: ExternPointer<StaticData>) -> BorrowedPtr<MoveLibrary> {
data.as_mut().moves_mut() as *mut MoveLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_items(data: ExternPointer<StaticData>) -> BorrowedPtr<ItemLibrary> {
data.as_mut().items_mut() as *mut ItemLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_growth_rates(data: ExternPointer<StaticData>) -> BorrowedPtr<GrowthRateLibrary> {
data.as_mut().growth_rates_mut() as *mut GrowthRateLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_types(data: ExternPointer<StaticData>) -> BorrowedPtr<TypeLibrary> {
data.as_mut().types_mut() as *mut TypeLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_natures(data: ExternPointer<StaticData>) -> BorrowedPtr<NatureLibrary> {
data.as_mut().natures_mut() as *mut NatureLibrary
}
#[no_mangle]
unsafe extern "C" fn static_data_abilities(data: ExternPointer<StaticData>) -> BorrowedPtr<AbilityLibrary> {
data.as_mut().abilities_mut() as *mut AbilityLibrary
}

View File

@ -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<TypeLibrary> {
Box::into_raw(Box::new(TypeLibrary::new(capacity)))
}
#[no_mangle]
unsafe extern "C" fn type_library_drop(ptr: OwnedPtr<TypeLibrary>) {
drop_in_place(ptr);
}
#[no_mangle]
unsafe extern "C" fn type_library_get_type_id(
ptr: ExternPointer<TypeLibrary>,
key: BorrowedPtr<c_char>,
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<TypeLibrary>,
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<TypeLibrary>,
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<TypeLibrary>,
attacking: TypeIdentifier,
defending: OwnedPtr<TypeIdentifier>,
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<TypeLibrary>,
name: BorrowedPtr<c_char>,
) -> TypeIdentifier {
ptr.as_mut().register_type(&CStr::from_ptr(name).into())
}
#[no_mangle]
unsafe extern "C" fn type_library_set_effectiveness(
ptr: ExternPointer<TypeLibrary>,
attacking: TypeIdentifier,
defending: TypeIdentifier,
effectiveness: f32,
) {
ptr.as_mut().set_effectiveness(attacking, defending, effectiveness);
}

View File

@ -12,6 +12,7 @@ mod move_data;
mod nature; mod nature;
mod species; mod species;
mod statistic_set; mod statistic_set;
mod libraries;
#[no_mangle] #[no_mangle]
extern "C" fn effect_parameter_new_bool(value: u8) -> OwnedPtr<EffectParameter> { extern "C" fn effect_parameter_new_bool(value: u8) -> OwnedPtr<EffectParameter> {

View File

@ -4,7 +4,6 @@
#![deny(missing_docs)] #![deny(missing_docs)]
#![deny(clippy::missing_docs_in_private_items)] #![deny(clippy::missing_docs_in_private_items)]
#![feature(test)] #![feature(test)]
#![feature(bench_black_box)]
#![feature(once_cell)] #![feature(once_cell)]
#![feature(const_option)] #![feature(const_option)]
#![feature(is_some_with)] #![feature(is_some_with)]

View File

@ -33,6 +33,11 @@ pub trait DataLibrary<'a, T: 'a> {
self.map().get::<u32>(&key) 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. /// Gets the amount of values in the library.
fn len(&self) -> usize { fn len(&self) -> usize {
self.map().len() self.map().len()

View File

@ -11,6 +11,8 @@ pub use library_settings::LibrarySettings;
#[doc(inline)] #[doc(inline)]
pub use move_library::MoveLibrary; pub use move_library::MoveLibrary;
#[doc(inline)] #[doc(inline)]
pub use nature_library::*;
#[doc(inline)]
pub use species_library::SpeciesLibrary; pub use species_library::SpeciesLibrary;
#[doc(inline)] #[doc(inline)]
pub use static_data::StaticData; pub use static_data::StaticData;
@ -29,6 +31,8 @@ mod item_library;
mod library_settings; mod library_settings;
/// The library data for moves. /// The library data for moves.
mod move_library; mod move_library;
/// The library data for natures.
mod nature_library;
/// The library data for species. /// The library data for species.
mod species_library; mod species_library;
/// The combination of all libraries. /// The combination of all libraries.

View 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());
}
}

View File

@ -111,9 +111,8 @@ pub mod test {
use crate::static_data::libraries::library_settings::LibrarySettings; use crate::static_data::libraries::library_settings::LibrarySettings;
use crate::static_data::libraries::static_data::StaticData; use crate::static_data::libraries::static_data::StaticData;
use crate::static_data::libraries::{ 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 { pub fn build() -> StaticData {
StaticData { StaticData {
@ -123,7 +122,7 @@ pub mod test {
items: item_library::tests::build(), items: item_library::tests::build(),
growth_rates: growth_rate_library::tests::build(), growth_rates: growth_rate_library::tests::build(),
types: type_library::tests::build(), types: type_library::tests::build(),
natures: natures::tests::build(), natures: nature_library::tests::build(),
abilities: ability_library::tests::build(), abilities: ability_library::tests::build(),
} }
} }

View File

@ -44,13 +44,23 @@ impl TypeLibrary {
} }
/// Gets the type identifier for a type with a name. /// Gets the type identifier for a type with a name.
pub fn get_type_id(&self, key: &StringKey) -> TypeIdentifier { pub fn get_type_id(&self, key: &StringKey) -> Option<TypeIdentifier> {
self.types[key] 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. /// Gets the effectiveness for a single attacking type against a single defending type.
pub fn get_single_effectiveness(&self, attacking: TypeIdentifier, defending: TypeIdentifier) -> f32 { 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. /// 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. /// Registers a new type in the library.
pub fn register_type(&mut self, name: &StringKey) -> TypeIdentifier { pub fn register_type(&mut self, name: &StringKey) -> TypeIdentifier {
let id = TypeIdentifier { let id = TypeIdentifier {
val: self.types.len() as u8, val: (self.types.len() + 1) as u8,
}; };
self.types.insert(name.clone(), id); 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 { for effectiveness in &mut self.effectiveness {
effectiveness.resize((id.val + 1) as usize, 1.0) effectiveness.resize((id.val) as usize, 1.0)
} }
id id
} }
/// Sets the effectiveness for an attacking type against a defending type. /// Sets the effectiveness for an attacking type against a defending type.
pub fn set_effectiveness(&mut self, attacking: TypeIdentifier, defending: TypeIdentifier, effectiveness: f32) { 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 // Borrow as read so we can read
let r = &lib; let r = &lib;
assert_eq!(r.get_type_id(&"foo".into()), t0); assert_eq!(r.get_type_id(&"foo".into()).unwrap(), t0);
assert_eq!(r.get_type_id(&"bar".into()), t1); assert_eq!(r.get_type_id(&"bar".into()).unwrap(), t1);
} }
#[test] #[test]

View File

@ -1,9 +1,4 @@
use std::sync::Arc;
use hashbrown::HashMap;
use crate::static_data::Statistic; 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 /// 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.
@ -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());
}
}

View File

@ -58,11 +58,13 @@ pub fn load_types(path: &String, type_library: &mut TypeLibrary) {
for record in reader.records() { for record in reader.records() {
let record = record.unwrap(); let record = record.unwrap();
let offensive_type = record.get(0).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() { for (i, v) in record.iter().skip(1).enumerate() {
let effectiveness = v.parse::<f32>().unwrap(); let effectiveness = v.parse::<f32>().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_data = move_data.as_object().unwrap();
let move_name = StringKey::new(move_data.get("name").unwrap().as_str().unwrap().into()); 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 = 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 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 base_power = move_data.get("power").unwrap().as_i64().unwrap() as u8;
let accuracy = move_data.get("accuracy").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() .as_array()
.unwrap() .unwrap()
.iter() .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(); .collect();
let moves = parse_moves(&value.get("moves").unwrap(), library.moves()); let moves = parse_moves(&value.get("moves").unwrap(), library.moves());
@ -410,8 +417,8 @@ fn test_type_library_loaded() {
assert_eq!( assert_eq!(
lib.get_effectiveness( lib.get_effectiveness(
lib.get_type_id(&StringKey::new("fire".into())), lib.get_type_id(&StringKey::new("fire".into())).unwrap(),
&[lib.get_type_id(&StringKey::new("grass".into()))], &[lib.get_type_id(&StringKey::new("grass".into())).unwrap()],
), ),
2.0 2.0
); );