Advertisement
Brenner650

DMS_fn_SetAILocality.sqf

Mar 4th, 2019
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 2.14 KB | None | 0 0
  1. /*
  2.     DMS_fnc_SetAILocality
  3.     Created by Defent and eraser1
  4.  
  5.     Usage:
  6.     [
  7.         _groupOrUnit,
  8.         _posOrObject        // Does not have to be defined if element 1 is a unit
  9.     ] call DMS_fnc_SetAILocality;
  10.  
  11.     Makes a random player within 3 KM of the AI unit or group the owner.
  12.     Offloading AI will improve server performance, but the unit will no longer be local, which will limit the server's control over it.
  13.     Could however have negative effects if target player has a potato PC.
  14.  
  15.     Returns true if a viable owner was found, false otherwise.
  16. */
  17.  
  18. private _AI = param [0,objNull,[objNull,grpNull]];
  19.  
  20. if (isNull _AI) exitWith
  21. {
  22.     diag_log format ["DMS ERROR :: Calling DMS_SetAILocality with null parameter; _this: %1",_this];
  23. };
  24.  
  25. private _AIType = typeName _AI;
  26.  
  27. private _pos = if (_AIType isEqualTo "OBJECT") then {_AI} else {param [1,"",[objNull,[]],[2,3]]};
  28.  
  29. if (_pos isEqualTo "") exitWith
  30. {
  31.     diag_log format ["DMS ERROR :: Calling DMS_SetAILocality with invalid position; this: %1",_this];
  32. };
  33.  
  34. private _client = objNull;
  35. private _allHCs = entities "HeadlessClient_F";
  36.  
  37. if ((count _allHCs) > 0) then
  38. {
  39.     _client = _allHCs select 0;
  40. }
  41. else
  42. {
  43.     {
  44.         if ((alive _x) && {(_x distance2D _pos)<=3000}) exitWith
  45.         {
  46.             _client = _x;
  47.         };
  48.     }forEach allPlayers;
  49. };
  50.  
  51. if (!isNull _client) then
  52. {
  53.     private _swapped = if (_AIType isEqualTo "OBJECT") then {_AI setOwner (owner _client)} else {_AI setGroupOwner (owner _client)};
  54.  
  55.     if (!_swapped) then
  56.     {
  57.         ExileServerOwnershipSwapQueue pushBack [_AI,_client];
  58.     };
  59.  
  60.     private _msg = format ["DMS :: AI %1 |%2| has been offloaded to you.",_AIType,_AI];
  61.     _msg remoteExecCall ["diag_log", _client];
  62.     if (DMS_ai_offload_notifyClient) then
  63.     {
  64.         _msg remoteExecCall ["systemChat", _client];
  65.     };
  66.  
  67.     if (DMS_DEBUG) then
  68.     {
  69.         (format ["SetAILocality :: Ownership swap of %1 (%4) to %2 (%3) is initialized. Initial swap attempt successful: %5",_AI, name _client, getPlayerUID _client, _AIType, _swapped]) call DMS_fnc_DebugLog;
  70.     };
  71.  
  72.     true
  73. }
  74. else
  75. {
  76.     if (DMS_DEBUG) then
  77.     {
  78.         (format ["SetAILocality :: No viable client found for the ownership of %1! _pos: %2.",_AI,_pos]) call DMS_fnc_DebugLog;
  79.     };
  80.  
  81.     false
  82. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement