2023-01-03 12:56:20 +00:00
|
|
|
use crate::ffi::{
|
|
|
|
ffi_arc_dyn_getter, ffi_arc_stringkey_getter, BorrowedPtr, ExternPointer, IdentifiablePointer, OwnedPtr,
|
|
|
|
};
|
2023-04-02 12:17:30 +00:00
|
|
|
use crate::static_data::{Form, Gender, Species, SpeciesImpl};
|
|
|
|
use crate::{Random, StringKey};
|
2022-09-18 16:02:13 +00:00
|
|
|
use hashbrown::HashSet;
|
|
|
|
use std::ffi::{c_char, CStr};
|
2022-09-22 15:33:45 +00:00
|
|
|
use std::ptr::drop_in_place;
|
2022-09-18 16:02:13 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Creates a new species.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn species_new(
|
|
|
|
id: u16,
|
|
|
|
name: BorrowedPtr<c_char>,
|
|
|
|
gender_rate: f32,
|
|
|
|
growth_rate: BorrowedPtr<c_char>,
|
|
|
|
capture_rate: u8,
|
2022-11-28 20:34:28 +00:00
|
|
|
default_form: OwnedPtr<Arc<dyn Form>>,
|
2022-09-18 16:02:13 +00:00
|
|
|
flags: *const *const c_char,
|
|
|
|
flags_length: usize,
|
2022-11-28 20:34:28 +00:00
|
|
|
) -> IdentifiablePointer<Arc<dyn Species>> {
|
2022-09-18 16:02:13 +00:00
|
|
|
let name: StringKey = CStr::from_ptr(name).to_str().unwrap().into();
|
|
|
|
let growth_rate: StringKey = CStr::from_ptr(growth_rate).to_str().unwrap().into();
|
|
|
|
|
|
|
|
let flags = std::slice::from_raw_parts(flags, flags_length);
|
|
|
|
let mut flags_set: HashSet<StringKey> = HashSet::with_capacity(flags_length);
|
|
|
|
for flag in flags {
|
|
|
|
flags_set.insert(CStr::from_ptr(*flag).to_str().unwrap().into());
|
|
|
|
}
|
2022-11-28 20:34:28 +00:00
|
|
|
let a: Arc<dyn Species> = Arc::new(SpeciesImpl::new(
|
2022-09-18 16:02:13 +00:00
|
|
|
id,
|
|
|
|
&name,
|
|
|
|
gender_rate,
|
|
|
|
&growth_rate,
|
|
|
|
capture_rate,
|
2022-10-08 11:15:04 +00:00
|
|
|
default_form.as_ref().unwrap().clone(),
|
2022-09-18 16:02:13 +00:00
|
|
|
flags_set,
|
2022-11-28 20:34:28 +00:00
|
|
|
));
|
|
|
|
a.into()
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Drop a reference to the species.
|
2022-09-22 15:33:45 +00:00
|
|
|
#[no_mangle]
|
2022-11-28 20:34:28 +00:00
|
|
|
unsafe extern "C" fn species_drop(ptr: OwnedPtr<Arc<dyn Species>>) {
|
2022-09-22 15:33:45 +00:00
|
|
|
drop_in_place(ptr);
|
|
|
|
}
|
|
|
|
|
2023-01-03 12:56:20 +00:00
|
|
|
ffi_arc_dyn_getter!(Species, id, u16);
|
|
|
|
ffi_arc_stringkey_getter!(Species, name);
|
|
|
|
ffi_arc_dyn_getter!(Species, gender_rate, f32);
|
|
|
|
ffi_arc_stringkey_getter!(Species, growth_rate);
|
|
|
|
ffi_arc_dyn_getter!(Species, capture_rate, u8);
|
2022-09-18 16:02:13 +00:00
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Adds a new form to the species.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn species_add_form(
|
2022-11-28 20:34:28 +00:00
|
|
|
mut species: ExternPointer<Arc<dyn Species>>,
|
2022-09-18 16:02:13 +00:00
|
|
|
name: BorrowedPtr<c_char>,
|
2022-11-28 20:34:28 +00:00
|
|
|
form: OwnedPtr<Arc<dyn Form>>,
|
2022-09-18 16:02:13 +00:00
|
|
|
) {
|
2022-10-08 11:15:04 +00:00
|
|
|
let form = form.as_ref().unwrap().clone();
|
2022-09-18 16:02:13 +00:00
|
|
|
species.as_mut().add_form(CStr::from_ptr(name).into(), form)
|
|
|
|
}
|
|
|
|
|
2022-10-14 14:53:30 +00:00
|
|
|
/// Gets a form by name.
|
2022-09-18 16:02:13 +00:00
|
|
|
#[no_mangle]
|
2022-10-08 11:15:04 +00:00
|
|
|
unsafe extern "C" fn species_get_form(
|
2022-11-28 20:34:28 +00:00
|
|
|
species: ExternPointer<Arc<dyn Species>>,
|
2022-10-08 11:15:04 +00:00
|
|
|
name: BorrowedPtr<c_char>,
|
2022-11-28 20:34:28 +00:00
|
|
|
) -> IdentifiablePointer<Arc<dyn Form>> {
|
2022-09-18 16:02:13 +00:00
|
|
|
let form = species.as_ref().get_form(&CStr::from_ptr(name).into());
|
|
|
|
if let Some(form) = form {
|
2022-10-08 11:15:04 +00:00
|
|
|
form.into()
|
2022-09-18 16:02:13 +00:00
|
|
|
} else {
|
2022-10-08 11:15:04 +00:00
|
|
|
IdentifiablePointer::none()
|
2022-09-18 16:02:13 +00:00
|
|
|
}
|
|
|
|
}
|
2023-04-02 12:17:30 +00:00
|
|
|
|
|
|
|
/// Gets a form by name.
|
|
|
|
#[no_mangle]
|
|
|
|
unsafe extern "C" fn species_get_random_gender(species: ExternPointer<Arc<dyn Species>>, seed: u64) -> Gender {
|
|
|
|
let mut rand = Random::new(seed as u128);
|
|
|
|
species.as_ref().get_random_gender(&mut rand)
|
|
|
|
}
|