add one shots to replay buffer
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
index c019f698484881551872ff89a171ecedfb1314de..c115162099fb6d4b075ebcccb8f420b90eda5632 100644 (file)
--- a/player.c
+++ b/player.c
@@ -323,36 +323,51 @@ static void net_sfx_play( struct net_sfx *sfx ){
    }
 };
 
-static void player__networked_sfx( u8 system, u8 priority, u8 id, 
-                                   v3f pos, f32 volume ){
-   struct net_sfx null, *sfx = &null;
-
-   if( localplayer.sfx_buffer_count < vg_list_size(localplayer.sfx_buffer) )
-      sfx = &localplayer.sfx_buffer[ localplayer.sfx_buffer_count ++ ];
+static struct net_sfx *find_lower_priority_sfx( struct net_sfx *buffer, u32 len, 
+                                                u32 *count, u8 priority ){
+   struct net_sfx *p_sfx = NULL;
+   if( *count < len ){
+      p_sfx = &buffer[ *count ];
+      *count = *count+1;
+   }
    else {
-      for( u32 i=0; i<vg_list_size(localplayer.sfx_buffer); i++ ){
-         struct net_sfx *a = &localplayer.sfx_buffer[i];
+      for( u32 i=0; i<len; i++ ){
+         struct net_sfx *a = &buffer[i];
          if( a->priority < priority ){
-            sfx = a;
+            p_sfx = a;
             break;
          }
       }
    }
 
-   sfx->id = id;
-   sfx->priority = priority;
-   sfx->volume = volume;
-   v3_copy( pos, sfx->location );
-   sfx->system = system;
+   return p_sfx;
+}
 
+static void player__networked_sfx( u8 system, u8 priority, u8 id, 
+                                   v3f pos, f32 volume ){
+   struct net_sfx sfx,
+         *p_net = find_lower_priority_sfx( 
+               localplayer.sfx_buffer, 4, 
+               &localplayer.sfx_buffer_count, priority ),
+         *p_replay = find_lower_priority_sfx(
+               localplayer.local_sfx_buffer, 2,
+               &localplayer.local_sfx_buffer_count, priority );
+
+   sfx.id = id;
+   sfx.priority = priority;
+   sfx.volume = volume;
+   v3_copy( pos, sfx.location );
+   sfx.system = system;
+
+   /* we only care about subframe in networked sfx. local replays run at a 
+    * high enough framerate. */
    f32 t = (vg.time_real - network_client.last_frame) / NETWORK_FRAMERATE;
-   sfx->subframe = vg_clampf( t, 0.0f, 1.0f );
+   sfx.subframe = vg_clampf( t, 0.0f, 1.0f );
 
-   net_sfx_play( sfx );
-}
+   if( p_net ) *p_net = sfx;
+   if( p_replay ) *p_replay = sfx;
 
-static void player__clear_sfx_buffer(void){
-   localplayer.sfx_buffer_count = 0;
+   net_sfx_play( &sfx );
 }
 
 /* implementation */