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