route verify
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
index 4c198e1a429a23001ed299045790297353e96575..91fde294c7cb5e439ebd75ffb524a1fe27af17fc 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;
@@ -43,15 +49,24 @@ struct subworld_routes
    {
       teleport_gate gate;
       
-      u32 route_count,
-          node_id;
+      u32 node_id;
+      u32 passed_version; /* Incremented on every teleport */
    }
    *gates;
 
+   struct route_collector
+   {
+      double time_passed; /* When did we last pass this? */
+   }
+   *collectors;
+
    u32 gate_count,
-       gate_cap;
+       gate_cap,
+       collector_count,
+       collector_cap;
 
-   u32 active_gate;
+   u32 active_gate,
+       current_run_version;
 
    scene scene_lines;
 };
@@ -84,37 +99,12 @@ static void debug_sbpath( struct route_node *rna, struct route_node *rnb,
    }
 }
 
-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],
-                     *pdest = &r->nodes[pnode->next[0]];
-   
-   r->active_gate = id;
-
-   for( int i=0; i<r->route_count; i++ )
-   {
-      struct route *route = &r->routes[i];
-
-      route->active = 0;
-      for( int j=0; j<pdest->ref_count; j++ )
-      {
-         if( pdest->route_ids[j] == i )
-         {
-            route->active = 1;
-            break;
-         }
-      }
-   }
-}
-
-static u32 world_routes_get_path( struct route *route, u32 stack[64] )
+static u32 world_routes_get_path( u32 starter, u32 stack[64] )
 {
    struct subworld_routes *r = subworld_routes();
    u32 stack_i[64];
 
-   stack[0] = route->start;
+   stack[0] = starter;
    stack_i[0] = 0;
 
    u32 si = 1;
@@ -166,6 +156,142 @@ static u32 world_routes_get_path( struct route *route, u32 stack[64] )
    return 0;
 }
 
+static void world_routes_verify_run( u32 route, double new_pass_time )
+{
+   struct subworld_routes *r = subworld_routes();
+
+   u32 stack[64];
+   u32 si = world_routes_get_path( r->routes[route].start, stack );
+
+   /* 
+    * we only care about gates that ref gates, so shuffle down the array
+    */
+   u32 gates[64];
+   u32 sj = 0;
+   for( u32 i=0; i<si; i++ )
+      if( r->nodes[stack[i]].special_type == k_route_special_type_gate )
+         gates[sj ++] = r->nodes[stack[i]].special_id;
+
+   /* 
+    * run versions & times must always ASCEND apart from exactly once, where
+    * the tail connects to the head
+    */
+
+   vg_info("Verifying run (%u)\n", route);
+
+   u32 offset = 0xffffffff;
+
+   vg_info( "  ver: %u\n", r->current_run_version );
+
+   for( u32 i=0; i<sj; i++ )
+   {
+      struct route_gate *pa = &r->gates[gates[i]];
+      u32 na = r->nodes[r->nodes[pa->node_id].next[0]].special_id;
+      struct route_collector *pca = &r->collectors[ na ];
+      
+      if( pa->passed_version != r->current_run_version )
+      {
+         vg_info( "  [%u]: %u (%f)\n", gates[i], pa->passed_version, 
+                                                 pca->time_passed );
+      }
+      else
+      {
+         vg_info( "  [%u]: %u (%f & %f)\n", gates[i], pa->passed_version,
+                                                      pca->time_passed,
+                                                      new_pass_time );
+         offset = i;
+      }
+   }
+
+   if( offset == 0xffffffff )
+   {
+      /* If we don't find the emitter we passed through, we've only just 
+       * started this route, so no blocks need to be emmited */
+      return;
+   }
+   
+   double lap_time = 0.0;
+   int verify_count = 0;
+
+   for( u32 i=0; i<sj; i++ )
+   {
+      u32 j = i+offset;
+      struct route_gate *pa = &r->gates[gates[j%sj]],
+                        *pb = &r->gates[gates[(j+1) % sj]];
+
+      u32 na = r->nodes[r->nodes[pa->node_id].next[0]].special_id,
+          nb = r->nodes[r->nodes[pb->node_id].next[0]].special_id;
+
+      struct route_collector *pca = &r->collectors[ na ],
+                             *pcb = &r->collectors[ nb ];
+
+      double diff = 0.0;
+
+      /* Verifying the path: either of this conditions must be true */
+      int verified = 0;
+      if( pa->passed_version == r->current_run_version )
+      {
+         /* The version should drop back down to pa+1-sj */
+      }
+      else
+      {
+         /* The version should be pa+1 */
+      }
+      
+      verify_count += verified;
+
+      if( pb->passed_version == r->current_run_version )
+      {
+         /* we need to use new val */
+         diff = new_pass_time - pca->time_passed;
+         vg_info( " LOOP %f\n", diff );
+      }
+      else
+      {
+         diff = pcb->time_passed - pca->time_passed;
+         vg_info( "      %f\n", diff );
+      }
+      
+      lap_time += diff;
+   }
+
+   /* Verify count is how many blocks we want to emit */
+}
+
+static void world_routes_activate_gate( u32 id )
+{
+   struct subworld_routes *r = subworld_routes();
+   struct route_gate *rg = &r->gates[id];
+   struct route_node *pnode = &r->nodes[rg->node_id],
+                     *pdest = &r->nodes[pnode->next[0]];
+
+   struct route_collector *rc = &r->collectors[ pdest->special_id ];
+
+   r->active_gate = id;
+   rg->passed_version = r->current_run_version;
+
+   vg_info( "collector updated: %u\n", pdest->special_id );
+
+   for( u32 i=0; i<r->route_count; i++ )
+   {
+      struct route *route = &r->routes[i];
+
+      route->active = 0;
+      for( u32 j=0; j<pdest->ref_count; j++ )
+      {
+         if( pdest->route_ids[j] == i )
+         {
+            world_routes_verify_run( i, vg_time );
+            route->active = 1;
+            break;
+         }
+      }
+   }
+   
+   rc->time_passed = vg_time;
+   r->current_run_version ++;
+}
+
 static void world_routes_debug(void)
 {
    struct subworld_routes *r = subworld_routes();
@@ -173,7 +299,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++ )
@@ -181,7 +307,7 @@ static void world_routes_debug(void)
       struct route *route = &r->routes[i];
 
       u32 stack[64];
-      u32 si = world_routes_get_path( route, stack );
+      u32 si = world_routes_get_path( route->start, stack );
 
       u32 colours[] = { 0xfff58142, 0xff42cbf5, 0xff42f56c, 0xfff542b3,
                         0xff5442f5 };
@@ -239,7 +365,7 @@ static void world_routes_gen_meshes(void)
       struct route *route = &r->routes[i];
 
       u32 stack[64];
-      u32 si = world_routes_get_path( route, stack );
+      u32 si = world_routes_get_path( route->start, stack );
 
       u32 last_valid = 0;
 
@@ -251,7 +377,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;
@@ -260,7 +386,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;
@@ -435,20 +561,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 )
@@ -457,8 +580,13 @@ 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 = 0xffffffff;
 
                   v3_copy( pnode->co,  rg->gate.co[0] );
                   v3_copy( pother->co, rg->gate.co[1] );
@@ -467,17 +595,33 @@ 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->time_passed = 0.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 ++;
@@ -527,7 +671,7 @@ static void world_routes_loadfrom( mdl_header *mdl )
       struct route *route = &r->routes[i];
 
       u32 stack[64];
-      u32 si = world_routes_get_path( route, stack );
+      u32 si = world_routes_get_path( route->start, stack );
 
       for( int sj=0; sj<si; sj++ )
       {