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