b08f5eee6e52ddfb45b56839cd5b8e38f8dbef45
[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 vg_info( "recv %i from %i.\n", (i32)tmp->inetmsg_id, (i32)frame->client );
152
153 if( frame->client >= vg_list_size(netplayers.list) ){
154 vg_error( "inetmsg_playerframe: player index out of range\n" );
155 return;
156 }
157
158 if( frame->subsystem >= k_player_subsystem_max ){
159 vg_error( "inetmsg_playerframe: subsystem out of range\n" );
160 return;
161 }
162 vg_info( "subsystem: %i, sfx: %i.\n", (i32)frame->subsystem,
163 (i32)frame->sound_effects );
164
165 struct interp_buffer *ib = &netplayers.interp_data[ frame->client ];
166 struct interp_frame *dest = NULL;
167
168 f64 min_time = INFINITY;
169 for( u32 i=0; i<vg_list_size(ib->frames); i++ ){
170 struct interp_frame *ifr = &ib->frames[i];
171
172 if( !ifr->active ){
173 dest = ifr;
174 break;
175 }
176
177 if( ifr->timestamp < min_time ){
178 min_time = ifr->timestamp;
179 dest = ifr;
180 }
181 }
182
183 dest->active = 1;
184 dest->subsystem = frame->subsystem;
185 dest->flags = frame->flags;
186
187 bitpack_ctx ctx = {
188 .mode = k_bitpack_decompress,
189 .buffer = frame->animdata,
190 .buffer_len = datasize,
191 .bytes = 0,
192 };
193
194 /* animation
195 * -------------------------------------------------------------*/
196
197 dest->timestamp = frame->timestamp;
198 dest->boundary_hash = frame->boundary_hash;
199
200 struct network_player *player = &netplayers.list[ frame->client ];
201 struct player_subsystem_interface *sys =
202 player_subsystems[ frame->subsystem ];
203
204 memset( &dest->data, 0, sys->animator_size );
205 if( sys->network_animator_exchange )
206 sys->network_animator_exchange( &ctx, &dest->data );
207 else
208 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
209
210 /* sfx
211 * -------------------------------------------------------------*/
212
213 for( u32 i=0; i<frame->sound_effects; i ++ ){
214 struct net_sfx sfx;
215 net_sfx_exchange( &ctx, &sfx );
216
217 f64 t = (frame->timestamp - NETWORK_FRAMERATE) +
218 (sfx.subframe*NETWORK_FRAMERATE);
219
220 f32 remaining = t - ib->t;
221
222 if( remaining <= 0.0f )
223 net_sfx_play( &sfx );
224 else{
225 struct net_sfx *dst = NULL;
226
227 for( u32 j=0; j<NETWORK_SFX_QUEUE_LENGTH; j ++ ){
228 struct net_sfx *sj = &netplayers.sfx_queue[j];
229 if( sj->system == k_player_subsystem_invalid ){
230 dst = sj;
231 break;
232 }
233
234 if( sj->priority < sfx.priority )
235 dst = sj;
236 }
237
238 *dst = sfx;
239 dst->subframe = remaining;
240 }
241 }
242
243 /* glider
244 * -------------------------------------------------------------*/
245
246 memset( &dest->data_glider, 0, sizeof(struct remote_glider_animator) );
247 if( dest->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
248 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
249 player_glide_remote_animator_exchange( &ctx, &dest->data_glider );
250 }
251
252 player->subsystem = frame->subsystem;
253 player->down_bytes += msg->m_cbSize;
254 }
255 else if( tmp->inetmsg_id == k_inetmsg_playeritem ){
256 netmsg_playeritem *item = msg->m_pData;
257 if( !packet_minsize( msg, sizeof(*item)+1 )) return;
258
259 if( item->client >= vg_list_size(netplayers.list) ){
260 vg_error( "inetmsg_playerframe: player index out of range\n" );
261 return;
262 }
263
264 if( item->type_index >= k_netmsg_playeritem_max ){
265 vg_warn( "Client #%d invalid equip type %u\n",
266 (i32)item->client, (u32)item->type_index );
267 return;
268 }
269
270 vg_info( "Client #%d equiped: [%s] %s\n",
271 item->client,
272 (const char *[]){[k_netmsg_playeritem_board]="board",
273 [k_netmsg_playeritem_player]="player",
274 [k_netmsg_playeritem_world0]="world0",
275 [k_netmsg_playeritem_world1]="world1"
276 }[item->type_index], item->uid );
277
278 struct network_player *player = &netplayers.list[ item->client ];
279 char *uid = player->items[ item->type_index ];
280
281 network_msgstring( item->uid, msg->m_cbSize, sizeof(*item),
282 uid, ADDON_UID_MAX );
283
284 if( item->type_index == k_netmsg_playeritem_board ){
285 addon_cache_unwatch( k_addon_type_board, player->board_view_slot );
286 player->board_view_slot =
287 addon_cache_create_viewer_from_uid( k_addon_type_board, uid );
288 }
289 else if( item->type_index == k_netmsg_playeritem_player ){
290 addon_cache_unwatch( k_addon_type_player,
291 player->playermodel_view_slot );
292 player->playermodel_view_slot =
293 addon_cache_create_viewer_from_uid( k_addon_type_player, uid );
294 }
295 else if( (item->type_index == k_netmsg_playeritem_world0) ||
296 (item->type_index == k_netmsg_playeritem_world1) ){
297 relink_remote_player_worlds( item->client );
298 }
299 }
300 else if( tmp->inetmsg_id == k_inetmsg_chat ){
301 netmsg_chat *chat = msg->m_pData;
302
303 struct network_player *player = &netplayers.list[ chat->client ];
304 network_msgstring( chat->msg, msg->m_cbSize, sizeof(netmsg_chat),
305 player->chat, NETWORK_MAX_CHAT );
306 player->chat_time = vg.time_real;
307 vg_info( "[%d]: %s\n", chat->client, player->chat );
308 }
309 else if( tmp->inetmsg_id == k_inetmsg_region ){
310 netmsg_region *region = msg->m_pData;
311 struct network_player *player = &netplayers.list[ region->client ];
312
313 u32 l = network_msgstring(
314 region->loc, msg->m_cbSize, sizeof(netmsg_region),
315 player->region, NETWORK_REGION_MAX );
316 player->region_flags = region->flags;
317
318 if( l )
319 player->region_flags |= k_ent_region_flag_hasname;
320
321 player->effect_data.spark.colour = region_spark_colour(region->flags);
322 }
323 }
324
325 /*
326 * Write localplayer pose to network
327 */
328 void remote_player_send_playerframe(void)
329 {
330 u8 sysid = localplayer.subsystem;
331 if( sysid >= k_player_subsystem_max ) return;
332
333 struct player_subsystem_interface *sys = player_subsystems[sysid];
334
335 if( sys->animator_size ){
336 u32 max_buf_size = sys->animator_size + sizeof(localplayer.sfx_buffer),
337 base_size = sizeof(struct netmsg_playerframe),
338 max_packet = base_size + max_buf_size;
339
340 netmsg_playerframe *frame = alloca( max_packet );
341 frame->inetmsg_id = k_inetmsg_playerframe;
342 frame->client = 0xff;
343 frame->subsystem = localplayer.subsystem;
344 frame->flags = world_static.active_instance;
345
346 bitpack_ctx ctx = {
347 .mode = k_bitpack_compress,
348 .buffer = frame->animdata,
349 .buffer_len = max_buf_size,
350 .bytes = 0
351 };
352
353 /* animation
354 * -----------------------------------------------*/
355
356 frame->timestamp = vg.time_real;
357 frame->boundary_hash = localplayer.boundary_hash;
358 if( sys->network_animator_exchange )
359 sys->network_animator_exchange( &ctx, sys->animator_data );
360 else
361 bitpack_bytes( &ctx, sys->animator_size, sys->animator_data );
362
363 /* sfx
364 * ---------------------------------------------*/
365
366 frame->sound_effects = localplayer.sfx_buffer_count;
367 for( u32 i=0; i<localplayer.sfx_buffer_count; i ++ )
368 net_sfx_exchange( &ctx, &localplayer.sfx_buffer[i] );
369
370 /* glider
371 * -------------------------------------------------------------*/
372
373 if( localplayer.have_glider ||
374 (localplayer.subsystem == k_player_subsystem_glide) ) {
375 frame->flags |= NETMSG_PLAYERFRAME_HAVE_GLIDER;
376 }
377
378 if( localplayer.glider_orphan )
379 frame->flags |= NETMSG_PLAYERFRAME_GLIDER_ORPHAN;
380
381 if( frame->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
382 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
383 player_glide_remote_animator_exchange( &ctx,
384 &player_glide.remote_animator );
385 }
386
387 /* ------- */
388
389 u32 wire_size = base_size + ctx.bytes;
390 netplayers.up_bytes += wire_size;
391
392 SteamAPI_ISteamNetworkingSockets_SendMessageToConnection(
393 hSteamNetworkingSockets, network_client.remote,
394 frame, wire_size,
395 k_nSteamNetworkingSend_Unreliable, NULL );
396 }
397 }
398
399 /*
400 * Updates network traffic stats
401 */
402 void remote_player_debug_update(void)
403 {
404 if( (vg.time_real - netplayers.last_data_measurement) > 1.0 ){
405 netplayers.last_data_measurement = vg.time_real;
406 u32 total_down = 0;
407
408 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
409 struct network_player *player = &netplayers.list[i];
410 if( player->active ){
411 total_down += player->down_bytes;
412 player->down_kbs = ((f32)player->down_bytes)/1024.0f;
413 player->down_bytes = 0;
414 }
415 }
416
417 netplayers.down_kbs = ((f32)total_down)/1024.0f;
418 netplayers.up_kbs = ((f32)netplayers.up_bytes)/1024.0f;
419 netplayers.up_bytes = 0;
420 }
421 }
422
423 /*
424 * Debugging information
425 */
426 void remote_player_network_imgui( m4x4f pv )
427 {
428 if( network_client.user_intent == k_server_intent_online ){
429 if( !(steam_ready &&
430 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
431 {
432 char buf[128];
433 vg_str str;
434 vg_strnull( &str, buf, sizeof(buf) );
435 u32 fg = 0;
436 network_status_string( &str, &fg );
437 ui_text( (ui_rect){ vg.window_x - 200, 0, 200, 48 }, buf, 1,
438 k_ui_align_middle_center, fg );
439 }
440 }
441
442 if( !network_client.network_info )
443 return;
444
445 ui_rect panel = { (vg.window_x / 2) - 200, 0, 400, 600 };
446 ui_fill( panel, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 );
447
448 char buf[512];
449 const char *netstatus = "PROGRAMMING ERROR";
450
451 struct { enum ESteamNetworkingConnectionState state; const char *str; }
452 states[] = {
453 { k_ESteamNetworkingConnectionState_None, "None" },
454 { k_ESteamNetworkingConnectionState_Connecting,
455 (const char *[]){"Connecting -",
456 "Connecting /",
457 "Connecting |",
458 "Connecting \\",
459 }[(u32)(vg.time_real/0.25) & 0x3 ] },
460 { k_ESteamNetworkingConnectionState_FindingRoute, "Finding Route" },
461 { k_ESteamNetworkingConnectionState_Connected, "Connected" },
462 { k_ESteamNetworkingConnectionState_ClosedByPeer, "Closed by peer" },
463 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally,
464 "Problem Detected Locally" },
465 { k_ESteamNetworkingConnectionState_FinWait, "Fin Wait" },
466 { k_ESteamNetworkingConnectionState_Linger, "Linger" },
467 { k_ESteamNetworkingConnectionState_Dead, "Dead" }
468 };
469 for( u32 i=0; i<vg_list_size(states); i ++ ){
470 if( states[i].state == network_client.state ){
471 netstatus = states[i].str;
472 break;
473 }
474 }
475 snprintf( buf, 512, "Network: %s", netstatus );
476 ui_info( panel, buf );
477 ui_info( panel, "---------------------" );
478
479 if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
480 ui_info( panel, "#-1: localplayer" );
481
482 snprintf( buf, 512, "U%.3f/D%.3fkbs",
483 netplayers.up_kbs, netplayers.down_kbs );
484 ui_info( panel, buf );
485
486 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
487 struct network_player *player = &netplayers.list[i];
488 if( player->active ){
489 const char *sysname = "invalid";
490
491 if( player->subsystem < k_player_subsystem_max ){
492 sysname = player_subsystems[ player->subsystem ]->name;
493 }
494 snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs",
495 i, player->username, sysname, player->down_kbs );
496 ui_info( panel, buf );
497 }
498 }
499 }
500 else {
501 ui_info( panel, "offline" );
502 }
503 }
504
505 static void remote_player_effect( struct network_player *player,
506 player_pose *final_pose ){
507 /* effects */
508 }
509
510 /*
511 * write the remote players final_mtx
512 */
513 static void pose_remote_player( u32 index,
514 struct interp_frame *f0,
515 struct interp_frame *f1 ){
516
517 struct network_player *player = &netplayers.list[ index ];
518
519 struct interp_buffer *buf = &netplayers.interp_data[ index ];
520 struct skeleton *sk = &localplayer.skeleton;
521 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
522 struct player_board_pose *board_pose = &netplayers.board_poses[index];
523
524 struct player_subsystem_interface *sys0 = player_subsystems[f0->subsystem],
525 *sys1 = NULL;
526
527 struct player_board *board =
528 addon_cache_item_if_loaded( k_addon_type_board, player->board_view_slot );
529
530 player_pose pose0, pose1, posed;
531 sys0->pose( &f0->data, &pose0 );
532
533 u8 instance_id = 0;
534
535 f32 t = 0.0f;
536
537 if( f1 ){
538 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
539 t = vg_clampf( t, 0.0f, 1.0f );
540
541 sys1 = player_subsystems[f1->subsystem];
542 sys1->pose( &f1->data, &pose1 );
543
544 u16 bounds = f0->boundary_hash^f1->boundary_hash;
545
546 if( bounds & NETMSG_BOUNDARY_BIT )
547 t = 1.0f;
548
549 if( bounds & NETMSG_GATE_BOUNDARY_BIT ){
550 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
551 t = 1.0f;
552 }
553
554 instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
555 lerp_player_pose( &pose0, &pose1, t, &posed );
556 effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
557
558 apply_full_skeleton_pose( sk, &posed, final_mtx );
559
560 if( t < 0.5f ){
561 if( sys0->effects )
562 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
563 }
564 else{
565 if( sys1->effects )
566 sys1->effects( &f1->data, final_mtx, board, &player->effect_data );
567 }
568
569 memcpy( board_pose, &posed.board, sizeof(*board_pose) );
570 }
571 else {
572 instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
573 effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
574 apply_full_skeleton_pose( sk, &pose0, final_mtx );
575 if( sys0->effects )
576 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
577 memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
578 }
579
580 if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
581 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
582 player->render_glider = 1;
583
584 v3f co;
585 v4f q;
586 f32 s;
587
588 if( f1 ){
589 v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
590 q_nlerp( f0->data_glider.root_q, f1->data_glider.root_q, t, q );
591 s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
592 }
593 else {
594 v3_copy( f0->data_glider.root_co, co );
595 v4_copy( f0->data_glider.root_q, q );
596 s = f0->data_glider.s;
597 }
598
599 v3f *mtx = netplayers.glider_mtx[ index ];
600 q_m3x3( q, mtx );
601 m3x3_scalef( mtx, s );
602 v3_copy( co, mtx[3] );
603 }
604 else
605 player->render_glider = 0;
606
607 if( player->world_match[ instance_id ] )
608 player->active_world = &world_static.instances[ instance_id ];
609 }
610
611 /*
612 * animate remote player and store in final_mtx
613 */
614 void animate_remote_player( u32 index )
615 {
616 /*
617 * Trys to keep the cursor inside the buffer
618 */
619 f64 min_time = -999999999.9,
620 max_time = 999999999.9,
621 abs_max_time = -999999999.9;
622
623 struct interp_frame *minframe = NULL,
624 *maxframe = NULL,
625 *abs_max_frame = NULL;
626
627 struct interp_buffer *buf = &netplayers.interp_data[index];
628 for( u32 i=0; i<vg_list_size(buf->frames); i ++ ){
629 struct interp_frame *ifr = &buf->frames[i];
630
631 if( ifr->active ){
632 if( (ifr->timestamp > min_time) && (ifr->timestamp < buf->t) ){
633 min_time = ifr->timestamp;
634 minframe = ifr;
635 }
636
637 if( (ifr->timestamp < max_time) && (ifr->timestamp > buf->t) ){
638 max_time = ifr->timestamp;
639 maxframe = ifr;
640 }
641
642 if( ifr->timestamp > abs_max_time ){
643 abs_max_time = ifr->timestamp;
644 abs_max_frame = ifr;
645 }
646 }
647 }
648
649 struct network_player *player = &netplayers.list[ index ];
650 if( player->active != 2 )
651 player->active_world = NULL;
652
653 if( minframe && maxframe ){
654 pose_remote_player( index, minframe, maxframe );
655 buf->t += vg.time_frame_delta;
656 }
657 else {
658 buf->t = abs_max_time - 0.25;
659
660 if( abs_max_frame )
661 pose_remote_player( index, abs_max_frame, NULL );
662 else
663 return;
664 }
665 }
666
667 /*
668 * Update full final_mtx for all remote players
669 */
670 void animate_remote_players(void)
671 {
672 for( u32 i=0; i<vg_list_size(netplayers.list); i ++ ){
673 struct network_player *player = &netplayers.list[i];
674 if( !player->active ) continue;
675
676 animate_remote_player( i );
677 }
678 }
679
680 /*
681 * Draw remote players
682 */
683 void render_remote_players( world_instance *world, vg_camera *cam )
684 {
685 u32 draw_list[ NETWORK_MAX_PLAYERS ],
686 draw_list_count = 0,
687 gliders = 0;
688
689 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
690 struct network_player *player = &netplayers.list[i];
691 if( !player->active || player->isblocked ) continue;
692 if( player->active_world != world ) continue;
693 if( !player->isfriend &&
694 (world-world_static.instances == k_world_purpose_hub)) continue;
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 }