update overlay to have ps controls too
[carveJwlIkooP6JGAAIwe30JlM.git] / player_remote.c
1 #include "player_remote.h"
2 #include "skeleton.h"
3 #include "player_render.h"
4 #include "player_api.h"
5 #include "network_common.h"
6 #include "addon.h"
7 #include "font.h"
8 #include "gui.h"
9 #include "ent_miniworld.h"
10 #include "ent_region.h"
11 #include "shaders/model_entity.h"
12 #include "vg/vg_steam_friends.h"
13
14 struct global_netplayers netplayers;
15
16 static i32 k_show_own_name = 0;
17
18 static void player_remote_clear( struct network_player *player )
19 {
20 addon_cache_unwatch( k_addon_type_player, player->playermodel_view_slot );
21 addon_cache_unwatch( k_addon_type_board, player->board_view_slot );
22
23 memset( player, 0, sizeof(*player) );
24 strcpy( player->username, "unknown" );
25 player->subsystem = k_player_subsystem_invalid;
26 }
27
28 /*
29 * re-attatches addon_reg pointers on the remote client if we have the same
30 * world loaded.
31 */
32 static void relink_remote_player_worlds( u32 client_id ){
33 struct network_player *player = &netplayers.list[client_id];
34
35 addon_alias q[2];
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] );
38
39 /*
40 * currently in 10.23, the hub world will always be the same.
41 * this might but probably wont change in the future
42 */
43 for( u32 i=0; i<k_world_max; i++ ){
44 addon_reg *reg = world_static.instance_addons[ i ];
45
46 player->world_match[i] = 0;
47
48 if( reg ){
49 if( addon_alias_eq( &q[i], &world_static.instance_addons[i]->alias ) )
50 player->world_match[i] = 1;
51 }
52 }
53 }
54
55 /*
56 * re-attatches addon_reg pointers on the remote client if we have the mod
57 * installed locally.
58 *
59 * Run if local worlds change
60 */
61 void relink_all_remote_player_worlds(void)
62 {
63 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
64 struct network_player *player = &netplayers.list[i];
65 if( player->active )
66 relink_remote_player_worlds(i);
67 }
68 }
69
70 void player_remote_update_friendflags( struct network_player *remote )
71 {
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 );
77 }
78
79 void player_remote_rx_200_300( SteamNetworkingMessage_t *msg )
80 {
81 netmsg_blank *tmp = msg->m_pData;
82
83 if( tmp->inetmsg_id == k_inetmsg_playerjoin ){
84 netmsg_playerjoin *playerjoin = msg->m_pData;
85 if( !packet_minsize( msg, sizeof(*playerjoin) )) return;
86
87 if( playerjoin->index < vg_list_size(netplayers.list) ){
88 struct network_player *player = &netplayers.list[ playerjoin->index ];
89 player_remote_clear( player );
90 player->active = 1;
91 player->steamid = playerjoin->steamid;
92 player_remote_update_friendflags( player );
93
94 /* TODO: interpret the uids */
95 player->board_view_slot = 0;
96 player->playermodel_view_slot = 0;
97
98 struct interp_buffer *buf = &netplayers.interp_data[playerjoin->index];
99 buf->t = -99999999.9;
100 for( u32 i=0; i<vg_list_size(buf->frames); i ++ ){
101 buf->frames[i].active = 0;
102 }
103
104 vg_info( "#%u joined friend: %d, blocked: %d\n",
105 playerjoin->index, player->isfriend, player->isblocked );
106 }
107 else {
108 vg_error( "inetmsg_playerjoin: player index out of range\n" );
109 }
110 }
111 else if( tmp->inetmsg_id == k_inetmsg_playerleave ){
112 netmsg_playerleave *playerleave = msg->m_pData;
113 if( !packet_minsize( msg, sizeof(*playerleave) )) return;
114
115 if( playerleave->index < vg_list_size(netplayers.list) ){
116 struct network_player *player = &netplayers.list[ playerleave->index ];
117 player_remote_clear( player );
118 player->active = 0;
119 vg_info( "player leave (%d)\n", playerleave->index );
120 }
121 else {
122 vg_error( "inetmsg_playerleave: player index out of range\n" );
123 }
124 }
125 else if( tmp->inetmsg_id == k_inetmsg_playerusername ){
126 netmsg_playerusername *update = msg->m_pData;
127 if( !packet_minsize( msg, sizeof(*update) )) return;
128
129 if( update->index < vg_list_size(netplayers.list) ){
130 struct network_player *player = &netplayers.list[ update->index ];
131
132 network_msgstring( update->name, msg->m_cbSize, sizeof(*update),
133 player->username, sizeof(player->username) );
134
135 vg_info( "#%u changed username to: %s\n",
136 update->index, player->username );
137 }
138 else {
139 vg_error( "inetmsg_playerleave: player index out of range\n" );
140 }
141 }
142 else if( tmp->inetmsg_id == k_inetmsg_playerframe ){
143 u32 datasize = msg->m_cbSize - sizeof(netmsg_playerframe);
144
145 if( datasize > sizeof(union interp_animdata) ){
146 vg_error( "Player frame data exceeds animdata size\n" );
147 return;
148 }
149
150 netmsg_playerframe *frame = msg->m_pData;
151
152 if( frame->client >= vg_list_size(netplayers.list) ){
153 vg_error( "inetmsg_playerframe: player index out of range\n" );
154 return;
155 }
156
157 if( frame->subsystem >= k_player_subsystem_max ){
158 vg_error( "inetmsg_playerframe: subsystem out of range\n" );
159 return;
160 }
161
162 struct interp_buffer *ib = &netplayers.interp_data[ frame->client ];
163 struct interp_frame *dest = NULL;
164
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];
168
169 if( !ifr->active ){
170 dest = ifr;
171 break;
172 }
173
174 if( ifr->timestamp < min_time ){
175 min_time = ifr->timestamp;
176 dest = ifr;
177 }
178 }
179
180 dest->active = 1;
181 dest->subsystem = frame->subsystem;
182 dest->flags = frame->flags;
183
184 bitpack_ctx ctx = {
185 .mode = k_bitpack_decompress,
186 .buffer = frame->animdata,
187 .buffer_len = datasize,
188 .bytes = 0,
189 };
190
191 /* animation
192 * -------------------------------------------------------------*/
193
194 dest->timestamp = frame->timestamp;
195 dest->boundary_hash = frame->boundary_hash;
196
197 struct network_player *player = &netplayers.list[ frame->client ];
198 struct player_subsystem_interface *sys =
199 player_subsystems[ frame->subsystem ];
200
201 memset( &dest->data, 0, sys->animator_size );
202 if( sys->network_animator_exchange )
203 sys->network_animator_exchange( &ctx, &dest->data );
204 else
205 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
206
207 /* sfx
208 * -------------------------------------------------------------*/
209
210 for( u32 i=0; i<frame->sound_effects; i ++ ){
211 struct net_sfx sfx;
212 net_sfx_exchange( &ctx, &sfx );
213
214 f64 t = (frame->timestamp - NETWORK_FRAMERATE) +
215 (sfx.subframe*NETWORK_FRAMERATE);
216
217 f32 remaining = t - ib->t;
218
219 if( remaining <= 0.0f )
220 net_sfx_play( &sfx );
221 else{
222 struct net_sfx *dst = NULL;
223
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 ){
227 dst = sj;
228 break;
229 }
230
231 if( sj->priority < sfx.priority )
232 dst = sj;
233 }
234
235 *dst = sfx;
236 dst->subframe = remaining;
237 }
238 }
239
240 /* glider
241 * -------------------------------------------------------------*/
242
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 );
247 }
248
249 player->subsystem = frame->subsystem;
250 player->down_bytes += msg->m_cbSize;
251 }
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;
255
256 if( item->client >= vg_list_size(netplayers.list) ){
257 vg_error( "inetmsg_playerframe: player index out of range\n" );
258 return;
259 }
260
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 );
264 return;
265 }
266
267 vg_info( "Client #%d equiped: [%s] %s\n",
268 item->client,
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 );
274
275 struct network_player *player = &netplayers.list[ item->client ];
276 char *uid = player->items[ item->type_index ];
277
278 network_msgstring( item->uid, msg->m_cbSize, sizeof(*item),
279 uid, ADDON_UID_MAX );
280
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 );
285 }
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 );
291 }
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 );
295 }
296 }
297 else if( tmp->inetmsg_id == k_inetmsg_chat ){
298 netmsg_chat *chat = msg->m_pData;
299
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 );
305 }
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 ];
309
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;
314
315 if( l )
316 player->region_flags |= k_ent_region_flag_hasname;
317
318 player->effect_data.spark.colour = region_spark_colour(region->flags);
319 }
320 }
321
322 /*
323 * Write localplayer pose to network
324 */
325 void remote_player_send_playerframe(void)
326 {
327 u8 sysid = localplayer.subsystem;
328 if( sysid >= k_player_subsystem_max ) return;
329
330 struct player_subsystem_interface *sys = player_subsystems[sysid];
331
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;
336
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;
342
343 bitpack_ctx ctx = {
344 .mode = k_bitpack_compress,
345 .buffer = frame->animdata,
346 .buffer_len = max_buf_size,
347 .bytes = 0
348 };
349
350 /* animation
351 * -----------------------------------------------*/
352
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 );
357 else
358 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
359
360 /* sfx
361 * ---------------------------------------------*/
362
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] );
366
367 /* glider
368 * -------------------------------------------------------------*/
369
370 if( localplayer.have_glider ||
371 (localplayer.subsystem == k_player_subsystem_glide) ) {
372 frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
373 }
374
375 if( localplayer.glider_orphan )
376 frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
377
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 );
382 }
383
384 /* ------- */
385
386 u32 wire_size = base_size + ctx.bytes;
387 netplayers.up_bytes += wire_size;
388
389 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
390 hSteamNetworkingSockets, network_client.remote,
391 frame, wire_size,
392 k_nSteamNetworkingSend_Unreliable, NULL );
393 }
394 }
395
396 /*
397 * Updates network traffic stats
398 */
399 void remote_player_debug_update(void)
400 {
401 if( (vg.time_real - netplayers.last_data_measurement) > 1.0 ){
402 netplayers.last_data_measurement = vg.time_real;
403 u32 total_down = 0;
404
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;
411 }
412 }
413
414 netplayers.down_kbs = ((f32)total_down)/1024.0f;
415 netplayers.up_kbs = ((f32)netplayers.up_bytes)/1024.0f;
416 netplayers.up_bytes = 0;
417 }
418 }
419
420 /*
421 * Debugging information
422 */
423 void remote_player_network_imgui( m4x4f pv )
424 {
425 if( network_client.user_intent == k_server_intent_online ){
426 if( !(steam_ready &&
427 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
428 {
429 char buf[128];
430 vg_str str;
431 vg_strnull( &str, buf, sizeof(buf) );
432 u32 fg = 0;
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 );
436 }
437 }
438
439 if( !network_client.network_info )
440 return;
441
442 ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 };
443 ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
444
445 char buf[512];
446 const char *netstatus = "PROGRAMMING ERROR";
447
448 struct { enum ESteamNetworkingConnectionState state; const char *str; }
449 states[] = {
450 { k_ESteamNetworkingConnectionState_None, "None" },
451 { k_ESteamNetworkingConnectionState_Connecting,
452 (const char *[]){"Connecting -",
453 "Connecting /",
454 "Connecting |",
455 "Connecting \\",
456 }[(u32)(vg.time_real/0.25) & 0x3 ] },
457 { k_ESteamNetworkingConnectionState_FindingRoute, "Finding Route" },
458 { k_ESteamNetworkingConnectionState_Connected, "Connected" },
459 { k_ESteamNetworkingConnectionState_ClosedByPeer, "Closed by peer" },
460 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally,
461 "Problem Detected Locally" },
462 { k_ESteamNetworkingConnectionState_FinWait, "Fin Wait" },
463 { k_ESteamNetworkingConnectionState_Linger, "Linger" },
464 { k_ESteamNetworkingConnectionState_Dead, "Dead" }
465 };
466 for( u32 i=0; i<vg_list_size(states); i ++ ){
467 if( states[i].state == network_client.state ){
468 netstatus = states[i].str;
469 break;
470 }
471 }
472 snprintf( buf, 512, "Network: %s", netstatus );
473 ui_info( panel, buf );
474 ui_info( panel, "---------------------" );
475
476 if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
477 ui_info( panel, "#-1: localplayer" );
478
479 snprintf( buf, 512, "U%.3f/D%.3fkbs",
480 netplayers.up_kbs, netplayers.down_kbs );
481 ui_info( panel, buf );
482
483 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
484 struct network_player *player = &netplayers.list[i];
485 if( player->active ){
486 const char *sysname = "invalid";
487
488 if( player->subsystem < k_player_subsystem_max ){
489 sysname = player_subsystems[ player->subsystem ]->name;
490 }
491 snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs",
492 i, player->username, sysname, player->down_kbs );
493 ui_info( panel, buf );
494 }
495 }
496 }
497 else {
498 ui_info( panel, "offline" );
499 }
500 }
501
502 static void remote_player_effect( struct network_player *player,
503 player_pose *final_pose ){
504 /* effects */
505 }
506
507 /*
508 * write the remote players final_mtx
509 */
510 static void pose_remote_player( u32 index,
511 struct interp_frame *f0,
512 struct interp_frame *f1 ){
513
514 struct network_player *player = &netplayers.list[ index ];
515
516 struct interp_buffer *buf = &netplayers.interp_data[ index ];
517 struct skeleton *sk = &localplayer.skeleton;
518 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
519 struct player_board_pose *board_pose = &netplayers.board_poses[index];
520
521 struct player_subsystem_interface *sys0 = player_subsystems[f0->subsystem],
522 *sys1 = NULL;
523
524 struct player_board *board =
525 addon_cache_item_if_loaded( k_addon_type_board, player->board_view_slot );
526
527 player_pose pose0, pose1, posed;
528 sys0->pose( &f0->data, &pose0 );
529
530 u8 instance_id = 0;
531
532 f32 t = 0.0f;
533
534 if( f1 ){
535 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
536 t = vg_clampf( t, 0.0f, 1.0f );
537
538 sys1 = player_subsystems[f1->subsystem];
539 sys1->pose( &f1->data, &pose1 );
540
541 u16 bounds = f0->boundary_hash^f1->boundary_hash;
542
543 if( bounds & NETMSG_BOUNDARY_BIT )
544 t = 1.0f;
545
546 if( bounds & NETMSG_GATE_BOUNDARY_BIT ){
547 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
548 t = 1.0f;
549 }
550
551 instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
552 lerp_player_pose( &pose0, &pose1, t, &posed );
553 effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
554
555 apply_full_skeleton_pose( sk, &posed, final_mtx );
556
557 if( t < 0.5f ){
558 if( sys0->effects )
559 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
560 }
561 else{
562 if( sys1->effects )
563 sys1->effects( &f1->data, final_mtx, board, &player->effect_data );
564 }
565
566 memcpy( board_pose, &posed.board, sizeof(*board_pose) );
567 }
568 else {
569 instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
570 effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
571 apply_full_skeleton_pose( sk, &pose0, final_mtx );
572 if( sys0->effects )
573 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
574 memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
575 }
576
577 if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
578 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
579 player->render_glider = 1;
580
581 v3f co;
582 v4f q;
583 f32 s;
584
585 if( f1 ){
586 v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
587 q_nlerp( f0->data_glider.root_q, f1->data_glider.root_q, t, q );
588 s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
589 }
590 else {
591 v3_copy( f0->data_glider.root_co, co );
592 v4_copy( f0->data_glider.root_q, q );
593 s = f0->data_glider.s;
594 }
595
596 v3f *mtx = netplayers.glider_mtx[ index ];
597 q_m3x3( q, mtx );
598 m3x3_scalef( mtx, s );
599 v3_copy( co, mtx[3] );
600 }
601 else
602 player->render_glider = 0;
603
604 if( player->world_match[ instance_id ] )
605 player->active_world = &world_static.instances[ instance_id ];
606 }
607
608 /*
609 * animate remote player and store in final_mtx
610 */
611 void animate_remote_player( u32 index )
612 {
613 /*
614 * Trys to keep the cursor inside the buffer
615 */
616 f64 min_time = -999999999.9,
617 max_time = 999999999.9,
618 abs_max_time = -999999999.9;
619
620 struct interp_frame *minframe = NULL,
621 *maxframe = NULL,
622 *abs_max_frame = NULL;
623
624 struct interp_buffer *buf = &netplayers.interp_data[index];
625 for( u32 i=0; i<vg_list_size(buf->frames); i ++ ){
626 struct interp_frame *ifr = &buf->frames[i];
627
628 if( ifr->active ){
629 if( (ifr->timestamp > min_time) && (ifr->timestamp < buf->t) ){
630 min_time = ifr->timestamp;
631 minframe = ifr;
632 }
633
634 if( (ifr->timestamp < max_time) && (ifr->timestamp > buf->t) ){
635 max_time = ifr->timestamp;
636 maxframe = ifr;
637 }
638
639 if( ifr->timestamp > abs_max_time ){
640 abs_max_time = ifr->timestamp;
641 abs_max_frame = ifr;
642 }
643 }
644 }
645
646 struct network_player *player = &netplayers.list[ index ];
647 if( player->active != 2 )
648 player->active_world = NULL;
649
650 if( minframe && maxframe ){
651 pose_remote_player( index, minframe, maxframe );
652 buf->t += vg.time_frame_delta;
653 }
654 else {
655 buf->t = abs_max_time - 0.25;
656
657 if( abs_max_frame )
658 pose_remote_player( index, abs_max_frame, NULL );
659 else
660 return;
661 }
662 }
663
664 /*
665 * Update full final_mtx for all remote players
666 */
667 void animate_remote_players(void)
668 {
669 for( u32 i=0; i<vg_list_size(netplayers.list); i ++ ){
670 struct network_player *player = &netplayers.list[i];
671 if( !player->active ) continue;
672
673 animate_remote_player( i );
674 }
675 }
676
677 /*
678 * Draw remote players
679 */
680 void render_remote_players( world_instance *world, vg_camera *cam )
681 {
682 u32 draw_list[ NETWORK_MAX_PLAYERS ],
683 draw_list_count = 0,
684 gliders = 0;
685
686 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
687 struct network_player *player = &netplayers.list[i];
688 if( !player->active || player->isblocked ) continue;
689 if( player->active_world != world ) continue;
690
691 #if 0
692 if( !player->isfriend &&
693 (world-world_static.instances == k_world_purpose_hub)) continue;
694 #endif
695
696 draw_list[draw_list_count ++] = i;
697
698 if( player->render_glider )
699 gliders ++;
700 }
701
702 struct skeleton *sk = &localplayer.skeleton;
703
704 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
705
706 for( u32 j=0; j<draw_list_count; j ++ ){
707 u32 index = draw_list[j];
708
709 struct network_player *player = &netplayers.list[index];
710 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
711
712 struct player_model *model =
713 addon_cache_item_if_loaded( k_addon_type_player,
714 player->playermodel_view_slot );
715
716 if( !model ) model = &localplayer.fallback_model;
717 render_playermodel( cam, world, 0, model, sk, final_mtx );
718
719 struct player_board *board =
720 addon_cache_item_if_loaded( k_addon_type_board,
721 player->board_view_slot );
722 render_board( cam, world, board, final_mtx[localplayer.id_board],
723 &netplayers.board_poses[ index ], k_board_shader_player );
724 }
725
726 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
727
728 if( !gliders )
729 return;
730
731 /* TODO: we really, really only need to do all this once. at some point
732 * PLEASE figure out a good place to do this once per frame!
733 */
734
735 shader_model_entity_use();
736 shader_model_entity_uTexMain( 0 );
737 shader_model_entity_uCamera( cam->transform[3] );
738 shader_model_entity_uPv( cam->mtx.pv );
739
740 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
741
742 for( u32 j=0; j<draw_list_count; j ++ ){
743 u32 index = draw_list[j];
744
745 struct network_player *player = &netplayers.list[index];
746 if( !player->render_glider ) continue;
747
748 if( player->render_glider ){
749 v3f *glider_mtx = netplayers.glider_mtx[ index ];
750 render_glider_model( cam, world, glider_mtx, k_board_shader_entity );
751 }
752 }
753 }
754
755 static int remote_players_randomize( int argc, const char *argv[] ){
756 for( int i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
757 struct network_player *player = &netplayers.list[i];
758
759 player->active = (vg_randu32(&vg.rand) & 0x1)? 2: 0;
760 player->isfriend = vg_randu32(&vg.rand) & vg_randu32(&vg.rand) & 0x1;
761 player->isblocked = vg_randu32(&vg.rand) &
762 vg_randu32(&vg.rand) &
763 vg_randu32(&vg.rand) & 0x1;
764 player->world_match[ 0 ] = vg_randu32(&vg.rand) & 0x1;
765 player->world_match[ 1 ] = 0;
766
767 if( player->world_match[0] )
768 player->active_world = &world_static.instances[0];
769 else
770 player->active_world = NULL;
771
772 for( int i=0; i<sizeof(player->username)-1; i ++ ){
773 player->username[i] = 'a' + (vg_randu32(&vg.rand) % 30);
774 player->username[i+1] = '\0';
775
776 if( (vg_randu32(&vg.rand) % 8) == 3 )
777 break;
778 }
779
780 for( int i=0; i<3; i ++ ){
781 player->medals[i] = vg_randu32(&vg.rand) % 3;
782 }
783
784 v3f pos;
785
786 vg_rand_sphere( &vg.rand, pos );
787 v3_muladds( localplayer.rb.co, pos, 100.0f,
788 netplayers.final_mtx[ i*localplayer.skeleton.bone_count][3] );
789 }
790
791 return 0;
792 }
793
794 void remote_players_init(void)
795 {
796 vg_console_reg_cmd( "add_test_players", remote_players_randomize, NULL );
797 vg_console_reg_var( "k_show_own_name", &k_show_own_name,
798 k_var_dtype_i32, 0 );
799 for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
800 netplayers.sfx_queue[i].system = k_player_subsystem_invalid;
801 }
802 }
803
804 void remote_sfx_pre_update(void)
805 {
806 for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
807 struct net_sfx *si = &netplayers.sfx_queue[i];
808
809 if( si->system != k_player_subsystem_invalid ){
810 si->subframe -= vg.time_frame_delta;
811 if( si->subframe <= 0.0f ){
812 net_sfx_play( si );
813 si->system = k_player_subsystem_invalid;
814 }
815 }
816 }
817 }
818
819 /*
820 * animator->root_co of remote player
821 */
822 static void remote_player_position( int id, v3f out_co ){
823 struct skeleton *sk = &localplayer.skeleton;
824 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*id ];
825 v3_copy( final_mtx[0][3], out_co );
826 }
827
828 enum remote_player_gui_type {
829 k_remote_player_gui_type_stranger,
830 k_remote_player_gui_type_friend,
831 k_remote_player_gui_type_you,
832 };
833
834 /*
835 * Given players' root_co, get the screen point where we should draw tag info.
836 */
837 static int player_tag_position( m4x4f pv, v3f root_co, ui_point out_point ){
838 v4f wpos;
839 v3_copy( root_co, wpos );
840 wpos[1] += 2.0f;
841 wpos[3] = 1.0f;
842
843 m4x4_mulv( pv, wpos, wpos );
844
845 if( wpos[3] > 0.0f ){
846 v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
847 v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
848
849 float k_max = 32000.0f;
850
851 out_point[0] = vg_clampf(wpos[0] * vg.window_x, -k_max, k_max );
852 out_point[1] = vg_clampf((1.0f-wpos[1]) * vg.window_y, -k_max, k_max );
853 return 1;
854 }
855 else
856 return 0;
857 }
858
859 /*
860 * Draw chat box relative to the root tag position on the screen
861 */
862 static void chat_box( ui_point tag_root, f64 time, const char *message ){
863 if( (vg.time_real - time) > 15.0 )
864 return;
865
866 ui_rect wr;
867 wr[2] = ui_text_line_width( message ) + 8;
868 wr[3] = vg_ui.font->glyph_height + 2;
869 wr[0] = tag_root[0]-(wr[2]/2);
870 wr[1] = tag_root[1] - wr[3] - 8;
871
872 ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
873 ui_text( wr, message, 1, k_ui_align_middle_center, 0 );
874 }
875
876 /*
877 * Draw full imgui for remote player
878 */
879 static void remote_player_nametag( ui_point tag_root,
880 struct network_player *player ){
881 vg_ui.font = &vg_ui_font_big;
882
883 ui_rect wr;
884 wr[2] = VG_MAX( ui_text_line_width( player->username ), 140 ) + 8;
885 wr[3] = 32;
886 wr[0] = tag_root[0]-(wr[2]/2);
887 wr[1] = tag_root[1]-(wr[3]/2);
888
889 ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
890 ui_text( wr, player->username, 1, k_ui_align_middle_center, 0 );
891
892 vg_ui.font = &vg_ui_font_small;
893
894 /* medals */
895 int cols = 0;
896 for( int i=0; i<3; i ++ )
897 if( player->medals[i] )
898 cols ++;
899
900 char buf[32];
901 vg_str str;
902
903 if( cols ){
904 f32 w = (f32)wr[2] / (f32)cols;
905 cols = 0;
906
907 for( int i=0; i<3; i ++ ){
908 if( player->medals[i] ){
909 ui_rect col = { wr[0] + (f32)cols*w, wr[1] + wr[3], w,
910 vg_ui.font->glyph_height };
911
912 vg_strnull( &str, buf, 32 );
913 vg_strcatch( &str, (char)k_SRglyph_vg_circle );
914 vg_strcati32( &str, player->medals[i] );
915
916 ui_text( col, buf, 1, k_ui_align_middle_center,
917 ui_colour( (enum ui_scheme_colour[]){
918 k_ui_yellow, k_ui_gray, k_ui_orange }[i] ) );
919
920 cols ++;
921 }
922 }
923 }
924 }
925
926 static void remote_player_world_gui( m4x4f pv, v3f root_co,
927 struct network_player *player ){
928 ui_point tag_root;
929 if( !player_tag_position( pv, root_co, tag_root ) )
930 return;
931
932 if( player ){
933 remote_player_nametag( tag_root, player );
934 chat_box( tag_root, player->chat_time, player->chat );
935 }
936 else {
937 if( netplayers.chatting )
938 chat_box( tag_root, vg.time_real, netplayers.chat_buffer );
939 else
940 chat_box( tag_root, netplayers.chat_time, netplayers.chat_message );
941 }
942 }
943
944 static void remote_player_gui_info( ui_rect box,
945 const char *username,
946 const char *activity,
947 enum remote_player_gui_type type,
948 int in_world ){
949
950 f32 opacity = in_world? 0.6f: 0.3f;
951
952 if( type == k_remote_player_gui_type_you )
953 ui_fill( box, ui_opacity( 0xff555555, opacity ) );
954 else
955 ui_fill( box, ui_opacity( 0xff000000, opacity ) );
956
957 if( type == k_remote_player_gui_type_friend )
958 ui_outline( box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
959
960 ui_rect top, bottom;
961 ui_split_ratio( box, k_ui_axis_h, 0.6666f, 1, top, bottom );
962
963 u32 fg;
964
965 if( type == k_remote_player_gui_type_friend )
966 fg = ui_colour( k_ui_yellow + (in_world? k_ui_brighter: 0) );
967 else
968 fg = ui_colour( in_world? k_ui_fg: k_ui_fg+4 );
969
970 vg_ui.font = &vg_ui_font_big;
971 ui_text( top, username, 1, k_ui_align_middle_center, fg );
972 vg_ui.font = &vg_ui_font_small;
973
974 ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
975 }
976
977 void remote_players_imgui_lobby(void)
978 {
979 if( network_client.user_intent == k_server_intent_online ){
980 if( !(steam_ready &&
981 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
982 {
983 return;
984 }
985 }
986
987 ui_px y = 50, width = 200, height = 42, gap = 2,
988 x = vg.window_x - width;
989
990 vg_ui.font = &vg_ui_font_big;
991 ui_text( (ui_rect){ x, 0, width, height },
992 "In World", 1, k_ui_align_middle_center, 0 );
993 vg_ui.font = &vg_ui_font_small;
994
995
996 ui_rect us = { x, y, width, height };
997 /* FIXME: your location */
998 remote_player_gui_info( us, steam_username_at_startup, "you",
999 k_remote_player_gui_type_you, 1 );
1000 y += height + gap;
1001
1002 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
1003 struct network_player *player = &netplayers.list[i];
1004 if( !player->active || player->isblocked ) continue;
1005
1006 int in_same_world = player->active_world == world_current_instance();
1007 if( !player->isfriend && !in_same_world )
1008 continue;
1009
1010 const char *location = in_same_world? "": "another world";
1011 if( player->region_flags & k_ent_region_flag_hasname ){
1012 location = player->region;
1013 }
1014
1015 ui_rect box = { x, y, width, height };
1016 remote_player_gui_info( box, player->username, location,
1017 player->isfriend, in_same_world );
1018 y += height + gap;
1019 }
1020 }
1021
1022 void remote_players_imgui_world( world_instance *world, m4x4f pv,
1023 f32 max_dist, int geo_cull )
1024 {
1025 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1026
1027 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
1028 struct network_player *player = &netplayers.list[i];
1029 if( player->active ){
1030 v3f co;
1031 remote_player_position( i, co );
1032
1033 if( !player->active_world )
1034 continue;
1035 if( !player->isfriend &&
1036 (world-world_static.instances == k_world_purpose_hub)) continue;
1037
1038 /* their in our active subworld */
1039 if( player->active_world != world ){
1040 m4x3_mulv( global_miniworld.mmdl, co, co );
1041 co[1] -= 2.0f; /* HACK lol */
1042 }
1043
1044 f32 d2 = v3_dist2( co, localplayer.rb.co );
1045
1046 if( d2 > (max_dist*max_dist) )
1047 continue;
1048
1049 f32 dist = sqrtf(d2);
1050 f32 opacity = 0.95f * sqrtf(((max_dist-dist)/max_dist));
1051
1052 if( geo_cull ){
1053 ray_hit hit;
1054 hit.dist = dist;
1055
1056 v3f dir;
1057 v3_sub( co, skaterift.cam.pos, dir );
1058 v3_normalize( dir );
1059
1060 if( ray_world( world, skaterift.cam.pos, dir, &hit,
1061 k_material_flag_ghosts ) ){
1062 opacity *= 0.5f;
1063 }
1064 }
1065
1066 player->opacity = vg_lerpf( player->opacity, opacity,
1067 vg.time_frame_delta * 2.0f );
1068
1069 remote_player_world_gui( pv, co, player );
1070
1071 vg_ui.colour[3] = player->opacity;
1072 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1073 }
1074 }
1075
1076 vg_ui.colour[3] = 1.0f;
1077
1078 remote_player_world_gui( pv, localplayer.rb.co, NULL );
1079 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1080 }
1081
1082 static void chat_escape(void){
1083 netplayers.chatting = -1;
1084 }
1085
1086 static void chat_enter( char *buf, u32 len ){
1087 vg_strncpy( buf, netplayers.chat_message, NETWORK_MAX_CHAT,
1088 k_strncpy_always_add_null );
1089 netplayers.chatting = -1;
1090 netplayers.chat_time = vg.time_real;
1091 chat_send_message( buf );
1092 }
1093
1094 void remote_players_chat_imgui(void)
1095 {
1096 if( netplayers.chatting == 1 ){
1097 ui_rect box = { 0, 0, 400, 40 },
1098 window = { 0, 0, vg.window_x, vg.window_y };
1099 ui_rect_center( window, box );
1100
1101 struct ui_textbox_callbacks callbacks = {
1102 .enter = chat_enter,
1103 .escape = chat_escape
1104 };
1105
1106 ui_textbox( box, NULL,
1107 netplayers.chat_buffer, NETWORK_MAX_CHAT, 1,
1108 UI_TEXTBOX_AUTOFOCUS, &callbacks );
1109 }
1110 else {
1111 if( netplayers.chatting == -1 ){
1112 netplayers.chatting = 0;
1113 srinput.state = k_input_state_resume;
1114 }
1115 else {
1116 if( (skaterift.activity == k_skaterift_default) &&
1117 button_down( k_srbind_chat ) ){
1118 netplayers.chatting = 1;
1119 netplayers.chat_buffer[0] = '\0';
1120 srinput.state = k_input_state_pause;
1121 }
1122 }
1123 }
1124 }