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