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

150 lines
5.0 KiB
Rust

use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{
cached_value, ExternRef, ExternalReferenceType, FFIArray, ImmutableList, StringKey,
VecExternRef,
};
use alloc::rc::Rc;
use alloc::vec::Vec;
#[repr(u8)]
pub enum Gender {
Male = 0,
Female = 1,
Genderless = 2,
}
#[repr(u8)]
pub enum Statistic {
HP = 0,
Attack = 1,
Defense = 2,
SpecialAttack = 3,
SpecialDefense = 4,
Speed = 5,
}
pub struct ImmutableStatisticSetInner {
ptr: ExternRef<ImmutableStatisticSet>,
/// The health point stat value.
hp: CachedValue<u16>,
/// The physical attack stat value.
attack: CachedValue<u16>,
/// The physical defense stat value.
defense: CachedValue<u16>,
/// The special attack stat value.
special_attack: CachedValue<u16>,
/// The special defense stat value.
special_defense: CachedValue<u16>,
/// The speed stat value.
speed: CachedValue<u16>,
}
#[derive(Clone)]
pub struct ImmutableStatisticSet {
inner: Rc<ImmutableStatisticSetInner>,
}
impl ImmutableStatisticSet {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self::from_ref(ptr, &|ptr| Self {
inner: Rc::new(ImmutableStatisticSetInner {
ptr,
hp: cached_value!({ static_statistics_set_get_hp(ptr) }),
attack: cached_value!({ static_statistics_set_get_attack(ptr) }),
defense: cached_value!({ static_statistics_set_get_defense(ptr) }),
special_attack: cached_value!({ static_statistics_set_get_special_attack(ptr) }),
special_defense: cached_value!({ static_statistics_set_get_special_defense(ptr) }),
speed: cached_value!({ static_statistics_set_get_speed(ptr) }),
}),
})
}
pub fn hp(&self) -> u16 {
self.inner.hp.value()
}
pub fn attack(&self) -> u16 {
self.inner.attack.value()
}
pub fn defense(&self) -> u16 {
self.inner.defense.value()
}
pub fn special_attack(&self) -> u16 {
self.inner.special_attack.value()
}
pub fn special_defense(&self) -> u16 {
self.inner.special_defense.value()
}
pub fn speed(&self) -> u16 {
self.inner.speed.value()
}
}
struct FormInner {
ptr: ExternRef<Form>,
name: CachedValue<StringKey>,
height: CachedValue<f32>,
weight: CachedValue<f32>,
types: CachedValue<Vec<u8>>,
base_experience: CachedValue<u32>,
base_stats: CachedValue<ImmutableStatisticSet>,
abilities: CachedValue<ImmutableList<StringKey>>,
hidden_abilities: CachedValue<ImmutableList<StringKey>>,
// moves: CachedValue<LearnableMoves>,
}
#[derive(Clone)]
pub struct Form {
inner: Rc<FormInner>,
}
impl Form {
pub(crate) fn new(ptr: ExternRef<Self>) -> Self {
Self::from_ref(ptr, &|ptr| Self {
inner: Rc::new(FormInner {
ptr,
name: cached_value!({ form_get_name(ptr).get_value().unwrap() }),
height: cached_value!({ form_get_height(ptr) }),
weight: cached_value!({ form_get_weight(ptr) }),
types: cached_value!({
let raw = form_get_types(ptr);
Vec::from_raw_parts(raw.ptr(), raw.len(), raw.len())
}),
base_experience: cached_value!({ form_get_base_experience(ptr) }),
base_stats: cached_value!({ form_get_base_stats(ptr).get_value().unwrap() }),
abilities: cached_value!({ form_get_abilities(ptr).get_immutable_list() }),
hidden_abilities: cached_value!({
form_get_hidden_abilities(ptr).get_immutable_list()
}),
}),
})
}
}
impl ExternalReferenceType for ImmutableStatisticSet {
fn from_extern_value(reference: ExternRef<Self>) -> Self {
ImmutableStatisticSet::new(reference)
}
}
crate::handling::cacheable::cacheable!(ImmutableStatisticSet);
crate::handling::cacheable::cacheable!(Form);
extern "wasm" {
fn static_statistics_set_get_hp(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_attack(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_defense(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_special_attack(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_special_defense(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn static_statistics_set_get_speed(r: ExternRef<ImmutableStatisticSet>) -> u16;
fn form_get_name(r: ExternRef<Form>) -> ExternRef<StringKey>;
fn form_get_height(r: ExternRef<Form>) -> f32;
fn form_get_weight(r: ExternRef<Form>) -> f32;
fn form_get_types(r: ExternRef<Form>) -> FFIArray<u8>;
fn form_get_base_experience(r: ExternRef<Form>) -> u32;
fn form_get_base_stats(r: ExternRef<Form>) -> ExternRef<ImmutableStatisticSet>;
fn form_get_abilities(r: ExternRef<Form>) -> VecExternRef<StringKey>;
fn form_get_hidden_abilities(r: ExternRef<Form>) -> VecExternRef<StringKey>;
}