imgui stuff
[carveJwlIkooP6JGAAIwe30JlM.git] / player.c
index b705d7fe75d92929918d679f7162c819479b7fdd..0339a8d889f87c0faa08bf6b2443cf2bba9e783b 100644 (file)
--- a/player.c
+++ b/player.c
@@ -4,10 +4,41 @@
 #include "player.h"
 #include "camera.h"
 #include "player_model.h"
+#include "input.h"
+
+VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] )
+{
+   ent_spawn *rp = NULL, *r;
+   world_instance *world = localplayer.viewable_world;
+
+   if( argc == 1 ){
+      rp = world_find_spawn_by_name( world, argv[0] );
+   }
+   else if( argc == 0 ){
+      rp = world_find_closest_spawn( world, localplayer.rb.co );
+   }
+
+   if( !rp )
+      return 0;
+
+   player__spawn( &localplayer, rp );
+   return 1;
+}
+
+VG_STATIC void player_init(void)
+{
+   for( u32 i=0; i<vg_list_size(_player_system_register); i++ ){
+      if( _player_system_register[i] )
+         _player_system_register[i]();
+   }
+
+   vg_console_reg_cmd( "respawn", localplayer_cmd_respawn, NULL );
+}
 
 PLAYER_API
 void player__debugtext( int size, const char *fmt, ... )
 {
+#if 0
        char buffer[ 1024 ];
 
    va_list args;
@@ -17,6 +48,7 @@ void player__debugtext( int size, const char *fmt, ... )
 
    ui_text( vg_uictx.cursor, buffer, size, k_text_align_right );
        vg_uictx.cursor[1] += 14*size;
+#endif
 }
 
 /*
@@ -29,77 +61,18 @@ void player__create( player_instance *inst )
    assert( only_once == 0 );
    only_once ++;
 
-   inst->input_js1h = vg_create_named_input( "steer-h", k_input_type_axis );
-   inst->input_js1v = vg_create_named_input( "steer-v", k_input_type_axis );
-   inst->input_grab = vg_create_named_input( "grab",    k_input_type_axis_norm);
-   inst->input_js2h = vg_create_named_input( "grab-h",  k_input_type_axis );
-   inst->input_js2v = vg_create_named_input( "grab-v",  k_input_type_axis );
-   inst->input_jump = vg_create_named_input( "jump",    k_input_type_button );
-   inst->input_push = vg_create_named_input( "push",    k_input_type_button );
-   inst->input_walk = vg_create_named_input( "walk",    k_input_type_button );
-   inst->input_walkh= vg_create_named_input( "walk-h",  k_input_type_axis );
-   inst->input_walkv= vg_create_named_input( "walk-v",  k_input_type_axis );
-   inst->input_use  = vg_create_named_input( "use",     k_input_type_button );
-   inst->input_reset= vg_create_named_input( "reset",   k_input_type_button );
-   inst->input_camera=vg_create_named_input( "camera",  k_input_type_button );
-   inst->input_trick0=vg_create_named_input( "trick0",  k_input_type_button );
-   inst->input_trick1=vg_create_named_input( "trick1",  k_input_type_button );
-   inst->input_trick2=vg_create_named_input( "trick2",  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 shift",
-      "bind  grab-h gp-rs-h",
-      "bind  grab-v gp-rs-v",
-
-      "bind jump space",
-      "bind jump gp-a",
-
-      "bind trick0 mouse1",
-      "bind trick0 gp-a",
-      "bind trick1 mouse2",
-      "bind trick1 gp-b",
-      "bind trick2 gp-x",     /* keyboard: m0 + m1 */
-
-      "bind push gp-b",
-      "bind push w",
-
-      "bind walk shift",
-      "bind walk gp-ls",
-      
-      "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 use gp-y",
-      "bind use e",
-      "bind camera c"
-   };
-
-   for( int i=0; i<vg_list_size(default_cfg); i++ )
-      vg_execute_console_input(default_cfg[i]);
-
    v3_zero( inst->rb.co );
    v3_zero( inst->rb.w );
    v3_zero( inst->rb.v );
    q_identity( inst->rb.q );
    m4x3_identity( inst->rb.to_world );
    m4x3_identity( inst->rb.to_local );
+
+   inst->rewind_length = 0;
+   inst->rewind_buffer = 
+      vg_linear_alloc( vg_mem.rtmemory, 
+                       sizeof(struct rewind_frame) * PLAYER_REWIND_FRAMES );
+
 }
 
 /* 
@@ -113,33 +86,94 @@ void player__use_avatar( player_instance *player, struct player_avatar *av )
 }
 
 PLAYER_API
-void player__use_mesh( player_instance *player, glmesh *mesh )
+void player__use_model( player_instance *player, struct player_model *mdl )
 {
-   player->playermesh = mesh;
-}
-
-PLAYER_API
-void player__use_texture( player_instance *player, vg_tex2d *tex )
-{
-   player->playertex = tex;
+   player->playermodel = mdl;
 }
 
 PLAYER_API
 void player__bind( player_instance *player )
 {
-   player__skate_bind( player );
-   player__walk_bind( player );
+   for( u32 i=0; i<vg_list_size(_player_bind); i++ ){
+      if( _player_bind[i] )
+         _player_bind[i]( player );
+   }
 }
 
 /*
  * Gameloop events
  * ----------------------------------------------------------------------------
  */
+
+VG_STATIC void player_save_rewind_frame( player_instance *player )
+{
+   if( player->rewind_length < PLAYER_REWIND_FRAMES ){
+      struct rewind_frame *fr = 
+         &player->rewind_buffer[ player->rewind_length ++ ];
+
+      v2_copy( player->cam.angles, fr->ang );
+      v3_copy( player->cam.pos, fr->pos );
+
+      if( player->rewind_length >= 2 ){
+         player->rewind_total_length += 
+            v3_dist( player->rewind_buffer[player->rewind_length-1].pos,
+                     player->rewind_buffer[player->rewind_length-2].pos );
+      }
+   }
+}
+
 PLAYER_API
 void player__pre_update( player_instance *player )
 {
-   if( vg_input_button_down( player->input_camera ) )
-   {
+   if( player->rewinding ){
+      return;
+   }
+
+   if( button_down( k_srbind_reset ) && !player->immobile ){
+      double delta = world_global.time - world_global.last_use;
+
+      if( (delta <= RESET_MAX_TIME) && (world_global.last_use != 0.0) ){
+         player->rewinding = 1;
+         player->rewind_sound_wait = 1;
+         player->rewind_time = (double)player->rewind_length - 0.0001;
+         player_save_rewind_frame( player );
+
+         audio_lock();
+         audio_oneshot( &audio_rewind[0], 1.0f, 0.0f );
+         audio_unlock();
+
+         /* based on 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->rewind_start = vg.time;
+         player->subsystem = player->subsystem_gate;
+         player->rb = player->rb_gate_storage;
+         v3_copy( player->angles_storage, player->angles );
+
+         if( _player_restore[ player->subsystem ] )
+            _player_restore[ player->subsystem ]( player );
+      }
+      else{
+         if( player->subsystem == k_player_subsystem_dead ){
+            localplayer_cmd_respawn( 0, NULL );
+         }
+         else{
+            /* cant do that */
+            audio_lock();
+            audio_oneshot( &audio_rewind[4], 1.0f, 0.0f );
+            audio_unlock();
+         }
+      }
+   }
+
+   if( button_down( k_srbind_camera ) && !player->immobile ){
       if( player->camera_mode == k_cam_firstperson )
          player->camera_mode = k_cam_thirdperson;
       else
@@ -153,6 +187,9 @@ void player__pre_update( player_instance *player )
 PLAYER_API
 void player__update( player_instance *player )
 {
+   if( player->rewinding )
+      return;
+
    if( _player_update[ player->subsystem ] )
       _player_update[ player->subsystem ]( player );
 }
@@ -160,46 +197,33 @@ void player__update( player_instance *player )
 PLAYER_API
 void player__post_update( player_instance *player )
 {
+   if( player->rewinding )
+      return;
+
    if( _player_post_update[ player->subsystem ] )
       _player_post_update[ player->subsystem ]( player );
-}
-
-VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
-{
-   /* FIXME: Applies to main_camera directly! */
-
-   /* Pre-emptively edit the camera matrices so that the motion vectors 
-    * are correct */
-   m4x3f transport_i;
-   m4x4f transport_4;
-   m4x3_invert_affine( transport, transport_i );
-   m4x3_expand( transport_i, transport_4 );
-   m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
-   m4x4_mul( main_camera.mtx.v,  transport_4, main_camera.mtx.v );
-}
 
-__attribute__ ((deprecated))
-VG_STATIC void gate_rotate_angles( teleport_gate *gate, v3f angles, v3f d )
-{
-   v3_copy( angles, d );
-   return;
+   if((player->subsystem != k_player_subsystem_dead) && !player->gate_waiting){
+      player->rewind_accum += vg.time_frame_delta;
 
-   v3f fwd_dir = { cosf(angles[0]),
-                   0.0f,
-                   sinf(angles[0])};
-   m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
-
-   v3_copy( angles, d );
-   d[0] = atan2f( fwd_dir[2], fwd_dir[0] );
+      if( player->rewind_accum > 0.25f ){
+         player->rewind_accum -= 0.25f;
+         player_save_rewind_frame( player );
+      }
+   }
 }
 
 /*
  * Applies gate transport to a player_interface
  */
 PLAYER_API
-void player__pass_gate( player_instance *player, teleport_gate *gate )
+void player__pass_gate( player_instance *player, ent_gate *gate )
 {
+   world_routes_fracture( get_active_world(), gate, 
+                          player->rb.co, player->rb.v );
+
    player->gate_waiting = gate;
+   world_routes_activate_entry_gate( get_active_world(), gate );
 
    m4x3_mulv( gate->transport, player->tpv_lpf, player->tpv_lpf );
    m3x3_mulv( gate->transport, player->cam_velocity_smooth, 
@@ -213,76 +237,65 @@ void player__pass_gate( player_instance *player, teleport_gate *gate )
    q_normalize( player->qbasis );
    q_m3x3( player->qbasis, player->basis );
    m3x3_transpose( player->basis, player->invbasis );
-}
 
-VG_STATIC void player__pre_render( player_instance *player )
-{
-   if( _player_animate[ player->subsystem ] )
-   {
-      player_animation res;
-      _player_animate[ player->subsystem ]( player, &res );
-
-      /* TODO: eventually, blending code goes here */
+   player->subsystem_gate = player->subsystem;
+   player->rb_gate_storage = player->rb;
+   v3_copy( player->angles, player->angles_storage );
+   player->rewind_length = 0;
+   player->rewind_total_length = 0.0f;
+   player->rewind_gate = gate;
+   player->rewind_accum = 0.0f;
 
-      m4x3f transform;
-      q_m3x3( res.root_q, transform );
-      v3_copy( res.root_co, transform[3] );
+   m4x3_mulv( gate->transport, player->cam.pos, player->cam.pos );
+   player_save_rewind_frame( player );
 
-      struct skeleton *sk = &player->playeravatar->sk;
+   if( gate->type == k_gate_type_nonlocel )
+      world_global.active_world = gate->target;
 
-      if( player->holdout_time > 0.0f )
-      {
-         skeleton_lerp_pose( sk, res.pose, player->holdout_pose, 
-                                           player->holdout_time, res.pose );
-         player->holdout_time -= vg.frame_delta * 2.0f;
-      }
+   world_global.in_volume = 0;
 
-      skeleton_apply_pose( sk, res.pose, k_anim_apply_defer_ik );
-      skeleton_apply_ik_pass( sk );
-      skeleton_apply_pose( sk, res.pose, k_anim_apply_deffered_only );
-      skeleton_apply_inverses( sk );
-      skeleton_apply_transform( sk, transform );
-
-#if 0
-      skeleton_debug( sk );
-#endif
-   }
-
-   if( _player_post_animate[ player->subsystem ] )
-      _player_post_animate[ player->subsystem ]( player );
+   audio_lock();
+   audio_oneshot( &audio_gate_pass, 1.0f, 0.0f );
+   audio_unlock();
+}
 
-   struct player_avatar *av = player->playeravatar;
-   v3f vp0 = {0.0f,0.1f, 0.55f},
-       vp1 = {0.0f,0.1f,-0.55f};
+VG_STATIC void player_apply_transport_to_cam( m4x3f transport )
+{
+   /* FIXME: Applies to main_camera directly! */
 
-   m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp0, TEMP_BOARD_0 );
-   m4x3_mulv( av->sk.final_mtx[ av->id_board ], vp1, TEMP_BOARD_1 );
+   /* Pre-emptively edit the camera matrices so that the motion vectors 
+    * are correct */
+   m4x3f transport_i;
+   m4x4f transport_4;
+   m4x3_invert_affine( transport, transport_i );
+   m4x3_expand( transport_i, transport_4 );
+   m4x4_mul( main_camera.mtx.pv, transport_4, main_camera.mtx.pv );
+   m4x4_mul( main_camera.mtx.v,  transport_4, main_camera.mtx.v );
 
-   player__cam_iterate( player );
+   /* we want the regular transform here no the inversion */
+   m4x3_expand( transport, transport_4 );
+   m4x4_mul( gate_camera.mtx.pv, transport_4, gate_camera.mtx.pv );
+   m4x4_mul( gate_camera.mtx.v,  transport_4, gate_camera.mtx.v );
 }
 
-PLAYER_API void player__render( camera *cam, player_instance *player )
+__attribute__ ((deprecated))
+VG_STATIC void gate_rotate_angles( ent_gate *gate, v3f angles, v3f d )
 {
-   shader_model_character_view_use();
-   vg_tex2d_bind( player->playertex, 0 );
-   shader_model_character_view_uTexMain( 0 );
-   shader_model_character_view_uCamera( cam->transform[3] );
-   shader_model_character_view_uPv( cam->mtx.pv );
-
-   world_instance *world = get_active_world();
-   world_link_lighting_ub( world, _shader_model_character_view.id, 2 );
-
-   glUniformMatrix4x3fv( _uniform_model_character_view_uTransforms,
-                         player->playeravatar->sk.bone_count,
-                         0,
-                         (float *)player->playeravatar->sk.final_mtx );
-   
-   mesh_bind( player->playermesh );
-   mesh_draw( player->playermesh );
+   v3_copy( angles, d );
+   return;
+
+   v3f fwd_dir = { cosf(angles[0]),
+                   0.0f,
+                   sinf(angles[0])};
+   m3x3_mulv( gate->transport, fwd_dir, fwd_dir );
+
+   v3_copy( angles, d );
+   d[0] = atan2f( fwd_dir[2], fwd_dir[0] );
 }
 
 PLAYER_API void player__im_gui( player_instance *player )
 {
+#if 0
    vg_uictx.cursor[0] = vg.window_x - 200;
    vg_uictx.cursor[1] = 0;
    vg_uictx.cursor[2] = 200;
@@ -300,12 +313,14 @@ PLAYER_API void player__im_gui( player_instance *player )
 
    b[2].co[1] = vg_uictx.cursor[1];
    b[3].co[1] = vg_uictx.cursor[1];
+#endif
 }
 
+VG_STATIC void global_skateshop_exit(void);
 PLAYER_API void player__spawn( player_instance *player, 
-                              struct respawn_point *rp )
+                               ent_spawn *rp )
 {
-   v3_copy( rp->co, player->rb.co );
+   v3_copy( rp->transform.co, player->rb.co );
    v3_zero( player->rb.v );
    v3_zero( player->rb.w );
    q_identity( player->rb.q );
@@ -316,6 +331,10 @@ PLAYER_API void player__spawn( player_instance *player,
    m3x3_identity( player->invbasis );
 
    player->subsystem = k_player_subsystem_walk;
+   player->immobile = 0;
+   player->gate_waiting = NULL;
+
+   global_skateshop_exit();
 
    if( _player_reset[ player->subsystem ] )
       _player_reset[ player->subsystem ]( player, rp );