43c56079cd436123f39f638e8f4706aa7e6581f4
[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 ui_font_face( &vgf_default_title );
446 ui_info( panel, "Network" );
447 ui_font_face( &vgf_default_large );
448 ui_info( panel, "Status" );
449 ui_font_face( &vgf_default_small );
450
451 char buf[512];
452 const char *netstatus = "PROGRAMMING ERROR";
453
454 struct { enum ESteamNetworkingConnectionState state; const char *str; }
455 states[] = {
456 { k_ESteamNetworkingConnectionState_None, "None" },
457 { k_ESteamNetworkingConnectionState_Connecting,
458 (const char *[]){"Connecting -",
459 "Connecting /",
460 "Connecting |",
461 "Connecting \\",
462 }[(u32)(vg.time_real/0.25) & 0x3 ] },
463 { k_ESteamNetworkingConnectionState_FindingRoute, "Finding Route" },
464 { k_ESteamNetworkingConnectionState_Connected, "Connected" },
465 { k_ESteamNetworkingConnectionState_ClosedByPeer, "Closed by peer" },
466 { k_ESteamNetworkingConnectionState_ProblemDetectedLocally,
467 "Problem Detected Locally" },
468 { k_ESteamNetworkingConnectionState_FinWait, "Fin Wait" },
469 { k_ESteamNetworkingConnectionState_Linger, "Linger" },
470 { k_ESteamNetworkingConnectionState_Dead, "Dead" }
471 };
472 for( u32 i=0; i<vg_list_size(states); i ++ ){
473 if( states[i].state == network_client.state ){
474 netstatus = states[i].str;
475 break;
476 }
477 }
478 snprintf( buf, 512, "Network: %s", netstatus );
479 ui_info( panel, buf );
480 ui_info( panel, "---------------------" );
481
482 if( network_client.state == k_ESteamNetworkingConnectionState_Connected ){
483 ui_info( panel, "#-1: localplayer" );
484
485 snprintf( buf, 512, "U%.3f/D%.3fkbs",
486 netplayers.up_kbs, netplayers.down_kbs );
487 ui_info( panel, buf );
488
489 for( u32 i=0; i<vg_list_size(netplayers.list); i++ ){
490 struct network_player *player = &netplayers.list[i];
491 if( player->active ){
492 const char *sysname = "invalid";
493
494 if( player->subsystem < k_player_subsystem_max ){
495 sysname = player_subsystems[ player->subsystem ]->name;
496 }
497 snprintf( buf, 512, "#%u: %s [%s] D%.1fkbs",
498 i, player->username, sysname, player->down_kbs );
499 ui_info( panel, buf );
500 }
501 }
502 }
503 else {
504 ui_info( panel, "offline" );
505 }
506 }
507
508 static void remote_player_effect( struct network_player *player,
509 player_pose *final_pose ){
510 /* effects */
511 }
512
513 /*
514 * write the remote players final_mtx
515 */
516 static void pose_remote_player( u32 index,
517 struct interp_frame *f0,
518 struct interp_frame *f1 ){
519
520 struct network_player *player = &netplayers.list[ index ];
521
522 struct interp_buffer *buf = &netplayers.interp_data[ index ];
523 struct skeleton *sk = &localplayer.skeleton;
524 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
525 struct player_board_pose *board_pose = &netplayers.board_poses[index];
526
527 struct player_subsystem_interface *sys0 = player_subsystems[f0->subsystem],
528 *sys1 = NULL;
529
530 struct player_board *board =
531 addon_cache_item_if_loaded( k_addon_type_board, player->board_view_slot );
532
533 player_pose pose0, pose1, posed;
534 sys0->pose( &f0->data, &pose0 );
535
536 u8 instance_id = 0;
537
538 f32 t = 0.0f;
539
540 if( f1 ){
541 t = (buf->t - f0->timestamp) / (f1->timestamp - f0->timestamp);
542 t = vg_clampf( t, 0.0f, 1.0f );
543
544 sys1 = player_subsystems[f1->subsystem];
545 sys1->pose( &f1->data, &pose1 );
546
547 u16 bounds = f0->boundary_hash^f1->boundary_hash;
548
549 if( bounds & NETMSG_BOUNDARY_BIT )
550 t = 1.0f;
551
552 if( bounds & NETMSG_GATE_BOUNDARY_BIT ){
553 /* TODO: Extra work retransforming the root_co, instance_id.. etc */
554 t = 1.0f;
555 }
556
557 instance_id = f1->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
558 lerp_player_pose( &pose0, &pose1, t, &posed );
559 effect_blink_apply( &player->effect_data.blink, &posed, vg.time_delta );
560
561 apply_full_skeleton_pose( sk, &posed, final_mtx );
562
563 if( t < 0.5f ){
564 if( sys0->effects )
565 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
566 }
567 else{
568 if( sys1->effects )
569 sys1->effects( &f1->data, final_mtx, board, &player->effect_data );
570 }
571
572 memcpy( board_pose, &posed.board, sizeof(*board_pose) );
573 }
574 else {
575 instance_id = f0->flags & NETMSG_PLAYERFRAME_INSTANCE_ID;
576 effect_blink_apply( &player->effect_data.blink, &pose0, vg.time_delta );
577 apply_full_skeleton_pose( sk, &pose0, final_mtx );
578 if( sys0->effects )
579 sys0->effects( &f0->data, final_mtx, board, &player->effect_data );
580 memcpy( board_pose, &pose0.board, sizeof(*board_pose) );
581 }
582
583 if( f0->flags & (NETMSG_PLAYERFRAME_HAVE_GLIDER|
584 NETMSG_PLAYERFRAME_GLIDER_ORPHAN) ){
585 player->render_glider = 1;
586
587 v3f co;
588 v4f q;
589 f32 s;
590
591 if( f1 ){
592 v3_lerp( f0->data_glider.root_co, f1->data_glider.root_co, t, co );
593 q_nlerp( f0->data_glider.root_q, f1->data_glider.root_q, t, q );
594 s = vg_lerpf( f0->data_glider.s, f1->data_glider.s, t );
595 }
596 else {
597 v3_copy( f0->data_glider.root_co, co );
598 v4_copy( f0->data_glider.root_q, q );
599 s = f0->data_glider.s;
600 }
601
602 v3f *mtx = netplayers.glider_mtx[ index ];
603 q_m3x3( q, mtx );
604 m3x3_scalef( mtx, s );
605 v3_copy( co, mtx[3] );
606 }
607 else
608 player->render_glider = 0;
609
610 if( player->world_match[ instance_id ] )
611 player->active_world = &world_static.instances[ instance_id ];
612 }
613
614 /*
615 * animate remote player and store in final_mtx
616 */
617 void animate_remote_player( u32 index )
618 {
619 /*
620 * Trys to keep the cursor inside the buffer
621 */
622 f64 min_time = -999999999.9,
623 max_time = 999999999.9,
624 abs_max_time = -999999999.9;
625
626 struct interp_frame *minframe = NULL,
627 *maxframe = NULL,
628 *abs_max_frame = NULL;
629
630 struct interp_buffer *buf = &netplayers.interp_data[index];
631 for( u32 i=0; i<vg_list_size(buf->frames); i ++ ){
632 struct interp_frame *ifr = &buf->frames[i];
633
634 if( ifr->active ){
635 if( (ifr->timestamp > min_time) && (ifr->timestamp < buf->t) ){
636 min_time = ifr->timestamp;
637 minframe = ifr;
638 }
639
640 if( (ifr->timestamp < max_time) && (ifr->timestamp > buf->t) ){
641 max_time = ifr->timestamp;
642 maxframe = ifr;
643 }
644
645 if( ifr->timestamp > abs_max_time ){
646 abs_max_time = ifr->timestamp;
647 abs_max_frame = ifr;
648 }
649 }
650 }
651
652 struct network_player *player = &netplayers.list[ index ];
653 if( player->active != 2 )
654 player->active_world = NULL;
655
656 if( minframe && maxframe ){
657 pose_remote_player( index, minframe, maxframe );
658 buf->t += vg.time_frame_delta;
659 }
660 else {
661 buf->t = abs_max_time - 0.25;
662
663 if( abs_max_frame )
664 pose_remote_player( index, abs_max_frame, NULL );
665 else
666 return;
667 }
668 }
669
670 /*
671 * Update full final_mtx for all remote players
672 */
673 void animate_remote_players(void)
674 {
675 for( u32 i=0; i<vg_list_size(netplayers.list); i ++ ){
676 struct network_player *player = &netplayers.list[i];
677 if( !player->active ) continue;
678
679 animate_remote_player( i );
680 }
681 }
682
683 /*
684 * Draw remote players
685 */
686 void render_remote_players( world_instance *world, vg_camera *cam )
687 {
688 u32 draw_list[ NETWORK_MAX_PLAYERS ],
689 draw_list_count = 0,
690 gliders = 0;
691
692 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
693 struct network_player *player = &netplayers.list[i];
694 if( !player->active || player->isblocked ) continue;
695 if( player->active_world != world ) continue;
696
697 #if 0
698 if( !player->isfriend &&
699 (world-world_static.instances == k_world_purpose_hub)) continue;
700 #endif
701
702 draw_list[draw_list_count ++] = i;
703
704 if( player->render_glider )
705 gliders ++;
706 }
707
708 struct skeleton *sk = &localplayer.skeleton;
709
710 SDL_AtomicLock( &addon_system.sl_cache_using_resources );
711
712 for( u32 j=0; j<draw_list_count; j ++ ){
713 u32 index = draw_list[j];
714
715 struct network_player *player = &netplayers.list[index];
716 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*index ];
717
718 struct player_model *model =
719 addon_cache_item_if_loaded( k_addon_type_player,
720 player->playermodel_view_slot );
721
722 if( !model ) model = &localplayer.fallback_model;
723 render_playermodel( cam, world, 0, model, sk, final_mtx );
724
725 struct player_board *board =
726 addon_cache_item_if_loaded( k_addon_type_board,
727 player->board_view_slot );
728 render_board( cam, world, board, final_mtx[localplayer.id_board],
729 &netplayers.board_poses[ index ], k_board_shader_player );
730 }
731
732 SDL_AtomicUnlock( &addon_system.sl_cache_using_resources );
733
734 if( !gliders )
735 return;
736
737 /* TODO: we really, really only need to do all this once. at some point
738 * PLEASE figure out a good place to do this once per frame!
739 */
740
741 shader_model_entity_use();
742 shader_model_entity_uTexMain( 0 );
743 shader_model_entity_uCamera( cam->transform[3] );
744 shader_model_entity_uPv( cam->mtx.pv );
745
746 WORLD_BIND_LIGHT_BUFFERS_UB0_TEX234( world, model_entity );
747
748 for( u32 j=0; j<draw_list_count; j ++ ){
749 u32 index = draw_list[j];
750
751 struct network_player *player = &netplayers.list[index];
752 if( !player->render_glider ) continue;
753
754 if( player->render_glider ){
755 v3f *glider_mtx = netplayers.glider_mtx[ index ];
756 render_glider_model( cam, world, glider_mtx, k_board_shader_entity );
757 }
758 }
759 }
760
761 static int remote_players_randomize( int argc, const char *argv[] ){
762 for( int i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
763 struct network_player *player = &netplayers.list[i];
764
765 player->active = (vg_randu32(&vg.rand) & 0x1)? 2: 0;
766 player->isfriend = vg_randu32(&vg.rand) & vg_randu32(&vg.rand) & 0x1;
767 player->isblocked = vg_randu32(&vg.rand) &
768 vg_randu32(&vg.rand) &
769 vg_randu32(&vg.rand) & 0x1;
770 player->world_match[ 0 ] = vg_randu32(&vg.rand) & 0x1;
771 player->world_match[ 1 ] = 0;
772
773 if( player->world_match[0] )
774 player->active_world = &world_static.instances[0];
775 else
776 player->active_world = NULL;
777
778 for( int i=0; i<sizeof(player->username)-1; i ++ ){
779 player->username[i] = 'a' + (vg_randu32(&vg.rand) % 30);
780 player->username[i+1] = '\0';
781
782 if( (vg_randu32(&vg.rand) % 8) == 3 )
783 break;
784 }
785
786 for( int i=0; i<3; i ++ ){
787 player->medals[i] = vg_randu32(&vg.rand) % 3;
788 }
789
790 v3f pos;
791
792 vg_rand_sphere( &vg.rand, pos );
793 v3_muladds( localplayer.rb.co, pos, 100.0f,
794 netplayers.final_mtx[ i*localplayer.skeleton.bone_count][3] );
795 }
796
797 return 0;
798 }
799
800 void remote_players_init(void)
801 {
802 vg_console_reg_cmd( "add_test_players", remote_players_randomize, NULL );
803 vg_console_reg_var( "k_show_own_name", &k_show_own_name,
804 k_var_dtype_i32, 0 );
805 for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
806 netplayers.sfx_queue[i].system = k_player_subsystem_invalid;
807 }
808 }
809
810 void remote_sfx_pre_update(void)
811 {
812 for( u32 i=0; i<NETWORK_SFX_QUEUE_LENGTH; i ++ ){
813 struct net_sfx *si = &netplayers.sfx_queue[i];
814
815 if( si->system != k_player_subsystem_invalid ){
816 si->subframe -= vg.time_frame_delta;
817 if( si->subframe <= 0.0f ){
818 net_sfx_play( si );
819 si->system = k_player_subsystem_invalid;
820 }
821 }
822 }
823 }
824
825 /*
826 * animator->root_co of remote player
827 */
828 static void remote_player_position( int id, v3f out_co ){
829 struct skeleton *sk = &localplayer.skeleton;
830 m4x3f *final_mtx = &netplayers.final_mtx[ sk->bone_count*id ];
831 v3_copy( final_mtx[0][3], out_co );
832 }
833
834 enum remote_player_gui_type {
835 k_remote_player_gui_type_stranger,
836 k_remote_player_gui_type_friend,
837 k_remote_player_gui_type_you,
838 };
839
840 /*
841 * Given players' root_co, get the screen point where we should draw tag info.
842 */
843 static int player_tag_position( m4x4f pv, v3f root_co, ui_point out_point ){
844 v4f wpos;
845 v3_copy( root_co, wpos );
846 wpos[1] += 2.0f;
847 wpos[3] = 1.0f;
848
849 m4x4_mulv( pv, wpos, wpos );
850
851 if( wpos[3] > 0.0f ){
852 v2_muls( wpos, (1.0f/wpos[3]) * 0.5f, wpos );
853 v2_add( wpos, (v2f){ 0.5f, 0.5f }, wpos );
854
855 float k_max = 32000.0f;
856
857 out_point[0] = vg_clampf(wpos[0] * vg.window_x, -k_max, k_max );
858 out_point[1] = vg_clampf((1.0f-wpos[1]) * vg.window_y, -k_max, k_max );
859 return 1;
860 }
861 else
862 return 0;
863 }
864
865 /*
866 * Draw chat box relative to the root tag position on the screen
867 */
868 static void chat_box( ui_point tag_root, f64 time, const char *message )
869 {
870 if( (vg.time_real - time) > 15.0 )
871 return;
872
873 ui_rect wr;
874 wr[2] = ui_text_line_width( message ) + 8;
875 wr[3] = vg_ui.font->ch + 2;
876 wr[0] = tag_root[0]-(wr[2]/2);
877 wr[1] = tag_root[1] - wr[3] - 8;
878
879 ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
880 ui_text( wr, message, 1, k_ui_align_middle_center, 0 );
881 }
882
883 /*
884 * Draw full imgui for remote player
885 */
886 static void remote_player_nametag( ui_point tag_root,
887 struct network_player *player )
888 {
889 ui_font_face( &vgf_default_large );
890
891 ui_rect wr;
892 wr[2] = VG_MAX( ui_text_line_width( player->username ), 140 ) + 8;
893 wr[3] = 32;
894 wr[0] = tag_root[0]-(wr[2]/2);
895 wr[1] = tag_root[1]-(wr[3]/2);
896
897 ui_fill( wr, ui_opacity( ui_colour(k_ui_bg), 0.23f ) );
898 ui_text( wr, player->username, 1, k_ui_align_middle_center, 0 );
899
900 ui_font_face( &vgf_default_small );
901
902 /* medals */
903 int cols = 0;
904 for( int i=0; i<3; i ++ )
905 if( player->medals[i] )
906 cols ++;
907
908 char buf[32];
909 vg_str str;
910
911 if( cols ){
912 f32 w = (f32)wr[2] / (f32)cols;
913 cols = 0;
914
915 for( int i=0; i<3; i ++ ){
916 if( player->medals[i] ){
917 ui_rect col = { wr[0] + (f32)cols*w, wr[1] + wr[3], w,
918 vg_ui.font->ch };
919
920 vg_strnull( &str, buf, 32 );
921 #if 0
922 vg_strcatch( &str, (char)k_SRglyph_vg_circle );
923 #endif
924 vg_strcati32( &str, player->medals[i] );
925
926 ui_text( col, buf, 1, k_ui_align_middle_center,
927 ui_colour( (enum ui_scheme_colour[]){
928 k_ui_yellow, k_ui_gray, k_ui_orange }[i] ) );
929
930 cols ++;
931 }
932 }
933 }
934 }
935
936 static void remote_player_world_gui( m4x4f pv, v3f root_co,
937 struct network_player *player ){
938 ui_point tag_root;
939 if( !player_tag_position( pv, root_co, tag_root ) )
940 return;
941
942 if( player ){
943 remote_player_nametag( tag_root, player );
944 chat_box( tag_root, player->chat_time, player->chat );
945 }
946 else {
947 if( netplayers.chatting )
948 chat_box( tag_root, vg.time_real, netplayers.chat_buffer );
949 else
950 chat_box( tag_root, netplayers.chat_time, netplayers.chat_message );
951 }
952 }
953
954 static void remote_player_gui_info( ui_rect box,
955 const char *username,
956 const char *activity,
957 enum remote_player_gui_type type,
958 int in_world ){
959
960 f32 opacity = in_world? 0.6f: 0.3f;
961
962 if( type == k_remote_player_gui_type_you )
963 ui_fill( box, ui_opacity( 0xff555555, opacity ) );
964 else
965 ui_fill( box, ui_opacity( 0xff000000, opacity ) );
966
967 if( type == k_remote_player_gui_type_friend )
968 ui_outline( box, -1, ui_opacity( 0xff00c4f0, opacity ), 0 );
969
970 ui_rect top, bottom;
971 ui_split_ratio( box, k_ui_axis_h, 0.6666f, 1, top, bottom );
972
973 u32 fg;
974
975 if( type == k_remote_player_gui_type_friend )
976 fg = ui_colour( k_ui_yellow + (in_world? k_ui_brighter: 0) );
977 else
978 fg = ui_colour( in_world? k_ui_fg: k_ui_fg+4 );
979
980 ui_font_face( &vgf_default_large );
981 ui_text( top, username, 1, k_ui_align_middle_center, fg );
982 ui_font_face( &vgf_default_small );
983
984 ui_text( bottom, activity, 1, k_ui_align_middle_center, fg );
985 }
986
987 void remote_players_imgui_lobby(void)
988 {
989 if( network_client.user_intent == k_server_intent_online ){
990 if( !(steam_ready &&
991 (network_client.state == k_ESteamNetworkingConnectionState_Connected)))
992 {
993 return;
994 }
995 }
996
997 ui_px y = 50, width = 200, height = 42, gap = 2,
998 x = vg.window_x - width;
999
1000 ui_font_face( &vgf_default_large );
1001 ui_text( (ui_rect){ x, 0, width, height },
1002 "In World", 1, k_ui_align_middle_center, 0 );
1003 ui_font_face( &vgf_default_small );
1004
1005
1006 ui_rect us = { x, y, width, height };
1007 /* FIXME: your location */
1008 remote_player_gui_info( us, steam_username_at_startup, "you",
1009 k_remote_player_gui_type_you, 1 );
1010 y += height + gap;
1011
1012 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i ++ ){
1013 struct network_player *player = &netplayers.list[i];
1014 if( !player->active || player->isblocked ) continue;
1015
1016 int in_same_world = player->active_world == world_current_instance();
1017 if( !player->isfriend && !in_same_world )
1018 continue;
1019
1020 const char *location = in_same_world? "": "another world";
1021 if( player->region_flags & k_ent_region_flag_hasname ){
1022 location = player->region;
1023 }
1024
1025 ui_rect box = { x, y, width, height };
1026 remote_player_gui_info( box, player->username, location,
1027 player->isfriend, in_same_world );
1028 y += height + gap;
1029 }
1030 }
1031
1032 void remote_players_imgui_world( world_instance *world, m4x4f pv,
1033 f32 max_dist, int geo_cull )
1034 {
1035 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1036
1037 for( u32 i=0; i<NETWORK_MAX_PLAYERS; i++ ){
1038 struct network_player *player = &netplayers.list[i];
1039 if( player->active ){
1040 v3f co;
1041 remote_player_position( i, co );
1042
1043 if( !player->active_world )
1044 continue;
1045 if( !player->isfriend &&
1046 (world-world_static.instances == k_world_purpose_hub)) continue;
1047
1048 /* their in our active subworld */
1049 if( player->active_world != world ){
1050 m4x3_mulv( global_miniworld.mmdl, co, co );
1051 co[1] -= 2.0f; /* HACK lol */
1052 }
1053
1054 f32 d2 = v3_dist2( co, localplayer.rb.co );
1055
1056 if( d2 > (max_dist*max_dist) )
1057 continue;
1058
1059 f32 dist = sqrtf(d2);
1060 f32 opacity = 0.95f * sqrtf(((max_dist-dist)/max_dist));
1061
1062 if( geo_cull ){
1063 ray_hit hit;
1064 hit.dist = dist;
1065
1066 v3f dir;
1067 v3_sub( co, skaterift.cam.pos, dir );
1068 v3_normalize( dir );
1069
1070 if( ray_world( world, skaterift.cam.pos, dir, &hit,
1071 k_material_flag_ghosts ) ){
1072 opacity *= 0.5f;
1073 }
1074 }
1075
1076 player->opacity = vg_lerpf( player->opacity, opacity,
1077 vg.time_frame_delta * 2.0f );
1078
1079 remote_player_world_gui( pv, co, player );
1080
1081 vg_ui.colour[3] = player->opacity;
1082 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1083 }
1084 }
1085
1086 vg_ui.colour[3] = 1.0f;
1087
1088 remote_player_world_gui( pv, localplayer.rb.co, NULL );
1089 ui_flush( k_ui_shader_colour, vg.window_x, vg.window_y );
1090 }
1091
1092 static void chat_escape(void){
1093 netplayers.chatting = -1;
1094 }
1095
1096 static void chat_enter( char *buf, u32 len ){
1097 vg_strncpy( buf, netplayers.chat_message, NETWORK_MAX_CHAT,
1098 k_strncpy_always_add_null );
1099 netplayers.chatting = -1;
1100 netplayers.chat_time = vg.time_real;
1101 chat_send_message( buf );
1102 }
1103
1104 void remote_players_chat_imgui(void)
1105 {
1106 if( netplayers.chatting == 1 ){
1107 ui_rect box = { 0, 0, 400, 40 },
1108 window = { 0, 0, vg.window_x, vg.window_y };
1109 ui_rect_center( window, box );
1110
1111 struct ui_textbox_callbacks callbacks = {
1112 .enter = chat_enter,
1113 .escape = chat_escape
1114 };
1115
1116 ui_textbox( box, NULL,
1117 netplayers.chat_buffer, NETWORK_MAX_CHAT, 1,
1118 UI_TEXTBOX_AUTOFOCUS, &callbacks );
1119 }
1120 else {
1121 if( netplayers.chatting == -1 ){
1122 netplayers.chatting = 0;
1123 srinput.state = k_input_state_resume;
1124 }
1125 else {
1126 if( (skaterift.activity == k_skaterift_default) &&
1127 button_down( k_srbind_chat ) ){
1128 netplayers.chatting = 1;
1129 netplayers.chat_buffer[0] = '\0';
1130 srinput.state = k_input_state_pause;
1131 }
1132 }
1133 }
1134 }