Compare commits

...

5 Commits

Author SHA1 Message Date
9f6771d90a use new cinttypes 2025-01-23 14:30:39 +01:00
e7d7131a52 Add coments 2025-01-23 14:16:49 +01:00
7a96951243 extract DnaManager system loading in to seperet implementations 2025-01-23 13:55:33 +01:00
971330b3e0 move stb_perlin.h 2025-01-23 12:17:32 +01:00
6f227026b9 remove Math 2025-01-23 12:14:56 +01:00
14 changed files with 276 additions and 289 deletions

View File

@ -1,6 +1,6 @@
#include <array>
#include "canvas/Canvas.hpp"
#include "values/DnaManager.hpp"
#include "DnaStore.hpp"
class App
{
@ -36,7 +36,7 @@ private:
std::array<UiUnit, 2> unit = {0};
DnaManager manager;
DnaManagerData manager;
Rectangle likedTextBox;
Rectangle genTextBox;

9
inc/DnaStore.hpp Normal file
View File

@ -0,0 +1,9 @@
#include "values/DnaManager.hpp"
namespace DnaStore
{
void load(DnaManagerData *data);
void saveData(DnaManagerData *data);
void saveVec(DnaManagerData *data);
void saveGen(DnaManagerData *data);
} // namespace DnaStore

View File

@ -1,5 +0,0 @@
#include <raylib.h>
Color ColorAdd(Color c1, Color c2);
Color ColorAddValue(Color c, int add);
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom);

View File

@ -28,19 +28,9 @@ struct NetUnit
static_assert(24 == sizeof(NetUnit));
class DnaManager
struct DnaManagerData
{
public:
void init();
void deinit();
UiUnit next();
void like(UiUnit unit);
int generation;
private:
void saveData();
void saveVec();
void saveGen();
uint128 randSeed;
uint128 id;
int queued;
@ -48,6 +38,11 @@ private:
std::vector<Dna> vector;
std::vector<int> liked;
std::vector<int> disliked;
void newGen();
};
namespace DnaManager
{
UiUnit next(DnaManagerData *data);
bool like(UiUnit unit, DnaManagerData *data);
void newGen(DnaManagerData *data);
};

View File

@ -2,7 +2,6 @@
#include <cmath>
#include "App.hpp"
#include "Math.hpp"
#include <raylib.h>
#include <raymath.h>
@ -10,6 +9,44 @@
#define TOP (1 - pos)
#define BOTTOM pos
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom)
{
float br = box.width / box.height;
float tr = textW / textH;
Rectangle ret = {0, 0, 0, 0};
float hm = box.height * margin;
float wm = box.width * margin;
ret.height = box.height - hm;
ret.width = box.width - wm;
if (br < tr)
{
// bolj kvadrat izracunaj visino iz sirine
ret.height = ret.width / tr;
}
else
{
// bolj podolgovat izracunaj sirino iz visine
ret.width = ret.height * tr;
}
if (topOrBottom)
{
ret.y = box.y + hm;
}
else
{
ret.y = box.y + box.height - hm - ret.height;
}
ret.x = box.x + wm;
return ret;
}
// Dimentions for font size 20
// DISLIKE 83
// LIKE 46
@ -30,7 +67,7 @@ void App::init(int screenWidth, int screenHeight)
canvasTexure[i] = LoadRenderTexture(screenWidth, screenWidth);
}
manager.init();
DnaStore::load(&manager);
upTex(Liked::tbd);
while (!canvas.tick(canvasTexure[TOP]))
{
@ -71,9 +108,17 @@ void App::upTex(Liked liked)
if (liked != Liked::tbd)
{
unit[TOP].liked = liked;
manager.like(unit[TOP]);
bool ng = DnaManager::like(unit[TOP], &manager);
if (ng)
{
DnaStore::saveGen(&manager);
DnaManager::newGen(&manager);
DnaStore::saveVec(&manager);
}
DnaStore::saveData(&manager);
}
unit[TOP] = manager.next();
unit[TOP] = DnaManager::next(&manager);
if (unit[TOP].dna != nullptr)
{
canvas.newGen(canvasTexure[TOP], unit[TOP].dna);
@ -175,5 +220,4 @@ void App::deinit()
UnloadRenderTexture(canvasTexure[i]);
}
canvas.deinit();
manager.deinit();
}

140
src/DnaStore.cpp Normal file
View File

@ -0,0 +1,140 @@
#include <cstdio>
#include <ctime>
#include "sys.hpp"
#include "DnaStore.hpp"
#include <raylib.h>
#define ID_FILE_NAME "ID.bin"
#define DATA_FILE_NAME "DATA.bin"
#define VECTOR_FILE_NAME "VECTOR.bin"
void DnaStore::load(DnaManagerData *data)
{
if (sys::fileExists(ID_FILE_NAME))
{
sys::loadDataFromFile(ID_FILE_NAME, &data->id, sizeof(uint128));
}
else
{
data->id = mrand::getState(time(nullptr));
sys::saveDataToFile(ID_FILE_NAME, &data->id, sizeof(uint128));
}
if (sys::fileExists(DATA_FILE_NAME))
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "rb");
if (file == NULL)
return;
fread(&data->randSeed, 1, sizeof(uint128), file);
fread(&data->generation, 1, sizeof(int), file);
fread(&data->showed, 1, sizeof(int), file);
data->queued = data->showed;
int size, tmp;
fread(&size, sizeof(size), 1, file);
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
data->liked.push_back(tmp);
}
fread(&size, sizeof(size), 1, file);
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
data->disliked.push_back(tmp);
}
fclose(file);
}
else
{
data->randSeed = data->id;
data->queued = 0;
data->showed = 0;
data->generation = 0;
}
data->vector.resize(NUM_PER_GEN);
if (sys::fileExists(VECTOR_FILE_NAME))
{
sys::loadDataFromFile(VECTOR_FILE_NAME, data->vector.data(), sizeof(Dna) * NUM_PER_GEN);
}
else
{
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::newDna(&data->vector[i], &data->randSeed);
}
saveVec(data); // v primeru da prvic ne gres cez celo generacijo se vec ne shrani in bo drugacen ker se bo regeneriral z drugim rand seed
saveData(data); // save ce rand seed se je spremenil in v premeru user ugasne brez da like se rand seed ne shrani in se resetira v ID
}
}
void DnaStore::saveData(DnaManagerData *data)
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(&data->randSeed, 1, sizeof(uint128), file);
fwrite(&data->generation, 1, sizeof(int), file);
fwrite(&data->showed, 1, sizeof(int), file);
int tmp;
tmp = data->liked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(data->liked.data(), sizeof(int) * tmp, 1, file);
tmp = data->disliked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(data->disliked.data(), sizeof(tmp) * tmp, 1, file);
fclose(file);
}
void DnaStore::saveVec(DnaManagerData *data)
{
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(data->vector.data(), sizeof(Dna) * NUM_PER_GEN, 1, file);
fclose(file);
}
void DnaStore::saveGen(DnaManagerData *data)
{
std::vector<NetUnit> gen;
gen.resize(NUM_PER_GEN);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
gen[i].hash = ComputeCRC32((unsigned char *)&data->vector[i], sizeof(Dna));
gen[i].liked = Liked::tbd;
gen[i].index = i;
}
for (auto &&i : data->liked)
{
gen[i].liked = Liked::yes;
}
for (auto &&i : data->disliked)
{
gen[i].liked = Liked::no;
}
const char *fileName = TextFormat("gen%04d.bin", data->generation);
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
}

View File

@ -1,64 +0,0 @@
#include <cmath>
#include <algorithm>
#include "Math.hpp"
#include "values/mrand.hpp"
#include <raymath.h>
Color ColorAddValue(Color c, int add)
{
int r = std::clamp(c.r + add, 0, 255);
int g = std::clamp(c.g + add, 0, 255);
int b = std::clamp(c.b + add, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
}
Color ColorAdd(Color c1, Color c2)
{
int r = std::clamp(c1.r + c2.r, 0, 255);
int g = std::clamp(c1.g + c2.g, 0, 255);
int b = std::clamp(c1.b + c2.b, 0, 255);
int a = std::clamp(c1.a + c2.a, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
}
Rectangle TextInSpace(Rectangle box, float textH, float textW, float margin, bool topOrBottom)
{
float br = box.width / box.height;
float tr = textW / textH;
Rectangle ret = {0, 0, 0, 0};
float hm = box.height * margin;
float wm = box.width * margin;
ret.height = box.height - hm;
ret.width = box.width - wm;
if (br < tr)
{
// bolj kvadrat izracunaj visino iz sirine
ret.height = ret.width / tr;
}
else
{
// bolj podolgovat izracunaj sirino iz visine
ret.width = ret.height * tr;
}
if (topOrBottom)
{
ret.y = box.y + hm;
}
else
{
ret.y = box.y + box.height - hm - ret.height;
}
ret.x = box.x + wm;
return ret;
}

View File

@ -3,8 +3,7 @@
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "Math.hpp"
#include "stb_perlin.h"
#include "canvas/stb_perlin.h"
#include <raylib.h>
#include <rlgl.h>
@ -28,6 +27,25 @@ constexpr static float mounten2max = 0.90;
constexpr static float mounten3min = 0.90f;
constexpr static float mounten3max = 0.95f;
Color ColorAddValue(Color c, int add)
{
int r = std::clamp(c.r + add, 0, 255);
int g = std::clamp(c.g + add, 0, 255);
int b = std::clamp(c.b + add, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, c.a};
}
Color ColorAdd(Color c1, Color c2)
{
int r = std::clamp(c1.r + c2.r, 0, 255);
int g = std::clamp(c1.g + c2.g, 0, 255);
int b = std::clamp(c1.b + c2.b, 0, 255);
int a = std::clamp(c1.a + c2.a, 0, 255);
return {(unsigned char)r, (unsigned char)g, (unsigned char)b, (unsigned char)a};
}
// Public
void BackGround::init(int canvasSize)
{

View File

@ -1,8 +1,6 @@
#include "canvas/Circle.hpp"
#include "sunShader.hpp"
#include "Math.hpp"
#include <raylib.h>
#include <rlgl.h>

View File

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

View File

@ -3,8 +3,6 @@
#include "values/Dna.hpp"
#include <raymath.h>
namespace DNA
{

View File

@ -1,257 +1,112 @@
#include <ctime>
#include <stdio.h>
#include <unistd.h>
#include "values/DnaManager.hpp"
#include "sys.hpp"
#include <raylib.h>
#define ID_FILE_NAME "ID.bin"
#define DATA_FILE_NAME "DATA.bin"
#define VECTOR_FILE_NAME "VECTOR.bin"
void DnaManager::init()
UiUnit DnaManager::next(DnaManagerData *data)
{
if (sys::fileExists(ID_FILE_NAME))
{
sys::loadDataFromFile(ID_FILE_NAME, &id, sizeof(uint128));
}
else
{
id = mrand::getState(time(nullptr));
sys::saveDataToFile(ID_FILE_NAME, &id, sizeof(uint128));
}
if (sys::fileExists(DATA_FILE_NAME))
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "rb");
if (file == NULL)
return;
fread(&randSeed, 1, sizeof(uint128), file);
fread(&generation, 1, sizeof(int), file);
fread(&showed, 1, sizeof(showed), file);
queued = showed;
int size, tmp;
fread(&size, sizeof(size), 1, file);
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
liked.push_back(tmp);
}
fread(&size, sizeof(size), 1, file);
for (int i = 0; i < size; i++)
{
fread(&tmp, sizeof(tmp), 1, file);
if (tmp >= NUM_PER_GEN) // out of bounds error prevention if buffer was in the past biiger then its now
continue;
disliked.push_back(tmp);
}
fclose(file);
}
else
{
randSeed = id;
queued = 0;
showed = 0;
generation = 0;
}
vector.resize(NUM_PER_GEN);
if (sys::fileExists(VECTOR_FILE_NAME))
{
sys::loadDataFromFile(VECTOR_FILE_NAME, vector.data(), sizeof(Dna) * NUM_PER_GEN);
}
else
{
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::newDna(&vector[i], &randSeed);
}
saveVec();
}
}
void DnaManager::deinit()
{
}
void DnaManager::saveData()
{
const char *filename = sys::transformFilePath(DATA_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(&randSeed, 1, sizeof(uint128), file);
fwrite(&generation, 1, sizeof(int), file);
fwrite(&showed, 1, sizeof(showed), file);
int tmp;
tmp = liked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(liked.data(), sizeof(int) * tmp, 1, file);
tmp = disliked.size();
fwrite(&tmp, sizeof(tmp), 1, file);
fwrite(disliked.data(), sizeof(tmp) * tmp, 1, file);
fclose(file);
}
void DnaManager::saveVec()
{
const char *filename = sys::transformFilePath(VECTOR_FILE_NAME);
FILE *file = fopen(filename, "wb");
if (file == NULL)
return;
fwrite(vector.data(), sizeof(Dna) * NUM_PER_GEN, 1, file);
fclose(file);
}
UiUnit DnaManager::next()
{
if (queued >= NUM_PER_GEN)
if (data->queued >= NUM_PER_GEN)
{
return {nullptr, Liked::tbd, -1};
}
Dna *ret = &vector[queued];
int index = queued++;
Dna *ret = &data->vector[data->queued];
int index = data->queued++;
return {ret, Liked::tbd, index};
}
void DnaManager::like(UiUnit unit)
bool DnaManager::like(UiUnit unit, DnaManagerData *data)
{
int found = -1;
if (unit.index == showed)
if (unit.index == data->showed)
{
found = showed;
found = data->showed;
}
if (found == -1)
{
// RUN OUT OF GEN WAITING FOR NEW GEN
return;
return false;
}
if (unit.liked == Liked::yes)
{
liked.push_back(found);
data->liked.push_back(found);
}
else if (unit.liked == Liked::no)
{
disliked.push_back(found);
data->disliked.push_back(found);
}
else
{
TraceLog(LOG_ERROR, "LIKED WITH NO VAULE");
return;
// could be infinite loop if something went wrong and user lost UiUnit and next is returning null thinking that it queued everiting
// maybe return true to move on and maybe tread Liked::tbd as Liked::no
return false;
}
showed++;
data->showed++;
if (showed >= NUM_PER_GEN && queued >= NUM_PER_GEN) // if buffer was biger in the past showed could be more then NUM_PER_GEN so its changed to >= insted of ==
if (data->showed >= NUM_PER_GEN && data->queued >= NUM_PER_GEN) // if buffer was biger in the past showed could be more then NUM_PER_GEN so its changed to >= insted of ==
{
saveGen();
newGen();
queued = 0;
showed = 0;
generation += 1;
saveVec();
return true;
}
saveData();
return false;
}
void DnaManager::saveGen()
void DnaManager::newGen(DnaManagerData *data)
{
std::vector<NetUnit> gen;
gen.resize(NUM_PER_GEN);
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
unsigned int *hash = ComputeMD5((unsigned char *)&vector[i], sizeof(Dna));
gen[i].hash.a = hash[0];
gen[i].hash.b = hash[1];
gen[i].hash.c = hash[2];
gen[i].hash.d = hash[3];
gen[i].liked = Liked::tbd;
gen[i].index = i;
}
for (auto &&i : liked)
{
gen[i].liked = Liked::yes;
}
for (auto &&i : disliked)
{
gen[i].liked = Liked::no;
}
const char *fileName = TextFormat("gen%04d.bin", generation);
sys::saveDataToFile(fileName, gen.data(), sizeof(NetUnit) * NUM_PER_GEN);
}
void DnaManager::newGen()
{
if (liked.size() == 0)
data->queued = 0;
data->showed = 0;
data->generation += 1;
if (data->liked.size() == 0)
{
for (std::size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::newDna(&vector[i], &randSeed);
DNA::newDna(&data->vector[i], &data->randSeed);
}
disliked.clear();
data->disliked.clear();
return;
}
if (liked.size() == 1)
if (data->liked.size() == 1)
{
int front = liked.front();
int front = data->liked.front();
for (auto &&i : disliked)
for (auto &&i : data->disliked)
{
DNA::clone(&vector[front], &vector[i], &randSeed);
DNA::clone(&data->vector[front], &data->vector[i], &data->randSeed);
}
disliked.clear();
liked.clear();
data->disliked.clear();
data->liked.clear();
}
if (liked.size() >= 2)
if (data->liked.size() >= 2)
{
for (auto &&i : disliked)
for (auto &&i : data->disliked)
{
size_t p1 = mrand::getValue(0, liked.size() - 1, &randSeed);
size_t p2 = mrand::getValue(0, liked.size() - 1, &randSeed);
size_t p1 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
size_t p2 = mrand::getValue(0, data->liked.size() - 1, &data->randSeed);
while (p1 == p2)
{
p2 = mrand::getValue(0, liked.size(), &randSeed);
p2 = mrand::getValue(0, data->liked.size(), &data->randSeed);
}
p1 = liked[p1];
p2 = liked[p2];
Dna *p1p = &vector[p1];
Dna *p2p = &vector[p2];
Dna *c = &vector[i];
p1 = data->liked[p1];
p2 = data->liked[p2];
Dna *p1p = &data->vector[p1];
Dna *p2p = &data->vector[p2];
Dna *c = &data->vector[i];
DNA::makeChild(p1p, p2p, c, &randSeed);
DNA::makeChild(p1p, p2p, c, &data->randSeed);
}
}
for (size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::mutate(&vector[i], NUM_OF_MUT, &randSeed);
DNA::mutate(&data->vector[i], NUM_OF_MUT, &data->randSeed);
}
disliked.clear();
liked.clear();
}
data->disliked.clear();
data->liked.clear();
}

View File

@ -1,4 +1,4 @@
#include <inttypes.h>
#include <cinttypes>
#include <algorithm>
#include "values/mrand.hpp"