/*******************************************************************************
* B T K   V E H I C L E   R E P A I R
*
* Execute the actions.
* 
* By BTK (bufu@mail.nu)
*******************************************************************************/


/***********************************************************************
* SCRIPT INIT, LOCALITY
***********************************************************************/
waitUntil {(isDedicated) || !(isNull player)};
scriptName "Engine.sqf: 1";
if (isDedicated) exitWith {};


/***********************************************************************
* VARIABLES
***********************************************************************/
_Vehicle = _this select 0;
_Unit = _this select 1;
_Action = _this select 2;
_Param = (_this select 3) select 0;
_UnitType = typeOf _Unit;
_VehicleType = typeOf _Vehicle;
_VehicleTypeDisp = getText (configFile >> "CfgVehicles" >> (_VehicleType) >> "displayName");
_VehicleDamage = (damage _Vehicle);
_VehicleDamageText = round(100*_VehicleDamage);
_VehicleFuel = (fuel _Vehicle);
_VehicleFuelText = round(100*_VehicleFuel);
_VehicleMags = (magazines _Vehicle);
_PercentText = "%";


/***********************************************************************
* LOCALITY CHECK
***********************************************************************/
if (player != _Unit) exitWith {};


/***********************************************************************
* GENERATE RANDOM REPAIR AND REFUEL VALUES
***********************************************************************/
if (!BTK_Vehicle_Repair_Can_Repair_All) then {
	if ((_UnitType in BTK_Vehicle_Repair_Can_Repair) && !(_UnitType in BTK_Vehicle_Repair_Can_Repair_Full)) then {
		RefuelValue = (0.05 + (random 0.05)); RepairValue = (0.10 + (random 0.10));
	};
} else {
	RefuelValue = (0.05 + (random 0.05)); RepairValue = (0.10 + (random 0.10));
};

if (_UnitType in BTK_Vehicle_Repair_Can_Repair_Full) then {
	RefuelValue = (0.10 + (random 0.10)); RepairValue = (0.25 + (random 0.25));
};

waitUntil {!(isNil "RepairValue") && !(isNil "RefuelValue")};
RepairValueText = round(100*RepairValue);
RefuelValueText = round(100*RefuelValue);


/***********************************************************************
* EXEC INFO
***********************************************************************/
if (_Param == "Info") exitWith {
	hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>Vehicle: <t color='#e9bc55'>%1</t><br />Damage: <t color='#e9bc55'>%2%4</t><br />Fuel: <t color='#e9bc55'>%3%4</t><br />Magazines: <t color='#e9bc55'>%5</t></t>",_VehicleTypeDisp,_VehicleDamageText,_VehicleFuelText,_PercentText,_VehicleMags];
};


/***********************************************************************
* EXEC REPAIR
***********************************************************************/
if (_Param == "Repair") exitWith {

	// Unit in vehicle
	if (_Unit in _Vehicle) exitWith {
		hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>You have to <t color='#e9bc55'>get out</t> to repair the vehicle!</t>"];
	};

	// Start the repairing
	_Vehicle setVehicleLock "LOCKED";
	_Vehicle removeAction _Action;
	_Unit playMove 'ActsPercSnonWnonDnon_carFixing2';
	
	if (BTK_Vehicle_Repair_Sound) then {
		[[_Unit], "BTK_Vehicle_Repair_Loop"] call CBA_fnc_globalSay;
	};
	
	sleep 1;
	hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>Vehicle: <t color='#e9bc55'>%1</t><br />Damage: <t color='#e9bc55'>%2%4</t><br />Fuel: <t color='#e9bc55'>%3%4</t><br /><br />Repairing, please wait.</t>",_VehicleTypeDisp,_VehicleDamageText,_VehicleFuelText,_PercentText];

	sleep 1;
	_Dummy = "Fuel_can" createVehicle (getPos player);
	_Dummy setPos [(getPos player select 0)+1.5, (getPos player select 1)-0.5, 0.4];
	
	// Wait until repairing done, exit if unit dies
	for "_j" from 1 to 58 do {
		sleep 0.5;
		if (!alive _Unit) exitWith {
			hint "aborted";
			_Unit playMove "";
			_Vehicle setVehicleLock "UNLOCKED";
		};
	};
	
	// Set new values
	_VehicleDamage = (damage _Vehicle);
	_Vehicle setDamage _VehicleDamage - RepairValue;
	_Vehicle setDamage _VehicleDamage - RepairValue;
	_Vehicle setVehicleArmor 1;
	_Vehicle setVehicleLock "UNLOCKED";
	
	// Done
	_VehicleDamage = (damage _Vehicle);
	_VehicleDamageText = round(100*_VehicleDamage);
	_VehicleFuel = (fuel _Vehicle);
	_VehicleFuelText = round(100*_VehicleFuel);
	sleep 1;
	hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>Vehicle: <t color='#e9bc55'>%1</t><br />Damage: <t color='#e9bc55'>%2%4</t><br />Fuel: <t color='#e9bc55'>%3%4</t><br /><br />Vehicle repaired. Damage <t color='#e9bc55'>-%5%4</t></t>",_VehicleTypeDisp,_VehicleDamageText,_VehicleFuelText,_PercentText,RepairValueText];
	_Vehicle setVariable ["BTK_Vehicle_Repair_Action", nil];
	
	sleep 8;
	deleteVehicle _Dummy;
};


/***********************************************************************
* EXEC REFUEL
***********************************************************************/
if (_Param == "Refuel") exitWith {

	// Unit in vehicle
	if (_Unit in _Vehicle) exitWith {
		hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>You have to <t color='#e9bc55'>get out</t> to refuel the vehicle!</t>"];
	};
	
	// Start
	_Vehicle removeAction _Action;
	_Unit playMove 'AinvPknlMstpSlayWrflDnon_medic';
	
	sleep 1;
	hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>Vehicle: <t color='#e9bc55'>%1</t><br />Damage: <t color='#e9bc55'>%2%4</t><br />Fuel: <t color='#e9bc55'>%3%4</t><br /><br />Refueling, please wait.</t>",_VehicleTypeDisp,_VehicleDamageText,_VehicleFuelText,_PercentText];

	sleep 1;
	_VehicleFuel = (fuel _Vehicle);
	_RefuelValue = RefuelValue;
	_Dummy = "Fuel_can" createVehicle (getPos player);
	_Dummy setPos [(getPos player select 0)+1.5, (getPos player select 1)-0.5, 0.4];
	
	// Wait until repairing done, exit if unit dies
	for "_j" from 1 to 14 do {
		sleep 0.5;
		if (!alive player) exitWith {};
	};
	
	// Set new value
	_Vehicle setFuel (_VehicleFuel + _RefuelValue);
	
	// Done
	_VehicleDamage = (damage _Vehicle);
	_VehicleDamageText = round(100*_VehicleDamage);
	_VehicleFuel = (fuel _Vehicle);
	_VehicleFuelText = round(100*_VehicleFuel);
	sleep 0.8;
	hint parseText format ["<t font='Zeppelin33' align='left' color='#b2b999' size='1.0'>Vehicle: <t color='#e9bc55'>%1</t><br />Damage: <t color='#e9bc55'>%2%4</t><br />Fuel: <t color='#e9bc55'>%3%4</t><br /><br />Vehicle refueled. Fuel <t color='#e9bc55'>+%5%4</t></t>",_VehicleTypeDisp,_VehicleDamageText,_VehicleFuelText,_PercentText,RefuelValueText];
	_Vehicle setVariable ["BTK_Vehicle_Refuel_Action", nil];
	
	sleep 8;
	deleteVehicle _Dummy;
};
