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