Skip to content

Commit 5636633

Browse files
committed
Migrate some SwSh programs to TimeDurationOption.
1 parent 437d61e commit 5636633

22 files changed

+174
-149
lines changed

Common/Cpp/Options/TimeDurationOption.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,33 @@ struct TimeDurationCell<Type>::Data{
5353

5454
static const std::map<std::string, int64_t> SYMBOLS{
5555
{"ms", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},
56+
{"millisecond", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},
57+
{"milliseconds", std::chrono::duration_cast<Type>(std::chrono::milliseconds(1)).count()},
58+
5659
{"s", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
60+
{"secs", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
61+
{"second", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
62+
{"seconds", std::chrono::duration_cast<Type>(std::chrono::seconds(1)).count()},
63+
5764
{"min", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},
65+
{"minute", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},
66+
{"minutes", std::chrono::duration_cast<Type>(std::chrono::minutes(1)).count()},
67+
5868
{"h", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},
69+
{"hour", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},
70+
{"hours", std::chrono::duration_cast<Type>(std::chrono::hours(1)).count()},
71+
5972
{"d", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},
73+
{"day", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},
74+
{"days", std::chrono::duration_cast<Type>(std::chrono::days(1)).count()},
75+
76+
{"w", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},
77+
{"week", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},
78+
{"weeks", std::chrono::duration_cast<Type>(std::chrono::days(7)).count()},
79+
6080
{"y", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
81+
{"year", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
82+
{"years", std::chrono::duration_cast<Type>(std::chrono::years(1)).count()},
6183
};
6284

6385
using Rep = typename Type::rep;
@@ -162,6 +184,10 @@ TimeDurationCell<Type>::operator Type() const{
162184
return data.m_value;
163185
}
164186
template <typename Type>
187+
TimeDurationCell<Type>::operator WallDuration() const{
188+
return get();
189+
}
190+
template <typename Type>
165191
Type TimeDurationCell<Type>::get() const{
166192
const Data& data = *m_data;
167193
ReadSpinLock lg(data.m_lock);

Common/Cpp/Options/TimeDurationOption.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class TimeDurationCell : public ConfigOption{
4848
std::string current_text() const;
4949

5050
operator Type() const;
51+
operator WallDuration() const;
5152
Type get() const;
5253
std::string set(std::string text);
5354

SerialPrograms/Source/PokemonSwSh/Programs/DateSpamFarmers/PokemonSwSh_DateSpam-BerryFarmer2.cpp

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,10 @@ BerryFarmer2::BerryFarmer2()
8989
, m_advanced_options(
9090
"<font size=4><b>Advanced Options:</b> You should not need to touch anything below here.</font>"
9191
)
92-
, EXIT_BATTLE_TIMEOUT(
92+
, EXIT_BATTLE_TIMEOUT0(
9393
"<b>Exit Battle Timeout:</b><br>After running, wait this long to return to overworld.",
9494
LockMode::UNLOCK_WHILE_RUNNING,
95-
TICKS_PER_SECOND,
96-
"10 * TICKS_PER_SECOND"
95+
"10 s"
9796
)
9897
// , START_BATTLE_TIMEOUT(
9998
// "<b>Start Battle Timeout:</b><br>After a battle is detected, wait this long to flee in seconds.",
@@ -105,17 +104,15 @@ BerryFarmer2::BerryFarmer2()
105104
LockMode::UNLOCK_WHILE_RUNNING,
106105
1350
107106
)
108-
, RUSTLING_TIMEOUT(
107+
, RUSTLING_TIMEOUT0(
109108
"<b>Rustling Timeout:</b><br>Wait this many ticks to detect rustling.",
110109
LockMode::UNLOCK_WHILE_RUNNING,
111-
TICKS_PER_SECOND,
112-
"400"
110+
"3200 ms"
113111
)
114-
, SECONDARY_ATTEMPT_MASH_TIME(
112+
, SECONDARY_ATTEMPT_MASH_TIME0(
115113
"<b>Secondary attempt mash time:</b><br>Mash ZL this many ticks for secondary fetch attempts.",
116114
LockMode::UNLOCK_WHILE_RUNNING,
117-
TICKS_PER_SECOND,
118-
"240"
115+
"1920 ms"
119116
)
120117
, SOUND_THRESHOLD(
121118
"<b>Maximum Sound Error Coefficient",
@@ -133,11 +130,11 @@ BerryFarmer2::BerryFarmer2()
133130
PA_ADD_OPTION(NOTIFICATIONS);
134131

135132
PA_ADD_STATIC(m_advanced_options);
136-
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT);
133+
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT0);
137134
// PA_ADD_OPTION(START_BATTLE_TIMEOUT);
138135
PA_ADD_OPTION(RUSTLING_INTERVAL);
139-
PA_ADD_OPTION(RUSTLING_TIMEOUT);
140-
PA_ADD_OPTION(SECONDARY_ATTEMPT_MASH_TIME);
136+
PA_ADD_OPTION(RUSTLING_TIMEOUT0);
137+
PA_ADD_OPTION(SECONDARY_ATTEMPT_MASH_TIME0);
141138
PA_ADD_OPTION(SOUND_THRESHOLD);
142139
}
143140

@@ -172,7 +169,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
172169
int ret = run_until<SwitchControllerContext>(
173170
env.console, context,
174171
[&](SwitchControllerContext& context){
175-
pbf_wait(context, RUSTLING_TIMEOUT);
172+
pbf_wait(context, RUSTLING_TIMEOUT0);
176173
context.wait_for_all_requests();
177174
},
178175
{ {initial_rustling_detector}, {battle_menu_detector}, {start_battle_detector} }
@@ -186,7 +183,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
186183
int ret1 = run_until<SwitchControllerContext>(
187184
env.console, context,
188185
[&](SwitchControllerContext& context){
189-
pbf_wait(context, RUSTLING_TIMEOUT);
186+
pbf_wait(context, RUSTLING_TIMEOUT0);
190187
context.wait_for_all_requests();
191188
},
192189
{ {secondary_rustling_detector} }
@@ -206,7 +203,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
206203
stats.add_error();
207204
env.update_stats();
208205
pbf_mash_button(context, BUTTON_B, TICKS_PER_SECOND);
209-
run_away(env.console, context, EXIT_BATTLE_TIMEOUT);
206+
run_away(env.console, context, EXIT_BATTLE_TIMEOUT0);
210207
result = Rustling::Battle;
211208
break;
212209
case 2:{
@@ -227,7 +224,7 @@ BerryFarmer2::Rustling BerryFarmer2::check_rustling(SingleSwitchProgramEnvironme
227224
stats
228225
);
229226

230-
bool stop = handler.handle_standard_encounter_end_battle(encounter_result, EXIT_BATTLE_TIMEOUT);
227+
bool stop = handler.handle_standard_encounter_end_battle(encounter_result, EXIT_BATTLE_TIMEOUT0);
231228
if (stop){
232229
throw ProgramFinishedException();
233230
}
@@ -269,7 +266,7 @@ uint16_t BerryFarmer2::do_secondary_attempts(SingleSwitchProgramEnvironment& env
269266
}
270267
if (current_rustling == Rustling::Fast){
271268
// this is the last tree interaction for this time skip
272-
pbf_mash_button(context, BUTTON_ZL, SECONDARY_ATTEMPT_MASH_TIME);
269+
pbf_mash_button(context, BUTTON_ZL, SECONDARY_ATTEMPT_MASH_TIME0);
273270
pbf_mash_button(context, BUTTON_B, 10);
274271
attempts++;
275272
stats.shakes++;
@@ -315,7 +312,7 @@ void BerryFarmer2::program(SingleSwitchProgramEnvironment& env, SwitchController
315312
switch (current_rustling){
316313
case Rustling::Battle:
317314
pbf_mash_button(context, BUTTON_B, 1 * TICKS_PER_SECOND);
318-
run_away(env.console, context, EXIT_BATTLE_TIMEOUT);
315+
run_away(env.console, context, EXIT_BATTLE_TIMEOUT0);
319316
break;
320317
case Rustling::Fast:
321318
// Do nothing -> stop current tree session

SerialPrograms/Source/PokemonSwSh/Programs/DateSpamFarmers/PokemonSwSh_DateSpam-BerryFarmer2.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include "Common/Cpp/Options/StaticTextOption.h"
1111
#include "Common/Cpp/Options/SimpleIntegerOption.h"
1212
#include "Common/Cpp/Options/FloatingPointOption.h"
13-
#include "Common/Cpp/Options/TimeExpressionOption.h"
13+
#include "Common/Cpp/Options/TimeDurationOption.h"
1414
#include "CommonFramework/Notifications/EventNotificationsTable.h"
1515
#include "NintendoSwitch/NintendoSwitch_SingleSwitchProgram.h"
1616
#include "NintendoSwitch/Options/NintendoSwitch_StartInGripMenuOption.h"
@@ -64,11 +64,11 @@ class BerryFarmer2 : public SingleSwitchProgramInstance{
6464
EventNotificationsOption NOTIFICATIONS;
6565

6666
SectionDividerOption m_advanced_options;
67-
TimeExpressionOption<uint16_t> EXIT_BATTLE_TIMEOUT;
67+
MillisecondsOption EXIT_BATTLE_TIMEOUT0;
6868
// SimpleIntegerOption<uint16_t> START_BATTLE_TIMEOUT;
6969
SimpleIntegerOption<uint16_t> RUSTLING_INTERVAL;
70-
TimeExpressionOption<uint16_t> RUSTLING_TIMEOUT;
71-
TimeExpressionOption<uint16_t> SECONDARY_ATTEMPT_MASH_TIME;
70+
MillisecondsOption RUSTLING_TIMEOUT0;
71+
MillisecondsOption SECONDARY_ATTEMPT_MASH_TIME0;
7272
FloatingPointOption SOUND_THRESHOLD;
7373
};
7474

SerialPrograms/Source/PokemonSwSh/Programs/OverworldBot/PokemonSwSh_ShinyHuntAutonomous-Overworld.cpp

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,11 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
9797
LockMode::LOCK_WHILE_RUNNING,
9898
TriggerMethod::Whistle3Circle1
9999
)
100-
, MAX_MOVE_DURATION(
100+
, MAX_MOVE_DURATION0(
101101
"<b>Maximum Move Duration:</b><br>Do not move in the same direction for more than this long."
102102
" If you set this too high, you may wander too far from the grassy area.",
103103
LockMode::LOCK_WHILE_RUNNING,
104-
TICKS_PER_SECOND,
105-
"200"
104+
"1600 ms"
106105
)
107106
, MAX_TARGET_ALPHA(
108107
"<b>Max Target Alpha:</b><br>Ignore all targets with alpha larger than this. Set to zero to ignore all marks.",
@@ -121,17 +120,15 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
121120
, m_advanced_options(
122121
"<font size=4><b>Advanced Options:</b> You should not need to touch anything below here.</font>"
123122
)
124-
, WATCHDOG_TIMER(
123+
, WATCHDOG_TIMER0(
125124
"<b>Watchdog Timer:</b><br>Reset the game if you go this long without any encounters.",
126125
LockMode::LOCK_WHILE_RUNNING,
127-
TICKS_PER_SECOND,
128-
"60 * TICKS_PER_SECOND"
126+
"60 s"
129127
)
130-
, EXIT_BATTLE_TIMEOUT(
128+
, EXIT_BATTLE_TIMEOUT0(
131129
"<b>Exit Battle Timeout:</b><br>After running, wait this long to return to overworld.",
132130
LockMode::LOCK_WHILE_RUNNING,
133-
TICKS_PER_SECOND,
134-
"10 * TICKS_PER_SECOND"
131+
"10 s"
135132
)
136133
, TARGET_CIRCLING(
137134
"<b>Target Circling:</b><br>After moving towards a " + STRING_POKEMON + ", make a circle."
@@ -148,15 +145,15 @@ ShinyHuntAutonomousOverworld::ShinyHuntAutonomousOverworld()
148145
PA_ADD_OPTION(MARK_OFFSET);
149146
PA_ADD_OPTION(MARK_PRIORITY);
150147
PA_ADD_OPTION(TRIGGER_METHOD);
151-
PA_ADD_OPTION(MAX_MOVE_DURATION);
148+
PA_ADD_OPTION(MAX_MOVE_DURATION0);
152149
PA_ADD_OPTION(MAX_TARGET_ALPHA);
153150

154151
PA_ADD_OPTION(ENCOUNTER_BOT_OPTIONS);
155152
PA_ADD_OPTION(NOTIFICATIONS);
156153

157154
PA_ADD_STATIC(m_advanced_options);
158-
PA_ADD_OPTION(WATCHDOG_TIMER);
159-
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT);
155+
PA_ADD_OPTION(WATCHDOG_TIMER0);
156+
PA_ADD_OPTION(EXIT_BATTLE_TIMEOUT0);
160157
PA_ADD_OPTION(TARGET_CIRCLING);
161158
}
162159

@@ -310,10 +307,8 @@ bool ShinyHuntAutonomousOverworld::charge_at_target(
310307
", Direction = " + tostr_default(-angle) + " degrees"
311308
);
312309

313-
int duration = trajectory.distance_in_ticks + 16;
314-
if (duration > (int)MAX_MOVE_DURATION){
315-
duration = MAX_MOVE_DURATION;
316-
}
310+
Milliseconds duration = (trajectory.distance_in_ticks + 16) * 8ms;
311+
duration = std::min<Milliseconds>(duration, MAX_MOVE_DURATION0);
317312

318313

319314
StandardBattleMenuWatcher battle_menu_detector(false);
@@ -334,7 +329,7 @@ bool ShinyHuntAutonomousOverworld::charge_at_target(
334329
context,
335330
trajectory.joystick_x,
336331
trajectory.joystick_y,
337-
(uint16_t)duration, 0
332+
duration, 0ms
338333
);
339334

340335
// Circle Maneuver
@@ -383,7 +378,7 @@ void ShinyHuntAutonomousOverworld::program(SingleSwitchProgramEnvironment& env,
383378
}
384379
pbf_move_right_joystick(context, 128, 255, TICKS_PER_SECOND, 0);
385380

386-
WallDuration TIMEOUT = std::chrono::milliseconds((uint64_t)WATCHDOG_TIMER * 1000 / TICKS_PER_SECOND);
381+
WallDuration TIMEOUT = WATCHDOG_TIMER0;
387382
WallDuration PERIOD = std::chrono::hours(TIME_ROLLBACK_HOURS);
388383
WallClock last_touch = current_time();
389384
// const std::chrono::milliseconds TIMEOUT((uint64_t)WATCHDOG_TIMER * 1000 / TICKS_PER_SECOND);
@@ -441,7 +436,7 @@ void ShinyHuntAutonomousOverworld::program(SingleSwitchProgramEnvironment& env,
441436
);
442437
// shininess = ShinyDetection::SQUARE_SHINY;
443438

444-
bool stop = handler.handle_standard_encounter_end_battle(result, EXIT_BATTLE_TIMEOUT);
439+
bool stop = handler.handle_standard_encounter_end_battle(result, EXIT_BATTLE_TIMEOUT0);
445440
if (stop){
446441
break;
447442
}

SerialPrograms/Source/PokemonSwSh/Programs/OverworldBot/PokemonSwSh_ShinyHuntAutonomous-Overworld.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "Common/Cpp/Options/SimpleIntegerOption.h"
1313
#include "Common/Cpp/Options/FloatingPointOption.h"
1414
#include "Common/Cpp/Options/EnumDropdownOption.h"
15-
#include "Common/Cpp/Options/TimeExpressionOption.h"
15+
#include "Common/Cpp/Options/TimeDurationOption.h"
1616
#include "CommonFramework/Notifications/EventNotificationsTable.h"
1717
#include "NintendoSwitch/Options/NintendoSwitch_StartInGripMenuOption.h"
1818
#include "NintendoSwitch/Options/NintendoSwitch_GoHomeWhenDoneOption.h"
@@ -80,16 +80,16 @@ class ShinyHuntAutonomousOverworld : public SingleSwitchProgramInstance{
8080
};
8181
EnumDropdownOption<TriggerMethod> TRIGGER_METHOD;
8282

83-
TimeExpressionOption<uint16_t> MAX_MOVE_DURATION;
83+
MillisecondsOption MAX_MOVE_DURATION0;
8484
FloatingPointOption MAX_TARGET_ALPHA;
8585

8686
EncounterBotCommonOptions ENCOUNTER_BOT_OPTIONS;
8787

8888
EventNotificationsOption NOTIFICATIONS;
8989

9090
SectionDividerOption m_advanced_options;
91-
TimeExpressionOption<uint16_t> WATCHDOG_TIMER;
92-
TimeExpressionOption<uint16_t> EXIT_BATTLE_TIMEOUT;
91+
MillisecondsOption WATCHDOG_TIMER0;
92+
MillisecondsOption EXIT_BATTLE_TIMEOUT0;
9393
BooleanCheckBoxOption TARGET_CIRCLING;
9494
};
9595

SerialPrograms/Source/PokemonSwSh/Programs/PokemonSwSh_EncounterHandler.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,16 @@ void take_video(SwitchControllerContext& context){
3131
}
3232
void run_away(
3333
VideoStream& stream, SwitchControllerContext& context,
34-
uint16_t exit_battle_time
34+
Milliseconds exit_battle_time
3535
){
3636
BlackScreenOverWatcher black_screen_detector;
3737
run_until<SwitchControllerContext>(
3838
stream, context,
3939
[exit_battle_time](SwitchControllerContext& context){
4040
pbf_press_dpad(context, DPAD_UP, 10, 0);
41-
pbf_mash_button(context, BUTTON_A, TICKS_PER_SECOND);
42-
if (exit_battle_time > TICKS_PER_SECOND){
43-
pbf_mash_button(context, BUTTON_B, exit_battle_time - TICKS_PER_SECOND);
41+
pbf_mash_button(context, BUTTON_A, 1000ms);
42+
if (exit_battle_time > 1000ms){
43+
pbf_mash_button(context, BUTTON_B, exit_battle_time - 1000ms);
4444
}
4545
},
4646
{{black_screen_detector}}
@@ -76,7 +76,7 @@ void StandardEncounterHandler::update_frequencies(StandardEncounterDetection& en
7676
}
7777
void StandardEncounterHandler::run_away_and_update_stats(
7878
StandardEncounterDetection& encounter,
79-
uint16_t exit_battle_time,
79+
Milliseconds exit_battle_time,
8080
const ShinyDetectionResult& result
8181
){
8282
// Read the name.
@@ -106,9 +106,9 @@ void StandardEncounterHandler::run_away_and_update_stats(
106106
int ret = run_until<SwitchControllerContext>(
107107
m_stream, m_context,
108108
[exit_battle_time](SwitchControllerContext& context){
109-
pbf_mash_button(context, BUTTON_A, TICKS_PER_SECOND);
110-
if (exit_battle_time > TICKS_PER_SECOND){
111-
pbf_mash_button(context, BUTTON_B, exit_battle_time - TICKS_PER_SECOND);
109+
pbf_mash_button(context, BUTTON_A, 1000ms);
110+
if (exit_battle_time > 1000ms){
111+
pbf_mash_button(context, BUTTON_B, exit_battle_time - 1000ms);
112112
}
113113
},
114114
{{black_screen_detector}}
@@ -174,7 +174,7 @@ bool StandardEncounterHandler::handle_standard_encounter(const ShinyDetectionRes
174174
}
175175
bool StandardEncounterHandler::handle_standard_encounter_end_battle(
176176
const ShinyDetectionResult& result,
177-
uint16_t exit_battle_time
177+
Milliseconds exit_battle_time
178178
){
179179
if (result.shiny_type == ShinyType::UNKNOWN){
180180
m_stream.log("Unable to determine result of battle.", COLOR_RED);

SerialPrograms/Source/PokemonSwSh/Programs/PokemonSwSh_EncounterHandler.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,17 @@ class StandardEncounterHandler{
3131

3232
// Return true if program should stop.
3333
bool handle_standard_encounter(const ShinyDetectionResult& result);
34-
bool handle_standard_encounter_end_battle(const ShinyDetectionResult& result, uint16_t exit_battle_time);
34+
bool handle_standard_encounter_end_battle(
35+
const ShinyDetectionResult& result,
36+
Milliseconds exit_battle_time
37+
);
3538

3639

3740
private:
3841
void update_frequencies(StandardEncounterDetection& encounter);
3942
void run_away_and_update_stats(
4043
StandardEncounterDetection& encounter,
41-
uint16_t exit_battle_time,
44+
Milliseconds exit_battle_time,
4245
const ShinyDetectionResult& result
4346
);
4447

@@ -60,7 +63,7 @@ class StandardEncounterHandler{
6063
void take_video(SwitchControllerContext& context);
6164
void run_away(
6265
VideoStream& stream, SwitchControllerContext& context,
63-
uint16_t exit_battle_time
66+
Milliseconds exit_battle_time
6467
);
6568

6669

0 commit comments

Comments
 (0)