Advertisement
Guest User

SAR_functions.sqf (error)

a guest
Mar 26th, 2015
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.00 KB | None | 0 0
  1. // =========================================================================================================
  2. // SAR_AI - DayZ AI library
  3. // Version: 1.1.0
  4. // Author: Sarge (sarge@krumeich.ch)
  5. //
  6. // Wiki: to come
  7. // Forum: http://opendayz.net/index.php?threads/sarge-ai-framework-public-release.8391/
  8. //
  9. // ---------------------------------------------------------------------------------------------------------
  10. // Required:
  11. // UPSMon
  12. // SHK_pos
  13. //
  14. // ---------------------------------------------------------------------------------------------------------
  15. // Sar_functions - generic functions library
  16. // last modified: 1.4.2013
  17. // ---------------------------------------------------------------------------------------------------------
  18. SAR_circle = {
  19. //
  20. //
  21. //
  22.  
  23.  
  24. private ["_center","_defend","_veh","_angle","_dir","_newpos","_forEachIndex","_leader","_action","_grp","_leadername","_pos","_units","_count"];
  25.  
  26. _leader = _this select 0;
  27. _action = _this select 1;
  28.  
  29. _grp = group _leader;
  30. _defend = false;
  31.  
  32. _leadername = _leader getVariable ["SAR_leader_name",false];
  33.  
  34. // suspend UPSMON
  35. call compile format ["KRON_UPS_%1=2",_leadername];
  36.  
  37. _pos = getposASL _leader;
  38. //diag_log _pos;
  39.  
  40. _pos = (_leader) modelToWorld[0,0,0];
  41. //diag_log _pos;
  42.  
  43. if(_action == "defend") then {
  44. _center = _leader;
  45. _defend = true;
  46. };
  47.  
  48. if(_action == "campfire") then {
  49. _veh = createvehicle["Land_Campfire_burning",_pos,[],0,"NONE"];
  50. _center = _veh;
  51. };
  52.  
  53.  
  54. _units = units _grp;
  55. _count = count _units;
  56.  
  57. if(_defend) then {
  58. _angle = 360/(_count-1);
  59. }else{
  60. _angle = 360/(_count);
  61. };
  62.  
  63. SAR_circle_radius = 10;
  64.  
  65. {
  66. if(_x != _leader || {_x == _leader && !_defend}) then {
  67.  
  68. _newpos = (_center modelToWorld [(sin (_forEachIndex * _angle))*SAR_circle_radius, (cos (_forEachIndex *_angle))*SAR_circle_radius, 0]);
  69.  
  70. // diag_log format["Newpos %1: %2",_foreachindex,_newpos];
  71.  
  72. _x moveTo _newpos;
  73. _x doMove _newpos;
  74. while{(_newpos distance (getpos _x)) > 1} do {};
  75.  
  76. //_x doWatch (_veh modelToWorld [(sin (_foreachindex * _angle))*SAR_sit_radius, (cos (_foreachindex * _angle))*SAR_sit_radius, 0]);
  77. //_x doWatch _veh;
  78.  
  79. if(_defend) then {
  80. _dir = 0;
  81. }else{
  82. _dir = 180;
  83. };
  84.  
  85. _x setDir ((_foreachIndex * _angle)+ _dir);
  86.  
  87. if(!_defend) then {
  88. _x playActionNow "SitDown";
  89. sleep 1;
  90. };
  91.  
  92. _x disableAI "MOVE";
  93.  
  94. };
  95.  
  96. } foreach _units;
  97.  
  98. // wait for medic animation to end
  99. if(_defend) then {
  100. sleep 10;
  101. };
  102. // wait for random campfire time
  103. if(_action =="campfire") then {
  104. sleep 60;
  105. //cleanup campfire
  106. deletevehicle _veh;
  107. };
  108.  
  109. {
  110. _x enableAI "MOVE";
  111. } foreach _units;
  112.  
  113. // resume UPSMON
  114. call compile format ["KRON_UPS_%1=1",_leadername];
  115.  
  116. };
  117.  
  118. SAR_fnc_selectRandom = {
  119.  
  120. private ["_ret"];
  121.  
  122. _ret = count _this; //number of elements in the array
  123. _ret = floor (random _ret); //floor it first
  124. _ret = _this select _ret; //get the element, return it
  125. _ret;
  126.  
  127. };
  128.  
  129. SAR_isKindOf_weapon = {
  130. //
  131. // own function because the BiS one does only search vehicle config
  132. //
  133. //
  134. // parameters:
  135. // _weapon = the weapon for which we search the parent class
  136. // _class = class to search for
  137. //
  138. // return value: true if found, otherwise false
  139. //
  140.  
  141. private ["_class","_weapon","_cfg_entry","_found","_search_class"];
  142.  
  143. _weapon = _this select 0;
  144. _class = _this select 1;
  145.  
  146. _cfg_entry = configFile >> "CfgWeapons" >> _weapon;
  147. _search_class = configFile >> "CfgWeapons" >> _class;
  148.  
  149. _found = false;
  150. while {isClass _cfg_entry} do
  151. {
  152. if (_cfg_entry == _search_class) exitWith { _found = true; };
  153.  
  154. _cfg_entry = inheritsFrom _cfg_entry;
  155. };
  156.  
  157. _found;
  158.  
  159. };
  160.  
  161.  
  162. SAR_AI_veh_trig_on_static = {
  163. //
  164. //
  165. //
  166.  
  167. private ["_unit_list","_unitlist","_trigger","_triggername","_player_joined","_player_left","_trig_unitlist","_units_leaving","_player_rating","_clientmachine","_trigger_activator","_forEachIndex"];
  168.  
  169. if(!isServer) exitWith {};
  170.  
  171. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ----------------------------------Trigger activated, Script started ...------------------------------------------------------- > ";};
  172.  
  173. _unit_list = _this select 0;
  174. _trigger = _this select 1;
  175. _triggername = _this select 2;
  176. _unitlist=[];
  177. _trigger_activator = "";
  178.  
  179. if(SAR_EXTREME_DEBUG) then {
  180. {
  181. if(isPlayer _x) then {
  182. _trigger_activator = _unit_list select _forEachIndex;
  183. };
  184. } foreach _unit_list;
  185.  
  186. diag_log format["SAR_EXTREME_DEBUG: Trigger -> %1 at %2 was activated by %3!",_triggername,getpos _trigger,_trigger_activator];
  187. };
  188.  
  189.  
  190. // remove non players from the trigger unitlist
  191. {
  192. if (isPlayer _x) then {
  193. _unitlist set[count _unitlist,_x];
  194. };
  195. } foreach _unit_list;
  196.  
  197. if(SAR_EXTREME_DEBUG) then {[_unitlist] call SAR_debug_array;};
  198.  
  199. // get the units stored in the trigger variable
  200. _trig_unitlist = _trigger getVariable["unitlist",[]];
  201.  
  202.  
  203. // check if a unit left or joined the trigger
  204. // joined
  205. if(count _unitlist > count _trig_unitlist) then {
  206.  
  207. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger entry start ---------------------------------------------------------------- >";};
  208.  
  209. //figure out the player that joined
  210. _player_joined = _unitlist select ((count _unitlist) -1);
  211.  
  212. if(SAR_EXTREME_DEBUG) then {diag_log format["SAR_EXTREME_DEBUG: Trigger DEBUG: Unit joined, name of joining unit is: %1",_player_joined];};
  213.  
  214. // if player has negative addrating, store it on player and set to 0
  215.  
  216. _player_rating = rating _player_joined;
  217.  
  218.  
  219. if (_player_rating < 0) then {
  220.  
  221. // store old rating on the player
  222. _player_joined setVariable ["SAR_rating",_player_rating,true];
  223.  
  224. //define global variable
  225. adjustrating = [_player_joined,(0 - _player_rating)];
  226.  
  227. // get the players machine ID
  228. _clientmachine = owner _player_joined;
  229.  
  230. // transmit the global variable to this client machine
  231. _clientmachine publicVariableClient "adjustrating";
  232.  
  233. };
  234.  
  235. // add joining player to the trigger list
  236. _trig_unitlist set [count _trig_unitlist, _player_joined];
  237. _trigger setVariable ["unitlist",_trig_unitlist,true];
  238.  
  239. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger entry stop ---------------------------------------------------------------- >";};
  240.  
  241. } else { // a player left the trigger area
  242.  
  243. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger exit start ---------------------------------------------------------------- >";};
  244.  
  245. // figure out which unit left by comparing _unitlist with _trig_unitlist
  246. _units_leaving = _trig_unitlist - _unitlist;
  247.  
  248. _player_left = _units_leaving select 0;
  249.  
  250. if(SAR_EXTREME_DEBUG) then {diag_log format["SAR_EXTREME_DEBUG: Trigger DEBUG: Unit left, name of leaving unit is: %1",_player_left];};
  251.  
  252. // remove the leaving unit from the trigger list by overwriting it with the real triggerlist contents
  253. if (count _unitlist == 0) then {
  254. _trigger setVariable ["unitlist",[],true];
  255. } else {
  256. _trigger setVariable ["unitlist",_unitlist,true];
  257. };
  258.  
  259. // restore unit rating
  260.  
  261. // get old rating from the player
  262. _player_rating = _player_left getVariable ["SAR_rating",0];
  263.  
  264. //define global variable
  265. adjustrating = [_player_left,(0 + _player_rating)];
  266.  
  267. // get the players machine ID
  268. _clientmachine = owner _player_left;
  269.  
  270. // transmit the global variable to this client machine
  271. _clientmachine publicVariableClient "adjustrating";
  272.  
  273. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger exit stop ---------------------------------------------------------------- >";};
  274. };
  275.  
  276.  
  277. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: --------------------------------Trigger activated, Script finished----------------------------------------------------- > ";};
  278.  
  279. };
  280. SAR_AI_veh_trig_on_static_backup = {
  281. //
  282. //
  283. //
  284.  
  285. private ["_unit_list","_unitlist","_trigger","_triggername","_player_joined","_player_left","_trig_unitlist","_units_leaving","_player_rating","_player_humanity","_bandits_in_trigger","_player_orig_group","_clientmachine","_trigger_activator","_forEachIndex","_dummy"];
  286.  
  287. if(!isServer) exitWith {};
  288.  
  289. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ----------------------------------Trigger activated, Script started ...------------------------------------------------------- > ";};
  290.  
  291. _unit_list = _this select 0;
  292. _trigger = _this select 1;
  293. _triggername = _this select 2;
  294. _unitlist=[];
  295. _trigger_activator = "";
  296.  
  297. if(SAR_EXTREME_DEBUG) then {
  298. {
  299. if(isPlayer _x) then {
  300. _trigger_activator = _unit_list select _forEachIndex;
  301. };
  302. } foreach _unit_list;
  303.  
  304. diag_log format["SAR_EXTREME_DEBUG: Trigger -> %1 at %2 was activated by %3!",_triggername,getpos _trigger,_trigger_activator];
  305. //diag_log format["SAR_EXTREME_DEBUG: count thislist = %1, count getvariable unitlist = %2",{(isPlayer _x) && (vehicle _x == _x) } count _unit_list, count (_trigger getVariable['unitlist',[]])];
  306. };
  307.  
  308.  
  309. // remove non players from the trigger unitlist
  310. {
  311. if (isPlayer _x) then {
  312. _unitlist set[count _unitlist,_x];
  313. };
  314. } foreach _unit_list;
  315.  
  316. if(SAR_EXTREME_DEBUG) then {[_unitlist] call SAR_debug_array;};
  317.  
  318. // get the units stored in the trigger variable
  319. _trig_unitlist = _trigger getVariable["unitlist",[]];
  320.  
  321.  
  322. // check if a unit left or joined the trigger
  323. // joined
  324. if(count _unitlist > count _trig_unitlist) then {
  325.  
  326. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger entry start ---------------------------------------------------------------- >";};
  327.  
  328. //figure out the player that joined
  329. _player_joined = _unitlist select ((count _unitlist) -1);
  330.  
  331. if(SAR_EXTREME_DEBUG) then {diag_log format["SAR_EXTREME_DEBUG: Trigger DEBUG: Unit joined, name of joining unit is: %1",_player_joined];};
  332.  
  333. // if player has negative addrating, store it on player and set to 0
  334.  
  335. _player_rating = rating _player_joined;
  336.  
  337.  
  338. if (_player_rating < 0) then {
  339.  
  340. // store old rating on the player
  341. _player_joined setVariable ["SAR_rating",_player_rating,true];
  342.  
  343. //define global variable
  344. adjustrating = [_player_joined,(0 - _player_rating)];
  345.  
  346. // get the players machine ID
  347. _clientmachine = owner _player_joined;
  348.  
  349. // transmit the global variable to this client machine
  350. _clientmachine publicVariableClient "adjustrating";
  351.  
  352. };
  353.  
  354. // save and protect the old groupdata
  355. // save old group to player
  356. _player_joined setVariable ["SAR_player_group",group _player_joined,true];
  357.  
  358. // add a dummy unit into the group to keep it alive
  359. _dummy = (group _player_joined) createunit ["Rocket_DZ", [2500, 13100, 0], [],0, "FORM"];
  360.  
  361. [nil, _dummy, "per", rhideObject, true] call RE;
  362. [nil, _dummy, "per", rallowDamage, false] call RE;
  363. _dummy disableAI "FSM";
  364. _dummy disableAI "ANIM";
  365. _dummy disableAI "MOVE";
  366. _dummy disableAI "TARGET";
  367. _dummy disableAI "AUTOTARGET";
  368. _dummy setVehicleInit "this setIdentity 'id_SAR';this hideObject true;this allowDamage false;";
  369. [_dummy] joinSilent (group _player_joined);
  370.  
  371. diag_log "Joined a dummy unit to the original player group";
  372.  
  373.  
  374. // set variable to group so it doesnt get cleaned up
  375. (group _player_joined) setVariable ["SAR_protect",true,true];
  376.  
  377. // if bandit (humanity check) then switch the activating player and everyone in/close to the vehicle to resistance
  378. _player_humanity = _player_joined getVariable ["humanity",0];
  379.  
  380. if (_player_humanity < SAR_HUMANITY_HOSTILE_LIMIT) then { // player is a bandit
  381.  
  382. if(count _trig_unitlist > 0) then{ // there are already units in the trigger
  383.  
  384. _bandits_in_trigger = [_trig_unitlist] call SAR_AI_is_unfriendly_group;
  385.  
  386. if (!_bandits_in_trigger) then { // there are no bandits in the trigger list yet, create the group and add all units in the trigger list to the global resistance group
  387.  
  388. // join all units in the trigger list to that group
  389.  
  390. _trig_unitlist joinSilent SAR_grp_unfriendly;
  391.  
  392. if(SAR_EXTREME_DEBUG) then {diag_log "|||||||||||| A bandit joining created an unfriendly group and switched survivors in the trigger to the global unfriendly group!";};
  393.  
  394. };
  395.  
  396. // join player to global resistance group
  397. [_player_joined] joinSilent SAR_grp_unfriendly;
  398.  
  399. } else { // bandit player is the first at the vehicle
  400.  
  401. diag_log format["Tried to join %1 to unfriendly group: %2",_player_joined,SAR_grp_unfriendly];
  402. // join player to global resistance group
  403. [_player_joined] joinSilent grpNull;
  404. sleep .5;
  405. [_player_joined] joinSilent SAR_grp_unfriendly;
  406. sleep .5;
  407. diag_log format["Player is now %1, part of group: %2",_player_joined,group _player_joined];
  408.  
  409.  
  410. };
  411.  
  412. } else { // player is a survivor
  413.  
  414. if(count _trig_unitlist > 1) then{ // there are already units in the trigger
  415.  
  416. _bandits_in_trigger = [_trig_unitlist] call SAR_AI_is_unfriendly_group;
  417.  
  418. if (_bandits_in_trigger) then { // there are bandits in the trigger list, add survivor to the global resistance group
  419.  
  420. // join player to global unfriendly group
  421. [_player_joined] joinSilent SAR_grp_unfriendly;
  422.  
  423. } else { // the existing group is a survivor group
  424.  
  425. // join player to global friendly group
  426. [_player_joined] joinSilent SAR_grp_friendly;
  427.  
  428. };
  429. } else { // the survivor is the first at the vehicle
  430.  
  431. // join player to global friendly group
  432. [_player_joined] joinSilent SAR_grp_friendly;
  433. };
  434. };
  435.  
  436. // add joining player to the trigger list
  437. _trig_unitlist set [count _trig_unitlist, _player_joined];
  438. _trigger setVariable ["unitlist",_trig_unitlist,true];
  439.  
  440. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger entry stop ---------------------------------------------------------------- >";};
  441.  
  442. } else { // a player left the trigger area
  443.  
  444. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger exit start ---------------------------------------------------------------- >";};
  445.  
  446. // figure out which unit left by comparing _unitlist with _trig_unitlist
  447. _units_leaving = _trig_unitlist - _unitlist;
  448.  
  449. _player_left = _units_leaving select 0;
  450.  
  451. if(SAR_EXTREME_DEBUG) then {diag_log format["SAR_EXTREME_DEBUG: Trigger DEBUG: Unit left, name of leaving unit is: %1",_player_left];};
  452.  
  453. // remove the leaving unit from the trigger list by overwriting it with the real triggerlist contents
  454. if (count _unitlist == 0) then {
  455. _trigger setVariable ["unitlist",[],true];
  456. } else {
  457. _trigger setVariable ["unitlist",_unitlist,true];
  458. };
  459.  
  460. // if bandit, need to recheck the trigger group if still bandits in there, if not, remove all units from the resistance group
  461.  
  462. _player_humanity = _player_left getVariable ["humanity",0];
  463.  
  464. // restore old group of leaving player
  465. // load old group from player
  466. _player_orig_group = _player_left getVariable "SAR_player_group";
  467.  
  468. // join exiting player to his old group
  469. [_player_left] joinSilent _player_orig_group;
  470.  
  471. // remove dummy unit fom group
  472. {
  473. if !(isPlayer _x) then {
  474. deletevehicle _x;
  475. }
  476. } foreach units _player_orig_group;
  477.  
  478. // remove anti cleanup variable from player group
  479. _player_orig_group setVariable ["SAR_protect",nil,true];
  480.  
  481.  
  482. if (_player_humanity < SAR_HUMANITY_HOSTILE_LIMIT) then { // player is a bandit
  483.  
  484. if(count _unitlist > 0) then{ // there are still units in the trigger
  485.  
  486. // check if there are no bandits anymore in the trigger list
  487. _bandits_in_trigger = [_unitlist] call SAR_AI_is_unfriendly_group;
  488.  
  489. if (!_bandits_in_trigger) then { // no more bandits in the group, remove all of the remaining units from the resistance group and delete the group
  490.  
  491. // move all group members to the global friendly group
  492. _unitlist joinSilent SAR_grp_friendly;
  493.  
  494. diag_log "|||||||||||| A leaving bandit switched a trigger unfriendly group to friendly status";
  495.  
  496. };
  497. };
  498. };
  499.  
  500. // restore unit rating
  501.  
  502. // get old rating from the player
  503. _player_rating = _player_left getVariable ["SAR_rating",0];
  504.  
  505. //define global variable
  506. adjustrating = [_player_left,(0 + _player_rating)];
  507.  
  508. // get the players machine ID
  509. _clientmachine = owner _player_left;
  510.  
  511. // transmit the global variable to this client machine
  512. _clientmachine publicVariableClient "adjustrating";
  513.  
  514. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: ------------------------------------ Trigger exit stop ---------------------------------------------------------------- >";};
  515. };
  516.  
  517.  
  518. if(SAR_EXTREME_DEBUG) then {diag_log "SAR_EXTREME_DEBUG: --------------------------------Trigger activated, Script finished----------------------------------------------------- > ";};
  519.  
  520. };
  521.  
  522. SAR_AI_veh_trig_off = {
  523. };
  524.  
  525. SAR_AI_is_unfriendly_group = {
  526. // parameters
  527. //
  528. // _trig_player_list = list of players in the trigger array
  529.  
  530. private ["_trig_player_list","_bandits_in_trigger","_player_humanity"];
  531.  
  532. _trig_player_list = _this select 0;
  533.  
  534. _bandits_in_trigger = false;
  535. {
  536. _player_humanity = _x getVariable ["humanity",0];
  537.  
  538. if(_player_humanity < SAR_HUMANITY_HOSTILE_LIMIT) then {
  539. _bandits_in_trigger = true;
  540. };
  541. } foreach _trig_player_list;
  542.  
  543. _bandits_in_trigger;
  544. };
  545.  
  546. SAR_debug_array = {
  547.  
  548. private ["_array","_foreachIndex"];
  549.  
  550. _array = _this select 0;
  551.  
  552. diag_log " ";
  553. diag_log "SAR_DEBUG: Array contents ---------- start -----------";
  554. diag_log " ";
  555.  
  556. {
  557.  
  558. diag_log format[" %1. entry: %2",_foreachIndex,_x];
  559.  
  560. } foreach _array;
  561.  
  562. diag_log " ";
  563. diag_log "SAR_DEBUG: Array contents ----------- end ----------";
  564. diag_log " ";
  565. };
  566.  
  567. SAR_log = {
  568.  
  569. private ["_loglevel","_values","_descs","_logstring","_resultstring","_forEachIndex","_percstring","_finalstring","_resultstring"];
  570.  
  571. _loglevel = _this select 0;
  572. _descs = _this select 1;
  573. _values = _this select 2;
  574.  
  575.  
  576. switch (_loglevel) do {
  577.  
  578. case 0:
  579. {
  580. _logstring = "SAR_DEBUG: ";
  581. };
  582. case 1:
  583. {
  584. _logstring = "SAR_EXTREME_DEBUG: ";
  585. };
  586. };
  587.  
  588. {
  589. _logstring = _logstring + _descs select _forEachIndex;
  590.  
  591. if(_forEachIndex < (count _values) - 1) then {_logstring = _logstring + "|";};
  592.  
  593. _resultstring = _resultstring + _values select _forEachIndex;
  594.  
  595. _percstring = _percstring + "%" + str(_forEachIndex + 1) + " ";
  596.  
  597. if(_forEachIndex < (count _values) - 1) then {_resultstring = _resultstring + ",";};
  598.  
  599. } foreach _values;
  600.  
  601. _finalstring = "diag_log format[" + _logstring + _percstring +"," + _resultstring + "];";
  602.  
  603. Call Compile Format ["%1",_finalstring];
  604. };
  605.  
  606.  
  607. KRON_StrToArray = {
  608. private ["_in","_arr","_out"];
  609. _in=_this select 0;
  610. _arr = toArray(_in);
  611. _out=[];
  612. for "_i" from 0 to (count _arr)-1 do {
  613. _out=_out+[toString([_arr select _i])];
  614. };
  615. _out
  616. };
  617.  
  618. KRON_StrLeft = {
  619. private["_in","_len","_arr","_out"];
  620. _in=_this select 0;
  621. _len=(_this select 1)-1;
  622. _arr=[_in] call KRON_StrToArray;
  623. _out="";
  624. if (_len>=(count _arr)) then {
  625. _out=_in;
  626. } else {
  627. for "_i" from 0 to _len do {
  628. _out=_out + (_arr select _i);
  629. };
  630. };
  631. _out
  632. };
  633.  
  634.  
  635. SAR_unit_loadout_tools = {
  636. // Parameters:
  637. // _unittype (leader, soldier, sniper)
  638. //
  639. // return value: tools array
  640. //
  641.  
  642. private ["_unittype","_unit_tools_list","_unit_tools","_tool","_probability","_chance"];
  643.  
  644. _unittype = _this select 0;
  645.  
  646. switch (_unittype) do {
  647.  
  648. case "leader" :
  649. {
  650. _unit_tools_list = SAR_leader_tools;
  651. };
  652. case "soldier" :
  653. {
  654. _unit_tools_list = SAR_rifleman_tools;
  655. };
  656. case "sniper" :
  657. {
  658. _unit_tools_list = SAR_sniper_tools;
  659. };
  660.  
  661. };
  662.  
  663. _unit_tools = [];
  664. {
  665. _tool = _x select 0;
  666. _probability = _x select 1;
  667. _chance = (random 100);
  668. if(_chance < _probability) then {
  669. _unit_tools set [count _unit_tools, _tool];
  670. };
  671. } foreach _unit_tools_list;
  672.  
  673. _unit_tools;
  674.  
  675. };
  676.  
  677. SAR_unit_loadout_items = {
  678. // Parameters:
  679. // _unittype (leader, soldier, sniper)
  680. //
  681. // return value: items array
  682. //
  683.  
  684. private ["_unittype","_unit_items_list","_unit_items","_item","_probability","_chance"];
  685.  
  686. _unittype = _this select 0;
  687.  
  688. switch (_unittype) do {
  689.  
  690. case "leader" :
  691. {
  692. _unit_items_list = SAR_leader_items;
  693. };
  694. case "soldier" :
  695. {
  696. _unit_items_list = SAR_rifleman_items;
  697. };
  698. case "sniper" :
  699. {
  700. _unit_items_list = SAR_sniper_items;
  701. };
  702.  
  703. };
  704.  
  705. _unit_items = [];
  706. {
  707. _item = _x select 0;
  708. _probability = _x select 1;
  709. _chance = (random 100);
  710. if(_chance < _probability) then {
  711. _unit_items set [count _unit_items, _item];
  712. };
  713. } foreach _unit_items_list;
  714.  
  715. _unit_items;
  716.  
  717. };
  718. SAR_unit_loadout_weapons = {
  719. // Parameters:
  720. // _unittype (leader, soldier, sniper)
  721. //
  722. // return value: weapons array
  723. //
  724.  
  725. private ["_unittype","_unit_weapon_list","_unit_pistol_list","_unit_pistol_name","_unit_weapon_name","_unit_weapon_names"];
  726.  
  727. _unittype = _this select 0;
  728.  
  729. switch (_unittype) do {
  730.  
  731. case "leader" :
  732. {
  733. _unit_weapon_list = SAR_leader_weapon_list;
  734. _unit_pistol_list = SAR_leader_pistol_list;
  735. };
  736. case "soldier" :
  737. {
  738. _unit_weapon_list = SAR_rifleman_weapon_list;
  739. _unit_pistol_list = SAR_rifleman_pistol_list;
  740. };
  741. case "sniper" :
  742. {
  743. _unit_weapon_list = SAR_sniper_weapon_list;
  744. _unit_pistol_list = SAR_sniper_pistol_list;
  745. };
  746.  
  747. };
  748.  
  749. // TODO: changed, test
  750.  
  751. _unit_weapon_names = [];
  752. _unit_weapon_name = _unit_weapon_list select (floor(random (count _unit_weapon_list)));
  753. _unit_pistol_name = _unit_pistol_list select (floor(random (count _unit_pistol_list)));
  754. _unit_weapon_names set [0, _unit_weapon_name];
  755. _unit_weapon_names set [1, _unit_pistol_name]; <<<----------------E_R_R_O_R----------------
  756.  
  757. _unit_weapon_names;
  758.  
  759. };
  760.  
  761. SAR_unit_loadout = {
  762. // Parameters:
  763. // _unit (Unit to apply the loadout to)
  764. // _weapons (array with weapons for the loadout)
  765. // _items (array with items for the loadout)
  766. // _tools (array with tools for the loadout)
  767.  
  768. private ["_unit","_weapons","_weapon","_items","_unit_magazine_name","_item","_tool","_tools","_forEachIndex"];
  769.  
  770. _unit = _this select 0;
  771. _weapons = _this select 1;
  772. _items = _this select 2;
  773. _tools = _this select 3;
  774.  
  775. removeAllWeapons _unit;
  776.  
  777. // the above doesnt remove the tools, need this as well
  778. _unit removeweapon "ItemMap";
  779. _unit removeweapon "ItemCompass";
  780. _unit removeweapon "ItemRadio";
  781.  
  782. {
  783. _weapon = _weapons select _forEachIndex;
  784.  
  785. if (_weapon !="") then <<<----------------E_R_R_O_R----------------
  786. {
  787. _unit_magazine_name = getArray (configFile >> "CfgWeapons" >> _weapon >> "magazines") select 0;
  788. _unit addMagazine _unit_magazine_name;
  789. _unit addWeapon _weapon;
  790. };
  791.  
  792. } foreach _weapons;
  793.  
  794. {
  795. _item = _items select _forEachIndex;
  796. _unit addMagazine _item;
  797. } foreach _items;
  798.  
  799. {
  800. _tool = _tools select _forEachIndex;
  801. _unit addWeapon _tool;
  802. } foreach _tools;
  803.  
  804. };
  805.  
  806. SAR_AI_mon_upd = {
  807.  
  808. // Parameters:
  809. // _typearray (possible values = "max_grps", "rnd_grps", "max_p_grp", "grps_band","grps_sold","grps_surv")
  810. // _valuearray (must be an array)
  811. // _gridname (is the areaname of the grid for this change)
  812.  
  813. private ["_typearray","_valuearray","_gridname","_path","_success","_forEachIndex"];
  814.  
  815. _typearray = _this select 0;
  816. _valuearray =_this select 1;
  817. _gridname = _this select 2;
  818.  
  819. _path = [SAR_AI_monitor, _gridname] call BIS_fnc_findNestedElement;
  820.  
  821. {
  822.  
  823. switch (_x) do
  824. {
  825. case "max_grps":
  826. {
  827. _path set [1,1];
  828. };
  829. case "rnd_grps":
  830. {
  831. _path set [1,2];
  832. };
  833. case "max_p_grp":
  834. {
  835. _path set [1,3];
  836. };
  837. case "grps_band":
  838. {
  839. _path set [1,4];
  840. };
  841. case "grps_sold":
  842. {
  843. _path set [1,5];
  844. };
  845. case "grps_surv":
  846. {
  847. _path set [1,6];
  848. };
  849.  
  850. };
  851.  
  852. _success = [SAR_AI_monitor, _path, _valuearray select _forEachIndex] call BIS_fnc_setNestedElement;
  853.  
  854. }foreach _typearray;
  855.  
  856. _success;
  857.  
  858.  
  859. };
  860. SAR_AI_mon_read = {
  861.  
  862. // Parameters:
  863. // _typearray (possible values = "max_grps", "rnd_grps", "max_p_grp", "grps_band","grps_sold","grps_surv")
  864. // _gridname (is the areaname of the grid for this change)
  865.  
  866. private ["_typearray","_gridname","_path","_resultarray"];
  867.  
  868. _typearray = _this select 0;
  869. _gridname = _this select 1;
  870. _resultarray=[];
  871.  
  872. _path = [SAR_AI_monitor, _gridname] call BIS_fnc_findNestedElement;
  873.  
  874. {
  875.  
  876. switch (_x) do
  877. {
  878. case "max_grps":
  879. {
  880. _path set [1,1];
  881. };
  882. case "rnd_grps":
  883. {
  884. _path set [1,2];
  885. };
  886. case "max_p_grp":
  887. {
  888. _path set [1,3];
  889. };
  890. case "grps_band":
  891. {
  892. _path set [1,4];
  893. };
  894. case "grps_sold":
  895. {
  896. _path set [1,5];
  897. };
  898. case "grps_surv":
  899. {
  900. _path set [1,6];
  901. };
  902.  
  903. };
  904.  
  905. _resultarray set[count _resultarray,[SAR_AI_monitor, _path ] call BIS_fnc_returnNestedElement];
  906.  
  907. }foreach _typearray;
  908.  
  909. _resultarray;
  910.  
  911. };
  912.  
  913. SAR_DEBUG_mon = {
  914.  
  915. diag_log "--------------------Start of AI monitor values -------------------------";
  916. {
  917. diag_log format["SAR EXTREME DEBUG: %1",_x];
  918. }foreach SAR_AI_monitor;
  919.  
  920. diag_log "--------------------End of AI monitor values -------------------------";
  921. };
  922.  
  923.  
  924. SAR_fnc_returnConfigEntry = {
  925.  
  926. private ["_config", "_entryName","_entry", "_value"];
  927.  
  928. _config = _this select 0;
  929. _entryName = _this select 1;
  930. _entry = _config >> _entryName;
  931.  
  932. //If the entry is not found and we are not yet at the config root, explore the class' parent.
  933. if (((configName (_config >> _entryName)) == "") && {!((configName _config) in ["CfgVehicles", "CfgWeapons", ""])}) then {
  934. [inheritsFrom _config, _entryName] call SAR_fnc_returnConfigEntry;
  935. }
  936. else { if (isNumber _entry) then { _value = getNumber _entry; } else { if (isText _entry) then { _value = getText _entry; }; }; };
  937. //Make sure returning 'nil' works.
  938. if (isNil "_value") exitWith {nil};
  939.  
  940. _value;
  941. };
  942.  
  943. // *WARNING* BIS FUNCTION RIPOFF - Taken from fn_fnc_returnVehicleTurrets and shortened a bit
  944. SAR_fnc_returnVehicleTurrets = {
  945.  
  946. private ["_entry","_turrets","_turretIndex"];
  947.  
  948. _entry = _this select 0;
  949. _turrets = [];
  950. _turretIndex = 0;
  951.  
  952. //Explore all turrets and sub-turrets recursively.
  953. for "_i" from 0 to ((count _entry) - 1) do {
  954. private ["_subEntry"];
  955. _subEntry = _entry select _i;
  956. if (isClass _subEntry) then {
  957. private ["_hasGunner"];
  958. _hasGunner = [_subEntry, "hasGunner"] call SAR_fnc_returnConfigEntry;
  959. //Make sure the entry was found.
  960. if (!(isNil "_hasGunner")) then {
  961. if (_hasGunner == 1) then {
  962. _turrets = _turrets + [_turretIndex];
  963. //Include sub-turrets, if present.
  964. if (isClass (_subEntry >> "Turrets")) then { _turrets = _turrets + [[_subEntry >> "Turrets"] call SAR_fnc_returnVehicleTurrets]; }
  965. else { _turrets = _turrets + [[]]; };
  966. };
  967. };
  968. _turretIndex = _turretIndex + 1;
  969. };
  970. sleep 0.01;
  971. };
  972. _turrets;
  973. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement