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