revision 2
[carveJwlIkooP6JGAAIwe30JlM.git] / world_load.c
1 #ifndef WORLD_LOAD_C
2 #define WORLD_LOAD_C
3
4 #include "world_load.h"
5 #include "world_routes.h"
6 #include "world_gate.h"
7 #include "ent_skateshop.h"
8 #include "addon.h"
9 #include "save.h"
10 #include "vg/vg_msg.h"
11 #include "network.h"
12 #include "player_remote.h"
13 #include "vg/vg_loader.h"
14
15 /*
16 * load the .mdl file located in path as a world instance
17 */
18 static void world_instance_load_mdl( u32 instance_id, const char *path ){
19 world_instance *world = &world_static.instances[ instance_id ];
20 world_init_blank( world );
21 world->status = k_world_status_loading;
22
23 vg_info( "Loading instance[%u]: %s\n", instance_id, path );
24
25 void *allocator = NULL;
26 if( instance_id == 0 ) allocator = world_static.heap;
27 else allocator = world_static.instances[instance_id-1].heap;
28
29 u32 heap_availible = vg_linear_remaining( allocator );
30 u32 min_overhead = sizeof(vg_linear_allocator);
31
32 if( heap_availible < (min_overhead+1024) ){
33 vg_fatal_error( "out of memory" );
34 }
35
36 u32 size = heap_availible - min_overhead;
37 void *heap = vg_create_linear_allocator( allocator, size, VG_MEMORY_SYSTEM );
38
39 world->heap = heap;
40 mdl_context *meta = &world->meta;
41
42 mdl_open( meta, path, world->heap );
43 mdl_load_metadata_block( meta, world->heap );
44 mdl_load_animation_block( meta, world->heap );
45 mdl_load_mesh_block( meta, world->heap );
46
47 vg_info( "%u\n", sizeof(ent_cubemap) );
48
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 );
71 MDL_LOAD_ARRAY( meta, &world->ent_prop, ent_prop, heap );
72 MDL_LOAD_ARRAY( meta, &world->ent_region, ent_region, heap );
73 MDL_LOAD_ARRAY( meta, &world->ent_glider, ent_glider, heap );
74
75 mdl_array_ptr infos;
76 MDL_LOAD_ARRAY( meta, &infos, ent_worldinfo, vg_mem.scratch );
77
78 world->skybox = k_skybox_default;
79 if( mdl_arrcount(&infos) )
80 {
81 world->info = *((ent_worldinfo *)mdl_arritm(&infos,0));
82
83 if( world->meta.info.version >= 104 )
84 {
85 if( MDL_CONST_PSTREQ( &world->meta, world->info.pstr_skybox,"space"))
86 {
87 world->skybox = k_skybox_space;
88 }
89 }
90 }
91 else
92 {
93 world->info.pstr_author = 0;
94 world->info.pstr_desc = 0;
95 world->info.pstr_name = 0;
96 world->info.timezone = 0.0f;
97 world->info.flags = 0;
98 }
99
100 time_t seconds = time(NULL) % ((u32)vg_maxf(1.0f,k_day_length)*60);
101 world->time = ((f64)(seconds)/(k_day_length*60.0));
102 world->time += (world->info.timezone/24.0);
103
104 /* process resources from pack */
105 u64 t4 = SDL_GetPerformanceCounter();
106 world_gen_load_surfaces( world );
107 u64 t5 = SDL_GetPerformanceCounter();
108 world_gen_routes_ent_init( world );
109 world_gen_entities_init( world );
110 u64 t6 = SDL_GetPerformanceCounter();
111
112 /* main bulk */
113 u64 t0 = SDL_GetPerformanceCounter();
114 world_gen_generate_meshes( world );
115 u64 t1 = SDL_GetPerformanceCounter();
116 world_gen_routes_generate( instance_id );
117 u64 t2 = SDL_GetPerformanceCounter();
118 world_gen_compute_light_indices( world );
119 u64 t3 = SDL_GetPerformanceCounter();
120 mdl_close( meta );
121
122 u64 utime_mesh = t1-t0,
123 utime_route = t2-t1,
124 utime_indices = t3-t2,
125 utime_tex = t5-t4,
126 utime_ent = t6-t5,
127 ufreq = SDL_GetPerformanceFrequency();
128
129 f64 ftime_mesh = ((f64)utime_mesh / (f64)ufreq)*1000.0,
130 ftime_route = ((f64)utime_route / (f64)ufreq)*1000.0,
131 ftime_ind = ((f64)utime_route / (f64)ufreq)*1000.0,
132 ftime_tex = ((f64)utime_tex / (f64)ufreq)*1000.0,
133 ftime_ent = ((f64)utime_ent / (f64)ufreq)*1000.0;
134
135 vg_info( "wtime:mesh %.2fms route %.2fms ind %.2fms tex %.2fms ent %.2fms\n",
136 ftime_mesh, ftime_route, ftime_ind, ftime_tex, ftime_ent );
137
138 /* init player position.
139 * - this is overriden by the save state when(if) it loads */
140 world_default_spawn_pos( world, world->player_co );
141
142 /* allocate leaderboard buffers */
143 u32 bs = mdl_arrcount(&world->ent_route)*sizeof(struct leaderboard_cache);
144 world->leaderboard_cache = vg_linear_alloc( heap, bs );
145
146 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i ++ )
147 {
148 struct leaderboard_cache *board = &world->leaderboard_cache[i];
149 board->data = vg_linear_alloc( heap, NETWORK_REQUEST_MAX );
150 board->status = k_request_status_client_error;
151 board->cache_time = 0.0;
152 board->data_len = 0;
153 }
154
155 world->routes_ui = vg_linear_alloc( heap,
156 sizeof(struct route_ui)*mdl_arrcount(&world->ent_route) );
157
158 vg_async_call( async_world_postprocess, world, 0 );
159 vg_async_stall();
160 }
161
162 struct world_load_complete_data{
163 savedata_file save;
164 enum world_purpose purpose;
165 };
166
167 static void skaterift_world_load_done( void *payload, u32 size ){
168 struct world_load_complete_data *data = payload;
169 world_instance *world = &world_static.instances[ data->purpose ];
170
171 vg_msg sav;
172 vg_msg_init( &sav, data->save.buf, data->save.len );
173
174 if( data->purpose != k_world_purpose_hub ){
175 vg_msg player_frame = sav;
176 if( vg_msg_seekframe( &player_frame, "player" ) ){
177 vg_msg_getkvvecf( &player_frame, "position", k_vg_msg_v3f,
178 world->player_co, NULL );
179 }
180 }
181
182 world_entity_start( world, &sav );
183 world->status = k_world_status_loaded;
184 world_static.load_state = k_world_loader_none;
185 }
186
187 /*
188 * Does a complete world switch using the remaining free slots
189 */
190 void skaterift_world_load_thread( void *_args )
191 {
192 struct world_load_args args = *((struct world_load_args *)_args);
193
194 addon_reg *reg = args.reg;
195 world_static.instance_addons[ args.purpose ] = reg;
196
197 char uid[ADDON_UID_MAX];
198 addon_alias_uid( &reg->alias, uid );
199 vg_info( "LOAD WORLD %s @%d\n", uid, args.purpose );
200
201 char path_buf[4096];
202 vg_str path;
203 vg_strnull( &path, path_buf, 4096 );
204
205 addon_get_content_folder( reg, &path, 1 );
206
207 vg_str folder = path;
208 if( !vg_strgood( &folder ) ) {
209 vg_error( "Load target too long\n" );
210 return;
211 }
212
213 char worlds[k_world_max-1][4096];
214 u32 i=0;
215
216 vg_dir dir;
217 if( !vg_dir_open(&dir, folder.buffer) ){
218 vg_error( "opendir('%s') failed\n", folder.buffer );
219 return;
220 }
221
222 while( vg_dir_next_entry(&dir) ){
223 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
224 const char *d_name = vg_dir_entry_name(&dir);
225 if( d_name[0] == '.' ) continue;
226
227 vg_str file = folder;
228 vg_strcat( &file, "/" );
229 vg_strcat( &file, d_name );
230 if( !vg_strgood( &file ) ) continue;
231
232 char *ext = vg_strch( &file, '.' );
233 if( !ext ) continue;
234 if( strcmp(ext,".mdl") ) continue;
235
236 if( i == k_world_max-1 ){
237 vg_warn( "There are too many .mdl files in the map folder!(3)\n" );
238 break;
239 }
240
241 strcpy( worlds[i++], file.buffer );
242 }
243 }
244 vg_dir_close(&dir);
245
246 if( i == 0 ){
247 vg_warn( "There are no .mdl files in the map folder.\n" );
248 }
249
250 u32 first_index = 0;
251 for( u32 j=0; j<i; j++ ){
252 vg_str name = { .buffer = worlds[j], .i=strlen(worlds[j]),
253 sizeof(worlds[j]) };
254 char *fname = vg_strch( &name, '/' );
255 if( fname ){
256 if( !strcmp( fname+1, "main.mdl" ) ){
257 first_index = j;
258 }
259 }
260 }
261
262 world_instance_load_mdl( args.purpose, worlds[first_index] );
263
264 vg_async_item *final_call =
265 vg_async_alloc( sizeof(struct world_load_complete_data) );
266
267 struct world_load_complete_data *data = final_call->payload;
268 data->purpose = args.purpose;
269
270 skaterift_world_get_save_path( args.purpose, data->save.path );
271 savedata_file_read( &data->save );
272
273 vg_async_dispatch( final_call, skaterift_world_load_done );
274 vg_async_stall();
275 }
276
277 /* holding pattern before we can start loading the new world, since we might be
278 * waiting for audio to stop */
279 void skaterift_change_client_world_preupdate(void)
280 {
281 for( u32 i=1; i<k_world_max; i++ ){
282 world_instance *inst = &world_static.instances[i];
283
284 if( inst->status == k_world_status_unloading ){
285 if( world_freeable( inst ) ){
286 world_free( inst );
287 }
288 return;
289 }
290 }
291
292 if( vg_loader_availible() ){
293 vg_info( "worlds cleared, begining load\n" );
294 world_static.load_state = k_world_loader_load;
295
296 vg_linear_clear( vg_async.buffer );
297 struct world_load_args *args =
298 vg_linear_alloc( vg_async.buffer, sizeof(struct world_load_args) );
299 args->purpose = k_world_purpose_client;
300 args->reg = world_static.instance_addons[ k_world_purpose_client ];
301
302 /* this is replaces the already correct reg but we have to set it again
303 * TOO BAD */
304
305 /* finally can start the loader */
306 vg_loader_start( skaterift_world_load_thread, args );
307 }
308 }
309
310 /* places all loaded worlds into unloading state */
311 void skaterift_change_world_start( addon_reg *reg )
312 {
313 if( world_static.active_instance != 0 )
314 vg_error( "Cannot change worlds while in non-root world\n" );
315 else{
316 if( world_static.instance_addons[ k_world_purpose_client ] == reg ){
317 vg_warn( "World is already loaded\n" );
318 return;
319 }
320
321 char buf[76];
322 addon_alias_uid( &reg->alias, buf );
323 vg_info( "switching to: %s\n", buf );
324 skaterift_autosave(1);
325
326 world_static.load_state = k_world_loader_preload;
327
328 vg_linear_clear( vg_mem.scratch ); /* ?? */
329 vg_info( "unloading old worlds\n" );
330
331 world_instance *client_world =
332 &world_static.instances[ k_world_purpose_client ];
333
334 if( client_world->status == k_world_status_loaded ){
335 client_world->status = k_world_status_unloading;
336 world_fadeout_audio( client_world );
337 }
338
339 world_static.instance_addons[ k_world_purpose_client ] = reg;
340 network_send_item( k_netmsg_playeritem_world1 );
341 relink_all_remote_player_worlds();
342 world_unlink_nonlocal( &world_static.instances[k_world_purpose_hub] );
343 }
344 }
345
346 /* console command for the above function */
347 int skaterift_load_world_command( int argc, const char *argv[] )
348 {
349 if( !vg_loader_availible() )
350 {
351 vg_error( "Loading thread is currently unavailible\n" );
352 return 0;
353 }
354
355 if( argc == 1 ){
356 addon_alias q;
357 addon_uid_to_alias( argv[0], &q );
358
359 u32 reg_id = addon_match( &q );
360 if( reg_id != 0xffffffff ){
361 addon_reg *reg = get_addon_from_index( k_addon_type_world, reg_id, 0 );
362 skaterift_change_world_start( reg );
363 }
364 else {
365 vg_error( "Addon '%s' is not installed or not found.\n", argv[0] );
366 }
367 }
368 else {
369 vg_info( "worlds availible to load:\n" );
370
371 for( int i=0; i<addon_count(k_addon_type_world,0); i ++ ){
372 addon_reg *w = get_addon_from_index( k_addon_type_world, i, 0);
373
374 char buf[ADDON_UID_MAX];
375 addon_alias_uid( &w->alias, buf );
376
377 if( w->flags & ADDON_REG_HIDDEN )
378 vg_info( " %s [hidden]\n", buf );
379 else
380 vg_info( " %s\n", buf );
381 }
382 }
383
384 return 0;
385 }
386
387 /*
388 * checks:
389 * 1. to see if all audios owned by the world have been stopped
390 * 2. that this is the least significant world
391 */
392 int world_freeable( world_instance *world )
393 {
394 if( world->status != k_world_status_unloading ) return 0;
395 u8 world_id = (world - world_static.instances) + 1;
396
397 for( u32 i=world_id; i<vg_list_size(world_static.instances); i++ ){
398 if( world_static.instances[i].status != k_world_status_unloaded ){
399 return 0;
400 }
401 }
402
403 int freeable = 1;
404 audio_lock();
405 for( u32 i=0; i<AUDIO_CHANNELS; i++ ){
406 audio_channel *ch = &vg_audio.channels[i];
407
408 if( ch->allocated && (ch->world_id == world_id)){
409 if( !audio_channel_finished( ch ) ){
410 freeable = 0;
411 break;
412 }
413 }
414 }
415 audio_unlock();
416 return freeable;
417 }
418
419 /*
420 * Free all resources for world instance
421 */
422 void world_free( world_instance *world )
423 {
424 vg_info( "Free world @%p\n", world );
425
426 /* free meshes */
427 mesh_free( &world->mesh_route_lines );
428 mesh_free( &world->mesh_geo );
429 mesh_free( &world->mesh_no_collide );
430 mesh_free( &world->mesh_water );
431
432 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
433 * existing buffer objects.
434 * */
435 glDeleteBuffers( 1, &world->tbo_light_entities );
436 glDeleteTextures( 1, &world->tex_light_entities );
437 glDeleteTextures( 1, &world->tex_light_cubes );
438
439 /* delete textures and meshes */
440 glDeleteTextures( world->texture_count-1, world->textures+1 );
441
442 u32 world_index = world - world_static.instances;
443 if( world_index ){
444 vg_linear_del( world_static.instances[world_index-1].heap,
445 vg_linear_header(world->heap) );
446 }
447
448 for( u32 i=0; i<mdl_arrcount(&world->ent_cubemap); i++ ){
449 ent_cubemap *cm = mdl_arritm(&world->ent_cubemap,i);
450 glDeleteTextures( 1, &cm->texture_id );
451 glDeleteFramebuffers( 1, &cm->framebuffer_id );
452 glDeleteRenderbuffers( 1, &cm->renderbuffer_id );
453 }
454
455 world->status = k_world_status_unloaded;
456 }
457
458 /*
459 * reset the world structure without deallocating persistent buffers
460 * TODO: Make this a memset(0), and have persistent items live in a static loc
461 */
462 static void world_init_blank( world_instance *world ){
463 memset( &world->meta, 0, sizeof(mdl_context) );
464
465 world->textures = NULL;
466 world->texture_count = 0;
467 world->surfaces = NULL;
468 world->surface_count = 0;
469
470 world->geo_bh = NULL;
471 world->entity_bh = NULL;
472 world->entity_list = NULL;
473 world->rendering_gate = NULL;
474
475 world->water.enabled = 0;
476 world->time = 0.0;
477
478 /* default lighting conditions
479 * -------------------------------------------------------------*/
480 struct ub_world_lighting *state = &world->ub_lighting;
481
482 state->g_light_preview = 0;
483 state->g_shadow_samples = 8;
484 state->g_water_fog = 0.04f;
485
486 v4_zero( state->g_water_plane );
487 v4_zero( state->g_depth_bounds );
488
489 state->g_shadow_length = 9.50f;
490 state->g_shadow_spread = 0.65f;
491
492 v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
493 v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
494 v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
495 v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
496 v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
497 v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
498 }
499
500 #endif /* WORLD_LOAD_C */