Advertisement
Guest User

Untitled

a guest
Apr 20th, 2011
1,136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1.  
  2.  
  3. #include "prediction.h"
  4. #include "utils.h"
  5. #include "clienthook.h"
  6. #include "HAPI.h"
  7.  
  8. Prediction gPrediction;
  9.  
  10. Prediction::Prediction( ) : UpdateContext( )
  11. {
  12.  
  13. }
  14.  
  15. Prediction::~Prediction( )
  16. {
  17.  
  18. }
  19.  
  20. float flSpeed = 1100.0f;
  21.  
  22. float Prediction::GetProjectileSpeed( )
  23. {
  24.     switch( m_weaponID )
  25.     {
  26.     case Demoman_Grenade_Launcher:
  27.         return 910.0f;
  28.  
  29.     case Demoman_Sticky_Launcher:
  30.         return 805.0f + ( m_charge * 261.63 );
  31.  
  32.     case Soldier_Direct_Hit:
  33.         return 1850.0f;
  34.  
  35.     case Soldier_Rocket_Launcher:
  36.         return 1100.0f;
  37.  
  38.     case Pyro_Flaregun:
  39.         return 1450.0f;
  40.  
  41.     default:
  42.         return 1000.0f;
  43.        
  44.     }
  45. }
  46.  
  47. bool Prediction::PredictEntityLocation( CBaseEntity * ent, Vector in, Vector & out )
  48. {
  49.     Vector vVelocity, vPosition;
  50.  
  51.     if( !ent )
  52.         return false;
  53.  
  54.     if( !Util::GetAbsVelocity( ent, vVelocity ) )
  55.         return false;
  56.  
  57.     float flCurrentTime = *( float* )( ( DWORD_PTR )ent + 0x68 );
  58.     float flOldTime = *( float* )( ( DWORD_PTR )ent + 0x6C );
  59.  
  60.     float flDelta = flCurrentTime - flOldTime;
  61.  
  62.     out = in + vVelocity * g_pGlobalVars->frametime;
  63.  
  64.     return true;
  65. }
  66.  
  67. void Prediction::Reset( )
  68. {
  69.     /* Update the local player context */
  70.     ResetFrameData( );
  71. }
  72.  
  73. float Dist2D( Vector from, Vector to )
  74. {
  75.     float x = to.x - from.x;
  76.     float y = to.y - from.y;
  77.  
  78.     return sqrt( x*x + y*y );
  79. }
  80.  
  81. /* How long is it going to take the projectile to reach the position at 'to'? */
  82. float Prediction::TimeToImpact( Vector from, Vector to )
  83. {
  84.     float flSpeed = gPrediction.GetProjectileSpeed( );
  85.  
  86.     float flTime = 1.0f;
  87.  
  88.     float flDistance = 1.0f;
  89.    
  90.     /* Some weapons ignore gravity...*/
  91.     if( m_weaponID == Soldier_Rocket_Launcher || m_weaponID == Soldier_Direct_Hit )
  92.     {
  93.         flDistance = from.DistTo( to );
  94.     }
  95.     else /* others you have to take into account the arc required to hit the target given the known gravity */
  96.     {
  97.         float v = flSpeed;
  98.         float v2 = v*v;
  99.         float v4 = v2*v2;
  100.         float g = -800.0f;
  101.         float x = from.DistTo( to );
  102.         float x2 = x*x;
  103.         float y = to.z - from.z;
  104.         float anglep = 0.0f;
  105.        
  106.         //if( to.z < from.z )
  107.         //  anglep = atan2( v2 - sqrt( v4 - g * ( g * x2 + ( 2 * y ) * v2 ) ), g * x ) ;
  108.         //else
  109.             anglep = atan2( v2 + sqrt( v4 - g * ( g * x2 + ( 2 * y ) * v2 ) ), g * x ) ;
  110.        
  111.         float vca = v * cos( anglep );
  112.         float vsa = v * sin( anglep );
  113.  
  114.         flDistance = ( vca / g ) * ( vsa + sqrt( vsa*vsa + ( 2 * g * from.z ) ) );
  115.     }
  116.  
  117.     flTime = flDistance / flSpeed;
  118.  
  119.     return flTime;
  120. }
  121.  
  122. Vector PredictedPos( CBaseEntity * target, Vector pos, float time )
  123. {
  124.     Vector vel, vpos;
  125.     Util::GetAbsVelocity( target, vel );
  126.    
  127.     vpos.x = pos.x + vel.x * time;
  128.     vpos.y = pos.y + vel.y * time;
  129.     vpos.z = pos.z + vel.z * time;
  130.    
  131.     int nFlags = Util::GetFlags( target );
  132.  
  133.     /* FIX: this flag is based on CURRENT position, not the predicted position, so they could actually be on the floor when they aren't */
  134.     if( !( nFlags & FL_ONGROUND ) )
  135.         vpos.z += ( -800.0f * 0.5f * time * time );
  136.  
  137.     return vpos;
  138. }
  139.  
  140. bool Prediction::PredictProjectiles( /*CUserCmd * cmd, */CBaseEntity * target, Vector in, Vector & out )
  141. {
  142.     Vector vNextFrame, f, r, u, vShootPos;
  143.     QAngle vAngles;
  144.  
  145.     CBaseEntity * pLocal = NULL;
  146.     CBaseCombatWeapon * pWeapon = NULL;
  147.  
  148.     if( !target )
  149.         return false;
  150.  
  151.     Reset( );
  152.  
  153.     if( !Util::HasProjectileLauncher( m_weaponID ) )
  154.         return true;
  155.  
  156.     if( !Util::GetLocalPlayer( &pLocal ) )
  157.         return false;
  158.  
  159.     if( !Util::GetBaseCombatWeapon( pLocal, &pWeapon ) )
  160.         return false;
  161.  
  162.     Vector vTargetVelocity;
  163.  
  164.     //g_pEngine->GetViewAngles( vAngles );
  165.  
  166.     //Util::Math::_AngleVectors( vAngles, &f, &r, &u );
  167.  
  168.     //Util::GetLocalEyePos( vShootPos );
  169.  
  170.     //vShootPos = vShootPos + f + 3 * r + -18.0f * u;  
  171.  
  172.     vShootPos = Util::GetWeaponShootPosition( pLocal );
  173.  
  174.     //PredictEntityLocation( pLocal, vShootPos, vShootPos );
  175.  
  176.     float flProjTime = 0.1f;
  177.  
  178.     Vector pos = in;
  179.  
  180.     float flProjectileSpeed = GetProjectileSpeed( );
  181.  
  182.     if( !Util::GetAbsVelocity( target, vTargetVelocity ) )
  183.         return false;
  184.  
  185.     float flTargetSpeed = vTargetVelocity.Length( );
  186.  
  187.     /* This could cause a crash! */
  188.     if( flTargetSpeed == 0.0f )
  189.         return true;
  190.  
  191.     float flCurrentTime = *( float* )( ( DWORD_PTR )target + 0x68 );
  192.     float flOldTime = *( float* )( ( DWORD_PTR )target + 0x6C );
  193.     float flDelta = 0.00001f;//flCurrentTime - flOldTime;
  194.  
  195.     if( !flDelta )
  196.         return true;
  197.  
  198.     Ray_t   ray;
  199.     trace_t tr;
  200.  
  201.     float flDeltaOfTimes = 1.0f;
  202.  
  203.     /* Keep going until we find a point in space that it takes the same time for the projectile to reach as it does for the player, THAT will be the most probable hit location */
  204.     while( flDeltaOfTimes > 0.0f )
  205.     {
  206.         pos = PredictedPos( target, in, flDelta );  // the position at guestimated time x
  207.  
  208.         flProjTime = TimeToImpact( vShootPos, pos ); // the time it takes for our projectile to reach position predicted in space  
  209.  
  210.         float flTimeFromPlayerToNewPos = in.DistTo( pos ) / flTargetSpeed; //the time it takes to get from the current position to the predicted position in space
  211.  
  212.         flDeltaOfTimes = flProjTime - flTimeFromPlayerToNewPos;
  213.  
  214.         flDelta *= 1.001f;
  215.     }
  216.  
  217.     ray.Init( in, pos );
  218.        
  219.     g_pEngineTrace->TraceRay( ray, 0x4600400B, NULL, &tr );
  220.        
  221.     /* If the predicted position intersected with an object, stop the prediction there */
  222.     if( tr.fraction != 1.0f )
  223.     {
  224.         pos = tr.endpos;
  225.     }
  226.  
  227.     out = pos;// + f + -3 * r + 18.0f * u;
  228.  
  229.     return true;
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement