diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2022-07-20 21:06:31 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2022-07-20 21:06:31 -0700 |
commit | b38db01c835fa222aa11ace3a2d84095fe2c3f83 (patch) | |
tree | 18464eac0c46a1e8a82f96faadc370066d8bf61c /src | |
parent | b68ec04f86f83ba3d7926ffafc1d8a6456e72c05 (diff) |
modules/sparkler: s/rand/rand_r/ and wire up seed
This is a little contorted but not too bad. The input to
particles_new() is just a const conf struct, so instead of
passing in the seed value for particles_t to contain, a pointer
to where the seed lives is passed in via the conf. This requires
the caller to persist a seed somewhere outside the particles
instance, but at least in rototiller we already have that
conveniently in til_module_context_t.
Diffstat (limited to 'src')
-rw-r--r-- | src/modules/sparkler/particle.h | 2 | ||||
-rw-r--r-- | src/modules/sparkler/particles.c | 5 | ||||
-rw-r--r-- | src/modules/sparkler/particles.h | 1 | ||||
-rw-r--r-- | src/modules/sparkler/rocket.c | 30 | ||||
-rw-r--r-- | src/modules/sparkler/simple.c | 24 | ||||
-rw-r--r-- | src/modules/sparkler/spark.c | 4 | ||||
-rw-r--r-- | src/modules/sparkler/sparkler.c | 1 | ||||
-rw-r--r-- | src/modules/sparkler/xplode.c | 6 |
8 files changed, 38 insertions, 35 deletions
diff --git a/src/modules/sparkler/particle.h b/src/modules/sparkler/particle.h index 6a5bf73..a7c4138 100644 --- a/src/modules/sparkler/particle.h +++ b/src/modules/sparkler/particle.h @@ -43,7 +43,7 @@ struct particle_t { //#define rand_within_range(_min, _max) ((rand() % (_max - _min)) + _min) // the style of random number generator used by c libraries has less entropy in the lower bits meaning one shouldn't just use modulo, while this is slower, the results do seem a little different. -#define rand_within_range(_min, _max) (int)(((float)_min) + ((float)rand() / (float)RAND_MAX) * (_max - _min)) +#define rand_within_range(_seedp, _min, _max) (int)(((float)_min) + ((float)rand_r(_seedp) / (float)RAND_MAX) * (_max - _min)) #define INHERIT_OPS NULL #define INHERIT_PROPS NULL diff --git a/src/modules/sparkler/particles.c b/src/modules/sparkler/particles.c index 2389e93..f6adf13 100644 --- a/src/modules/sparkler/particles.c +++ b/src/modules/sparkler/particles.c @@ -49,6 +49,8 @@ particles_t * particles_new(const particles_conf_t *conf) { particles_t *particles; + assert(conf && conf->seedp); + particles = calloc(1, sizeof(particles_t)); if (!particles) { return NULL; @@ -66,8 +68,7 @@ particles_t * particles_new(const particles_conf_t *conf) INIT_LIST_HEAD(&particles->active); - if (conf) - particles->conf = *conf; + particles->conf = *conf; return particles; } diff --git a/src/modules/sparkler/particles.h b/src/modules/sparkler/particles.h index 1cb737f..a419e2c 100644 --- a/src/modules/sparkler/particles.h +++ b/src/modules/sparkler/particles.h @@ -12,6 +12,7 @@ typedef struct particles_conf_t { unsigned show_bsp_matches:1; unsigned show_bsp_matches_affected_only:1; unsigned show_bsp_leafs_min_depth; + unsigned *seedp; } particles_conf_t; typedef struct particles_t particles_t; diff --git a/src/modules/sparkler/rocket.c b/src/modules/sparkler/rocket.c index 0949d30..8afceb4 100644 --- a/src/modules/sparkler/rocket.c +++ b/src/modules/sparkler/rocket.c @@ -38,12 +38,12 @@ static int rocket_init(particles_t *particles, const particles_conf_t *conf, par } rockets_cnt++; - ctxt->decay_rate = rand_within_range(ROCKET_MIN_DECAY_RATE, ROCKET_MAX_DECAY_RATE); - ctxt->longevity = rand_within_range(ROCKET_MIN_LIFETIME, ROCKET_MAX_LIFETIME); + ctxt->decay_rate = rand_within_range(conf->seedp, ROCKET_MIN_DECAY_RATE, ROCKET_MAX_DECAY_RATE); + ctxt->longevity = rand_within_range(conf->seedp, ROCKET_MIN_LIFETIME, ROCKET_MAX_LIFETIME); - ctxt->wander.x = (float)(rand_within_range(0, 628) - 314) / 10000.0f; - ctxt->wander.y = (float)(rand_within_range(0, 628) - 314) / 10000.0f; - ctxt->wander.z = (float)(rand_within_range(0, 628) - 314) / 10000.0f; + ctxt->wander.x = (float)(rand_within_range(conf->seedp, 0, 628) - 314) / 10000.0f; + ctxt->wander.y = (float)(rand_within_range(conf->seedp, 0, 628) - 314) / 10000.0f; + ctxt->wander.z = (float)(rand_within_range(conf->seedp, 0, 628) - 314) / 10000.0f; ctxt->wander = v3f_normalize(&ctxt->wander); ctxt->last_velocity = p->props->velocity; @@ -75,18 +75,18 @@ static particle_status_t rocket_sim(particles_t *particles, const particles_conf /* add a bunch of new explosion particles */ /* TODO: also particle-type-specific parameters, colors! rocket bursts should be able to vary the color. */ - n_xplode = rand_within_range(ROCKETS_XPLODE_MIN_SIZE, ROCKETS_XPLODE_MAX_SIZE); + n_xplode = rand_within_range(conf->seedp, ROCKETS_XPLODE_MIN_SIZE, ROCKETS_XPLODE_MAX_SIZE); for (i = 0; i < n_xplode; i++) { particle_props_t props = *p->props; particle_ops_t *ops = &xplode_ops; - props.direction.x = ((float)(rand_within_range(0, 314159 * 2) - 314159) / 100000.0); - props.direction.y = ((float)(rand_within_range(0, 314159 * 2) - 314159) / 100000.0); - props.direction.z = ((float)(rand_within_range(0, 314159 * 2) - 314159) / 100000.0); + props.direction.x = ((float)(rand_within_range(conf->seedp, 0, 314159 * 2) - 314159) / 100000.0); + props.direction.y = ((float)(rand_within_range(conf->seedp, 0, 314159 * 2) - 314159) / 100000.0); + props.direction.z = ((float)(rand_within_range(conf->seedp, 0, 314159 * 2) - 314159) / 100000.0); props.direction = v3f_normalize(&props.direction); //props->velocity = ((float)rand_within_range(100, 200) / 100000.0); - props.velocity = ((float)rand_within_range(100, 300) / 100000.0); + props.velocity = ((float)rand_within_range(conf->seedp, 100, 300) / 100000.0); particles_spawn_particle(particles, p, &props, ops); } return PARTICLE_DEAD; @@ -100,18 +100,18 @@ static particle_status_t rocket_sim(particles_t *particles, const particles_conf p->props->velocity += .00003; /* spray some sparks behind the rocket */ - n_sparks = rand_within_range(10, 40); + n_sparks = rand_within_range(conf->seedp, 10, 40); for (i = 0; i < n_sparks; i++) { particle_props_t props = *p->props; props.direction = v3f_negate(&props.direction); - props.direction.x += (float)(rand_within_range(0, 40) - 20) / 100.0; - props.direction.y += (float)(rand_within_range(0, 40) - 20) / 100.0; - props.direction.z += (float)(rand_within_range(0, 40) - 20) / 100.0; + props.direction.x += (float)(rand_within_range(conf->seedp, 0, 40) - 20) / 100.0; + props.direction.y += (float)(rand_within_range(conf->seedp, 0, 40) - 20) / 100.0; + props.direction.z += (float)(rand_within_range(conf->seedp, 0, 40) - 20) / 100.0; props.direction = v3f_normalize(&props.direction); - props.velocity = (float)rand_within_range(10, 50) / 100000.0; + props.velocity = (float)rand_within_range(conf->seedp, 10, 50) / 100000.0; particles_spawn_particle(particles, p, &props, &spark_ops); } diff --git a/src/modules/sparkler/simple.c b/src/modules/sparkler/simple.c index 78e415b..3bc3076 100644 --- a/src/modules/sparkler/simple.c +++ b/src/modules/sparkler/simple.c @@ -28,8 +28,8 @@ static int simple_init(particles_t *particles, const particles_conf_t *conf, par { simple_ctxt_t *ctxt = p->ctxt; - ctxt->decay_rate = rand_within_range(SIMPLE_MIN_DECAY_RATE, SIMPLE_MAX_DECAY_RATE); - ctxt->lifetime = ctxt->longevity = rand_within_range(SIMPLE_MIN_LIFETIME, SIMPLE_MAX_LIFETIME); + ctxt->decay_rate = rand_within_range(conf->seedp, SIMPLE_MIN_DECAY_RATE, SIMPLE_MAX_DECAY_RATE); + ctxt->lifetime = ctxt->longevity = rand_within_range(conf->seedp, SIMPLE_MIN_LIFETIME, SIMPLE_MAX_LIFETIME); if (!p->props->of_use) { /* everything starts from the bottom center */ @@ -38,12 +38,12 @@ static int simple_init(particles_t *particles, const particles_conf_t *conf, par p->props->position.z = 0; /* TODO: direction random-ish within the range of a narrow upward facing cone */ - p->props->direction.x = (float)(rand_within_range(0, 6) - 3) * .1f; - p->props->direction.y = 1.0f + (float)(rand_within_range(0, 6) - 3) * .1f; - p->props->direction.z = (float)(rand_within_range(0, 6) - 3) * .1f; + p->props->direction.x = (float)(rand_within_range(conf->seedp, 0, 6) - 3) * .1f; + p->props->direction.y = 1.0f + (float)(rand_within_range(conf->seedp, 0, 6) - 3) * .1f; + p->props->direction.z = (float)(rand_within_range(conf->seedp, 0, 6) - 3) * .1f; p->props->direction = v3f_normalize(&p->props->direction); - p->props->velocity = (float)rand_within_range(300, 800) / 100000.0; + p->props->velocity = (float)rand_within_range(conf->seedp, 300, 800) / 100000.0; p->props->drag = 0.03; p->props->mass = 0.3; @@ -70,7 +70,7 @@ static particle_status_t simple_sim(particles_t *particles, const particles_conf /* create particles inheriting our type based on some silly conditions, with some tweaks to their direction */ if (ctxt->longevity == 42 || (ctxt->longevity > 500 && !(ctxt->longevity % 50))) { - int i, num = rand_within_range(SIMPLE_MIN_SPAWN, SIMPLE_MAX_SPAWN); + int i, num = rand_within_range(conf->seedp, SIMPLE_MIN_SPAWN, SIMPLE_MAX_SPAWN); for (i = 0; i < num; i++) { particle_props_t props = *p->props; @@ -78,14 +78,14 @@ static particle_status_t simple_sim(particles_t *particles, const particles_conf if (i == (SIMPLE_MAX_SPAWN - 2)) { ops = &rocket_ops; - props.velocity = (float)rand_within_range(60, 100) / 1000000.0; + props.velocity = (float)rand_within_range(conf->seedp, 60, 100) / 1000000.0; } else { - props.velocity = (float)rand_within_range(30, 100) / 10000.0; + props.velocity = (float)rand_within_range(conf->seedp, 30, 100) / 10000.0; } - props.direction.x += (float)(rand_within_range(0, 315 * 2) - 315) / 100.0; - props.direction.y += (float)(rand_within_range(0, 315 * 2) - 315) / 100.0; - props.direction.z += (float)(rand_within_range(0, 315 * 2) - 315) / 100.0; + props.direction.x += (float)(rand_within_range(conf->seedp, 0, 315 * 2) - 315) / 100.0; + props.direction.y += (float)(rand_within_range(conf->seedp, 0, 315 * 2) - 315) / 100.0; + props.direction.z += (float)(rand_within_range(conf->seedp, 0, 315 * 2) - 315) / 100.0; props.direction = v3f_normalize(&props.direction); particles_spawn_particle(particles, p, &props, ops); // XXX diff --git a/src/modules/sparkler/spark.c b/src/modules/sparkler/spark.c index 8627baa..3e45cc8 100644 --- a/src/modules/sparkler/spark.c +++ b/src/modules/sparkler/spark.c @@ -26,8 +26,8 @@ static int spark_init(particles_t *particles, const particles_conf_t *conf, part p->props->drag = 20.0; p->props->mass = 0.1; p->props->virtual = 0; - ctxt->decay_rate = rand_within_range(SPARK_MIN_DECAY_RATE, SPARK_MAX_DECAY_RATE); - ctxt->lifetime = ctxt->longevity = rand_within_range(SPARK_MIN_LIFETIME, SPARK_MAX_LIFETIME); + ctxt->decay_rate = rand_within_range(conf->seedp, SPARK_MIN_DECAY_RATE, SPARK_MAX_DECAY_RATE); + ctxt->lifetime = ctxt->longevity = rand_within_range(conf->seedp, SPARK_MIN_LIFETIME, SPARK_MAX_LIFETIME); return 1; } diff --git a/src/modules/sparkler/sparkler.c b/src/modules/sparkler/sparkler.c index c93c018..f0f2cb6 100644 --- a/src/modules/sparkler/sparkler.c +++ b/src/modules/sparkler/sparkler.c @@ -53,6 +53,7 @@ static til_module_context_t * sparkler_create_context(unsigned seed, unsigned ti .show_bsp_matches = ((sparkler_setup_t *)setup)->show_bsp_matches, .show_bsp_leafs_min_depth = ((sparkler_setup_t *)setup)->show_bsp_leafs_min_depth, .show_bsp_matches_affected_only = ((sparkler_setup_t *)setup)->show_bsp_matches_affected_only, + .seedp = &ctxt->til_module_context.seed, }); if (!ctxt->particles) { free(ctxt); diff --git a/src/modules/sparkler/xplode.c b/src/modules/sparkler/xplode.c index 31f56fd..1534369 100644 --- a/src/modules/sparkler/xplode.c +++ b/src/modules/sparkler/xplode.c @@ -26,8 +26,8 @@ static int xplode_init(particles_t *particles, const particles_conf_t *conf, par { xplode_ctxt_t *ctxt = p->ctxt; - ctxt->decay_rate = rand_within_range(XPLODE_MIN_DECAY_RATE, XPLODE_MAX_DECAY_RATE); - ctxt->lifetime = ctxt->longevity = rand_within_range(XPLODE_MIN_LIFETIME, XPLODE_MAX_LIFETIME); + ctxt->decay_rate = rand_within_range(conf->seedp, XPLODE_MIN_DECAY_RATE, XPLODE_MAX_DECAY_RATE); + ctxt->lifetime = ctxt->longevity = rand_within_range(conf->seedp, XPLODE_MIN_LIFETIME, XPLODE_MAX_LIFETIME); p->props->drag = 10.9; p->props->mass = 0.3; @@ -50,7 +50,7 @@ static particle_status_t xplode_sim(particles_t *particles, const particles_conf if (!(ctxt->lifetime % 30)) { particle_props_t props = *p->props; - props.velocity = (float)rand_within_range(10, 50) / 10000.0; + props.velocity = (float)rand_within_range(conf->seedp, 10, 50) / 10000.0; particles_spawn_particle(particles, p, &props, &xplode_ops); } |