better physics
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
index fc435dc849bfdc34e19433108157b9bd90ab50f9..c8c9abc54b8932ecd480838014a44001ca9c4dfb 100644 (file)
--- a/player.h
+++ b/player.h
@@ -13,8 +13,8 @@
 #include "skeleton.h"
 #include "bvh.h"
 
-static float 
-   k_walkspeed             = 20.0f,  /* no longer used */
+VG_STATIC float 
+   k_walkspeed             = 12.0f,  /* no longer used */
    k_runspeed              = 20.0f,
    k_board_radius          = 0.3f,
    k_board_length          = 0.45f,
@@ -38,9 +38,10 @@ static float
    k_walk_accel            = 150.0f,
    k_walk_friction         = 8.0f;
 
-static int freecam = 0;
-static int walk_grid_iterations = 1;
-static float fc_speed = 10.0f;
+VG_STATIC int cl_playermdl_id = 0;
+VG_STATIC int freecam = 0;
+VG_STATIC int walk_grid_iterations = 1;
+VG_STATIC float fc_speed = 10.0f;
 
 /* 
  * -----------------------------------------------------------------------------
@@ -48,7 +49,7 @@ static float fc_speed = 10.0f;
  * -----------------------------------------------------------------------------
  */
 
-static struct gplayer
+VG_STATIC struct gplayer
 {
    /* Physics */
    rigidbody collide_front, collide_back;
@@ -77,6 +78,7 @@ static struct gplayer
          inv_visual_transform;
 
    int is_dead, death_tick_allowance, rewinding;
+   int rewind_sound_wait;
 
    v3f land_target;
    v3f land_target_log[22];
@@ -85,9 +87,25 @@ static struct gplayer
 
    v3f handl_target, handr_target,
        handl, handr;
+
+   /* Input */
+   struct input_binding *input_js1h,
+                        *input_js1v,
+                        *input_js2h,
+                        *input_js2v,
+                        *input_emjs2h,
+                        *input_emjs2v,
+                        *input_jump,
+                        *input_push,
+                        *input_walkh,
+                        *input_walkv,
+                        *input_switch_mode,
+                        *input_reset,
+                        *input_grab;
    
    /* Camera */
    float air_blend;
+   float air_time;
    
    v3f camera_pos, smooth_localcam;
    v2f angles;
@@ -101,7 +119,9 @@ static struct gplayer
    u32 rewind_incrementer,
        rewind_length;
 
-   float rewind_time;
+   float rewind_time, rewind_total_length, rewind_predicted_time;
+   double diag_rewind_start, diag_rewind_time;
+   float dist_accum;
 
    /* animation */
    double jump_time;
@@ -119,11 +139,14 @@ static struct gplayer
 
    float walk;
    int step_phase;
+   enum mdl_surface_prop surface_prop;
 
    /* player model */
    struct player_model
    {
-      glmesh mesh;
+      glmesh player_meshes[3];
+
+      mdl_context meta;
       struct skeleton sk;
       struct skeleton_anim *anim_stand,
                            *anim_highg,
@@ -155,7 +178,7 @@ static struct gplayer
          rigidbody  rb;
          u32 parent;
       }
-      *ragdoll;
+      ragdoll[32];
       u32 ragdoll_count;
 
       int shoes[2];
@@ -171,16 +194,18 @@ player =
 /* 
  * API 
  */
-static float *player_get_pos(void);
-static void player_kill(void);
-static float *player_cam_pos(void);
-static void player_save_frame(void);
-static void player_restore_frame(void);
-static void player_save_rewind_frame(void);
+VG_STATIC float *player_get_pos(void);
+VG_STATIC void player_kill(void);
+VG_STATIC float *player_cam_pos(void);
+VG_STATIC void player_save_frame(void);
+VG_STATIC void player_restore_frame(void);
+VG_STATIC void player_save_rewind_frame(void);
 
 /* 
  * Submodules
  */
+VG_STATIC void player_mouseview(void);
+
 #include "player_physics.h"
 #include "player_ragdoll.h"
 #include "player_model.h"
@@ -193,18 +218,88 @@ static void player_save_rewind_frame(void);
  * -----------------------------------------------------------------------------
  */
 
-static void player_init(void)                                            /* 1 */
+VG_STATIC void player_init(void)                                            /* 1 */
 {
+   player.input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
+   player.input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
+   player.input_grab = vg_create_named_input( "grab", k_input_type_axis_norm );
+   player.input_js2h = vg_create_named_input( "grab-h", k_input_type_axis );
+   player.input_js2v = vg_create_named_input( "grab-v", k_input_type_axis );
+   player.input_emjs2h = vg_create_named_input( "kbgrab-h", k_input_type_axis );
+   player.input_emjs2v = vg_create_named_input( "kbgrab-v", k_input_type_axis );
+   player.input_jump = vg_create_named_input( "jump", k_input_type_button );
+   player.input_push = vg_create_named_input( "push", k_input_type_axis_norm );
+
+   player.input_walkh = vg_create_named_input( "walk-h", 
+                                               k_input_type_axis );
+   player.input_walkv = vg_create_named_input( "walk-v", 
+                                               k_input_type_axis );
+
+
+   player.input_switch_mode = vg_create_named_input( "switch-mode",
+                                                     k_input_type_button );
+   player.input_reset = vg_create_named_input( "reset", k_input_type_button );
+
+   const char *default_cfg[] = 
+   {
+      "bind  steer-h gp-ls-h",
+      "bind -steer-h a",
+      "bind +steer-h d",
+
+      "bind  steer-v gp-ls-v",
+      "bind -steer-v w",
+      "bind +steer-v s",
+
+      "bind grab gp-rt",
+      "bind grab-h gp-rs-h",
+      "bind grab-v gp-rs-v",
+
+      "bind -kbgrab-h left",
+      "bind +kbgrab-h right",
+      "bind -kbgrab-v down",
+      "bind +kbgrab-v up",
+
+      "bind jump space",
+      "bind jump gp-a",
+
+      "bind  push gp-lt",
+      "bind +push shift",
+      
+      "bind  walk-h gp-ls-h",
+      "bind  walk-v gp-ls-v",
+      "bind +walk-h d",
+      "bind -walk-h a",
+      "bind +walk-v w",
+      "bind -walk-v s",
+
+      "bind reset gp-lb",
+      "bind reset r",
+
+      "bind switch-mode gp-y",
+      "bind switch-mode e",
+   };
+
+   for( int i=0; i<vg_list_size(default_cfg); i++ )
+      execute_console_input(default_cfg[i]);
+
    rb_init( &player.phys.rb );
    rb_init( &player.collide_front );
    rb_init( &player.collide_back  );
 
+   vg_convar_push( (struct vg_convar){
+      .name = "cl_playermdl_id",
+      .data = &cl_playermdl_id,
+      .data_type = k_convar_dtype_i32,
+      .opt_i32 = { .min=0, .max=2, .clamp=1 },
+      .persistent = 1
+   });
+
    vg_convar_push( (struct vg_convar){
       .name = "walk_speed",
       .data = &k_walkspeed,
       .data_type = k_convar_dtype_f32,
       .opt_f32 = { .clamp = 0 },
-      .persistent = 1
+      .persistent = 0
    });
 
    vg_convar_push( (struct vg_convar){
@@ -245,14 +340,17 @@ static void player_init(void)                                            /* 1 */
        });
 
    player.rewind_length = 0;
-   player.rewind_buffer = vg_alloc( sizeof(struct rewind_frame) 
-                                       * PLAYER_REWIND_FRAMES );
+   player.rewind_buffer = 
+      vg_linear_alloc( vg_mem.rtmemory, 
+                       sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
+
+   player_model_init();
 
-   /* other systems */
-   vg_loader_highwater( player_model_init, player_model_free, NULL );
+   /* controls */
+   
 }
 
-static void player_save_rewind_frame(void)
+VG_STATIC void player_save_rewind_frame(void)
 {
    if( player.rewind_length < PLAYER_REWIND_FRAMES )
    {
@@ -263,11 +361,30 @@ static void player_save_rewind_frame(void)
       v3_copy( player.camera_pos, fr->pos );
 
       player.rewind_incrementer = 0;
+
+      if( player.rewind_length > 1 )
+      {
+         player.rewind_total_length += 
+            v3_dist( player.rewind_buffer[player.rewind_length-1].pos,
+                     player.rewind_buffer[player.rewind_length-2].pos );
+      }
    }
 }
 
+/*
+ * Free camera movement
+ */
+VG_STATIC void player_mouseview(void)
+{
+   if( ui_want_mouse() )
+      return;
+
+   v2_muladds( player.angles, vg.mouse_delta, 0.0025f, player.angles );
+   player.angles[1] = vg_clampf( player.angles[1], -VG_PIf*0.5f, VG_PIf*0.5f );
+}
+
 /* Deal with input etc */
-static void player_update_pre(void)
+VG_STATIC void player_update_pre(void)
 {
    struct player_phys *phys = &player.phys;
 
@@ -276,43 +393,93 @@ static void player_update_pre(void)
       return;
    }
 
-   if( vg_get_button_down( "reset" ) )
+   if( vg_input_button_down( player.input_reset ) )
    {
-      player.rewinding = 1;
-      player.rewind_time = (float)player.rewind_length - 0.0001f;
-      player_save_rewind_frame();
-
-      player.is_dead = 0;
-      player.death_tick_allowance = 30;
-      player_restore_frame();
-
-      if( !phys->on_board )
+      if( player.is_dead )
       {
-         player.angles[0] = atan2f( -phys->rb.forward[2], 
-                                    -phys->rb.forward[0] );
+         reset_player( 0, NULL );
+         audio_lock();
+         audio_play_oneshot( &audio_ui[0], 1.0f );
+         audio_unlock();
       }
+      else
+      {
+         double delta = world.time - world.last_use;
 
-      player.mdl.shoes[0] = 1;
-      player.mdl.shoes[1] = 1;
+         if( (delta <= RESET_MAX_TIME) && (world.last_use != 0.0) )
+         {
+            player.rewinding = 1;
+            player.rewind_sound_wait = 1;
+            player.rewind_time = (float)player.rewind_length - 0.0001f;
+            player_save_rewind_frame();
+            audio_lock();
+            audio_play_oneshot( &audio_rewind[0], 1.0f );
+            audio_unlock();
+
+            /* based on analytical testing. DONT CHANGE!
+             * 
+             *    time taken: y = (x^(4/5)) * 74.5
+             *    inverse   : x = (2/149)^(4/5) * y^(4/5)
+             */
+
+            float constant = powf( 2.0f/149.0f, 4.0f/5.0f ),
+                  curve    = powf( player.rewind_total_length, 4.0f/5.0f );
+            
+            player.rewind_predicted_time = constant * curve;
+            player.diag_rewind_start = vg.time;
+            player.diag_rewind_time  = player.rewind_time;
 
-      world_routes_notify_reset();
+            player.is_dead = 0;
+            player.death_tick_allowance = 30;
+            player_restore_frame();
 
-      /* apply 1 frame of movement */
-      player_do_motion();
+            if( !phys->on_board )
+            {
+               player.angles[0] = atan2f( -phys->rb.forward[2], 
+                                          -phys->rb.forward[0] );
+            }
+
+            player.mdl.shoes[0] = 1;
+            player.mdl.shoes[1] = 1;
+
+            world_routes_notify_reset();
+
+            /* apply 1 frame of movement */
+            player_do_motion();
+         }
+         else
+         {
+            /* cant do that */
+            audio_lock();
+            audio_play_oneshot( &audio_rewind[4], 1.0f );
+            audio_unlock();
+         }
+      }
    }
 
-   if( vg_get_button_down( "switchmode" ) )
+   if( vg_input_button_down( player.input_switch_mode ) )
    {
       phys->on_board ^= 0x1;
 
+      audio_lock();
       if( phys->on_board )
       {
          v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
+         audio_play_oneshot( &audio_lands[6], 1.0f );
+      }
+      else
+      {
+         audio_play_oneshot( &audio_lands[5], 1.0f );
       }
+
+      audio_unlock();
    }
+
+   if( !phys->on_board )
+      player_mouseview();
 }
 
-static void player_update_fixed(void)                                    /* 2 */
+VG_STATIC void player_update_fixed(void)                                    /* 2 */
 {
    if( player.rewinding )
       return;
@@ -339,7 +506,7 @@ static void player_update_fixed(void)                                    /* 2 */
    }
 }
 
-static void player_update_post(void)
+VG_STATIC void player_update_post(void)
 {
    for( int i=0; i<player.land_log_count; i++ )
       vg_line_cross( player.land_target_log[i], 
@@ -372,18 +539,29 @@ static void player_update_post(void)
    {
       if( player.rewind_time <= 0.0f )
       {
+         double taken = vg.time - player.diag_rewind_start;
+         vg_success( "Rewind took (rt, pl, tl): %f, %f, %f\n", 
+                           taken, player.diag_rewind_time,
+                           player.rewind_total_length );
+
          player.rewinding = 0;
          player.rewind_length = 1;
+         player.rewind_total_length = 0.0f;
+         player.rewind_incrementer = 0;
+         world.sky_target_rate = 1.0;
       }
       else
       {
+         world.sky_target_rate = -100.0;
          assert( player.rewind_length > 0 );
 
          v2f override_angles;
          v3f override_pos;
 
          float budget         = vg.time_delta,
-               overall_length = player.rewind_length*0.25f;
+               overall_length = player.rewind_length;
+
+         world_routes_rollback_time( player.rewind_time / overall_length );
 
          for( int i=0; (i<10)&&(player.rewind_time>0.0f)&&(budget>0.0f); i ++ )
          {
@@ -403,12 +581,52 @@ static void player_update_post(void)
 
                   advl = vg_minf( mod, subl ),
                   advt = (advl / mod) * budget;
-
+            
+            player.dist_accum += speed * advt;
             player.rewind_time -= advl;
             budget -= advt;
          }
 
          player.rewind_time = vg_maxf( 0.0f, player.rewind_time );
+
+         float current_time = vg.time - player.diag_rewind_start,
+               remaining    = player.rewind_predicted_time - current_time;
+
+         if( player.rewind_sound_wait )
+         {
+            if( player.rewind_predicted_time >= 6.5f )
+            {
+               if( remaining <= 6.5f )
+               {
+                  audio_lock();
+                  audio_play_oneshot( &audio_rewind[3], 1.0f );
+                  audio_unlock();
+                  player.rewind_sound_wait = 0;
+               }
+            }
+            else if( player.rewind_predicted_time >= 2.5f )
+            {
+               if( remaining <= 2.5f )
+               {
+                  audio_lock();
+                  audio_play_oneshot( &audio_rewind[2], 1.0f );
+                  audio_unlock();
+                  player.rewind_sound_wait = 0;
+               }
+            }
+            else if( player.rewind_predicted_time >= 1.5f )
+            {
+               if( remaining <= 1.5f )
+               {
+                  audio_lock();
+                  audio_play_oneshot( &audio_rewind[1], 1.0f );
+                  audio_unlock();
+                  player.rewind_sound_wait = 0;
+               }
+            }
+
+
+         }
          
          int i0 = floorf( player.rewind_time ),
              i1 = VG_MIN( i0+1, player.rewind_length-1 );
@@ -436,7 +654,7 @@ static void player_update_post(void)
    player_audio();
 }
 
-static void draw_player( m4x3f cam )
+VG_STATIC void draw_player( m4x3f cam )
 {
    if( player.is_dead )
       player_model_copy_ragdoll();
@@ -452,8 +670,8 @@ static void draw_player( m4x3f cam )
                          0,
                          (float *)player.mdl.sk.final_mtx );
    
-   mesh_bind( &player.mdl.mesh );
-   mesh_draw( &player.mdl.mesh );
+   mesh_bind( &player.mdl.player_meshes[cl_playermdl_id] );
+   mesh_draw( &player.mdl.player_meshes[cl_playermdl_id] );
 }
 
 /* 
@@ -462,21 +680,22 @@ static void draw_player( m4x3f cam )
  * -----------------------------------------------------------------------------
  */
 
-static float *player_get_pos(void)
+VG_STATIC float *player_get_pos(void)
 {
    return player.phys.rb.co;
 }
 
-static void player_kill(void)
+VG_STATIC void player_kill(void)
 {
    if( player.death_tick_allowance == 0 )
    {
       player.is_dead = 1;
       player_ragdoll_copy_model( player.phys.rb.v );
+      world_routes_clear();
    }
 }
 
-static float *player_cam_pos(void)
+VG_STATIC float *player_cam_pos(void)
 {
    return player.camera_pos;
 }