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