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