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