//based on Xeno scripts


_search_array = [];
for "_i" from 0 to (count (configFile >> "cfgPatches") - 1) do {_search_array = _search_array + [configName ((configFile >> "cfgPatches") select _i)];};
ch_found_DMSmokeGrenadeVB = (if ("DMSmokeGrenadeVB" in _search_array) then {true} else {false});
_search_array = nil;


if (ch_found_DMSmokeGrenadeVB) then {
	ch_DM_SMOKE_SHELL = {

		private ["_ThisSmoke","_here","_ViewBlock","_wind","_timeout"];
		_ThisSmoke = _this select 0;
		DM_MP_THROW_OBJ = _ThisSmoke;
		publicVariable "DM_MP_THROW_OBJ";
		_here = position _ThisSmoke;
		sleep ((random 5) + 5);
		_ViewBlock = "DMShellSmokeVBinv" createVehicleLocal _here;
	
		_wind = wind;
				
		_ViewBlock setPos [(_here select 0), (_here select 1), ((_here select 2) +5)];
		_ViewBlock setVectorUp [-1*(_wind select 0),-1*(_wind select 1),1];
				
		_timeout = time + 60;
		while {time < _timeout} do {
			_wind = wind;
				
			sleep 5; // is here to allow particles to drift first before adjusting Viewblock
				
			_here = position _ThisSmoke;
			_ViewBlock setPos [(_here select 0), (_here select 1), ((_here select 2) +5)]; //is in the loop in case of v. high launch
			_ViewBlock setVectorUp [-1*(_wind select 0),-1*(_wind select 1),1];
		};
		
		deleteVehicle _ViewBlock;
	};
};


private ["_vehicle","_type","_weapons"];
_vehicle = _this select 0;
_type = typeOf _vehicle;

scopeName "throwsmoke";

if (!isNull ch_throw_smoke_vec) then {
	if (ch_throw_smoke_vec == _vehicle) then {
		ch_throw_smoke_vec vehicleChat localize "STR_VC_CH_Smoke1";
		breakOut "throwsmoke";
	};
};

_weapons = getArray (configFile >> "CfgVehicles" >> _type >> "Turrets" >> "MainTurret" >> "weapons");

if (count _weapons > 0) then {
	ch_throw_smoke_vec = _vehicle;
	[] spawn {
		sleep 25 + random (5);
		ch_throw_smoke_vec vehicleChat localize "STR_VC_CH_Smoke2";
		ch_throw_smoke_vec = objNull;
	};
	_mainweapon = _this select 0;
	_wDir = _vehicle weaponDirection (_weapons select 0);
	_weaponDir = (_wDir select 0) atan2 (_wDir select 1);
	_start_angle = (_weaponDir - 60) + 180;
	_end_angle = (_weaponDir + 60) + 180;
	_smoke_positions = [];
	_center_x = (position _vehicle) select 0;
	_center_y = (position _vehicle) select 1;
	_radius = 25;
	_angle = _start_angle;
	
	_posvec = position _vehicle;
	_dir = _weaponDir;
	_vehpos = [(_posvec select 0)+sin(_dir)*1, (_posvec select 1)+cos(_dir)*1, (_posvec select 2)];

	sleep 0.2;
	
	_shellpos = [(_vehpos select 0)+sin(_dir)*1,(_vehpos select 1)+cos(_dir)*1,3.5];
	_shell1 = "Grenade" createVehicle _shellpos;
	_shell1 setpos _shellpos;
	_shell1 setdir random 360;
	_shell1 setvelocity [sin(_dir)*10,cos(_dir)*10,12+random 3];

	_shellpos = [(_vehpos select 0)+sin(_dir+20)*1,(_vehpos select 1)+cos(_dir+20)*1,3.5];
	_shell2 = "Grenade" createVehicle _shellpos;
	_shell2 setpos _shellpos;
	_shell2 setdir random 360;
	_shell2 setvelocity [sin(_dir+20)*10,cos(_dir+20)*10,12+random 3];

	_shellpos = [(_vehpos select 0)+sin(_dir-20)*1,(_vehpos select 1)+cos(_dir-20)*1,3.5];
	_shell3 = "Grenade" createVehicle _shellpos;
	_shell3 setpos _shellpos;
	_shell3 setdir random 360;
	_shell3 setvelocity [sin(_dir-20)*10,cos(_dir-20)*10,12+random 3];
	
	_shellpos = [(_vehpos select 0)+sin(_dir+40)*1,(_vehpos select 1)+cos(_dir+40)*1,3.5];
	_shell4 = "Grenade" createVehicle _shellpos;
	_shell4 setpos _shellpos;
	_shell4 setdir random 360;
	_shell4 setvelocity [sin(_dir+40)*10,cos(_dir+40)*10,12+random 3];

	_shellpos = [(_vehpos select 0)+sin(_dir-40)*1,(_vehpos select 1)+cos(_dir-40)*1,3.5];
	_shell5 = "Grenade" createVehicle _shellpos;
	_shell5 setpos _shellpos;
	_shell5 setdir random 360;
	_shell5 setvelocity [sin(_dir-40)*10,cos(_dir-40)*10,12+random 3];

	_shellpos = [(_vehpos select 0)+sin(_dir+60)*1,(_vehpos select 1)+cos(_dir+60)*1,3.5];
	_shell6 = "Grenade" createVehicle _shellpos;
	_shell6 setpos _shellpos;
	_shell6 setdir random 360;
	_shell6 setvelocity [sin(_dir+60)*10,cos(_dir+60)*10,12+random 3];

	_shellpos = [(_vehpos select 0)+sin(_dir-60)*1,(_vehpos select 1)+cos(_dir-60)*1,3.5];
	_shell7 = "Grenade" createVehicle _shellpos;
	_shell7 setpos _shellpos;
	_shell7 setdir random 360;
	_shell7 setvelocity [sin(_dir-60)*10,cos(_dir-60)*10,12+random 3];

	sleep 1.45;
	deletevehicle _shell1;
	sleep random 0.10;
	deletevehicle _shell2;
	sleep random 0.10;
	deletevehicle _shell3;
	sleep random 0.10;
	deletevehicle _shell4;
	sleep random 0.10;
	deletevehicle _shell5;
	sleep random 0.10;
	deletevehicle _shell6;
	sleep random 0.10;
	deletevehicle _shell7;
	
	for "_i" from 1 to 7 do {
		_x1 = _center_x - (_radius * sin _angle);
		_y1 = _center_y - (_radius * cos _angle);
		_smoke_positions = _smoke_positions + [[_x1, _y1, 0]];
		_angle = _angle + 20;
	};
	_shells = [];
	{
		_shell = "SmokeShell" createVehicle _x;
		_shells = _shells + [_shell];
		if (ch_found_DMSmokeGrenadeVB) then {
			[_shell] spawn ch_DM_SMOKE_SHELL;
		};
	} forEach _smoke_positions;
	sleep 65 + random 3;
	{
		if (!isNull _x) then {
			deleteVehicle _x;
		};
	} forEach _shells;
};