Adds assist and assurance, fixes

This commit is contained in:
Deukhoofd 2022-09-09 20:09:56 +02:00
parent 05430c5e84
commit 365bdc8aec
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
57 changed files with 533 additions and 95 deletions

0
.cargo/config.toml Normal file → Executable file
View File

0
.gitignore vendored Normal file → Executable file
View File

0
Cargo.toml Normal file → Executable file
View File

0
gen_7_scripts/Cargo.toml Normal file → Executable file
View File

1
gen_7_scripts/src/lib.rs Normal file → Executable file
View File

@ -14,6 +14,7 @@ use pkmn_lib_interface::set_load_script_fn;
pub mod registered_scripts; pub mod registered_scripts;
pub mod moves; pub mod moves;
pub mod util_scripts; pub mod util_scripts;
pub(crate) mod utils;
#[no_mangle] #[no_mangle]
#[cfg(not(test))] #[cfg(not(test))]

16
gen_7_scripts/src/moves/acrobatics.rs Normal file → Executable file
View File

@ -1,20 +1,16 @@
use crate::script;
use core::any::Any;
use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
pub struct Acrobatics {} script!(Acrobatics, "acrobatics");
impl Acrobatics {
pub const fn get_const_name() -> &'static str {
"acrobatics"
}
}
impl Script for Acrobatics { impl Script for Acrobatics {
fn new() -> Self { fn new() -> Self {
Self {} Self {}
} }
fn get_name() -> &'static str { fn get_name(&self) -> &'static str {
Self::get_const_name() Self::get_const_name()
} }
@ -37,4 +33,8 @@ impl Script for Acrobatics {
} }
} }
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

7
gen_7_scripts/src/moves/acupressure.rs Normal file → Executable file
View File

@ -1,3 +1,4 @@
use core::any::Any;
use core::mem::transmute; use core::mem::transmute;
use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon, Statistic}; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon, Statistic};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -15,7 +16,7 @@ impl Script for Acupressure {
Self {} Self {}
} }
fn get_name() -> &'static str { fn get_name(&self) -> &'static str {
Self::get_const_name() Self::get_const_name()
} }
@ -32,4 +33,8 @@ impl Script for Acupressure {
unsafe { transmute(target.battle().unwrap().random().get_between(1, 6) as u8) }; unsafe { transmute(target.battle().unwrap().random().get_between(1, 6) as u8) };
target.change_stat_boost(rand_stat, 2, false); target.change_stat_boost(rand_stat, 2, false);
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

7
gen_7_scripts/src/moves/after_you.rs Normal file → Executable file
View File

@ -1,3 +1,4 @@
use core::any::Any;
use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -14,7 +15,7 @@ impl Script for AfterYou {
Self {} Self {}
} }
fn get_name() -> &'static str { fn get_name(&self) -> &'static str {
Self::get_const_name() Self::get_const_name()
} }
@ -32,4 +33,8 @@ impl Script for AfterYou {
mv.get_hit_data(&target, hit).fail() mv.get_hit_data(&target, hit).fail()
} }
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

View File

@ -0,0 +1,66 @@
use crate::script;
use alloc::vec::Vec;
use core::any::Any;
use pkmn_lib_interface::app_interface::{MoveData, Party, Pokemon, StringKey, TurnChoice};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
script!(Assist, "assist");
impl Assist {
fn get_party_moves(party: &Party, user: &Pokemon) -> Vec<MoveData> {
let mut possible_moves = Vec::new();
// Iterate over every mon in the party
for mon_index in 0..party.length() {
let mon = party.get_pokemon(mon_index);
if let Some(mon) = mon {
// Ignore moves from the user
if mon == *user {
continue;
}
// Iterate over all moves. We make the assumption of 4 moves.
for move_index in 0..4 {
let mv = mon.get_learned_move(move_index);
if let Some(mv) = mv {
// Make sure we can copy the move, otherwise add it as possible move.
if crate::utils::copyable_moves::can_copy_move(&mv.move_data()) {
possible_moves.push(mv.move_data())
}
}
}
}
}
possible_moves
}
}
impl Script for Assist {
fn new() -> Self {
Self {}
}
fn get_name(&self) -> &'static str {
Self::get_const_name()
}
fn get_capabilities(&self) -> &[ScriptCapabilities] {
&[ScriptCapabilities::ChangeMove]
}
fn change_move(&self, choice: TurnChoice, move_name: &mut StringKey) {
let user = choice.user();
let battle = user.battle().unwrap();
let party = battle.find_party_for_pokemon(&user).unwrap().party();
let possible_moves = Self::get_party_moves(&party, &user);
if possible_moves.len() == 0 {
choice.fail();
return;
}
let random = battle.random().get_max(possible_moves.len() as i32);
*move_name = possible_moves[random as usize].name();
}
fn as_any(&self) -> &dyn Any {
self
}
}

View File

@ -0,0 +1,109 @@
use crate::script;
use alloc::boxed::Box;
use core::any::Any;
use core::sync::atomic::{AtomicBool, Ordering};
use pkmn_lib_interface::app_interface::{
BattleSide, DamageSource, ExecutingMove, Pokemon, TurnChoice,
};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
script!(Assurance, "assurance");
impl Script for Assurance {
fn new() -> Self {
Self {}
}
fn get_name(&self) -> &'static str {
Self::get_const_name()
}
fn get_capabilities(&self) -> &[ScriptCapabilities] {
&[
ScriptCapabilities::OnBeforeTurn,
ScriptCapabilities::ChangeBasePower,
]
}
fn on_before_turn(&self, choice: TurnChoice) {
if let TurnChoice::Move(data) = &choice {
let side: BattleSide = choice
.user()
.battle()
.unwrap()
.sides()
.get(data.target_side() as u32)
.unwrap();
side.add_volatile(Box::new(AssuranceData {
for_position: data.target_index(),
has_hit: AtomicBool::new(false),
}));
}
}
fn change_base_power(
&self,
_move: ExecutingMove,
target: Pokemon,
_hit: u8,
base_power: &mut u8,
) {
if let Some(s) = target
.battle_side()
.get_volatile::<AssuranceData>(AssuranceData::get_const_name())
{
if s.has_hit.load(Ordering::Relaxed) {
*base_power *= 2;
}
}
}
fn as_any(&self) -> &dyn Any {
self
}
}
script!(
AssuranceData,
"assurance_data",
for_position: u8,
has_hit: AtomicBool
);
impl Script for AssuranceData {
fn new() -> Self {
Self {
for_position: 0,
has_hit: AtomicBool::new(false),
}
}
fn get_name(&self) -> &'static str {
Self::get_const_name()
}
fn get_capabilities(&self) -> &[ScriptCapabilities] {
&[ScriptCapabilities::OnEndTurn, ScriptCapabilities::OnDamage]
}
fn on_end_turn(&self) {
let side: BattleSide = self.get_owner().unwrap();
side.remove_volatile(self);
}
fn on_damage(
&self,
pokemon: Pokemon,
_source: DamageSource,
_old_health: u32,
_new_health: u32,
) {
if pokemon.battle_side_index() == self.for_position {
self.has_hit.store(true, Ordering::Relaxed);
}
}
fn as_any(&self) -> &dyn Any {
self
}
}

2
gen_7_scripts/src/moves/mod.rs Normal file → Executable file
View File

@ -2,3 +2,5 @@ pub mod acrobatics;
pub mod acupressure; pub mod acupressure;
pub mod after_you; pub mod after_you;
pub mod multi_hit_move; pub mod multi_hit_move;
pub mod assist;
pub mod assurance;

7
gen_7_scripts/src/moves/multi_hit_move.rs Normal file → Executable file
View File

@ -1,3 +1,4 @@
use core::any::Any;
use pkmn_lib_interface::app_interface::TurnChoice; use pkmn_lib_interface::app_interface::TurnChoice;
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -14,7 +15,7 @@ impl Script for MultiHitMove {
Self {} Self {}
} }
fn get_name() -> &'static str { fn get_name(&self) -> &'static str {
Self::get_const_name() Self::get_const_name()
} }
@ -34,4 +35,8 @@ impl Script for MultiHitMove {
_ => *number_of_hits, _ => *number_of_hits,
} }
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

2
gen_7_scripts/src/registered_scripts.rs Normal file → Executable file
View File

@ -29,6 +29,8 @@ pub fn get_script(category: ScriptCategory, name: &StringKey) -> Option<Box<dyn
acrobatics::Acrobatics, acrobatics::Acrobatics,
acupressure::Acupressure, acupressure::Acupressure,
after_you::AfterYou, after_you::AfterYou,
assist::Assist,
assurance::Assurance,
multi_hit_move::MultiHitMove, multi_hit_move::MultiHitMove,
); );
} }

7
gen_7_scripts/src/util_scripts/force_effect_trigger.rs Normal file → Executable file
View File

@ -1,3 +1,4 @@
use core::any::Any;
use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon};
use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities};
@ -14,7 +15,7 @@ impl Script for ForceEffectTriggerScript {
Self {} Self {}
} }
fn get_name() -> &'static str { fn get_name(&self) -> &'static str {
Self::get_const_name() Self::get_const_name()
} }
@ -32,4 +33,8 @@ impl Script for ForceEffectTriggerScript {
// Set to 50_000% chance. // Set to 50_000% chance.
*chance = 50_000.0; *chance = 50_000.0;
} }
fn as_any(&self) -> &dyn Any {
self
}
} }

0
gen_7_scripts/src/util_scripts/mod.rs Normal file → Executable file
View File

View File

@ -0,0 +1,74 @@
use pkmn_lib_interface::app_interface::{get_hash, MoveData};
macro_rules! non_copyable {
(
$mv:ident,
$($move_name:literal),+
) => {
match $mv.name().hash() {
0
$(
| const { get_hash($move_name) }
)* => false,
_ => true
}
};
}
pub fn can_copy_move(mv: &MoveData) -> bool {
// A list of all moves that cannot be copied. This expands to a match statement, so is fast on
// runtime.
non_copyable!(
mv,
"assist",
"baneful_bunker",
"beak_blast",
"belch",
"bestow",
"bounce",
"celebrate",
"chatter",
"circle_throw",
"copycat",
"counter",
"covet",
"destiny_bond",
"detect",
"dig",
"dive",
"dragon_tail",
"endure",
"feint",
"fly",
"focus_punch",
"follow_me",
"helping_hand",
"hold_hands",
"kings_shield",
"mat_block",
"me_first",
"metronome",
"mimic",
"mirror_coat",
"mirror_move",
"nature_power",
"phantom_force",
"protect",
"rage_powder",
"roar",
"shadow_force",
"shell_trap",
"sketch",
"sky_drop",
"sleep_talk",
"snatch",
"spiky_shield",
"spotlight",
"struggle",
"switcheroo",
"thief",
"transform",
"trick",
"whirlwind"
)
}

25
gen_7_scripts/src/utils/mod.rs Executable file
View File

@ -0,0 +1,25 @@
pub mod copyable_moves;
#[macro_export]
macro_rules! script{
(
$name: ident,
$id: literal
$(
,
$field_name:ident : $field_type:ty
)*
) => {
pub struct $name {
$(
$field_name: $field_type,
)*
}
impl $name {
pub const fn get_const_name() -> &'static str {
$id
}
}
}
}

0
pkmn_lib_interface/Cargo.toml Normal file → Executable file
View File

0
pkmn_lib_interface/gen7_scripts.wat Normal file → Executable file
View File

View File

@ -50,6 +50,12 @@ impl Battle {
pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> { pub fn get_pokemon(&self, side: u8, index: u8) -> Option<Pokemon> {
unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() } unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() }
} }
pub fn find_party_for_pokemon(&self, pokemon: &Pokemon) -> Option<BattleParty> {
unsafe {
battle_find_party_for_pokemon(self.inner.reference, pokemon.reference()).get_value()
}
}
} }
wasm_value_getters! { wasm_value_getters! {
@ -80,4 +86,8 @@ extern "wasm" {
fn battle_get_random(r: ExternRef<Battle>) -> ExternRef<BattleRandom>; fn battle_get_random(r: ExternRef<Battle>) -> ExternRef<BattleRandom>;
fn battle_get_choice_queue(r: ExternRef<Battle>) -> ExternRef<ChoiceQueue>; fn battle_get_choice_queue(r: ExternRef<Battle>) -> ExternRef<ChoiceQueue>;
fn battle_get_pokemon(r: ExternRef<Battle>, side: u8, index: u8) -> ExternRef<Pokemon>; fn battle_get_pokemon(r: ExternRef<Battle>, side: u8, index: u8) -> ExternRef<Pokemon>;
fn battle_find_party_for_pokemon(
r: ExternRef<Battle>,
mon: ExternRef<Pokemon>,
) -> ExternRef<BattleParty>;
} }

View File

View File

View File

@ -1,10 +1,13 @@
use crate::app_interface::{Battle, Pokemon}; use crate::app_interface::{Battle, Pokemon};
use crate::handling::cacheable::Cacheable;
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable;
use crate::{ use crate::{
cached_value, cached_value_getters, wasm_value_getters, ExternRef, ExternalReferenceType, cached_value, cached_value_getters, wasm_value_getters, ExternRef, ExternalReferenceType,
Script, ScriptPtr,
}; };
use alloc::boxed::Box;
use alloc::rc::Rc; use alloc::rc::Rc;
use cstr_core::{c_char, CString};
struct BattleSideInner { struct BattleSideInner {
reference: ExternRef<BattleSide>, reference: ExternRef<BattleSide>,
@ -41,6 +44,39 @@ impl BattleSide {
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> { pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() } unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() }
} }
#[cfg(not(feature = "mock_data"))]
pub fn add_volatile(&self, script: Box<dyn Script>) -> &dyn Script {
unsafe {
battleside_add_volatile(self.inner.reference, ScriptPtr::new(script))
.val()
.unwrap()
}
}
#[cfg(not(feature = "mock_data"))]
pub fn remove_volatile(&self, script: &dyn Script) {
unsafe {
let name = CString::new(script.get_name()).unwrap();
battleside_remove_volatile(self.inner.reference, name.into_raw());
}
}
#[cfg(not(feature = "mock_data"))]
pub fn get_volatile<T>(&self, script_name: &str) -> Option<&T>
where
T: Script + 'static,
{
unsafe {
let script_name = CString::new(script_name).unwrap();
let s = battleside_get_volatile(self.inner.reference, script_name.into_raw()).val();
if let Some(s) = s {
Some(s.as_any().downcast_ref().unwrap())
} else {
None
}
}
}
} }
wasm_value_getters! { wasm_value_getters! {
@ -64,4 +100,10 @@ extern "wasm" {
fn battleside_get_pokemon_per_side(r: ExternRef<BattleSide>) -> u8; fn battleside_get_pokemon_per_side(r: ExternRef<BattleSide>) -> u8;
fn battleside_get_battle(r: ExternRef<BattleSide>) -> ExternRef<Battle>; fn battleside_get_battle(r: ExternRef<BattleSide>) -> ExternRef<Battle>;
fn battleside_get_pokemon(r: ExternRef<BattleSide>, index: usize) -> ExternRef<Pokemon>; fn battleside_get_pokemon(r: ExternRef<BattleSide>, index: usize) -> ExternRef<Pokemon>;
fn battleside_add_volatile_by_name(r: ExternRef<BattleSide>, name: *const c_char) -> ScriptPtr;
fn battleside_add_volatile(r: ExternRef<BattleSide>, script: ScriptPtr) -> ScriptPtr;
fn battleside_has_volatile(r: ExternRef<BattleSide>, name: *const c_char) -> bool;
fn battleside_remove_volatile(r: ExternRef<BattleSide>, name: *const c_char);
fn battleside_get_volatile(r: ExternRef<BattleSide>, name: *const c_char) -> ScriptPtr;
} }

View File

View File

View File

View File

View File

View File

@ -15,6 +15,10 @@ impl Party {
pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> { pub fn get_pokemon(&self, index: usize) -> Option<Pokemon> {
unsafe { party_get_pokemon(self.reference, index).get_value() } unsafe { party_get_pokemon(self.reference, index).get_value() }
} }
pub fn length(&self) -> usize {
unsafe { party_get_length(self.reference) }
}
} }
impl ExternalReferenceType for Party { impl ExternalReferenceType for Party {
@ -26,4 +30,5 @@ impl ExternalReferenceType for Party {
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
fn party_get_pokemon(r: ExternRef<Party>, index: usize) -> ExternRef<Pokemon>; fn party_get_pokemon(r: ExternRef<Party>, index: usize) -> ExternRef<Pokemon>;
fn party_get_length(r: ExternRef<Party>) -> usize;
} }

View File

@ -1,7 +1,7 @@
use crate::app_interface::ability::{Ability, AbilityIndex}; use crate::app_interface::ability::{Ability, AbilityIndex};
use crate::app_interface::{ use crate::app_interface::{
Battle, ClampedStatisticSet, Form, Gender, Item, LearnedMove, LevelInt, Nature, Species, Battle, BattleSide, ClampedStatisticSet, Form, Gender, Item, LearnedMove, LevelInt, Nature,
Statistic, StatisticSet, Species, Statistic, StatisticSet,
}; };
use crate::handling::cached_value::CachedValue; use crate::handling::cached_value::CachedValue;
use crate::handling::Cacheable; use crate::handling::Cacheable;
@ -169,6 +169,15 @@ impl Pokemon {
pub fn heal(&self, amount: u32, allow_revive: bool) -> bool { pub fn heal(&self, amount: u32, allow_revive: bool) -> bool {
unsafe { pokemon_heal(self.inner.reference, amount, allow_revive) } unsafe { pokemon_heal(self.inner.reference, amount, allow_revive) }
} }
#[cfg(not(feature = "mock_data"))]
pub fn battle_side(&self) -> BattleSide {
self.battle()
.unwrap()
.sides()
.get(self.battle_side_index() as u32)
.unwrap()
}
} }
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]

View File

View File

0
pkmn_lib_interface/src/app_interface/list.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/app_interface/mod.rs Normal file → Executable file
View File

View File

View File

View File

View File

View File

View File

View File

View File

0
pkmn_lib_interface/src/app_interface/string_key.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/handling/cacheable.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/handling/cached_value.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/handling/capabilities.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/handling/extern_ref.rs Normal file → Executable file
View File

0
pkmn_lib_interface/src/handling/ffi_array.rs Normal file → Executable file
View File

4
pkmn_lib_interface/src/handling/mod.rs Normal file → Executable file
View File

@ -71,7 +71,7 @@ macro_rules! wasm_optional_reference_getters {
$v fn $name(&self) -> Option<$type> { $v fn $name(&self) -> Option<$type> {
paste::paste!{ paste::paste!{
unsafe{ unsafe{
[<$base_type:snake get_ $name>](self.inner.reference).get_value() [<$base_type:snake _get_ $name>](self.inner.reference).get_value()
} }
} }
} }
@ -83,7 +83,7 @@ macro_rules! wasm_optional_reference_getters {
extern "wasm" { extern "wasm" {
$( $(
paste::paste!{ paste::paste!{
fn [<$base_type:snake get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>; fn [<$base_type:snake _get_ $name>](r: ExternRef<$base_type>) -> ExternRef<$type>;
} }
)* )*
} }

31
pkmn_lib_interface/src/handling/script.rs Normal file → Executable file
View File

@ -3,17 +3,15 @@ use crate::app_interface::{
Battle, DamageSource, DynamicLibrary, EffectParameter, ExecutingMove, Item, Pokemon, Statistic, Battle, DamageSource, DynamicLibrary, EffectParameter, ExecutingMove, Item, Pokemon, Statistic,
}; };
use crate::handling::ScriptCapabilities; use crate::handling::ScriptCapabilities;
use crate::{ExternRef, StringKey, TurnChoice, TypeIdentifier}; use crate::{ExternRef, ExternalReferenceType, ScriptPtr, StringKey, TurnChoice, TypeIdentifier};
use core::ffi::c_void; use core::any::Any;
use core::fmt::Debug; use core::fmt::Debug;
pub trait Script { pub trait Script {
fn new() -> Self fn new() -> Self
where where
Self: Sized; Self: Sized;
fn get_name() -> &'static str fn get_name(&self) -> &'static str;
where
Self: Sized;
fn get_capabilities(&self) -> &[ScriptCapabilities]; fn get_capabilities(&self) -> &[ScriptCapabilities];
/// This function is ran when a volatile effect is added while that volatile effect already is /// This function is ran when a volatile effect is added while that volatile effect already is
@ -343,6 +341,21 @@ pub trait Script {
/// rate of this attempt. Pokeball modifier effects should be implemented here, as well as for /// rate of this attempt. Pokeball modifier effects should be implemented here, as well as for
/// example status effects that change capture rates. /// example status effects that change capture rates.
fn change_capture_rate_bonus(&self, _target: Pokemon, _pokeball: Item, _modifier: &mut u8) {} fn change_capture_rate_bonus(&self, _target: Pokemon, _pokeball: Item, _modifier: &mut u8) {}
fn as_any(&self) -> &dyn Any;
#[cfg(not(feature = "mock_data"))]
fn get_owner<T>(&self) -> Option<T>
where
T: ExternalReferenceType,
Self: Sized,
{
unsafe {
script_get_owner(ScriptPtr::from_existing(self))
.cast::<T>()
.get_value()
}
}
} }
impl Debug for dyn Script { impl Debug for dyn Script {
@ -351,13 +364,7 @@ impl Debug for dyn Script {
} }
} }
pub trait OwnerGetter {
fn get_owner<T>(&self) -> Option<ExternRef<T>> {
unimplemented!()
}
}
#[cfg(not(feature = "mock_data"))] #[cfg(not(feature = "mock_data"))]
extern "wasm" { extern "wasm" {
pub fn get_script_owner(pointer: *const c_void) -> ExternRef<u8>; fn script_get_owner(pointer: ScriptPtr) -> ExternRef<u8>;
} }

0
pkmn_lib_interface/src/handling/temporary.rs Normal file → Executable file
View File

195
pkmn_lib_interface/src/lib.rs Normal file → Executable file
View File

@ -36,6 +36,9 @@ use crate::handling::ffi_array::FFIArray;
use crate::handling::ScriptCapabilities; use crate::handling::ScriptCapabilities;
use crate::handling::{Script, ScriptCategory}; use crate::handling::{Script, ScriptCategory};
use alloc::boxed::Box; use alloc::boxed::Box;
use core::sync::atomic::{AtomicU32, Ordering};
use cstr_core::{c_char, CString};
use hashbrown::HashMap;
#[macro_use] #[macro_use]
#[allow(dead_code)] #[allow(dead_code)]
@ -71,22 +74,76 @@ macro_rules! exported_functions {
}; };
} }
static mut script_ptr_cache: Option<hashbrown::HashMap<*const dyn Script, u32>> = None;
static mut script_ptr_reverse_cache: Option<hashbrown::HashMap<u32, Box<dyn Script>>> = None;
static mut script_index_counter: AtomicU32 = AtomicU32::new(1);
#[repr(C)] #[repr(C)]
pub struct ScriptPtr { pub struct ScriptPtr {
ptr: *const Box<dyn Script>, index: u32,
} }
impl ScriptPtr { impl ScriptPtr {
pub fn new(ptr: *const Box<dyn Script>) -> Self { fn get_cache<'a>() -> &'a mut HashMap<*const dyn Script, u32> {
Self { ptr } unsafe {
if let None = script_ptr_cache {
script_ptr_cache = Some(hashbrown::HashMap::new());
}
let cache = script_ptr_cache.as_mut().unwrap();
cache
}
} }
pub fn val(&self) -> &dyn Script { fn get_reverse_cache<'a>() -> &'a mut HashMap<u32, Box<dyn Script>> {
unsafe { self.ptr.as_ref().unwrap().as_ref() } unsafe {
if let None = script_ptr_reverse_cache {
script_ptr_reverse_cache = Some(hashbrown::HashMap::new());
}
let cache = script_ptr_reverse_cache.as_mut().unwrap();
cache
}
} }
pub fn ptr(&self) -> *const Box<dyn Script> { pub fn from_existing(ptr: &dyn Script) -> Self {
self.ptr let cache = Self::get_cache();
let index = *cache.get(&(ptr as *const dyn Script)).unwrap();
Self { index }
}
pub fn new(ptr: Box<dyn Script>) -> Self {
unsafe {
let cache = Self::get_cache();
let mut index = cache.get(&(ptr.as_ref() as *const dyn Script)).cloned();
if index.is_none() {
index = Some(script_index_counter.fetch_add(1, Ordering::SeqCst));
let reverse_cache = Self::get_reverse_cache();
reverse_cache.insert(index.unwrap(), ptr);
let v = reverse_cache.get(&index.unwrap()).unwrap();
cache.insert(v.as_ref(), index.unwrap());
}
Self {
index: index.unwrap(),
}
}
}
pub fn null() -> Self {
Self { index: 0 }
}
pub fn val<'a, 'b>(&'a self) -> Option<&'b dyn Script> {
unsafe {
if self.index == 0 {
return None;
}
let cache = Self::get_reverse_cache();
if let Some(c) = cache.get(&self.index) {
Some(c.as_ref())
} else {
None
}
}
} }
} }
@ -96,10 +153,10 @@ fn load_script(category: ScriptCategory, name: ExternRef<StringKey>) -> ScriptPt
let name_c = StringKey::new(name); let name_c = StringKey::new(name);
let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c); let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c);
if boxed_script.is_none() { if boxed_script.is_none() {
return ScriptPtr::new(core::ptr::null()); return ScriptPtr::null();
} }
let b = Box::new(boxed_script.unwrap()); let b = boxed_script.unwrap();
ScriptPtr::new(Box::into_raw(b)) ScriptPtr::new(b)
} }
fn destroy_script(script: *mut Box<dyn Script>) { fn destroy_script(script: *mut Box<dyn Script>) {
@ -109,17 +166,23 @@ fn destroy_script(script: *mut Box<dyn Script>) {
drop(boxed_script); drop(boxed_script);
} }
fn script_get_name(script: ScriptPtr) -> *mut c_char {
let c = script.val().unwrap().get_name();
CString::new(c).unwrap().into_raw()
}
fn get_script_capabilities(script: ScriptPtr) -> FFIArray<ScriptCapabilities> { fn get_script_capabilities(script: ScriptPtr) -> FFIArray<ScriptCapabilities> {
let c = script.val().get_capabilities(); let c = script.val().unwrap().get_capabilities();
FFIArray::new(c) FFIArray::new(c)
} }
fn script_stack(script: ScriptPtr) { fn script_stack(script: ScriptPtr) {
script.val().stack(); script.val().unwrap().stack();
} }
fn script_on_remove(script: ScriptPtr) { fn script_on_remove(script: ScriptPtr) {
script.val().on_remove(); script.val().unwrap().on_remove();
} }
fn script_on_initialize( fn script_on_initialize(
@ -128,14 +191,14 @@ fn script_on_initialize(
parameters: VecExternRef<EffectParameter>, parameters: VecExternRef<EffectParameter>,
) { ) {
let parameters = ImmutableList::from_ref(parameters); let parameters = ImmutableList::from_ref(parameters);
script.val().on_initialize(&library.not_null(), Some(parameters)); script.val().unwrap().on_initialize(&library.not_null(), Some(parameters));
} }
fn script_on_before_turn( fn script_on_before_turn(
script: ScriptPtr, script: ScriptPtr,
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
) { ) {
script.val().on_before_turn(choice.not_null()) script.val().unwrap().on_before_turn(choice.not_null())
} }
fn script_change_speed( fn script_change_speed(
@ -143,7 +206,7 @@ fn script_change_speed(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
speed: *mut u32, speed: *mut u32,
) { ) {
script.val().change_speed(choice.not_null(), speed.as_mut().unwrap()) script.val().unwrap().change_speed(choice.not_null(), speed.as_mut().unwrap())
} }
fn script_change_priority( fn script_change_priority(
@ -151,7 +214,7 @@ fn script_change_priority(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
priority: *mut i8, priority: *mut i8,
) { ) {
script.val().change_priority(choice.not_null(), priority.as_mut().unwrap()) script.val().unwrap().change_priority(choice.not_null(), priority.as_mut().unwrap())
} }
fn script_change_move( fn script_change_move(
@ -161,7 +224,7 @@ fn script_change_move(
) { ) {
let old = mv.as_ref().unwrap().not_null(); let old = mv.as_ref().unwrap().not_null();
let mut new = old.clone(); let mut new = old.clone();
script.val().change_move(choice.not_null(), &mut new); script.val().unwrap().change_move(choice.not_null(), &mut new);
if old != new { if old != new {
*mv = new.ptr(); *mv = new.ptr();
} }
@ -172,7 +235,7 @@ fn script_change_number_of_hits(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
out: *mut u8, out: *mut u8,
) { ) {
script.val().change_number_of_hits(choice.not_null(), out.as_mut().unwrap()); script.val().unwrap().change_number_of_hits(choice.not_null(), out.as_mut().unwrap());
} }
fn script_prevent_move( fn script_prevent_move(
@ -180,7 +243,7 @@ fn script_prevent_move(
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_move(mv.not_null(), out.as_mut().unwrap()); script.val().unwrap().prevent_move(mv.not_null(), out.as_mut().unwrap());
} }
fn script_fail_move( fn script_fail_move(
@ -188,7 +251,7 @@ fn script_fail_move(
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().fail_move(mv.not_null(), out.as_mut().unwrap()); script.val().unwrap().fail_move(mv.not_null(), out.as_mut().unwrap());
} }
fn script_stop_before_move( fn script_stop_before_move(
@ -196,14 +259,14 @@ fn script_stop_before_move(
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().stop_before_move(mv.not_null(), out.as_mut().unwrap()); script.val().unwrap().stop_before_move(mv.not_null(), out.as_mut().unwrap());
} }
fn script_on_before_move( fn script_on_before_move(
script: ScriptPtr, script: ScriptPtr,
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
) { ) {
script.val().on_before_move(mv.not_null()); script.val().unwrap().on_before_move(mv.not_null());
} }
fn script_fail_incoming_move( fn script_fail_incoming_move(
@ -212,7 +275,7 @@ fn script_fail_incoming_move(
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().fail_incoming_move(mv.not_null(), target.not_null(), out.as_mut().unwrap()); script.val().unwrap().fail_incoming_move(mv.not_null(), target.not_null(), out.as_mut().unwrap());
} }
fn script_is_invulnerable( fn script_is_invulnerable(
@ -221,7 +284,7 @@ fn script_is_invulnerable(
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().is_invulnerable(mv.not_null(), target.not_null(), out.as_mut().unwrap()); script.val().unwrap().is_invulnerable(mv.not_null(), target.not_null(), out.as_mut().unwrap());
} }
fn script_on_move_miss( fn script_on_move_miss(
@ -229,7 +292,7 @@ fn script_on_move_miss(
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
) { ) {
script.val().on_move_miss(mv.not_null(), target.not_null()); script.val().unwrap().on_move_miss(mv.not_null(), target.not_null());
} }
fn script_change_move_type( fn script_change_move_type(
@ -239,7 +302,7 @@ fn script_change_move_type(
hit: u8, hit: u8,
out: *mut TypeIdentifier, out: *mut TypeIdentifier,
) { ) {
script.val().change_move_type(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_move_type(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_effectiveness( fn script_change_effectiveness(
@ -249,7 +312,7 @@ fn script_change_effectiveness(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_effectiveness(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_effectiveness(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_block_critical( fn script_block_critical(
@ -259,7 +322,7 @@ fn script_block_critical(
hit: u8, hit: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().block_critical(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().block_critical(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_block_incoming_critical( fn script_block_incoming_critical(
@ -269,7 +332,7 @@ fn script_block_incoming_critical(
hit: u8, hit: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().block_incoming_critical(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().block_incoming_critical(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_accuracy( fn script_change_accuracy(
@ -279,7 +342,7 @@ fn script_change_accuracy(
hit: u8, hit: u8,
out: *mut u8, out: *mut u8,
) { ) {
script.val().change_accuracy(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_accuracy(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_critical_stage( fn script_change_critical_stage(
@ -289,7 +352,7 @@ fn script_change_critical_stage(
hit: u8, hit: u8,
out: *mut u8, out: *mut u8,
) { ) {
script.val().change_critical_stage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_critical_stage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_critical_modifier( fn script_change_critical_modifier(
@ -299,7 +362,7 @@ fn script_change_critical_modifier(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_critical_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_critical_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_stab_modifier( fn script_change_stab_modifier(
@ -309,7 +372,7 @@ fn script_change_stab_modifier(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_stab_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_stab_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_base_power( fn script_change_base_power(
@ -319,7 +382,7 @@ fn script_change_base_power(
hit: u8, hit: u8,
out: *mut u8, out: *mut u8,
) { ) {
script.val().change_base_power(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_base_power(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_bypass_defensive_stat_boost( fn script_bypass_defensive_stat_boost(
@ -329,7 +392,7 @@ fn script_bypass_defensive_stat_boost(
hit: u8, hit: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().bypass_defensive_stat_boost(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().bypass_defensive_stat_boost(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_bypass_offensive_stat_boost( fn script_bypass_offensive_stat_boost(
@ -339,7 +402,7 @@ fn script_bypass_offensive_stat_boost(
hit: u8, hit: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().bypass_offensive_stat_boost(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().bypass_offensive_stat_boost(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_defensive_stat_value( fn script_change_defensive_stat_value(
@ -349,7 +412,7 @@ fn script_change_defensive_stat_value(
hit: u8, hit: u8,
out: *mut u32, out: *mut u32,
) { ) {
script.val().change_defensive_stat_value(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_defensive_stat_value(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_offensive_stat_value( fn script_change_offensive_stat_value(
@ -359,7 +422,7 @@ fn script_change_offensive_stat_value(
hit: u8, hit: u8,
out: *mut u32, out: *mut u32,
) { ) {
script.val().change_offensive_stat_value(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_offensive_stat_value(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_damage_stat_modifier( fn script_change_damage_stat_modifier(
@ -369,7 +432,7 @@ fn script_change_damage_stat_modifier(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_damage_stat_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_damage_stat_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_damage_modifier( fn script_change_damage_modifier(
@ -379,7 +442,7 @@ fn script_change_damage_modifier(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_damage_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_damage_modifier(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_damage( fn script_change_damage(
@ -389,7 +452,7 @@ fn script_change_damage(
hit: u8, hit: u8,
out: *mut u32, out: *mut u32,
) { ) {
script.val().change_damage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_damage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_incoming_damage( fn script_change_incoming_damage(
@ -399,7 +462,7 @@ fn script_change_incoming_damage(
hit: u8, hit: u8,
out: *mut u32, out: *mut u32,
) { ) {
script.val().change_incoming_damage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_incoming_damage(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_on_incoming_hit( fn script_on_incoming_hit(
@ -408,7 +471,7 @@ fn script_on_incoming_hit(
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
hit: u8, hit: u8,
) { ) {
script.val().on_incoming_hit(mv.not_null(), target.not_null(), hit); script.val().unwrap().on_incoming_hit(mv.not_null(), target.not_null(), hit);
} }
fn script_on_opponent_faints( fn script_on_opponent_faints(
@ -417,7 +480,7 @@ fn script_on_opponent_faints(
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
hit: u8, hit: u8,
) { ) {
script.val().on_opponent_faints(mv.not_null(), target.not_null(), hit); script.val().unwrap().on_opponent_faints(mv.not_null(), target.not_null(), hit);
} }
fn script_prevent_stat_boost_change( fn script_prevent_stat_boost_change(
@ -428,7 +491,7 @@ fn script_prevent_stat_boost_change(
self_inflicted: u8, self_inflicted: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_stat_boost_change(target.not_null(), stat, amount, self_inflicted == 1, out.as_mut().unwrap()); script.val().unwrap().prevent_stat_boost_change(target.not_null(), stat, amount, self_inflicted == 1, out.as_mut().unwrap());
} }
fn script_prevent_secondary_effect( fn script_prevent_secondary_effect(
@ -438,7 +501,7 @@ fn script_prevent_secondary_effect(
hit: u8, hit: u8,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_secondary_effect(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().prevent_secondary_effect(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_effect_chance( fn script_change_effect_chance(
@ -448,7 +511,7 @@ fn script_change_effect_chance(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_effect_chance(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_effect_chance(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_change_incoming_effect_chance( fn script_change_incoming_effect_chance(
@ -458,7 +521,7 @@ fn script_change_incoming_effect_chance(
hit: u8, hit: u8,
out: *mut f32, out: *mut f32,
) { ) {
script.val().change_incoming_effect_chance(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap()); script.val().unwrap().change_incoming_effect_chance(mv.not_null(), target.not_null(), hit, out.as_mut().unwrap());
} }
fn script_on_secondary_effect( fn script_on_secondary_effect(
@ -467,7 +530,7 @@ fn script_on_secondary_effect(
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
hit: u8, hit: u8,
) { ) {
script.val().on_secondary_effect(mv.not_null(), target.not_null(), hit); script.val().unwrap().on_secondary_effect(mv.not_null(), target.not_null(), hit);
} }
fn script_on_after_hits( fn script_on_after_hits(
@ -475,7 +538,7 @@ fn script_on_after_hits(
mv: ExternRef<ExecutingMove>, mv: ExternRef<ExecutingMove>,
target: ExternRef<Pokemon>, target: ExternRef<Pokemon>,
) { ) {
script.val().on_after_hits(mv.not_null(), target.not_null()); script.val().unwrap().on_after_hits(mv.not_null(), target.not_null());
} }
fn script_prevent_self_switch( fn script_prevent_self_switch(
@ -483,7 +546,7 @@ fn script_prevent_self_switch(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_self_switch(choice.not_null(), out.as_mut().unwrap()); script.val().unwrap().prevent_self_switch(choice.not_null(), out.as_mut().unwrap());
} }
fn script_prevent_opponent_switch( fn script_prevent_opponent_switch(
@ -491,21 +554,21 @@ fn script_prevent_opponent_switch(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_opponent_switch(choice.not_null(), out.as_mut().unwrap()); script.val().unwrap().prevent_opponent_switch(choice.not_null(), out.as_mut().unwrap());
} }
fn script_on_fail( fn script_on_fail(
script: ScriptPtr, script: ScriptPtr,
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
) { ) {
script.val().on_fail(pokemon.not_null()); script.val().unwrap().on_fail(pokemon.not_null());
} }
fn script_on_opponent_fail( fn script_on_opponent_fail(
script: ScriptPtr, script: ScriptPtr,
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
) { ) {
script.val().on_opponent_fail(pokemon.not_null()); script.val().unwrap().on_opponent_fail(pokemon.not_null());
} }
fn script_prevent_self_run_away( fn script_prevent_self_run_away(
@ -513,7 +576,7 @@ fn script_prevent_self_run_away(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_self_run_away(choice.not_null(), out.as_mut().unwrap()); script.val().unwrap().prevent_self_run_away(choice.not_null(), out.as_mut().unwrap());
} }
fn script_prevent_opponent_run_away( fn script_prevent_opponent_run_away(
@ -521,13 +584,13 @@ fn script_prevent_opponent_run_away(
choice: ExternRef<TurnChoice>, choice: ExternRef<TurnChoice>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().prevent_opponent_run_away(choice.not_null(), out.as_mut().unwrap()); script.val().unwrap().prevent_opponent_run_away(choice.not_null(), out.as_mut().unwrap());
} }
fn script_on_end_turn( fn script_on_end_turn(
script: ScriptPtr, script: ScriptPtr,
) { ) {
script.val().on_end_turn(); script.val().unwrap().on_end_turn();
} }
fn script_on_damage( fn script_on_damage(
@ -537,7 +600,7 @@ fn script_on_damage(
old_health: u32, old_health: u32,
new_health: u32, new_health: u32,
) { ) {
script.val().on_damage(pokemon.not_null(), source, old_health, new_health); script.val().unwrap().on_damage(pokemon.not_null(), source, old_health, new_health);
} }
fn script_on_faint( fn script_on_faint(
@ -545,14 +608,14 @@ fn script_on_faint(
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
source: DamageSource, source: DamageSource,
) { ) {
script.val().on_faint(pokemon.not_null(), source); script.val().unwrap().on_faint(pokemon.not_null(), source);
} }
fn script_on_switch_in( fn script_on_switch_in(
script: ScriptPtr, script: ScriptPtr,
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
) { ) {
script.val().on_switch_in(pokemon.not_null()); script.val().unwrap().on_switch_in(pokemon.not_null());
} }
fn script_on_after_held_item_consume( fn script_on_after_held_item_consume(
@ -560,7 +623,7 @@ fn script_on_after_held_item_consume(
pokemon: ExternRef<Pokemon>, pokemon: ExternRef<Pokemon>,
item: ExternRef<Item> item: ExternRef<Item>
) { ) {
script.val().on_after_held_item_consume(pokemon.not_null(), &item.not_null()); script.val().unwrap().on_after_held_item_consume(pokemon.not_null(), &item.not_null());
} }
fn script_change_experience_gained( fn script_change_experience_gained(
@ -569,7 +632,7 @@ fn script_change_experience_gained(
winning_pokemon: ExternRef<Pokemon>, winning_pokemon: ExternRef<Pokemon>,
out: *mut u32 out: *mut u32
) { ) {
script.val().change_experience_gained(fainted_pokemon.not_null(), winning_pokemon.not_null(), out.as_mut().unwrap()); script.val().unwrap().change_experience_gained(fainted_pokemon.not_null(), winning_pokemon.not_null(), out.as_mut().unwrap());
} }
fn script_share_experience( fn script_share_experience(
@ -578,7 +641,7 @@ fn script_share_experience(
winning_pokemon: ExternRef<Pokemon>, winning_pokemon: ExternRef<Pokemon>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().share_experience(fainted_pokemon.not_null(), winning_pokemon.not_null(), out.as_mut().unwrap()); script.val().unwrap().share_experience(fainted_pokemon.not_null(), winning_pokemon.not_null(), out.as_mut().unwrap());
} }
fn script_block_weather( fn script_block_weather(
@ -586,7 +649,7 @@ fn script_block_weather(
battle: ExternRef<Battle>, battle: ExternRef<Battle>,
out: *mut bool, out: *mut bool,
) { ) {
script.val().block_weather(battle.not_null(), out.as_mut().unwrap()); script.val().unwrap().block_weather(battle.not_null(), out.as_mut().unwrap());
} }
fn script_change_capture_rate_bonus( fn script_change_capture_rate_bonus(
@ -595,7 +658,7 @@ fn script_change_capture_rate_bonus(
pokeball: ExternRef<Item>, pokeball: ExternRef<Item>,
out: *mut u8, out: *mut u8,
) { ) {
script.val().change_capture_rate_bonus(target.not_null(), pokeball.not_null(), out.as_mut().unwrap()); script.val().unwrap().change_capture_rate_bonus(target.not_null(), pokeball.not_null(), out.as_mut().unwrap());
} }

3
pkmn_lib_interface/src/utils.rs Normal file → Executable file
View File

@ -37,6 +37,9 @@ pub fn print_raw(s: &[u8]) {
#[macro_export] #[macro_export]
macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } macro_rules! println { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
#[macro_export]
macro_rules! crate_println { ($($args:tt)*) => { crate::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }
#[macro_export] #[macro_export]
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } } macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }