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