Implements event batch id to events to display multiple events at the same time
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-06-30 11:32:00 +02:00
parent 0163c7a105
commit bc9b3ed964
11 changed files with 217 additions and 106 deletions

View File

@@ -1,22 +1,25 @@
use crate::dynamic_data::{Event, Pokemon};
use crate::ffi::ExternPointer;
use crate::dynamic_data::{EventData, Pokemon};
use crate::ffi::ffi_handle::FromFFIHandle;
use crate::ffi::FFIHandle;
use std::ops::Deref;
use std::sync::Arc;
/// The kind of the event.
#[no_mangle]
extern "C" fn event_kind(ptr: ExternPointer<Event>) -> u8 {
match ptr.as_ref() {
Event::Switch { .. } => 1,
Event::Swap { .. } => 2,
Event::SpeciesChange { .. } => 3,
Event::FormChange { .. } => 4,
Event::Damage { .. } => 5,
Event::Heal { .. } => 6,
Event::Faint { .. } => 7,
Event::MoveUse { .. } => 8,
Event::Miss { .. } => 9,
Event::EndTurn => 10,
Event::StatBoostChange { .. } => 11,
extern "C" fn event_kind(ptr: FFIHandle<Arc<EventData>>) -> u8 {
match ptr.from_ffi_handle().deref() {
EventData::Switch { .. } => 1,
EventData::Swap { .. } => 2,
EventData::SpeciesChange { .. } => 3,
EventData::FormChange { .. } => 4,
EventData::Damage { .. } => 5,
EventData::Heal { .. } => 6,
EventData::Faint { .. } => 7,
EventData::MoveUse { .. } => 8,
EventData::Miss { .. } => 9,
EventData::EndTurn => 10,
EventData::StatBoostChange { .. } => 11,
EventData::MoveHit { .. } => 12,
}
}
@@ -43,12 +46,12 @@ macro_rules! event_ffi {
}
/// The getter for the data for the relevant event.
#[no_mangle]
extern "C" fn [<event_ $event_name:snake _data>](ptr: ExternPointer<Event>) -> [<$event_name Data>] {
if let Event::$event_name {
extern "C" fn [<event_ $event_name:snake _data>](ptr: FFIHandle<Arc<EventData>>) -> [<$event_name Data>] {
if let EventData::$event_name {
$(
$par_name,
)+
} = ptr.as_ref()
} = ptr.from_ffi_handle().deref()
{
$ctor
}

View File

@@ -1,25 +1,29 @@
use crate::dynamic_data::Event;
use crate::dynamic_data::{EventBatchId, EventData};
use crate::ffi::ffi_handle::FFIHandle;
use std::sync::Arc;
/// Wrapper class for easier use of an external function pointer.
#[repr(C)]
pub(super) struct NativeEventHook {
/// The actual C function to be called.
f: extern "C" fn(*const Event<'_>),
/// The actual C function to be called. Note that we pass the batch id as a pair of u64s. This
/// is because u128 does not have a stable ABI yet.
f: extern "C" fn(FFIHandle<Arc<EventData>>, u64, u64),
}
impl NativeEventHook {
/// Calls the actual wrapped function in the correct format.
fn call_self(&self, b: &&Event<'_>) {
(self.f)(*b as *const Event)
fn call_self(&self, b: Arc<EventData>, batch_id: EventBatchId) {
let batch_id = batch_id.as_u64_pair();
(self.f)(FFIHandle::get_handle(b.into()), batch_id.0, batch_id.1);
}
}
/// A tuple with the arguments of the event hook function
type EventHookArgs<'a, 'b, 'c> = (&'a Box<&'b Event<'c>>,);
type EventHookArgs<'a, 'b, 'c> = (Arc<EventData>, EventBatchId);
impl FnMut<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call_mut(&mut self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
self.call_self(args.0, args.1)
}
}
@@ -27,12 +31,12 @@ impl FnOnce<EventHookArgs<'_, '_, '_>> for NativeEventHook {
type Output = ();
extern "rust-call" fn call_once(self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
self.call_self(args.0, args.1)
}
}
impl Fn<EventHookArgs<'_, '_, '_>> for NativeEventHook {
extern "rust-call" fn call(&self, args: EventHookArgs<'_, '_, '_>) -> Self::Output {
self.call_self(args.0)
self.call_self(args.0, args.1)
}
}

View File

@@ -1,5 +1,5 @@
use crate::defines::LevelInt;
use crate::dynamic_data::{Battle, DamageSource, DynamicLibrary, LearnedMove, MoveLearnMethod, Pokemon};
use crate::dynamic_data::{Battle, DamageSource, DynamicLibrary, EventBatchId, LearnedMove, MoveLearnMethod, Pokemon};
use crate::ffi::ffi_handle::{FFIHandle, FromFFIHandle};
use crate::ffi::{FFIResult, OwnedPtrString};
use crate::static_data::{
@@ -397,8 +397,21 @@ extern "C" fn pokemon_is_on_battlefield(handle: FFIHandle<Pokemon>) -> u8 {
/// Damages the Pokemon by a certain amount of damage, from a damage source.
#[no_mangle]
extern "C" fn pokemon_damage(handle: FFIHandle<Pokemon>, damage: u32, source: DamageSource) -> FFIResult<()> {
handle.from_ffi_handle().damage(damage, source).into()
extern "C" fn pokemon_damage(
handle: FFIHandle<Pokemon>,
damage: u32,
source: DamageSource,
evt_batch_id_1: u64,
evt_batch_id_2: u64,
) -> FFIResult<()> {
handle
.from_ffi_handle()
.damage(
damage,
source,
EventBatchId::from_u64_pair(evt_batch_id_1, evt_batch_id_2),
)
.into()
}
/// Heals the Pokemon by a specific amount. Unless allow_revive is set to true, this will not

View File

@@ -100,6 +100,9 @@ pub(super) enum FFIObject {
MiscLibrary(Arc<dyn crate::dynamic_data::MiscLibrary>),
ScriptResolver(Arc<dyn crate::dynamic_data::ScriptResolver>),
DynamicLibrary(Arc<dyn crate::dynamic_data::DynamicLibrary>),
// Events
Event(Arc<crate::dynamic_data::EventData>),
}
unsafe impl Send for FFIObject {}
@@ -218,6 +221,7 @@ impl Hash for FFIObject {
Self::MiscLibrary(a) => Arc::as_ptr(a).hash(state),
Self::ScriptResolver(a) => Arc::as_ptr(a).hash(state),
Self::DynamicLibrary(a) => Arc::as_ptr(a).hash(state),
FFIObject::Event(a) => Arc::as_ptr(a).hash(state),
}
}
}
@@ -265,6 +269,7 @@ impl PartialEq for FFIObject {
(Self::MiscLibrary(a), Self::MiscLibrary(b)) => Arc::as_ptr(a).addr() == Arc::as_ptr(b).addr(),
(Self::ScriptResolver(a), Self::ScriptResolver(b)) => Arc::as_ptr(a).addr() == Arc::as_ptr(b).addr(),
(Self::DynamicLibrary(a), Self::DynamicLibrary(b)) => Arc::as_ptr(a).addr() == Arc::as_ptr(b).addr(),
(Self::Event(a), Self::Event(b)) => Arc::as_ptr(a).addr() == Arc::as_ptr(b).addr(),
_ => false,
}
}
@@ -337,3 +342,4 @@ ffi_obj_conversions!(Arc<dyn crate::dynamic_data::DamageLibrary>, DamageLibrary)
ffi_obj_conversions!(Arc<dyn crate::dynamic_data::MiscLibrary>, MiscLibrary);
ffi_obj_conversions!(Arc<dyn crate::dynamic_data::ScriptResolver>, ScriptResolver);
ffi_obj_conversions!(Arc<dyn crate::dynamic_data::DynamicLibrary>, DynamicLibrary);
ffi_obj_conversions!(Arc<crate::dynamic_data::EventData>, Event);