X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=player.h;h=18936a7ade2248844468cc7e182299c46ea14042;hb=6e6c7f31b8f17af3814727109e48fc6f85ef04b1;hp=c8c9abc54b8932ecd480838014a44001ca9c4dfb;hpb=6294ef64d948eab2365e39a2645c9843aa96fba8;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/player.h b/player.h index c8c9abc..18936a7 100644 --- a/player.h +++ b/player.h @@ -1,704 +1,218 @@ -/* - * Copyright (C) 2021-2022 Mt.ZERO Software, Harry Godden - All Rights Reserved - */ - #ifndef PLAYER_H #define PLAYER_H -#define PLAYER_REWIND_FRAMES 60*4 - -#include "audio.h" -#include "common.h" -#include "world.h" -#include "skeleton.h" -#include "bvh.h" - -VG_STATIC float - k_walkspeed = 12.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_lat = 12.0f, - 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; - -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; - -/* - * ----------------------------------------------------------------------------- - * Memory - * ----------------------------------------------------------------------------- - */ - -VG_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, rewinding; - int rewind_sound_wait; - - v3f land_target; - v3f land_target_log[22]; - u32 land_target_colours[22]; - int land_log_count; - - 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; - - struct rewind_frame - { - v3f pos; - v2f ang; +struct player_cam_controller { + enum camera_mode{ + k_cam_firstperson = 1, + k_cam_thirdperson = 0 } - *rewind_buffer; - u32 rewind_incrementer, - rewind_length; - - float rewind_time, rewind_total_length, rewind_predicted_time; - double diag_rewind_start, diag_rewind_time; - float dist_accum; - - /* animation */ - double jump_time; - float fslide, - fdirz, fdirx, - fstand, - ffly, - fpush, - fairdir, - fsetup, - walk_timer, - fjump, - fonboard, - frun; - - float walk; - int step_phase; - enum mdl_surface_prop surface_prop; - - /* player model */ - struct player_model - { - glmesh player_meshes[3]; - - mdl_context meta; - 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[32]; - u32 ragdoll_count; - - int shoes[2]; - } - 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; }; -/* - * API - */ -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); +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 *animator_data; + u32 animator_size; + + const char *name; +}; -#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 - * ----------------------------------------------------------------------------- - */ - -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; iang ); - 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 */ -VG_STATIC void player_update_pre(void) -{ - struct player_phys *phys = &player.phys; - - if( player.rewinding ) - { - return; - } +#include "player_replay.h" - if( vg_input_button_down( player.input_reset ) ) - { - if( player.is_dead ) - { - reset_player( 0, NULL ); - audio_lock(); - audio_play_oneshot( &audio_ui[0], 1.0f ); - audio_unlock(); - } - else - { - double delta = world.time - world.last_use; - - 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; - - 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(); - - /* 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_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(); -} - -VG_STATIC void player_update_fixed(void) /* 2 */ -{ - if( player.rewinding ) - return; - - if( player.death_tick_allowance ) - player.death_tick_allowance --; - - struct player_phys *phys = &player.phys; - - if( player.is_dead ) - { - player_ragdoll_iter(); - } - else - { - player.rewind_incrementer ++; - - if( player.rewind_incrementer > (u32)(0.25/VG_TIMESTEP_FIXED) ) - { - player_save_rewind_frame(); - } - - player_do_motion(); +#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 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; + + u32 id_hip, + id_chest, + id_ik_hand_l, + id_ik_hand_r, + id_ik_elbow_l, + id_ik_elbow_r, + id_head, + 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; + + struct player_effects_data effect_data; } - -VG_STATIC void player_update_post(void) -{ - for( int i=0; i 0 ); - - v2f override_angles; - v3f override_pos; - - float budget = vg.time_delta, - 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 ++ ) - { - /* Interpolate frames */ - int i0 = floorf( player.rewind_time ), - i1 = VG_MIN( i0+1, player.rewind_length-1 ); - - struct rewind_frame *fr = &player.rewind_buffer[i0], - *fr1 = &player.rewind_buffer[i1]; - - float dist = vg_maxf( v3_dist( fr->pos, fr1->pos ), 0.001f ), - subl = vg_fractf( player.rewind_time ) + 0.001f, - - sramp= 3.0f-(1.0f/(0.4f+0.4f*player.rewind_time)), - speed = sramp*28.0f + 0.5f*player.rewind_time, - mod = speed * (budget / dist), - - 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 ); - - struct rewind_frame *fr = &player.rewind_buffer[i0], - *fr1 = &player.rewind_buffer[i1]; - - float sub = vg_fractf(player.rewind_time); - - v3_lerp( fr->pos, fr1->pos, sub, override_pos ); - override_angles[0] = vg_alerpf( fr->ang[0], fr1->ang[0], sub ); - override_angles[1] = vg_lerpf ( fr->ang[1], fr1->ang[1], sub ); - - /* CAMERA POSITIONING: LAYER 1 */ - float blend = (4.0f-player.rewind_time) * 0.25f, - c = vg_clampf( blend, 0.0f, 1.0f ); - - camera_angles[0] = vg_alerpf(override_angles[0], player.angles[0], c); - camera_angles[1] = vg_lerpf (override_angles[1], player.angles[1], c); - v3_lerp( override_pos, player.camera_pos, c, camera_pos ); - } - } - - camera_update(); - player_audio(); -} +}; -VG_STATIC void draw_player( m4x3f cam ) -{ - if( player.is_dead ) - player_model_copy_ragdoll(); - - shader_viewchar_use(); - vg_tex2d_bind( &tex_characters, 0 ); - shader_viewchar_uTexMain( 0 ); - shader_viewchar_uCamera( cam[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.player_meshes[cl_playermdl_id] ); - mesh_draw( &player.mdl.player_meshes[cl_playermdl_id] ); -} +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 + * --------------------------------------------------------- */ -VG_STATIC float *player_get_pos(void) -{ - return player.phys.rb.co; -} - -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(); - } -} - -VG_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 */