diff --git a/src/common/consts.rs b/src/common/consts.rs index 5eda959..fcc9916 100644 --- a/src/common/consts.rs +++ b/src/common/consts.rs @@ -129,6 +129,25 @@ macro_rules! to_vec_impl { } } +macro_rules! get_random_impl { + ($e:ty) => { + pub fn get_random(&self) -> $e { + let options = self.to_vec(); + match options.len() { + 0 => { + return <$e>::empty(); + } + 1 => { + return options[0]; + } + _ => { + return *random_option(&options); + } + } + } + } +} + // Ledge Option bitflags! { pub struct LedgeOption : u32 @@ -140,7 +159,7 @@ bitflags! { } } -pub unsafe fn random_option<T>(arg: &Vec<T>) -> &T { +pub fn random_option<T>(arg: &Vec<T>) -> &T { return &arg[get_random_int(arg.len() as i32) as usize]; } @@ -155,6 +174,7 @@ impl LedgeOption { }) } to_vec_impl! {LedgeOption} + get_random_impl! {LedgeOption} } // Tech options @@ -169,6 +189,7 @@ bitflags! { impl TechFlags { to_vec_impl! {TechFlags} + get_random_impl! {TechFlags} } /// Mash States diff --git a/src/training/ledge.rs b/src/training/ledge.rs index 42927fc..f0d37c6 100644 --- a/src/training/ledge.rs +++ b/src/training/ledge.rs @@ -24,20 +24,7 @@ pub unsafe fn force_option(module_accessor: &mut app::BattleObjectModuleAccessor } let mut status = 0; - let ledge_case: LedgeOption; - - let ledge_options = MENU.ledge_state.to_vec(); - match ledge_options.len() { - 0 => { - ledge_case = LedgeOption::empty(); - } - 1 => { - ledge_case = ledge_options[0]; - } - _ => { - ledge_case = *random_option(&ledge_options); - } - } + let ledge_case: LedgeOption = MENU.ledge_state.get_random(); if let Some(new_status) = ledge_case.into_status() { status = new_status; diff --git a/src/training/tech.rs b/src/training/tech.rs index 22ddd4f..b774371 100644 --- a/src/training/tech.rs +++ b/src/training/tech.rs @@ -45,17 +45,7 @@ unsafe fn mod_handle_change_status( if status_kind_int == FIGHTER_STATUS_KIND_DOWN || status_kind_int == FIGHTER_STATUS_KIND_DAMAGE_FLY_REFLECT_D { - let states = MENU.tech_state.to_vec(); - let mut state = if states.is_empty() { - TechFlags::empty() - } else { - states[0] - }; - - if states.len() > 1 { - let idx = get_random_int(states.len() as i32) as usize; - state = states[idx]; - } + let state: TechFlags = MENU.tech_state.get_random(); match state { TechFlags::IN_PLACE => {