Add random_float functions with min and max arguments

Add `random_float(float min, float max)` to generate a random `float` between `min` and `max`.

Add `random_float(float max)` to generate a random `float` between `0.0f` and `max`.
This commit is contained in:
Robert Müller 2023-03-13 00:44:38 +01:00
parent 0cf5dd7ad3
commit a9ef757475
4 changed files with 35 additions and 25 deletions

View file

@ -32,6 +32,16 @@ inline float random_float()
return rand() / (float)(RAND_MAX);
}
inline float random_float(float min, float max)
{
return min + random_float() * (max - min);
}
inline float random_float(float max)
{
return random_float(0.0f, max);
}
inline float random_angle()
{
return 2.0f * pi * (rand() / std::nextafter((float)RAND_MAX, std::numeric_limits<float>::max()));

View file

@ -64,7 +64,7 @@ void CEffects::PowerupShine(vec2 Pos, vec2 Size)
CParticle p;
p.SetDefault();
p.m_Spr = SPRITE_PART_SLICE;
p.m_Pos = Pos + vec2((random_float() - 0.5f) * Size.x, (random_float() - 0.5f) * Size.y);
p.m_Pos = Pos + vec2(random_float(-0.5f, 0.5f), random_float(-0.5f, 0.5f)) * Size;
p.m_Vel = vec2(0, 0);
p.m_LifeSpan = 0.5f;
p.m_StartSize = 16.0f;
@ -85,17 +85,17 @@ void CEffects::FreezingFlakes(vec2 Pos, vec2 Size)
CParticle p;
p.SetDefault();
p.m_Spr = SPRITE_PART_SNOWFLAKE;
p.m_Pos = Pos + vec2((random_float() - 0.5f) * Size.x, (random_float() - 0.5f) * Size.y);
p.m_Pos = Pos + vec2(random_float(-0.5f, 0.5f), random_float(-0.5f, 0.5f)) * Size;
p.m_Vel = vec2(0, 0);
p.m_LifeSpan = 1.5f;
p.m_StartSize = (random_float() + 0.5f) * 16.0f;
p.m_StartSize = random_float(1.0f, 1.5f) * 16.0f;
p.m_EndSize = p.m_StartSize * 0.5f;
p.m_UseAlphaFading = true;
p.m_StartAlpha = 1.0f;
p.m_EndAlpha = 0.0f;
p.m_Rot = random_angle();
p.m_Rotspeed = pi;
p.m_Gravity = random_float() * 250.0f;
p.m_Gravity = random_float(250.0f);
p.m_Friction = 0.9f;
p.m_FlowAffected = 0.0f;
p.m_Collides = false;
@ -112,11 +112,11 @@ void CEffects::SmokeTrail(vec2 Pos, vec2 Vel, float Alpha, float TimePassed)
p.m_Spr = SPRITE_PART_SMOKE;
p.m_Pos = Pos;
p.m_Vel = Vel + random_direction() * 50.0f;
p.m_LifeSpan = 0.5f + random_float() * 0.5f;
p.m_StartSize = 12.0f + random_float() * 8;
p.m_LifeSpan = random_float(0.5f, 1.0f);
p.m_StartSize = random_float(12.0f, 20.0f);
p.m_EndSize = 0;
p.m_Friction = 0.7f;
p.m_Gravity = random_float() * -500.0f;
p.m_Gravity = random_float(-500.0f);
p.m_Color.a *= Alpha;
m_pClient->m_Particles.Add(CParticles::GROUP_PROJECTILE_TRAIL, &p, TimePassed);
}
@ -131,11 +131,11 @@ void CEffects::SkidTrail(vec2 Pos, vec2 Vel)
p.m_Spr = SPRITE_PART_SMOKE;
p.m_Pos = Pos;
p.m_Vel = Vel + random_direction() * 50.0f;
p.m_LifeSpan = 0.5f + random_float() * 0.5f;
p.m_StartSize = 24.0f + random_float() * 12;
p.m_LifeSpan = random_float(0.5f, 1.0f);
p.m_StartSize = random_float(24.0f, 36.0f);
p.m_EndSize = 0;
p.m_Friction = 0.7f;
p.m_Gravity = random_float() * -500.0f;
p.m_Gravity = random_float(-500.0f);
p.m_Color = ColorRGBA(0.75f, 0.75f, 0.75f, 1.0f);
m_pClient->m_Particles.Add(CParticles::GROUP_GENERAL, &p);
}
@ -149,7 +149,7 @@ void CEffects::BulletTrail(vec2 Pos, float Alpha, float TimePassed)
p.SetDefault();
p.m_Spr = SPRITE_PART_BALL;
p.m_Pos = Pos;
p.m_LifeSpan = 0.25f + random_float() * 0.25f;
p.m_LifeSpan = random_float(0.25f, 0.5f);
p.m_StartSize = 8.0f;
p.m_EndSize = 0;
p.m_Friction = 0.7f;
@ -166,12 +166,12 @@ void CEffects::PlayerSpawn(vec2 Pos)
p.m_Spr = SPRITE_PART_SHELL;
p.m_Pos = Pos;
p.m_Vel = random_direction() * (std::pow(random_float(), 3) * 600.0f);
p.m_LifeSpan = 0.3f + random_float() * 0.3f;
p.m_StartSize = 64.0f + random_float() * 32;
p.m_LifeSpan = random_float(0.3f, 0.6f);
p.m_StartSize = random_float(64.0f, 96.0f);
p.m_EndSize = 0;
p.m_Rot = random_angle();
p.m_Rotspeed = random_float();
p.m_Gravity = random_float() * -400.0f;
p.m_Gravity = random_float(-400.0f);
p.m_Friction = 0.7f;
p.m_Color = ColorRGBA(0xb5 / 255.0f, 0x50 / 255.0f, 0xcb / 255.0f, 1.0f);
m_pClient->m_Particles.Add(CParticles::GROUP_GENERAL, &p);
@ -206,15 +206,15 @@ void CEffects::PlayerDeath(vec2 Pos, int ClientID)
p.SetDefault();
p.m_Spr = SPRITE_PART_SPLAT01 + (rand() % 3);
p.m_Pos = Pos;
p.m_Vel = random_direction() * ((random_float() + 0.1f) * 900.0f);
p.m_LifeSpan = 0.3f + random_float() * 0.3f;
p.m_StartSize = 24.0f + random_float() * 16;
p.m_Vel = random_direction() * (random_float(1.0f, 1.1f) * 900.0f);
p.m_LifeSpan = random_float(0.3f, 0.6f);
p.m_StartSize = random_float(24.0f, 40.0f);
p.m_EndSize = 0;
p.m_Rot = random_angle();
p.m_Rotspeed = (random_float() - 0.5f) * pi;
p.m_Rotspeed = random_float(-0.5f, 0.5f) * pi;
p.m_Gravity = 800.0f;
p.m_Friction = 0.8f;
ColorRGBA c = BloodColor.v4() * (0.75f + random_float() * 0.25f);
ColorRGBA c = BloodColor.v4() * random_float(0.75f, 1.0f);
p.m_Color = ColorRGBA(c.r, c.g, c.b, 0.75f);
m_pClient->m_Particles.Add(CParticles::GROUP_GENERAL, &p);
}
@ -250,11 +250,11 @@ void CEffects::Explosion(vec2 Pos)
p.SetDefault();
p.m_Spr = SPRITE_PART_SMOKE;
p.m_Pos = Pos;
p.m_Vel = random_direction() * ((1.0f + random_float() * 0.2f) * 1000.0f);
p.m_LifeSpan = 0.5f + random_float() * 0.4f;
p.m_StartSize = 32.0f + random_float() * 8;
p.m_Vel = random_direction() * (random_float(1.0f, 1.2f) * 1000.0f);
p.m_LifeSpan = random_float(0.5f, 0.9f);
p.m_StartSize = random_float(32.0f, 40.0f);
p.m_EndSize = 0;
p.m_Gravity = random_float() * -800.0f;
p.m_Gravity = random_float(-800.0f);
p.m_Friction = 0.4f;
p.m_Color = mix(vec4(0.75f, 0.75f, 0.75f, 1.0f), vec4(0.5f, 0.5f, 0.5f, 1.0f), random_float());
m_pClient->m_Particles.Add(CParticles::GROUP_GENERAL, &p);

View file

@ -633,7 +633,7 @@ void CItems::ReconstructSmokeTrail(const CProjectileData *pCurrent, int DestroyT
float Step = maximum(Client()->FrameTimeAvg(), (pCurrent->m_Type == WEAPON_GRENADE) ? 0.02f : 0.01f);
for(int i = 1 + (int)(Gt / Step); i < (int)(T / Step); i++)
{
float t = Step * (float)i + 0.4f * Step * (random_float() - 0.5f);
float t = Step * (float)i + 0.4f * Step * random_float(-0.5f, 0.5f);
vec2 Pos = CalcPos(pCurrent->m_StartPos, pCurrent->m_StartVel, Curvature, Speed, t);
vec2 PrevPos = CalcPos(pCurrent->m_StartPos, pCurrent->m_StartVel, Curvature, Speed, t - 0.001f);
vec2 Vel = Pos - PrevPos;

View file

@ -107,7 +107,7 @@ void CParticles::Update(float TimePassed)
vec2 Vel = m_aParticles[i].m_Vel * TimePassed;
if(m_aParticles[i].m_Collides)
{
Collision()->MovePoint(&m_aParticles[i].m_Pos, &Vel, 0.1f + 0.9f * random_float(), NULL);
Collision()->MovePoint(&m_aParticles[i].m_Pos, &Vel, random_float(0.1f, 1.0f), NULL);
}
else
{