fix regression with gate flipping
[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 if( frame->client >= vg_list_size(netplayers.list) ){
152 vg_error( "inetmsg_playerframe: player index out of range\n" );
153 return;
154 }
155
156 if( frame->subsystem >= k_player_subsystem_max ){
157 vg_error( "inetmsg_playerframe: subsystem out of range\n" );
158 return;
159 }
160
161 struct interp_buffer *ib = &netplayers.interp_data[ frame->client ];
162 struct interp_frame *dest = NULL;
163
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];
167
168 if( !ifr->active ){
169 dest = ifr;
170 break;
171 }
172
173 if( ifr->timestamp < min_time ){
174 min_time = ifr->timestamp;
175 dest = ifr;
176 }
177 }
178
179 dest->active = 1;
180 dest->subsystem = frame->subsystem;
181 dest->flags = frame->flags;
182
183 bitpack_ctx ctx = {
184 .mode = k_bitpack_decompress,
185 .buffer = frame->animdata,
186 .buffer_len = datasize,
187 .bytes = 0,
188 };
189
190 /* animation
191 * -------------------------------------------------------------*/
192
193 dest->timestamp = frame->timestamp;
194 dest->boundary_hash = frame->boundary_hash;
195
196 struct network_player *player = &netplayers.list[ frame->client ];
197 struct player_subsystem_interface *sys =
198 player_subsystems[ frame->subsystem ];
199
200 memset( &dest->data, 0, sys->animator_size );
201 if( sys->network_animator_exchange )
202 sys->network_animator_exchange( &ctx, &dest->data );
203 else
204 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
205
206 /* sfx
207 * -------------------------------------------------------------*/
208
209 for( u32 i=0; i<frame->sound_effects; i ++ ){
210 struct net_sfx sfx;
211 net_sfx_exchange( &ctx, &sfx );
212
213 f64 t = (frame->timestamp - NETWORK_FRAMERATE) +
214 (sfx.subframe*NETWORK_FRAMERATE);
215
216 f32 remaining = t - ib->t;
217
218 if( remaining <= 0.0f )
219 net_sfx_play( &sfx );
220 else{
221 struct net_sfx *dst = NULL;
222
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 ){
226 dst = sj;
227 break;
228 }
229
230 if( sj->priority < sfx.priority )
231 dst = sj;
232 }
233
234 *dst = sfx;
235 dst->subframe = remaining;
236 }
237 }
238
239 /* glider
240 * -------------------------------------------------------------*/
241
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 );
246 }
247
248 player->subsystem = frame->subsystem;
249 player->down_bytes += msg->m_cbSize;
250 }
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;
254
255 if( item->client >= vg_list_size(netplayers.list) ){
256 vg_error( "inetmsg_playerframe: player index out of range\n" );
257 return;
258 }
259
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 );
263 return;
264 }
265
266 vg_info( "Client #%d equiped: [%s] %s\n",
267 item->client,
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 );
273
274 struct network_player *player = &netplayers.list[ item->client ];
275 char *uid = player->items[ item->type_index ];
276
277 network_msgstring( item->uid, msg->m_cbSize, sizeof(*item),
278 uid, ADDON_UID_MAX );
279
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 );
284 }
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 );
290 }
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 );
294 }
295 }
296 else if( tmp->inetmsg_id == k_inetmsg_chat ){
297 netmsg_chat *chat = msg->m_pData;
298
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 );
304 }
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 ];
308
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;
313
314 if( l )
315 player->region_flags |= k_ent_region_flag_hasname;
316
317 player->effect_data.spark.colour = region_spark_colour(region->flags);
318 }
319 }
320
321 /*
322 * Write localplayer pose to network
323 */
324 void remote_player_send_playerframe(void)
325 {
326 u8 sysid = localplayer.subsystem;
327 if( sysid >= k_player_subsystem_max ) return;
328
329 struct player_subsystem_interface *sys = player_subsystems[sysid];
330
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;
335
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;
341
342 bitpack_ctx ctx = {
343 .mode = k_bitpack_compress,
344 .buffer = frame->animdata,
345 .buffer_len = max_buf_size,
346 .bytes = 0
347 };
348
349 /* animation
350 * -----------------------------------------------*/
351
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 );
356 else
357 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
358
359 /* sfx
360 * ---------------------------------------------*/
361
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] );
365
366 /* glider
367 * -------------------------------------------------------------*/
368
369 if( localplayer.have_glider ||
370 (localplayer.subsystem == k_player_subsystem_glide) ) {
371 frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
372 }
373
374 if( localplayer.glider_orphan )
375 frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
376
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 );
381 }
382
383 /* ------- */
384
385 u32 wire_size = base_size + ctx.bytes;
386 netplayers.up_bytes += wire_size;
387
388 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
389 hSteamNetworkingSockets, network_client.remote,
390 frame, wire_size,
391 k_nSteamNetworkingSend_Unreliable, NULL );
392 }
393 }
394
395 /*
396 * Updates network traffic stats
397 */
398 void remote_player_debug_update(void)
399 {
400 if( (vg.time_real - netplayers.last_data_measurement) > 1.0 ){
401 netplayers.last_data_measurement = vg.time_real;
402 u32 total_down = 0;
403
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;
410 }
411 }
412
413 netplayers.down_kbs = ((f32)total_down)/1024.0f;
414 netplayers.up_kbs = ((f32)netplayers.up_bytes)/1024.0f;
415 netplayers.up_bytes = 0;
416 }
417 }
418
419 /*
420 * Debugging information
421 */
422 void remote_player_network_imgui( m4x4f pv )
423 {
424 if( network_client.user_intent == k_server_intent_online ){
425 if( !(steam_ready &&
426 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
427 {
428 char buf[128];
429 vg_str str;
430 vg_strnull( &str, buf, sizeof(buf) );
431 u32 fg = 0;
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 );
435 }
436 }
437
438 if( !network_client.network_info )
439 return;
440
441 ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 };
442 ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
443
444 char buf[512];
445 const char *netstatus = "PROGRAMMING ERROR";
446
447 struct { enum ESteamNetworkingConnectionState state; const char *str; }
448 states[] = {
449 { k_ESteamNetworkingConnectionState_None, "None" },
450 { k_ESteamNetworkingConnectionState_Connecting,
451 (const char *[]){"Connecting -",
452 "Connecting /",
453 "Connecting |",
454 "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" }
464 };
465 for( u32 i=0; i<vg_list_size(states); i ++ ){
466 if( states[i].state == network_client.state ){
467 netstatus = states[i].str;
468 break;
469 }
470 }
471 snprintf( buf, 512, "Network: %s", netstatus );
472 ui_info( panel, buf );
473 ui_info( panel, "---------------------" );
474
475 if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
476 ui_info( panel, "#-1: localplayer" );
477
478 snprintf( buf, 512, "U%.3f/D%.3fkbs",
479 netplayers.up_kbs, netplayers.down_kbs );
480 ui_info( panel, buf );
481
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";
486
487 if( player->subsystem < k_player_subsystem_max ){
488 sysname = player_subsystems[ player->subsystem ]->name;
489 }
490 snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs",
491 i, player->username, sysname, player->down_kbs );
492 ui_info( panel, buf );
493 }
494 }
495 }
496 else {
497 ui_info( panel, "offline" );
498 }
499 }
500
501 static void remote_player_effect( struct network_player *player,
502 player_pose *final_pose ){
503 /* effects */
504 }
505
506 /*
507 * write the remote players final_mtx
508 */
509 static void pose_remote_player( u32 index,
510 struct interp_frame *f0,
511 struct interp_frame *f1 ){
512
513 struct network_player *player = &netplayers.list[ index ];
514
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];
519
520 struct player_subsystem_interface *sys0 = player_subsystems[f0->subsystem],
521 *sys1 = NULL;
522
523 struct player_board *board =
524 addon_cache_item_if_loaded( k_addon_type_board, player->board_view_slot );
525
526 player_pose pose0, pose1, posed;
527 sys0->pose( &f0->data, &pose0 );
528
529 u8 instance_id = 0;
530
531 f32 t = 0.0f;
532
533 if( f1 ){
534 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
535 t = vg_clampf( t, 0.0f, 1.0f );
536
537 sys1 = player_subsystems[f1->subsystem];
538 sys1->pose( &f1->data, &pose1 );
539
540 u16 bounds = f0->boundary_hash^f1->boundary_hash;
541
542 if( bounds & NETMSG_BOUNDARY_BIT )
543 t = 1.0f;
544
545 if( bounds & NETMSG_GATE_BOUNDARY_BIT ){
546 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
547 t = 1.0f;
548 }
549
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 );
553
554 apply_full_skeleton_pose( sk, &posed, final_mtx );
555
556 if( t < 0.5f ){
557 if( sys0->effects )
558 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
559 }
560 else{
561 if( sys1->effects )
562 sys1->effects( &f1->data, final_mtx, board, &player->effect_data );
563 }
564
565 memcpy( board_pose, &posed.board, sizeof(*board_pose) );
566 }
567 else {
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 );
571 if( sys0->effects )
572 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
573 memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
574 }
575
576 if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
577 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
578 player->render_glider = 1;
579
580 v3f co;
581 v4f q;
582 f32 s;
583
584 if( f1 ){
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 );
588 }
589 else {
590 v3_copy( f0->data_glider.root_co, co );
591 v4_copy( f0->data_glider.root_q, q );
592 s = f0->data_glider.s;
593 }
594
595 v3f *mtx = netplayers.glider_mtx[ index ];
596 q_m3x3( q, mtx );
597 m3x3_scalef( mtx, s );
598 v3_copy( co, mtx[3] );
599 }
600 else
601 player->render_glider = 0;
602
603 if( player->world_match[ instance_id ] )
604 player->active_world = &world_static.instances[ instance_id ];
605 }
606
607 /*
608 * animate remote player and store in final_mtx
609 */
610 void animate_remote_player( u32 index )
611 {
612 /*
613 * Trys to keep the cursor inside the buffer
614 */
615 f64 min_time = -999999999.9,
616 max_time = 999999999.9,
617 abs_max_time = -999999999.9;
618
619 struct interp_frame *minframe = NULL,
620 *maxframe = NULL,
621 *abs_max_frame = NULL;
622
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];
626
627 if( ifr->active ){
628 if( (ifr->timestamp > min_time) && (ifr->timestamp < buf->t) ){
629 min_time = ifr->timestamp;
630 minframe = ifr;
631 }
632
633 if( (ifr->timestamp < max_time) && (ifr->timestamp > buf->t) ){
634 max_time = ifr->timestamp;
635 maxframe = ifr;
636 }
637
638 if( ifr->timestamp > abs_max_time ){
639 abs_max_time = ifr->timestamp;
640 abs_max_frame = ifr;
641 }
642 }
643 }
644
645 struct network_player *player = &netplayers.list[ index ];
646 if( player->active != 2 )
647 player->active_world = NULL;
648
649 if( minframe && maxframe ){
650 pose_remote_player( index, minframe, maxframe );
651 buf->t += vg.time_frame_delta;
652 }
653 else {
654 buf->t = abs_max_time - 0.25;
655
656 if( abs_max_frame )
657 pose_remote_player( index, abs_max_frame, NULL );
658 else
659 return;
660 }
661 }
662
663 /*
664 * Update full final_mtx for all remote players
665 */
666 void animate_remote_players(void)
667 {
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;
671
672 animate_remote_player( i );
673 }
674 }
675
676 /*
677 * Draw remote players
678 */
679 void render_remote_players( world_instance *world, vg_camera *cam )
680 {
681 u32 draw_list[ NETWORK_MAX_PLAYERS ],
682 draw_list_count = 0,
683 gliders = 0;
684
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;
691
692 draw_list[draw_list_count ++] = i;
693
694 if( player->render_glider )
695 gliders ++;
696 }
697
698 struct skeleton *sk = &localplayer.skeleton;
699
700 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
701
702 for( u32 j=0; j<draw_list_count; j ++ ){
703 u32 index = draw_list[j];
704
705 struct network_player *player = &netplayers.list[index];
706 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
707
708 struct player_model *model =
709 addon_cache_item_if_loaded( k_addon_type_player,
710 player->playermodel_view_slot );
711
712 if( !model ) model = &localplayer.fallback_model;
713 render_playermodel( cam, world, 0, model, sk, final_mtx );
714
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 );
720 }
721
722 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
723
724 if( !gliders )
725 return;
726
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!
729 */
730
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 );
735
736 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
737
738 for( u32 j=0; j<draw_list_count; j ++ ){
739 u32 index = draw_list[j];
740
741 struct network_player *player = &netplayers.list[index];
742 if( !player->render_glider ) continue;
743
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 );
747 }
748 }
749 }
750
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];
754
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;
762
763 if( player->world_match[0] )
764 player->active_world = &world_static.instances[0];
765 else
766 player->active_world = NULL;
767
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';
771
772 if( (vg_randu32(&vg.rand) % 8) == 3 )
773 break;
774 }
775
776 for( int i=0; i<3; i ++ ){
777 player->medals[i] = vg_randu32(&vg.rand) % 3;
778 }
779
780 v3f pos;
781
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] );
785 }
786
787 return 0;
788 }
789
790 void remote_players_init(void)
791 {
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;
797 }
798 }
799
800 void remote_sfx_pre_update(void)
801 {
802 for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
803 struct net_sfx *si = &netplayers.sfx_queue[i];
804
805 if( si->system != k_player_subsystem_invalid ){
806 si->subframe -= vg.time_frame_delta;
807 if( si->subframe <= 0.0f ){
808 net_sfx_play( si );
809 si->system = k_player_subsystem_invalid;
810 }
811 }
812 }
813 }
814
815 /*
816 * animator->root_co of remote player
817 */
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 );
822 }
823
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,
828 };
829
830 /*
831 * Given players' root_co, get the screen point where we should draw tag info.
832 */
833 static int player_tag_position( m4x4f pv, v3f root_co, ui_point out_point ){
834 v4f wpos;
835 v3_copy( root_co, wpos );
836 wpos[1] += 2.0f;
837 wpos[3] = 1.0f;
838
839 m4x4_mulv( pv, wpos, wpos );
840
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 );
844
845 float k_max = 32000.0f;
846
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 );
849 return 1;
850 }
851 else
852 return 0;
853 }
854
855 /*
856 * Draw chat box relative to the root tag position on the screen
857 */
858 static void chat_box( ui_point tag_root, f64 time, const char *message ){
859 if( (vg.time_real - time) > 15.0 )
860 return;
861
862 ui_rect wr;
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;
867
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 );
870 }
871
872 /*
873 * Draw full imgui for remote player
874 */
875 static void remote_player_nametag( ui_point tag_root,
876 struct network_player *player ){
877 vg_ui.font = &vg_ui_font_big;
878
879 ui_rect wr;
880 wr[2] = VG_MAX( ui_text_line_width( player->username ), 140 ) + 8;
881 wr[3] = 32;
882 wr[0] = tag_root[0]-(wr[2]/2);
883 wr[1] = tag_root[1]-(wr[3]/2);
884
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 );
887
888 vg_ui.font = &vg_ui_font_small;
889
890 /* medals */
891 int cols = 0;
892 for( int i=0; i<3; i ++ )
893 if( player->medals[i] )
894 cols ++;
895
896 char buf[32];
897 vg_str str;
898
899 if( cols ){
900 f32 w = (f32)wr[2] / (f32)cols;
901 cols = 0;
902
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 };
907
908 vg_strnull( &str, buf, 32 );
909 vg_strcatch( &str, (char)k_SRglyph_vg_circle );
910 vg_strcati32( &str, player->medals[i] );
911
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] ) );
915
916 cols ++;
917 }
918 }
919 }
920 }
921
922 static void remote_player_world_gui( m4x4f pv, v3f root_co,
923 struct network_player *player ){
924 ui_point tag_root;
925 if( !player_tag_position( pv, root_co, tag_root ) )
926 return;
927
928 if( player ){
929 remote_player_nametag( tag_root, player );
930 chat_box( tag_root, player->chat_time, player->chat );
931 }
932 else {
933 if( netplayers.chatting )
934 chat_box( tag_root, vg.time_real, netplayers.chat_buffer );
935 else
936 chat_box( tag_root, netplayers.chat_time, netplayers.chat_message );
937 }
938 }
939
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,
944 int in_world ){
945
946 f32 opacity = in_world? 0.6f: 0.3f;
947
948 if( type == k_remote_player_gui_type_you )
949 ui_fill( box, ui_opacity( 0xff555555, opacity ) );
950 else
951 ui_fill( box, ui_opacity( 0xff000000, opacity ) );
952
953 if( type == k_remote_player_gui_type_friend )
954 ui_outline( box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
955
956 ui_rect top, bottom;
957 ui_split_ratio( box, k_ui_axis_h, 0.6666f, 1, top, bottom );
958
959 u32 fg;
960
961 if( type == k_remote_player_gui_type_friend )
962 fg = ui_colour( k_ui_yellow + (in_world? k_ui_brighter: 0) );
963 else
964 fg = ui_colour( in_world? k_ui_fg: k_ui_fg+4 );
965
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;
969
970 ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
971 }
972
973 void remote_players_imgui_lobby(void)
974 {
975 if( network_client.user_intent == k_server_intent_online ){
976 if( !(steam_ready &&
977 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
978 {
979 return;
980 }
981 }
982
983 ui_px y = 50, width = 200, height = 42, gap = 2,
984 x = vg.window_x - width;
985
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;
990
991
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 );
996 y += height + gap;
997
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;
1001
1002 int in_same_world = player->active_world == world_current_instance();
1003 if( !player->isfriend && !in_same_world )
1004 continue;
1005
1006 const char *location = in_same_world? "": "another world";
1007 if( player->region_flags & k_ent_region_flag_hasname ){
1008 location = player->region;
1009 }
1010
1011 ui_rect box = { x, y, width, height };
1012 remote_player_gui_info( box, player->username, location,
1013 player->isfriend, in_same_world );
1014 y += height + gap;
1015 }
1016 }
1017
1018 void remote_players_imgui_world( world_instance *world, m4x4f pv,
1019 f32 max_dist, int geo_cull )
1020 {
1021 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1022
1023 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
1024 struct network_player *player = &netplayers.list[i];
1025 if( player->active ){
1026 v3f co;
1027 remote_player_position( i, co );
1028
1029 if( !player->active_world )
1030 continue;
1031 if( !player->isfriend &&
1032 (world-world_static.instances == k_world_purpose_hub)) continue;
1033
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 */
1038 }
1039
1040 f32 d2 = v3_dist2( co, localplayer.rb.co );
1041
1042 if( d2 > (max_dist*max_dist) )
1043 continue;
1044
1045 f32 dist = sqrtf(d2);
1046 f32 opacity = 0.95f * sqrtf(((max_dist-dist)/max_dist));
1047
1048 if( geo_cull ){
1049 ray_hit hit;
1050 hit.dist = dist;
1051
1052 v3f dir;
1053 v3_sub( co, skaterift.cam.pos, dir );
1054 v3_normalize( dir );
1055
1056 if( ray_world( world, skaterift.cam.pos, dir, &hit,
1057 k_material_flag_ghosts ) ){
1058 opacity *= 0.5f;
1059 }
1060 }
1061
1062 player->opacity = vg_lerpf( player->opacity, opacity,
1063 vg.time_frame_delta * 2.0f );
1064
1065 remote_player_world_gui( pv, co, player );
1066
1067 vg_ui.colour[3] = player->opacity;
1068 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1069 }
1070 }
1071
1072 vg_ui.colour[3] = 1.0f;
1073
1074 remote_player_world_gui( pv, localplayer.rb.co, NULL );
1075 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1076 }
1077
1078 static void chat_escape(void){
1079 netplayers.chatting = -1;
1080 }
1081
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 );
1088 }
1089
1090 void remote_players_chat_imgui(void)
1091 {
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 );
1096
1097 struct ui_textbox_callbacks callbacks = {
1098 .enter = chat_enter,
1099 .escape = chat_escape
1100 };
1101
1102 ui_textbox( box, NULL,
1103 netplayers.chat_buffer, NETWORK_MAX_CHAT, 1,
1104 UI_TEXTBOX_AUTOFOCUS, &callbacks );
1105 }
1106 else {
1107 if( netplayers.chatting == -1 ){
1108 netplayers.chatting = 0;
1109 srinput.state = k_input_state_resume;
1110 }
1111 else {
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;
1117 }
1118 }
1119 }
1120 }