- audio_lock();
- for( int i=0; i<world.audio_things_count; i++ )
- {
- struct world_audio_thing *at = &world.audio_things[i];
-
- if( audio_player_is_playing( &at->player ) )
- {
- u32 cflags = audio_player_get_flags( &at->player );
- audio_player_set_flags( &at->player, cflags | AUDIO_FLAG_KILL );
- }
- }
- audio_unlock();
-
- return 0;
-}
-
-VG_STATIC int world_change_world( int argc, const char *argv[] )
-{
- if( argc == 0 )
- {
- vg_info( "%s\n", world.world_name );
- return 0;
- }
- else
- {
- vg_info( "Switching world...\n" );
- strcpy( world.world_name, argv[0] );
- world.switching_to_new_world = 1;
- world_stop_sound( 0, NULL );
- }
-
- return 0;
-}
-
-VG_STATIC void world_init(void)
-{
- vg_var_push( (struct vg_var){
- .name = "water_enable",
- .data = &world.water.enabled,
- .data_type = k_var_dtype_i32,
- .opt_i32 = { .min=0, .max=1, .clamp=1 },
- .persistent = 0
- });
-
- vg_function_push( (struct vg_cmd)
- {
- .name = "world_stop_sound",
- .function = world_stop_sound
- });
-
- vg_function_push( (struct vg_cmd)
- {
- .name = "world",
- .function = world_change_world
- });
-
- world.sky_rate = 1.0;
- world.sky_target_rate = 1.0;
-
- shader_scene_standard_register();
- shader_scene_standard_alphatest_register();
- shader_scene_vertex_blend_register();
- shader_scene_terrain_register();
- shader_scene_depth_register();
-
- shader_model_sky_register();
-
- vg_info( "Loading world resources\n" );
-
- vg_linear_clear( vg_mem.scratch );
- mdl_context *msky = mdl_load_full( vg_mem.scratch, "models/rs_skydome.mdl" );
-
- mdl_node *nupper = mdl_node_from_name( msky, "dome_complete" );
- world.dome_upper = *mdl_node_submesh( msky, nupper, 0 );
-
- vg_acquire_thread_sync();
- {
- mdl_unpack_glmesh( msky, &world.skydome );
- }
- vg_release_thread_sync();
-
- /* Other systems */
- vg_info( "Loading other world systems\n" );
-
- vg_loader_step( world_render_init, NULL );
- vg_loader_step( world_sfd_init, NULL );
- vg_loader_step( world_water_init, NULL );
- vg_loader_step( world_gates_init, NULL );
- vg_loader_step( world_routes_init, NULL );
-
- /* Allocate dynamic world memory arena */
- u32 max_size = 76*1024*1024;
- world.dynamic_vgl = vg_create_linear_allocator( vg_mem.rtmemory, max_size,
- VG_MEMORY_SYSTEM );
-}
-
-VG_STATIC void world_audio_init(void)
-{
- u32 size = vg_linear_remaining( vg_audio.audio_pool )
- - sizeof(vg_linear_allocator);
-
- world.audio_vgl = vg_create_linear_allocator( vg_audio.audio_pool,
- size, VG_MEMORY_SYSTEM );
-}
-
-VG_STATIC void world_trigger_achievement( u32 uid )
-{
- struct logic_achievement *ach = &world.logic_achievements[ uid ];
-
- if( ach->achieved )
- return;
-
- steam_set_achievement( ach->achievement_id );
- steam_store_achievements();
-
- ach->achieved = 1;
-}
-
-VG_STATIC void world_run_relay( struct relay_target *rt );
-VG_STATIC void world_trigger_relay( u32 uid )
-{
- struct logic_relay *relay = &world.logic_relays[ uid ];
-
- for( int i=0; i<relay->target_count; i++ )
- {
- world_run_relay( &relay->targets[i] );
- }
-}
-
-VG_STATIC void world_trigger_audio( u32 uid )
-{
- struct world_audio_thing *wat = &world.audio_things[ uid ];
-
- audio_lock();
- audio_player_playclip( &wat->player,
- &wat->temp_embedded_clip );
- audio_unlock();
-}
-
-VG_STATIC void world_run_relay( struct relay_target *rt )
-{
- struct entity_instruction
- {
- enum classtype classtype;
- void (*p_trigger)( u32 uid );
- }
- entity_instructions[] =
- {
- { k_classtype_logic_achievement, world_trigger_achievement },
- { k_classtype_logic_relay, world_trigger_relay },
- { k_classtype_audio, world_trigger_audio }
- };
-
- for( int i=0; i<vg_list_size(entity_instructions); i++ )
- {
- struct entity_instruction *instr = &entity_instructions[i];
-
- if( instr->classtype == rt->classtype )
- {
- instr->p_trigger( rt->sub_id );
- return;
- }
- }
-
- vg_error( "Don't know how to trigger classtype %d\n", rt->classtype );
-}
-
-VG_STATIC void world_update( v3f pos )
-{
- if( world.switching_to_new_world )
- {
- int all_stopped = 1;
-
- audio_lock();
- for( int i=0; i<world.audio_things_count; i++ )
- {
- struct world_audio_thing *at = &world.audio_things[i];
-
- if( audio_player_is_playing( &at->player ) )
- {
- all_stopped = 0;
- break;
- }
- }
- audio_unlock();
-
- if( all_stopped )
- {
- world.switching_to_new_world = 0;
- world_unload();
- vg_loader_start( world_load );
- return;
- }
- }
-
- world.sky_time += world.sky_rate * vg.time_delta;
- world.sky_rate = vg_lerp( world.sky_rate, world.sky_target_rate,
- vg.time_delta * 5.0 );
-
- world_routes_update();
-#if 0
- world_routes_debug();
-#endif
-
- if( world.route_count > 0 )
- {
- int closest = 0;
- float min_dist = INFINITY;
-
- for( int i=0; i<world.route_count; i++ )
- {
- float d = v3_dist2( world.routes[i].scoreboard_transform[3], pos );
-
- if( d < min_dist )
- {
- min_dist = d;
- closest = i;
- }
- }
-
- if( (world.active_route_board != closest) || network_scores_updated )
- {
- network_scores_updated = 0;
- world.active_route_board = closest;
-
- struct route *route = &world.routes[closest];
-
- u32 id = route->track_id;
-
- if( id != 0xffffffff )
- {
- struct netmsg_board *local_board =
- &scoreboard_client_data.boards[id];
-
- for( int i=0; i<13; i++ )
- {
- sfd_encode( i, &local_board->data[27*i] );
- }
- }
- }
- }
-
- int in_trigger = 0;
- for( int i=0; i<world.trigger_count; i++ )
- {
- struct trigger_zone *zone = &world.triggers[i];
-
- v3f local;
- m4x3_mulv( zone->inv_transform, pos, local );
-
- if( (fabsf(local[0]) <= 1.0f) &&
- (fabsf(local[1]) <= 1.0f) &&
- (fabsf(local[2]) <= 1.0f) )
- {
- in_trigger = 1;
-
- if( !world.in_trigger )
- {
- world_run_relay( &zone->target );
- }
- }
-
- vg_line_boxf_transformed( zone->transform, (boxf){{-1.0f,-1.0f,-1.0f},
- { 1.0f, 1.0f, 1.0f}},
- 0xff00ff00 );
- }
-
- for( int i=0; i<world.light_count; i++ )
- {
- struct world_light *light = &world.lights[i];
-
- u32 colour = 0xff000000;
- u8 r = light->colour[0] * 255.0f,
- g = light->colour[1] * 255.0f,
- b = light->colour[2] * 255.0f;
-
- colour |= r;
- colour |= g << 8;
- colour |= b << 16;