Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
3.0 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #ifndef SHOT_MANIPULATOR_H
  7. #define SHOT_MANIPULATOR_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include "mathlib/vector.h"
  12. extern ConVar ai_shot_bias_min;
  13. extern ConVar ai_shot_bias_max;
  14. //---------------------------------------------------------
  15. // Caches off a shot direction and allows you to perform
  16. // various operations on it without having to recalculate
  17. // vecRight and vecUp each time.
  18. //---------------------------------------------------------
  19. class CShotManipulator
  20. {
  21. public:
  22. CShotManipulator( const Vector &vecForward )
  23. {
  24. SetShootDir( vecForward );
  25. };
  26. void SetShootDir( const Vector &vecForward )
  27. {
  28. m_vecShotDirection = vecForward;
  29. VectorVectors( m_vecShotDirection, m_vecRight, m_vecUp );
  30. }
  31. const Vector &ApplySpread( const Vector &vecSpread, float bias = 1.0 );
  32. const Vector &ApplyAngularSpread( const Vector &vecSpread, float bias = 1.0 );
  33. const Vector &GetShotDirection() { return m_vecShotDirection; }
  34. const Vector &GetResult() { return m_vecResult; }
  35. const Vector &GetRightVector() { return m_vecRight; }
  36. const Vector &GetUpVector() { return m_vecUp;}
  37. private:
  38. Vector m_vecShotDirection;
  39. Vector m_vecRight;
  40. Vector m_vecUp;
  41. Vector m_vecResult;
  42. };
  43. //---------------------------------------------------------
  44. // Take a vector (direction) and another vector (spread)
  45. // and modify the direction to point somewhere within the
  46. // spread. This used to live inside FireBullets.
  47. //---------------------------------------------------------
  48. inline const Vector &CShotManipulator::ApplySpread( const Vector &vecSpread, float bias )
  49. {
  50. // get circular gaussian spread
  51. float x, y, z;
  52. if ( bias > 1.0 )
  53. bias = 1.0;
  54. else if ( bias < 0.0 )
  55. bias = 0.0;
  56. float shotBiasMin = ai_shot_bias_min.GetFloat();
  57. float shotBiasMax = ai_shot_bias_max.GetFloat();
  58. // 1.0 gaussian, 0.0 is flat, -1.0 is inverse gaussian
  59. float shotBias = ( ( shotBiasMax - shotBiasMin ) * bias ) + shotBiasMin;
  60. float flatness = ( fabsf(shotBias) * 0.5 );
  61. do
  62. {
  63. x = random->RandomFloat(-1,1) * flatness + random->RandomFloat(-1,1) * (1 - flatness);
  64. y = random->RandomFloat(-1,1) * flatness + random->RandomFloat(-1,1) * (1 - flatness);
  65. if ( shotBias < 0 )
  66. {
  67. x = ( x >= 0 ) ? 1.0 - x : -1.0 - x;
  68. y = ( y >= 0 ) ? 1.0 - y : -1.0 - y;
  69. }
  70. z = x*x+y*y;
  71. } while (z > 1);
  72. m_vecResult = m_vecShotDirection + x * vecSpread.x * m_vecRight + y * vecSpread.y * m_vecUp;
  73. return m_vecResult;
  74. }
  75. inline const Vector &CShotManipulator::ApplyAngularSpread( const Vector &vecSpread, float bias )
  76. {
  77. float x, y, z;
  78. x = vecSpread[0] * random->RandomFloat(-0.5f, 0.5f);
  79. y = vecSpread[1] * random->RandomFloat(-0.5f, 0.5f);
  80. z = vecSpread[2] * random->RandomFloat(-0.5f, 0.5f);
  81. matrix3x4_t matrix;
  82. QAngle qa(x,y,z);
  83. AngleMatrix( qa, matrix );
  84. VectorTransform(m_vecShotDirection, matrix, m_vecResult);
  85. return m_vecResult;
  86. }
  87. #endif // SHOT_MANIPULATOR_H