RNG refactor: use in Extractor, only apply rand_init to default_state (#6123)
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h"
|
#include "soh/Enhancements/game-interactor/GameInteractor_Hooks.h"
|
||||||
#include "soh/ShipInit.hpp"
|
#include "soh/ShipInit.hpp"
|
||||||
|
#include "soh/ResourceManagerHelpers.h"
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#include "macros.h"
|
#include "macros.h"
|
||||||
#include "soh/ResourceManagerHelpers.h"
|
|
||||||
#include "objects/object_link_boy/object_link_boy.h"
|
#include "objects/object_link_boy/object_link_boy.h"
|
||||||
#include "objects/object_link_child/object_link_child.h"
|
#include "objects/object_link_child/object_link_child.h"
|
||||||
extern SaveContext gSaveContext;
|
extern SaveContext gSaveContext;
|
||||||
|
|||||||
@@ -1,10 +1,5 @@
|
|||||||
#include "random.hpp"
|
#include "random.hpp"
|
||||||
|
|
||||||
#include <bit>
|
|
||||||
#include <random>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
static bool init = false;
|
|
||||||
uint64_t rando_state = 0;
|
uint64_t rando_state = 0;
|
||||||
const uint64_t multiplier = 6364136223846793005ULL;
|
const uint64_t multiplier = 6364136223846793005ULL;
|
||||||
const uint64_t increment = 11634580027462260723ULL;
|
const uint64_t increment = 11634580027462260723ULL;
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "Extract.h"
|
#include "Extract.h"
|
||||||
#include "portable-file-dialogs.h"
|
#include "portable-file-dialogs.h"
|
||||||
#include <ship/utils/binarytools/BitConverter.h>
|
#include <ship/utils/binarytools/BitConverter.h>
|
||||||
|
#include "soh/ShipUtils.h"
|
||||||
#include "variables.h"
|
#include "variables.h"
|
||||||
|
|
||||||
#ifdef unix
|
#ifdef unix
|
||||||
@@ -46,7 +47,6 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <random>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
extern "C" uint32_t CRC32C(unsigned char* data, size_t dataSize);
|
extern "C" uint32_t CRC32C(unsigned char* data, size_t dataSize);
|
||||||
@@ -619,13 +619,10 @@ std::string Extractor::Mkdtemp() {
|
|||||||
|
|
||||||
// create 6 random alphanumeric characters
|
// create 6 random alphanumeric characters
|
||||||
static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
static const char charset[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
||||||
std::random_device rd;
|
|
||||||
std::mt19937 gen(rd());
|
|
||||||
std::uniform_int_distribution<> dist(0, sizeof(charset) - 1);
|
|
||||||
|
|
||||||
char randchr[7];
|
char randchr[7];
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
randchr[i] = charset[dist(gen)];
|
randchr[i] = charset[ShipUtils::Random(0, sizeof(charset))];
|
||||||
}
|
}
|
||||||
randchr[6] = '\0';
|
randchr[6] = '\0';
|
||||||
|
|
||||||
|
|||||||
@@ -98,14 +98,13 @@ extern "C" void* Ship_GetCharFontTexture(u8 character) {
|
|||||||
return (void*)fontTbl[adjustedChar];
|
return (void*)fontTbl[adjustedChar];
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool rand_init = false;
|
static bool default_init = false;
|
||||||
uint64_t default_state = 0;
|
uint64_t default_state = 0;
|
||||||
const uint64_t multiplier = 6364136223846793005ULL;
|
const uint64_t multiplier = 6364136223846793005ULL;
|
||||||
const uint64_t increment = 11634580027462260723ULL;
|
const uint64_t increment = 11634580027462260723ULL;
|
||||||
|
|
||||||
// Initialize with seed specified
|
// Initialize with seed specified
|
||||||
void ShipUtils::RandInit(uint64_t seed, uint64_t* state) {
|
void ShipUtils::RandInit(uint64_t seed, uint64_t* state) {
|
||||||
rand_init = true;
|
|
||||||
if (state == nullptr) {
|
if (state == nullptr) {
|
||||||
state = &default_state;
|
state = &default_state;
|
||||||
}
|
}
|
||||||
@@ -115,16 +114,16 @@ void ShipUtils::RandInit(uint64_t seed, uint64_t* state) {
|
|||||||
uint32_t ShipUtils::next32(uint64_t* state) {
|
uint32_t ShipUtils::next32(uint64_t* state) {
|
||||||
if (state == nullptr) {
|
if (state == nullptr) {
|
||||||
state = &default_state;
|
state = &default_state;
|
||||||
}
|
if (!default_init) {
|
||||||
|
// No seed given, get a random number from device to seed
|
||||||
if (!rand_init) {
|
|
||||||
// No seed given, get a random number from device to seed
|
|
||||||
#if !defined(__SWITCH__) && !defined(__WIIU__)
|
#if !defined(__SWITCH__) && !defined(__WIIU__)
|
||||||
uint64_t seed = static_cast<uint64_t>(std::random_device{}());
|
uint64_t seed = static_cast<uint64_t>(std::random_device{}());
|
||||||
#else
|
#else
|
||||||
uint64_t seed = static_cast<uint64_t>(rand());
|
uint64_t seed = static_cast<uint64_t>(rand());
|
||||||
#endif
|
#endif
|
||||||
ShipUtils::RandInit(seed, state);
|
default_init = true;
|
||||||
|
ShipUtils::RandInit(seed, state);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*state = *state * multiplier + increment;
|
*state = *state * multiplier + increment;
|
||||||
|
|||||||
@@ -1,10 +1,8 @@
|
|||||||
#include "UIWidgets.hpp"
|
#include "UIWidgets.hpp"
|
||||||
#define IMGUI_DEFINE_MATH_OPERATORS
|
#define IMGUI_DEFINE_MATH_OPERATORS
|
||||||
#include <imgui_internal.h>
|
#include <imgui_internal.h>
|
||||||
#include <sstream>
|
|
||||||
#include <libultraship/libultraship.h>
|
#include <libultraship/libultraship.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <random>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <libultraship/libultra/types.h>
|
#include <libultraship/libultra/types.h>
|
||||||
|
|||||||
Reference in New Issue
Block a user