FFI for Battle Party

This commit is contained in:
Deukhoofd 2022-10-15 11:51:44 +02:00
parent 554a665b8f
commit 6c976216b8
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
3 changed files with 67 additions and 0 deletions

View File

@ -2,12 +2,15 @@ use std::sync::Arc;
use crate::dynamic_data::models::pokemon::Pokemon; use crate::dynamic_data::models::pokemon::Pokemon;
use crate::dynamic_data::models::pokemon_party::PokemonParty; use crate::dynamic_data::models::pokemon_party::PokemonParty;
use crate::{ValueIdentifiable, ValueIdentifier};
/// A battle party is a wrapper around a party, with the indices for which the party is responsible /// A battle party is a wrapper around a party, with the indices for which the party is responsible
/// on the field attached. /// on the field attached.
#[derive(Debug)] #[derive(Debug)]
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))] #[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
pub struct BattleParty { pub struct BattleParty {
/// A unique identifier so we know what value this is.
identifier: ValueIdentifier,
/// The party the BattleParty is holding. /// The party the BattleParty is holding.
party: Arc<PokemonParty>, party: Arc<PokemonParty>,
/// The indices for which the party is responsible, in the format (side, index) /// The indices for which the party is responsible, in the format (side, index)
@ -19,6 +22,7 @@ impl BattleParty {
/// for. /// for.
pub fn new(party: Arc<PokemonParty>, responsible_indices: Vec<(u8, u8)>) -> Self { pub fn new(party: Arc<PokemonParty>, responsible_indices: Vec<(u8, u8)>) -> Self {
Self { Self {
identifier: Default::default(),
party, party,
responsible_indices, responsible_indices,
} }
@ -54,3 +58,9 @@ impl BattleParty {
&self.party &self.party
} }
} }
impl ValueIdentifiable for BattleParty {
fn value_identifier(&self) -> ValueIdentifier {
self.identifier
}
}

View File

@ -0,0 +1,55 @@
use crate::dynamic_data::{BattleParty, Pokemon, PokemonParty};
use crate::ffi::{ExternPointer, IdentifiablePointer};
use std::sync::Arc;
/// A battle party is a wrapper around a party, with the indices for which the party is responsible
/// on the field attached. The indices are stored
#[no_mangle]
extern "C" fn battle_party_new(
party: ExternPointer<Arc<PokemonParty>>,
responsible_indices_ptr: *const u8,
responsible_indices_length: usize,
) -> IdentifiablePointer<Arc<BattleParty>> {
if responsible_indices_length % 2 == 1 {
panic!("The length of responsible indices should be dividable by two");
}
let responsible_indices_slice =
unsafe { std::slice::from_raw_parts(responsible_indices_ptr, responsible_indices_length) };
let mut responsible_indices: Vec<(u8, u8)> = Vec::with_capacity(responsible_indices_length / 2);
for i in 0..responsible_indices_length / 2 {
responsible_indices[i] = (responsible_indices_slice[i * 2], responsible_indices_slice[i * 2 + 1])
}
Arc::new(BattleParty::new(party.as_ref().clone(), responsible_indices)).into()
}
/// Checks whether the party is responsible for the given index.
#[no_mangle]
extern "C" fn battle_party_is_responsible_for_index(ptr: ExternPointer<Arc<BattleParty>>, side: u8, index: u8) -> u8 {
u8::from(ptr.as_ref().is_responsible_for_index(side, index))
}
/// Whether or not the party has non fainted Pokemon that could be thrown out into the field.
#[no_mangle]
extern "C" fn battle_party_has_pokemon_not_in_field(ptr: ExternPointer<Arc<BattleParty>>) -> u8 {
u8::from(ptr.as_ref().has_pokemon_not_in_field())
}
/// Gets a Pokemon at an index.
#[no_mangle]
extern "C" fn battle_party_get_pokemon(
ptr: ExternPointer<Arc<BattleParty>>,
index: usize,
) -> IdentifiablePointer<Arc<Pokemon>> {
if let Some(v) = ptr.as_ref().get_pokemon(index) {
v.into()
} else {
IdentifiablePointer::none()
}
}
/// Gets the underlying Pokemon Party
#[no_mangle]
extern "C" fn battle_party_party(ptr: ExternPointer<Arc<BattleParty>>) -> IdentifiablePointer<Arc<PokemonParty>> {
ptr.as_ref().party().clone().into()
}

View File

@ -1,3 +1,5 @@
/// The foreign function interface for a battle wrapper of a party.
mod battle_party;
/// The foreign function interface for a Learned Move. /// The foreign function interface for a Learned Move.
mod learned_move; mod learned_move;
/// The foreign function interface for a Pokemon. /// The foreign function interface for a Pokemon.