Merge branch 'master' of harrygodden.com:/home/carveJwlIkooP6JGAAIwe30JlM
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
index 3e2aa5b9b1cd051c44b5d93afc3d478a369d2f9e..e910170ca3f2d4462adc4a9d1b42811fe4bc39ff 100644 (file)
@@ -5,6 +5,8 @@
 #include "addon.h"
 #include "font.h"
 #include "gui.h"
+#include "ent_miniworld.h"
+#include "ent_region.h"
 
 static i32 k_show_own_name = 0;
 
@@ -175,7 +177,24 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
          .buffer_len = datasize,
          .bytes = 0,
       };
-         
+      
+      /* animation 
+       * -------------------------------------------------------------*/
+
+      dest->timestamp = frame->timestamp;
+      dest->boundary_hash = frame->boundary_hash;
+
+      struct network_player *player = &netplayers.list[ frame->client ];
+      struct player_subsystem_interface *sys = 
+         player_subsystems[ frame->subsystem ];
+
+      if( sys->network_animator_exchange ){
+         memset( &dest->data, 0, sys->animator_size );
+         sys->network_animator_exchange( &ctx, &dest->data );
+      }
+      else {
+         bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
+      }
 
       /* sfx
        * -------------------------------------------------------------*/
@@ -209,24 +228,6 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
             dst->subframe = remaining;
          }
       }
-      
-      /* animation 
-       * -------------------------------------------------------------*/
-
-      dest->timestamp = frame->timestamp;
-      dest->boundary_hash = frame->boundary_hash;
-
-      struct network_player *player = &netplayers.list[ frame->client ];
-      struct player_subsystem_interface *sys = 
-         player_subsystems[ frame->subsystem ];
-
-      if( sys->network_animator_exchange ){
-         memset( &dest->data, 0, sys->animator_size );
-         sys->network_animator_exchange( &ctx, &dest->data );
-      }
-      else {
-         bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
-      }
 
       player->subsystem = frame->subsystem;
       player->down_bytes += msg->m_cbSize;
@@ -276,6 +277,29 @@ static void player_remote_rx_200_300( SteamNetworkingMessage_t *msg ){
          relink_remote_player_worlds( item->client );
       }
    }
+   else if( tmp->inetmsg_id == k_inetmsg_chat ){
+      netmsg_chat *chat = msg->m_pData;
+      
+      struct network_player *player = &netplayers.list[ chat->client ];
+      network_msgstring( chat->msg, msg->m_cbSize, sizeof(netmsg_chat),
+                         player->chat, NETWORK_MAX_CHAT );
+      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);
+   }
 }
 
 /*
@@ -305,13 +329,6 @@ static void remote_player_send_playerframe(void){
          .bytes = 0
       };
 
-      /* sfx
-       * ---------------------------------------------*/
-
-      frame->sound_effects = localplayer.sfx_buffer_count;
-      for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
-         net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
-
       /* animation 
        * -----------------------------------------------*/
 
@@ -322,6 +339,13 @@ static void remote_player_send_playerframe(void){
       else
          bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
 
+      /* sfx
+       * ---------------------------------------------*/
+
+      frame->sound_effects = localplayer.sfx_buffer_count;
+      for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
+         net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
+
       u32 wire_size = base_size + ctx.bytes;
       netplayers.up_bytes += wire_size;
 
@@ -359,6 +383,20 @@ static void remote_player_debug_update(void){
  * Debugging information
  */
 static 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)))
+      {
+         char buf[128];
+         vg_str str;
+         vg_strnull( &str, buf, sizeof(buf) );
+         u32 fg = 0;
+         network_status_string( &str, &fg );
+         ui_text( (ui_rect){ vg.window_x - 200, 0, 200, 48 }, buf, 1, 
+                  k_ui_align_middle_center, fg );
+      }
+   }
+
    if( !network_client.network_info ) 
       return;
 
@@ -422,6 +460,11 @@ static void remote_player_network_imgui( m4x4f pv ){
    }
 }
 
+static void remote_player_effect( struct network_player *player, 
+                                  player_pose *final_pose ){
+   /* effects */
+}
+
 /*
  * write the remote players final_mtx 
  */
@@ -439,6 +482,9 @@ static void pose_remote_player( u32 index,
    struct player_subsystem_interface *sys0 = player_subsystems[f0->subsystem],
                                      *sys1 = NULL;
 
+   struct player_board *board = 
+      addon_cache_item_if_loaded( k_addon_type_board, player->board_view_slot );
+
    player_pose pose0, pose1, posed;
    sys0->pose( &f0->data, &pose0 );
 
@@ -462,15 +508,28 @@ static void pose_remote_player( u32 index,
       }
 
       instance_id = f1->instance_id;
-
       lerp_player_pose( &pose0, &pose1, t, &posed );
+      effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
+
       apply_full_skeleton_pose( sk, &posed, final_mtx );
+
+      if( t < 0.5f ){
+         if( sys0->effects ) 
+            sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
+      }
+      else{
+         if( sys1->effects ) 
+            sys1->effects( &f1->data, final_mtx, board, &player->effect_data );
+      }
+
       memcpy( board_pose, &posed.board, sizeof(*board_pose) );
    }
    else {
       instance_id = f0->instance_id;
-
+      effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
       apply_full_skeleton_pose( sk, &pose0, final_mtx );
+      if( sys0->effects ) 
+         sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
       memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
    }
 
@@ -557,6 +616,8 @@ static void render_remote_players( world_instance *world, camera *cam ){
       struct network_player *player = &netplayers.list[i];
       if( !player->active || player->isblocked ) continue;
       if( player->active_world != world ) continue;
+      if( !player->isfriend && 
+            (world-world_static.instances == k_world_purpose_hub)) continue;
       
       m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*i ];
 
@@ -581,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] )
@@ -593,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] );
    }
@@ -796,15 +859,20 @@ static void remote_player_gui_info( ui_rect box,
 }
 
 static void remote_players_imgui_lobby(void){
-    /*
-    * TODO: send location string over the network */
+   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;
 
 
@@ -818,13 +886,17 @@ 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, "<null>", 
+      remote_player_gui_info( box, player->username, location,
                               player->isfriend, in_same_world );
       y += height + gap;
    }
@@ -832,7 +904,7 @@ static void remote_players_imgui_lobby(void){
 
 static void remote_players_imgui_world( world_instance *world, m4x4f pv,
                                         f32 max_dist, int geo_cull ){
-   ui_flush( k_ui_shader_colour );
+   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
 
    for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
       struct network_player *player = &netplayers.list[i];
@@ -840,8 +912,15 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
          v3f co;
          remote_player_position( i, co );
 
-         if( player->active_world != world ){
+         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 ){
+            m4x3_mulv( global_miniworld.mmdl, co, co );
+            co[1] -= 2.0f; /* HACK lol */
          }
 
          f32 d2 = v3_dist2( co, localplayer.rb.co );
@@ -869,19 +948,17 @@ static void remote_players_imgui_world( world_instance *world, m4x4f pv,
          player->opacity = vg_lerpf( player->opacity, opacity,
                                      vg.time_frame_delta * 2.0f );
          
-         remote_player_world_gui( 
-               pv, netplayers.final_mtx[localplayer.skeleton.bone_count*i][3], 
-               player );
+         remote_player_world_gui( pv, co, player );
 
          vg_ui.colour[3] = player->opacity;
-         ui_flush( k_ui_shader_colour );
+         ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
       }
    }
 
    vg_ui.colour[3] = 1.0f;
 
    remote_player_world_gui( pv, localplayer.rb.co, NULL );
-   ui_flush( k_ui_shader_colour );
+   ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
 }
 
 static void chat_escape(void){
@@ -912,16 +989,16 @@ static void remote_players_chat_imgui(void){
                   UI_TEXTBOX_AUTOFOCUS, &callbacks );
    }
    else {
-      /* TODO: we gotta fix this input fighting crap. */
       if( netplayers.chatting == -1 ){
          netplayers.chatting = 0;
+         srinput.state = k_input_state_resume;
       }
       else {
          if( (skaterift.activity == k_skaterift_default) && 
                button_down( k_srbind_chat ) ){
             netplayers.chatting = 1;
             netplayers.chat_buffer[0] = '\0';
-            srinput.enabled = 0;
+            srinput.state = k_input_state_pause;
          }
       }
    }