Start work on saving state of the manager

This commit is contained in:
Nikola Petrov 2025-01-04 20:53:34 +01:00
parent ddb19902ee
commit adb0b2b5da
10 changed files with 47 additions and 37 deletions

View File

@ -2,6 +2,7 @@
namespace sys
{
const char *transformFilePath(const char *filename);
size_t saveDataToFile(const char *filename, const char *data, size_t size);
size_t loadDataFromFile(const char *filename, char *data, size_t size);
bool fileExists(const char *filename);

View File

@ -4,18 +4,12 @@
#include <cinttypes>
#include <array>
#include "values/mrand.hpp"
#define MAX_DEPTH 8
#define MAX_POSIBLE_DEPTH 11
static_assert(MAX_DEPTH <= MAX_POSIBLE_DEPTH);
struct uint128
{
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
};
struct Branch
{
uint8_t colorR;

View File

@ -28,6 +28,7 @@ public:
private:
uint128 randSeed;
uint128 id;
std::vector<Dna> vector;
std::list<Dna *> queued;
std::list<Dna *> showed;

View File

@ -1,11 +1,16 @@
#include <inttypes.h>
struct uint128;
struct uint128
{
uint32_t a;
uint32_t b;
uint32_t c;
uint32_t d;
};
namespace mrand
{
void setSeed(unsigned long long seed);
uint128 getState(unsigned long long seed);
float getFloat(uint128 *state);
int getValue(int min, int max, uint128 *state);
uint32_t getInt();
}

View File

@ -3,7 +3,6 @@
#include "App.hpp"
#include "Math.hpp"
#include "values/mrand.hpp"
#include <raylib.h>
#include <raymath.h>

View File

@ -4,8 +4,6 @@
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "Math.hpp"
#include "values/mrand.hpp"
#include "stb_perlin.h"
#include <raylib.h>

View File

@ -2,7 +2,6 @@
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include "values/mrand.hpp"
#include "Math.hpp"
#include <raylib.h>

View File

@ -2,7 +2,6 @@
#include <cstddef>
#include "values/Dna.hpp"
#include "values/mrand.hpp"
#include <raymath.h>

View File

@ -1,28 +1,39 @@
#include <ctime>
#include <stdio.h>
#include <unistd.h>
#include "values/DnaManager.hpp"
#include "values/mrand.hpp"
#include "sys.hpp"
#include <raylib.h>
void DnaManager::init()
{
mrand::setSeed(time(nullptr));
if (sys::fileExists("id"))
{
sys::loadDataFromFile("id", (char *)&randSeed, sizeof(uint128));
sys::loadDataFromFile("id", (char *)&id, sizeof(uint128));
}
else
{
randSeed.a = mrand::getInt();
randSeed.b = mrand::getInt();
randSeed.c = mrand::getInt();
randSeed.d = mrand::getInt();
sys::saveDataToFile("id", (const char *)&randSeed, sizeof(uint128));
id = mrand::getState(time(nullptr));
sys::saveDataToFile("id", (const char *)&id, sizeof(uint128));
}
if (sys::fileExists("data"))
{
const char *filename = sys::transformFilePath("data");
FILE *file = fopen(filename, "rb");
if (file == NULL)
return;
size_t ret = fread(&randSeed, 1, sizeof(uint128), file);
fclose(file);
}
else
{
randSeed = id;
}
vector.resize(NUM_PER_GEN);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
@ -33,6 +44,13 @@ void DnaManager::init()
void DnaManager::deinit()
{
const char *filename = sys::transformFilePath("data");
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
size_t ret = fwrite(&randSeed, 1, sizeof(uint128), file);
fclose(file);
}
Unit DnaManager::next()

View File

@ -2,8 +2,6 @@
#include <algorithm>
#include "values/mrand.hpp"
static uint32_t rprand_state[4] = {0};
static inline uint32_t my_rotate_left(const uint32_t x, int k)
{
return (x << k) | (x >> (32 - k));
@ -36,21 +34,19 @@ uint64_t rprand_splitmix64(uint64_t &rprand_seed)
namespace mrand
{
void setSeed(unsigned long long seed)
uint128 getState(unsigned long long seed)
{
uint64_t rprand_seed = (uint64_t)seed; // Set SplitMix64 seed for further use
uint128 rprand_state;
// To generate the Xoshiro128** state, we use SplitMix64 generator first
// We generate 4 pseudo-random 64bit numbers that we combine using their LSB|MSB
rprand_state[0] = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state[1] = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
rprand_state[2] = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state[3] = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
}
uint32_t getInt()
{
return my_rprand_xoshiro(rprand_state);
rprand_state.a = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state.b = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
rprand_state.c = (uint32_t)(rprand_splitmix64(rprand_seed) & 0xffffffff);
rprand_state.d = (uint32_t)((rprand_splitmix64(rprand_seed) & 0xffffffff00000000) >> 32);
return rprand_state;
}
int getValue(int min, int max, uint128 *state)