Advertisement
Guest User

Untitled

a guest
May 18th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQF 5.26 KB | None | 0 0
  1. /* ----------------------------------------------------------------------------
  2. Function: perf_fnc_updateClusters
  3.  
  4. Description:
  5.     Updates clusters
  6.  
  7. Parameters:
  8. 0:  _players            - makes clusters of players instead
  9. Returns:
  10. 0:  _clusters           - array, containing clusters
  11.  
  12. Examples:
  13.     call perf_fnc_updateClusters;
  14.  
  15. Author:
  16.     nigel
  17. ---------------------------------------------------------------------------- */
  18. #include "script_component.cpp"
  19.  
  20. params [['_players',false,[false]]];
  21.  
  22. // player / server have differnet ones
  23. private _namespace = [player,missionNamespace] select (isServer);
  24.  
  25. // non player variable names
  26. private _nToCheck = 'perf_clusterToCheck';
  27. private _nClustersUsed = 'perf_clustersUsed';
  28. private _nClusters = 'perf_clusters';
  29. private _nObjCluster = 'perf_objCluster';
  30. private _nClusterUnits = 'perf_clusterUnits';
  31. private _nClusterType = 'Invisible';
  32.  
  33. // player variable names
  34. if (_players) then {
  35.     _namespace = missionNamespace;
  36.     _nToCheck = 'perf_pClusterToCheck';
  37.     _nClustersUsed = 'perf_pClustersUsed';
  38.     _nClusters = 'perf_pClusters';
  39.     _nObjCluster = 'perf_pObjCluster';
  40.     _nClusterUnits = 'perf_pClusterUnits';
  41.     _nClusterType = 'Name';
  42. };
  43.  
  44. private _arrayAll = _namespace getVariable _nToCheck;
  45. if (isNil '_arrayAll') then {
  46.     _arrayAll = [];
  47.     _namespace setVariable [_nToCheck,_arrayAll];
  48. };
  49.  
  50. // used clusters ( used to find the empty ones)
  51. private _usedClusters = _namespace getVariable _nClustersUsed;
  52. if (isNil '_usedClusters') then {
  53.     _usedClusters = [];
  54.     _namespace setVariable [_nClustersUsed,_usedClusters];
  55. };
  56.  
  57. // get all clusters
  58. private _clusters = _namespace getVariable _nClusters;
  59. if (isNil '_clusters') then {
  60.     _clusters = [];
  61.     _namespace setVariable [_nClusters,_clusters];
  62. };
  63.  
  64.  
  65. // Unit array is empty. Get new.
  66. if (_arrayAll isEqualTo []) then {
  67.  
  68.     call {
  69.  
  70.         // get player list
  71.         if (_players) exitWith {
  72.             _arrayAll append (allPlayers - (entities "HeadlessClient_F"));
  73.         };
  74.  
  75.         // objects / units to cluster
  76.         private _arrayB = _namespace getVariable ['perf_arrayB',[]];
  77.         private _array = _namespace getVariable ['perf_array',[]];
  78.         _arrayAll append _array;
  79.         _arrayAll append _arrayB;
  80.  
  81.     };
  82.  
  83.     // remove empty clusters
  84.     private _emptyClusters = _clusters - _usedClusters;
  85.     _clusters = _clusters - _emptyClusters;
  86.  
  87.     private _nil = {
  88.         deleteLocation _x;
  89.         false
  90.     } count _emptyClusters;
  91.  
  92.     _usedClusters = [];
  93.     _namespace setVariable [_nClustersUsed,_usedClusters];
  94.     _namespace setVariable [_nClusters,_clusters];
  95.  
  96. };
  97.  
  98.  
  99. call {
  100.     if (_arrayAll isEqualTo []) exitWith {true};
  101.  
  102.     // get first cluster
  103.     private _target = _arrayAll deleteAt 0;
  104.     private _clusterPos = getpos _target;
  105.  
  106.     // size of clusters
  107.     private _distance = 250;
  108.     if (_players) then {
  109.         _distance = 50;
  110.     };
  111.  
  112.  
  113.     private _location = (nearestLocations [_clusterPos, [_nClusterType],_distance,_clusterPos]) param [0,nil];
  114.  
  115.     call {
  116.         // if no location, create a new one
  117.         if (isNil '_location') exitWith {
  118.  
  119.             private _clusterUnits = [];
  120.  
  121.  
  122.             // create location
  123.             _location = createLocation [_nClusterType,_clusterPos,_distance,_distance];
  124.             _location setVariable ['perf_cluster',true];
  125.  
  126.             _location setVariable [_nClusterUnits,_clusterUnits];
  127.  
  128.             // check for other units near this 1
  129.             private _near = _arrayAll inAreaArray [_clusterPos,_distance,_distance,0,false];
  130.  
  131.             // add current target to near units so it will also be removed from old cluster and added to the new one
  132.             _near pushBack _target;
  133.  
  134.             // if there are other things near, update their cluster to this 1
  135.             if !(_near isEqualTo []) then {
  136.  
  137.                 // add near units to cluster and update their current cluster variable
  138.                 private _nil = {
  139.                     _clusterUnits pushBack _x;
  140.                     private _oldCluster = _x getVariable _nObjCluster;
  141.                     if (!isNil '_oldCluster') then {
  142.                         private _oldClusterUnits = _oldCluster getVariable _nClusterUnits;
  143.                         if !(isNil '_oldClusterUnits') then {
  144.                             private _deleted = _oldClusterUnits deleteAt (_oldClusterUnits find _x);
  145.                         };
  146.                     };
  147.                     _x setVariable [_nObjCluster,_location];
  148.                     _arrayAll deleteAt (_arrayAll find _x);
  149.                     false
  150.                 } count _near;
  151.             };
  152.  
  153.             _clusters pushBack _location;
  154.             //_target setVariable ['perf_objCluster',_location];
  155.  
  156.         };
  157.  
  158.  
  159.         // we had a location, so we update the current one
  160.         call {
  161.             // Get units current cluster
  162.             private _oldCluster = _target getVariable [_nObjCluster,objNull];
  163.  
  164.             // if the old one is the same as the new, exit
  165.             if (_oldCluster isEqualTo _location) exitWith {
  166.             };
  167.  
  168.  
  169.             // cluster is not the same, so remove unit from old cluster
  170.             private _oldClusterUnits = _oldCluster getVariable _nClusterUnits;
  171.  
  172.             // if oldclusterunits exists..
  173.             if !(isNil '_oldClusterUnits') then {
  174.  
  175.                 private _deleted = _oldClusterUnits deleteAt (_oldClusterUnits find _target);
  176.             };
  177.  
  178.             // get the new cluster units
  179.             private _clusterUnits = _location getVariable _nClusterUnits;
  180.  
  181.             if (isNil '_clusterUnits') then {
  182.                 _clusterUnits = [];
  183.                 _location setVariable [_nClusterUnits,_clusterUnits];
  184.             };
  185.  
  186.             // add to this cluster units
  187.             _clusterUnits pushBack _target;
  188.             _target setVariable [_nObjCluster,_location];
  189.  
  190.         };
  191.     };
  192.     _usedClusters pushBackUnique _location;
  193. };
  194.  
  195.  
  196. _namespace setVariable [_nClustersUsed,_usedClusters];
  197. _clusters
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement