1 #include "player_remote.h"
3 #include "player_render.h"
4 #include "player_api.h"
5 #include "network_common.h"
9 #include "ent_miniworld.h"
10 #include "ent_region.h"
11 #include "shaders/model_entity.h"
12 #include "vg/vg_steam_friends.h"
14 struct global_netplayers netplayers
;
16 static i32 k_show_own_name
= 0;
18 static void player_remote_clear( struct network_player
*player
)
20 addon_cache_unwatch( k_addon_type_player
, player
->playermodel_view_slot
);
21 addon_cache_unwatch( k_addon_type_board
, player
->board_view_slot
);
23 memset( player
, 0, sizeof(*player
) );
24 strcpy( player
->username
, "unknown" );
25 player
->subsystem
= k_player_subsystem_invalid
;
29 * re-attatches addon_reg pointers on the remote client if we have the same
32 static void relink_remote_player_worlds( u32 client_id
){
33 struct network_player
*player
= &netplayers
.list
[client_id
];
36 addon_uid_to_alias( player
->items
[k_netmsg_playeritem_world0
], &q
[0] );
37 addon_uid_to_alias( player
->items
[k_netmsg_playeritem_world1
], &q
[1] );
40 * currently in 10.23, the hub world will always be the same.
41 * this might but probably wont change in the future
43 for( u32 i
=0; i
<k_world_max
; i
++ ){
44 addon_reg
*reg
= world_static
.instance_addons
[ i
];
46 player
->world_match
[i
] = 0;
49 if( addon_alias_eq( &q
[i
], &world_static
.instance_addons
[i
]->alias
) )
50 player
->world_match
[i
] = 1;
56 * re-attatches addon_reg pointers on the remote client if we have the mod
59 * Run if local worlds change
61 void relink_all_remote_player_worlds(void)
63 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
64 struct network_player
*player
= &netplayers
.list
[i
];
66 relink_remote_player_worlds(i
);
70 void player_remote_update_friendflags( struct network_player
*remote
)
72 ISteamFriends
*hSteamFriends
= SteamAPI_SteamFriends();
73 remote
->isfriend
= SteamAPI_ISteamFriends_HasFriend( hSteamFriends
,
74 remote
->steamid
, k_EFriendFlagImmediate
);
75 remote
->isblocked
= SteamAPI_ISteamFriends_HasFriend( hSteamFriends
,
76 remote
->steamid
, k_EFriendFlagBlocked
);
79 void player_remote_rx_200_300( SteamNetworkingMessage_t
*msg
)
81 netmsg_blank
*tmp
= msg
->m_pData
;
83 if( tmp
->inetmsg_id
== k_inetmsg_playerjoin
){
84 netmsg_playerjoin
*playerjoin
= msg
->m_pData
;
85 if( !packet_minsize( msg
, sizeof(*playerjoin
) )) return;
87 if( playerjoin
->index
< vg_list_size(netplayers
.list
) ){
88 struct network_player
*player
= &netplayers
.list
[ playerjoin
->index
];
89 player_remote_clear( player
);
91 player
->steamid
= playerjoin
->steamid
;
92 player_remote_update_friendflags( player
);
94 /* TODO: interpret the uids */
95 player
->board_view_slot
= 0;
96 player
->playermodel_view_slot
= 0;
98 struct interp_buffer
*buf
= &netplayers
.interp_data
[playerjoin
->index
];
100 for( u32 i
=0; i
<vg_list_size(buf
->frames
); i
++ ){
101 buf
->frames
[i
].active
= 0;
104 vg_info( "#%u joined friend: %d, blocked: %d\n",
105 playerjoin
->index
, player
->isfriend
, player
->isblocked
);
108 vg_error( "inetmsg_playerjoin: player index out of range\n" );
111 else if( tmp
->inetmsg_id
== k_inetmsg_playerleave
){
112 netmsg_playerleave
*playerleave
= msg
->m_pData
;
113 if( !packet_minsize( msg
, sizeof(*playerleave
) )) return;
115 if( playerleave
->index
< vg_list_size(netplayers
.list
) ){
116 struct network_player
*player
= &netplayers
.list
[ playerleave
->index
];
117 player_remote_clear( player
);
119 vg_info( "player leave (%d)\n", playerleave
->index
);
122 vg_error( "inetmsg_playerleave: player index out of range\n" );
125 else if( tmp
->inetmsg_id
== k_inetmsg_playerusername
){
126 netmsg_playerusername
*update
= msg
->m_pData
;
127 if( !packet_minsize( msg
, sizeof(*update
) )) return;
129 if( update
->index
< vg_list_size(netplayers
.list
) ){
130 struct network_player
*player
= &netplayers
.list
[ update
->index
];
132 network_msgstring( update
->name
, msg
->m_cbSize
, sizeof(*update
),
133 player
->username
, sizeof(player
->username
) );
135 vg_info( "#%u changed username to: %s\n",
136 update
->index
, player
->username
);
139 vg_error( "inetmsg_playerleave: player index out of range\n" );
142 else if( tmp
->inetmsg_id
== k_inetmsg_playerframe
){
143 u32 datasize
= msg
->m_cbSize
- sizeof(netmsg_playerframe
);
145 if( datasize
> sizeof(union interp_animdata
) ){
146 vg_error( "Player frame data exceeds animdata size\n" );
150 netmsg_playerframe
*frame
= msg
->m_pData
;
152 if( frame
->client
>= vg_list_size(netplayers
.list
) ){
153 vg_error( "inetmsg_playerframe: player index out of range\n" );
157 if( frame
->subsystem
>= k_player_subsystem_max
){
158 vg_error( "inetmsg_playerframe: subsystem out of range\n" );
162 struct interp_buffer
*ib
= &netplayers
.interp_data
[ frame
->client
];
163 struct interp_frame
*dest
= NULL
;
165 f64 min_time
= INFINITY
;
166 for( u32 i
=0; i
<vg_list_size(ib
->frames
); i
++ ){
167 struct interp_frame
*ifr
= &ib
->frames
[i
];
174 if( ifr
->timestamp
< min_time
){
175 min_time
= ifr
->timestamp
;
181 dest
->subsystem
= frame
->subsystem
;
182 dest
->flags
= frame
->flags
;
185 .mode
= k_bitpack_decompress
,
186 .buffer
= frame
->animdata
,
187 .buffer_len
= datasize
,
192 * -------------------------------------------------------------*/
194 dest
->timestamp
= frame
->timestamp
;
195 dest
->boundary_hash
= frame
->boundary_hash
;
197 struct network_player
*player
= &netplayers
.list
[ frame
->client
];
198 struct player_subsystem_interface
*sys
=
199 player_subsystems
[ frame
->subsystem
];
201 memset( &dest
->data
, 0, sys
->animator_size
);
202 if( sys
->network_animator_exchange
)
203 sys
->network_animator_exchange( &ctx
, &dest
->data
);
205 bitpack_bytes( &ctx
, sys
->animator_size
, sys
->animator_data
);
208 * -------------------------------------------------------------*/
210 for( u32 i
=0; i
<frame
->sound_effects
; i
++ ){
212 net_sfx_exchange( &ctx
, &sfx
);
214 f64 t
= (frame
->timestamp
- NETWORK_FRAMERATE
) +
215 (sfx
.subframe
*NETWORK_FRAMERATE
);
217 f32 remaining
= t
- ib
->t
;
219 if( remaining
<= 0.0f
)
220 net_sfx_play( &sfx
);
222 struct net_sfx
*dst
= NULL
;
224 for( u32 j
=0; j
<NETWORK_SFX_QUEUE_LENGTH
; j
++ ){
225 struct net_sfx
*sj
= &netplayers
.sfx_queue
[j
];
226 if( sj
->system
== k_player_subsystem_invalid
){
231 if( sj
->priority
< sfx
.priority
)
236 dst
->subframe
= remaining
;
241 * -------------------------------------------------------------*/
243 memset( &dest
->data_glider
, 0, sizeof(struct remote_glider_animator
) );
244 if( dest
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
245 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
246 player_glide_remote_animator_exchange( &ctx
, &dest
->data_glider
);
249 player
->subsystem
= frame
->subsystem
;
250 player
->down_bytes
+= msg
->m_cbSize
;
252 else if( tmp
->inetmsg_id
== k_inetmsg_playeritem
){
253 netmsg_playeritem
*item
= msg
->m_pData
;
254 if( !packet_minsize( msg
, sizeof(*item
)+1 )) return;
256 if( item
->client
>= vg_list_size(netplayers
.list
) ){
257 vg_error( "inetmsg_playerframe: player index out of range\n" );
261 if( item
->type_index
>= k_netmsg_playeritem_max
){
262 vg_warn( "Client #%d invalid equip type %u\n",
263 (i32
)item
->client
, (u32
)item
->type_index
);
267 vg_info( "Client #%d equiped: [%s] %s\n",
269 (const char *[]){[k_netmsg_playeritem_board
]="board",
270 [k_netmsg_playeritem_player
]="player",
271 [k_netmsg_playeritem_world0
]="world0",
272 [k_netmsg_playeritem_world1
]="world1"
273 }[item
->type_index
], item
->uid
);
275 struct network_player
*player
= &netplayers
.list
[ item
->client
];
276 char *uid
= player
->items
[ item
->type_index
];
278 network_msgstring( item
->uid
, msg
->m_cbSize
, sizeof(*item
),
279 uid
, ADDON_UID_MAX
);
281 if( item
->type_index
== k_netmsg_playeritem_board
){
282 addon_cache_unwatch( k_addon_type_board
, player
->board_view_slot
);
283 player
->board_view_slot
=
284 addon_cache_create_viewer_from_uid( k_addon_type_board
, uid
);
286 else if( item
->type_index
== k_netmsg_playeritem_player
){
287 addon_cache_unwatch( k_addon_type_player
,
288 player
->playermodel_view_slot
);
289 player
->playermodel_view_slot
=
290 addon_cache_create_viewer_from_uid( k_addon_type_player
, uid
);
292 else if( (item
->type_index
== k_netmsg_playeritem_world0
) ||
293 (item
->type_index
== k_netmsg_playeritem_world1
) ){
294 relink_remote_player_worlds( item
->client
);
297 else if( tmp
->inetmsg_id
== k_inetmsg_chat
){
298 netmsg_chat
*chat
= msg
->m_pData
;
300 struct network_player
*player
= &netplayers
.list
[ chat
->client
];
301 network_msgstring( chat
->msg
, msg
->m_cbSize
, sizeof(netmsg_chat
),
302 player
->chat
, NETWORK_MAX_CHAT
);
303 player
->chat_time
= vg
.time_real
;
304 vg_info( "[%d]: %s\n", chat
->client
, player
->chat
);
306 else if( tmp
->inetmsg_id
== k_inetmsg_region
){
307 netmsg_region
*region
= msg
->m_pData
;
308 struct network_player
*player
= &netplayers
.list
[ region
->client
];
310 u32 l
= network_msgstring(
311 region
->loc
, msg
->m_cbSize
, sizeof(netmsg_region
),
312 player
->region
, NETWORK_REGION_MAX
);
313 player
->region_flags
= region
->flags
;
316 player
->region_flags
|= k_ent_region_flag_hasname
;
318 player
->effect_data
.spark
.colour
= region_spark_colour(region
->flags
);
323 * Write localplayer pose to network
325 void remote_player_send_playerframe(void)
327 u8 sysid
= localplayer
.subsystem
;
328 if( sysid
>= k_player_subsystem_max
) return;
330 struct player_subsystem_interface
*sys
= player_subsystems
[sysid
];
332 if( sys
->animator_size
){
333 u32 max_buf_size
= sys
->animator_size
+ sizeof(localplayer
.sfx_buffer
),
334 base_size
= sizeof(struct netmsg_playerframe
),
335 max_packet
= base_size
+ max_buf_size
;
337 netmsg_playerframe
*frame
= alloca( max_packet
);
338 frame
->inetmsg_id
= k_inetmsg_playerframe
;
339 frame
->client
= 0xff;
340 frame
->subsystem
= localplayer
.subsystem
;
341 frame
->flags
= world_static
.active_instance
;
344 .mode
= k_bitpack_compress
,
345 .buffer
= frame
->animdata
,
346 .buffer_len
= max_buf_size
,
351 * -----------------------------------------------*/
353 frame
->timestamp
= vg
.time_real
;
354 frame
->boundary_hash
= localplayer
.boundary_hash
;
355 if( sys
->network_animator_exchange
)
356 sys
->network_animator_exchange( &ctx
, sys
->animator_data
);
358 bitpack_bytes( &ctx
, sys
->animator_size
, sys
->animator_data
);
361 * ---------------------------------------------*/
363 frame
->sound_effects
= localplayer
.sfx_buffer_count
;
364 for( u32 i
=0; i
<localplayer
.sfx_buffer_count
; i
++ )
365 net_sfx_exchange( &ctx
, &localplayer
.sfx_buffer
[i
] );
368 * -------------------------------------------------------------*/
370 if( localplayer
.have_glider
||
371 (localplayer
.subsystem
== k_player_subsystem_glide
) ) {
372 frame
->flags
|= NETMSG_PLAYERFRAME_HAVE_GLIDER
;
375 if( localplayer
.glider_orphan
)
376 frame
->flags
|= NETMSG_PLAYERFRAME_GLIDER_ORPHAN
;
378 if( frame
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
379 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
380 player_glide_remote_animator_exchange( &ctx
,
381 &player_glide
.remote_animator
);
386 u32 wire_size
= base_size
+ ctx
.bytes
;
387 netplayers
.up_bytes
+= wire_size
;
389 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
390 hSteamNetworkingSockets
, network_client
.remote
,
392 k_nSteamNetworkingSend_Unreliable
, NULL
);
397 * Updates network traffic stats
399 void remote_player_debug_update(void)
401 if( (vg
.time_real
- netplayers
.last_data_measurement
) > 1.0 ){
402 netplayers
.last_data_measurement
= vg
.time_real
;
405 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
406 struct network_player
*player
= &netplayers
.list
[i
];
407 if( player
->active
){
408 total_down
+= player
->down_bytes
;
409 player
->down_kbs
= ((f32
)player
->down_bytes
)/1024.0f
;
410 player
->down_bytes
= 0;
414 netplayers
.down_kbs
= ((f32
)total_down
)/1024.0f
;
415 netplayers
.up_kbs
= ((f32
)netplayers
.up_bytes
)/1024.0f
;
416 netplayers
.up_bytes
= 0;
421 * Debugging information
423 void remote_player_network_imgui( m4x4f pv
)
425 if( network_client
.user_intent
== k_server_intent_online
){
427 (network_client
.state
== k_ESteamNetworkingConnectionState_Connected
)))
431 vg_strnull( &str
, buf
, sizeof(buf
) );
433 network_status_string( &str
, &fg
);
434 ui_text( (ui_rect
){ vg
.window_x
- 200, 0, 200, 48 }, buf
, 1,
435 k_ui_align_middle_center
, fg
);
439 if( !network_client
.network_info
)
442 ui_rect panel
= { (vg
.window_x
/ 2) - 200, 0, 400, 600 };
443 ui_fill( panel
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
445 ui_font_face( &vgf_default_title
);
446 ui_info( panel
, "Network" );
447 ui_font_face( &vgf_default_large
);
448 ui_info( panel
, "Status" );
449 ui_font_face( &vgf_default_small
);
452 const char *netstatus
= "PROGRAMMING ERROR";
454 struct { enum ESteamNetworkingConnectionState state
; const char *str
; }
456 { k_ESteamNetworkingConnectionState_None
, "None" },
457 { k_ESteamNetworkingConnectionState_Connecting
,
458 (const char *[]){"Connecting -",
462 }[(u32
)(vg
.time_real
/0.25) & 0x3 ] },
463 { k_ESteamNetworkingConnectionState_FindingRoute
, "Finding Route" },
464 { k_ESteamNetworkingConnectionState_Connected
, "Connected" },
465 { k_ESteamNetworkingConnectionState_ClosedByPeer
, "Closed by peer" },
466 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally
,
467 "Problem Detected Locally" },
468 { k_ESteamNetworkingConnectionState_FinWait
, "Fin Wait" },
469 { k_ESteamNetworkingConnectionState_Linger
, "Linger" },
470 { k_ESteamNetworkingConnectionState_Dead
, "Dead" }
472 for( u32 i
=0; i
<vg_list_size(states
); i
++ ){
473 if( states
[i
].state
== network_client
.state
){
474 netstatus
= states
[i
].str
;
478 snprintf( buf
, 512, "Network: %s", netstatus
);
479 ui_info( panel
, buf
);
480 ui_info( panel
, "---------------------" );
482 if( network_client
.state
== k_ESteamNetworkingConnectionState_Connected
){
483 ui_info( panel
, "#-1: localplayer" );
485 snprintf( buf
, 512, "U%.3f/D%.3fkbs",
486 netplayers
.up_kbs
, netplayers
.down_kbs
);
487 ui_info( panel
, buf
);
489 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
490 struct network_player
*player
= &netplayers
.list
[i
];
491 if( player
->active
){
492 const char *sysname
= "invalid";
494 if( player
->subsystem
< k_player_subsystem_max
){
495 sysname
= player_subsystems
[ player
->subsystem
]->name
;
497 snprintf( buf
, 512, "#%u: %s [%s] D%.1fkbs",
498 i
, player
->username
, sysname
, player
->down_kbs
);
499 ui_info( panel
, buf
);
504 ui_info( panel
, "offline" );
508 static void remote_player_effect( struct network_player
*player
,
509 player_pose
*final_pose
){
514 * write the remote players final_mtx
516 static void pose_remote_player( u32 index
,
517 struct interp_frame
*f0
,
518 struct interp_frame
*f1
){
520 struct network_player
*player
= &netplayers
.list
[ index
];
522 struct interp_buffer
*buf
= &netplayers
.interp_data
[ index
];
523 struct skeleton
*sk
= &localplayer
.skeleton
;
524 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*index
];
525 struct player_board_pose
*board_pose
= &netplayers
.board_poses
[index
];
527 struct player_subsystem_interface
*sys0
= player_subsystems
[f0
->subsystem
],
530 struct player_board
*board
=
531 addon_cache_item_if_loaded( k_addon_type_board
, player
->board_view_slot
);
533 player_pose pose0
, pose1
, posed
;
534 sys0
->pose( &f0
->data
, &pose0
);
541 t
= (buf
->t
- f0
->timestamp
) / (f1
->timestamp
- f0
->timestamp
);
542 t
= vg_clampf( t
, 0.0f
, 1.0f
);
544 sys1
= player_subsystems
[f1
->subsystem
];
545 sys1
->pose( &f1
->data
, &pose1
);
547 u16 bounds
= f0
->boundary_hash
^f1
->boundary_hash
;
549 if( bounds
& NETMSG_BOUNDARY_BIT
)
552 if( bounds
& NETMSG_GATE_BOUNDARY_BIT
){
553 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
557 instance_id
= f1
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
558 lerp_player_pose( &pose0
, &pose1
, t
, &posed
);
559 effect_blink_apply( &player
->effect_data
.blink
, &posed
, vg
.time_delta
);
561 apply_full_skeleton_pose( sk
, &posed
, final_mtx
);
565 sys0
->effects( &f0
->data
, final_mtx
, board
, &player
->effect_data
);
569 sys1
->effects( &f1
->data
, final_mtx
, board
, &player
->effect_data
);
572 memcpy( board_pose
, &posed
.board
, sizeof(*board_pose
) );
575 instance_id
= f0
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
576 effect_blink_apply( &player
->effect_data
.blink
, &pose0
, vg
.time_delta
);
577 apply_full_skeleton_pose( sk
, &pose0
, final_mtx
);
579 sys0
->effects( &f0
->data
, final_mtx
, board
, &player
->effect_data
);
580 memcpy( board_pose
, &pose0
.board
, sizeof(*board_pose
) );
583 if( f0
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
584 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
585 player
->render_glider
= 1;
592 v3_lerp( f0
->data_glider
.root_co
, f1
->data_glider
.root_co
, t
, co
);
593 q_nlerp( f0
->data_glider
.root_q
, f1
->data_glider
.root_q
, t
, q
);
594 s
= vg_lerpf( f0
->data_glider
.s
, f1
->data_glider
.s
, t
);
597 v3_copy( f0
->data_glider
.root_co
, co
);
598 v4_copy( f0
->data_glider
.root_q
, q
);
599 s
= f0
->data_glider
.s
;
602 v3f
*mtx
= netplayers
.glider_mtx
[ index
];
604 m3x3_scalef( mtx
, s
);
605 v3_copy( co
, mtx
[3] );
608 player
->render_glider
= 0;
610 if( player
->world_match
[ instance_id
] )
611 player
->active_world
= &world_static
.instances
[ instance_id
];
615 * animate remote player and store in final_mtx
617 void animate_remote_player( u32 index
)
620 * Trys to keep the cursor inside the buffer
622 f64 min_time
= -999999999.9,
623 max_time
= 999999999.9,
624 abs_max_time
= -999999999.9;
626 struct interp_frame
*minframe
= NULL
,
628 *abs_max_frame
= NULL
;
630 struct interp_buffer
*buf
= &netplayers
.interp_data
[index
];
631 for( u32 i
=0; i
<vg_list_size(buf
->frames
); i
++ ){
632 struct interp_frame
*ifr
= &buf
->frames
[i
];
635 if( (ifr
->timestamp
> min_time
) && (ifr
->timestamp
< buf
->t
) ){
636 min_time
= ifr
->timestamp
;
640 if( (ifr
->timestamp
< max_time
) && (ifr
->timestamp
> buf
->t
) ){
641 max_time
= ifr
->timestamp
;
645 if( ifr
->timestamp
> abs_max_time
){
646 abs_max_time
= ifr
->timestamp
;
652 struct network_player
*player
= &netplayers
.list
[ index
];
653 if( player
->active
!= 2 )
654 player
->active_world
= NULL
;
656 if( minframe
&& maxframe
){
657 pose_remote_player( index
, minframe
, maxframe
);
658 buf
->t
+= vg
.time_frame_delta
;
661 buf
->t
= abs_max_time
- 0.25;
664 pose_remote_player( index
, abs_max_frame
, NULL
);
671 * Update full final_mtx for all remote players
673 void animate_remote_players(void)
675 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
676 struct network_player
*player
= &netplayers
.list
[i
];
677 if( !player
->active
) continue;
679 animate_remote_player( i
);
684 * Draw remote players
686 void render_remote_players( world_instance
*world
, vg_camera
*cam
)
688 u32 draw_list
[ NETWORK_MAX_PLAYERS
],
692 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
693 struct network_player
*player
= &netplayers
.list
[i
];
694 if( !player
->active
|| player
->isblocked
) continue;
695 if( player
->active_world
!= world
) continue;
698 if( !player
->isfriend
&&
699 (world
-world_static
.instances
== k_world_purpose_hub
)) continue;
702 draw_list
[draw_list_count
++] = i
;
704 if( player
->render_glider
)
708 struct skeleton
*sk
= &localplayer
.skeleton
;
710 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
712 for( u32 j
=0; j
<draw_list_count
; j
++ ){
713 u32 index
= draw_list
[j
];
715 struct network_player
*player
= &netplayers
.list
[index
];
716 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*index
];
718 struct player_model
*model
=
719 addon_cache_item_if_loaded( k_addon_type_player
,
720 player
->playermodel_view_slot
);
722 if( !model
) model
= &localplayer
.fallback_model
;
723 render_playermodel( cam
, world
, 0, model
, sk
, final_mtx
);
725 struct player_board
*board
=
726 addon_cache_item_if_loaded( k_addon_type_board
,
727 player
->board_view_slot
);
728 render_board( cam
, world
, board
, final_mtx
[localplayer
.id_board
],
729 &netplayers
.board_poses
[ index
], k_board_shader_player
);
732 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
737 /* TODO: we really, really only need to do all this once. at some point
738 * PLEASE figure out a good place to do this once per frame!
741 shader_model_entity_use();
742 shader_model_entity_uTexMain( 0 );
743 shader_model_entity_uCamera( cam
->transform
[3] );
744 shader_model_entity_uPv( cam
->mtx
.pv
);
746 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
748 for( u32 j
=0; j
<draw_list_count
; j
++ ){
749 u32 index
= draw_list
[j
];
751 struct network_player
*player
= &netplayers
.list
[index
];
752 if( !player
->render_glider
) continue;
754 if( player
->render_glider
){
755 v3f
*glider_mtx
= netplayers
.glider_mtx
[ index
];
756 render_glider_model( cam
, world
, glider_mtx
, k_board_shader_entity
);
761 static int remote_players_randomize( int argc
, const char *argv
[] ){
762 for( int i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
763 struct network_player
*player
= &netplayers
.list
[i
];
765 player
->active
= (vg_randu32(&vg
.rand
) & 0x1)? 2: 0;
766 player
->isfriend
= vg_randu32(&vg
.rand
) & vg_randu32(&vg
.rand
) & 0x1;
767 player
->isblocked
= vg_randu32(&vg
.rand
) &
768 vg_randu32(&vg
.rand
) &
769 vg_randu32(&vg
.rand
) & 0x1;
770 player
->world_match
[ 0 ] = vg_randu32(&vg
.rand
) & 0x1;
771 player
->world_match
[ 1 ] = 0;
773 if( player
->world_match
[0] )
774 player
->active_world
= &world_static
.instances
[0];
776 player
->active_world
= NULL
;
778 for( int i
=0; i
<sizeof(player
->username
)-1; i
++ ){
779 player
->username
[i
] = 'a' + (vg_randu32(&vg
.rand
) % 30);
780 player
->username
[i
+1] = '\0';
782 if( (vg_randu32(&vg
.rand
) % 8) == 3 )
786 for( int i
=0; i
<3; i
++ ){
787 player
->medals
[i
] = vg_randu32(&vg
.rand
) % 3;
792 vg_rand_sphere( &vg
.rand
, pos
);
793 v3_muladds( localplayer
.rb
.co
, pos
, 100.0f
,
794 netplayers
.final_mtx
[ i
*localplayer
.skeleton
.bone_count
][3] );
800 void remote_players_init(void)
802 vg_console_reg_cmd( "add_test_players", remote_players_randomize
, NULL
);
803 vg_console_reg_var( "k_show_own_name", &k_show_own_name
,
804 k_var_dtype_i32
, 0 );
805 for( u32 i
=0; i
<NETWORK_SFX_QUEUE_LENGTH
; i
++ ){
806 netplayers
.sfx_queue
[i
].system
= k_player_subsystem_invalid
;
810 void remote_sfx_pre_update(void)
812 for( u32 i
=0; i
<NETWORK_SFX_QUEUE_LENGTH
; i
++ ){
813 struct net_sfx
*si
= &netplayers
.sfx_queue
[i
];
815 if( si
->system
!= k_player_subsystem_invalid
){
816 si
->subframe
-= vg
.time_frame_delta
;
817 if( si
->subframe
<= 0.0f
){
819 si
->system
= k_player_subsystem_invalid
;
826 * animator->root_co of remote player
828 static void remote_player_position( int id
, v3f out_co
){
829 struct skeleton
*sk
= &localplayer
.skeleton
;
830 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*id
];
831 v3_copy( final_mtx
[0][3], out_co
);
834 enum remote_player_gui_type
{
835 k_remote_player_gui_type_stranger
,
836 k_remote_player_gui_type_friend
,
837 k_remote_player_gui_type_you
,
841 * Given players' root_co, get the screen point where we should draw tag info.
843 static int player_tag_position( m4x4f pv
, v3f root_co
, ui_point out_point
){
845 v3_copy( root_co
, wpos
);
849 m4x4_mulv( pv
, wpos
, wpos
);
851 if( wpos
[3] > 0.0f
){
852 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
853 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
855 float k_max
= 32000.0f
;
857 out_point
[0] = vg_clampf(wpos
[0] * vg
.window_x
, -k_max
, k_max
);
858 out_point
[1] = vg_clampf((1.0f
-wpos
[1]) * vg
.window_y
, -k_max
, k_max
);
866 * Draw chat box relative to the root tag position on the screen
868 static void chat_box( ui_point tag_root
, f64 time
, const char *message
)
870 if( (vg
.time_real
- time
) > 15.0 )
874 wr
[2] = ui_text_line_width( message
) + 8;
875 wr
[3] = vg_ui
.font
->ch
+ 2;
876 wr
[0] = tag_root
[0]-(wr
[2]/2);
877 wr
[1] = tag_root
[1] - wr
[3] - 8;
879 ui_fill( wr
, ui_opacity( ui_colour(k_ui_bg
), 0.23f
) );
880 ui_text( wr
, message
, 1, k_ui_align_middle_center
, 0 );
884 * Draw full imgui for remote player
886 static void remote_player_nametag( ui_point tag_root
,
887 struct network_player
*player
)
889 ui_font_face( &vgf_default_large
);
892 wr
[2] = VG_MAX( ui_text_line_width( player
->username
), 140 ) + 8;
894 wr
[0] = tag_root
[0]-(wr
[2]/2);
895 wr
[1] = tag_root
[1]-(wr
[3]/2);
897 ui_fill( wr
, ui_opacity( ui_colour(k_ui_bg
), 0.23f
) );
898 ui_text( wr
, player
->username
, 1, k_ui_align_middle_center
, 0 );
900 ui_font_face( &vgf_default_small
);
904 for( int i
=0; i
<3; i
++ )
905 if( player
->medals
[i
] )
912 f32 w
= (f32
)wr
[2] / (f32
)cols
;
915 for( int i
=0; i
<3; i
++ ){
916 if( player
->medals
[i
] ){
917 ui_rect col
= { wr
[0] + (f32
)cols
*w
, wr
[1] + wr
[3], w
,
920 vg_strnull( &str
, buf
, 32 );
922 vg_strcatch( &str
, (char)k_SRglyph_vg_circle
);
924 vg_strcati32( &str
, player
->medals
[i
] );
926 ui_text( col
, buf
, 1, k_ui_align_middle_center
,
927 ui_colour( (enum ui_scheme_colour
[]){
928 k_ui_yellow
, k_ui_gray
, k_ui_orange
}[i
] ) );
936 static void remote_player_world_gui( m4x4f pv
, v3f root_co
,
937 struct network_player
*player
){
939 if( !player_tag_position( pv
, root_co
, tag_root
) )
943 remote_player_nametag( tag_root
, player
);
944 chat_box( tag_root
, player
->chat_time
, player
->chat
);
947 if( netplayers
.chatting
)
948 chat_box( tag_root
, vg
.time_real
, netplayers
.chat_buffer
);
950 chat_box( tag_root
, netplayers
.chat_time
, netplayers
.chat_message
);
954 static void remote_player_gui_info( ui_rect box
,
955 const char *username
,
956 const char *activity
,
957 enum remote_player_gui_type type
,
960 f32 opacity
= in_world
? 0.6f
: 0.3f
;
962 if( type
== k_remote_player_gui_type_you
)
963 ui_fill( box
, ui_opacity( 0xff555555, opacity
) );
965 ui_fill( box
, ui_opacity( 0xff000000, opacity
) );
967 if( type
== k_remote_player_gui_type_friend
)
968 ui_outline( box
, -1, ui_opacity( 0xff00c4f0, opacity
), 0 );
971 ui_split_ratio( box
, k_ui_axis_h
, 0.6666f
, 1, top
, bottom
);
975 if( type
== k_remote_player_gui_type_friend
)
976 fg
= ui_colour( k_ui_yellow
+ (in_world
? k_ui_brighter
: 0) );
978 fg
= ui_colour( in_world
? k_ui_fg
: k_ui_fg
+4 );
980 ui_font_face( &vgf_default_large
);
981 ui_text( top
, username
, 1, k_ui_align_middle_center
, fg
);
982 ui_font_face( &vgf_default_small
);
984 ui_text( bottom
, activity
, 1, k_ui_align_middle_center
, fg
);
987 void remote_players_imgui_lobby(void)
989 if( network_client
.user_intent
== k_server_intent_online
){
991 (network_client
.state
== k_ESteamNetworkingConnectionState_Connected
)))
997 ui_px y
= 50, width
= 200, height
= 42, gap
= 2,
998 x
= vg
.window_x
- width
;
1000 ui_font_face( &vgf_default_large
);
1001 ui_text( (ui_rect
){ x
, 0, width
, height
},
1002 "In World", 1, k_ui_align_middle_center
, 0 );
1003 ui_font_face( &vgf_default_small
);
1006 ui_rect us
= { x
, y
, width
, height
};
1007 /* FIXME: your location */
1008 remote_player_gui_info( us
, steam_username_at_startup
, "you",
1009 k_remote_player_gui_type_you
, 1 );
1012 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
1013 struct network_player
*player
= &netplayers
.list
[i
];
1014 if( !player
->active
|| player
->isblocked
) continue;
1016 int in_same_world
= player
->active_world
== world_current_instance();
1017 if( !player
->isfriend
&& !in_same_world
)
1020 const char *location
= in_same_world
? "": "another world";
1021 if( player
->region_flags
& k_ent_region_flag_hasname
){
1022 location
= player
->region
;
1025 ui_rect box
= { x
, y
, width
, height
};
1026 remote_player_gui_info( box
, player
->username
, location
,
1027 player
->isfriend
, in_same_world
);
1032 void remote_players_imgui_world( world_instance
*world
, m4x4f pv
,
1033 f32 max_dist
, int geo_cull
)
1035 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1037 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
1038 struct network_player
*player
= &netplayers
.list
[i
];
1039 if( player
->active
){
1041 remote_player_position( i
, co
);
1043 if( !player
->active_world
)
1045 if( !player
->isfriend
&&
1046 (world
-world_static
.instances
== k_world_purpose_hub
)) continue;
1048 /* their in our active subworld */
1049 if( player
->active_world
!= world
){
1050 m4x3_mulv( global_miniworld
.mmdl
, co
, co
);
1051 co
[1] -= 2.0f
; /* HACK lol */
1054 f32 d2
= v3_dist2( co
, localplayer
.rb
.co
);
1056 if( d2
> (max_dist
*max_dist
) )
1059 f32 dist
= sqrtf(d2
);
1060 f32 opacity
= 0.95f
* sqrtf(((max_dist
-dist
)/max_dist
));
1067 v3_sub( co
, skaterift
.cam
.pos
, dir
);
1068 v3_normalize( dir
);
1070 if( ray_world( world
, skaterift
.cam
.pos
, dir
, &hit
,
1071 k_material_flag_ghosts
) ){
1076 player
->opacity
= vg_lerpf( player
->opacity
, opacity
,
1077 vg
.time_frame_delta
* 2.0f
);
1079 remote_player_world_gui( pv
, co
, player
);
1081 vg_ui
.colour
[3] = player
->opacity
;
1082 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1086 vg_ui
.colour
[3] = 1.0f
;
1088 remote_player_world_gui( pv
, localplayer
.rb
.co
, NULL
);
1089 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1092 static void chat_escape(void){
1093 netplayers
.chatting
= -1;
1096 static void chat_enter( char *buf
, u32 len
){
1097 vg_strncpy( buf
, netplayers
.chat_message
, NETWORK_MAX_CHAT
,
1098 k_strncpy_always_add_null
);
1099 netplayers
.chatting
= -1;
1100 netplayers
.chat_time
= vg
.time_real
;
1101 chat_send_message( buf
);
1104 void remote_players_chat_imgui(void)
1106 if( netplayers
.chatting
== 1 ){
1107 ui_rect box
= { 0, 0, 400, 40 },
1108 window
= { 0, 0, vg
.window_x
, vg
.window_y
};
1109 ui_rect_center( window
, box
);
1111 struct ui_textbox_callbacks callbacks
= {
1112 .enter
= chat_enter
,
1113 .escape
= chat_escape
1116 ui_textbox( box
, NULL
,
1117 netplayers
.chat_buffer
, NETWORK_MAX_CHAT
, 1,
1118 UI_TEXTBOX_AUTOFOCUS
, &callbacks
);
1121 if( netplayers
.chatting
== -1 ){
1122 netplayers
.chatting
= 0;
1123 srinput
.state
= k_input_state_resume
;
1126 if( (skaterift
.activity
== k_skaterift_default
) &&
1127 button_down( k_srbind_chat
) ){
1128 netplayers
.chatting
= 1;
1129 netplayers
.chat_buffer
[0] = '\0';
1130 srinput
.state
= k_input_state_pause
;