2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
10 typedef struct world_instance world_instance
;
12 #include "vg/vg_loader.h"
15 #include "network_msg.h"
18 #include "rigidbody.h"
23 #include "shaders/scene_standard.h"
24 #include "shaders/scene_standard_alphatest.h"
25 #include "shaders/scene_vertex_blend.h"
26 #include "shaders/scene_terrain.h"
27 #include "shaders/scene_depth.h"
28 #include "shaders/scene_position.h"
30 #include "shaders/model_sky.h"
32 enum { k_max_ui_segments
= 8 };
34 enum { k_max_ui_elements
= k_max_ui_segments
};
35 enum { k_max_element_verts
= 10 };
36 enum { k_max_element_indices
= 20 };
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
};
43 k_logic_type_relay
= 1,
44 k_logic_type_chance
= 2,
45 k_logic_type_achievement
= 3
51 k_geo_type_nonsolid
= 1,
55 static const float k_light_cube_size
= 8.0f
;
59 /* This is a small flag we use to changelevel.
60 * It will not be cleared until all sounds stop playing
64 * -------------------------------------------------------
67 char world_name
[ 64 ];
81 struct ub_world_lighting
90 v4f g_nightsky_colour
;
100 float g_shadow_length
;
101 float g_shadow_spread
;
105 float g_sunset_phase
;
108 int g_shadow_samples
;
111 int g_debug_complexity
;
117 GLuint tbo_light_entities
,
121 float probabilities
[3];
125 struct framebuffer heightmap
;
128 * Dynamically allocated when world_load is called.
130 * the following arrays index somewhere into this linear
134 * --------------------------------------------------------------------------
153 mdl_array_ptr ent_spawn
,
166 ent_gate
*rendering_gate
;
170 * Named safe places to respawn
182 * Audio player entities
184 struct world_audio_thing
189 audio_clip temp_embedded_clip
;
192 u32 audio_things_count
;
197 audio_channel
*channels
[4];
199 /* accessable without locking */
204 u32 allow_transitions
;
205 float transition_duration
;
209 u32 soundscape_count
;
212 * Box volume entities
216 m4x3f transform
, inv_transform
;
228 struct classtype_world_light
*inf
;
236 * Routes (world_routes.h)
237 * --------------------------------------------------------------------------
242 v3f co
, right
, up
, h
;
245 u32 special_type
, special_id
, current_refs
, ref_count
;
246 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
262 double best_lap
, latest_pass
; /* Session */
264 m4x3f scoreboard_transform
;
277 m4x3f to_world
, transport
;
285 u32 version
; /* Incremented on every teleport */
295 struct teleport_gate gate
;
298 u32 target_map_index
, working
;
301 u32 nonlocalgate_count
;
303 struct route_collector
305 struct route_timing timing
;
313 * ----------------------------------------------------
321 /* spacial mappings */
327 glmesh mesh_route_lines
;
332 rigidbody rb_geo
; /* todo.. ... */
335 VG_STATIC
struct world_global
338 * Allocated as system memory
339 * --------------------------------------------------------------------------
346 mdl_submesh sm_gate_surface
,
349 double sky_time
, sky_rate
, sky_target_rate
;
351 u32 current_run_version
;
352 double time
, rewind_from
, rewind_to
, last_use
;
354 /* water rendering */
357 struct framebuffer fbreflect
, fbdepth
;
361 /* split flap display */
364 glmesh mesh_base
, mesh_display
;
366 u32 active_route_board
;
376 int switching_to_new_world
;
378 world_instance worlds
[4];
384 VG_STATIC world_instance
*get_active_world( void )
386 return &world_global
.worlds
[ world_global
.active_world
];
394 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
397 struct world_surface
*ray_hit_surface( world_instance
*world
, ray_hit
*hit
);
400 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
403 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
409 #include "world_routes.h"
410 #include "world_sfd.h"
411 #include "world_render.h"
412 #include "world_water.h"
413 #include "world_volumes.h"
414 #include "world_gen.h"
415 #include "world_gate.h"
418 * -----------------------------------------------------------------------------
420 * -----------------------------------------------------------------------------
423 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
425 world_instance
*world
= get_active_world();
429 VG_STATIC
void world_init(void)
431 world_global
.sky_rate
= 1.0;
432 world_global
.sky_target_rate
= 1.0;
434 shader_scene_standard_register();
435 shader_scene_standard_alphatest_register();
436 shader_scene_vertex_blend_register();
437 shader_scene_terrain_register();
438 shader_scene_depth_register();
439 shader_scene_position_register();
441 shader_model_sky_register();
443 vg_info( "Loading world resources\n" );
445 vg_linear_clear( vg_mem
.scratch
);
448 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
449 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
450 mdl_load_mesh_block( &msky
, vg_mem
.scratch
);
453 vg_acquire_thread_sync();
455 mdl_unpack_glmesh( &msky
, &world_global
.skydome
);
457 vg_release_thread_sync();
460 vg_info( "Loading other world systems\n" );
462 vg_loader_step( world_render_init
, NULL
);
463 vg_loader_step( world_sfd_init
, NULL
);
464 vg_loader_step( world_water_init
, NULL
);
465 vg_loader_step( world_gates_init
, NULL
);
466 vg_loader_step( world_routes_init
, NULL
);
468 /* Allocate dynamic world memory arena */
469 u32 max_size
= 76*1024*1024;
470 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
475 typedef struct ent_call ent_call
;
482 VG_STATIC
void entity_call( world_instance
*world
, ent_call
*call
);
484 VG_STATIC
void ent_volume_call( world_instance
*world
, ent_call
*call
)
486 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, call
->ent
.index
);
487 if( !volume
->target
.type
) return;
489 if( call
->function
== k_ent_function_trigger
){
490 call
->ent
= volume
->target
;
492 if( volume
->type
== k_volume_subtype_particle
){
494 co
[0] = vg_randf()*2.0f
-1.0f
;
495 co
[1] = vg_randf()*2.0f
-1.0f
;
496 co
[2] = vg_randf()*2.0f
-1.0f
;
497 m4x3_mulv( volume
->to_world
, co
, co
);
499 call
->function
= k_ent_function_particle_spawn
;
501 entity_call( world
, call
);
504 entity_call( world
, call
);
508 VG_STATIC
void ent_audio_call( world_instance
*world
, ent_call
*call
)
510 ent_audio
*audio
= mdl_arritm( &world
->ent_audio
, call
->ent
.index
);
514 if( call
->function
== k_ent_function_particle_spawn
){
515 v3_copy( call
->data
, sound_co
);
517 else if( call
->function
== k_ent_function_trigger
){
518 v3_copy( audio
->transform
.co
, sound_co
);
521 vg_fatal_exit_loop( "ent_audio_call (invalid function id)" );
523 float chance
= vg_randf()*100.0f
,
526 for( u32 i
=0; i
<audio
->clip_count
; i
++ ){
527 ent_audio_clip
*clip
= mdl_arritm( &world
->ent_audio_clip
,
528 audio
->clip_start
+i
);
530 float mod
= world
->probabilities
[ audio
->probability_curve
],
531 p
= clip
->probability
* mod
;
539 if( audio
->behaviour
== k_channel_behaviour_unlimited
){
540 audio_oneshot_3d( &clip
->clip
, sound_co
,
541 audio
->transform
.s
[0],
544 else if( audio
->behaviour
== k_channel_behaviour_discard_if_full
){
546 audio_get_group_idle_channel( audio
->group
,
547 audio
->max_channels
);
550 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
551 audio_channel_group( ch
, audio
->group
);
552 audio_channel_set_spacial( ch
, sound_co
, audio
->transform
.s
[0] );
553 audio_channel_edit_volume( ch
, audio
->volume
, 1 );
554 ch
= audio_relinquish_channel( ch
);
557 else if( audio
->behaviour
== k_channel_behaviour_crossfade_if_full
){
559 audio_get_group_idle_channel( audio
->group
,
560 audio
->max_channels
);
564 audio_channel
*existing
=
565 audio_get_group_first_active_channel( audio
->group
);
568 if( existing
->source
== &clip
->clip
){
574 existing
= audio_channel_fadeout(existing
, audio
->crossfade
);
577 ch
= audio_get_first_idle_channel();
581 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
582 audio_channel_group( ch
, audio
->group
);
583 audio_channel_fadein( ch
, audio
->crossfade
);
584 ch
= audio_relinquish_channel( ch
);
594 VG_STATIC
void entity_call( world_instance
*world
, ent_call
*call
)
596 if( call
->ent
.type
== k_ent_volume
){
597 ent_volume_call( world
, call
);
598 } else if( call
->ent
.type
== k_ent_audio
){
599 ent_audio_call( world
, call
);
603 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
606 static double g_time
= 0.0;
607 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
610 struct ub_world_lighting
*state
= &world
->ub_lighting
;
612 state
->g_time
= g_time
;
613 state
->g_realtime
= vg
.time
;
614 state
->g_debug_indices
= k_debug_light_indices
;
615 state
->g_light_preview
= k_light_preview
;
616 state
->g_debug_complexity
= k_debug_light_complexity
;
618 state
->g_time_of_day
= vg_fractf( g_time
);
619 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
620 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
622 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
623 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
625 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
626 state
->g_sun_dir
[0] = sinf( a
);
627 state
->g_sun_dir
[1] = cosf( a
);
628 state
->g_sun_dir
[2] = 0.2f
;
629 v3_normalize( state
->g_sun_dir
);
632 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
634 float dp
= state
->g_day_phase
;
636 world
->probabilities
[ k_probability_curve_wildlife_day
] =
637 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
638 world
->probabilities
[ k_probability_curve_wildlife_night
] =
639 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
642 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
643 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
644 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
647 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
648 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
649 world_global
.sky_target_rate
,
650 vg
.time_delta
* 5.0 );
652 world_routes_update( world
);
653 world_routes_debug( world
);
655 /* ---- SFD ------------ */
657 if( mdl_arrcount( &world
->ent_route
) ){
659 float min_dist
= INFINITY
;
661 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
662 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
663 float dist
= v3_dist2( route
->board_transform
[3], pos
);
665 if( dist
< min_dist
){
671 if( (world_global
.sfd
.active_route_board
!= closest
)
672 || network_scores_updated
)
674 network_scores_updated
= 0;
675 world_global
.sfd
.active_route_board
= closest
;
677 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
678 u32 id
= route
->official_track_id
;
680 if( id
!= 0xffffffff ){
681 struct netmsg_board
*local_board
=
682 &scoreboard_client_data
.boards
[id
];
684 for( int i
=0; i
<13; i
++ ){
685 sfd_encode( i
, &local_board
->data
[27*i
] );
688 sfd_encode( 0, mdl_pstr( &world
->meta
, route
->pstr_name
) );
689 sfd_encode( 1, "No data" );
697 static float random_accum
= 0.0f
;
698 random_accum
+= vg
.time_delta
;
700 u32 random_ticks
= 0;
702 while( random_accum
> 0.1f
){
703 random_accum
-= 0.1f
;
707 float radius
= 25.0f
;
708 boxf volume_proximity
;
709 v3_add( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[1] );
710 v3_sub( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[0] );
713 bh_iter_init( 0, &it
);
718 while( bh_next( world
->volume_bh
, &it
, volume_proximity
, &idx
) ){
719 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, idx
);
721 boxf cube
= {{-1.0f
,-1.0f
,-1.0f
},{1.0f
,1.0f
,1.0f
}};
723 if( volume
->type
== k_volume_subtype_trigger
){
725 m4x3_mulv( volume
->to_local
, pos
, local
);
727 if( (fabsf(local
[0]) <= 1.0f
) &&
728 (fabsf(local
[1]) <= 1.0f
) &&
729 (fabsf(local
[2]) <= 1.0f
) )
732 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00ff00 );
734 if( !world_global
.in_volume
){
736 basecall
.ent
.index
= idx
;
737 basecall
.ent
.type
= k_ent_volume
;
738 basecall
.function
= k_ent_function_trigger
;
739 basecall
.data
= NULL
;
741 entity_call( world
, &basecall
);
745 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff0000ff );
747 else if( volume
->type
== k_volume_subtype_particle
){
748 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00c0ff );
750 for( int j
=0; j
<random_ticks
; j
++ ){
752 basecall
.ent
.index
= idx
;
753 basecall
.ent
.type
= k_ent_volume
;
754 basecall
.function
= k_ent_function_trigger
;
755 basecall
.data
= NULL
;
757 entity_call( world
, &basecall
);
761 world_global
.in_volume
= in_volume
;
764 if( k_debug_light_indices
)
766 for( int i
=0; i
<world
->light_count
; i
++ ){
767 struct world_light
*light
= &world
->lights
[i
];
768 struct classtype_world_light
*inf
= light
->inf
;
770 u32 colour
= 0xff000000;
771 u8 r
= inf
->colour
[0] * 255.0f
,
772 g
= inf
->colour
[1] * 255.0f
,
773 b
= inf
->colour
[2] * 255.0f
;
779 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
787 /* process soundscape transactions */
789 for( int i
=0; i
<world
->soundscape_count
; i
++ )
791 struct soundscape
*s
= &world
->soundscapes
[i
];
794 for( int j
=0; j
<s
->max_instances
; j
++ )
798 if( audio_channel_finished(s
->channels
[j
]) )
799 s
->channels
[j
] = audio_relinquish_channel( s
->channels
[j
] );
810 * -----------------------------------------------------------------------------
812 * -----------------------------------------------------------------------------
815 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
816 ray_hit
*hit
, v3f tri
[3] )
818 for( int i
=0; i
<3; i
++ )
819 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
822 VG_STATIC
int ray_world( world_instance
*world
,
823 v3f pos
, v3f dir
, ray_hit
*hit
)
825 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
829 * Cast a sphere from a to b and see what time it hits
831 VG_STATIC
int spherecast_world( world_instance
*world
,
832 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
835 bh_iter_init( 0, &it
);
838 box_init_inf( region
);
839 box_addpt( region
, pa
);
840 box_addpt( region
, pb
);
842 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
843 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
846 v3_sub( pb
, pa
, dir
);
849 dir_inv
[0] = 1.0f
/dir
[0];
850 dir_inv
[1] = 1.0f
/dir
[1];
851 dir_inv
[2] = 1.0f
/dir
[2];
857 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) ){
858 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
864 for( int j
=0; j
<3; j
++ ){
865 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
866 box_addpt( box
, tri
[j
] );
869 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
870 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
872 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
877 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) ){
891 struct world_surface
*world_tri_index_surface( world_instance
*world
,
894 for( int i
=1; i
<world
->surface_count
; i
++ ){
895 struct world_surface
*surf
= &world
->surfaces
[i
];
897 if( (index
>= surf
->sm_geo
.vertex_start
) &&
898 (index
< surf
->sm_geo
.vertex_start
+surf
->sm_geo
.vertex_count
) )
904 return &world
->surfaces
[0];
907 VG_STATIC
struct world_surface
*world_contact_surface( world_instance
*world
,
910 return world_tri_index_surface( world
, ct
->element_id
);
913 VG_STATIC
struct world_surface
*ray_hit_surface( world_instance
*world
,
916 return world_tri_index_surface( world
, hit
->tri
[0] );