chaos caused by async
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_H
6 #define ROUTES_H
7
8 #include <time.h>
9 #include "world.h"
10 #include "world_gate.h"
11 #include "font.h"
12
13 #if 0
14 #include "shaders/vblend.h"
15 #endif
16
17 #include "shaders/scene_route.h"
18 #include "shaders/routeui.h"
19
20
21 VG_STATIC
22 void world_routes_local_set_record( world_instance *world, ent_route *route,
23 double lap_time )
24 {
25 vg_success( " NEW LAP TIME: %f\n", lap_time );
26
27 if( route->official_track_id != 0xffffffff ){
28 double time_centiseconds = lap_time * 100.0;
29 if( time_centiseconds > (float)0xfffe ) /* skill issue */
30 return;
31
32 highscore_record temp;
33 temp.trackid = route->official_track_id;
34 temp.datetime = time(NULL);
35 temp.playerid = 0;
36 temp.points = 0;
37 temp.time = time_centiseconds;
38
39 #if 0
40 highscores_push_record( &temp );
41 #endif
42
43 struct track_info *ti = &track_infos[ route->official_track_id ];
44 ti->push = 1;
45
46 if( ti->achievement_id ){
47 #if 0
48 steam_set_achievement( ti->achievement_id );
49 steam_store_achievements();
50 #endif
51 }
52 }
53 else{
54 vg_warn( "There is no associated track for this record...\n" );
55 }
56 }
57
58
59 VG_STATIC void world_routes_clear( world_instance *world )
60 {
61 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
62 ent_route *route = mdl_arritm( &world->ent_route, i );
63 route->active_checkpoint = 0xffff;
64 }
65
66 for( u32 i=0; i<mdl_arrcount( &world->ent_gate ); i++ ){
67 ent_gate *rg = mdl_arritm( &world->ent_gate, i );
68 rg->timing_version = 0;
69 rg->timing_time = 0.0;
70 }
71
72 world_global.current_run_version += 4;
73 world_global.last_use = 0.0;
74 }
75
76 VG_STATIC void world_routes_time_lap( world_instance *world, ent_route *route )
77 {
78 vg_info( "------- time lap %s -------\n",
79 mdl_pstr(&world->meta,route->pstr_name) );
80
81 double start_time = 0.0;
82 u32 last_version=0;
83
84 u32 valid_count=0;
85
86 for( u32 i=0; i<route->checkpoints_count; i++ ){
87 u32 cpid = (i+route->active_checkpoint) % route->checkpoints_count;
88 cpid += route->checkpoints_start;
89
90 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, cpid );
91 ent_gate *rg = mdl_arritm( &world->ent_gate, cp->gate_index );
92 rg = mdl_arritm( &world->ent_gate, rg->target );
93
94 if( i == 1 ){
95 route->timing_base = rg->timing_time;
96 }
97
98 if( i == 0 )
99 start_time = rg->timing_time;
100 else{
101 if( last_version+1 == rg->timing_version ) valid_count ++;
102 else valid_count = 0;
103 }
104
105 last_version = rg->timing_version;
106 vg_info( "%u %f\n", rg->timing_version, rg->timing_time );
107 }
108
109 if( world_global.current_run_version == last_version+1 ){
110 valid_count ++;
111
112 if( route->checkpoints_count == 1 ){
113 route->timing_base = world_global.time;
114 }
115 }
116 else valid_count = 0;
117
118 vg_info( "%u %f\n", world_global.current_run_version, world_global.time );
119
120 if( valid_count==route->checkpoints_count ){
121 double lap_time = world_global.time - start_time;
122 world_routes_local_set_record( world, route, lap_time );
123 }
124
125 route->valid_checkpoints = valid_count+1;
126
127 vg_info( "valid: %u\n", valid_count );
128 vg_info( "----------------------------\n" );
129 }
130
131 /*
132 * When going through a gate this is called for bookkeeping purposes
133 */
134 VG_STATIC void world_routes_activate_entry_gate( world_instance *world,
135 ent_gate *rg )
136 {
137 world_global.last_use = world_global.time;
138
139 /* disable all routes and leave the world */
140 if( rg->type == k_gate_type_nonlocel ){
141 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
142 ent_route *route = mdl_arritm( &world->ent_route, i );
143 route->active_checkpoint = 0xffff;
144 }
145 return;
146 }
147
148 ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
149
150 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
151 ent_route *route = mdl_arritm( &world->ent_route, i );
152
153 u32 active_prev = route->active_checkpoint;
154 route->active_checkpoint = 0xffff;
155
156 for( u32 j=0; j<4; j++ ){
157 if( dest->routes[j] == i ){
158 for( u32 k=0; k<route->checkpoints_count; k++ ){
159 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
160 route->checkpoints_start+k );
161
162 ent_gate *gk = mdl_arritm( &world->ent_gate, cp->gate_index );
163 gk = mdl_arritm( &world->ent_gate, gk->target );
164 if( gk == dest ){
165 route->active_checkpoint = k;
166 world_routes_time_lap( world, route );
167 break;
168 }
169 }
170 break;
171 }
172 }
173 }
174
175 dest->timing_version = world_global.current_run_version;
176 dest->timing_time = world_global.time;
177
178 world_global.current_run_version ++;
179 }
180
181 /* draw lines along the paths */
182 VG_STATIC void world_routes_debug( world_instance *world )
183 {
184 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
185 ent_route_node *rn = mdl_arritm(&world->ent_route_node,i);
186 vg_line_pt3( rn->co, 0.25f, VG__WHITE );
187 }
188
189 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
190 ent_route *route = mdl_arritm(&world->ent_route, i);
191
192 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
193 0xff5442f5 };
194
195 u32 cc = 0xffcccccc;
196 if( route->active_checkpoint != 0xffff ){
197 cc = colours[i%vg_list_size(colours)];
198 }
199
200 for( int i=0; i<route->checkpoints_count; i++ ){
201 int i0 = route->checkpoints_start+i,
202 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
203
204 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
205 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
206
207 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
208 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
209
210 v3f p0, p1;
211 v3_copy( start_gate->co[1], p0 );
212
213 for( int j=0; j<c0->path_count; j ++ ){
214 ent_path_index *index = mdl_arritm( &world->ent_path_index,
215 c0->path_start+j );
216
217 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
218 index->index );
219
220 v3_copy( rn->co, p1 );
221 vg_line( p0, p1, cc );
222 v3_copy( p1, p0 );
223 }
224
225 v3_copy( end_gate->co[0], p1 );
226 vg_line( p0, p1, cc );
227 }
228 }
229 }
230
231 VG_STATIC
232 void world_routes_place_curve( world_instance *world,
233 v4f h[3], v3f n0, v3f n2, scene_context *scene )
234 {
235 float t;
236 v3f p, pd;
237 int last_valid=0;
238
239 float total_length = 0.0f,
240 travel_length = 0.0;
241
242 v3f last;
243 v3_copy( h[0], last );
244 for( int it=0; it<128; it ++ ){
245 t = (float)(it+1) * (1.0f/128.0f);
246 eval_bezier3( h[0], h[1], h[2], t, p );
247 total_length += v3_dist( p, last );
248 v3_copy( p, last );
249 }
250
251 float patch_size = 4.0f,
252 patch_count = ceilf( total_length / patch_size );
253
254 t = 0.0f;
255 v3_copy( h[0], last );
256
257 for( int it=0; it<128; it ++ ){
258 float const k_sample_dist = 0.0025f,
259 k_line_width = 1.5f;
260
261 eval_bezier3( h[0], h[1], h[2], t, p );
262 eval_bezier3( h[0], h[1], h[2], t+k_sample_dist, pd );
263
264 travel_length += v3_dist( p, last );
265
266 float mod = k_sample_dist / v3_dist( p, pd );
267
268 v3f v0,up, right;
269
270 v3_muls( n0, -(1.0f-t), up );
271 v3_muladds( up, n2, -t, up );
272 v3_normalize( up );
273
274 v3_sub( pd,p,v0 );
275 v3_cross( up, v0, right );
276 v3_normalize( right );
277
278 float cur_x = (1.0f-t)*h[0][3] + t*h[2][3];
279
280 v3f sc, sa, sb, down;
281 v3_muladds( p, right, cur_x * k_line_width, sc );
282 v3_muladds( sc, up, 1.5f, sc );
283 v3_muladds( sc, right, k_line_width*0.95f, sa );
284 v3_muladds( sc, right, 0.0f, sb );
285 v3_muls( up, -1.0f, down );
286
287 ray_hit ha, hb;
288 ha.dist = 8.0f;
289 hb.dist = 8.0f;
290
291 int resa = ray_world( world, sa, down, &ha ),
292 resb = ray_world( world, sb, down, &hb );
293
294 if( resa && resb ){
295 struct world_surface *surfa = ray_hit_surface( world, &ha ),
296 *surfb = ray_hit_surface( world, &hb );
297
298 if( (surfa->info.flags & k_material_flag_skate_target) &&
299 (surfb->info.flags & k_material_flag_skate_target) )
300 {
301 scene_vert va, vb;
302
303 float gap = vg_fractf(cur_x*0.5f)*0.02f;
304
305 v3_muladds( ha.pos, up, 0.06f+gap, va.co );
306 v3_muladds( hb.pos, up, 0.06f+gap, vb.co );
307
308 scene_vert_pack_norm( &va, up );
309 scene_vert_pack_norm( &vb, up );
310
311 float t1 = (travel_length / total_length) * patch_count;
312 va.uv[0] = t1;
313 va.uv[1] = 0.0f;
314 vb.uv[0] = t1;
315 vb.uv[1] = 1.0f;
316
317 scene_push_vert( scene, &va );
318 scene_push_vert( scene, &vb );
319
320 if( last_valid ){
321 /* Connect them with triangles */
322 scene_push_tri( scene, (u32[3]){
323 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
324 scene_push_tri( scene, (u32[3]){
325 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
326 }
327
328 last_valid = scene->vertex_count;
329 }
330 else
331 last_valid = 0;
332 }
333 else
334 last_valid = 0;
335
336 if( t == 1.0f )
337 return;
338
339 t += 1.0f*mod;
340 if( t > 1.0f )
341 t = 1.0f;
342
343 v3_copy( p, last );
344 }
345 }
346
347 VG_STATIC
348 void world_routes_create_mesh( world_instance *world, u32 route_id,
349 scene_context *sc )
350 {
351 ent_route *route = mdl_arritm( &world->ent_route, route_id );
352 u32 last_valid = 0;
353
354 for( int i=0; i<route->checkpoints_count; i++ ){
355 int i0 = route->checkpoints_start+i,
356 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
357
358 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
359 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
360
361 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
362 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
363
364 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index ),
365 *collector = mdl_arritm( &world->ent_gate, end_gate->target );
366
367 v4f p[3];
368
369 v3_add( (v3f){0.0f,0.1f,0.0f}, start_gate->co[0], p[0] );
370 p[0][3] = start_gate->ref_count;
371 p[0][3] -= (float)start_gate->route_count * 0.5f;
372 start_gate->ref_count ++;
373
374 if( !c0->path_count )
375 continue;
376
377 /* this is so that we get nice flow through the gates */
378 v3f temp_alignments[2];
379 ent_gate *both[] = { start_gate, end_gate };
380
381 for( int j=0; j<2; j++ ){
382 int pi = c0->path_start + ((j==1)? c0->path_count-1: 0);
383
384 ent_path_index *index = mdl_arritm( &world->ent_path_index, pi );
385 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
386 index->index );
387 v3f v0;
388 v3_sub( rn->co, both[j]->co[0], v0 );
389 float d = v3_dot( v0, both[j]->to_world[2] );
390
391 v3_muladds( both[j]->co[0], both[j]->to_world[2], d,
392 temp_alignments[j] );
393 v3_add( (v3f){0.0f,0.1f,0.0f}, temp_alignments[j], temp_alignments[j]);
394 }
395
396
397 for( int j=0; j<c0->path_count; j ++ ){
398 ent_path_index *index = mdl_arritm( &world->ent_path_index,
399 c0->path_start+j );
400 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
401 index->index );
402 if( j==0 || j==c0->path_count-1 )
403 if( j == 0 )
404 v3_copy( temp_alignments[0], p[1] );
405 else
406 v3_copy( temp_alignments[1], p[1] );
407 else
408 v3_copy( rn->co, p[1] );
409
410 p[1][3] = rn->ref_count;
411 p[1][3] -= (float)rn->ref_total * 0.5f;
412 rn->ref_count ++;
413
414 if( j+1 < c0->path_count ){
415 index = mdl_arritm( &world->ent_path_index,
416 c0->path_start+j+1 );
417 rn = mdl_arritm( &world->ent_route_node, index->index );
418
419 if( j+1 == c0->path_count-1 )
420 v3_lerp( p[1], temp_alignments[1], 0.5f, p[2] );
421 else
422 v3_lerp( p[1], rn->co, 0.5f, p[2] );
423
424 p[2][3] = rn->ref_count;
425 p[2][3] -= (float)rn->ref_total * 0.5f;
426 }
427 else{
428 v3_copy( end_gate->co[0], p[2] );
429 v3_add( (v3f){0.0f,0.1f,0.0f}, p[2], p[2] );
430 p[2][3] = collector->ref_count;
431
432 if( i == route->checkpoints_count-1)
433 p[2][3] -= 1.0f;
434
435 p[2][3] -= (float)collector->route_count * 0.5f;
436 //collector->ref_count ++;
437 }
438
439 /* p0,p1,p2 bezier patch is complete
440 * --------------------------------------*/
441 v3f surf0, surf2, n0, n2;
442
443 if( bh_closest_point( world->geo_bh, p[0], surf0, 5.0f ) == -1 )
444 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[0], surf0 );
445
446 if( bh_closest_point( world->geo_bh, p[2], surf2, 5.0f ) == -1 )
447 v3_add( (v3f){0.0f,-0.1f,0.0f}, p[2], surf2 );
448
449 v3_sub( surf0, p[0], n0 );
450 v3_sub( surf2, p[2], n2 );
451 v3_normalize( n0 );
452 v3_normalize( n2 );
453
454 world_routes_place_curve( world, p, n0, n2, sc );
455
456 /* --- */
457 v4_copy( p[2], p[0] );
458 }
459 }
460
461 scene_copy_slice( sc, &route->sm );
462 }
463
464 /*
465 * Create the strips of colour that run through the world along course paths
466 */
467 VG_STATIC void world_routes_generate( world_instance *world )
468 {
469 vg_info( "Generating route meshes\n" );
470 vg_async_item *call = scene_alloc_async( &world->scene_lines,
471 &world->mesh_route_lines,
472 200000, 300000 );
473
474 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
475 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
476 gate->ref_count = 0;
477 gate->route_count = 0;
478 }
479
480 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
481 ent_route_node *rn = mdl_arritm( &world->ent_route_node, i );
482 rn->ref_count = 0;
483 rn->ref_total = 0;
484 }
485
486 for( u32 k=0; k<mdl_arrcount(&world->ent_route); k++ ){
487 ent_route *route = mdl_arritm( &world->ent_route, k );
488
489 for( int i=0; i<route->checkpoints_count; i++ ){
490 int i0 = route->checkpoints_start+i,
491 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
492
493 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
494 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
495
496 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
497 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
498 start_gate->route_count ++;
499
500 if( !c0->path_count )
501 continue;
502
503 for( int j=0; j<c0->path_count; j ++ ){
504 ent_path_index *index = mdl_arritm( &world->ent_path_index,
505 c0->path_start+j );
506 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
507 index->index );
508 rn->ref_total ++;
509 }
510 }
511 }
512
513 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ )
514 world_routes_create_mesh( world, i, &world->scene_lines );
515
516 vg_async_dispatch( call, async_scene_upload );
517 world_routes_clear( world );
518 }
519
520 /* load all routes from model header */
521 VG_STATIC void world_routes_ent_init( world_instance *world )
522 {
523 vg_info( "Initializing routes\n" );
524
525 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
526 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
527 for( u32 j=0; j<4; j++ ){
528 gate->routes[j] = 0xffff;
529 }
530 }
531
532 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
533 ent_route *route = mdl_arritm(&world->ent_route,i);
534 mdl_transform_m4x3( &route->transform, route->board_transform );
535
536 route->official_track_id = 0xffffffff;
537 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
538 if( !strcmp(track_infos[j].name,
539 mdl_pstr(&world->meta,route->pstr_name))){
540 route->official_track_id = j;
541 }
542 }
543
544 for( u32 j=0; j<route->checkpoints_count; j++ ){
545 u32 id = route->checkpoints_start + j;
546 ent_checkpoint *cp = mdl_arritm(&world->ent_checkpoint,id);
547
548 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
549
550 for( u32 k=0; k<4; k++ ){
551 if( gate->routes[k] == 0xffff ){
552 gate->routes[k] = i;
553 break;
554 }
555 }
556
557 if( gate->type == k_gate_type_teleport ){
558 gate = mdl_arritm(&world->ent_gate, gate->target );
559
560 for( u32 k=0; k<4; k++ ){
561 if( gate->routes[k] == i ){
562 vg_error( "already assigned route to gate\n" );
563 break;
564 }
565 if( gate->routes[k] == 0xffff ){
566 gate->routes[k] = i;
567 break;
568 }
569 }
570 }
571 }
572 }
573
574 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
575 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
576 }
577
578 world_routes_clear( world );
579 }
580
581 /*
582 * -----------------------------------------------------------------------------
583 * Events
584 * -----------------------------------------------------------------------------
585 */
586
587 VG_STATIC void world_routes_init(void)
588 {
589 world_global.current_run_version = 200;
590 world_global.time = 300.0;
591 world_global.last_use = 0.0;
592
593 shader_scene_route_register();
594 shader_routeui_register();
595 }
596
597 VG_STATIC void world_routes_update( world_instance *world )
598 {
599 world_global.time += vg.time_delta;
600
601 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
602 ent_route *route = mdl_arritm( &world->ent_route, i );
603
604 int target = route->active_checkpoint == 0xffff? 0: 1;
605 route->factive = vg_lerpf( route->factive, target, 0.6f*vg.time_delta );
606 }
607
608 for( u32 i=0; i<world_global.text_particle_count; i++ ){
609 struct text_particle *particle = &world_global.text_particles[i];
610 rb_object_debug( &particle->obj, VG__RED );
611 }
612 }
613
614 VG_STATIC void world_routes_fixedupdate( world_instance *world )
615 {
616 rb_solver_reset();
617
618 for( u32 i=0; i<world_global.text_particle_count; i++ ){
619 struct text_particle *particle = &world_global.text_particles[i];
620
621 if( rb_global_has_space() ){
622 rb_ct *buf = rb_global_buffer();
623
624 int l = rb_sphere__scene( particle->obj.rb.to_world,
625 &particle->obj.inf.sphere,
626 NULL, &world->rb_geo.inf.scene, buf );
627
628 for( int j=0; j<l; j++ ){
629 buf[j].rba = &particle->obj.rb;
630 buf[j].rbb = &world->rb_geo.rb;
631 }
632
633 rb_contact_count += l;
634 }
635 }
636
637 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
638
639 for( int i=0; i<rb_contact_count; i++ ){
640 rb_contact_restitution( rb_contact_buffer+i, vg_randf() );
641 }
642
643 for( int i=0; i<6; i++ ){
644 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
645 }
646
647 for( u32 i=0; i<world_global.text_particle_count; i++ ){
648 struct text_particle *particle = &world_global.text_particles[i];
649 rb_iter( &particle->obj.rb );
650 }
651
652 for( u32 i=0; i<world_global.text_particle_count; i++ ){
653 struct text_particle *particle = &world_global.text_particles[i];
654 rb_update_transform( &particle->obj.rb );
655 }
656 }
657
658 VG_STATIC void bind_terrain_noise(void);
659 VG_STATIC void world_bind_light_array( world_instance *world,
660 GLuint shader, GLuint location,
661 int slot );
662 VG_STATIC void world_bind_light_index( world_instance *world,
663 GLuint shader, GLuint location,
664 int slot );
665
666 VG_STATIC void world_routes_update_timer_texts( world_instance *world )
667 {
668 world_global.timer_text_count = 0;
669
670 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
671 ent_route *route = mdl_arritm( &world->ent_route, i );
672
673 if( route->active_checkpoint != 0xffff ){
674 u32 next = route->active_checkpoint+1;
675 next = next % route->checkpoints_count;
676 next += route->checkpoints_start;
677
678 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
679 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
680 ent_gate *dest = mdl_arritm( &world->ent_gate, gate->target );
681
682 u32 j=0;
683 for( ; j<4; j++ ){
684 if( dest->routes[j] == i ){
685 break;
686 }
687 }
688
689 float h0 = 0.8f,
690 h1 = 1.2f,
691 depth = 0.4f,
692 size = 0.4f;
693
694 struct timer_text *text =
695 &world_global.timer_texts[ world_global.timer_text_count ++ ];
696
697 text->gate = gate;
698 text->route = route;
699
700 if( route->valid_checkpoints >= route->checkpoints_count ){
701 double lap_time = world_global.time - route->timing_base,
702 time_centiseconds = lap_time * 100.0;
703
704 if( time_centiseconds > (float)0xfffe ) time_centiseconds = 0.0;
705
706 u16 centiseconds = time_centiseconds,
707 seconds = centiseconds / 100,
708 minutes = seconds / 60;
709
710 centiseconds %= 100;
711 seconds %= 60;
712 minutes %= 60;
713
714 if( minutes > 9 ) minutes = 9;
715
716 int j=0;
717 if( minutes ){
718 highscore_intr( text->text, minutes, 1, ' ' ); j++;
719 text->text[j++] = ':';
720 }
721
722 if( seconds >= 10 || minutes ){
723 highscore_intr( text->text+j, seconds, 2, '0' ); j+=2;
724 }
725 else{
726 highscore_intr( text->text+j, seconds, 1, '0' ); j++;
727 }
728
729 text->text[j++] = '.';
730 highscore_intr( text->text+j, centiseconds, 1, '0' ); j++;
731 text->text[j] = '\0';
732 }
733 else{
734 highscore_intr( text->text, route->valid_checkpoints, 1, ' ' );
735 text->text[1] = '/';
736 highscore_intr( text->text+2, route->checkpoints_count+1, 1, ' ' );
737 text->text[3] = '\0';
738 }
739
740 float align_r = font3d_string_width( &world_global.font, 0,
741 text->text );
742 align_r *= size;
743
744 v3f positions[] = {
745 { -0.92f, h0, depth },
746 { 0.92f - align_r, h0, depth },
747 { -0.92f, h1, depth },
748 { 0.92f - align_r, h1, depth },
749 };
750
751 if( dest->route_count == 1 ){
752 positions[0][0] = -align_r*0.5f;
753 positions[0][1] = h1;
754 }
755
756 m3x3_copy( gate->to_world, text->transform );
757 float ratio = v3_length(text->transform[0]) /
758 v3_length(text->transform[1]);
759
760 m3x3_scale( text->transform, (v3f){ size, size*ratio, 0.1f } );
761 m4x3_mulv( gate->to_world, positions[j], text->transform[3] );
762 }
763 }
764 }
765
766 VG_STATIC void world_routes_fracture( world_instance *world, ent_gate *gate,
767 v3f imp_co, v3f imp_v )
768 {
769 world_global.text_particle_count = 0;
770
771 for( u32 i=0; i<world_global.timer_text_count; i++ ){
772 struct timer_text *text = &world_global.timer_texts[i];
773
774 if( text->gate != gate ) continue;
775
776 m4x3f transform;
777 m4x3_mul( gate->transport, text->transform, transform );
778
779 v3f co, s;
780 v4f q;
781 m4x3_decompose( transform, co, q, s );
782
783 v3f offset;
784 v3_zero( offset );
785
786 v4f colour;
787 float brightness = 0.3f + world->ub_lighting.g_day_phase;
788 v3_muls( text->route->colour, brightness, colour );
789 colour[3] = 1.0f-text->route->factive;
790
791 for( u32 j=0;; j++ ){
792 char c = text->text[j];
793 if( !c ) break;
794
795 ent_glyph *glyph = font3d_glyph( &world_global.font, 0, c );
796 if( !glyph ) continue;
797
798 if( c >= (u32)'0' && c <= (u32)'9' && glyph->indice_count ){
799 struct text_particle *particle =
800 &world_global.text_particles[world_global.text_particle_count++];
801
802 particle->glyph = glyph;
803 v4_copy( colour, particle->colour );
804
805 v3f origin;
806 v2_muls( glyph->size, 0.5f, origin );
807 origin[2] = -0.5f;
808
809 v3f world_co;
810
811 v3_add( offset, origin, world_co );
812 m4x3_mulv( transform, world_co, world_co );
813
814 float r = vg_maxf( s[0]*glyph->size[0], s[1]*glyph->size[1] )*0.5f;
815
816 m3x3_identity( particle->mlocal );
817 m3x3_scale( particle->mlocal, s );
818 origin[2] *= s[2];
819 v3_muls( origin, -1.0f, particle->mlocal[3] );
820
821 v3_copy( world_co, particle->obj.rb.co );
822 v3_muls( imp_v, 1.0f+vg_randf(), particle->obj.rb.v );
823 particle->obj.rb.v[1] += 2.0f;
824
825 v4_copy( q, particle->obj.rb.q );
826 particle->obj.rb.w[0] = vg_randf()*2.0f-1.0f;
827 particle->obj.rb.w[1] = vg_randf()*2.0f-1.0f;
828 particle->obj.rb.w[2] = vg_randf()*2.0f-1.0f;
829
830 particle->obj.type = k_rb_shape_sphere;
831 particle->obj.inf.sphere.radius = r*0.6f;
832
833 rb_init_object( &particle->obj );
834 }
835 offset[0] += glyph->size[0];
836 }
837 }
838 }
839
840 VG_STATIC void render_world_routes( world_instance *world, camera *cam,
841 int layer_depth )
842 {
843 m4x3f identity_matrix;
844 m4x3_identity( identity_matrix );
845
846 shader_scene_route_use();
847 shader_scene_route_uTexGarbage(0);
848 world_link_lighting_ub( world, _shader_scene_route.id );
849 world_bind_position_texture( world, _shader_scene_route.id,
850 _uniform_scene_route_g_world_depth, 2 );
851 world_bind_light_array( world, _shader_scene_route.id,
852 _uniform_scene_route_uLightsArray, 3 );
853 world_bind_light_index( world, _shader_scene_route.id,
854 _uniform_scene_route_uLightsIndex, 4 );
855 bind_terrain_noise();
856
857 shader_scene_route_uPv( cam->mtx.pv );
858 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
859 shader_scene_route_uMdl( identity_matrix );
860 shader_scene_route_uCamera( cam->transform[3] );
861
862 mesh_bind( &world->mesh_route_lines );
863
864 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
865 ent_route *route = mdl_arritm( &world->ent_route, i );
866
867 v4f colour;
868 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
869 colour[3] = route->factive*0.2f;
870
871 shader_scene_route_uColour( colour );
872 mdl_draw_submesh( &route->sm );
873 }
874
875 /* timers
876 * ---------------------------------------------------- */
877 if( layer_depth == 0 ){
878 font3d_bind( &world_global.font, cam );
879
880 for( u32 i=0; i<world_global.timer_text_count; i++ ){
881 struct timer_text *text = &world_global.timer_texts[i];
882
883 v4f colour;
884 float brightness = 0.3f + world->ub_lighting.g_day_phase;
885 v3_muls( text->route->colour, brightness, colour );
886 colour[3] = 1.0f-text->route->factive;
887
888 shader_model_font_uColour( colour );
889 font3d_simple_draw( &world_global.font, 0, text->text,
890 cam, text->transform );
891 }
892
893 shader_model_font_uOffset( (v3f){0.0f,0.0f,0.0f} );
894
895 for( u32 i=0; i<world_global.text_particle_count; i++ ){
896 struct text_particle *particle = &world_global.text_particles[i];
897
898 m4x4f prev_mtx;
899
900 m4x3_expand( particle->mdl, prev_mtx );
901 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
902
903 shader_model_font_uPvmPrev( prev_mtx );
904
905 v4f q;
906 m4x3f model;
907 rb_extrapolate( &particle->obj.rb, model[3], q );
908 q_m3x3( q, model );
909
910 m4x3_mul( model, particle->mlocal, particle->mdl );
911 shader_model_font_uMdl( particle->mdl );
912 shader_model_font_uColour( particle->colour );
913
914 mesh_drawn( particle->glyph->indice_start,
915 particle->glyph->indice_count );
916 }
917 }
918
919 /* gate markers
920 * ---------------------------------------------------- */
921
922 shader_model_gate_use();
923 shader_model_gate_uPv( cam->mtx.pv );
924 shader_model_gate_uCam( cam->pos );
925 shader_model_gate_uTime( vg.time*0.25f );
926 shader_model_gate_uInvRes( (v2f){
927 1.0f / (float)vg.window_x,
928 1.0f / (float)vg.window_y });
929
930 mesh_bind( &world_global.mesh_gate );
931
932 /* skip writing into the motion vectors for this */
933 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
934
935 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
936 ent_route *route = mdl_arritm( &world->ent_route, i );
937
938 if( route->active_checkpoint != 0xffff ){
939 v4f colour;
940 float brightness = 0.3f + world->ub_lighting.g_day_phase;
941 v3_muls( route->colour, brightness, colour );
942 colour[3] = 1.0f-route->factive;
943
944 shader_model_gate_uColour( colour );
945
946 u32 next = route->active_checkpoint+1+layer_depth;
947 next = next % route->checkpoints_count;
948 next += route->checkpoints_start;
949
950 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
951 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
952 shader_model_gate_uMdl( gate->to_world );
953
954 for( u32 j=0; j<4; j++ ){
955 if( gate->routes[j] == i ){
956 mdl_draw_submesh( &world_global.sm_gate_marker[j] );
957 break;
958 }
959 }
960 }
961 }
962 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
963 }
964
965 #endif /* ROUTES_H */