/*
Create and position static
Based on Build 004 of Victor Farbers VFladder script
Items are as follows:
0 Short ladder "Land_ladder_half"
1 Long ladder "Land_ladder"
2 Grenade Launcher Nest 
3 AA Nest
4 AT Nest
5 MG Nest
6 Camo Net
7 Refuel Depot
8 Mortar
9 Foxhole
10 hedgehog
*/

private ["_who","_params","_statparam","_stattype","_statpos","_laction1","_laction2","_laction3","_zvector","_dist","_mkr_name","_marker2","_custom","_zfactor"];

//"foxhole","cstatA"

_params = _this select 3;
_who = _params select 0;
_playertype = typeOf (vehicle _who);
_statparam = _params select 1;
_dist = 2;
_custom = 0;
_zfactor = 1;
_scost = 2; //cost of statics

if (vehicle _who != _who) exitwith {};

if (_who in list AirportIn) exitWith {hint localize "STR_M04t88"};
//you cannot call support in the base

srank = 0;
_sco = score _who;
if(_sco >= EGG_EVO_rank1) then 
{
	srank = EGG_EVO_rank1;
};
if(_sco >= EGG_EVO_rank2) then 
{
	srank = EGG_EVO_rank2;
};
if(_sco >= EGG_EVO_rank3) then 
{
	srank = EGG_EVO_rank3;
};
if(_sco >= EGG_EVO_rank4) then 
{
	srank = EGG_EVO_rank4;
};
if(_sco >= EGG_EVO_rank5) then 
{
	srank = EGG_EVO_rank5;
};
if(_sco >= EGG_EVO_rank6) then 
{
	srank = EGG_EVO_rank6;
};

if ( (_scost >((_sco)-srank)) AND (_statparam != 0 and _statparam != 1 and _statparam != 6 and _statparam != 9 and _statparam != 10) ) exitwith {hint format[localize "STR_M04t89",_scost, ((_sco)-srank)]};
//You do not have the required spare points \nRequired: %1 \nCurrent: %2.

switch (_statparam) do
//returns _stattype and _mkr_name
{
	 case 0: //Short ladder "Land_ladder_half"
	{
//		_stattype = "Land_ladder_half";
		_allstat = [(EGG_EVO_SWEPS select 6)];
		_custom = 1;
		_zfactor = 0;
	};
	 case 1: //Long ladder "Land_ladder"
	{
//		_stattype = "Land_ladder";
		_allstat = [(EGG_EVO_SWEPS select 7)];
		_custom = 1;
		_zfactor = 0;
	};
	 case 2: //Grenade Launcher Nest 
	{
//		_stattype = "MK19_TriPod_US_EP1";
		_allstat = [(EGG_EVO_SWEPS select 10)];
		_mkr_name = format ["Mk18 Nest %1", name _who];
		_custom = 1;
		_dist = 3;
		if NOT( ((primaryWeapon _who) in EGG_EVO_GLweapons) OR ((secondaryWeapon _who) in EGG_EVO_GLweapons) ) exitWith {hint "You cannot build a Grenade Launcher Nest without an underslung grenade launcher";_custom = 2};
	};
	 case 3: //AA Nest
	{
//		_allstat = ["KORD_high_UN_EP1","Stinger_Pod","PRACS_Type74AAgun","ZU23_TK_INS_EP1","PRACS_M266AAgun","PRACS_HAWK"];
//		_allstat = ["KORD_high_UN_EP1","Stinger_Pod","ZU23_TK_INS_EP1"];
		_allstat = [(EGG_EVO_SWEPS select 2)];
		_maxb = (count _allstat)-1;
		_stattype = _allstat select (round random _maxb);
		_mkr_name = format ["AA Nest %1", name _who];
		_custom = 1;
		_dist = 4;
		if NOT( ((primaryWeapon _who) in EGG_EVO_AAirweapons) OR ((secondaryWeapon _who) in EGG_EVO_AAirweapons) ) exitWith {hint "You cannot build an AA Nest without a Shoulder Launched Surface-to-Air Missile";_custom = 2};
	};
	 case 4: //AT Nest
	{
//		_allstat = ["TOW_TriPod_US_EP1","PRACS_M1","SPG9_TK_GUE_EP1","Metis_TK_EP1","PRACS_M101"];
//		_allstat = ["TOW_TriPod_US_EP1","SPG9_TK_GUE_EP1","Metis_TK_EP1"];
		_allstat = [(EGG_EVO_SWEPS select 3)];
		_maxb = (count _allstat)-1;
		_stattype = _allstat select (round random _maxb);
		_mkr_name = format ["Antitank Nest %1", name _who];
		_custom = 1;
		_dist = 6;
		if NOT( ((primaryWeapon _who) in EGG_EVO_ATweapons) OR ((secondaryWeapon _who) in EGG_EVO_ATweapons) ) exitWith {hint "You cannot build an Antitank Nest without an Anti-Tank Rocket Launcher";_custom = 2};
	};
	 case 5: //MG Nest
	{
//		_allstat = ["M2StaticMG_US_EP1","M2HD_mini_TriPod_US_EP1","BAF_L2A1_Tripod_D","BAF_L2A1_Minitripod_D","BAF_GPMG_Minitripod_D"];
		_allstat = [(EGG_EVO_SWEPS select 1)];
		_maxb = (count _allstat)-1;
		_stattype = _allstat select (round random _maxb);
		_mkr_name = format ["MG Nest %1", name _who];
		_custom = 1;
		_dist = 4;
		if NOT( ((primaryWeapon _who) in EGG_EVO_MGweapons) OR ((secondaryWeapon _who) in EGG_EVO_MGweapons) ) exitWith {hint "You cannot build an MG Nest without a Heavy Machine Gun";_custom = 2};
	};
	 case 6: //Camo Net
	{
//		_stattype = "Land_CamoNetVar_NATO";
		_stattype = (EGG_EVO_SWEPS select 11);
		_dist = 6;
	};
	 case 7: //Refuel Depot
	{
//		_stattype = "Barrels";
		_stattype = (EGG_EVO_SWEPS select 9);
		_mkr_name = format ["Refuel %1", name _who];
		_custom = 1;
		_dist = 3;
		_zfactor = 3;
	};
	 case 8: //Mortar
	{
//		_stattype = "M252_US_EP1";
		_stattype = (EGG_EVO_SWEPS select 0);
		_mkr_name = format ["Mortar %1", name _who];
		_custom = 1;
		_dist = 3;
	};
	 case 9: //Foxhole
	{
		_allstat = [(EGG_EVO_SWEPS select 5)];
		_dist = 3;
		if (_playertype in EGG_EVO_playerOff) then {_allstat = [(EGG_EVO_SWEPS select 13)];_dist = 4};//modify for larger sandbag
		_maxb = (count _allstat)-1;
		_stattype = _allstat select (round random _maxb);
		_mkr_name = format ["Foxhole %1", name _who];
		_zfactor = 1;
	};
	 case 10: //Small stackable Sandbags
	{
		_stattype = (EGG_EVO_SWEPS select 8);
		_mkr_name = format ["Sandbags %1", name _who];
		_dist = 3;
		_zfactor = 1;
	};

};

switch (_custom) do
//manages deployment and deletion of statics
{
	 case 0: //Foxhole, bunker or camo net or hedgehog
	{
		if (!isNil "foxhole") then 
		{
			if ((alive foxhole) and (_statparam != 10)) then 
			{
				deletevehicle foxhole;
			};
		};
		Foxholeplaced = 0;
		foxhole = _stattype createVehicle [(getposATL _who select 0) + (sin(getdir _who) * _dist), (getposATL _who select 1) + (cos(getdir _who) * _dist)];
		_laction1 = _who addAction [("<t color='#855009'>" + "#Drop Equipment" + "</t>"),"actions\static\foxhole_placed.sqf"];
		_foxrotated =0;
		WHILE {(Foxholeplaced == 0) and (alive _who) and (vehicle _who == _who)} DO
		{
			_zvector = ((_who weaponDirection (primaryWeapon _who)) select 2) * 3;
			foxhole setposATL [(getposATL _who select 0) + (sin(getdir _who) * _dist), (getposATL _who select 1) + (cos(getdir _who) * _dist), (getposATL _who select 2) + _zvector + _zfactor];
//			if (_stattype == "Land_fort_bagfence_round") then 
//				{foxhole setDir (getDir _who)}
//			else
//				{
//				foxhole setDir ((getDir _who) -180);
//				};
//rotate bag if foxhole
			if (_statparam==9) then //foxhole
			{
//				if (_foxrotated ==0) then
//				{
				foxhole setDir ((getDir _who) -180);
//				_foxrotated =1;
			} else 
			{
				foxhole setDir (getDir _who);
			};
				sleep 0.05;
			//adding to fix rocket bug
			if not(vehicle _who == _who) then 
			{
				deletevehicle cstatA;
				StaticAplaced =1;
				_who setposATL [(getpos _who select 0), (getpos_who select 1),0];
			};
		};
		_who removeAction _laction1;
		foxhole addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	};
	 case 1: //Custom static based on class
	{
		if (!isNil "cstatA") then {
			if (alive cstatA) then {
				deletevehicle cstatA;
			};
		};
		
		StaticAplaced = 0;
		cstatA = _stattype createVehicle [(getposATL _who select 0) + (sin(getdir _who) * _dist), (getposATL _who select 1) + (cos(getdir _who) * _dist)];
		_laction1 = _who addAction [("<t color='#855009'>" + "#Drop Equipment" + "</t>"),"actions\static\staticA_placed.sqf"];

		WHILE {(StaticAplaced == 0) and (alive _who) and (vehicle _who == _who)} DO
		{
			_zvector = ((_who weaponDirection (primaryWeapon _who)) select 2) * 3;
			cstatA setposATL [(getposATL _who select 0) + (sin(getdir _who) * _dist), (getposATL _who select 1) + (cos(getdir _who) * _dist), (getposATL _who select 2) + _zvector + _zfactor];
			cstatA setDir (getDir _who);
			sleep 0.05;
//adding to fix rocket bug
			if not(vehicle _who == _who) then 
			{
				deletevehicle cstatA;
				StaticAplaced =1;
				_who setposATL [(getpos _who select 0), (getpos_who select 1),0];
			};
		};
		_who removeAction _laction1;

//enable ladder adjustment
		if(_statparam == 0 || _statparam == 1) then
		{
			_laction2 = cstatA addAction [("<t color='#855009'>" + "#Rotate ladder" + "</t>"),"actions\static\lad_switch.sqf",cstatA,1,false];
			cstatA addAction [("<t color='#855009'>" + "#Raise ladder" + "</t>"),"actions\static\lad_higher.sqf",0,1,false];
			cstatA addAction [("<t color='#855009'>" + "#Lower ladder" + "</t>"),"actions\static\lad_lower.sqf",0,1,false];
// After 30 sec, remove flip and removal, just keep raise and lower
			sleep 30;
			cstatA removeAction _laction2;
		};
		if((_statparam == 8) and (gitsnades > 0)) then
		{
			cstatA addMagazineTurret ["8Rnd_82mm_Mo_Smoke_white",[0]];
			cstatA addMagazineTurret ["8Rnd_82mm_Mo_Flare_white",[0]];
//			cstatA addMagazineTurret ["8Rnd_82mm_Mo_LG",[0]];
//			cstatA addMagazineTurret ["8Rnd_82mm_Mo_guided",[0]];
		};
/*
		if((_statparam == 2) and (gitsnades > 0)) then
		{
			cstatA addMagazineTurret ["EB_48Rnd_40mm_Chem_MK19",[0]];
		};
		if(_statparam == 8) then
		{
			cstatA addMagazineTurret ["ARTY_8Rnd_81mmHE_M252",[0]];
			cstatA addMagazineTurret ["ARTY_8Rnd_81mmWP_M252",[0]];
			cstatA addMagazineTurret ["ARTY_8Rnd_81mmILLUM_M252",[0]];
		};
*/

		if(_statparam != 0 and _statparam != 1 and _statparam != 6) then
		{
			if (!isNil "_marker2") then {
				deleteMarker _marker2;
			};
			
			_stat_pos = position cstatA;
			_marker2 = createMarker [_mkr_name, _stat_pos];
			_marker2 setMarkerShape "ICON";
			_marker2 setMarkerColor "ColorGreen";
			_marker2 setMarkerSize [0.5,0.5];
			_marker2 setMarkerType "Mil_Dot";
			_marker2 setMarkerText _mkr_name;
		//adding cost
			EGG_EVO_lunit = _who;
			EGG_EVO_lrew  = (0 - _scost);
			publicVariable "EGG_EVO_lrew";
			publicVariable "EGG_EVO_lunit";
			_txtpasshead = localize "str_cfg_cutscenes_dropweapon";//Dropping Weapon
			_txtpassbody = format["+%1 %2",(-_scost),localize "STR_M04t0_5"];//points
			["#0000FF",_txtpasshead,_txtpassbody] call EGG_EVO_Message;
		};
		cstatA addEventHandler ["killed", {handle = [_this select 0] execVM "scripts\bury.sqf"}];
	};

	 case 3: //Exit because tried to call a static weapon without the correct weapon
	{
		exit
	};

};

