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
;
151 if( frame
->client
>= vg_list_size(netplayers
.list
) ){
152 vg_error( "inetmsg_playerframe: player index out of range\n" );
156 if( frame
->subsystem
>= k_player_subsystem_max
){
157 vg_error( "inetmsg_playerframe: subsystem out of range\n" );
161 struct interp_buffer
*ib
= &netplayers
.interp_data
[ frame
->client
];
162 struct interp_frame
*dest
= NULL
;
164 f64 min_time
= INFINITY
;
165 for( u32 i
=0; i
<vg_list_size(ib
->frames
); i
++ ){
166 struct interp_frame
*ifr
= &ib
->frames
[i
];
173 if( ifr
->timestamp
< min_time
){
174 min_time
= ifr
->timestamp
;
180 dest
->subsystem
= frame
->subsystem
;
181 dest
->flags
= frame
->flags
;
184 .mode
= k_bitpack_decompress
,
185 .buffer
= frame
->animdata
,
186 .buffer_len
= datasize
,
191 * -------------------------------------------------------------*/
193 dest
->timestamp
= frame
->timestamp
;
194 dest
->boundary_hash
= frame
->boundary_hash
;
196 struct network_player
*player
= &netplayers
.list
[ frame
->client
];
197 struct player_subsystem_interface
*sys
=
198 player_subsystems
[ frame
->subsystem
];
200 memset( &dest
->data
, 0, sys
->animator_size
);
201 if( sys
->network_animator_exchange
)
202 sys
->network_animator_exchange( &ctx
, &dest
->data
);
204 bitpack_bytes( &ctx
, sys
->animator_size
, sys
->animator_data
);
207 * -------------------------------------------------------------*/
209 for( u32 i
=0; i
<frame
->sound_effects
; i
++ ){
211 net_sfx_exchange( &ctx
, &sfx
);
213 f64 t
= (frame
->timestamp
- NETWORK_FRAMERATE
) +
214 (sfx
.subframe
*NETWORK_FRAMERATE
);
216 f32 remaining
= t
- ib
->t
;
218 if( remaining
<= 0.0f
)
219 net_sfx_play( &sfx
);
221 struct net_sfx
*dst
= NULL
;
223 for( u32 j
=0; j
<NETWORK_SFX_QUEUE_LENGTH
; j
++ ){
224 struct net_sfx
*sj
= &netplayers
.sfx_queue
[j
];
225 if( sj
->system
== k_player_subsystem_invalid
){
230 if( sj
->priority
< sfx
.priority
)
235 dst
->subframe
= remaining
;
240 * -------------------------------------------------------------*/
242 memset( &dest
->data_glider
, 0, sizeof(struct remote_glider_animator
) );
243 if( dest
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
244 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
245 player_glide_remote_animator_exchange( &ctx
, &dest
->data_glider
);
248 player
->subsystem
= frame
->subsystem
;
249 player
->down_bytes
+= msg
->m_cbSize
;
251 else if( tmp
->inetmsg_id
== k_inetmsg_playeritem
){
252 netmsg_playeritem
*item
= msg
->m_pData
;
253 if( !packet_minsize( msg
, sizeof(*item
)+1 )) return;
255 if( item
->client
>= vg_list_size(netplayers
.list
) ){
256 vg_error( "inetmsg_playerframe: player index out of range\n" );
260 if( item
->type_index
>= k_netmsg_playeritem_max
){
261 vg_warn( "Client #%d invalid equip type %u\n",
262 (i32
)item
->client
, (u32
)item
->type_index
);
266 vg_info( "Client #%d equiped: [%s] %s\n",
268 (const char *[]){[k_netmsg_playeritem_board
]="board",
269 [k_netmsg_playeritem_player
]="player",
270 [k_netmsg_playeritem_world0
]="world0",
271 [k_netmsg_playeritem_world1
]="world1"
272 }[item
->type_index
], item
->uid
);
274 struct network_player
*player
= &netplayers
.list
[ item
->client
];
275 char *uid
= player
->items
[ item
->type_index
];
277 network_msgstring( item
->uid
, msg
->m_cbSize
, sizeof(*item
),
278 uid
, ADDON_UID_MAX
);
280 if( item
->type_index
== k_netmsg_playeritem_board
){
281 addon_cache_unwatch( k_addon_type_board
, player
->board_view_slot
);
282 player
->board_view_slot
=
283 addon_cache_create_viewer_from_uid( k_addon_type_board
, uid
);
285 else if( item
->type_index
== k_netmsg_playeritem_player
){
286 addon_cache_unwatch( k_addon_type_player
,
287 player
->playermodel_view_slot
);
288 player
->playermodel_view_slot
=
289 addon_cache_create_viewer_from_uid( k_addon_type_player
, uid
);
291 else if( (item
->type_index
== k_netmsg_playeritem_world0
) ||
292 (item
->type_index
== k_netmsg_playeritem_world1
) ){
293 relink_remote_player_worlds( item
->client
);
296 else if( tmp
->inetmsg_id
== k_inetmsg_chat
){
297 netmsg_chat
*chat
= msg
->m_pData
;
299 struct network_player
*player
= &netplayers
.list
[ chat
->client
];
300 network_msgstring( chat
->msg
, msg
->m_cbSize
, sizeof(netmsg_chat
),
301 player
->chat
, NETWORK_MAX_CHAT
);
302 player
->chat_time
= vg
.time_real
;
303 vg_info( "[%d]: %s\n", chat
->client
, player
->chat
);
305 else if( tmp
->inetmsg_id
== k_inetmsg_region
){
306 netmsg_region
*region
= msg
->m_pData
;
307 struct network_player
*player
= &netplayers
.list
[ region
->client
];
309 u32 l
= network_msgstring(
310 region
->loc
, msg
->m_cbSize
, sizeof(netmsg_region
),
311 player
->region
, NETWORK_REGION_MAX
);
312 player
->region_flags
= region
->flags
;
315 player
->region_flags
|= k_ent_region_flag_hasname
;
317 player
->effect_data
.spark
.colour
= region_spark_colour(region
->flags
);
322 * Write localplayer pose to network
324 void remote_player_send_playerframe(void)
326 u8 sysid
= localplayer
.subsystem
;
327 if( sysid
>= k_player_subsystem_max
) return;
329 struct player_subsystem_interface
*sys
= player_subsystems
[sysid
];
331 if( sys
->animator_size
){
332 u32 max_buf_size
= sys
->animator_size
+ sizeof(localplayer
.sfx_buffer
),
333 base_size
= sizeof(struct netmsg_playerframe
),
334 max_packet
= base_size
+ max_buf_size
;
336 netmsg_playerframe
*frame
= alloca( max_packet
);
337 frame
->inetmsg_id
= k_inetmsg_playerframe
;
338 frame
->client
= 0xff;
339 frame
->subsystem
= localplayer
.subsystem
;
340 frame
->flags
= world_static
.active_instance
;
343 .mode
= k_bitpack_compress
,
344 .buffer
= frame
->animdata
,
345 .buffer_len
= max_buf_size
,
350 * -----------------------------------------------*/
352 frame
->timestamp
= vg
.time_real
;
353 frame
->boundary_hash
= localplayer
.boundary_hash
;
354 if( sys
->network_animator_exchange
)
355 sys
->network_animator_exchange( &ctx
, sys
->animator_data
);
357 bitpack_bytes( &ctx
, sys
->animator_size
, sys
->animator_data
);
360 * ---------------------------------------------*/
362 frame
->sound_effects
= localplayer
.sfx_buffer_count
;
363 for( u32 i
=0; i
<localplayer
.sfx_buffer_count
; i
++ )
364 net_sfx_exchange( &ctx
, &localplayer
.sfx_buffer
[i
] );
367 * -------------------------------------------------------------*/
369 if( localplayer
.have_glider
||
370 (localplayer
.subsystem
== k_player_subsystem_glide
) ) {
371 frame
->flags
|= NETMSG_PLAYERFRAME_HAVE_GLIDER
;
374 if( localplayer
.glider_orphan
)
375 frame
->flags
|= NETMSG_PLAYERFRAME_GLIDER_ORPHAN
;
377 if( frame
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
378 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
379 player_glide_remote_animator_exchange( &ctx
,
380 &player_glide
.remote_animator
);
385 u32 wire_size
= base_size
+ ctx
.bytes
;
386 netplayers
.up_bytes
+= wire_size
;
388 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
389 hSteamNetworkingSockets
, network_client
.remote
,
391 k_nSteamNetworkingSend_Unreliable
, NULL
);
396 * Updates network traffic stats
398 void remote_player_debug_update(void)
400 if( (vg
.time_real
- netplayers
.last_data_measurement
) > 1.0 ){
401 netplayers
.last_data_measurement
= vg
.time_real
;
404 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
405 struct network_player
*player
= &netplayers
.list
[i
];
406 if( player
->active
){
407 total_down
+= player
->down_bytes
;
408 player
->down_kbs
= ((f32
)player
->down_bytes
)/1024.0f
;
409 player
->down_bytes
= 0;
413 netplayers
.down_kbs
= ((f32
)total_down
)/1024.0f
;
414 netplayers
.up_kbs
= ((f32
)netplayers
.up_bytes
)/1024.0f
;
415 netplayers
.up_bytes
= 0;
420 * Debugging information
422 void remote_player_network_imgui( m4x4f pv
)
424 if( network_client
.user_intent
== k_server_intent_online
){
426 (network_client
.state
== k_ESteamNetworkingConnectionState_Connected
)))
430 vg_strnull( &str
, buf
, sizeof(buf
) );
432 network_status_string( &str
, &fg
);
433 ui_text( (ui_rect
){ vg
.window_x
- 200, 0, 200, 48 }, buf
, 1,
434 k_ui_align_middle_center
, fg
);
438 if( !network_client
.network_info
)
441 ui_rect panel
= { (vg
.window_x
/ 2) - 200, 0, 400, 600 };
442 ui_fill( panel
, (ui_colour(k_ui_bg
)&0x00ffffff)|0x50000000 );
445 const char *netstatus
= "PROGRAMMING ERROR";
447 struct { enum ESteamNetworkingConnectionState state
; const char *str
; }
449 { k_ESteamNetworkingConnectionState_None
, "None" },
450 { k_ESteamNetworkingConnectionState_Connecting
,
451 (const char *[]){"Connecting -",
455 }[(u32
)(vg
.time_real
/0.25) & 0x3 ] },
456 { k_ESteamNetworkingConnectionState_FindingRoute
, "Finding Route" },
457 { k_ESteamNetworkingConnectionState_Connected
, "Connected" },
458 { k_ESteamNetworkingConnectionState_ClosedByPeer
, "Closed by peer" },
459 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally
,
460 "Problem Detected Locally" },
461 { k_ESteamNetworkingConnectionState_FinWait
, "Fin Wait" },
462 { k_ESteamNetworkingConnectionState_Linger
, "Linger" },
463 { k_ESteamNetworkingConnectionState_Dead
, "Dead" }
465 for( u32 i
=0; i
<vg_list_size(states
); i
++ ){
466 if( states
[i
].state
== network_client
.state
){
467 netstatus
= states
[i
].str
;
471 snprintf( buf
, 512, "Network: %s", netstatus
);
472 ui_info( panel
, buf
);
473 ui_info( panel
, "---------------------" );
475 if( network_client
.state
== k_ESteamNetworkingConnectionState_Connected
){
476 ui_info( panel
, "#-1: localplayer" );
478 snprintf( buf
, 512, "U%.3f/D%.3fkbs",
479 netplayers
.up_kbs
, netplayers
.down_kbs
);
480 ui_info( panel
, buf
);
482 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
483 struct network_player
*player
= &netplayers
.list
[i
];
484 if( player
->active
){
485 const char *sysname
= "invalid";
487 if( player
->subsystem
< k_player_subsystem_max
){
488 sysname
= player_subsystems
[ player
->subsystem
]->name
;
490 snprintf( buf
, 512, "#%u: %s [%s] D%.1fkbs",
491 i
, player
->username
, sysname
, player
->down_kbs
);
492 ui_info( panel
, buf
);
497 ui_info( panel
, "offline" );
501 static void remote_player_effect( struct network_player
*player
,
502 player_pose
*final_pose
){
507 * write the remote players final_mtx
509 static void pose_remote_player( u32 index
,
510 struct interp_frame
*f0
,
511 struct interp_frame
*f1
){
513 struct network_player
*player
= &netplayers
.list
[ index
];
515 struct interp_buffer
*buf
= &netplayers
.interp_data
[ index
];
516 struct skeleton
*sk
= &localplayer
.skeleton
;
517 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*index
];
518 struct player_board_pose
*board_pose
= &netplayers
.board_poses
[index
];
520 struct player_subsystem_interface
*sys0
= player_subsystems
[f0
->subsystem
],
523 struct player_board
*board
=
524 addon_cache_item_if_loaded( k_addon_type_board
, player
->board_view_slot
);
526 player_pose pose0
, pose1
, posed
;
527 sys0
->pose( &f0
->data
, &pose0
);
534 t
= (buf
->t
- f0
->timestamp
) / (f1
->timestamp
- f0
->timestamp
);
535 t
= vg_clampf( t
, 0.0f
, 1.0f
);
537 sys1
= player_subsystems
[f1
->subsystem
];
538 sys1
->pose( &f1
->data
, &pose1
);
540 u16 bounds
= f0
->boundary_hash
^f1
->boundary_hash
;
542 if( bounds
& NETMSG_BOUNDARY_BIT
)
545 if( bounds
& NETMSG_GATE_BOUNDARY_BIT
){
546 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
550 instance_id
= f1
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
551 lerp_player_pose( &pose0
, &pose1
, t
, &posed
);
552 effect_blink_apply( &player
->effect_data
.blink
, &posed
, vg
.time_delta
);
554 apply_full_skeleton_pose( sk
, &posed
, final_mtx
);
558 sys0
->effects( &f0
->data
, final_mtx
, board
, &player
->effect_data
);
562 sys1
->effects( &f1
->data
, final_mtx
, board
, &player
->effect_data
);
565 memcpy( board_pose
, &posed
.board
, sizeof(*board_pose
) );
568 instance_id
= f0
->flags
& NETMSG_PLAYERFRAME_INSTANCE_ID
;
569 effect_blink_apply( &player
->effect_data
.blink
, &pose0
, vg
.time_delta
);
570 apply_full_skeleton_pose( sk
, &pose0
, final_mtx
);
572 sys0
->effects( &f0
->data
, final_mtx
, board
, &player
->effect_data
);
573 memcpy( board_pose
, &pose0
.board
, sizeof(*board_pose
) );
576 if( f0
->flags
& (NETMSG_PLAYERFRAME_HAVE_GLIDER
|
577 NETMSG_PLAYERFRAME_GLIDER_ORPHAN
) ){
578 player
->render_glider
= 1;
585 v3_lerp( f0
->data_glider
.root_co
, f1
->data_glider
.root_co
, t
, co
);
586 q_nlerp( f0
->data_glider
.root_q
, f1
->data_glider
.root_q
, t
, q
);
587 s
= vg_lerpf( f0
->data_glider
.s
, f1
->data_glider
.s
, t
);
590 v3_copy( f0
->data_glider
.root_co
, co
);
591 v4_copy( f0
->data_glider
.root_q
, q
);
592 s
= f0
->data_glider
.s
;
595 v3f
*mtx
= netplayers
.glider_mtx
[ index
];
597 m3x3_scalef( mtx
, s
);
598 v3_copy( co
, mtx
[3] );
601 player
->render_glider
= 0;
603 if( player
->world_match
[ instance_id
] )
604 player
->active_world
= &world_static
.instances
[ instance_id
];
608 * animate remote player and store in final_mtx
610 void animate_remote_player( u32 index
)
613 * Trys to keep the cursor inside the buffer
615 f64 min_time
= -999999999.9,
616 max_time
= 999999999.9,
617 abs_max_time
= -999999999.9;
619 struct interp_frame
*minframe
= NULL
,
621 *abs_max_frame
= NULL
;
623 struct interp_buffer
*buf
= &netplayers
.interp_data
[index
];
624 for( u32 i
=0; i
<vg_list_size(buf
->frames
); i
++ ){
625 struct interp_frame
*ifr
= &buf
->frames
[i
];
628 if( (ifr
->timestamp
> min_time
) && (ifr
->timestamp
< buf
->t
) ){
629 min_time
= ifr
->timestamp
;
633 if( (ifr
->timestamp
< max_time
) && (ifr
->timestamp
> buf
->t
) ){
634 max_time
= ifr
->timestamp
;
638 if( ifr
->timestamp
> abs_max_time
){
639 abs_max_time
= ifr
->timestamp
;
645 struct network_player
*player
= &netplayers
.list
[ index
];
646 if( player
->active
!= 2 )
647 player
->active_world
= NULL
;
649 if( minframe
&& maxframe
){
650 pose_remote_player( index
, minframe
, maxframe
);
651 buf
->t
+= vg
.time_frame_delta
;
654 buf
->t
= abs_max_time
- 0.25;
657 pose_remote_player( index
, abs_max_frame
, NULL
);
664 * Update full final_mtx for all remote players
666 void animate_remote_players(void)
668 for( u32 i
=0; i
<vg_list_size(netplayers
.list
); i
++ ){
669 struct network_player
*player
= &netplayers
.list
[i
];
670 if( !player
->active
) continue;
672 animate_remote_player( i
);
677 * Draw remote players
679 void render_remote_players( world_instance
*world
, vg_camera
*cam
)
681 u32 draw_list
[ NETWORK_MAX_PLAYERS
],
685 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
686 struct network_player
*player
= &netplayers
.list
[i
];
687 if( !player
->active
|| player
->isblocked
) continue;
688 if( player
->active_world
!= world
) continue;
689 if( !player
->isfriend
&&
690 (world
-world_static
.instances
== k_world_purpose_hub
)) continue;
692 draw_list
[draw_list_count
++] = i
;
694 if( player
->render_glider
)
698 struct skeleton
*sk
= &localplayer
.skeleton
;
700 SDL_AtomicLock( &addon_system
.sl_cache_using_resources
);
702 for( u32 j
=0; j
<draw_list_count
; j
++ ){
703 u32 index
= draw_list
[j
];
705 struct network_player
*player
= &netplayers
.list
[index
];
706 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*index
];
708 struct player_model
*model
=
709 addon_cache_item_if_loaded( k_addon_type_player
,
710 player
->playermodel_view_slot
);
712 if( !model
) model
= &localplayer
.fallback_model
;
713 render_playermodel( cam
, world
, 0, model
, sk
, final_mtx
);
715 struct player_board
*board
=
716 addon_cache_item_if_loaded( k_addon_type_board
,
717 player
->board_view_slot
);
718 render_board( cam
, world
, board
, final_mtx
[localplayer
.id_board
],
719 &netplayers
.board_poses
[ index
], k_board_shader_player
);
722 SDL_AtomicUnlock( &addon_system
.sl_cache_using_resources
);
727 /* TODO: we really, really only need to do all this once. at some point
728 * PLEASE figure out a good place to do this once per frame!
731 shader_model_entity_use();
732 shader_model_entity_uTexMain( 0 );
733 shader_model_entity_uCamera( cam
->transform
[3] );
734 shader_model_entity_uPv( cam
->mtx
.pv
);
736 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world
, model_entity
);
738 for( u32 j
=0; j
<draw_list_count
; j
++ ){
739 u32 index
= draw_list
[j
];
741 struct network_player
*player
= &netplayers
.list
[index
];
742 if( !player
->render_glider
) continue;
744 if( player
->render_glider
){
745 v3f
*glider_mtx
= netplayers
.glider_mtx
[ index
];
746 render_glider_model( cam
, world
, glider_mtx
, k_board_shader_entity
);
751 static int remote_players_randomize( int argc
, const char *argv
[] ){
752 for( int i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
753 struct network_player
*player
= &netplayers
.list
[i
];
755 player
->active
= (vg_randu32(&vg
.rand
) & 0x1)? 2: 0;
756 player
->isfriend
= vg_randu32(&vg
.rand
) & vg_randu32(&vg
.rand
) & 0x1;
757 player
->isblocked
= vg_randu32(&vg
.rand
) &
758 vg_randu32(&vg
.rand
) &
759 vg_randu32(&vg
.rand
) & 0x1;
760 player
->world_match
[ 0 ] = vg_randu32(&vg
.rand
) & 0x1;
761 player
->world_match
[ 1 ] = 0;
763 if( player
->world_match
[0] )
764 player
->active_world
= &world_static
.instances
[0];
766 player
->active_world
= NULL
;
768 for( int i
=0; i
<sizeof(player
->username
)-1; i
++ ){
769 player
->username
[i
] = 'a' + (vg_randu32(&vg
.rand
) % 30);
770 player
->username
[i
+1] = '\0';
772 if( (vg_randu32(&vg
.rand
) % 8) == 3 )
776 for( int i
=0; i
<3; i
++ ){
777 player
->medals
[i
] = vg_randu32(&vg
.rand
) % 3;
782 vg_rand_sphere( &vg
.rand
, pos
);
783 v3_muladds( localplayer
.rb
.co
, pos
, 100.0f
,
784 netplayers
.final_mtx
[ i
*localplayer
.skeleton
.bone_count
][3] );
790 void remote_players_init(void)
792 vg_console_reg_cmd( "add_test_players", remote_players_randomize
, NULL
);
793 vg_console_reg_var( "k_show_own_name", &k_show_own_name
,
794 k_var_dtype_i32
, 0 );
795 for( u32 i
=0; i
<NETWORK_SFX_QUEUE_LENGTH
; i
++ ){
796 netplayers
.sfx_queue
[i
].system
= k_player_subsystem_invalid
;
800 void remote_sfx_pre_update(void)
802 for( u32 i
=0; i
<NETWORK_SFX_QUEUE_LENGTH
; i
++ ){
803 struct net_sfx
*si
= &netplayers
.sfx_queue
[i
];
805 if( si
->system
!= k_player_subsystem_invalid
){
806 si
->subframe
-= vg
.time_frame_delta
;
807 if( si
->subframe
<= 0.0f
){
809 si
->system
= k_player_subsystem_invalid
;
816 * animator->root_co of remote player
818 static void remote_player_position( int id
, v3f out_co
){
819 struct skeleton
*sk
= &localplayer
.skeleton
;
820 m4x3f
*final_mtx
= &netplayers
.final_mtx
[ sk
->bone_count
*id
];
821 v3_copy( final_mtx
[0][3], out_co
);
824 enum remote_player_gui_type
{
825 k_remote_player_gui_type_stranger
,
826 k_remote_player_gui_type_friend
,
827 k_remote_player_gui_type_you
,
831 * Given players' root_co, get the screen point where we should draw tag info.
833 static int player_tag_position( m4x4f pv
, v3f root_co
, ui_point out_point
){
835 v3_copy( root_co
, wpos
);
839 m4x4_mulv( pv
, wpos
, wpos
);
841 if( wpos
[3] > 0.0f
){
842 v2_muls( wpos
, (1.0f
/wpos
[3]) * 0.5f
, wpos
);
843 v2_add( wpos
, (v2f
){ 0.5f
, 0.5f
}, wpos
);
845 float k_max
= 32000.0f
;
847 out_point
[0] = vg_clampf(wpos
[0] * vg
.window_x
, -k_max
, k_max
);
848 out_point
[1] = vg_clampf((1.0f
-wpos
[1]) * vg
.window_y
, -k_max
, k_max
);
856 * Draw chat box relative to the root tag position on the screen
858 static void chat_box( ui_point tag_root
, f64 time
, const char *message
){
859 if( (vg
.time_real
- time
) > 15.0 )
863 wr
[2] = ui_text_line_width( message
) + 8;
864 wr
[3] = vg_ui
.font
->glyph_height
+ 2;
865 wr
[0] = tag_root
[0]-(wr
[2]/2);
866 wr
[1] = tag_root
[1] - wr
[3] - 8;
868 ui_fill( wr
, ui_opacity( ui_colour(k_ui_bg
), 0.23f
) );
869 ui_text( wr
, message
, 1, k_ui_align_middle_center
, 0 );
873 * Draw full imgui for remote player
875 static void remote_player_nametag( ui_point tag_root
,
876 struct network_player
*player
){
877 vg_ui
.font
= &vg_ui_font_big
;
880 wr
[2] = VG_MAX( ui_text_line_width( player
->username
), 140 ) + 8;
882 wr
[0] = tag_root
[0]-(wr
[2]/2);
883 wr
[1] = tag_root
[1]-(wr
[3]/2);
885 ui_fill( wr
, ui_opacity( ui_colour(k_ui_bg
), 0.23f
) );
886 ui_text( wr
, player
->username
, 1, k_ui_align_middle_center
, 0 );
888 vg_ui
.font
= &vg_ui_font_small
;
892 for( int i
=0; i
<3; i
++ )
893 if( player
->medals
[i
] )
900 f32 w
= (f32
)wr
[2] / (f32
)cols
;
903 for( int i
=0; i
<3; i
++ ){
904 if( player
->medals
[i
] ){
905 ui_rect col
= { wr
[0] + (f32
)cols
*w
, wr
[1] + wr
[3], w
,
906 vg_ui
.font
->glyph_height
};
908 vg_strnull( &str
, buf
, 32 );
909 vg_strcatch( &str
, (char)k_SRglyph_vg_circle
);
910 vg_strcati32( &str
, player
->medals
[i
] );
912 ui_text( col
, buf
, 1, k_ui_align_middle_center
,
913 ui_colour( (enum ui_scheme_colour
[]){
914 k_ui_yellow
, k_ui_gray
, k_ui_orange
}[i
] ) );
922 static void remote_player_world_gui( m4x4f pv
, v3f root_co
,
923 struct network_player
*player
){
925 if( !player_tag_position( pv
, root_co
, tag_root
) )
929 remote_player_nametag( tag_root
, player
);
930 chat_box( tag_root
, player
->chat_time
, player
->chat
);
933 if( netplayers
.chatting
)
934 chat_box( tag_root
, vg
.time_real
, netplayers
.chat_buffer
);
936 chat_box( tag_root
, netplayers
.chat_time
, netplayers
.chat_message
);
940 static void remote_player_gui_info( ui_rect box
,
941 const char *username
,
942 const char *activity
,
943 enum remote_player_gui_type type
,
946 f32 opacity
= in_world
? 0.6f
: 0.3f
;
948 if( type
== k_remote_player_gui_type_you
)
949 ui_fill( box
, ui_opacity( 0xff555555, opacity
) );
951 ui_fill( box
, ui_opacity( 0xff000000, opacity
) );
953 if( type
== k_remote_player_gui_type_friend
)
954 ui_outline( box
, -1, ui_opacity( 0xff00c4f0, opacity
), 0 );
957 ui_split_ratio( box
, k_ui_axis_h
, 0.6666f
, 1, top
, bottom
);
961 if( type
== k_remote_player_gui_type_friend
)
962 fg
= ui_colour( k_ui_yellow
+ (in_world
? k_ui_brighter
: 0) );
964 fg
= ui_colour( in_world
? k_ui_fg
: k_ui_fg
+4 );
966 vg_ui
.font
= &vg_ui_font_big
;
967 ui_text( top
, username
, 1, k_ui_align_middle_center
, fg
);
968 vg_ui
.font
= &vg_ui_font_small
;
970 ui_text( bottom
, activity
, 1, k_ui_align_middle_center
, fg
);
973 void remote_players_imgui_lobby(void)
975 if( network_client
.user_intent
== k_server_intent_online
){
977 (network_client
.state
== k_ESteamNetworkingConnectionState_Connected
)))
983 ui_px y
= 50, width
= 200, height
= 42, gap
= 2,
984 x
= vg
.window_x
- width
;
986 vg_ui
.font
= &vg_ui_font_big
;
987 ui_text( (ui_rect
){ x
, 0, width
, height
},
988 "In World", 1, k_ui_align_middle_center
, 0 );
989 vg_ui
.font
= &vg_ui_font_small
;
992 ui_rect us
= { x
, y
, width
, height
};
993 /* FIXME: your location */
994 remote_player_gui_info( us
, steam_username_at_startup
, "you",
995 k_remote_player_gui_type_you
, 1 );
998 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
999 struct network_player
*player
= &netplayers
.list
[i
];
1000 if( !player
->active
|| player
->isblocked
) continue;
1002 int in_same_world
= player
->active_world
== world_current_instance();
1003 if( !player
->isfriend
&& !in_same_world
)
1006 const char *location
= in_same_world
? "": "another world";
1007 if( player
->region_flags
& k_ent_region_flag_hasname
){
1008 location
= player
->region
;
1011 ui_rect box
= { x
, y
, width
, height
};
1012 remote_player_gui_info( box
, player
->username
, location
,
1013 player
->isfriend
, in_same_world
);
1018 void remote_players_imgui_world( world_instance
*world
, m4x4f pv
,
1019 f32 max_dist
, int geo_cull
)
1021 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1023 for( u32 i
=0; i
<NETWORK_MAX_PLAYERS
; i
++ ){
1024 struct network_player
*player
= &netplayers
.list
[i
];
1025 if( player
->active
){
1027 remote_player_position( i
, co
);
1029 if( !player
->active_world
)
1031 if( !player
->isfriend
&&
1032 (world
-world_static
.instances
== k_world_purpose_hub
)) continue;
1034 /* their in our active subworld */
1035 if( player
->active_world
!= world
){
1036 m4x3_mulv( global_miniworld
.mmdl
, co
, co
);
1037 co
[1] -= 2.0f
; /* HACK lol */
1040 f32 d2
= v3_dist2( co
, localplayer
.rb
.co
);
1042 if( d2
> (max_dist
*max_dist
) )
1045 f32 dist
= sqrtf(d2
);
1046 f32 opacity
= 0.95f
* sqrtf(((max_dist
-dist
)/max_dist
));
1053 v3_sub( co
, skaterift
.cam
.pos
, dir
);
1054 v3_normalize( dir
);
1056 if( ray_world( world
, skaterift
.cam
.pos
, dir
, &hit
,
1057 k_material_flag_ghosts
) ){
1062 player
->opacity
= vg_lerpf( player
->opacity
, opacity
,
1063 vg
.time_frame_delta
* 2.0f
);
1065 remote_player_world_gui( pv
, co
, player
);
1067 vg_ui
.colour
[3] = player
->opacity
;
1068 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1072 vg_ui
.colour
[3] = 1.0f
;
1074 remote_player_world_gui( pv
, localplayer
.rb
.co
, NULL
);
1075 ui_flush( k_ui_shader_colour
, vg
.window_x
, vg
.window_y
);
1078 static void chat_escape(void){
1079 netplayers
.chatting
= -1;
1082 static void chat_enter( char *buf
, u32 len
){
1083 vg_strncpy( buf
, netplayers
.chat_message
, NETWORK_MAX_CHAT
,
1084 k_strncpy_always_add_null
);
1085 netplayers
.chatting
= -1;
1086 netplayers
.chat_time
= vg
.time_real
;
1087 chat_send_message( buf
);
1090 void remote_players_chat_imgui(void)
1092 if( netplayers
.chatting
== 1 ){
1093 ui_rect box
= { 0, 0, 400, 40 },
1094 window
= { 0, 0, vg
.window_x
, vg
.window_y
};
1095 ui_rect_center( window
, box
);
1097 struct ui_textbox_callbacks callbacks
= {
1098 .enter
= chat_enter
,
1099 .escape
= chat_escape
1102 ui_textbox( box
, NULL
,
1103 netplayers
.chat_buffer
, NETWORK_MAX_CHAT
, 1,
1104 UI_TEXTBOX_AUTOFOCUS
, &callbacks
);
1107 if( netplayers
.chatting
== -1 ){
1108 netplayers
.chatting
= 0;
1109 srinput
.state
= k_input_state_resume
;
1112 if( (skaterift
.activity
== k_skaterift_default
) &&
1113 button_down( k_srbind_chat
) ){
1114 netplayers
.chatting
= 1;
1115 netplayers
.chat_buffer
[0] = '\0';
1116 srinput
.state
= k_input_state_pause
;