the coolest fucking thing ive ever made
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 /*
2 * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
3 */
4
5 #ifndef ROUTES_H
6 #define ROUTES_H
7
8 #include "world.h"
9 #include "world_info.h"
10 #include "world_gate.h"
11
12 #include "shaders/vblend.h"
13 #include "shaders/route.h"
14 #include "shaders/routeui.h"
15
16 enum route_special_type
17 {
18 k_route_special_type_gate = 1,
19 k_route_special_type_collector = 2
20 };
21
22 static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
23 u32 colour, float xoffset )
24 {
25 v3f p0, h0, p1, h1, l, p;
26
27 v3_copy( rna->co, p0 );
28 v3_muladds( rna->co, rna->h, 1.0f, h0 );
29 v3_copy( rnb->co, p1 );
30 v3_muladds( rnb->co, rnb->h, -1.0f, h1 );
31
32 v3_muladds( p0, rna->right, xoffset, p0 );
33 v3_muladds( h0, rna->right, xoffset, h0 );
34 v3_muladds( p1, rnb->right, xoffset, p1 );
35 v3_muladds( h1, rnb->right, xoffset, h1 );
36
37 v3_copy( p0, l );
38
39 for( int i=0; i<5; i++ )
40 {
41 float t = (float)(i+1)/5.0f;
42 eval_bezier_time( p0, p1, h0, h1, t, p );
43 vg_line( p, l, colour );
44 v3_copy( p, l );
45 }
46 }
47
48 /*
49 * Get a list of node ids in stack, and return how many there is
50 */
51 static u32 world_routes_get_path( u32 starter, u32 stack[64] )
52 {
53 struct subworld_routes *r = &world.routes;
54 u32 stack_i[64];
55
56 stack[0] = starter;
57 stack_i[0] = 0;
58
59 u32 si = 1;
60 int loop_complete = 0;
61
62 while( si )
63 {
64 if( stack_i[si-1] == 2 )
65 {
66 si --;
67 continue;
68 }
69
70 struct route_node *rn = &r->nodes[stack[si-1]];
71 u32 nextid = rn->next[stack_i[si-1]];
72 stack_i[si-1] ++;
73
74 if( nextid != 0xffffffff )
75 {
76 if( nextid == stack[0] )
77 {
78 loop_complete = 1;
79 break;
80 }
81
82 int valid = 1;
83 for( int sj=0; sj<si; sj++ )
84 {
85 if( stack[sj] == nextid )
86 {
87 valid = 0;
88 break;
89 }
90 }
91
92 if( valid )
93 {
94 stack_i[si] = 0;
95 stack[si] = nextid;
96 si ++;
97 continue;
98 }
99 }
100 }
101
102 if( loop_complete )
103 return si;
104
105 return 0;
106 }
107
108 /*
109 * Free a segment from the UI bar to be reused later
110 */
111 static void world_routes_ui_popfirst( u32 route )
112 {
113 struct subworld_routes *r = &world.routes;
114 struct route *pr = &r->routes[route];
115
116 if( pr->ui.segment_count )
117 {
118 pr->ui.segment_start ++;
119
120 if( pr->ui.segment_start == 32 )
121 pr->ui.segment_start = 0;
122
123 pr->ui.segment_count --;
124 }
125 }
126
127 /*
128 * Reset ui bar completely
129 */
130 static void world_routes_ui_clear( u32 route )
131 {
132 struct subworld_routes *r = &world.routes;
133 struct route *pr = &r->routes[route];
134 pr->ui.segment_start = (pr->ui.segment_start + pr->ui.segment_count) %
135 k_max_ui_segments;
136 pr->ui.segment_count = 0;
137 }
138
139 /*
140 * Break a index range into two pieces over the edge of the maximum it can
141 * store. s1 is 0 always, so its a ring buffer.
142 */
143 static void world_routes_ui_split_indices( u32 s0, u32 count, u32 *c0, u32 *c1 )
144 {
145 *c0 = (VG_MIN( s0+count, k_route_ui_max_indices )) - s0;
146 *c1 = count-(*c0);
147 }
148
149 /*
150 * Place a set of indices into gpu array automatically splits
151 * across bounds
152 */
153 static void world_routes_ui_set_indices( struct route *pr,
154 u16 *indices, u32 count )
155 {
156 u32 c0, c1;
157 world_routes_ui_split_indices( pr->ui.indices_head, count, &c0, &c1 );
158
159 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pr->ui.ebo );
160
161 if( c0 )
162 {
163 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, pr->ui.indices_head*sizeof(u16),
164 c0*sizeof(u16), indices );
165 }
166
167 if( c1 )
168 {
169 glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, c1*sizeof(u16), indices+c0 );
170 pr->ui.indices_head = c1;
171 }
172 else
173 pr->ui.indices_head += c0;
174 }
175
176 /*
177 * Place a set of vertices into gpu array
178 */
179 static u32 world_routes_ui_set_verts( struct route *pr, v2f *verts, u32 count )
180 {
181 if( pr->ui.vertex_head + count >= k_route_ui_max_verts )
182 pr->ui.vertex_head = 0;
183
184 u32 vert_start = pr->ui.vertex_head;
185 pr->ui.vertex_head += count;
186
187 glBindBuffer( GL_ARRAY_BUFFER, pr->ui.vbo );
188 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
189 sizeof(v2f)*count, verts );
190
191 return vert_start;
192 }
193
194 /*
195 * Update the last (count) vertices positions, does not add any.
196 * Data must already be written to, and not cross either array boundaries.
197 */
198 static u32 world_routes_ui_update_verts( struct route *pr,
199 v2f *verts, u32 count )
200 {
201 u32 vert_start = pr->ui.vertex_head-count;
202
203 glBindBuffer( GL_ARRAY_BUFFER, pr->ui.vbo );
204 glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
205 sizeof(v2f)*count, verts );
206
207 return vert_start;
208 }
209
210 /*
211 * Current/active segment of this UI bar
212 */
213 static struct route_ui_segment *world_routes_ui_curseg( struct route *pr )
214 {
215 u32 index = (pr->ui.segment_start+pr->ui.segment_count-1)%k_max_ui_segments;
216 return &pr->ui.segments[ index ];
217 }
218
219 /*
220 * Start a new segment in the UI bar, will create a split on the last one if
221 * there is one active currently. (api)
222 */
223 static void world_routes_ui_newseg( u32 route )
224 {
225 struct subworld_routes *r = &world.routes;
226 struct route *pr = &r->routes[route];
227
228 pr->ui.last_notch = 0.0;
229
230 glBindVertexArray( pr->ui.vao );
231 if( pr->ui.segment_count )
232 {
233 float const k_gap_width = 1.0f;
234
235 struct route_ui_segment *cseg = world_routes_ui_curseg(pr);
236
237 v2f verts[2];
238 verts[0][0] = cseg->length-k_gap_width;
239 verts[0][1] = 0.5f;
240 verts[1][0] = cseg->length-k_gap_width;
241 verts[1][1] = -0.5f;
242
243 world_routes_ui_update_verts( pr, verts, 2 );
244 }
245
246 pr->ui.segment_count ++;
247 struct route_ui_segment *segment = world_routes_ui_curseg(pr);
248
249 v2f verts[4];
250 verts[0][0] = 0.0f;
251 verts[0][1] = 0.5f;
252 verts[1][0] = 0.0f;
253 verts[1][1] = -0.5f;
254 verts[2][0] = 0.0f;
255 verts[2][1] = 0.5f;
256 verts[3][0] = 0.0f;
257 verts[3][1] = -0.5f;
258
259 u32 vert_start = world_routes_ui_set_verts( pr, verts, 4 );
260
261 u16 indices[6];
262 indices[0] = vert_start + 0;
263 indices[1] = vert_start + 1;
264 indices[2] = vert_start + 3;
265 indices[3] = vert_start + 0;
266 indices[4] = vert_start + 3;
267 indices[5] = vert_start + 2;
268
269 segment->vertex_start = vert_start;
270 segment->vertex_count = 4;
271 segment->index_start = pr->ui.indices_head;
272 segment->index_count = 6;
273 segment->notches = 0;
274
275 world_routes_ui_set_indices( pr, indices, 6 );
276 }
277
278 /*
279 * Extend the end of the bar
280 */
281 static void world_routes_ui_updatetime( u32 route, float time )
282 {
283 struct subworld_routes *r = &world.routes;
284 struct route *pr = &r->routes[route];
285
286 v2f verts[2];
287 verts[0][0] = time;
288 verts[0][1] = 0.5f;
289 verts[1][0] = time;
290 verts[1][1] = -0.5f;
291
292 u32 vert_start = pr->ui.vertex_head-2;
293
294 glBindVertexArray( pr->ui.vao );
295 world_routes_ui_update_verts( pr, verts, 2 );
296
297 struct route_ui_segment *cseg = world_routes_ui_curseg(pr);
298 cseg->length = time;
299 }
300
301 /*
302 * Create a notch in the bar, used when a reset is triggered by the user
303 */
304 static void world_routes_ui_notch( u32 route, float time )
305 {
306 return; /* FIXME: Temporarily disabled */
307
308 struct subworld_routes *r = &world.routes;
309 struct route *pr = &r->routes[route];
310
311 if( (time - pr->ui.last_notch) > 1.0 )
312 {
313 struct route_ui_segment *segment = world_routes_ui_curseg(pr);
314 if( segment->notches == k_max_ui_splits_per_segment )
315 return;
316
317 segment->notches ++;
318
319 v2f verts[8];
320
321 float const k_notch_width = 1.0f;
322
323 float xa = time-k_notch_width,
324 xb = time-k_notch_width * 0.5f,
325 xc = time;
326
327 verts[0][0] = xa;
328 verts[0][1] = 0.5f;
329 verts[1][0] = xa;
330 verts[1][1] = -0.5f;
331
332 verts[2][0] = xb;
333 verts[2][1] = 0.25f;
334 verts[3][0] = xb;
335 verts[3][1] = -0.25f;
336
337 verts[4][0] = xc;
338 verts[4][1] = 0.5f;
339 verts[5][0] = xc;
340 verts[5][1] = -0.5f;
341
342 verts[6][0] = xc;
343 verts[6][1] = 0.5f;
344 verts[7][0] = xc;
345 verts[7][1] = -0.5f;
346
347 glBindVertexArray( pr->ui.vao );
348 u32 vert_start_mod = world_routes_ui_update_verts( pr, verts, 2 ),
349 vert_start_new = world_routes_ui_set_verts( pr, verts+2, 6 );
350
351 u16 indices[18];
352 indices[ 0] = vert_start_mod+1;
353 indices[ 1] = vert_start_new+0;
354 indices[ 2] = vert_start_mod+0;
355 indices[ 3] = vert_start_mod+1;
356 indices[ 4] = vert_start_new+1;
357 indices[ 5] = vert_start_new+0;
358
359 indices[ 6] = vert_start_new+0;
360 indices[ 7] = vert_start_new+1;
361 indices[ 8] = vert_start_new+3;
362 indices[ 9] = vert_start_new+0;
363 indices[10] = vert_start_new+3;
364 indices[11] = vert_start_new+2;
365
366 indices[12] = vert_start_new+3;
367 indices[13] = vert_start_new+4;
368 indices[14] = vert_start_new+2;
369 indices[15] = vert_start_new+3;
370 indices[16] = vert_start_new+5;
371 indices[17] = vert_start_new+4;
372
373 world_routes_ui_set_indices( pr, indices, 18 );
374
375 pr->ui.last_notch = time;
376
377 segment->vertex_count += 6;
378 segment->index_count += 18;
379 }
380 }
381
382 static void world_routes_ui_draw_segment( struct route_ui_segment *segment )
383 {
384 u32 c0, c1;
385 world_routes_ui_split_indices( segment->index_start,
386 segment->index_count, &c0, &c1 );
387 if( c0 )
388 glDrawElements( GL_TRIANGLES, c0, GL_UNSIGNED_SHORT,
389 (void *)(segment->index_start*sizeof(u16)));
390 if( c1 )
391 glDrawElements( GL_TRIANGLES, c1, GL_UNSIGNED_SHORT, (void *)(0) );
392 }
393
394 /*
395 * Draws full bar at Y offset(offset).
396 */
397 static void world_routes_ui_draw( u32 route, v4f colour, float offset )
398 {
399 float const k_bar_height = 0.05f,
400 k_bar_scale_x = 0.005f;
401
402 struct subworld_routes *r = &world.routes;
403 struct route *pr = &r->routes[route];
404
405 float cx = pr->ui.xpos;
406
407 shader_routeui_use();
408 glBindVertexArray( pr->ui.vao );
409
410 float fade_amt = r->time - pr->ui.fade_timer_start;
411 fade_amt = vg_clampf( fade_amt / 1.0f, 0.0f, 1.0f );
412
413 float fade_block_size = 0.0f,
414 main_block_size = 0.0f;
415
416 for( u32 i=0; i<pr->ui.fade_count; i++ )
417 {
418 u32 j = (pr->ui.fade_start + i) % k_max_ui_segments;
419 struct route_ui_segment *segment = &pr->ui.segments[j];
420
421 fade_block_size += segment->length;
422 }
423
424 cx -= fade_block_size * fade_amt;
425
426 v4f fade_colour;
427 v4_copy( colour, fade_colour );
428 fade_colour[3] *= 1.0f-fade_amt;
429
430 /* 1 minute timer */
431 float timer_delta = (r->time - r->last_use) * (1.0/45.0),
432 timer_scale = 1.0f - vg_minf( timer_delta, 1.0f );
433
434 /*
435 * Draw fadeout bar
436 */
437
438 float height = pr->factive*k_bar_height * timer_scale,
439 base = -1.0f + (offset+0.5f)*k_bar_height * timer_scale;
440
441 shader_routeui_uColour( fade_colour );
442 for( u32 i=0; i<pr->ui.fade_count; i++ )
443 {
444 u32 j = (pr->ui.fade_start + i) % k_max_ui_segments;
445 struct route_ui_segment *segment = &pr->ui.segments[j];
446
447 shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
448 k_bar_scale_x, height } );
449
450 world_routes_ui_draw_segment( segment );
451 cx += segment->length;
452 }
453
454 /*
455 * Draw main bar
456 */
457 shader_routeui_uColour( colour );
458 for( u32 i=0; i<pr->ui.segment_count; i++ )
459 {
460 u32 j = (pr->ui.segment_start + i) % k_max_ui_segments;
461 struct route_ui_segment *segment = &pr->ui.segments[j];
462
463 shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
464 k_bar_scale_x, height } );
465
466 world_routes_ui_draw_segment( segment );
467 cx += segment->length;
468
469 main_block_size += segment->length;
470 }
471
472 pr->ui.xpos = vg_lerpf( pr->ui.xpos, -main_block_size * 0.5f, 0.03f );
473 }
474
475 static void world_routes_local_set_record( u32 route, double lap_time )
476 {
477 vg_success( " NEW LAP TIME: %f\n", lap_time );
478
479 struct subworld_routes *r = &world.routes;
480 struct route *pr = &r->routes[route];
481
482 if( pr->track_id != 0xffffffff )
483 {
484 double time_centiseconds = lap_time * 100.0;
485 if( time_centiseconds > (float)0xfffe )
486 return;
487
488 highscore_record temp;
489 temp.trackid = pr->track_id;
490 temp.datetime = time(NULL);
491 temp.playerid = 0;
492 temp.points = 0;
493 temp.time = time_centiseconds;
494
495 highscores_push_record( &temp );
496
497 struct track_info *pti = &track_infos[ pr->track_id ];
498 pti->push = 1;
499
500 if( pti->achievement_id )
501 {
502 steam_set_achievement( pti->achievement_id );
503 steam_store_achievements();
504 }
505 }
506 else
507 {
508 vg_warn( "There is no associated track for this record...\n" );
509 }
510 }
511
512 /*
513 * Will scan the whole run for two things;
514 * 1: we set a new record for the total, complete loop around the course
515 * 2: the time of each segment will be recorded into the data buffer
516 * (not implemented: TODO)
517 */
518 static void world_routes_verify_run( u32 route )
519 {
520 struct subworld_routes *r = &world.routes;
521 struct route *pr = &r->routes[route];
522
523 u32 stack[64];
524 u32 si = world_routes_get_path( r->routes[route].start, stack );
525
526 /*
527 * we only care about gates that ref gates, so shuffle down the array
528 */
529 struct route_timing *timings[64];
530 u32 sj = 0, maxv = 0, begin = 0;
531 for( u32 i=0; i<si; i++ )
532 {
533 if( r->nodes[stack[i]].special_type == k_route_special_type_collector )
534 timings[sj ++] = &r->collectors[r->nodes[stack[i]].special_id].timing;
535 else if( r->nodes[stack[i]].special_type == k_route_special_type_gate )
536 timings[sj ++] = &r->gates[r->nodes[stack[i]].special_id].timing;
537 }
538
539 for( u32 i=0; i<sj; i++ )
540 {
541 if( timings[i]->version > maxv )
542 {
543 maxv = timings[i]->version;
544 begin = i;
545 }
546 }
547
548 vg_info( "== begin verification (%u) ==\n", route );
549 vg_info( " current version: %u\n", r->current_run_version );
550
551 int verified = 0;
552 if( timings[begin]->version == r->current_run_version )
553 verified = 1;
554
555 int valid_segment_count = 0;
556
557 double lap_time = 0.0;
558
559 for( u32 i=0; i<sj; i++ )
560 {
561 u32 j = (sj+begin-i-1) % sj,
562 j1 = (j+1) % sj;
563
564 double diff = 0.0;
565
566 if( i<sj-1 )
567 {
568 /* j1v should equal jv+1 */
569 if( timings[j1]->version == timings[j]->version+1 )
570 {
571 diff = timings[j1]->time - timings[j]->time;
572 lap_time += diff;
573
574 if( verified && diff > 0.0 ) valid_segment_count ++;
575 }
576 else
577 verified = 0;
578 }
579
580 if( verified )
581 vg_success( " [ %u %f ] %f\n", timings[j1]->time,
582 timings[j1]->version, diff );
583 else
584 vg_warn( " [ %u %f ]\n", timings[j1]->time, timings[j1]->version );
585 }
586
587 pr->ui.fade_start = pr->ui.segment_start;
588 pr->ui.fade_count = 0;
589 pr->ui.fade_timer_start = r->time;
590
591 int orig_seg_count = pr->ui.segment_count;
592
593 world_routes_ui_newseg( route );
594
595 if( verified )
596 {
597 world_routes_local_set_record( route, lap_time );
598 world_routes_ui_popfirst(route);
599 pr->ui.fade_count ++;
600 }
601 else
602 vg_info( " ctime: %f\n", lap_time );
603
604 /* remove any excess we had from previous runs */
605 int to_remove = orig_seg_count-valid_segment_count;
606 for( int i=0; i<to_remove; i++ )
607 {
608 world_routes_ui_popfirst(route);
609 pr->ui.fade_count ++;
610 }
611
612 r->routes[route].latest_pass = r->time;
613 }
614
615 /*
616 * When going through a gate this is called for bookkeeping purposes
617 */
618 static void world_routes_activate_gate( u32 id )
619 {
620 struct subworld_routes *r = &world.routes;
621 struct route_gate *rg = &r->gates[id];
622 struct route_node *pnode = &r->nodes[rg->node_id],
623 *pdest = &r->nodes[pnode->next[0]];
624
625 r->last_use = r->time;
626
627 struct route_collector *rc = &r->collectors[ pdest->special_id ];
628
629 r->active_gate = id;
630 rg->timing.version = r->current_run_version;
631 rg->timing.time = r->time;
632 for( u32 i=0; i<r->route_count; i++ )
633 {
634 struct route *route = &r->routes[i];
635
636 int was_active = route->active;
637
638 route->active = 0;
639 for( u32 j=0; j<pdest->ref_count; j++ )
640 {
641 if( pdest->route_ids[j] == i )
642 {
643 world_routes_verify_run( i );
644 route->active = 1;
645 break;
646 }
647 }
648
649 if( was_active && !route->active )
650 {
651 route->ui.fade_start = route->ui.segment_start;
652 route->ui.fade_count = route->ui.segment_count;
653 route->ui.fade_timer_start = r->time;
654 world_routes_ui_clear(i);
655
656 vg_success( "CLEARING -> %u %u \n", route->ui.fade_start,
657 route->ui.fade_count );
658 }
659 }
660
661 r->current_run_version ++;
662
663 rc->timing.version = r->current_run_version;
664 rc->timing.time = r->time;
665 r->current_run_version ++;
666 }
667
668 /*
669 * Notify the UI system that we've reset the player
670 */
671 static void world_routes_notify_reset(void)
672 {
673 struct subworld_routes *r = &world.routes;
674 r->rewind_from = r->time;
675 r->rewind_to = r->last_use;
676
677 #if 0
678 for( int i=0; i<r->route_count; i++ )
679 {
680 struct route *route = &r->routes[i];
681
682 if( route->active )
683 world_routes_ui_notch( i, r->time - route->latest_pass );
684 }
685 #endif
686 }
687
688 static void world_routes_rollback_time( double t )
689 {
690 struct subworld_routes *r = &world.routes;
691 r->time = vg_lerp( r->rewind_to, r->rewind_from, t );
692 }
693
694 static void world_routes_debug(void)
695 {
696 struct subworld_routes *r = &world.routes;
697
698 for( int i=0; i<r->node_count; i++ )
699 {
700 struct route_node *rn = &r->nodes[i];
701 vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
702 }
703
704 for( int i=0; i<r->route_count; i++ )
705 {
706 struct route *route = &r->routes[i];
707
708 u32 stack[64];
709 u32 si = world_routes_get_path( route->start, stack );
710
711 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
712 0xff5442f5 };
713
714 u32 cc = colours[i%vg_list_size(colours)];
715
716 for( int sj=0; sj<si; sj++ )
717 {
718 int sk = (sj+1)%si;
719 debug_sbpath( &r->nodes[stack[sj]], &r->nodes[stack[sk]], cc,
720 (float)i );
721 }
722 }
723
724 for( int i=0; i<r->node_count; i++ )
725 {
726 struct route_node *ri = &r->nodes[i],
727 *rj = NULL;
728
729 for( int j=0; j<2; j++ )
730 {
731 if( ri->next[j] != 0xffffffff )
732 {
733 rj = &r->nodes[ri->next[j]];
734 vg_line( ri->co, rj->co, 0x20ffffff );
735 }
736 }
737 }
738 }
739
740 static void world_id_fixup( u32 *uid, mdl_header *mdl )
741 {
742 if( *uid )
743 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
744 else
745 *uid = 0xffffffff;
746 }
747
748 static void world_routes_create_mesh( u32 route_id )
749 {
750 struct subworld_routes *r = &world.routes;
751 struct route *route = &r->routes[ route_id ];
752
753 u32 stack[64];
754 u32 si = world_routes_get_path( route->start, stack );
755
756 u32 last_valid = 0;
757
758 for( int sj=0; sj<si; sj++ )
759 {
760 int sk=(sj+1)%si;
761
762 struct route_node *rnj = &r->nodes[ stack[sj] ],
763 *rnk = &r->nodes[ stack[sk] ],
764 *rnl;
765
766 if( rnj->special_type && rnk->special_type )
767 {
768 last_valid = 0;
769 continue;
770 }
771
772 float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
773 base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
774
775 if( rnk->special_type )
776 {
777 rnl = &r->nodes[ rnk->next[0] ];
778 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
779 }
780
781 if( sk == 0 )
782 {
783 base_x1 -= 1.0f;
784 }
785
786 v3f p0, h0, p1, h1, p, pd;
787
788 v3_copy( rnj->co, p0 );
789 v3_muladds( rnj->co, rnj->h, 1.0f, h0 );
790 v3_copy( rnk->co, p1 );
791 v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
792
793 float t=0.0f;
794 int it = 0;
795
796 for( int it=0; it<256; it ++ )
797 {
798 float const k_sample_dist = 0.02f;
799 eval_bezier_time( p0,p1,h0,h1, t,p );
800 eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
801
802 float mod = k_sample_dist / v3_dist( p, pd );
803
804 v3f v0,up, right;
805 v3_muls( rnj->up, 1.0f-t, up );
806 v3_muladds( up, rnk->up, t, up );
807
808 v3_sub( pd,p,v0 );
809 v3_cross( up, v0, right );
810 v3_normalize( right );
811
812 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
813
814 v3f sc, sa, sb, down;
815 v3_muladds( p, right, cur_x, sc );
816 v3_muladds( sc, up, 1.5f, sc );
817 v3_muladds( sc, right, 0.45f, sa );
818 v3_muladds( sc, right, -0.45f, sb );
819 v3_muls( up, -1.0f, down );
820
821 ray_hit ha, hb;
822 ha.dist = 8.0f;
823 hb.dist = 8.0f;
824 if( ray_world( sa, down, &ha ) &&
825 ray_world( sb, down, &hb ))
826 {
827 mdl_vert va, vb;
828
829 v3_muladds( ha.pos, up, 0.06f, va.co );
830 v3_muladds( hb.pos, up, 0.06f, vb.co );
831 v3_copy( up, va.norm );
832 v3_copy( up, vb.norm );
833 v2_zero( va.uv );
834 v2_zero( vb.uv );
835
836 scene_push_vert( &r->scene_lines, &va );
837 scene_push_vert( &r->scene_lines, &vb );
838
839 if( last_valid )
840 {
841 /* Connect them with triangles */
842 scene_push_tri( &r->scene_lines, (u32[3]){
843 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
844 scene_push_tri( &r->scene_lines, (u32[3]){
845 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
846 }
847
848 last_valid = r->scene_lines.vertex_count;
849 }
850 else
851 last_valid = 0;
852
853 t += 1.0f*mod;
854
855 if( t >= 1.0f )
856 {
857 /* TODO special case for end of loop, need to add triangles
858 * between first and last rungs */
859 break;
860 }
861 }
862
863 rnj->current_refs ++;
864 }
865
866 scene_copy_slice( &r->scene_lines, &route->sm );
867 }
868
869 /*
870 * Create the strips of colour that run through the world along course paths
871 */
872 static int world_routes_create_all_meshes(void)
873 {
874 vg_info( "Generating route meshes\n" );
875
876 struct subworld_routes *r = &world.routes;
877 scene_init( &r->scene_lines );
878
879 for( u32 i=0; i<r->route_count; i++ )
880 world_routes_create_mesh( i );
881
882 vg_acquire_thread_sync();
883 {
884 scene_upload( &r->scene_lines );
885
886 /* UI buffers */
887 for( int i=0; i<r->route_count; i++ )
888 {
889 /* OpenGL strips */
890 struct route *route = &r->routes[i];
891
892 glGenVertexArrays( 1, &route->ui.vao );
893 glGenBuffers( 1, &route->ui.vbo );
894 glGenBuffers( 1, &route->ui.ebo );
895 glBindVertexArray( route->ui.vao );
896
897 size_t stride = sizeof(v2f);
898
899 glBindBuffer( GL_ARRAY_BUFFER, route->ui.vbo );
900 glBufferData( GL_ARRAY_BUFFER, k_route_ui_max_verts*stride,
901 NULL, GL_DYNAMIC_DRAW );
902
903 glBindVertexArray( route->ui.vao );
904 glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, route->ui.ebo );
905 glBufferData( GL_ELEMENT_ARRAY_BUFFER,
906 k_route_ui_max_indices*sizeof(u16), NULL,
907 GL_DYNAMIC_DRAW );
908
909 glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, stride, (void *)0 );
910 glEnableVertexAttribArray( 0 );
911 VG_CHECK_GL_ERR();
912 }
913 }
914 vg_release_thread_sync();
915
916 scene_free_offline_buffers( &r->scene_lines );
917 return 1;
918 }
919
920
921 static void world_routes_loadfrom( mdl_header *mdl )
922 {
923 vg_info( "Initializing routes\n" );
924
925 struct subworld_routes *r = &world.routes;
926 r->nodes = NULL;
927 r->node_count = 0; r->node_cap = 0; r->routes = NULL;
928 r->route_count = 0;
929 r->route_cap = 0;
930 r->gates = NULL;
931 r->gate_count = 0;
932 r->gate_cap = 0;
933
934 /* TODO Break this up */
935 for( int i=0; i<mdl->node_count; i++ )
936 {
937 mdl_node *pnode = mdl_node_from_id(mdl,i);
938 m4x3f transform;
939
940 if( pnode->classtype == k_classtype_route_node ||
941 pnode->classtype == k_classtype_gate )
942 {
943 mdl_node_transform( pnode, transform );
944 pnode->sub_uid = r->node_count;
945
946 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
947 sizeof( struct route_node ) );
948
949 struct route_node *rn = &r->nodes[r->node_count];
950
951 v3_copy( transform[0], rn->right );
952 v3_normalize( rn->right );
953 v3_copy( transform[1], rn->up );
954 v3_normalize( rn->up );
955 v3_muls( transform[2], -1.0f, rn->h );
956 v3_copy( transform[3], rn->co );
957 rn->ref_count = 0;
958 rn->current_refs = 0;
959 rn->special_type = 0;
960 rn->special_id = 0;
961
962 if( pnode->classtype == k_classtype_gate )
963 {
964 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
965
966 /* H is later scaled based on link distance */
967 v3_normalize( rn->h );
968 rn->next[0] = inf->target;
969 rn->next[1] = 0;
970
971 /* TODO */
972 if( inf->target )
973 {
974 mdl_node *pother = mdl_node_from_id( mdl, inf->target );
975
976 if( pother->classtype == k_classtype_gate )
977 {
978 r->gates = buffer_reserve( r->gates, r->gate_count,
979 &r->gate_cap,
980 1, sizeof( struct route_gate ) );
981
982 struct route_gate *rg = &r->gates[r->gate_count];
983 rg->node_id = r->node_count;
984 rg->timing.time = 0.0;
985 rg->timing.version = 0;
986
987 v3_copy( pnode->co, rg->gate.co[0] );
988 v3_copy( pother->co, rg->gate.co[1] );
989 v4_copy( pnode->q, rg->gate.q[0] );
990 v4_copy( pother->q, rg->gate.q[1] );
991 v2_copy( inf->dims, rg->gate.dims );
992
993 gate_transform_update( &rg->gate );
994 rn->special_type = k_route_special_type_gate;
995 rn->special_id = r->gate_count;
996
997 r->gate_count ++;
998 }
999 }
1000
1001 if( rn->special_type == 0 )
1002 {
1003 r->collectors = buffer_reserve(
1004 r->collectors, r->collector_count, &r->collector_cap,
1005 1, sizeof( struct route_collector ));
1006
1007 struct route_collector *rc = &r->collectors[r->collector_count];
1008 rc->timing.time = 0.0;
1009 rc->timing.version = 0;
1010
1011 rn->special_type = k_route_special_type_collector;
1012 rn->special_id = r->collector_count;
1013
1014 r->collector_count ++;
1015 }
1016 }
1017 else
1018 {
1019 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
1020 rn->next[0] = inf->target;
1021 rn->next[1] = inf->target1;
1022 }
1023
1024 r->node_count ++;
1025 }
1026 else if( pnode->classtype == k_classtype_route )
1027 {
1028 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
1029 r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
1030 1, sizeof( struct route ) );
1031
1032 struct route *route = &r->routes[r->route_count];
1033 memset( route, 0, sizeof(struct route) );
1034
1035 v3_copy( inf->colour, route->colour );
1036 route->colour[3] = 1.0f;
1037
1038
1039 route->track_id = 0xffffffff;
1040 for( u32 j=0; j<vg_list_size(track_infos); j++ )
1041 {
1042 if( !strcmp( mdl_pstr(mdl,pnode->pstr_name), track_infos[j].name ))
1043 {
1044 route->track_id = j;
1045 break;
1046 }
1047 }
1048
1049 route->start = inf->id_start;
1050 route->active = 0;
1051 route->factive = 0.0f;
1052 mdl_node_transform( pnode, route->scoreboard_transform );
1053
1054 route->ui.indices_head = k_route_ui_max_indices - 9;
1055 route->ui.vertex_head = k_route_ui_max_verts - 200;
1056 route->ui.segment_start = 0;
1057 route->ui.segment_count = 0;
1058 route->ui.last_notch = 0.0;
1059 route->ui.fade_start = 0;
1060 route->ui.fade_count = 0;
1061 route->ui.fade_timer_start = 0.0;
1062
1063 r->route_count ++;
1064 }
1065 }
1066
1067 /*
1068 * Apply correct system-local ids
1069 */
1070 for( int i=0; i<r->node_count; i++ )
1071 {
1072 struct route_node *rn = &r->nodes[i];
1073
1074 for( int j=0; j<2; j++ )
1075 world_id_fixup( &rn->next[j], mdl );
1076 }
1077
1078 for( int i=0; i<r->route_count; i++ )
1079 {
1080 struct route *route = &r->routes[i];
1081 world_id_fixup( &route->start, mdl );
1082 }
1083
1084 /*
1085 * Gather references
1086 */
1087 for( int i=0; i<r->route_count; i++ )
1088 {
1089 struct route *route = &r->routes[i];
1090
1091 u32 stack[64];
1092 u32 si = world_routes_get_path( route->start, stack );
1093
1094 for( int sj=0; sj<si; sj++ )
1095 {
1096 struct route_node *rn = &r->nodes[ stack[sj] ];
1097 rn->route_ids[ rn->ref_count ++ ] = i;
1098
1099 if( rn->ref_count > 4 )
1100 vg_warn( "Too many references on route node %i\n", i );
1101 }
1102 }
1103
1104 world_routes_create_all_meshes();
1105 }
1106
1107 /*
1108 * -----------------------------------------------------------------------------
1109 * Events
1110 * -----------------------------------------------------------------------------
1111 */
1112
1113 static void world_routes_init(void)
1114 {
1115 struct subworld_routes *r = &world.routes;
1116 r->current_run_version = 2;
1117
1118 shader_route_register();
1119 shader_routeui_register();
1120 }
1121
1122 static void world_routes_free(void*_)
1123 {
1124 struct subworld_routes *r = &world.routes;
1125
1126 vg_free( r->nodes );
1127 vg_free( r->routes );
1128 vg_free( r->gates );
1129 }
1130
1131 static void world_routes_update(void)
1132 {
1133 struct subworld_routes *r = &world.routes;
1134 r->time += vg.time_delta;
1135
1136 for( int i=0; i<r->route_count; i++ )
1137 {
1138 struct route *route = &r->routes[i];
1139 route->factive = vg_lerpf( route->factive, route->active,
1140 0.6f*vg.time_delta );
1141
1142 if( route->active )
1143 {
1144 world_routes_ui_updatetime( i, r->time - route->latest_pass );
1145 }
1146 }
1147 }
1148
1149 static void bind_terrain_textures(void);
1150 static void render_world_routes( m4x4f projection, v3f camera )
1151 {
1152 struct subworld_routes *r = &world.routes;
1153
1154 m4x3f identity_matrix;
1155 m4x3_identity( identity_matrix );
1156
1157 shader_route_use();
1158 shader_route_uTexGarbage(0);
1159 shader_link_standard_ub( _shader_route.id, 2 );
1160 bind_terrain_textures();
1161
1162 shader_route_uPv( projection );
1163 shader_route_uMdl( identity_matrix );
1164 shader_route_uCamera( camera );
1165
1166 scene_bind( &r->scene_lines );
1167
1168 for( int i=0; i<r->route_count; i++ )
1169 {
1170 struct route *route = &r->routes[i];
1171
1172 v4f colour;
1173 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
1174 colour[3] = 1.0f;
1175
1176 shader_route_uColour( colour );
1177 mdl_draw_submesh( &route->sm );
1178 }
1179 }
1180
1181 static void render_world_routes_ui(void)
1182 {
1183 glEnable(GL_BLEND);
1184 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1185 glBlendEquation(GL_FUNC_ADD);
1186
1187 struct subworld_routes *r = &world.routes;
1188
1189 float active_offset = 0.0f;
1190 for( int i=0; i<r->route_count; i++ )
1191 {
1192 struct route *route = &r->routes[i];
1193 world_routes_ui_draw( i, route->colour, active_offset );
1194 active_offset += route->factive;
1195 }
1196
1197 glDisable(GL_BLEND);
1198 }
1199
1200 #endif /* ROUTES_H */