2022-10-15 09:16:41 +00:00
|
|
|
use crate::dynamic_data::{Pokemon, PokemonParty};
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
|
|
|
|
use crate::ffi::FFIResult;
|
2022-10-15 09:16:41 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// Instantiates a party with a set size.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_new(capacity: usize) -> FFIHandle<Arc<PokemonParty>> {
|
|
|
|
FFIHandle::get_handle(Arc::new(PokemonParty::new(capacity)).into())
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a Pokemon at an index in the party.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_at(ptr: FFIHandle<Arc<PokemonParty>>, index: usize) -> FFIHandle<Pokemon> {
|
|
|
|
if let Some(v) = ptr.from_ffi_handle().at(index) {
|
|
|
|
FFIHandle::get_handle(v.into())
|
2022-10-15 09:16:41 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIHandle::none()
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a Pokemon at an index in the party.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_switch(ptr: FFIHandle<Arc<PokemonParty>>, a: usize, b: usize) {
|
|
|
|
ptr.from_ffi_handle().switch(a, b);
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_swap_into(
|
2023-06-24 12:44:23 +00:00
|
|
|
ptr: FFIHandle<Arc<PokemonParty>>,
|
2022-10-15 09:16:41 +00:00
|
|
|
index: usize,
|
2023-06-24 12:44:23 +00:00
|
|
|
pokemon: FFIHandle<Pokemon>,
|
|
|
|
) -> FFIResult<FFIHandle<Pokemon>> {
|
|
|
|
let pokemon = if pokemon.is_none() {
|
2022-10-15 09:16:41 +00:00
|
|
|
None
|
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
Some(pokemon.from_ffi_handle())
|
2022-10-15 09:16:41 +00:00
|
|
|
};
|
2023-06-24 12:44:23 +00:00
|
|
|
match ptr.from_ffi_handle().swap_into(index, pokemon) {
|
|
|
|
Ok(Some(v)) => FFIResult::ok(FFIHandle::get_handle(v.into())),
|
|
|
|
Ok(None) => FFIResult::ok(FFIHandle::none()),
|
|
|
|
Err(e) => FFIResult::err(e),
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether or not the party still has Pokemon that can be used in battle.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_has_usable_pokemon(ptr: FFIHandle<Arc<PokemonParty>>) -> u8 {
|
|
|
|
u8::from(ptr.from_ffi_handle().has_usable_pokemon())
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the length of the underlying list of Pokemon.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_length(ptr: FFIHandle<Arc<PokemonParty>>) -> usize {
|
|
|
|
ptr.from_ffi_handle().length()
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes sure there are no empty spots in the party anymore, leaving the length the same.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_pack_party(ptr: FFIHandle<Arc<PokemonParty>>) -> FFIResult<()> {
|
|
|
|
ptr.from_ffi_handle().pack_party().into()
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the party contains a given pokemon.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn pokemon_party_has_pokemon(ptr: FFIHandle<Arc<PokemonParty>>, pokemon: FFIHandle<Pokemon>) -> u8 {
|
|
|
|
u8::from(ptr.from_ffi_handle().has_pokemon(&pokemon.from_ffi_handle()))
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|