initial gui for remote players lobby
[carveJwlIkooP6JGAAIwe30JlM.git] / skaterift.c
1 /*
2 * =============================================================================
3 *
4 * Copyright . . . -----, ,----- ,---. .---.
5 * 2021-2023 |\ /| | / | | | | /|
6 * | \ / | +-- / +----- +---' | / |
7 * | \ / | | / | | \ | / |
8 * | \/ | | / | | \ | / |
9 * ' ' '--' [] '----- '----- ' ' '---' SOFTWARE
10 *
11 * =============================================================================
12 */
13
14 #if 1
15
16 #define SR_NETWORKED
17 #define SR_USE_LOCALHOST
18
19 #ifndef VG_RELEASE
20 #define VG_DEVWINDOW
21 #endif
22
23 /*
24 * system headers
25 * --------------------- */
26
27 #include "skaterift.h"
28 #include "steam.h"
29 #include "render.h"
30 #include "audio.h"
31
32 #include "world.h"
33
34 #include "font.h"
35 #include "player.h"
36
37 #include "network.h"
38 #include "menu.h"
39 #include "vehicle.h"
40 #include "pointcloud.h"
41 #include "save.h"
42 #include "player_remote.h"
43
44 /* unity build
45 * ----------------- */
46
47 #include "world.c"
48 #include "player.c"
49 #include "vehicle.c"
50 #include "entity.c"
51 #include "workshop.c"
52 #include "addon.c"
53 #include "highscores.c"
54 #include "save.c"
55 #include "respawn.c"
56 #include "network.c"
57 #include "player_remote.c"
58 #include "vg/vg_audio_dsp.h"
59
60 static struct player_avatar localplayer_avatar;
61 static int k_tools_mode = 0;
62
63 int main( int argc, char *argv[] ){
64 vg_mem.use_libc_malloc = 0;
65 vg_set_mem_quota( 160*1024*1024 );
66 vg_enter( argc, argv, "Voyager Game Engine" );
67 return 0;
68 }
69
70 static void vg_launch_opt(void){
71 const char *arg;
72 if( vg_long_opt( "noauth" ) ){
73 network_client.auth_mode = eServerModeNoAuthentication;
74 }
75
76 if( (arg = vg_long_opt_arg( "server" )) ){
77 vg_strncpy( arg, network_client.server_adress, 64,
78 k_strncpy_overflow_fatal );
79 }
80
81 if( vg_long_opt( "tools" ) ){
82 k_tools_mode = 1;
83 }
84 }
85
86 static void vg_preload(void){
87 //skaterift_read_savedata();
88
89 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
90 vg_info(" 2021-2023 |\\ /| | / | | | | /| \n" );
91 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
92 vg_info(" | \\ / | | / | | \\ | / | \n" );
93 vg_info(" | \\/ | | / | | \\ | / | \n" );
94 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
95 "SOFTWARE\n" );
96
97 if( k_tools_mode )
98 return;
99
100 steam_init();
101 vg_loader_step( NULL, steam_end );
102 vg_loader_step( remote_players_init, NULL );
103 vg_loader_step( network_init, network_end );
104 }
105
106 static void load_playermodels(void){
107 /* FIXME: hack */
108 shader_model_character_view_register();
109 shader_model_board_view_register();
110 shader_model_entity_register();
111 }
112
113 static void async_skaterift_player_start( void *payload, u32 size ){
114 localplayer.viewable_world = world_current_instance();
115 localplayer_cmd_respawn( 1, (const char *[]){ "start" } );
116 }
117
118 static void async_call_ready( void *payload, u32 size ){
119 skaterift.op = k_async_op_none;
120 }
121
122 static void skaterift_restore_state(void){
123 savedata_file sav;
124 strcpy( sav.path, str_skaterift_main_save );
125 savedata_file_read( &sav );
126
127 vg_msg kvsav;
128 vg_msg_init( &kvsav, sav.buf, sizeof(sav.buf) );
129
130 u32 board_reg_id = 0,
131 player_reg_id = 0;
132
133 vg_msg_cursor orig = kvsav.cur;
134
135 if( vg_msg_seekframe( &kvsav, "player" ) ){
136 addon_alias q;
137
138 /* board */
139 skaterift_read_addon_alias( &kvsav, "board", k_addon_type_board, &q );
140 u32 reg_id = addon_match( &q );
141 if( reg_id != 0xffffffff )
142 board_reg_id = reg_id;
143
144 /* playermodel */
145 skaterift_read_addon_alias( &kvsav, "playermodel",
146 k_addon_type_player, &q );
147 reg_id = addon_match( &q );
148 if( reg_id != 0xffffffff )
149 player_reg_id = reg_id;
150 }
151
152 localplayer.board_view_slot =
153 addon_cache_create_viewer( k_addon_type_board, board_reg_id );
154 localplayer.playermodel_view_slot =
155 addon_cache_create_viewer( k_addon_type_player, player_reg_id );
156
157 kvsav.cur = orig;
158 if( vg_msg_seekframe( &kvsav, "world" ) ){
159 addon_alias q;
160
161 /* world */
162 skaterift_read_addon_alias( &kvsav, "alias", k_addon_type_world, &q );
163 u32 reg_id = addon_match( &q );
164 if( reg_id != 0xffffffff ){
165 addon_reg *reg = get_addon_from_index( k_addon_type_world, reg_id );
166
167 world_static.load_state = k_world_loader_load;
168 struct world_load_args args = {
169 .purpose = k_world_purpose_client,
170 .reg = reg
171 };
172 skaterift_world_load_thread( &args );
173
174 world_set_active_instance( vg_msg_getkvu32( &kvsav, "index", 0 ) );
175 world_static.active_trigger_volume_count = 0;
176 localplayer.viewable_world = world_current_instance();
177 }
178
179 v3f pos;
180 vg_msg_getkvv3f( &kvsav, "position", pos, (v3f){0.0f,0.0f,0.0f} );
181
182 if( v3_length2(pos) > 1.0f )
183 player__setpos( pos );
184 }
185 }
186
187 static void vg_load(void){
188 if( k_tools_mode ){
189 vg_async_call( async_call_ready, NULL, 0 );
190 return;
191 }
192
193 vg_console_reg_cmd( "changeworld", skaterift_change_world_command, NULL );
194
195 vg_loader_step( render_init, NULL );
196 vg_loader_step( menu_init, NULL );
197 vg_loader_step( pointcloud_init, NULL );
198 vg_loader_step( world_init, NULL );
199 vg_loader_step( vehicle_init, NULL );
200 vg_loader_step( font3d_init, NULL );
201 vg_loader_step( gui_init, NULL );
202
203 vg_loader_step( player_init, NULL );
204 vg_loader_step( player_ragdoll_init, NULL );
205
206 /* content stuff */
207 vg_loader_step( addon_system_init, NULL );
208 vg_loader_step( workshop_init, NULL );
209 vg_loader_step( skateshop_init, NULL );
210
211 /* ----------------- */
212 vg_loader_step( load_playermodels, NULL );
213
214 /* player setup */
215 u32 bytes = 1024*1024*10;
216 skaterift.replay.data = vg_linear_alloc( vg_mem.rtmemory, bytes );
217 skaterift.replay.size = bytes;
218 replay_clear( &skaterift.replay );
219
220 player_avatar_load( &localplayer_avatar, "models/ch_none.mdl" );
221 player__use_avatar( &localplayer_avatar );
222
223 /* FIXME FIXME FIXME FIXME FIXME */
224 u32 mtx_size = sizeof(m4x3f)*localplayer_avatar.sk.bone_count;
225 localplayer.final_mtx = vg_linear_alloc( vg_mem.rtmemory, mtx_size );
226 netplayers.final_mtx = vg_linear_alloc( vg_mem.rtmemory,
227 mtx_size*NETWORK_MAX_PLAYERS );
228
229 player_model_load( &localplayer.fallback_model, "models/ch_none.mdl" );
230 player__bind();
231
232 /* --------------------- */
233
234 vg_bake_shaders();
235 vg_loader_step( audio_init, audio_free );
236
237 /* 'systems' are completely loaded now */
238
239 /* Completing addon registrations
240 * -------------------------------------
241 */
242
243 /* understate diy. */
244 addon_reg *spawn = addon_mount_local_addon( "maps/mp_spawn",
245 k_addon_type_world,
246 ".mdl" );
247 {
248 vg_msg msg;
249 vg_msg_init( &msg, spawn->metadata, sizeof(spawn->metadata) );
250 vg_msg_frame( &msg, "workshop" );
251 vg_msg_wkvstr( &msg, "title", "Understate DIY" );
252 vg_msg_end_frame( &msg );
253 vg_msg_wkvstr( &msg, "location", "USA" );
254 spawn->metadata_len = msg.cur.co;
255 }
256
257 /* mtzero island */
258 addon_reg *mtzero = addon_mount_local_addon( "maps/mp_mtzero",
259 k_addon_type_world,
260 ".mdl" );
261 {
262 vg_msg msg;
263 vg_msg_init( &msg, spawn->metadata, sizeof(mtzero->metadata) );
264 vg_msg_frame( &msg, "workshop" );
265 vg_msg_wkvstr( &msg, "title", "Mt.Zero Island" );
266 vg_msg_end_frame( &msg );
267 vg_msg_wkvstr( &msg, "location", "Australia" );
268 mtzero->metadata_len = msg.cur.co;
269 }
270
271 /* load home/permanent world manually */
272 world_static.load_state = k_world_loader_load;
273 struct world_load_args args = {
274 .purpose = k_world_purpose_hub,
275 .reg = spawn
276 };
277 skaterift_world_load_thread( &args );
278
279 vg_async_call( async_skaterift_player_start, NULL, 0 );
280 vg_async_stall();
281
282
283 global_skateshop.selected_world_id=1;
284 global_skateshop.pointcloud_world_id=1;
285 skateshop_world_preview_loader_thread( mtzero ); /* HACK */
286
287 vg_console_load_autos();
288 menu_link();
289
290 addon_mount_content_folder( k_addon_type_player,
291 "playermodels", ".mdl" );
292 addon_mount_content_folder( k_addon_type_board, "boards", ".mdl" );
293 addon_mount_content_folder( k_addon_type_world, "maps", ".mdl" );
294 addon_mount_workshop_items();
295 vg_async_call( async_addon_reg_update, NULL, 0 );
296 vg_async_stall();
297
298 /* and now */
299 skaterift_restore_state();
300 vg_loader_step( NULL, skaterift_autosave_synchronous );
301
302 //board_processview_thread(NULL);
303 vg_async_call( async_call_ready, NULL, 0 );
304 }
305
306 static void draw_origin_axis(void){
307 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
308 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
309 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
310 }
311 static void skaterift_change_client_world_preupdate(void);
312
313 /*
314 * UPDATE LOOP
315 * ---------------------------------------------------------------------------*/
316
317 static void vg_pre_update(void){
318 srinput.enabled = 1;
319 skaterift_preupdate_inputs();
320
321 if( k_tools_mode ) return;
322
323 steam_update();
324 if( skaterift.op == k_async_op_clientloading ) return;
325 if( world_static.load_state == k_world_loader_preload )
326 skaterift_change_client_world_preupdate();
327
328 draw_origin_axis();
329 skateshop_autostart_loading();
330 network_update();
331
332 /* time rate */
333 f32 target = 1;
334 if( skaterift.activity & (k_skaterift_replay|k_skaterift_menu|
335 k_skaterift_respawning) ) {
336 target = 0;
337 }
338
339 vg_slewf( &skaterift.time_rate, target, vg.time_frame_delta * (1.0f/0.3f) );
340 vg.time_rate = vg_smoothstepf( skaterift.time_rate );
341
342 player__pre_update();
343 world_entity_focus_preupdate();
344 skaterift_replay_pre_update();
345 remote_sfx_pre_update();
346
347 world_update( world_current_instance(), localplayer.rb.co );
348 audio_ambient_sprites_update( world_current_instance(), localplayer.rb.co );
349 respawn_chooser_pre_update();
350 }
351
352 static void vg_fixed_update(void){
353 if( k_tools_mode ) return;
354 if( skaterift.op == k_async_op_clientloading ) return;
355
356 world_routes_fixedupdate( world_current_instance() );
357 player__update();
358 vehicle_update_fixed();
359 }
360
361 static void vg_post_update(void){
362 if( k_tools_mode ) return;
363 if( skaterift.op == k_async_op_clientloading ) return;
364
365 player__post_update();
366
367 float dist;
368 int sample_index;
369 world_audio_sample_distances( localplayer.rb.co, &sample_index, &dist );
370
371 audio_lock();
372 vg_dsp.echo_distances[sample_index] = dist;
373
374 v3f ears = { 1.0f,0.0f,0.0f };
375 m3x3_mulv( skaterift.cam.transform, ears, ears );
376 v3_copy( ears, vg_audio.external_listener_ears );
377 v3_copy( skaterift.cam.transform[3], vg_audio.external_listener_pos );
378
379 if( localplayer.gate_waiting ){
380 m4x3_mulv( localplayer.gate_waiting->transport,
381 vg_audio.external_listener_pos,
382 vg_audio.external_listener_pos );
383 }
384
385 v3_copy( localplayer.rb.v, vg_audio.external_lister_velocity );
386 audio_unlock();
387
388 vehicle_update_post();
389
390 if( vg.time - skaterift.last_autosave > 20.0 ){
391 if( skaterift_autosave(1) ){
392 skaterift.last_autosave = vg.time;
393 }
394 }
395 }
396
397 /*
398 * RENDERING
399 * ---------------------------------------------------------------------------*/
400
401 static void vg_framebuffer_resize( int w, int h ){
402 render_fb_resize();
403 }
404
405 static void present_view_with_post_processing(void){
406 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
407 glViewport( 0,0, vg.window_x, vg.window_y );
408
409 glEnable(GL_BLEND);
410 glDisable(GL_DEPTH_TEST);
411 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
412 glBlendEquation(GL_FUNC_ADD);
413
414 v2f inverse;
415 render_fb_inverse_ratio( gpipeline.fb_main, inverse );
416
417 if( k_blur_effect ){
418 shader_blitblur_use();
419 shader_blitblur_uTexMain( 0 );
420 shader_blitblur_uTexMotion( 1 );
421 shader_blitblur_uBlurStrength( k_blur_strength /
422 (vg.time_frame_delta*60.0) );
423 shader_blitblur_uInverseRatio( inverse );
424
425 inverse[0] -= 0.0001f;
426 inverse[1] -= 0.0001f;
427 shader_blitblur_uClampUv( inverse );
428
429 static f64 effect_time = 133.0;
430 effect_time += vg.time_frame_delta;
431 shader_blitblur_uTime( effect_time );
432
433 f32 glitch_strength = 0.0f;
434 if( skaterift.activity == k_skaterift_replay ){
435 glitch_strength = 0.005f;
436 if( skaterift.replay_control == k_replay_control_play )
437 glitch_strength = 0.005f;
438 else
439 glitch_strength += 0.005f*skaterift.track_velocity;
440 }
441 shader_blitblur_uGlitchStrength( glitch_strength );
442
443 v2f override;
444 if( skaterift.activity == k_skaterift_menu )
445 v2_muls( (v2f){ 0.04f, 0.001f }, 1.0f-skaterift.time_rate, override );
446 else
447 v2_zero( override );
448 shader_blitblur_uOverrideDir( override );
449
450 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
451 render_fb_bind_texture( gpipeline.fb_main, 1, 1 );
452 }
453 else{
454 shader_blit_use();
455 shader_blit_uTexMain( 0 );
456 shader_blit_uInverseRatio( inverse );
457 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
458 }
459
460 render_fsquad();
461 }
462
463 static void render_player_transparent(void){
464 static camera small_cam; /* DOES NOT NEED TO BE STATIC BUT MINGW
465 SAIS OTHERWISE */
466
467 m4x3_copy( skaterift.cam.transform, small_cam.transform );
468
469 small_cam.fov = skaterift.cam.fov;
470 small_cam.nearz = 0.05f;
471 small_cam.farz = 60.0f;
472
473 camera_update_view( &small_cam );
474 camera_update_projection( &small_cam );
475 camera_finalize( &small_cam );
476
477 /* Draw player to window buffer and blend background ontop */
478 player__render( &small_cam );
479 render_remote_players_tags( localplayer.viewable_world, &skaterift.cam );
480 }
481
482 static void render_scene(void){
483 /* Draw world */
484 glEnable( GL_DEPTH_TEST );
485
486 world_instance *view_world = localplayer.viewable_world;
487
488 if( view_world == NULL ){
489 glClearColor( 0.25f, 0.25f, 0.0f, 1.0f );
490 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT );
491 return;
492 }
493
494 for( u32 i=0; i<vg_list_size(world_static.instances); i++ ){
495 if( world_static.instances[i].status == k_world_status_loaded ){
496 world_prerender( &world_static.instances[i] );
497 }
498 }
499
500 if( skaterift.activity == k_skaterift_respawning ){
501 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
502 glClearColor( 0.624f, 0.659f, 0.769f, 0.0f );
503 glClear( GL_COLOR_BUFFER_BIT );
504 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0,
505 GL_COLOR_ATTACHMENT1 } );
506 view_world = &world_static.instances[respawn_chooser.world_id];
507 render_world_override( view_world );
508 render_world_routes( view_world, &skaterift.cam, 1 );
509 }
510 else{
511 render_world( view_world, &skaterift.cam, 0 );
512 render_water_texture( view_world, &skaterift.cam, 0 );
513 render_fb_bind( gpipeline.fb_main, 1 );
514 render_water_surface( view_world, &skaterift.cam );
515 render_remote_players( view_world, &skaterift.cam );
516 }
517 }
518
519 static void render_scene_gate_subview(void){
520 render_fb_bind( gpipeline.fb_main, 1 );
521 world_instance *view_world = localplayer.viewable_world;
522 if( !view_world ) return; /* ??? */
523 if( skaterift.activity == k_skaterift_respawning ) return;
524
525 int depth = 1;
526 if( localplayer.gate_waiting ) depth = 0;
527 render_world_gates( view_world, &skaterift.cam, depth );
528 }
529
530 static void skaterift_composite_maincamera(void){
531 camera_lerp( &localplayer.cam, &world_static.focus_cam,
532 vg_smoothstepf(world_static.focus_strength), &skaterift.cam );
533
534 if( skaterift.activity == k_skaterift_replay ){
535 skaterift_get_replay_camera( &skaterift.cam );
536 }
537
538 skaterift.cam.nearz = 0.1f;
539 skaterift.cam.farz = 2100.0f;
540
541 if( skaterift.activity == k_skaterift_respawning ){
542 camera_copy( &respawn_chooser.cam, &skaterift.cam );
543 skaterift.cam.nearz = 4.0f;
544 skaterift.cam.farz = 3100.0f;
545 }
546
547 camera_update_transform( &skaterift.cam );
548
549 if( skaterift.activity != k_skaterift_respawning ){
550 if( localplayer.gate_waiting ){
551 m3x3_mul( localplayer.basis_gate, skaterift.cam.transform,
552 skaterift.cam.transform );
553 }
554 else{
555 m3x3_mul( localplayer.basis, skaterift.cam.transform,
556 skaterift.cam.transform );
557 }
558 }
559
560 camera_update_view( &skaterift.cam );
561 camera_update_projection( &skaterift.cam );
562 camera_finalize( &skaterift.cam );
563 }
564
565 static void render_main_game(void){
566 if( skaterift.activity == k_skaterift_replay ){
567 player__animate_from_replay( &skaterift.replay );
568 }
569 else{
570 player__animate();
571 skaterift_record_frame( &skaterift.replay, 0 );
572 }
573 animate_remote_players();
574 player__pre_render();
575
576 skaterift_composite_maincamera();
577
578 /* --------------------------------------------------------------------- */
579
580 world_instance *view_world = localplayer.viewable_world;
581 if( (view_world != NULL) && (skaterift.activity != k_skaterift_respawning) ){
582 render_world_cubemaps( view_world );
583
584 ent_gate *gate = view_world->rendering_gate;
585 if( gate ){
586 if( gate->flags & k_ent_gate_nonlocal ){
587 world_instance *dest = &world_static.instances[ gate->target ];
588 render_world_cubemaps( dest );
589 }
590 }
591 }
592
593 /* variable res target */
594 render_fb_bind( gpipeline.fb_main, 1 );
595 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
596 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT );
597
598 render_scene();
599 glEnable( GL_DEPTH_TEST );
600
601 /* full res target */
602 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
603 glViewport( 0,0, vg.window_x, vg.window_y );
604
605 render_player_transparent(); /* needs to read the depth buffer before we fuck
606 it up with the oblique rendering inside the
607 portals */
608
609 /* continue with variable rate */
610 render_scene_gate_subview();
611
612 /* composite */
613 present_view_with_post_processing();
614
615 /* --------------------------------------------------------------------- */
616
617 /* capture the current resume frame at the very last point */
618 if( button_down( k_srbind_reset ) ){
619 if( skaterift.activity == k_skaterift_default ){
620 skaterift.activity = k_skaterift_replay;
621 skaterift_record_frame( &skaterift.replay, 1 );
622 if( skaterift.replay.head ){
623 skaterift.replay.cursor = skaterift.replay.head->time;
624 skaterift.replay.cursor_frame = skaterift.replay.head;
625 }
626 skaterift.replay_control = k_replay_control_scrub;
627 }
628 }
629 }
630
631 static void vg_render(void){
632 if( k_tools_mode ){
633 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
634 glViewport( 0,0, vg.window_x, vg.window_y );
635 glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
636 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
637 return;
638 }
639
640 if( skaterift.op == k_async_op_clientloading ){
641 _vg_loader_render();
642 return;
643 }
644
645 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
646
647 glViewport( 0,0, vg.window_x, vg.window_y );
648 glDisable( GL_DEPTH_TEST );
649 glDisable( GL_BLEND );
650
651 glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
652 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
653
654 render_main_game();
655 m4x4_copy( skaterift.cam.mtx.pv, vg.pv );
656
657 /* Other shite */
658 glDisable(GL_BLEND);
659 glDisable(GL_DEPTH_TEST);
660 vg_lines_drawall();
661 glViewport( 0,0, vg.window_x, vg.window_y );
662 gui_draw();
663 }
664
665 static void vg_gui(void){
666 if( skaterift.op == k_async_op_clientloading ) return;
667
668 menu_update();
669 if( skaterift.activity == k_skaterift_menu ){
670 glClear( GL_DEPTH_BUFFER_BIT );
671 menu_render();
672 }
673
674 player__im_gui();
675 world_instance *world = world_current_instance();
676
677 skaterift_replay_imgui();
678 workshop_form_gui();
679 render_view_framebuffer_ui();
680 remote_player_network_imgui( vg.pv );
681 remote_players_imgui();
682 }
683
684
685 #else
686
687 #include "skaterift_imgui_dev.c"
688
689 #endif