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.

195 lines
5.4 KiB

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