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.

275 lines
8.4 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #ifndef C_ROPE_H
  8. #define C_ROPE_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include "c_baseentity.h"
  13. #include "rope_physics.h"
  14. #include "materialsystem/imaterial.h"
  15. #include "rope_shared.h"
  16. #include "bitvec.h"
  17. class KeyValues;
  18. class C_BaseAnimating;
  19. struct RopeSegData_t;
  20. #define MAX_ROPE_SUBDIVS 8
  21. #define MAX_ROPE_SEGMENTS (ROPE_MAX_SEGMENTS+(ROPE_MAX_SEGMENTS-1)*MAX_ROPE_SUBDIVS)
  22. enum rope_type
  23. {
  24. ROPE_TYPE_DEFAULT = 0,
  25. ROPE_TYPE_DEFUSECABLES,
  26. };
  27. //=============================================================================
  28. class C_RopeKeyframe : public C_BaseEntity
  29. {
  30. public:
  31. DECLARE_CLASS( C_RopeKeyframe, C_BaseEntity );
  32. DECLARE_CLIENTCLASS();
  33. private:
  34. class CPhysicsDelegate : public CSimplePhysics::IHelper
  35. {
  36. public:
  37. virtual void GetNodeForces( CSimplePhysics::CNode *pNodes, int iNode, Vector *pAccel );
  38. virtual void ApplyConstraints( CSimplePhysics::CNode *pNodes, int nNodes );
  39. C_RopeKeyframe *m_pKeyframe;
  40. };
  41. friend class CPhysicsDelegate;
  42. public:
  43. C_RopeKeyframe();
  44. ~C_RopeKeyframe();
  45. // This can be used for client-only ropes.
  46. static C_RopeKeyframe* Create(
  47. C_BaseEntity *pStartEnt,
  48. C_BaseEntity *pEndEnt,
  49. int iStartAttachment=0,
  50. int iEndAttachment=0,
  51. float ropeWidth = 2,
  52. const char *pMaterialName = "cable/cable", // Note: whoever creates the rope must
  53. // use PrecacheModel for whatever material
  54. // it specifies here.
  55. int numSegments = 5,
  56. int ropeFlags = ROPE_SIMULATE,
  57. rope_type ropeType = ROPE_TYPE_DEFAULT
  58. );
  59. // Create a client-only rope and initialize it with the parameters from the KeyValues.
  60. static C_RopeKeyframe* CreateFromKeyValues( C_BaseAnimating *pEnt, KeyValues *pValues );
  61. // Find ropes (with both endpoints connected) that intersect this AABB. This is just an approximation.
  62. static int GetRopesIntersectingAABB( C_RopeKeyframe **pRopes, int nMaxRopes, const Vector &vAbsMin, const Vector &vAbsMax );
  63. // Set the slack.
  64. void SetSlack( int slack );
  65. void SetRopeFlags( int flags );
  66. void AddRopeFlags( int flags );
  67. int GetRopeFlags() const;
  68. void SetupHangDistance( float flHangDist );
  69. // Change which entities the rope is connected to.
  70. void SetStartEntity( C_BaseEntity *pEnt );
  71. void SetEndEntity( C_BaseEntity *pEnt );
  72. C_BaseEntity* GetStartEntity() const;
  73. C_BaseEntity* GetEndEntity() const;
  74. // Hook the physics. Pass in your own implementation of CSimplePhysics::IHelper. The
  75. // default implementation is returned so you can call through to it if you want.
  76. CSimplePhysics::IHelper* HookPhysics( CSimplePhysics::IHelper *pHook );
  77. // Attach to things (you can also just lock the endpoints down yourself if you hook the physics).
  78. // Client-only right now. This could be moved to the server if there was a good reason.
  79. void SetColorMod( const Vector &vColorMod );
  80. // Use this when rope length and slack change to recompute the spring length.
  81. void RecomputeSprings();
  82. void ShakeRope( const Vector &vCenter, float flRadius, float flMagnitude );
  83. // Get the attachment position of one of the endpoints.
  84. bool GetEndPointPos( int iPt, Vector &vPos );
  85. // Get the rope material data.
  86. IMaterial *GetSolidMaterial( void ) { return m_pMaterial; }
  87. struct BuildRopeQueuedData_t
  88. {
  89. Vector *m_pPredictedPositions;
  90. Vector *m_pLightValues;
  91. int m_iNodeCount;
  92. Vector m_vColorMod;
  93. float m_RopeLength;
  94. float m_Slack;
  95. };
  96. void BuildRope( RopeSegData_t *pRopeSegment, const Vector &vCurrentViewForward, const Vector &vCurrentViewOrigin, BuildRopeQueuedData_t *pQueuedData );
  97. // C_BaseEntity overrides.
  98. public:
  99. virtual void OnDataChanged( DataUpdateType_t updateType );
  100. virtual void ClientThink();
  101. virtual int DrawModel( int flags, const RenderableInstance_t &instance );
  102. virtual RenderableTranslucencyType_t ComputeTranslucencyType() { return RENDERABLE_IS_OPAQUE; }
  103. virtual bool ShouldDraw();
  104. virtual const Vector& WorldSpaceCenter() const;
  105. // Specify ROPE_ATTACHMENT_START_POINT or ROPE_ATTACHMENT_END_POINT for the attachment.
  106. virtual bool GetAttachment( int number, Vector &origin, QAngle &angles );
  107. virtual bool GetAttachment( int number, matrix3x4_t &matrix );
  108. virtual bool GetAttachment( int number, Vector &origin );
  109. virtual bool GetAttachmentVelocity( int number, Vector &originVel, Quaternion &angleVel );
  110. private:
  111. void FinishInit( const char *pMaterialName );
  112. void RunRopeSimulation( float flSeconds );
  113. Vector ConstrainNode( const Vector &vNormal, const Vector &vNodePosition, const Vector &vMidpiont, float fNormalLength );
  114. void ConstrainNodesBetweenEndpoints( void );
  115. bool AnyPointsMoved();
  116. bool DidEndPointMove( int iPt );
  117. bool DetectRestingState( bool &bApplyWind );
  118. void UpdateBBox();
  119. bool InitRopePhysics();
  120. bool GetEndPointAttachment( int iPt, Vector &vPos, QAngle &angle );
  121. Vector *GetRopeSubdivVectors( int *nSubdivs );
  122. void CalcLightValues();
  123. void ReceiveMessage( int classID, bf_read &msg );
  124. bool CalculateEndPointAttachment( C_BaseEntity *pEnt, int iAttachment, Vector &vPos, QAngle *pAngles );
  125. void UpdateHolidayLights( void );
  126. private:
  127. // Track which links touched something last frame. Used to prevent wind from gusting on them.
  128. CBitVec<ROPE_MAX_SEGMENTS> m_LinksTouchingSomething;
  129. int m_nLinksTouchingSomething;
  130. bool m_bApplyWind;
  131. int m_fPrevLockedPoints; // Which points are locked down.
  132. int m_iForcePointMoveCounter;
  133. // Used to control resting state.
  134. bool m_bPrevEndPointPos[2];
  135. Vector m_vPrevEndPointPos[2];
  136. float m_flCurScroll; // for scrolling texture.
  137. float m_flScrollSpeed;
  138. int m_RopeFlags; // Combo of ROPE_ flags.
  139. int m_iRopeMaterialModelIndex; // Index of sprite model with the rope's material.
  140. int m_iDefaultRopeMaterialModelIndex; // Index of the default sprite model with the rope's material.
  141. CRopePhysics<ROPE_MAX_SEGMENTS> m_RopePhysics;
  142. Vector m_LightValues[ROPE_MAX_SEGMENTS]; // light info when the rope is created.
  143. int m_nSegments; // Number of segments.
  144. EHANDLE m_hStartPoint; // StartPoint/EndPoint are entities
  145. EHANDLE m_hEndPoint;
  146. short m_iStartAttachment; // StartAttachment/EndAttachment are attachment points.
  147. short m_iEndAttachment;
  148. int m_Subdiv; // Number of subdivions in between segments.
  149. int m_RopeLength; // Length of the rope, used for tension.
  150. int m_Slack; // Extra length the rope is given.
  151. float m_TextureScale; // pixels per inch
  152. int m_fLockedPoints; // Which points are locked down.
  153. int m_nChangeCount;
  154. float m_Width;
  155. CPhysicsDelegate m_PhysicsDelegate;
  156. IMaterial *m_pMaterial;
  157. int m_TextureHeight; // Texture height, for texture scale calculations.
  158. // Instantaneous force
  159. Vector m_vecImpulse;
  160. Vector m_vecPreviousImpulse;
  161. // Simulated wind gusts.
  162. float m_flCurrentGustTimer;
  163. float m_flCurrentGustLifetime; // How long will the current gust last?
  164. float m_flTimeToNextGust; // When will the next wind gust be?
  165. Vector m_vWindDir; // What direction does the current gust go in?
  166. Vector m_vColorMod; // Color modulation on all verts?
  167. Vector m_vCachedEndPointAttachmentPos[2];
  168. QAngle m_vCachedEndPointAttachmentAngle[2];
  169. // In network table, can't bit-compress
  170. bool m_bConstrainBetweenEndpoints; // Simulated segment points won't stretch beyond the endpoints
  171. bool m_bEndPointAttachmentPositionsDirty : 1;
  172. bool m_bEndPointAttachmentAnglesDirty : 1;
  173. bool m_bNewDataThisFrame : 1; // Set to true in OnDataChanged so that we simulate that frame
  174. bool m_bPhysicsInitted : 1; // It waits until all required entities are
  175. // present to start simulating and rendering.
  176. rope_type m_ropeType;
  177. friend class CRopeManager;
  178. };
  179. // Profiling info.
  180. void Rope_ResetCounters();
  181. //void Rope_ShowRSpeeds();
  182. //=============================================================================
  183. //
  184. // Rope Manager
  185. //
  186. abstract_class IRopeManager
  187. {
  188. public:
  189. virtual ~IRopeManager() {}
  190. virtual void ResetRenderCache( void ) = 0;
  191. virtual void AddToRenderCache( C_RopeKeyframe *pRope ) = 0;
  192. virtual void DrawRenderCache( IMatRenderContext *pRenderContext, bool bShadowDepth ) = 0;
  193. virtual void SetHolidayLightMode( bool bHoliday ) = 0;
  194. virtual bool IsHolidayLightMode( void ) = 0;
  195. virtual int GetHolidayLightStyle( void ) = 0;
  196. };
  197. IRopeManager *RopeManager();
  198. #endif // C_ROPE_H