+
+ struct player_avatar *av = localplayer.playeravatar;
+ remote_player_nametag(
+ pv,
+ localplayer.final_mtx[0][3],
+ "Localplayer" );
+}
+
+/*
+ * 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 player_avatar *av = localplayer.playeravatar;
+ m4x3f *final_mtx = &netplayers.final_mtx[ av->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;
+
+ player_pose pose0, pose1, posed;
+ sys0->pose( &f0->data, &pose0 );
+
+ u8 instance_id = 0;
+
+ if( f1 ){
+ f32 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->instance_id;
+
+ lerp_player_pose( &pose0, &pose1, t, &posed );
+ apply_full_skeleton_pose( &av->sk, &posed, final_mtx );
+ memcpy( board_pose, &posed.board, sizeof(*board_pose) );
+ }
+ else {
+ instance_id = f0->instance_id;
+
+ apply_full_skeleton_pose( &av->sk, &pose0, final_mtx );
+ memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
+ }
+
+ if( instance_id ){
+ if( player->client_match ){
+ player->active_world = &world_static.instances[ instance_id ];
+ }
+ }
+ else{
+ if( player->hub_match ){
+ player->active_world = &world_static.instances[ instance_id ];
+ }
+ }
+}
+
+/*
+ * animate remote player and store in final_mtx
+ */
+static 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 ];
+ 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
+ */
+static 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
+ */
+static void render_remote_players( world_instance *world, camera *cam ){
+ SDL_AtomicLock( &addon_system.sl_cache_using_resources );
+
+ for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
+ struct network_player *player = &netplayers.list[i];
+ if( !player->active ) continue;
+ if( player->active_world != world ) continue;
+
+ struct player_avatar *av = localplayer.playeravatar;
+ m4x3f *final_mtx = &netplayers.final_mtx[ av->sk.bone_count*i ];
+
+ 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, &av->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.playeravatar->id_board],
+ &netplayers.board_poses[ i ],
+ k_board_shader_player );
+ }
+
+ SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
+}
+
+static void render_remote_player_nametag( v3f co, const char *name ){
+ m4x3f mlocal;
+ m4x3_identity( mlocal );
+ mlocal[3][0] -= font3d_string_width( 2, name ) * 0.5f;
+
+ m4x3f mmdl;
+ m3x3_identity( mmdl );
+ for( int i=0; i<3; i++ )
+ v3_muls( skaterift.cam.mtx.v[i], 0.2f, mmdl[i] );
+ m3x3_transpose( mmdl, mmdl );
+ v3_add( co, (v3f){0.0f,2.0f,0.0f}, mmdl[3] );
+
+ m4x3_mul( mmdl, mlocal, mmdl );
+ font3d_simple_draw( 2, name, &skaterift.cam, mmdl );
+}
+
+static void render_remote_players_tags( world_instance *world, camera *cam ){
+ glEnable(GL_BLEND);
+ glEnable(GL_DEPTH_TEST);
+ glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
+ glBlendEquation(GL_FUNC_ADD);
+
+ font3d_bind( &gui.font, k_font_shader_default, 1, NULL, &skaterift.cam );
+ font3d_setcolour( (v4f){1.0f,1.0f,1.0f,1.0f} );
+
+ render_remote_player_nametag(
+ localplayer.final_mtx[0][3],
+ steam_username_at_startup );
+
+ for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
+ struct network_player *player = &netplayers.list[i];
+ if( !player->active ) continue;
+ if( player->active_world != world ) continue;
+
+ struct player_avatar *av = localplayer.playeravatar;
+ render_remote_player_nametag(
+ netplayers.final_mtx[av->sk.bone_count*i][3],
+ player->username );
+ }
+
+ glDisable(GL_BLEND);
+}
+
+static void remote_players_init(void){
+ for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
+ netplayers.sfx_queue[i].system = k_player_subsystem_invalid;
+ }
+}
+
+static 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;
+ }
+ }
+ }