PkmnLib_rs/src/ffi/dynamic_data/choices.rs

103 lines
3.7 KiB
Rust

use crate::dynamic_data::{FleeChoice, LearnedMove, MoveChoice, PassChoice, Pokemon, TurnChoice};
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
use crate::ffi::FFIResult;
use anyhow::anyhow;
use std::ops::Deref;
use std::sync::Arc;
/// Get the user of the given choice.
#[no_mangle]
extern "C" fn turn_choice_user(choice: FFIHandle<Arc<TurnChoice>>) -> FFIHandle<Pokemon> {
FFIHandle::get_handle(choice.from_ffi_handle().user().clone().into())
}
/// Get the speed of the user for the choice. Note that this speed is the speed of the Pokemon
/// at the start of the turn!
#[no_mangle]
extern "C" fn turn_choice_speed(choice: FFIHandle<Arc<TurnChoice>>) -> u32 {
choice.from_ffi_handle().speed()
}
/// Gets whether or not the choice has failed. If we notice this when we execute the choice, we
/// will not execute it.
#[no_mangle]
extern "C" fn turn_choice_has_failed(choice: FFIHandle<Arc<TurnChoice>>) -> u8 {
u8::from(choice.from_ffi_handle().has_failed())
}
/// Fails the choice. This will prevent it from executing and run a specific fail handling during
/// execution. Note that this can not be undone.
#[no_mangle]
extern "C" fn turn_choice_fail(choice: FFIHandle<Arc<TurnChoice>>) {
choice.from_ffi_handle().fail()
}
/// Creates a new Turn Choice with a move to use.
#[no_mangle]
extern "C" fn turn_choice_move_new(
user: FFIHandle<Pokemon>,
learned_move: FFIHandle<Arc<LearnedMove>>,
target_side: u8,
target_index: u8,
) -> FFIHandle<Arc<TurnChoice>> {
FFIHandle::get_handle(
Arc::new(TurnChoice::Move(MoveChoice::new(
user.from_ffi_handle(),
learned_move.from_ffi_handle(),
target_side,
target_index,
)))
.into(),
)
}
/// The actual learned move on the Pokemon we use for this choice.
#[no_mangle]
extern "C" fn turn_choice_move_learned_move(
choice: FFIHandle<Arc<TurnChoice>>,
) -> FFIResult<FFIHandle<Arc<LearnedMove>>> {
if let TurnChoice::Move(c) = choice.from_ffi_handle().deref() {
return FFIResult::ok(FFIHandle::get_handle(c.used_move().clone().into()));
}
FFIResult::err(anyhow!("Turn choice was not a learned move"))
}
/// The target side the move is aimed at.
#[no_mangle]
extern "C" fn turn_choice_move_target_side(choice: FFIHandle<Arc<TurnChoice>>) -> FFIResult<u8> {
if let TurnChoice::Move(c) = choice.from_ffi_handle().deref() {
return FFIResult::ok(c.target_side());
}
FFIResult::err(anyhow!("Turn choice was not a learned move"))
}
/// The Pokemon index on the side we're aiming at.
#[no_mangle]
extern "C" fn turn_choice_move_target_index(choice: FFIHandle<Arc<TurnChoice>>) -> FFIResult<u8> {
if let TurnChoice::Move(c) = choice.from_ffi_handle().deref() {
return FFIResult::ok(c.target_index());
}
FFIResult::err(anyhow!("Turn choice was not a learned move"))
}
/// The priority of the move choice at the beginning of the turn.
#[no_mangle]
extern "C" fn turn_choice_move_priority(choice: FFIHandle<Arc<TurnChoice>>) -> FFIResult<i8> {
if let TurnChoice::Move(c) = choice.from_ffi_handle().deref() {
return FFIResult::ok(c.priority());
}
FFIResult::err(anyhow!("Turn choice was not a learned move"))
}
/// Creates a new Turn Choice for the user to flee.
#[no_mangle]
extern "C" fn turn_choice_flee_new(user: FFIHandle<Pokemon>) -> FFIHandle<Arc<TurnChoice>> {
FFIHandle::get_handle(Arc::new(TurnChoice::Flee(FleeChoice::new(user.from_ffi_handle()))).into())
}
/// Creates a new Turn Choice for the user to pass the turn.
#[no_mangle]
extern "C" fn turn_choice_pass_new(user: FFIHandle<Pokemon>) -> FFIHandle<Arc<TurnChoice>> {
FFIHandle::get_handle(Arc::new(TurnChoice::Pass(PassChoice::new(user.from_ffi_handle()))).into())
}