Advertisement
Gr8Boi

player_craftItem.sqf

Jul 17th, 2014
451
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.80 KB | None | 0 0
  1. /*
  2. DayZ Epoch Crafting 0.3
  3. Modified By [GG] Gr8Boi for no animation
  4. Made for DayZ Epoch && Unleashed by [VB]AWOL please ask permission to use/edit/distrubute email vbawol@veteranbastards.com.
  5. Thanks to thevisad for help with the spawn call fixes.
  6.  
  7. USAGE EXAMPLE:
  8. class ItemActions
  9. {
  10. class Crafting
  11. {
  12. text = "Craft Tent";
  13. script = ";['Crafting','CfgMagazines', _id] spawn player_craftItem;"; // [Class of itemaction,CfgMagazines || CfgWeapons, item]
  14. neednearby[] = {"workshop","fire"};
  15. requiretools[] = {"ItemToolbox","ItemKnife"}; // (cfgweapons only)
  16. output[] = {{"ItemTent",1}}; // (CfgMagazines, qty)
  17. input[] = {{"ItemCanvas",2},{"ItemPole",2}}; // (CfgMagazines, qty)
  18. inputstrict = true; // (CfgMagazines input without inheritsFrom) Optional
  19. inputweapons[] = {"ItemToolbox"}; // consume toolbox (cfgweapons only)
  20. outputweapons[] = {"ItemToolbox"}; // return toolbox (cfgweapons only)
  21. };
  22. };
  23. */
  24. private ["_tradeComplete","_onLadder","_canDo","_selectedRecipeOutput","_proceed","_itemIn","_countIn","_missing","_missingQty","_qty","_itemOut","_countOut","_removed","_tobe_removed_total","_textCreate","_textMissing","_selectedRecipeInput","_selectedRecipeInputStrict","_num_removed","_removed_total","_temp_removed_array","_abort","_waterLevel","_waterLevel_lowest","_reason","_isNear","_missingTools","_hastoolweapon","_selectedRecipeTools","_distance","_crafting","_needNear","_item","_baseClass","_num_removed_weapons","_outputWeapons","_inputWeapons","_randomOutput","_craft_doLoop","_selectedWeapon","_selectedMag","_sfx"];
  25.  
  26. if(DZE_ActionInProgress) exitWith { cutText [(localize "str_epoch_player_63") , "PLAIN DOWN"]; };
  27. DZE_ActionInProgress = true;
  28.  
  29. // This is used to find correct recipe based what itemaction was click allows multiple recipes per item.
  30. _crafting = _this select 0;
  31.  
  32. // This tells the script what type of item we are clicking on
  33. _baseClass = _this select 1;
  34.  
  35. _item = _this select 2;
  36.  
  37. _abort = false;
  38. _distance = 3;
  39. _reason = "";
  40. _waterLevel = 0;
  41. _outputWeapons = [];
  42. _selectedRecipeOutput = [];
  43. _onLadder = (getNumber (configFile >> "CfgMovesMaleSdr" >> "States" >> (animationState player) >> "onLadder")) == 1;
  44. _canDo = (!r_drag_sqf && !r_player_unconscious && !_onLadder);
  45.  
  46. // Need Near Requirements
  47. _needNear = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "neednearby");
  48. if("fire" in _needNear) then {
  49. _isNear = {inflamed _x} count (getPosATL player nearObjects _distance);
  50. if(_isNear == 0) then {
  51. _abort = true;
  52. _reason = "fire";
  53. };
  54. };
  55. if("workshop" in _needNear) then {
  56. _isNear = count (nearestObjects [player, ["Wooden_shed_DZ","WoodShack_DZ","WorkBench_DZ"], _distance]);
  57. if(_isNear == 0) then {
  58. _abort = true;
  59. _reason = "workshop";
  60. };
  61. };
  62. if(_abort) exitWith {
  63. cutText [format[(localize "str_epoch_player_149"),_reason,_distance], "PLAIN DOWN"];
  64. DZE_ActionInProgress = false;
  65. };
  66.  
  67. // diag_log format["Checking for fire: %1", _isFireNear];
  68.  
  69. if (_canDo) then {
  70.  
  71. _selectedRecipeTools = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "requiretools");
  72. _selectedRecipeOutput = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "output");
  73. _selectedRecipeInput = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "input");
  74. _selectedRecipeInputStrict = if ((isNumber (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "inputstrict")) && (getNumber (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "inputstrict") > 0)) then {true} else {false};
  75. _outputWeapons = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "outputweapons");
  76. _inputWeapons = getArray (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "inputweapons");
  77.  
  78. _sfx = getText(configFile >> _baseClass >> _item >> "sfx");
  79. if(_sfx == "") then {
  80. _sfx = "repair";
  81. };
  82.  
  83. _randomOutput = 0;
  84. if(isNumber (configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "randomOutput")) then {
  85. _randomOutput = getNumber(configFile >> _baseClass >> _item >> "ItemActions" >> _crafting >> "randomOutput");
  86. };
  87.  
  88. _craft_doLoop = true;
  89. _tradeComplete = 0;
  90.  
  91. while {_craft_doLoop} do {
  92.  
  93. _temp_removed_array = [];
  94.  
  95. _missing = "";
  96. _missingTools = false;
  97. {
  98. _hastoolweapon = _x in weapons player;
  99. if(!_hastoolweapon) exitWith { _craft_doLoop = false; _missingTools = true; _missing = _x; };
  100. } forEach _selectedRecipeTools;
  101.  
  102. if(!_missingTools) then {
  103.  
  104.  
  105. // Dry run to see if all parts are available.
  106. _proceed = true;
  107. if (count _selectedRecipeInput > 0) then {
  108. {
  109. _itemIn = _x select 0;
  110. _countIn = _x select 1;
  111.  
  112. _qty = { (_x == _itemIn) || (!_selectedRecipeInputStrict && configName(inheritsFrom(configFile >> "cfgMagazines" >> _x)) == _itemIn) } count magazines player;
  113.  
  114. if(_qty < _countIn) exitWith { _missing = _itemIn; _missingQty = (_countIn - _qty); _proceed = false; };
  115.  
  116. } forEach _selectedRecipeInput;
  117. };
  118.  
  119. // If all parts proceed
  120. if (_proceed) then {
  121.  
  122. cutText [(localize "str_epoch_player_62"), "PLAIN DOWN"];
  123.  
  124.  
  125. _removed_total = 0; // count total of removed items
  126. _tobe_removed_total = 0; // count total of all to be removed items
  127. _waterLevel_lowest = 0; // find the lowest _waterLevel
  128. // Take items
  129. {
  130. _removed = 0;
  131. _itemIn = _x select 0;
  132. _countIn = _x select 1;
  133. // diag_log format["Recipe Finish: %1 %2", _itemIn,_countIn];
  134. _tobe_removed_total = _tobe_removed_total + _countIn;
  135.  
  136. // Preselect the item
  137. {
  138. _configParent = configName(inheritsFrom(configFile >> "cfgMagazines" >> _x));
  139. if ((_x == _itemIn) || (!_selectedRecipeInputStrict && _configParent == _itemIn)) then {
  140. // Get lowest waterlevel
  141. if ((_x == "ItemWaterbottle") ||( _configParent == "ItemWaterbottle")) then {
  142. _waterLevel = floor((getNumber(configFile >> "CfgMagazines" >> _x >> "wateroz")) - 1);
  143. if (_waterLevel_lowest == 0 || _waterLevel < _waterLevel_lowest) then {
  144. _waterLevel_lowest = _waterLevel;
  145. };
  146. };
  147. };
  148. } forEach magazines player;
  149.  
  150. {
  151. _configParent = configName(inheritsFrom(configFile >> "cfgMagazines" >> _x));
  152. if( (_removed < _countIn) && ((_x == _itemIn) || (!_selectedRecipeInputStrict && _configParent == _itemIn))) then {
  153. if ((_waterLevel_lowest == 0) || ((_waterLevel_lowest > 0) && (getNumber(configFile >> "CfgMagazines" >> _x >> "wateroz") == _waterLevel_lowest))) then {
  154. _num_removed = ([player,_x] call BIS_fnc_invRemove);
  155. }
  156. else {
  157. _num_removed = 0;
  158. };
  159. _removed = _removed + _num_removed;
  160. _removed_total = _removed_total + _num_removed;
  161. if(_num_removed >= 1) then {
  162. //diag_log format["debug remove: %1 of: %2", _configParent, _x];
  163. if (_x == "ItemWaterbottle" || _configParent == "ItemWaterbottle") then {
  164. _waterLevel = floor((getNumber(configFile >> "CfgMagazines" >> _x >> "wateroz")) - 1);
  165. };
  166. _temp_removed_array set [count _temp_removed_array,_x];
  167. };
  168. };
  169. } forEach magazines player;
  170.  
  171. } forEach _selectedRecipeInput;
  172.  
  173. //diag_log format["removed: %1 of: %2", _removed, _tobe_removed_total];
  174.  
  175. // Only proceed if all parts were removed successfully
  176. if(_removed_total == _tobe_removed_total) then {
  177. _num_removed_weapons = 0;
  178. {
  179. _num_removed_weapons = _num_removed_weapons + ([player,_x] call BIS_fnc_invRemove);
  180. } forEach _inputWeapons;
  181. if (_num_removed_weapons == (count _inputWeapons)) then {
  182. if(_randomOutput == 1) then {
  183. if (!isNil "_outputWeapons" && count _outputWeapons > 0) then {
  184. _selectedWeapon = _outputWeapons call BIS_fnc_selectRandom;
  185. _outputWeapons = [_selectedWeapon];
  186. };
  187. if (!isNil "_selectedRecipeOutput" && count _selectedRecipeOutput > 0) then {
  188. _selectedMag = _selectedRecipeOutput call BIS_fnc_selectRandom;
  189. _selectedRecipeOutput = [_selectedMag];
  190. };
  191. // exit loop
  192. _craft_doLoop = false;
  193. };
  194. {
  195. player addWeapon _x;
  196. } forEach _outputWeapons;
  197. {
  198.  
  199. _itemOut = _x select 0;
  200. _countOut = _x select 1;
  201.  
  202. if (_itemOut == "ItemWaterbottleUnfilled") then {
  203.  
  204. if (_waterLevel > 0) then {
  205. _itemOut = format["ItemWaterbottle%1oz",_waterLevel];
  206. };
  207.  
  208. };
  209.  
  210. // diag_log format["Checking for water level: %1", _waterLevel];
  211.  
  212. for "_x" from 1 to _countOut do {
  213. player addMagazine _itemOut;
  214. };
  215.  
  216. _textCreate = getText(configFile >> "CfgMagazines" >> _itemOut >> "displayName");
  217.  
  218. // Add crafted item
  219. cutText [format[(localize "str_epoch_player_150"),_textCreate,_countOut], "PLAIN DOWN"];
  220. // sleep here
  221. sleep 1;
  222.  
  223. } forEach _selectedRecipeOutput;
  224.  
  225. _tradeComplete = _tradeComplete+1;
  226. };
  227.  
  228. } else {
  229. // Refund parts since we failed
  230. {player addMagazine _x; } forEach _temp_removed_array;
  231.  
  232. cutText [format[(localize "str_epoch_player_151"),_removed_total,_tobe_removed_total], "PLAIN DOWN"];
  233. };
  234.  
  235. } else {
  236. _textMissing = getText(configFile >> "CfgMagazines" >> _missing >> "displayName");
  237. cutText [format[(localize "str_epoch_player_152"),_missingQty, _textMissing,_tradeComplete], "PLAIN DOWN"];
  238. _craft_doLoop = false;
  239. };
  240. } else {
  241. _textMissing = getText(configFile >> "CfgWeapons" >> _missing >> "displayName");
  242. cutText [format[(localize "STR_EPOCH_PLAYER_137"),_textMissing], "PLAIN DOWN"];
  243. _craft_doLoop = false;
  244. };
  245. };
  246. } else {
  247. cutText [(localize "str_epoch_player_64"), "PLAIN DOWN"];
  248. };
  249. DZE_ActionInProgress = false;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement