From 4d59f8fefcfca1525498c99e2941759ac5311239 Mon Sep 17 00:00:00 2001 From: Alexander Yee Date: Sat, 1 Feb 2025 15:13:40 -0800 Subject: [PATCH] Make `Button` an enum. Replace some uses of pbf_controller_state with ssf. --- .../Source/Integrations/IntegrationsAPI.cpp | 2 +- .../NintendoSwitch_KeyboardMapping.cpp | 2 +- .../NintendoSwitch_SerialPABotBase.cpp | 6 ++-- .../NintendoSwitch_VirtualControllerState.cpp | 2 +- .../NintendoSwitch_VirtualControllerState.h | 4 +-- .../Programs/Eggs/PokemonBDSP_EggRoutines.cpp | 29 +++++++------------ .../Programs/Eggs/PokemonSV_EggRoutines.cpp | 16 ++++------ .../Programs/PokemonSV_Navigation.cpp | 6 ++-- .../PokemonSV/Programs/PokemonSV_Navigation.h | 7 +++-- .../Sandwiches/PokemonSV_SandwichRoutines.cpp | 6 ++-- 10 files changed, 36 insertions(+), 44 deletions(-) diff --git a/SerialPrograms/Source/Integrations/IntegrationsAPI.cpp b/SerialPrograms/Source/Integrations/IntegrationsAPI.cpp index 54c33475b..4b92af1dc 100644 --- a/SerialPrograms/Source/Integrations/IntegrationsAPI.cpp +++ b/SerialPrograms/Source/Integrations/IntegrationsAPI.cpp @@ -106,7 +106,7 @@ void pai_stop_program(DllSafeString& error, uint64_t program_id){ } void pai_nsw_press_button(DllSafeString& error, uint64_t console_id, uint16_t button, uint16_t ticks){ - error = ProgramTracker::instance().nsw_press_button(console_id, button, ticks); + error = ProgramTracker::instance().nsw_press_button(console_id, (NintendoSwitch::Button)button, ticks); } void pai_nsw_press_dpad(DllSafeString& error, uint64_t console_id, uint8_t position, uint16_t ticks){ error = ProgramTracker::instance().nsw_press_dpad(console_id, (NintendoSwitch::DpadPosition)position, ticks); diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_KeyboardMapping.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_KeyboardMapping.cpp index 6bcd2079a..1cdbef469 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_KeyboardMapping.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_KeyboardMapping.cpp @@ -75,7 +75,7 @@ std::unique_ptr KeyMapTableRow::clone() const{ } ControllerDeltas KeyMapTableRow::snapshot() const{ return { - .buttons = buttons, + .buttons = (Button)buttons.current_value(), .dpad_x = dpad_x, .dpad_y = dpad_y, .left_x = left_stick_x, diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp index b6fc562c0..19e6410ad 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_SerialPABotBase.cpp @@ -180,9 +180,11 @@ SwitchController_SerialPABotBase::SwitchController_SerialPABotBase( } void SwitchController_SerialPABotBase::push_state(const Cancellable* cancellable, WallDuration duration){ - uint16_t buttons = 0; + Button buttons = BUTTON_NONE; for (size_t c = 0; c < 14; c++){ - buttons |= m_buttons[c].is_busy() ? ((uint16_t)1 << c) : 0; + buttons |= m_buttons[c].is_busy() + ? (Button)((uint16_t)1 << c) + : BUTTON_NONE; } uint8_t left_x = 128; diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp index 7969fd0fe..1e7c4b8e7 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.cpp @@ -22,7 +22,7 @@ using namespace std::chrono_literals; void SwitchControllerState::clear(){ - buttons = 0; + buttons = BUTTON_NONE; dpad = DPAD_NONE; left_x = 128; left_y = 128; diff --git a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.h b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.h index 64b507f03..c98bdbcb2 100644 --- a/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.h +++ b/SerialPrograms/Source/NintendoSwitch/Controllers/NintendoSwitch_VirtualControllerState.h @@ -25,7 +25,7 @@ class SwitchControllerState : public ControllerState{ virtual bool send_to_controller(ControllerSession& controller) const override; public: - Button buttons = 0; + Button buttons = BUTTON_NONE; DpadPosition dpad = DPAD_NONE; uint8_t left_x = 128; uint8_t left_y = 128; @@ -35,7 +35,7 @@ class SwitchControllerState : public ControllerState{ struct ControllerDeltas{ - Button buttons = 0; + Button buttons = BUTTON_NONE; int dpad_x = 0; int dpad_y = 0; int left_x = 0; diff --git a/SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggRoutines.cpp b/SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggRoutines.cpp index 99d85a842..9c459fc59 100644 --- a/SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggRoutines.cpp +++ b/SerialPrograms/Source/PokemonBDSP/Programs/Eggs/PokemonBDSP_EggRoutines.cpp @@ -5,6 +5,7 @@ */ #include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h" +#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h" #include "PokemonBDSP/PokemonBDSP_Settings.h" #include "PokemonBDSP/Programs/PokemonBDSP_GameNavigation.h" #include "PokemonBDSP_EggRoutines.h" @@ -16,7 +17,6 @@ namespace PokemonBDSP{ void egg_spin(SwitchControllerContext& context, uint16_t duration){ for (uint16_t c = 0; c < duration; c += 42){ -#if 1 pbf_move_left_joystick(context, 0, 0, 5, 0); pbf_move_left_joystick(context, 128, 0, 5, 0); pbf_move_left_joystick(context, 255, 0, 5, 0); @@ -25,28 +25,19 @@ void egg_spin(SwitchControllerContext& context, uint16_t duration){ pbf_move_left_joystick(context, 128, 255, 5, 0); pbf_move_left_joystick(context, 0, 255, 6, 0); pbf_move_left_joystick(context, 0, 128, 6, 0); -#else - pbf_controller_state(context, 0, DPAD_NONE, 0, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 128, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 128, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 255, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 128, 255, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 0, 255, 128, 128, 6); - pbf_controller_state(context, 0, DPAD_NONE, 0, 128, 128, 128, 6); -#endif } } void egg_spin_with_A(SwitchControllerContext& context, uint16_t duration){ for (uint16_t c = 0; c < duration; c += 42){ - pbf_controller_state(context, BUTTON_ZL, DPAD_NONE, 0, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 128, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 0, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 128, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 255, 255, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 128, 255, 128, 128, 5); - pbf_controller_state(context, 0, DPAD_NONE, 0, 255, 128, 128, 6); - pbf_controller_state(context, 0, DPAD_NONE, 0, 128, 128, 128, 6); + ssf_press_button(context, BUTTON_ZL, 0, 10); + pbf_move_left_joystick(context, 0, 0, 5, 0); + pbf_move_left_joystick(context, 128, 0, 5, 0); + pbf_move_left_joystick(context, 255, 0, 5, 0); + pbf_move_left_joystick(context, 255, 128, 5, 0); + pbf_move_left_joystick(context, 255, 255, 5, 0); + pbf_move_left_joystick(context, 128, 255, 5, 0); + pbf_move_left_joystick(context, 0, 255, 6, 0); + pbf_move_left_joystick(context, 0, 128, 6, 0); } } diff --git a/SerialPrograms/Source/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.cpp b/SerialPrograms/Source/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.cpp index c2a3e1eb7..7e0ffb031 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.cpp +++ b/SerialPrograms/Source/PokemonSV/Programs/Eggs/PokemonSV_EggRoutines.cpp @@ -127,13 +127,9 @@ void do_egg_cycle_motion( // hatch circle: // Left joystick forward, right joystick right // click left joystick - pbf_controller_state(context, BUTTON_LCLICK, DPAD_NONE, - 128, 0, 255, 128, 20); - pbf_controller_state(context, 0, DPAD_NONE, 128, 0, 255, 128, 20); - for(int j = 0; j < 600; j++){ - pbf_controller_state(context, BUTTON_LCLICK, DPAD_NONE, - 128, 0, 255, 128, TICKS_PER_SECOND); - } + ssf_press_left_joystick(context, 128, 0, 0ms, std::chrono::minutes(10)); + ssf_press_right_joystick(context, 255, 128, 0ms, std::chrono::minutes(10)); + pbf_press_button(context, BUTTON_LCLICK, std::chrono::minutes(10), 0ms); }, {dialog} ); @@ -410,7 +406,7 @@ void check_basket_to_collect_eggs( ){ bool checked = false; size_t consecutive_nothing = 0; - Button last_prompt = 0; + Button last_prompt = BUTTON_NONE; bool pending_refuse = false; WallClock start = current_time(); @@ -446,7 +442,7 @@ void check_basket_to_collect_eggs( case 0: stream.log("Detected no dialog."); consecutive_nothing++; - last_prompt = 0; + last_prompt = BUTTON_NONE; if (consecutive_nothing >= 10){ dump_image_and_throw_recoverable_exception( info, stream, "BasketNotFound", @@ -463,7 +459,7 @@ void check_basket_to_collect_eggs( case 1: stream.log("Detected advanced dialog."); - last_prompt = 0; + last_prompt = BUTTON_NONE; pbf_press_button(context, BUTTON_B, 20, 30); checked = true; continue; diff --git a/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.cpp b/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.cpp index e0e3f33ad..fc671f08b 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.cpp +++ b/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.cpp @@ -895,8 +895,8 @@ void walk_forward_while_clear_front_path( void mash_button_till_overworld( VideoStream& stream, - SwitchControllerContext& context, - uint16_t button, uint16_t seconds_run + SwitchControllerContext& context, + Button button, uint16_t seconds_run ){ OverworldWatcher overworld(stream.logger(), COLOR_CYAN); context.wait_for_all_requests(); @@ -1043,7 +1043,7 @@ void press_button_until_gradient_arrow( VideoStream& stream, SwitchControllerContext& context, ImageFloatBox box_area_to_check, - uint16_t button, + Button button, GradientArrowType arrow_type ){ GradientArrowWatcher arrow(COLOR_RED, arrow_type, box_area_to_check); diff --git a/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.h b/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.h index ac054fba1..cbef16860 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.h +++ b/SerialPrograms/Source/PokemonSV/Programs/PokemonSV_Navigation.h @@ -145,8 +145,9 @@ void walk_forward_while_clear_front_path( // mashes A button by default void mash_button_till_overworld( VideoStream& stream, - SwitchControllerContext& context, - uint16_t button = BUTTON_A, uint16_t seconds_run = 360 + SwitchControllerContext& context, + Button button = BUTTON_A, + uint16_t seconds_run = 360 ); // fly to the pokecenter that overlaps with the player on the map, and return true. @@ -178,7 +179,7 @@ void press_button_until_gradient_arrow( VideoStream& stream, SwitchControllerContext& context, ImageFloatBox box_area_to_check, - uint16_t button = BUTTON_A, + Button button = BUTTON_A, GradientArrowType arrow_type = GradientArrowType::RIGHT ); diff --git a/SerialPrograms/Source/PokemonSV/Programs/Sandwiches/PokemonSV_SandwichRoutines.cpp b/SerialPrograms/Source/PokemonSV/Programs/Sandwiches/PokemonSV_SandwichRoutines.cpp index 9e97165cf..23979eec5 100644 --- a/SerialPrograms/Source/PokemonSV/Programs/Sandwiches/PokemonSV_SandwichRoutines.cpp +++ b/SerialPrograms/Source/PokemonSV/Programs/Sandwiches/PokemonSV_SandwichRoutines.cpp @@ -575,7 +575,7 @@ void repeat_press_until( void repeat_button_press_until( const ProgramInfo& info, VideoStream& stream, SwitchControllerContext& context, - uint16_t button, uint16_t hold_ticks, uint16_t release_ticks, + Button button, uint16_t hold_ticks, uint16_t release_ticks, const std::vector& callbacks, const std::string &error_name, const std::string &error_message, std::chrono::milliseconds iteration_length = std::chrono::seconds(5), @@ -586,7 +586,9 @@ void repeat_button_press_until( const std::chrono::milliseconds button_time = std::chrono::milliseconds((hold_ticks + release_ticks) * (1000 / TICKS_PER_SECOND)); repeat_press_until( info, stream, context, - [&](){ pbf_press_button(context, button, hold_ticks, release_ticks); }, + [&](){ + pbf_press_button(context, button, hold_ticks, release_ticks); + }, callbacks, error_name, error_message, iteration_length - button_time, max_presses, default_video_period, default_audio_period );