enable workshop world submissions
[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
9 /*
10 * load the .mdl file located in path (relative to exe), it will load into the
11 * slot specified by world_loader.world_index
12 */
13 VG_STATIC void world_load_mdl( const char *path )
14 {
15 vg_rand_seed( 9001 );
16
17 world_instance *world = world_loading_instance();
18 world_init_blank( world );
19 world->status = k_world_status_loading;
20
21 vg_info( "Loading world: %s\n", path );
22
23 void *allocator = NULL;
24 if( world_loader.world_index == 0 ) allocator = world_static.heap;
25 else allocator = world_static.worlds[world_loader.world_index-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
63 mdl_array_ptr infos;
64 mdl_load_array( meta, &infos, "ent_worldinfo", vg_mem.scratch );
65
66 if( mdl_arrcount(&infos) ){
67 world->info = *((ent_worldinfo *)mdl_arritm(&infos,0));
68 }
69 else{
70 world->info.pstr_author = 0;
71 world->info.pstr_desc = 0;
72 world->info.pstr_name = 0;
73 world->info.timezone = 0.0f;
74 }
75
76 time_t t;
77 struct tm *tm;
78 time( &t );
79 tm = gmtime( &t );
80 world->time = (float)tm->tm_min/20.0f + (world->info.timezone/24.0f);
81
82 /* process resources from pack */
83 world_gen_load_surfaces();
84 world_gen_routes_ent_init();
85 world_gen_entities_init();
86 world->volume_bh = bh_create( heap, &bh_system_volumes, world,
87 mdl_arrcount( &world->ent_volume ), 1 );
88
89 /* main bulk */
90 world_gen_generate_meshes();
91 world_gen_routes_generate();
92 world_gen_compute_light_indices();
93 vg_async_call( async_world_postprocess_render, NULL, 0 );
94 vg_async_stall();
95
96 mdl_close( meta );
97 world->status = k_world_status_loaded;
98 }
99
100 /*
101 * op: k_async_op_world_loading
102 * k_async_op_world_preloading
103 * -----------------------------------------------------------------------------
104 */
105
106 static void async_skaterift_world_loaded( void *payload, u32 size )
107 {
108 skaterift_end_op();
109 }
110
111 /*
112 * Does a complete world switch using the remaining free slots
113 */
114 static void skaterift_world_changer_thread( void *data ){
115 char path_buf[4096];
116 vg_str path;
117 vg_strnull( &path, path_buf, 4096 );
118
119 if( world_loader.reg ){
120 addon_get_content_folder( world_loader.reg, &path );
121 }
122 else {
123 vg_strcat( &path, "maps/" );
124 vg_strcat( &path, world_loader.override_name );
125 }
126
127 vg_str folder = path;
128 if( !vg_strgood( &folder ) ) {
129 vg_error( "Load target too long\n" );
130 vg_async_call( workshop_async_any_complete, NULL, 0 );
131 return;
132 }
133
134 char worlds[3][4096];
135 u32 i=0;
136
137 vg_dir dir;
138 if( !vg_dir_open(&dir, folder.buffer) ){
139 vg_error( "opendir('%s') failed\n", folder.buffer );
140 vg_async_call( async_skaterift_world_loaded, NULL, 0 );
141 return;
142 }
143
144 while( vg_dir_next_entry(&dir) ){
145 if( vg_dir_entry_type(&dir) == k_vg_entry_type_file ){
146 const char *d_name = vg_dir_entry_name(&dir);
147 if( d_name[0] == '.' ) continue;
148
149 vg_str file = folder;
150 vg_strcat( &file, "/" );
151 vg_strcat( &file, d_name );
152 if( !vg_strgood( &file ) ) continue;
153
154 char *ext = vg_strch( &file, '.' );
155 if( !ext ) continue;
156 if( strcmp(ext,".mdl") ) continue;
157
158 if( i == 3 ){
159 vg_warn( "There are too many .mdl files in the map folder!(3)\n" );
160 break;
161 }
162
163 strcpy( worlds[i++], file.buffer );
164 }
165 }
166 vg_dir_close(&dir);
167
168 if( i == 0 ){
169 vg_warn( "There are no .mdl files in the map folder.\n" );
170 }
171
172 u32 first_index = 0;
173 for( u32 j=0; j<i; j++ ){
174 vg_str name = { .buffer = worlds[j], .i=strlen(worlds[j]),
175 sizeof(worlds[j]) };
176 char *fname = vg_strch( &name, '/' );
177 if( fname ){
178 if( !strcmp( fname+1, "main.mdl" ) ){
179 first_index = j;
180 }
181 }
182 }
183
184 world_loader.generate_point_cloud = 1;
185 world_loader.world_index = 1;
186 world_load_mdl( worlds[first_index] );
187
188 world_loader.generate_point_cloud = 0;
189 for( u32 j=0; j<i; j++ ){
190 if( j != first_index ){
191 world_loader.world_index = j+1;
192 world_load_mdl( worlds[j] );
193 }
194 }
195
196 vg_async_call( async_skaterift_world_loaded, NULL, 0 );
197 }
198
199 /* holding pattern before we can start loading the new world, since we might be
200 * waiting for audio to stop */
201 static void skaterift_change_world_preupdate(void)
202 {
203 for( u32 i=1; i<vg_list_size(world_static.worlds); i++ ){
204 world_instance *inst = &world_static.worlds[i];
205
206 if( inst->status == k_world_status_unloading ){
207 if( world_freeable( inst ) ){
208 world_free( inst );
209 }
210 return;
211 }
212 }
213
214 vg_info( "worlds cleared, begining load\n" );
215 skaterift_shift_op( k_async_op_world_loading );
216
217 /* finally can start the loader */
218 vg_loader_start( skaterift_world_changer_thread, NULL );
219 }
220
221 /* places all loaded worlds into unloading state */
222 static void skaterift_change_world_start(void){
223 if( world_loader.reg ){
224 vg_info( "switching to %s ("PRINTF_U64"\n",
225 world_loader.reg->foldername, world_loader.reg->workshop_id );
226 }
227 else{
228 vg_info( "switching to %s(local)\n", world_loader.override_name );
229 }
230
231 if( world_static.active_world != 0 ){
232 vg_error( "Cannot change worlds while in non-root world\n" );
233 }
234 else{
235 skaterift_begin_op( k_async_op_world_preloading );
236
237 vg_linear_clear( vg_mem.scratch ); /* ?? */
238 vg_info( "unloading old worlds\n" );
239 world_unlink_nonlocal( &world_static.worlds[0] );
240
241 for( u32 i=1; i<vg_list_size(world_static.worlds); i++ ){
242 world_instance *inst = &world_static.worlds[i];
243
244 if( inst->status == k_world_status_loaded ){
245 inst->status = k_world_status_unloading;
246 world_fadeout_audio( inst );
247 }
248 }
249 }
250 }
251
252 /* console command for the above function */
253 static int skaterift_change_world_command( int argc, const char *argv[] )
254 {
255 if( argc == 1 ){
256 world_loader.reg = NULL;
257 vg_strncpy( argv[0], world_loader.override_name,
258 vg_list_size( world_loader.override_name ),
259 k_strncpy_always_add_null );
260 skaterift_change_world_start();
261 }
262
263 return 0;
264 }
265
266
267 static world_instance *world_loading_instance(void){
268 return &world_static.worlds[ world_loader.world_index ];
269 }
270
271 /*
272 * checks:
273 * 1. to see if all audios owned by the world have been stopped
274 * 2. that this is the least significant world
275 */
276 static int world_freeable( world_instance *world )
277 {
278 if( world->status != k_world_status_unloading ) return 0;
279 u8 world_id = (world - world_static.worlds) + 1;
280
281 for( u32 i=world_id; i<vg_list_size(world_static.worlds); i++ ){
282 if( world_static.worlds[i].status != k_world_status_unloaded ){
283 return 0;
284 }
285 }
286
287 int freeable = 1;
288 audio_lock();
289 for( u32 i=0; i<AUDIO_CHANNELS; i++ ){
290 audio_channel *ch = &vg_audio.channels[i];
291
292 if( ch->allocated && (ch->world_id == world_id)){
293 if( !audio_channel_finished( ch ) ){
294 freeable = 0;
295 break;
296 }
297 }
298 }
299 audio_unlock();
300 return freeable;
301 }
302
303 /*
304 * Free all resources for world instance
305 */
306 static void world_free( world_instance *world )
307 {
308 vg_info( "Free world @%p\n", world );
309
310 /* free meshes */
311 mesh_free( &world->mesh_route_lines );
312 mesh_free( &world->mesh_geo );
313 mesh_free( &world->mesh_no_collide );
314 mesh_free( &world->mesh_water );
315
316 /* glDeleteBuffers silently ignores 0's and names that do not correspond to
317 * existing buffer objects.
318 * */
319 glDeleteBuffers( 1, &world->tbo_light_entities );
320 glDeleteTextures( 1, &world->tex_light_entities );
321 glDeleteTextures( 1, &world->tex_light_cubes );
322
323 /* delete textures and meshes */
324 glDeleteTextures( world->texture_count, world->textures );
325
326 u32 world_index = world - world_static.worlds;
327 if( world_index ){
328 vg_linear_del( world_static.worlds[world_index-1].heap,
329 vg_linear_header(world->heap) );
330 }
331
332 world->status = k_world_status_unloaded;
333 }
334
335 /*
336 * reset the world structure without deallocating persistent buffers
337 * TODO: Make this a memset(0), and have persistent items live in a static loc
338 */
339 VG_STATIC void world_init_blank( world_instance *world )
340 {
341 memset( &world->meta, 0, sizeof(mdl_context) );
342
343 world->textures = NULL;
344 world->texture_count = 0;
345 world->surfaces = NULL;
346 world->surface_count = 0;
347
348 world->geo_bh = NULL;
349 world->volume_bh = NULL;
350 world->audio_bh = NULL;
351 world->rendering_gate = NULL;
352
353 world->water.enabled = 0;
354 world->time = 0.0;
355
356 /* default lighting conditions
357 * -------------------------------------------------------------*/
358 struct ub_world_lighting *state = &world->ub_lighting;
359
360 state->g_light_preview = 0;
361 state->g_shadow_samples = 8;
362 state->g_water_fog = 0.04f;
363
364 v4_zero( state->g_water_plane );
365 v4_zero( state->g_depth_bounds );
366
367 state->g_shadow_length = 9.50f;
368 state->g_shadow_spread = 0.65f;
369
370 v3_copy( (v3f){0.37f, 0.54f, 0.97f}, state->g_daysky_colour );
371 v3_copy( (v3f){0.03f, 0.05f, 0.20f}, state->g_nightsky_colour );
372 v3_copy( (v3f){1.00f, 0.32f, 0.01f}, state->g_sunset_colour );
373 v3_copy( (v3f){0.13f, 0.17f, 0.35f}, state->g_ambient_colour );
374 v3_copy( (v3f){0.25f, 0.17f, 0.51f}, state->g_sunset_ambient );
375 v3_copy( (v3f){1.10f, 0.89f, 0.35f}, state->g_sun_colour );
376 }
377
378
379
380 #endif /* WORLD_LOAD_C */