kleinToHCs.sqf 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * kleinToHCs.sqf
  3. *
  4. * In the mission editor, name the Headless Clients "HC1", "HC2", "HC3" without the quotes
  5. *
  6. * In the mission init.sqf, call kleinToHCs.sqf with:
  7. * execVM "kleinToHCs.sqf";
  8. *
  9. * It seems that the dedicated server and headless client processes never use more than 20-22% CPU each.
  10. * With a dedicated server and 3 headless clients, that's about 88% CPU with 10-12% left over. Far more efficient use of your processing power.
  11. *
  12. */
  13. if (!isServer) exitWith {};
  14. diag_log "kleinToHCs: Started";
  15. waitUntil {!isNil "HC1_1"};
  16. waitUntil {!isNull HC1_1};
  17. _HC_ID = -1; // Will become the Client ID of HC
  18. _HC2_ID = -1; // Will become the Client ID of HC2
  19. _HC3_ID = -1; // Will become the Client ID of HC3
  20. rebalanceTimer = 60; // Rebalance sleep timer in seconds
  21. cleanUpThreshold = 50; // Threshold of number of dead bodies + destroyed vehicles before forcing a clean up
  22. diag_log format["kleinToHCs: First pass will begin in %1 seconds", rebalanceTimer];
  23. while {true} do {
  24. // Rebalance every rebalanceTimer seconds to avoid hammering the server
  25. sleep rebalanceTimer;
  26. // Do not enable load balancing unless more than one HC is present
  27. // Leave this variable false, we'll enable it automatically under the right conditions
  28. _loadBalance = false;
  29. // Get HC Client ID else set variables to null
  30. try {
  31. _HC_ID = owner HC1_1;
  32. if (_HC_ID > 2) then {
  33. diag_log format ["kleinToHCs: Found HC with Client ID %1", _HC_ID];
  34. } else {
  35. diag_log "kleinToHCs: [WARN] HC disconnected";
  36. HC1_1 = objNull;
  37. _HC_ID = -1;
  38. };
  39. } catch { diag_log format ["kleinToHCs: [ERROR] [HC1_1] %1", _exception]; HC1_1 = objNull; _HC_ID = -1; };
  40. // Get HC2 Client ID else set variables to null
  41. if (!isNil "HC2_1") then {
  42. try {
  43. _HC2_ID = owner HC2_1;
  44. if (_HC2_ID > 2) then {
  45. diag_log format ["kleinToHCs: Found HC2 with Client ID %1", _HC2_ID];
  46. } else {
  47. diag_log "kleinToHCs: [WARN] HC2 disconnected";
  48. HC2_1 = objNull;
  49. _HC2_ID = -1;
  50. };
  51. } catch { diag_log format ["kleinToHCs: [ERROR] [HC2_1] %1", _exception]; HC2_1 = objNull; _HC2_ID = -1; };
  52. };
  53. // Get HC3 Client ID else set variables to null
  54. if (!isNil "HC3_1") then {
  55. try {
  56. _HC3_ID = owner HC3_1;
  57. if (_HC3_ID > 2) then {
  58. diag_log format ["kleinToHCs: Found HC2 with Client ID %1", _HC3_ID];
  59. } else {
  60. diag_log "kleinToHCs: [WARN] HC3 disconnected";
  61. HC3_1 = objNull;
  62. _HC3_ID = -1;
  63. };
  64. } catch { diag_log format ["kleinToHCs: [ERROR] [HC3_1] %1", _exception]; HC3_1 = objNull; _HC3_ID = -1; };
  65. };
  66. // If no HCs present, wait for HC to rejoin
  67. if ( (isNull HC1_1) && (isNull HC2_1) && (isNull HC3_1) ) then { waitUntil {!isNull HC1_1}; };
  68. // Check to auto enable Round-Robin load balancing strategy
  69. if ( (!isNull HC1_1 && !isNull HC2_1) || (!isNull HC1_1 && !isNull HC3_1) || (!isNull HC2_1 && !isNull HC3_1) ) then { _loadBalance = true; };
  70. if ( _loadBalance ) then {
  71. diag_log "kleinToHCs: Starting load-balanced transfer of AI groups to HCs";
  72. } else {
  73. // No load balancing
  74. diag_log "kleinToHCs: Starting transfer of AI groups to HC";
  75. };
  76. // Determine first HC to start with
  77. _currentHC = 0;
  78. if (!isNull HC1_1) then { _currentHC = 1; } else {
  79. if (!isNull HC2_1) then { _currentHC = 2; } else { _currentHC = 3; };
  80. };
  81. // Pass the AI
  82. _numTransfered = 0;
  83. {
  84. _swap = true;
  85. // If a player is in this group, don't swap to an HC
  86. { if (isPlayer _x) then { _swap = false; }; } forEach (units _x);
  87. // If load balance enabled, round robin between the HCs - else pass all to HC
  88. if ( _swap ) then {
  89. _rc = false;
  90. if ( _loadBalance ) then {
  91. switch (_currentHC) do {
  92. case 1: { _rc = _x setGroupOwner _HC_ID; if (!isNull HC2_1) then { _currentHC = 2; } else { _currentHC = 3; }; };
  93. case 2: { _rc = _x setGroupOwner _HC2_ID; if (!isNull HC3_1) then { _currentHC = 3; } else { _currentHC = 1; }; };
  94. case 3: { _rc = _x setGroupOwner _HC3_ID; if (!isNull HC1_1) then { _currentHC = 1; } else { _currentHC = 2; }; };
  95. default { diag_log format["kleinToHCs: [ERROR] No Valid HC to pass to. _currentHC = %1", _currentHC]; };
  96. };
  97. } else {
  98. switch (_currentHC) do {
  99. case 1: { _rc = _x setGroupOwner _HC_ID; };
  100. case 2: { _rc = _x setGroupOwner _HC2_ID; };
  101. case 3: { _rc = _x setGroupOwner _HC3_ID; };
  102. default { diag_log format["kleinToHCs: [ERROR] No Valid HC to pass to. _currentHC = %1", _currentHC]; };
  103. };
  104. };
  105. // If the transfer was successful, count it for accounting and diagnostic information
  106. if ( _rc ) then { _numTransfered = _numTransfered + 1; };
  107. };
  108. } forEach (allGroups);
  109. if (_numTransfered > 0) then {
  110. // More accounting and diagnostic information
  111. diag_log format ["kleinToHCs: Transfered %1 AI groups to HC(s)", _numTransfered];
  112. _numHC = 0;
  113. _numHC2 = 0;
  114. _numHC3 = 0;
  115. {
  116. switch (owner ((units _x) select 0)) do {
  117. case _HC_ID: { _numHC = _numHC + 1; };
  118. case _HC2_ID: { _numHC2 = _numHC2 + 1; };
  119. case _HC3_ID: { _numHC3 = _numHC3+ 1; };
  120. };
  121. } forEach (allGroups);
  122. if (_numHC > 0) then { diag_log format ["kleinToHCs: %1 AI groups currently on HC1_1", _numHC]; };
  123. if (_numHC2 > 0) then { diag_log format ["kleinToHCs: %1 AI groups currently on HC2_1", _numHC2]; };
  124. if (_numHC3 > 0) then { diag_log format ["kleinToHCs: %1 AI groups currently on HC3_1", _numHC3]; };
  125. diag_log format ["kleinToHCs: %1 AI groups total across all HC(s)", (_numHC + _numHC2 + _numHC3)];
  126. } else {
  127. diag_log "kleinToHCs: No rebalance or transfers required this round";
  128. };
  129. // Force clean up dead bodies and destroyed vehicles
  130. if (count allDead > cleanUpThreshold) then {
  131. _numDeleted = 0;
  132. {
  133. deleteVehicle _x;
  134. _numDeleted = _numDeleted + 1;
  135. } forEach allDead;
  136. diag_log format ["kleinToHCs: Cleaned up %1 dead bodies/destroyed vehicles", _numDeleted];
  137. };
  138. };