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