2022-10-01 13:40:15 +00:00
|
|
|
use crate::defines::LevelInt;
|
2022-10-08 11:15:04 +00:00
|
|
|
use crate::ffi::{BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr};
|
2022-10-01 13:40:15 +00:00
|
|
|
use crate::static_data::{GrowthRate, GrowthRateLibrary};
|
|
|
|
use std::ffi::{c_char, CStr};
|
|
|
|
use std::ptr::drop_in_place;
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Instantiates a new growth rate library with a capacity
|
2022-10-01 13:40:15 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
extern "C" fn growth_rate_library_new(capacity: usize) -> IdentifiablePointer<GrowthRateLibrary> {
|
|
|
|
Box::new(GrowthRateLibrary::new(capacity)).into()
|
2022-10-01 13:40:15 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Drops the growthrate library.
|
2022-10-01 13:40:15 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn growth_rate_library_drop(ptr: OwnedPtr<GrowthRateLibrary>) {
|
|
|
|
drop_in_place(ptr)
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Calculates the level for a given growth key name and a certain experience.
|
2022-10-01 13:40:15 +00:00
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Calculates the experience for a given growth key name and a certain level.
|
2022-10-01 13:40:15 +00:00
|
|
|
#[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)
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Adds a new growth rate with a name and value.
|
2022-10-01 13:40:15 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn growth_rate_library_add_growth_rate(
|
2022-10-14 14:53:30 +00:00
|
|
|
mut ptr: ExternPointer<GrowthRateLibrary>,
|
2022-10-01 13:40:15 +00:00
|
|
|
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));
|
|
|
|
}
|