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.

203 lines
5.8 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef PREDICTABLE_ENTITY_H
  8. #define PREDICTABLE_ENTITY_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. // For introspection
  13. #include "tier0/platform.h"
  14. #include "predictioncopy.h"
  15. #include "shared_classnames.h"
  16. #ifndef NO_ENTITY_PREDICTION
  17. #define UsePrediction() 1
  18. #else
  19. #define UsePrediction() 0
  20. #endif
  21. // CLIENT DLL includes
  22. #if defined( CLIENT_DLL )
  23. #include "iclassmap.h"
  24. #include "recvproxy.h"
  25. class SendTable;
  26. // Game DLL includes
  27. #else
  28. #include "sendproxy.h"
  29. #endif // !CLIENT_DLL
  30. #if defined( CLIENT_DLL )
  31. #define DECLARE_NETWORKCLASS() \
  32. DECLARE_CLIENTCLASS()
  33. #define DECLARE_NETWORKCLASS_NOBASE() \
  34. DECLARE_CLIENTCLASS_NOBASE()
  35. #else
  36. #define DECLARE_NETWORKCLASS() \
  37. DECLARE_SERVERCLASS()
  38. #define DECLARE_NETWORKCLASS_NOBASE() \
  39. DECLARE_SERVERCLASS_NOBASE()
  40. #endif
  41. #if defined( CLIENT_DLL )
  42. #ifndef NO_ENTITY_PREDICTION
  43. #define DECLARE_PREDICTABLE() \
  44. public: \
  45. static typedescription_t m_PredDesc[]; \
  46. static datamap_t m_PredMap; \
  47. virtual datamap_t *GetPredDescMap( void ); \
  48. template <typename T> friend datamap_t *PredMapInit(T *)
  49. #else
  50. #define DECLARE_PREDICTABLE() template <typename T> friend datamap_t *PredMapInit(T *)
  51. #endif
  52. #ifndef NO_ENTITY_PREDICTION
  53. #define BEGIN_PREDICTION_DATA( className ) \
  54. datamap_t className::m_PredMap = { 0, 0, #className, &BaseClass::m_PredMap }; \
  55. datamap_t *className::GetPredDescMap( void ) { return &m_PredMap; } \
  56. BEGIN_PREDICTION_DATA_GUTS( className )
  57. #define BEGIN_PREDICTION_DATA_NO_BASE( className ) \
  58. datamap_t className::m_PredMap = { 0, 0, #className, NULL }; \
  59. datamap_t *className::GetPredDescMap( void ) { return &m_PredMap; } \
  60. BEGIN_PREDICTION_DATA_GUTS( className )
  61. #define BEGIN_PREDICTION_DATA_GUTS( className ) \
  62. template <typename T> datamap_t *PredMapInit(T *); \
  63. template <> datamap_t *PredMapInit<className>( className * ); \
  64. namespace className##_PredDataDescInit \
  65. { \
  66. datamap_t *g_PredMapHolder = PredMapInit( (className *)NULL ); /* This can/will be used for some clean up duties later */ \
  67. } \
  68. \
  69. template <> datamap_t *PredMapInit<className>( className * ) \
  70. { \
  71. typedef className classNameTypedef; \
  72. static typedescription_t predDesc[] = \
  73. { \
  74. { FIELD_VOID,0, {0,0},0,0,0,0,0,0}, /* so you can define "empty" tables */
  75. #define END_PREDICTION_DATA() \
  76. }; \
  77. \
  78. if ( sizeof( predDesc ) > sizeof( predDesc[0] ) ) \
  79. { \
  80. classNameTypedef::m_PredMap.dataNumFields = ARRAYSIZE( predDesc ) - 1; \
  81. classNameTypedef::m_PredMap.dataDesc = &predDesc[1]; \
  82. } \
  83. else \
  84. { \
  85. classNameTypedef::m_PredMap.dataNumFields = 1; \
  86. classNameTypedef::m_PredMap.dataDesc = predDesc; \
  87. } \
  88. return &classNameTypedef::m_PredMap; \
  89. }
  90. #else
  91. #define BEGIN_PREDICTION_DATA( className ) \
  92. template <> inline datamap_t *PredMapInit<className>( className * ) \
  93. { \
  94. if ( 0 ) \
  95. { \
  96. typedef className classNameTypedef; \
  97. typedescription_t predDesc[] = \
  98. { \
  99. { FIELD_VOID,0, {0,0},0,0,0,0,0,0},
  100. #define BEGIN_PREDICTION_DATA_NO_BASE( className ) BEGIN_PREDICTION_DATA( className )
  101. #define END_PREDICTION_DATA() \
  102. }; \
  103. predDesc[0].flags = 0; /* avoid compiler warning of unused data */ \
  104. } \
  105. }
  106. #endif
  107. #else
  108. // nothing, only client has a prediction system
  109. #define DECLARE_PREDICTABLE()
  110. #define BEGIN_PREDICTION_DATA( className )
  111. #define END_PREDICTION_DATA()
  112. #endif
  113. #if defined( CLIENT_DLL )
  114. // On the client .dll this creates a mapping between a classname and
  115. // a client side class. Probably could be templatized at some point.
  116. #define LINK_ENTITY_TO_CLASS( localName, className ) \
  117. static C_BaseEntity *C##className##Factory( void ) \
  118. { \
  119. return static_cast< C_BaseEntity * >( new className ); \
  120. }; \
  121. class C##localName##Foo \
  122. { \
  123. public: \
  124. C##localName##Foo( void ) \
  125. { \
  126. GetClassMap().Add( #localName, #className, sizeof( className ), \
  127. &C##className##Factory ); \
  128. } \
  129. }; \
  130. static C##localName##Foo g_C##localName##Foo;
  131. #define BEGIN_NETWORK_TABLE( className, tableName ) BEGIN_RECV_TABLE( className, tableName )
  132. #define BEGIN_NETWORK_TABLE_NOBASE( className, tableName ) BEGIN_RECV_TABLE_NOBASE( className, tableName )
  133. #define END_NETWORK_TABLE END_RECV_TABLE
  134. #define IMPLEMENT_NETWORKCLASS_ALIASED(className, dataTable) \
  135. IMPLEMENT_CLIENTCLASS( C_##className, dataTable, C##className )
  136. #define IMPLEMENT_NETWORKCLASS(className, dataTable) \
  137. IMPLEMENT_CLIENTCLASS(className, dataTable, className)
  138. #define IMPLEMENT_NETWORKCLASS_DT(className, dataTable) \
  139. IMPLEMENT_CLIENTCLASS_DT(className, dataTable, className)
  140. #else
  141. #define BEGIN_NETWORK_TABLE( className, tableName ) BEGIN_SEND_TABLE( className, tableName )
  142. #define BEGIN_NETWORK_TABLE_NOBASE( className, tableName ) BEGIN_SEND_TABLE_NOBASE( className, tableName )
  143. #define END_NETWORK_TABLE END_SEND_TABLE
  144. #define IMPLEMENT_NETWORKCLASS_ALIASED(className, dataTable) \
  145. IMPLEMENT_SERVERCLASS( C##className, dataTable )
  146. #define IMPLEMENT_NETWORKCLASS(className, dataTable) \
  147. IMPLEMENT_SERVERCLASS(className, dataTable)
  148. #define IMPLEMENT_NETWORKCLASS_DT(className, dataTable) \
  149. IMPLEMENT_SERVERCLASS_ST(className, dataTable)
  150. #endif
  151. // Interface used by client and server to track predictable entities
  152. abstract_class IPredictableList
  153. {
  154. public:
  155. // Get predictables by index
  156. virtual CBaseEntity *GetPredictable( int slot ) = 0;
  157. // Get count of predictables
  158. virtual int GetPredictableCount( void ) = 0;
  159. };
  160. // Expose interface to rest of .dll
  161. extern IPredictableList *predictables;
  162. #endif // PREDICTABLE_ENTITY_H