some audio things
[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
22 #include "shaders/scene_standard.h"
23 #include "shaders/scene_standard_alphatest.h"
24 #include "shaders/scene_vertex_blend.h"
25 #include "shaders/scene_terrain.h"
26 #include "shaders/scene_depth.h"
27 #include "shaders/scene_position.h"
28
29 #include "shaders/model_sky.h"
30
31 typedef struct teleport_gate teleport_gate;
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 {
60 /* This is a small flag we use to changelevel.
61 * It will not be cleared until all sounds stop playing
62 */
63
64 /* Fixed items
65 * -------------------------------------------------------
66 */
67
68 char world_name[ 64 ];
69
70 struct
71 {
72 boxf depthbounds;
73 int depth_computed;
74
75 float height;
76 int enabled;
77 v4f plane;
78 }
79 water;
80
81 /* STD140 */
82 struct ub_world_lighting
83 {
84 v4f g_cube_min,
85 g_cube_inv_range;
86
87 v4f g_water_plane,
88 g_depth_bounds;
89
90 v4f g_daysky_colour;
91 v4f g_nightsky_colour;
92 v4f g_sunset_colour;
93 v4f g_ambient_colour;
94 v4f g_sunset_ambient;
95 v4f g_sun_colour;
96 v4f g_sun_dir;
97
98 float g_water_fog;
99 float g_time;
100 float g_shadow_length;
101 float g_shadow_spread;
102
103 float g_time_of_day;
104 float g_day_phase;
105 float g_sunset_phase;
106
107 int g_light_preview;
108 int g_shadow_samples;
109
110 int g_debug_indices;
111 int g_debug_complexity;
112 }
113 ub_lighting;
114 GLuint ubo_lighting;
115 int ubo_bind_point;
116
117 GLuint tbo_light_entities,
118 tex_light_entities,
119 tex_light_cubes;
120
121 v3i light_cubes;
122
123 struct framebuffer heightmap;
124
125 /*
126 * Dynamically allocated when world_load is called.
127 *
128 * the following arrays index somewhere into this linear
129 * allocator
130 *
131 * (world_gen.h)
132 * --------------------------------------------------------------------------
133 */
134 /*
135 * Main world .mdl
136 */
137 mdl_context *meta;
138
139 /*
140 * Materials / textures
141 */
142
143 GLuint *textures;
144 u32 texture_count;
145
146 struct world_material
147 {
148 mdl_material info;
149 mdl_submesh sm_geo,
150 sm_no_collide;
151 }
152 * materials;
153 u32 material_count;
154
155 /*
156 * Named safe places to respawn
157 */
158 struct respawn_point
159 {
160 v3f co;
161 v4f q;
162 const char *name;
163 }
164 * spawns;
165 u32 spawn_count;
166
167 /*
168 * Audio player entities
169 */
170 struct world_audio_thing
171 {
172 v3f pos;
173 float volume;
174 u32 flags;
175
176 audio_player player;
177 audio_clip temp_embedded_clip;
178 }
179 * audio_things;
180 u32 audio_things_count;
181
182 /*
183 * Relays
184 */
185 struct logic_relay
186 {
187 v3f pos;
188
189 struct relay_target
190 {
191 u32 sub_id;
192 enum classtype classtype;
193 }
194 targets[4];
195 u32 target_count;
196 }
197 * logic_relays;
198 u32 relay_count;
199
200 /*
201 * Box trigger entities
202 */
203 struct trigger_zone
204 {
205 m4x3f transform, inv_transform;
206
207 struct relay_target target;
208 }
209 * triggers;
210 u32 trigger_count;
211
212 /*
213 * Achievements
214 */
215 struct logic_achievement
216 {
217 v3f pos;
218 const char *achievement_id;
219 u32 achieved;
220 }
221 * logic_achievements;
222 u32 achievement_count;
223
224 /*
225 * Lights
226 */
227 struct world_light
228 {
229 mdl_node *node;
230 struct classtype_world_light *inf;
231 m4x3f inverse_world;
232 v2f angle_sin_cos;
233
234 /* enabled.. etc?
235 * TODO: we should order entities in the binary by their type */
236 }
237 * lights;
238 u32 light_count;
239
240 /*
241 * Routes (world_routes.h)
242 * --------------------------------------------------------------------------
243 */
244 struct route_node
245 {
246 v3f co, right, up, h;
247 u32 next[2];
248
249 u32 special_type, special_id, current_refs, ref_count;
250 u32 route_ids[4]; /* Gates can be linked into up to four routes */
251 }
252 *nodes;
253 u32 node_count;
254
255 struct route
256 {
257 u32 track_id;
258 v4f colour;
259
260 u32 start;
261 mdl_submesh sm;
262
263 int active;
264 float factive;
265
266 double best_lap, latest_pass; /* Session */
267
268 m4x3f scoreboard_transform;
269 }
270 *routes;
271 u32 route_count;
272
273 struct route_gate
274 {
275 struct teleport_gate
276 {
277 v3f co[2];
278 v4f q[2];
279 v2f dims;
280
281 m4x3f to_world, transport;
282 }
283 gate;
284
285 u32 node_id;
286
287 struct route_timing
288 {
289 u32 version; /* Incremented on every teleport */
290 double time;
291 }
292 timing;
293 }
294 *gates;
295 u32 gate_count;
296
297 struct nonlocal_gate
298 {
299 struct teleport_gate gate;
300 mdl_node *node;
301
302 u32 target_map_index, working;
303 }
304 *nonlocal_gates;
305 u32 nonlocalgate_count;
306
307 struct route_collector
308 {
309 struct route_timing timing;
310 }
311 *collectors;
312 u32 collector_count;
313
314
315 /* logic
316 * ----------------------------------------------------
317 */
318
319 /* world geometry */
320 scene *scene_geo,
321 *scene_no_collide,
322 *scene_lines;
323
324 /* spacial mappings */
325 bh_tree *audio_bh,
326 *trigger_bh,
327 *geo_bh;
328
329 /* graphics */
330 glmesh mesh_route_lines;
331 glmesh mesh_geo,
332 mesh_no_collide,
333 mesh_water;
334
335 rigidbody rb_geo; /* todo.. ... */
336 };
337
338 VG_STATIC struct world_global
339 {
340 /*
341 * Allocated as system memory
342 * --------------------------------------------------------------------------
343 */
344 void *generic_heap;
345
346 /* rendering */
347 glmesh skydome;
348 mdl_submesh dome_upper, dome_lower;
349
350 glmesh mesh_gate_surface;
351
352 double sky_time, sky_rate, sky_target_rate;
353
354 /* gates, TODO: active_gate should also know which instance */
355 u32 active_gate,
356 current_run_version;
357 double time, rewind_from, rewind_to, last_use;
358
359 /* water rendering */
360 struct
361 {
362 struct framebuffer fbreflect, fbdepth;
363 }
364 water;
365
366 /* split flap display */
367 struct
368 {
369 mdl_submesh *sm_module, *sm_card;
370 glmesh mesh_base, mesh_display;
371
372 u32 w, h;
373 float *buffer;
374 }
375 sfd;
376
377 /* timing bars, fixed maximum amount */
378 struct route_ui_bar
379 {
380 GLuint vao, vbo, ebo;
381
382 u32 indices_head;
383 u32 vertex_head;
384
385 struct route_ui_segment
386 {
387 float length;
388 u32 vertex_start, vertex_count,
389 index_start, index_count, notches;
390 }
391 segments[k_max_ui_segments];
392
393 u32 segment_start, segment_count, fade_start, fade_count;
394 double fade_timer_start;
395 float xpos;
396 }
397 ui_bars[16];
398
399 v3f render_gate_pos;
400 int active_route_board;
401 int in_trigger;
402
403 int switching_to_new_world;
404
405 world_instance worlds[4];
406 u32 world_count;
407 u32 active_world;
408 }
409 world_global;
410
411 VG_STATIC world_instance *get_active_world( void )
412 {
413 return &world_global.worlds[ world_global.active_world ];
414 }
415
416 /*
417 * API
418 */
419
420 VG_STATIC
421 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
422
423 VG_STATIC
424 struct world_material *ray_hit_material( world_instance *world, ray_hit *hit );
425
426 VG_STATIC
427 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
428
429 VG_STATIC
430 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
431
432 /*
433 * Submodules
434 */
435
436 #include "world_routes.h"
437 #include "world_sfd.h"
438 #include "world_render.h"
439 #include "world_water.h"
440 #include "world_gen.h"
441 #include "world_gate.h"
442
443 /*
444 * -----------------------------------------------------------------------------
445 * Events
446 * -----------------------------------------------------------------------------
447 */
448
449 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
450 {
451 world_instance *world = get_active_world();
452
453 /*
454 * None of our world audio runs as one shots, they always have a player.
455 * Therefore it is safe to delete clip data after the players are
456 * disconnected
457 */
458 audio_lock();
459 for( int i=0; i<world->audio_things_count; i++ )
460 {
461 struct world_audio_thing *at = &world->audio_things[i];
462
463 if( audio_player_is_playing( &at->player ) )
464 {
465 u32 cflags = audio_player_get_flags( &at->player );
466 audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
467 }
468 }
469 audio_unlock();
470
471 return 0;
472 }
473
474 VG_STATIC int world_change_world( int argc, const char *argv[] )
475 {
476 #if 0
477 world_instance *world = get_active_world();
478
479 if( argc == 0 )
480 {
481 vg_info( "%s\n", world.world_name );
482 return 0;
483 }
484 else
485 {
486 vg_info( "Switching world...\n" );
487 strcpy( world.world_name, argv[0] );
488 world.switching_to_new_world = 1;
489 world_stop_sound( 0, NULL );
490 }
491 #endif
492
493 return 0;
494 }
495
496 VG_STATIC void world_init(void)
497 {
498 #if 0
499 vg_var_push( (struct vg_var){
500 .name = "water_enable",
501 .data = &world.water.enabled,
502 .data_type = k_var_dtype_i32,
503 .opt_i32 = { .min=0, .max=1, .clamp=1 },
504 .persistent = 0
505 });
506 #endif
507
508 vg_function_push( (struct vg_cmd)
509 {
510 .name = "world_stop_sound",
511 .function = world_stop_sound
512 });
513
514 vg_function_push( (struct vg_cmd)
515 {
516 .name = "world",
517 .function = world_change_world
518 });
519
520 world_global.sky_rate = 1.0;
521 world_global.sky_target_rate = 1.0;
522
523 shader_scene_standard_register();
524 shader_scene_standard_alphatest_register();
525 shader_scene_vertex_blend_register();
526 shader_scene_terrain_register();
527 shader_scene_depth_register();
528 shader_scene_position_register();
529
530 shader_model_sky_register();
531
532 vg_info( "Loading world resources\n" );
533
534 vg_linear_clear( vg_mem.scratch );
535 mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
536
537 mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
538 world_global.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
539
540 vg_acquire_thread_sync();
541 {
542 mdl_unpack_glmesh( msky, &world_global.skydome );
543 }
544 vg_release_thread_sync();
545
546 /* Other systems */
547 vg_info( "Loading other world systems\n" );
548
549 vg_loader_step( world_render_init, NULL );
550 vg_loader_step( world_sfd_init, NULL );
551 vg_loader_step( world_water_init, NULL );
552 vg_loader_step( world_gates_init, NULL );
553 vg_loader_step( world_routes_init, NULL );
554
555 /* Allocate dynamic world memory arena */
556 u32 max_size = 76*1024*1024;
557 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
558 max_size,
559 VG_MEMORY_SYSTEM );
560 }
561
562 VG_STATIC void world_audio_init(void)
563 {
564 #if 0
565 u32 size = vg_linear_remaining( vg_audio.audio_pool )
566 - sizeof(vg_linear_allocator);
567
568 world_global.audio_heap = vg_create_linear_allocator( vg_audio.audio_pool,
569 size,
570 VG_MEMORY_SYSTEM );
571 #endif
572 }
573
574 VG_STATIC void world_trigger_achievement( world_instance *world, u32 uid )
575 {
576 struct logic_achievement *ach = &world->logic_achievements[ uid ];
577
578 if( ach->achieved )
579 return;
580
581 steam_set_achievement( ach->achievement_id );
582 steam_store_achievements();
583
584 ach->achieved = 1;
585 }
586
587 VG_STATIC void world_run_relay( world_instance *world,
588 struct relay_target *rt );
589
590 VG_STATIC void world_trigger_relay( world_instance *world, u32 uid )
591 {
592 struct logic_relay *relay = &world->logic_relays[ uid ];
593
594 for( int i=0; i<relay->target_count; i++ )
595 {
596 world_run_relay( world, &relay->targets[i] );
597 }
598 }
599
600 VG_STATIC void world_trigger_audio( world_instance *world, u32 uid )
601 {
602 struct world_audio_thing *wat = &world->audio_things[ uid ];
603
604 audio_lock();
605 audio_player_playclip( &wat->player,
606 &wat->temp_embedded_clip );
607 audio_unlock();
608 }
609
610 VG_STATIC void world_run_relay( world_instance *world,
611 struct relay_target *rt )
612 {
613 struct entity_instruction
614 {
615 enum classtype classtype;
616 void (*p_trigger)( world_instance *world, u32 uid );
617 }
618 entity_instructions[] =
619 {
620 { k_classtype_logic_achievement, world_trigger_achievement },
621 { k_classtype_logic_relay, world_trigger_relay },
622 { k_classtype_audio, world_trigger_audio }
623 };
624
625 for( int i=0; i<vg_list_size(entity_instructions); i++ )
626 {
627 struct entity_instruction *instr = &entity_instructions[i];
628
629 if( instr->classtype == rt->classtype )
630 {
631 instr->p_trigger( world, rt->sub_id );
632 return;
633 }
634 }
635
636 vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
637 }
638
639 VG_STATIC void world_update( world_instance *world, v3f pos )
640 {
641 /* TEMP!!!!!! */
642 static double g_time = 0.0;
643 g_time += vg.time_delta * (1.0/(k_day_length*60.0));
644
645
646 struct ub_world_lighting *state = &world->ub_lighting;
647
648 state->g_time = g_time;
649 state->g_debug_indices = k_debug_light_indices;
650 state->g_light_preview = k_light_preview;
651 state->g_debug_complexity = k_debug_light_complexity;
652
653 state->g_time_of_day = vg_fractf( g_time );
654 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
655 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
656
657 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
658 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
659
660 float a = state->g_time_of_day * VG_PIf * 2.0f;
661 state->g_sun_dir[0] = sinf( a );
662 state->g_sun_dir[1] = cosf( a );
663 state->g_sun_dir[2] = 0.2f;
664 v3_normalize( state->g_sun_dir );
665
666
667
668 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
669 glBufferSubData( GL_UNIFORM_BUFFER, 0,
670 sizeof(struct ub_world_lighting), &world->ub_lighting );
671 /* TEMP!!!!!! */
672
673
674 #if 0
675 if( world.switching_to_new_world )
676 {
677 int all_stopped = 1;
678
679 audio_lock();
680 for( int i=0; i<world.audio_things_count; i++ )
681 {
682 struct world_audio_thing *at = &world.audio_things[i];
683
684 if( audio_player_is_playing( &at->player ) )
685 {
686 all_stopped = 0;
687 break;
688 }
689 }
690 audio_unlock();
691
692 if( all_stopped )
693 {
694 world.switching_to_new_world = 0;
695 world_unload();
696 vg_loader_start( world_load );
697 return;
698 }
699 }
700
701 #endif
702 world_global.sky_time += world_global.sky_rate * vg.time_delta;
703 world_global.sky_rate = vg_lerp( world_global.sky_rate,
704 world_global.sky_target_rate,
705 vg.time_delta * 5.0 );
706
707 world_routes_update( world );
708 #if 0
709 world_routes_debug();
710 #endif
711
712 if( world->route_count > 0 )
713 {
714 int closest = 0;
715 float min_dist = INFINITY;
716
717 for( int i=0; i<world->route_count; i++ )
718 {
719 float d = v3_dist2( world->routes[i].scoreboard_transform[3], pos );
720
721 if( d < min_dist )
722 {
723 min_dist = d;
724 closest = i;
725 }
726 }
727
728 if( (world_global.active_route_board != closest)
729 || network_scores_updated )
730 {
731 network_scores_updated = 0;
732 world_global.active_route_board = closest;
733
734 struct route *route = &world->routes[closest];
735
736 u32 id = route->track_id;
737
738 if( id != 0xffffffff )
739 {
740 struct netmsg_board *local_board =
741 &scoreboard_client_data.boards[id];
742
743 for( int i=0; i<13; i++ )
744 {
745 sfd_encode( i, &local_board->data[27*i] );
746 }
747 }
748 }
749 }
750
751 int in_trigger = 0;
752 for( int i=0; i<world->trigger_count; i++ )
753 {
754 struct trigger_zone *zone = &world->triggers[i];
755
756 v3f local;
757 m4x3_mulv( zone->inv_transform, pos, local );
758
759 if( (fabsf(local[0]) <= 1.0f) &&
760 (fabsf(local[1]) <= 1.0f) &&
761 (fabsf(local[2]) <= 1.0f) )
762 {
763 in_trigger = 1;
764
765 if( !world_global.in_trigger )
766 {
767 world_run_relay( world, &zone->target );
768 }
769 }
770
771 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
772 { 1.0f, 1.0f, 1.0f}},
773 0xff00ff00 );
774 }
775
776 if( k_debug_light_indices )
777 {
778 for( int i=0; i<world->light_count; i++ )
779 {
780 struct world_light *light = &world->lights[i];
781 struct classtype_world_light *inf = light->inf;
782
783 u32 colour = 0xff000000;
784 u8 r = inf->colour[0] * 255.0f,
785 g = inf->colour[1] * 255.0f,
786 b = inf->colour[2] * 255.0f;
787
788 colour |= r;
789 colour |= g << 8;
790 colour |= b << 16;
791
792 vg_line_pt3( light->node->co, 0.25f, colour );
793 }
794 }
795
796 world_global.in_trigger = in_trigger;
797 sfd_update();
798 }
799
800 /*
801 * -----------------------------------------------------------------------------
802 * API implementation
803 * -----------------------------------------------------------------------------
804 */
805
806 VG_STATIC void ray_world_get_tri( world_instance *world,
807 ray_hit *hit, v3f tri[3] )
808 {
809 for( int i=0; i<3; i++ )
810 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
811 }
812
813 VG_STATIC int ray_world( world_instance *world,
814 v3f pos, v3f dir, ray_hit *hit )
815 {
816 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
817 }
818
819 /*
820 * Cast a sphere from a to b and see what time it hits
821 */
822 VG_STATIC int spherecast_world( world_instance *world,
823 v3f pa, v3f pb, float r, float *t, v3f n )
824 {
825 bh_iter it;
826 bh_iter_init( 0, &it );
827
828 boxf region;
829 box_init_inf( region );
830 box_addpt( region, pa );
831 box_addpt( region, pb );
832
833 v3_add( (v3f){ r, r, r}, region[1], region[1] );
834 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
835
836 v3f dir;
837 v3_sub( pb, pa, dir );
838
839 v3f dir_inv;
840 dir_inv[0] = 1.0f/dir[0];
841 dir_inv[1] = 1.0f/dir[1];
842 dir_inv[2] = 1.0f/dir[2];
843
844 int hit = -1;
845 float min_t = 1.0f;
846
847 int idx;
848 while( bh_next( world->geo_bh, &it, region, &idx ) )
849 {
850 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
851 v3f tri[3];
852
853 boxf box;
854 box_init_inf( box );
855
856 for( int j=0; j<3; j++ )
857 {
858 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
859 box_addpt( box, tri[j] );
860 }
861
862 v3_add( (v3f){ r, r, r}, box[1], box[1] );
863 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
864
865 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
866 continue;
867
868 float t;
869 v3f n1;
870 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
871 {
872 if( t < min_t )
873 {
874 min_t = t;
875 hit = idx;
876 v3_copy( n1, n );
877 }
878 }
879 }
880
881 *t = min_t;
882 return hit;
883 }
884
885 VG_STATIC
886 struct world_material *world_tri_index_material( world_instance *world,
887 u32 index )
888 {
889 for( int i=1; i<world->material_count; i++ )
890 {
891 struct world_material *mat = &world->materials[i];
892
893 if( (index >= mat->sm_geo.vertex_start) &&
894 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
895 {
896 return mat;
897 }
898 }
899
900 /* error material */
901 return &world->materials[0];
902 }
903
904 VG_STATIC struct world_material *world_contact_material( world_instance *world,
905 rb_ct *ct )
906 {
907 return world_tri_index_material( world, ct->element_id );
908 }
909
910 VG_STATIC struct world_material *ray_hit_material( world_instance *world,
911 ray_hit *hit )
912 {
913 return world_tri_index_material( world, hit->tri[0] );
914 }
915
916 #endif /* WORLD_H */