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