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