handplants
[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 u32 index = mdl_entity_id_id( call->id );
282 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
283 if( !volume->target ) return;
284
285 if( call->function == k_ent_function_trigger ){
286 call->id = volume->target;
287
288 if( volume->flags & k_ent_volume_flag_particles ){
289 float *co = alloca( sizeof(float)*3 );
290 co[0] = vg_randf64(&vg.rand)*2.0f-1.0f;
291 co[1] = vg_randf64(&vg.rand)*2.0f-1.0f;
292 co[2] = vg_randf64(&vg.rand)*2.0f-1.0f;
293 m4x3_mulv( volume->to_world, co, co );
294
295 call->function = k_ent_function_particle_spawn;
296 call->data = co;
297 entity_call( world, call );
298 }
299 else{
300 call->function = volume->trigger.event;
301 entity_call( world, call );
302 }
303 }
304 else if( call->function == k_ent_function_trigger_leave ){
305 call->id = volume->target;
306
307 if( volume->flags & k_ent_volume_flag_particles ){
308 assert(0);
309 }
310 else{
311 call->function = volume->trigger.event_leave;
312 entity_call( world, call );
313 }
314 }
315 }
316
317 static void ent_audio_call( world_instance *world, ent_call *call ){
318 if( world->status == k_world_status_unloading ){
319 vg_warn( "cannot modify audio while unloading world\n" );
320 return;
321 }
322
323 u8 world_id = (world - world_static.instances) + 1;
324 u32 index = mdl_entity_id_id( call->id );
325 ent_audio *audio = mdl_arritm( &world->ent_audio, index );
326
327 v3f sound_co;
328
329 if( call->function == k_ent_function_particle_spawn ){
330 v3_copy( call->data, sound_co );
331 }
332 else if( call->function == k_ent_function_trigger ){
333 v3_copy( audio->transform.co, sound_co );
334 }
335 else
336 return;
337
338 float chance = vg_randf64(&vg.rand)*100.0f,
339 bar = 0.0f;
340
341 for( u32 i=0; i<audio->clip_count; i++ ){
342 ent_audio_clip *clip = mdl_arritm( &world->ent_audio_clip,
343 audio->clip_start+i );
344
345 float mod = world->probabilities[ audio->probability_curve ],
346 p = clip->probability * mod;
347
348 bar += p;
349 if( chance < bar ){
350 audio_lock();
351
352 if( audio->behaviour == k_channel_behaviour_unlimited ){
353 audio_oneshot_3d( &clip->_.clip, sound_co,
354 audio->transform.s[0],
355 audio->volume );
356 }
357 else if( audio->behaviour == k_channel_behaviour_discard_if_full ){
358 audio_channel *ch =
359 audio_get_group_idle_channel( audio->group,
360 audio->max_channels );
361
362 if( ch ){
363 audio_channel_init( ch, &clip->_.clip, audio->flags );
364 audio_channel_group( ch, audio->group );
365 audio_channel_world( ch, world_id );
366 audio_channel_set_spacial( ch, sound_co, audio->transform.s[0] );
367 audio_channel_edit_volume( ch, audio->volume, 1 );
368 ch = audio_relinquish_channel( ch );
369 }
370 }
371 else if( audio->behaviour == k_channel_behaviour_crossfade_if_full){
372 audio_channel *ch =
373 audio_get_group_idle_channel( audio->group,
374 audio->max_channels );
375
376 /* group is full */
377 if( !ch ){
378 audio_channel *existing =
379 audio_get_group_first_active_channel( audio->group );
380
381 if( existing ){
382 if( existing->source == &clip->_.clip ){
383 audio_unlock();
384 return;
385 }
386
387 existing->group = 0;
388 existing = audio_channel_fadeout(existing, audio->crossfade);
389 }
390
391 ch = audio_get_first_idle_channel();
392 }
393
394 if( ch ){
395 audio_channel_init( ch, &clip->_.clip, audio->flags );
396 audio_channel_group( ch, audio->group );
397 audio_channel_world( ch, world_id );
398 audio_channel_fadein( ch, audio->crossfade );
399 ch = audio_relinquish_channel( ch );
400 }
401 }
402
403 audio_unlock();
404 return;
405 }
406 }
407 }
408
409
410 static void ent_ccmd_call( world_instance *world, ent_call *call ){
411 if( call->function == k_ent_function_trigger ){
412 u32 index = mdl_entity_id_id( call->id );
413 ent_ccmd *ccmd = mdl_arritm( &world->ent_ccmd, index );
414 vg_execute_console_input( mdl_pstr(&world->meta, ccmd->pstr_command) );
415 }
416 }
417
418 /*
419 * BVH implementation
420 * ----------------------------------------------------------------------------
421 */
422
423 static void
424 entity_bh_expand_bound( void *user, boxf bound, u32 item_index ){
425 world_instance *world = user;
426
427 u32 id = world->entity_list[ item_index ],
428 type = mdl_entity_id_type( id ),
429 index = mdl_entity_id_id( id );
430
431 if( type == k_ent_gate ){
432 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
433 boxf box = {{ -gate->dimensions[0], -gate->dimensions[1], -0.1f },
434 { gate->dimensions[0], gate->dimensions[1], 0.1f }};
435
436 m4x3_expand_aabb_aabb( gate->to_world, bound, box );
437 }
438 else if( type == k_ent_objective ){
439 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
440
441 /* TODO: This might be more work than necessary. could maybe just get
442 * away with representing them as points */
443
444 boxf box;
445 box_init_inf( box );
446
447 for( u32 i=0; i<objective->submesh_count; i++ ){
448 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
449 objective->submesh_start+i );
450 box_concat( box, sm->bbx );
451 }
452
453 m4x3f transform;
454 mdl_transform_m4x3( &objective->transform, transform );
455 m4x3_expand_aabb_aabb( transform, bound, box );
456 }
457 else if( type == k_ent_volume ){
458 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
459 m4x3_expand_aabb_aabb( volume->to_world, bound,
460 (boxf){{-1.0f,-1.0f,-1.0f},{ 1.0f, 1.0f, 1.0f}} );
461 }
462 else if( type == k_ent_challenge ){
463 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
464
465 boxf box = {{-1.2f*0.5f,-0.72f*0.5f,-0.01f*0.5f},
466 { 1.2f*0.5f, 0.72f*0.5f, 0.01f*0.5f}};
467 m4x3f transform;
468 mdl_transform_m4x3( &challenge->transform, transform );
469 m4x3_expand_aabb_aabb( transform, bound, box );
470 }
471 else{
472 vg_fatal_error( "Programming error\n" );
473 }
474 }
475
476 static float entity_bh_centroid( void *user, u32 item_index, int axis ){
477 world_instance *world = user;
478
479 u32 id = world->entity_list[ item_index ],
480 type = mdl_entity_id_type( id ),
481 index = mdl_entity_id_id( id );
482
483 if( type == k_ent_gate ){
484 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
485 return gate->to_world[3][axis];
486 }
487 else if( type == k_ent_objective ){
488 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
489 return objective->transform.co[axis];
490 }
491 else if( type == k_ent_volume ){
492 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
493 return volume->transform.co[axis];
494 }
495 else if( type == k_ent_challenge ){
496 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
497 return challenge->transform.co[axis];
498 }
499 else {
500 vg_fatal_error( "Programming error\n" );
501 return INFINITY;
502 }
503 }
504
505 static void entity_bh_swap( void *user, u32 ia, u32 ib ){
506 world_instance *world = user;
507
508 u32 a = world->entity_list[ ia ],
509 b = world->entity_list[ ib ];
510
511 world->entity_list[ ia ] = b;
512 world->entity_list[ ib ] = a;
513 }
514
515 static void entity_bh_debug( void *user, u32 item_index ){
516 world_instance *world = user;
517
518 u32 id = world->entity_list[ item_index ],
519 type = mdl_entity_id_type( id ),
520 index = mdl_entity_id_id( id );
521
522 if( type == k_ent_gate ){
523 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
524 boxf box = {{ -gate->dimensions[0], -gate->dimensions[1], -0.1f },
525 { gate->dimensions[0], gate->dimensions[1], 0.1f }};
526 vg_line_boxf_transformed( gate->to_world, box, 0xf000ff00 );
527 }
528 else if( type == k_ent_objective ){
529 ent_objective *objective = mdl_arritm( &world->ent_objective, index );
530 boxf box;
531 box_init_inf( box );
532
533 for( u32 i=0; i<objective->submesh_count; i++ ){
534 mdl_submesh *sm = mdl_arritm( &world->meta.submeshs,
535 objective->submesh_start+i );
536 box_concat( box, sm->bbx );
537 }
538
539 m4x3f transform;
540 mdl_transform_m4x3( &objective->transform, transform );
541 vg_line_boxf_transformed( transform, box, 0xf000ff00 );
542 }
543 else if( type == k_ent_volume ){
544 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
545 vg_line_boxf_transformed( volume->to_world,
546 (boxf){{-1.0f,-1.0f,-1.0f},{ 1.0f, 1.0f, 1.0f}},
547 0xf000ff00 );
548 }
549 else if( type == k_ent_challenge ){
550 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
551
552 boxf box = {{-1.2f*0.5f,-0.72f*0.5f,-0.01f*0.5f},
553 { 1.2f*0.5f, 0.72f*0.5f, 0.01f*0.5f}};
554 m4x3f transform;
555 mdl_transform_m4x3( &challenge->transform, transform );
556 vg_line_boxf_transformed( transform, box, 0xf0ff0000 );
557 }
558 else{
559 vg_fatal_error( "Programming error\n" );
560 }
561 }
562
563 static void update_ach_models(void){
564 world_instance *hub = &world_static.instances[k_world_purpose_hub];
565 if( hub->status != k_world_status_loaded ) return;
566
567 for( u32 i=0; i<mdl_arrcount( &hub->ent_prop ); i ++ ){
568 ent_prop *prop = mdl_arritm( &hub->ent_prop, i );
569 if( prop->flags & 0x2 ){
570 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "MARC" ) )
571 if( skaterift.achievements & 0x1 )
572 prop->flags &= ~0x1;
573 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "ALBERT" ) )
574 if( skaterift.achievements & 0x2 )
575 prop->flags &= ~0x1;
576 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "JANET" ) )
577 if( skaterift.achievements & 0x4 )
578 prop->flags &= ~0x1;
579 if( MDL_CONST_PSTREQ( &hub->meta, prop->pstr_alias, "BERNADETTA" ) )
580 if( skaterift.achievements & 0x8 )
581 prop->flags &= ~0x1;
582 }
583 }
584 }
585
586 static void entity_bh_closest( void *user, u32 item_index, v3f point,
587 v3f closest ){
588 world_instance *world = user;
589
590 u32 id = world->entity_list[ item_index ],
591 type = mdl_entity_id_type( id ),
592 index = mdl_entity_id_id( id );
593
594 if( type == k_ent_gate ){
595 ent_gate *gate = mdl_arritm( &world->ent_gate, index );
596 v3_copy( gate->to_world[3], closest );
597 }
598 else if( type == k_ent_objective ){
599 ent_objective *challenge = mdl_arritm( &world->ent_objective, index );
600 v3_copy( challenge->transform.co, closest );
601 }
602 else if( type == k_ent_volume ){
603 ent_volume *volume = mdl_arritm( &world->ent_volume, index );
604 v3_copy( volume->to_world[3], closest );
605 }
606 else if( type == k_ent_challenge ){
607 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, index );
608 v3_copy( challenge->transform.co, closest );
609 }
610 else{
611 vg_fatal_error( "Programming error\n" );
612 }
613 }
614
615 static void world_entity_start( world_instance *world, vg_msg *sav ){
616 vg_info( "Start instance %p\n", world );
617
618 world->probabilities[ k_probability_curve_constant ] = 1.0f;
619 for( u32 i=0; i<mdl_arrcount(&world->ent_audio); i++ ){
620 ent_audio *audio = mdl_arritm(&world->ent_audio,i);
621 if( audio->flags & AUDIO_FLAG_AUTO_START ){
622 ent_call call;
623 call.data = NULL;
624 call.function = k_ent_function_trigger;
625 call.id = mdl_entity_id( k_ent_audio, i );
626 entity_call( world, &call );
627 }
628 }
629
630 /* read savedata
631 * ----------------------------------------------------------------------- */
632
633 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
634 ent_challenge *challenge = mdl_arritm( &world->ent_challenge, i );
635 const char *alias = mdl_pstr( &world->meta, challenge->pstr_alias );
636
637 if( vg_msg_getkvu32( sav, alias, 0 ) ){
638 ent_call call;
639 call.data = NULL;
640 call.function = 0;
641 call.id = mdl_entity_id( k_ent_challenge, i );
642 entity_call( world, &call );
643 }
644 }
645
646 vg_msg routes_block = *sav;
647 if( vg_msg_seekframe( &routes_block, "routes" ) ){
648 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
649 ent_route *route = mdl_arritm( &world->ent_route, i );
650
651 vg_msg route_info = routes_block;
652 if( vg_msg_seekframe( &route_info,
653 mdl_pstr(&world->meta,route->pstr_name) ) ){
654 route->flags |= vg_msg_getkvu32( &route_info, "flags", 0 );
655 route->best_laptime =
656 vg_msg_getkvf64( &route_info, "best_laptime", 0.0 );
657
658 f32 sections[ route->checkpoints_count ];
659 vg_msg_cmd cmd;
660 if( vg_msg_getkvcmd( &route_info, "sections", &cmd ) ){
661 vg_msg_cast( cmd.value, cmd.code, sections,
662 k_vg_msg_f32 |
663 vg_msg_count_bits(route->checkpoints_count) );
664 }
665 else{
666 for( u32 j=0; j<route->checkpoints_count; j ++ )
667 sections[j] = 0.0f;
668 }
669
670 for( u32 j=0; j<route->checkpoints_count; j ++ ){
671 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
672 route->checkpoints_start + j );
673
674 cp->best_time = sections[j];
675 }
676
677 /* LEGACY: check if steam achievements can give us a medal */
678 if( steam_ready && steam_stats_ready ){
679 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
680 struct track_info *inf = &track_infos[j];
681 if( !strcmp(inf->name,
682 mdl_pstr(&world->meta,route->pstr_name))){
683
684 steamapi_bool set = 0;
685 if( SteamAPI_ISteamUserStats_GetAchievement(
686 hSteamUserStats, inf->achievement_id, &set ) )
687 {
688 if( set ){
689 route->flags |= k_ent_route_flag_achieve_silver;
690 }
691 }
692 }
693 }
694 }
695 }
696 }
697 }
698
699 ent_region_re_eval( world );
700 }
701
702 static void world_entity_serialize( world_instance *world, vg_msg *sav ){
703 for( u32 i=0; i<mdl_arrcount(&world->ent_challenge); i++ ){
704 ent_challenge *challenge = mdl_arritm(&world->ent_challenge,i);
705
706 const char *alias = mdl_pstr(&world->meta,challenge->pstr_alias);
707 vg_msg_wkvu32( sav, alias, challenge->status );
708 }
709
710 if( mdl_arrcount(&world->ent_route) ){
711 vg_msg_frame( sav, "routes" );
712 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
713 ent_route *route = mdl_arritm( &world->ent_route, i );
714
715 vg_msg_frame( sav, mdl_pstr( &world->meta, route->pstr_name ) );
716 {
717 vg_msg_wkvu32( sav, "flags", route->flags );
718 vg_msg_wkvf64( sav, "best_laptime", route->best_laptime );
719
720 f32 sections[ route->checkpoints_count ];
721
722 for( u32 j=0; j<route->checkpoints_count; j ++ ){
723 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
724 route->checkpoints_start + j );
725
726 sections[j] = cp->best_time;
727 }
728
729 vg_msg_wkvnum( sav, "sections", k_vg_msg_f32,
730 route->checkpoints_count, sections );
731 }
732 vg_msg_end_frame( sav );
733 }
734 vg_msg_end_frame( sav );
735 }
736 }
737
738 #endif /* WORLD_ENTITY_C */