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