X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=player.c;h=a60c8c2613aae02452ee8aae98bd0be3724847ab;hb=HEAD;hp=0339a8d889f87c0faa08bf6b2443cf2bba9e783b;hpb=bc40c302f1b8313bdd3c773fcfa2f850511c4634;p=carveJwlIkooP6JGAAIwe30JlM.git diff --git a/player.c b/player.c index 0339a8d..a60c8c2 100644 --- a/player.c +++ b/player.c @@ -1,15 +1,57 @@ -#ifndef PLAYER_C -#define PLAYER_C - #include "player.h" +#include "addon.h" #include "camera.h" #include "player_model.h" #include "input.h" +#include "world.h" +#include "audio.h" +#include "player_replay.h" +#include "network.h" +#include "network_common.h" +#include "world_routes.h" +#include "ent_miniworld.h" +#include "gui.h" + +#include "shaders/model_entity.h" +#include "shaders/model_character_view.h" +#include "shaders/model_board_view.h" + +#include "player_walk.h" +#include "player_dead.h" +#include "player_drive.h" +#include "player_skate.h" +#include "player_basic_info.h" +#include "player_glide.h" +#include + +i32 k_invert_y = 0; +struct localplayer 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 + } +}; -VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] ) +struct player_subsystem_interface *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, + [k_player_subsystem_glide] = &player_subsystem_glide, +}; + +int localplayer_cmd_respawn( int argc, const char *argv[] ) { ent_spawn *rp = NULL, *r; - world_instance *world = localplayer.viewable_world; + world_instance *world = world_current_instance(); if( argc == 1 ){ rp = world_find_spawn_by_name( world, argv[0] ); @@ -21,24 +63,36 @@ VG_STATIC int localplayer_cmd_respawn( int argc, const char *argv[] ) if( !rp ) return 0; - player__spawn( &localplayer, rp ); + player__spawn( rp ); return 1; } -VG_STATIC void player_init(void) +void player_init(void) { - for( u32 i=0; isystem_register ) sys->system_register(); } vg_console_reg_cmd( "respawn", localplayer_cmd_respawn, NULL ); + VG_VAR_F32( k_cam_damp ); + VG_VAR_F32( k_cam_spring ); + VG_VAR_F32( k_cam_punch ); + VG_VAR_F32( k_cam_shake_strength ); + VG_VAR_F32( k_cam_shake_trackspeed ); + VG_VAR_I32( k_player_debug_info, flags=VG_VAR_PERSISTENT ); + +#if 0 + vg_console_reg_var( "cinema", &k_cinema, k_var_dtype_f32, 0 ); + vg_console_reg_var( "cinema_fixed", &k_cinema_fixed, k_var_dtype_i32, 0 ); +#endif + vg_console_reg_var( "invert_y", &k_invert_y, + k_var_dtype_i32, VG_VAR_PERSISTENT ); } -PLAYER_API void player__debugtext( int size, const char *fmt, ... ) { -#if 0 char buffer[ 1024 ]; va_list args; @@ -46,57 +100,29 @@ void player__debugtext( int size, const char *fmt, ... ) vsnprintf( buffer, 1024, fmt, args ); va_end( args ); - ui_text( vg_uictx.cursor, buffer, size, k_text_align_right ); - vg_uictx.cursor[1] += 14*size; -#endif -} - -/* - * Init - */ -PLAYER_API -void player__create( player_instance *inst ) -{ - static int only_once = 0; - assert( only_once == 0 ); - only_once ++; - - 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 ); - + ui_text( g_player_debugger, buffer, size, k_ui_align_left, 0 ); + g_player_debugger[1] += size*16; } /* * Appearence */ -PLAYER_API -void player__use_avatar( player_instance *player, struct player_avatar *av ) -{ - player->playeravatar = av; - player_setup_ragdoll_from_avatar( &player->ragdoll, av ); -} -PLAYER_API -void player__use_model( player_instance *player, struct player_model *mdl ) +void player__use_model( u16 reg_id ) { - player->playermodel = mdl; + addon_cache_unwatch( k_addon_type_player, + localplayer.playermodel_view_slot ); + localplayer.playermodel_view_slot = + addon_cache_create_viewer( k_addon_type_player, reg_id ); } -PLAYER_API -void player__bind( player_instance *player ) +void player__bind(void) { - for( u32 i=0; ibind ) sys->bind(); } } @@ -105,245 +131,294 @@ void player__bind( player_instance *player ) * ---------------------------------------------------------------------------- */ -VG_STATIC void player_save_rewind_frame( player_instance *player ) +void player__pre_update(void) { - 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( 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; + if( button_down( k_srbind_camera ) && !localplayer.immobile && + (localplayer.subsystem != k_player_subsystem_dead) ){ + if( localplayer.cam_control.camera_mode == k_cam_firstperson ) + localplayer.cam_control.camera_mode = k_cam_thirdperson; else - player->camera_mode = k_cam_firstperson; + localplayer.cam_control.camera_mode = k_cam_firstperson; } - if( _player_pre_update[ player->subsystem ] ) - _player_pre_update[ player->subsystem ]( player ); + if( player_subsystems[ localplayer.subsystem ]->pre_update ) + player_subsystems[ localplayer.subsystem ]->pre_update(); } -PLAYER_API -void player__update( player_instance *player ) +void player__update(void) { - if( player->rewinding ) - return; + if( player_subsystems[ localplayer.subsystem ]->update ) + player_subsystems[ localplayer.subsystem ]->update(); - if( _player_update[ player->subsystem ] ) - _player_update[ player->subsystem ]( player ); + if( localplayer.glider_orphan && + (skaterift.activity != k_skaterift_replay) ) + glider_physics( (v2f){0,0} ); } -PLAYER_API -void player__post_update( player_instance *player ) +void player__post_update(void) { - if( player->rewinding ) - return; - - if( _player_post_update[ player->subsystem ] ) - _player_post_update[ player->subsystem ]( player ); + struct player_subsystem_interface *sys = + player_subsystems[ localplayer.subsystem ]; - if((player->subsystem != k_player_subsystem_dead) && !player->gate_waiting){ - player->rewind_accum += vg.time_frame_delta; + if( sys->post_update ) sys->post_update(); - if( player->rewind_accum > 0.25f ){ - player->rewind_accum -= 0.25f; - player_save_rewind_frame( player ); - } - } + SDL_AtomicLock( &air_audio_data.sl ); + air_audio_data.speed = v3_length( localplayer.rb.v ) * vg.time_rate; + SDL_AtomicUnlock( &air_audio_data.sl ); } /* * Applies gate transport to a player_interface */ -PLAYER_API -void player__pass_gate( player_instance *player, ent_gate *gate ) +void player__pass_gate( u32 id ) { - 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, - player->cam_velocity_smooth ); - - m3x3_copy( player->basis, player->basis_gate ); + world_instance *world = world_current_instance(); + skaterift_record_frame( &player_replay.local, 1 ); + + /* update boundary hash (network animation) */ + u16 index = mdl_entity_id_id(id) & ~NETMSG_BOUNDARY_MASK; + localplayer.boundary_hash ^= NETMSG_GATE_BOUNDARY_BIT; + localplayer.boundary_hash &= ~NETMSG_BOUNDARY_MASK; + localplayer.boundary_hash |= index; + + ent_gate *gate = mdl_arritm( &world->ent_gate, mdl_entity_id_id(id) ); + world_routes_fracture( world, gate, localplayer.rb.co, localplayer.rb.v ); - v4f q; - m3x3_q( gate->transport, q ); - q_mul( q, player->qbasis, player->qbasis ); - q_normalize( player->qbasis ); - q_m3x3( player->qbasis, player->basis ); - m3x3_transpose( player->basis, player->invbasis ); + localplayer.gate_waiting = gate; + localplayer.deferred_frame_record = 1; - 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; + struct player_cam_controller *cc = &localplayer.cam_control; + m4x3_mulv( gate->transport, cc->tpv_lpf, cc->tpv_lpf ); + m3x3_mulv( gate->transport, cc->cam_velocity_smooth, + cc->cam_velocity_smooth ); - m4x3_mulv( gate->transport, player->cam.pos, player->cam.pos ); - player_save_rewind_frame( player ); + m4x3_mulv( gate->transport, localplayer.cam.pos, localplayer.cam.pos ); - if( gate->type == k_gate_type_nonlocel ) - world_global.active_world = gate->target; + if( gate->flags & k_ent_gate_nonlocal ) + { + world_default_spawn_pos( world, world->player_co ); + world_static.active_instance = gate->target; + player__clean_refs(); - world_global.in_volume = 0; + replay_clear( &player_replay.local ); + } + else + { + world_routes_activate_entry_gate( world, gate ); + } + + v3f v0; + v3_angles_vector( localplayer.angles, v0 ); + m3x3_mulv( gate->transport, v0, v0 ); + v3_angles( v0, localplayer.angles ); audio_lock(); audio_oneshot( &audio_gate_pass, 1.0f, 0.0f ); audio_unlock(); } -VG_STATIC void player_apply_transport_to_cam( m4x3f transport ) +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 ); + m4x4_mul( skaterift.cam.mtx.pv, transport_4, skaterift.cam.mtx.pv ); + m4x4_mul( skaterift.cam.mtx.v, transport_4, skaterift.cam.mtx.v ); /* 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 ); + m4x4_mul( world_gates.cam.mtx.pv, transport_4, world_gates.cam.mtx.pv ); + m4x4_mul( world_gates.cam.mtx.v, transport_4, world_gates.cam.mtx.v ); } -__attribute__ ((deprecated)) -VG_STATIC void gate_rotate_angles( ent_gate *gate, v3f angles, v3f d ) +void player__im_gui(void) { - v3_copy( angles, d ); - return; + if( !k_player_debug_info ) return; + + ui_rect box = { + vg.window_x - 300, + 0, + 300, + vg.window_y + }; + + ui_fill( box, (ui_colour(k_ui_bg)&0x00ffffff)|0x50000000 ); + + g_player_debugger[0] = box[0]; + g_player_debugger[1] = 0; + g_player_debugger[2] = 300; + g_player_debugger[3] = 32; + + player__debugtext( 2, "instance #%u", world_static.active_instance ); + + char buf[96]; + for( u32 i=0; ialias, buf ); + else + strcpy( buf, "none" ); + + player__debugtext( 1, "world #%u: %s", i, buf ); + } + + player__debugtext( 2, "director" ); + player__debugtext( 1, "activity: %s", + (const char *[]){ [k_skaterift_menu] = "menu", + [k_skaterift_replay] = "replay", + [k_skaterift_ent_focus] = "ent_focus", + [k_skaterift_default] = "default", + [k_skaterift_world_map] = "world map" + } [skaterift.activity] ); + player__debugtext( 1, "time_rate: %.4f", skaterift.time_rate ); + + player__debugtext( 2, "player" ); + player__debugtext( 1, "angles: " PRINTF_v3f( localplayer.cam.angles ) ); - v3f fwd_dir = { cosf(angles[0]), - 0.0f, - sinf(angles[0])}; - m3x3_mulv( gate->transport, fwd_dir, fwd_dir ); + if( player_subsystems[ localplayer.subsystem ]->im_gui ) + player_subsystems[ localplayer.subsystem ]->im_gui(); - v3_copy( angles, d ); - d[0] = atan2f( fwd_dir[2], fwd_dir[0] ); + skaterift_replay_debug_info(); } -PLAYER_API void player__im_gui( player_instance *player ) +void player__setpos( v3f pos ) { -#if 0 - vg_uictx.cursor[0] = vg.window_x - 200; - vg_uictx.cursor[1] = 0; - vg_uictx.cursor[2] = 200; - vg_uictx.cursor[3] = 200; + v3_copy( pos, localplayer.rb.co ); + v3_zero( localplayer.rb.v ); + rb_update_matrices( &localplayer.rb ); +} + +void player__clean_refs(void) +{ + replay_clear( &player_replay.local ); + gui_helper_clear(); + + world_static.challenge_target = NULL; + world_static.challenge_timer = 0.0f; + world_static.active_trigger_volume_count = 0; + world_static.last_use = 0.0; + world_entity_exit_modal(); + world_entity_clear_focus(); + + localplayer.boundary_hash ^= NETMSG_BOUNDARY_BIT; + + for( u32 i=0; istatus == k_world_status_loaded ){ + world_routes_clear( instance ); + } + } +} - struct ui_vert *b = ui_fill_rect( vg_uictx.cursor, 0x70000000 ); +void player__reset(void) +{ + v3_zero( localplayer.rb.v ); + v3_zero( localplayer.rb.w ); + + f32 l = v4_length( localplayer.rb.q ); + if( (l < 0.9f) || (l > 1.1f) ) + q_identity( localplayer.rb.q ); - vg_uictx.cursor[0] = vg.window_x; + rb_update_matrices( &localplayer.rb ); - player__debugtext( 1, "angles: " PRINTF_v3f( player->cam.angles ) ); - player__debugtext( 1, "basis: " PRINTF_v4f( player->qbasis ) ); + localplayer.subsystem = k_player_subsystem_walk; + player__walk_reset(); - if( _player_im_gui[ player->subsystem ] ) - _player_im_gui[ player->subsystem ]( player ); + localplayer.immobile = 0; + localplayer.gate_waiting = NULL; + localplayer.have_glider = 0; + localplayer.glider_orphan = 0; + localplayer.drowned = 0; - b[2].co[1] = vg_uictx.cursor[1]; - b[3].co[1] = vg_uictx.cursor[1]; -#endif + v3_copy( localplayer.rb.co, localplayer.cam_control.tpv_lpf ); + player__clean_refs(); } -VG_STATIC void global_skateshop_exit(void); -PLAYER_API void player__spawn( player_instance *player, - ent_spawn *rp ) +void player__spawn( ent_spawn *rp ) { - v3_copy( rp->transform.co, player->rb.co ); - v3_zero( player->rb.v ); - v3_zero( player->rb.w ); - q_identity( player->rb.q ); - rb_update_transform( &player->rb ); - - q_identity( player->qbasis ); - m3x3_identity( player->basis ); - m3x3_identity( player->invbasis ); + player__setpos( rp->transform.co ); + player__reset(); +} - player->subsystem = k_player_subsystem_walk; - player->immobile = 0; - player->gate_waiting = NULL; - global_skateshop_exit(); +void player__kill(void) +{ +} - if( _player_reset[ player->subsystem ] ) - _player_reset[ player->subsystem ]( player, rp ); +void player__begin_holdout( v3f offset ) +{ + memcpy( &localplayer.holdout_pose, &localplayer.pose, + sizeof(localplayer.pose) ); + v3_copy( offset, localplayer.holdout_pose.root_co ); + localplayer.holdout_time = 1.0f; } +void net_sfx_exchange( bitpack_ctx *ctx, struct net_sfx *sfx ) +{ + bitpack_bytes( ctx, 1, &sfx->system ); + bitpack_bytes( ctx, 1, &sfx->priority ); + bitpack_bytes( ctx, 1, &sfx->id ); + bitpack_qf32( ctx, 8, 0.0f, 1.0f, &sfx->subframe ); + bitpack_qf32( ctx, 8, 0.0f, 1.0f, &sfx->volume ); + bitpack_qv3f( ctx, 16, -1024.0f, 1024.0f, sfx->location ); +} -PLAYER_API void player__kill( player_instance *player ) +void net_sfx_play( struct net_sfx *sfx ) { - + if( sfx->system < k_player_subsystem_max ){ + struct player_subsystem_interface *sys = player_subsystems[sfx->system]; + if( sys->sfx_oneshot ){ + sys->sfx_oneshot( sfx->id, sfx->location, sfx->volume ); + } + } +}; + +static struct net_sfx *find_lower_priority_sfx( struct net_sfx *buffer, u32 len, + u32 *count, u8 priority ){ + struct net_sfx *p_sfx = NULL; + if( *count < len ){ + p_sfx = &buffer[ *count ]; + *count = *count+1; + } + else { + for( u32 i=0; ipriority < priority ){ + p_sfx = a; + break; + } + } + } + + return p_sfx; } -#endif /* PLAYER_C */ +void player__networked_sfx( u8 system, u8 priority, u8 id, + v3f pos, f32 volume ) +{ + struct net_sfx sfx, + *p_net = find_lower_priority_sfx( + localplayer.sfx_buffer, 4, + &localplayer.sfx_buffer_count, priority ), + *p_replay = find_lower_priority_sfx( + localplayer.local_sfx_buffer, 2, + &localplayer.local_sfx_buffer_count, priority ); + + sfx.id = id; + sfx.priority = priority; + sfx.volume = volume; + v3_copy( pos, sfx.location ); + sfx.system = system; + + /* we only care about subframe in networked sfx. local replays run at a + * high enough framerate. */ + f32 t = (vg.time_real - network_client.last_frame) / NETWORK_FRAMERATE; + sfx.subframe = vg_clampf( t, 0.0f, 1.0f ); + + if( p_net ) *p_net = sfx; + if( p_replay ) *p_replay = sfx; + + net_sfx_play( &sfx ); +}