move audio comp to its own thing
[carveJwlIkooP6JGAAIwe30JlM.git] / player.h
index 82381c27b84e14fbb0f706c62dc151e7f06fa04e..e1b44fe1104a3fc87cb6da6ebc372a7f74833be3 100644 (file)
--- a/player.h
+++ b/player.h
-/*
- * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved
- */
-
 #ifndef PLAYER_H
 #define PLAYER_H
 
-#include "audio.h"
-#include "common.h"
-#include "world.h"
-#include "skeleton.h"
-#include "bvh.h"
-
-static float 
-   k_walkspeed             = 20.0f,  /* no longer used */
-   k_runspeed              = 20.0f,
-   k_board_radius          = 0.3f,
-   k_board_length          = 0.45f,
-   k_board_allowance       = 0.04f,
-   k_friction_lat          = 8.8f,
-   k_friction_resistance   = 0.01f,
-   k_max_push_speed        = 16.0f,
-   k_push_accel            = 10.0f,
-   k_push_cycle_rate       = 8.0f,
-   k_steer_ground          = 2.5f,
-   k_steer_air             = 3.6f,
-   k_steer_air_lerp        = 0.3f,
-   k_pump_force            = 0.0f,
-   k_downforce             = 5.0f,
-   k_walk_downforce        = 8.0f,
-   k_jump_charge_speed     = (1.0f/1.0f),
-   k_jump_force            = 5.0f,
-   k_pitch_limit           = 1.5f,
-   k_look_speed            = 2.0f,
-   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;
-
-/* 
- * -----------------------------------------------------------------------------
- *                                  Memory
- * -----------------------------------------------------------------------------
- */
-
-static struct gplayer
-{
-   /* Physics */
-   rigidbody collide_front, collide_back;
-
-   struct player_phys
-   {
-      rigidbody rb, rb_gate_frame;
-      float iY, siY;   /* Yaw inertia */
-
-      v3f a, v_last, m, bob, vl;
-
-      /* Utility */
-      float vswitch, slip, slip_last,
-            reverse;
-
-      float grab, jump, pushing, push_time;
-      double start_push;
-      int in_air, on_board, jump_charge, jump_dir;
+#include "skaterift.h"
+#include "player_common.h"
+#include "network_compression.h"
+#include "player_effects.h"
+
+enum player_subsystem{
+   k_player_subsystem_walk = 0,
+   k_player_subsystem_skate = 1,
+   k_player_subsystem_dead = 2,
+   k_player_subsystem_drive = 3,
+   k_player_subsystem_basic_info = 4,
+   k_player_subsystem_max,
+   k_player_subsystem_invalid = 255
+};
 
-      m3x3f vr,vr_pstep;
-   }
-   phys, 
-   phys_gate_frame;
-
-   m4x3f visual_transform,
-         inv_visual_transform;
-
-   int is_dead, death_tick_allowance;
-
-   v3f land_target;
-   v3f land_target_log[22];
-   u32 land_target_colours[22];
-   int land_log_count;
-
-   v3f handl_target, handr_target,
-       handl, handr;
-   
-   /* Camera */
-   float air_blend;
-   
-   v3f camera_pos, smooth_localcam;
-   v2f angles;
-   m4x3f camera, camera_inverse;
-
-   /* animation */
-   double jump_time;
-   float fslide,
-         fdirz, fdirx,
-         fstand,
-         ffly,
-         fpush,
-         fairdir,
-         fsetup,
-         walk_timer,
-         fjump,
-         fonboard,
-         frun;
-
-   float walk;
-   int step_phase;
-
-   /* player model */
-   struct player_model
-   {
-      glmesh mesh;
-      struct skeleton sk;
-      struct skeleton_anim *anim_stand,
-                           *anim_highg,
-                           *anim_slide,
-                           *anim_air,
-                           *anim_push, *anim_push_reverse,
-                           *anim_ollie, *anim_ollie_reverse,
-                           *anim_grabs, *anim_stop,
-                           *anim_walk, *anim_run, *anim_idle,
-                           *anim_jump;
-
-      u32 id_hip,
-          id_ik_hand_l,
-          id_ik_hand_r,
-          id_ik_elbow_l,
-          id_ik_elbow_r,
-          id_head;
-
-      v3f cam_pos;
-
-      struct ragdoll_part
-      {
-         u32 bone_id;
-         v3f offset;
-
-         u32 use_limits;
-         v3f limits[2];
-
-         rigidbody  rb;
-         u32 parent;
-      }
-      *ragdoll;
-      u32 ragdoll_count;
-
-      int shoes[2];
+struct player_cam_controller {
+   enum camera_mode{
+      k_cam_firstperson = 1,
+      k_cam_thirdperson = 0
    }
-   mdl;
-}
-player = 
-{
-   .collide_front = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f },
-   .collide_back  = { .type = k_rb_shape_sphere, .inf.sphere.radius = 0.3f }
+   camera_mode;
+   f32 camera_type_blend;
+
+   v3f fpv_offset,         /* expressed relative to rigidbody */
+       tpv_offset,
+       tpv_offset_extra,
+       fpv_viewpoint,      /* expressed relative to neck bone inverse final*/
+       fpv_offset_smooth,
+       fpv_viewpoint_smooth,
+       tpv_offset_smooth,
+       tpv_lpf,
+       cam_velocity_smooth;
+};
+
+struct player_subsystem_interface{
+   void(*system_register)(void);
+   void(*bind)(void);
+   void(*pre_update)(void);
+   void(*update)(void);
+   void(*post_update)(void);
+   void(*im_gui)(void);
+   void(*animate)(void);
+   void(*pose)( void *animator, player_pose *pose );
+   void(*effects)( void *animator, m4x3f *final_mtx, struct player_board *board,
+                   struct player_effects_data *effect_data );
+   void(*post_animate)(void);
+   void(*network_animator_exchange)( bitpack_ctx *ctx, void *data );
+   void(*sfx_oneshot)( u8 id, v3f pos, f32 volume );
+
+   void(*sfx_comp)(void *animator);
+   void(*sfx_kill)(void);
+
+   void *animator_data;
+   u32 animator_size;
+
+   const char *name;
 };
 
-/* 
- * 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);
-
-/* 
- * Submodules
- */
-#include "player_physics.h"
 #include "player_ragdoll.h"
+#include "player_render.h"
 #include "player_model.h"
-#include "player_animation.h"
-#include "player_audio.h"
-
-/* 
- * -----------------------------------------------------------------------------
- *                                    Events
- * -----------------------------------------------------------------------------
- */
-
-static void player_init(void)                                            /* 1 */
-{
-   rb_init( &player.phys.rb );
-   rb_init( &player.collide_front );
-   rb_init( &player.collide_back  );
-
-   vg_convar_push( (struct vg_convar){
-      .name = "walk_speed",
-      .data = &k_walkspeed,
-      .data_type = k_convar_dtype_f32,
-      .opt_f32 = { .clamp = 0 },
-      .persistent = 1
-   });
-
-   vg_convar_push( (struct vg_convar){
-      .name = "run_speed",
-      .data = &k_runspeed,
-      .data_type = k_convar_dtype_f32,
-      .opt_f32 = { .clamp = 0 },
-      .persistent = 1
-   });
-
-   vg_convar_push( (struct vg_convar){
-      .name = "walk_accel",
-      .data = &k_walk_accel,
-      .data_type = k_convar_dtype_f32,
-      .opt_f32 = { .clamp = 0 },
-      .persistent = 1
-   });
-
-   vg_convar_push( (struct vg_convar){
-      .name = "fc",
-      .data = &freecam,
-      .data_type = k_convar_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
-
-   vg_convar_push( (struct vg_convar){
-      .name = "fcs",
-      .data = &fc_speed,
-      .data_type = k_convar_dtype_f32,
-      .opt_f32 = { .clamp = 0 },
-      .persistent = 1
-   });
-
-       vg_function_push( (struct vg_cmd){
-               .name = "reset",
-               .function = reset_player
-       });
-
-   /* other systems */
-   vg_loader_highwater( player_model_init, player_model_free, NULL );
-}
-
-/* Deal with input etc */
-static void player_update_pre(void)
-{
-   struct player_phys *phys = &player.phys;
-
-   if( vg_get_button_down( "reset" ) )
-   {
-      player.is_dead = 0;
-      player.death_tick_allowance = 30;
-      player_restore_frame();
-
-      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();
-   }
-
-   if( vg_get_button_down( "switchmode" ) )
-   {
-      phys->on_board ^= 0x1;
 
-      if( phys->on_board )
-      {
-         v3_muladds( phys->rb.v, phys->rb.forward, 0.2f, phys->rb.v );
-      }
+/* subsystem headers */
+#include "player_walk.h"
+#include "player_skate.h"
+#include "player_dead.h"
+#include "player_drive.h"
+#include "player_basic_info.h"
+
+#include "player_replay.h"
+
+#define PLAYER_REWIND_FRAMES 60*4
+#define RESET_MAX_TIME 45.0
+
+static i32 k_cinema_fixed = 0;
+static f32 k_cinema       = 0.0f;
+static i32 k_invert_y     = 0;
+
+struct {
+   /* transform definition */
+   rigidbody rb;
+   v3f angles;
+
+   /*
+    * Camera management
+    * ---------------------------
+    */
+   camera cam;
+   struct player_cam_controller cam_control;
+   f32 cam_trackshake;
+
+   float cam_velocity_influence,
+         cam_velocity_coefficient,
+         cam_velocity_constant,
+         cam_velocity_coefficient_smooth,
+         cam_velocity_constant_smooth,
+         cam_velocity_influence_smooth;
+
+   v3f cam_land_punch, cam_land_punch_v;
+   ent_gate *gate_waiting;
+   int deferred_frame_record;
+
+   int immobile;
+
+   int rewinded_since_last_gate;
+
+   /* 
+    * Network
+    * --------------------------------------------------
+    */
+   u16 boundary_hash;
+   struct net_sfx {
+      u8 system, priority, id;
+      f32 subframe, volume;
+      v3f location;
    }
+   sfx_buffer[4];
+   u32 sfx_buffer_count;
+
+   /*
+    * Animation
+    * --------------------------------------------------
+    */
+
+   struct player_ragdoll  ragdoll;
+   struct player_model    fallback_model;
+   struct player_board    fallback_board;
+
+   u16 board_view_slot, playermodel_view_slot;
+
+   player_pose            pose;
+   player_pose            holdout_pose;
+   float                  holdout_time;
+
+   m4x3f                 *final_mtx;
+
+   /*
+    * Subsystems
+    * -------------------------------------------------
+    */
+
+   enum player_subsystem subsystem; /* .. prev */
+
+   /*
+    * Rendering
+    */
+   mdl_context skeleton_meta;
+   struct skeleton skeleton;
+
+   u8 id_hip,
+      id_chest,
+      id_ik_hand_l,
+      id_ik_hand_r,
+      id_ik_elbow_l,
+      id_ik_elbow_r,
+      id_head,
+      id_foot_l,
+      id_foot_r,
+      id_ik_foot_l,
+      id_ik_foot_r,
+      id_ik_knee_l,
+      id_ik_knee_r,
+      id_wheel_l,
+      id_wheel_r,
+      id_board,
+      id_eyes,
+      id_world;
+
+   u8 skeleton_mirror[32];
+
+   struct player_effects_data effect_data;
 }
-
-static void player_update_fixed(void)                                    /* 2 */
-{
-   if( player.death_tick_allowance )
-      player.death_tick_allowance --;
-
-   struct player_phys *phys = &player.phys;
-
-   if( player.is_dead )
-   {
-      player_ragdoll_iter();
-   }
-   else
-   {
-      player_do_motion();
+static localplayer = {
+   .rb = {
+      .co = { 0,0,0 },
+      .w = { 0,0,0 },
+      .v = { 0,0,0 },
+      .q = { 0,0,0,1 },
+      .to_world = M4X3_IDENTITY,
+      .to_local = M4X3_IDENTITY
    }
+};
 
-   player_audio(); /* FUTURE: can probably move this to post()
-                              BUT, it uses deltas from fixed step physics,
-                              AND this *might* be what we want for realtime
-                              audio, anyway. */
-}
-
-static void player_update_post(void)
-{
-   for( int i=0; i<player.land_log_count; i++ )
-      vg_line_cross( player.land_target_log[i], 
-            player.land_target_colours[i], 0.25f);
-
-   if( player.is_dead )
-   {
-      player_debug_ragdoll();
-
-      if( !freecam )
-         player_animate_death_cam();
-   }
-   else
-   {
-      player_animate();
-
-      if( !freecam )
-         player_animate_camera();
-   }
-
-   if( freecam )
-      player_freecam();
-
-   player_camera_update();
-}
-
-static void draw_player(void)                                            /* 3 */
-{
-   if( player.is_dead )
-      player_model_copy_ragdoll();
-
-   shader_viewchar_use();
-   vg_tex2d_bind( &tex_characters, 0 );
-   shader_viewchar_uTexMain( 0 );
-   shader_viewchar_uCamera( player.camera[3] );
-   shader_viewchar_uPv( vg.pv );
-   shader_link_standard_ub( _shader_viewchar.id, 2 );
-   glUniformMatrix4x3fv( _uniform_viewchar_uTransforms, 
-                         player.mdl.sk.bone_count,
-                         0,
-                         (float *)player.mdl.sk.final_mtx );
-   
-   mesh_bind( &player.mdl.mesh );
-   mesh_draw( &player.mdl.mesh );
-}
+struct player_subsystem_interface static *player_subsystems[] = {
+   [k_player_subsystem_walk]  = &player_subsystem_walk,
+   [k_player_subsystem_dead]  = &player_subsystem_dead,
+   [k_player_subsystem_drive] = &player_subsystem_drive,
+   [k_player_subsystem_skate] = &player_subsystem_skate,
+   [k_player_subsystem_basic_info]=&player_subsystem_basic_info
+};
 
-/* 
- * -----------------------------------------------------------------------------
- *                              API implementation
- * -----------------------------------------------------------------------------
+/*
+ * Gameloop tables
+ * ---------------------------------------------------------
  */
 
-static float *player_get_pos(void)
-{
-   return player.phys.rb.co;
-}
-
-static void player_kill(void)
-{
-   if( player.death_tick_allowance == 0 )
-   {
-      player.is_dead = 1;
-      player_ragdoll_copy_model( player.phys.rb.v );
-   }
-}
-
-static float *player_cam_pos(void)
-{
-   return player.camera_pos;
-}
-
+static void player__debugtext( int size, const char *fmt, ... );
+static void player__use_mesh( glmesh *mesh );
+static void player__use_texture( vg_tex2d *tex );
+static void player__use_model( u16 reg_id );
+
+static void player__bind(void);
+static void player__pre_update(void);
+static void player__update(void);
+static void player__post_update(void);
+
+static void player__pass_gate( u32 id );
+static void player__im_gui(void);
+static void player__setpos( v3f pos );
+static void player__spawn( ent_spawn *rp );
+static void player__clean_refs(void);
+static void player__reset(void);
+static void player__kill(void);
+static void player__begin_holdout( v3f offset );
+
+static int localplayer_cmd_respawn( int argc, const char *argv[] );
+static void player_apply_transport_to_cam( m4x3f transport );
+
+static void player__clear_sfx_buffer(void);
+static void player__networked_sfx( u8 system, u8 priority, u8 id, 
+                                   v3f pos, f32 volume );
+static void net_sfx_exchange( bitpack_ctx *ctx, struct net_sfx *sfx );
+static void net_sfx_play( struct net_sfx *sfx );
 
 #endif /* PLAYER_H */