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