apply_turrets.sqf 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. params[
  2. ["_vic",objNull,[player]],
  3. ["_turret_class","",["string"]],
  4. ["_position_to_attach",[0,0,0],[[]],3]
  5. ];
  6. //if cant sleep
  7. if (!canSuspend) exitWith {};
  8. //if vic null
  9. if (isNull _vic) exitWith {};
  10. //if no turret class
  11. if (_turret_class isEqualTo '') exitWith {};
  12. _disable_sim_time=60;
  13. _time_of_select=time;
  14. //First w8 until simulation is enabled,check every X seconds. If Y seconds has passed,dont apply script
  15. waitUntil {
  16. sleep 0.25;
  17. ((simulationEnabled _vic) || ((time-_time_of_select)>_disable_sim_time))
  18. };
  19. //If its been in disabled simulation for 60 seconds, dont put turrets
  20. if((time-_time_of_select)>_disable_sim_time) exitWith {};
  21. //if no crew,exit
  22. if(count(crew _vic) ==0) exitWith {};
  23. //if not the server,exit
  24. if (!isServer) exitWith {};
  25. //we put in a delay to allow vehicle to be placed
  26. sleep 2;
  27. //We check if the vehicle still exists after that X second sleep, incase zeus delets it.
  28. if(!(alive _vic)) exitWith{};
  29. //First we make the vehicle invincible to prevent damage
  30. _vic allowDamage false;
  31. //attach the specified turret to the specified location
  32. _created_attached_turret = _turret_class createVehicle (getPosASL _vic);
  33. _created_attached_turret attachTo [_vic,_position_to_attach];
  34. //we also make this invincible for a while
  35. _created_attached_turret allowDamage false;
  36. //Now we create a crew for that vehicle and hide it
  37. createVehicleCrew _created_attached_turret;
  38. _created_attached_turret hideObjectGlobal true;
  39. //Then we save that unit to the vehicle for future use when killed or deleted.
  40. _current_attached=_vic getVariable ["rd501_attached_gun_object",[]];
  41. _current_attached pushback _created_attached_turret;
  42. _vic setVariable ["rd501_attached_gun_object",_current_attached ,true];
  43. //Then we add an action to remove the turret.
  44. [_vic,
  45. [
  46. "<t color='#0000FF'>Disable Turret</t>",
  47. {
  48. params ["_vic", "_caller", "_actionId", "_arguments"];
  49. _attached_objectes=attachedObjects _vic;
  50. {
  51. detach _x;
  52. deleteVehicle _x;
  53. } forEach _attached_objectes;
  54. _vic removeAction _actionId;
  55. },
  56. [],10,true,false,"","true",5, false, "", ""
  57. ]
  58. ] remoteExec ["addAction"];
  59. //Now to handle when the vic dies, we do the same logic we did with addaction. We use MP killed cause MP but also because
  60. //This EH is clever enough to be triggered globally only once even if added on all clients or a single client that is then disconnected,
  61. //EH will still trigger globally only once.
  62. _vic addMPEventHandler ["MPKilled",
  63. {
  64. params ["_vic", "_killer", "_instigator", "_useEffects"];
  65. _attached_objectes=attachedObjects _vic;
  66. {
  67. detach _x;
  68. deleteVehicle _x;
  69. } forEach _attached_objectes;
  70. _vic setVariable ["rd501_attached_gun_object",[],true];
  71. }];
  72. //Same thing, but for single player
  73. _vic addEventHandler ["Killed",
  74. {
  75. params ["_vic", "_killer", "_instigator", "_useEffects"];
  76. _attached_objectes=attachedObjects _vic;
  77. {
  78. detach _x;
  79. deleteVehicle _x;
  80. } forEach _attached_objectes;
  81. _vic setVariable ["rd501_attached_gun_object",[],true];
  82. }];
  83. //This is here so that after X seconds the vehicle is killable again.
  84. [_vic] spawn {
  85. params["_vic_spawn"];
  86. sleep 2;
  87. //We check if the vehicle still exists after that X second sleep, incase zeus delets it.
  88. if(!(alive _vic_spawn)) exitWith{ };
  89. _vic_spawn allowDamage true;
  90. _attached_objectes=attachedObjects _vic_spawn;
  91. {
  92. _x allowDamage true;
  93. } forEach _attached_objectes;
  94. };