params[["_ammo", ""], ["_position", [0,0,0]], ["_direction", 0]];

diag_log format["[RD501][Fired Deployable][Deploy Server] Deploying Squad Shield for %1 at %2", _ammo, _position];

private _config = configFile >> "CfgAmmo" >> _ammo; 
if!(isClass _config) exitWith { 
	diag_log format["[RD501][Fired Deployable][Deploy Server] Unable to find class '%1' in CfgAmmo", _ammo]; 
}; 

private _isValid = getNumber (_config >> "rd501_fired_deployable") == 1;
private _deployable = getText (_config >> "rd501_fired_deployable_object");
private _timeToLive = getNumber (_config >> "rd501_fired_deployable_timeToLive");

if(!_isValid) exitWith {
	diag_log format["[RD501][Fired Deployable][Deploy Server] Requested throw type '%1' is does not contain rd501_fired_deployable=1", _ammo];
};

if(isNil "_deployable" || _deployable isEqualTo "") exitWith { 
	diag_log format["[RD501][Fired Deployable][Deploy Server] Failed to find rd501_fired_deployable_object defined in ammo type %1", _ammo];
};

_config = configFile >> "CfgVehicles" >> _deployable;

private _setInvincible = 0;
if(isNumber(_config >> "rd501_fired_deployable_ignoreDamage")) then {
	_setInvincible = getNumber (_config >> "rd501_fired_deployable_ignoreDamage");
};

private _hasLoopSound = isText (_config >> "rd501_fired_deployable_loopSound");
private _loopSound = getText (_config >> "rd501_fired_deployable_loopSound");
private _loopDuration = getNumber (_config >> "rd501_fired_deployable_loopDuration");
private _hasEndSound = isText (_config >> "rd501_fired_deployable_endSound");
private _endSound = getText (_config >> "rd501_fired_deployable_endSound");
private _endDuration = getNumber (_config >> "rd501_fired_deployable_endDuration");
private _soundDistance = getNumber (_config >> "rd501_fired_deployable_soundDistance");

private _deployed = createVehicle [_deployable, _position, [], 0, "CAN_COLLIDE"];
_deployed setPosATL _position;
_deployed setDir _direction;

if(_setInvincible == 1) then {
	_deployed allowDamage false;
};

if(_timeToLive > 0) then {
	[
		{
			params["_deployable"];
			deleteVehicle _deployable;
		},
		[_deployed],
		_timeToLive
	] call CBA_fnc_waitAndExecute;

	if(_hasEndSound && _endSound != "") then {
		[
			{
				params["_deployed", "_endSound", "_endDuration", "_distance"];
				["rd501_fired_deployable_soundEnd", [_deployed, _endSound, _endDuration, _distance]] call CBA_fnc_globalEvent;
			},
			[_deployed, _endSound, _endDuration, _soundDistance],
			(_timeToLive - _endDuration)
		] call CBA_fnc_waitAndExecute;
	};
};

if(!_hasLoopSound || _loopSound == "") exitWith {
	diag_log format["Decided not to play sound for: Loop Sound: '%1', Loop Sound: '%2'", _loopSound, _hasLoopSound];
};

["rd501_fired_deployable_soundLoop", [_deployed, _loopSound, _loopDuration, _timeToLive, _soundDistance]] call CBA_fnc_globalEvent;