Skip to content

Commit 27f6905

Browse files
authored
Merge branch 'PokemonAutomation:main' into main
2 parents 9fc21a0 + cdf0d1d commit 27f6905

File tree

6 files changed

+88
-9
lines changed

6 files changed

+88
-9
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* Randomization
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#include <chrono>
8+
#include "Common/CRC32/pabb_CRC32.h"
9+
#include "Common/Cpp/Exceptions.h"
10+
#include "Random.h"
11+
12+
namespace PokemonAutomation{
13+
14+
15+
16+
uint32_t random_u32(){
17+
uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
18+
uint32_t crc = 0;
19+
pabb_crc32_buffer(&crc, &seed, sizeof(seed));
20+
return crc;
21+
}
22+
uint32_t random_u32(uint32_t min, uint32_t max){
23+
if (min > max){
24+
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "Invalid random_u32() range.");
25+
}
26+
27+
max++;
28+
uint32_t diff = max - min;
29+
30+
// Full range
31+
if (diff == 0){
32+
return random_u32();
33+
}
34+
35+
// Power-of-two
36+
if ((diff & (diff - 1)) == 0){
37+
return (random_u32() & (diff - 1)) + min;
38+
}
39+
40+
uint32_t quo = 0xffffffff / diff;
41+
uint32_t retry_threshold = quo + diff;
42+
43+
while (true){
44+
uint32_t u32 = random_u32();
45+
if (u32 < retry_threshold){
46+
return u32 % diff + min;
47+
}
48+
}
49+
}
50+
51+
52+
53+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Randomization
2+
*
3+
* From: https://github.com/PokemonAutomation/
4+
*
5+
*/
6+
7+
#ifndef PokemonAutomation_Randomization_H
8+
#define PokemonAutomation_Randomization_H
9+
10+
#include <stdint.h>
11+
12+
namespace PokemonAutomation{
13+
14+
15+
16+
uint32_t random_u32();
17+
uint32_t random_u32(uint32_t min, uint32_t max);
18+
19+
20+
21+
}
22+
#endif

SerialPrograms/Source/NintendoSwitch/DevPrograms/TestProgramComputer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
#include "Common/Cpp/StreamConnections/ReliableStreamConnection.h"
145145
#include "Common/PABotBase2/PABotbase2_ReliableStreamConnection.h"
146146
#include "Common/Cpp/StreamConnections/MockDevice.h"
147+
#include "CommonTools/Random.h"
147148

148149

149150
//#include <opencv2/core.hpp>
@@ -358,8 +359,13 @@ void TestProgramComputer::program(ProgramEnvironment& env, CancellableScope& sco
358359

359360
[[maybe_unused]] Logger& logger = env.logger();
360361

362+
// cout << random_u32(100, 115) << endl;
363+
364+
361365
stress_test(logger, scope);
362366

367+
368+
363369
#if 0
364370
{
365371
MockDevice device(GlobalThreadPools::unlimited_normal());

SerialPrograms/Source/PokemonFRLG/PokemonFRLG_Navigation.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
*/
88

99
#include "CommonFramework/Exceptions/OperationFailedException.h"
10+
#include "CommonTools/Random.h"
1011
#include "CommonTools/Async/InferenceRoutines.h"
1112
#include "CommonTools/VisualDetectors/BlackScreenDetector.h"
1213
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_PushButtons.h"
1314
#include "NintendoSwitch/Commands/NintendoSwitch_Commands_Superscalar.h"
1415
#include "NintendoSwitch/Controllers/Procon/NintendoSwitch_ProController.h"
1516
#include "NintendoSwitch/NintendoSwitch_ConsoleHandle.h"
16-
#include "PokemonSwSh/MaxLair/AI/PokemonSwSh_MaxLair_AI.h"
1717
#include "PokemonFRLG/Inference/Dialogs/PokemonFRLG_DialogDetector.h"
1818
#include "PokemonFRLG/Inference/Sounds/PokemonFRLG_ShinySoundDetector.h"
1919
#include "PokemonFRLG/Inference/Menus/PokemonFRLG_StartMenuDetector.h"
@@ -34,7 +34,7 @@ void soft_reset(ConsoleHandle& console, ProControllerContext& context){
3434

3535
//Random wait before pressing start/A
3636
console.log("Randomly waiting...");
37-
Milliseconds rng_wait = std::chrono::milliseconds(PokemonSwSh::MaxLairInternal::random(0, 5000));
37+
Milliseconds rng_wait = std::chrono::milliseconds(random_u32(0, 5000));
3838
pbf_wait(context, rng_wait);
3939
context.wait_for_all_requests();
4040

@@ -92,7 +92,7 @@ void soft_reset(ConsoleHandle& console, ProControllerContext& context){
9292

9393
//Random wait no.2
9494
console.log("Randomly waiting...");
95-
Milliseconds rng_wait2 = std::chrono::milliseconds(PokemonSwSh::MaxLairInternal::random(0, 5000));
95+
Milliseconds rng_wait2 = std::chrono::milliseconds(random_u32(0, 5000));
9696
pbf_wait(context, rng_wait2);
9797
context.wait_for_all_requests();
9898

SerialPrograms/Source/PokemonSwSh/MaxLair/AI/PokemonSwSh_MaxLair_AI.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "Common/CRC32/pabb_CRC32.h"
88
//#include "CommonFramework/Environment/Environment.h"
9+
#include "CommonTools/Random.h"
910
#include "PokemonSwSh_MaxLair_AI.h"
1011

1112
namespace PokemonAutomation{
@@ -15,12 +16,7 @@ namespace MaxLairInternal{
1516

1617

1718
int random(int min, int max){
18-
// uint64_t seed = x86_rdtsc();
19-
uint64_t seed = std::chrono::high_resolution_clock::now().time_since_epoch().count();
20-
uint32_t crc = 0;
21-
pabb_crc32_buffer(&crc, &seed, sizeof(seed));
22-
seed = crc % (max - min + 1);
23-
return (int)seed + min;
19+
return random_u32(min, max);
2420
}
2521

2622

SerialPrograms/cmake/SourceFiles.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,8 @@ file(GLOB LIBRARY_SOURCES
658658
Source/CommonTools/Options/StringSelectOption.h
659659
Source/CommonTools/Options/StringSelectTableOption.h
660660
Source/CommonTools/Options/TrainOCRModeOption.h
661+
Source/CommonTools/Random.cpp
662+
Source/CommonTools/Random.h
661663
Source/CommonTools/Resources/SpriteDatabase.cpp
662664
Source/CommonTools/Resources/SpriteDatabase.h
663665
Source/CommonTools/StartupChecks/StartProgramChecks.cpp

0 commit comments

Comments
 (0)