Skip to content

Commit 7877a3d

Browse files
authored
Merge pull request #615 from fyex/highlight-rng
SwSh_DailyHighlightRNG
2 parents bb00e31 + cd20640 commit 7877a3d

11 files changed

+833
-9
lines changed

SerialPrograms/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,8 @@ file(GLOB MAIN_SOURCES
22232223
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_BasicRNG.h
22242224
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_CramomaticRNG.cpp
22252225
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_CramomaticRNG.h
2226+
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_DailyHighlightRNG.cpp
2227+
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_DailyHighlightRNG.h
22262228
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_SeedFinder.cpp
22272229
Source/PokemonSwSh/Programs/RNG/PokemonSwSh_SeedFinder.h
22282230
Source/PokemonSwSh/Programs/ShinyHuntAutonomous/PokemonSwSh_ShinyHuntAutonomous-BerryTree.cpp
@@ -2261,6 +2263,8 @@ file(GLOB MAIN_SOURCES
22612263
Source/PokemonSwSh/Programs/ShinyHuntUnattended/PokemonSwSh_ShinyHuntUnattended-SwordsOfJustice.h
22622264
Source/PokemonSwSh/Programs/TestPrograms/PokemonSwSh_ShinyEncounterTester.cpp
22632265
Source/PokemonSwSh/Programs/TestPrograms/PokemonSwSh_ShinyEncounterTester.h
2266+
Source/PokemonSwSh/Resources/PokemonSwSh_DailyHighlightDatabase.cpp
2267+
Source/PokemonSwSh/Resources/PokemonSwSh_DailyHighlightDatabase.h
22642268
Source/PokemonSwSh/Resources/PokemonSwSh_MaxLairDatabase.cpp
22652269
Source/PokemonSwSh/Resources/PokemonSwSh_MaxLairDatabase.h
22662270
Source/PokemonSwSh/Resources/PokemonSwSh_NameDatabase.cpp

SerialPrograms/Source/Pokemon/Pokemon_Xoroshiro128Plus.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,22 @@ std::vector<bool> Xoroshiro128Plus::generate_last_bit_sequence(size_t max_advanc
7777
return sequence;
7878
}
7979

80+
81+
std::pair<bool, uint64_t> Xoroshiro128Plus::advances_to_state(Xoroshiro128PlusState other_state, uint64_t max_advances) {
82+
Xoroshiro128Plus temp_rng(get_state());
83+
uint64_t advances = 0;
84+
85+
while (advances <= max_advances) {
86+
Xoroshiro128PlusState temp_state = temp_rng.get_state();
87+
if (temp_state.s0 == other_state.s0 && temp_state.s1 == other_state.s1) {
88+
return { true, advances };
89+
}
90+
temp_rng.next();
91+
advances++;
92+
}
93+
return { false, advances };
94+
}
95+
8096
// The generic solution to the system of equations to calculate the initial state from the last bits of 128 consecutive Xoroshiro128+ results.
8197
uint64_t Xoroshiro128Plus::last_bits_reverse_matrix[128][2] = {
8298
/*s0 bit 0*/ {0b0101001100100001111011111110111001010011111110101011100011001101, 0b0111010111110111000101010100001111101001111001011111001011010111} ,

SerialPrograms/Source/Pokemon/Pokemon_Xoroshiro128Plus.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ class Xoroshiro128Plus{
3131
Xoroshiro128PlusState get_state();
3232
std::vector<bool> generate_last_bit_sequence(size_t max_advances);
3333

34+
// Calculates how many advances are required to reach the given state.
35+
// The given state must be reachable within max_advances advances.
36+
// Returns a pair:
37+
// first: true if the state is reachable within max_advances, false otherwise
38+
// second: the number of advances required (if first is true)
39+
std::pair<bool, uint64_t> advances_to_state(Xoroshiro128PlusState other_state, uint64_t max_advances = 100000);
40+
3441
static Xoroshiro128Plus xoroshiro128plus_from_last_bits(std::pair<uint64_t, uint64_t> last_bits);
3542

3643

SerialPrograms/Source/PokemonSwSh/PokemonSwSh_Panels.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
#include "Programs/OverworldBot/PokemonSwSh_ShinyHuntAutonomous-Overworld.h"
7676

7777
#include "Programs/RNG/PokemonSwSh_CramomaticRNG.h"
78+
#include "Programs/RNG/PokemonSwSh_DailyHighlightRNG.h"
7879
#include "Programs/RNG/PokemonSwSh_SeedFinder.h"
7980

8081
#include "Programs/PokemonSwSh_SynchronizedSpinning.h"
@@ -200,7 +201,8 @@ std::vector<PanelEntry> PanelListFactory::make_panels() const{
200201
ret.emplace_back(make_single_switch_program<ShinyHuntUnattendedIoATrade_Descriptor, ShinyHuntUnattendedIoATrade>());
201202

202203
if (PreloadSettings::instance().DEVELOPER_MODE){
203-
// ret.emplace_back("---- Untested/Beta/WIP ----");
204+
ret.emplace_back("---- Untested/Beta/WIP ----");
205+
ret.emplace_back(make_single_switch_program<DailyHighlightRNG_Descriptor, DailyHighlightRNG>());
204206
}
205207
if (PreloadSettings::instance().DEVELOPER_MODE){
206208
ret.emplace_back("---- Developer Tools ----");

SerialPrograms/Source/PokemonSwSh/Programs/RNG/PokemonSwSh_BasicRNG.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ Xoroshiro128PlusState find_rng_state(
6666
return rng.get_state();
6767
}
6868

69-
7069
Xoroshiro128PlusState refind_rng_state(
7170
VideoStream& stream,
7271
ProControllerContext& context,
@@ -75,6 +74,18 @@ Xoroshiro128PlusState refind_rng_state(
7574
size_t max_advances,
7675
bool save_screenshots,
7776
bool log_image_values
77+
) {
78+
return refind_rng_state_and_animations(stream, context, last_known_state, min_advances, max_advances, save_screenshots, log_image_values).first;
79+
}
80+
81+
std::pair<Xoroshiro128PlusState, uint64_t> refind_rng_state_and_animations(
82+
VideoStream& stream,
83+
ProControllerContext& context,
84+
Xoroshiro128PlusState last_known_state,
85+
size_t min_advances,
86+
size_t max_advances,
87+
bool save_screenshots,
88+
bool log_image_values
7889
)
7990
{
8091
Xoroshiro128Plus rng(last_known_state.s0, last_known_state.s1);
@@ -140,7 +151,7 @@ Xoroshiro128PlusState refind_rng_state(
140151
stream.log("RNG: state[0] = " + tostr_hex(rng.get_state().s0));
141152
stream.log("RNG: state[1] = " + tostr_hex(rng.get_state().s1));
142153

143-
return rng.get_state();
154+
return { rng.get_state(), sequence.size() };
144155
}
145156

146157

@@ -169,6 +180,18 @@ void do_rng_advances(
169180
pbf_wait(context, 1000ms);
170181
}
171182

183+
Xoroshiro128PlusState predict_state_after_menu_close(Xoroshiro128PlusState current_state, uint8_t num_npcs) {
184+
Xoroshiro128Plus rng(current_state);
185+
186+
for (size_t i = 0; i < num_npcs; i++) {
187+
rng.nextInt(91);
188+
}
189+
rng.next();
190+
rng.nextInt(61);
191+
192+
return rng.get_state();
193+
}
194+
172195

173196
}
174197
}

SerialPrograms/Source/PokemonSwSh/Programs/RNG/PokemonSwSh_BasicRNG.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "NintendoSwitch/Controllers/NintendoSwitch_ProController.h"
99
#include "Pokemon/Pokemon_Xoroshiro128Plus.h"
1010

11+
1112
namespace PokemonAutomation{
1213
namespace NintendoSwitch{
1314
namespace PokemonSwSh{
@@ -33,6 +34,20 @@ Xoroshiro128PlusState refind_rng_state(
3334
bool log_values
3435
);
3536

37+
// Performs Orbeetle attack animations until only one possible state is left.
38+
// Returns a pair:
39+
// first: current RNG state
40+
// second: the number of animations required to find the state
41+
std::pair<Xoroshiro128PlusState, uint64_t> refind_rng_state_and_animations(
42+
VideoStream& stream,
43+
ProControllerContext& context,
44+
Xoroshiro128PlusState last_known_state,
45+
size_t min_advances,
46+
size_t max_advances,
47+
bool save_screenshots,
48+
bool log_values
49+
);
50+
3651
void do_rng_advances(
3752
VideoStream& stream, ProControllerContext& context,
3853
Xoroshiro128Plus& rng,
@@ -41,6 +56,8 @@ void do_rng_advances(
4156
Milliseconds release_duration
4257
);
4358

59+
Xoroshiro128PlusState predict_state_after_menu_close(Xoroshiro128PlusState current_state, uint8_t num_npcs);
60+
4461

4562

4663
}

SerialPrograms/Source/PokemonSwSh/Programs/RNG/PokemonSwSh_CramomaticRNG.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,9 @@ CramomaticTarget CramomaticRNG::calculate_target(SingleSwitchProgramEnvironment&
194194
// priority_advances only starts counting up after the first good result is found
195195
while (priority_advances <= MAX_PRIORITY_ADVANCES){
196196
// calculate the result for the current temp_rng state
197-
Xoroshiro128Plus temp_rng(rng.get_state());
197+
Xoroshiro128PlusState temp_state = predict_state_after_menu_close(rng.get_state(), NUM_NPCS);
198+
Xoroshiro128Plus temp_rng(temp_state);
198199

199-
for (size_t i = 0; i < NUM_NPCS; i++){
200-
temp_rng.nextInt(91);
201-
}
202-
temp_rng.next();
203-
temp_rng.nextInt(60);
204200

205201
/*uint64_t item_roll =*/ temp_rng.nextInt(4);
206202
uint64_t ball_roll = temp_rng.nextInt(100);

0 commit comments

Comments
 (0)