Gen7ScriptsRs/pkmn_lib_interface/src/app_interface/static_data/move_data.rs

150 lines
4.6 KiB
Rust
Executable File

use crate::app_interface::StringKey;
use alloc::rc::Rc;
#[repr(u8)]
#[derive(Clone, Eq, PartialEq)]
pub enum MoveCategory {
Physical = 0,
Special = 1,
Status = 2,
}
impl Default for MoveCategory {
fn default() -> Self {
Self::Physical
}
}
#[repr(u8)]
#[derive(Clone, Eq, PartialEq)]
pub enum MoveTarget {
Adjacent,
AdjacentAlly,
AdjacentAllySelf,
AdjacentOpponent,
All,
AllAdjacent,
AllAdjacentOpponent,
AllAlly,
AllOpponent,
Any,
RandomOpponent,
OnSelf,
}
impl Default for MoveTarget {
fn default() -> Self {
Self::Adjacent
}
}
#[cfg_attr(feature = "mock_data", mockall::automock)]
pub trait MoveDataTrait {
fn name(&self) -> StringKey;
fn move_type(&self) -> u8;
fn category(&self) -> MoveCategory;
fn base_power(&self) -> u8;
fn accuracy(&self) -> u8;
fn base_usages(&self) -> u8;
fn target(&self) -> MoveTarget;
fn priority(&self) -> i8;
fn has_flag(&self, flag: &str) -> PkmnResult<bool>;
}
pub type MoveData = Rc<dyn MoveDataTrait>;
#[cfg(feature = "mock_data")]
pub type MockMoveData = MockMoveDataTrait;
#[cfg(not(feature = "mock_data"))]
mod implementation {
use super::*;
use crate::app_interface::get_hash;
use crate::handling::cached_value::CachedValue;
use crate::handling::extern_ref::{ExternRef, ExternalReferenceType};
use crate::handling::{Cacheable, WasmResult};
use crate::{cached_value, cached_value_getters, PkmnResult};
struct MoveDataInner {
ptr: ExternRef<MoveDataImpl>,
name: CachedValue<StringKey>,
move_type: CachedValue<u8>,
category: CachedValue<MoveCategory>,
base_power: CachedValue<u8>,
accuracy: CachedValue<u8>,
base_usages: CachedValue<u8>,
target: CachedValue<MoveTarget>,
priority: CachedValue<i8>,
}
#[derive(Clone)]
pub struct MoveDataImpl {
inner: Rc<MoveDataInner>,
}
impl MoveDataImpl {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
MoveDataImpl::from_ref(ptr, &|ptr| Self {
inner: Rc::new(MoveDataInner {
ptr,
name: cached_value!({ StringKey::new(move_data_get_name(ptr).unwrap()) }),
move_type: cached_value!({ move_data_get_type(ptr).unwrap() }),
category: cached_value!({ move_data_get_category(ptr).unwrap() }),
base_power: cached_value!({ move_data_get_base_power(ptr).unwrap() }),
accuracy: cached_value!({ move_data_get_accuracy(ptr).unwrap() }),
base_usages: cached_value!({ move_data_get_base_power(ptr).unwrap() }),
target: cached_value!({ move_data_get_target(ptr).unwrap() }),
priority: cached_value!({ move_data_get_priority(ptr).unwrap() }),
}),
})
}
}
impl MoveDataTrait for MoveDataImpl {
cached_value_getters! {
fn name(&self) -> StringKey;
fn move_type(&self) -> u8;
fn category(&self) -> MoveCategory;
fn base_power(&self) -> u8;
fn accuracy(&self) -> u8;
fn base_usages(&self) -> u8;
fn target(&self) -> MoveTarget;
fn priority(&self) -> i8;
}
fn has_flag(&self, flag: &str) -> PkmnResult<bool> {
let hash = get_hash(flag);
unsafe { move_data_has_flag_by_hash(self.inner.ptr, hash).as_res() }
}
}
impl ExternalReferenceType for MoveDataImpl {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
MoveDataImpl::new(reference)
}
}
crate::handling::cacheable::cacheable!(MoveDataImpl);
extern "wasm" {
fn move_data_get_name(ptr: ExternRef<MoveDataImpl>) -> WasmResult<ExternRef<StringKey>>;
fn move_data_get_type(ptr: ExternRef<MoveDataImpl>) -> WasmResult<u8>;
fn move_data_get_category(ptr: ExternRef<MoveDataImpl>) -> WasmResult<MoveCategory>;
fn move_data_get_base_power(ptr: ExternRef<MoveDataImpl>) -> WasmResult<u8>;
fn move_data_get_accuracy(ptr: ExternRef<MoveDataImpl>) -> WasmResult<u8>;
fn move_data_get_base_usages(ptr: ExternRef<MoveDataImpl>) -> WasmResult<u8>;
fn move_data_get_target(ptr: ExternRef<MoveDataImpl>) -> WasmResult<MoveTarget>;
fn move_data_get_priority(ptr: ExternRef<MoveDataImpl>) -> WasmResult<i8>;
fn move_data_has_flag_by_hash(
ptr: ExternRef<MoveDataImpl>,
flag_hash: u32,
) -> WasmResult<bool>;
}
}
use crate::PkmnResult;
#[cfg(not(feature = "mock_data"))]
pub use implementation::*;