dusting
[carveJwlIkooP6JGAAIwe30JlM.git] / world.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #include "common.h"
6
7 #ifndef WORLD_H
8 #define WORLD_H
9
10 typedef struct world_instance world_instance;
11
12 #include "vg/vg_loader.h"
13
14 #include "network.h"
15 #include "network_msg.h"
16 #include "scene.h"
17 #include "render.h"
18 #include "rigidbody.h"
19 #include "bvh.h"
20 #include "model.h"
21 #include "entity.h"
22 #include "font.h"
23
24 #include "shaders/scene_standard.h"
25 #include "shaders/scene_standard_alphatest.h"
26 #include "shaders/scene_vertex_blend.h"
27 #include "shaders/scene_terrain.h"
28 #include "shaders/scene_depth.h"
29 #include "shaders/scene_position.h"
30
31 #include "shaders/model_sky.h"
32
33 enum { k_max_ui_segments = 8 };
34
35 enum { k_max_ui_elements = k_max_ui_segments };
36 enum { k_max_element_verts = 10 };
37 enum { k_max_element_indices = 20 };
38
39 enum { k_route_ui_max_verts = k_max_ui_elements*k_max_element_verts };
40 enum { k_route_ui_max_indices = k_max_ui_elements*k_max_element_indices };
41
42 enum logic_type
43 {
44 k_logic_type_relay = 1,
45 k_logic_type_chance = 2,
46 k_logic_type_achievement = 3
47 };
48
49 enum geo_type
50 {
51 k_geo_type_solid = 0,
52 k_geo_type_nonsolid = 1,
53 k_geo_type_water = 2
54 };
55
56 static const float k_light_cube_size = 8.0f;
57
58 struct world_instance {
59 /* Fixed items
60 * -------------------------------------------------------
61 */
62
63 char world_name[ 64 ];
64
65 struct{
66 boxf depthbounds;
67 int depth_computed;
68
69 float height;
70 int enabled;
71 v4f plane;
72 }
73 water;
74
75 /* STD140 */
76 struct ub_world_lighting{
77 v4f g_cube_min,
78 g_cube_inv_range;
79
80 v4f g_water_plane,
81 g_depth_bounds;
82
83 v4f g_daysky_colour;
84 v4f g_nightsky_colour;
85 v4f g_sunset_colour;
86 v4f g_ambient_colour;
87 v4f g_sunset_ambient;
88 v4f g_sun_colour;
89 v4f g_sun_dir;
90 v4f g_board_0;
91 v4f g_board_1;
92
93 float g_water_fog;
94 float g_time;
95 float g_realtime;
96 float g_shadow_length;
97 float g_shadow_spread;
98
99 float g_time_of_day;
100 float g_day_phase;
101 float g_sunset_phase;
102
103 int g_light_preview;
104 int g_shadow_samples;
105
106 int g_debug_indices;
107 int g_debug_complexity;
108 }
109 ub_lighting;
110 GLuint ubo_lighting;
111 int ubo_bind_point;
112
113 GLuint tbo_light_entities,
114 tex_light_entities,
115 tex_light_cubes;
116
117 float probabilities[3];
118
119 v3i light_cubes;
120
121 struct framebuffer heightmap;
122
123 /*
124 * Dynamically allocated when world_load is called.
125 *
126 * the following arrays index somewhere into this linear
127 * allocator
128 *
129 * (world_gen.h)
130 * --------------------------------------------------------------------------
131 */
132
133 /*
134 * Main world .mdl
135 */
136 mdl_context meta;
137
138 GLuint *textures;
139 u32 texture_count;
140
141 struct world_surface{
142 mdl_material info;
143 mdl_submesh sm_geo,
144 sm_no_collide;
145 }
146 * surfaces;
147 u32 surface_count;
148
149 mdl_array_ptr ent_spawn,
150 ent_gate,
151 ent_light,
152 ent_route_node,
153 ent_path_index,
154 ent_checkpoint,
155 ent_route,
156 ent_water,
157
158 ent_audio_clip,
159 ent_audio,
160 ent_volume;
161
162 ent_gate *rendering_gate;
163
164 /* logic
165 * ----------------------------------------------------
166 */
167
168 /* world geometry */
169 scene *scene_geo,
170 *scene_no_collide,
171 *scene_lines;
172
173 /* spacial mappings */
174 bh_tree *audio_bh,
175 *volume_bh,
176 *geo_bh;
177
178 /* graphics */
179 glmesh mesh_route_lines;
180 glmesh mesh_geo,
181 mesh_no_collide,
182 mesh_water;
183
184 rb_object rb_geo;
185 };
186
187 VG_STATIC struct world_global{
188 /*
189 * Allocated as system memory
190 * --------------------------------------------------------------------------
191 */
192 void *generic_heap;
193
194 /* rendering */
195 glmesh skydome;
196 glmesh mesh_gate;
197 mdl_submesh sm_gate_surface,
198 sm_gate_marker[4];
199
200 double sky_time, sky_rate, sky_target_rate;
201
202 u32 current_run_version;
203 double time, rewind_from, rewind_to, last_use;
204
205 /* water rendering */
206 struct{
207 struct framebuffer fbreflect, fbdepth;
208 }
209 water;
210
211 /* split flap display */
212 struct{
213 glmesh mesh_base, mesh_display;
214 mdl_submesh sm_base;
215 u32 active_route_board;
216
217 u32 w, h;
218 float *buffer;
219 }
220 sfd;
221
222 v3f render_gate_pos;
223 int in_volume;
224
225 int switching_to_new_world;
226
227 world_instance worlds[4];
228 u32 world_count;
229 u32 active_world;
230
231 /* text particles */
232 font3d font;
233
234 struct timer_text{
235 char text[8];
236 m4x3f transform;
237 ent_gate *gate;
238 ent_route *route;
239 }
240 timer_texts[4];
241 u32 timer_text_count;
242
243 struct text_particle{
244 rb_object obj;
245 m4x3f mlocal;
246 ent_glyph *glyph;
247 v4f colour;
248
249 m4x3f mdl;
250 }
251 text_particles[6*4];
252 u32 text_particle_count;
253
254 }
255 world_global;
256
257 VG_STATIC world_instance *get_active_world( void )
258 {
259 return &world_global.worlds[ world_global.active_world ];
260 }
261
262 /*
263 * API
264 */
265
266 VG_STATIC
267 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
268
269 VG_STATIC
270 struct world_surface *ray_hit_surface( world_instance *world, ray_hit *hit );
271
272 VG_STATIC
273 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
274
275 VG_STATIC
276 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
277
278 /*
279 * Submodules
280 */
281
282 VG_STATIC float
283 k_day_length = 30.0f; /* minutes */
284
285 VG_STATIC int k_debug_light_indices = 0,
286 k_debug_light_complexity = 0,
287 k_light_preview = 0;
288
289 #include "world_routes.h"
290 #include "world_sfd.h"
291 #include "world_render.h"
292 #include "world_water.h"
293 #include "world_volumes.h"
294 #include "world_gen.h"
295 #include "world_gate.h"
296
297 /*
298 * -----------------------------------------------------------------------------
299 * Events
300 * -----------------------------------------------------------------------------
301 */
302
303 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
304 {
305 world_instance *world = get_active_world();
306 return 0;
307 }
308
309 VG_STATIC void world_init(void)
310 {
311 VG_VAR_F32( k_day_length );
312 VG_VAR_I32( k_debug_light_indices );
313 VG_VAR_I32( k_debug_light_complexity );
314 VG_VAR_I32( k_light_preview );
315
316 world_global.sky_rate = 1.0;
317 world_global.sky_target_rate = 1.0;
318
319 shader_scene_standard_register();
320 shader_scene_standard_alphatest_register();
321 shader_scene_vertex_blend_register();
322 shader_scene_terrain_register();
323 shader_scene_depth_register();
324 shader_scene_position_register();
325
326 shader_model_sky_register();
327
328 vg_info( "Loading world resources\n" );
329
330 vg_linear_clear( vg_mem.scratch );
331
332 mdl_context msky;
333 mdl_open( &msky, "models/rs_skydome.mdl", vg_mem.scratch );
334 mdl_load_metadata_block( &msky, vg_mem.scratch );
335 mdl_load_mesh_block( &msky, vg_mem.scratch );
336 mdl_close( &msky );
337
338 vg_acquire_thread_sync();
339 {
340 mdl_unpack_glmesh( &msky, &world_global.skydome );
341 }
342 vg_release_thread_sync();
343
344 /* Other systems */
345 vg_info( "Loading other world systems\n" );
346
347 vg_loader_step( world_render_init, NULL );
348 vg_loader_step( world_sfd_init, NULL );
349 vg_loader_step( world_water_init, NULL );
350 vg_loader_step( world_gates_init, NULL );
351 vg_loader_step( world_routes_init, NULL );
352
353 /* Allocate dynamic world memory arena */
354 u32 max_size = 76*1024*1024;
355 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
356 max_size,
357 VG_MEMORY_SYSTEM );
358 }
359
360 typedef struct ent_call ent_call;
361 struct ent_call{
362 ent_index ent;
363 u32 function;
364 void *data;
365 };
366
367 VG_STATIC void entity_call( world_instance *world, ent_call *call );
368
369 VG_STATIC void ent_volume_call( world_instance *world, ent_call *call )
370 {
371 ent_volume *volume = mdl_arritm( &world->ent_volume, call->ent.index );
372 if( !volume->target.type ) return;
373
374 if( call->function == k_ent_function_trigger ){
375 call->ent = volume->target;
376
377 if( volume->type == k_volume_subtype_particle ){
378 float *co = alloca( sizeof(float)*3 );
379 co[0] = vg_randf()*2.0f-1.0f;
380 co[1] = vg_randf()*2.0f-1.0f;
381 co[2] = vg_randf()*2.0f-1.0f;
382 m4x3_mulv( volume->to_world, co, co );
383
384 call->function = k_ent_function_particle_spawn;
385 call->data = co;
386 entity_call( world, call );
387 }
388 else
389 entity_call( world, call );
390 }
391 }
392
393 VG_STATIC void ent_audio_call( world_instance *world, ent_call *call )
394 {
395 ent_audio *audio = mdl_arritm( &world->ent_audio, call->ent.index );
396
397 v3f sound_co;
398
399 if( call->function == k_ent_function_particle_spawn ){
400 v3_copy( call->data, sound_co );
401 }
402 else if( call->function == k_ent_function_trigger ){
403 v3_copy( audio->transform.co, sound_co );
404 }
405 else
406 vg_fatal_exit_loop( "ent_audio_call (invalid function id)" );
407
408 float chance = vg_randf()*100.0f,
409 bar = 0.0f;
410
411 for( u32 i=0; i<audio->clip_count; i++ ){
412 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
413 audio->clip_start+i );
414
415 float mod = world->probabilities[ audio->probability_curve ],
416 p = clip->probability * mod;
417
418 bar += p;
419
420 if( chance < bar ){
421
422 audio_lock();
423
424 if( audio->behaviour == k_channel_behaviour_unlimited ){
425 audio_oneshot_3d( &clip->clip, sound_co,
426 audio->transform.s[0],
427 audio->volume );
428 }
429 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
430 audio_channel *ch =
431 audio_get_group_idle_channel( audio->group,
432 audio->max_channels );
433
434 if( ch ){
435 audio_channel_init( ch, &clip->clip, audio->flags );
436 audio_channel_group( ch, audio->group );
437 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
438 audio_channel_edit_volume( ch, audio->volume, 1 );
439 ch = audio_relinquish_channel( ch );
440 }
441 }
442 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
443 audio_channel *ch =
444 audio_get_group_idle_channel( audio->group,
445 audio->max_channels );
446
447 /* group is full */
448 if( !ch ){
449 audio_channel *existing =
450 audio_get_group_first_active_channel( audio->group );
451
452 if( existing ){
453 if( existing->source == &clip->clip ){
454 audio_unlock();
455 return;
456 }
457
458 existing->group = 0;
459 existing = audio_channel_fadeout(existing, audio->crossfade);
460 }
461
462 ch = audio_get_first_idle_channel();
463 }
464
465 if( ch ){
466 audio_channel_init( ch, &clip->clip, audio->flags );
467 audio_channel_group( ch, audio->group );
468 audio_channel_fadein( ch, audio->crossfade );
469 ch = audio_relinquish_channel( ch );
470 }
471 }
472
473 audio_unlock();
474 return;
475 }
476 }
477 }
478
479 VG_STATIC void entity_call( world_instance *world, ent_call *call )
480 {
481 if( call->ent.type == k_ent_volume ){
482 ent_volume_call( world, call );
483 } else if( call->ent.type == k_ent_audio ){
484 ent_audio_call( world, call );
485 }
486 }
487
488 VG_STATIC void world_update( world_instance *world, v3f pos )
489 {
490 world_global.sky_time += world_global.sky_rate * vg.time_delta;
491 world_global.sky_rate = vg_lerp( world_global.sky_rate,
492 world_global.sky_target_rate,
493 vg.time_delta * 5.0 );
494
495 world_routes_update_timer_texts( world );
496 world_routes_update( world );
497 //world_routes_debug( world );
498
499 /* ---- SFD ------------ */
500
501 if( mdl_arrcount( &world->ent_route ) ){
502 u32 closest = 0;
503 float min_dist = INFINITY;
504
505 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
506 ent_route *route = mdl_arritm( &world->ent_route, i );
507 float dist = v3_dist2( route->board_transform[3], pos );
508
509 if( dist < min_dist ){
510 min_dist = dist;
511 closest = i;
512 }
513 }
514
515 if( (world_global.sfd.active_route_board != closest)
516 || network_scores_updated )
517 {
518 network_scores_updated = 0;
519 world_global.sfd.active_route_board = closest;
520
521 ent_route *route = mdl_arritm( &world->ent_route, closest );
522 u32 id = route->official_track_id;
523
524 if( id != 0xffffffff ){
525 struct netmsg_board *local_board =
526 &scoreboard_client_data.boards[id];
527
528 for( int i=0; i<13; i++ ){
529 sfd_encode( i, &local_board->data[27*i] );
530 }
531 }else{
532 sfd_encode( 0, mdl_pstr( &world->meta, route->pstr_name ) );
533 sfd_encode( 1, "No data" );
534 }
535 }
536 }
537 sfd_update();
538
539 static float random_accum = 0.0f;
540 random_accum += vg.time_delta;
541
542 u32 random_ticks = 0;
543
544 while( random_accum > 0.1f ){
545 random_accum -= 0.1f;
546 random_ticks ++;
547 }
548
549 float radius = 25.0f;
550 boxf volume_proximity;
551 v3_add( pos, (v3f){ radius, radius, radius }, volume_proximity[1] );
552 v3_sub( pos, (v3f){ radius, radius, radius }, volume_proximity[0] );
553
554 bh_iter it;
555 bh_iter_init( 0, &it );
556 int idx;
557
558 int in_volume = 0;
559
560 while( bh_next( world->volume_bh, &it, volume_proximity, &idx ) ){
561 ent_volume *volume = mdl_arritm( &world->ent_volume, idx );
562
563 boxf cube = {{-1.0f,-1.0f,-1.0f},{1.0f,1.0f,1.0f}};
564
565 if( volume->type == k_volume_subtype_trigger ){
566 v3f local;
567 m4x3_mulv( volume->to_local, pos, local );
568
569 if( (fabsf(local[0]) <= 1.0f) &&
570 (fabsf(local[1]) <= 1.0f) &&
571 (fabsf(local[2]) <= 1.0f) )
572 {
573 in_volume = 1;
574 vg_line_boxf_transformed( volume->to_world, cube, 0xff00ff00 );
575
576 if( !world_global.in_volume ){
577 ent_call basecall;
578 basecall.ent.index = idx;
579 basecall.ent.type = k_ent_volume;
580 basecall.function = k_ent_function_trigger;
581 basecall.data = NULL;
582
583 entity_call( world, &basecall );
584 }
585 }
586 else
587 vg_line_boxf_transformed( volume->to_world, cube, 0xff0000ff );
588 }
589 else if( volume->type == k_volume_subtype_particle ){
590 vg_line_boxf_transformed( volume->to_world, cube, 0xff00c0ff );
591
592 for( int j=0; j<random_ticks; j++ ){
593 ent_call basecall;
594 basecall.ent.index = idx;
595 basecall.ent.type = k_ent_volume;
596 basecall.function = k_ent_function_trigger;
597 basecall.data = NULL;
598
599 entity_call( world, &basecall );
600 }
601 }
602 }
603 world_global.in_volume = in_volume;
604
605 #if 0
606 if( k_debug_light_indices )
607 {
608 for( int i=0; i<world->light_count; i++ ){
609 struct world_light *light = &world->lights[i];
610 struct classtype_world_light *inf = light->inf;
611
612 u32 colour = 0xff000000;
613 u8 r = inf->colour[0] * 255.0f,
614 g = inf->colour[1] * 255.0f,
615 b = inf->colour[2] * 255.0f;
616
617 colour |= r;
618 colour |= g << 8;
619 colour |= b << 16;
620
621 vg_line_pt3( light->node->co, 0.25f, colour );
622 }
623 }
624
625 #endif
626
627 #if 0
628
629 /* process soundscape transactions */
630 audio_lock();
631 for( int i=0; i<world->soundscape_count; i++ )
632 {
633 struct soundscape *s = &world->soundscapes[i];
634 s->usage_count = 0;
635
636 for( int j=0; j<s->max_instances; j++ )
637 {
638 if( s->channels[j] )
639 {
640 if( audio_channel_finished(s->channels[j]) )
641 s->channels[j] = audio_relinquish_channel( s->channels[j] );
642 else
643 s->usage_count ++;
644 }
645 }
646 }
647 audio_unlock();
648 #endif
649 }
650
651 /*
652 * -----------------------------------------------------------------------------
653 * API implementation
654 * -----------------------------------------------------------------------------
655 */
656
657 VG_STATIC void ray_world_get_tri( world_instance *world,
658 ray_hit *hit, v3f tri[3] )
659 {
660 for( int i=0; i<3; i++ )
661 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
662 }
663
664 VG_STATIC int ray_world( world_instance *world,
665 v3f pos, v3f dir, ray_hit *hit )
666 {
667 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
668 }
669
670 /*
671 * Cast a sphere from a to b and see what time it hits
672 */
673 VG_STATIC int spherecast_world( world_instance *world,
674 v3f pa, v3f pb, float r, float *t, v3f n )
675 {
676 bh_iter it;
677 bh_iter_init( 0, &it );
678
679 boxf region;
680 box_init_inf( region );
681 box_addpt( region, pa );
682 box_addpt( region, pb );
683
684 v3_add( (v3f){ r, r, r}, region[1], region[1] );
685 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
686
687 v3f dir;
688 v3_sub( pb, pa, dir );
689
690 v3f dir_inv;
691 dir_inv[0] = 1.0f/dir[0];
692 dir_inv[1] = 1.0f/dir[1];
693 dir_inv[2] = 1.0f/dir[2];
694
695 int hit = -1;
696 float min_t = 1.0f;
697
698 int idx;
699 while( bh_next( world->geo_bh, &it, region, &idx ) ){
700 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
701 v3f tri[3];
702
703 boxf box;
704 box_init_inf( box );
705
706 for( int j=0; j<3; j++ ){
707 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
708 box_addpt( box, tri[j] );
709 }
710
711 v3_add( (v3f){ r, r, r}, box[1], box[1] );
712 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
713
714 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
715 continue;
716
717 float t;
718 v3f n1;
719 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) ){
720 if( t < min_t ){
721 min_t = t;
722 hit = idx;
723 v3_copy( n1, n );
724 }
725 }
726 }
727
728 *t = min_t;
729 return hit;
730 }
731
732 VG_STATIC
733 struct world_surface *world_tri_index_surface( world_instance *world,
734 u32 index )
735 {
736 for( int i=1; i<world->surface_count; i++ ){
737 struct world_surface *surf = &world->surfaces[i];
738
739 if( (index >= surf->sm_geo.vertex_start) &&
740 (index < surf->sm_geo.vertex_start+surf->sm_geo.vertex_count ) )
741 {
742 return surf;
743 }
744 }
745
746 return &world->surfaces[0];
747 }
748
749 VG_STATIC struct world_surface *world_contact_surface( world_instance *world,
750 rb_ct *ct )
751 {
752 return world_tri_index_surface( world, ct->element_id );
753 }
754
755 VG_STATIC struct world_surface *ray_hit_surface( world_instance *world,
756 ray_hit *hit )
757 {
758 return world_tri_index_surface( world, hit->tri[0] );
759 }
760
761 #endif /* WORLD_H */