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