Fix dumbass animator fucking uninitialized memory
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
index 9e46321fc35e5ea6da930c0ab4977b6278050ab0..5b6aef0e8ac15b83245ee75a0234508fd345c7a4 100644 (file)
@@ -1,15 +1,22 @@
 #include "player_remote.h"
 #include "skeleton.h"
 #include "player_render.h"
+#include "player_api.h"
 #include "network_common.h"
 #include "addon.h"
 #include "font.h"
 #include "gui.h"
 #include "ent_miniworld.h"
+#include "ent_region.h"
+#include "shaders/model_entity.h"
+#include "vg/vg_steam_friends.h"
+
+struct global_netplayers netplayers;
 
 static i32 k_show_own_name = 0;
 
-static void player_remote_clear( struct network_player *player ){
+static void player_remote_clear( struct network_player *player )
+{
    addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot );
    addon_cache_unwatch( k_addon_type_board, player->board_view_slot );
 
@@ -51,7 +58,8 @@ static void relink_remote_player_worlds( u32 client_id ){
  *
  * Run if local worlds change
  */
-static void relink_all_remote_player_worlds(void){
+void relink_all_remote_player_worlds(void)
+{
    for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
       struct network_player *player = &netplayers.list[i];
       if( player->active )
@@ -59,7 +67,8 @@ static void relink_all_remote_player_worlds(void){
    }
 }
 
-static void player_remote_update_friendflags( struct network_player *remote ){
+void player_remote_update_friendflags( struct network_player *remote )
+{
    ISteamFriends *hSteamFriends = SteamAPI_SteamFriends();
    remote->isfriend = SteamAPI_ISteamFriends_HasFriend( hSteamFriends,
                         remote->steamid, k_EFriendFlagImmediate );
@@ -67,7 +76,8 @@ static void player_remote_update_friendflags( struct network_player *remote ){
                         remote->steamid, k_EFriendFlagBlocked );
 }
 
-static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
+void player_remote_rx_200_300( SteamNetworkingMessage_t *msg )
+{
    netmsg_blank *tmp = msg->m_pData;
 
    if( tmp->inetmsg_id == k_inetmsg_playerjoin ){
@@ -138,6 +148,7 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       }
       
       netmsg_playerframe *frame = msg->m_pData;
+
       if( frame->client >= vg_list_size(netplayers.list) ){
          vg_error( "inetmsg_playerframe: player index out of range\n" );
          return;
@@ -168,7 +179,7 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
 
       dest->active = 1;
       dest->subsystem = frame->subsystem;
-      dest->instance_id = frame->instance_id;
+      dest->flags = frame->flags;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_decompress,
@@ -187,13 +198,11 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       struct player_subsystem_interface *sys = 
          player_subsystems[ frame->subsystem ];
 
-      if( sys->network_animator_exchange ){
-         memset( &dest->data, 0, sys->animator_size );
+      memset( &dest->data, 0, sys->animator_size );
+      if( sys->network_animator_exchange )
          sys->network_animator_exchange( &ctx, &dest->data );
-      }
-      else {
+      else 
          bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
-      }
 
       /* sfx
        * -------------------------------------------------------------*/
@@ -228,6 +237,15 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
          }
       }
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      memset( &dest->data_glider, 0, sizeof(struct remote_glider_animator) );
+      if( dest->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                         NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx, &dest->data_glider );
+      }
+
       player->subsystem = frame->subsystem;
       player->down_bytes += msg->m_cbSize;
    }
@@ -285,12 +303,27 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
       player->chat_time = vg.time_real;
       vg_info( "[%d]: %s\n", chat->client, player->chat );
    }
+   else if( tmp->inetmsg_id == k_inetmsg_region ){
+      netmsg_region *region = msg->m_pData;
+      struct network_player *player = &netplayers.list[ region->client ];
+
+      u32 l = network_msgstring( 
+               region->loc, msg->m_cbSize, sizeof(netmsg_region),
+               player->region, NETWORK_REGION_MAX );
+      player->region_flags = region->flags;
+
+      if( l )
+         player->region_flags |= k_ent_region_flag_hasname;
+
+      player->effect_data.spark.colour = region_spark_colour(region->flags);
+   }
 }
 
 /*
  * Write localplayer pose to network
  */
-static void remote_player_send_playerframe(void){
+void remote_player_send_playerframe(void)
+{
    u8 sysid = localplayer.subsystem;
    if( sysid >= k_player_subsystem_max ) return;
 
@@ -305,7 +338,7 @@ static void remote_player_send_playerframe(void){
       frame->inetmsg_id = k_inetmsg_playerframe;
       frame->client = 0xff;
       frame->subsystem = localplayer.subsystem;
-      frame->instance_id = world_static.active_instance;
+      frame->flags = world_static.active_instance;
 
       bitpack_ctx ctx = {
          .mode = k_bitpack_compress,
@@ -331,6 +364,25 @@ static void remote_player_send_playerframe(void){
       for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
          net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
 
+      /* glider
+       * -------------------------------------------------------------*/
+
+      if( localplayer.have_glider || 
+            (localplayer.subsystem == k_player_subsystem_glide) ) {
+         frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
+      }
+
+      if( localplayer.glider_orphan )
+         frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
+
+      if( frame->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                          NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+         player_glide_remote_animator_exchange( &ctx,    
+                                                &player_glide.remote_animator );
+      }
+
+      /* ------- */
+
       u32 wire_size = base_size + ctx.bytes;
       netplayers.up_bytes += wire_size;
 
@@ -344,7 +396,8 @@ static void remote_player_send_playerframe(void){
 /*
  * Updates network traffic stats
  */
-static void remote_player_debug_update(void){
+void remote_player_debug_update(void)
+{
    if( (vg.time_real - netplayers.last_data_measurement) > 1.0 ){
       netplayers.last_data_measurement = vg.time_real;
       u32 total_down = 0;
@@ -367,7 +420,8 @@ static void remote_player_debug_update(void){
 /*
  * Debugging information
  */
-static void remote_player_network_imgui( m4x4f pv ){
+void remote_player_network_imgui( m4x4f pv )
+{
    if( network_client.user_intent == k_server_intent_online ){
       if( !(steam_ready &&
          (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
@@ -475,9 +529,11 @@ static void pose_remote_player( u32 index,
 
    u8 instance_id = 0;
 
+   f32 t = 0.0f;
+
    if( f1 ){
-      f32 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
-          t = vg_clampf( t, 0.0f, 1.0f );
+      t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
+      t = vg_clampf( t, 0.0f, 1.0f );
 
       sys1 = player_subsystems[f1->subsystem];
       sys1->pose( &f1->data, &pose1 );
@@ -492,7 +548,7 @@ static void pose_remote_player( u32 index,
          t = 1.0f;
       }
 
-      instance_id = f1->instance_id;
+      instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       lerp_player_pose( &pose0, &pose1, t, &posed );
       effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
 
@@ -510,7 +566,7 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &posed.board, sizeof(*board_pose) );
    }
    else {
-      instance_id = f0->instance_id;
+      instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
       effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
       apply_full_skeleton_pose( sk, &pose0, final_mtx );
       if( sys0->effects ) 
@@ -518,6 +574,33 @@ static void pose_remote_player( u32 index,
       memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
    }
 
+   if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
+                    NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
+      player->render_glider = 1;
+
+      v3f co;
+      v4f q;
+      f32 s;
+
+      if( f1 ){
+         v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
+         q_nlerp( f0->data_glider.root_q,  f1->data_glider.root_q,  t, q );
+         s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
+      }
+      else {
+         v3_copy( f0->data_glider.root_co, co );
+         v4_copy( f0->data_glider.root_q, q );
+         s = f0->data_glider.s;
+      }
+
+      v3f *mtx = netplayers.glider_mtx[ index ];
+      q_m3x3( q, mtx );
+      m3x3_scalef( mtx, s );
+      v3_copy( co, mtx[3] );
+   }
+   else
+      player->render_glider = 0;
+
    if( player->world_match[ instance_id ] )
       player->active_world = &world_static.instances[ instance_id ];
 }
@@ -525,8 +608,8 @@ static void pose_remote_player( u32 index,
 /* 
  * animate remote player and store in final_mtx
  */
-static void animate_remote_player( u32 index ){
-
+void animate_remote_player( u32 index )
+{
    /*
     * Trys to keep the cursor inside the buffer
     */
@@ -581,7 +664,8 @@ static void animate_remote_player( u32 index ){
 /*
  * Update full final_mtx for all remote players
  */
-static void animate_remote_players(void){
+void animate_remote_players(void)
+{
    for( u32 i=0; i<vg_list_size(netplayers.list); i ++ ){
       struct network_player *player = &netplayers.list[i];
       if( !player->active ) continue;
@@ -593,16 +677,37 @@ static void animate_remote_players(void){
 /*
  * Draw remote players
  */
-static void render_remote_players( world_instance *world, camera *cam ){
-   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
-   struct skeleton *sk = &localplayer.skeleton;
+void render_remote_players( world_instance *world, vg_camera *cam )
+{
+   u32 draw_list[ NETWORK_MAX_PLAYERS ],
+       draw_list_count = 0,
+       gliders = 0;
 
    for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
       struct network_player *player = &netplayers.list[i];
       if( !player->active || player->isblocked ) continue;
       if( player->active_world != world ) continue;
-      
-      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*i ];
+
+#if 0
+      if( !player->isfriend && 
+            (world-world_static.instances == k_world_purpose_hub)) continue;
+#endif
+
+      draw_list[draw_list_count ++] = i;
+
+      if( player->render_glider )
+         gliders ++;
+   }
+
+   struct skeleton *sk = &localplayer.skeleton;
+
+   SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
 
       struct player_model *model = 
          addon_cache_item_if_loaded( k_addon_type_player, 
@@ -615,20 +720,48 @@ static void render_remote_players( world_instance *world, camera *cam ){
          addon_cache_item_if_loaded( k_addon_type_board,
                                      player->board_view_slot );
       render_board( cam, world, board, final_mtx[localplayer.id_board],
-                     &netplayers.board_poses[ i ], k_board_shader_player );
+                     &netplayers.board_poses[ index ], k_board_shader_player );
    }
 
    SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+
+   if( !gliders )
+      return;
+
+   /* TODO: we really, really only need to do all this once. at some point
+    *       PLEASE figure out a good place to do this once per frame!
+    */
+   
+   shader_model_entity_use();
+   shader_model_entity_uTexMain( 0 );
+   shader_model_entity_uCamera( cam->transform[3] );
+   shader_model_entity_uPv( cam->mtx.pv );
+   
+   WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
+
+   for( u32 j=0; j<draw_list_count; j ++ ){
+      u32 index = draw_list[j];
+
+      struct network_player *player = &netplayers.list[index];
+      if( !player->render_glider ) continue;
+
+      if( player->render_glider ){
+         v3f *glider_mtx = netplayers.glider_mtx[ index ];
+         render_glider_model( cam, world, glider_mtx, k_board_shader_entity );
+      }
+   }
 }
 
 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] )
@@ -637,20 +770,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] );
    }
@@ -658,7 +791,8 @@ static int remote_players_randomize( int argc, const char *argv[] ){
    return 0;
 }
 
-static void remote_players_init(void){
+void remote_players_init(void)
+{
    vg_console_reg_cmd( "add_test_players", remote_players_randomize, NULL );
    vg_console_reg_var( "k_show_own_name", &k_show_own_name, 
                        k_var_dtype_i32, 0 );
@@ -667,7 +801,8 @@ static void remote_players_init(void){
    }
 }
 
-static void remote_sfx_pre_update(void){
+void remote_sfx_pre_update(void)
+{
    for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
       struct net_sfx *si = &netplayers.sfx_queue[i];
 
@@ -839,16 +974,22 @@ static void remote_player_gui_info( ui_rect box,
    ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
 }
 
-static void remote_players_imgui_lobby(void){
-    /*
-    * TODO: send location string over the network */
+void remote_players_imgui_lobby(void)
+{
+   if( network_client.user_intent == k_server_intent_online ){
+      if( !(steam_ready &&
+         (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
+      {
+         return;
+      }
+   }
 
    ui_px y = 50, width = 200, height = 42, gap = 2,
          x = vg.window_x - width;
 
    vg_ui.font = &vg_ui_font_big;
    ui_text( (ui_rect){ x, 0, width, height }, 
-            "Online Players", 1, k_ui_align_middle_center, 0 );
+            "In World", 1, k_ui_align_middle_center, 0 );
    vg_ui.font = &vg_ui_font_small;
 
 
@@ -862,21 +1003,25 @@ static void remote_players_imgui_lobby(void){
       struct network_player *player = &netplayers.list[i];
       if( !player->active || player->isblocked ) continue;
 
-      int in_same_world = player->active_world != world_current_instance();
-
+      int in_same_world = player->active_world == world_current_instance();
       if( !player->isfriend && !in_same_world )
          continue;
+
+      const char *location = in_same_world? "": "another world";
+      if( player->region_flags & k_ent_region_flag_hasname ){
+         location = player->region;
+      }
       
       ui_rect box = { x, y, width, height };
-      remote_player_gui_info( box, player->username, 
-                              in_same_world? "": "another world", 
+      remote_player_gui_info( box, player->username, location,
                               player->isfriend, in_same_world );
       y += height + gap;
    }
 }
 
-static void remote_players_imgui_world( world_instance *world, m4x4f pv,
-                                        f32 max_dist, int geo_cull ){
+void remote_players_imgui_world( world_instance *world, m4x4f pv,
+                                 f32 max_dist, int geo_cull )
+{
    ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
 
    for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
@@ -887,6 +1032,8 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
 
          if( !player->active_world )
             continue;
+         if( !player->isfriend && 
+               (world-world_static.instances == k_world_purpose_hub)) continue;
 
          /* their in our active subworld */
          if( player->active_world != world ){
@@ -944,7 +1091,8 @@ static void chat_enter( char *buf, u32 len ){
    chat_send_message( buf );
 }
 
-static void remote_players_chat_imgui(void){
+void remote_players_chat_imgui(void)
+{
    if( netplayers.chatting == 1 ){
       ui_rect box = { 0, 0, 400, 40 },
               window = { 0, 0, vg.window_x, vg.window_y };