apply_turrets.sqf 4.0 KB

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