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.

201 lines
5.6 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: Declares basic entity communications classes, for input/output of data
  4. // between entities
  5. //
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #ifndef ENTITYOUTPUT_H
  9. #define ENTITYOUTPUT_H
  10. #ifdef _WIN32
  11. #pragma once
  12. #endif
  13. class CBaseEntity;
  14. #define EVENT_FIRE_ALWAYS -1
  15. //-----------------------------------------------------------------------------
  16. // Purpose: A COutputEvent consists of an array of these CEventActions.
  17. // Each CEventAction holds the information to fire a single input in
  18. // a target entity, after a specific delay.
  19. //-----------------------------------------------------------------------------
  20. class CEventAction
  21. {
  22. public:
  23. CEventAction( const char *ActionData = NULL );
  24. CEventAction( const CEventAction &p_EventAction );
  25. string_t m_iTarget; // name of the entity(s) to cause the action in
  26. string_t m_iTargetInput; // the name of the action to fire
  27. string_t m_iParameter; // parameter to send, 0 if none
  28. float m_flDelay; // the number of seconds to wait before firing the action
  29. int m_nTimesToFire; // The number of times to fire this event, or EVENT_FIRE_ALWAYS.
  30. int m_iIDStamp; // unique identifier stamp
  31. static int s_iNextIDStamp;
  32. CEventAction *m_pNext;
  33. // allocates memory from engine.MPool/g_EntityListPool
  34. static void *operator new( size_t stAllocateBlock );
  35. static void *operator new( size_t stAllocateBlock, int nBlockUse, const char *pFileName, int nLine );
  36. static void operator delete( void *pMem );
  37. static void operator delete( void *pMem , int nBlockUse, const char *pFileName, int nLine ) { operator delete(pMem); }
  38. DECLARE_SIMPLE_DATADESC();
  39. };
  40. //-----------------------------------------------------------------------------
  41. // Purpose: Stores a list of connections to other entities, for data/commands to be
  42. // communicated along.
  43. //-----------------------------------------------------------------------------
  44. class CBaseEntityOutput
  45. {
  46. public:
  47. ~CBaseEntityOutput();
  48. void ParseEventAction( const char *EventData );
  49. void AddEventAction( CEventAction *pEventAction );
  50. void RemoveEventAction( CEventAction *pEventAction );
  51. int Save( ISave &save );
  52. int Restore( IRestore &restore, int elementCount );
  53. int NumberOfElements( void );
  54. float GetMaxDelay( void );
  55. fieldtype_t ValueFieldType() { return m_Value.FieldType(); }
  56. void FireOutput( variant_t Value, CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay = 0 );
  57. /// Delete every single action in the action list.
  58. void DeleteAllElements( void ) ;
  59. CEventAction *GetFirstAction() { return m_ActionList; }
  60. const CEventAction *GetActionForTarget( string_t iSearchTarget ) const;
  61. protected:
  62. variant_t m_Value;
  63. CEventAction *m_ActionList;
  64. DECLARE_SIMPLE_DATADESC();
  65. CBaseEntityOutput() {} // this class cannot be created, only it's children
  66. private:
  67. CBaseEntityOutput( CBaseEntityOutput& ); // protect from accidental copying
  68. };
  69. //-----------------------------------------------------------------------------
  70. // Purpose: wraps variant_t data handling in convenient, compiler type-checked template
  71. //-----------------------------------------------------------------------------
  72. template< class Type, fieldtype_t fieldType >
  73. class CEntityOutputTemplate : public CBaseEntityOutput
  74. {
  75. public:
  76. //
  77. // Sets an initial value without firing the output.
  78. //
  79. void Init( Type value )
  80. {
  81. m_Value.Set( fieldType, &value );
  82. }
  83. //
  84. // Sets a value and fires the output.
  85. //
  86. void Set( Type value, CBaseEntity *pActivator, CBaseEntity *pCaller )
  87. {
  88. m_Value.Set( fieldType, &value );
  89. FireOutput( m_Value, pActivator, pCaller );
  90. }
  91. //
  92. // Returns the current value.
  93. //
  94. Type Get( void )
  95. {
  96. return *((Type*)&m_Value);
  97. }
  98. };
  99. //
  100. // Template specializations for type Vector, so we can implement Get, Set, and Init differently.
  101. //
  102. template<>
  103. class CEntityOutputTemplate< Vector, FIELD_VECTOR> : public CBaseEntityOutput
  104. {
  105. public:
  106. void Init( const Vector &value )
  107. {
  108. m_Value.SetVector3D( value );
  109. }
  110. void Set( const Vector &value, CBaseEntity *pActivator, CBaseEntity *pCaller )
  111. {
  112. m_Value.SetVector3D( value );
  113. FireOutput( m_Value, pActivator, pCaller );
  114. }
  115. void Get( Vector &vec )
  116. {
  117. m_Value.Vector3D(vec);
  118. }
  119. };
  120. template<>
  121. class CEntityOutputTemplate< Vector, FIELD_POSITION_VECTOR> : public CBaseEntityOutput
  122. {
  123. public:
  124. void Init( const Vector &value )
  125. {
  126. m_Value.SetPositionVector3D( value );
  127. }
  128. void Set( const Vector &value, CBaseEntity *pActivator, CBaseEntity *pCaller )
  129. {
  130. m_Value.SetPositionVector3D( value );
  131. FireOutput( m_Value, pActivator, pCaller );
  132. }
  133. void Get( Vector &vec )
  134. {
  135. m_Value.Vector3D(vec);
  136. }
  137. };
  138. //-----------------------------------------------------------------------------
  139. // Purpose: parameterless entity event
  140. //-----------------------------------------------------------------------------
  141. class COutputEvent : public CBaseEntityOutput
  142. {
  143. public:
  144. // void Firing, no parameter
  145. void FireOutput( CBaseEntity *pActivator, CBaseEntity *pCaller, float fDelay = 0 );
  146. };
  147. // useful typedefs for allowed output data types
  148. typedef CEntityOutputTemplate<variant_t,FIELD_INPUT> COutputVariant;
  149. typedef CEntityOutputTemplate<int,FIELD_INTEGER> COutputInt;
  150. typedef CEntityOutputTemplate<float,FIELD_FLOAT> COutputFloat;
  151. typedef CEntityOutputTemplate<string_t,FIELD_STRING> COutputString;
  152. typedef CEntityOutputTemplate<EHANDLE,FIELD_EHANDLE> COutputEHANDLE;
  153. typedef CEntityOutputTemplate<Vector,FIELD_VECTOR> COutputVector;
  154. typedef CEntityOutputTemplate<Vector,FIELD_POSITION_VECTOR> COutputPositionVector;
  155. typedef CEntityOutputTemplate<color32,FIELD_COLOR32> COutputColor32;
  156. #endif // ENTITYOUTPUT_H