////////////////////////////////////////////////
// Make Player Unconscious
////////////////////////////////////////////////


private["_unit", "_killer"];
_unit = _this select 0;
_killer = _this select 1;
	
// Death message
if (STS24_EnableDeathMessages && {!isNil "_killer"} && {isPlayer _killer} && {_killer != _unit}) then
{
	STS24_deathMessage = [_unit, _killer];
	publicVariable "STS24_deathMessage";
	["STS24_deathMessage", [_unit, _killer]] call STS24_publicvariable_EH;
};
	
if (isPlayer _unit) then
{
	disableUserInput true;
	titleText ["", "BLACK FADED"];
};
	
// Eject unit if inside vehicle
if (vehicle _unit != _unit) then 
{
	unAssignVehicle _unit;
	_unit action ["eject", vehicle _unit];
		
	sleep 2;
};
	
_unit setDamage 0;
_unit setVelocity [0,0,0];
_unit allowDamage false;
_unit setCaptive true;
_unit playMove "AinjPpneMstpSnonWrflDnon_rolltoback";
	
sleep 4;
    
if (isPlayer _unit) then
{
	titleText ["", "BLACK IN", 1];
	disableUserInput false;
};
	
_unit switchMove "AinjPpneMstpSnonWrflDnon";
_unit enableSimulation false;
_unit setVariable ["STS24_isUnconscious", 1, true];
	
// Call this code only on players
if (isPlayer _unit) then 
{
	_bleedOut = time + STS24_BleedOut;
		
	while { !isNull _unit && {alive _unit} && {_unit getVariable "STS24_isUnconscious" == 1} && {(STS24_BleedOut <= 0 || time < _bleedOut)} } do
	{
		hintSilent format["Bleedout in %1 seconds\n\n%2", round (_bleedOut - time), call STS24_CheckFriendlies];
			
		sleep 0.5;
	};
		
	// Player bled out
	if (STS24_BleedOut > 0 && {time > _bleedOut}) then
	{
		_unit setDamage 1;
		_unit setVariable ["STS24_isUnconscious", 0, true];
		_unit setVariable ["STS24_isDragged", 0, true];
	}
	else
	{
		// Player got revived
		sleep 6;
			
		// Clear the "medic nearby" hint
		hintSilent "";
			
		_unit enableSimulation true;
		_unit allowDamage true;
		_unit setDamage 0;
		_unit setCaptive false;
			
		_unit playMove "amovppnemstpsraswrfldnon";
		_unit playMove "";
	};
}
else
{
	// [Debugging] Bleedout for AI
	_bleedOut = time + STS24_BleedOut;
		
	while { !isNull _unit && {alive _unit} && {_unit getVariable "STS24_isUnconscious" == 1} && {(STS24_BleedOut <= 0 || time < _bleedOut)} } do
	{
		sleep 0.5;
	};
		
	// AI bled out
	if (STS24_BleedOut > 0 && {time > _bleedOut}) then
	{
		_unit setDamage 1;
		_unit setVariable ["STS24_isUnconscious", 0, true];
		_unit setVariable ["STS24_isDragged", 0, true];
	}
};
