apply_turrets.sqf 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. // add turret to vehicle group
  40. [_created_attached_turret] joinSilent _vic;
  41. //Then we save that unit to the vehicle for future use when killed or deleted.
  42. _current_attached=_vic getVariable ["rd501_attached_gun_object",[]];
  43. _current_attached pushback _created_attached_turret;
  44. _vic setVariable ["rd501_attached_gun_object",_current_attached ,true];
  45. //Then we add an action to remove the turret.
  46. [_vic,
  47. [
  48. "<t color='#0000FF'>Disable Turret</t>",
  49. {
  50. params ["_vic", "_caller", "_actionId", "_arguments"];
  51. _attached_objectes=attachedObjects _vic;
  52. {
  53. detach _x;
  54. deleteVehicle _x;
  55. } forEach _attached_objectes;
  56. _vic removeAction _actionId;
  57. },
  58. [],10,true,false,"","true",5, false, "", ""
  59. ]
  60. ] remoteExec ["addAction"];
  61. //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
  62. //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,
  63. //EH will still trigger globally only once.
  64. _vic addMPEventHandler ["MPKilled",
  65. {
  66. params ["_vic", "_killer", "_instigator", "_useEffects"];
  67. _attached_objectes=attachedObjects _vic;
  68. {
  69. detach _x;
  70. deleteVehicle _x;
  71. } forEach _attached_objectes;
  72. _vic setVariable ["rd501_attached_gun_object",[],true];
  73. }];
  74. //Same thing, but for single player
  75. _vic addEventHandler ["Killed",
  76. {
  77. params ["_vic", "_killer", "_instigator", "_useEffects"];
  78. _attached_objectes=attachedObjects _vic;
  79. {
  80. detach _x;
  81. deleteVehicle _x;
  82. } forEach _attached_objectes;
  83. _vic setVariable ["rd501_attached_gun_object",[],true];
  84. }];
  85. //This is here so that after X seconds the vehicle is killable again.
  86. [_vic] spawn {
  87. params["_vic_spawn"];
  88. sleep 2;
  89. //We check if the vehicle still exists after that X second sleep, incase zeus delets it.
  90. if(!(alive _vic_spawn)) exitWith{ };
  91. _vic_spawn allowDamage true;
  92. _attached_objectes=attachedObjects _vic_spawn;
  93. {
  94. _x allowDamage true;
  95. } forEach _attached_objectes;
  96. };