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