FFI for Pokemon Party, make Pokemon Party use interior mutability.

This commit is contained in:
2022-10-15 11:16:41 +02:00
parent c7b5bb7d12
commit 554a665b8f
5 changed files with 120 additions and 26 deletions

View File

@@ -2,3 +2,5 @@
mod learned_move;
/// The foreign function interface for a Pokemon.
mod pokemon;
/// The foreign function interface for a party of Pokemon.
mod pokemon_party;

View File

@@ -0,0 +1,74 @@
use crate::dynamic_data::{Pokemon, PokemonParty};
use crate::ffi::{ExternPointer, IdentifiablePointer};
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>>,
) -> IdentifiablePointer<Arc<Pokemon>> {
let pokemon = if pokemon.ptr.is_null() {
None
} else {
Some(pokemon.as_ref().clone())
};
if let Some(v) = ptr.as_ref().swap_into(index, pokemon) {
v.into()
} else {
IdentifiablePointer::none()
}
}
/// 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]
extern "C" fn pokemon_party_pack_party(ptr: ExternPointer<Arc<PokemonParty>>) {
ptr.as_ref().pack_party()
}
/// 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()))
}