fn_setBehaviour.sqf 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. File: fn_setBehaviour.sqf
  3. Author: Rarek [AW]
  4. Description
  5. | Sets behaviour of the given array of groups / units according
  6. | to a set of passed parameters.
  7. |
  8. | Behaviours are currently limited to 4 "types" which can be
  9. | customised in regards to their positioning and radius.
  10. |____________________
  11. Parameters
  12. | 0 - Units / groups to be controlled
  13. | - REQUIRED
  14. | - TYPE: Array
  15. | - DEFAULT: None
  16. |
  17. | 1 - Behaviours to perform
  18. | - REQUIRED
  19. | - TYPE: Array
  20. | - DEFAULT: None
  21. |____________________
  22. Example
  23. | _enemies = ["O_soldier_F", group player, ["More", "Soldiers", "Here"]];
  24. | _behaviours = [["defend", _pos, _radius], ["patrol", _pos, _radius]];
  25. | [_enemies, _behaviours] call AW_fnc_setBehaviour;
  26. |____________________
  27. */
  28. private["_obj", "_behaviours", "_type", "_x", "_behaviour", "_order", "_pos", "_radius", "_near", "_leader", "_group", "_dir", "_retreatPos", "_waypoint"];
  29. _obj = [_this,0,[],[[],objNull,grpNull]] call BIS_fnc_param;
  30. if (typeName _obj == "OBJECT") then { _obj = [group _obj]; };
  31. if (typeName _obj == "GROUP") then { _obj = [_obj]; };
  32. _behaviours = [_this,1,[["patrol", true, 100]],[[]]] call BIS_fnc_param;
  33. {
  34. _type = typeName _x;
  35. switch (_type) do
  36. {
  37. case "ARRAY": { [_x, _behaviours] call AW_fnc_setBehaviour; };
  38. default
  39. {
  40. _behaviour = _behaviours call BIS_fnc_selectRandom;
  41. _order = [_behaviour,0,"patrol",[""]] call BIS_fnc_param;
  42. _pos = [_behaviour,1,[0,0,0],[[],"",true],[2,3]] call BIS_fnc_param;
  43. if (typeName _pos == "BOOL") then
  44. {
  45. if (_type == "GROUP") then { _pos = getPos (leader _x); };
  46. if (_type == "OBJECT") then { _pos = getPos _x; };
  47. };
  48. if (typeName _pos == "STRING") then
  49. {
  50. _pos = [_pos, ((markerSize _pos) call BIS_fnc_lowestNum)] call AW_fnc_randomPosTrigger;
  51. };
  52. _radius = [_behaviour,2,100,[0]] call BIS_fnc_param;
  53. switch (_order) do
  54. {
  55. case "defend":
  56. {
  57. _group = if (_type == "GROUP") then { _x } else { group _x };
  58. _near = [[[_pos, _radius]], ["water", "out"]] call BIS_fnc_randomPos;
  59. [_group, _near] call BIS_fnc_taskDefend;
  60. };
  61. case "active_defend":
  62. {
  63. _group = if (_type == "GROUP") then { _x } else { group _x };
  64. _near = [[[_pos, _radius]], ["water", "out"]] call BIS_fnc_randomPos;
  65. [_group, _near] call BIS_fnc_taskDefend;
  66. _x setBehaviour "AWARE";
  67. _x setSpeedMode "FULL";
  68. };
  69. case "patrol":
  70. {
  71. _group = if (_type == "GROUP") then { _x } else { group _x };
  72. _didWork = false;
  73. _attempt_limit = 10;
  74. _attempts = 0;
  75. while { !_didWork && _attempts < _attempt_limit } do
  76. {
  77. _attempts = _attempts + 1;
  78. _didWork = [_group, _pos, _radius] call BIS_fnc_taskPatrol;
  79. diag_log format["::::AW:::: _x = %1 :::: _pos = %2 :::: _radius = %3 :::: _didWork = %4", _x, _pos, _radius, _didWork];
  80. };
  81. if (!_didWork) then
  82. {
  83. _x setFormation "DIAMOND";
  84. };
  85. };
  86. case "attack": { [_x, _pos] call BIS_fnc_taskAttack; };
  87. case "retreat":
  88. {
  89. _leader = if (_type == "GROUP") then { leader _x } else { _x };
  90. _group = if (_type == "GROUP") then { _x } else { group _x };
  91. _dir = [_pos, getPos _leader] call BIS_fnc_dirTo;
  92. _retreatPos = [];
  93. while {true} do
  94. {
  95. _retreatPos = [_pos, _radius, _dir] call BIS_fnc_relPos;
  96. if (!(surfaceIsWater [(_retreatPos select 0), (_retreatPos select 1)])) exitWith {};
  97. _dir = if ((_dir + 45) > 359) then { 0 } else { (_dir + 45) };
  98. };
  99. _waypoint = _group addWaypoint [_retreatPos, 0];
  100. _waypoint setWaypointType "MOVE";
  101. _waypoint setWaypointBehaviour "AWARE";
  102. _waypoint setWaypointCombatMode "GREEN";
  103. _waypoint setWaypointSpeed "FULL";
  104. _group setCurrentWaypoint _waypoint;
  105. };
  106. };
  107. };
  108. };
  109. } forEach _obj;