restructure and add server/client example

This commit is contained in:
2025-01-24 10:34:04 +01:00
parent 729b475135
commit ec7b293bd5
30 changed files with 299 additions and 15 deletions

28
shared/inc/TcpSocket.hpp Normal file
View File

@@ -0,0 +1,28 @@
#include <functional>
#include <string>
#include <netinet/in.h>
#define AS_DEFAULT_BUFFER_SIZE 0x1000 /*4096 bytes*/
namespace TcpSocket
{
void setTimeout(int seconds, int sock);
ssize_t sendt(int sock, const void *bytes, size_t byteslength);
ssize_t recvt(int sock, void *bytes, size_t byteslength);
void closet(int sock);
std::string remoteAddress(sockaddr_in &address);
int remotePort(sockaddr_in &address);
int connectt(const char *host, uint16_t port);
typedef std::function<void(int sock, sockaddr_in newSocketInfo)> OnNewConnectionCallBack;
int listent(const char *host, uint16_t port, OnNewConnectionCallBack callback);
} // namespace TcpSocket

View File

@@ -0,0 +1,30 @@
#include <vector>
#include "values/Dna.hpp"
#include <raylib.h>
class BackGround
{
public:
BackGround() = default;
~BackGround() = default;
void init(int canvasSize);
void deinit();
void draw(Dna *dna);
private:
void drawStars();
void drawSun();
void drawMounten(size_t mountenSegments, int min, int max, Color color, float scale);
int getColorSet();
int getTime();
Dna *m_dna;
uint128 mountenSeed;
uint128 starSeed;
int canvasSize = 0;
};

View File

@@ -0,0 +1,14 @@
#include <raylib.h>
class BackGroundColors
{
public:
static void setColor(int color, int time);
static Color backGroundColor;
static Color MountenColor1;
static Color MountenColor2;
static Color MountenColor3;
constexpr static Color moonColor = {240, 240, 190, 255};
constexpr static Color starColor = WHITE;
};

View File

@@ -0,0 +1,19 @@
#include <raylib.h>
#include "BackGround.hpp"
#include "Tree.hpp"
class Canvas
{
public:
Canvas() = default;
~Canvas() = default;
void init(int size);
void deinit();
void newGen(RenderTexture2D &target, Dna *dna);
bool tick(RenderTexture2D &target);
private:
BackGround backGround;
Tree tree;
};

View File

@@ -0,0 +1,26 @@
#include <raylib.h>
class Circle
{
public:
static void init();
static void deinit();
static void setColor(Color color);
static void setSoftEdge(bool soft);
static void draw(float x, float y, float size);
Circle() = delete;
private:
static const int sizeTexute = 250;
static RenderTexture2D target;
static Shader shader;
static float radius;
static float start_transperency;
static float c[3];
static int radius_loc;
static int start_transperency_loc;
static int colorLoc;
};

View File

@@ -0,0 +1,44 @@
#include <cstdint>
#include <vector>
#include <list>
#include <raylib.h>
#include "values/Dna.hpp"
struct DrawArgs
{
Vector2 start;
float angleDeg;
int dep;
Color parent;
int size;
};
class Tree
{
public:
Tree() = default;
~Tree() = default;
void init(int size);
void draw(Dna *dna);
bool tick();
private:
Dna *m_dna;
uint128 branchSeed;
int canvasSize = 0;
Vector2 start = {0};
std::list<DrawArgs> drawCalls;
void drawBranch();
inline size_t getNumOfBranches(int dep);
inline Color getStartColor(DrawArgs &arg);
inline Color getEndColor(int dep, Color &start);
inline int getStartSize(DrawArgs &arg);
inline int getEndSize(DrawArgs &arg, int start);
inline float getLength(DrawArgs &arg);
inline float getAngleVar(DrawArgs &arg);
};

View File

@@ -0,0 +1,428 @@
// stb_perlin.h - v0.5 - perlin noise
// public domain single-file C implementation by Sean Barrett
//
// LICENSE
//
// See end of file.
//
//
// to create the implementation,
// #define STB_PERLIN_IMPLEMENTATION
// in *one* C/CPP file that includes this file.
//
//
// Documentation:
//
// float stb_perlin_noise3( float x,
// float y,
// float z,
// int x_wrap=0,
// int y_wrap=0,
// int z_wrap=0)
//
// This function computes a random value at the coordinate (x,y,z).
// Adjacent random values are continuous but the noise fluctuates
// its randomness with period 1, i.e. takes on wholly unrelated values
// at integer points. Specifically, this implements Ken Perlin's
// revised noise function from 2002.
//
// The "wrap" parameters can be used to create wraparound noise that
// wraps at powers of two. The numbers MUST be powers of two. Specify
// 0 to mean "don't care". (The noise always wraps every 256 due
// details of the implementation, even if you ask for larger or no
// wrapping.)
//
// float stb_perlin_noise3_seed( float x,
// float y,
// float z,
// int x_wrap=0,
// int y_wrap=0,
// int z_wrap=0,
// int seed)
//
// As above, but 'seed' selects from multiple different variations of the
// noise function. The current implementation only uses the bottom 8 bits
// of 'seed', but possibly in the future more bits will be used.
//
//
// Fractal Noise:
//
// Three common fractal noise functions are included, which produce
// a wide variety of nice effects depending on the parameters
// provided. Note that each function will call stb_perlin_noise3
// 'octaves' times, so this parameter will affect runtime.
//
// float stb_perlin_ridge_noise3(float x, float y, float z,
// float lacunarity, float gain, float offset, int octaves)
//
// float stb_perlin_fbm_noise3(float x, float y, float z,
// float lacunarity, float gain, int octaves)
//
// float stb_perlin_turbulence_noise3(float x, float y, float z,
// float lacunarity, float gain, int octaves)
//
// Typical values to start playing with:
// octaves = 6 -- number of "octaves" of noise3() to sum
// lacunarity = ~ 2.0 -- spacing between successive octaves (use exactly 2.0 for wrapping output)
// gain = 0.5 -- relative weighting applied to each successive octave
// offset = 1.0? -- used to invert the ridges, may need to be larger, not sure
//
//
// Contributors:
// Jack Mott - additional noise functions
// Jordan Peck - seeded noise
//
#ifdef __cplusplus
extern "C" {
#endif
extern float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap);
extern float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed);
extern float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves);
extern float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
extern float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves);
extern float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed);
#ifdef __cplusplus
}
#endif
#ifdef STB_PERLIN_IMPLEMENTATION
#include <math.h> // fabs()
// not same permutation table as Perlin's reference to avoid copyright issues;
// Perlin's table can be found at http://mrl.nyu.edu/~perlin/noise/
static unsigned char stb__perlin_randtab[512] =
{
23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
// and a second copy so we don't need an extra mask or static initializer
23, 125, 161, 52, 103, 117, 70, 37, 247, 101, 203, 169, 124, 126, 44, 123,
152, 238, 145, 45, 171, 114, 253, 10, 192, 136, 4, 157, 249, 30, 35, 72,
175, 63, 77, 90, 181, 16, 96, 111, 133, 104, 75, 162, 93, 56, 66, 240,
8, 50, 84, 229, 49, 210, 173, 239, 141, 1, 87, 18, 2, 198, 143, 57,
225, 160, 58, 217, 168, 206, 245, 204, 199, 6, 73, 60, 20, 230, 211, 233,
94, 200, 88, 9, 74, 155, 33, 15, 219, 130, 226, 202, 83, 236, 42, 172,
165, 218, 55, 222, 46, 107, 98, 154, 109, 67, 196, 178, 127, 158, 13, 243,
65, 79, 166, 248, 25, 224, 115, 80, 68, 51, 184, 128, 232, 208, 151, 122,
26, 212, 105, 43, 179, 213, 235, 148, 146, 89, 14, 195, 28, 78, 112, 76,
250, 47, 24, 251, 140, 108, 186, 190, 228, 170, 183, 139, 39, 188, 244, 246,
132, 48, 119, 144, 180, 138, 134, 193, 82, 182, 120, 121, 86, 220, 209, 3,
91, 241, 149, 85, 205, 150, 113, 216, 31, 100, 41, 164, 177, 214, 153, 231,
38, 71, 185, 174, 97, 201, 29, 95, 7, 92, 54, 254, 191, 118, 34, 221,
131, 11, 163, 99, 234, 81, 227, 147, 156, 176, 17, 142, 69, 12, 110, 62,
27, 255, 0, 194, 59, 116, 242, 252, 19, 21, 187, 53, 207, 129, 64, 135,
61, 40, 167, 237, 102, 223, 106, 159, 197, 189, 215, 137, 36, 32, 22, 5,
};
// perlin's gradient has 12 cases so some get used 1/16th of the time
// and some 2/16ths. We reduce bias by changing those fractions
// to 5/64ths and 6/64ths
// this array is designed to match the previous implementation
// of gradient hash: indices[stb__perlin_randtab[i]&63]
static unsigned char stb__perlin_randtab_grad_idx[512] =
{
7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,
8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,
7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,
8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,
5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,
2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,
9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,
1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,
10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,
6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,
4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,
11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,
10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,
3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,
11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,
9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,
// and a second copy so we don't need an extra mask or static initializer
7, 9, 5, 0, 11, 1, 6, 9, 3, 9, 11, 1, 8, 10, 4, 7,
8, 6, 1, 5, 3, 10, 9, 10, 0, 8, 4, 1, 5, 2, 7, 8,
7, 11, 9, 10, 1, 0, 4, 7, 5, 0, 11, 6, 1, 4, 2, 8,
8, 10, 4, 9, 9, 2, 5, 7, 9, 1, 7, 2, 2, 6, 11, 5,
5, 4, 6, 9, 0, 1, 1, 0, 7, 6, 9, 8, 4, 10, 3, 1,
2, 8, 8, 9, 10, 11, 5, 11, 11, 2, 6, 10, 3, 4, 2, 4,
9, 10, 3, 2, 6, 3, 6, 10, 5, 3, 4, 10, 11, 2, 9, 11,
1, 11, 10, 4, 9, 4, 11, 0, 4, 11, 4, 0, 0, 0, 7, 6,
10, 4, 1, 3, 11, 5, 3, 4, 2, 9, 1, 3, 0, 1, 8, 0,
6, 7, 8, 7, 0, 4, 6, 10, 8, 2, 3, 11, 11, 8, 0, 2,
4, 8, 3, 0, 0, 10, 6, 1, 2, 2, 4, 5, 6, 0, 1, 3,
11, 9, 5, 5, 9, 6, 9, 8, 3, 8, 1, 8, 9, 6, 9, 11,
10, 7, 5, 6, 5, 9, 1, 3, 7, 0, 2, 10, 11, 2, 6, 1,
3, 11, 7, 7, 2, 1, 7, 3, 0, 8, 1, 1, 5, 0, 6, 10,
11, 11, 0, 2, 7, 0, 10, 8, 3, 5, 7, 1, 11, 1, 0, 7,
9, 0, 11, 5, 10, 3, 2, 3, 5, 9, 7, 9, 8, 4, 6, 5,
};
static float stb__perlin_lerp(float a, float b, float t)
{
return a + (b-a) * t;
}
static int stb__perlin_fastfloor(float a)
{
int ai = (int) a;
return (a < ai) ? ai-1 : ai;
}
// different grad function from Perlin's, but easy to modify to match reference
static float stb__perlin_grad(int grad_idx, float x, float y, float z)
{
static float basis[12][4] =
{
{ 1, 1, 0 },
{ -1, 1, 0 },
{ 1,-1, 0 },
{ -1,-1, 0 },
{ 1, 0, 1 },
{ -1, 0, 1 },
{ 1, 0,-1 },
{ -1, 0,-1 },
{ 0, 1, 1 },
{ 0,-1, 1 },
{ 0, 1,-1 },
{ 0,-1,-1 },
};
float *grad = basis[grad_idx];
return grad[0]*x + grad[1]*y + grad[2]*z;
}
float stb_perlin_noise3_internal(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
{
float u,v,w;
float n000,n001,n010,n011,n100,n101,n110,n111;
float n00,n01,n10,n11;
float n0,n1;
unsigned int x_mask = (x_wrap-1) & 255;
unsigned int y_mask = (y_wrap-1) & 255;
unsigned int z_mask = (z_wrap-1) & 255;
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x0 = px & x_mask, x1 = (px+1) & x_mask;
int y0 = py & y_mask, y1 = (py+1) & y_mask;
int z0 = pz & z_mask, z1 = (pz+1) & z_mask;
int r0,r1, r00,r01,r10,r11;
#define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
x -= px; u = stb__perlin_ease(x);
y -= py; v = stb__perlin_ease(y);
z -= pz; w = stb__perlin_ease(z);
r0 = stb__perlin_randtab[x0+seed];
r1 = stb__perlin_randtab[x1+seed];
r00 = stb__perlin_randtab[r0+y0];
r01 = stb__perlin_randtab[r0+y1];
r10 = stb__perlin_randtab[r1+y0];
r11 = stb__perlin_randtab[r1+y1];
n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );
n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );
n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );
n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );
n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );
n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );
n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );
n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );
n00 = stb__perlin_lerp(n000,n001,w);
n01 = stb__perlin_lerp(n010,n011,w);
n10 = stb__perlin_lerp(n100,n101,w);
n11 = stb__perlin_lerp(n110,n111,w);
n0 = stb__perlin_lerp(n00,n01,v);
n1 = stb__perlin_lerp(n10,n11,v);
return stb__perlin_lerp(n0,n1,u);
}
float stb_perlin_noise3(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap)
{
return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap,0);
}
float stb_perlin_noise3_seed(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, int seed)
{
return stb_perlin_noise3_internal(x,y,z,x_wrap,y_wrap,z_wrap, (unsigned char) seed);
}
float stb_perlin_ridge_noise3(float x, float y, float z, float lacunarity, float gain, float offset, int octaves)
{
int i;
float frequency = 1.0f;
float prev = 1.0f;
float amplitude = 0.5f;
float sum = 0.0f;
for (i = 0; i < octaves; i++) {
float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i);
r = offset - (float) fabs(r);
r = r*r;
sum += r*amplitude*prev;
prev = r;
frequency *= lacunarity;
amplitude *= gain;
}
return sum;
}
float stb_perlin_fbm_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)
{
int i;
float frequency = 1.0f;
float amplitude = 1.0f;
float sum = 0.0f;
for (i = 0; i < octaves; i++) {
sum += stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;
frequency *= lacunarity;
amplitude *= gain;
}
return sum;
}
float stb_perlin_turbulence_noise3(float x, float y, float z, float lacunarity, float gain, int octaves)
{
int i;
float frequency = 1.0f;
float amplitude = 1.0f;
float sum = 0.0f;
for (i = 0; i < octaves; i++) {
float r = stb_perlin_noise3_internal(x*frequency,y*frequency,z*frequency,0,0,0,(unsigned char)i)*amplitude;
sum += (float) fabs(r);
frequency *= lacunarity;
amplitude *= gain;
}
return sum;
}
float stb_perlin_noise3_wrap_nonpow2(float x, float y, float z, int x_wrap, int y_wrap, int z_wrap, unsigned char seed)
{
float u,v,w;
float n000,n001,n010,n011,n100,n101,n110,n111;
float n00,n01,n10,n11;
float n0,n1;
int px = stb__perlin_fastfloor(x);
int py = stb__perlin_fastfloor(y);
int pz = stb__perlin_fastfloor(z);
int x_wrap2 = (x_wrap ? x_wrap : 256);
int y_wrap2 = (y_wrap ? y_wrap : 256);
int z_wrap2 = (z_wrap ? z_wrap : 256);
int x0 = px % x_wrap2, x1;
int y0 = py % y_wrap2, y1;
int z0 = pz % z_wrap2, z1;
int r0,r1, r00,r01,r10,r11;
if (x0 < 0) x0 += x_wrap2;
if (y0 < 0) y0 += y_wrap2;
if (z0 < 0) z0 += z_wrap2;
x1 = (x0+1) % x_wrap2;
y1 = (y0+1) % y_wrap2;
z1 = (z0+1) % z_wrap2;
#define stb__perlin_ease(a) (((a*6-15)*a + 10) * a * a * a)
x -= px; u = stb__perlin_ease(x);
y -= py; v = stb__perlin_ease(y);
z -= pz; w = stb__perlin_ease(z);
r0 = stb__perlin_randtab[x0];
r0 = stb__perlin_randtab[r0+seed];
r1 = stb__perlin_randtab[x1];
r1 = stb__perlin_randtab[r1+seed];
r00 = stb__perlin_randtab[r0+y0];
r01 = stb__perlin_randtab[r0+y1];
r10 = stb__perlin_randtab[r1+y0];
r11 = stb__perlin_randtab[r1+y1];
n000 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z0], x , y , z );
n001 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r00+z1], x , y , z-1 );
n010 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z0], x , y-1, z );
n011 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r01+z1], x , y-1, z-1 );
n100 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z0], x-1, y , z );
n101 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r10+z1], x-1, y , z-1 );
n110 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z0], x-1, y-1, z );
n111 = stb__perlin_grad(stb__perlin_randtab_grad_idx[r11+z1], x-1, y-1, z-1 );
n00 = stb__perlin_lerp(n000,n001,w);
n01 = stb__perlin_lerp(n010,n011,w);
n10 = stb__perlin_lerp(n100,n101,w);
n11 = stb__perlin_lerp(n110,n111,w);
n0 = stb__perlin_lerp(n00,n01,v);
n1 = stb__perlin_lerp(n10,n11,v);
return stb__perlin_lerp(n0,n1,u);
}
#endif // STB_PERLIN_IMPLEMENTATION
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Sean Barrett
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/

55
shared/inc/values/Dna.hpp Normal file
View File

@@ -0,0 +1,55 @@
#ifndef DNA_H
#define DNA_H
#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 Branch
{
uint8_t colorR;
uint8_t colorG;
uint8_t colorB;
int8_t colorR_change;
int8_t colorG_change;
int8_t colorB_change;
uint8_t colorVar;
uint8_t size;
uint8_t sizeParent;
uint8_t sizeLevel;
uint8_t sizeChange;
uint8_t sizeVar;
uint8_t length;
uint8_t lengthVar;
uint8_t branchCount;
uint8_t branchAngleVar;
};
struct Dna
{
uint128 mountenSeed;
uint128 starSeed;
uint128 branchSeed;
uint8_t moonX;
uint8_t moonY;
uint8_t moonSize;
uint8_t colorSet;
Branch branches[MAX_DEPTH];
};
namespace DNA
{
void newDna(Dna *dna, uint128 *state);
void makeChild(Dna *p1, Dna *p2, Dna *c, uint128 *state);
void clone(Dna *p1, Dna *c, uint128 *state);
void mutate(Dna *dna, uint32_t num, uint128 *state);
}
#endif /* DNA_H */

View File

@@ -0,0 +1,48 @@
#include "values/Dna.hpp"
#include <list>
#include <vector>
#define NUM_PER_GEN 10
#define NUM_OF_MUT 1
enum Liked
{
tbd,
yes,
no
};
struct UiUnit
{
Dna *dna;
Liked liked;
int index;
};
struct NetUnit
{
uint32_t hash;
uint32_t index;
Liked liked;
};
static_assert(12 == sizeof(NetUnit));
struct DnaManagerData
{
int generation;
uint128 randSeed;
uint128 id;
int queued;
int showed;
std::vector<Dna> vector;
std::vector<int> liked;
std::vector<int> disliked;
};
namespace DnaManager
{
UiUnit next(DnaManagerData *data);
bool like(UiUnit unit, DnaManagerData *data);
void newGen(DnaManagerData *data);
};

View File

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

169
shared/src/TcpSocket.cpp Normal file
View File

@@ -0,0 +1,169 @@
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <netdb.h>
#include <string>
#include <functional>
#include <cerrno>
#include <cstring>
#include <thread>
namespace TcpSocket
{
void setTimeout(int seconds, int sock)
{
struct timeval tv;
tv.tv_sec = seconds;
tv.tv_usec = 0;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
}
ssize_t sendt(int sock, const void *bytes, size_t byteslength) { return send(sock, bytes, byteslength, 0); }
ssize_t recvt(int sock, void *bytes, size_t byteslength) { return recv(sock, bytes, byteslength, 0); }
void closet(int sock)
{
shutdown(sock, SHUT_RDWR);
close(sock);
}
std::string remoteAddress(sockaddr_in &address)
{
char ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &(address.sin_addr), ip, INET_ADDRSTRLEN);
return std::string(ip);
}
int remotePort(sockaddr_in &address) { return ntohs(address.sin_port); }
int connectt(const char *host, uint16_t port)
{
struct addrinfo hints, *res, *it;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
// Get address info from DNS
int status = getaddrinfo(host, NULL, &hints, &res);
if (status != 0)
{
// onError(errno, "Invalid address." + std::string(gai_strerror(status)));
return -1;
}
sockaddr_in address;
for (it = res; it != NULL; it = it->ai_next)
{
if (it->ai_family == AF_INET)
{ // IPv4
memcpy((void *)(&address), (void *)it->ai_addr, sizeof(sockaddr_in));
break; // for now, just get the first ip (ipv4).
}
}
freeaddrinfo(res);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
// onError(errno, "Socket creating error.");
return -2;
}
address.sin_family = AF_INET;
address.sin_port = htons(port);
// setTimeout(5, sock);
// Try to connect.
status = connect(sock, (const sockaddr *)&address, sizeof(sockaddr_in));
if (status == -1)
{
// onError(errno, "Connection failed to the host.");
close(sock);
return -3;
}
return sock;
}
typedef std::function<void(int sock, sockaddr_in newSocketInfo)> OnNewConnectionCallBack;
int listent(const char *host, uint16_t port, OnNewConnectionCallBack callback)
{
sockaddr_in address;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock == -1)
{
// onError(errno, "Socket creating error.");
return -1;
}
int opt = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(int));
int status = inet_pton(AF_INET, host, &address.sin_addr);
switch (status)
{
case -1:
close(sock);
// onError(errno, "Invalid address. Address type not supported.");
return -2;
case 0:
close(sock);
// onError(errno, "AF_INET is not supported. Please send message to developer.");
return -3;
default:
break;
}
address.sin_family = AF_INET;
address.sin_port = htons(port);
if (bind(sock, (const sockaddr *)&address, sizeof(address)) == -1)
{
// onError(errno, "Cannot bind the socket.");
close(sock);
return -4;
}
if (listen(sock, 20) == -1)
{
// onError(errno, "Error: Server can't listen the socket.");
close(sock);
return -5;
}
sockaddr_in newSocketInfo;
socklen_t newSocketInfoLength = sizeof(newSocketInfo);
int newSocketFileDescriptor = -1;
while (true)
{
newSocketFileDescriptor = accept(sock, (sockaddr *)&newSocketInfo, &newSocketInfoLength);
if (newSocketFileDescriptor == -1)
{
if (errno == EBADF || errno == EINVAL)
return -6;
return -7;
}
std::thread t(callback, newSocketFileDescriptor, newSocketInfo);
t.detach();
}
return 0;
}
}

View File

@@ -0,0 +1,226 @@
#include <cmath>
#include "canvas/BackGround.hpp"
#include "canvas/BackGroundColors.hpp"
#include "canvas/Circle.hpp"
#include "canvas/stb_perlin.h"
#include <raylib.h>
#include <rlgl.h>
#include <raymath.h>
constexpr static size_t numOfStarts = 150;
constexpr static float moonXOffset = 0.1f;
constexpr static float minSizeOfMoon = 0.1f;
constexpr static float maxSizeOfMoon = 0.15f;
constexpr static float maxYPosOfMoon = 0.80f;
constexpr static float bigRingRatio = 0.5f;
constexpr static float smallRingRatio = 0.25f;
constexpr static float bigRingBlend = 0.02f;
constexpr static float smallRingBlend = 0.05f;
constexpr static float colorRatio1 = 0.3f;
constexpr static float colorRatio2 = 0.7f;
constexpr static float mounten1min = 0.65f;
constexpr static float mounten1max = 0.85f;
constexpr static float mounten2min = 0.80f;
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)
{
this->canvasSize = canvasSize;
}
void BackGround::deinit()
{
}
void BackGround::draw(Dna *dna)
{
Circle::setSoftEdge(true);
m_dna = dna;
mountenSeed = dna->mountenSeed;
starSeed = dna->starSeed;
int time = getTime();
int colorSet = getColorSet();
BackGroundColors::setColor(colorSet, time);
if (colorSet == 3)
{
ClearBackground(BackGroundColors::backGroundColor);
drawStars();
}
else
{
DrawRectangleGradientV(0, 0, canvasSize, canvasSize, ColorAddValue(BackGroundColors::backGroundColor, 60), BackGroundColors::backGroundColor);
}
drawSun();
drawMounten(150, (int)(mounten1min * canvasSize), (int)(mounten1max * canvasSize), BackGroundColors::MountenColor1, 5);
drawMounten(100, (int)(mounten2min * canvasSize), (int)(mounten2max * canvasSize), BackGroundColors::MountenColor2, 3);
drawMounten(50, (int)(mounten3min * canvasSize), (int)(mounten3max * canvasSize), BackGroundColors::MountenColor3, 1);
}
void BackGround::drawStars()
{
rlSetTexture(1);
rlBegin(RL_TRIANGLES);
rlNormal3f(0.0f, 0.0f, 1.0f);
bool star = true;
for (size_t i = 0; i < numOfStarts; i++)
{
int x = mrand::getFloat(&starSeed) * canvasSize;
int y = mrand::getFloat(&starSeed) * canvasSize;
int weight = mrand::getFloat(&starSeed) * 3 + 1;
float alph = Normalize(mrand::getFloat(&starSeed), 0.04f, 0.8f);
Color color = ColorLerp(BackGroundColors::backGroundColor, BackGroundColors::starColor, alph);
rlColor4ub(color.r, color.g, color.b, color.a);
if (star)
{
// topLeft
rlVertex2f(x, y);
// bottomLeft
rlVertex2f(x, y + weight);
// topRight
rlVertex2f(x + weight, y);
}
else
{
// bottomRight
rlVertex2f(x + weight, y + weight);
// topRight
rlVertex2f(x + weight, y);
// bottomLeft
rlVertex2f(x, y + weight);
}
star = !star;
}
rlEnd();
rlSetTexture(0);
}
void BackGround::drawSun()
{
int r = ((m_dna->moonY / 255.0f * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
int xpos = Lerp(canvasSize * moonXOffset, canvasSize - canvasSize * moonXOffset, m_dna->moonX / 255.0f);
int ypos = Lerp(canvasSize * moonXOffset, maxYPosOfMoon * canvasSize, m_dna->moonY / 255.0f);
if (getColorSet() == 3)
{
Circle::setColor(BackGroundColors::moonColor);
r = (((m_dna->moonSize / 255.0f) * (maxSizeOfMoon - minSizeOfMoon)) + minSizeOfMoon) * canvasSize;
}
else
{
Color color = {0};
color.r = 255;
color.g = std::lerp(200, 50, m_dna->moonY / 255.0f);
color.b = std::lerp(50, 0, m_dna->moonY / 255.0f);
color.a = 255;
Circle::setColor(color);
}
Circle::draw(xpos, ypos, r);
}
void BackGround::drawMounten(size_t mountenSegments, int min, int max, Color color, float scale)
{
float x = 0;
float diff = (float)canvasSize / (mountenSegments - 1);
float ny = mrand::getFloat(&mountenSeed);
float nz = mrand::getFloat(&mountenSeed);
int offsetX = mrand::getFloat(&mountenSeed) * 255;
float nx = (float)(0 + offsetX) * (scale / (float)mountenSegments);
float p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
float np = (p + 1.0f) / 2.0f;
int y = Lerp(min, max, np);
rlSetTexture(1);
rlBegin(RL_TRIANGLES);
rlNormal3f(0.0f, 0.0f, 1.0f);
rlColor4ub(color.r, color.g, color.b, color.a);
for (size_t i = 1; i <= mountenSegments; i++)
{
// topLeft
rlVertex2f(x, y);
// bottomLeft
rlVertex2f(x, canvasSize);
nx = (float)(i + offsetX) * (scale / (float)mountenSegments);
p = stb_perlin_fbm_noise3(nx, ny, nz, 2.0f, 0.5f, 6);
if (p < -1.0f)
p = -1.0f;
if (p > 1.0f)
p = 1.0f;
np = (p + 1.0f) / 2.0f;
y = Lerp(min, max, np);
x += diff;
// topRight
rlVertex2f(x, y);
// bottomRight
rlVertex2f(x, canvasSize);
// topRight
rlVertex2f(x, y);
rlVertex2f(x - diff, canvasSize);
}
rlEnd();
rlSetTexture(0);
}
int BackGround::getColorSet()
{
uint8_t colorSet = m_dna->colorSet;
if (colorSet < 64)
return 0;
if (colorSet < 128)
return 1;
if (colorSet < 192)
return 2;
return 3;
}
int BackGround::getTime()
{
uint8_t ret = m_dna->moonY;
if (ret < 64)
return 0;
if (ret < 128)
return 1;
if (ret < 192)
return 2;
return 3;
}

View File

@@ -0,0 +1,104 @@
#include "canvas/BackGroundColors.hpp"
Color BackGroundColors::backGroundColor;
Color BackGroundColors::MountenColor1;
Color BackGroundColors::MountenColor2;
Color BackGroundColors::MountenColor3;
void BackGroundColors::setColor(int color, int time)
{
if (color == 3)
{
backGroundColor = {21, 34, 56, 255};
MountenColor1 = {28, 28, 38, 255};
MountenColor2 = {21, 21, 27, 255};
MountenColor3 = {17, 17, 20, 255};
return;
}
Color colors[3][4][4] = {
{
{
{90, 60, 60, 255},
{118, 91, 91, 255},
{154, 131, 131, 255},
{193, 161, 161, 255},
},
{
{90, 60, 60, 255},
{118, 91, 91, 255},
{154, 131, 131, 255},
{193, 161, 161, 255},
},
{
{70, 40, 40, 255},
{98, 71, 71, 255},
{134, 111, 111, 255},
{173, 141, 141, 255},
},
{
{60, 30, 30, 255},
{88, 61, 61, 255},
{124, 101, 101, 255},
{163, 131, 131, 255},
},
},
{
{
{103, 88, 51, 255},
{148, 127, 73, 255},
{186, 159, 92, 255},
{163, 154, 132, 255},
},
{
{103, 88, 51, 255},
{148, 127, 73, 255},
{186, 159, 92, 255},
{163, 154, 132, 255},
},
{
{83, 68, 31, 255},
{128, 107, 53, 255},
{166, 139, 72, 255},
{143, 134, 112, 255},
},
{
{73, 58, 21, 255},
{118, 97, 43, 255},
{156, 129, 62, 255},
{133, 124, 102, 255},
},
},
{
{
{77, 84, 99, 255},
{104, 126, 144, 255},
{152, 219, 206, 255},
{213, 240, 235, 255},
},
{
{81, 58, 36, 255},
{111, 98, 78, 255},
{180, 148, 119, 255},
{200, 165, 133, 255},
},
{
{67, 44, 22, 255},
{97, 84, 64, 255},
{166, 134, 105, 255},
{186, 151, 119, 255},
},
{
{46, 23, 1, 255},
{76, 63, 43, 255},
{145, 113, 84, 255},
{165, 130, 98, 255},
},
},
};
backGroundColor = colors[color][0][time];
MountenColor1 = colors[color][1][time];
MountenColor2 = colors[color][2][time];
MountenColor3 = colors[color][3][time];
}

View File

@@ -0,0 +1,34 @@
#include "canvas/Canvas.hpp"
#include "canvas/Circle.hpp"
void Canvas::init(int size)
{
backGround.init(size);
tree.init(size);
Circle::init();
}
void Canvas::newGen(RenderTexture2D &target, Dna *dna)
{
BeginTextureMode(target);
ClearBackground(WHITE);
backGround.draw(dna);
tree.draw(dna);
EndTextureMode();
}
bool Canvas::tick(RenderTexture2D &target)
{
BeginTextureMode(target);
bool ret = tree.tick();
EndTextureMode();
return ret;
}
void Canvas::deinit()
{
backGround.deinit();
Circle::deinit();
}

View File

@@ -0,0 +1,83 @@
#include "canvas/Circle.hpp"
#include "sunShader.hpp"
#include <raylib.h>
#include <rlgl.h>
RenderTexture2D Circle::target;
Shader Circle::shader;
float Circle::radius;
float Circle::start_transperency;
float Circle::c[3];
int Circle::radius_loc;
int Circle::start_transperency_loc;
int Circle::colorLoc;
void Circle::init()
{
#if defined(PLATFORM_ANDROID) || defined(PLATFORM_WEB)
shader = LoadShaderFromMemory(0, sun_shader_100);
#else
shader = LoadShaderFromMemory(0, (const char *)shaders_sun_330_fs);
#endif
target = LoadRenderTexture(sizeTexute, sizeTexute);
radius = 0.6f;
radius_loc = GetShaderLocation(shader, "sun_radius");
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
start_transperency = 0.40f;
start_transperency_loc = GetShaderLocation(shader, "start_transperency");
SetShaderValue(shader, start_transperency_loc, &start_transperency, SHADER_UNIFORM_FLOAT);
c[0] = 1.0f;
c[1] = 1.0f;
c[2] = 1.0f;
colorLoc = GetShaderLocation(shader, "color");
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
// resitev da ne postane tekstura okoli sonca transparentna in da se ne vidi nasledna slika
// https://github.com/raysan5/raylib/issues/3820
rlSetBlendFactorsSeparate(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA, RL_ONE, RL_ONE, RL_FUNC_ADD, RL_MAX);
}
void Circle::deinit()
{
UnloadShader(shader);
UnloadRenderTexture(target);
}
void Circle::setColor(Color color)
{
c[0] = color.r / 255.0f;
c[1] = color.g / 255.0f;
c[2] = color.b / 255.0f;
SetShaderValue(shader, colorLoc, c, SHADER_UNIFORM_VEC3);
}
void Circle::setSoftEdge(bool soft)
{
if (soft)
{
radius = 0.6f;
}
else
{
radius = 1.0f;
}
SetShaderValue(shader, radius_loc, &radius, SHADER_UNIFORM_FLOAT);
}
void Circle::draw(float x, float y, float size)
{
Rectangle dest = {x - size, y - size, size * 2, size * 2};
Rectangle source = {0, 0, (float)target.texture.width, (float)-target.texture.height};
Vector2 origin = {0.0f, 0.0f};
// zgorni komentar da se mesanje barv oklopi pravilno
BeginBlendMode(RL_BLEND_CUSTOM_SEPARATE);
BeginShaderMode(shader);
DrawTexturePro(target.texture, source, dest, origin, 0.0f, WHITE);
EndShaderMode();
EndBlendMode();
}

206
shared/src/canvas/Tree.cpp Normal file
View File

@@ -0,0 +1,206 @@
#include <cmath>
#include "canvas/Tree.hpp"
#include "canvas/Circle.hpp"
#include <raylib.h>
#include <raymath.h>
#define ITER_PER_FRAME 5000
constexpr int maxColorChange = 15;
constexpr int minColorChange = -15;
constexpr float colorParentMix = 0.6f;
constexpr int maxSize = 20;
constexpr int minSize = 2;
constexpr int maxSizeVar = 5;
constexpr int minSizeVar = -5;
constexpr int maxSizeChange = 5;
constexpr int MinSizeChange = -5;
constexpr int sizes[] = {2, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
static_assert(sizeof(sizes) / sizeof(int) == MAX_POSIBLE_DEPTH);
float lengths[MAX_DEPTH];
constexpr float maxAngles[] = {5.0f, 5.0f, 5.0f, 10.0f, 10.0f, 10.0f, 15.0f, 15.0f, 20.0f, 20.0f, 20.0f};
static_assert(sizeof(maxAngles) / sizeof(float) == MAX_POSIBLE_DEPTH);
void calculateLevels(int canvasSize)
{
lengths[0] = canvasSize / 4.0f;
for (size_t i = 1; i < MAX_DEPTH; i++)
{
lengths[i] = lengths[i - 1] * 0.7f;
}
}
// Public
void Tree::init(int size)
{
this->canvasSize = size;
start.x = size / 2;
start.y = size;
calculateLevels(size);
}
void Tree::draw(Dna *dna)
{
Circle::setSoftEdge(false);
m_dna = dna;
branchSeed = dna->branchSeed;
drawCalls.push_back({start, 180.0f, 0});
tick();
}
bool Tree::tick()
{
size_t i = 0;
while (!drawCalls.empty())
{
drawBranch();
drawCalls.pop_front();
i++;
if (i >= ITER_PER_FRAME)
break;
}
return drawCalls.empty();
}
// Private
void Tree::drawBranch()
{
DrawArgs arg = drawCalls.front();
if (arg.dep == MAX_DEPTH)
return;
float angleVar = getAngleVar(arg);
float angle = ((arg.angleDeg + angleVar) * PI) / 180.0f;
float length = getLength(arg);
float nx = length * std::sin(angle);
float ny = length * std::cos(angle);
Vector2 end = {arg.start.x + nx, arg.start.y + ny};
int sizeStart = getStartSize(arg);
int sizeEnd = getEndSize(arg, sizeStart);
float fstep = 1.0 / ((length / sizeStart) * 2.0f);
Color colorStart = getStartColor(arg);
Color colorEnd = getEndColor(arg.dep, colorStart);
for (float i = 0; i < 1; i += fstep)
{
Vector2 point = Vector2Lerp(arg.start, end, i);
Color color = ColorLerp(colorStart, colorEnd, i);
int size = Lerp(sizeStart, sizeEnd, i);
DrawCircleV(point, size, color); // Fester on the phone to call DrawCircle insted of the Circle shader
// Circle::setColor(color);
// Circle::draw(point.x, point.y, thick); // TODO Change to BeginShaderMode and EndShaderMode only onece
// use
// DrawRectangleGradientEx
}
// add more branches to draw
if (arg.dep + 1 >= MAX_DEPTH)
return;
float sectors = getNumOfBranches(arg.dep) + 1;
float degres = 180.0f / sectors;
for (size_t i = 0; i < getNumOfBranches(arg.dep); i++)
{
float newAngle = arg.angleDeg - 90 + (degres * (i + 1));
drawCalls.push_back({end, newAngle, arg.dep + 1, colorEnd, sizeEnd});
}
}
inline size_t Tree::getNumOfBranches(int dep)
{
if (m_dna->branches[dep].branchCount < 128)
return 2;
else
return 3;
}
inline Color Tree::getStartColor(DrawArgs &arg)
{
Color ret = {
m_dna->branches[arg.dep].colorR,
m_dna->branches[arg.dep].colorG,
m_dna->branches[arg.dep].colorB,
255};
if (arg.dep > 0)
{
ret = ColorLerp(ret, arg.parent, colorParentMix);
}
int colorVar = Remap(m_dna->branches[arg.dep].colorVar, 0, 255, minColorChange, maxColorChange);
ret.r += colorVar * mrand::getFloat(&branchSeed);
ret.g += colorVar * mrand::getFloat(&branchSeed);
ret.b += colorVar * mrand::getFloat(&branchSeed);
return ret;
}
inline Color Tree::getEndColor(int dep, Color &start)
{
uint8_t r = start.r + m_dna->branches[dep].colorR_change;
uint8_t g = start.g + m_dna->branches[dep].colorG_change;
uint8_t b = start.b + m_dna->branches[dep].colorB_change;
return {r, g, b, 255};
}
inline int Tree::getStartSize(DrawArgs &arg)
{
int size = Remap(m_dna->branches[arg.dep].size, 0, 255, minSize, maxSize);
size += Remap(m_dna->branches[arg.dep].sizeVar, 0, 255, minSizeVar, maxSizeVar) * mrand::getFloat(&branchSeed);
if (arg.dep > 0)
{
float sizeParent = m_dna->branches[arg.dep].sizeParent / 255.0f;
size = std::lerp(size, arg.size, sizeParent);
}
float mixLevel = m_dna->branches[arg.dep].sizeLevel / 255.0f;
size = std::lerp(size, sizes[MAX_DEPTH - arg.dep - 1], mixLevel);
if (size < 1)
size = 1;
return size;
}
inline int Tree::getEndSize(DrawArgs &arg, int start)
{
int size = Remap(m_dna->branches[arg.dep].sizeChange, 0, 255, MinSizeChange, maxSizeChange);
size += start;
if (size < 1)
size = 1;
return size;
}
inline float Tree::getLength(DrawArgs &arg)
{
float lenght = lengths[arg.dep];
float lenghtRatio = Remap(m_dna->branches[arg.dep].length, 0, 255, 0.5f, 1.3f);
lenght *= lenghtRatio;
float lenghtVar = Remap(m_dna->branches[arg.dep].lengthVar, 0, 255, -0.15f, 0.15f);
lenght += lenght * lenghtVar * mrand::getFloat(&branchSeed);
if (lenght < 1)
lenght = 1;
return lenght;
}
inline float Tree::getAngleVar(DrawArgs &arg)
{
float angleVar = Remap(m_dna->branches[arg.dep].branchAngleVar, 0, 255, 0.0f, maxAngles[arg.dep]);
angleVar = Lerp(angleVar, -angleVar, mrand::getFloat(&branchSeed));
return angleVar;
}

65
shared/src/values/Dna.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <cmath>
#include <cstddef>
#include "values/Dna.hpp"
namespace DNA
{
void newDna(Dna *dna, uint128 *state)
{
uint8_t *array = (uint8_t *)dna;
for (size_t i = 0; i < sizeof(Dna); i++)
{
array[i] = mrand::getValue(0, 255, state);
}
return;
}
void makeChild(Dna *p1, Dna *p2, Dna *c, uint128 *state)
{
uint8_t *p1a = (uint8_t *)p1;
uint8_t *p2a = (uint8_t *)p2;
uint8_t *ca = (uint8_t *)c;
for (size_t i = 0; i < sizeof(Dna); i++)
{
int val = mrand::getValue(0, 1, state);
if (val == 0)
{
ca[i] = p1a[i];
}
else
{
ca[i] = p2a[i];
}
}
}
void clone(Dna *p1, Dna *c, uint128 *state)
{
uint8_t *p1a = (uint8_t *)p1;
uint8_t *ca = (uint8_t *)c;
for (size_t i = 0; i < sizeof(Dna); i++)
{
int val = mrand::getValue(0, 1, state);
if (val == 0)
{
ca[i] = p1a[i];
}
else
{
ca[i] = mrand::getValue(0, 255, state);
}
}
}
void mutate(Dna *dna, uint32_t num, uint128 *state)
{
uint8_t *array = (uint8_t *)dna;
for (size_t i = 0; i < num; i++)
{
int pos = mrand::getValue(0, sizeof(Dna), state);
array[pos] = mrand::getValue(0, 255, state);
}
}
}

View File

@@ -0,0 +1,112 @@
#include <unistd.h>
#include "values/DnaManager.hpp"
UiUnit DnaManager::next(DnaManagerData *data)
{
if (data->queued >= NUM_PER_GEN)
{
return {nullptr, Liked::tbd, -1};
}
Dna *ret = &data->vector[data->queued];
int index = data->queued++;
return {ret, Liked::tbd, index};
}
bool DnaManager::like(UiUnit unit, DnaManagerData *data)
{
int found = -1;
if (unit.index == data->showed)
{
found = data->showed;
}
if (found == -1)
{
// RUN OUT OF GEN WAITING FOR NEW GEN
return false;
}
if (unit.liked == Liked::yes)
{
data->liked.push_back(found);
}
else if (unit.liked == Liked::no)
{
data->disliked.push_back(found);
}
else
{
// 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;
}
data->showed++;
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 ==
{
return true;
}
return false;
}
void DnaManager::newGen(DnaManagerData *data)
{
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(&data->vector[i], &data->randSeed);
}
data->disliked.clear();
return;
}
if (data->liked.size() == 1)
{
int front = data->liked.front();
for (auto &&i : data->disliked)
{
DNA::clone(&data->vector[front], &data->vector[i], &data->randSeed);
}
data->disliked.clear();
data->liked.clear();
}
if (data->liked.size() >= 2)
{
for (auto &&i : data->disliked)
{
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, data->liked.size(), &data->randSeed);
}
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, &data->randSeed);
}
}
for (size_t i = 0; i < NUM_PER_GEN; i++)
{
DNA::mutate(&data->vector[i], NUM_OF_MUT, &data->randSeed);
}
data->disliked.clear();
data->liked.clear();
}

View File

@@ -0,0 +1,63 @@
#include <cinttypes>
#include <algorithm>
#include "values/mrand.hpp"
static inline uint32_t my_rotate_left(const uint32_t x, int k)
{
return (x << k) | (x >> (32 - k));
}
uint32_t my_rprand_xoshiro(uint32_t state[4])
{
const uint32_t result = my_rotate_left(state[1] * 5, 7) * 9;
const uint32_t t = state[1] << 9;
state[2] ^= state[0];
state[3] ^= state[1];
state[1] ^= state[2];
state[0] ^= state[3];
state[2] ^= t;
state[3] = my_rotate_left(state[3], 11);
return result;
}
uint64_t rprand_splitmix64(uint64_t &rprand_seed)
{
uint64_t z = (rprand_seed += 0x9e3779b97f4a7c15);
z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
return z ^ (z >> 31);
}
namespace mrand
{
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.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)
{
int value = my_rprand_xoshiro((uint32_t *)state) % (std::abs(max - min) + 1) + min;
return value;
}
float getFloat(uint128 *state)
{
return my_rprand_xoshiro((uint32_t *)state) / 4294967295.0f;
}
}