Skip to content

Commit

Permalink
update rng functions in a lot of element files to use .between/.chance
Browse files Browse the repository at this point in the history
conversion is still very incomplete
  • Loading branch information
jacob1 committed May 3, 2018
1 parent d2e0986 commit 44a8665
Show file tree
Hide file tree
Showing 38 changed files with 141 additions and 134 deletions.
11 changes: 9 additions & 2 deletions src/common/tpt-rand.cpp
Expand Up @@ -22,6 +22,11 @@ uint64_t RNG::next()
return result;
}

unsigned int RNG::gen()
{
return next() & 0x7FFFFFFF;
}

unsigned int RNG::operator()()
{
return next()&0xFFFFFFFF;
Expand All @@ -33,9 +38,11 @@ int RNG::between(int lower, int upper)
return static_cast<int>(r % (upper - lower + 1)) + lower;
}

bool RNG::chance(float chance)
bool RNG::chance(int nominator, unsigned int denominator)
{
return uniform01() < chance;
if (nominator < 0)
return false;
return next() % denominator < static_cast<unsigned int>(nominator);
}

float RNG::uniform01()
Expand Down
3 changes: 2 additions & 1 deletion src/common/tpt-rand.h
Expand Up @@ -11,8 +11,9 @@ class RNG : public Singleton<RNG>
uint64_t next();
public:
unsigned int operator()();
unsigned int gen();
int between(int lower, int upper);
bool chance(float chance);
bool chance(int nominator, unsigned int denominator);
float uniform01();

RNG();
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/elements/ACID.cpp
Expand Up @@ -69,14 +69,14 @@ int Element_ACID::update(UPDATE_FUNC_ARGS)
}
else if (rt == PT_WTRV)
{
if (RNG::Ref().chance(1/250.0f))
if (RNG::Ref().chance(1, 250))
{
sim->part_change_type(i, x, y, PT_CAUS);
parts[i].life = RNG::Ref().between(25, 74);
sim->kill_part(ID(r));
}
}
else if (rt != PT_CLNE && rt != PT_PCLN && parts[i].life >= 50 && RNG::Ref().chance(sim->elements[rt].Hardness/1000.0))
else if (rt != PT_CLNE && rt != PT_PCLN && parts[i].life >= 50 && RNG::Ref().chance(sim->elements[rt].Hardness, 1000.0))
{
if (sim->parts_avg(i, ID(r),PT_GLAS)!= PT_GLAS)//GLAS protects stuff from acid
{
Expand Down
6 changes: 3 additions & 3 deletions src/simulation/elements/BANG.cpp
Expand Up @@ -87,9 +87,9 @@ int Element_BANG::update(UPDATE_FUNC_ARGS)
//Explode!!
sim->pv[y/CELL][x/CELL] += 0.5f;
parts[i].tmp = 0;
if (RNG::Ref().chance(1.0/3))
if (RNG::Ref().chance(1, 3))
{
if (RNG::Ref().chance(1.0/2))
if (RNG::Ref().chance(1, 2))
{
sim->create_part(i, x, y, PT_FIRE);
}
Expand All @@ -102,7 +102,7 @@ int Element_BANG::update(UPDATE_FUNC_ARGS)
}
else
{
if (RNG::Ref().chance(1.0/15))
if (RNG::Ref().chance(1, 15))
{
sim->create_part(i, x, y, PT_EMBR);
parts[i].tmp = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/elements/IGNT.cpp
Expand Up @@ -66,7 +66,7 @@ int Element_IGNT::update(UPDATE_FUNC_ARGS)
}
else if(parts[i].life > 0)
{
if (RNG::Ref().chance(2.0/3))
if (RNG::Ref().chance(2, 3))
{
int nb = sim->create_part(-1, x + RNG::Ref().between(-1, 1), y + RNG::Ref().between(-1, 1), PT_EMBR);
if (nb!=-1) {
Expand Down
6 changes: 3 additions & 3 deletions src/simulation/elements/PBCN.cpp
Expand Up @@ -52,7 +52,7 @@ int Element_PBCN::update(UPDATE_FUNC_ARGS)
{
int r, rx, ry, rt;
if (!parts[i].tmp2 && sim->pv[y/CELL][x/CELL]>4.0f)
parts[i].tmp2 = random_gen()%40+80;
parts[i].tmp2 = RNG::Ref().between(80, 119);
if (parts[i].tmp2)
{
parts[i].vx += ADVECTION*sim->vx[y/CELL][x/CELL];
Expand Down Expand Up @@ -132,9 +132,9 @@ int Element_PBCN::update(UPDATE_FUNC_ARGS)
for (ry=-1; ry<2; ry++)
sim->create_part(-1, x+rx, y+ry, PT_LIFE, parts[i].tmp);

else if (parts[i].ctype!=PT_LIGH || !(random_gen()%30))
else if (parts[i].ctype!=PT_LIGH || RNG::Ref().chance(1, 30))
{
int np = sim->create_part(-1, x+random_gen()%3-1, y+random_gen()%3-1, TYP(parts[i].ctype));
int np = sim->create_part(-1, x + RNG::Ref().between(-1, 1), y + RNG::Ref().between(-1, 1), TYP(parts[i].ctype));
if (np>-1)
{
if (parts[i].ctype==PT_LAVA && parts[i].tmp>0 && parts[i].tmp<PT_NUM && sim->elements[parts[i].tmp].HighTemperatureTransition==PT_LAVA)
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/elements/PCLN.cpp
Expand Up @@ -123,9 +123,9 @@ int Element_PCLN::update(UPDATE_FUNC_ARGS)
for (ry=-1; ry<2; ry++)
sim->create_part(-1, x+rx, y+ry, PT_LIFE, parts[i].tmp);

else if (parts[i].ctype!=PT_LIGH || (random_gen()%30)==0)
else if (parts[i].ctype != PT_LIGH || RNG::Ref().chance(1, 30))
{
int np = sim->create_part(-1, x+random_gen()%3-1, y+random_gen()%3-1, TYP(parts[i].ctype));
int np = sim->create_part(-1, x + RNG::Ref().between(-1, 1), y + RNG::Ref().between(-1, 1), TYP(parts[i].ctype));
if (np>=0)
{
if (parts[i].ctype==PT_LAVA && parts[i].tmp>0 && parts[i].tmp<PT_NUM && sim->elements[parts[i].tmp].HighTemperatureTransition==PT_LAVA)
Expand Down
21 changes: 11 additions & 10 deletions src/simulation/elements/PHOT.cpp
Expand Up @@ -55,7 +55,8 @@ int Element_PHOT::update(UPDATE_FUNC_ARGS)
return 1;
}
if (parts[i].temp > 506)
if (!(random_gen()%10)) Element_FIRE::update(UPDATE_FUNC_SUBCALL_ARGS);
if (RNG::Ref().chance(1, 10))
Element_FIRE::update(UPDATE_FUNC_SUBCALL_ARGS);
for (rx=-1; rx<2; rx++)
for (ry=-1; ry<2; ry++)
if (BOUNDS_CHECK) {
Expand All @@ -64,34 +65,34 @@ int Element_PHOT::update(UPDATE_FUNC_ARGS)
continue;
if (TYP(r)==PT_ISOZ || TYP(r)==PT_ISZS)
{
if (!(random_gen()%400))
if (RNG::Ref().chance(1, 400))
{
parts[i].vx *= 0.90;
parts[i].vy *= 0.90;
sim->create_part(ID(r), x+rx, y+ry, PT_PHOT);
rrr = (random_gen()%360)*3.14159f/180.0f;
rrr = RNG::Ref().between(0, 359) * 3.14159f / 180.0f;
if (TYP(r) == PT_ISOZ)
rr = (random_gen()%128+128)/127.0f;
rr = RNG::Ref().between(128, 255) / 127.0f;
else
rr = (random_gen()%228+128)/127.0f;
rr = RNG::Ref().between(128, 355) / 127.0f;
parts[ID(r)].vx = rr*cosf(rrr);
parts[ID(r)].vy = rr*sinf(rrr);
sim->pv[y/CELL][x/CELL] -= 15.0f * CFDS;
}
}
else if((TYP(r) == PT_QRTZ || TYP(r) == PT_PQRT) && !ry && !rx)//if on QRTZ
{
float a = (random_gen()%360)*3.14159f/180.0f;
float a = RNG::Ref().between(0, 359) * 3.14159f / 180.0f;
parts[i].vx = 3.0f*cosf(a);
parts[i].vy = 3.0f*sinf(a);
if(parts[i].ctype == 0x3FFFFFFF)
parts[i].ctype = 0x1F<<(random_gen()%26);
parts[i].ctype = 0x1F << RNG::Ref().between(0, 25);
if (parts[i].life)
parts[i].life++; //Delay death
}
else if(TYP(r) == PT_BGLA && !ry && !rx)//if on BGLA
{
float a = (random_gen()%101 - 50) * 0.001f;
float a = RNG::Ref().between(-50, 50) * 0.001f;
float rx = cosf(a), ry = sinf(a), vx, vy;
vx = rx * parts[i].vx + ry * parts[i].vy;
vy = rx * parts[i].vy - ry * parts[i].vx;
Expand All @@ -100,8 +101,8 @@ int Element_PHOT::update(UPDATE_FUNC_ARGS)
}
else if (TYP(r) == PT_FILT && parts[ID(r)].tmp==9)
{
parts[i].vx += ((float)(random_gen()%1000-500))/1000.0f;
parts[i].vy += ((float)(random_gen()%1000-500))/1000.0f;
parts[i].vx += ((float)RNG::Ref().between(-500, 500))/1000.0f;
parts[i].vy += ((float)RNG::Ref().between(-500, 500))/1000.0f;
}
}
return 0;
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/elements/PIPE.cpp
Expand Up @@ -191,7 +191,7 @@ int Element_PIPE::update(UPDATE_FUNC_ARGS)

if (nt)//there is something besides PIPE around current particle
{
rndstore = random_gen();
rndstore = RNG::Ref().gen();
rnd = rndstore&7;
//rndstore = rndstore>>3;
rx = pos_1_rx[rnd];
Expand Down Expand Up @@ -433,7 +433,7 @@ void Element_PIPE::pushParticle(Simulation * sim, int i, int count, int original
if( !(sim->parts[i].tmp&0x200) )
{
//normal random push
rndstore = random_gen();
rndstore = RNG::Ref().gen();
// RAND_MAX is at least 32767 on all platforms i.e. pow(8,5)-1
// so can go 5 cycles without regenerating rndstore
for (q=0; q<3; q++)//try to push 3 times
Expand Down
10 changes: 5 additions & 5 deletions src/simulation/elements/PLNT.cpp
Expand Up @@ -60,30 +60,30 @@ int Element_PLNT::update(UPDATE_FUNC_ARGS)
switch (TYP(r))
{
case PT_WATR:
if (!(random_gen()%50))
if (RNG::Ref().chance(1, 50))
{
np = sim->create_part(ID(r),x+rx,y+ry,PT_PLNT);
if (np<0) continue;
parts[np].life = 0;
}
break;
case PT_LAVA:
if (!(random_gen()%50))
if (RNG::Ref().chance(1, 50))
{
sim->part_change_type(i,x,y,PT_FIRE);
parts[i].life = 4;
}
break;
case PT_SMKE:
case PT_CO2:
if (!(random_gen()%50))
if (RNG::Ref().chance(1, 50))
{
sim->kill_part(ID(r));
parts[i].life = random_gen()%60 + 60;
parts[i].life = RNG::Ref().between(60, 119);
}
break;
case PT_WOOD:
rndstore = random_gen();
rndstore = RNG::Ref().gen();
if (surround_space && !(rndstore%4) && parts[i].tmp==1)
{
rndstore >>= 3;
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/elements/PLUT.cpp
Expand Up @@ -48,7 +48,7 @@ Element_PLUT::Element_PLUT()
//#TPT-Directive ElementHeader Element_PLUT static int update(UPDATE_FUNC_ARGS)
int Element_PLUT::update(UPDATE_FUNC_ARGS)
{
if (!(random_gen()%100) && (5.0f*sim->pv[y/CELL][x/CELL]) > (random_gen()%1000))
if (RNG::Ref().chance(1, 100) && RNG::Ref().chance(5.0f*sim->pv[y/CELL][x/CELL], 1000))
{
sim->create_part(i, x, y, PT_NEUT);
}
Expand Down
4 changes: 2 additions & 2 deletions src/simulation/elements/POLO.cpp
Expand Up @@ -55,7 +55,7 @@ int Element_POLO::update(UPDATE_FUNC_ARGS)
int r = sim->photons[y][x];
if (parts[i].tmp < LIMIT && !parts[i].life)
{
if (!(random_gen()%10000) && !parts[i].tmp)
if (RNG::Ref().chance(1, 10000) && !parts[i].tmp)
{
int s = sim->create_part(-3, x, y, PT_NEUT);
if (s >= 0)
Expand All @@ -68,7 +68,7 @@ int Element_POLO::update(UPDATE_FUNC_ARGS)
}
}

if (r && !(random_gen()%100))
if (r && RNG::Ref().chance(1, 100))
{
int s = sim->create_part(-3, x, y, PT_NEUT);
if (s >= 0)
Expand Down
6 changes: 3 additions & 3 deletions src/simulation/elements/PROT.cpp
Expand Up @@ -68,15 +68,15 @@ int Element_PROT::update(UPDATE_FUNC_ARGS)
break;
}
case PT_DEUT:
if ((-((int)sim->pv[y / CELL][x / CELL] - 4) + (parts[uID].life / 100)) > (int)(random_gen() % 200))
if (RNG::Ref().chance(-((int)sim->pv[y / CELL][x / CELL] - 4) + (parts[uID].life / 100), 200))
{
DeutImplosion(sim, parts[uID].life, x, y, restrict_flt(parts[uID].temp + parts[uID].life * 500, MIN_TEMP, MAX_TEMP), PT_PROT);
sim->kill_part(uID);
}
break;
case PT_LCRY:
//Powered LCRY reaction: PROT->PHOT
if (parts[uID].life > 5 && !(random_gen() % 10))
if (parts[uID].life > 5 && RNG::Ref().chance(1, 10))
{
sim->part_change_type(i, x, y, PT_PHOT);
parts[i].life *= 2;
Expand Down Expand Up @@ -143,7 +143,7 @@ int Element_PROT::update(UPDATE_FUNC_ARGS)
element = PT_CO2;
else
element = PT_NBLE;
newID = sim->create_part(-1, x+random_gen()%3-1, y+random_gen()%3-1, element);
newID = sim->create_part(-1, x + RNG::Ref().between(-1, 1), y + RNG::Ref().between(-1, 1), element);
if (newID >= 0)
parts[newID].temp = restrict_flt(100.0f*parts[i].tmp, MIN_TEMP, MAX_TEMP);
sim->kill_part(i);
Expand Down
12 changes: 6 additions & 6 deletions src/simulation/elements/PRTI.cpp
Expand Up @@ -116,22 +116,22 @@ int Element_PRTI::update(UPDATE_FUNC_ARGS)
if (fe) {
int orbd[4] = {0, 0, 0, 0}; //Orbital distances
int orbl[4] = {0, 0, 0, 0}; //Orbital locations
if (!sim->parts[i].life) parts[i].life = random_gen();
if (!sim->parts[i].ctype) parts[i].ctype = random_gen();
if (!sim->parts[i].life) parts[i].life = RNG::Ref().gen();
if (!sim->parts[i].ctype) parts[i].ctype = RNG::Ref().gen();
sim->orbitalparts_get(parts[i].life, parts[i].ctype, orbd, orbl);
for (int r = 0; r < 4; r++) {
if (orbd[r]>1) {
orbd[r] -= 12;
if (orbd[r]<1) {
orbd[r] = (random_gen()%128)+128;
orbl[r] = random_gen()%255;
orbd[r] = RNG::Ref().between(128, 255);
orbl[r] = RNG::Ref().between(0, 254);
} else {
orbl[r] += 2;
orbl[r] = orbl[r]%255;
}
} else {
orbd[r] = (random_gen()%128)+128;
orbl[r] = random_gen()%255;
orbd[r] = RNG::Ref().between(128, 255);
orbl[r] = RNG::Ref().between(0, 254);
}
}
sim->orbitalparts_set(&parts[i].life, &parts[i].ctype, orbd, orbl);
Expand Down
10 changes: 5 additions & 5 deletions src/simulation/elements/PRTO.cpp
Expand Up @@ -73,7 +73,7 @@ int Element_PRTO::update(UPDATE_FUNC_ARGS)
fe = 1;
for ( nnx =0 ; nnx<80; nnx++)
{
int randomness = (count + random_gen()%3-1 + 4)%8;//add -1,0,or 1 to count
int randomness = (count + RNG::Ref().between(-1, 1) + 4) % 8;//add -1,0,or 1 to count
if (sim->portalp[parts[i].tmp][randomness][nnx].type==PT_SPRK)// TODO: make it look better, spark creation
{
sim->create_part(-1,x+1,y,PT_SPRK);
Expand Down Expand Up @@ -141,15 +141,15 @@ int Element_PRTO::update(UPDATE_FUNC_ARGS)
if (fe) {
int orbd[4] = {0, 0, 0, 0}; //Orbital distances
int orbl[4] = {0, 0, 0, 0}; //Orbital locations
if (!sim->parts[i].life) parts[i].life = random_gen();
if (!sim->parts[i].ctype) parts[i].ctype = random_gen();
if (!sim->parts[i].life) parts[i].life = RNG::Ref().gen();
if (!sim->parts[i].ctype) parts[i].ctype = RNG::Ref().gen();
sim->orbitalparts_get(parts[i].life, parts[i].ctype, orbd, orbl);
for (r = 0; r < 4; r++) {
if (orbd[r]<254) {
orbd[r] += 16;
if (orbd[r]>254) {
orbd[r] = 0;
orbl[r] = random_gen()%255;
orbl[r] = RNG::Ref().between(0, 254);
}
else
{
Expand All @@ -160,7 +160,7 @@ int Element_PRTO::update(UPDATE_FUNC_ARGS)
//orbl[r] = orbl[r]%255;
} else {
orbd[r] = 0;
orbl[r] = random_gen()%255;
orbl[r] = RNG::Ref().between(0, 254);
}
}
sim->orbitalparts_set(&parts[i].life, &parts[i].ctype, orbd, orbl);
Expand Down
8 changes: 4 additions & 4 deletions src/simulation/elements/QRTZ.cpp
Expand Up @@ -70,7 +70,7 @@ int Element_QRTZ::update(UPDATE_FUNC_ARGS)
r = pmap[y+ry][x+rx];
if (!r)
continue;
else if (TYP(r)==PT_SLTW && !(random_gen()%500))
else if (TYP(r)==PT_SLTW && RNG::Ref().chance(1, 500))
{
sim->kill_part(ID(r));
parts[i].tmp++;
Expand All @@ -83,7 +83,7 @@ int Element_QRTZ::update(UPDATE_FUNC_ARGS)
int rnd, sry, srx;
for (trade = 0; trade < 9; trade++)
{
rnd = random_gen()%0x3FF;
rnd = RNG::Ref().gen() % 0x3FF;
rx = (rnd%5)-2;
srx = (rnd%3)-1;
rnd >>= 3;
Expand All @@ -106,11 +106,11 @@ int Element_QRTZ::update(UPDATE_FUNC_ARGS)
// If PQRT is stationary and has started growing particles of QRTZ, the PQRT is basically part of a new QRTZ crystal. So turn it back into QRTZ so that it behaves more like part of the crystal.
sim->part_change_type(i,x,y,PT_QRTZ);
}
if (random_gen()%2)
if (RNG::Ref().chance(1, 2))
{
parts[np].tmp=-1;//dead qrtz
}
else if (!parts[i].tmp && !(random_gen()%15))
else if (!parts[i].tmp && RNG::Ref().chance(1, 15))
{
parts[i].tmp=-1;
}
Expand Down
2 changes: 1 addition & 1 deletion src/simulation/elements/RIME.cpp
Expand Up @@ -58,7 +58,7 @@ int Element_RIME::update(UPDATE_FUNC_ARGS)
if (TYP(r)==PT_SPRK)
{
sim->part_change_type(i,x,y,PT_FOG);
parts[i].life = random_gen()%50 + 60;
parts[i].life = RNG::Ref().between(60, 119);
}
else if (TYP(r)==PT_FOG&&parts[ID(r)].life>0)
{
Expand Down

0 comments on commit 44a8665

Please sign in to comment.