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