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