Skip to content

Commit

Permalink
Make Button an enum. Replace some uses of pbf_controller_state with…
Browse files Browse the repository at this point in the history
… ssf.
  • Loading branch information
Mysticial committed Feb 1, 2025
1 parent 3936a49 commit 4d59f8f
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 44 deletions.
2 changes: 1 addition & 1 deletion SerialPrograms/Source/Integrations/IntegrationsAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::unique_ptr<EditableTableRow> 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}
);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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",
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PeriodicInferenceCallback>& callbacks,
const std::string &error_name, const std::string &error_message,
std::chrono::milliseconds iteration_length = std::chrono::seconds(5),
Expand All @@ -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
);
Expand Down

0 comments on commit 4d59f8f

Please sign in to comment.