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