Advertisement
psy_commando

vehicle_es_test.cpp

May 7th, 2012
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 26.96 KB | None | 0 0
  1. #include "cbase.h"
  2. #include <string>
  3. #include "in_buttons.h"
  4. #include "physics.h"
  5. #include "convar_serverbounded.h"
  6.  
  7. #ifdef CLIENT_DLL
  8.    #include "c_prop_sfr_vehicle.h"
  9.    #include "asw_input.h"
  10.    #define  CPropSFRVehicle C_PropSFRVehicle
  11.    #define CESTestVehicle C_ESTestVehicle
  12.    #define CBasePlayer C_BasePlayer
  13. #else
  14.    #include "prop_sfr_vehicle.h"
  15. #endif
  16.  
  17. static const Vector m_angTurnMax( 75.0f, 75.0f, 45.0f );
  18. static const Vector m_angAccelMax( 110.0f, 300.0f, 140.0f );
  19. static const Vector m_vecAccelMax( 60.0f, 30.0f, 40.0f );
  20.  
  21. class CESTestVehicle : public CPropSFRVehicle
  22. {
  23. public:
  24.    DECLARE_CLASS( CESTestVehicle, CPropSFRVehicle );
  25.    DECLARE_NETWORKCLASS();
  26. #ifdef CLIENT_DLL
  27.    DECLARE_PREDICTABLE();
  28.    DECLARE_INTERPOLATION();
  29. #else
  30.    DECLARE_DATADESC();
  31. #endif
  32.  
  33.    CESTestVehicle();
  34.  
  35.    virtual void Precache()
  36.    {
  37.       PrecacheModel( c_VEHICLE_MODEL.c_str() );
  38.       BaseClass::Precache();
  39.    }
  40.  
  41.    virtual bool ShouldRegenerateOriginFromCellBits()const
  42.    {
  43.       return false; //we don't use cells anymore
  44.    }
  45.  
  46.    virtual void Spawn();
  47.    void InitPhysics();
  48.    virtual bool IsPredicted() const {return true;}
  49.    virtual CBasePlayer  *GetPredictionOwner( void ){ return ToBasePlayer( GetPassenger( 0 ) ); }
  50.  
  51.    virtual void DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased );
  52.    //virtual void SetupMove( C_BasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  53.     void SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move );
  54.     void ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData );
  55.     void FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move );
  56.     void ItemPostFrame( CBasePlayer *pPlayer ); // AFAIK used for weapon stuff, though the inputs up there can be used too.
  57.  
  58.    void CommitInputs(float Interval);
  59.  
  60.  
  61. #ifdef GAME_DLL
  62.    virtual int ShouldTransmit( const CCheckTransmitInfo *pInfo );
  63.     virtual int UpdateTransmitState(); 
  64.     //void SetTransmit( CCheckTransmitInfo *pInfo, bool bAlways );
  65.  
  66.     //  VPhysics stuff.
  67.     virtual void VPhysicsShadowUpdate( IPhysicsObject *pPhysics );
  68.     virtual void VPhysicsPreCollision( int index, gamevcollisionevent_t *pEvent );
  69.     virtual void VPhysicsShadowCollision( int index, gamevcollisionevent_t *pEvent );
  70. #endif
  71.  
  72.    virtual void VPhysicsUpdate( IPhysicsObject *pPhysics );
  73.  
  74.     //  Custom physics stuff.
  75.     void PhysicsSimulate( void );
  76.  
  77. #ifdef CLIENT_DLL
  78.    virtual void ClientThink();
  79.    
  80.    virtual bool ShouldPredict();
  81.    virtual bool ShouldInterpolate();
  82.     virtual void PostDataUpdate( DataUpdateType_t updateType );
  83.     virtual void OnDataChanged(DataUpdateType_t type);
  84.     virtual void UpdateViewAngles( C_BasePlayer *pLocalPlayer, CUserCmd *pCmd );
  85.  
  86.    virtual const Vector & GetRenderOrigin( void );
  87.  
  88.  
  89.     // Called by prediction when it detects a prediction correction.
  90.     // vDelta is the line from where the client had predicted the player to at the usercmd in question,
  91.     // to where the server says the client should be at said usercmd.
  92.     void NotePredictionError( const Vector &vDelta );
  93.    
  94.     // Called by the renderer to apply the prediction error smoothing.
  95.     void GetPredictionErrorSmoothingVector( Vector &vOffset );
  96.  
  97. #else
  98.    virtual void Think();
  99. #endif
  100.    virtual void GetVehicleViewPosition( int nRole, Vector *pOrigin, QAngle *pAngles, float *pFOV = NULL );
  101.  
  102. protected:
  103.    static const std::string c_VEHICLE_MODEL;
  104.  
  105.    //CNetworkVar( QAngle, m_angCur );
  106.    //CNetworkVar( QAngle, m_angTarget );
  107.    CNetworkVector( m_vOffset );
  108.    QAngle m_angLastViewAngles;
  109.    AngularImpulse m_anginVehicleTurning;
  110.  
  111.    CNetworkVar( Vector, m_vVphysicsPos );
  112.    CNetworkVar( QAngle, m_angVPhysAngles );
  113.  
  114.    CNetworkVar( Vector, m_vNetPos );
  115.    CNetworkVar( QAngle, m_angNetAngles );
  116.  
  117.    //es
  118.    CNetworkVector( m_vecEntityAngVelocity );
  119.    Vector       m_vecMetersVelocity;
  120.    Vector   input_torque;
  121.     Vector  input_force;
  122.    Vector m_vecCustomRenderOrigin;
  123.  
  124.  
  125. #ifdef CLIENT_DLL
  126.    Vector m_vecPredictionError;
  127.     float m_flPredictionErrorTime;
  128. #endif
  129.  
  130.    float m_fForce;
  131.  
  132. };
  133.  
  134. const std::string CESTestVehicle::c_VEHICLE_MODEL("models/arwing/arwing.mdl");
  135.  
  136. PRECACHE_REGISTER( CESTestVehicle );
  137.  
  138. IMPLEMENT_NETWORKCLASS_ALIASED( ESTestVehicle, DT_ESTestVehicle );
  139.  
  140. BEGIN_NETWORK_TABLE( CESTestVehicle, DT_ESTestVehicle )
  141. #ifdef CLIENT_DLL
  142.    //RecvPropQAngles( RECVINFO( m_angCur ) ),
  143.    //RecvPropQAngles( RECVINFO( m_angTarget ) ),
  144.    RecvPropVector( RECVINFO( m_vOffset ) ),
  145.    RecvPropVector( RECVINFO( m_vVphysicsPos ) ),
  146.    RecvPropQAngles( RECVINFO( m_angVPhysAngles ) ),
  147.    RecvPropVector( RECVINFO( m_vNetPos ) ),
  148.    RecvPropQAngles( RECVINFO( m_angNetAngles ) ),
  149.    RecvPropVector( RECVINFO( m_vecEntityAngVelocity ) ),
  150.  
  151.    RecvPropVectorXY( RECVINFO_NAME( m_vecNetworkOrigin, m_vecOrigin ), 0, C_BasePlayer::RecvProxy_LocalOriginXY ),
  152.     RecvPropFloat( RECVINFO_NAME( m_vecNetworkOrigin[2], m_vecOrigin[2] ), 0, C_BasePlayer::RecvProxy_LocalOriginZ ),
  153.  
  154.    //RecvPropVector     ( RECVINFO_NAME( m_vecMetersVelocity, m_vecVelocity ) ),
  155. #else
  156.    //SendPropQAngles( SENDINFO( m_angCur ) ),
  157.    //SendPropQAngles( SENDINFO( m_angTarget ) ),
  158.    SendPropVector( SENDINFO( m_vOffset ) ),
  159.    SendPropVector( SENDINFO( m_vVphysicsPos ) ),
  160.    SendPropQAngles( SENDINFO( m_angVPhysAngles ) ),
  161.    SendPropVector( SENDINFO( m_vNetPos ) ),
  162.    SendPropQAngles( SENDINFO( m_angNetAngles ) ),
  163.    SendPropVector       ( SENDINFO( m_vecEntityAngVelocity ), 16, SPROP_CHANGES_OFTEN, -256.0f, 256.0f ), // Artificially high for collisions
  164.  
  165.     // We need to send a hi-res origin to avoid prediction errors sliding along walls
  166.     SendPropExclude( "DT_BaseEntity", "m_vecOrigin" ),
  167.     //// send a hi-res origin to the local player for use in prediction
  168.     ////SendPropVector  (SENDINFO(m_vecOrigin), -1,  SPROP_NOSCALE|SPROP_CHANGES_OFTEN, 0.0f, HIGH_DEFAULT, SendProxy_Origin ),
  169.     SendPropVectorXY(SENDINFO(m_vecOrigin),               -1, SPROP_NOSCALE | SPROP_CHANGES_OFTEN, 0.0f, HIGH_DEFAULT, SendProxy_OriginXY ),
  170.     SendPropFloat   (SENDINFO_VECTORELEM(m_vecOrigin, 2), -1, SPROP_NOSCALE | SPROP_CHANGES_OFTEN, 0.0f, HIGH_DEFAULT, SendProxy_OriginZ ),
  171.    
  172.  
  173.    //SendPropVector     ( SENDINFO( m_vecVelocity ), 32, SPROP_CHANGES_OFTEN ),
  174. #endif
  175. END_NETWORK_TABLE();
  176.  
  177. LINK_ENTITY_TO_CLASS( vehicle_es_test, CESTestVehicle );
  178.  
  179. #ifndef CLIENT_DLL
  180. BEGIN_DATADESC(CESTestVehicle)
  181.  
  182. END_DATADESC();
  183. #else
  184. BEGIN_PREDICTION_DATA(CESTestVehicle)
  185.    //DEFINE_PRED_FIELD( m_angCur, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
  186.    //DEFINE_PRED_FIELD( m_angTarget, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
  187.    DEFINE_PRED_FIELD( m_vOffset, FIELD_VECTOR, FTYPEDESC_INSENDTABLE ),
  188.    DEFINE_PRED_FIELD( m_vVphysicsPos, FIELD_VECTOR, FTYPEDESC_INSENDTABLE  ),
  189.    DEFINE_PRED_FIELD( m_angVPhysAngles, FIELD_VECTOR, FTYPEDESC_INSENDTABLE   ),
  190.    DEFINE_PRED_FIELD_TOL( m_vecEntityAngVelocity, FIELD_VECTOR, FTYPEDESC_INSENDTABLE, 0.5 ), // 15 bits over 1440 spread
  191.    //DEFINE_PRED_FIELD_TOL( m_vecMetersVelocity, FIELD_VECTOR, FTYPEDESC_INSENDTABLE, 0.05 ), // 18 bits over 512 spread
  192. END_PREDICTION_DATA();
  193. #endif
  194.  
  195. CESTestVehicle::CESTestVehicle()
  196. {
  197.    m_vOffset.GetForModify().Init(0,0,0);
  198.    //m_angCur.GetForModify().Init(0,0,0);
  199.    m_angLastViewAngles.Init();
  200.    m_angVPhysAngles.GetForModify().Init();
  201.    m_vVphysicsPos.GetForModify().Init();
  202.    m_anginVehicleTurning.Init();
  203.  
  204.    m_vecEntityAngVelocity.GetForModify().Init();
  205.    SetPredictionEligible( true );
  206.  
  207. #ifdef CLIENT_DLL
  208.    m_vecPredictionError.Init();
  209.    m_flPredictionErrorTime = 0;
  210. #endif
  211. }
  212.  
  213. #ifdef CLIENT_DLL
  214.    void CESTestVehicle::UpdateViewAngles( C_BasePlayer *pLocalPlayer, CUserCmd *pCmd )
  215.    {
  216.         // Blend the view angles.
  217.         //int eyeAttachmentIndex = LookupAttachment( "vehicle_driver_eyes" );
  218.         //Vector vehicleEyeOrigin;
  219.         //QAngle vehicleEyeAngles;
  220.         //GetAttachmentLocal( eyeAttachmentIndex, vehicleEyeOrigin, vehicleEyeAngles );
  221.  
  222.   //    QAngle outAngles;
  223.         //InterpolateAngles( pCmd->viewangles, vehicleEyeAngles, outAngles, 1.0f );
  224.         //pCmd->viewangles = outAngles;
  225.  
  226.       BaseClass::UpdateViewAngles(pLocalPlayer,pCmd);
  227.  
  228.  
  229.    }
  230. #endif
  231.  
  232. void CESTestVehicle::GetVehicleViewPosition( int nRole, Vector *pOrigin, QAngle *pAngles, float *pFOV )
  233. {
  234.    if( pOrigin )
  235.    {
  236. //      *pOrigin = GetAbsOrigin();
  237. //
  238. //#ifdef CLIENT_DLL
  239.       Vector eyePos;
  240.       QAngle eyeAngles;
  241.       GetAttachment( "vehicle_driver_eyes3rd", eyePos, eyeAngles );
  242.  
  243.       *pOrigin = eyePos;
  244. //#endif
  245.    }
  246.  
  247.    if( pAngles )
  248.    {
  249.       *pAngles = GetAbsAngles();/* + m_angCur;*/
  250.    }
  251.  
  252.    if( pFOV )
  253.    {
  254.       *pFOV = 110.0f;
  255.    }
  256. }
  257.  
  258. #ifdef CLIENT_DLL
  259.    void CESTestVehicle::ClientThink()
  260. #else
  261.    void CESTestVehicle::Think()
  262. #endif
  263. {
  264.    #ifdef CLIENT_DLL
  265.    //if( !physenv )
  266.    //   return;
  267.    //else if( !VPhysicsGetObject() )
  268.    //   InitPhysics();
  269.  
  270.    //Draw Client entity pos
  271.    NDebugOverlay::Cross3DOriented( GetAbsOrigin(), GetAbsAngles(), 10.0f, 0, 255, 0, true, 0.0f);
  272.  
  273.    #else
  274.  
  275.    //Draw Server entity pos
  276.    NDebugOverlay::Cross3DOriented( GetAbsOrigin(), GetAbsAngles(), 10.0f, 255, 0, 0, true, 0.0f);
  277.  
  278.    #endif
  279.  
  280.     //SetSimulationTime( gpGlobals->curtime );
  281.     //SetNextThink( gpGlobals->curtime );
  282.     //SetAnimatedEveryTick( true );
  283.  
  284.    //if( VPhysicsGetObject() )
  285.    //{
  286.       //Vector pos = GetAbsOrigin();
  287.       //QAngle ang = GetAbsAngles();
  288.  
  289.    //   //Draw Vphysics entity pos
  290.    //   NDebugOverlay::Cross3DOriented( pos, ang, 10.0f, 128, 128, 0, true, 0.0f );
  291.  
  292.    //#ifdef CLIENT_DLL
  293.    //   Vector Smoothed;
  294.    //   Smoothed = VectorLerp( m_vVphysicsPos.Get(), pos, 1.0f/gpGlobals->frametime );
  295.    //   SetAbsOrigin(Smoothed);
  296.    //   SetAbsAngles(m_angVPhysAngles.Get());
  297.    //#else
  298.    //   m_vVphysicsPos = pos;
  299.    //   m_angVPhysAngles = ang;
  300.    //#endif
  301.    //}
  302.  
  303.  
  304.    //if( VPhysicsGetObject() )
  305.    //{
  306.    //   m_anginVehicleTurning = m_anginVehicleTurning * 10.0f;
  307.  
  308.    //   Vector vel, forward;
  309.    //   GetVectors( &forward, NULL, NULL );
  310.    //   vel = m_fForce * forward;
  311.  
  312.    //   VPhysicsGetObject()->SetVelocity( &vel, &m_anginVehicleTurning );
  313.    //}
  314.  
  315. #ifdef CLIENT_DLL
  316.    BaseClass::ClientThink();
  317. #else
  318.    BaseClass::Think();
  319. #endif
  320. }
  321.  
  322. void CESTestVehicle::Spawn()
  323. {
  324.    PrecacheModel( c_VEHICLE_MODEL.c_str() );
  325.    SetModel( c_VEHICLE_MODEL.c_str() );
  326.    //PrecacheModel( c_VEHICLE_MODEL.c_str() );
  327.    BaseClass::Spawn();
  328.    m_fForce = 0;
  329.  
  330. #ifdef CLIENT_DLL
  331.    SetNextClientThink( CLIENT_THINK_ALWAYS );
  332. #else
  333.    SetSolid( SOLID_VPHYSICS );
  334.     SetSolidFlags( 0 );
  335.  
  336.    InitPhysics(); //init on server this early, wait for the client
  337. #endif
  338.    
  339. }
  340.  
  341.  
  342.  
  343. void CESTestVehicle::InitPhysics()
  344. {
  345.    //SetSimulatedEveryTick(true);
  346. #ifdef GAME_DLL
  347.    VPhysicsDestroyObject();
  348.  
  349.    solid_t solid;
  350.    PhysModelParseSolid( solid, this, GetModelIndex() );
  351.    IPhysicsObject *pPhysicsObject =  PhysModelCreate( this, GetModelIndex(), GetAbsOrigin(), GetAbsAngles(), &solid );//VPhysicsInitNormal( SOLID_VPHYSICS, 0, false, &solid );
  352.  
  353.    //Set physics flags
  354.    Assert( pPhysicsObject );
  355.     PhysSetGameFlags( pPhysicsObject, FVPHYSICS_NO_SELF_COLLISIONS );
  356.  
  357.    pPhysicsObject->EnableGravity( false );
  358.    pPhysicsObject->EnableDrag( false );
  359.    pPhysicsObject->SetCallbackFlags( CALLBACK_GLOBAL_COLLISION | CALLBACK_SHADOW_COLLISION | CALLBACK_GLOBAL_FRICTION );
  360.  
  361.     //// Setup Defaults
  362.     VPhysicsSetObject( pPhysicsObject );
  363.  
  364.     pPhysicsObject->SetShadow( 1024.0f, 1024.0f, true, true );
  365.     pPhysicsObject->GetShadowController()->SetPhysicallyControlled( true );
  366.     pPhysicsObject->GetShadowController()->SetTeleportDistance( 0.0f );
  367.  
  368.     pPhysicsObject->EnableCollisions( true );
  369.     pPhysicsObject->Wake();
  370.  
  371.     PhysAddShadow( this );
  372. #endif
  373. }
  374.  
  375. void CESTestVehicle::DriveVehicle( float flFrameTime, CUserCmd *ucmd, int iButtonsDown, int iButtonsReleased )
  376. {
  377. }
  378.  
  379.  
  380. //-----------------------------------------------------------------------------
  381. // Do the move (apply usercmd to vehicle/physics here)
  382. //-----------------------------------------------------------------------------
  383.  
  384. void CESTestVehicle::SetupMove( CBasePlayer *player, CUserCmd *ucmd, IMoveHelper *pHelper, CMoveData *move )
  385. {
  386.     if( GetPassenger(0) == player )
  387.     {
  388.  
  389.         #define SCALEINPUT( x ) x > 0 ? (float)( x * x ) * ( 1.0f / ( 128.0f * 128.0f ) ) : (float)( x * x ) * ( -1.0f / ( 128.0f * 128.0f ) )
  390.       input_torque.Init( 0 , SCALEINPUT(ucmd->mouse_control_axis.y), -SCALEINPUT(ucmd->mouse_control_axis.x) /*-SCALEINPUT(ucmd->mouse_control_axis.z)*/ );
  391.       input_force.Init( -SCALEINPUT( ( ucmd->buttons & IN_FORWARD )? 128.0f : 0.0f ), 0, 0 );
  392.         #undef SCALEINPUT
  393.  
  394.         #ifdef CLIENT_DLL
  395.            //SetAbsVelocity( m_vecMetersVelocity );
  396.       #else
  397.             engine->Con_NPrintf( 4, "input_torque x: %f y: %f z: %f", input_torque.x, input_torque.y, input_torque.z );
  398.             engine->Con_NPrintf( 6, "mouse_control_axis x: %i y: %i z: %i", ucmd->mouse_control_axis.x, ucmd->mouse_control_axis.y, ucmd->mouse_control_axis.z );
  399.             engine->Con_NPrintf( 8, "input_force x: %f y: %f z: %f", input_force.x, input_force.y, input_force.z );
  400.         #endif
  401.     }
  402. }
  403.  
  404. //-----------------------------------------------------------------------------
  405. // Process movement functions
  406. //-----------------------------------------------------------------------------
  407.  
  408. void CESTestVehicle::ProcessMovement( CBasePlayer *pPlayer, CMoveData *pMoveData )
  409. {
  410.     //  Not sure what this function does.  I think it's safe to stub for now.
  411.     // Run the move and update the velocity of the craft
  412.     if ( GetPassenger(0) == pPlayer )
  413.     {
  414.         CommitInputs( gpGlobals->frametime );
  415.     }
  416. }
  417.  
  418. void CESTestVehicle::PhysicsSimulate( void )
  419. {
  420.    BaseClass::PhysicsSimulate();
  421. #ifdef GAME_DLL // This could be shared, but it could be causing hitching and desync
  422.     //BaseClass::PhysicsSimulate();
  423.  
  424.     //Vector origin = GetAbsOrigin();
  425.  
  426.     //if( origin.x >= MAX_COORD_INTEGER ||
  427.     //  origin.y >= MAX_COORD_INTEGER ||
  428.     //  origin.z >= MAX_COORD_INTEGER ||
  429.     //  origin.x <= MIN_COORD_INTEGER ||
  430.     //  origin.y <= MIN_COORD_INTEGER ||
  431.     //  origin.z <= MIN_COORD_INTEGER || enginetrace->GetPointContents( origin ) == CONTENTS_SOLID )
  432.     //{
  433.     //  Warning("Out of world ship was cleaned up\n");
  434.     //  Event_Killed( CTakeDamageInfo( this, this, m_iMaxHealth, DMG_NEVERGIB ) );
  435.     //  return;
  436.     //}
  437.  
  438.  
  439.     //if( !GetPassenger(0) ) // No players present
  440.     //{
  441.     //  input_force = vec3_origin;
  442.     //  input_torque = vec3_origin;
  443.  
  444.     //  if( GetAbsVelocity() != vec3_origin || m_vecEntityAngVelocity != vec3_origin )
  445.     //      CommitInputs( gpGlobals->frametime );
  446.     //}
  447. #endif
  448. }
  449.  
  450. void CESTestVehicle::VPhysicsUpdate( IPhysicsObject *pPhysics )
  451. {
  452.    BaseClass::VPhysicsUpdate( pPhysics );
  453. }
  454.  
  455. //-----------------------------------------------------------------------------
  456. // Finish movement functions
  457. //-----------------------------------------------------------------------------
  458.  
  459. void CESTestVehicle::FinishMove( CBasePlayer *player, CUserCmd *ucmd, CMoveData *move )
  460. {
  461.     if( GetPassenger(0) == player )
  462.     {
  463. #ifdef CLIENT_DLL
  464.         SetNetworkOrigin( GetLocalOrigin() );
  465.         SetNetworkAngles( GetLocalAngles() );
  466.  
  467.         // For prediction
  468.         //m_vecMetersVelocity = GetAbsVelocity();
  469. #else
  470.      
  471.         PhysicsTouchTriggers();
  472.       PhysicsRelinkChildren( gpGlobals->frametime );
  473. #endif
  474.     }
  475. }
  476.  
  477. //-----------------------------------------------------------------------------
  478. // Post-frame player processing
  479. //-----------------------------------------------------------------------------
  480.  
  481. void CESTestVehicle::ItemPostFrame( CBasePlayer *player )
  482. {
  483.    BaseClass::ItemPostFrame(player);
  484. }
  485.  
  486. /*  This is what sdk_playermove.cpp has to say about these functions:
  487.  
  488. SetupMove:
  489. This is called pre player movement and copies all the data necessary from the player for movement.
  490. (Server-side, the client-side version of this code can be found in prediction.cpp.)
  491.  
  492. FinishMove:
  493. This is called post player movement to copy back all data that movement could have modified and that
  494. is necessary for future movement. (Server-side, the client-side version of this code can be found
  495. in prediction.cpp.)
  496.  
  497. */
  498.  
  499. /*  Guesstimates about what the functions do:
  500.  
  501. SetupMove:
  502. Called before player movement code, to handle commands
  503.  
  504. ProcessMovement:
  505. Called after player movement code; now has access to processed player movements.
  506. Not relevant for us because we don't rely on player movement.
  507.  
  508. */
  509.  
  510. void CESTestVehicle::CommitInputs(float Interval)
  511. {
  512.     // Ok, it's time to redo this flying code, and write long comments like this one
  513.     // The main idea behind this is to go for full arcade flight
  514.     // with a bit of a newtonian flare on the "cut engines" key
  515.     // so what we will do is rotate the velocity vector, rather than do all the acceleration shit
  516.     // that will allow us to use realistic accels for translational without compromising turn speed
  517.  
  518.     // Define a bunch of vectors over here
  519.     Vector localvelocity, accel;
  520.     AngularImpulse angaccel(0,0,0);
  521.  
  522.     {
  523.         // Speed depends on wether or not turbo is on
  524.         float flSpeedMax = 100.0f;
  525.  
  526.         VectorIRotate( GetAbsVelocity(), EntityToWorldTransform(), localvelocity );
  527.  
  528.         // Just regular turning for angles
  529.         VectorMultiply( m_angTurnMax, input_torque, angaccel );
  530.         angaccel -= m_vecEntityAngVelocity.Get();  //  "angaccel" and "angular" are angular velocities
  531.         angaccel /= Interval;  //  "angaccel" now an angular acceleration
  532.         angaccel.x = MIN( m_angAccelMax.x, angaccel.x );
  533.       angaccel.y = MIN( m_angAccelMax.y, angaccel.y );
  534.       angaccel.z = MIN( m_angAccelMax.z, angaccel.z );
  535.  
  536.         // Poor man's normalization. Basically projects the box to a circle
  537.         // Its not perfect, but it works (problem is being at 0.7 x and 0.7 y is the same as 1 x and 1 y)
  538.         float vel = input_force.LengthSqr();
  539.         if( vel > 1.0f )
  540.             input_force *= FastRSqrt( vel );
  541.  
  542.         accel = input_force * flSpeedMax - localvelocity; //  "accel" now a velocity delta in local space
  543.         //  Note: here and in the VectorMultiply following are the only
  544.         //  cases in which a specific input parameter is referenced.
  545.         accel /= Interval;  //  "accel" now an acceleration
  546.         accel.x = MIN( m_vecAccelMax.x, accel.x );  //  Clamp to max
  547.       accel.y = MIN( m_vecAccelMax.y, accel.y );  //  Clamp to max
  548.       accel.z = MIN( m_vecAccelMax.z, accel.z );  //  Clamp to max
  549.  
  550.         accel *= Interval;
  551.         angaccel *= Interval;
  552.         localvelocity += accel;
  553.         m_vecEntityAngVelocity += angaccel;
  554.  
  555.         QAngle angvel;
  556.         matrix3x4_t AngVel, output, localToWorld;
  557.  
  558.         AngularImpulseToQAngle( m_vecEntityAngVelocity, angvel );
  559.  
  560. #ifdef CLIENT_DLL
  561.         AngleMatrix( GetNetworkAngles(), localToWorld ); // Nothing lost here, it gets done if you call EntityToWorld too
  562. #else
  563.         AngleMatrix( GetLocalAngles(), localToWorld ); // Nothing lost here, it gets done if you call EntityToWorld too
  564. #endif
  565.         AngleMatrix( angvel * Interval, AngVel );
  566.         ConcatTransforms( localToWorld, AngVel, output );
  567.         MatrixAngles( output, angvel );
  568. #ifdef CLIENT_DLL
  569.         // This should fix pred errors
  570.         angvel[0] = anglemod(angvel[0]);
  571.         angvel[1] = anglemod(angvel[1]);
  572.         angvel[2] = anglemod(angvel[2]);
  573. #endif
  574.  
  575.         SetAbsAngles( angvel );
  576.  
  577.         // NOTE: output is the new angle, this bypasses all the slippage inducing stuff by hard clamping the rotation
  578.         VectorRotate( localvelocity, output, accel );
  579.         SetAbsVelocity( accel );
  580.  
  581.    #ifdef GAME_DLL
  582.       engine->Con_NPrintf( 10, "server velocity x: %f y: %f z: %f", GetLocalVelocity().x, GetLocalVelocity().y, GetLocalVelocity().z );
  583.    #else
  584.       engine->Con_NPrintf( 12, "client velocity x: %f y: %f z: %f", GetLocalVelocity().x, GetLocalVelocity().y, GetLocalVelocity().z );
  585.    #endif
  586.     }
  587.  
  588.     //m_iEngineBits = EngineBits( input_force );
  589.  
  590.  
  591. #ifdef CLIENT_DLL
  592.     Vector origin = GetNetworkOrigin() + GetAbsVelocity() *  TICK_INTERVAL;
  593.    //DevMsg( "Client New Origin ( %f, %f, %f )\n", origin.x, origin.y, origin.z );
  594. #else
  595.     Vector origin = GetLocalOrigin() + GetAbsVelocity() *  TICK_INTERVAL;
  596.    //DevMsg( "Server New Origin ( %f, %f, %f )\n", origin.x, origin.y, origin.z );
  597. #endif
  598.  
  599.    SetLocalOrigin( origin );
  600.  
  601.  
  602. #ifdef GAME_DLL
  603.     if( VPhysicsGetObject() )
  604.     {
  605.         VPhysicsGetObject()->UpdateShadow( GetAbsOrigin(), GetAbsAngles(), false, TICK_INTERVAL );
  606.     }
  607. #endif
  608. }
  609.  
  610. //-----------------------------------------------------------------------------
  611. // VPhysics stuff
  612. //-----------------------------------------------------------------------------
  613. #ifdef GAME_DLL
  614.  
  615.    int  CESTestVehicle::UpdateTransmitState()
  616.    {
  617.        // always call ShouldTransmit() for maines
  618.        //return SetTransmitState( FL_EDICT_FULLCHECK );
  619.        return SetTransmitState( FL_EDICT_ALWAYS );
  620.    }
  621.  
  622.    int CESTestVehicle::ShouldTransmit( const CCheckTransmitInfo *pInfo )
  623.    {
  624.        // asw temp
  625.        return FL_EDICT_ALWAYS;
  626.  
  627.        // always transmit if we're inhabited by the target client
  628.        if ( GetPassenger(0) && pInfo->m_pClientEnt == GetPassenger(0)->edict() )
  629.        {
  630.            return FL_EDICT_ALWAYS;
  631.        }   
  632.  
  633.        return BaseClass::ShouldTransmit( pInfo );
  634.    }
  635.  
  636.  
  637.  
  638.    void CESTestVehicle::VPhysicsPreCollision( int index, gamevcollisionevent_t *pEvent )
  639.    {
  640.        pEvent->pObjects[index]->SetVelocity( &(GetAbsVelocity() ), &m_vecEntityAngVelocity.Get() );
  641.    }
  642.  
  643.    void CESTestVehicle::VPhysicsShadowCollision( int index, gamevcollisionevent_t *pEvent )
  644.    {
  645.        Vector velocity; QAngle angles;
  646.        pEvent->pObjects[index]->GetPosition( &velocity, &angles );
  647.        SetAbsOrigin( velocity );
  648.        SetAbsAngles( angles );
  649.        AngularImpulse angvel;
  650.        pEvent->pObjects[index]->GetVelocity( &velocity, &angvel );
  651.        SetAbsVelocity( velocity );
  652.        m_vecEntityAngVelocity = angvel;
  653.  
  654.        Vector damagePos;
  655.        pEvent->pInternalData->GetContactPoint( damagePos );
  656.        Vector damageForce = pEvent->postVelocity[index] * pEvent->pObjects[index]->GetMass();
  657.        if ( damageForce == vec3_origin )
  658.        {
  659.            // This can happen if this entity is a func_breakable, and can't move.
  660.            // Use the velocity of the entity that hit us instead.
  661.            damageForce = pEvent->postVelocity[!index] * pEvent->pObjects[!index]->GetMass();
  662.        }
  663.  
  664.        //int otherIndex = !index;
  665.        //CBaseEntity *pOther = pEvent->pEntities[otherIndex];
  666.  
  667.        //// We're to take normal damage from this
  668.        //int damageType;
  669.        //float damage = CalculateDefaultPhysicsDamage( index, pEvent, (IsInSpace()?ES_SCALEFACTOR*4.0f:4.0f/ES_SCALEFACTOR), true, damageType );
  670.        //if ( damage > 0 )
  671.        //{
  672.        //   CTakeDamageInfo dmgInfo( pOther, pOther, damageForce, damagePos, damage, damageType | DMG_TACTICAL_SHIP );
  673.        //   PhysCallbackDamage( this, dmgInfo, *pEvent, index );
  674.  
  675.        //   CBasePlayer *player = dynamic_cast< CBasePlayer * >( GetPassenger() );
  676.        //   if( player )
  677.        //   {
  678.        //       CSingleUserRecipientFilter filter( player );
  679.        //       EmitSound( filter, entindex(),  "Cockpit.ImpactAlarm" );
  680.        //   }
  681.        //}
  682.    }
  683.  
  684.    void CESTestVehicle::VPhysicsShadowUpdate( IPhysicsObject *pPhysics )
  685.    {
  686.        Vector origin = GetAbsOrigin();
  687.  
  688.        if ( pPhysics->GetGameFlags() & FVPHYSICS_PENETRATING || pPhysics->GetContactPoint( NULL, NULL ) )
  689.        {
  690.            Vector velocity, angvel; QAngle angles;
  691.            pPhysics->GetPosition( &velocity, &angles );
  692.            SetAbsOrigin( velocity );
  693.            SetAbsAngles( angles );
  694.  
  695.            PhysicsTouchTriggers( &origin );
  696.          PhysicsRelinkChildren(gpGlobals->frametime);
  697.        }
  698.    }
  699. #endif
  700.  
  701.  
  702. #ifdef CLIENT_DLL
  703.    const Vector & CESTestVehicle::GetRenderOrigin( void )
  704.    {
  705.       //Vector vOrigin;
  706.  
  707.       m_vecCustomRenderOrigin = GetAbsOrigin();// - ( GetAbsOrigin() - GetNetworkOrigin() ) + (GetAbsVelocity() * TICK_INTERVAL);
  708.  
  709.        // Apply a smoothing offset to smooth out prediction errors.
  710.        Vector vSmoothOffset;
  711.        GetPredictionErrorSmoothingVector( vSmoothOffset );
  712.        m_vecCustomRenderOrigin += vSmoothOffset;
  713.  
  714.       //NDebugOverlay::Cross3DOriented( m_vecCustomRenderOrigin, GetAbsAngles(), 10.0f, 0, 0, 255, true, 0.0f);
  715.  
  716.       return  m_vecCustomRenderOrigin;
  717.    }
  718.  
  719.    bool CESTestVehicle::ShouldPredict()
  720.    {
  721.        C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  722.  
  723.        if( !player )
  724.            return false;
  725.  
  726.       if( GetPassenger(0) == player && gpGlobals->maxClients > 1 ) //MP only
  727.            return true;
  728.    
  729.        return false;
  730.    }
  731.  
  732.    bool CESTestVehicle::ShouldInterpolate( void )
  733.    {
  734.       if( gpGlobals->maxClients == 1 ) //MP only
  735.          return false;
  736.  
  737.        C_BasePlayer *player = C_BasePlayer::GetLocalPlayer();
  738.        if ( player && player->GetVehicle() && static_cast<CESTestVehicle *>(player->GetVehicle()->GetVehicleEnt() ) == this )
  739.            return true;
  740.        return BaseClass::ShouldInterpolate();
  741.    }
  742.  
  743.    void CESTestVehicle::PostDataUpdate( DataUpdateType_t updateType )
  744.    {
  745.        BaseClass::PostDataUpdate( updateType );
  746.    }
  747.  
  748.    void CESTestVehicle::OnDataChanged( DataUpdateType_t type )
  749.    {
  750.        BaseClass::OnDataChanged( type );
  751.  
  752.        if( type == DATA_UPDATE_CREATED )
  753.        {
  754.            //m_nType = GetVehicleInfo()->nRole;
  755.            //m_vecAccelMax = GetVehicleInfo()->vecAccel;
  756.            //m_angAccelMax = GetVehicleInfo()->vecAngularAccel;
  757.            //m_flSpeedMax = GetVehicleInfo()->flMaxSpeed; // Updated later
  758.            //m_flBoostMax = GetVehicleInfo()->flMaxBoostSpeed;
  759.            //m_angTurnMax = GetVehicleInfo()->angTurnMax;
  760.  
  761.            //CreateVPhysics();
  762.        
  763.            MDLCACHE_CRITICAL_SECTION();
  764.  
  765.            //m_VehicleFX.Init( this );
  766.  
  767.            SetNextClientThink( CLIENT_THINK_ALWAYS );
  768.        }
  769.  
  770.        UpdateVisibility();
  771.  
  772.        //if( m_iOldTeamNum != GetTeamNumber() )
  773.        //{
  774.        //   m_VehicleFX.InitTeam();
  775.        //   m_iOldTeamNum = GetTeamNumber();
  776.        //}
  777.    }
  778.  
  779.  
  780.    void CESTestVehicle::NotePredictionError( const Vector &vDelta )
  781.    {
  782.        // don't worry about prediction errors when dead
  783.        if ( !GetPassenger(0) )
  784.            return;
  785.  
  786.    #if !defined( NO_ENTITY_PREDICTION )
  787.        Vector vOldDelta;
  788.  
  789.       GetPredictionErrorSmoothingVector( vOldDelta );
  790.  
  791.        // sum all errors within smoothing time
  792.        m_vecPredictionError = vDelta + vOldDelta;
  793.  
  794.        // remember when last error happened
  795.        m_flPredictionErrorTime = gpGlobals->curtime;
  796.  
  797.        ResetLatched();
  798.    #endif
  799.    }
  800.  
  801.  
  802.    //extern ConVar cl_smooth;
  803.    extern ConVar_ServerBounded *cl_predict;
  804.    //extern ConVar cl_smoothtime;
  805.    extern ConVar_ServerBounded *cl_interp;
  806.  
  807.    void CESTestVehicle::GetPredictionErrorSmoothingVector( Vector &vOffset )
  808.    {
  809.       ConVarRef cl_smooth("cl_smooth");
  810.       ConVarRef cl_smoothtime("cl_smoothtime");
  811.    #if !defined( NO_ENTITY_PREDICTION )
  812.        if ( engine->IsPlayingDemo() || !cl_smooth.GetInt() || !cl_predict->GetInt() || engine->IsPaused() )
  813.        {
  814.            vOffset.Init();
  815.            return;
  816.        }
  817.  
  818.        float errorAmount = ( gpGlobals->curtime - m_flPredictionErrorTime ) / cl_smoothtime.GetFloat();
  819.  
  820.        if ( errorAmount >= 1.0f )
  821.        {
  822.            vOffset.Init();
  823.            return;
  824.        }
  825.    
  826.        errorAmount = 1.0f - errorAmount;
  827.  
  828.        vOffset = m_vecPredictionError * errorAmount;
  829.    #else
  830.        vOffset.Init();
  831.    #endif
  832.  
  833.    //   vOffset.Init();
  834.    //#if !defined( NO_ENTITY_PREDICTION )
  835.    //   // don't worry about prediction errors when dead
  836.    //   if ( !GetPassenger(0) )
  837.    //   {
  838.    //       return;
  839.    //   }
  840.    //
  841.    //   C_BasePlayer * player = dynamic_cast<C_BasePlayer *>(GetPassenger(0) );
  842.    //   Assert(player);
  843.    //   player->GetPredictionErrorSmoothingVector( vOffset );
  844.    //#endif
  845.    }
  846.  
  847. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement