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