seperate rand instances for each thread
authorhgn <hgodden00@gmail.com>
Sun, 10 Dec 2023 15:36:18 +0000 (15:36 +0000)
committerhgn <hgodden00@gmail.com>
Sun, 10 Dec 2023 15:36:18 +0000 (15:36 +0000)
12 files changed:
audio.h
particle.c
player_effects.c
player_ragdoll.c
player_remote.c
player_skate.c
player_walk.c
world_audio.c
world_entity.c
world_gen.c
world_load.c
world_routes.c

diff --git a/audio.h b/audio.h
index 0d025c8195e75ef7159dcf5fb7f7371b97c87092..917b8ec8732ab6c6a8be7b374ec7f2112df8eac3 100644 (file)
--- 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] );
          }
       }
    }
index 13cc5d423c2b364da639e4a06f8d86ed077101a7..d12a87a024e3db67de24a1134810655e884775d2 100644 (file)
@@ -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 );
index 0092f19a0c3bbab18a1e5730755a2cf4887a8387..2bfc3bf3bcfcaad691c4ec16471a079a3e64eac1 100644 (file)
@@ -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 );
index ac6e90240fc9fd3a6743dafa057b246777d86da5..f0a6f88c7b5de75bd31888f0fe70a699d6e7d72b 100644 (file)
@@ -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();
    }
 }
index 2234c6dc994aa3f50b8fdc0cf217603762dd551a..e910170ca3f2d4462adc4a9d1b42811fe4bc39ff 100644 (file)
@@ -642,10 +642,12 @@ static int remote_players_randomize( int argc, const char *argv[] ){
    for( int i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
       struct network_player *player = &netplayers.list[i];
 
-      player->active = (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; i<sizeof(player->username)-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] );
    }
index c70d1f39b9ea4f4b0fbe91322ff7ea731670a4fd..7f4bc37f741991deb72b99cca2495cfe0a2731af 100644 (file)
@@ -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 );
index 717411c88476cdd7bd56f74f060e186c22cd005a..34ad072a8a5b5d9d27f1cf38dc174f1386cddd28 100644 (file)
@@ -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 
          );
index 1e902fad724f3581941a999b25d35dc159cb376e..0ecb6eb5d0c03443ff8ceeccc3cf778c0690d9c3 100644 (file)
@@ -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 );
index 9095e1b4310a63f7ebbc5a97ccc0de4a0610e5dd..ec7c2fe13426a5cbc0d773ef7711080bee0b0e80 100644 (file)
@@ -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; i<audio->clip_count; i++ ){
index 3d705ab50156a0c5fa89c51bb03ab855147ad9e9..e2ec14af8edd774ad28c7f441b624d3c66109d4c 100644 (file)
@@ -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; i<particles; i++ ){
@@ -148,6 +146,9 @@ static void world_apply_procedural_foliage( world_instance *world,
       }
    }
 #else
+
+   vg_rand rand;
+   vg_rand_seed( &rand, 3030 );
    
    const f32 tile_scale = 16.0f;
    v2i tiles = { volume[0]/tile_scale, volume[2]/tile_scale };
@@ -157,7 +158,7 @@ static void world_apply_procedural_foliage( world_instance *world,
    for( i32 x=0; x<tiles[0]; x ++ ){
       for( i32 z=0; z<tiles[1]; z ++ ){
          for( u32 i=0; i<per_tile; i ++ ){
-            v3f co = { (f32)x+vg_randf64(), 0.0f, (f32)z+vg_randf64() };
+            v3f co = { (f32)x+vg_randf64(&rand), 0, (f32)z+vg_randf64(&rand) };
             v3_muls( co, tile_scale, co );
             co[1] = 1000.0f;
             v3_add( co, world->scene_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 ++;
                }
             }
index 4c768bfe4e1027124d17349b760b4e9608dad9fe..b1363b7a9a056c9d55a29685e80bfb4b0c024350 100644 (file)
@@ -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;
index 2fe52a66367e61c76441c31b01295f64ada2610b..ca072e62985290569c6a7d780e2ed0b588efea99 100644 (file)
@@ -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; i<rb_contact_count; i++ ){
-      rb_contact_restitution( rb_contact_buffer+i, vg_randf64() );
+      rb_contact_restitution( rb_contact_buffer+i, vg_randf64(&vg.rand) );
    }
 
    for( int i=0; i<6; i++ ){
@@ -882,13 +881,13 @@ static void world_routes_fracture( world_instance *world, ent_gate *gate,
             v3_muls( origin, -1.0f, particle->mlocal[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;