networkobject.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** Confidential - Westwood Studios ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Commando *
  23. * *
  24. * $Archive:: /Commando/Code/wwnet/networkobject.h $*
  25. * *
  26. * $Author:: Jani_p $*
  27. * *
  28. * $Modtime:: 1/15/02 2:03p $*
  29. * *
  30. * $Revision:: 34 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #if defined(_MSC_VER)
  36. #pragma once
  37. #endif
  38. #ifndef __NETWORKOBJECT_H
  39. #define __NETWORKOBJECT_H
  40. #include "wwpacket.h"
  41. enum PACKET_TIER_ENUM
  42. {
  43. PACKET_TIER_CREATION,
  44. PACKET_TIER_RARE,
  45. PACKET_TIER_OCCASIONAL,
  46. PACKET_TIER_FREQUENT,
  47. PACKET_TIER_COUNT
  48. };
  49. ////////////////////////////////////////////////////////////////
  50. // Forward delcarations
  51. ////////////////////////////////////////////////////////////////
  52. class BitStreamClass;
  53. ////////////////////////////////////////////////////////////////
  54. //
  55. // NetworkObjectClass
  56. //
  57. // Base class that all objects which need to transmit state
  58. // updates across the network are derived from.
  59. //
  60. ////////////////////////////////////////////////////////////////
  61. class NetworkObjectClass
  62. {
  63. public:
  64. ////////////////////////////////////////////////////////////////
  65. // Public constants
  66. ////////////////////////////////////////////////////////////////
  67. typedef enum
  68. {
  69. BIT_FREQUENT = 0x01,
  70. BIT_OCCASIONAL = 0x02 | BIT_FREQUENT,
  71. BIT_RARE = 0x04 | BIT_OCCASIONAL,
  72. BIT_CREATION = 0x08 | BIT_RARE,
  73. } DIRTY_BIT;
  74. enum
  75. {
  76. MAX_CLIENT_COUNT = 128
  77. };
  78. ////////////////////////////////////////////////////////////////
  79. // Public constructors/destructors
  80. ////////////////////////////////////////////////////////////////
  81. NetworkObjectClass (void);
  82. virtual ~NetworkObjectClass (void);
  83. ////////////////////////////////////////////////////////////////
  84. // Public methods
  85. ////////////////////////////////////////////////////////////////
  86. //
  87. // ID support
  88. //
  89. int Get_Network_ID (void) const { return NetworkID; }
  90. void Set_Network_ID (int id);
  91. #ifdef WWDEBUG
  92. int Get_Created_By_Packet_ID (void) const { return CreatedByPacketID; }
  93. void Set_Created_By_Packet_ID (int id);
  94. #endif //WWDEBUG
  95. //
  96. // Class ID support
  97. //
  98. virtual uint32 Get_Network_Class_ID (void) const { return 0; }
  99. //
  100. // Server-to-client data importing/exporting
  101. //
  102. virtual void Import_Creation (BitStreamClass &packet) {}
  103. virtual void Import_Rare (BitStreamClass &packet) {}
  104. virtual void Import_Occasional (BitStreamClass &packet) {}
  105. virtual void Import_Frequent (BitStreamClass &packet) {}
  106. virtual void Export_Creation (BitStreamClass &packet) {}
  107. virtual void Export_Rare (BitStreamClass &packet) {}
  108. virtual void Export_Occasional (BitStreamClass &packet) {}
  109. virtual void Export_Frequent (BitStreamClass &packet) {}
  110. //
  111. // Timestep support
  112. //
  113. virtual void Network_Think (void) {}
  114. //
  115. // Delete support.
  116. // Override Delete in the subclass if you have a destructor there.
  117. //
  118. bool Is_Delete_Pending (void) { return IsDeletePending; }
  119. virtual void Set_Delete_Pending (void);
  120. virtual void Delete (void) = 0;
  121. //
  122. // Record application packet type
  123. //
  124. void Set_App_Packet_Type (BYTE type) { AppPacketType = type; }
  125. BYTE Get_App_Packet_Type (void) { return AppPacketType; }
  126. //
  127. // Dirty bit support
  128. //
  129. virtual void Set_Object_Dirty_Bit (DIRTY_BIT dirty_bit, bool onoff);
  130. virtual void Set_Object_Dirty_Bit (int client_id, DIRTY_BIT dirty_bit, bool onoff);
  131. virtual void Clear_Object_Dirty_Bits (void);
  132. virtual bool Get_Object_Dirty_Bit (int client_id, DIRTY_BIT dirty_bit);
  133. virtual BYTE Get_Object_Dirty_Bits (int client_id);
  134. virtual void Set_Object_Dirty_Bits (int client_id, BYTE bits);
  135. virtual bool Is_Client_Dirty (int client_id);
  136. inline bool Get_Object_Dirty_Bit_2 (int client_id, DIRTY_BIT dirty_bit);
  137. inline BYTE Get_Object_Dirty_Bits_2 (int client_id);
  138. //
  139. // Filtering support
  140. //
  141. virtual int Get_Vis_ID (void) { return -1; }
  142. virtual bool Get_World_Position (Vector3 &pos) const { return false; }
  143. virtual float Get_Filter_Distance(void) const { return 10000.0f; }
  144. //
  145. // Client-side update tracking
  146. //
  147. void Reset_Client_Hint_Count(int client_id);
  148. void Increment_Client_Hint_Count(int client_id);
  149. void Hint_To_All_Clients(void);
  150. BYTE Get_Client_Hint_Count(int client_id);
  151. inline BYTE Get_Client_Hint_Count_2(int client_id);
  152. void Reset_Import_State_Count (void) { ImportStateCount = 0; }
  153. void Increment_Import_State_Count (void) { ImportStateCount ++; }
  154. int Get_Import_State_Count (void) { return ImportStateCount; }
  155. void Reset_Last_Clientside_Update_Time (void);
  156. void Set_Last_Clientside_Update_Time (ULONG time);
  157. ULONG Get_Last_Clientside_Update_Time (void) { return LastClientsideUpdateTime; }
  158. int Get_Clientside_Update_Frequency(void);
  159. //
  160. // Ownership
  161. //
  162. bool Belongs_To_Client(int client_id);
  163. //
  164. // Per client update functions.
  165. //
  166. unsigned char Get_Frequent_Update_Export_Size(void) {return(FrequentExportPacketSize);}
  167. void Set_Frequent_Update_Export_Size(unsigned char size) {FrequentExportPacketSize = size;}
  168. unsigned long Get_Last_Update_Time(int client_id);
  169. unsigned short Get_Update_Rate(int client_id);
  170. void Set_Last_Update_Time(int client_id, unsigned long time);
  171. void Set_Update_Rate(int client_id, unsigned short rate);
  172. //
  173. // Diagnostics
  174. //
  175. virtual bool Is_Tagged(void) { return false; }
  176. virtual void Get_Description(StringClass & description) {}
  177. void Set_Unreliable_Override(bool flag) {UnreliableOverride = flag;}
  178. bool Get_Unreliable_Override(void) {return UnreliableOverride;}
  179. //
  180. // Static methods
  181. //
  182. static void Set_Is_Server(bool flag) { IsServer = flag; }
  183. void Set_Cached_Priority (float priority);
  184. virtual float Get_Cached_Priority (void) const { return CachedPriority; }
  185. inline void Set_Cached_Priority_2 (int client_id, float priority);
  186. inline float Get_Cached_Priority_2 (int client_id) const;
  187. void Set_Last_Object_Id_I_Damaged(int id) {LastObjectIdIDamaged = id;}
  188. int Get_Last_Object_Id_I_Damaged(void) const {return LastObjectIdIDamaged;}
  189. void Set_Last_Object_Id_I_Got_Damaged_By(int id) {LastObjectIdIGotDamagedBy = id;}
  190. int Get_Last_Object_Id_I_Got_Damaged_By(void) const {return LastObjectIdIGotDamagedBy;}
  191. private:
  192. ////////////////////////////////////////////////////////////////
  193. // Private constants
  194. ////////////////////////////////////////////////////////////////
  195. ////////////////////////////////////////////////////////////////
  196. // Private member data
  197. ////////////////////////////////////////////////////////////////
  198. int NetworkID;
  199. #ifdef WWDEBUG
  200. int CreatedByPacketID;
  201. #endif //WWDEBUG
  202. //
  203. // Per client update information. Bandwidth will be allocated per object, per client.
  204. //
  205. struct PerClientUpdateInfoStruct {
  206. unsigned long LastUpdateTime;
  207. unsigned short UpdateRate;
  208. BYTE ClientHintCount;
  209. } UpdateInfo [MAX_CLIENT_COUNT];
  210. BYTE ClientStatus[MAX_CLIENT_COUNT];
  211. int ImportStateCount;
  212. ULONG LastClientsideUpdateTime;
  213. ULONG ClientsideUpdateFrequencySampleStartTime;
  214. int ClientsideUpdateFrequencySampleCount;
  215. int ClientsideUpdateRate;
  216. bool IsDeletePending;
  217. BYTE AppPacketType;
  218. int LastObjectIdIDamaged;
  219. int LastObjectIdIGotDamagedBy;
  220. //
  221. // The size of this objects FREQUENT tier export. Used as a starting point for bandwidth calculation.
  222. // It better not be exporting more than 255 bytes!
  223. //
  224. unsigned char FrequentExportPacketSize;
  225. float CachedPriority;
  226. float CachedPriority_2[MAX_CLIENT_COUNT];
  227. bool UnreliableOverride;
  228. static bool IsServer;
  229. };
  230. #endif // __NETWORKOBJECT_H
  231. ////////////////////////////////////////////////////////////////
  232. //
  233. // Set_Cached_Priority
  234. //
  235. ////////////////////////////////////////////////////////////////
  236. inline void NetworkObjectClass::Set_Cached_Priority_2(int client_id, float priority)
  237. {
  238. CachedPriority_2[client_id] = priority;
  239. }
  240. ////////////////////////////////////////////////////////////////
  241. //
  242. // Set_Cached_Priority
  243. //
  244. ////////////////////////////////////////////////////////////////
  245. inline float NetworkObjectClass::Get_Cached_Priority_2(int client_id) const
  246. {
  247. return(CachedPriority_2[client_id]);
  248. }
  249. ////////////////////////////////////////////////////////////////
  250. //
  251. // Get_Object_Dirty_Bit
  252. //
  253. ////////////////////////////////////////////////////////////////
  254. inline bool NetworkObjectClass::Get_Object_Dirty_Bit_2 (int client_id, DIRTY_BIT dirty_bit)
  255. {
  256. return ((ClientStatus[client_id] & dirty_bit) == dirty_bit);
  257. }
  258. ////////////////////////////////////////////////////////////////
  259. //
  260. // Get_Object_Dirty_Bits
  261. //
  262. ////////////////////////////////////////////////////////////////
  263. inline BYTE NetworkObjectClass::Get_Object_Dirty_Bits_2 (int client_id)
  264. {
  265. return ClientStatus[client_id];
  266. }
  267. ////////////////////////////////////////////////////////////////
  268. //
  269. // Get_Client_Hint_Count
  270. //
  271. ////////////////////////////////////////////////////////////////
  272. inline BYTE NetworkObjectClass::Get_Client_Hint_Count_2(int client_id)
  273. {
  274. return UpdateInfo[client_id].ClientHintCount;
  275. }
  276. //virtual void Clear_Object_Dirty_Bits (int client_id);
  277. //static void Set_Random_Float (float random_float);
  278. //static float RandomFloat;
  279. //float CachedPriority;
  280. //static float MaxDistance;
  281. //virtual float Compute_Object_Priority (int client_id, const Vector3 &client_pos);
  282. //void Set_Cached_Priority (float priority);
  283. //virtual float Get_Object_Distance (const Vector3 &client_pos);
  284. //virtual float Get_Cached_Priority (void) const { return CachedPriority; }
  285. //static void Set_Max_Distance (float distance) { MaxDistance = distance; }
  286. //static float MaxDistance;