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