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