HeadlessClient_SCFramework.SQF 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. HeadlessClient_SCFramework.SQF
  3. Script file for any and all headless clients that connect to dedicated server
  4. containing eventhandlers and related functions
  5. */
  6. if (hasInterface || isServer) exitWith {}; // Headless clients only
  7. // Reset global variables
  8. SCFramework_HCPingResponseServer = []; // Reset default
  9. SCFramework_ClientID = nil; // ClientID is the owner number that is used during publicVariableClient calls from the server for offloading AI units/groups
  10. SCFramework_HCNumber = nil; // HC Number differs from ClientID; Number is an arbitrary figure
  11. // Eventhandlers/Functions
  12. // Called from server-side AI spawning module that checks for ready state of a specific headless client - Randomnumber and owner ID is sent
  13. // Can be used to verify said ready state for selected headless client (via ClientID) from any server-side script
  14. "SCFramework_PingHeadlessClient" addPublicVariableEventHandler {
  15. _Array = _this select 1;
  16. _RandomNum = _Array select 0;
  17. _Owner = _Array select 1;
  18. if (BDC_SCFramework_DetailedLogging) then {
  19. diag_log format["(SCFramework) Received ping from server (%1). Sending response back.",_RandomNum];
  20. };
  21. SCFramework_HCPingResponseServer = [_RandomNum,_Owner];
  22. publicVariableServer "SCFramework_HCPingResponseServer";
  23. };
  24. // One-way ping sent from server during onPlayerConnected routine that sends the headless client its unique Client ID (SCFramework_ClientID)
  25. // as well as its headless client number (1, 2, 3, etc). Can be referenced in other scripts when sending requests to server
  26. "SCFramework_HCSendClientID" addPublicVariableEventHandler {
  27. _Array = _this select 1;
  28. _SentNum = _Array select 0;
  29. _SentClientID = _Array select 1;
  30. _SendHCNumber = _Array select 2;
  31. if (_SentNum == 1) then {
  32. SCFramework_ClientID = _SentClientID;
  33. SCFramework_HCNumber = _SendHCNumber;
  34. if (BDC_SCFramework_DetailedLogging) then {
  35. diag_log format["(SCFramework) Ping sent from server - Headless Client owner ID: %1 | Headless Client number: %2",SCFramework_ClientID,SCFramework_HCNumber];
  36. };
  37. };
  38. };
  39. // Ping for request of FPS information from player client to server and HCs
  40. "SCFramework_ServerFPSRequest" addPublicVariableEventHandler {
  41. _ClientID = _this select 1;
  42. private["_OwnedAI","_CachedAI","_Name","_FPS","_ReturnArray","_OwnedVeh","_CachedVeh"];
  43. // Gather owned AI (cached and not, alive only, no players)
  44. _OwnedAI = 0;
  45. _CachedAI = 0;
  46. _VehArray = [];
  47. {
  48. // Check for AI
  49. if (local _x && alive _x && !isPlayer _x) then {
  50. if (vehicle _x != _x) then {
  51. if !(vehicle _x in _VehArray) then {
  52. _VehArray pushBackUnique (vehicle _x)
  53. };
  54. };
  55. _OwnedAI = _OwnedAI + 1;
  56. _isAIVCached = false;
  57. _isAIVCached = _x getVariable ["isAIVCached",false]; // Fnc_AIVManager
  58. if (_isAIVCached || !(simulationEnabled _x)) then {
  59. _CachedAI = _CachedAI + 1;
  60. };
  61. };
  62. } forEach allUnits;
  63. // Get server FPS and FPSmin
  64. _FPS = round(diag_fps);
  65. // Add to array we are building to send back to client
  66. _Name = "";
  67. switch (SCFramework_HCNumber) do {
  68. case 1 : { _Name = "HC1"; };
  69. case 2 : { _Name = "HC2"; };
  70. case 3 : { _Name = "HC3"; };
  71. case 4 : { _Name = "HC4"; };
  72. case 5 : { _Name = "HC5"; };
  73. };
  74. _ReturnArray = [_Name,_FPS,_OwnedAI,_CachedAI,0,0];
  75. SCFramework_ServerFPSResponse = _ReturnArray;
  76. _ClientID publicVariableClient "SCFramework_ServerFPSResponse";
  77. };
  78. // Performance/AI Ownership logging
  79. BDC_SCFramework_Logging = {
  80. _LastLog = 0;
  81. while {true} do {
  82. _TimeDiff = time - _LastLog;
  83. if (_TimeDiff >= BDC_SCFramework_LoggingFreq) then {
  84. _LastLog = time;
  85. _StartLogTime = time;
  86. _FPS = diag_FPS;
  87. _FPSMin = diag_fpsMin;
  88. _CachedAI = 0;
  89. _OwnedUnits = 0;
  90. {
  91. if (local _x && !isPlayer _x) then {
  92. _OwnedUnits = _OwnedUnits + 1;
  93. if !(simulationEnabled _x) then {
  94. _CachedAI = _CachedAI + 1;
  95. };
  96. };
  97. } forEach allUnits;
  98. _ActiveAI = _OwnedUnits - _CachedAI;
  99. diag_log format["(SCFramework) Headless Client #%5 - Current FPS: %1 | FPS Min: %2 | Locally Owned AI: %3 | Cached AI: %4 | Active AI: %6",_FPS,_FPSMin,_OwnedUnits,_CachedAI,SCFramework_HCNumber,_ActiveAI];
  100. };
  101. sleep 30;
  102. };
  103. };
  104. // Logging routine starter
  105. _LogRoutine = {
  106. if (BDC_SCFramework_LoggingFreq > 0) then {
  107. if (BDC_SCFramework_LoggingDelay > 0) then {
  108. diag_log format["(SCFramework) Delaying start of performance and AI ownership automated logging for %1 seconds.",BDC_SCFramework_LoggingDelay];
  109. sleep BDC_SCFramework_LoggingDelay;
  110. };
  111. if (!isServer) then {
  112. diag_log format["(SCFramework) Starting performance and AI ownership logging for Headless Client %1 every %2 seconds.",SCFramework_HCNumber,BDC_SCFramework_LoggingFreq];
  113. } else {
  114. diag_log format["(SCFramework) Starting performance and AI ownership logging for server every %2 seconds.",BDC_SCFramework_LoggingFreq];
  115. };
  116. [] spawn BDC_SCFramework_Logging;
  117. };
  118. };
  119. // Request our Client ID
  120. if (BDC_SCFramework_DetailedLogging) then {
  121. diag_log format["(SCFramework) Requesting our Client ID and Headless Client Number from server. Sending UID: %1",(getPlayerUID player)];
  122. };
  123. SCFramework_HCPingRequestClientID = (getPlayerUID player);
  124. publicVariableServer "SCFramework_HCPingRequestClientID";
  125. // Start logging routine, if enabled
  126. [] spawn _LogRoutine;
  127. diag_log format["(SCFramework) HeadlessClient: Global variables reset and eventhandlers/functions loaded."];