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