//========= Copyright © 1996-2002, Valve LLC, All rights reserved. ============
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================

#ifndef C_ROPE_H
#define C_ROPE_H
#ifdef _WIN32
#pragma once
#endif


#include "c_baseentity.h"
#include "rope_physics.h"
#include "materialsystem/imaterial.h"
#include "rope_shared.h"
#include "bitvec.h"


class KeyValues;
class C_BaseAnimating;


class C_RopeKeyframe : public C_BaseEntity
{
public:

	DECLARE_CLASS( C_RopeKeyframe, C_BaseEntity );
	DECLARE_CLIENTCLASS();


private:

	class CPhysicsDelegate : public CSimplePhysics::IHelper
	{
	public:
		virtual void	GetNodeForces( CSimplePhysics::CNode *pNodes, int iNode, Vector *pAccel );
		virtual void	ApplyConstraints( CSimplePhysics::CNode *pNodes, int nNodes );
	
		C_RopeKeyframe	*m_pKeyframe;
	};

	friend class CPhysicsDelegate;


public:

					C_RopeKeyframe();
					~C_RopeKeyframe();

	// This can be used for client-only ropes.
	static C_RopeKeyframe* Create(
		C_BaseEntity *pStartEnt,
		C_BaseEntity *pEndEnt,
		int iStartAttachment=0,
		int iEndAttachment=0,
		float ropeWidth = 2,
		const char *pMaterialName = "cable/cable",		// Note: whoever creates the rope must
														// use PrecacheModel for whatever material
														// it specifies here.
		int numSegments = 5,
		int ropeFlags = ROPE_SIMULATE
		);

	// Create a client-only rope and initialize it with the parameters from the KeyValues.
	static C_RopeKeyframe* CreateFromKeyValues( C_BaseAnimating *pEnt, KeyValues *pValues );

	// Find ropes (with both endpoints connected) that intersect this AABB. This is just an approximation.
	static int GetRopesIntersectingAABB( C_RopeKeyframe **pRopes, int nMaxRopes, const Vector &vAbsMin, const Vector &vAbsMax );

	// Set the slack.
	void SetSlack( int slack );

	void SetRopeFlags( int flags );
	int GetRopeFlags() const;

	void SetupHangDistance( float flHangDist );

	// Change which entities the rope is connected to.
	void SetStartEntity( C_BaseEntity *pEnt );
	void SetEndEntity( C_BaseEntity *pEnt );

	C_BaseEntity* GetStartEntity() const;
	C_BaseEntity* GetEndEntity() const;

	// Hook the physics. Pass in your own implementation of CSimplePhysics::IHelper. The
	// default implementation is returned so you can call through to it if you want.
	CSimplePhysics::IHelper*	HookPhysics( CSimplePhysics::IHelper *pHook );

	// Attach to things (you can also just lock the endpoints down yourself if you hook the physics).

	// Client-only right now. This could be moved to the server if there was a good reason.
	void			SetColorMod( const Vector &vColorMod );

	// Use this when rope length and slack change to recompute the spring length.
	void			RecomputeSprings();

	void ShakeRope( const Vector &vCenter, float flRadius, float flMagnitude );

	// Get the attachment position of one of the endpoints.
	bool			GetEndPointPos( int iPt, Vector &vPos );
	

// C_BaseEntity overrides.
public:

	virtual void	OnDataChanged( DataUpdateType_t updateType );
	virtual void	ClientThink();
	virtual int		DrawModel( int flags );
	virtual bool	ShouldDraw();
	virtual const Vector& WorldSpaceCenter() const;

private:
	
	void FinishInit( const char *pMaterialName );

	void			RunRopeSimulation( float flSeconds );

	bool			AnyPointsMoved();

	bool			DidEndPointMove( int iPt );
	bool			DetectRestingState( bool &bApplyWind );

	void			DrawBeams();
	void			UpdateBBox();
	bool			InitRopePhysics();
	
	bool			GetEndPointAttachment( int iPt, Vector &vPos, QAngle &angle );
	
	void			UpdateRopeSubdivs( float **pSubdivs, int *nSubdivs );
	void			CalcLightValues();

	void			ReceiveMessage( const char *msgname, int length, void *data );
	bool			CalculateEndPointAttachment( C_BaseEntity *pEnt, int iAttachment, Vector &vPos, QAngle &angles );


private:

	bool			m_bNewDataThisFrame; // Set to true in OnDataChanged so that we simulate that frame

	bool			m_bPhysicsInitted;	// It waits until all required entities are 
										// present to start simulating and rendering.

	// Track which links touched something last frame. Used to prevent wind from gusting on them.
	CBitVec<ROPE_MAX_SEGMENTS>		m_LinksTouchingSomething;
	int								m_nLinksTouchingSomething;
	bool							m_bApplyWind;
	int								m_fPrevLockedPoints;	// Which points are locked down.
	int								m_iForcePointMoveCounter;

	// Used to control resting state.
	bool			m_bPrevEndPointPos[2];
	Vector			m_vPrevEndPointPos[2];

	float			m_flCurScroll;		// for scrolling texture.
	float			m_flScrollSpeed;

	int				m_RopeFlags;			// Combo of ROPE_ flags.
	int				m_iRopeMaterialModel;	// Index of sprite model with the rope's material.
		
	CRopePhysics<ROPE_MAX_SEGMENTS>	m_RopePhysics;
	Vector			m_LightValues[ROPE_MAX_SEGMENTS]; // light info when the rope is created.

	int				m_nSegments;		// Number of segments.
	
	EHANDLE			m_hStartPoint;		// StartPoint/EndPoint are entities
	EHANDLE			m_hEndPoint;
	short			m_iStartAttachment;	// StartAttachment/EndAttachment are attachment points.
	short			m_iEndAttachment;

	unsigned char	m_Subdiv;			// Number of subdivions in between segments.

	int				m_RopeLength;		// Length of the rope, used for tension.
	int				m_Slack;			// Extra length the rope is given.
	float			m_TextureScale;		// pixels per inch
	
	int				m_fLockedPoints;	// Which points are locked down.

	float				m_Width;

	CPhysicsDelegate	m_PhysicsDelegate;

	IMaterial		*m_pMaterial;
	IMaterial		*m_pBackMaterial; // Optional translucent background material for the rope to help reduce aliasing.
	int				m_TextureHeight;	// Texture height, for texture scale calculations.

	// Instantaneous force
	Vector			m_flImpulse;

	// Simulated wind gusts.
	float			m_flCurrentGustTimer;
	float			m_flCurrentGustLifetime;	// How long will the current gust last?

	float			m_flTimeToNextGust;			// When will the next wind gust be?
	Vector			m_vWindDir;					// What direction does the current gust go in?

	Vector			m_vColorMod;				// Color modulation on all verts?

	bool			m_bEndPointAttachmentsDirty;
	Vector			m_vCachedEndPointAttachmentPos[2];
	QAngle			m_vCachedEndPointAttachmentAngle[2];
};


// Profiling info.
void Rope_ResetCounters();
//void Rope_ShowRSpeeds();


#endif // C_ROPE_H
