9095e1b4310a63f7ebbc5a97ccc0de4a0610e5dd
[carveJwlIkooP6JGAAIwe30JlM.git] / world_entity.c
1 #ifndef WORLD_ENTITY_C
2 #define WORLD_ENTITY_C
3
4 #include "model.h"
5 #include "entity.h"
6 #include "world.h"
7 #include "world_load.h"
8 #include "save.h"
9 #include "vg/vg_msg.h"
10 #include "menu.h"
11 #include "ent_challenge.h"
12 #include "ent_skateshop.h"
13 #include "ent_route.h"
14 #include "ent_traffic.h"
15
16 static void world_entity_focus( u32 entity_id ){
17 localplayer.immobile = 1;
18 menu.disable_open = 1;
19
20 v3_zero( localplayer.rb.v );
21 v3_zero( localplayer.rb.w );
22 player_walk.move_speed = 0.0f;
23 world_static.focused_entity = entity_id;
24 skaterift.activity = k_skaterift_ent_focus;
25 }
26
27 static void world_entity_unfocus(void){
28 localplayer.immobile = 0;
29 skaterift.activity = k_skaterift_default;
30 menu.disable_open = 0;
31 srinput.state = k_input_state_resume;
32 }
33
34 static void world_entity_focus_camera( world_instance *world, u32 uid ){
35 if( mdl_entity_id_type( uid ) == k_ent_camera ){
36 u32 index = mdl_entity_id_id( uid );
37 ent_camera *cam = mdl_arritm( &world->ent_camera, index );
38
39 v3f dir = {0.0f,-1.0f,0.0f};
40 mdl_transform_vector( &cam->transform, dir, dir );
41 v3_angles( dir, world_static.focus_cam.angles );
42 v3_copy( cam->transform.co, world_static.focus_cam.pos );
43 world_static.focus_cam.fov = cam->fov;
44 }
45 else {
46 camera_copy( &localplayer.cam, &world_static.focus_cam );
47
48 /* TODO ? */
49 world_static.focus_cam.nearz = localplayer.cam.nearz;
50 world_static.focus_cam.farz = localplayer.cam.farz;
51 }
52 }
53
54 /* logic preupdate */
55 static void world_entity_focus_preupdate(void){
56 f32 rate = vg_minf( 1.0f, vg.time_frame_delta * 2.0f );
57 int active = 0;
58 if( skaterift.activity == k_skaterift_ent_focus )
59 active = 1;
60
61 vg_slewf( &world_static.focus_strength, active,
62 vg.time_frame_delta * (1.0f/0.5f) );
63
64 u32 type = mdl_entity_id_type( world_static.focused_entity ),
65 index = mdl_entity_id_id( world_static.focused_entity );
66 world_instance *world = world_current_instance();
67
68 /* TODO: Table. */
69 if( type == k_ent_skateshop ){
70 ent_skateshop *skateshop = mdl_arritm( &world->ent_skateshop, index );
71 ent_skateshop_preupdate( skateshop, active );
72 }
73 else if( type == k_ent_challenge ){
74 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
75 ent_challenge_preupdate( challenge, active );
76 }
77 else if( type == k_ent_route ){
78 ent_route *route = mdl_arritm( &world->ent_route, index );
79 ent_route_preupdate( route, active );
80 }
81 }
82
83 /* additional renderings like text etc.. */
84 static void world_entity_focus_render(void){
85 world_instance *world = world_current_instance();
86 if( skaterift.activity != k_skaterift_ent_focus ){
87 skateshop_render_nonfocused( world, &skaterift.cam );
88 return;
89 }
90
91 u32 type = mdl_entity_id_type( world_static.focused_entity ),
92 index = mdl_entity_id_id( world_static.focused_entity );
93
94 if( type == k_ent_skateshop ){
95 ent_skateshop *skateshop = mdl_arritm( &world->ent_skateshop, index );
96 skateshop_render( skateshop );
97 }
98 else if( type == k_ent_challenge ){}
99 else if( type == k_ent_route ){}
100 else if( type == k_ent_miniworld ){}
101 else {
102 vg_fatal_error( "Programming error\n" );
103 }
104 }
105
106 static void world_gen_entities_init( world_instance *world ){
107 /* lights */
108 for( u32 j=0; j<mdl_arrcount(&world->ent_light); j ++ ){
109 ent_light *light = mdl_arritm( &world->ent_light, j );
110
111 m4x3f to_world;
112 q_m3x3( light->transform.q, to_world );
113 v3_copy( light->transform.co, to_world[3] );
114 m4x3_invert_affine( to_world, light->inverse_world );
115
116 light->angle_sin_cos[0] = sinf( light->angle * 0.5f );
117 light->angle_sin_cos[1] = cosf( light->angle * 0.5f );
118 }
119
120 /* gates */
121 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
122 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
123
124 if( !(gate->flags & k_ent_gate_nonlocal) ) {
125 gate_transform_update( gate );
126 }
127 }
128
129 vg_async_call( world_link_nonlocal_async, world, 0 );
130
131 /* water */
132 for( u32 j=0; j<mdl_arrcount(&world->ent_water); j++ ){
133 ent_water *water = mdl_arritm( &world->ent_water, j );
134 if( world->water.enabled ){
135 vg_warn( "Multiple water surfaces in level!\n" );
136 break;
137 }
138
139 world->water.enabled = 1;
140 water_set_surface( world, water->transform.co[1] );
141 }
142
143 /* volumes */
144 for( u32 j=0; j<mdl_arrcount(&world->ent_volume); j++ ){
145 ent_volume *volume = mdl_arritm( &world->ent_volume, j );
146 mdl_transform_m4x3( &volume->transform, volume->to_world );
147 m4x3_invert_full( volume->to_world, volume->to_local );
148 }
149
150 /* audio packs */
151 for( u32 j=0; j<mdl_arrcount(&world->ent_audio); j++ ){
152 ent_audio *audio = mdl_arritm( &world->ent_audio, j );
153
154 for( u32 k=0; k<audio->clip_count; k++ ){
155 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
156 audio->clip_start+k );
157
158 if( clip->_.file.pack_size ){
159 u32 size = clip->_.file.pack_size,
160 offset = clip->_.file.pack_offset;
161
162 /* embedded files are fine to clear the scratch buffer, only
163 * external audio uses it */
164
165 vg_linear_clear( vg_mem.scratch );
166 void *data = vg_linear_alloc( vg_mem.scratch,
167 clip->_.file.pack_size );
168
169 mdl_fread_pack_file( &world->meta, &clip->_.file, data );
170
171 clip->_.clip.path = NULL;
172 clip->_.clip.flags = audio->flags;
173 clip->_.clip.data = data;
174 clip->_.clip.size = size;
175 }
176 else{
177 clip->_.clip.path = mdl_pstr(&world->meta,clip->_.file.pstr_path);
178 clip->_.clip.flags = audio->flags;
179 clip->_.clip.data = NULL;
180 clip->_.clip.size = 0;
181 }
182
183 audio_clip_load( &clip->_.clip, world->heap );
184 }
185 }
186
187 /* create generic entity hierachy for those who need it */
188 u32 indexed_count = 0;
189 struct {
190 u32 type;
191 mdl_array_ptr *array;
192 }
193 indexables[] = {
194 { k_ent_gate, &world->ent_gate },
195 { k_ent_objective, &world->ent_objective },
196 { k_ent_volume, &world->ent_volume },
197 { k_ent_challenge, &world->ent_challenge }
198 };
199
200 for( u32 i=0; i<vg_list_size(indexables); i++ )
201 indexed_count += mdl_arrcount( indexables[i].array );
202 vg_info( "indexing %u entities\n", indexed_count );
203
204 world->entity_list = vg_linear_alloc( world->heap,
205 vg_align8(indexed_count*sizeof(u32)));
206
207 u32 index=0;
208 for( u32 i=0; i<vg_list_size(indexables); i++ ){
209 u32 type = indexables[i].type,
210 count = mdl_arrcount( indexables[i].array );
211
212 for( u32 j=0; j<count; j ++ )
213 world->entity_list[index ++] = mdl_entity_id( type, j );
214 }
215
216 world->entity_bh = bh_create( world->heap, &bh_system_entity_list, world,
217 indexed_count, 2 );
218
219 world->tar_min = world->entity_bh->nodes[0].bbx[0][1];
220 world->tar_max = world->entity_bh->nodes[0].bbx[1][1] + 20.0f;
221
222 for( u32 i=0; i<mdl_arrcount(&world->ent_marker); i++ ){
223 ent_marker *marker = mdl_arritm( &world->ent_marker, i );
224
225 if( MDL_CONST_PSTREQ( &world->meta, marker->pstr_alias, "tar_min" ) )
226 world->tar_min = marker->transform.co[1];
227
228 if( MDL_CONST_PSTREQ( &world->meta, marker->pstr_alias, "tar_max" ) )
229 world->tar_max = marker->transform.co[1];
230 }
231 }
232
233 static
234 ent_spawn *world_find_closest_spawn( world_instance *world, v3f position )
235 {
236 ent_spawn *rp = NULL, *r;
237 float min_dist = INFINITY;
238
239 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
240 r = mdl_arritm( &world->ent_spawn, i );
241 float d = v3_dist2( r->transform.co, position );
242
243 if( d < min_dist ){
244 min_dist = d;
245 rp = r;
246 }
247 }
248
249 if( !rp ){
250 if( mdl_arrcount(&world->ent_spawn) ){
251 vg_warn( "Invalid distances to spawns.. defaulting to first one.\n" );
252 return mdl_arritm( &world->ent_spawn, 0 );
253 }
254 else{
255 vg_error( "There are no spawns in the level!\n" );
256 }
257 }
258
259 return rp;
260 }
261
262 static
263 ent_spawn *world_find_spawn_by_name( world_instance *world, const char *name )
264 {
265 ent_spawn *rp = NULL, *r;
266 for( u32 i=0; i<mdl_arrcount(&world->ent_spawn); i++ ){
267 r = mdl_arritm( &world->ent_spawn, i );
268 if( !strcmp( mdl_pstr(&world->meta, r->pstr_name), name ) ){
269 rp = r;
270 break;
271 }
272 }
273
274 if( !rp )
275 vg_warn( "No spawn named '%s'\n", name );
276
277 return rp;
278 }
279
280 static void ent_volume_call( world_instance *world, ent_call *call )
281 {
282 u32 index = mdl_entity_id_id( call->id );
283 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
284 if( !volume->target ) return;
285
286 if( call->function == k_ent_function_trigger ){
287 call->id = volume->target;
288
289 if( volume->flags & k_ent_volume_flag_particles ){
290 float *co = alloca( sizeof(float)*3 );
291 co[0] = vg_randf64()*2.0f-1.0f;
292 co[1] = vg_randf64()*2.0f-1.0f;
293 co[2] = vg_randf64()*2.0f-1.0f;
294 m4x3_mulv( volume->to_world, co, co );
295
296 call->function = k_ent_function_particle_spawn;
297 call->data = co;
298 entity_call( world, call );
299 }
300 else{
301 call->function = volume->trigger.event;
302 entity_call( world, call );
303 }
304 }
305 else if( call->function == k_ent_function_trigger_leave ){
306 call->id = volume->target;
307
308 if( volume->flags & k_ent_volume_flag_particles ){
309 assert(0);
310 }
311 else{
312 call->function = volume->trigger.event_leave;
313 entity_call( world, call );
314 }
315 }
316 }
317
318 static void ent_audio_call( world_instance *world, ent_call *call ){
319 if( world->status == k_world_status_unloading ){
320 vg_warn( "cannot modify audio while unloading world\n" );
321 return;
322 }
323
324 u8 world_id = (world - world_static.instances) + 1;
325 u32 index = mdl_entity_id_id( call->id );
326 ent_audio *audio = mdl_arritm( &world->ent_audio, index );
327
328 v3f sound_co;
329
330 if( call->function == k_ent_function_particle_spawn ){
331 v3_copy( call->data, sound_co );
332 }
333 else if( call->function == k_ent_function_trigger ){
334 v3_copy( audio->transform.co, sound_co );
335 }
336 else
337 return;
338
339 float chance = vg_randf64()*100.0f,
340 bar = 0.0f;
341
342 for( u32 i=0; i<audio->clip_count; i++ ){
343 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
344 audio->clip_start+i );
345
346 float mod = world->probabilities[ audio->probability_curve ],
347 p = clip->probability * mod;
348
349 bar += p;
350 if( chance < bar ){
351 audio_lock();
352
353 if( audio->behaviour == k_channel_behaviour_unlimited ){
354 audio_oneshot_3d( &clip->_.clip, sound_co,
355 audio->transform.s[0],
356 audio->volume );
357 }
358 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
359 audio_channel *ch =
360 audio_get_group_idle_channel( audio->group,
361 audio->max_channels );
362
363 if( ch ){
364 audio_channel_init( ch, &clip->_.clip, audio->flags );
365 audio_channel_group( ch, audio->group );
366 audio_channel_world( ch, world_id );
367 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
368 audio_channel_edit_volume( ch, audio->volume, 1 );
369 ch = audio_relinquish_channel( ch );
370 }
371 }
372 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
373 audio_channel *ch =
374 audio_get_group_idle_channel( audio->group,
375 audio->max_channels );
376
377 /* group is full */
378 if( !ch ){
379 audio_channel *existing =
380 audio_get_group_first_active_channel( audio->group );
381
382 if( existing ){
383 if( existing->source == &clip->_.clip ){
384 audio_unlock();
385 return;
386 }
387
388 existing->group = 0;
389 existing = audio_channel_fadeout(existing, audio->crossfade);
390 }
391
392 ch = audio_get_first_idle_channel();
393 }
394
395 if( ch ){
396 audio_channel_init( ch, &clip->_.clip, audio->flags );
397 audio_channel_group( ch, audio->group );
398 audio_channel_world( ch, world_id );
399 audio_channel_fadein( ch, audio->crossfade );
400 ch = audio_relinquish_channel( ch );
401 }
402 }
403
404 audio_unlock();
405 return;
406 }
407 }
408 }
409
410
411 static void ent_ccmd_call( world_instance *world, ent_call *call ){
412 if( call->function == k_ent_function_trigger ){
413 u32 index = mdl_entity_id_id( call->id );
414 ent_ccmd *ccmd = mdl_arritm( &world->ent_ccmd, index );
415 vg_execute_console_input( mdl_pstr(&world->meta, ccmd->pstr_command) );
416 }
417 }
418
419 /*
420 * BVH implementation
421 * ----------------------------------------------------------------------------
422 */
423
424 static void
425 entity_bh_expand_bound( void *user, boxf bound, u32 item_index ){
426 world_instance *world = user;
427
428 u32 id = world->entity_list[ item_index ],
429 type = mdl_entity_id_type( id ),
430 index = mdl_entity_id_id( id );
431
432 if( type == k_ent_gate ){
433 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
434 boxf box = {{ -gate->dimensions[0], -gate->dimensions[1], -0.1f },
435 { gate->dimensions[0], gate->dimensions[1], 0.1f }};
436
437 m4x3_expand_aabb_aabb( gate->to_world, bound, box );
438 }
439 else if( type == k_ent_objective ){
440 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
441
442 /* TODO: This might be more work than necessary. could maybe just get
443 * away with representing them as points */
444
445 boxf box;
446 box_init_inf( box );
447
448 for( u32 i=0; i<objective->submesh_count; i++ ){
449 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
450 objective->submesh_start+i );
451 box_concat( box, sm->bbx );
452 }
453
454 m4x3f transform;
455 mdl_transform_m4x3( &objective->transform, transform );
456 m4x3_expand_aabb_aabb( transform, bound, box );
457 }
458 else if( type == k_ent_volume ){
459 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
460 m4x3_expand_aabb_aabb( volume->to_world, bound,
461 (boxf){{-1.0f,-1.0f,-1.0f},{ 1.0f, 1.0f, 1.0f}} );
462 }
463 else if( type == k_ent_challenge ){
464 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
465
466 boxf box = {{-1.2f*0.5f,-0.72f*0.5f,-0.01f*0.5f},
467 { 1.2f*0.5f, 0.72f*0.5f, 0.01f*0.5f}};
468 m4x3f transform;
469 mdl_transform_m4x3( &challenge->transform, transform );
470 m4x3_expand_aabb_aabb( transform, bound, box );
471 }
472 else{
473 vg_fatal_error( "Programming error\n" );
474 }
475 }
476
477 static float entity_bh_centroid( void *user, u32 item_index, int axis ){
478 world_instance *world = user;
479
480 u32 id = world->entity_list[ item_index ],
481 type = mdl_entity_id_type( id ),
482 index = mdl_entity_id_id( id );
483
484 if( type == k_ent_gate ){
485 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
486 return gate->to_world[3][axis];
487 }
488 else if( type == k_ent_objective ){
489 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
490 return objective->transform.co[axis];
491 }
492 else if( type == k_ent_volume ){
493 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
494 return volume->transform.co[axis];
495 }
496 else if( type == k_ent_challenge ){
497 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
498 return challenge->transform.co[axis];
499 }
500 else {
501 vg_fatal_error( "Programming error\n" );
502 return INFINITY;
503 }
504 }
505
506 static void entity_bh_swap( void *user, u32 ia, u32 ib ){
507 world_instance *world = user;
508
509 u32 a = world->entity_list[ ia ],
510 b = world->entity_list[ ib ];
511
512 world->entity_list[ ia ] = b;
513 world->entity_list[ ib ] = a;
514 }
515
516 static void entity_bh_debug( void *user, u32 item_index ){
517 world_instance *world = user;
518
519 u32 id = world->entity_list[ item_index ],
520 type = mdl_entity_id_type( id ),
521 index = mdl_entity_id_id( id );
522
523 if( type == k_ent_gate ){
524 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
525 boxf box = {{ -gate->dimensions[0], -gate->dimensions[1], -0.1f },
526 { gate->dimensions[0], gate->dimensions[1], 0.1f }};
527 vg_line_boxf_transformed( gate->to_world, box, 0xf000ff00 );
528 }
529 else if( type == k_ent_objective ){
530 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
531 boxf box;
532 box_init_inf( box );
533
534 for( u32 i=0; i<objective->submesh_count; i++ ){
535 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
536 objective->submesh_start+i );
537 box_concat( box, sm->bbx );
538 }
539
540 m4x3f transform;
541 mdl_transform_m4x3( &objective->transform, transform );
542 vg_line_boxf_transformed( transform, box, 0xf000ff00 );
543 }
544 else if( type == k_ent_volume ){
545 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
546 vg_line_boxf_transformed( volume->to_world,
547 (boxf){{-1.0f,-1.0f,-1.0f},{ 1.0f, 1.0f, 1.0f}},
548 0xf000ff00 );
549 }
550 else if( type == k_ent_challenge ){
551 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
552
553 boxf box = {{-1.2f*0.5f,-0.72f*0.5f,-0.01f*0.5f},
554 { 1.2f*0.5f, 0.72f*0.5f, 0.01f*0.5f}};
555 m4x3f transform;
556 mdl_transform_m4x3( &challenge->transform, transform );
557 vg_line_boxf_transformed( transform, box, 0xf0ff0000 );
558 }
559 else{
560 vg_fatal_error( "Programming error\n" );
561 }
562 }
563
564 static void update_ach_models(void){
565 world_instance *hub = &world_static.instances[k_world_purpose_hub];
566 if( hub->status != k_world_status_loaded ) return;
567
568 for( u32 i=0; i<mdl_arrcount( &hub->ent_prop ); i ++ ){
569 ent_prop *prop = mdl_arritm( &hub->ent_prop, i );
570 if( prop->flags & 0x2 ){
571 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "MARC" ) )
572 if( skaterift.achievements & 0x1 )
573 prop->flags &= ~0x1;
574 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "ALBERT" ) )
575 if( skaterift.achievements & 0x2 )
576 prop->flags &= ~0x1;
577 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "JANET" ) )
578 if( skaterift.achievements & 0x4 )
579 prop->flags &= ~0x1;
580 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "BERNADETTA" ) )
581 if( skaterift.achievements & 0x8 )
582 prop->flags &= ~0x1;
583 }
584 }
585 }
586
587 static void entity_bh_closest( void *user, u32 item_index, v3f point,
588 v3f closest ){
589 world_instance *world = user;
590
591 u32 id = world->entity_list[ item_index ],
592 type = mdl_entity_id_type( id ),
593 index = mdl_entity_id_id( id );
594
595 if( type == k_ent_gate ){
596 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
597 v3_copy( gate->to_world[3], closest );
598 }
599 else if( type == k_ent_objective ){
600 ent_objective *challenge = mdl_arritm( &world->ent_objective, index );
601 v3_copy( challenge->transform.co, closest );
602 }
603 else if( type == k_ent_volume ){
604 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
605 v3_copy( volume->to_world[3], closest );
606 }
607 else if( type == k_ent_challenge ){
608 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
609 v3_copy( challenge->transform.co, closest );
610 }
611 else{
612 vg_fatal_error( "Programming error\n" );
613 }
614 }
615
616 static void world_entity_start( world_instance *world, vg_msg *sav ){
617 vg_info( "Start instance %p\n", world );
618
619 world->probabilities[ k_probability_curve_constant ] = 1.0f;
620 for( u32 i=0; i<mdl_arrcount(&world->ent_audio); i++ ){
621 ent_audio *audio = mdl_arritm(&world->ent_audio,i);
622 if( audio->flags & AUDIO_FLAG_AUTO_START ){
623 ent_call call;
624 call.data = NULL;
625 call.function = k_ent_function_trigger;
626 call.id = mdl_entity_id( k_ent_audio, i );
627 entity_call( world, &call );
628 }
629 }
630
631 /* read savedata
632 * ----------------------------------------------------------------------- */
633
634 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
635 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
636 const char *alias = mdl_pstr( &world->meta, challenge->pstr_alias );
637
638 if( vg_msg_getkvu32( sav, alias, 0 ) ){
639 ent_call call;
640 call.data = NULL;
641 call.function = 0;
642 call.id = mdl_entity_id( k_ent_challenge, i );
643 entity_call( world, &call );
644 }
645 }
646
647 vg_msg routes_block = *sav;
648 if( vg_msg_seekframe( &routes_block, "routes" ) ){
649 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
650 ent_route *route = mdl_arritm( &world->ent_route, i );
651
652 vg_msg route_info = routes_block;
653 if( vg_msg_seekframe( &route_info,
654 mdl_pstr(&world->meta,route->pstr_name) ) ){
655 route->flags |= vg_msg_getkvu32( &route_info, "flags", 0 );
656 route->best_laptime =
657 vg_msg_getkvf64( &route_info, "best_laptime", 0.0 );
658
659 f32 sections[ route->checkpoints_count ];
660 vg_msg_cmd cmd;
661 if( vg_msg_getkvcmd( &route_info, "sections", &cmd ) ){
662 vg_msg_cast( cmd.value, cmd.code, sections,
663 k_vg_msg_f32 |
664 vg_msg_count_bits(route->checkpoints_count) );
665 }
666 else{
667 for( u32 j=0; j<route->checkpoints_count; j ++ )
668 sections[j] = 0.0f;
669 }
670
671 for( u32 j=0; j<route->checkpoints_count; j ++ ){
672 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
673 route->checkpoints_start + j );
674
675 cp->best_time = sections[j];
676 }
677
678 /* LEGACY: check if steam achievements can give us a medal */
679 if( steam_ready && steam_stats_ready ){
680 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
681 struct track_info *inf = &track_infos[j];
682 if( !strcmp(inf->name,
683 mdl_pstr(&world->meta,route->pstr_name))){
684
685 steamapi_bool set = 0;
686 if( SteamAPI_ISteamUserStats_GetAchievement(
687 hSteamUserStats, inf->achievement_id, &set ) )
688 {
689 if( set ){
690 route->flags |= k_ent_route_flag_achieve_silver;
691 }
692 }
693 }
694 }
695 }
696 }
697 }
698 }
699
700 ent_region_re_eval( world );
701 }
702
703 static void world_entity_serialize( world_instance *world, vg_msg *sav ){
704 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
705 ent_challenge *challenge = mdl_arritm(&world->ent_challenge,i);
706
707 const char *alias = mdl_pstr(&world->meta,challenge->pstr_alias);
708 vg_msg_wkvu32( sav, alias, challenge->status );
709 }
710
711 if( mdl_arrcount(&world->ent_route) ){
712 vg_msg_frame( sav, "routes" );
713 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
714 ent_route *route = mdl_arritm( &world->ent_route, i );
715
716 vg_msg_frame( sav, mdl_pstr( &world->meta, route->pstr_name ) );
717 {
718 vg_msg_wkvu32( sav, "flags", route->flags );
719 vg_msg_wkvf64( sav, "best_laptime", route->best_laptime );
720
721 f32 sections[ route->checkpoints_count ];
722
723 for( u32 j=0; j<route->checkpoints_count; j ++ ){
724 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
725 route->checkpoints_start + j );
726
727 sections[j] = cp->best_time;
728 }
729
730 vg_msg_wkvnum( sav, "sections", k_vg_msg_f32,
731 route->checkpoints_count, sections );
732 }
733 vg_msg_end_frame( sav );
734 }
735 vg_msg_end_frame( sav );
736 }
737 }
738
739 #endif /* WORLD_ENTITY_C */