player2_dz

Untitled

Dec 14th, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.21 KB | None | 0 0
  1. private ["_ai","_sleepTimeMax","_sleepTimeMin","_roofAI","_centerPos"];
  2. //get input values: [AI Object, AI Type (Minor/Major/Castle), is Roof AI (True/false), Mission Center Pos Array]
  3. _ai = _this select 0;
  4. //_aiArrayName = _this select 1; //Unused for now
  5. _roofAI = _this select 2;
  6. _centerPos = _this select 3;
  7.  
  8. //just in case this hasnt been parsed in properly
  9. if (isNil '_roofAI') then {
  10.     _roofAI = false;
  11. };
  12.  
  13. //set to current ai pos if not parsed properly
  14. if (isNil '_centerPos') then {
  15.     _centerPos = getPos _ai;
  16. };
  17.  
  18. //debugging outputs
  19. private["_f","_d"]; _f = "aiThread"; _d = (P2AI_debugAI);
  20. if (_d) then { diag_log(format["[P2AI]:%1: Starting, Input: %2", _f, _this]); };
  21.  
  22. //Set up sleep times
  23. _sleepTimeMax = 6;
  24. _sleepTimeMin = 3;
  25.  
  26. //if roofai, sleep less, turn more.
  27. if (_roofAI) then {
  28.     _sleepTimeMin = 1;
  29.     _sleepTimeMax = 3;
  30. };
  31.  
  32. while {alive _ai} do
  33. {
  34.     private["_sleepTime","_turnChance","_timeSlept","_bestDist","_cDist","_bDistNum","_count","_closestPlayer"];
  35.     //generate random sleeptime to offset the ai shooting
  36.     _sleepTime = random _sleepTimeMax;
  37.     _sleepTime = _sleepTimeMin + _sleepTime;
  38.     //initialize variables
  39.     _timeSlept = 0;
  40.     _bestDist = 100000;
  41.     _cDist =    1000;
  42.     _bDistNum = 0;
  43.     _count =    0;
  44.  
  45.     //find closest player to AI
  46.     {
  47.         if (alive _x) then {
  48.             if (name _x != "www.ZombZ.net") then {
  49.  
  50.                 _cDist =  (getPosASL _ai) distance (getPosASL _x);
  51.                 if (_bestDist > _cDist) then {
  52.                     _bDistNum = _count;
  53.                     _bestDist = _cDist;
  54.                 };
  55.                 _count = _count + 1;
  56.             };
  57.         };
  58.     } count playableUnits;
  59.  
  60.     //select closest player from created bdistnum
  61.     _closestPlayer = playableUnits select _bDistNum;
  62.     //make sure real value was returned, if not set to null
  63.     if (isNil '_closestPlayer') then { _closestPlayer = objNull };
  64.     //make sure object isnt null
  65.     if (!isNull _closestPlayer) then {
  66.         //reveal enemey
  67.         _ai reveal [(vehicle _closestPlayer), 4];
  68.  
  69.         //watch enemey
  70.         _ai doWatch (vehicle _closestPlayer);
  71.  
  72.         //target enemey
  73.         _ai doTarget (vehicle _closestPlayer);
  74.  
  75.         //order units to fire when ready
  76.         _ai doFire (vehicle _closestPlayer);
  77.  
  78.         //give ai a chance to turn if not on roof
  79.         if (_roofAI) then { _turnChance = 1; } else {
  80.             _turnChance = random 1;
  81.         };
  82.  
  83.         //face towards enemey 50% chance, 100% for roof ai
  84.         if (_turnChance > 0.5) then {
  85.             private["_p1","_p2","_dx","_dy","_heading"];
  86.             _p1 = getPos _ai; //unit position
  87.             _p2 = getPos (vehicle _closestPlayer); //target position
  88.             _dx = (_p2 select 0) - (_p1 select 0);
  89.             _dy = (_p2 select 1) - (_p1 select 1);
  90.             _heading = _dx atan2 _dy;
  91.             if (_heading < 0) then {_heading = _heading + 360};
  92.             _ai setDir _heading;
  93.         };
  94.     };
  95.  
  96.     //if AI wanders too far from mision, tell em to go back
  97.     if (((getPos _ai) distance _centerPos) > 150) then {
  98.         _ai moveTo _centerPos;
  99.         _ai doMove _centerPos;
  100.     };
  101.  
  102.     //take away extra time slept by shooting at people
  103.     _sleepTime = _sleepTime - _timeSlept;
  104.     //round it up to a whole number
  105.     _sleepTime = round(_sleepTime);
  106.     //make sure if less than _sleepTimeMin then set to _sleepTimeMin
  107.     if (_sleepTime < _sleepTimeMin) then { _sleepTime = _sleepTimeMin; };
  108.     uiSleep _sleepTime;
  109. };
  110.  
  111. if (_d) then { diag_log(format["[P2AI]:%1: AI Thread Quitting", _f]); }
Advertisement
Add Comment
Please, Sign In to add comment