Skip to content

Commit

Permalink
Migrate some SwSh programs to TimeDurationOption.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mysticial committed Feb 5, 2025
1 parent 437d61e commit 5636633
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 149 deletions.
26 changes: 26 additions & 0 deletions Common/Cpp/Options/TimeDurationOption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,33 @@ struct TimeDurationCell<Type>::Data{

static const std::map<std::string, int64_t> SYMBOLS{
{"ms", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},
{"millisecond", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},
{"milliseconds", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},

{"s", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
{"secs", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
{"second", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
{"seconds", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},

{"min", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},
{"minute", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},
{"minutes", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},

{"h", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},
{"hour", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},
{"hours", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},

{"d", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},
{"day", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},
{"days", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},

{"w", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},
{"week", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},
{"weeks", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},

{"y", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
{"year", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
{"years", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
};

using Rep = typename Type::rep;
Expand Down Expand Up @@ -162,6 +184,10 @@ TimeDurationCell<Type>::operator Type() const{
return data.m_value;
}
template <typename Type>
TimeDurationCell<Type>::operator WallDuration() const{
return get();
}
template <typename Type>
Type TimeDurationCell<Type>::get() const{
const Data& data = *m_data;
ReadSpinLock lg(data.m_lock);
Expand Down
1 change: 1 addition & 0 deletions Common/Cpp/Options/TimeDurationOption.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class TimeDurationCell : public ConfigOption{
std::string current_text() const;

operator Type() const;
operator WallDuration() const;
Type get() const;
std::string set(std::string text);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ BerryFarmer2::BerryFarmer2()
, m_advanced_options(
"<font size=4><b>Advanced Options:</b> You should not need to touch anything below here.</font>"
)
, EXIT_BATTLE_TIMEOUT(
, EXIT_BATTLE_TIMEOUT0(
"<b>Exit Battle Timeout:</b><br>After running, wait this long to return to overworld.",
LockMode::UNLOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"10 * TICKS_PER_SECOND"
"10 s"
)
// , START_BATTLE_TIMEOUT(
// "<b>Start Battle Timeout:</b><br>After a battle is detected, wait this long to flee in seconds.",
Expand All @@ -105,17 +104,15 @@ BerryFarmer2::BerryFarmer2()
LockMode::UNLOCK_WHILE_RUNNING,
1350
)
, RUSTLING_TIMEOUT(
, RUSTLING_TIMEOUT0(
"<b>Rustling Timeout:</b><br>Wait this many ticks to detect rustling.",
LockMode::UNLOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"400"
"3200 ms"
)
, SECONDARY_ATTEMPT_MASH_TIME(
, SECONDARY_ATTEMPT_MASH_TIME0(
"<b>Secondary attempt mash time:</b><br>Mash ZL this many ticks for secondary fetch attempts.",
LockMode::UNLOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"240"
"1920 ms"
)
, SOUND_THRESHOLD(
"<b>Maximum Sound Error Coefficient",
Expand All @@ -133,11 +130,11 @@ BerryFarmer2::BerryFarmer2()
PA_ADD_OPTION(NOTIFICATIONS);

PA_ADD_STATIC(m_advanced_options);
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT);
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT0);
// PA_ADD_OPTION(START_BATTLE_TIMEOUT);
PA_ADD_OPTION(RUSTLING_INTERVAL);
PA_ADD_OPTION(RUSTLING_TIMEOUT);
PA_ADD_OPTION(SECONDARY_ATTEMPT_MASH_TIME);
PA_ADD_OPTION(RUSTLING_TIMEOUT0);
PA_ADD_OPTION(SECONDARY_ATTEMPT_MASH_TIME0);
PA_ADD_OPTION(SOUND_THRESHOLD);
}

Expand Down Expand Up @@ -172,7 +169,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
int ret = run_until<SwitchControllerContext>(
env.console, context,
[&](SwitchControllerContext& context){
pbf_wait(context, RUSTLING_TIMEOUT);
pbf_wait(context, RUSTLING_TIMEOUT0);
context.wait_for_all_requests();
},
{ {initial_rustling_detector}, {battle_menu_detector}, {start_battle_detector} }
Expand All @@ -186,7 +183,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
int ret1 = run_until<SwitchControllerContext>(
env.console, context,
[&](SwitchControllerContext& context){
pbf_wait(context, RUSTLING_TIMEOUT);
pbf_wait(context, RUSTLING_TIMEOUT0);
context.wait_for_all_requests();
},
{ {secondary_rustling_detector} }
Expand All @@ -206,7 +203,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
stats.add_error();
env.update_stats();
pbf_mash_button(context, BUTTON_B, TICKS_PER_SECOND);
run_away(env.console, context, EXIT_BATTLE_TIMEOUT);
run_away(env.console, context, EXIT_BATTLE_TIMEOUT0);
result = Rustling::Battle;
break;
case 2:{
Expand All @@ -227,7 +224,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
stats
);

bool stop = handler.handle_standard_encounter_end_battle(encounter_result, EXIT_BATTLE_TIMEOUT);
bool stop = handler.handle_standard_encounter_end_battle(encounter_result, EXIT_BATTLE_TIMEOUT0);
if (stop){
throw ProgramFinishedException();
}
Expand Down Expand Up @@ -269,7 +266,7 @@ uint16_t BerryFarmer2::do_secondary_attempts(SingleSwitchProgramEnvironment& env
}
if (current_rustling == Rustling::Fast){
// this is the last tree interaction for this time skip
pbf_mash_button(context, BUTTON_ZL, SECONDARY_ATTEMPT_MASH_TIME);
pbf_mash_button(context, BUTTON_ZL, SECONDARY_ATTEMPT_MASH_TIME0);
pbf_mash_button(context, BUTTON_B, 10);
attempts++;
stats.shakes++;
Expand Down Expand Up @@ -315,7 +312,7 @@ void BerryFarmer2::program(SingleSwitchProgramEnvironment& env, SwitchController
switch (current_rustling){
case Rustling::Battle:
pbf_mash_button(context, BUTTON_B, 1 * TICKS_PER_SECOND);
run_away(env.console, context, EXIT_BATTLE_TIMEOUT);
run_away(env.console, context, EXIT_BATTLE_TIMEOUT0);
break;
case Rustling::Fast:
// Do nothing -> stop current tree session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "Common/Cpp/Options/StaticTextOption.h"
#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Common/Cpp/Options/FloatingPointOption.h"
#include "Common/Cpp/Options/TimeExpressionOption.h"
#include "Common/Cpp/Options/TimeDurationOption.h"
#include "CommonFramework/Notifications/EventNotificationsTable.h"
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
#include "NintendoSwitch/Options/NintendoSwitch_StartInGripMenuOption.h"
Expand Down Expand Up @@ -64,11 +64,11 @@ class BerryFarmer2 : public SingleSwitchProgramInstance{
EventNotificationsOption NOTIFICATIONS;

SectionDividerOption m_advanced_options;
TimeExpressionOption<uint16_t> EXIT_BATTLE_TIMEOUT;
MillisecondsOption EXIT_BATTLE_TIMEOUT0;
// SimpleIntegerOption<uint16_t> START_BATTLE_TIMEOUT;
SimpleIntegerOption<uint16_t> RUSTLING_INTERVAL;
TimeExpressionOption<uint16_t> RUSTLING_TIMEOUT;
TimeExpressionOption<uint16_t> SECONDARY_ATTEMPT_MASH_TIME;
MillisecondsOption RUSTLING_TIMEOUT0;
MillisecondsOption SECONDARY_ATTEMPT_MASH_TIME0;
FloatingPointOption SOUND_THRESHOLD;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,11 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
LockMode::LOCK_WHILE_RUNNING,
TriggerMethod::Whistle3Circle1
)
, MAX_MOVE_DURATION(
, MAX_MOVE_DURATION0(
"<b>Maximum Move Duration:</b><br>Do not move in the same direction for more than this long."
" If you set this too high, you may wander too far from the grassy area.",
LockMode::LOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"200"
"1600 ms"
)
, MAX_TARGET_ALPHA(
"<b>Max Target Alpha:</b><br>Ignore all targets with alpha larger than this. Set to zero to ignore all marks.",
Expand All @@ -121,17 +120,15 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
, m_advanced_options(
"<font size=4><b>Advanced Options:</b> You should not need to touch anything below here.</font>"
)
, WATCHDOG_TIMER(
, WATCHDOG_TIMER0(
"<b>Watchdog Timer:</b><br>Reset the game if you go this long without any encounters.",
LockMode::LOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"60 * TICKS_PER_SECOND"
"60 s"
)
, EXIT_BATTLE_TIMEOUT(
, EXIT_BATTLE_TIMEOUT0(
"<b>Exit Battle Timeout:</b><br>After running, wait this long to return to overworld.",
LockMode::LOCK_WHILE_RUNNING,
TICKS_PER_SECOND,
"10 * TICKS_PER_SECOND"
"10 s"
)
, TARGET_CIRCLING(
"<b>Target Circling:</b><br>After moving towards a " + STRING_POKEMON + ", make a circle."
Expand All @@ -148,15 +145,15 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
PA_ADD_OPTION(MARK_OFFSET);
PA_ADD_OPTION(MARK_PRIORITY);
PA_ADD_OPTION(TRIGGER_METHOD);
PA_ADD_OPTION(MAX_MOVE_DURATION);
PA_ADD_OPTION(MAX_MOVE_DURATION0);
PA_ADD_OPTION(MAX_TARGET_ALPHA);

PA_ADD_OPTION(ENCOUNTER_BOT_OPTIONS);
PA_ADD_OPTION(NOTIFICATIONS);

PA_ADD_STATIC(m_advanced_options);
PA_ADD_OPTION(WATCHDOG_TIMER);
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT);
PA_ADD_OPTION(WATCHDOG_TIMER0);
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT0);
PA_ADD_OPTION(TARGET_CIRCLING);
}

Expand Down Expand Up @@ -310,10 +307,8 @@ bool ShinyHuntAutonomousOverworld::charge_at_target(
", Direction = " + tostr_default(-angle) + " degrees"
);

int duration = trajectory.distance_in_ticks + 16;
if (duration > (int)MAX_MOVE_DURATION){
duration = MAX_MOVE_DURATION;
}
Milliseconds duration = (trajectory.distance_in_ticks + 16) * 8ms;
duration = std::min<Milliseconds>(duration, MAX_MOVE_DURATION0);


StandardBattleMenuWatcher battle_menu_detector(false);
Expand All @@ -334,7 +329,7 @@ bool ShinyHuntAutonomousOverworld::charge_at_target(
context,
trajectory.joystick_x,
trajectory.joystick_y,
(uint16_t)duration, 0
duration, 0ms
);

// Circle Maneuver
Expand Down Expand Up @@ -383,7 +378,7 @@ void ShinyHuntAutonomousOverworld::program(SingleSwitchProgramEnvironment& env,
}
pbf_move_right_joystick(context, 128, 255, TICKS_PER_SECOND, 0);

WallDuration TIMEOUT = std::chrono::milliseconds((uint64_t)WATCHDOG_TIMER * 1000 / TICKS_PER_SECOND);
WallDuration TIMEOUT = WATCHDOG_TIMER0;
WallDuration PERIOD = std::chrono::hours(TIME_ROLLBACK_HOURS);
WallClock last_touch = current_time();
// const std::chrono::milliseconds TIMEOUT((uint64_t)WATCHDOG_TIMER * 1000 / TICKS_PER_SECOND);
Expand Down Expand Up @@ -441,7 +436,7 @@ void ShinyHuntAutonomousOverworld::program(SingleSwitchProgramEnvironment& env,
);
// shininess = ShinyDetection::SQUARE_SHINY;

bool stop = handler.handle_standard_encounter_end_battle(result, EXIT_BATTLE_TIMEOUT);
bool stop = handler.handle_standard_encounter_end_battle(result, EXIT_BATTLE_TIMEOUT0);
if (stop){
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "Common/Cpp/Options/SimpleIntegerOption.h"
#include "Common/Cpp/Options/FloatingPointOption.h"
#include "Common/Cpp/Options/EnumDropdownOption.h"
#include "Common/Cpp/Options/TimeExpressionOption.h"
#include "Common/Cpp/Options/TimeDurationOption.h"
#include "CommonFramework/Notifications/EventNotificationsTable.h"
#include "NintendoSwitch/Options/NintendoSwitch_StartInGripMenuOption.h"
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
Expand Down Expand Up @@ -80,16 +80,16 @@ class ShinyHuntAutonomousOverworld : public SingleSwitchProgramInstance{
};
EnumDropdownOption<TriggerMethod> TRIGGER_METHOD;

TimeExpressionOption<uint16_t> MAX_MOVE_DURATION;
MillisecondsOption MAX_MOVE_DURATION0;
FloatingPointOption MAX_TARGET_ALPHA;

EncounterBotCommonOptions ENCOUNTER_BOT_OPTIONS;

EventNotificationsOption NOTIFICATIONS;

SectionDividerOption m_advanced_options;
TimeExpressionOption<uint16_t> WATCHDOG_TIMER;
TimeExpressionOption<uint16_t> EXIT_BATTLE_TIMEOUT;
MillisecondsOption WATCHDOG_TIMER0;
MillisecondsOption EXIT_BATTLE_TIMEOUT0;
BooleanCheckBoxOption TARGET_CIRCLING;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ void take_video(SwitchControllerContext& context){
}
void run_away(
VideoStream& stream, SwitchControllerContext& context,
uint16_t exit_battle_time
Milliseconds exit_battle_time
){
BlackScreenOverWatcher black_screen_detector;
run_until<SwitchControllerContext>(
stream, context,
[exit_battle_time](SwitchControllerContext& context){
pbf_press_dpad(context, DPAD_UP, 10, 0);
pbf_mash_button(context, BUTTON_A, TICKS_PER_SECOND);
if (exit_battle_time > TICKS_PER_SECOND){
pbf_mash_button(context, BUTTON_B, exit_battle_time - TICKS_PER_SECOND);
pbf_mash_button(context, BUTTON_A, 1000ms);
if (exit_battle_time > 1000ms){
pbf_mash_button(context, BUTTON_B, exit_battle_time - 1000ms);
}
},
{{black_screen_detector}}
Expand Down Expand Up @@ -76,7 +76,7 @@ void StandardEncounterHandler::update_frequencies(StandardEncounterDetection& en
}
void StandardEncounterHandler::run_away_and_update_stats(
StandardEncounterDetection& encounter,
uint16_t exit_battle_time,
Milliseconds exit_battle_time,
const ShinyDetectionResult& result
){
// Read the name.
Expand Down Expand Up @@ -106,9 +106,9 @@ void StandardEncounterHandler::run_away_and_update_stats(
int ret = run_until<SwitchControllerContext>(
m_stream, m_context,
[exit_battle_time](SwitchControllerContext& context){
pbf_mash_button(context, BUTTON_A, TICKS_PER_SECOND);
if (exit_battle_time > TICKS_PER_SECOND){
pbf_mash_button(context, BUTTON_B, exit_battle_time - TICKS_PER_SECOND);
pbf_mash_button(context, BUTTON_A, 1000ms);
if (exit_battle_time > 1000ms){
pbf_mash_button(context, BUTTON_B, exit_battle_time - 1000ms);
}
},
{{black_screen_detector}}
Expand Down Expand Up @@ -174,7 +174,7 @@ bool StandardEncounterHandler::handle_standard_encounter(const ShinyDetectionRes
}
bool StandardEncounterHandler::handle_standard_encounter_end_battle(
const ShinyDetectionResult& result,
uint16_t exit_battle_time
Milliseconds exit_battle_time
){
if (result.shiny_type == ShinyType::UNKNOWN){
m_stream.log("Unable to determine result of battle.", COLOR_RED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ class StandardEncounterHandler{

// Return true if program should stop.
bool handle_standard_encounter(const ShinyDetectionResult& result);
bool handle_standard_encounter_end_battle(const ShinyDetectionResult& result, uint16_t exit_battle_time);
bool handle_standard_encounter_end_battle(
const ShinyDetectionResult& result,
Milliseconds exit_battle_time
);


private:
void update_frequencies(StandardEncounterDetection& encounter);
void run_away_and_update_stats(
StandardEncounterDetection& encounter,
uint16_t exit_battle_time,
Milliseconds exit_battle_time,
const ShinyDetectionResult& result
);

Expand All @@ -60,7 +63,7 @@ class StandardEncounterHandler{
void take_video(SwitchControllerContext& context);
void run_away(
VideoStream& stream, SwitchControllerContext& context,
uint16_t exit_battle_time
Milliseconds exit_battle_time
);


Expand Down
Loading

0 comments on commit 5636633

Please sign in to comment.