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"
24 #include "shaders/scene_standard.h"
25 #include "shaders/scene_standard_alphatest.h"
26 #include "shaders/scene_vertex_blend.h"
27 #include "shaders/scene_terrain.h"
28 #include "shaders/scene_depth.h"
29 #include "shaders/scene_position.h"
31 #include "shaders/model_sky.h"
33 enum { k_max_ui_segments
= 8 };
35 enum { k_max_ui_elements
= k_max_ui_segments
};
36 enum { k_max_element_verts
= 10 };
37 enum { k_max_element_indices
= 20 };
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
};
44 k_logic_type_relay
= 1,
45 k_logic_type_chance
= 2,
46 k_logic_type_achievement
= 3
52 k_geo_type_nonsolid
= 1,
56 static const float k_light_cube_size
= 8.0f
;
58 struct world_instance
{
60 * -------------------------------------------------------
64 char world_name
[ 64 ];
66 k_world_status_unloaded
= 0,
67 k_world_status_loading
= 1,
68 k_world_status_loaded
= 2
83 struct ub_world_lighting
{
91 v4f g_nightsky_colour
;
103 float g_shadow_length
;
104 float g_shadow_spread
;
108 float g_sunset_phase
;
111 int g_shadow_samples
;
114 int g_debug_complexity
;
120 GLuint tbo_light_entities
,
124 float probabilities
[3];
128 struct framebuffer heightmap
;
131 * Dynamically allocated when world_load is called.
133 * the following arrays index somewhere into this linear
137 * --------------------------------------------------------------------------
148 struct world_surface
{
156 mdl_array_ptr ent_spawn
,
173 ent_gate
*rendering_gate
;
176 * ----------------------------------------------------
180 scene_context scene_geo
,
184 /* spacial mappings */
190 glmesh mesh_route_lines
;
200 * Allocated as system memory
201 * --------------------------------------------------------------------------
208 mdl_submesh sm_gate_surface
,
211 double sky_time
, sky_rate
, sky_target_rate
;
213 u32 current_run_version
;
214 double time
, rewind_from
, rewind_to
, last_use
;
216 /* water rendering */
218 struct framebuffer fbreflect
, fbdepth
;
222 /* split flap display */
224 glmesh mesh_base
, mesh_display
;
226 u32 active_route_board
;
237 int switching_to_new_world
;
239 world_instance worlds
[4];
253 u32 timer_text_count
;
255 struct text_particle
{
264 u32 text_particle_count
;
268 VG_STATIC world_instance
*get_active_world( void )
270 return &world_global
.worlds
[ world_global
.active_world
];
278 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
281 struct world_surface
*ray_hit_surface( world_instance
*world
, ray_hit
*hit
);
284 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
287 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
290 ent_spawn
*world_find_closest_spawn( world_instance
*world
, v3f position
)
292 ent_spawn
*rp
= NULL
, *r
;
293 float min_dist
= INFINITY
;
295 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_spawn
); i
++ ){
296 r
= mdl_arritm( &world
->ent_spawn
, i
);
297 float d
= v3_dist2( r
->transform
.co
, position
);
306 if( mdl_arrcount(&world
->ent_spawn
) ){
307 vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
308 return mdl_arritm( &world
->ent_spawn
, 0 );
311 vg_error( "There are no spawns in the level!\n" );
319 ent_spawn
*world_find_spawn_by_name( world_instance
*world
, const char *name
)
321 ent_spawn
*rp
= NULL
, *r
;
322 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_spawn
); i
++ ){
323 r
= mdl_arritm( &world
->ent_spawn
, i
);
324 if( !strcmp( mdl_pstr(&world
->meta
, r
->pstr_name
), name
) ){
331 vg_warn( "No spawn named '%s'\n", name
);
341 k_day_length
= 30.0f
; /* minutes */
343 VG_STATIC
int k_debug_light_indices
= 0,
344 k_debug_light_complexity
= 0,
347 #include "world_routes.h"
348 #include "world_sfd.h"
349 #include "world_render.h"
350 #include "world_water.h"
351 #include "world_volumes.h"
352 #include "world_gen.h"
353 #include "world_gate.h"
356 * -----------------------------------------------------------------------------
358 * -----------------------------------------------------------------------------
361 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
363 world_instance
*world
= get_active_world();
367 VG_STATIC
void world_init(void)
369 VG_VAR_F32( k_day_length
);
370 VG_VAR_I32( k_debug_light_indices
);
371 VG_VAR_I32( k_debug_light_complexity
);
372 VG_VAR_I32( k_light_preview
);
374 world_global
.sky_rate
= 1.0;
375 world_global
.sky_target_rate
= 1.0;
377 shader_scene_standard_register();
378 shader_scene_standard_alphatest_register();
379 shader_scene_vertex_blend_register();
380 shader_scene_terrain_register();
381 shader_scene_depth_register();
382 shader_scene_position_register();
384 shader_model_sky_register();
386 vg_info( "Loading world resources\n" );
388 vg_linear_clear( vg_mem
.scratch
);
391 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
392 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
393 mdl_async_load_glmesh( &msky
, &world_global
.skydome
);
397 vg_info( "Loading other world systems\n" );
399 vg_loader_step( world_render_init
, NULL
);
400 vg_loader_step( world_sfd_init
, NULL
);
401 vg_loader_step( world_water_init
, NULL
);
402 vg_loader_step( world_gates_init
, NULL
);
403 vg_loader_step( world_routes_init
, NULL
);
405 /* Allocate dynamic world memory arena */
406 u32 max_size
= 76*1024*1024;
407 world_global
.heap
= vg_create_linear_allocator( vg_mem
.rtmemory
, max_size
,
411 VG_STATIC
void ent_volume_call( world_instance
*world
, ent_call
*call
)
413 u32 index
= mdl_entity_id_id( call
->id
);
414 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, index
);
415 if( !volume
->target
) return;
417 if( call
->function
== k_ent_function_trigger
){
418 call
->id
= volume
->target
;
420 if( volume
->type
== k_volume_subtype_particle
){
421 float *co
= alloca( sizeof(float)*3 );
422 co
[0] = vg_randf()*2.0f
-1.0f
;
423 co
[1] = vg_randf()*2.0f
-1.0f
;
424 co
[2] = vg_randf()*2.0f
-1.0f
;
425 m4x3_mulv( volume
->to_world
, co
, co
);
427 call
->function
= k_ent_function_particle_spawn
;
429 entity_call( world
, call
);
432 entity_call( world
, call
);
437 VG_STATIC
void ent_audio_call( world_instance
*world
, ent_call
*call
)
439 u32 index
= mdl_entity_id_id( call
->id
);
440 ent_audio
*audio
= mdl_arritm( &world
->ent_audio
, index
);
444 if( call
->function
== k_ent_function_particle_spawn
){
445 v3_copy( call
->data
, sound_co
);
447 else if( call
->function
== k_ent_function_trigger
){
448 v3_copy( audio
->transform
.co
, sound_co
);
451 vg_fatal_error( "ent_audio_call (invalid function id)" );
453 float chance
= vg_randf()*100.0f
,
456 for( u32 i
=0; i
<audio
->clip_count
; i
++ ){
457 ent_audio_clip
*clip
= mdl_arritm( &world
->ent_audio_clip
,
458 audio
->clip_start
+i
);
460 float mod
= world
->probabilities
[ audio
->probability_curve
],
461 p
= clip
->probability
* mod
;
469 if( audio
->behaviour
== k_channel_behaviour_unlimited
){
470 audio_oneshot_3d( &clip
->clip
, sound_co
,
471 audio
->transform
.s
[0],
474 else if( audio
->behaviour
== k_channel_behaviour_discard_if_full
){
476 audio_get_group_idle_channel( audio
->group
,
477 audio
->max_channels
);
480 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
481 audio_channel_group( ch
, audio
->group
);
482 audio_channel_set_spacial( ch
, sound_co
, audio
->transform
.s
[0] );
483 audio_channel_edit_volume( ch
, audio
->volume
, 1 );
484 ch
= audio_relinquish_channel( ch
);
487 else if( audio
->behaviour
== k_channel_behaviour_crossfade_if_full
){
489 audio_get_group_idle_channel( audio
->group
,
490 audio
->max_channels
);
494 audio_channel
*existing
=
495 audio_get_group_first_active_channel( audio
->group
);
498 if( existing
->source
== &clip
->clip
){
504 existing
= audio_channel_fadeout(existing
, audio
->crossfade
);
507 ch
= audio_get_first_idle_channel();
511 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
512 audio_channel_group( ch
, audio
->group
);
513 audio_channel_fadein( ch
, audio
->crossfade
);
514 ch
= audio_relinquish_channel( ch
);
524 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
526 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
527 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
528 world_global
.sky_target_rate
,
529 vg
.time_delta
* 5.0 );
531 world_routes_update_timer_texts( world
);
532 world_routes_update( world
);
533 //world_routes_debug( world );
535 /* ---- traffic -------- */
537 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_traffic
); i
++ ){
538 ent_traffic
*traffic
= mdl_arritm( &world
->ent_traffic
, i
);
540 u32 i1
= traffic
->index
,
544 if( i1
== 0 ) i0
= traffic
->node_count
-1;
547 if( i2
>= traffic
->node_count
) i2
= 0;
549 i0
+= traffic
->start_node
;
550 i1
+= traffic
->start_node
;
551 i2
+= traffic
->start_node
;
555 ent_route_node
*rn0
= mdl_arritm( &world
->ent_route_node
, i0
),
556 *rn1
= mdl_arritm( &world
->ent_route_node
, i1
),
557 *rn2
= mdl_arritm( &world
->ent_route_node
, i2
);
559 v3_copy( rn1
->co
, h
[1] );
560 v3_lerp( rn0
->co
, rn1
->co
, 0.5f
, h
[0] );
561 v3_lerp( rn1
->co
, rn2
->co
, 0.5f
, h
[2] );
563 float const k_sample_dist
= 0.0025f
;
565 eval_bezier3( h
[0], h
[1], h
[2], traffic
->t
, pc
);
566 eval_bezier3( h
[0], h
[1], h
[2], traffic
->t
+k_sample_dist
, pd
);
569 v3_sub( pd
, pc
, v0
);
570 float length
= vg_maxf( 0.0001f
, v3_length( v0
) );
571 v3_muls( v0
, 1.0f
/length
, v0
);
573 float mod
= k_sample_dist
/ length
;
575 traffic
->t
+= traffic
->speed
* vg
.time_delta
* mod
;
577 if( traffic
->t
> 1.0f
){
580 if( traffic
->t
> 1.0f
) traffic
->t
= 0.0f
;
584 if( traffic
->index
>= traffic
->node_count
)
588 v3_copy( pc
, traffic
->transform
.co
);
590 float a
= atan2f( -v0
[0], v0
[2] );
591 q_axis_angle( traffic
->transform
.q
, (v3f
){0.0f
,1.0f
,0.0f
}, -a
);
593 vg_line_pt3( traffic
->transform
.co
, 0.3f
, VG__BLUE
);
596 /* ---- SFD ------------ */
598 if( mdl_arrcount( &world
->ent_route
) ){
600 float min_dist
= INFINITY
;
602 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
603 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
604 float dist
= v3_dist2( route
->board_transform
[3], pos
);
606 if( dist
< min_dist
){
612 if( (world_global
.sfd
.active_route_board
!= closest
)
613 || network_scores_updated
)
615 network_scores_updated
= 0;
616 world_global
.sfd
.active_route_board
= closest
;
618 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
619 u32 id
= route
->official_track_id
;
621 if( id
!= 0xffffffff ){
622 struct netmsg_board
*local_board
=
623 &scoreboard_client_data
.boards
[id
];
625 for( int i
=0; i
<13; i
++ ){
626 sfd_encode( i
, &local_board
->data
[27*i
] );
629 sfd_encode( 0, mdl_pstr( &world
->meta
, route
->pstr_name
) );
630 sfd_encode( 1, "No data" );
636 static float random_accum
= 0.0f
;
637 random_accum
+= vg
.time_delta
;
639 u32 random_ticks
= 0;
641 while( random_accum
> 0.1f
){
642 random_accum
-= 0.1f
;
646 float radius
= 25.0f
;
647 boxf volume_proximity
;
648 v3_add( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[1] );
649 v3_sub( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[0] );
652 bh_iter_init( 0, &it
);
657 while( bh_next( world
->volume_bh
, &it
, volume_proximity
, &idx
) ){
658 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, idx
);
660 boxf cube
= {{-1.0f
,-1.0f
,-1.0f
},{1.0f
,1.0f
,1.0f
}};
662 if( volume
->type
== k_volume_subtype_trigger
){
664 m4x3_mulv( volume
->to_local
, pos
, local
);
666 if( (fabsf(local
[0]) <= 1.0f
) &&
667 (fabsf(local
[1]) <= 1.0f
) &&
668 (fabsf(local
[2]) <= 1.0f
) )
671 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00ff00 );
673 if( !world_global
.in_volume
){
675 basecall
.function
= k_ent_function_trigger
;
676 basecall
.id
= mdl_entity_id( k_ent_volume
, idx
);
677 basecall
.data
= NULL
;
679 entity_call( world
, &basecall
);
683 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff0000ff );
685 else if( volume
->type
== k_volume_subtype_particle
){
686 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00c0ff );
688 for( int j
=0; j
<random_ticks
; j
++ ){
690 basecall
.id
= mdl_entity_id( k_ent_volume
, idx
);
691 basecall
.data
= NULL
;
693 entity_call( world
, &basecall
);
697 world_global
.in_volume
= in_volume
;
700 if( k_debug_light_indices
)
702 for( int i
=0; i
<world
->light_count
; i
++ ){
703 struct world_light
*light
= &world
->lights
[i
];
704 struct classtype_world_light
*inf
= light
->inf
;
706 u32 colour
= 0xff000000;
707 u8 r
= inf
->colour
[0] * 255.0f
,
708 g
= inf
->colour
[1] * 255.0f
,
709 b
= inf
->colour
[2] * 255.0f
;
715 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
723 * -----------------------------------------------------------------------------
725 * -----------------------------------------------------------------------------
728 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
729 ray_hit
*hit
, v3f tri
[3] )
731 for( int i
=0; i
<3; i
++ )
732 v3_copy( world
->scene_geo
.arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
735 VG_STATIC
int ray_world( world_instance
*world
,
736 v3f pos
, v3f dir
, ray_hit
*hit
)
738 return scene_raycast( &world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
742 * Cast a sphere from a to b and see what time it hits
744 VG_STATIC
int spherecast_world( world_instance
*world
,
745 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
748 bh_iter_init( 0, &it
);
751 box_init_inf( region
);
752 box_addpt( region
, pa
);
753 box_addpt( region
, pb
);
755 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
756 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
759 v3_sub( pb
, pa
, dir
);
762 dir_inv
[0] = 1.0f
/dir
[0];
763 dir_inv
[1] = 1.0f
/dir
[1];
764 dir_inv
[2] = 1.0f
/dir
[2];
770 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) ){
771 u32
*ptri
= &world
->scene_geo
.arrindices
[ idx
*3 ];
777 for( int j
=0; j
<3; j
++ ){
778 v3_copy( world
->scene_geo
.arrvertices
[ptri
[j
]].co
, tri
[j
] );
779 box_addpt( box
, tri
[j
] );
782 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
783 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
785 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
790 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) ){
804 struct world_surface
*world_tri_index_surface( world_instance
*world
,
807 for( int i
=1; i
<world
->surface_count
; i
++ ){
808 struct world_surface
*surf
= &world
->surfaces
[i
];
810 if( (index
>= surf
->sm_geo
.vertex_start
) &&
811 (index
< surf
->sm_geo
.vertex_start
+surf
->sm_geo
.vertex_count
) )
817 return &world
->surfaces
[0];
820 VG_STATIC
struct world_surface
*world_contact_surface( world_instance
*world
,
823 return world_tri_index_surface( world
, ct
->element_id
);
826 VG_STATIC
struct world_surface
*ray_hit_surface( world_instance
*world
,
829 return world_tri_index_surface( world
, hit
->tri
[0] );
833 * -----------------------------------------------------------------------------
835 * -----------------------------------------------------------------------------
839 enum audio_sprite_type
world_audio_sample_sprite_random(v3f origin
, v3f output
);
840 VG_STATIC
void world_audio_sample_distances( v3f co
, int *index
, float *value
);
845 * Trace out a random point, near the player to try and determine water areas
848 enum audio_sprite_type
world_audio_sample_sprite_random(v3f origin
, v3f output
)
850 v3f chance
= { (vg_randf()-0.5f
) * 30.0f
,
852 (vg_randf()-0.5f
) * 30.0f
};
855 v3_add( chance
, origin
, pos
);
858 contact
.dist
= vg_minf( 16.0f
, pos
[1] );
860 world_instance
*world
= get_active_world();
862 if( ray_world( world
, pos
, (v3f
){0.0f
,-1.0f
,0.0f
}, &contact
) ){
863 struct world_surface
*mat
= ray_hit_surface( world
, &contact
);
865 if( mat
->info
.surface_prop
== k_surface_prop_grass
){
866 v3_copy( contact
.pos
, output
);
867 return k_audio_sprite_type_grass
;
870 return k_audio_sprite_type_none
;
878 float dist
= fabsf(output
[1] - origin
[1]);
880 if( world
->water
.enabled
&& dist
<=40.0f
)
881 return k_audio_sprite_type_water
;
883 return k_audio_sprite_type_none
;
886 VG_STATIC
void world_audio_sample_distances( v3f co
, int *index
, float *value
)
888 float inr3
= 0.57735027,
889 inr2
= 0.70710678118;
891 v3f sample_directions
[] = {
892 { -1.0f
, 0.0f
, 0.0f
},
893 { 1.0f
, 0.0f
, 0.0f
},
894 { 0.0f
, 0.0f
, 1.0f
},
895 { 0.0f
, 0.0f
, -1.0f
},
896 { 0.0f
, 1.0f
, 0.0f
},
897 { 0.0f
, -1.0f
, 0.0f
},
898 { -inr3
, inr3
, inr3
},
899 { inr3
, inr3
, inr3
},
900 { -inr3
, inr3
, -inr3
},
901 { inr3
, inr3
, -inr3
},
902 { -inr2
, 0.0f
, inr2
},
903 { inr2
, 0.0f
, inr2
},
904 { -inr2
, 0.0f
, -inr2
},
905 { inr2
, 0.0f
, -inr2
},
909 static float distances
[16];
915 v3_copy( sample_directions
[ si
], rd
);
916 v3_add( co
, (v3f
){0.0f
,1.5f
,0.0f
}, ro
);
921 for( int i
=0; i
<10; i
++ ){
922 if( ray_world( get_active_world(), rc
, rd
, &ray
) ){
923 dist
= (float)i
*5.0f
+ ray
.dist
;
927 v3_muladds( rc
, rd
, ray
.dist
, rc
);
931 distances
[si
] = dist
;
934 for( int i
=0; i
<14; i
++ ){
935 if( distances
[i
] != 200.0f
){
936 u32 colours
[] = { VG__RED
, VG__BLUE
, VG__GREEN
,
937 VG__CYAN
, VG__YELOW
, VG__PINK
,
940 u32 colour
= colours
[i
%7];
943 v3_muladds( ro
, sample_directions
[i
], distances
[i
], p1
);
944 vg_line( ro
, p1
, colour
);
945 vg_line_pt3( p1
, 0.1f
, colour
);