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"
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 #include "shaders/scene_position.h"
29 #include "shaders/model_sky.h"
31 typedef struct teleport_gate teleport_gate
;
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
;
60 /* This is a small flag we use to changelevel.
61 * It will not be cleared until all sounds stop playing
65 * -------------------------------------------------------
68 char world_name
[ 64 ];
82 struct ub_world_lighting
91 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 * --------------------------------------------------------------------------
140 * Materials / textures
146 struct world_material
156 * Named safe places to respawn
168 * Audio player entities
170 struct world_audio_thing
177 audio_clip temp_embedded_clip
;
180 u32 audio_things_count
;
192 enum classtype classtype
;
201 * Box trigger entities
205 m4x3f transform
, inv_transform
;
207 struct relay_target target
;
215 struct logic_achievement
218 const char *achievement_id
;
221 * logic_achievements
;
222 u32 achievement_count
;
230 struct classtype_world_light
*inf
;
235 * TODO: we should order entities in the binary by their type */
241 * Routes (world_routes.h)
242 * --------------------------------------------------------------------------
246 v3f co
, right
, up
, h
;
249 u32 special_type
, special_id
, current_refs
, ref_count
;
250 u32 route_ids
[4]; /* Gates can be linked into up to four routes */
266 double best_lap
, latest_pass
; /* Session */
268 m4x3f scoreboard_transform
;
281 m4x3f to_world
, transport
;
289 u32 version
; /* Incremented on every teleport */
299 struct teleport_gate gate
;
302 u32 target_map_index
, working
;
305 u32 nonlocalgate_count
;
307 struct route_collector
309 struct route_timing timing
;
316 * ----------------------------------------------------
324 /* spacial mappings */
330 glmesh mesh_route_lines
;
335 rigidbody rb_geo
; /* todo.. ... */
338 VG_STATIC
struct world_global
341 * Allocated as system memory
342 * --------------------------------------------------------------------------
345 *audio_heap
; /* sub buffer of the audio buffer */
349 mdl_submesh dome_upper
, dome_lower
;
351 glmesh mesh_gate_surface
;
353 double sky_time
, sky_rate
, sky_target_rate
;
355 /* gates, TODO: active_gate should also know which instance */
358 double time
, rewind_from
, rewind_to
, last_use
;
360 /* water rendering */
363 struct framebuffer fbreflect
, fbdepth
;
367 /* split flap display */
370 mdl_submesh
*sm_module
, *sm_card
;
371 glmesh mesh_base
, mesh_display
;
378 /* timing bars, fixed maximum amount */
381 GLuint vao
, vbo
, ebo
;
386 struct route_ui_segment
389 u32 vertex_start
, vertex_count
,
390 index_start
, index_count
, notches
;
392 segments
[k_max_ui_segments
];
394 u32 segment_start
, segment_count
, fade_start
, fade_count
;
395 double fade_timer_start
;
401 int active_route_board
;
404 int switching_to_new_world
;
406 world_instance worlds
[4];
412 VG_STATIC world_instance
*get_active_world( void )
414 return &world_global
.worlds
[ world_global
.active_world
];
422 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
425 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
428 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
431 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
437 #include "world_routes.h"
438 #include "world_sfd.h"
439 #include "world_render.h"
440 #include "world_water.h"
441 #include "world_gen.h"
442 #include "world_gate.h"
445 * -----------------------------------------------------------------------------
447 * -----------------------------------------------------------------------------
450 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
452 world_instance
*world
= get_active_world();
455 * None of our world audio runs as one shots, they always have a player.
456 * Therefore it is safe to delete clip data after the players are
460 for( int i
=0; i
<world
->audio_things_count
; i
++ )
462 struct world_audio_thing
*at
= &world
->audio_things
[i
];
464 if( audio_player_is_playing( &at
->player
) )
466 u32 cflags
= audio_player_get_flags( &at
->player
);
467 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
475 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
478 world_instance
*world
= get_active_world();
482 vg_info( "%s\n", world
.world_name
);
487 vg_info( "Switching world...\n" );
488 strcpy( world
.world_name
, argv
[0] );
489 world
.switching_to_new_world
= 1;
490 world_stop_sound( 0, NULL
);
497 VG_STATIC
void world_init(void)
500 vg_var_push( (struct vg_var
){
501 .name
= "water_enable",
502 .data
= &world
.water
.enabled
,
503 .data_type
= k_var_dtype_i32
,
504 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
509 vg_function_push( (struct vg_cmd
)
511 .name
= "world_stop_sound",
512 .function
= world_stop_sound
515 vg_function_push( (struct vg_cmd
)
518 .function
= world_change_world
521 world_global
.sky_rate
= 1.0;
522 world_global
.sky_target_rate
= 1.0;
524 shader_scene_standard_register();
525 shader_scene_standard_alphatest_register();
526 shader_scene_vertex_blend_register();
527 shader_scene_terrain_register();
528 shader_scene_depth_register();
529 shader_scene_position_register();
531 shader_model_sky_register();
533 vg_info( "Loading world resources\n" );
535 vg_linear_clear( vg_mem
.scratch
);
536 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
538 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
539 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
541 vg_acquire_thread_sync();
543 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
545 vg_release_thread_sync();
548 vg_info( "Loading other world systems\n" );
550 vg_loader_step( world_render_init
, NULL
);
551 vg_loader_step( world_sfd_init
, NULL
);
552 vg_loader_step( world_water_init
, NULL
);
553 vg_loader_step( world_gates_init
, NULL
);
554 vg_loader_step( world_routes_init
, NULL
);
556 /* Allocate dynamic world memory arena */
557 u32 max_size
= 76*1024*1024;
558 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
563 VG_STATIC
void world_audio_init(void)
565 u32 size
= vg_linear_remaining( vg_audio
.audio_pool
)
566 - sizeof(vg_linear_allocator
);
568 world_global
.audio_heap
= vg_create_linear_allocator( vg_audio
.audio_pool
,
573 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
575 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
580 steam_set_achievement( ach
->achievement_id
);
581 steam_store_achievements();
586 VG_STATIC
void world_run_relay( world_instance
*world
,
587 struct relay_target
*rt
);
589 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
591 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
593 for( int i
=0; i
<relay
->target_count
; i
++ )
595 world_run_relay( world
, &relay
->targets
[i
] );
599 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
601 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
604 audio_player_playclip( &wat
->player
,
605 &wat
->temp_embedded_clip
);
609 VG_STATIC
void world_run_relay( world_instance
*world
,
610 struct relay_target
*rt
)
612 struct entity_instruction
614 enum classtype classtype
;
615 void (*p_trigger
)( world_instance
*world
, u32 uid
);
617 entity_instructions
[] =
619 { k_classtype_logic_achievement
, world_trigger_achievement
},
620 { k_classtype_logic_relay
, world_trigger_relay
},
621 { k_classtype_audio
, world_trigger_audio
}
624 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
626 struct entity_instruction
*instr
= &entity_instructions
[i
];
628 if( instr
->classtype
== rt
->classtype
)
630 instr
->p_trigger( world
, rt
->sub_id
);
635 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
638 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
641 static double g_time
= 0.0;
642 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
645 struct ub_world_lighting
*state
= &world
->ub_lighting
;
647 state
->g_time
= g_time
;
648 state
->g_debug_indices
= k_debug_light_indices
;
649 state
->g_light_preview
= k_light_preview
;
650 state
->g_debug_complexity
= k_debug_light_complexity
;
652 state
->g_time_of_day
= vg_fractf( g_time
);
653 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
654 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
656 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
657 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
659 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
660 state
->g_sun_dir
[0] = sinf( a
);
661 state
->g_sun_dir
[1] = cosf( a
);
662 state
->g_sun_dir
[2] = 0.2f
;
663 v3_normalize( state
->g_sun_dir
);
667 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
668 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
669 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
674 if( world
.switching_to_new_world
)
679 for( int i
=0; i
<world
.audio_things_count
; i
++ )
681 struct world_audio_thing
*at
= &world
.audio_things
[i
];
683 if( audio_player_is_playing( &at
->player
) )
693 world
.switching_to_new_world
= 0;
695 vg_loader_start( world_load
);
701 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
702 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
703 world_global
.sky_target_rate
,
704 vg
.time_delta
* 5.0 );
706 world_routes_update( world
);
708 world_routes_debug();
711 if( world
->route_count
> 0 )
714 float min_dist
= INFINITY
;
716 for( int i
=0; i
<world
->route_count
; i
++ )
718 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
727 if( (world_global
.active_route_board
!= closest
)
728 || network_scores_updated
)
730 network_scores_updated
= 0;
731 world_global
.active_route_board
= closest
;
733 struct route
*route
= &world
->routes
[closest
];
735 u32 id
= route
->track_id
;
737 if( id
!= 0xffffffff )
739 struct netmsg_board
*local_board
=
740 &scoreboard_client_data
.boards
[id
];
742 for( int i
=0; i
<13; i
++ )
744 sfd_encode( i
, &local_board
->data
[27*i
] );
751 for( int i
=0; i
<world
->trigger_count
; i
++ )
753 struct trigger_zone
*zone
= &world
->triggers
[i
];
756 m4x3_mulv( zone
->inv_transform
, pos
, local
);
758 if( (fabsf(local
[0]) <= 1.0f
) &&
759 (fabsf(local
[1]) <= 1.0f
) &&
760 (fabsf(local
[2]) <= 1.0f
) )
764 if( !world_global
.in_trigger
)
766 world_run_relay( world
, &zone
->target
);
770 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
771 { 1.0f
, 1.0f
, 1.0f
}},
775 if( k_debug_light_indices
)
777 for( int i
=0; i
<world
->light_count
; i
++ )
779 struct world_light
*light
= &world
->lights
[i
];
780 struct classtype_world_light
*inf
= light
->inf
;
782 u32 colour
= 0xff000000;
783 u8 r
= inf
->colour
[0] * 255.0f
,
784 g
= inf
->colour
[1] * 255.0f
,
785 b
= inf
->colour
[2] * 255.0f
;
791 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
795 world_global
.in_trigger
= in_trigger
;
800 * -----------------------------------------------------------------------------
802 * -----------------------------------------------------------------------------
805 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
806 ray_hit
*hit
, v3f tri
[3] )
808 for( int i
=0; i
<3; i
++ )
809 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
812 VG_STATIC
int ray_world( world_instance
*world
,
813 v3f pos
, v3f dir
, ray_hit
*hit
)
815 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
819 * Cast a sphere from a to b and see what time it hits
821 VG_STATIC
int spherecast_world( world_instance
*world
,
822 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
825 bh_iter_init( 0, &it
);
828 box_init_inf( region
);
829 box_addpt( region
, pa
);
830 box_addpt( region
, pb
);
832 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
833 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
836 v3_sub( pb
, pa
, dir
);
839 dir_inv
[0] = 1.0f
/dir
[0];
840 dir_inv
[1] = 1.0f
/dir
[1];
841 dir_inv
[2] = 1.0f
/dir
[2];
847 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
849 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
855 for( int j
=0; j
<3; j
++ )
857 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
858 box_addpt( box
, tri
[j
] );
861 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
862 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
864 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
869 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
885 struct world_material
*world_tri_index_material( world_instance
*world
,
888 for( int i
=1; i
<world
->material_count
; i
++ )
890 struct world_material
*mat
= &world
->materials
[i
];
892 if( (index
>= mat
->sm_geo
.vertex_start
) &&
893 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
900 return &world
->materials
[0];
903 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
906 return world_tri_index_material( world
, ct
->element_id
);
909 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
912 return world_tri_index_material( world
, hit
->tri
[0] );