From 201430511bf3481e24d3d18b0ae70117b2981a30 Mon Sep 17 00:00:00 2001 From: hgn Date: Sun, 10 Dec 2023 15:36:18 +0000 Subject: [PATCH] seperate rand instances for each thread --- audio.h | 5 +++-- particle.c | 2 +- player_effects.c | 4 ++-- player_ragdoll.c | 3 ++- player_remote.c | 18 ++++++++++-------- player_skate.c | 18 +++++++++++------- player_walk.c | 7 ++++--- world_audio.c | 9 ++++----- world_entity.c | 11 +++++------ world_gen.c | 13 +++++++------ world_load.c | 2 -- world_routes.c | 11 +++++------ 12 files changed, 54 insertions(+), 49 deletions(-) diff --git a/audio.h b/audio.h index 0d025c8..917b8ec 100644 --- a/audio.h +++ b/audio.h @@ -224,12 +224,13 @@ static void audio_ambient_sprites_update( world_instance *world, v3f co ) if( sprite_type != k_audio_sprite_type_none ){ if( sprite_type == k_audio_sprite_type_grass ){ - audio_ambient_sprite_play( sprite_pos, &audio_grass[vg_randu32()%4] ); + audio_ambient_sprite_play( sprite_pos, + &audio_grass[vg_randu32(&vg.rand)%4] ); } else if( sprite_type == k_audio_sprite_type_water ){ if( world->water.enabled ){ audio_ambient_sprite_play( sprite_pos, - &audio_water[vg_randu32()%6] ); + &audio_water[vg_randu32(&vg.rand)%6] ); } } } diff --git a/particle.c b/particle.c index 13cc5d4..d12a87a 100644 --- a/particle.c +++ b/particle.c @@ -22,7 +22,7 @@ static void particle_spawn_cone( particle_system *sys, v3_tangent_basis( dir, tx, ty ); v3f rand; - vg_rand_cone( rand, angle ); + vg_rand_cone( &vg.rand, rand, angle ); v3_muls( tx, rand[0]*speed, p->v ); v3_muladds( p->v, ty, rand[1]*speed, p->v ); v3_muladds( p->v, dir, rand[2]*speed, p->v ); diff --git a/player_effects.c b/player_effects.c index 0092f19..2bfc3bf 100644 --- a/player_effects.c +++ b/player_effects.c @@ -3,7 +3,7 @@ static void effect_blink_apply( effect_blink *ef, player_pose *pose, f32 dt ){ if( ef->t < 0.0f ){ - ef->t = (1.0f-powf(vg_randf64(),4.0f))*4.0f; + ef->t = (1.0f-powf(vg_randf64(&vg.rand),4.0f))*4.0f; ef->l = 0.08f; } @@ -17,7 +17,7 @@ static void effect_spark_apply( effect_spark *ef, v3f co, v3f v, f32 dt ){ if( !ef->colour ) return; if( ef->t < 0.0f ){ - ef->t = 0.05f+vg_randf64()*0.1f; + ef->t = 0.05f+vg_randf64(&vg.rand)*0.1f; v3f dir; v3_copy( v, dir ); diff --git a/player_ragdoll.c b/player_ragdoll.c index ac6e902..f0a6f88 100644 --- a/player_ragdoll.c +++ b/player_ragdoll.c @@ -436,7 +436,8 @@ static void player_ragdoll_iter( struct player_ragdoll *rd ){ if( stress ){ temp_filter = 20; audio_lock(); - audio_oneshot_3d( &audio_hits[vg_randu32()%5], stress->co, 20.0f, 1.0f ); + audio_oneshot_3d( &audio_hits[vg_randu32(&vg.rand)%5], + stress->co, 20.0f, 1.0f ); audio_unlock(); } } diff --git a/player_remote.c b/player_remote.c index 2234c6d..e910170 100644 --- a/player_remote.c +++ b/player_remote.c @@ -642,10 +642,12 @@ static int remote_players_randomize( int argc, const char *argv[] ){ for( int i=0; iactive = (vg_randu32() & 0x1)? 2: 0; - player->isfriend = vg_randu32() & vg_randu32() & 0x1; - player->isblocked = vg_randu32() & vg_randu32() & vg_randu32() & 0x1; - player->world_match[ 0 ] = vg_randu32() & 0x1; + player->active = (vg_randu32(&vg.rand) & 0x1)? 2: 0; + player->isfriend = vg_randu32(&vg.rand) & vg_randu32(&vg.rand) & 0x1; + player->isblocked = vg_randu32(&vg.rand) & + vg_randu32(&vg.rand) & + vg_randu32(&vg.rand) & 0x1; + player->world_match[ 0 ] = vg_randu32(&vg.rand) & 0x1; player->world_match[ 1 ] = 0; if( player->world_match[0] ) @@ -654,20 +656,20 @@ static int remote_players_randomize( int argc, const char *argv[] ){ player->active_world = NULL; for( int i=0; iusername)-1; i ++ ){ - player->username[i] = 'a' + (vg_randu32() % 30); + player->username[i] = 'a' + (vg_randu32(&vg.rand) % 30); player->username[i+1] = '\0'; - if( (vg_randu32() % 8) == 3 ) + if( (vg_randu32(&vg.rand) % 8) == 3 ) break; } for( int i=0; i<3; i ++ ){ - player->medals[i] = vg_randu32() % 3; + player->medals[i] = vg_randu32(&vg.rand) % 3; } v3f pos; - vg_rand_sphere( pos ); + vg_rand_sphere( &vg.rand, pos ); v3_muladds( localplayer.rb.co, pos, 100.0f, netplayers.final_mtx[ i*localplayer.skeleton.bone_count][3] ); } diff --git a/player_skate.c b/player_skate.c index c70d1f3..7f4bc37 100644 --- a/player_skate.c +++ b/player_skate.c @@ -746,7 +746,7 @@ static void skate_apply_trick_model(void){ v3_zero( state->trick_vel ); audio_lock(); - audio_oneshot_3d( &audio_flips[vg_randu32()%4], + audio_oneshot_3d( &audio_flips[vg_randu32(&vg.rand)%4], localplayer.rb.co, 40.0f, 1.0f ); audio_unlock(); } @@ -1105,7 +1105,7 @@ static void player__skate_pre_update(void){ if( (trick = player_skate_trick_input()) ){ if( state->trick_time == 0.0f ){ audio_lock(); - audio_oneshot_3d( &audio_flips[vg_randu32()%4], + audio_oneshot_3d( &audio_flips[vg_randu32(&vg.rand)%4], localplayer.rb.co, 40.0f, 1.0f ); audio_unlock(); } @@ -2671,7 +2671,7 @@ static void player__skate_animate(void){ float curspeed = v3_length( localplayer.rb.v ), kickspeed = vg_clampf( curspeed*(1.0f/40.0f), 0.0f, 1.0f ), - kicks = (vg_randf64()-0.5f)*2.0f*kickspeed, + kicks = (vg_randf64(&vg.rand)-0.5f)*2.0f*kickspeed, sign = vg_signf( kicks ); animator->wobble[0] = vg_lerpf( animator->wobble[0], kicks*kicks*sign, @@ -3311,16 +3311,20 @@ static void player__skate_sfx_oneshot( u8 id, v3f pos, f32 volume ){ audio_lock(); if( id == k_player_skate_soundeffect_jump ){ - audio_oneshot_3d( &audio_jumps[vg_randu32()%2], pos, 40.0f, volume ); + audio_oneshot_3d( &audio_jumps[vg_randu32(&vg.rand)%2], + pos, 40.0f, volume ); } else if( id == k_player_skate_soundeffect_tap ){ - audio_oneshot_3d( &audio_taps[vg_randu32()%4], pos, 40.0f, volume ); + audio_oneshot_3d( &audio_taps[vg_randu32(&vg.rand)%4], + pos, 40.0f, volume ); } else if( id == k_player_skate_soundeffect_land_good ){ - audio_oneshot_3d( &audio_lands[vg_randu32()%3], pos, 40.0f, volume ); + audio_oneshot_3d( &audio_lands[vg_randu32(&vg.rand)%3], + pos, 40.0f, volume ); } else if( id == k_player_skate_soundeffect_land_bad ){ - audio_oneshot_3d( &audio_lands[vg_randu32()%2+3], pos, 40.0f, volume ); + audio_oneshot_3d( &audio_lands[vg_randu32(&vg.rand)%2+3], + pos, 40.0f, volume ); } else if( id == k_player_skate_soundeffect_grind_metal ){ audio_oneshot_3d( &audio_board[3], pos, 40.0f, volume ); diff --git a/player_walk.c b/player_walk.c index 717411c..34ad072 100644 --- a/player_walk.c +++ b/player_walk.c @@ -711,20 +711,21 @@ static void player__walk_post_update(void){ audio_lock(); if( w->surface == k_surface_prop_concrete ){ audio_oneshot_3d( - &audio_footsteps[vg_randu32()%vg_list_size(audio_footsteps)], + &audio_footsteps[vg_randu32(&vg.rand) % + vg_list_size(audio_footsteps)], localplayer.rb.co, 40.0f, 1.0f ); } else if( w->surface == k_surface_prop_grass ){ audio_oneshot_3d( - &audio_footsteps_grass[ vg_randu32()% + &audio_footsteps_grass[ vg_randu32(&vg.rand) % vg_list_size(audio_footsteps_grass)], localplayer.rb.co, 40.0f, 1.0f ); } else if( w->surface == k_surface_prop_wood ){ audio_oneshot_3d( - &audio_footsteps_wood[ vg_randu32()% + &audio_footsteps_wood[ vg_randu32(&vg.rand) % vg_list_size(audio_footsteps_wood)], localplayer.rb.co, 40.0f, 1.0f ); diff --git a/world_audio.c b/world_audio.c index 1e902fa..0ecb6eb 100644 --- a/world_audio.c +++ b/world_audio.c @@ -30,11 +30,10 @@ static void world_fadeout_audio( world_instance *world ) * Trace out a random point, near the player to try and determine water areas */ static -enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output) -{ - v3f chance = { (vg_randf64()-0.5f) * 30.0f, - 8.0f, - (vg_randf64()-0.5f) * 30.0f }; +enum audio_sprite_type world_audio_sample_sprite_random(v3f origin, v3f output){ + v3f chance = { (vg_randf64(&vg.rand)-0.5f) * 30.0f, + 8, + (vg_randf64(&vg.rand)-0.5f) * 30.0f }; v3f pos; v3_add( chance, origin, pos ); diff --git a/world_entity.c b/world_entity.c index 9095e1b..ec7c2fe 100644 --- a/world_entity.c +++ b/world_entity.c @@ -277,8 +277,7 @@ ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name ) return rp; } -static void ent_volume_call( world_instance *world, ent_call *call ) -{ +static void ent_volume_call( world_instance *world, ent_call *call ){ u32 index = mdl_entity_id_id( call->id ); ent_volume *volume = mdl_arritm( &world->ent_volume, index ); if( !volume->target ) return; @@ -288,9 +287,9 @@ static void ent_volume_call( world_instance *world, ent_call *call ) if( volume->flags & k_ent_volume_flag_particles ){ float *co = alloca( sizeof(float)*3 ); - co[0] = vg_randf64()*2.0f-1.0f; - co[1] = vg_randf64()*2.0f-1.0f; - co[2] = vg_randf64()*2.0f-1.0f; + co[0] = vg_randf64(&vg.rand)*2.0f-1.0f; + co[1] = vg_randf64(&vg.rand)*2.0f-1.0f; + co[2] = vg_randf64(&vg.rand)*2.0f-1.0f; m4x3_mulv( volume->to_world, co, co ); call->function = k_ent_function_particle_spawn; @@ -336,7 +335,7 @@ static void ent_audio_call( world_instance *world, ent_call *call ){ else return; - float chance = vg_randf64()*100.0f, + float chance = vg_randf64(&vg.rand)*100.0f, bar = 0.0f; for( u32 i=0; iclip_count; i++ ){ diff --git a/world_gen.c b/world_gen.c index 3d705ab..e2ec14a 100644 --- a/world_gen.c +++ b/world_gen.c @@ -46,7 +46,7 @@ static void world_add_all_if_material( m4x3f transform, scene_context *scene, * | | * |________| */ -static void world_gen_add_blob( world_instance *world, +static void world_gen_add_blob( vg_rand *rand, world_instance *world, scene_context *scene, ray_hit *hit ) { m4x3f transform; @@ -57,7 +57,7 @@ static void world_gen_add_blob( world_instance *world, float angle = v3_dot(hit->normal,(v3f){0.0f,1.0f,0.0f}); q_axis_angle( qsurface, axis, angle ); - q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf64()*VG_TAUf ); + q_axis_angle( qrandom, (v3f){0.0f,1.0f,0.0f}, vg_randf64(rand)*VG_TAUf ); q_mul( qsurface, qrandom, qsurface ); q_m3x3( qsurface, transform ); v3_copy( hit->pos, transform[3] ); @@ -123,10 +123,8 @@ static void world_apply_procedural_foliage( world_instance *world, float area = volume[0]*volume[2]; u32 particles = 0.08f * area; - /* TODO: Quasirandom? */ vg_info( "Map area: %f. Max particles: %u\n", area, particles ); - u64 t0 = SDL_GetPerformanceCounter(); #if 0 for( u32 i=0; iscene_geo.bbx[0], co ); @@ -170,7 +171,7 @@ static void world_apply_procedural_foliage( world_instance *world, struct world_surface *m1 = ray_hit_surface( world, &hit ); if((hit.normal[1] > 0.8f) && (m1 == mat) && (hit.pos[1] > 0.0f+10.0f)){ - world_gen_add_blob( world, scene, &hit ); + world_gen_add_blob( &rand, world, scene, &hit ); count ++; } } diff --git a/world_load.c b/world_load.c index 4c768bf..b1363b7 100644 --- a/world_load.c +++ b/world_load.c @@ -15,8 +15,6 @@ * load the .mdl file located in path as a world instance */ static void world_instance_load_mdl( u32 instance_id, const char *path ){ - vg_rand_seed( 9001 ); - world_instance *world = &world_static.instances[ instance_id ]; world_init_blank( world ); world->status = k_world_status_loading; diff --git a/world_routes.c b/world_routes.c index 2fe52a6..ca072e6 100644 --- a/world_routes.c +++ b/world_routes.c @@ -488,7 +488,6 @@ static void world_gen_routes_generate( u32 instance_id ){ vg_info( "Generating route meshes\n" ); vg_async_stall(); - vg_rand_seed( 2000 ); vg_async_item *call_scene = scene_alloc_async( &world->scene_lines, &world->mesh_route_lines, 200000, 300000 ); @@ -698,7 +697,7 @@ static void world_routes_fixedupdate( world_instance *world ){ rb_presolve_contacts( rb_contact_buffer, rb_contact_count ); for( int i=0; imlocal[3] ); v3_copy( world_co, particle->obj.rb.co ); - v3_muls( imp_v, 1.0f+vg_randf64(), particle->obj.rb.v ); + v3_muls( imp_v, 1.0f+vg_randf64(&vg.rand), particle->obj.rb.v ); particle->obj.rb.v[1] += 2.0f; v4_copy( q, particle->obj.rb.q ); - particle->obj.rb.w[0] = vg_randf64()*2.0f-1.0f; - particle->obj.rb.w[1] = vg_randf64()*2.0f-1.0f; - particle->obj.rb.w[2] = vg_randf64()*2.0f-1.0f; + particle->obj.rb.w[0] = vg_randf64(&vg.rand)*2.0f-1.0f; + particle->obj.rb.w[1] = vg_randf64(&vg.rand)*2.0f-1.0f; + particle->obj.rb.w[2] = vg_randf64(&vg.rand)*2.0f-1.0f; particle->obj.type = k_rb_shape_sphere; particle->obj.inf.sphere.radius = r*0.6f; -- 2.25.1