fix long standing grind bug
[carveJwlIkooP6JGAAIwe30JlM.git] / player_skate.c
index d1be6b8b82c84adf3d7978e176b138e4afec7117..87c8cba7483e9ea22e07e042b967e34189eb4309 100644 (file)
@@ -5,6 +5,7 @@
 #include "audio.h"
 #include "vg/vg_perlin.h"
 #include "menu.h"
+#include "ent_skateshop.h"
 
 VG_STATIC void player__skate_bind( player_instance *player )
 {
@@ -54,7 +55,7 @@ VG_STATIC int skate_collide_smooth( player_instance *player,
                                     m4x3f mtx, rb_sphere *sphere,
                                     rb_ct *man )
 {
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
 
    int len = 0;
    len = rb_sphere__scene( mtx, sphere, NULL, &world->rb_geo.inf.scene, man );
@@ -91,7 +92,7 @@ VG_STATIC int skate_grind_scansq( player_instance *player,
                                   v3f pos, v3f dir, float r,
                                   struct grind_info *inf )
 {
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
 
    v4f plane;
    v3_copy( dir, plane );
@@ -101,13 +102,8 @@ VG_STATIC int skate_grind_scansq( player_instance *player,
    boxf box;
    v3_add( pos, (v3f){ r, r, r }, box[1] );
    v3_sub( pos, (v3f){ r, r, r }, box[0] );
-   
-   bh_iter it;
-   bh_iter_init( 0, &it );
-   int idx;
 
-   struct grind_sample
-   {
+   struct grind_sample{
       v2f co;
       v2f normal;
       v3f normal3,
@@ -123,7 +119,11 @@ VG_STATIC int skate_grind_scansq( player_instance *player,
    v3_cross( plane, player->basis[1], support_axis );
    v3_normalize( support_axis );
    
-   while( bh_next( world->geo_bh, &it, box, &idx ) ){
+   bh_iter it;
+   bh_iter_init_box( 0, &it, box );
+   i32 idx;
+   
+   while( bh_next( world->geo_bh, &it, &idx ) ){
       u32 *ptri = &world->scene_geo.arrindices[ idx*3 ];
       v3f tri[3];
 
@@ -233,10 +233,8 @@ too_many_samples:
          float yi = v3_dot( player->basis[1], si->normal3 ),
                yj = v3_dot( player->basis[1], sj->normal3 );
 
-         if( yi > yj )
-            v3_add( si->normal3, average_normal, average_normal );
-         else
-            v3_add( sj->normal3, average_normal, average_normal );
+         if( yi > yj ) v3_add( si->normal3, average_normal, average_normal );
+         else          v3_add( sj->normal3, average_normal, average_normal );
 
          passed_samples ++;
       }
@@ -347,7 +345,7 @@ VG_STATIC int create_jumps_to_hit_target( player_instance *player,
 VG_STATIC 
 void player__approximate_best_trajectory( player_instance *player )
 {
-   world_instance *world0 = get_active_world();
+   world_instance *world0 = world_current_instance();
 
    struct player_skate *s = &player->_skate;
    float k_trace_delta = k_rb_delta * 10.0f;
@@ -501,7 +499,7 @@ void player__approximate_best_trajectory( player_instance *player )
                m3x3_mul( gate->transport, basis, basis );
 
                if( gate->type == k_gate_type_nonlocel ){
-                  trace_world = &world_global.worlds[ gate->target ];
+                  trace_world = &world_static.worlds[ gate->target ];
                }
             }
          }
@@ -979,7 +977,7 @@ VG_STATIC void skate_apply_jump_model( player_instance *player )
       s->state.jump_time = vg.time;
 
       audio_lock();
-      audio_oneshot_3d( &audio_jumps[rand()%2], player->rb.co, 40.0f, 1.0f );
+      audio_oneshot_3d( &audio_jumps[vg_randu32()%2], player->rb.co, 40.0f, 1.0f );
       audio_unlock();
    }
 }
@@ -1191,13 +1189,10 @@ VG_STATIC void player__skate_post_update( player_instance *player )
       slide = 0.0f;
    }
 
-   static float menu_gate = 1.0f;
-   menu_gate = vg_lerpf( menu_gate, 1-cl_menu, vg.time_frame_delta*4.0f );
-
-   float
-         vol_main    = sqrtf( (1.0f-air)*attn*(1.0f-slide) * 0.4f ) * menu_gate,
-         vol_air     = sqrtf(       air *attn * 0.5f )              * menu_gate,
-         vol_slide   = sqrtf( (1.0f-air)*attn*slide * 0.25f )       * menu_gate;
+   f32 gate        = 1.0f-menu.factive,
+       vol_main    = sqrtf( (1.0f-air)*attn*(1.0f-slide) * 0.4f ) * gate,
+       vol_air     = sqrtf(       air *attn * 0.5f )              * gate,
+       vol_slide   = sqrtf( (1.0f-air)*attn*slide * 0.25f )       * gate;
 
    const u32 flags = AUDIO_FLAG_SPACIAL_3D|AUDIO_FLAG_LOOP;
 
@@ -1322,7 +1317,7 @@ int skate_compute_surface_alignment( player_instance *player,
                                      v3f surface_normal, v3f axel_dir )
 {
    struct player_skate *s = &player->_skate;
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
 
    v3f truck, left, right;
    m4x3_mulv( player->rb.to_world, ra, truck );
@@ -1535,7 +1530,7 @@ VG_STATIC int skate_point_visible( v3f origin, v3f target )
    v3_muls( dir, 1.0f/ray.dist, dir );
    ray.dist -= 0.025f;
 
-   if( ray_world( get_active_world(), origin, dir, &ray ) )
+   if( ray_world( world_current_instance(), origin, dir, &ray ) )
       return 0;
 
    return 1;
@@ -1668,6 +1663,10 @@ VG_STATIC void skate_5050_apply( player_instance *player,
    v3_muladds( inf_back->co, inf_avg.dir, 0.5f, inf_avg.co );
    v3_normalize( inf_avg.dir );
 
+   /* dont ask */
+   v3_muls( inf_avg.dir, vg_signf(v3_dot(inf_avg.dir,player->rb.v)), 
+            inf_avg.dir );
+
    v3f axis_front, axis_back, axis;
    v3_cross( inf_front->dir, inf_front->n, axis_front );
    v3_cross( inf_back->dir,  inf_back->n,  axis_back  );
@@ -1696,6 +1695,8 @@ VG_STATIC void skate_5050_apply( player_instance *player,
                                     k_grind_spring, 
                                     k_grind_dampener,
                                     k_rb_delta );
+   vg_line_arrow( player->rb.co, up, 1.0f, VG__GREEN );
+   vg_line_arrow( player->rb.co, target_up, 1.0f, VG__GREEN );
 
    v3f fwd_nplane, dir_nplane;
    v3_muladds( player->rb.to_world[2], inf_avg.n,
@@ -1712,6 +1713,8 @@ VG_STATIC void skate_5050_apply( player_instance *player,
                                     1000.0f,
                                     k_grind_dampener,
                                     k_rb_delta );
+   vg_line_arrow( player->rb.co, fwd_nplane, 0.8f, VG__RED );
+   vg_line_arrow( player->rb.co, dir_nplane, 0.8f, VG__RED );
 
    v3f pos_front = { 0.0f, -k_board_radius, -1.0f * k_board_length },
        pos_back  = { 0.0f, -k_board_radius,  1.0f * k_board_length },
@@ -2077,7 +2080,7 @@ VG_STATIC enum skate_activity skate_availible_grind( player_instance *player )
 VG_STATIC void player__skate_update( player_instance *player )
 {
    struct player_skate *s = &player->_skate;
-   world_instance *world = get_active_world();
+   world_instance *world = world_current_instance();
 
    v3_copy( player->rb.co, s->state.prev_pos );
    s->state.activity_prev = s->state.activity;
@@ -2198,7 +2201,7 @@ VG_STATIC void player__skate_update( player_instance *player )
          if( !prev_contacts[i] ){
             v3f co;
             m4x3_mulv( player->rb.to_world, wheels[i].pos, co );
-            audio_oneshot_3d( &audio_taps[rand()%4], co, 40.0f, 0.75f );
+            audio_oneshot_3d( &audio_taps[vg_randu32()%4], co, 40.0f, 0.75f );
          }
       }
       audio_unlock();
@@ -2572,11 +2575,11 @@ begin_collision:;
 
       if( s->state.activity == k_skate_activity_ground ){
          if( (fabsf(s->state.slip) > 0.75f) ){
-            audio_oneshot_3d( &audio_lands[rand()%2+3], player->rb.co, 
+            audio_oneshot_3d( &audio_lands[vg_randu32()%2+3], player->rb.co, 
                               40.0f, 1.0f );
          }
          else{
-            audio_oneshot_3d( &audio_lands[rand()%3], player->rb.co, 
+            audio_oneshot_3d( &audio_lands[vg_randu32()%3], player->rb.co, 
                               40.0f, 1.0f );
          }
       }
@@ -2667,7 +2670,7 @@ VG_STATIC void player__skate_animate( player_instance *player,
 
    float curspeed  = v3_length( player->rb.v ),
          kickspeed = vg_clampf( curspeed*(1.0f/40.0f), 0.0f, 1.0f ),
-         kicks     = (vg_randf()-0.5f)*2.0f*kickspeed,
+         kicks     = (vg_randf64()-0.5f)*2.0f*kickspeed,
          sign      = vg_signf( kicks );
 
    s->wobble[0] = vg_lerpf( s->wobble[0], kicks*kicks*sign, 6.0f*vg.time_delta);
@@ -2966,7 +2969,7 @@ VG_STATIC void player__skate_animate( player_instance *player,
       q_mul( kf_board->q, qtrick, kf_board->q );
       q_normalize( kf_board->q );
 
-      struct player_board *board = player->board;
+      struct player_board *board = player_get_player_board( player );
       
       if( board ){
          /* foot weight distribution */