add medals to miniworld display
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.c
1 /*
2 * Copyright (C) 2021-2023 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_C
6 #define ROUTES_C
7
8 #include <time.h>
9 #include "entity.h"
10 #include "world_routes.h"
11 #include "world_gate.h"
12 #include "world_load.h"
13 #include "highscores.h"
14 #include "network.h"
15
16 #include "font.h"
17 #include "gui.h"
18 #include "steam.h"
19 #include "network_msg.h"
20 #include "network_common.h"
21
22 #include "shaders/scene_route.h"
23 #include "shaders/routeui.h"
24
25 static void world_routes_clear( world_instance *world )
26 {
27 for( u32 i=0; i<mdl_arrcount( &world->ent_route ); i++ ){
28 ent_route *route = mdl_arritm( &world->ent_route, i );
29 route->active_checkpoint = 0xffff;
30 }
31
32 for( u32 i=0; i<mdl_arrcount( &world->ent_gate ); i++ ){
33 ent_gate *rg = mdl_arritm( &world->ent_gate, i );
34 rg->timing_version = 0;
35 rg->timing_time = 0.0;
36 }
37
38 world_static.current_run_version += 4;
39 world_static.last_use = 0.0;
40 }
41
42 static void world_routes_time_lap( world_instance *world, ent_route *route )
43 {
44 vg_info( "------- time lap %s -------\n",
45 mdl_pstr(&world->meta,route->pstr_name) );
46
47 double start_time = 0.0;
48 u32 last_version=0;
49
50 u32 valid_count=0;
51 int clean = 1;
52
53 for( u32 i=0; i<route->checkpoints_count; i++ ){
54 u32 cpid = (i+route->active_checkpoint) % route->checkpoints_count;
55 cpid += route->checkpoints_start;
56
57 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, cpid );
58 ent_gate *rg = mdl_arritm( &world->ent_gate, cp->gate_index );
59 rg = mdl_arritm( &world->ent_gate, rg->target );
60
61 if( i == 1 ){
62 route->timing_base = rg->timing_time;
63 }
64
65 if( i == 0 )
66 start_time = rg->timing_time;
67 else{
68 if( last_version+1 == rg->timing_version ) valid_count ++;
69 else valid_count = 0;
70 }
71
72 last_version = rg->timing_version;
73
74 vg_info( "%u %f [%s]\n", rg->timing_version, rg->timing_time,
75 (rg->flags & k_ent_gate_clean_pass)? "CLEAN": "-----");
76
77 if( !(rg->flags & k_ent_gate_clean_pass) )
78 clean = 0;
79 }
80
81 if( world_static.current_run_version == last_version+1 ){
82 valid_count ++;
83
84 if( route->checkpoints_count == 1 ){
85 route->timing_base = world_static.time;
86 }
87 }
88 else valid_count = 0;
89
90 vg_info( "%u %f\n", world_static.current_run_version, world_static.time );
91
92 if( valid_count==route->checkpoints_count ){
93 f64 lap_time = world_static.time - start_time;
94
95 if( (route->best_laptime == 0.0) || (lap_time < route->best_laptime) ){
96 route->best_laptime = lap_time;
97 route->achievment_status |= 0x1;
98
99 if( clean )
100 route->achievment_status |= 0x2;
101 }
102
103 /* for steam achievements. */
104 if( route->anon.official_track_id != 0xffffffff ){
105 struct track_info *ti = &track_infos[ route->anon.official_track_id ];
106 if( ti->achievement_id ){
107 steam_set_achievement( ti->achievement_id );
108 steam_store_achievements();
109 }
110 }
111
112 addon_alias *alias =
113 &world_static.instance_addons[ world_static.active_instance ]->alias;
114
115 char mod_uid[ ADDON_UID_MAX ];
116 addon_alias_uid( alias, mod_uid );
117 network_publish_laptime( mod_uid,
118 mdl_pstr( &world->meta, route->pstr_name ),
119 lap_time );
120 }
121
122 route->valid_checkpoints = valid_count+1;
123
124 vg_info( "valid: %u\n", valid_count );
125 vg_info( "----------------------------\n" );
126 }
127
128 /*
129 * When going through a gate this is called for bookkeeping purposes
130 */
131 static void world_routes_activate_entry_gate( world_instance *world,
132 ent_gate *rg )
133 {
134 world_static.last_use = world_static.time;
135 ent_gate *dest = mdl_arritm( &world->ent_gate, rg->target );
136
137 if( localplayer.rewinded_since_last_gate ){
138 localplayer.rewinded_since_last_gate = 0;
139 dest->flags &= ~k_ent_gate_clean_pass;
140 }
141 else
142 dest->flags |= k_ent_gate_clean_pass;
143
144 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
145 ent_route *route = mdl_arritm( &world->ent_route, i );
146
147 u32 active_prev = route->active_checkpoint;
148 route->active_checkpoint = 0xffff;
149
150 for( u32 j=0; j<4; j++ ){
151 if( dest->routes[j] == i ){
152 for( u32 k=0; k<route->checkpoints_count; k++ ){
153 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint,
154 route->checkpoints_start+k );
155
156 ent_gate *gk = mdl_arritm( &world->ent_gate, cp->gate_index );
157 gk = mdl_arritm( &world->ent_gate, gk->target );
158 if( gk == dest ){
159 route->active_checkpoint = k;
160 world_routes_time_lap( world, route );
161 break;
162 }
163 }
164 break;
165 }
166 }
167 }
168
169 dest->timing_version = world_static.current_run_version;
170 dest->timing_time = world_static.time;
171
172 world_static.current_run_version ++;
173 }
174
175 /* draw lines along the paths */
176 static void world_routes_debug( world_instance *world )
177 {
178 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
179 ent_route_node *rn = mdl_arritm(&world->ent_route_node,i);
180 vg_line_point( rn->co, 0.25f, VG__WHITE );
181 }
182
183 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
184 ent_route *route = mdl_arritm(&world->ent_route, i);
185
186 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
187 0xff5442f5 };
188
189 u32 cc = 0xffcccccc;
190 if( route->active_checkpoint != 0xffff ){
191 cc = colours[i%vg_list_size(colours)];
192 }
193
194 for( int i=0; i<route->checkpoints_count; i++ ){
195 int i0 = route->checkpoints_start+i,
196 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
197
198 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
199 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
200
201 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
202 ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index );
203
204 v3f p0, p1;
205 v3_copy( start_gate->co[1], p0 );
206
207 for( int j=0; j<c0->path_count; j ++ ){
208 ent_path_index *index = mdl_arritm( &world->ent_path_index,
209 c0->path_start+j );
210
211 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
212 index->index );
213
214 v3_copy( rn->co, p1 );
215 vg_line( p0, p1, cc );
216 v3_copy( p1, p0 );
217 }
218
219 v3_copy( end_gate->co[0], p1 );
220 vg_line( p0, p1, cc );
221 }
222 }
223 }
224
225
226 static
227 void world_routes_place_curve( world_instance *world, ent_route *route,
228 v4f h[3], v3f n0, v3f n2, scene_context *scene )
229 {
230 float t;
231 v3f p, pd;
232 int last_valid=0;
233
234 float total_length = 0.0f,
235 travel_length = 0.0;
236
237 v3f last;
238 v3_copy( h[0], last );
239 for( int it=0; it<128; it ++ ){
240 t = (float)(it+1) * (1.0f/128.0f);
241 eval_bezier3( h[0], h[1], h[2], t, p );
242 total_length += v3_dist( p, last );
243 v3_copy( p, last );
244 }
245
246 float patch_size = 4.0f,
247 patch_count = ceilf( total_length / patch_size );
248
249 t = 0.0f;
250 v3_copy( h[0], last );
251
252 for( int it=0; it<128; it ++ ){
253 float const k_sample_dist = 0.0025f,
254 k_line_width = 1.5f;
255
256 eval_bezier3( h[0], h[1], h[2], t, p );
257 eval_bezier3( h[0], h[1], h[2], t+k_sample_dist, pd );
258
259 travel_length += v3_dist( p, last );
260
261 float mod = k_sample_dist / v3_dist( p, pd );
262
263 v3f v0,up, right;
264
265 v3_muls( n0, -(1.0f-t), up );
266 v3_muladds( up, n2, -t, up );
267 v3_normalize( up );
268
269 v3_sub( pd,p,v0 );
270 v3_cross( up, v0, right );
271 v3_normalize( right );
272
273 float cur_x = (1.0f-t)*h[0][3] + t*h[2][3];
274
275 v3f sc, sa, sb, down;
276 v3_muladds( p, right, cur_x * k_line_width, sc );
277 v3_muladds( sc, up, 1.5f, sc );
278 v3_muladds( sc, right, k_line_width*0.95f, sa );
279 v3_muladds( sc, right, 0.0f, sb );
280 v3_muls( up, -1.0f, down );
281
282 ray_hit ha, hb;
283 ha.dist = 8.0f;
284 hb.dist = 8.0f;
285
286 int resa = ray_world( world, sa, down, &ha, k_material_flag_ghosts ),
287 resb = ray_world( world, sb, down, &hb, k_material_flag_ghosts );
288
289 if( resa && resb ){
290 struct world_surface *surfa = ray_hit_surface( world, &ha ),
291 *surfb = ray_hit_surface( world, &hb );
292
293 if( (surfa->info.flags & k_material_flag_skate_target) &&
294 (surfb->info.flags & k_material_flag_skate_target) )
295 {
296 scene_vert va, vb;
297
298 float gap = vg_fractf(cur_x*0.5f)*0.02f;
299
300 v3_muladds( ha.pos, up, 0.06f+gap, va.co );
301 v3_muladds( hb.pos, up, 0.06f+gap, vb.co );
302
303 scene_vert_pack_norm( &va, up );
304 scene_vert_pack_norm( &vb, up );
305
306 float t1 = (travel_length / total_length) * patch_count;
307 va.uv[0] = t1;
308 va.uv[1] = 0.0f;
309 vb.uv[0] = t1;
310 vb.uv[1] = 1.0f;
311
312 scene_push_vert( scene, &va );
313 scene_push_vert( scene, &vb );
314
315 if( last_valid ){
316 /* Connect them with triangles */
317 scene_push_tri( scene, (u32[3]){
318 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
319 scene_push_tri( scene, (u32[3]){
320 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
321 }
322
323 last_valid = scene->vertex_count;
324 }
325 else
326 last_valid = 0;
327 }
328 else
329 last_valid = 0;
330
331 if( t == 1.0f )
332 return;
333
334 t += 1.0f*mod;
335 if( t > 1.0f )
336 t = 1.0f;
337
338 v3_copy( p, last );
339 }
340 }
341
342 static void world_routes_gen_meshes( world_instance *world, u32 route_id,
343 scene_context *sc )
344 {
345 ent_route *route = mdl_arritm( &world->ent_route, route_id );
346 u8 colour[4];
347 colour[0] = route->colour[0] * 255.0f;
348 colour[1] = route->colour[1] * 255.0f;
349 colour[2] = route->colour[2] * 255.0f;
350 colour[3] = route->colour[3] * 255.0f;
351
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, route, 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 static
465 struct world_surface *world_tri_index_surface( world_instance *world,
466 u32 index );
467
468 /*
469 * Create the strips of colour that run through the world along course paths
470 */
471 static void world_gen_routes_generate( u32 instance_id ){
472 world_instance *world = &world_static.instances[ instance_id ];
473 vg_info( "Generating route meshes\n" );
474 vg_async_stall();
475
476 vg_rand_seed( 2000 );
477 vg_async_item *call_scene = scene_alloc_async( &world->scene_lines,
478 &world->mesh_route_lines,
479 200000, 300000 );
480
481 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
482 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
483 gate->ref_count = 0;
484 gate->route_count = 0;
485 }
486
487 for( u32 i=0; i<mdl_arrcount(&world->ent_route_node); i++ ){
488 ent_route_node *rn = mdl_arritm( &world->ent_route_node, i );
489 rn->ref_count = 0;
490 rn->ref_total = 0;
491 }
492
493 for( u32 k=0; k<mdl_arrcount(&world->ent_route); k++ ){
494 ent_route *route = mdl_arritm( &world->ent_route, k );
495
496 for( int i=0; i<route->checkpoints_count; i++ ){
497 int i0 = route->checkpoints_start+i,
498 i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
499
500 ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
501 *c1 = mdl_arritm(&world->ent_checkpoint, i1);
502
503 ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
504 start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
505 start_gate->route_count ++;
506
507 if( !c0->path_count )
508 continue;
509
510 for( int j=0; j<c0->path_count; j ++ ){
511 ent_path_index *index = mdl_arritm( &world->ent_path_index,
512 c0->path_start+j );
513 ent_route_node *rn = mdl_arritm( &world->ent_route_node,
514 index->index );
515 rn->ref_total ++;
516 }
517 }
518 }
519
520 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
521 world_routes_gen_meshes( world, i, &world->scene_lines );
522 }
523
524 vg_async_dispatch( call_scene, async_scene_upload );
525 world_routes_clear( world );
526 }
527
528 /* load all routes from model header */
529 static void world_gen_routes_ent_init( world_instance *world ){
530 vg_info( "Initializing routes\n" );
531
532 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
533 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
534 for( u32 j=0; j<4; j++ ){
535 gate->routes[j] = 0xffff;
536 }
537 }
538
539 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
540 ent_route *route = mdl_arritm(&world->ent_route,i);
541 mdl_transform_m4x3( &route->anon.transform, route->board_transform );
542
543 route->anon.official_track_id = 0xffffffff;
544 for( u32 j=0; j<vg_list_size(track_infos); j ++ ){
545 if( !strcmp(track_infos[j].name,
546 mdl_pstr(&world->meta,route->pstr_name))){
547 route->anon.official_track_id = j;
548 }
549 }
550
551 for( u32 j=0; j<route->checkpoints_count; j++ ){
552 u32 id = route->checkpoints_start + j;
553 ent_checkpoint *cp = mdl_arritm(&world->ent_checkpoint,id);
554
555 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
556
557 for( u32 k=0; k<4; k++ ){
558 if( gate->routes[k] == 0xffff ){
559 gate->routes[k] = i;
560 break;
561 }
562 }
563
564 if( (gate->flags & k_ent_gate_linked) &
565 !(gate->flags & k_ent_gate_nonlocal) ){
566 gate = mdl_arritm(&world->ent_gate, gate->target );
567
568 for( u32 k=0; k<4; k++ ){
569 if( gate->routes[k] == i ){
570 vg_error( "already assigned route to gate\n" );
571 break;
572 }
573 if( gate->routes[k] == 0xffff ){
574 gate->routes[k] = i;
575 break;
576 }
577 }
578 }
579 }
580 }
581
582 for( u32 i=0; i<mdl_arrcount(&world->ent_gate); i++ ){
583 ent_gate *gate = mdl_arritm( &world->ent_gate, i );
584 }
585
586 world_routes_clear( world );
587 }
588
589 static void world_routes_recv_scoreboard( world_instance *world,
590 vg_msg *body, u32 route_id,
591 enum request_status status ){
592 if( route_id >= mdl_arrcount( &world->ent_route ) ){
593 vg_error( "Scoreboard route_id out of range (%u)\n", route_id );
594 return;
595 }
596
597 struct leaderboard_cache *board = &world->leaderboard_cache[ route_id ];
598 board->status = status;
599
600 if( body == NULL ){
601 board->data_len = 0;
602 return;
603 }
604
605 if( body->max > NETWORK_REQUEST_MAX ){
606 vg_error( "Scoreboard leaderboard too big (%u>%u)\n", body->max,
607 NETWORK_REQUEST_MAX );
608 return;
609 }
610
611 memcpy( board->data, body->buf, body->max );
612 board->data_len = body->max;
613 }
614
615 /*
616 * -----------------------------------------------------------------------------
617 * Events
618 * -----------------------------------------------------------------------------
619 */
620
621 static void world_routes_init(void){
622 world_static.current_run_version = 200;
623 world_static.time = 300.0;
624 world_static.last_use = 0.0;
625
626 shader_scene_route_register();
627 shader_routeui_register();
628 }
629
630 static void world_routes_update( world_instance *world ){
631 world_static.time += vg.time_delta;
632
633 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
634 ent_route *route = mdl_arritm( &world->ent_route, i );
635
636 int target = route->active_checkpoint == 0xffff? 0: 1;
637 route->factive = vg_lerpf( route->factive, target,
638 0.6f*vg.time_frame_delta );
639 }
640
641 for( u32 i=0; i<world_render.text_particle_count; i++ ){
642 struct text_particle *particle = &world_render.text_particles[i];
643 rb_object_debug( &particle->obj, VG__RED );
644 }
645 }
646
647 static void world_routes_fixedupdate( world_instance *world ){
648 rb_solver_reset();
649
650 for( u32 i=0; i<world_render.text_particle_count; i++ ){
651 struct text_particle *particle = &world_render.text_particles[i];
652
653 if( rb_global_has_space() ){
654 rb_ct *buf = rb_global_buffer();
655
656 int l = rb_sphere__scene( particle->obj.rb.to_world,
657 &particle->obj.inf.sphere,
658 NULL, &world->rb_geo.inf.scene, buf,
659 k_material_flag_ghosts );
660
661 for( int j=0; j<l; j++ ){
662 buf[j].rba = &particle->obj.rb;
663 buf[j].rbb = &world->rb_geo.rb;
664 }
665
666 rb_contact_count += l;
667 }
668 }
669
670 rb_presolve_contacts( rb_contact_buffer, rb_contact_count );
671
672 for( int i=0; i<rb_contact_count; i++ ){
673 rb_contact_restitution( rb_contact_buffer+i, vg_randf64() );
674 }
675
676 for( int i=0; i<6; i++ ){
677 rb_solve_contacts( rb_contact_buffer, rb_contact_count );
678 }
679
680 for( u32 i=0; i<world_render.text_particle_count; i++ ){
681 struct text_particle *particle = &world_render.text_particles[i];
682 rb_iter( &particle->obj.rb );
683 }
684
685 for( u32 i=0; i<world_render.text_particle_count; i++ ){
686 struct text_particle *particle = &world_render.text_particles[i];
687 rb_update_transform( &particle->obj.rb );
688 }
689 }
690
691 static void bind_terrain_noise(void);
692 static void world_bind_light_array( world_instance *world,
693 GLuint shader, GLuint location,
694 int slot );
695 static void world_bind_light_index( world_instance *world,
696 GLuint shader, GLuint location,
697 int slot );
698
699 static void world_routes_update_timer_texts( world_instance *world ){
700 world_render.timer_text_count = 0;
701
702 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
703 ent_route *route = mdl_arritm( &world->ent_route, i );
704
705 if( route->active_checkpoint != 0xffff ){
706 u32 next = route->active_checkpoint+1;
707 next = next % route->checkpoints_count;
708 next += route->checkpoints_start;
709
710 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
711 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
712 ent_gate *dest = mdl_arritm( &world->ent_gate, gate->target );
713
714 u32 j=0;
715 for( ; j<4; j++ ){
716 if( dest->routes[j] == i ){
717 break;
718 }
719 }
720
721 float h0 = 0.8f,
722 h1 = 1.2f,
723 depth = 0.4f,
724 size = 0.4f;
725
726 struct timer_text *text =
727 &world_render.timer_texts[ world_render.timer_text_count ++ ];
728
729 text->gate = gate;
730 text->route = route;
731
732 if( route->valid_checkpoints >= route->checkpoints_count ){
733 double lap_time = world_static.time - route->timing_base,
734 time_centiseconds = lap_time * 100.0;
735
736 if( time_centiseconds > (float)0xfffe ) time_centiseconds = 0.0;
737
738 u16 centiseconds = time_centiseconds,
739 seconds = centiseconds / 100,
740 minutes = seconds / 60;
741
742 centiseconds %= 100;
743 seconds %= 60;
744 minutes %= 60;
745
746 if( minutes > 9 ) minutes = 9;
747
748 int j=0;
749 if( minutes ){
750 highscore_intr( text->text, minutes, 1, ' ' ); j++;
751 text->text[j++] = ':';
752 }
753
754 if( seconds >= 10 || minutes ){
755 highscore_intr( text->text+j, seconds, 2, '0' ); j+=2;
756 }
757 else{
758 highscore_intr( text->text+j, seconds, 1, '0' ); j++;
759 }
760
761 text->text[j++] = '.';
762 highscore_intr( text->text+j, centiseconds, 1, '0' ); j++;
763 text->text[j] = '\0';
764 }
765 else{
766 highscore_intr( text->text, route->valid_checkpoints, 1, ' ' );
767 text->text[1] = '/';
768 highscore_intr( text->text+2, route->checkpoints_count+1, 1, ' ' );
769 text->text[3] = '\0';
770 }
771
772 gui_font3d.font = &gui.font;
773 float align_r = font3d_string_width( 0, text->text );
774 align_r *= size;
775
776 v3f positions[] = {
777 { -0.92f, h0, depth },
778 { 0.92f - align_r, h0, depth },
779 { -0.92f, h1, depth },
780 { 0.92f - align_r, h1, depth },
781 };
782
783 if( dest->route_count == 1 ){
784 positions[0][0] = -align_r*0.5f;
785 positions[0][1] = h1;
786 }
787
788 m4x3f mmdl;
789 ent_gate_get_mdl_mtx( gate, mmdl );
790
791 m3x3_copy( mmdl, text->transform );
792 float ratio = v3_length(text->transform[0]) /
793 v3_length(text->transform[1]);
794
795 m3x3_scale( text->transform, (v3f){ size, size*ratio, 0.1f } );
796 m4x3_mulv( mmdl, positions[j], text->transform[3] );
797 }
798 }
799 }
800
801 static void world_routes_fracture( world_instance *world, ent_gate *gate,
802 v3f imp_co, v3f imp_v )
803 {
804 world_render.text_particle_count = 0;
805
806 for( u32 i=0; i<world_render.timer_text_count; i++ ){
807 struct timer_text *text = &world_render.timer_texts[i];
808
809 if( text->gate != gate ) continue;
810
811 m4x3f transform;
812 m4x3_mul( gate->transport, text->transform, transform );
813
814 v3f co, s;
815 v4f q;
816 m4x3_decompose( transform, co, q, s );
817
818 v3f offset;
819 v3_zero( offset );
820
821 v4f colour;
822 float brightness = 0.3f + world->ub_lighting.g_day_phase;
823 v3_muls( text->route->colour, brightness, colour );
824 colour[3] = 1.0f-text->route->factive;
825
826 for( u32 j=0;; j++ ){
827 char c = text->text[j];
828 if( !c ) break;
829
830 ent_glyph *glyph = font3d_glyph( &gui.font, 0, c );
831 if( !glyph ) continue;
832
833 if( c >= (u32)'0' && c <= (u32)'9' && glyph->indice_count ){
834 struct text_particle *particle =
835 &world_render.text_particles[world_render.text_particle_count++];
836
837 particle->glyph = glyph;
838 v4_copy( colour, particle->colour );
839
840 v3f origin;
841 v2_muls( glyph->size, 0.5f, origin );
842 origin[2] = -0.5f;
843
844 v3f world_co;
845
846 v3_add( offset, origin, world_co );
847 m4x3_mulv( transform, world_co, world_co );
848
849 float r = vg_maxf( s[0]*glyph->size[0], s[1]*glyph->size[1] )*0.5f;
850
851 m3x3_identity( particle->mlocal );
852 m3x3_scale( particle->mlocal, s );
853 origin[2] *= s[2];
854 v3_muls( origin, -1.0f, particle->mlocal[3] );
855
856 v3_copy( world_co, particle->obj.rb.co );
857 v3_muls( imp_v, 1.0f+vg_randf64(), particle->obj.rb.v );
858 particle->obj.rb.v[1] += 2.0f;
859
860 v4_copy( q, particle->obj.rb.q );
861 particle->obj.rb.w[0] = vg_randf64()*2.0f-1.0f;
862 particle->obj.rb.w[1] = vg_randf64()*2.0f-1.0f;
863 particle->obj.rb.w[2] = vg_randf64()*2.0f-1.0f;
864
865 particle->obj.type = k_rb_shape_sphere;
866 particle->obj.inf.sphere.radius = r*0.6f;
867
868 rb_init_object( &particle->obj );
869 }
870 offset[0] += glyph->size[0];
871 }
872 }
873 }
874
875 static void render_gate_markers( m4x3f world_mmdl, int run_id, ent_gate *gate ){
876 for( u32 j=0; j<4; j++ ){
877 if( gate->routes[j] == run_id ){
878 m4x3f mmdl;
879 ent_gate_get_mdl_mtx( gate, mmdl );
880 m4x3_mul( world_mmdl, mmdl, mmdl );
881 shader_model_gate_uMdl( mmdl );
882 mdl_draw_submesh( &world_gates.sm_marker[j] );
883 break;
884 }
885 }
886 }
887
888 static void render_world_routes( world_instance *world,
889 world_instance *host_world,
890 m4x3f mmdl, camera *cam,
891 int viewing_from_gate, int viewing_from_hub ){
892
893 shader_scene_route_use();
894 shader_scene_route_uTexGarbage(0);
895 world_link_lighting_ub( host_world, _shader_scene_route.id );
896 world_bind_position_texture( host_world, _shader_scene_route.id,
897 _uniform_scene_route_g_world_depth, 2 );
898 world_bind_light_array( host_world, _shader_scene_route.id,
899 _uniform_scene_route_uLightsArray, 3 );
900 world_bind_light_index( host_world, _shader_scene_route.id,
901 _uniform_scene_route_uLightsIndex, 4 );
902 bind_terrain_noise();
903
904 shader_scene_route_uPv( cam->mtx.pv );
905
906 if( viewing_from_hub ){
907 m4x4f m4mdl, pvm;
908 m4x3_expand( mmdl, m4mdl );
909 m4x4_mul( cam->mtx_prev.pv, m4mdl, pvm );
910 shader_scene_route_uMdl( mmdl );
911 shader_scene_route_uPvmPrev( pvm );
912
913 m3x3f mnormal;
914 m3x3_inv( mmdl, mnormal );
915 m3x3_transpose( mnormal, mnormal );
916 v3_normalize( mnormal[0] );
917 v3_normalize( mnormal[1] );
918 v3_normalize( mnormal[2] );
919 shader_scene_route_uNormalMtx( mnormal );
920 }
921 else{
922 shader_scene_route_uMdl( mmdl );
923 shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
924 m3x3f ident;
925 m3x3_identity( ident );
926 shader_scene_route_uNormalMtx( ident );
927 }
928
929 shader_scene_route_uCamera( cam->transform[3] );
930
931 mesh_bind( &world->mesh_route_lines );
932
933 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
934 ent_route *route = mdl_arritm( &world->ent_route, i );
935
936 f32 t = viewing_from_hub? 1.0f: route->factive;
937
938 v4f colour;
939 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, t, colour );
940 colour[3] = t*0.2f;
941
942 shader_scene_route_uColour( colour );
943 mdl_draw_submesh( &route->sm );
944 }
945
946 /* timers
947 * ---------------------------------------------------- */
948 if( !viewing_from_gate && !viewing_from_hub ){
949 font3d_bind( &gui.font, k_font_shader_default, 0, world, cam );
950
951 for( u32 i=0; i<world_render.timer_text_count; i++ ){
952 struct timer_text *text = &world_render.timer_texts[i];
953
954 v4f colour;
955 float brightness = 0.3f + world->ub_lighting.g_day_phase;
956 v3_muls( text->route->colour, brightness, colour );
957 colour[3] = 1.0f-text->route->factive;
958
959 shader_model_font_uColour( colour );
960 font3d_simple_draw( 0, text->text, cam, text->transform );
961 }
962
963 shader_model_font_uOffset( (v4f){0.0f,0.0f,0.0f,1.0f} );
964
965 for( u32 i=0; i<world_render.text_particle_count; i++ ){
966 struct text_particle *particle = &world_render.text_particles[i];
967
968 m4x4f prev_mtx;
969
970 m4x3_expand( particle->mdl, prev_mtx );
971 m4x4_mul( cam->mtx_prev.pv, prev_mtx, prev_mtx );
972
973 shader_model_font_uPvmPrev( prev_mtx );
974
975 v4f q;
976 m4x3f model;
977 rb_extrapolate( &particle->obj.rb, model[3], q );
978 q_m3x3( q, model );
979
980 m4x3_mul( model, particle->mlocal, particle->mdl );
981 shader_model_font_uMdl( particle->mdl );
982 shader_model_font_uColour( particle->colour );
983
984 mesh_drawn( particle->glyph->indice_start,
985 particle->glyph->indice_count );
986 }
987 }
988
989 /* gate markers
990 * ---------------------------------------------------- */
991
992 shader_model_gate_use();
993 shader_model_gate_uPv( cam->mtx.pv );
994 shader_model_gate_uCam( cam->pos );
995 shader_model_gate_uTime( vg.time*0.25f );
996 shader_model_gate_uInvRes( (v2f){
997 1.0f / (float)vg.window_x,
998 1.0f / (float)vg.window_y });
999
1000 mesh_bind( &world_gates.mesh );
1001
1002 /* skip writing into the motion vectors for this */
1003 glDrawBuffers( 1, (GLenum[]){ GL_COLOR_ATTACHMENT0 } );
1004 glDisable( GL_CULL_FACE );
1005
1006 if( viewing_from_hub ){
1007 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1008 ent_route *route = mdl_arritm( &world->ent_route, i );
1009
1010 v4f colour;
1011 v3_muls( route->colour, 1.6666f, colour );
1012 colour[3] = 0.0f;
1013
1014 shader_model_gate_uColour( colour );
1015
1016 for( u32 j=0; j<mdl_arrcount(&world->ent_gate); j ++ ){
1017 ent_gate *gate = mdl_arritm( &world->ent_gate, j );
1018 if( !(gate->flags & k_ent_gate_nonlocal) )
1019 render_gate_markers( mmdl, i, gate );
1020 }
1021 }
1022 }
1023 else{
1024 for( u32 i=0; i<mdl_arrcount(&world->ent_route); i++ ){
1025 ent_route *route = mdl_arritm( &world->ent_route, i );
1026
1027 if( route->active_checkpoint != 0xffff ){
1028 v4f colour;
1029 float brightness = 0.3f + world->ub_lighting.g_day_phase;
1030 v3_muls( route->colour, brightness, colour );
1031 colour[3] = 1.0f-route->factive;
1032
1033 shader_model_gate_uColour( colour );
1034
1035 u32 next = route->active_checkpoint+1+viewing_from_gate;
1036 next = next % route->checkpoints_count;
1037 next += route->checkpoints_start;
1038
1039 ent_checkpoint *cp = mdl_arritm( &world->ent_checkpoint, next );
1040 ent_gate *gate = mdl_arritm( &world->ent_gate, cp->gate_index );
1041 render_gate_markers( mmdl, i, gate );
1042 }
1043 }
1044 }
1045 glEnable( GL_CULL_FACE );
1046 glDrawBuffers( 2, (GLenum[]){ GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1 } );
1047 }
1048
1049 #endif /* ROUTES_C */