Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.61 KB | None | 0 0
  1. // File: murk_spawn.sqf
  2. // Function: To allow for simple trigger based spawning using editor placed units and waypoints. The script deletes all units when the mission start and the recreate them on command.
  3. // Parameters:
  4. // _this select 0: OBJECT - unit name (this)
  5. // _this select 1: STRING - spawn type ("once","repeated","wave" and "reset")
  6. // _this select 2 (optional): NUMBER - spawn lives (the amount of time the unit respawns, or wave number)
  7. // _this select 3 (optional): NUMBER - spawn delay
  8. // _this select 4 (optional): STRING - init string called for the leader of the group
  9. // _this select 5 (optional): NUMBER- will start removal sequence of all dead group members after X seconds (default 120)
  10. // Usage:
  11. // Example trigger: Anybody Once (or whatever you want), onActivation: triggername setVariable ["murk_spawn",true,true];
  12. // Unit (leader of group): nul = [this,"SPAWNTYPE",LIVES,DELAY] execVM "murk_spawn.sqf";
  13. //
  14. // Example: nul = [this,"once"] execVM "murk_spawn3.sqf"; ---- Will spawn the editor unit once based on the trigger
  15. // Example: nul = [this,"repeated",4,30] execVM "murk_spawn.sqf"; ---- Will spawn the editor unit based on the trigger, then respawn it 4 times with a 30 second delay upon death
  16. // Example: nul = [this,"wave",5,60] execVM "murk_spawn.sqf"; ---- Will spawn the editor unit once based on the trigger, then respawn the entire group (regardless of deaths) 5 times with 60 seconds between
  17. // Example: nul = [this,"reset",5] execVM "murk_spawn.sqf"; ---- Will spawn the editor unit once based on the trigger, then reset the trigger after a preset time (15 seconds default). The unit will be created when trigger is true again, maximum number of lives.
  18. // -----------------------------------------------------------------------------------------------------
  19. // V9 (Murklor again :p)
  20. // - Note: Initial Arma 3 test script. Note that some functionality from V8/V7 has been taken out because my brain is too foggy to understand everything, this is simplified. Sort of.
  21. // - Fix: setVehicleInit has been removed from A3, testing another way
  22. // - Add: New example mission on Stratis
  23. // - Change: The trigger variable is held in the object that is synchronized to the group for improved WYSIWYG. This means that the trigger argument is no longer needed.
  24. // - Change: Updated this readme to reflect the changes
  25. // V8 (ArmAIIholic)
  26. // - Waypoints are also remembered -- no dummy groups at all!!!
  27. // - You get the output on Clipboard you can paste and execute. Added loader for Clipboard missions.
  28. // V7 (ArmAIIholic)
  29. // - Added GameLogic center to reduce number of dummy groups at the beginning, and in modes repeated and reset
  30. // - Changed beginning scope to isServer, rather than using exitWith
  31. // - Shortened initializing trigger part in init.sqf
  32. // - Added original instructions and examples, adapted for v7
  33. // - SQM is from v5 with some groups added
  34. // V6
  35. // - See older versions
  36.  
  37. // This script is serverside
  38. if(isServer) then
  39. {
  40.  
  41. // ------------------- Init ----------------------- //
  42. _countThis = count _this;
  43. _waitingPeriod = 5; // Waiting period between script refresh
  44.  
  45. // ---------------- Parameters -------------------- //
  46. _unit = _this select 0;
  47. _spawntype = _this select 1;
  48. _spawnlives = if (_countThis >= 3) then { _this select 2; } else { 1 }; // Optional
  49. _spawndelay = if (_countThis >= 4) then { _this select 3; } else { 1 }; // Optional
  50. _initString = if (_countThis >= 5) then { _this select 4; } else { "" }; // Optional
  51. _bodyRemove = if (_countThis >= 6) then { _this select 5; } else { 120 }; // Optional
  52.  
  53. // The object (trigger, whatever) the unit is synchronized to hold the trigger variable
  54. _triggerObject = (synchronizedObjects _unit) select 0;
  55. _triggerObject setVariable ["murk_spawn",false,false];
  56.  
  57. // -- Delete the unit (this is always done ASAP) -- //
  58. _unitArray = [];
  59. _unitGroup = group _unit;
  60. _unitsInGroup = units _unitGroup;
  61. _unitCount = count _unitsInGroup;
  62. _unitsInGroupAdd = [];
  63. _side = side _unitGroup;
  64.  
  65. //hint format ["Deleting: %1 - more: %2 - last: %3", _unitsInGroup, _unitGroup, _unit]; sleep 1;
  66.  
  67. scopeName "escape";
  68. while { _unitCount > 1 } do {
  69. // The currently worked on unit
  70. _unitsInGroup = units _unitGroup;
  71. _unit = _unitsInGroup select 0;
  72.  
  73. if (isNil "_unit") then {
  74. breakTo "escape"
  75. };
  76.  
  77. //hint format ["Deleting: %1 - vehicleVarName: %2", _unit, vehicleVarName _unit]; sleep 1;
  78.  
  79. // Check if its a vehicle
  80. if ( (vehicle _unit) isKindOf "LandVehicle" OR (vehicle _unit) isKindOf "Air") then {
  81. _vcl = vehicle _unit;
  82. if (!(_vcl in _unitsInGroupAdd) AND (typeOf _vcl != "")) then {
  83. _unitsIngroupAdd set [count _unitsInGroupAdd, _vcl];
  84. _unitCrewArray = [];
  85. _crew = crew _vcl;
  86. { _unitCrewArray set [count _unitCrewArray, typeOf _x]; } forEach _crew;
  87. _unitInfoArray = [typeOf _vcl,getPos _vcl,getDir _vcl,vehicleVarName _vcl,skill _vcl,rank _vcl,weapons _vcl,magazines _vcl,_unitCrewArray];
  88. _unitArray set [count _unitArray, _unitInfoArray];
  89. deleteVehicle _vcl;
  90. { deleteVehicle _x; } forEach _crew;
  91. };
  92. }
  93. // Otherwise its infantry
  94. else {
  95. _unitInfoArray = [typeOf _unit,getPos _unit,getDir _unit,vehicleVarName _unit,skill _unit,rank _unit,weapons _unit,magazines _unit,[]];
  96. _unitArray set [count _unitArray, _unitInfoArray];
  97. deleteVehicle _unit;
  98. };
  99. sleep 0.01;
  100. _unitCount = count _unitsInGroup;
  101. };
  102.  
  103. // Gathering waypoints
  104. _countWaypoints = 0;
  105. _waypointsArray = [];
  106. _waypointsEntry = [];
  107. _countWaypoints = count(waypoints _unitGroup);
  108.  
  109. for [{_i=0}, {_i < _countWaypoints}, {_i=_i+1}] do
  110. {
  111. _waypointsEntry = [];
  112.  
  113. _wPos = waypointPosition [_unitGroup, _i];
  114. _wHPos = waypointHousePosition [_unitGroup, _i];
  115. _wBih = waypointBehaviour [_unitGroup, _i];
  116. _wCM = waypointCombatMode [_unitGroup, _i];
  117. _wCR = waypointCompletionRadius [_unitGroup, _i];
  118. _wDes = waypointDescription [_unitGroup, _i];
  119. _wForm = waypointFormation [_unitGroup, _i];
  120. _wScr = waypointScript [_unitGroup, _i];
  121. _wShw = waypointShow [_unitGroup, _i];
  122. _wSp = waypointSpeed [_unitGroup, _i];
  123. _wSt = waypointStatements [_unitGroup, _i]; //diag_log _wSt;
  124. _wTo = waypointTimeout [_unitGroup, _i];
  125. _wTy = waypointType [_unitGroup, _i];
  126.  
  127. _waypointsEntry = _waypointsEntry + [_wPos] + [_wHPos] + [_wBih] + [_wCM] + [_wCR] + [_wDes] + [_wForm] + [_wScr] + [_wShw] + [_wSp] + [_wSt] + [_wTo] + [_wTy];
  128.  
  129. _waypointsArray = _waypointsArray + [_waypointsEntry];
  130. };
  131.  
  132. //diag_log format ["Waypoints' array : %1",_waypointsArray];
  133.  
  134. deleteGroup _unitGroup;
  135.  
  136. // ----------------- Functions -------------------- //
  137.  
  138. // *WARNING* BIS FUNCTION RIPOFF - Taken from fn_returnConfigEntry as its needed for turrets and shortened a bit
  139. _fnc_returnConfigEntry = {
  140. private ["_config", "_entryName","_entry", "_value"];
  141. _config = _this select 0;
  142. _entryName = _this select 1;
  143. _entry = _config >> _entryName;
  144. //If the entry is not found and we are not yet at the config root, explore the class' parent.
  145. if (((configName (_config >> _entryName)) == "") && (!((configName _config) in ["CfgVehicles", "CfgWeapons", ""]))) then {
  146. [inheritsFrom _config, _entryName] call _fnc_returnConfigEntry;
  147. }
  148. else { if (isNumber _entry) then { _value = getNumber _entry; } else { if (isText _entry) then { _value = getText _entry; }; }; };
  149. //Make sure returning 'nil' works.
  150. if (isNil "_value") exitWith {nil};
  151. _value;
  152. };
  153.  
  154. // *WARNING* BIS FUNCTION RIPOFF - Taken from fn_fnc_returnVehicleTurrets and shortened a bit
  155. _fnc_returnVehicleTurrets = {
  156. private ["_entry","_turrets", "_turretIndex"];
  157. _entry = _this select 0;
  158. _turrets = [];
  159. _turretIndex = 0;
  160. //Explore all turrets and sub-turrets recursively.
  161. for "_i" from 0 to ((count _entry) - 1) do {
  162. private ["_subEntry"];
  163. _subEntry = _entry select _i;
  164. if (isClass _subEntry) then {
  165. private ["_hasGunner"];
  166. _hasGunner = [_subEntry, "hasGunner"] call _fnc_returnConfigEntry;
  167. //Make sure the entry was found.
  168. if (!(isNil "_hasGunner")) then {
  169. if (_hasGunner == 1) then {
  170. _turrets = _turrets + [_turretIndex];
  171. //Include sub-turrets, if present.
  172. if (isClass (_subEntry >> "Turrets")) then { _turrets = _turrets + [[_subEntry >> "Turrets"] call _fnc_returnVehicleTurrets]; }
  173. else { _turrets = _turrets + [[]]; };
  174. };
  175. };
  176. _turretIndex = _turretIndex + 1;
  177. };
  178. };
  179. _turrets;
  180. };
  181.  
  182. _fnc_moveInTurrets = {
  183. private ["_turrets","_path","_i"];
  184. _turrets = _this select 0;
  185. _path = _this select 1;
  186. _currentCrewMember = _this select 2;
  187. _crew = _this select 3;
  188. _spawnUnit = _this select 4;
  189. _i = 0;
  190. while {_i < (count _turrets)} do {
  191. _turretIndex = _turrets select _i;
  192. _thisTurret = _path + [_turretIndex];
  193. //(_crew select _currentCrewMember) moveInTurret [_spawnUnit, _thisTurret]; //This causes a bug because it's counting wrong. not sure how to fix. easier is to just have units get in.
  194. _currentCrewMember = _currentCrewMember + 1;
  195. //Spawn units into subturrets.
  196. [_turrets select (_i + 1), _thisTurret, _currentCrewmember, _crew, _spawnUnit] call _fnc_moveInTurrets;
  197. _i = _i + 2;
  198. };
  199. };
  200.  
  201. // This is the general cleanup function running in the background for the group, replaces the removebody eventhandler and delete group in V5
  202. _fnc_cleanGroup = {
  203. _group = _this select 0;
  204. _unitsGroup = units _group;
  205. _sleep = _this select 1;
  206. // Hold until the entire group is dead
  207. while { ({alive _x} count _unitsGroup) > 0 } do { sleep 5; };
  208. sleep _sleep;
  209. {
  210. _origPos = getPos _x;
  211. _z = _origPos select 2;
  212. _desiredPosZ = if ( (vehicle _x) iskindOf "Man") then { (_origPos select 2) - 0.5 } else { (_origPos select 2) - 3 };
  213. if ( vehicle _x == _x ) then {
  214. _x enableSimulation false;
  215. while { _z > _desiredPosZ } do {
  216. _z = _z - 0.01;
  217. _x setPos [_origPos select 0, _origPos select 1, _z];
  218. sleep 0.1;
  219. };
  220. };
  221. deleteVehicle _x;
  222. sleep 5;
  223. } forEach _unitsGroup;
  224. // Now we know that all units are deleted
  225. deleteGroup _group;
  226. };
  227.  
  228.  
  229. _fnc_spawnUnit = {
  230. // We need to pass the old group so we can copy waypoints from it, the rest we already know
  231. _oldGroup = _this select 0;
  232. _newGroup = createGroup (_this select 1);
  233. _waypointsArray = _this select 2;
  234. // If the old group doesnt have any units in it its a spawned group rather than respawned
  235. if ( count (units _oldGroup) == 0) then { deleteGroup _oldGroup; };
  236. {
  237. //_spawnUnit = Object;
  238. _spawnUnit = 0;
  239. _unitType = _x select 0; _unitPos = _x select 1; _unitDir = _x select 2;
  240. _unitName = _x select 3; _unitSkill = _x select 4; _unitRank = _x select 5;
  241. _unitWeapons = _x select 6; _unitMagazines = _x select 7; _unitCrew = _x select 8;
  242. // Check if the unit has a crew, if so we know its a vehicle
  243. if (count _unitCrew > 0) then {
  244. if (_unitPos select 2 >= 10) then {
  245. _spawnUnit = createVehicle [_unitType,_unitPos, [], 0, "FLY"];
  246. _spawnUnit setVelocity [50 * (sin _unitDir), 50 * (cos _unitDir), 0];
  247. }
  248. else { _spawnUnit = _unitType createVehicle _unitPos; };
  249. // Create the entire crew
  250. _crew = [];
  251. { _unit = _newGroup createUnit [_x, getPos _spawnUnit, [], 0, "NONE"]; _crew set [count _crew, _unit]; } forEach _unitCrew;
  252. // We assume that all vehicles have a driver, the first one of the crew
  253. (_crew select 0) moveInDriver _spawnUnit;
  254. // Count the turrets and move the men inside
  255. _turrets = [configFile >> "CfgVehicles" >> _unitType >> "turrets"] call _fnc_returnVehicleTurrets;
  256. [_turrets, [], 1, _crew, _spawnUnit] call _fnc_moveInTurrets;
  257. }
  258. // Otherwise its infantry
  259. else {
  260. _spawnUnit = _newGroup createUnit [_unitType,_unitPos, [], 0, "NONE"];
  261. removeAllWeapons _spawnUnit;
  262. {_spawnUnit removeMagazine _x} forEach magazines _spawnUnit;
  263. removeAllItems _spawnUnit;
  264. {_spawnUnit addMagazine _x} forEach _unitMagazines;
  265. {_spawnUnit addWeapon _x} forEach _unitWeapons;
  266. _spawnUnit selectWeapon (primaryWeapon _spawnUnit);
  267. };
  268. // Set all the things common to the spawned unit
  269. _spawnUnit setDir _unitDir;
  270. _spawnUnit setSkill _unitSkill;
  271. _spawnUnit setUnitRank _unitRank;
  272. if (_spawntype == "once" OR _spawntype == "repeated") then {
  273. //_spawnUnit call compile format ["%1= _this; _this setVehicleVarName '%1'; PublicVariable '%1';",_unitName];
  274. _spawnUnit call compile format ["_myTest= _this; _this setVehicleVarName '_myTest'; PublicVariable '_myTest';",_unitName];
  275. };
  276. } forEach _unitArray;
  277.  
  278. private ["_i"]; _i = 0;
  279.  
  280. //Let's return them their waypoints
  281. {
  282. //diag_log format ["All data : %1",_x];
  283. _wp = _newGroup addWaypoint [(_x select 0),0,_i];
  284. [_newGroup, _i] setWaypointHousePosition (_x select 1);
  285. [_newGroup, _i] setWaypointBehaviour (_x select 2);
  286. [_newGroup, _i] setWaypointCombatMode (_x select 3);
  287. [_newGroup, _i] setWaypointCompletionRadius (_x select 4);
  288. [_newGroup, _i] setWaypointDescription (_x select 5);
  289. [_newGroup, _i] setWaypointFormation (_x select 6);
  290. [_newGroup, _i] setWaypointScript (_x select 7);
  291. [_newGroup, _i] showWaypoint (_x select 8);
  292. [_newGroup, _i] setWaypointSpeed (_x select 9);
  293. [_newGroup, _i] setWaypointStatements (_x select 10);
  294. [_newGroup, _i] setWaypointTimeout (_x select 11);
  295. [_newGroup, _i] setWaypointType (_x select 12);
  296.  
  297. _i = _i + 1;
  298.  
  299. } forEach _waypointsArray;
  300.  
  301. (vehicle (leader _newGroup)) call compile format ["%1",_initString];
  302.  
  303. // hint format ["Created object: %1 - vehicleVarName: %2", (vehicle (leader _newGroup)), vehicleVarName (vehicle (leader _newGroup))]; sleep 1;
  304.  
  305. // Run the cleanup function for this group
  306. [_newGroup, _bodyRemove] spawn _fnc_cleanGroup;
  307.  
  308. // Have to return the new group
  309. _newGroup;
  310. };
  311.  
  312. //Specific to BW it will move units to HC. Would need to add another paramter to make generic
  313. _fnc_moveToHC = {
  314. //No need to check if server. Parent function forces that
  315.  
  316. //First we sleep for a short while to ensure the unit has spawned completely.
  317. sleep 1;
  318. _unitGroup = _this select 0;
  319. _HC = HC_SLOT_1;
  320. if(isNull _HC) exitWith{};
  321. _HCid = owner _HC;
  322.  
  323. _unitGroup setGroupOwner _HCid ;
  324. };
  325.  
  326. // -------------- Waiting period ------------------ //
  327. while { !(_triggerObject getVariable "murk_spawn") } do { sleep _waitingPeriod; };
  328.  
  329. //hint "Triggered spawn!";
  330.  
  331. // --------------- Spawn Modes ------------------- //
  332. // REPEAT MODE, i.e. basic respawn based on lives
  333. if (_spawntype == "repeated") then {
  334. while { _spawnlives > 0 } do {
  335. _unitGroup = [_unitGroup,_side,_waypointsArray] call _fnc_spawnUnit;
  336. [_unitGroup] call _fnc_moveToHC; //Custom added to move units to HC for BW
  337. _spawnLives = _spawnLives - 1;
  338. _unitsGroup = units _unitGroup;
  339. while { ({alive _x} count _unitsGroup) > 0 } do { sleep 2; };
  340. sleep _spawndelay;
  341. };
  342. };
  343.  
  344. // WAVE MODE, this is fairly simple, just sleep a while then respawn. Spawnlives in this case is number of waves
  345. if (_spawntype == "wave") then {
  346. while { _spawnlives > 0 } do {
  347. _unitGroup = [_unitGroup,_side,_waypointsArray] call _fnc_spawnUnit;
  348. [_unitGroup] call _fnc_moveToHC;
  349. _spawnLives = _spawnLives - 1;
  350. sleep _spawndelay;
  351. };
  352. };
  353.  
  354. // RESET MODE, sleep a while then set the variable to false (even if you set it like 50 times over). Spawn lives is used to tick how many times its possible to reset.
  355. if (_spawntype == "reset") then {
  356. while { _spawnlives > 0 } do {
  357. _unitGroup = [_unitGroup,_side,_waypointsArray] call _fnc_spawnUnit;
  358. [_unitGroup] call _fnc_moveToHC;
  359. _spawnLives = _spawnLives - 1;
  360. sleep 15;
  361. _triggerObject setVariable ["murk_spawn",false,false];
  362. while { !(_triggerObject getVariable "murk_spawn") } do { sleep _waitingPeriod; };
  363. };
  364. };
  365.  
  366. // ONCE MODE
  367. if (_spawntype == "once") then {
  368. _unitGroup = [_unitGroup,_side,_waypointsArray] call _fnc_spawnUnit;
  369. [_unitGroup] call _fnc_moveToHC;
  370. };
  371. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement