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