Advertisement
psy_commando

ES test vehicle

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