Team Fortress 2 Source Code as on 22/4/2020
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.

261 lines
8.1 KiB

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