Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace rnd macros with STL alternatives #225

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/engine/master.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ void reqauth(masterclient &c, uint id, char *name, char *hostname)
a.reqtime = totalmillis;
a.id = id;
copystring(a.hostname, host);
uint seed[3] = { uint(starttime), uint(totalmillis), randomMT() };
uint seed[3] = { uint(starttime), uint(totalmillis), tmprnd() };
static vector<char> buf;
buf.setsize(0);
a.answer = genchallenge(u->pubkey, seed, sizeof(seed), buf);
Expand All @@ -270,7 +270,7 @@ void reqserverauth(masterclient &c, char *name)

c.serverauthreq.user = u;
c.serverauthreq.reqtime = totalmillis;
uint seed[3] = { uint(starttime), uint(totalmillis), randomMT() };
uint seed[3] = { uint(starttime), uint(totalmillis), tmprnd() };
static vector<char> buf;
buf.setsize(0);
c.serverauthreq.answer = genchallenge(u->pubkey, seed, sizeof(seed), buf);
Expand Down
6 changes: 3 additions & 3 deletions src/engine/renderparticles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1552,8 +1552,8 @@ static inline vec offsetvec(vec o, int dir, int dist)
}
else if(dir < 27) // flat plane
{
to[dir%3] = float(rndscale(2*radius)-radius);
to[(dir+1)%3] = float(rndscale(2*radius)-radius);
to[dir%3] = float(rndfloat(2*radius)-radius);
to[(dir+1)%3] = float(rndfloat(2*radius)-radius);
to[(dir+2)%3] = 0.0;
to.add(p);
from = to;
Expand Down Expand Up @@ -1599,7 +1599,7 @@ void regularflame(int type, const vec &p, float radius, float height, int color,
float collidez = collide ? p.z - raycube(p, vec(0, 0, -1), collide >= 0 ? COLLIDERADIUS : max(p.z, 0.0f), RAY_CLIPMAT) + (collide >= 0 ? COLLIDEERROR : 0) : -1;
loopi(density)
{
vec q = vec(p).add(vec(rndscale(radius*2.f)-radius, rndscale(radius*2.f)-radius, 0));
vec q = vec(p).add(vec(rndfloat(radius*2.f)-radius, rndfloat(radius*2.f)-radius, 0));
newparticle(q, v, rnd(max(int(fade*height), 1))+1, type, color, s, blend, grav, collide)->val = collidez;
}
}
Expand Down
47 changes: 21 additions & 26 deletions src/shared/tools.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
// implementation of generic tools

#include <random>
#include "cube.h"

////////////////////////// rnd numbers ////////////////////////////////////////

#define N (624)
#define M (397)
#define K (0x9908B0DFU)
// Seed stored statically to avoid excessive syscall overhead
static std::random_device rndseed;
static std::mt19937 rndalg(rndseed());

static uint state[N];
static int next = N;
// Generate a random number up to a certain value and do so starting from 0
int rnd(int value) {
std::uniform_int_distribution<> rndint(0, value - 1);
return rndint(rndalg);
}

void seedMT(uint seed)
{
state[0] = seed;
for(uint i = 1; i < N; i++)
state[i] = seed = 1812433253U * (seed ^ (seed >> 30)) + i;
next = 0;
// Like rnd(), but generate a floating point number instead
float rndfloat(int value) {
std::uniform_real_distribution<> rndreal(0, value - 1);
return rndreal(rndalg);
}

uint randomMT()
{
int cur = next;
if(++next >= N)
{
if(next > N) { seedMT(5489U + time(NULL)); cur = next++; }
else next = 0;
}
uint y = (state[cur] & 0x80000000U) | (state[next] & 0x7FFFFFFFU);
state[cur] = y = state[cur < N-M ? cur + M : cur + M-N] ^ (y >> 1) ^ (-int(y & 1U) & K);
y ^= (y >> 11);
y ^= (y << 7) & 0x9D2C5680U;
y ^= (y << 15) & 0xEFC60000U;
y ^= (y >> 18);
return y;
// Deterministic RNG: used to generate closer results by specifying the seed
int detrnd(int seed, int value) {
std::mt19937 algseed(seed);
std::uniform_int_distribution<> rnddet(0, value - 1);
return rnddet(algseed);
}

// Generate a positive, ad-hoc random number
uint tmprnd() { return rndalg(); }

///////////////////////// network ///////////////////////

// all network traffic is in 32bit ints, which are then compressed using the following simple scheme (assumes that most values are small).
Expand Down
9 changes: 4 additions & 5 deletions src/shared/tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,6 @@ static inline int bitscan(uint mask)
#endif
#endif

#define rnd(x) ((int)(randomMT()&0x7FFFFFFF)%(x))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a good idea to replace these macros.

#define rndscale(x) (float((randomMT()&0x7FFFFFFF)*double(x)/double(0x7FFFFFFF)))
#define detrnd(s, x) ((int)(((((uint)(s))*1103515245+12345)>>16)%(x)))
#define isnumeric(c) (isdigit(c) || c == '+' || c == '-')

#define loop(v,m) for(int v = 0; v < int(m); ++v)
Expand Down Expand Up @@ -1685,8 +1682,10 @@ extern int listzipfiles(const char *dir, const char *ext, vector<char *> &files)
extern void backup(const char *fname, const char *ext, int revision = 0, int start = 1, bool store = false, bool full = true);

extern void endianswap(void *, int, int);
extern void seedMT(uint seed);
extern uint randomMT();
extern int rnd(int);
extern float rndfloat(int);
extern int detrnd(int, int);
extern uint tmprnd();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume the following mapping now, given their previous use:

  • rnd -> rnd
  • rndscale -> rndfloat
  • detrnd -> detrnd
  • randomMT -> tmprnd

In order to allow people who are used to the old code understand the new format, I think it makes sense to add docstrings to the new functions, explaining what the new ones do.

extern void putint(ucharbuf &p, int n);
extern void putint(packetbuf &p, int n);
extern void putint(vector<uchar> &p, int n);
Expand Down