PkmnLib_rs/src/ffi/static_data/growth_rate.rs

27 lines
1.2 KiB
Rust

use crate::defines::LevelInt;
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
use crate::ffi::FFIResult;
use crate::static_data::{GrowthRate, LookupGrowthRate};
use std::sync::Arc;
/// Instantiates a new lookup growth rate. The experience array should be the amount of experience
/// required per level, with the first element being the experience required for level 1 (generally 0).
#[no_mangle]
unsafe extern "C" fn growth_rate_lookup_new(array: *const u32, length: usize) -> FFIHandle<Arc<dyn GrowthRate>> {
let array = std::slice::from_raw_parts(array, length);
let g: Arc<dyn GrowthRate> = Arc::new(LookupGrowthRate::new(array.to_vec()));
FFIHandle::get_handle(g.into())
}
/// Calculate the level something with this growth rate would have at a certain experience.
#[no_mangle]
extern "C" fn growth_rate_calculate_level(ptr: FFIHandle<Arc<dyn GrowthRate>>, experience: u32) -> LevelInt {
ptr.from_ffi_handle().calculate_level(experience)
}
/// Calculate the experience something with this growth rate would have at a certain level.
#[no_mangle]
extern "C" fn growth_rate_calculate_experience(ptr: FFIHandle<Arc<dyn GrowthRate>>, level: LevelInt) -> FFIResult<u32> {
ptr.from_ffi_handle().calculate_experience(level).into()
}