/*
==============================================================================================
by Cyborg11
Version: 1.03 @ 17.02.2010
Parameters: unit addAction ["Change Uniform", "uniformchange.sqf", [newunit,side,exit,debug]];
	_unit = unit which gets the action to change the uniform	(Object)
	_newunit = classname of the new unit				(Object)
	_side = side of the new unit						(side)
	_exit = if the unit can only change his uniform once, then
		    set the variable to true (old -> new -> old)		(Boolean)
		    default: false
	_debug =	optional - default: false		(Boolean)
Example: this addAction ["Change uniform", "uniformchange.sqf", ["GUE_Soldier_1",guerr]];
Example 2: player addAction ["Change uniform", "uniformchange.sqf", ["FR_GL",west,true]];

The new unit has the same weapons, magazines and the same name as the old unit
BUT: Because the script deletes the old unit and create a new one you must edit this script and execute all
	 scripts on the new unit again!
==============================================================================================
*/

private [
		"_ace","_unit","_action","_sideold","_type","_posunit","_varname","_array",
		"_newunit","_side","_exit","_weapons","_items","_primweap","_magazines",
		"_hasruck","_ruck","_ruckMags","_ruckWeaps","_debug","_weaponback"
		];

// Variables
_ace = isClass (configFile >> "CfgPatches" >> "ace_version");	// Check if ACE is installed
_unit = _this select 1;		// old unit
_action = _this select 2;	// action id 
_sideold = side _unit;		// side of old unit
_type = typeOf _unit;		// classname of old unit
_posunit = getPos _unit;	// position of the old unit
_varname = vehicleVarName _unit;	// Editorname of the old unit
_array = _this select 3;	// the new soldier array => ["FR_GL",west]
_newunit = _array select 0;	// classname of the new unit
_side = _array select 1;	// side of the new unit
If (count _array > 2) then {_exit = _array select 2} else {_exit = false};	// if set to true the script will exit
If (count _array > 3) then {_debug = _array select 3} else {_debug = false};
_weapons = (weapons _unit) - (items _unit);	// weapons of the old unit
_items = items _unit;		// items of old unit
_primweap = primaryWeapon _unit;	// primaryWeapon of the old unit
_magazines = magazines _unit;	// magazines of the old unit
If (_exit) then {
	If (isNil "CYBP_changecounter") then {
		CYBP_changecounter = 0;
	};
	CYBP_changecounter = CYBP_changecounter + 1;
};

If (_ace) then {
	_hasruck = player call ACE_Sys_Ruck_fnc_hasRuck;
	If (_hasruck) then {
		//_ruck = _unit call ACE_Sys_Ruck_fnc_FindRuck;
		_ruckMags = _unit getVariable "ACE_RuckMagContents";
		_ruckWeaps = _unit getVariable "ACE_RuckWepContents";
	};
	_weaponback = _unit getvariable "ACE_weapononback";
	If (_debug) then {player globalchat format ["%1",_weaponback]};
};

titleText ["Changing uniform ...", "BLACK FADED"];	// Black Screen
_unit setPos [0,0,0];		// teleport the old unit to [0,0,0]
_unit removeAction _action;

// Create the new unit
_uniform = (group _unit) createUnit [_newunit, _posunit, [], 0, "NONE"];
setPlayable _uniform;
sleep 0.5;
If (!isnil ("_uniform")) then {selectPlayer _uniform};
If (leader group _unit == leader _unit) then {			// Check if old unit is leader
	(group _unit) selectLeader _uniform;
};

// New Unit gets the sames weapons as the old unit
removeAllWeapons _uniform;
{_uniform addMagazine _x} forEach _magazines;
{_uniform addWeapon _x} forEach _weapons;
{_uniform addWeapon _x} forEach _items;
If (_ace) then {
	If (_hasruck) then {
		_uniform setVariable ["ACE_RuckMagContents", _ruckMags, true];
		_uniform setVariable ["ACE_RuckWepContents", _ruckWeaps, true];
	};
	If (_weaponback != "") then {
		_uniform addWeapon _weaponback;
		[_uniform, _weaponback] call ACE_fnc_PutWeaponOnBack;
	};
};	

// Select the primaryWeapon	
_uniform selectWeapon _primweap;
// Fix for weapons with grenade launcher
_muzzles = getArray(configFile>>"cfgWeapons" >> _primweap >> "muzzles");
_uniform selectWeapon (_muzzles select 0);

sleep 0.2;
deleteVehicle _unit;			// delete old unit
sleep 1;
titleText [" ", "BLACK IN"];	// delete Black Screen
_uniform SetVehicleVarName _varname;
_uniform call Compile Format ["%1=_This ; PublicVariable ""%1""",_varname];
If (!isNil ("_exit") && _exit && CYBP_changecounter >= 2) exitWith{};
_uniform addAction ["Change uniform", "uniformchange.sqf", [_type,_side,_exit,_debug], 0, false, false, ""]; 

If (_debug) then {
	hint format ["Counter: %9\nUnit: %1\nType: %2\nPosUnit: %3\nArray: %4\nNewUnit: %5\nSide: %6\nVarNameOld: %7\nVarNameNew: %8\nExit: %10",_unit,_type,_posunit,_array,_newunit,_side,_varname,(vehicleVarName _uniform),CYBP_changecounter,_exit]
};