cleaned routes
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
1 #ifndef ROUTES_H
2 #define ROUTES_H
3
4 #include "common.h"
5 #include "model.h"
6 #include "gate.h"
7
8 #include "shaders/vblend.h"
9 #include "shaders/route.h"
10
11 enum route_special_type
12 {
13 k_route_special_type_gate = 1,
14 k_route_special_type_collector = 2
15 };
16
17 struct subworld_routes
18 {
19 struct route_node
20 {
21 v3f co, right, up, h;
22 u32 next[2];
23
24 u32 special_type, special_id, current_refs, ref_count;
25 u32 route_ids[4]; /* Gates can be linked into up to four routes */
26 }
27 *nodes;
28
29 u32 node_count,
30 node_cap;
31
32 struct route
33 {
34 const char *name;
35 v4f colour;
36
37 u32 start;
38 mdl_submesh sm;
39
40 int active;
41 float factive;
42 }
43 *routes;
44
45 u32 route_count,
46 route_cap;
47
48 struct route_gate
49 {
50 teleport_gate gate;
51
52 u32 node_id;
53
54 struct route_timing
55 {
56 u32 version; /* Incremented on every teleport */
57 double time;
58 }
59 timing;
60 }
61 *gates;
62
63 struct route_collector
64 {
65 struct route_timing timing;
66 }
67 *collectors;
68
69 u32 gate_count,
70 gate_cap,
71 collector_count,
72 collector_cap;
73
74 u32 active_gate,
75 current_run_version;
76
77 scene scene_lines;
78 };
79
80 static struct subworld_routes *subworld_routes(void);
81
82 static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
83 u32 colour, float xoffset )
84 {
85 v3f p0, h0, p1, h1, l, p;
86
87 v3_copy( rna->co, p0 );
88 v3_muladds( rna->co, rna->h, 1.0f, h0 );
89 v3_copy( rnb->co, p1 );
90 v3_muladds( rnb->co, rnb->h, -1.0f, h1 );
91
92 v3_muladds( p0, rna->right, xoffset, p0 );
93 v3_muladds( h0, rna->right, xoffset, h0 );
94 v3_muladds( p1, rnb->right, xoffset, p1 );
95 v3_muladds( h1, rnb->right, xoffset, h1 );
96
97 v3_copy( p0, l );
98
99 for( int i=0; i<5; i++ )
100 {
101 float t = (float)(i+1)/5.0f;
102 eval_bezier_time( p0, p1, h0, h1, t, p );
103 vg_line( p, l, colour );
104 v3_copy( p, l );
105 }
106 }
107
108 /*
109 * Get a list of node ids in stack, and return how many there is
110 */
111 static u32 world_routes_get_path( u32 starter, u32 stack[64] )
112 {
113 struct subworld_routes *r = subworld_routes();
114 u32 stack_i[64];
115
116 stack[0] = starter;
117 stack_i[0] = 0;
118
119 u32 si = 1;
120 int loop_complete = 0;
121
122 while( si )
123 {
124 if( stack_i[si-1] == 2 )
125 {
126 si --;
127 continue;
128 }
129
130 struct route_node *rn = &r->nodes[stack[si-1]];
131 u32 nextid = rn->next[stack_i[si-1]];
132 stack_i[si-1] ++;
133
134 if( nextid != 0xffffffff )
135 {
136 if( nextid == stack[0] )
137 {
138 loop_complete = 1;
139 break;
140 }
141
142 int valid = 1;
143 for( int sj=0; sj<si; sj++ )
144 {
145 if( stack[sj] == nextid )
146 {
147 valid = 0;
148 break;
149 }
150 }
151
152 if( valid )
153 {
154 stack_i[si] = 0;
155 stack[si] = nextid;
156 si ++;
157 continue;
158 }
159 }
160 }
161
162 if( loop_complete )
163 return si;
164
165 return 0;
166 }
167
168 /*
169 * Will scan the whole run for two things;
170 * 1: we set a new record for the total, complete loop around the course
171 * 2: the time of each segment will be recorded into the data buffer
172 * (not implemented: TODO)
173 */
174 static void world_routes_verify_run( u32 route )
175 {
176 struct subworld_routes *r = subworld_routes();
177
178 u32 stack[64];
179 u32 si = world_routes_get_path( r->routes[route].start, stack );
180
181 /*
182 * we only care about gates that ref gates, so shuffle down the array
183 */
184 struct route_timing *timings[64];
185 u32 sj = 0, maxv = 0, begin = 0;
186 for( u32 i=0; i<si; i++ )
187 {
188 if( r->nodes[stack[i]].special_type == k_route_special_type_collector )
189 timings[sj ++] = &r->collectors[r->nodes[stack[i]].special_id].timing;
190 else if( r->nodes[stack[i]].special_type == k_route_special_type_gate )
191 timings[sj ++] = &r->gates[r->nodes[stack[i]].special_id].timing;
192 }
193
194 for( u32 i=0; i<sj; i++ )
195 {
196 if( timings[i]->version > maxv )
197 {
198 maxv = timings[i]->version;
199 begin = i;
200 }
201 }
202
203 vg_info( "== begin verification (%u) ==\n", route );
204 vg_info( " current version: %u\n", r->current_run_version );
205
206 int verified = 0;
207 if( timings[begin]->version == r->current_run_version )
208 verified = 1;
209
210 double lap_time = 0.0;
211
212 for( u32 i=0; i<sj; i++ )
213 {
214 u32 j = (sj+begin-i-1) % sj,
215 j1 = (j+1) % sj;
216
217 double diff = 0.0;
218
219 if( i<sj-1 )
220 {
221 /* j1v should equal jv+1 */
222 if( timings[j1]->version == timings[j]->version+1 )
223 {
224 diff = timings[j1]->time - timings[j]->time;
225 lap_time += diff;
226 }
227 else
228 verified = 0;
229 }
230
231 if( verified )
232 vg_success( " [ %u %f ] %f\n", timings[j1]->time,
233 timings[j1]->version, diff );
234 else
235 vg_warn( " [ %u %f ]\n", timings[j1]->time, timings[j1]->version );
236 }
237
238 if( verified )
239 vg_success( " NEW LAP TIME: %f\n", lap_time );
240 else
241 vg_info( " ctime: %f\n", lap_time );
242 }
243
244 /*
245 * When going through a gate this is called for bookkeeping purposes
246 */
247 static void world_routes_activate_gate( u32 id )
248 {
249 struct subworld_routes *r = subworld_routes();
250 struct route_gate *rg = &r->gates[id];
251 struct route_node *pnode = &r->nodes[rg->node_id],
252 *pdest = &r->nodes[pnode->next[0]];
253
254 struct route_collector *rc = &r->collectors[ pdest->special_id ];
255
256 r->active_gate = id;
257 rg->timing.version = r->current_run_version;
258 rg->timing.time = vg_time;
259 for( u32 i=0; i<r->route_count; i++ )
260 {
261 struct route *route = &r->routes[i];
262
263 route->active = 0;
264 for( u32 j=0; j<pdest->ref_count; j++ )
265 {
266 if( pdest->route_ids[j] == i )
267 {
268 world_routes_verify_run( i );
269 route->active = 1;
270 break;
271 }
272 }
273 }
274
275 r->current_run_version ++;
276
277 rc->timing.version = r->current_run_version;
278 rc->timing.time = vg_time;
279 r->current_run_version ++;
280 }
281
282 static void world_routes_debug(void)
283 {
284 struct subworld_routes *r = subworld_routes();
285
286 for( int i=0; i<r->node_count; i++ )
287 {
288 struct route_node *rn = &r->nodes[i];
289 vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
290 }
291
292 for( int i=0; i<r->route_count; i++ )
293 {
294 struct route *route = &r->routes[i];
295
296 u32 stack[64];
297 u32 si = world_routes_get_path( route->start, stack );
298
299 u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
300 0xff5442f5 };
301
302 u32 cc = colours[i%vg_list_size(colours)];
303
304 for( int sj=0; sj<si; sj++ )
305 {
306 int sk = (sj+1)%si;
307 debug_sbpath( &r->nodes[stack[sj]], &r->nodes[stack[sk]], cc,
308 (float)i );
309 }
310 }
311
312 for( int i=0; i<r->node_count; i++ )
313 {
314 struct route_node *ri = &r->nodes[i],
315 *rj = NULL;
316
317 for( int j=0; j<2; j++ )
318 {
319 if( ri->next[j] != 0xffffffff )
320 {
321 rj = &r->nodes[ri->next[j]];
322 vg_line( ri->co, rj->co, 0x20ffffff );
323 }
324 }
325 }
326 }
327
328 static void world_routes_free(void)
329 {
330 struct subworld_routes *r = subworld_routes();
331
332 free( r->nodes );
333 free( r->routes );
334 free( r->gates );
335 }
336
337 static void world_id_fixup( u32 *uid, mdl_header *mdl )
338 {
339 if( *uid )
340 *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
341 else
342 *uid = 0xffffffff;
343 }
344
345 /*
346 * Create the strips of colour that run through the world along course paths
347 */
348 static void world_routes_gen_meshes(void)
349 {
350 struct subworld_routes *r = subworld_routes();
351 scene_init( &r->scene_lines );
352
353 for( int i=0; i<r->route_count; i++ )
354 {
355 struct route *route = &r->routes[i];
356
357 u32 stack[64];
358 u32 si = world_routes_get_path( route->start, stack );
359
360 u32 last_valid = 0;
361
362 for( int sj=0; sj<si; sj++ )
363 {
364 int sk=(sj+1)%si;
365
366 struct route_node *rnj = &r->nodes[ stack[sj] ],
367 *rnk = &r->nodes[ stack[sk] ],
368 *rnl;
369
370 if( rnj->special_type && rnk->special_type )
371 {
372 last_valid = 0;
373 continue;
374 }
375
376 float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
377 base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
378
379 if( rnk->special_type )
380 {
381 rnl = &r->nodes[ rnk->next[0] ];
382 base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
383 }
384
385 if( sk == 0 )
386 {
387 base_x1 -= 1.0f;
388 }
389
390 v3f p0, h0, p1, h1, p, pd;
391
392 v3_copy( rnj->co, p0 );
393 v3_muladds( rnj->co, rnj->h, 1.0f, h0 );
394 v3_copy( rnk->co, p1 );
395 v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
396
397 float t=0.0f;
398 int it = 0;
399
400 for( int it=0; it<256; it ++ )
401 {
402 float const k_sample_dist = 0.02f;
403 eval_bezier_time( p0,p1,h0,h1, t,p );
404 eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
405
406 float mod = k_sample_dist / v3_dist( p, pd );
407
408 v3f v0,up, right;
409 v3_muls( rnj->up, 1.0f-t, up );
410 v3_muladds( up, rnk->up, t, up );
411
412 v3_sub( pd,p,v0 );
413 v3_cross( up, v0, right );
414 v3_normalize( right );
415
416 float cur_x = (1.0f-t)*base_x0 + t*base_x1;
417
418 v3f sc, sa, sb, down;
419 v3_muladds( p, right, cur_x, sc );
420 v3_muladds( sc, up, 1.5f, sc );
421 v3_muladds( sc, right, 0.45f, sa );
422 v3_muladds( sc, right, -0.45f, sb );
423 v3_muls( up, -1.0f, down );
424
425 ray_hit ha, hb;
426 ha.dist = 8.0f;
427 hb.dist = 8.0f;
428 if(ray_world( sa, down, &ha ) &&
429 ray_world( sb, down, &hb ))
430 {
431 mdl_vert va, vb;
432
433 v3_muladds( ha.pos, up, 0.06f, va.co );
434 v3_muladds( hb.pos, up, 0.06f, vb.co );
435 v3_copy( up, va.norm );
436 v3_copy( up, vb.norm );
437 v3_zero( va.colour );
438 v3_zero( vb.colour );
439 v2_zero( va.uv );
440 v2_zero( vb.uv );
441
442 scene_push_vert( &r->scene_lines, &va );
443 scene_push_vert( &r->scene_lines, &vb );
444
445 if( last_valid )
446 {
447 /* Connect them with triangles */
448 scene_push_tri( &r->scene_lines, (u32[3]){
449 last_valid+0-2, last_valid+1-2, last_valid+2-2} );
450 scene_push_tri( &r->scene_lines, (u32[3]){
451 last_valid+1-2, last_valid+3-2, last_valid+2-2} );
452 }
453
454 last_valid = r->scene_lines.vertex_count;
455 }
456 else
457 last_valid = 0;
458
459 t += 1.0f*mod;
460
461 if( t >= 1.0f )
462 {
463 /* TODO special case for end of loop, need to add triangles
464 * between first and last rungs */
465 break;
466 }
467 }
468
469 rnj->current_refs ++;
470 }
471
472 scene_copy_slice( &r->scene_lines, &route->sm );
473 }
474
475 scene_upload( &r->scene_lines );
476 scene_free_offline_buffers( &r->scene_lines );
477 }
478
479 static void bind_terrain_textures(void);
480 static void render_world_routes( m4x4f projection, v3f camera )
481 {
482 struct subworld_routes *r = subworld_routes();
483
484 m4x3f identity_matrix;
485 m4x3_identity( identity_matrix );
486
487 shader_route_use();
488 shader_route_uTexGarbage(0);
489 shader_link_standard_ub( _shader_route.id, 2 );
490 bind_terrain_textures();
491
492 shader_route_uPv( projection );
493 shader_route_uMdl( identity_matrix );
494 shader_route_uCamera( camera );
495
496 scene_bind( &r->scene_lines );
497
498 for( int i=0; i<r->route_count; i++ )
499 {
500 struct route *route = &r->routes[i];
501 route->factive = vg_lerpf( route->factive, route->active, 0.01f );
502
503 v4f colour;
504 v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
505 colour[3] = 1.0f;
506
507 shader_route_uColour( colour );
508 mdl_draw_submesh( &route->sm );
509 }
510 }
511
512 static void world_routes_register(void)
513 {
514 struct subworld_routes *r = subworld_routes();
515 r->current_run_version = 2;
516
517 shader_route_register();
518 }
519
520 static void world_routes_loadfrom( mdl_header *mdl )
521 {
522 struct subworld_routes *r = subworld_routes();
523 r->nodes = NULL;
524 r->node_count = 0;
525 r->node_cap = 0;
526 r->routes = NULL;
527 r->route_count = 0;
528 r->route_cap = 0;
529 r->gates = NULL;
530 r->gate_count = 0;
531 r->gate_cap = 0;
532
533 for( int i=0; i<mdl->node_count; i++ )
534 {
535 mdl_node *pnode = mdl_node_from_id(mdl,i);
536 m4x3f transform;
537
538 if( pnode->classtype == k_classtype_route_node ||
539 pnode->classtype == k_classtype_gate )
540 {
541 mdl_node_transform( pnode, transform );
542 pnode->sub_uid = r->node_count;
543
544 r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
545 sizeof( struct route_node ) );
546
547 struct route_node *rn = &r->nodes[r->node_count];
548
549 v3_copy( transform[0], rn->right );
550 v3_normalize( rn->right );
551 v3_copy( transform[1], rn->up );
552 v3_normalize( rn->up );
553 v3_muls( transform[2], -1.0f, rn->h );
554 v3_copy( transform[3], rn->co );
555 rn->ref_count = 0;
556 rn->current_refs = 0;
557 rn->special_type = 0;
558 rn->special_id = 0;
559
560 if( pnode->classtype == k_classtype_gate )
561 {
562 struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
563
564 /* H is later scaled based on link distance */
565 v3_normalize( rn->h );
566 rn->next[0] = inf->target;
567 rn->next[1] = 0;
568
569 /* TODO */
570 if( inf->target )
571 {
572 mdl_node *pother = mdl_node_from_id( mdl, inf->target );
573
574 if( pother->classtype == k_classtype_gate )
575 {
576 r->gates = buffer_reserve( r->gates, r->gate_count,
577 &r->gate_cap,
578 1, sizeof( struct route_gate ) );
579
580 struct route_gate *rg = &r->gates[r->gate_count];
581 rg->node_id = r->node_count;
582 rg->timing.time = 0.0;
583 rg->timing.version = 0;
584
585 v3_copy( pnode->co, rg->gate.co[0] );
586 v3_copy( pother->co, rg->gate.co[1] );
587 v4_copy( pnode->q, rg->gate.q[0] );
588 v4_copy( pother->q, rg->gate.q[1] );
589 v2_copy( inf->dims, rg->gate.dims );
590
591 gate_transform_update( &rg->gate );
592 rn->special_type = k_route_special_type_gate;
593 rn->special_id = r->gate_count;
594
595 r->gate_count ++;
596 }
597 }
598
599 if( rn->special_type == 0 )
600 {
601 r->collectors = buffer_reserve(
602 r->collectors, r->collector_count, &r->collector_cap,
603 1, sizeof( struct route_collector ));
604
605 struct route_collector *rc = &r->collectors[r->collector_count];
606 rc->timing.time = 0.0;
607 rc->timing.version = 0;
608
609 rn->special_type = k_route_special_type_collector;
610 rn->special_id = r->collector_count;
611
612 r->collector_count ++;
613 }
614 }
615 else
616 {
617 struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
618 rn->next[0] = inf->target;
619 rn->next[1] = inf->target1;
620 }
621
622 r->node_count ++;
623 }
624 else if( pnode->classtype == k_classtype_route )
625 {
626 struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
627 r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
628 1, sizeof( struct route ) );
629
630 struct route *route = &r->routes[r->route_count];
631
632 v3_copy( inf->colour, route->colour );
633 route->colour[3] = 1.0f;
634
635 route->name = NULL;
636 route->start = inf->id_start;
637 route->active = 0;
638 route->factive = 0.0f;
639
640 r->route_count ++;
641 }
642 }
643
644 /*
645 * Apply correct system-local ids
646 */
647 for( int i=0; i<r->node_count; i++ )
648 {
649 struct route_node *rn = &r->nodes[i];
650
651 for( int j=0; j<2; j++ )
652 world_id_fixup( &rn->next[j], mdl );
653 }
654
655 for( int i=0; i<r->route_count; i++ )
656 {
657 struct route *route = &r->routes[i];
658 world_id_fixup( &route->start, mdl );
659 }
660
661 /*
662 * Gather references
663 */
664 for( int i=0; i<r->route_count; i++ )
665 {
666 struct route *route = &r->routes[i];
667
668 u32 stack[64];
669 u32 si = world_routes_get_path( route->start, stack );
670
671 for( int sj=0; sj<si; sj++ )
672 {
673 struct route_node *rn = &r->nodes[ stack[sj] ];
674 rn->route_ids[ rn->ref_count ++ ] = i;
675
676 if( rn->ref_count > 4 )
677 vg_warn( "Too many references on route node %i\n", i );
678 }
679 }
680
681 world_routes_gen_meshes();
682 }
683
684 #endif /* ROUTES_H */