baby lock the door and turn the lights down low
[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 *audio_heap; /* sub buffer of the audio buffer */
346
347 /* rendering */
348 glmesh skydome;
349 mdl_submesh dome_upper, dome_lower;
350
351 glmesh mesh_gate_surface;
352
353 double sky_time, sky_rate, sky_target_rate;
354
355 /* gates, TODO: active_gate should also know which instance */
356 u32 active_gate,
357 current_run_version;
358 double time, rewind_from, rewind_to, last_use;
359
360 /* water rendering */
361 struct
362 {
363 struct framebuffer fbreflect, fbdepth;
364 }
365 water;
366
367 /* split flap display */
368 struct
369 {
370 mdl_submesh *sm_module, *sm_card;
371 glmesh mesh_base, mesh_display;
372
373 u32 w, h;
374 float *buffer;
375 }
376 sfd;
377
378 /* timing bars, fixed maximum amount */
379 struct route_ui_bar
380 {
381 GLuint vao, vbo, ebo;
382
383 u32 indices_head;
384 u32 vertex_head;
385
386 struct route_ui_segment
387 {
388 float length;
389 u32 vertex_start, vertex_count,
390 index_start, index_count, notches;
391 }
392 segments[k_max_ui_segments];
393
394 u32 segment_start, segment_count, fade_start, fade_count;
395 double fade_timer_start;
396 float xpos;
397 }
398 ui_bars[16];
399
400 v3f render_gate_pos;
401 int active_route_board;
402 int in_trigger;
403
404 int switching_to_new_world;
405
406 world_instance worlds[4];
407 u32 world_count;
408 u32 active_world;
409 }
410 world_global;
411
412 VG_STATIC world_instance *get_active_world( void )
413 {
414 return &world_global.worlds[ world_global.active_world ];
415 }
416
417 /*
418 * API
419 */
420
421 VG_STATIC
422 int ray_hit_is_ramp( world_instance *world, ray_hit *hit );
423
424 VG_STATIC
425 struct world_material *ray_hit_material( world_instance *world, ray_hit *hit );
426
427 VG_STATIC
428 void ray_world_get_tri( world_instance *world, ray_hit *hit, v3f tri[3] );
429
430 VG_STATIC
431 int ray_world( world_instance *world, v3f pos, v3f dir, ray_hit *hit );
432
433 /*
434 * Submodules
435 */
436
437 #include "world_routes.h"
438 #include "world_sfd.h"
439 #include "world_render.h"
440 #include "world_water.h"
441 #include "world_gen.h"
442 #include "world_gate.h"
443
444 /*
445 * -----------------------------------------------------------------------------
446 * Events
447 * -----------------------------------------------------------------------------
448 */
449
450 VG_STATIC int world_stop_sound( int argc, const char *argv[] )
451 {
452 world_instance *world = get_active_world();
453
454 /*
455 * None of our world audio runs as one shots, they always have a player.
456 * Therefore it is safe to delete clip data after the players are
457 * disconnected
458 */
459 audio_lock();
460 for( int i=0; i<world->audio_things_count; i++ )
461 {
462 struct world_audio_thing *at = &world->audio_things[i];
463
464 if( audio_player_is_playing( &at->player ) )
465 {
466 u32 cflags = audio_player_get_flags( &at->player );
467 audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
468 }
469 }
470 audio_unlock();
471
472 return 0;
473 }
474
475 VG_STATIC int world_change_world( int argc, const char *argv[] )
476 {
477 #if 0
478 world_instance *world = get_active_world();
479
480 if( argc == 0 )
481 {
482 vg_info( "%s\n", world.world_name );
483 return 0;
484 }
485 else
486 {
487 vg_info( "Switching world...\n" );
488 strcpy( world.world_name, argv[0] );
489 world.switching_to_new_world = 1;
490 world_stop_sound( 0, NULL );
491 }
492 #endif
493
494 return 0;
495 }
496
497 VG_STATIC void world_init(void)
498 {
499 #if 0
500 vg_var_push( (struct vg_var){
501 .name = "water_enable",
502 .data = &world.water.enabled,
503 .data_type = k_var_dtype_i32,
504 .opt_i32 = { .min=0, .max=1, .clamp=1 },
505 .persistent = 0
506 });
507 #endif
508
509 vg_function_push( (struct vg_cmd)
510 {
511 .name = "world_stop_sound",
512 .function = world_stop_sound
513 });
514
515 vg_function_push( (struct vg_cmd)
516 {
517 .name = "world",
518 .function = world_change_world
519 });
520
521 world_global.sky_rate = 1.0;
522 world_global.sky_target_rate = 1.0;
523
524 shader_scene_standard_register();
525 shader_scene_standard_alphatest_register();
526 shader_scene_vertex_blend_register();
527 shader_scene_terrain_register();
528 shader_scene_depth_register();
529 shader_scene_position_register();
530
531 shader_model_sky_register();
532
533 vg_info( "Loading world resources\n" );
534
535 vg_linear_clear( vg_mem.scratch );
536 mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
537
538 mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
539 world_global.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
540
541 vg_acquire_thread_sync();
542 {
543 mdl_unpack_glmesh( msky, &world_global.skydome );
544 }
545 vg_release_thread_sync();
546
547 /* Other systems */
548 vg_info( "Loading other world systems\n" );
549
550 vg_loader_step( world_render_init, NULL );
551 vg_loader_step( world_sfd_init, NULL );
552 vg_loader_step( world_water_init, NULL );
553 vg_loader_step( world_gates_init, NULL );
554 vg_loader_step( world_routes_init, NULL );
555
556 /* Allocate dynamic world memory arena */
557 u32 max_size = 76*1024*1024;
558 world_global.generic_heap = vg_create_linear_allocator( vg_mem.rtmemory,
559 max_size,
560 VG_MEMORY_SYSTEM );
561 }
562
563 VG_STATIC void world_audio_init(void)
564 {
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 }
572
573 VG_STATIC void world_trigger_achievement( world_instance *world, u32 uid )
574 {
575 struct logic_achievement *ach = &world->logic_achievements[ uid ];
576
577 if( ach->achieved )
578 return;
579
580 steam_set_achievement( ach->achievement_id );
581 steam_store_achievements();
582
583 ach->achieved = 1;
584 }
585
586 VG_STATIC void world_run_relay( world_instance *world,
587 struct relay_target *rt );
588
589 VG_STATIC void world_trigger_relay( world_instance *world, u32 uid )
590 {
591 struct logic_relay *relay = &world->logic_relays[ uid ];
592
593 for( int i=0; i<relay->target_count; i++ )
594 {
595 world_run_relay( world, &relay->targets[i] );
596 }
597 }
598
599 VG_STATIC void world_trigger_audio( world_instance *world, u32 uid )
600 {
601 struct world_audio_thing *wat = &world->audio_things[ uid ];
602
603 audio_lock();
604 audio_player_playclip( &wat->player,
605 &wat->temp_embedded_clip );
606 audio_unlock();
607 }
608
609 VG_STATIC void world_run_relay( world_instance *world,
610 struct relay_target *rt )
611 {
612 struct entity_instruction
613 {
614 enum classtype classtype;
615 void (*p_trigger)( world_instance *world, u32 uid );
616 }
617 entity_instructions[] =
618 {
619 { k_classtype_logic_achievement, world_trigger_achievement },
620 { k_classtype_logic_relay, world_trigger_relay },
621 { k_classtype_audio, world_trigger_audio }
622 };
623
624 for( int i=0; i<vg_list_size(entity_instructions); i++ )
625 {
626 struct entity_instruction *instr = &entity_instructions[i];
627
628 if( instr->classtype == rt->classtype )
629 {
630 instr->p_trigger( world, rt->sub_id );
631 return;
632 }
633 }
634
635 vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
636 }
637
638 VG_STATIC void world_update( world_instance *world, v3f pos )
639 {
640 /* TEMP!!!!!! */
641 static double g_time = 0.0;
642 g_time += vg.time_delta * (1.0/(k_day_length*60.0));
643
644
645 struct ub_world_lighting *state = &world->ub_lighting;
646
647 state->g_time = g_time;
648 state->g_debug_indices = k_debug_light_indices;
649 state->g_light_preview = k_light_preview;
650 state->g_debug_complexity = k_debug_light_complexity;
651
652 state->g_time_of_day = vg_fractf( g_time );
653 state->g_day_phase = cosf( state->g_time_of_day * VG_PIf * 2.0f );
654 state->g_sunset_phase= cosf( state->g_time_of_day * VG_PIf * 4.0f + VG_PIf );
655
656 state->g_day_phase = state->g_day_phase * 0.5f + 0.5f;
657 state->g_sunset_phase = powf( state->g_sunset_phase * 0.5f + 0.5f, 6.0f );
658
659 float a = state->g_time_of_day * VG_PIf * 2.0f;
660 state->g_sun_dir[0] = sinf( a );
661 state->g_sun_dir[1] = cosf( a );
662 state->g_sun_dir[2] = 0.2f;
663 v3_normalize( state->g_sun_dir );
664
665
666
667 glBindBuffer( GL_UNIFORM_BUFFER, world->ubo_lighting );
668 glBufferSubData( GL_UNIFORM_BUFFER, 0,
669 sizeof(struct ub_world_lighting), &world->ub_lighting );
670 /* TEMP!!!!!! */
671
672
673 #if 0
674 if( world.switching_to_new_world )
675 {
676 int all_stopped = 1;
677
678 audio_lock();
679 for( int i=0; i<world.audio_things_count; i++ )
680 {
681 struct world_audio_thing *at = &world.audio_things[i];
682
683 if( audio_player_is_playing( &at->player ) )
684 {
685 all_stopped = 0;
686 break;
687 }
688 }
689 audio_unlock();
690
691 if( all_stopped )
692 {
693 world.switching_to_new_world = 0;
694 world_unload();
695 vg_loader_start( world_load );
696 return;
697 }
698 }
699
700 #endif
701 world_global.sky_time += world_global.sky_rate * vg.time_delta;
702 world_global.sky_rate = vg_lerp( world_global.sky_rate,
703 world_global.sky_target_rate,
704 vg.time_delta * 5.0 );
705
706 world_routes_update( world );
707 #if 0
708 world_routes_debug();
709 #endif
710
711 if( world->route_count > 0 )
712 {
713 int closest = 0;
714 float min_dist = INFINITY;
715
716 for( int i=0; i<world->route_count; i++ )
717 {
718 float d = v3_dist2( world->routes[i].scoreboard_transform[3], pos );
719
720 if( d < min_dist )
721 {
722 min_dist = d;
723 closest = i;
724 }
725 }
726
727 if( (world_global.active_route_board != closest)
728 || network_scores_updated )
729 {
730 network_scores_updated = 0;
731 world_global.active_route_board = closest;
732
733 struct route *route = &world->routes[closest];
734
735 u32 id = route->track_id;
736
737 if( id != 0xffffffff )
738 {
739 struct netmsg_board *local_board =
740 &scoreboard_client_data.boards[id];
741
742 for( int i=0; i<13; i++ )
743 {
744 sfd_encode( i, &local_board->data[27*i] );
745 }
746 }
747 }
748 }
749
750 int in_trigger = 0;
751 for( int i=0; i<world->trigger_count; i++ )
752 {
753 struct trigger_zone *zone = &world->triggers[i];
754
755 v3f local;
756 m4x3_mulv( zone->inv_transform, pos, local );
757
758 if( (fabsf(local[0]) <= 1.0f) &&
759 (fabsf(local[1]) <= 1.0f) &&
760 (fabsf(local[2]) <= 1.0f) )
761 {
762 in_trigger = 1;
763
764 if( !world_global.in_trigger )
765 {
766 world_run_relay( world, &zone->target );
767 }
768 }
769
770 vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
771 { 1.0f, 1.0f, 1.0f}},
772 0xff00ff00 );
773 }
774
775 if( k_debug_light_indices )
776 {
777 for( int i=0; i<world->light_count; i++ )
778 {
779 struct world_light *light = &world->lights[i];
780 struct classtype_world_light *inf = light->inf;
781
782 u32 colour = 0xff000000;
783 u8 r = inf->colour[0] * 255.0f,
784 g = inf->colour[1] * 255.0f,
785 b = inf->colour[2] * 255.0f;
786
787 colour |= r;
788 colour |= g << 8;
789 colour |= b << 16;
790
791 vg_line_pt3( light->node->co, 0.25f, colour );
792 }
793 }
794
795 world_global.in_trigger = in_trigger;
796 sfd_update();
797 }
798
799 /*
800 * -----------------------------------------------------------------------------
801 * API implementation
802 * -----------------------------------------------------------------------------
803 */
804
805 VG_STATIC void ray_world_get_tri( world_instance *world,
806 ray_hit *hit, v3f tri[3] )
807 {
808 for( int i=0; i<3; i++ )
809 v3_copy( world->scene_geo->arrvertices[ hit->tri[i] ].co, tri[i] );
810 }
811
812 VG_STATIC int ray_world( world_instance *world,
813 v3f pos, v3f dir, ray_hit *hit )
814 {
815 return scene_raycast( world->scene_geo, world->geo_bh, pos, dir, hit );
816 }
817
818 /*
819 * Cast a sphere from a to b and see what time it hits
820 */
821 VG_STATIC int spherecast_world( world_instance *world,
822 v3f pa, v3f pb, float r, float *t, v3f n )
823 {
824 bh_iter it;
825 bh_iter_init( 0, &it );
826
827 boxf region;
828 box_init_inf( region );
829 box_addpt( region, pa );
830 box_addpt( region, pb );
831
832 v3_add( (v3f){ r, r, r}, region[1], region[1] );
833 v3_add( (v3f){-r,-r,-r}, region[0], region[0] );
834
835 v3f dir;
836 v3_sub( pb, pa, dir );
837
838 v3f dir_inv;
839 dir_inv[0] = 1.0f/dir[0];
840 dir_inv[1] = 1.0f/dir[1];
841 dir_inv[2] = 1.0f/dir[2];
842
843 int hit = -1;
844 float min_t = 1.0f;
845
846 int idx;
847 while( bh_next( world->geo_bh, &it, region, &idx ) )
848 {
849 u32 *ptri = &world->scene_geo->arrindices[ idx*3 ];
850 v3f tri[3];
851
852 boxf box;
853 box_init_inf( box );
854
855 for( int j=0; j<3; j++ )
856 {
857 v3_copy( world->scene_geo->arrvertices[ptri[j]].co, tri[j] );
858 box_addpt( box, tri[j] );
859 }
860
861 v3_add( (v3f){ r, r, r}, box[1], box[1] );
862 v3_add( (v3f){-r,-r,-r}, box[0], box[0] );
863
864 if( !ray_aabb1( box, pa, dir_inv, 1.0f ) )
865 continue;
866
867 float t;
868 v3f n1;
869 if( spherecast_triangle( tri, pa, dir, r, &t, n1 ) )
870 {
871 if( t < min_t )
872 {
873 min_t = t;
874 hit = idx;
875 v3_copy( n1, n );
876 }
877 }
878 }
879
880 *t = min_t;
881 return hit;
882 }
883
884 VG_STATIC
885 struct world_material *world_tri_index_material( world_instance *world,
886 u32 index )
887 {
888 for( int i=1; i<world->material_count; i++ )
889 {
890 struct world_material *mat = &world->materials[i];
891
892 if( (index >= mat->sm_geo.vertex_start) &&
893 (index < mat->sm_geo.vertex_start+mat->sm_geo.vertex_count ) )
894 {
895 return mat;
896 }
897 }
898
899 /* error material */
900 return &world->materials[0];
901 }
902
903 VG_STATIC struct world_material *world_contact_material( world_instance *world,
904 rb_ct *ct )
905 {
906 return world_tri_index_material( world, ct->element_id );
907 }
908
909 VG_STATIC struct world_material *ray_hit_material( world_instance *world,
910 ray_hit *hit )
911 {
912 return world_tri_index_material( world, hit->tri[0] );
913 }
914
915 #endif /* WORLD_H */