Registration of MoveTurnChoices

This commit is contained in:
Deukhoofd 2022-08-16 18:43:02 +02:00
parent f06d46d854
commit 45b16f415f
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
4 changed files with 80 additions and 7 deletions

View File

@ -1,6 +1,6 @@
use pkmn_lib_interface::app_interface::list::ImmutableList; use pkmn_lib_interface::app_interface::list::ImmutableList;
use pkmn_lib_interface::app_interface::{ use pkmn_lib_interface::app_interface::{
get_hash, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice, get_hash, DamageSource, DataLibrary, DynamicLibrary, EffectParameter, TurnChoice,
}; };
use pkmn_lib_interface::dbg; use pkmn_lib_interface::dbg;
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -46,5 +46,12 @@ impl Script for TestScript {
"On before turn for user: {}", "On before turn for user: {}",
choice.user().species().name() choice.user().species().name()
); );
choice.user().damage(50, DamageSource::Misc);
if let TurnChoice::Move(d) = choice {
dbg!(
"On before turn for move choice: {}",
d.used_move().move_data().name()
);
}
} }
} }

View File

@ -1,7 +1,8 @@
use crate::app_interface::Pokemon; use crate::app_interface::{LearnedMove, Pokemon};
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::temporary::Temporary; use crate::handling::temporary::Temporary;
use crate::{cached_value, ExternRef, ExternalReferenceType}; use crate::{cached_value, wasm_value_getters, ExternRef, ExternalReferenceType, Script};
use alloc::boxed::Box;
use alloc::rc::Rc; use alloc::rc::Rc;
struct BaseTurnChoiceData { struct BaseTurnChoiceData {
@ -11,6 +12,9 @@ struct BaseTurnChoiceData {
struct MoveTurnChoiceDataInner { struct MoveTurnChoiceDataInner {
base: BaseTurnChoiceData, base: BaseTurnChoiceData,
used_move: CachedValue<LearnedMove>,
target_side: CachedValue<u8>,
target_index: CachedValue<u8>,
} }
#[derive(Clone)] #[derive(Clone)]
struct MoveTurnChoiceDataTemporary { struct MoveTurnChoiceDataTemporary {
@ -29,12 +33,50 @@ pub enum TurnChoice {
} }
impl TurnChoice { impl TurnChoice {
pub fn user(&self) -> Pokemon { fn base(&self) -> &BaseTurnChoiceData {
match self { match self {
TurnChoice::Move(data) => data.temp.value().inner.base.user.value(), TurnChoice::Move(d) => &d.temp.value_ref().inner.base,
_ => panic!("Unknown turn choice type"), TurnChoice::Item() => unimplemented!(),
TurnChoice::Switch() => unimplemented!(),
TurnChoice::Flee => unimplemented!(),
TurnChoice::Pass => unimplemented!(),
} }
} }
pub fn user(&self) -> Pokemon {
self.base().user.value()
}
pub fn speed(&self) -> u32 {
unsafe { turn_choice_get_speed(self.base().reference) }
}
pub fn has_failed(&self) -> bool {
unsafe { turn_choice_has_failed(self.base().reference) }
}
pub fn fail(&self) {
unsafe { turn_choice_fail(self.base().reference) }
}
}
impl MoveTurnChoiceData {
pub fn used_move(&self) -> LearnedMove {
self.temp.value().inner.used_move.value()
}
pub fn target_side(&self) -> u8 {
self.temp.value().inner.target_side.value()
}
pub fn target_index(&self) -> u8 {
self.temp.value().inner.target_index.value()
}
pub fn priority(&self) -> i8 {
unsafe { turn_choice_move_priority(self.temp.value().inner.base.reference.cast()) }
}
pub fn move_script(&self) -> Option<&Box<dyn Script>> {
unsafe { turn_choice_move_script(self.temp.value().inner.base.reference.cast()).as_ref() }
}
} }
impl ExternalReferenceType for TurnChoice { impl ExternalReferenceType for TurnChoice {
@ -59,6 +101,13 @@ impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
turn_choice_get_user(reference.cast()).get_value().unwrap() turn_choice_get_user(reference.cast()).get_value().unwrap()
}), }),
}, },
used_move: cached_value!({
turn_choice_move_used_move(reference.cast())
.get_value()
.unwrap()
}),
target_side: cached_value!({ turn_choice_move_target_side(reference.cast()) }),
target_index: cached_value!({ turn_choice_move_target_index(reference.cast()) }),
}), }),
} }
} }
@ -67,6 +116,15 @@ impl ExternalReferenceType for MoveTurnChoiceDataTemporary {
extern "wasm" { extern "wasm" {
fn turn_choice_get_kind(r: ExternRef<TurnChoice>) -> u8; fn turn_choice_get_kind(r: ExternRef<TurnChoice>) -> u8;
fn turn_choice_get_user(r: ExternRef<TurnChoice>) -> ExternRef<Pokemon>; fn turn_choice_get_user(r: ExternRef<TurnChoice>) -> ExternRef<Pokemon>;
fn turn_choice_get_speed(r: ExternRef<TurnChoice>) -> u32;
fn turn_choice_has_failed(r: ExternRef<TurnChoice>) -> bool;
fn turn_choice_fail(r: ExternRef<TurnChoice>);
fn turn_choice_move_used_move(r: ExternRef<MoveTurnChoiceData>) -> ExternRef<LearnedMove>;
fn turn_choice_move_target_side(r: ExternRef<MoveTurnChoiceData>) -> u8;
fn turn_choice_move_target_index(r: ExternRef<MoveTurnChoiceData>) -> u8;
fn turn_choice_move_priority(r: ExternRef<MoveTurnChoiceData>) -> i8;
fn turn_choice_move_script(r: ExternRef<MoveTurnChoiceData>) -> *const Box<dyn Script>;
} }
#[no_mangle] #[no_mangle]

View File

@ -18,6 +18,7 @@ impl<T> ExternRef<T> {
self.p self.p
} }
#[inline]
pub fn get_value(&self) -> Option<T> pub fn get_value(&self) -> Option<T>
where where
T: ExternalReferenceType, T: ExternalReferenceType,

View File

@ -1,4 +1,4 @@
use crate::{dbg, println, ExternRef, ExternalReferenceType}; use crate::{ExternRef, ExternalReferenceType};
use alloc::boxed::Box; use alloc::boxed::Box;
use core::cell::Cell; use core::cell::Cell;
use core::mem::forget; use core::mem::forget;
@ -20,6 +20,7 @@ where
T: ExternalReferenceType, T: ExternalReferenceType,
T: Clone, T: Clone,
{ {
#[inline]
pub fn from_reference(reference: ExternRef<T>) -> Self { pub fn from_reference(reference: ExternRef<T>) -> Self {
let temporaries = unsafe { &mut TEMPORARIES }; let temporaries = unsafe { &mut TEMPORARIES };
let existing = temporaries.get(&reference.get_internal_index()); let existing = temporaries.get(&reference.get_internal_index());
@ -47,10 +48,16 @@ where
Self { value: ptr } Self { value: ptr }
} }
#[inline]
pub fn value(&self) -> T { pub fn value(&self) -> T {
unsafe { self.value.as_ref().unwrap().value.clone() } unsafe { self.value.as_ref().unwrap().value.clone() }
} }
#[inline]
pub fn value_ref(&self) -> &T {
unsafe { &self.value.as_ref().unwrap().value }
}
pub(crate) fn mark_as_deleted(reference: ExternRef<T>) { pub(crate) fn mark_as_deleted(reference: ExternRef<T>) {
let temporaries = unsafe { &mut TEMPORARIES }; let temporaries = unsafe { &mut TEMPORARIES };
let existing = temporaries.get(&reference.get_internal_index()); let existing = temporaries.get(&reference.get_internal_index());