comnetrcvinst.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/Commando/comnetrcvinst.cpp $*
  25. * *
  26. * $Author:: Steve_t $*
  27. * *
  28. * $Modtime:: 2/22/02 7:00p $*
  29. * *
  30. * $Revision:: 118 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "comnetrcvinst.h"
  36. #include "gameobjmanager.h"
  37. #include "playermanager.h"
  38. #include "cnetwork.h"
  39. #include "WWAudio.H"
  40. #include "ccamera.h"
  41. #include "gametype.h"
  42. #include "textdisplay.h"
  43. #include "wwprofile.h"
  44. #include "serverfps.h"
  45. #include "useroptions.h"
  46. #include "serversettings.h"
  47. #include "consolemode.h"
  48. #include "demosupport.h"
  49. //-----------------------------------------------------------------------------
  50. void CombatNetworkReceiverInstanceClass::Print( const char *format, ... )
  51. {
  52. va_list arg_list;
  53. va_start (arg_list, format);
  54. StringClass string;
  55. string.Format_Args( format, arg_list );
  56. ConsoleBox.Print_Maybe(string);
  57. if (!ConsoleBox.Is_Exclusive()) {
  58. WWASSERT( Get_Text_Display() );
  59. Get_Text_Display()->Print_System( string );
  60. }
  61. va_end (arg_list);
  62. }
  63. //-----------------------------------------------------------------------------
  64. void CombatNetworkReceiverInstanceClass::Print( const Vector3 & color, const char *format, ... )
  65. {
  66. WWASSERT( Get_Text_Display() );
  67. va_list arg_list;
  68. va_start (arg_list, format);
  69. StringClass string;
  70. string.Format_Args( format, arg_list );
  71. WideStringClass ws(string, true);
  72. ConsoleBox.Add_Message(&ws, (Vector3*)&color);
  73. if (!ConsoleBox.Is_Exclusive()) {
  74. WWASSERT( Get_Text_Display() );
  75. Get_Text_Display()->Print( string, color );
  76. }
  77. va_end (arg_list);
  78. }
  79. //-----------------------------------------------------------------------------
  80. void CombatNetworkReceiverInstanceClass::Server_Send_Delete_Notifications(void)
  81. {
  82. WWASSERT(cNetwork::I_Am_Server());
  83. if (IS_SOLOPLAY) {
  84. return;
  85. }
  86. //
  87. // Figure out what data to send to each player (client)
  88. //
  89. for (
  90. SLNode<cPlayer> * player_node = cPlayerManager::Get_Player_Object_List()->Head();
  91. player_node;
  92. player_node = player_node->Next()) {
  93. cPlayer * p_player = player_node->Data();
  94. WWASSERT(p_player != NULL);
  95. int client_id = p_player->Get_Id();
  96. if (client_id < 0) {
  97. //
  98. // AI... no sends
  99. //
  100. continue;
  101. }
  102. if (p_player->Get_Is_Active().Is_False()) {
  103. continue;
  104. }
  105. if (p_player->Get_Is_In_Game().Is_False()) {
  106. //
  107. // Loading... delay send
  108. //
  109. continue;
  110. }
  111. cNetwork::Tell_Client_About_Delete_Notifications(client_id);
  112. }
  113. return ;
  114. }
  115. //-----------------------------------------------------------------------------
  116. bool CombatNetworkReceiverInstanceClass::Server_Update_Dynamic_Objects(bool is_urgent)
  117. {
  118. WWASSERT(cNetwork::I_Am_Server());
  119. if (IS_SOLOPLAY) {
  120. return(false);
  121. }
  122. DEMO_SECURITY_CHECK;
  123. //
  124. // Limit the maximum rate at which the server sends updates.
  125. // This code will result in updates at a little less than NetUpdateRate.
  126. // This saves bandwidth at the cost of added latency.
  127. //
  128. static DWORD last_update_time = 0;
  129. DWORD time_now = TIMEGETTIME();
  130. if (!is_urgent && (time_now - last_update_time < 1000 / (float) cUserOptions::NetUpdateRate.Get())) {
  131. return(false);
  132. }
  133. last_update_time = time_now;
  134. //
  135. // cRemoteHost figures out when to update priorities based on this so keep it in sync with the NetUpdateRate.
  136. //
  137. cRemoteHost::Set_Priority_Update_Rate(cUserOptions::NetUpdateRate.Get());
  138. //
  139. // TSS - bug
  140. // Must handle sniper... also, should use camera position
  141. // rather than commando position
  142. //
  143. //
  144. // Figure out what data to send to each player (client)
  145. //
  146. for (
  147. SLNode<cPlayer> * player_node = cPlayerManager::Get_Player_Object_List()->Head();
  148. player_node;
  149. player_node = player_node->Next()) {
  150. cPlayer * p_player = player_node->Data();
  151. WWASSERT(p_player != NULL);
  152. int client_id = p_player->Get_Id();
  153. if (client_id < 0) {
  154. //
  155. // AI... no sends
  156. //
  157. continue;
  158. }
  159. if (p_player->Get_Is_Active().Is_False()) {
  160. continue;
  161. }
  162. if (!is_urgent && p_player->Get_Is_In_Game().Is_False()) {
  163. //
  164. // Loading... delay sends
  165. //
  166. continue;
  167. }
  168. //is_urgent = true;//XXX
  169. if (!is_urgent) {
  170. //
  171. // Do not send to the client at a rate much higher than his framerate
  172. // This code will only impact if his framerate is below NetUpdateRate.
  173. // This code should help prevent players whose framerate bogs from
  174. // getting packet-swamped.
  175. //
  176. int min_delay_ms = 0;
  177. if (p_player->Get_Fps() > 0) {
  178. const float arbitrary_scale_factor = 1.0;
  179. min_delay_ms = (int)(arbitrary_scale_factor * 1000 / (float) p_player->Get_Fps());
  180. }
  181. int time_elapsed_ms = time_now - p_player->Get_Last_Update_Time_Ms();
  182. if (time_elapsed_ms < min_delay_ms) {
  183. continue;
  184. }
  185. }
  186. p_player->Set_Last_Update_Time_Ms(time_now);
  187. SmartGameObj * p_soldier = GameObjManager::Find_Soldier_Of_Client_ID(client_id);
  188. Vector3 dest_pos;
  189. if (p_soldier == NULL) {
  190. //
  191. // Act like the guy is far, far away.
  192. // He'll only receive priority 1 messages.
  193. //
  194. dest_pos.Set(Vector3(0, 0, -10000));
  195. } else {
  196. p_soldier->Get_Position(&dest_pos);
  197. //
  198. // His eyes are say 1.5 m above his feet
  199. //
  200. dest_pos.Z += 1.5;
  201. }
  202. cNetwork::Tell_Client_About_Dynamic_Objects(client_id, dest_pos);
  203. }
  204. return(true);
  205. }
  206. //-----------------------------------------------------------------------------
  207. bool CombatNetworkReceiverInstanceClass::Client_Update_Dynamic_Objects(bool is_urgent)
  208. {
  209. //
  210. // TSS092001
  211. // Do not send updates to the server at a rate higher than NetUpdateRate.
  212. // If the server framerate is below this, match the client send rate to it.
  213. //
  214. /*TSS092101*/
  215. WWASSERT(cNetwork::I_Am_Client());
  216. static DWORD last_update_time_ms = 0;
  217. DWORD time_now_ms = TIMEGETTIME();
  218. DWORD time_elapsed_ms = time_now_ms - last_update_time_ms;
  219. int max_updates_per_second = cUserOptions::NetUpdateRate.Get();
  220. WWASSERT(cServerFps::Get_Instance() != NULL);
  221. int server_fps = cServerFps::Get_Instance()->Get_Fps();
  222. if (server_fps > 0 && server_fps < cUserOptions::NetUpdateRate.Get()) {
  223. max_updates_per_second = server_fps;
  224. }
  225. DWORD min_delay_ms = (DWORD)(1000 / (float) max_updates_per_second);
  226. //is_urgent = true;//XXX
  227. if (is_urgent || (time_elapsed_ms >= min_delay_ms)) {
  228. last_update_time_ms = time_now_ms;
  229. cNetwork::Tell_Server_About_Dynamic_Objects();
  230. return(true);
  231. }
  232. return(false);
  233. /**/
  234. }