diff --git a/.cargo/config.toml b/.cargo/config.toml old mode 100644 new mode 100755 diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/Cargo.toml b/Cargo.toml old mode 100644 new mode 100755 diff --git a/gen_7_scripts/Cargo.toml b/gen_7_scripts/Cargo.toml old mode 100644 new mode 100755 diff --git a/gen_7_scripts/src/lib.rs b/gen_7_scripts/src/lib.rs old mode 100644 new mode 100755 index 29ae7c8..be04797 --- a/gen_7_scripts/src/lib.rs +++ b/gen_7_scripts/src/lib.rs @@ -14,6 +14,7 @@ use pkmn_lib_interface::set_load_script_fn; pub mod registered_scripts; pub mod moves; pub mod util_scripts; +pub(crate) mod utils; #[no_mangle] #[cfg(not(test))] diff --git a/gen_7_scripts/src/moves/acrobatics.rs b/gen_7_scripts/src/moves/acrobatics.rs old mode 100644 new mode 100755 index 967a6e5..7d75b29 --- a/gen_7_scripts/src/moves/acrobatics.rs +++ b/gen_7_scripts/src/moves/acrobatics.rs @@ -1,20 +1,16 @@ +use crate::script; +use core::any::Any; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; -pub struct Acrobatics {} - -impl Acrobatics { - pub const fn get_const_name() -> &'static str { - "acrobatics" - } -} +script!(Acrobatics, "acrobatics"); impl Script for Acrobatics { fn new() -> Self { Self {} } - fn get_name() -> &'static str { + fn get_name(&self) -> &'static str { Self::get_const_name() } @@ -37,4 +33,8 @@ impl Script for Acrobatics { } } } + + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/gen_7_scripts/src/moves/acupressure.rs b/gen_7_scripts/src/moves/acupressure.rs old mode 100644 new mode 100755 index fa915f8..1dd9d05 --- a/gen_7_scripts/src/moves/acupressure.rs +++ b/gen_7_scripts/src/moves/acupressure.rs @@ -1,3 +1,4 @@ +use core::any::Any; use core::mem::transmute; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon, Statistic}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; @@ -15,7 +16,7 @@ impl Script for Acupressure { Self {} } - fn get_name() -> &'static str { + fn get_name(&self) -> &'static str { Self::get_const_name() } @@ -32,4 +33,8 @@ impl Script for Acupressure { unsafe { transmute(target.battle().unwrap().random().get_between(1, 6) as u8) }; target.change_stat_boost(rand_stat, 2, false); } + + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/gen_7_scripts/src/moves/after_you.rs b/gen_7_scripts/src/moves/after_you.rs old mode 100644 new mode 100755 index 477b59f..f41c48b --- a/gen_7_scripts/src/moves/after_you.rs +++ b/gen_7_scripts/src/moves/after_you.rs @@ -1,3 +1,4 @@ +use core::any::Any; use pkmn_lib_interface::app_interface::{ExecutingMove, Pokemon}; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; @@ -14,7 +15,7 @@ impl Script for AfterYou { Self {} } - fn get_name() -> &'static str { + fn get_name(&self) -> &'static str { Self::get_const_name() } @@ -32,4 +33,8 @@ impl Script for AfterYou { mv.get_hit_data(&target, hit).fail() } } + + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/gen_7_scripts/src/moves/assist.rs b/gen_7_scripts/src/moves/assist.rs new file mode 100755 index 0000000..caa911f --- /dev/null +++ b/gen_7_scripts/src/moves/assist.rs @@ -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 { + 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 + } +} diff --git a/gen_7_scripts/src/moves/assurance.rs b/gen_7_scripts/src/moves/assurance.rs new file mode 100755 index 0000000..26c11b8 --- /dev/null +++ b/gen_7_scripts/src/moves/assurance.rs @@ -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::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 + } +} diff --git a/gen_7_scripts/src/moves/mod.rs b/gen_7_scripts/src/moves/mod.rs old mode 100644 new mode 100755 index afbce0c..b425e98 --- a/gen_7_scripts/src/moves/mod.rs +++ b/gen_7_scripts/src/moves/mod.rs @@ -2,3 +2,5 @@ pub mod acrobatics; pub mod acupressure; pub mod after_you; pub mod multi_hit_move; +pub mod assist; +pub mod assurance; diff --git a/gen_7_scripts/src/moves/multi_hit_move.rs b/gen_7_scripts/src/moves/multi_hit_move.rs old mode 100644 new mode 100755 index 121ec0d..d3d96cb --- a/gen_7_scripts/src/moves/multi_hit_move.rs +++ b/gen_7_scripts/src/moves/multi_hit_move.rs @@ -1,3 +1,4 @@ +use core::any::Any; use pkmn_lib_interface::app_interface::TurnChoice; use pkmn_lib_interface::handling::{Script, ScriptCapabilities}; @@ -14,7 +15,7 @@ impl Script for MultiHitMove { Self {} } - fn get_name() -> &'static str { + fn get_name(&self) -> &'static str { Self::get_const_name() } @@ -34,4 +35,8 @@ impl Script for MultiHitMove { _ => *number_of_hits, } } + + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/gen_7_scripts/src/registered_scripts.rs b/gen_7_scripts/src/registered_scripts.rs old mode 100644 new mode 100755 index 17655f1..f616152 --- a/gen_7_scripts/src/registered_scripts.rs +++ b/gen_7_scripts/src/registered_scripts.rs @@ -29,6 +29,8 @@ pub fn get_script(category: ScriptCategory, name: &StringKey) -> Option &'static str { + fn get_name(&self) -> &'static str { Self::get_const_name() } @@ -32,4 +33,8 @@ impl Script for ForceEffectTriggerScript { // Set to 50_000% chance. *chance = 50_000.0; } + + fn as_any(&self) -> &dyn Any { + self + } } diff --git a/gen_7_scripts/src/util_scripts/mod.rs b/gen_7_scripts/src/util_scripts/mod.rs old mode 100644 new mode 100755 diff --git a/gen_7_scripts/src/utils/copyable_moves.rs b/gen_7_scripts/src/utils/copyable_moves.rs new file mode 100755 index 0000000..822c7ef --- /dev/null +++ b/gen_7_scripts/src/utils/copyable_moves.rs @@ -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" + ) +} diff --git a/gen_7_scripts/src/utils/mod.rs b/gen_7_scripts/src/utils/mod.rs new file mode 100755 index 0000000..3cf6585 --- /dev/null +++ b/gen_7_scripts/src/utils/mod.rs @@ -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 + } + } + } +} diff --git a/pkmn_lib_interface/Cargo.toml b/pkmn_lib_interface/Cargo.toml old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/gen7_scripts.wat b/pkmn_lib_interface/gen7_scripts.wat old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs old mode 100644 new mode 100755 index 9314a70..08ffcc8 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/battle.rs @@ -50,6 +50,12 @@ impl Battle { pub fn get_pokemon(&self, side: u8, index: u8) -> Option { unsafe { battle_get_pokemon(self.inner.reference, side, index).get_value() } } + + pub fn find_party_for_pokemon(&self, pokemon: &Pokemon) -> Option { + unsafe { + battle_find_party_for_pokemon(self.inner.reference, pokemon.reference()).get_value() + } + } } wasm_value_getters! { @@ -80,4 +86,8 @@ extern "wasm" { fn battle_get_random(r: ExternRef) -> ExternRef; fn battle_get_choice_queue(r: ExternRef) -> ExternRef; fn battle_get_pokemon(r: ExternRef, side: u8, index: u8) -> ExternRef; + fn battle_find_party_for_pokemon( + r: ExternRef, + mon: ExternRef, + ) -> ExternRef; } diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_party.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_random.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_random.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_side.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_side.rs old mode 100644 new mode 100755 index f6175e1..c3bcd41 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/battle_side.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/battle_side.rs @@ -1,10 +1,13 @@ use crate::app_interface::{Battle, Pokemon}; +use crate::handling::cacheable::Cacheable; use crate::handling::cached_value::CachedValue; -use crate::handling::Cacheable; use crate::{ cached_value, cached_value_getters, wasm_value_getters, ExternRef, ExternalReferenceType, + Script, ScriptPtr, }; +use alloc::boxed::Box; use alloc::rc::Rc; +use cstr_core::{c_char, CString}; struct BattleSideInner { reference: ExternRef, @@ -41,6 +44,39 @@ impl BattleSide { pub fn get_pokemon(&self, index: usize) -> Option { unsafe { battleside_get_pokemon(self.inner.reference, index).get_value() } } + + #[cfg(not(feature = "mock_data"))] + pub fn add_volatile(&self, script: Box) -> &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(&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! { @@ -64,4 +100,10 @@ extern "wasm" { fn battleside_get_pokemon_per_side(r: ExternRef) -> u8; fn battleside_get_battle(r: ExternRef) -> ExternRef; fn battleside_get_pokemon(r: ExternRef, index: usize) -> ExternRef; + + fn battleside_add_volatile_by_name(r: ExternRef, name: *const c_char) -> ScriptPtr; + fn battleside_add_volatile(r: ExternRef, script: ScriptPtr) -> ScriptPtr; + fn battleside_has_volatile(r: ExternRef, name: *const c_char) -> bool; + fn battleside_remove_volatile(r: ExternRef, name: *const c_char); + fn battleside_get_volatile(r: ExternRef, name: *const c_char) -> ScriptPtr; } diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/choice_queue.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/choice_queue.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/dynamic_library.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/executing_move.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/learned_move.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/mod.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs old mode 100644 new mode 100755 index abd50ea..9c9d4f9 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/party.rs @@ -15,6 +15,10 @@ impl Party { pub fn get_pokemon(&self, index: usize) -> Option { unsafe { party_get_pokemon(self.reference, index).get_value() } } + + pub fn length(&self) -> usize { + unsafe { party_get_length(self.reference) } + } } impl ExternalReferenceType for Party { @@ -26,4 +30,5 @@ impl ExternalReferenceType for Party { #[cfg(not(feature = "mock_data"))] extern "wasm" { fn party_get_pokemon(r: ExternRef, index: usize) -> ExternRef; + fn party_get_length(r: ExternRef) -> usize; } diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs old mode 100644 new mode 100755 index b8ef9f7..6282c97 --- a/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs +++ b/pkmn_lib_interface/src/app_interface/dynamic_data/pokemon.rs @@ -1,7 +1,7 @@ use crate::app_interface::ability::{Ability, AbilityIndex}; use crate::app_interface::{ - Battle, ClampedStatisticSet, Form, Gender, Item, LearnedMove, LevelInt, Nature, Species, - Statistic, StatisticSet, + Battle, BattleSide, ClampedStatisticSet, Form, Gender, Item, LearnedMove, LevelInt, Nature, + Species, Statistic, StatisticSet, }; use crate::handling::cached_value::CachedValue; use crate::handling::Cacheable; @@ -169,6 +169,15 @@ impl Pokemon { pub fn heal(&self, amount: u32, allow_revive: bool) -> bool { 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"))] diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/statistic_set.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/statistic_set.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs b/pkmn_lib_interface/src/app_interface/dynamic_data/turn_choices.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/list.rs b/pkmn_lib_interface/src/app_interface/list.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/mod.rs b/pkmn_lib_interface/src/app_interface/mod.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/ability.rs b/pkmn_lib_interface/src/app_interface/static_data/ability.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/item_library.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/mod.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/move_library.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/move_library.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/species_library.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/species_library.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/data_libraries/type_library.rs b/pkmn_lib_interface/src/app_interface/static_data/data_libraries/type_library.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/effect_parameter.rs b/pkmn_lib_interface/src/app_interface/static_data/effect_parameter.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/item.rs b/pkmn_lib_interface/src/app_interface/static_data/item.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/mod.rs b/pkmn_lib_interface/src/app_interface/static_data/mod.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/move_data.rs b/pkmn_lib_interface/src/app_interface/static_data/move_data.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/nature.rs b/pkmn_lib_interface/src/app_interface/static_data/nature.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/static_data/species.rs b/pkmn_lib_interface/src/app_interface/static_data/species.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/app_interface/string_key.rs b/pkmn_lib_interface/src/app_interface/string_key.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/cacheable.rs b/pkmn_lib_interface/src/handling/cacheable.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/cached_value.rs b/pkmn_lib_interface/src/handling/cached_value.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/capabilities.rs b/pkmn_lib_interface/src/handling/capabilities.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/extern_ref.rs b/pkmn_lib_interface/src/handling/extern_ref.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/ffi_array.rs b/pkmn_lib_interface/src/handling/ffi_array.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/handling/mod.rs b/pkmn_lib_interface/src/handling/mod.rs old mode 100644 new mode 100755 index b22b94f..490e77e --- a/pkmn_lib_interface/src/handling/mod.rs +++ b/pkmn_lib_interface/src/handling/mod.rs @@ -71,7 +71,7 @@ macro_rules! wasm_optional_reference_getters { $v fn $name(&self) -> Option<$type> { paste::paste!{ 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" { $( 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>; } )* } diff --git a/pkmn_lib_interface/src/handling/script.rs b/pkmn_lib_interface/src/handling/script.rs old mode 100644 new mode 100755 index e37c4ee..986eb0b --- a/pkmn_lib_interface/src/handling/script.rs +++ b/pkmn_lib_interface/src/handling/script.rs @@ -3,17 +3,15 @@ use crate::app_interface::{ Battle, DamageSource, DynamicLibrary, EffectParameter, ExecutingMove, Item, Pokemon, Statistic, }; use crate::handling::ScriptCapabilities; -use crate::{ExternRef, StringKey, TurnChoice, TypeIdentifier}; -use core::ffi::c_void; +use crate::{ExternRef, ExternalReferenceType, ScriptPtr, StringKey, TurnChoice, TypeIdentifier}; +use core::any::Any; use core::fmt::Debug; pub trait Script { fn new() -> Self where Self: Sized; - fn get_name() -> &'static str - where - Self: Sized; + fn get_name(&self) -> &'static str; fn get_capabilities(&self) -> &[ScriptCapabilities]; /// 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 /// example status effects that change capture rates. 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(&self) -> Option + where + T: ExternalReferenceType, + Self: Sized, + { + unsafe { + script_get_owner(ScriptPtr::from_existing(self)) + .cast::() + .get_value() + } + } } impl Debug for dyn Script { @@ -351,13 +364,7 @@ impl Debug for dyn Script { } } -pub trait OwnerGetter { - fn get_owner(&self) -> Option> { - unimplemented!() - } -} - #[cfg(not(feature = "mock_data"))] extern "wasm" { - pub fn get_script_owner(pointer: *const c_void) -> ExternRef; + fn script_get_owner(pointer: ScriptPtr) -> ExternRef; } diff --git a/pkmn_lib_interface/src/handling/temporary.rs b/pkmn_lib_interface/src/handling/temporary.rs old mode 100644 new mode 100755 diff --git a/pkmn_lib_interface/src/lib.rs b/pkmn_lib_interface/src/lib.rs old mode 100644 new mode 100755 index 8ee60a4..31fee07 --- a/pkmn_lib_interface/src/lib.rs +++ b/pkmn_lib_interface/src/lib.rs @@ -36,6 +36,9 @@ use crate::handling::ffi_array::FFIArray; use crate::handling::ScriptCapabilities; use crate::handling::{Script, ScriptCategory}; use alloc::boxed::Box; +use core::sync::atomic::{AtomicU32, Ordering}; +use cstr_core::{c_char, CString}; +use hashbrown::HashMap; #[macro_use] #[allow(dead_code)] @@ -71,22 +74,76 @@ macro_rules! exported_functions { }; } +static mut script_ptr_cache: Option> = None; +static mut script_ptr_reverse_cache: Option>> = None; +static mut script_index_counter: AtomicU32 = AtomicU32::new(1); + #[repr(C)] pub struct ScriptPtr { - ptr: *const Box, + index: u32, } impl ScriptPtr { - pub fn new(ptr: *const Box) -> Self { - Self { ptr } + fn get_cache<'a>() -> &'a mut HashMap<*const dyn Script, u32> { + 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 { - unsafe { self.ptr.as_ref().unwrap().as_ref() } + fn get_reverse_cache<'a>() -> &'a mut HashMap> { + 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 { - self.ptr + pub fn from_existing(ptr: &dyn Script) -> Self { + let cache = Self::get_cache(); + let index = *cache.get(&(ptr as *const dyn Script)).unwrap(); + Self { index } + } + + pub fn new(ptr: Box) -> 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) -> ScriptPt let name_c = StringKey::new(name); let boxed_script = unsafe { &LOAD_SCRIPT_FN }.as_ref().unwrap()(category, &name_c); if boxed_script.is_none() { - return ScriptPtr::new(core::ptr::null()); + return ScriptPtr::null(); } - let b = Box::new(boxed_script.unwrap()); - ScriptPtr::new(Box::into_raw(b)) + let b = boxed_script.unwrap(); + ScriptPtr::new(b) } fn destroy_script(script: *mut Box) { @@ -109,17 +166,23 @@ fn destroy_script(script: *mut Box) { 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 { - let c = script.val().get_capabilities(); + let c = script.val().unwrap().get_capabilities(); FFIArray::new(c) } fn script_stack(script: ScriptPtr) { - script.val().stack(); + script.val().unwrap().stack(); } fn script_on_remove(script: ScriptPtr) { - script.val().on_remove(); + script.val().unwrap().on_remove(); } fn script_on_initialize( @@ -128,14 +191,14 @@ fn script_on_initialize( parameters: VecExternRef, ) { 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( script: ScriptPtr, choice: ExternRef, ) { - script.val().on_before_turn(choice.not_null()) + script.val().unwrap().on_before_turn(choice.not_null()) } fn script_change_speed( @@ -143,7 +206,7 @@ fn script_change_speed( choice: ExternRef, 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( @@ -151,7 +214,7 @@ fn script_change_priority( choice: ExternRef, 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( @@ -161,7 +224,7 @@ fn script_change_move( ) { let old = mv.as_ref().unwrap().not_null(); 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 { *mv = new.ptr(); } @@ -172,7 +235,7 @@ fn script_change_number_of_hits( choice: ExternRef, 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( @@ -180,7 +243,7 @@ fn script_prevent_move( mv: ExternRef, 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( @@ -188,7 +251,7 @@ fn script_fail_move( mv: ExternRef, 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( @@ -196,14 +259,14 @@ fn script_stop_before_move( mv: ExternRef, 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( script: ScriptPtr, mv: ExternRef, ) { - script.val().on_before_move(mv.not_null()); + script.val().unwrap().on_before_move(mv.not_null()); } fn script_fail_incoming_move( @@ -212,7 +275,7 @@ fn script_fail_incoming_move( target: ExternRef, 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( @@ -221,7 +284,7 @@ fn script_is_invulnerable( target: ExternRef, 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( @@ -229,7 +292,7 @@ fn script_on_move_miss( mv: ExternRef, target: ExternRef, ) { - 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( @@ -239,7 +302,7 @@ fn script_change_move_type( hit: u8, 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( @@ -249,7 +312,7 @@ fn script_change_effectiveness( hit: u8, 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( @@ -259,7 +322,7 @@ fn script_block_critical( hit: u8, 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( @@ -269,7 +332,7 @@ fn script_block_incoming_critical( hit: u8, 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( @@ -279,7 +342,7 @@ fn script_change_accuracy( hit: 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( @@ -289,7 +352,7 @@ fn script_change_critical_stage( hit: 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( @@ -299,7 +362,7 @@ fn script_change_critical_modifier( hit: u8, 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( @@ -309,7 +372,7 @@ fn script_change_stab_modifier( hit: u8, 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( @@ -319,7 +382,7 @@ fn script_change_base_power( hit: 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( @@ -329,7 +392,7 @@ fn script_bypass_defensive_stat_boost( hit: u8, 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( @@ -339,7 +402,7 @@ fn script_bypass_offensive_stat_boost( hit: u8, 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( @@ -349,7 +412,7 @@ fn script_change_defensive_stat_value( hit: u8, 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( @@ -359,7 +422,7 @@ fn script_change_offensive_stat_value( hit: u8, 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( @@ -369,7 +432,7 @@ fn script_change_damage_stat_modifier( hit: u8, 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( @@ -379,7 +442,7 @@ fn script_change_damage_modifier( hit: u8, 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( @@ -389,7 +452,7 @@ fn script_change_damage( hit: u8, 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( @@ -399,7 +462,7 @@ fn script_change_incoming_damage( hit: u8, 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( @@ -408,7 +471,7 @@ fn script_on_incoming_hit( target: ExternRef, 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( @@ -417,7 +480,7 @@ fn script_on_opponent_faints( target: ExternRef, 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( @@ -428,7 +491,7 @@ fn script_prevent_stat_boost_change( self_inflicted: u8, 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( @@ -438,7 +501,7 @@ fn script_prevent_secondary_effect( hit: u8, 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( @@ -448,7 +511,7 @@ fn script_change_effect_chance( hit: u8, 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( @@ -458,7 +521,7 @@ fn script_change_incoming_effect_chance( hit: u8, 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( @@ -467,7 +530,7 @@ fn script_on_secondary_effect( target: ExternRef, 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( @@ -475,7 +538,7 @@ fn script_on_after_hits( mv: ExternRef, target: ExternRef, ) { - 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( @@ -483,7 +546,7 @@ fn script_prevent_self_switch( choice: ExternRef, 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( @@ -491,21 +554,21 @@ fn script_prevent_opponent_switch( choice: ExternRef, 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( script: ScriptPtr, pokemon: ExternRef, ) { - script.val().on_fail(pokemon.not_null()); + script.val().unwrap().on_fail(pokemon.not_null()); } fn script_on_opponent_fail( script: ScriptPtr, pokemon: ExternRef, ) { - script.val().on_opponent_fail(pokemon.not_null()); + script.val().unwrap().on_opponent_fail(pokemon.not_null()); } fn script_prevent_self_run_away( @@ -513,7 +576,7 @@ fn script_prevent_self_run_away( choice: ExternRef, 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( @@ -521,13 +584,13 @@ fn script_prevent_opponent_run_away( choice: ExternRef, 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( script: ScriptPtr, ) { - script.val().on_end_turn(); + script.val().unwrap().on_end_turn(); } fn script_on_damage( @@ -537,7 +600,7 @@ fn script_on_damage( old_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( @@ -545,14 +608,14 @@ fn script_on_faint( pokemon: ExternRef, source: DamageSource, ) { - script.val().on_faint(pokemon.not_null(), source); + script.val().unwrap().on_faint(pokemon.not_null(), source); } fn script_on_switch_in( script: ScriptPtr, pokemon: ExternRef, ) { - script.val().on_switch_in(pokemon.not_null()); + script.val().unwrap().on_switch_in(pokemon.not_null()); } fn script_on_after_held_item_consume( @@ -560,7 +623,7 @@ fn script_on_after_held_item_consume( pokemon: ExternRef, item: ExternRef ) { - 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( @@ -569,7 +632,7 @@ fn script_change_experience_gained( winning_pokemon: ExternRef, 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( @@ -578,7 +641,7 @@ fn script_share_experience( winning_pokemon: ExternRef, 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( @@ -586,7 +649,7 @@ fn script_block_weather( battle: ExternRef, 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( @@ -595,7 +658,7 @@ fn script_change_capture_rate_bonus( pokeball: ExternRef, 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()); } diff --git a/pkmn_lib_interface/src/utils.rs b/pkmn_lib_interface/src/utils.rs old mode 100644 new mode 100755 index 44971bd..9ca696a --- a/pkmn_lib_interface/src/utils.rs +++ b/pkmn_lib_interface/src/utils.rs @@ -37,6 +37,9 @@ pub fn print_raw(s: &[u8]) { #[macro_export] 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] #[cfg(debug_assertions)] macro_rules! dbg { ($($args:tt)*) => { pkmn_lib_interface::utils::print_raw(alloc::format!($($args)*).as_bytes()); } }