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