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