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