medium sized dollop
[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 /*
730 * Create the strips of colour that run through the world along course paths
731 */
732 static int world_routes_gen_meshes(void)
733 {
734 vg_info( "Generating route meshes\n" );
735
736 struct subworld_routes *r = &world.routes;
737 scene_init( &r->scene_lines );
738
739 for( int i=0; i<r->route_count; i++ )
740 {
741 struct route *route = &r->routes[i];
742
743 u32 stack[64];
744 u32 si = world_routes_get_path( route->start, stack );
745
746 u32 last_valid = 0;
747
748 for( int sj=0; sj<si; sj++ )
749 {
750 int sk=(sj+1)%si;
751
752 struct route_node *rnj = &r->nodes[ stack[sj] ],
753 *rnk = &r->nodes[ stack[sk] ],
754 *rnl;
755
756 if( rnj->special_type && rnk->special_type )
757 {
758 last_valid = 0;
759 continue;
760 }
761
762 float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
763 base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
764
765 if( rnk->special_type )
766 {
767 rnl = &r->nodes[ rnk->next[0] ];
768 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
769 }
770
771 if( sk == 0 )
772 {
773 base_x1 -= 1.0f;
774 }
775
776 v3f p0, h0, p1, h1, p, pd;
777
778 v3_copy( rnj->co, p0 );
779 v3_muladds( rnj->co, rnj->h, 1.0f, h0 );
780 v3_copy( rnk->co, p1 );
781 v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
782
783 float t=0.0f;
784 int it = 0;
785
786 for( int it=0; it<256; it ++ )
787 {
788 float const k_sample_dist = 0.02f;
789 eval_bezier_time( p0,p1,h0,h1, t,p );
790 eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
791
792 float mod = k_sample_dist / v3_dist( p, pd );
793
794 v3f v0,up, right;
795 v3_muls( rnj->up, 1.0f-t, up );
796 v3_muladds( up, rnk->up, t, up );
797
798 v3_sub( pd,p,v0 );
799 v3_cross( up, v0, right );
800 v3_normalize( right );
801
802 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
803
804 v3f sc, sa, sb, down;
805 v3_muladds( p, right, cur_x, sc );
806 v3_muladds( sc, up, 1.5f, sc );
807 v3_muladds( sc, right, 0.45f, sa );
808 v3_muladds( sc, right, -0.45f, sb );
809 v3_muls( up, -1.0f, down );
810
811 ray_hit ha, hb;
812 ha.dist = 8.0f;
813 hb.dist = 8.0f;
814 if(ray_world( sa, down, &ha ) &&
815 ray_world( sb, down, &hb ))
816 {
817 mdl_vert va, vb;
818
819 v3_muladds( ha.pos, up, 0.06f, va.co );
820 v3_muladds( hb.pos, up, 0.06f, vb.co );
821 v3_copy( up, va.norm );
822 v3_copy( up, vb.norm );
823 v2_zero( va.uv );
824 v2_zero( vb.uv );
825
826 scene_push_vert( &r->scene_lines, &va );
827 scene_push_vert( &r->scene_lines, &vb );
828
829 if( last_valid )
830 {
831 /* Connect them with triangles */
832 scene_push_tri( &r->scene_lines, (u32[3]){
833 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
834 scene_push_tri( &r->scene_lines, (u32[3]){
835 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
836 }
837
838 last_valid = r->scene_lines.vertex_count;
839 }
840 else
841 last_valid = 0;
842
843 t += 1.0f*mod;
844
845 if( t >= 1.0f )
846 {
847 /* TODO special case for end of loop, need to add triangles
848 * between first and last rungs */
849 break;
850 }
851 }
852
853 rnj->current_refs ++;
854 }
855
856 scene_copy_slice( &r->scene_lines, &route->sm );
857 }
858
859 if( vg_acquire_thread_sync(1) )
860 {
861 if( !scene_upload( &r->scene_lines ) )
862 {
863 vg_release_thread_sync(1);
864 return 0;
865 }
866
867 /* UI buffers */
868
869 for( int i=0; i<r->route_count; i++ )
870 {
871 /* OpenGL strips */
872 struct route *route = &r->routes[i];
873
874 glGenVertexArrays( 1, &route->ui.vao );
875 glGenBuffers( 1, &route->ui.vbo );
876 glGenBuffers( 1, &route->ui.ebo );
877 glBindVertexArray( route->ui.vao );
878
879 size_t stride = sizeof(v2f);
880
881 glBindBuffer( GL_ARRAY_BUFFER, route->ui.vbo );
882 glBufferData( GL_ARRAY_BUFFER, k_route_ui_max_verts*stride,
883 NULL, GL_DYNAMIC_DRAW );
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(1);
896 }
897 else
898 {
899 return 0;
900 }
901
902 scene_free_offline_buffers( &r->scene_lines );
903 return 1;
904 }
905
906
907 static void world_routes_loadfrom( mdl_header *mdl )
908 {
909 vg_info( "Initializing routes\n" );
910
911 struct subworld_routes *r = &world.routes;
912 r->nodes = NULL;
913 r->node_count = 0;
914 r->node_cap = 0;
915 r->routes = NULL;
916 r->route_count = 0;
917 r->route_cap = 0;
918 r->gates = NULL;
919 r->gate_count = 0;
920 r->gate_cap = 0;
921
922 for( int i=0; i<mdl->node_count; i++ )
923 {
924 mdl_node *pnode = mdl_node_from_id(mdl,i);
925 m4x3f transform;
926
927 if( pnode->classtype == k_classtype_route_node ||
928 pnode->classtype == k_classtype_gate )
929 {
930 mdl_node_transform( pnode, transform );
931 pnode->sub_uid = r->node_count;
932
933 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
934 sizeof( struct route_node ) );
935
936 struct route_node *rn = &r->nodes[r->node_count];
937
938 v3_copy( transform[0], rn->right );
939 v3_normalize( rn->right );
940 v3_copy( transform[1], rn->up );
941 v3_normalize( rn->up );
942 v3_muls( transform[2], -1.0f, rn->h );
943 v3_copy( transform[3], rn->co );
944 rn->ref_count = 0;
945 rn->current_refs = 0;
946 rn->special_type = 0;
947 rn->special_id = 0;
948
949 if( pnode->classtype == k_classtype_gate )
950 {
951 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
952
953 /* H is later scaled based on link distance */
954 v3_normalize( rn->h );
955 rn->next[0] = inf->target;
956 rn->next[1] = 0;
957
958 /* TODO */
959 if( inf->target )
960 {
961 mdl_node *pother = mdl_node_from_id( mdl, inf->target );
962
963 if( pother->classtype == k_classtype_gate )
964 {
965 r->gates = buffer_reserve( r->gates, r->gate_count,
966 &r->gate_cap,
967 1, sizeof( struct route_gate ) );
968
969 struct route_gate *rg = &r->gates[r->gate_count];
970 rg->node_id = r->node_count;
971 rg->timing.time = 0.0;
972 rg->timing.version = 0;
973
974 v3_copy( pnode->co, rg->gate.co[0] );
975 v3_copy( pother->co, rg->gate.co[1] );
976 v4_copy( pnode->q, rg->gate.q[0] );
977 v4_copy( pother->q, rg->gate.q[1] );
978 v2_copy( inf->dims, rg->gate.dims );
979
980 gate_transform_update( &rg->gate );
981 rn->special_type = k_route_special_type_gate;
982 rn->special_id = r->gate_count;
983
984 r->gate_count ++;
985 }
986 }
987
988 if( rn->special_type == 0 )
989 {
990 r->collectors = buffer_reserve(
991 r->collectors, r->collector_count, &r->collector_cap,
992 1, sizeof( struct route_collector ));
993
994 struct route_collector *rc = &r->collectors[r->collector_count];
995 rc->timing.time = 0.0;
996 rc->timing.version = 0;
997
998 rn->special_type = k_route_special_type_collector;
999 rn->special_id = r->collector_count;
1000
1001 r->collector_count ++;
1002 }
1003 }
1004 else
1005 {
1006 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
1007 rn->next[0] = inf->target;
1008 rn->next[1] = inf->target1;
1009 }
1010
1011 r->node_count ++;
1012 }
1013 else if( pnode->classtype == k_classtype_route )
1014 {
1015 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
1016 r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
1017 1, sizeof( struct route ) );
1018
1019 struct route *route = &r->routes[r->route_count];
1020
1021 v3_copy( inf->colour, route->colour );
1022 route->colour[3] = 1.0f;
1023
1024
1025 route->track_id = 0xffffffff;
1026 for( u32 j=0; j<vg_list_size(track_infos); j++ )
1027 {
1028 if( !strcmp( mdl_pstr(mdl,pnode->pstr_name), track_infos[j].name ))
1029 {
1030 route->track_id = j;
1031 break;
1032 }
1033 }
1034
1035 route->start = inf->id_start;
1036 route->active = 0;
1037 route->factive = 0.0f;
1038 mdl_node_transform( pnode, route->scoreboard_transform );
1039
1040 route->ui.indices_head = k_route_ui_max_indices - 9;
1041 route->ui.vertex_head = k_route_ui_max_verts - 200;
1042 route->ui.segment_start = 0;
1043 route->ui.segment_count = 0;
1044 route->ui.last_notch = 0.0;
1045 route->ui.fade_start = 0;
1046 route->ui.fade_count = 0;
1047 route->ui.fade_timer_start = 0.0;
1048
1049 r->route_count ++;
1050 }
1051 }
1052
1053 /*
1054 * Apply correct system-local ids
1055 */
1056 for( int i=0; i<r->node_count; i++ )
1057 {
1058 struct route_node *rn = &r->nodes[i];
1059
1060 for( int j=0; j<2; j++ )
1061 world_id_fixup( &rn->next[j], mdl );
1062 }
1063
1064 for( int i=0; i<r->route_count; i++ )
1065 {
1066 struct route *route = &r->routes[i];
1067 world_id_fixup( &route->start, mdl );
1068 }
1069
1070 /*
1071 * Gather references
1072 */
1073 for( int i=0; i<r->route_count; i++ )
1074 {
1075 struct route *route = &r->routes[i];
1076
1077 u32 stack[64];
1078 u32 si = world_routes_get_path( route->start, stack );
1079
1080 for( int sj=0; sj<si; sj++ )
1081 {
1082 struct route_node *rn = &r->nodes[ stack[sj] ];
1083 rn->route_ids[ rn->ref_count ++ ] = i;
1084
1085 if( rn->ref_count > 4 )
1086 vg_warn( "Too many references on route node %i\n", i );
1087 }
1088 }
1089
1090 world_routes_gen_meshes();
1091 }
1092
1093 /*
1094 * -----------------------------------------------------------------------------
1095 * Events
1096 * -----------------------------------------------------------------------------
1097 */
1098
1099 static int world_routes_init(void)
1100 {
1101 struct subworld_routes *r = &world.routes;
1102 r->current_run_version = 2;
1103
1104 shader_route_register();
1105 shader_routeui_register();
1106
1107 return 1;
1108 }
1109
1110 static void world_routes_free(void*_)
1111 {
1112 struct subworld_routes *r = &world.routes;
1113
1114 free( r->nodes );
1115 free( r->routes );
1116 free( r->gates );
1117 }
1118
1119 static void world_routes_update(void)
1120 {
1121 struct subworld_routes *r = &world.routes;
1122
1123 for( int i=0; i<r->route_count; i++ )
1124 {
1125 struct route *route = &r->routes[i];
1126 route->factive = vg_lerpf( route->factive, route->active, 0.01f );
1127
1128 if( route->active )
1129 {
1130 world_routes_ui_updatetime( i, vg_time - route->latest_pass );
1131 }
1132 }
1133 }
1134
1135 static void bind_terrain_textures(void);
1136 static void render_world_routes( m4x4f projection, v3f camera )
1137 {
1138 struct subworld_routes *r = &world.routes;
1139
1140 m4x3f identity_matrix;
1141 m4x3_identity( identity_matrix );
1142
1143 shader_route_use();
1144 shader_route_uTexGarbage(0);
1145 shader_link_standard_ub( _shader_route.id, 2 );
1146 bind_terrain_textures();
1147
1148 shader_route_uPv( projection );
1149 shader_route_uMdl( identity_matrix );
1150 shader_route_uCamera( camera );
1151
1152 scene_bind( &r->scene_lines );
1153
1154 for( int i=0; i<r->route_count; i++ )
1155 {
1156 struct route *route = &r->routes[i];
1157
1158 v4f colour;
1159 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
1160 colour[3] = 1.0f;
1161
1162 shader_route_uColour( colour );
1163 mdl_draw_submesh( &route->sm );
1164 }
1165 }
1166
1167 static void render_world_routes_ui(void)
1168 {
1169 glEnable(GL_BLEND);
1170 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1171 glBlendEquation(GL_FUNC_ADD);
1172
1173 struct subworld_routes *r = &world.routes;
1174
1175 float active_offset = 0.0f;
1176 for( int i=0; i<r->route_count; i++ )
1177 {
1178 struct route *route = &r->routes[i];
1179 world_routes_ui_draw( i, route->colour, active_offset );
1180 active_offset += route->factive;
1181 }
1182
1183 glDisable(GL_BLEND);
1184 }
1185
1186 #endif /* ROUTES_H */