stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / world_routes.h
index d714ff3f70ed9e77481eb4fa446b2f2a82490c5f..9cee0df0d19b38dbd41b5c84042cc9cdaded5b83 100644 (file)
@@ -4,6 +4,8 @@
 #include "common.h"
 #include "model.h"
 #include "gate.h"
+#include "world_info.h"
+#include "highscores.h"
 
 #include "shaders/vblend.h"
 #include "shaders/route.h"
@@ -36,7 +38,7 @@ struct subworld_routes
 
    struct route
    {
-      const char *name;
+      u32 track_id;
       v4f colour;
 
       u32 start;
@@ -66,8 +68,11 @@ struct subworld_routes
 
          u32 segment_start, segment_count, fade_start, fade_count;
          double fade_timer_start;
+         float xpos;
       }
       ui;
+
+      m4x3f scoreboard_transform;
    }
    *routes;
 
@@ -460,12 +465,30 @@ static void world_routes_ui_notch( u32 route, float time )
    }
 }
 
+static void world_routes_ui_draw_segment( struct route_ui_segment *segment )
+{
+   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).
+ */
 static void world_routes_ui_draw( u32 route, v4f colour, float offset )
 {
+   float const k_bar_height = 0.05f,
+               k_bar_scale_x = 0.005f;
+
    struct subworld_routes *r = subworld_routes();
    struct route *pr = &r->routes[route];
 
-   float cx = 0.0f;
+   float cx = pr->ui.xpos;
 
    shader_routeui_use();
    glBindVertexArray( pr->ui.vao );
@@ -473,7 +496,9 @@ static void world_routes_ui_draw( u32 route, v4f colour, float offset )
    float fade_amt = vg_time - pr->ui.fade_timer_start;
    fade_amt = vg_clampf( fade_amt / 1.0f, 0.0f, 1.0f );
    
-   float fade_block_size = 0.0f;
+   float fade_block_size = 0.0f,
+         main_block_size = 0.0f;
+
    for( u32 i=0; i<pr->ui.fade_count; i++ )
    {
       u32 j = (pr->ui.fade_start + i) % k_max_ui_segments;
@@ -488,7 +513,12 @@ static void world_routes_ui_draw( u32 route, v4f colour, float offset )
    v4_copy( colour, fade_colour );
    fade_colour[3] *= 1.0f-fade_amt;
 
-   float height = pr->factive*0.1f;
+   /* 
+    * Draw fadeout bar
+    */
+
+   float height = pr->factive*k_bar_height,
+         base = -1.0f + (offset+0.5f)*k_bar_height;
 
    shader_routeui_uColour( fade_colour );
    for( u32 i=0; i<pr->ui.fade_count; i++ )
@@ -496,38 +526,60 @@ static void world_routes_ui_draw( u32 route, v4f colour, float offset )
       u32 j = (pr->ui.fade_start + i) % k_max_ui_segments;
       struct route_ui_segment *segment = &pr->ui.segments[j];
 
-      u32 c0, c1;
-      world_routes_ui_split_indices( segment->index_start, 
-            segment->index_count, &c0, &c1 );
-
-      shader_routeui_uOffset( (v4f){ cx, offset, 0.005f, height } );
-      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) );
+      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<pr->ui.segment_count; i++ )
    {
       u32 j = (pr->ui.segment_start + i) % k_max_ui_segments;
       struct route_ui_segment *segment = &pr->ui.segments[j];
 
-      u32 c0, c1;
-      world_routes_ui_split_indices( segment->index_start, 
-            segment->index_count, &c0, &c1 );
-
-      shader_routeui_uOffset( (v4f){ cx, offset, 0.005f, height } );
-      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) );
+      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;
+   }
+
+   pr->ui.xpos = vg_lerpf( pr->ui.xpos, -main_block_size * 0.5f, 0.03f );
+}
+
+static void world_routes_local_set_record( u32 route, double lap_time )
+{
+   vg_success( "  NEW LAP TIME: %f\n", lap_time );
+
+   struct subworld_routes *r = subworld_routes();
+   struct route *pr = &r->routes[route];
+
+   if( pr->track_id != 0xffffffff )
+   {
+      double time_centiseconds = lap_time * 100.0;
+      if( time_centiseconds > (float)0xfffe )
+         return;
+
+      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 );
+      track_infos[ pr->track_id ].push = 1;
+   }
+   else
+   {
+      vg_warn( "There is no associated track for this record...\n" );
    }
 }
 
@@ -616,7 +668,7 @@ static void world_routes_verify_run( u32 route )
 
    if( verified )
    {
-      vg_success( "  NEW LAP TIME: %f\n", lap_time );
+      world_routes_local_set_record( route, lap_time );
       world_routes_ui_popfirst(route);
       pr->ui.fade_count ++;
    }
@@ -856,8 +908,6 @@ static void world_routes_gen_meshes(void)
                v3_muladds( hb.pos, up, 0.06f, vb.co );
                v3_copy( up, va.norm );
                v3_copy( up, vb.norm );
-               v3_zero( va.colour );
-               v3_zero( vb.colour );
                v2_zero( va.uv );
                v2_zero( vb.uv );
 
@@ -958,7 +1008,7 @@ static void render_world_routes_ui(void)
    for( int i=0; i<r->route_count; i++ )
    {
       struct route *route = &r->routes[i];
-      world_routes_ui_draw( i, route->colour, active_offset*0.1f );
+      world_routes_ui_draw( i, route->colour, active_offset );
       active_offset += route->factive;
    }
 
@@ -1089,10 +1139,21 @@ static void world_routes_loadfrom( mdl_header *mdl )
          v3_copy( inf->colour, route->colour );
          route->colour[3] = 1.0f;
 
-         route->name = NULL;
+
+         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;
+            }
+         }
+
          route->start = inf->id_start;
          route->active = 0;
          route->factive = 0.0f;
+         mdl_node_transform( pnode, route->scoreboard_transform );
 
          /* OpenGL strips */
          glGenVertexArrays( 1, &route->ui.vao );