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
,
123 struct framebuffer heightmap
;
126 * Dynamically allocated when world_load is called.
128 * the following arrays index somewhere into this linear
132 * --------------------------------------------------------------------------
151 mdl_array_ptr ent_spawn
,
160 ent_gate
*rendering_gate
;
164 * Named safe places to respawn
176 * Audio player entities
178 struct world_audio_thing
183 audio_clip temp_embedded_clip
;
186 u32 audio_things_count
;
191 audio_channel
*channels
[4];
193 /* accessable without locking */
198 u32 allow_transitions
;
199 float transition_duration
;
203 u32 soundscape_count
;
206 * Box volume entities
210 m4x3f transform
, inv_transform
;
222 struct classtype_world_light
*inf
;
230 * Routes (world_routes.h)
231 * --------------------------------------------------------------------------
236 v3f co
, right
, up
, h
;
239 u32 special_type
, special_id
, current_refs
, ref_count
;
240 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
256 double best_lap
, latest_pass
; /* Session */
258 m4x3f scoreboard_transform
;
271 m4x3f to_world
, transport
;
279 u32 version
; /* Incremented on every teleport */
289 struct teleport_gate gate
;
292 u32 target_map_index
, working
;
295 u32 nonlocalgate_count
;
297 struct route_collector
299 struct route_timing timing
;
307 * ----------------------------------------------------
315 /* spacial mappings */
321 glmesh mesh_route_lines
;
326 rigidbody rb_geo
; /* todo.. ... */
329 VG_STATIC
struct world_global
332 * Allocated as system memory
333 * --------------------------------------------------------------------------
340 mdl_submesh sm_gate_surface
,
343 double sky_time
, sky_rate
, sky_target_rate
;
345 u32 current_run_version
;
346 double time
, rewind_from
, rewind_to
, last_use
;
348 /* water rendering */
351 struct framebuffer fbreflect
, fbdepth
;
355 /* split flap display */
358 glmesh mesh_base
, mesh_display
;
360 u32 active_route_board
;
370 int switching_to_new_world
;
372 world_instance worlds
[4];
378 VG_STATIC world_instance
*get_active_world( void )
380 return &world_global
.worlds
[ world_global
.active_world
];
388 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
391 struct world_surface
*ray_hit_surface( world_instance
*world
, ray_hit
*hit
);
394 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
397 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
403 #include "world_routes.h"
404 #include "world_sfd.h"
405 #include "world_render.h"
406 #include "world_water.h"
407 #include "world_volumes.h"
408 #include "world_gen.h"
409 #include "world_gate.h"
412 * -----------------------------------------------------------------------------
414 * -----------------------------------------------------------------------------
417 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
419 world_instance
*world
= get_active_world();
423 VG_STATIC
void world_init(void)
425 world_global
.sky_rate
= 1.0;
426 world_global
.sky_target_rate
= 1.0;
428 shader_scene_standard_register();
429 shader_scene_standard_alphatest_register();
430 shader_scene_vertex_blend_register();
431 shader_scene_terrain_register();
432 shader_scene_depth_register();
433 shader_scene_position_register();
435 shader_model_sky_register();
437 vg_info( "Loading world resources\n" );
439 vg_linear_clear( vg_mem
.scratch
);
442 mdl_open( &msky
, "models/rs_skydome.mdl", vg_mem
.scratch
);
443 mdl_load_metadata_block( &msky
, vg_mem
.scratch
);
444 mdl_load_mesh_block( &msky
, vg_mem
.scratch
);
447 vg_acquire_thread_sync();
449 mdl_unpack_glmesh( &msky
, &world_global
.skydome
);
451 vg_release_thread_sync();
454 vg_info( "Loading other world systems\n" );
456 vg_loader_step( world_render_init
, NULL
);
457 vg_loader_step( world_sfd_init
, NULL
);
458 vg_loader_step( world_water_init
, NULL
);
459 vg_loader_step( world_gates_init
, NULL
);
460 vg_loader_step( world_routes_init
, NULL
);
462 /* Allocate dynamic world memory arena */
463 u32 max_size
= 76*1024*1024;
464 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
469 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
472 static double g_time
= 0.0;
473 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
476 struct ub_world_lighting
*state
= &world
->ub_lighting
;
478 state
->g_time
= g_time
;
479 state
->g_realtime
= vg
.time
;
480 state
->g_debug_indices
= k_debug_light_indices
;
481 state
->g_light_preview
= k_light_preview
;
482 state
->g_debug_complexity
= k_debug_light_complexity
;
484 state
->g_time_of_day
= vg_fractf( g_time
);
485 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
486 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
488 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
489 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
491 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
492 state
->g_sun_dir
[0] = sinf( a
);
493 state
->g_sun_dir
[1] = cosf( a
);
494 state
->g_sun_dir
[2] = 0.2f
;
495 v3_normalize( state
->g_sun_dir
);
499 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
500 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
501 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
504 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
505 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
506 world_global
.sky_target_rate
,
507 vg
.time_delta
* 5.0 );
509 world_routes_update( world
);
510 world_routes_debug( world
);
512 /* ---- SFD ------------ */
514 if( mdl_arrcount( &world
->ent_route
) ){
516 float min_dist
= INFINITY
;
518 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
519 ent_route
*route
= mdl_arritm( &world
->ent_route
, i
);
520 float dist
= v3_dist2( route
->board_transform
[3], pos
);
522 if( dist
< min_dist
){
528 if( (world_global
.sfd
.active_route_board
!= closest
)
529 || network_scores_updated
)
531 network_scores_updated
= 0;
532 world_global
.sfd
.active_route_board
= closest
;
534 ent_route
*route
= mdl_arritm( &world
->ent_route
, closest
);
535 u32 id
= route
->official_track_id
;
537 if( id
!= 0xffffffff ){
538 struct netmsg_board
*local_board
=
539 &scoreboard_client_data
.boards
[id
];
541 for( int i
=0; i
<13; i
++ ){
542 sfd_encode( i
, &local_board
->data
[27*i
] );
545 sfd_encode( 0, mdl_pstr( &world
->meta
, route
->pstr_name
) );
546 sfd_encode( 1, "No data" );
555 static float random_accum
= 0.0f
;
556 random_accum
+= vg
.time_delta
;
558 u32 random_ticks
= 0;
560 while( random_accum
> 0.1f
){
561 random_accum
-= 0.1f
;
565 float radius
= 25.0f
;
566 boxf volume_proximity
;
567 v3_add( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[1] );
568 v3_sub( pos
, (v3f
){ radius
, radius
, radius
}, volume_proximity
[0] );
571 bh_iter_init( 0, &it
);
576 while( bh_next( world
->volume_bh
, &it
, volume_proximity
, &idx
) )
578 struct world_volume
*zone
= &world
->volumes
[idx
];
580 if( zone
->node
->classtype
== k_classtype_volume_audio
)
582 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
583 { 1.0f
, 1.0f
, 1.0f
}},
586 for( int j
=0; j
<random_ticks
; j
++ )
589 packet
.location
= zone
->target_logic_brick
;
592 packet
.type
= k_mdl_128bit_datatype_vec3
;
593 packet
.data
._v4f
[0] = vg_randf()*2.0f
-1.0f
;
594 packet
.data
._v4f
[1] = vg_randf()*2.0f
-1.0f
;
595 packet
.data
._v4f
[2] = vg_randf()*2.0f
-1.0f
;
596 m4x3_mulv( zone
->transform
, packet
.data
._v4f
, packet
.data
._v4f
);
598 logic_bricks_send_packet( world
, &packet
);
605 m4x3_mulv( zone
->inv_transform
, pos
, local
);
607 if( (fabsf(local
[0]) <= 1.0f
) &&
608 (fabsf(local
[1]) <= 1.0f
) &&
609 (fabsf(local
[2]) <= 1.0f
) )
613 if( !world_global
.in_volume
)
617 packet
.location
= zone
->target_logic_brick
;
620 packet
.type
= k_mdl_128bit_datatype_vec3
;
621 v3_copy( pos
, packet
.data
._v4f
);
623 logic_bricks_send_packet( world
, &packet
);
627 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
628 { 1.0f
, 1.0f
, 1.0f
}},
633 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
634 { 1.0f
, 1.0f
, 1.0f
}},
641 if( k_debug_light_indices
)
643 for( int i
=0; i
<world
->light_count
; i
++ ){
644 struct world_light
*light
= &world
->lights
[i
];
645 struct classtype_world_light
*inf
= light
->inf
;
647 u32 colour
= 0xff000000;
648 u8 r
= inf
->colour
[0] * 255.0f
,
649 g
= inf
->colour
[1] * 255.0f
,
650 b
= inf
->colour
[2] * 255.0f
;
656 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
660 world_global
.in_volume
= in_volume
;
665 /* process soundscape transactions */
667 for( int i
=0; i
<world
->soundscape_count
; i
++ )
669 struct soundscape
*s
= &world
->soundscapes
[i
];
672 for( int j
=0; j
<s
->max_instances
; j
++ )
676 if( audio_channel_finished(s
->channels
[j
]) )
677 s
->channels
[j
] = audio_relinquish_channel( s
->channels
[j
] );
688 * -----------------------------------------------------------------------------
690 * -----------------------------------------------------------------------------
693 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
694 ray_hit
*hit
, v3f tri
[3] )
696 for( int i
=0; i
<3; i
++ )
697 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
700 VG_STATIC
int ray_world( world_instance
*world
,
701 v3f pos
, v3f dir
, ray_hit
*hit
)
703 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
707 * Cast a sphere from a to b and see what time it hits
709 VG_STATIC
int spherecast_world( world_instance
*world
,
710 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
713 bh_iter_init( 0, &it
);
716 box_init_inf( region
);
717 box_addpt( region
, pa
);
718 box_addpt( region
, pb
);
720 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
721 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
724 v3_sub( pb
, pa
, dir
);
727 dir_inv
[0] = 1.0f
/dir
[0];
728 dir_inv
[1] = 1.0f
/dir
[1];
729 dir_inv
[2] = 1.0f
/dir
[2];
735 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) ){
736 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
742 for( int j
=0; j
<3; j
++ ){
743 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
744 box_addpt( box
, tri
[j
] );
747 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
748 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
750 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
755 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) ){
769 struct world_surface
*world_tri_index_surface( world_instance
*world
,
772 for( int i
=1; i
<world
->surface_count
; i
++ ){
773 struct world_surface
*surf
= &world
->surfaces
[i
];
775 if( (index
>= surf
->sm_geo
.vertex_start
) &&
776 (index
< surf
->sm_geo
.vertex_start
+surf
->sm_geo
.vertex_count
) )
782 return &world
->surfaces
[0];
785 VG_STATIC
struct world_surface
*world_contact_surface( world_instance
*world
,
788 return world_tri_index_surface( world
, ct
->element_id
);
791 VG_STATIC
struct world_surface
*ray_hit_surface( world_instance
*world
,
794 return world_tri_index_surface( world
, hit
->tri
[0] );