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 * -------------------------------------------------------
63 char world_name
[ 64 ];
76 struct ub_world_lighting
{
84 v4f g_nightsky_colour
;
94 float g_shadow_length
;
95 float g_shadow_spread
;
102 int g_shadow_samples
;
105 int g_debug_complexity
;
111 GLuint tbo_light_entities
,
115 float probabilities
[3];
119 struct framebuffer heightmap
;
122 * Dynamically allocated when world_load is called.
124 * the following arrays index somewhere into this linear
128 * --------------------------------------------------------------------------
139 struct world_surface
{
147 mdl_array_ptr ent_spawn
,
160 ent_gate
*rendering_gate
;
163 * ----------------------------------------------------
171 /* spacial mappings */
177 glmesh mesh_route_lines
;
185 VG_STATIC
struct world_global
{
187 * Allocated as system memory
188 * --------------------------------------------------------------------------
195 mdl_submesh sm_gate_surface
,
198 double sky_time
, sky_rate
, sky_target_rate
;
200 u32 current_run_version
;
201 double time
, rewind_from
, rewind_to
, last_use
;
203 /* water rendering */
205 struct framebuffer fbreflect
, fbdepth
;
209 /* split flap display */
211 glmesh mesh_base
, mesh_display
;
213 u32 active_route_board
;
223 int switching_to_new_world
;
225 world_instance worlds
[4];
239 u32 timer_text_count
;
241 struct text_particle
{
246 u32 text_particle_count
;
251 VG_STATIC world_instance
*get_active_world( void )
253 return &world_global
.worlds
[ world_global
.active_world
];
261 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
264 struct world_surface
*ray_hit_surface( world_instance
*world
, ray_hit
*hit
);
267 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
270 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
276 #include "world_routes.h"
277 #include "world_sfd.h"
278 #include "world_render.h"
279 #include "world_water.h"
280 #include "world_volumes.h"
281 #include "world_gen.h"
282 #include "world_gate.h"
285 * -----------------------------------------------------------------------------
287 * -----------------------------------------------------------------------------
290 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
292 world_instance
*world
= get_active_world();
296 VG_STATIC
void world_init(void)
298 world_global
.sky_rate
= 1.0;
299 world_global
.sky_target_rate
= 1.0;
301 shader_scene_standard_register();
302 shader_scene_standard_alphatest_register();
303 shader_scene_vertex_blend_register();
304 shader_scene_terrain_register();
305 shader_scene_depth_register();
306 shader_scene_position_register();
308 shader_model_sky_register();
310 vg_info( "Loading world resources\n" );
312 vg_linear_clear( vg_mem
.scratch
);
315 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
316 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
317 mdl_load_mesh_block( &msky
, vg_mem
.scratch
);
320 vg_acquire_thread_sync();
322 mdl_unpack_glmesh( &msky
, &world_global
.skydome
);
324 vg_release_thread_sync();
327 vg_info( "Loading other world systems\n" );
329 vg_loader_step( world_render_init
, NULL
);
330 vg_loader_step( world_sfd_init
, NULL
);
331 vg_loader_step( world_water_init
, NULL
);
332 vg_loader_step( world_gates_init
, NULL
);
333 vg_loader_step( world_routes_init
, NULL
);
335 /* Allocate dynamic world memory arena */
336 u32 max_size
= 76*1024*1024;
337 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
342 typedef struct ent_call ent_call
;
349 VG_STATIC
void entity_call( world_instance
*world
, ent_call
*call
);
351 VG_STATIC
void ent_volume_call( world_instance
*world
, ent_call
*call
)
353 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, call
->ent
.index
);
354 if( !volume
->target
.type
) return;
356 if( call
->function
== k_ent_function_trigger
){
357 call
->ent
= volume
->target
;
359 if( volume
->type
== k_volume_subtype_particle
){
360 float *co
= alloca( sizeof(float)*3 );
361 co
[0] = vg_randf()*2.0f
-1.0f
;
362 co
[1] = vg_randf()*2.0f
-1.0f
;
363 co
[2] = vg_randf()*2.0f
-1.0f
;
364 m4x3_mulv( volume
->to_world
, co
, co
);
366 call
->function
= k_ent_function_particle_spawn
;
368 entity_call( world
, call
);
371 entity_call( world
, call
);
375 VG_STATIC
void ent_audio_call( world_instance
*world
, ent_call
*call
)
377 ent_audio
*audio
= mdl_arritm( &world
->ent_audio
, call
->ent
.index
);
381 if( call
->function
== k_ent_function_particle_spawn
){
382 v3_copy( call
->data
, sound_co
);
384 else if( call
->function
== k_ent_function_trigger
){
385 v3_copy( audio
->transform
.co
, sound_co
);
388 vg_fatal_exit_loop( "ent_audio_call (invalid function id)" );
390 float chance
= vg_randf()*100.0f
,
393 for( u32 i
=0; i
<audio
->clip_count
; i
++ ){
394 ent_audio_clip
*clip
= mdl_arritm( &world
->ent_audio_clip
,
395 audio
->clip_start
+i
);
397 float mod
= world
->probabilities
[ audio
->probability_curve
],
398 p
= clip
->probability
* mod
;
406 if( audio
->behaviour
== k_channel_behaviour_unlimited
){
407 audio_oneshot_3d( &clip
->clip
, sound_co
,
408 audio
->transform
.s
[0],
411 else if( audio
->behaviour
== k_channel_behaviour_discard_if_full
){
413 audio_get_group_idle_channel( audio
->group
,
414 audio
->max_channels
);
417 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
418 audio_channel_group( ch
, audio
->group
);
419 audio_channel_set_spacial( ch
, sound_co
, audio
->transform
.s
[0] );
420 audio_channel_edit_volume( ch
, audio
->volume
, 1 );
421 ch
= audio_relinquish_channel( ch
);
424 else if( audio
->behaviour
== k_channel_behaviour_crossfade_if_full
){
426 audio_get_group_idle_channel( audio
->group
,
427 audio
->max_channels
);
431 audio_channel
*existing
=
432 audio_get_group_first_active_channel( audio
->group
);
435 if( existing
->source
== &clip
->clip
){
441 existing
= audio_channel_fadeout(existing
, audio
->crossfade
);
444 ch
= audio_get_first_idle_channel();
448 audio_channel_init( ch
, &clip
->clip
, audio
->flags
);
449 audio_channel_group( ch
, audio
->group
);
450 audio_channel_fadein( ch
, audio
->crossfade
);
451 ch
= audio_relinquish_channel( ch
);
461 VG_STATIC
void entity_call( world_instance
*world
, ent_call
*call
)
463 if( call
->ent
.type
== k_ent_volume
){
464 ent_volume_call( world
, call
);
465 } else if( call
->ent
.type
== k_ent_audio
){
466 ent_audio_call( world
, call
);
470 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
473 static double g_time
= 0.0;
474 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
477 struct ub_world_lighting
*state
= &world
->ub_lighting
;
479 state
->g_time
= g_time
;
480 state
->g_realtime
= vg
.time
;
481 state
->g_debug_indices
= k_debug_light_indices
;
482 state
->g_light_preview
= k_light_preview
;
483 state
->g_debug_complexity
= k_debug_light_complexity
;
485 state
->g_time_of_day
= vg_fractf( g_time
);
486 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
487 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
489 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
490 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
492 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
493 state
->g_sun_dir
[0] = sinf( a
);
494 state
->g_sun_dir
[1] = cosf( a
);
495 state
->g_sun_dir
[2] = 0.2f
;
496 v3_normalize( state
->g_sun_dir
);
499 world
->probabilities
[ k_probability_curve_constant
] = 1.0f
;
501 float dp
= state
->g_day_phase
;
503 world
->probabilities
[ k_probability_curve_wildlife_day
] =
504 (dp
*dp
*0.8f
+state
->g_sunset_phase
)*0.8f
;
505 world
->probabilities
[ k_probability_curve_wildlife_night
] =
506 1.0f
-powf(fabsf((state
->g_time_of_day
-0.5f
)*5.0f
),5.0f
);
509 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
510 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
511 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
514 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
515 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
516 world_global
.sky_target_rate
,
517 vg
.time_delta
* 5.0 );
519 world_routes_update_timer_texts( world
);
520 world_routes_update( world
);
521 //world_routes_debug( world );
523 /* ---- SFD ------------ */
525 if( mdl_arrcount( &world
->ent_route
) ){
527 float min_dist
= INFINITY
;
529 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
530 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
531 float dist
= v3_dist2( route
->board_transform
[3], pos
);
533 if( dist
< min_dist
){
539 if( (world_global
.sfd
.active_route_board
!= closest
)
540 || network_scores_updated
)
542 network_scores_updated
= 0;
543 world_global
.sfd
.active_route_board
= closest
;
545 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
546 u32 id
= route
->official_track_id
;
548 if( id
!= 0xffffffff ){
549 struct netmsg_board
*local_board
=
550 &scoreboard_client_data
.boards
[id
];
552 for( int i
=0; i
<13; i
++ ){
553 sfd_encode( i
, &local_board
->data
[27*i
] );
556 sfd_encode( 0, mdl_pstr( &world
->meta
, route
->pstr_name
) );
557 sfd_encode( 1, "No data" );
565 static float random_accum
= 0.0f
;
566 random_accum
+= vg
.time_delta
;
568 u32 random_ticks
= 0;
570 while( random_accum
> 0.1f
){
571 random_accum
-= 0.1f
;
575 float radius
= 25.0f
;
576 boxf volume_proximity
;
577 v3_add( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[1] );
578 v3_sub( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[0] );
581 bh_iter_init( 0, &it
);
586 while( bh_next( world
->volume_bh
, &it
, volume_proximity
, &idx
) ){
587 ent_volume
*volume
= mdl_arritm( &world
->ent_volume
, idx
);
589 boxf cube
= {{-1.0f
,-1.0f
,-1.0f
},{1.0f
,1.0f
,1.0f
}};
591 if( volume
->type
== k_volume_subtype_trigger
){
593 m4x3_mulv( volume
->to_local
, pos
, local
);
595 if( (fabsf(local
[0]) <= 1.0f
) &&
596 (fabsf(local
[1]) <= 1.0f
) &&
597 (fabsf(local
[2]) <= 1.0f
) )
600 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00ff00 );
602 if( !world_global
.in_volume
){
604 basecall
.ent
.index
= idx
;
605 basecall
.ent
.type
= k_ent_volume
;
606 basecall
.function
= k_ent_function_trigger
;
607 basecall
.data
= NULL
;
609 entity_call( world
, &basecall
);
613 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff0000ff );
615 else if( volume
->type
== k_volume_subtype_particle
){
616 vg_line_boxf_transformed( volume
->to_world
, cube
, 0xff00c0ff );
618 for( int j
=0; j
<random_ticks
; j
++ ){
620 basecall
.ent
.index
= idx
;
621 basecall
.ent
.type
= k_ent_volume
;
622 basecall
.function
= k_ent_function_trigger
;
623 basecall
.data
= NULL
;
625 entity_call( world
, &basecall
);
629 world_global
.in_volume
= in_volume
;
632 if( k_debug_light_indices
)
634 for( int i
=0; i
<world
->light_count
; i
++ ){
635 struct world_light
*light
= &world
->lights
[i
];
636 struct classtype_world_light
*inf
= light
->inf
;
638 u32 colour
= 0xff000000;
639 u8 r
= inf
->colour
[0] * 255.0f
,
640 g
= inf
->colour
[1] * 255.0f
,
641 b
= inf
->colour
[2] * 255.0f
;
647 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
655 /* process soundscape transactions */
657 for( int i
=0; i
<world
->soundscape_count
; i
++ )
659 struct soundscape
*s
= &world
->soundscapes
[i
];
662 for( int j
=0; j
<s
->max_instances
; j
++ )
666 if( audio_channel_finished(s
->channels
[j
]) )
667 s
->channels
[j
] = audio_relinquish_channel( s
->channels
[j
] );
678 * -----------------------------------------------------------------------------
680 * -----------------------------------------------------------------------------
683 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
684 ray_hit
*hit
, v3f tri
[3] )
686 for( int i
=0; i
<3; i
++ )
687 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
690 VG_STATIC
int ray_world( world_instance
*world
,
691 v3f pos
, v3f dir
, ray_hit
*hit
)
693 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
697 * Cast a sphere from a to b and see what time it hits
699 VG_STATIC
int spherecast_world( world_instance
*world
,
700 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
703 bh_iter_init( 0, &it
);
706 box_init_inf( region
);
707 box_addpt( region
, pa
);
708 box_addpt( region
, pb
);
710 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
711 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
714 v3_sub( pb
, pa
, dir
);
717 dir_inv
[0] = 1.0f
/dir
[0];
718 dir_inv
[1] = 1.0f
/dir
[1];
719 dir_inv
[2] = 1.0f
/dir
[2];
725 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) ){
726 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
732 for( int j
=0; j
<3; j
++ ){
733 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
734 box_addpt( box
, tri
[j
] );
737 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
738 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
740 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
745 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) ){
759 struct world_surface
*world_tri_index_surface( world_instance
*world
,
762 for( int i
=1; i
<world
->surface_count
; i
++ ){
763 struct world_surface
*surf
= &world
->surfaces
[i
];
765 if( (index
>= surf
->sm_geo
.vertex_start
) &&
766 (index
< surf
->sm_geo
.vertex_start
+surf
->sm_geo
.vertex_count
) )
772 return &world
->surfaces
[0];
775 VG_STATIC
struct world_surface
*world_contact_surface( world_instance
*world
,
778 return world_tri_index_surface( world
, ct
->element_id
);
781 VG_STATIC
struct world_surface
*ray_hit_surface( world_instance
*world
,
784 return world_tri_index_surface( world
, hit
->tri
[0] );