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 * --------------------------------------------------------------------------
348 mdl_submesh dome_upper
, dome_lower
;
350 glmesh mesh_gate_surface
;
352 double sky_time
, sky_rate
, sky_target_rate
;
354 /* gates, TODO: active_gate should also know which instance */
357 double time
, rewind_from
, rewind_to
, last_use
;
359 /* water rendering */
362 struct framebuffer fbreflect
, fbdepth
;
366 /* split flap display */
369 mdl_submesh
*sm_module
, *sm_card
;
370 glmesh mesh_base
, mesh_display
;
377 /* timing bars, fixed maximum amount */
380 GLuint vao
, vbo
, ebo
;
385 struct route_ui_segment
388 u32 vertex_start
, vertex_count
,
389 index_start
, index_count
, notches
;
391 segments
[k_max_ui_segments
];
393 u32 segment_start
, segment_count
, fade_start
, fade_count
;
394 double fade_timer_start
;
400 int active_route_board
;
403 int switching_to_new_world
;
405 world_instance worlds
[4];
411 VG_STATIC world_instance
*get_active_world( void )
413 return &world_global
.worlds
[ world_global
.active_world
];
421 int ray_hit_is_ramp( world_instance
*world
, ray_hit
*hit
);
424 struct world_material
*ray_hit_material( world_instance
*world
, ray_hit
*hit
);
427 void ray_world_get_tri( world_instance
*world
, ray_hit
*hit
, v3f tri
[3] );
430 int ray_world( world_instance
*world
, v3f pos
, v3f dir
, ray_hit
*hit
);
436 #include "world_routes.h"
437 #include "world_sfd.h"
438 #include "world_render.h"
439 #include "world_water.h"
440 #include "world_gen.h"
441 #include "world_gate.h"
444 * -----------------------------------------------------------------------------
446 * -----------------------------------------------------------------------------
449 VG_STATIC
int world_stop_sound( int argc
, const char *argv
[] )
451 world_instance
*world
= get_active_world();
454 * None of our world audio runs as one shots, they always have a player.
455 * Therefore it is safe to delete clip data after the players are
459 for( int i
=0; i
<world
->audio_things_count
; i
++ )
461 struct world_audio_thing
*at
= &world
->audio_things
[i
];
463 if( audio_player_is_playing( &at
->player
) )
465 u32 cflags
= audio_player_get_flags( &at
->player
);
466 audio_player_set_flags( &at
->player
, cflags
| AUDIO_FLAG_KILL
);
474 VG_STATIC
int world_change_world( int argc
, const char *argv
[] )
477 world_instance
*world
= get_active_world();
481 vg_info( "%s\n", world
.world_name
);
486 vg_info( "Switching world...\n" );
487 strcpy( world
.world_name
, argv
[0] );
488 world
.switching_to_new_world
= 1;
489 world_stop_sound( 0, NULL
);
496 VG_STATIC
void world_init(void)
499 vg_var_push( (struct vg_var
){
500 .name
= "water_enable",
501 .data
= &world
.water
.enabled
,
502 .data_type
= k_var_dtype_i32
,
503 .opt_i32
= { .min
=0, .max
=1, .clamp
=1 },
508 vg_function_push( (struct vg_cmd
)
510 .name
= "world_stop_sound",
511 .function
= world_stop_sound
514 vg_function_push( (struct vg_cmd
)
517 .function
= world_change_world
520 world_global
.sky_rate
= 1.0;
521 world_global
.sky_target_rate
= 1.0;
523 shader_scene_standard_register();
524 shader_scene_standard_alphatest_register();
525 shader_scene_vertex_blend_register();
526 shader_scene_terrain_register();
527 shader_scene_depth_register();
528 shader_scene_position_register();
530 shader_model_sky_register();
532 vg_info( "Loading world resources\n" );
534 vg_linear_clear( vg_mem
.scratch
);
535 mdl_context
*msky
= mdl_load_full( vg_mem
.scratch
, "models/rs_skydome.mdl" );
537 mdl_node
*nupper
= mdl_node_from_name( msky
, "dome_complete" );
538 world_global
.dome_upper
= *mdl_node_submesh( msky
, nupper
, 0 );
540 vg_acquire_thread_sync();
542 mdl_unpack_glmesh( msky
, &world_global
.skydome
);
544 vg_release_thread_sync();
547 vg_info( "Loading other world systems\n" );
549 vg_loader_step( world_render_init
, NULL
);
550 vg_loader_step( world_sfd_init
, NULL
);
551 vg_loader_step( world_water_init
, NULL
);
552 vg_loader_step( world_gates_init
, NULL
);
553 vg_loader_step( world_routes_init
, NULL
);
555 /* Allocate dynamic world memory arena */
556 u32 max_size
= 76*1024*1024;
557 world_global
.generic_heap
= vg_create_linear_allocator( vg_mem
.rtmemory
,
562 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
,
574 VG_STATIC
void world_trigger_achievement( world_instance
*world
, u32 uid
)
576 struct logic_achievement
*ach
= &world
->logic_achievements
[ uid
];
581 steam_set_achievement( ach
->achievement_id
);
582 steam_store_achievements();
587 VG_STATIC
void world_run_relay( world_instance
*world
,
588 struct relay_target
*rt
);
590 VG_STATIC
void world_trigger_relay( world_instance
*world
, u32 uid
)
592 struct logic_relay
*relay
= &world
->logic_relays
[ uid
];
594 for( int i
=0; i
<relay
->target_count
; i
++ )
596 world_run_relay( world
, &relay
->targets
[i
] );
600 VG_STATIC
void world_trigger_audio( world_instance
*world
, u32 uid
)
602 struct world_audio_thing
*wat
= &world
->audio_things
[ uid
];
605 audio_player_playclip( &wat
->player
,
606 &wat
->temp_embedded_clip
);
610 VG_STATIC
void world_run_relay( world_instance
*world
,
611 struct relay_target
*rt
)
613 struct entity_instruction
615 enum classtype classtype
;
616 void (*p_trigger
)( world_instance
*world
, u32 uid
);
618 entity_instructions
[] =
620 { k_classtype_logic_achievement
, world_trigger_achievement
},
621 { k_classtype_logic_relay
, world_trigger_relay
},
622 { k_classtype_audio
, world_trigger_audio
}
625 for( int i
=0; i
<vg_list_size(entity_instructions
); i
++ )
627 struct entity_instruction
*instr
= &entity_instructions
[i
];
629 if( instr
->classtype
== rt
->classtype
)
631 instr
->p_trigger( world
, rt
->sub_id
);
636 vg_error( "Don't know how to trigger classtype %d\n", rt
->classtype
);
639 VG_STATIC
void world_update( world_instance
*world
, v3f pos
)
642 static double g_time
= 0.0;
643 g_time
+= vg
.time_delta
* (1.0/(k_day_length
*60.0));
646 struct ub_world_lighting
*state
= &world
->ub_lighting
;
648 state
->g_time
= g_time
;
649 state
->g_debug_indices
= k_debug_light_indices
;
650 state
->g_light_preview
= k_light_preview
;
651 state
->g_debug_complexity
= k_debug_light_complexity
;
653 state
->g_time_of_day
= vg_fractf( g_time
);
654 state
->g_day_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 2.0f
);
655 state
->g_sunset_phase
= cosf( state
->g_time_of_day
* VG_PIf
* 4.0f
+ VG_PIf
);
657 state
->g_day_phase
= state
->g_day_phase
* 0.5f
+ 0.5f
;
658 state
->g_sunset_phase
= powf( state
->g_sunset_phase
* 0.5f
+ 0.5f
, 6.0f
);
660 float a
= state
->g_time_of_day
* VG_PIf
* 2.0f
;
661 state
->g_sun_dir
[0] = sinf( a
);
662 state
->g_sun_dir
[1] = cosf( a
);
663 state
->g_sun_dir
[2] = 0.2f
;
664 v3_normalize( state
->g_sun_dir
);
668 glBindBuffer( GL_UNIFORM_BUFFER
, world
->ubo_lighting
);
669 glBufferSubData( GL_UNIFORM_BUFFER
, 0,
670 sizeof(struct ub_world_lighting
), &world
->ub_lighting
);
675 if( world
.switching_to_new_world
)
680 for( int i
=0; i
<world
.audio_things_count
; i
++ )
682 struct world_audio_thing
*at
= &world
.audio_things
[i
];
684 if( audio_player_is_playing( &at
->player
) )
694 world
.switching_to_new_world
= 0;
696 vg_loader_start( world_load
);
702 world_global
.sky_time
+= world_global
.sky_rate
* vg
.time_delta
;
703 world_global
.sky_rate
= vg_lerp( world_global
.sky_rate
,
704 world_global
.sky_target_rate
,
705 vg
.time_delta
* 5.0 );
707 world_routes_update( world
);
709 world_routes_debug();
712 if( world
->route_count
> 0 )
715 float min_dist
= INFINITY
;
717 for( int i
=0; i
<world
->route_count
; i
++ )
719 float d
= v3_dist2( world
->routes
[i
].scoreboard_transform
[3], pos
);
728 if( (world_global
.active_route_board
!= closest
)
729 || network_scores_updated
)
731 network_scores_updated
= 0;
732 world_global
.active_route_board
= closest
;
734 struct route
*route
= &world
->routes
[closest
];
736 u32 id
= route
->track_id
;
738 if( id
!= 0xffffffff )
740 struct netmsg_board
*local_board
=
741 &scoreboard_client_data
.boards
[id
];
743 for( int i
=0; i
<13; i
++ )
745 sfd_encode( i
, &local_board
->data
[27*i
] );
752 for( int i
=0; i
<world
->trigger_count
; i
++ )
754 struct trigger_zone
*zone
= &world
->triggers
[i
];
757 m4x3_mulv( zone
->inv_transform
, pos
, local
);
759 if( (fabsf(local
[0]) <= 1.0f
) &&
760 (fabsf(local
[1]) <= 1.0f
) &&
761 (fabsf(local
[2]) <= 1.0f
) )
765 if( !world_global
.in_trigger
)
767 world_run_relay( world
, &zone
->target
);
771 vg_line_boxf_transformed( zone
->transform
, (boxf
){{-1.0f
,-1.0f
,-1.0f
},
772 { 1.0f
, 1.0f
, 1.0f
}},
776 if( k_debug_light_indices
)
778 for( int i
=0; i
<world
->light_count
; i
++ )
780 struct world_light
*light
= &world
->lights
[i
];
781 struct classtype_world_light
*inf
= light
->inf
;
783 u32 colour
= 0xff000000;
784 u8 r
= inf
->colour
[0] * 255.0f
,
785 g
= inf
->colour
[1] * 255.0f
,
786 b
= inf
->colour
[2] * 255.0f
;
792 vg_line_pt3( light
->node
->co
, 0.25f
, colour
);
796 world_global
.in_trigger
= in_trigger
;
801 * -----------------------------------------------------------------------------
803 * -----------------------------------------------------------------------------
806 VG_STATIC
void ray_world_get_tri( world_instance
*world
,
807 ray_hit
*hit
, v3f tri
[3] )
809 for( int i
=0; i
<3; i
++ )
810 v3_copy( world
->scene_geo
->arrvertices
[ hit
->tri
[i
] ].co
, tri
[i
] );
813 VG_STATIC
int ray_world( world_instance
*world
,
814 v3f pos
, v3f dir
, ray_hit
*hit
)
816 return scene_raycast( world
->scene_geo
, world
->geo_bh
, pos
, dir
, hit
);
820 * Cast a sphere from a to b and see what time it hits
822 VG_STATIC
int spherecast_world( world_instance
*world
,
823 v3f pa
, v3f pb
, float r
, float *t
, v3f n
)
826 bh_iter_init( 0, &it
);
829 box_init_inf( region
);
830 box_addpt( region
, pa
);
831 box_addpt( region
, pb
);
833 v3_add( (v3f
){ r
, r
, r
}, region
[1], region
[1] );
834 v3_add( (v3f
){-r
,-r
,-r
}, region
[0], region
[0] );
837 v3_sub( pb
, pa
, dir
);
840 dir_inv
[0] = 1.0f
/dir
[0];
841 dir_inv
[1] = 1.0f
/dir
[1];
842 dir_inv
[2] = 1.0f
/dir
[2];
848 while( bh_next( world
->geo_bh
, &it
, region
, &idx
) )
850 u32
*ptri
= &world
->scene_geo
->arrindices
[ idx
*3 ];
856 for( int j
=0; j
<3; j
++ )
858 v3_copy( world
->scene_geo
->arrvertices
[ptri
[j
]].co
, tri
[j
] );
859 box_addpt( box
, tri
[j
] );
862 v3_add( (v3f
){ r
, r
, r
}, box
[1], box
[1] );
863 v3_add( (v3f
){-r
,-r
,-r
}, box
[0], box
[0] );
865 if( !ray_aabb1( box
, pa
, dir_inv
, 1.0f
) )
870 if( spherecast_triangle( tri
, pa
, dir
, r
, &t
, n1
) )
886 struct world_material
*world_tri_index_material( world_instance
*world
,
889 for( int i
=1; i
<world
->material_count
; i
++ )
891 struct world_material
*mat
= &world
->materials
[i
];
893 if( (index
>= mat
->sm_geo
.vertex_start
) &&
894 (index
< mat
->sm_geo
.vertex_start
+mat
->sm_geo
.vertex_count
) )
901 return &world
->materials
[0];
904 VG_STATIC
struct world_material
*world_contact_material( world_instance
*world
,
907 return world_tri_index_material( world
, ct
->element_id
);
910 VG_STATIC
struct world_material
*ray_hit_material( world_instance
*world
,
913 return world_tri_index_material( world
, hit
->tri
[0] );