cleaned routes
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
index a4624dc8f579c6b7308f363c014308d2c6afd683..acb14ea84b7fe2aab24b91a577369f9edbaaa4f3 100644 (file)
@@ -8,6 +8,12 @@
 #include "shaders/vblend.h"
 #include "shaders/route.h"
 
+enum route_special_type
+{
+   k_route_special_type_gate = 1,
+   k_route_special_type_collector = 2
+};
+
 struct subworld_routes
 {
    struct route_node
@@ -15,7 +21,7 @@ struct subworld_routes
       v3f co, right, up, h;
       u32 next[2];
 
-      u32 is_gate, gate_id, current_refs, ref_count;
+      u32 special_type, special_id, current_refs, ref_count;
       u32 route_ids[4];    /* Gates can be linked into up to four routes */
    }
    *nodes;
@@ -45,13 +51,25 @@ struct subworld_routes
       
       u32 node_id;
 
-      double time_passed; /* When did we last pass this gate? */
-      u32    passed_version; /* Incremented on every reset */
+      struct route_timing
+      {
+         u32 version; /* Incremented on every teleport */
+         double time;
+      } 
+      timing;
    }
    *gates;
 
+   struct route_collector
+   {
+      struct route_timing timing;
+   }
+   *collectors;
+
    u32 gate_count,
-       gate_cap;
+       gate_cap,
+       collector_count,
+       collector_cap;
 
    u32 active_gate,
        current_run_version;
@@ -87,6 +105,9 @@ static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
    }
 }
 
+/*
+ * Get a list of node ids in stack, and return how many there is
+ */
 static u32 world_routes_get_path( u32 starter, u32 stack[64] )
 {
    struct subworld_routes *r = subworld_routes();
@@ -144,10 +165,15 @@ static u32 world_routes_get_path( u32 starter, u32 stack[64] )
    return 0;
 }
 
-static void world_routes_verify_run( u32 route, double new_pass_time )
+/* 
+ * 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)
+ */
+static void world_routes_verify_run( u32 route )
 {
    struct subworld_routes *r = subworld_routes();
-   if( r->current_run_version == 0 ) return;
 
    u32 stack[64];
    u32 si = world_routes_get_path( r->routes[route].start, stack );
@@ -155,62 +181,81 @@ static void world_routes_verify_run( u32 route, double new_pass_time )
    /* 
     * we only care about gates that ref gates, so shuffle down the array
     */
-   u32 sj = 0;
+   struct route_timing *timings[64];
+   u32 sj = 0, maxv = 0, begin = 0;
    for( u32 i=0; i<si; i++ )
-      if( r->nodes[stack[i]].is_gate && r->nodes[stack[(i+1)%si]].is_gate )
-         stack[sj ++] = r->nodes[stack[i]].gate_id;
+   {
+      if( r->nodes[stack[i]].special_type == k_route_special_type_collector )
+         timings[sj ++] = &r->collectors[r->nodes[stack[i]].special_id].timing;
+      else if( r->nodes[stack[i]].special_type == k_route_special_type_gate )
+         timings[sj ++] = &r->gates[r->nodes[stack[i]].special_id].timing;
+   }
+   
+   for( u32 i=0; i<sj; i++ )
+   {
+      if( timings[i]->version > maxv )
+      {
+         maxv = timings[i]->version;
+         begin = i;
+      }
+   }
 
-   /* 
-    * run versions & times must always ASCEND apart from exactly once, where
-    * the tail connects to the head
-    */
+   vg_info( "== begin verification (%u) ==\n", route );
+   vg_info( "  current version: %u\n", r->current_run_version );
 
-   vg_info("Verifying run (%u)\n", route);
+   int verified = 0;
+   if( timings[begin]->version == r->current_run_version )
+      verified = 1;
 
-   u32 descend_allowance = 1;
    double lap_time = 0.0;
 
    for( u32 i=0; i<sj; i++ )
    {
-      struct route_gate *pa = &r->gates[stack[i]],
-                        *pb = &r->gates[stack[(i+1) % sj]];
+      u32  j = (sj+begin-i-1) % sj,
+          j1 = (j+1) % sj;
 
-      vg_info( " pa: %u, pb: %u\n", pa->passed_version, pb->passed_version );
-      
-      int version_inorder = 0;
       double diff = 0.0;
-
-      if( pb->passed_version == pa->passed_version+1 )
-      {
-         version_inorder = 1;
-         diff = pb->time_passed - pa->time_passed;
-      }
-      else if( pb->passed_version == pa->passed_version+1-sj &&
-               pa->passed_version+1 == r->current_run_version )
+   
+      if( i<sj-1 )
       {
-         version_inorder = 1;
-         diff = new_pass_time - pa->time_passed;
+         /* j1v should equal jv+1 */
+         if( timings[j1]->version == timings[j]->version+1 )
+         {
+            diff = timings[j1]->time - timings[j]->time;
+            lap_time += diff;
+         }
+         else
+            verified = 0;
       }
-
-      if( !version_inorder )
-         return;
-
-      lap_time += diff;
+      
+      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 );
    }
 
-   /* We've now verified the run was completed correctly */
-   vg_success( "Lap time set. route %u: %lf\n", route, lap_time );
+   if( verified )
+      vg_success( "  NEW LAP TIME: %f\n", lap_time );
+   else
+      vg_info( "  ctime: %f\n", lap_time );
 }
 
+/*
+ * When going through a gate this is called for bookkeeping purposes
+ */
 static void world_routes_activate_gate( u32 id )
 {
    struct subworld_routes *r = subworld_routes();
-   struct route_gate *ig = &r->gates[id];
-   struct route_node *pnode = &r->nodes[ig->node_id],
+   struct route_gate *rg = &r->gates[id];
+   struct route_node *pnode = &r->nodes[rg->node_id],
                      *pdest = &r->nodes[pnode->next[0]];
 
-   r->active_gate = id;
+   struct route_collector *rc = &r->collectors[ pdest->special_id ];
 
+   r->active_gate = id;
+   rg->timing.version = r->current_run_version;
+   rg->timing.time = vg_time;
    for( u32 i=0; i<r->route_count; i++ )
    {
       struct route *route = &r->routes[i];
@@ -220,15 +265,17 @@ static void world_routes_activate_gate( u32 id )
       {
          if( pdest->route_ids[j] == i )
          {
-            world_routes_verify_run( i, vg_time );
+            world_routes_verify_run( i );
             route->active = 1;
             break;
          }
       }
    }
    
-   ig->time_passed = vg_time;
-   ig->passed_version = r->current_run_version;
+   r->current_run_version ++;
+
+   rc->timing.version = r->current_run_version;
+   rc->timing.time = vg_time;
    r->current_run_version ++;
 }
 
@@ -239,7 +286,7 @@ static void world_routes_debug(void)
    for( int i=0; i<r->node_count; i++ )
    {
       struct route_node *rn = &r->nodes[i];
-      vg_line_pt3( rn->co, 1.0f, rn->is_gate? 0xffffff00: 0xff00b2ff );
+      vg_line_pt3( rn->co, 1.0f, rn->special_type? 0xffffff00: 0xff00b2ff );
    }
 
    for( int i=0; i<r->route_count; i++ )
@@ -295,6 +342,9 @@ static void world_id_fixup( u32 *uid, mdl_header *mdl )
       *uid = 0xffffffff;
 }
 
+/* 
+ * Create the strips of colour that run through the world along course paths
+ */
 static void world_routes_gen_meshes(void)
 {
    struct subworld_routes *r = subworld_routes();
@@ -317,7 +367,7 @@ static void world_routes_gen_meshes(void)
                            *rnk = &r->nodes[ stack[sk] ],
                            *rnl;
          
-         if( rnj->is_gate && rnk->is_gate )
+         if( rnj->special_type && rnk->special_type )
          {
             last_valid = 0;
             continue;
@@ -326,7 +376,7 @@ static void world_routes_gen_meshes(void)
          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->is_gate )
+         if( rnk->special_type )
          {
             rnl = &r->nodes[ rnk->next[0] ];
             base_x1 = (float)rnl->ref_count*-0.5f + (float)rnl->current_refs;
@@ -461,6 +511,9 @@ static void render_world_routes( m4x4f projection, v3f camera )
 
 static void world_routes_register(void)
 {
+   struct subworld_routes *r = subworld_routes();
+   r->current_run_version = 2;
+
    shader_route_register();
 }
 
@@ -501,20 +554,17 @@ static void world_routes_loadfrom( mdl_header *mdl )
          v3_copy( transform[3], rn->co );
          rn->ref_count = 0;
          rn->current_refs = 0;
+         rn->special_type = 0;
+         rn->special_id = 0;
 
          if( pnode->classtype == k_classtype_gate )
          {
-            r->gates = buffer_reserve( r->gates, r->gate_count, &r->gate_cap,
-                                       1, sizeof( struct route_gate ) );
-
             struct classtype_gate *inf = mdl_get_entdata( mdl, pnode );
 
             /* 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;
 
             /* TODO */
             if( inf->target )
@@ -523,10 +573,14 @@ static void world_routes_loadfrom( mdl_header *mdl )
                
                if( pother->classtype == k_classtype_gate )
                {
+                  r->gates = buffer_reserve( r->gates, r->gate_count, 
+                                             &r->gate_cap,
+                                             1, sizeof( struct route_gate ) );
+
                   struct route_gate *rg = &r->gates[r->gate_count];
                   rg->node_id = r->node_count;
-                  rg->passed_version = 0;
-                  rg->time_passed = 0.0;
+                  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] );
@@ -535,17 +589,34 @@ static void world_routes_loadfrom( mdl_header *mdl )
                   v2_copy( inf->dims,  rg->gate.dims );
 
                   gate_transform_update( &rg->gate );
+                  rn->special_type = k_route_special_type_gate;
+                  rn->special_id = r->gate_count;
 
                   r->gate_count ++;
                }
             }
+
+            if( rn->special_type == 0 )
+            {
+               r->collectors = buffer_reserve( 
+                     r->collectors, r->collector_count, &r->collector_cap,
+                                          1, sizeof( struct route_collector ));
+
+               struct route_collector *rc = &r->collectors[r->collector_count];
+               rc->timing.time = 0.0;
+               rc->timing.version = 0;
+
+               rn->special_type = k_route_special_type_collector;
+               rn->special_id = r->collector_count;
+
+               r->collector_count ++;
+            }
          }
          else
          {
             struct classtype_route_node *inf = mdl_get_entdata( mdl, pnode );
             rn->next[0] = inf->target;
             rn->next[1] = inf->target1;
-            rn->is_gate = 0;
          }
 
          r->node_count ++;