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