semantics and world reloading
[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 VG_DEVWINDOW
18
19 #include "common.h"
20 #include "conf.h"
21 #include "steam.h"
22 #include "render.h"
23 #include "audio.h"
24 #include "world.h"
25 #include "font.h"
26 #include "player.h"
27
28 #include "entity.c"
29 #include "workshop.c"
30
31 #include "network.h"
32 #include "menu.h"
33 #include "vehicle.h"
34
35 static struct player_avatar localplayer_avatar;
36 static struct player_model localplayer_models[3];
37
38
39 int main( int argc, char *argv[] )
40 {
41 vg_mem.use_libc_malloc = 0;
42 vg_set_mem_quota( 160*1024*1024 );
43 vg_enter( argc, argv, "Voyager Game Engine" );
44 return 0;
45 }
46
47 VG_STATIC void highscores_save_at_exit(void)
48 {
49 highscores_serialize_all();
50 }
51
52 VG_STATIC void vg_launch_opt(void)
53 {
54 }
55
56 VG_STATIC void vg_preload(void)
57 {
58 g_conf_init();
59
60 vg_info(" Copyright . . . -----, ,----- ,---. .---. \n" );
61 vg_info(" 2021-2023 |\\ /| | / | | | | /| \n" );
62 vg_info(" | \\ / | +-- / +----- +---' | / | \n" );
63 vg_info(" | \\ / | | / | | \\ | / | \n" );
64 vg_info(" | \\/ | | / | | \\ | / | \n" );
65 vg_info(" ' ' '--' [] '----- '----- ' ' '---' "
66 "SOFTWARE\n" );
67
68 highscores_init( 2000, 50 );
69 if( !highscores_read() )
70 highscores_create_db();
71
72 vg_loader_step( NULL, highscores_save_at_exit );
73
74 steam_init();
75 vg_loader_step( NULL, steam_end );
76 vg_loader_step( network_init, network_end );
77 }
78
79 VG_STATIC void load_playermodels(void)
80 {
81 player_model_load( &localplayer_models[0], "models/ch_new.mdl" );
82 player_model_load( &localplayer_models[1], "models/ch_outlaw.mdl" );
83 player_model_load( &localplayer_models[2], "models/ch_jordan.mdl" );
84
85 /* FIXME: hack */
86 shader_model_character_view_register();
87 shader_model_board_view_register();
88 shader_model_entity_register();
89 }
90
91 void temp_update_playermodel(void){
92 player__use_model( &localplayer, &localplayer_models[cl_playermdl_id] );
93 }
94
95 VG_STATIC void async_skaterift_complete( void *payload, u32 size )
96 {
97 localplayer.viewable_world = get_active_world();
98 localplayer_cmd_respawn( 1, (const char *[]){ "start" } );
99
100 skaterift_end_op();
101 }
102
103 VG_STATIC void vg_load(void)
104 {
105 vg_console_reg_cmd( "changeworld", skaterift_change_world_command, NULL );
106
107 vg_loader_step( render_init, NULL );
108 vg_loader_step( menu_init, NULL );
109 vg_loader_step( world_init, NULL );
110 vg_loader_step( vehicle_init, NULL );
111 vg_loader_step( font3d_init, NULL );
112
113 font3d_load( &world_global.font, "models/rs_font.mdl", vg_mem.rtmemory );
114
115 vg_loader_step( player_init, NULL );
116 vg_loader_step( player_ragdoll_init, NULL );
117 vg_loader_step( workshop_init, NULL );
118 vg_loader_step( skateshop_init, NULL );
119
120 /* ----------------- */
121 vg_loader_step( load_playermodels, NULL );
122
123 /* player setup */
124 player__create( &localplayer );
125 player_avatar_load( &localplayer_avatar, "models/ch_new.mdl" );
126 player__use_avatar( &localplayer, &localplayer_avatar );
127 player__use_model( &localplayer, &localplayer_models[cl_playermdl_id] );
128 player__bind( &localplayer );
129
130 /* --------------------- */
131
132 vg_bake_shaders();
133 vg_loader_step( audio_init, audio_free );
134
135 /* 'systems' are completely loaded now */
136 /* load home/permanent world */
137 world_load( 0, "maps/mp_spawn.mdl" );
138
139 vg_console_load_autos();
140 menu_link();
141
142 vg_async_call( async_skaterift_complete, NULL, 0 );
143 }
144
145 VG_STATIC void draw_origin_axis(void)
146 {
147 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 1.0f, 0.0f, 0.0f }, 0xffff0000 );
148 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 1.0f, 0.0f }, 0xff00ff00 );
149 vg_line( (v3f){ 0.0f, 0.0f, 0.0f }, (v3f){ 0.0f, 0.0f, 1.0f }, 0xff0000ff );
150 }
151
152 static void skaterift_change_world_preupdate(void);
153 VG_STATIC void vg_update(void)
154 {
155 steam_update();
156 skaterift_preupdate_inputs();
157 if( skaterift.async_op == k_async_op_clientloading ) return;
158 if( skaterift.async_op == k_async_op_world_preloading ){
159 skaterift_change_world_preupdate();
160 }
161
162 draw_origin_axis();
163 network_update();
164
165 player__pre_update( &localplayer );
166 global_skateshop_preupdate();
167
168 world_update( get_active_world(), localplayer.rb.co );
169 audio_ambient_sprites_update( get_active_world(), localplayer.rb.co );
170 //gui_helper_action( localplayer.input_use, "\x7f Hello \x1f""A \x1e\x84" );
171 }
172
173 VG_STATIC void vg_update_fixed(void)
174 {
175 if( skaterift.async_op == k_async_op_clientloading ) return;
176
177 world_routes_fixedupdate( get_active_world() );
178 player__update( &localplayer );
179 vehicle_update_fixed();
180 }
181
182 VG_STATIC void vg_update_post(void)
183 {
184 if( skaterift.async_op == k_async_op_clientloading ) return;
185
186 player__post_update( &localplayer );
187
188 float dist;
189 int sample_index;
190 world_audio_sample_distances( localplayer.rb.co, &sample_index, &dist );
191
192 audio_lock();
193 vg_dsp.echo_distances[sample_index] = dist;
194
195 v3f ears = { 1.0f,0.0f,0.0f };
196 m3x3_mulv( main_camera.transform, ears, ears );
197 v3_copy( ears, vg_audio.external_listener_ears );
198 v3_copy( main_camera.transform[3], vg_audio.external_listener_pos );
199
200 if( localplayer.gate_waiting ){
201 m4x3_mulv( localplayer.gate_waiting->transport,
202 vg_audio.external_listener_pos,
203 vg_audio.external_listener_pos );
204 }
205
206 v3_copy( localplayer.rb.v, vg_audio.external_lister_velocity );
207 audio_unlock();
208
209 vg.time_rate = 1.0f-menu.factive;
210 vehicle_update_post();
211 }
212
213 VG_STATIC void vg_framebuffer_resize( int w, int h )
214 {
215 render_fb_resize();
216 }
217
218 VG_STATIC void present_view_with_post_processing(void)
219 {
220 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
221 glViewport( 0,0, vg.window_x, vg.window_y );
222
223 glEnable(GL_BLEND);
224 glDisable(GL_DEPTH_TEST);
225 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
226 glBlendEquation(GL_FUNC_ADD);
227
228 v2f inverse;
229 render_fb_inverse_ratio( gpipeline.fb_main, inverse );
230
231 if( cl_blur ){
232 shader_blitblur_use();
233 shader_blitblur_uTexMain( 0 );
234 shader_blitblur_uTexMotion( 1 );
235 shader_blitblur_uBlurStrength( cl_blur_strength /
236 (vg.time_frame_delta*60.0) );
237 shader_blitblur_uInverseRatio( inverse );
238
239 v2f menu_blurring;
240 v2_muls( (v2f){ 0.04f, 0.001f }, menu.factive, menu_blurring );
241 shader_blitblur_uOverrideDir( menu_blurring );
242
243 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
244 render_fb_bind_texture( gpipeline.fb_main, 1, 1 );
245 }
246 else{
247 shader_blit_use();
248 shader_blit_uTexMain( 0 );
249 shader_blit_uInverseRatio( inverse );
250 render_fb_bind_texture( gpipeline.fb_main, 0, 0 );
251 }
252
253 render_fsquad();
254 }
255
256 VG_STATIC void render_player_transparent(void)
257 {
258 static camera small_cam; /* DOES NOT NEED TO BE STATIC BUT MINGW
259 SAIS OTHERWISE */
260
261 m4x3_copy( main_camera.transform, small_cam.transform );
262
263 small_cam.fov = main_camera.fov;
264 small_cam.nearz = 0.05f;
265 small_cam.farz = 60.0f;
266
267 camera_update_view( &small_cam );
268 camera_update_projection( &small_cam );
269 camera_finalize( &small_cam );
270
271 /* Draw player to window buffer and blend background ontop */
272 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
273 glViewport( 0,0, vg.window_x, vg.window_y );
274 player__render( &small_cam, &localplayer );
275 }
276
277 VG_STATIC void render_scene(void)
278 {
279 render_fb_bind( gpipeline.fb_main, 1 );
280 glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
281 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT );
282
283 /* Draw world */
284 glEnable( GL_DEPTH_TEST );
285
286 world_instance *view_world = localplayer.viewable_world;
287
288 if( view_world == NULL ){
289 glClearColor( 0.25f, 0.25f, 0.0f, 1.0f );
290 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT );
291 return;
292 }
293
294 for( u32 i=0; i<vg_list_size(world_global.worlds); i++ ){
295 if( world_global.worlds[i].status == k_world_status_loaded ){
296 world_prerender( &world_global.worlds[i] );
297 }
298 }
299
300 render_world( view_world, &main_camera, 0 );
301
302 render_water_texture( view_world, &main_camera, 0 );
303 render_fb_bind( gpipeline.fb_main, 1 );
304 render_water_surface( view_world, &main_camera );
305 }
306
307 VG_STATIC void render_scene_gate_subview(void)
308 {
309 render_fb_bind( gpipeline.fb_main, 1 );
310 world_instance *view_world = localplayer.viewable_world;
311
312 int depth = 1;
313 if( localplayer.gate_waiting ) depth = 0;
314 render_world_gates( view_world, &main_camera, depth );
315 }
316
317 VG_STATIC void render_main_game(void)
318 {
319 player__pre_render( &localplayer );
320 main_camera.fov = localplayer.cam.fov;
321 v3_copy( localplayer.cam.pos, main_camera.pos );
322 v3_copy( localplayer.cam.angles, main_camera.angles );
323
324 main_camera.nearz = 0.1f;
325 main_camera.farz = 2100.0f;
326
327 camera_update_transform( &main_camera );
328
329 if( localplayer.gate_waiting ){
330 m3x3_mul( localplayer.basis_gate, main_camera.transform,
331 main_camera.transform );
332 }
333 else{
334 m3x3_mul( localplayer.basis, main_camera.transform,
335 main_camera.transform );
336 }
337
338 camera_update_view( &main_camera );
339 camera_update_projection( &main_camera );
340 camera_finalize( &main_camera );
341
342 /* ========== Begin Frame ========== */
343
344 render_scene();
345
346 glEnable( GL_DEPTH_TEST );
347
348 render_player_transparent();
349 render_scene_gate_subview();
350
351 present_view_with_post_processing();
352 /* =========== End Frame =========== */
353 }
354
355 VG_STATIC void vg_render(void)
356 {
357 if( skaterift.async_op == k_async_op_clientloading ){
358 _vg_loader_render();
359 return;
360 }
361
362 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
363
364 glViewport( 0,0, vg.window_x, vg.window_y );
365 glDisable( GL_DEPTH_TEST );
366 glDisable( GL_BLEND );
367
368 glClearColor( 1.0f, 0.0f, 0.0f, 0.0f );
369 glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
370
371 render_main_game();
372
373 m4x4_copy( main_camera.mtx.pv, vg.pv );
374
375 /* Other shite */
376 glDisable(GL_BLEND);
377 glDisable(GL_DEPTH_TEST);
378 vg_lines_drawall();
379 glViewport( 0,0, vg.window_x, vg.window_y );
380 gui_draw();
381 }
382
383 VG_STATIC void vg_gui(void)
384 {
385 if( skaterift.async_op == k_async_op_clientloading ) return;
386
387 menu_update();
388 if( menu.active ){
389 glClear( GL_DEPTH_BUFFER_BIT );
390 menu_render();
391 }
392
393 #if 0
394 player__im_gui( &localplayer );
395 #endif
396 world_instance *world = get_active_world();
397
398 workshop_form_gui();
399 render_view_framebuffer_ui();
400 }
401
402 static void async_skaterift_world_loaded( void *payload, u32 size )
403 {
404 skaterift_end_op();
405 }
406
407 static void skaterift_world_changer_thread( void *data )
408 {
409 const char *path = data;
410 world_load( 1, world_global.load_target );
411 vg_async_call( async_skaterift_world_loaded, NULL, 0 );
412 }
413
414 /* holding pattern before we can start loading the new world, since we might be
415 * waiting for audio to stop */
416 static void skaterift_change_world_preupdate(void)
417 {
418 for( u32 i=1; i<vg_list_size(world_global.worlds); i++ ){
419 world_instance *inst = &world_global.worlds[i];
420
421 if( inst->status == k_world_status_unloading ){
422 if( world_freeable( inst ) ){
423 world_free( inst );
424 }
425 return;
426 }
427 }
428
429 vg_info( "worlds cleared, begining load\n" );
430 skaterift_shift_op( k_async_op_world_loading );
431
432 /* finally can start the loader */
433 vg_loader_start( skaterift_world_changer_thread, NULL );
434 }
435
436 /* places all loaded worlds into unloading state */
437 static void skaterift_change_world( const char *world_path )
438 {
439 vg_info( "switching to %s\n", world_path );
440
441 if( world_global.active_world != 0 ){
442 vg_error( "Cannot change worlds while in non-root world\n" );
443 }
444 else{
445 skaterift_begin_op( k_async_op_world_preloading );
446
447 vg_linear_clear( vg_mem.scratch );
448 world_global.load_target = vg_linear_alloc( vg_mem.scratch, 1024 );
449 vg_strncpy( world_path, world_global.load_target,
450 1024, k_strncpy_overflow_fatal );
451
452 vg_info( "unloading old worlds\n" );
453 world_unlink_nonlocal( &world_global.worlds[0] );
454
455 for( u32 i=1; i<vg_list_size(world_global.worlds); i++ ){
456 world_instance *inst = &world_global.worlds[i];
457
458 if( inst->status == k_world_status_loaded ){
459 inst->status = k_world_status_unloading;
460 world_fadeout_audio( inst );
461 }
462 }
463 }
464 }
465
466 static int skaterift_change_world_command( int argc, const char *argv[] )
467 {
468 if( argc == 1 )
469 skaterift_change_world( argv[0] );
470
471 return 0;
472 }
473
474 #else
475
476 #include "skaterift_imgui_dev.c"
477
478 #endif