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 mdl_load_array( meta
, &world
->ent_gate
, "ent_gate", heap
);
49 mdl_load_array( meta
, &world
->ent_camera
, "ent_camera", heap
);
50 mdl_load_array( meta
, &world
->ent_spawn
, "ent_spawn", heap
);
51 mdl_load_array( meta
, &world
->ent_light
, "ent_light", heap
);
52 mdl_load_array( meta
, &world
->ent_route_node
,"ent_route_node", heap
);
53 mdl_load_array( meta
, &world
->ent_path_index
,"ent_path_index", heap
);
54 mdl_load_array( meta
, &world
->ent_checkpoint
,"ent_checkpoint", heap
);
55 mdl_load_array( meta
, &world
->ent_route
, "ent_route", heap
);
56 mdl_load_array( meta
, &world
->ent_water
, "ent_water", heap
);
57 mdl_load_array( meta
, &world
->ent_audio_clip
,"ent_audio_clip", heap
);
58 mdl_load_array( meta
, &world
->ent_audio
, "ent_audio", heap
);
59 mdl_load_array( meta
, &world
->ent_volume
, "ent_volume", heap
);
60 mdl_load_array( meta
, &world
->ent_traffic
, "ent_traffic", heap
);
61 mdl_load_array( meta
, &world
->ent_marker
, "ent_marker", heap
);
62 mdl_load_array( meta
, &world
->ent_skateshop
, "ent_skateshop", heap
);
63 mdl_load_array( meta
, &world
->ent_swspreview
,"ent_swspreview", heap
);
64 mdl_load_array( meta
, &world
->ent_ccmd
, "ent_ccmd", heap
);
65 mdl_load_array( meta
, &world
->ent_objective
, "ent_objective", heap
);
66 mdl_load_array( meta
, &world
->ent_challenge
, "ent_challenge", heap
);
67 mdl_load_array( meta
, &world
->ent_relay
, "ent_relay", heap
);
68 mdl_load_array( meta
, &world
->ent_cubemap
, "ent_cubemap", heap
);
71 mdl_load_array( meta
, &infos
, "ent_worldinfo", vg_mem
.scratch
);
73 if( mdl_arrcount(&infos
) ){
74 world
->info
= *((ent_worldinfo
*)mdl_arritm(&infos
,0));
77 world
->info
.pstr_author
= 0;
78 world
->info
.pstr_desc
= 0;
79 world
->info
.pstr_name
= 0;
80 world
->info
.timezone
= 0.0f
;
87 world
->time
= (float)tm
->tm_min
/20.0f
+ (world
->info
.timezone
/24.0f
);
89 /* process resources from pack */
90 world_gen_load_surfaces( world
);
91 world_gen_routes_ent_init( world
);
92 world_gen_entities_init( world
);
95 world_gen_generate_meshes( world
);
96 world_gen_routes_generate( instance_id
);
97 world_gen_compute_light_indices( world
);
100 vg_async_call( async_world_postprocess
, world
, 0 );
104 struct world_load_complete_data
{
106 u32 instance_start
, instance_count
;
109 static void skaterift_world_load_done( void *payload
, u32 size
){
110 struct world_load_complete_data
*data
= payload
;
113 sav
.buf
= data
->save
.buf
;
114 sav
.len
= data
->save
.len
;
115 sav
.max
= data
->save
.len
;
117 for( u32 i
=0; i
<data
->instance_count
; i
++ ){
118 world_instance
*world
= &world_static
.instances
[ data
->instance_start
+i
];
119 world_entity_start( world
, &sav
);
122 world_static
.load_state
= k_world_loader_none
;
125 struct world_load_args
{
126 enum world_purpose purpose
;
131 * Does a complete world switch using the remaining free slots
133 static void skaterift_world_load_thread( void *_args
){
134 struct world_load_args
*args
= _args
;
135 enum world_purpose purpose
= args
->purpose
;
136 addon_reg
*reg
= args
->reg
;
138 if( purpose
== k_world_purpose_hub
) world_static
.addon_hub
= reg
;
139 else world_static
.addon_client
= reg
;
143 vg_strnull( &path
, path_buf
, 4096 );
146 addon_get_content_folder( reg
, &path
);
148 vg_str folder
= path
;
149 if( !vg_strgood( &folder
) ) {
150 vg_error( "Load target too long\n" );
154 char worlds
[3][4096];
158 if( !vg_dir_open(&dir
, folder
.buffer
) ){
159 vg_error( "opendir('%s') failed\n", folder
.buffer
);
163 while( vg_dir_next_entry(&dir
) ){
164 if( vg_dir_entry_type(&dir
) == k_vg_entry_type_file
){
165 const char *d_name
= vg_dir_entry_name(&dir
);
166 if( d_name
[0] == '.' ) continue;
168 vg_str file
= folder
;
169 vg_strcat( &file
, "/" );
170 vg_strcat( &file
, d_name
);
171 if( !vg_strgood( &file
) ) continue;
173 char *ext
= vg_strch( &file
, '.' );
175 if( strcmp(ext
,".mdl") ) continue;
178 vg_warn( "There are too many .mdl files in the map folder!(3)\n" );
182 strcpy( worlds
[i
++], file
.buffer
);
188 vg_warn( "There are no .mdl files in the map folder.\n" );
192 for( u32 j
=0; j
<i
; j
++ ){
193 vg_str name
= { .buffer
= worlds
[j
], .i
=strlen(worlds
[j
]),
195 char *fname
= vg_strch( &name
, '/' );
197 if( !strcmp( fname
+1, "main.mdl" ) ){
203 u32 instance_start
= 0, instance_count
= 1;
204 if( purpose
== k_world_purpose_client
) instance_start
= 1;
206 world_instance_load_mdl( instance_start
, worlds
[first_index
] );
208 /* TODO: Support multiply packed worlds */
210 world_loader
.generate_point_cloud
= 0;
211 for( u32 j
=0; j
<i
; j
++ ){
212 if( j
!= first_index
){
213 world_loader
.world_index
= j
+1;
214 world_load_mdl( worlds
[j
] );
219 vg_async_item
*final_call
=
220 vg_async_alloc( sizeof(struct world_load_complete_data
) );
222 struct world_load_complete_data
*data
= final_call
->payload
;
223 data
->instance_start
= instance_start
;
224 data
->instance_count
= instance_count
;
226 skaterift_world_get_save_path( purpose
, data
->save
.path
);
227 savedata_file_read( &data
->save
);
229 vg_async_dispatch( final_call
, skaterift_world_load_done
);
233 /* holding pattern before we can start loading the new world, since we might be
234 * waiting for audio to stop */
235 static void skaterift_change_client_world_preupdate(void){
236 for( u32 i
=1; i
<vg_list_size(world_static
.instances
); i
++ ){
237 world_instance
*inst
= &world_static
.instances
[i
];
239 if( inst
->status
== k_world_status_unloading
){
240 if( world_freeable( inst
) ){
247 if( vg_loader_availible() ){
248 vg_info( "worlds cleared, begining load\n" );
249 world_static
.load_state
= k_world_loader_load
;
251 vg_linear_clear( vg_async
.buffer
);
252 struct world_load_args
*args
=
253 vg_linear_alloc( vg_async
.buffer
, sizeof(struct world_load_args
) );
254 args
->purpose
= k_world_purpose_client
;
255 args
->reg
= world_static
.addon_client
;
257 /* this is replaces the already correct reg but we have to set it again
260 /* finally can start the loader */
261 vg_loader_start( skaterift_world_load_thread
, args
);
265 /* places all loaded worlds into unloading state */
266 static void skaterift_change_world_start( addon_reg
*reg
){
267 if( world_static
.active_instance
!= 0 )
268 vg_error( "Cannot change worlds while in non-root world\n" );
270 if( world_static
.addon_client
== reg
){
271 vg_warn( "World is already loaded\n" );
276 addon_alias_uid( ®
->alias
, buf
);
277 vg_info( "switching to: %s\n", buf
);
278 skaterift_autosave(1);
280 world_static
.load_state
= k_world_loader_preload
;
282 vg_linear_clear( vg_mem
.scratch
); /* ?? */
283 vg_info( "unloading old worlds\n" );
284 world_unlink_nonlocal( &world_static
.instances
[0] );
286 for( u32 i
=1; i
<vg_list_size(world_static
.instances
); i
++ ){
287 world_instance
*inst
= &world_static
.instances
[i
];
289 if( inst
->status
== k_world_status_loaded
){
290 inst
->status
= k_world_status_unloading
;
291 world_fadeout_audio( inst
);
295 world_static
.addon_client
= reg
;
296 network_send_item( k_netmsg_playeritem_world1
);
297 relink_all_remote_player_worlds();
301 /* console command for the above function */
302 static int skaterift_change_world_command( int argc
, const char *argv
[] ){
303 if( !vg_loader_availible() ) return 0;
307 q
.type
= k_addon_type_world
;
309 vg_strncpy( argv
[0], q
.foldername
, 64, k_strncpy_always_add_null
);
311 u32 reg_id
= addon_match( &q
);
312 if( reg_id
!= 0xffffffff ){
313 addon_reg
*reg
= get_addon_from_index( k_addon_type_world
, reg_id
);
314 skaterift_change_world_start( reg
);
318 addon_alias_uid( &q
, buf
);
319 vg_error( "Addon '%s' is not installed or not found.\n", buf
);
328 * 1. to see if all audios owned by the world have been stopped
329 * 2. that this is the least significant world
331 static int world_freeable( world_instance
*world
){
332 if( world
->status
!= k_world_status_unloading
) return 0;
333 u8 world_id
= (world
- world_static
.instances
) + 1;
335 for( u32 i
=world_id
; i
<vg_list_size(world_static
.instances
); i
++ ){
336 if( world_static
.instances
[i
].status
!= k_world_status_unloaded
){
343 for( u32 i
=0; i
<AUDIO_CHANNELS
; i
++ ){
344 audio_channel
*ch
= &vg_audio
.channels
[i
];
346 if( ch
->allocated
&& (ch
->world_id
== world_id
)){
347 if( !audio_channel_finished( ch
) ){
358 * Free all resources for world instance
360 static void world_free( world_instance
*world
)
362 vg_info( "Free world @%p\n", world
);
365 mesh_free( &world
->mesh_route_lines
);
366 mesh_free( &world
->mesh_geo
);
367 mesh_free( &world
->mesh_no_collide
);
368 mesh_free( &world
->mesh_water
);
370 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
371 * existing buffer objects.
373 glDeleteBuffers( 1, &world
->tbo_light_entities
);
374 glDeleteTextures( 1, &world
->tex_light_entities
);
375 glDeleteTextures( 1, &world
->tex_light_cubes
);
377 /* delete textures and meshes */
378 glDeleteTextures( world
->texture_count
, world
->textures
);
380 u32 world_index
= world
- world_static
.instances
;
382 vg_linear_del( world_static
.instances
[world_index
-1].heap
,
383 vg_linear_header(world
->heap
) );
386 for( u32 i
=0; i
<mdl_arrcount(&world
->ent_cubemap
); i
++ ){
387 ent_cubemap
*cm
= mdl_arritm(&world
->ent_cubemap
,i
);
388 glDeleteTextures( 1, &cm
->texture_id
);
389 glDeleteFramebuffers( 1, &cm
->framebuffer_id
);
390 glDeleteRenderbuffers( 1, &cm
->renderbuffer_id
);
393 world
->status
= k_world_status_unloaded
;
397 * reset the world structure without deallocating persistent buffers
398 * TODO: Make this a memset(0), and have persistent items live in a static loc
400 static void world_init_blank( world_instance
*world
)
402 memset( &world
->meta
, 0, sizeof(mdl_context
) );
404 world
->textures
= NULL
;
405 world
->texture_count
= 0;
406 world
->surfaces
= NULL
;
407 world
->surface_count
= 0;
409 world
->geo_bh
= NULL
;
410 world
->entity_bh
= NULL
;
411 world
->entity_list
= NULL
;
412 world
->rendering_gate
= NULL
;
414 world
->water
.enabled
= 0;
417 /* default lighting conditions
418 * -------------------------------------------------------------*/
419 struct ub_world_lighting
*state
= &world
->ub_lighting
;
421 state
->g_light_preview
= 0;
422 state
->g_shadow_samples
= 8;
423 state
->g_water_fog
= 0.04f
;
425 v4_zero( state
->g_water_plane
);
426 v4_zero( state
->g_depth_bounds
);
428 state
->g_shadow_length
= 9.50f
;
429 state
->g_shadow_spread
= 0.65f
;
431 v3_copy( (v3f
){0.37f
, 0.54f
, 0.97f
}, state
->g_daysky_colour
);
432 v3_copy( (v3f
){0.03f
, 0.05f
, 0.20f
}, state
->g_nightsky_colour
);
433 v3_copy( (v3f
){1.00f
, 0.32f
, 0.01f
}, state
->g_sunset_colour
);
434 v3_copy( (v3f
){0.13f
, 0.17f
, 0.35f
}, state
->g_ambient_colour
);
435 v3_copy( (v3f
){0.25f
, 0.17f
, 0.51f
}, state
->g_sunset_ambient
);
436 v3_copy( (v3f
){1.10f
, 0.89f
, 0.35f
}, state
->g_sun_colour
);
439 #endif /* WORLD_LOAD_C */