PkmnLib_rs/src/ffi/dynamic_data/libraries/battle_stat_calculator.rs

65 lines
2.3 KiB
Rust

use crate::dynamic_data::{BattleStatCalculator, Gen7BattleStatCalculator, Pokemon};
use crate::ffi::{ExternPointer, IdentifiablePointer, NativeResult, OwnedPtr};
use crate::static_data::{Statistic, StatisticSet};
use std::ptr::drop_in_place;
use std::sync::Arc;
/// Creates a new Gen 7 battle stat calculator
#[no_mangle]
extern "C" fn gen_7_battle_stat_calculator_new() -> IdentifiablePointer<Box<dyn BattleStatCalculator>> {
let v: Box<dyn BattleStatCalculator> = Box::new(Gen7BattleStatCalculator::new());
let id = v.value_identifier();
let ptr = Box::into_raw(Box::new(v));
IdentifiablePointer::new(ptr, id)
}
/// Drops a battle stat calculator.
#[no_mangle]
extern "C" fn battle_stat_calculator_drop(ptr: OwnedPtr<Box<dyn BattleStatCalculator>>) {
unsafe { drop_in_place(ptr) };
}
/// Calculate all the flat stats of a Pokemon, disregarding stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_flat_stats(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
mut stats: ExternPointer<Box<StatisticSet<u32>>>,
) -> NativeResult<()> {
ptr.as_ref()
.calculate_flat_stats(pokemon.as_ref(), stats.as_mut())
.into()
}
/// Calculate a single flat stat of a Pokemon, disregarding stat boost
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_flat_stat(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
) -> NativeResult<u32> {
ptr.as_ref().calculate_flat_stat(pokemon.as_ref(), stat).into()
}
/// Calculate all the boosted stats of a Pokemon, including stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_boosted_stats(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
mut stats: ExternPointer<Box<StatisticSet<u32>>>,
) -> NativeResult<()> {
ptr.as_ref()
.calculate_boosted_stats(pokemon.as_ref(), stats.as_mut())
.into()
}
/// Calculate a single boosted stat of a Pokemon, including stat boosts.
#[no_mangle]
extern "C" fn battle_stat_calculator_calculate_boosted_stat(
ptr: ExternPointer<Box<dyn BattleStatCalculator>>,
pokemon: ExternPointer<Arc<Pokemon>>,
stat: Statistic,
) -> NativeResult<u32> {
ptr.as_ref().calculate_boosted_stat(pokemon.as_ref(), stat).into()
}