fn_respawnVehicle.sqf 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. if (!isServer) exitWith {};
  2. //Define parameters
  3. _unit_class = [_this,0,"",[""]] call BIS_fnc_param;
  4. _respawn_marker = [_this,1,"",[""]] call BIS_fnc_param;
  5. _base_marker = [_this,2,"",[""]] call BIS_fnc_param;
  6. _respawn_delay = [_this,3,30,[0,[]]] call BIS_fnc_param; //array not yet implemented
  7. _deserted_delay = [_this,4,300,[0,[]]] call BIS_fnc_param; //array not yet implemeneted
  8. _respawn_marker_tag = ""; //Add in a placeholder for our tag marker
  9. //Weed out the crap
  10. if (_unit_class == "" || _respawn_marker == "") exitWith {};
  11. //Define the functions we'll be using quickly
  12. _FUNC_create_unit = {
  13. //Get params given
  14. _unit_class = [_this,0,false,[""]] call BIS_fnc_param;
  15. _respawn_marker = [_this,1,"",[""]] call BIS_fnc_param;
  16. _pos = [_this,2,[0,0,0],[[]]] call BIS_fnc_param;
  17. _dir = [_this,3,0,[0]] call BIS_fnc_param;
  18. _unit_name = [_this,4,"",[""]] call BIS_fnc_param;
  19. //Create our unit!
  20. _unit = _unit_class createVehicle _pos; //Don't create via AW_fnc_createVehicle as we're handling the dead stuff here
  21. _unit setDir _dir;
  22. //Make sure they're where they should be...
  23. _unit setPos _pos;
  24. //Add AI to UAVs!
  25. if (["B_UAV", _unit_class] call BIS_fnc_inString) then
  26. {
  27. createVehicleCrew _unit;
  28. };
  29. //Set up our respawn tags (but we can't return these yet!)
  30. _respawn_marker_tag = [["veh_respawn", "at_base"], _respawn_marker, _pos, _unit_name] call AW_fnc_createJIPstateMarker;
  31. //Return the unit
  32. _unit
  33. };
  34. _FUNC_get_approx_distance = {
  35. //Get params
  36. _unit = [_this,0,objNull,[objNull]] call BIS_fnc_param;
  37. _respawn_marker = [_this,1,"",[""]] call BIS_fnc_param;
  38. //Get the position of our unit and marker
  39. _unit_pos = getPos _unit;
  40. _marker_pos = getMarkerPos _respawn_marker;
  41. //Calculate the approx distance!
  42. _distance = _unit_pos distance _marker_pos;
  43. _approx_distance = switch (true) do //probably a much more elegant way of doing this, but pressed for time!
  44. {
  45. case (_distance > 25000): { 25000 };
  46. case (_distance > 20000): { 20000 };
  47. case (_distance > 15000): { 15000 };
  48. case (_distance > 10000): { 10000 };
  49. case (_distance > 5000): { 5000 };
  50. case (_distance > 2500): { 2500 };
  51. case (_distance > 1000): { 1000 };
  52. case (_distance > 500): { 500 };
  53. case (_distance > 250): { 250 };
  54. case (_distance > 100): { 100 };
  55. default { 0 };
  56. };
  57. //If it's 0, set to <100, otherwise just >whatever
  58. if (_approx_distance == 0) then
  59. {
  60. _approx_distance = "(<100m)";
  61. }
  62. else
  63. {
  64. _approx_distance = format["(>%1m)", _approx_distance];
  65. };
  66. _approx_distance
  67. };
  68. //Let's get the marker properties so we can get ready to spawn our vehicle
  69. _pos = getMarkerPos _respawn_marker;
  70. _dir = markerDir _respawn_marker;
  71. //Get the pretty name of our vehicle
  72. _cfg = (configFile >> "CfgVehicles" >> _unit_class);
  73. _unit_name = getText (_cfg >> "DisplayName");
  74. //Create our respawning vehicle
  75. _unit = [_unit_class, _respawn_marker, _pos, _dir, _unit_name] call _FUNC_create_unit;
  76. //Start looping to check for respawns!
  77. _run = true;
  78. _dead = false;
  79. _deserted = false;
  80. while {_run} do
  81. {
  82. //Wait for it...
  83. sleep (2 + random 8);
  84. //Do we still own the base?
  85. _baseOwned = false;
  86. {
  87. if (_x == _base_marker) exitWith
  88. {
  89. _baseOwned = true;
  90. };
  91. } forEach basesOwned;
  92. //If not, delete the markers and get outta there!
  93. if (!_baseOwned) exitWith
  94. {
  95. [_respawn_marker_tag] call AW_fnc_deleteMarker;
  96. };
  97. //Are we dead?
  98. if (getDammage _unit > 0.8 && { alive _x } count (crew _unit) == 0) then
  99. {
  100. _dead = true;
  101. };
  102. if (!_dead && _deserted_delay > 0) then
  103. {
  104. //Is the vehicle abandoned?
  105. if (((getPosASL _unit) distance _pos) > 20 && ({ alive _x } count (crew _unit)) == 0 && (getDammage _unit) < 0.8) then
  106. {
  107. //Put that it's away on our tag, seeing as it's most definitely away...
  108. _approx_distance = [_unit, _respawn_marker] call _FUNC_get_approx_distance;
  109. _respawn_marker_tag = [["veh_respawn", "in_action"], _respawn_marker, _pos, format["%1 %2", _approx_distance, _unit_name]] call AW_fnc_createJIPstateMarker;
  110. //And we have been deserted!
  111. _deserted = true;
  112. };
  113. //Looks like it is! Let's wait and see...
  114. if (_deserted) then
  115. {
  116. //Temporarily set _deserted to false for later theory
  117. _deserted = false;
  118. _timeout = time + _deserted_delay;
  119. //Wait for either the timer to end, the vehicle to die or someone to get in
  120. while {_timeout >= time && alive _unit && ({ alive _x } count (crew _unit)) == 0} do
  121. {
  122. sleep 1;
  123. };
  124. //Did the timer run out?
  125. if (_timeout < time) then
  126. {
  127. _deserted = true;
  128. };
  129. //Did we die?
  130. if (!_deserted && !alive _unit) then
  131. {
  132. _dead = true;
  133. };
  134. //If someone got in the vehicle, _deserted's already set to false.
  135. };
  136. };
  137. //So... do we start respawning?
  138. if (_dead || _deserted) then
  139. {
  140. //If it's been deserted, we've already waited so let's not wait again!
  141. if (_deserted) then
  142. {
  143. sleep 0.1;
  144. }
  145. else
  146. {
  147. //Set our marker tag so everyone knows what's up
  148. _respawn_marker_tag = [["veh_respawn", "destroyed"], _respawn_marker, _pos, _unit_name] call AW_fnc_createJIPstateMarker;
  149. //Now wait for a bit before we respawn...
  150. sleep _respawn_delay;
  151. };
  152. //Courtesy sleep
  153. sleep 0.1;
  154. //Remove the dead/deserted vehicle
  155. deleteVehicle _unit;
  156. //Wait for that to process...
  157. sleep 2;
  158. //Create our new unit
  159. _unit = [_unit_class, _respawn_marker, _pos, _dir, _unit_name] call _FUNC_create_unit;
  160. //Reset our thing-bobs
  161. _dead = false;
  162. _deserted = false;
  163. }
  164. else //If we're not dead or deserted, set our marker up!
  165. {
  166. //Is the vehicle at its spawn?
  167. if ((_unit distance _pos) < 30) then
  168. {
  169. _respawn_marker_tag = [["veh_respawn", "at_base"], _respawn_marker, _pos, _unit_name] call AW_fnc_createJIPstateMarker;
  170. }
  171. else //If it ain't let's mark it as away...
  172. {
  173. _approx_distance = [_unit, _respawn_marker] call _FUNC_get_approx_distance;
  174. _respawn_marker_tag = [["veh_respawn", "in_action"], _respawn_marker, _pos, format["%1 %2", _approx_distance, _unit_name]] call AW_fnc_createJIPstateMarker;
  175. };
  176. };
  177. };