now we're doing a bunch of them
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
index 429c0fb1cdad6805cb519c4a9b362d9d6715d1d6..bd4ce0cf803fa91ac97eec10acfa429e31b1233d 100644 (file)
+/*
+ * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
+ */
+
 #ifndef ROUTES_H
 #define ROUTES_H
 
-#include "common.h"
-#include "model.h"
-#include "gate.h"
+#include <time.h>
+#include "world.h"
+#include "world_gate.h"
+
+#if 0
+#include "shaders/vblend.h"
+#endif
+
+#include "shaders/scene_route.h"
+#include "shaders/routeui.h"
+
+
+enum route_special_type
+{
+   k_route_special_type_none = 0,
+   k_route_special_type_gate = 1,
+   k_route_special_type_collector = 2
+};
+
+VG_STATIC void debug_sbpath( struct route_node *rna, struct route_node *rnb,
+                          u32 colour, float xoffset )
+{
+   v3f p0, h0, p1, h1, l, p;
+   
+   v3_copy( rna->co, p0 );
+   v3_muladds( rna->co, rna->h,  1.0f, h0 );
+   v3_copy( rnb->co, p1 );
+   v3_muladds( rnb->co, rnb->h, -1.0f, h1 );
+
+   v3_muladds( p0, rna->right, xoffset, p0 );
+   v3_muladds( h0, rna->right, xoffset, h0 );
+   v3_muladds( p1, rnb->right, xoffset, p1 );
+   v3_muladds( h1, rnb->right, xoffset, h1 );
+
+   v3_copy( p0, l );
+
+   for( int i=0; i<5; i++ )
+   {
+      float t = (float)(i+1)/5.0f;
+      eval_bezier_time( p0, p1, h0, h1, t, p );
+      vg_line( p, l, colour );
+      v3_copy( p, l );
+   }
+}
 
-struct subworld_routes
+/*
+ * Get a list of node ids in stack, and return how many there is
+ */
+VG_STATIC u32 world_routes_get_path( world_instance *world,
+                                     u32 starter, u32 stack[64] )
 {
-   struct route_node
+   u32 stack_i[64];
+
+   stack[0] = starter;
+   stack_i[0] = 0;
+
+   u32 si = 1;
+   int loop_complete = 0;
+
+   while( si )
    {
-      v3f co, right, h;
-      u32 next[2];
+      if( stack_i[si-1] == 2 )
+      {
+         si --;
+         continue;
+      }
+
+      struct route_node *rn = &world->nodes[stack[si-1]];
+      u32 nextid = rn->next[stack_i[si-1]];
+      stack_i[si-1] ++;
+
+      if( nextid != 0xffffffff )
+      {
+         if( nextid == stack[0] )
+         {
+            loop_complete = 1;
+            break;
+         }
+
+         int valid = 1;
+         for( int sj=0; sj<si; sj++ )
+         {
+            if( stack[sj] == nextid )
+            {
+               valid = 0;
+               break;
+            }
+         }
 
-      u32 is_gate, gate_id;
+         if( valid )
+         {
+            stack_i[si] = 0;
+            stack[si] = nextid;
+            si ++;
+            continue;
+         }
+      }
    }
-   *nodes;
 
-   u32 node_count,
-       node_cap;
+   if( loop_complete )
+      return si;
 
-   struct route
+   return 0;
+}
+
+/*
+ * Free a segment from the UI bar to be reused later
+ */
+VG_STATIC void world_routes_ui_popfirst( struct route_ui_bar *pui )
+{
+   if( pui->segment_count )
    {
-      const char *name;
-      v4f colour;
+      pui->segment_start ++;
+
+      if( pui->segment_start == 32 )
+         pui->segment_start = 0;
 
-      u32 start;
+      pui->segment_count --;
    }
-   *routes;
+}
 
-   u32 route_count,
-       route_cap;
+/*
+ * Reset ui bar completely
+ */
+VG_STATIC void world_routes_ui_clear( struct route_ui_bar *pui )
+{
+   pui->segment_start = (pui->segment_start + pui->segment_count) %
+                                                            k_max_ui_segments;
+   pui->segment_count = 0;
+}
 
-   struct route_gate
+/*
+ * Break a index range into two pieces over the edge of the maximum it can
+ * store. s1 is 0 always, so its a ring buffer.
+ */
+VG_STATIC void world_routes_ui_split_indices( u32 s0, u32 count, 
+                                              u32 *c0, u32 *c1 )
+{
+   *c0 = (VG_MIN( s0+count, k_route_ui_max_indices )) - s0;
+   *c1 = count-(*c0);
+}
+
+/*
+ * Place a set of indices into gpu array automatically splits
+ * across bounds
+ */
+VG_STATIC void world_routes_ui_set_indices( struct route_ui_bar *pui, 
+                                         u16 *indices, u32 count )
+{
+   u32 c0, c1;
+   world_routes_ui_split_indices( pui->indices_head, count, &c0, &c1 );
+
+   glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pui->ebo );
+
+   if( c0 )
    {
-      teleport_gate gate;
-      
-      u32 route_ids[4];    /* Gates can be linked into up to four routes */
-      u32 route_count,
-          node_id;
+      glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, pui->indices_head*sizeof(u16),
+            c0*sizeof(u16), indices );
    }
-   *gates;
 
-   u32 gate_count,
-       gate_cap;
-};
+   if( c1 )
+   {
+      glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, c1*sizeof(u16), indices+c0 );
+      pui->indices_head = c1;
+   }
+   else
+      pui->indices_head += c0;
+}
+
+/*
+ * Place a set of vertices into gpu array 
+ */
+VG_STATIC u32 world_routes_ui_set_verts( struct route_ui_bar *pui,
+                                      v2f *verts, u32 count )
+{
+   if( pui->vertex_head + count >= k_route_ui_max_verts )
+      pui->vertex_head = 0;
+
+   u32 vert_start = pui->vertex_head;
+   pui->vertex_head += count;
+
+   glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
+   glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
+                        sizeof(v2f)*count, verts );
+
+   return vert_start;
+}
+
+/*
+ * Update the last (count) vertices positions, does not add any.
+ * Data must already be written to, and not cross either array boundaries.
+ */
+VG_STATIC u32 world_routes_ui_update_verts( struct route_ui_bar *pui,
+                                         v2f *verts, u32 count )
+{
+   u32 vert_start = pui->vertex_head-count;
+
+   glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
+   glBufferSubData( GL_ARRAY_BUFFER, (GLintptr)(vert_start*sizeof(v2f)),
+                        sizeof(v2f)*count, verts );
 
-static struct subworld_routes *subworld_routes(void);
+   return vert_start;
+}
 
 /* 
- * TODO list:
- *      when a gate is passed through it needs to trigger into an active state
+ * Current/active segment of this UI bar 
+ */
+VG_STATIC struct route_ui_segment *world_routes_ui_curseg( 
+      struct route_ui_bar *pui )
+{
+   u32 index = (pui->segment_start+pui->segment_count-1)%k_max_ui_segments;
+   return &pui->segments[ index ];
+}
+
+/*
+ * Start a new segment in the UI bar, will create a split on the last one if
+ * there is one active currently. (api)
+ */
+VG_STATIC void world_routes_ui_newseg( u32 route )
+{
+   struct route_ui_bar *pui = &world_global.ui_bars[route];
+   
+   glBindVertexArray( pui->vao );
+   if( pui->segment_count )
+   {
+      float const k_gap_width = 1.0f;
+
+      struct route_ui_segment *cseg = world_routes_ui_curseg( pui );
+      
+      v2f verts[2];
+      verts[0][0] =  cseg->length-k_gap_width;
+      verts[0][1] =  0.5f;
+      verts[1][0] =  cseg->length-k_gap_width;
+      verts[1][1] = -0.5f;
+
+      world_routes_ui_update_verts( pui, verts, 2 );
+   }
+
+   pui->segment_count ++;
+   struct route_ui_segment *segment = world_routes_ui_curseg( pui );
+   
+   v2f verts[4];
+   verts[0][0] =  0.0f;
+   verts[0][1] =  0.5f;
+   verts[1][0] =  0.0f;
+   verts[1][1] = -0.5f;
+   verts[2][0] =  0.0f;
+   verts[2][1] =  0.5f;
+   verts[3][0] =  0.0f;
+   verts[3][1] = -0.5f;
+
+   u32 vert_start = world_routes_ui_set_verts( pui, verts, 4 );
+
+   u16 indices[6];
+   indices[0] = vert_start + 0;
+   indices[1] = vert_start + 1;
+   indices[2] = vert_start + 3;
+   indices[3] = vert_start + 0;
+   indices[4] = vert_start + 3;
+   indices[5] = vert_start + 2;
+
+   segment->vertex_start = vert_start;
+   segment->vertex_count = 4;
+   segment->index_start  = pui->indices_head;
+   segment->index_count  = 6;
+   segment->notches      = 0;
+
+   world_routes_ui_set_indices( pui, indices, 6 );
+}
+
+/*
+ * Extend the end of the bar 
  */
+VG_STATIC void world_routes_ui_updatetime( u32 route, float time )
+{
+   struct route_ui_bar *pui = &world_global.ui_bars[route];
+
+   v2f verts[2];
+   verts[0][0] =  time;
+   verts[0][1] =  0.5f;
+   verts[1][0] =  time;
+   verts[1][1] = -0.5f;
+
+   u32 vert_start = pui->vertex_head-2;
+
+   glBindVertexArray( pui->vao );
+   world_routes_ui_update_verts( pui, verts, 2 );
+
+   struct route_ui_segment *cseg = world_routes_ui_curseg( pui );
+   cseg->length = time;
+}
 
-static void world_routes_debug(void)
+VG_STATIC void world_routes_ui_draw_segment( struct route_ui_segment *segment )
 {
-   struct subworld_routes *r = subworld_routes();
+   u32 c0, c1;
+   world_routes_ui_split_indices( segment->index_start, 
+                                  segment->index_count, &c0, &c1 );
+   if( c0 )
+      glDrawElements( GL_TRIANGLES, c0, GL_UNSIGNED_SHORT,
+                        (void *)(segment->index_start*sizeof(u16)));
+   if( c1 )
+      glDrawElements( GL_TRIANGLES, c1, GL_UNSIGNED_SHORT, (void *)(0) );
+}
+
+/*
+ * Draws full bar at Y offset(offset).
+ */
+VG_STATIC void world_routes_ui_draw( world_instance *world,
+                                     u32 route, v4f colour, float offset )
+{
+   float const k_bar_height = 0.05f,
+               k_bar_scale_x = 0.005f;
+
+   /* FIXME(10) ID mishmatch */
+   struct route *pr = &world->routes[route];
+   struct route_ui_bar *pui = &world_global.ui_bars[route];
+
+   float cx = pui->xpos;
 
-   for( int i=0; i<r->node_count; i++ )
+   shader_routeui_use();
+   glBindVertexArray( pui->vao );
+
+   float fade_amt = world_global.time - pui->fade_timer_start;
+   fade_amt = vg_clampf( fade_amt / 1.0f, 0.0f, 1.0f );
+   
+   float fade_block_size = 0.0f,
+         main_block_size = 0.0f;
+
+   for( u32 i=0; i<pui->fade_count; i++ )
    {
-      struct route_node *rn = &r->nodes[i];
-      vg_line_pt3( rn->co, 1.0f, rn->is_gate? 0xffffff00: 0xff00b2ff );
+      u32 j = (pui->fade_start + i) % k_max_ui_segments;
+      struct route_ui_segment *segment = &pui->segments[j];
+
+      fade_block_size += segment->length;
    }
+
+   cx -= fade_block_size * fade_amt;
+
+   v4f fade_colour;
+   v4_copy( colour, fade_colour );
+   fade_colour[3] *= 1.0f-fade_amt;
+
+   /* 1 minute timer */
+   float timer_delta = (world_global.time - world_global.last_use) * (1.0/45.0),
+         timer_scale = 1.0f - vg_minf( timer_delta, 1.0f );
+
+   /* 
+    * Draw fadeout bar
+    */
+
+   float height = pr->factive*k_bar_height * timer_scale,
+         base = -1.0f + (offset+0.5f)*k_bar_height * timer_scale;
+
+   shader_routeui_uColour( fade_colour );
+   for( u32 i=0; i<pui->fade_count; i++ )
+   {
+      u32 j = (pui->fade_start + i) % k_max_ui_segments;
+      struct route_ui_segment *segment = &pui->segments[j];
+
+      shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
+                                     k_bar_scale_x, height } );
+
+      world_routes_ui_draw_segment( segment );
+      cx += segment->length;
+   }
+
+   /*
+    * Draw main bar 
+    */
+   shader_routeui_uColour( colour );
+   for( u32 i=0; i<pui->segment_count; i++ )
+   {
+      u32 j = (pui->segment_start + i) % k_max_ui_segments;
+      struct route_ui_segment *segment = &pui->segments[j];
+
+      shader_routeui_uOffset( (v4f){ cx*k_bar_scale_x, base,
+                                     k_bar_scale_x, height } );
+
+      world_routes_ui_draw_segment( segment );
+      cx += segment->length;
+
+      main_block_size += segment->length;
+   }
+
+   pui->xpos = vg_lerpf( pui->xpos, -main_block_size * 0.5f, 0.03f );
 }
 
-static void world_routes_free(void)
+VG_STATIC void world_routes_local_set_record( world_instance *world,
+                                              u32 route, double lap_time )
 {
-   struct subworld_routes *r = subworld_routes();
+   vg_success( "  NEW LAP TIME: %f\n", lap_time );
+
+   /* FIXME(10): ID mishmatch */
+   struct route *pr = &world->routes[route];
+
+   if( pr->track_id != 0xffffffff )
+   {
+      double time_centiseconds = lap_time * 100.0;
+      if( time_centiseconds > (float)0xfffe )
+         return;
 
-   free( r->nodes );
-   free( r->routes );
-   free( r->gates );
+      highscore_record temp;
+      temp.trackid  = pr->track_id;
+      temp.datetime = time(NULL);
+      temp.playerid = 0;
+      temp.points   = 0;
+      temp.time     = time_centiseconds;
+
+      highscores_push_record( &temp );
+
+      struct track_info *pti = &track_infos[ pr->track_id ];
+      pti->push = 1;
+      
+      if( pti->achievement_id )
+      {
+         steam_set_achievement( pti->achievement_id );
+         steam_store_achievements();
+      }
+   }
+   else
+   {
+      vg_warn( "There is no associated track for this record...\n" );
+   }
 }
 
-static void world_id_fixup( u32 *uid, mdl_header *mdl )
+/* 
+ * Will scan the whole run for two things;
+ *   1: we set a new record for the total, complete loop around the course
+ *   2: the time of each segment will be recorded into the data buffer
+ *       (not implemented: TODO)
+ */
+VG_STATIC void world_routes_verify_run( world_instance *world, u32 route )
 {
-   if( *uid )
-      *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
+   /* FIXME(10): ID mishmatch */
+   struct route *pr = &world->routes[route];
+   struct route_ui_bar *pui = &world_global.ui_bars[route];
+
+   u32 stack[64];
+   u32 si = world_routes_get_path( world, world->routes[route].start, stack );
+
+   /* 
+    * we only care about gates that ref gates, so shuffle down the array
+    */
+   struct route_timing *timings[64];
+   u32 sj = 0, maxv = 0, begin = 0;
+   for( u32 i=0; i<si; i++ )
+   {
+      struct route_node *inode = &world->nodes[stack[i]];
+
+      if( inode->special_type == k_route_special_type_collector )
+      {
+         timings[sj ++] = &world->collectors[ inode->special_id ].timing;
+      }
+      else if( inode->special_type == k_route_special_type_gate )
+      {
+         timings[sj ++] = &world->gates[inode->special_id].timing;
+      }
+   }
+   
+   for( u32 i=0; i<sj; i++ )
+   {
+      if( timings[i]->version > maxv )
+      {
+         maxv = timings[i]->version;
+         begin = i;
+      }
+   }
+
+   vg_info( "== begin verification (%u) ==\n", route );
+   vg_info( "  current version: %u\n", world_global.current_run_version );
+
+   int verified = 0;
+   if( timings[begin]->version == world_global.current_run_version )
+      verified = 1;
+
+   int valid_segment_count = 0;
+
+   double lap_time = 0.0;
+
+   for( u32 i=0; i<sj; i++ )
+   {
+      u32  j = (sj+begin-i-1) % sj,
+          j1 = (j+1) % sj;
+
+      double diff = 0.0;
+   
+      if( i<sj-1 )
+      {
+         /* j1v should equal jv+1 */
+         if( timings[j1]->version == timings[j]->version+1 )
+         {
+            diff = timings[j1]->time - timings[j]->time;
+            lap_time += diff;
+
+            if( verified && diff > 0.0 ) valid_segment_count ++;
+         }
+         else
+            verified = 0;
+      }
+      
+      if( verified )
+         vg_success( " [ %u %f ] %f\n", timings[j1]->time, 
+                                        timings[j1]->version, diff );
+      else
+         vg_warn( " [ %u %f ]\n", timings[j1]->time, timings[j1]->version );
+   }
+
+   pui->fade_start = pui->segment_start;
+   pui->fade_count = 0;
+   pui->fade_timer_start = world_global.time;
+
+   int orig_seg_count = pui->segment_count;
+
+   world_routes_ui_newseg( route );
+
+   if( verified )
+   {
+      world_routes_local_set_record( world, route, lap_time );
+      world_routes_ui_popfirst( pui );
+      pui->fade_count ++;
+   }
+   else
+      vg_info( "  ctime: %f\n", lap_time );
+
+   /* remove any excess we had from previous runs */
+   int to_remove = orig_seg_count-valid_segment_count;
+   for( int i=0; i<to_remove; i++ )
+   {
+      world_routes_ui_popfirst( pui );
+      pui->fade_count ++;
+   }
+
+   world->routes[route].latest_pass = world_global.time;
 }
 
-static void world_routes_init( mdl_header *mdl )
+VG_STATIC void world_routes_clear( world_instance *world )
+{
+   for( u32 i=0; i<world->route_count; i++ )
+   {
+      struct route *route = &world->routes[i];
+      route->active = 0;
+   }
+   world_global.current_run_version += 4;
+   world_global.last_use = 0.0;
+}
+
+/*
+ * When going through a gate this is called for bookkeeping purposes
+ */
+VG_STATIC void world_routes_activate_gate( world_instance *world, u32 id )
 {
-   struct subworld_routes *r = subworld_routes();
-   r->nodes = NULL;
-   r->node_count = 0;
-   r->node_cap = 0;
-   r->routes = NULL;
-   r->route_count = 0;
-   r->route_cap = 0;
-   r->gates = NULL;
-   r->gate_count = 0;
-   r->gate_cap = 0;
+   struct route_gate *rg = &world->gates[id];
+   struct route_node *pnode = &world->nodes[rg->node_id],
+                     *pdest = &world->nodes[pnode->next[0]];
 
-   for( int i=0; i<mdl->node_count; i++ )
+   world_global.last_use = world_global.time;
+
+   struct route_collector *rc = &world->collectors[ pdest->special_id ];
+
+   world_global.active_gate = id;
+   rg->timing.version = world_global.current_run_version;
+   rg->timing.time = world_global.time;
+
+   for( u32 i=0; i<world->route_count; i++ )
    {
-      mdl_node *pnode = mdl_node_from_id(mdl,i);
-      m4x3f transform;
+      struct route *route = &world->routes[i];
+      
+      int was_active = route->active;
 
-      if( pnode->classtype == k_classtype_route_node ||
-          pnode->classtype == k_classtype_gate )
+      route->active = 0;
+      for( u32 j=0; j<pdest->ref_count; j++ )
       {
-         mdl_node_transform( pnode, transform );
-         pnode->sub_uid = r->node_count;
-         
-         r->nodes = buffer_reserve( r->nodes, r->node_count, &r->node_cap, 1,
-                                    sizeof( struct route_node ) );
+         if( pdest->route_ids[j] == i )
+         {
+            world_routes_verify_run( world, i );
+            route->active = 1;
+            break;
+         }
+      }
 
-         struct route_node *rn = &r->nodes[r->node_count];
+      if( was_active && !route->active )
+      {
+         struct route_ui_bar *pui = &world_global.ui_bars[i];
+         pui->fade_start = pui->segment_start;
+         pui->fade_count = pui->segment_count;
+         pui->fade_timer_start = world_global.time;
 
-         v3_copy( transform[0], rn->right );
-         v3_normalize( rn->right );
-         v3_copy( transform[2], rn->h );
-         v3_copy( transform[3], rn->co );
+         world_routes_ui_clear( pui );
+         vg_success( "CLEARING -> %u %u \n", pui->fade_start,
+                                             pui->fade_count );
+      }
+   }
+   
+   world_global.current_run_version ++;
 
-         if( pnode->classtype == k_classtype_gate )
+   rc->timing.version = world_global.current_run_version;
+   rc->timing.time = world_global.time;
+   world_global.current_run_version ++;
+}
+
+/*
+ * Notify the UI system that we've reset the player
+ */
+VG_STATIC void world_routes_notify_reset(void)
+{
+   world_global.rewind_from = world_global.time;
+   world_global.rewind_to = world_global.last_use;
+}
+
+/* Rewind between the saved points in time */
+VG_STATIC void world_routes_rollback_time( double t )
+{
+   world_global.time = vg_lerp( world_global.rewind_to, 
+                                world_global.rewind_from, t );
+}
+
+/* draw lines along the paths */
+VG_STATIC void world_routes_debug( world_instance *world )
+{
+   for( int i=0; i<world->node_count; i++ )
+   {
+      struct route_node *rn = &world->nodes[i];
+      vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
+   }
+
+   for( int i=0; i<world->route_count; i++ )
+   {
+      struct route *route = &world->routes[i];
+
+      u32 stack[64];
+      u32 si = world_routes_get_path( world, route->start, stack );
+
+      u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
+                        0xff5442f5 };
+
+      u32 cc = colours[i%vg_list_size(colours)];
+
+      for( int sj=0; sj<si; sj++ )
+      {
+         int sk = (sj+1)%si;
+
+         struct route_node *pj = &world->nodes[stack[sj]],
+                           *pk = &world->nodes[stack[sk]];
+         debug_sbpath( pj, pk, cc, (float)i );
+      }
+   }
+
+   for( int i=0; i<world->node_count; i++ )
+   {
+      struct route_node *ri = &world->nodes[i],
+                        *rj = NULL;
+      
+      for( int j=0; j<2; j++ )
+      {
+         if( ri->next[j] != 0xffffffff )
          {
-            r->gates = buffer_reserve( r->gates, r->gate_count, &r->gate_cap,
-                                       1, sizeof( struct route_gate ) );
+            rj = &world->nodes[ri->next[j]];
+            vg_line( ri->co, rj->co, 0x20ffffff );
+         }
+      }
+   }
+}
+
+VG_STATIC void world_routes_create_mesh( world_instance *world, u32 route_id )
+{
+   struct route *route = &world->routes[ route_id ];
+
+   u32 stack[64];
+   u32 si = world_routes_get_path( world, route->start, stack );
+
+   u32 last_valid = 0;
+
+   for( int sj=0; sj<si; sj++ )
+   {
+      int sk=(sj+1)%si;
+
+      struct route_node *rnj = &world->nodes[ stack[sj] ],
+                        *rnk = &world->nodes[ stack[sk] ],
+                        *rnl;
+      
+      if( rnj->special_type && rnk->special_type )
+      {
+         last_valid = 0;
+         continue;
+      }
+
+      float base_x0 = (float)rnj->ref_count*-0.5f + (float)rnj->current_refs,
+            base_x1 = (float)rnk->ref_count*-0.5f + (float)rnk->current_refs;
+
+      if( rnk->special_type )
+      {
+         rnl = &world->nodes[ rnk->next[0] ];
+         base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
+      }
+
+      if( sk == 0 )
+      {
+         base_x1 -= 1.0f;
+      }
+
+      v3f p0, h0, p1, h1, p, pd;
+      
+      v3_copy( rnj->co, p0 );
+      v3_muladds( rnj->co, rnj->h,  1.0f, h0 );
+      v3_copy( rnk->co, p1 );
+      v3_muladds( rnk->co, rnk->h, -1.0f, h1 );
+
+      float t=0.0f;
+      int it = 0;
+
+      for( int it=0; it<256; it ++ )
+      {
+         float const k_sample_dist = 0.02f;
+         eval_bezier_time( p0,p1,h0,h1, t,p );
+         eval_bezier_time( p0,p1,h0,h1, t+k_sample_dist,pd );
 
-            struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
+         float mod = k_sample_dist / v3_dist( p, pd );
 
-            /* H is later scaled based on link distance */
-            v3_normalize( rn->h );
-            rn->next[0] = inf->target;
-            rn->next[1] = 0;
-            rn->gate_id = r->gate_count;
-            rn->is_gate = 1;
+         v3f v0,up, right;
+         v3_muls( rnj->up, 1.0f-t, up );
+         v3_muladds( up, rnk->up, t, up );
 
-            struct route_gate *rg = &r->gates[r->gate_count];
-            rg->node_id = r->node_count;
+         v3_sub( pd,p,v0 );
+         v3_cross( up, v0, right );
+         v3_normalize( right );
+
+         float cur_x = (1.0f-t)*base_x0 + t*base_x1;
+         
+         v3f sc, sa, sb, down;
+         v3_muladds( p, right, cur_x, sc );
+         v3_muladds( sc, up, 1.5f, sc );
+         v3_muladds( sc, right,  0.45f, sa );
+         v3_muladds( sc, right, -0.45f, sb );
+         v3_muls( up, -1.0f, down );
+         
+         ray_hit ha, hb;
+         ha.dist = 8.0f;
+         hb.dist = 8.0f;
+         if( ray_world( world, sa, down, &ha ) && 
+             ray_world( world, sb, down, &hb ))
+         {
+            scene_vert va, vb;
             
-            /* TODO */
+            v3_muladds( ha.pos, up, 0.06f, va.co );
+            v3_muladds( hb.pos, up, 0.06f, vb.co );
+
+            scene_vert_pack_norm( &va, up );
+            scene_vert_pack_norm( &vb, up );
+            v2_zero( va.uv );
+            v2_zero( vb.uv );
+
+            scene_push_vert( world->scene_lines, &va );
+            scene_push_vert( world->scene_lines, &vb );
 
-            r->gate_count ++;
+            if( last_valid )
+            {
+               /* Connect them with triangles */
+               scene_push_tri( world->scene_lines, (u32[3]){ 
+                     last_valid+0-2, last_valid+1-2, last_valid+2-2} );
+               scene_push_tri( world->scene_lines, (u32[3]){ 
+                     last_valid+1-2, last_valid+3-2, last_valid+2-2} );
+            }
+            
+            last_valid = world->scene_lines->vertex_count;
          }
          else
+            last_valid = 0;
+
+         t += 1.0f*mod;
+
+         if( t >= 1.0f )
          {
-            struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
-            rn->next[0] = inf->target;
-            rn->next[1] = inf->target1;
-            rn->is_gate = 0;
+            /* TODO special case for end of loop, need to add triangles
+             *      between first and last rungs */
+            break;
+         }
+      }
+
+      rnj->current_refs ++;
+   }
+
+   scene_copy_slice( world->scene_lines, &route->sm );
+}
+
+VG_STATIC void world_scene_compute_light_clusters( world_instance *world,
+                                                   scene *sc );
+/* 
+ * Create the strips of colour that run through the world along course paths
+ */
+VG_STATIC void world_routes_generate( world_instance *world )
+{
+   vg_info( "Generating route meshes\n" );
+   world->scene_lines = scene_init( world_global.generic_heap, 200000, 300000 );
+
+   for( u32 i=0; i<world->route_count; i++ )
+      world_routes_create_mesh( world, i );
+
+   world_scene_compute_light_clusters( world, world->scene_lines );
+
+   vg_acquire_thread_sync();
+   {
+      scene_upload( world->scene_lines, &world->mesh_route_lines );
+   }
+   vg_release_thread_sync();
+   vg_linear_del( world_global.generic_heap, world->scene_lines );
+}
+
+/* determine if special type is required for this gate */
+VG_STATIC enum route_special_type world_route_node_type( world_instance *world,
+                                                         mdl_node *pnode )
+{
+   if( pnode->classtype == k_classtype_gate )
+   {
+      struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
+
+      if( inf->target )
+      {
+         mdl_node *pother = mdl_node_from_id( world->meta, inf->target );
+         
+         if( pother->classtype == k_classtype_gate )
+         {
+            return k_route_special_type_gate;
          }
+      }
+
+      return k_route_special_type_collector;
+   }
+
+   return k_route_special_type_none;
+}
+
+/* count entities and allocate correct amount of memory in advance */
+VG_STATIC void world_routes_allocate( world_instance *world )
+{
+   vg_info( "Allocating routes\n" );
+
+   /* count */
+   u32 node_count       = 0,
+       route_count      = 0,
+       gate_count       = 0,
+       collector_count  = 0;
+   
+   for( int i=0; i<world->meta->info.node_count; i++ )
+   {
+      mdl_node *pnode = mdl_node_from_id( world->meta, i );
+
+      if( pnode->classtype == k_classtype_route_node ||
+          pnode->classtype == k_classtype_gate )
+      {
+         pnode->sub_uid = node_count;
+
+         enum route_special_type type = world_route_node_type( world, pnode );
+
+         if( type == k_route_special_type_gate )
+            gate_count ++;
+         else if( type == k_route_special_type_collector )
+            collector_count ++;
 
-         r->node_count ++;
+         node_count ++;
       }
       else if( pnode->classtype == k_classtype_route )
       {
-         struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
-         r->routes = buffer_reserve( r->routes, r->route_count, &r->route_cap,
-                                     1, sizeof( struct route ) );
+         route_count ++;
+      }
+   }
+
+   /* allocate */
+   u32 node_size        = node_count      * sizeof(struct route_node),
+       route_size       = route_count     * sizeof(struct route),
+       gate_size        = gate_count      * sizeof(struct route_gate),
+       collector_size   = collector_count * sizeof(struct route_collector);
+
+   world->nodes      = vg_linear_alloc( world_global.generic_heap, node_size );
+   world->routes     = vg_linear_alloc( world_global.generic_heap, route_size );
+   world->gates      = vg_linear_alloc( world_global.generic_heap, gate_size );
+   world->collectors = vg_linear_alloc( world_global.generic_heap, 
+                                        collector_size );
+}
+
+/* create node from mdl node */
+VG_STATIC struct route_node *world_routes_create_node( world_instance *world,
+                                                       mdl_node *pnode )
+{
+   struct route_node *rn = &world->nodes[ world->node_count ++ ];
 
-         struct route *route = &r->routes[r->route_count];
+   m4x3f transform;
+   mdl_node_transform( pnode, transform );
 
-         v4_zero( route->colour );
-         route->name = NULL;
-         route->start = inf->id_start;
+   v3_copy( transform[3], rn->co );
+   v3_copy( transform[0], rn->right );
+   v3_copy( transform[1], rn->up );
+   v3_muls( transform[2], -1.0f, rn->h );
+   v3_normalize( rn->right );
+   v3_normalize( rn->up );
 
-         r->route_count ++;
+   rn->next[0] = 0xffffffff;
+   rn->next[1] = 0xffffffff;
+
+   rn->special_type = 0;
+   rn->special_id = 0;
+   rn->current_refs = 0;
+   rn->ref_count = 0;
+
+   return rn;
+}
+
+/* retrieve the correct node id from mdl subuid */
+VG_STATIC u32 world_routes_get_subuid( world_instance *world, u32 target )
+{
+   if( target == 0 )
+      return 0xffffffff;
+   else
+      return mdl_node_from_id( world->meta, target )->sub_uid;
+}
+
+#if 0
+VG_STATIC void world_id_fixup( u32 *uid, mdl_context *mdl )
+{
+   if( *uid )
+      *uid = mdl_node_from_id( mdl, *uid )->sub_uid;
+   else
+      *uid = 0xffffffff;
+}
+#endif
+
+/* process gate attachement onto node */
+VG_STATIC void world_routes_process_gate( world_instance *world,
+                                          struct route_node *rn, 
+                                          mdl_node *pnode )
+{
+   struct classtype_gate *inf = mdl_get_entdata( world->meta, pnode );
+
+   /* H is later scaled based on link distance */
+   v3_normalize( rn->h );
+
+   rn->next[0] = world_routes_get_subuid( world, inf->target );
+   rn->next[1] = 0xffffffff;
+   rn->special_type = world_route_node_type( world, pnode );
+
+   /* process gate type */
+   if( rn->special_type == k_route_special_type_gate )
+   {
+      mdl_node *pother = mdl_node_from_id( world->meta, inf->target );
+      
+      struct route_gate *rg = &world->gates[ world->gate_count ];
+
+      rg->node_id = world->node_count-1;
+      rg->timing.time = 0.0;
+      rg->timing.version = 0;
+
+      v3_copy( pnode->co,  rg->gate.co[0] );
+      v3_copy( pother->co, rg->gate.co[1] );
+      v4_copy( pnode->q,   rg->gate.q[0] );
+      v4_copy( pother->q,  rg->gate.q[1] );
+      v2_copy( inf->dims,  rg->gate.dims );
+
+      gate_transform_update( &rg->gate );
+      rn->special_id = world->gate_count;
+
+      world->gate_count ++;
+   }
+
+   /* process collector type */
+   else if( rn->special_type == k_route_special_type_collector )
+   {
+      struct route_collector *rc = 
+         &world->collectors[ world->collector_count ];
+
+      rc->timing.time = 0.0;
+      rc->timing.version = 0;
+
+      rn->special_id = world->collector_count;
+      world->collector_count ++;
+   }
+   else
+      vg_fatal_exit_loop( "Invalid state" );
+}
+
+/* create route from node description */
+VG_STATIC void world_routes_create_route( world_instance *world,
+                                          mdl_node *pnode )
+{
+   mdl_context *mdl = world->meta;
+
+   struct classtype_route *inf = mdl_get_entdata( mdl, pnode );
+   struct route *route = &world->routes[ world->route_count ];
+   memset( route, 0, sizeof(struct route) );
+
+   v3_copy( inf->colour, route->colour );
+   route->colour[3] = 1.0f;
+   route->track_id = 0xffffffff;
+
+   for( u32 j=0; j<vg_list_size(track_infos); j++ )
+   {
+      if( !strcmp( mdl_pstr(mdl,pnode->pstr_name), track_infos[j].name ))
+      {
+         route->track_id = j;
+         break;
       }
    }
 
-   /* 
-    * Apply correct system-local ids
+   route->start = world_routes_get_subuid( world, inf->id_start );
+   route->active = 0;
+   route->factive = 0.0f;
+   mdl_node_transform( pnode, route->scoreboard_transform );
+
+   struct route_ui_bar *pui = &world_global.ui_bars[ world->route_count ];
+   pui->indices_head = k_route_ui_max_indices - 9;
+   pui->vertex_head = k_route_ui_max_verts - 200;
+   pui->segment_start = 0;
+   pui->segment_count = 0;
+   pui->fade_start = 0;
+   pui->fade_count = 0;
+   pui->fade_timer_start = 0.0;
+
+   world->route_count ++;
+}
+
+/* load all routes from model header */
+VG_STATIC void world_routes_process( world_instance *world )
+{
+   vg_info( "Initializing routes\n" );
+   mdl_context *mdl = world->meta;
+
+   for( int i=0; i<mdl->info.node_count; i++ )
+   {
+      mdl_node *pnode = mdl_node_from_id(mdl,i);
+
+      if( pnode->classtype == k_classtype_route_node ||
+          pnode->classtype == k_classtype_gate )
+      {
+         struct route_node *rn = world_routes_create_node( world, pnode );
+
+         if( pnode->classtype == k_classtype_gate )
+         {
+            world_routes_process_gate( world, rn, pnode );
+         }
+         else
+         {
+            struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
+            rn->next[0] = world_routes_get_subuid( world, inf->target  );
+            rn->next[1] = world_routes_get_subuid( world, inf->target1 );
+         }
+      }
+      else if( pnode->classtype == k_classtype_route )
+      {
+         world_routes_create_route( world, pnode );
+      }
+   }
+
+   /*
+    * Gather references
     */
-   for( int i=0; i<r->node_count; i++ )
+   for( int i=0; i<world->route_count; i++ )
    {
-      struct route_node *rn = &r->nodes[i];
-      
-      for( int j=0; j<2; j++ )
-         world_id_fixup( &rn->next[j], mdl );
+      struct route *route = &world->routes[i];
 
-      if( rn->is_gate )
-         world_id_fixup( &rn->gate_id, mdl );
+      u32 stack[64];
+      u32 si = world_routes_get_path( world, route->start, stack );
+
+      for( int sj=0; sj<si; sj++ )
+      {
+         struct route_node *rn = &world->nodes[ stack[sj] ];
+         rn->route_ids[ rn->ref_count ++ ] = i;
+
+         if( rn->ref_count > 4 )
+            vg_warn( "Too many references on route node %i\n", i );
+      }
    }
+}
+
+/* 
+ * -----------------------------------------------------------------------------
+ *                                    Events
+ * -----------------------------------------------------------------------------
+ */
+
+VG_STATIC void world_routes_init(void)
+{
+   world_global.current_run_version = 2;
+   world_global.time = RESET_MAX_TIME*2.0;
+   world_global.last_use = 0.0;
 
-   for( int i=0; i<r->gate_count; i++ )
+   shader_scene_route_register();
+   shader_routeui_register();
+
+   vg_acquire_thread_sync();
    {
-      struct route_gate *rg = &r->gates[i];
-      world_id_fixup( &rg->node_id, mdl );
+      /* UI buffers */
+      for( int i=0; i<vg_list_size(world_global.ui_bars); i++ )
+      {
+         /* OpenGL strips */
+         struct route_ui_bar *pui = &world_global.ui_bars[i];
+
+         glGenVertexArrays( 1, &pui->vao );
+         glGenBuffers( 1, &pui->vbo );
+         glGenBuffers( 1, &pui->ebo );
+         glBindVertexArray( pui->vao );
+
+         size_t stride = sizeof(v2f);
+
+         glBindBuffer( GL_ARRAY_BUFFER, pui->vbo );
+         glBufferData( GL_ARRAY_BUFFER, k_route_ui_max_verts*stride, 
+                       NULL, GL_DYNAMIC_DRAW );
+
+         glBindVertexArray( pui->vao );
+         glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, pui->ebo );
+         glBufferData( GL_ELEMENT_ARRAY_BUFFER, 
+               k_route_ui_max_indices*sizeof(u16), NULL,
+               GL_DYNAMIC_DRAW );
+
+         glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, stride, (void *)0 );
+         glEnableVertexAttribArray( 0 );
+         VG_CHECK_GL_ERR();
+      }
    }
+   vg_release_thread_sync();
+}
 
-   for( int i=0; i<r->route_count; i++ )
+VG_STATIC void world_routes_update( world_instance *world )
+{
+   world_global.time += vg.time_delta;
+
+   for( int i=0; i<world->route_count; i++ )
    {
-      struct route *route = &r->routes[i];
-      world_id_fixup( &route->start, mdl );
+      struct route *route = &world->routes[i];
+      route->factive = vg_lerpf( route->factive, route->active, 
+                                 0.6f*vg.time_delta );
+
+      if( route->active )
+      {
+         world_routes_ui_updatetime(i, world_global.time - route->latest_pass );
+      }
    }
 }
 
+VG_STATIC void bind_terrain_noise(void);
+VG_STATIC void world_bind_light_array( world_instance *world,
+                                       GLuint shader, GLuint location, 
+                                       int slot );
+
+VG_STATIC void render_world_routes( world_instance *world, camera *cam )
+{
+   m4x3f identity_matrix;
+   m4x3_identity( identity_matrix );
+
+   shader_scene_route_use();
+   shader_scene_route_uTexGarbage(0);
+   world_link_lighting_ub( world, _shader_scene_route.id );
+   world_bind_position_texture( world, _shader_scene_route.id, 
+                        _uniform_scene_route_g_world_depth, 2 );
+   world_bind_light_array( world, _shader_scene_route.id,
+                        _uniform_scene_route_uLightsArray, 3 );
+   bind_terrain_noise();
+
+   shader_scene_route_uPv( cam->mtx.pv );
+   shader_scene_route_uPvmPrev( cam->mtx_prev.pv );
+   shader_scene_route_uMdl( identity_matrix );
+   shader_scene_route_uCamera( cam->transform[3] );
+   shader_scene_route_uBoard0( TEMP_BOARD_0 );
+   shader_scene_route_uBoard1( TEMP_BOARD_1 );
+
+   mesh_bind( &world->mesh_route_lines );
+
+   for( int i=0; i<world->route_count; i++ )
+   {
+      struct route *route = &world->routes[i];
+
+      v4f colour;
+      v3_lerp( (v3f){0.7f,0.7f,0.7f}, route->colour, route->factive, colour );
+      colour[3] = 1.0f;
+
+      shader_scene_route_uColour( colour );
+      mdl_draw_submesh( &route->sm );
+   }
+}
+
+VG_STATIC void render_world_routes_ui( world_instance *world )
+{
+   glEnable(GL_BLEND);
+   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+   glBlendEquation(GL_FUNC_ADD);
+
+   float active_offset = 0.0f;
+   for( int i=0; i<world->route_count; i++ )
+   {
+      struct route *route = &world->routes[i];
+      world_routes_ui_draw( world, i, route->colour, active_offset );
+      active_offset += route->factive;
+   }
+
+   glDisable(GL_BLEND);
+}
+
 #endif /* ROUTES_H */