2022-10-15 09:16:41 +00:00
|
|
|
use crate::dynamic_data::{Pokemon, PokemonParty};
|
2023-04-16 17:57:21 +00:00
|
|
|
use crate::ffi::{ExternPointer, IdentifiablePointer, NativeResult};
|
2022-10-15 09:16:41 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
/// Instantiates a party with a set size.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_new(capacity: usize) -> IdentifiablePointer<Arc<PokemonParty>> {
|
|
|
|
Arc::new(PokemonParty::new(capacity)).into()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a Pokemon at an index in the party.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_at(
|
|
|
|
ptr: ExternPointer<Arc<PokemonParty>>,
|
|
|
|
index: usize,
|
|
|
|
) -> IdentifiablePointer<Arc<Pokemon>> {
|
|
|
|
if let Some(v) = ptr.as_ref().at(index) {
|
|
|
|
v.into()
|
|
|
|
} else {
|
|
|
|
IdentifiablePointer::none()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a Pokemon at an index in the party.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_switch(ptr: ExternPointer<Arc<PokemonParty>>, a: usize, b: usize) {
|
|
|
|
ptr.as_ref().switch(a, b);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the Pokemon at an index to a Pokemon, returning the old Pokemon.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_swap_into(
|
|
|
|
ptr: ExternPointer<Arc<PokemonParty>>,
|
|
|
|
index: usize,
|
|
|
|
pokemon: ExternPointer<Arc<Pokemon>>,
|
2023-04-16 17:57:21 +00:00
|
|
|
) -> NativeResult<IdentifiablePointer<Arc<Pokemon>>> {
|
2022-10-15 09:16:41 +00:00
|
|
|
let pokemon = if pokemon.ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(pokemon.as_ref().clone())
|
|
|
|
};
|
2023-04-16 17:57:21 +00:00
|
|
|
match ptr.as_ref().swap_into(index, pokemon) {
|
|
|
|
Ok(Some(v)) => NativeResult::ok(v.into()),
|
|
|
|
Ok(None) => NativeResult::ok(IdentifiablePointer::none()),
|
|
|
|
Err(e) => NativeResult::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]
|
|
|
|
extern "C" fn pokemon_party_has_usable_pokemon(ptr: ExternPointer<Arc<PokemonParty>>) -> u8 {
|
|
|
|
u8::from(ptr.as_ref().has_usable_pokemon())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the length of the underlying list of Pokemon.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_length(ptr: ExternPointer<Arc<PokemonParty>>) -> usize {
|
|
|
|
ptr.as_ref().length()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Makes sure there are no empty spots in the party anymore, leaving the length the same.
|
|
|
|
#[no_mangle]
|
2023-04-16 17:57:21 +00:00
|
|
|
extern "C" fn pokemon_party_pack_party(ptr: ExternPointer<Arc<PokemonParty>>) -> NativeResult<()> {
|
|
|
|
ptr.as_ref().pack_party().into()
|
2022-10-15 09:16:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks if the party contains a given pokemon.
|
|
|
|
#[no_mangle]
|
|
|
|
extern "C" fn pokemon_party_has_pokemon(
|
|
|
|
ptr: ExternPointer<Arc<PokemonParty>>,
|
|
|
|
pokemon: ExternPointer<Arc<Pokemon>>,
|
|
|
|
) -> u8 {
|
|
|
|
u8::from(ptr.as_ref().has_pokemon(pokemon.as_ref()))
|
|
|
|
}
|