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

121 lines
3.4 KiB
Rust

use crate::app_interface::StringKey;
use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{cached_value, ExternRef, ExternalReferenceType};
use alloc::collections::BTreeMap;
use alloc::rc::Rc;
#[repr(u8)]
#[derive(Clone)]
pub enum MoveCategory {
Physical = 0,
Special = 1,
Status = 2,
}
#[repr(u8)]
#[derive(Clone)]
pub enum MoveTarget {
Adjacent,
AdjacentAlly,
AdjacentAllySelf,
AdjacentOpponent,
All,
AllAdjacent,
AllAdjacentOpponent,
AllAlly,
AllOpponent,
Any,
RandomOpponent,
OnSelf,
}
struct MoveDataInner {
ptr: ExternRef<MoveData>,
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 MoveData {
inner: Rc<MoveDataInner>,
}
impl MoveData {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
MoveData::from_ref(ptr, &|ptr| Self {
inner: Rc::new(MoveDataInner {
ptr,
name: cached_value!({ StringKey::new(move_data_get_name(ptr)) }),
move_type: cached_value!({ move_data_get_type(ptr) }),
category: cached_value!({ move_data_get_category(ptr) }),
base_power: cached_value!({ move_data_get_base_power(ptr) }),
accuracy: cached_value!({ move_data_get_accuracy(ptr) }),
base_usages: cached_value!({ move_data_get_base_power(ptr) }),
target: cached_value!({ move_data_get_target(ptr) }),
priority: cached_value!({ move_data_get_priority(ptr) }),
}),
})
}
pub(crate) fn ptr(&self) -> ExternRef<Self> {
self.inner.ptr
}
pub fn name(&self) -> StringKey {
self.inner.name.value()
}
pub fn move_type(&self) -> u8 {
self.inner.move_type.value()
}
pub fn category(&self) -> MoveCategory {
self.inner.category.value()
}
pub fn base_power(&self) -> u8 {
self.inner.base_power.value()
}
pub fn accuracy(&self) -> u8 {
self.inner.accuracy.value()
}
pub fn base_usages(&self) -> u8 {
self.inner.base_usages.value()
}
pub fn target(&self) -> MoveTarget {
self.inner.target.value()
}
pub fn priority(&self) -> i8 {
self.inner.priority.value()
}
pub fn has_flag(&self, flag: &StringKey) -> bool {
unsafe { move_data_has_flag(self.ptr(), flag.ptr()) }
}
}
impl ExternalReferenceType for MoveData {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
MoveData::new(reference)
}
}
crate::handling::cacheable::cacheable!(MoveData);
extern "wasm" {
fn move_data_get_name(ptr: ExternRef<MoveData>) -> ExternRef<StringKey>;
fn move_data_get_type(ptr: ExternRef<MoveData>) -> u8;
fn move_data_get_category(ptr: ExternRef<MoveData>) -> MoveCategory;
fn move_data_get_base_power(ptr: ExternRef<MoveData>) -> u8;
fn move_data_get_accuracy(ptr: ExternRef<MoveData>) -> u8;
fn move_data_get_base_usages(ptr: ExternRef<MoveData>) -> u8;
fn move_data_get_target(ptr: ExternRef<MoveData>) -> MoveTarget;
fn move_data_get_priority(ptr: ExternRef<MoveData>) -> i8;
fn move_data_has_flag(ptr: ExternRef<MoveData>, flag: ExternRef<StringKey>) -> bool;
}