+ if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
+ ui_info( panel, "#-1: localplayer" );
+
+ snprintf( buf, 512, "U%.3f/D%.3fkbs",
+ netplayers.up_kbs, netplayers.down_kbs );
+ ui_info( panel, buf );
+
+ for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
+ struct network_player *player = &netplayers.list[i];
+ if( player->active ){
+ const char *sysname = "invalid";
+
+ if( player->subsystem < k_player_subsystem_max ){
+ sysname = player_subsystems[ player->subsystem ]->name;
+ }
+ snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs",
+ i, player->username, sysname, player->down_kbs );
+ ui_info( panel, buf );
+ }
+ }
+ }
+ else {
+ ui_info( panel, "offline" );
+ }
+}
+
+static void remote_player_effect( struct network_player *player,
+ player_pose *final_pose ){
+ /* effects */
+}
+
+/*
+ * write the remote players final_mtx
+ */
+static void pose_remote_player( u32 index,
+ struct interp_frame *f0,
+ struct interp_frame *f1 ){
+
+ struct network_player *player = &netplayers.list[ index ];
+
+ struct interp_buffer *buf = &netplayers.interp_data[ index ];
+ struct skeleton *sk = &localplayer.skeleton;
+ m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
+ struct player_board_pose *board_pose = &netplayers.board_poses[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 );
+
+ u8 instance_id = 0;
+
+ f32 t = 0.0f;
+
+ if( f1 ){
+ 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 );
+
+ u16 bounds = f0->boundary_hash^f1->boundary_hash;
+
+ if( bounds & NETMSG_BOUNDARY_BIT )
+ t = 1.0f;
+
+ if( bounds & NETMSG_GATE_BOUNDARY_BIT ){
+ /* TODO: Extra work retransforming the root_co, instance_id.. etc */
+ t = 1.0f;
+ }
+
+ 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 );
+
+ 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->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 )
+ sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
+ 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 ];
+}
+
+/*
+ * animate remote player and store in final_mtx
+ */
+void animate_remote_player( u32 index )
+{
+ /*
+ * Trys to keep the cursor inside the buffer
+ */
+ f64 min_time = -999999999.9,
+ max_time = 999999999.9,
+ abs_max_time = -999999999.9;
+
+ struct interp_frame *minframe = NULL,
+ *maxframe = NULL,
+ *abs_max_frame = NULL;
+
+ struct interp_buffer *buf = &netplayers.interp_data[index];
+ for( u32 i=0; i<vg_list_size(buf->frames); i ++ ){
+ struct interp_frame *ifr = &buf->frames[i];
+
+ if( ifr->active ){
+ if( (ifr->timestamp > min_time) && (ifr->timestamp < buf->t) ){
+ min_time = ifr->timestamp;
+ minframe = ifr;
+ }
+
+ if( (ifr->timestamp < max_time) && (ifr->timestamp > buf->t) ){
+ max_time = ifr->timestamp;
+ maxframe = ifr;
+ }
+
+ if( ifr->timestamp > abs_max_time ){
+ abs_max_time = ifr->timestamp;
+ abs_max_frame = ifr;
+ }
+ }
+ }
+
+ struct network_player *player = &netplayers.list[ index ];
+ if( player->active != 2 )
+ player->active_world = NULL;
+
+ if( minframe && maxframe ){
+ pose_remote_player( index, minframe, maxframe );
+ buf->t += vg.time_frame_delta;
+ }
+ else {
+ buf->t = abs_max_time - 0.25;
+
+ if( abs_max_frame )
+ pose_remote_player( index, abs_max_frame, NULL );
+ else
+ return;
+ }
+}
+
+/*
+ * Update full final_mtx for all remote players
+ */
+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;
+
+ animate_remote_player( i );
+ }
+}
+
+/*
+ * Draw remote players
+ */
+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;
+ if( !player->isfriend &&
+ (world-world_static.instances == k_world_purpose_hub)) continue;
+
+ 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,
+ player->playermodel_view_slot );
+
+ if( !model ) model = &localplayer.fallback_model;
+ render_playermodel( cam, world, 0, model, sk, final_mtx );
+
+ struct player_board *board =
+ 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[ 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(&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] )
+ player->active_world = &world_static.instances[0];
+ else
+ player->active_world = NULL;
+
+ for( int i=0; i<sizeof(player->username)-1; i ++ ){
+ player->username[i] = 'a' + (vg_randu32(&vg.rand) % 30);
+ player->username[i+1] = '\0';
+
+ if( (vg_randu32(&vg.rand) % 8) == 3 )
+ break;
+ }
+
+ for( int i=0; i<3; i ++ ){
+ player->medals[i] = vg_randu32(&vg.rand) % 3;
+ }
+
+ v3f pos;
+
+ vg_rand_sphere( &vg.rand, pos );
+ v3_muladds( localplayer.rb.co, pos, 100.0f,
+ netplayers.final_mtx[ i*localplayer.skeleton.bone_count][3] );
+ }
+
+ return 0;
+}
+
+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 );
+ for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
+ netplayers.sfx_queue[i].system = k_player_subsystem_invalid;
+ }
+}
+
+void remote_sfx_pre_update(void)
+{
+ for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
+ struct net_sfx *si = &netplayers.sfx_queue[i];
+
+ if( si->system != k_player_subsystem_invalid ){
+ si->subframe -= vg.time_frame_delta;
+ if( si->subframe <= 0.0f ){
+ net_sfx_play( si );
+ si->system = k_player_subsystem_invalid;
+ }
+ }
+ }
+}
+
+/*
+ * animator->root_co of remote player
+ */
+static void remote_player_position( int id, v3f out_co ){
+ struct skeleton *sk = &localplayer.skeleton;
+ m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*id ];
+ v3_copy( final_mtx[0][3], out_co );
+}
+
+enum remote_player_gui_type {
+ k_remote_player_gui_type_stranger,
+ k_remote_player_gui_type_friend,
+ k_remote_player_gui_type_you,
+};
+
+/*
+ * Given players' root_co, get the screen point where we should draw tag info.
+ */
+static int player_tag_position( m4x4f pv, v3f root_co, ui_point out_point ){
+ v4f wpos;
+ v3_copy( root_co, wpos );
+ wpos[1] += 2.0f;
+ wpos[3] = 1.0f;
+
+ m4x4_mulv( pv, wpos, wpos );
+
+ if( wpos[3] > 0.0f ){
+ v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
+ v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
+
+ float k_max = 32000.0f;
+
+ out_point[0] = vg_clampf(wpos[0] * vg.window_x, -k_max, k_max );
+ out_point[1] = vg_clampf((1.0f-wpos[1]) * vg.window_y, -k_max, k_max );
+ return 1;
+ }
+ else
+ return 0;
+}
+
+/*
+ * Draw chat box relative to the root tag position on the screen
+ */
+static void chat_box( ui_point tag_root, f64 time, const char *message ){
+ if( (vg.time_real - time) > 15.0 )
+ return;
+
+ ui_rect wr;
+ wr[2] = ui_text_line_width( message ) + 8;
+ wr[3] = vg_ui.font->glyph_height + 2;
+ wr[0] = tag_root[0]-(wr[2]/2);
+ wr[1] = tag_root[1] - wr[3] - 8;
+
+ ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
+ ui_text( wr, message, 1, k_ui_align_middle_center, 0 );
+}
+
+/*
+ * Draw full imgui for remote player
+ */
+static void remote_player_nametag( ui_point tag_root,
+ struct network_player *player ){
+ vg_ui.font = &vg_ui_font_big;
+
+ ui_rect wr;
+ wr[2] = VG_MAX( ui_text_line_width( player->username ), 140 ) + 8;
+ wr[3] = 32;
+ wr[0] = tag_root[0]-(wr[2]/2);
+ wr[1] = tag_root[1]-(wr[3]/2);
+
+ ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
+ ui_text( wr, player->username, 1, k_ui_align_middle_center, 0 );
+
+ vg_ui.font = &vg_ui_font_small;
+
+ /* medals */
+ int cols = 0;
+ for( int i=0; i<3; i ++ )
+ if( player->medals[i] )
+ cols ++;
+
+ char buf[32];
+ vg_str str;
+
+ if( cols ){
+ f32 w = (f32)wr[2] / (f32)cols;
+ cols = 0;
+
+ for( int i=0; i<3; i ++ ){
+ if( player->medals[i] ){
+ ui_rect col = { wr[0] + (f32)cols*w, wr[1] + wr[3], w,
+ vg_ui.font->glyph_height };
+
+ vg_strnull( &str, buf, 32 );
+ vg_strcatch( &str, (char)k_SRglyph_vg_circle );
+ vg_strcati32( &str, player->medals[i] );
+
+ ui_text( col, buf, 1, k_ui_align_middle_center,
+ ui_colour( (enum ui_scheme_colour[]){
+ k_ui_yellow, k_ui_gray, k_ui_orange }[i] ) );
+
+ cols ++;
+ }
+ }
+ }
+}
+
+static void remote_player_world_gui( m4x4f pv, v3f root_co,
+ struct network_player *player ){
+ ui_point tag_root;
+ if( !player_tag_position( pv, root_co, tag_root ) )
+ return;
+
+ if( player ){
+ remote_player_nametag( tag_root, player );
+ chat_box( tag_root, player->chat_time, player->chat );
+ }
+ else {
+ if( netplayers.chatting )
+ chat_box( tag_root, vg.time_real, netplayers.chat_buffer );
+ else
+ chat_box( tag_root, netplayers.chat_time, netplayers.chat_message );
+ }
+}
+
+static void remote_player_gui_info( ui_rect box,
+ const char *username,
+ const char *activity,
+ enum remote_player_gui_type type,
+ int in_world ){
+
+ f32 opacity = in_world? 0.6f: 0.3f;
+
+ if( type == k_remote_player_gui_type_you )
+ ui_fill( box, ui_opacity( 0xff555555, opacity ) );
+ else
+ ui_fill( box, ui_opacity( 0xff000000, opacity ) );
+
+ if( type == k_remote_player_gui_type_friend )
+ ui_outline( box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
+
+ ui_rect top, bottom;
+ ui_split_ratio( box, k_ui_axis_h, 0.6666f, 1, top, bottom );
+
+ u32 fg;
+
+ if( type == k_remote_player_gui_type_friend )
+ fg = ui_colour( k_ui_yellow + (in_world? k_ui_brighter: 0) );
+ else
+ fg = ui_colour( in_world? k_ui_fg: k_ui_fg+4 );
+
+ vg_ui.font = &vg_ui_font_big;
+ ui_text( top, username, 1, k_ui_align_middle_center, fg );
+ vg_ui.font = &vg_ui_font_small;
+
+ ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
+}
+
+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 },
+ "In World", 1, k_ui_align_middle_center, 0 );
+ vg_ui.font = &vg_ui_font_small;
+
+
+ ui_rect us = { x, y, width, height };
+ /* FIXME: your location */
+ remote_player_gui_info( us, steam_username_at_startup, "you",
+ k_remote_player_gui_type_you, 1 );
+ y += height + gap;
+
+ for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
+ struct network_player *player = &netplayers.list[i];
+ if( !player->active || player->isblocked ) continue;
+
+ 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, location,
+ player->isfriend, in_same_world );
+ y += height + gap;
+ }
+}
+
+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++ ){