4 #include "world_load.h"
5 #include "world_routes.h"
6 #include "world_gate.h"
7 #include "ent_skateshop.h"
10 #include "vg/vg_msg.h"
12 #include "player_remote.h"
15 * load the .mdl file located in path as a world instance
17 static void world_instance_load_mdl( u32 instance_id
, const char *path
){
20 world_instance
*world
= &world_static
.instances
[ instance_id
];
21 world_init_blank( world
);
22 world
->status
= k_world_status_loading
;
24 vg_info( "Loading instance[%u]: %s\n", instance_id
, path
);
26 void *allocator
= NULL
;
27 if( instance_id
== 0 ) allocator
= world_static
.heap
;
28 else allocator
= world_static
.instances
[instance_id
-1].heap
;
30 u32 heap_availible
= vg_linear_remaining( allocator
);
31 u32 min_overhead
= sizeof(vg_linear_allocator
);
33 if( heap_availible
< (min_overhead
+1024) ){
34 vg_fatal_error( "out of memory" );
37 u32 size
= heap_availible
- min_overhead
;
38 void *heap
= vg_create_linear_allocator( allocator
, size
, VG_MEMORY_SYSTEM
);
41 mdl_context
*meta
= &world
->meta
;
43 mdl_open( meta
, path
, world
->heap
);
44 mdl_load_metadata_block( meta
, world
->heap
);
45 mdl_load_animation_block( meta
, world
->heap
);
46 mdl_load_mesh_block( meta
, world
->heap
);
48 /* TODO: Make this a table? */
49 mdl_load_array( meta
, &world
->ent_gate
, "ent_gate", heap
);
50 mdl_load_array( meta
, &world
->ent_camera
, "ent_camera", heap
);
51 mdl_load_array( meta
, &world
->ent_spawn
, "ent_spawn", heap
);
52 mdl_load_array( meta
, &world
->ent_light
, "ent_light", heap
);
53 mdl_load_array( meta
, &world
->ent_route_node
,"ent_route_node", heap
);
54 mdl_load_array( meta
, &world
->ent_path_index
,"ent_path_index", heap
);
55 mdl_load_array( meta
, &world
->ent_checkpoint
,"ent_checkpoint", heap
);
56 mdl_load_array( meta
, &world
->ent_route
, "ent_route", heap
);
57 mdl_load_array( meta
, &world
->ent_water
, "ent_water", heap
);
58 mdl_load_array( meta
, &world
->ent_audio_clip
,"ent_audio_clip", heap
);
59 mdl_load_array( meta
, &world
->ent_audio
, "ent_audio", heap
);
60 mdl_load_array( meta
, &world
->ent_volume
, "ent_volume", heap
);
61 mdl_load_array( meta
, &world
->ent_traffic
, "ent_traffic", heap
);
62 mdl_load_array( meta
, &world
->ent_marker
, "ent_marker", heap
);
63 mdl_load_array( meta
, &world
->ent_skateshop
, "ent_skateshop", heap
);
64 mdl_load_array( meta
, &world
->ent_swspreview
,"ent_swspreview", heap
);
65 mdl_load_array( meta
, &world
->ent_ccmd
, "ent_ccmd", heap
);
66 mdl_load_array( meta
, &world
->ent_objective
, "ent_objective", heap
);
67 mdl_load_array( meta
, &world
->ent_challenge
, "ent_challenge", heap
);
68 mdl_load_array( meta
, &world
->ent_relay
, "ent_relay", heap
);
69 mdl_load_array( meta
, &world
->ent_cubemap
, "ent_cubemap", heap
);
70 mdl_load_array( meta
, &world
->ent_miniworld
, "ent_miniworld", heap
);
73 mdl_load_array( meta
, &infos
, "ent_worldinfo", vg_mem
.scratch
);
75 world
->skybox
= k_skybox_default
;
76 if( mdl_arrcount(&infos
) ){
77 world
->info
= *((ent_worldinfo
*)mdl_arritm(&infos
,0));
79 if( world
->meta
.info
.version
>= 104 ){
80 if( MDL_CONST_PSTREQ( &world
->meta
, world
->info
.pstr_skybox
,"space")){
81 world
->skybox
= k_skybox_space
;
86 world
->info
.pstr_author
= 0;
87 world
->info
.pstr_desc
= 0;
88 world
->info
.pstr_name
= 0;
89 world
->info
.timezone
= 0.0f
;
92 time_t seconds
= time(NULL
) % ((u32
)vg_maxf(1.0f
,k_day_length
)*60);
93 world
->time
= ((f64
)(seconds
)/(k_day_length
*60.0));
94 world
->time
+= (world
->info
.timezone
/24.0);
96 /* process resources from pack */
97 world_gen_load_surfaces( world
);
98 world_gen_routes_ent_init( world
);
99 world_gen_entities_init( world
);
102 world_gen_generate_meshes( world
);
103 world_gen_routes_generate( instance_id
);
104 world_gen_compute_light_indices( world
);
107 /* init player position.
108 * - this is overriden by the save state when(if) it loads */
109 v3_zero( world
->player_angles
);
110 ent_spawn
*rp
= world_find_spawn_by_name( world
, "start" );
111 if( !rp
) rp
= world_find_closest_spawn( world
, (v3f
){0.0f
,0.0f
,0.0f
} );
113 /* TODO: fallback to searching for a safe location using raycasts */
115 v3_copy( rp
->transform
.co
, world
->player_co
);
117 /* allocate leaderboard buffers */
118 u32 bs
= mdl_arrcount(&world
->ent_route
)*sizeof(struct leaderboard_cache
);
119 world
->leaderboard_cache
= vg_linear_alloc( heap
, bs
);
121 for( u32 i
=0; i
<mdl_arrcount( &world
->ent_route
); i
++ ){
122 struct leaderboard_cache
*board
= &world
->leaderboard_cache
[i
];
123 board
->data
= vg_linear_alloc( heap
, NETWORK_REQUEST_MAX
);
124 board
->status
= k_request_status_client_error
;
125 board
->cache_time
= 0.0;
129 vg_async_call( async_world_postprocess
, world
, 0 );
133 struct world_load_complete_data
{
135 enum world_purpose purpose
;
138 static void skaterift_world_load_done( void *payload
, u32 size
){
139 struct world_load_complete_data
*data
= payload
;
141 /* TODO(W2): Load player position from this save file */
143 vg_msg_init( &sav
, data
->save
.buf
, data
->save
.len
);
145 world_instance
*world
= &world_static
.instances
[ data
->purpose
];
146 world_entity_start( world
, &sav
);
147 world
->status
= k_world_status_loaded
;
148 world_static
.load_state
= k_world_loader_none
;
150 for( int i
=0; i
<k_world_max
; i
++ ){
151 world_instance
*wi
= &world_static
.instances
[i
];
153 if( wi
->status
== k_world_status_loaded
)
154 world_entity_relink( wi
);
158 struct world_load_args
{
159 enum world_purpose purpose
;
164 * Does a complete world switch using the remaining free slots
166 static void skaterift_world_load_thread( void *_args
){
167 /* FIXME: we need to check all threads that take args. args can dissapear! */
168 struct world_load_args args
= *((struct world_load_args
*)_args
);
170 addon_reg
*reg
= args
.reg
;
171 world_static
.instance_addons
[ args
.purpose
] = reg
;
173 char uid
[ADDON_UID_MAX
];
174 addon_alias_uid( ®
->alias
, uid
);
175 vg_info( "LOAD WORLD %s @%d\n", uid
, args
.purpose
);
179 vg_strnull( &path
, path_buf
, 4096 );
182 addon_get_content_folder( reg
, &path
);
184 vg_str folder
= path
;
185 if( !vg_strgood( &folder
) ) {
186 vg_error( "Load target too long\n" );
190 char worlds
[k_world_max
-1][4096];
194 if( !vg_dir_open(&dir
, folder
.buffer
) ){
195 vg_error( "opendir('%s') failed\n", folder
.buffer
);
199 while( vg_dir_next_entry(&dir
) ){
200 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_file
){
201 const char *d_name
= vg_dir_entry_name(&dir
);
202 if( d_name
[0] == '.' ) continue;
204 vg_str file
= folder
;
205 vg_strcat( &file
, "/" );
206 vg_strcat( &file
, d_name
);
207 if( !vg_strgood( &file
) ) continue;
209 char *ext
= vg_strch( &file
, '.' );
211 if( strcmp(ext
,".mdl") ) continue;
213 if( i
== k_world_max
-1 ){
214 vg_warn( "There are too many .mdl files in the map folder!(3)\n" );
218 strcpy( worlds
[i
++], file
.buffer
);
224 vg_warn( "There are no .mdl files in the map folder.\n" );
228 for( u32 j
=0; j
<i
; j
++ ){
229 vg_str name
= { .buffer
= worlds
[j
], .i
=strlen(worlds
[j
]),
231 char *fname
= vg_strch( &name
, '/' );
233 if( !strcmp( fname
+1, "main.mdl" ) ){
239 world_instance_load_mdl( args
.purpose
, worlds
[first_index
] );
241 vg_async_item
*final_call
=
242 vg_async_alloc( sizeof(struct world_load_complete_data
) );
244 struct world_load_complete_data
*data
= final_call
->payload
;
245 data
->purpose
= args
.purpose
;
247 skaterift_world_get_save_path( args
.purpose
, data
->save
.path
);
248 savedata_file_read( &data
->save
);
250 vg_async_dispatch( final_call
, skaterift_world_load_done
);
254 /* holding pattern before we can start loading the new world, since we might be
255 * waiting for audio to stop */
256 static void skaterift_change_client_world_preupdate(void){
257 for( u32 i
=1; i
<k_world_max
; i
++ ){
258 world_instance
*inst
= &world_static
.instances
[i
];
260 if( inst
->status
== k_world_status_unloading
){
261 if( world_freeable( inst
) ){
268 if( vg_loader_availible() ){
269 vg_info( "worlds cleared, begining load\n" );
270 world_static
.load_state
= k_world_loader_load
;
272 vg_linear_clear( vg_async
.buffer
);
273 struct world_load_args
*args
=
274 vg_linear_alloc( vg_async
.buffer
, sizeof(struct world_load_args
) );
275 args
->purpose
= k_world_purpose_client
;
276 args
->reg
= world_static
.instance_addons
[ k_world_purpose_client
];
278 /* this is replaces the already correct reg but we have to set it again
281 /* finally can start the loader */
282 vg_loader_start( skaterift_world_load_thread
, args
);
286 /* places all loaded worlds into unloading state */
287 static void skaterift_change_world_start( addon_reg
*reg
){
288 if( world_static
.active_instance
!= 0 )
289 vg_error( "Cannot change worlds while in non-root world\n" );
291 if( world_static
.instance_addons
[ k_world_purpose_client
] == reg
){
292 vg_warn( "World is already loaded\n" );
297 addon_alias_uid( ®
->alias
, buf
);
298 vg_info( "switching to: %s\n", buf
);
299 skaterift_autosave(1);
301 world_static
.load_state
= k_world_loader_preload
;
303 vg_linear_clear( vg_mem
.scratch
); /* ?? */
304 vg_info( "unloading old worlds\n" );
306 for( u32 i
=1; i
<vg_list_size(world_static
.instances
); i
++ ){
307 world_instance
*inst
= &world_static
.instances
[i
];
309 if( inst
->status
== k_world_status_loaded
){
310 inst
->status
= k_world_status_unloading
;
311 world_fadeout_audio( inst
);
315 world_entity_relink( &world_static
.instances
[k_world_purpose_hub
] );
317 world_static
.instance_addons
[ k_world_purpose_client
] = reg
;
318 network_send_item( k_netmsg_playeritem_world1
);
319 relink_all_remote_player_worlds();
323 /* console command for the above function */
324 static int skaterift_load_world_command( int argc
, const char *argv
[] ){
325 if( !vg_loader_availible() ) return 0; /* FIXME */
329 addon_uid_to_alias( argv
[0], &q
);
331 u32 reg_id
= addon_match( &q
);
332 if( reg_id
!= 0xffffffff ){
333 addon_reg
*reg
= get_addon_from_index( k_addon_type_world
, reg_id
);
334 skaterift_change_world_start( reg
);
337 vg_error( "Addon '%s' is not installed or not found.\n", argv
[0] );
341 vg_info( "worlds availible to load:\n" );
343 for( int i
=0; i
<addon_count(k_addon_type_world
); i
++ ){
344 addon_reg
*w
= get_addon_from_index( k_addon_type_world
, i
);
346 char buf
[ADDON_UID_MAX
];
347 addon_alias_uid( &w
->alias
, buf
);
348 vg_info( " %s\n", buf
);
357 * 1. to see if all audios owned by the world have been stopped
358 * 2. that this is the least significant world
360 static int world_freeable( world_instance
*world
){
361 if( world
->status
!= k_world_status_unloading
) return 0;
362 u8 world_id
= (world
- world_static
.instances
) + 1;
364 for( u32 i
=world_id
; i
<vg_list_size(world_static
.instances
); i
++ ){
365 if( world_static
.instances
[i
].status
!= k_world_status_unloaded
){
372 for( u32 i
=0; i
<AUDIO_CHANNELS
; i
++ ){
373 audio_channel
*ch
= &vg_audio
.channels
[i
];
375 if( ch
->allocated
&& (ch
->world_id
== world_id
)){
376 if( !audio_channel_finished( ch
) ){
387 * Free all resources for world instance
389 static void world_free( world_instance
*world
){
390 vg_info( "Free world @%p\n", world
);
393 mesh_free( &world
->mesh_route_lines
);
394 mesh_free( &world
->mesh_geo
);
395 mesh_free( &world
->mesh_no_collide
);
396 mesh_free( &world
->mesh_water
);
398 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
399 * existing buffer objects.
401 glDeleteBuffers( 1, &world
->tbo_light_entities
);
402 glDeleteTextures( 1, &world
->tex_light_entities
);
403 glDeleteTextures( 1, &world
->tex_light_cubes
);
405 /* delete textures and meshes */
406 glDeleteTextures( world
->texture_count
, world
->textures
);
408 u32 world_index
= world
- world_static
.instances
;
410 vg_linear_del( world_static
.instances
[world_index
-1].heap
,
411 vg_linear_header(world
->heap
) );
414 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_cubemap
); i
++ ){
415 ent_cubemap
*cm
= mdl_arritm(&world
->ent_cubemap
,i
);
416 glDeleteTextures( 1, &cm
->texture_id
);
417 glDeleteFramebuffers( 1, &cm
->framebuffer_id
);
418 glDeleteRenderbuffers( 1, &cm
->renderbuffer_id
);
421 world
->status
= k_world_status_unloaded
;
425 * reset the world structure without deallocating persistent buffers
426 * TODO: Make this a memset(0), and have persistent items live in a static loc
428 static void world_init_blank( world_instance
*world
){
429 memset( &world
->meta
, 0, sizeof(mdl_context
) );
431 world
->textures
= NULL
;
432 world
->texture_count
= 0;
433 world
->surfaces
= NULL
;
434 world
->surface_count
= 0;
436 world
->geo_bh
= NULL
;
437 world
->entity_bh
= NULL
;
438 world
->entity_list
= NULL
;
439 world
->rendering_gate
= NULL
;
441 world
->water
.enabled
= 0;
444 /* default lighting conditions
445 * -------------------------------------------------------------*/
446 struct ub_world_lighting
*state
= &world
->ub_lighting
;
448 state
->g_light_preview
= 0;
449 state
->g_shadow_samples
= 8;
450 state
->g_water_fog
= 0.04f
;
452 v4_zero( state
->g_water_plane
);
453 v4_zero( state
->g_depth_bounds
);
455 state
->g_shadow_length
= 9.50f
;
456 state
->g_shadow_spread
= 0.65f
;
458 v3_copy( (v3f
){0.37f
, 0.54f
, 0.97f
}, state
->g_daysky_colour
);
459 v3_copy( (v3f
){0.03f
, 0.05f
, 0.20f
}, state
->g_nightsky_colour
);
460 v3_copy( (v3f
){1.00f
, 0.32f
, 0.01f
}, state
->g_sunset_colour
);
461 v3_copy( (v3f
){0.13f
, 0.17f
, 0.35f
}, state
->g_ambient_colour
);
462 v3_copy( (v3f
){0.25f
, 0.17f
, 0.51f
}, state
->g_sunset_ambient
);
463 v3_copy( (v3f
){1.10f
, 0.89f
, 0.35f
}, state
->g_sun_colour
);
466 #endif /* WORLD_LOAD_C */