2022-10-15 09:51:44 +00:00
|
|
|
use crate::dynamic_data::{BattleParty, Pokemon, PokemonParty};
|
2023-06-24 12:44:23 +00:00
|
|
|
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
|
|
|
|
use crate::ffi::FFIResult;
|
2023-04-16 17:57:21 +00:00
|
|
|
use crate::VecExt;
|
|
|
|
use anyhow::{anyhow, Result};
|
2022-10-15 09:51:44 +00:00
|
|
|
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(
|
2023-06-24 12:44:23 +00:00
|
|
|
party: FFIHandle<Arc<PokemonParty>>,
|
2022-10-15 09:51:44 +00:00
|
|
|
responsible_indices_ptr: *const u8,
|
|
|
|
responsible_indices_length: usize,
|
2023-06-24 12:44:23 +00:00
|
|
|
) -> FFIResult<FFIHandle<Arc<BattleParty>>> {
|
2023-04-16 17:57:21 +00:00
|
|
|
if responsible_indices_length % 2 != 0 {
|
2023-06-24 12:44:23 +00:00
|
|
|
return FFIResult::err(anyhow!("The length of responsible indices should be dividable by two"));
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
2023-04-16 17:57:21 +00:00
|
|
|
|
2022-10-15 09:51:44 +00:00
|
|
|
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);
|
2023-04-16 17:57:21 +00:00
|
|
|
|
|
|
|
let mut split = |i| -> Result<()> {
|
|
|
|
*responsible_indices.get_mut_res(i)? = (
|
|
|
|
*responsible_indices_slice.get_res(i * 2)?,
|
|
|
|
*responsible_indices_slice.get_res(i * 2 + 1)?,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
2022-10-15 09:51:44 +00:00
|
|
|
for i in 0..responsible_indices_length / 2 {
|
2023-04-16 17:57:21 +00:00
|
|
|
match split(i) {
|
|
|
|
Ok(_) => (),
|
2023-06-24 12:44:23 +00:00
|
|
|
Err(e) => return FFIResult::err(e),
|
2023-04-16 17:57:21 +00:00
|
|
|
}
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-24 12:44:23 +00:00
|
|
|
match BattleParty::new(party.from_ffi_handle(), responsible_indices) {
|
|
|
|
Ok(v) => FFIResult::ok(FFIHandle::get_handle(Arc::new(v).into())),
|
|
|
|
Err(e) => FFIResult::err(e),
|
2023-04-16 17:57:21 +00:00
|
|
|
}
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks whether the party is responsible for the given index.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn battle_party_is_responsible_for_index(ptr: FFIHandle<Arc<BattleParty>>, side: u8, index: u8) -> u8 {
|
|
|
|
u8::from(ptr.from_ffi_handle().is_responsible_for_index(side, index))
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Whether or not the party has non fainted Pokemon that could be thrown out into the field.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn battle_party_has_pokemon_not_in_field(ptr: FFIHandle<Arc<BattleParty>>) -> u8 {
|
|
|
|
u8::from(ptr.from_ffi_handle().has_pokemon_not_in_field())
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets a Pokemon at an index.
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn battle_party_get_pokemon(ptr: FFIHandle<Arc<BattleParty>>, index: usize) -> FFIHandle<Pokemon> {
|
|
|
|
if let Some(v) = ptr.from_ffi_handle().get_pokemon(index) {
|
|
|
|
FFIHandle::get_handle(v.into())
|
2022-10-15 09:51:44 +00:00
|
|
|
} else {
|
2023-06-24 12:44:23 +00:00
|
|
|
FFIHandle::none()
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Gets the underlying Pokemon Party
|
|
|
|
#[no_mangle]
|
2023-06-24 12:44:23 +00:00
|
|
|
extern "C" fn battle_party_party(ptr: FFIHandle<Arc<BattleParty>>) -> FFIHandle<Arc<PokemonParty>> {
|
|
|
|
FFIHandle::get_handle(ptr.from_ffi_handle().party().clone().into())
|
2022-10-15 09:51:44 +00:00
|
|
|
}
|