+ world->status = k_world_status_unloaded;
+}
+#endif
+
+VG_STATIC void world_init_blank( world_instance *world )
+{
+ memset( &world->meta, 0, sizeof(mdl_context) );
+
+ world->textures = NULL;
+ world->texture_count = 0;
+ world->surfaces = NULL;
+ world->surface_count = 0;
+
+ world->geo_bh = NULL;
+ world->volume_bh = NULL;
+ world->audio_bh = NULL;
+ world->rendering_gate = NULL;
+
+ world->water.enabled = 0;
+
+ /* default lighting conditions
+ * -------------------------------------------------------------*/
+ struct ub_world_lighting *state = &world->ub_lighting;
+
+ state->g_light_preview = 0;
+ state->g_shadow_samples = 8;
+ state->g_water_fog = 0.04f;
+
+ v4_zero( state->g_water_plane );
+ v4_zero( state->g_depth_bounds );
+
+ state->g_shadow_length = 9.50f;
+ state->g_shadow_spread = 0.65f;
+
+ v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
+ v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
+ v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
+ v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
+ v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
+ v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
+}
+
+VG_STATIC void world_entities_init( u32 world_id )
+{
+ world_instance *world = &world_global.worlds[world_id];
+
+ /* lights */
+ for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
+ ent_light *light = mdl_arritm( &world->ent_light, j );
+
+ m4x3f to_world;
+ q_m3x3( light->transform.q, to_world );
+ v3_copy( light->transform.co, to_world[3] );
+ m4x3_invert_affine( to_world, light->inverse_world );
+
+ light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
+ light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
+ }
+
+ /* gates */
+ for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
+ ent_gate *gate = mdl_arritm( &world->ent_gate, j );
+
+ if( gate->type == k_gate_type_teleport ){
+ gate_transform_update( gate );
+ }
+ else if( gate->type == k_gate_type_nonlocal_unlinked ){
+ const char *key = mdl_pstr( &world->meta, gate->target );
+ vg_info( "key: %s\n", key );
+
+ for( u32 i=0; i<vg_list_size(world_global.worlds); i++ ){
+ world_instance *other = &world_global.worlds[i];
+ if( other == world ) continue;
+ if( other->status != k_world_status_loaded ) continue;
+ vg_info( "Checking world %u for key matches\n", i );
+
+ for( u32 j=0; j<mdl_arrcount( &other->ent_gate ); j++ ){
+ ent_gate *gate2 = mdl_arritm( &other->ent_gate, j );
+ if( gate2->type != k_gate_type_nonlocal_unlinked ) continue;
+
+ const char *key2 = mdl_pstr( &other->meta, gate2->target );
+ vg_info( " key2: %s\n", key2 );
+
+ if( strcmp( key, key2 ) ) continue;
+
+ vg_success( "Non-local matching pair '%s' found. (%u:%u)\n",
+ key, world_id, i );
+
+ gate->type = k_gate_type_nonlocel;
+ gate2->type = k_gate_type_nonlocel;
+ gate->target = i;
+ gate2->target = world_id;
+
+ v3_copy( gate->co[0], gate2->co[1] );
+ v3_copy( gate2->co[0], gate->co[1] );
+ v4_copy( gate->q[0], gate2->q[1] );
+ v4_copy( gate2->q[0], gate->q[1] );
+
+ v4f qflip;
+ q_axis_angle( qflip, (v3f){0.0f,1.0f,0.0f}, VG_PIf );
+ q_mul( gate->q[0], qflip, gate->q[0] );
+ q_mul( gate->q[1], qflip, gate->q[1] );
+
+ gate_transform_update( gate );
+ gate_transform_update( gate2 );
+
+ goto matched;
+ }
+ }
+matched:;
+ }
+ }
+
+ /* water */
+ for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
+ ent_water *water = mdl_arritm( &world->ent_water, j );
+ if( world->water.enabled ){
+ vg_warn( "Multiple water surfaces in level!\n" );
+ break;
+ }
+
+ world->water.enabled = 1;
+ water_set_surface( world, water->transform.co[1] );
+ }
+
+ /* volumes */
+ for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
+ ent_volume *volume = mdl_arritm( &world->ent_volume, j );
+ mdl_transform_m4x3( &volume->transform, volume->to_world );
+ m4x3_invert_full( volume->to_world, volume->to_local );
+ }
+
+ /* audio packs */
+ for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
+ ent_audio *audio = mdl_arritm( &world->ent_audio, j );
+
+ for( u32 k=0; k<audio->clip_count; k++ ){
+ ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
+ audio->clip_start+k );
+
+ if( clip->file.pack_size ){
+ u32 size = clip->file.pack_size,
+ offset = clip->file.pack_offset;
+
+ /* embedded files are fine to clear the scratch buffer, only
+ * external audio uses it */
+
+ vg_linear_clear( vg_mem.scratch );
+ void *data = vg_linear_alloc( vg_mem.scratch,
+ clip->file.pack_size );
+
+ mdl_fread_pack_file( &world->meta, &clip->file, data );
+
+ clip->clip.path = NULL;
+ clip->clip.flags = audio->flags;
+ clip->clip.data = data;
+ clip->clip.size = size;
+ }
+ else{
+ clip->clip.path = mdl_pstr( &world->meta, clip->file.pstr_path );
+ clip->clip.flags = audio->flags;
+ clip->clip.data = NULL;
+ clip->clip.size = 0;
+ }
+
+ audio_clip_load( &clip->clip, world->heap );
+ }
+ }
+}
+
+VG_STATIC void world_load( u32 index, const char *path )
+{
+ world_instance *world = &world_global.worlds[index];
+ world_init_blank( world );
+ world->status = k_world_status_loading;
+
+ vg_info( "Loading world: %s\n", path );
+
+ void *allocator = NULL;
+ if( index == 0 ) allocator = world_global.heap;
+ else allocator = world_global.worlds[index-1].heap;
+
+ u32 heap_availible = vg_linear_remaining( allocator );
+ u32 min_overhead = sizeof(vg_linear_allocator);
+
+ if( heap_availible < (min_overhead+1024) ){
+ vg_fatal_error( "out of memory" );
+ }
+
+ u32 size = heap_availible - min_overhead;
+ void *heap = vg_create_linear_allocator( allocator, size,
+ VG_MEMORY_SYSTEM );
+
+ world->heap = heap;
+ mdl_context *meta = &world->meta;
+
+ mdl_open( meta, path, world->heap );
+ mdl_load_metadata_block( meta, world->heap );
+ mdl_load_animation_block( meta, world->heap );
+ mdl_load_mesh_block( meta, world->heap );
+
+ mdl_load_array( meta, &world->ent_gate, "ent_gate", heap );
+ mdl_load_array( meta, &world->ent_camera, "ent_camera", heap );
+ mdl_load_array( meta, &world->ent_spawn, "ent_spawn", heap );
+ mdl_load_array( meta, &world->ent_light, "ent_light", heap );
+ mdl_load_array( meta, &world->ent_route_node,"ent_route_node", heap );
+ mdl_load_array( meta, &world->ent_path_index,"ent_path_index", heap );
+ mdl_load_array( meta, &world->ent_checkpoint,"ent_checkpoint", heap );
+ mdl_load_array( meta, &world->ent_route, "ent_route", heap );
+ mdl_load_array( meta, &world->ent_water, "ent_water", heap );
+ mdl_load_array( meta, &world->ent_audio_clip,"ent_audio_clip", heap );
+ mdl_load_array( meta, &world->ent_audio, "ent_audio", heap );
+ mdl_load_array( meta, &world->ent_volume, "ent_volume", heap );
+ mdl_load_array( meta, &world->ent_traffic, "ent_traffic", heap );
+ mdl_load_array( meta, &world->ent_marker, "ent_marker", heap );
+ mdl_load_array( meta, &world->ent_skateshop, "ent_skateshop", heap );
+ mdl_load_array( meta, &world->ent_swspreview,"ent_swspreview", heap );
+
+ /* process resources from pack */
+ world_process_resources( world );
+
+ world_routes_ent_init( world );
+ world_entities_init( index );
+ world->volume_bh = bh_create( heap, &bh_system_volumes, world,
+ mdl_arrcount( &world->ent_volume ), 1 );
+
+ /* main bulk */
+ world_generate( world );
+ world_routes_generate( world );
+ world_post_process( world );
+
+ mdl_close( meta );