
--------------------------------------------------------------------------------------------------''
--------------------------------------------------------------------------------------------------''
--oooooooooo.                  .oooooo..o             o8o                  .                      ''
--`888'   `Y8b                d8P'    `Y8             `"'                .o8                      ''
-- 888     888 oooo    ooo    Y88bo.      oo.ooooo.  oooo  ooo. .oo.   .o888oo  .ooooo.  oooo d8b ''
-- 888oooo888'  `88.  .8'      `"Y8888o.   888' `88b `888  `888P"Y88b    888   d88' `88b `888""8P ''
-- 888    `88b   `88..8'           `"Y88b  888   888  888   888   888    888   888ooo888  888     ''
-- 888    .88P    `888'       oo     .d8P  888   888  888   888   888    888 . 888    .o  888     ''
--o888bood8P'      .8'        8""88888P'   888boo8P' o888o o888o o888o   "888" `Y8bod8P' d888b    ''
--             .-..P'                      888                                                    ''
--             `Y8P'                      o888o                                  Copyright(c)2013 ''
--------------------------------------------------------------------------------------------------''
--------------------------------------------------------------------------Version 1.0-------------''

Sptool = {};
Dropinitialize = {}
Variabiles_Primary = {}
Group_flight = {}
name_pickup = {}
group_or_unit = {}
Diference_alt_oby = {}
Diference_alt_oby_min = {}
Diference_alt_oby_max = {}
set_time_advise_sound = {}
time_centre_to_hook = {}
radius_centre = {}
radius_centre_sound_direction = {}
radius_centre_message_advise = {}
name_f10_command = {}
time_advise_sound = {}
sim_cargo_mass_bool = {}
SpinterTiger1 = {}
Hook = {}
unit_flight = {}
unit_oby = {}
tipo = {}
test_Exist = {}
name_unit_life = {}
name_oby_life = {}
number_id = {}
id_scheduleFunction_player = {}
-- cargo
set_internal_cargo = {}
-- destination drop
destination_target = {}
name_destination = {}
-- smoke
smoke_drop = {}
smoke_destination = {}
smoke_color = {}
amount_drop = {}
max_amount_drop = {}
flag_drop_create = {}
info_message = {}
startup_message = {}



Hooked = {}
Hooked_on = {}

mix = {}
name_cargo = {}
alt_cargo_dest = {}
time_advise_sound_dest = {} 
freq_signal = {} 
type_country = {} 
completed = {} 

-- Default numbercargo = 10000
if numbercargo == nil then
 numbercargo = 10000	
end

--name_f10_command = 'Transport'               -- Name F10 List                              Default Transport
--Group_flight = 'New Helicopter Group'        -- You UH1-H Name Group                       Sample: New Helicopter Group
--name_pickup = 'New Trigger Zone'             -- Name pick up Trigger Zone                  Sample: New Trigger Zone
--name_destination = 'New Trigger Zone 2'      -- Name Trigger Zone                          Default New Trigger Zone 2
--radius_centre_sound_direction = 180          -- Radius to centre For Sound Direction       Default 180
--freq_signal = 32                             -- radioTransmission FM beacon  pickup  and destination is + 1  Default 32
--smoke_color = 1                              -- Smoke color 1 Green 2 Red 3 White 4 Orange 5 Blue   Default 1
--smoke_drop = false                           -- true smoke on loading area                 Default false
--smoke_destination = false                    -- true smoke on drop zone                    Default false
--set_internal_cargo = 300                     -- kl Cargo                                   Default 300
--max_amount_drop = 1
--flag_drop_create = nil        -- number flag to active to ends drops to zone destination   Default nil   nil = none active flag
--info_message =                               -- info  message Distance BRA  Wind           Default true
--startup_message = 'Here you enter your initial message important! do not use special characters!' 

 
 --completato
 --Destroyed

  --[[ function ]]--
  function getUnitPos(unitName)
  local unit = Unit.getByName(unitName)
  local unitPos = nil
  if unit ~= nil then
    unitPos = unit:getPosition()
  end
  return unitPos
  end

function getUnitID(unitName)
  local unit = Unit.getByName(unitName)
  local unitID = nil
  if unit ~= nil then
    unitID = unit:getID()
  end
  return unitID
end

function getStaticObjectPos(StaticObjectName)
  local unit = StaticObject.getByName(StaticObjectName)
  local unitPos = nil
  if unit then
  if unit ~= nil then
    unitPos = unit:getPosition()
  end
  end
  return unitPos
end

function getDistance(unit1, unit2,unit_or_static)
  local dist = nil
  local pos1 = getUnitPos(unit1)
  local pos2 = nil

  if unit_or_static == true then
  pos2 = getUnitPos(unit2)------
  else
  pos2 = getStaticObjectPos(unit2)------
  end

  if pos1 ~= nil and pos2 ~= nil then
    local px = pos1.p.x
	local py = pos1.p.y
    local pz = pos1.p.z
    local zx = pos2.p.x
	local zy = pos2.p.y
    local zz = pos2.p.z
    dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
  end
  return dist
end

function getAlt(unit1, unit2, alt,unit_or_static)
  local dist = nil
  local pos1 = getUnitPos(unit1)
  local pos2 = nil

  if unit_or_static == true then
  pos2 = getUnitPos(unit2)------
  else
  pos2 = getStaticObjectPos(unit2)------
  end

  if pos1 ~= nil and pos2 ~= nil then
    local px = pos1.p.x
	local py = pos1.p.y
    local pz = pos1.p.z
    local zx = pos2.p.x
	local zy = pos2.p.y
    local zz = pos2.p.z
	dist = (py - zy) - alt
  end
  return dist
end

function getBRA(unit1, unit2,unit_or_static)
  local BRA = nil
  local pos1 = getUnitPos(unit1)
  local pos2 = nil

  if unit_or_static == true then
  pos2 = getUnitPos(unit2)------
  else
  pos2 = getStaticObjectPos(unit2)------
  end

  if pos1 ~= nil and pos2 ~= nil then
    local px = pos1.p.x
    local pz = pos1.p.z
    local zx = pos2.p.x
    local zz = pos2.p.z

	local dx = zx - px
    local dz = zz - pz
    local dir = math.atan2( dz, dx ) * 180 / math.pi
    if dir < 0 then
      dir = 360 + dir
    end
	BRA = dir
  end
  return BRA
end

 function getmyBRA(unit1)
  local BRA = nil
  local pos1 = getUnitPos(unit1)
  if pos1 ~= nil then
    local px = pos1.p.x
    local pz = pos1.p.z
    local dir = math.atan2(pos1.x.z, pos1.x.x) * 180 / math.pi

    if dir < 0 then
      dir = 360 + dir
     end
	BRA = dir
  end
  return BRA
end


 function get_to(numero1, numero2, numero3)
  local onoff = nil
  if numero1 >= numero2 then
  if numero1 <= numero3 then
  to = true
  else
   to = false
  end
  end
  return to
end
-- message
 function message_to_group(id, mes)
  trigger.action.outTextForGroup(id, mes, 4)
 end

  --sound meter to distance distance
function sound_mt(mt, id_snd)
if mt >= 1 then
if mt < 4 then
trigger.action.outSoundForGroup(id_snd, 'Sound/Num/1.ogg')
end
end
if mt >= 4 then
if mt < 6 then
trigger.action.outSoundForGroup(id_snd, 'Sound/Num/2.ogg')
end
end
if mt >= 6 then
if mt < 8 then
trigger.action.outSoundForGroup(id_snd, 'Sound/Num/3.ogg')
end
end
if mt >= 8 then
if mt < 10 then
trigger.action.outSoundForGroup(id_snd, 'Sound/Num/4.ogg')
end
end
if mt >= 10 then
if mt < 13 then
trigger.action.outSoundForGroup(id_snd, 'Sound/Num/5.ogg')
end
end
end

 function get_name_unit(groupname)
   local units1 = Group.getByName(groupname):getUnits()
   local unit1name = nil
	if #units1 > 0 then
	unit1name = units1[1]:getName()
    end
	 return unit1name
  end
	
 function get_name_oby(si_no, unit)
	 local unit1name = nil
    if si_no == true then
      local units2 = Group.getByName(unit):getUnits()
	  
 	  if #units2 > 0 then
      local unit2name = units2[1]:getName()
	  unit1name = unit2name
      end
	  else
	  unit1name = unit
    end
 return unit1name
end
-------------------------------------destination------------------------------------------------------
-- ricavare distanza  trigger zone
function getDistance_destination(unit, trigger_zone)
  local dist = nil
  local pos1 = getUnitPos(unit)
  local pos2 = nil
  pos2 = trigger.misc.getZone(trigger_zone)
  if pos1 ~= nil and pos2 ~= nil then
    local px = pos1.p.x
	local py = pos1.p.y
    local pz = pos1.p.z
    local zx = pos2.point.x
	local zy = pos2.point.y
    local zz = pos2.point.z
    dist = math.sqrt( (px - zx)^2 + (pz - zz)^2 )
  end
  return dist
end
-- ricavare direzione  trigger zone
function getBRA_destination(unit, trigger_zone)
  local BRA = nil
  local pos1 = getUnitPos(unit)
  local pos2 = nil
  pos2 = trigger.misc.getZone(trigger_zone)
  if pos1 ~= nil and pos2 ~= nil then
  local dir = nil
    local px = pos1.p.x
    local pz = pos1.p.z
    local zx = pos2.point.x
    local zz = pos2.point.z

	local dx = zx - px
    local dz = zz - pz
    dir = math.atan2( dz, dx ) * 180 / math.pi
    if dir < 0 then
      dir = 360 + dir
    end
	BRA = dir
  end
  return BRA
end

-- ricavare altezza da elicottero a trigger zone
function getAlt_destination(unit1, alt, oby_alt)
  local dist = nil
  local pos1 = nil
  local pos1 = getUnitPos(unit1)
  if pos1 ~= nil then
	local py = pos1.p.y
	dist = (py - alt) - oby_alt
  end
  return dist
end

------------------------------
-- function split
------------------------------
function split(str, pat)
   local t = {}
   local fpat = "(.-)" .. pat
   local last_end = 1
   local s, e, cap = str:find(fpat, 1)
   while s do
          if s ~= 1 or cap ~= "" then
         table.insert(t,cap)
          end
          last_end = e+1
          s, e, cap = str:find(fpat, last_end)
   end
   if last_end <= #str then
          cap = str:sub(last_end)
          table.insert(t, cap)
   end
   return t
end
------------------------------
-- get Wind direction
------------------------------
function get_Wind(Location)
 local BRA = nil
 local wind = nil
 local pos = trigger.misc.getZone(Location)
 --local alt = land.getHeight(pos)
 local alt = land.getHeight({x = pos.point.x, y = pos.point.z})
Vec3 = { 
   x = pos.point.x, 
   y = alt, 
   z = pos.point.y
 } 
 if pos ~= nil then
    wind = atmosphere.getWind(Vec3)
    local dir = math.atan2(wind.z, wind.x) * 180 / math.pi

    if dir < 0 then
      dir = 360 + dir
     end
    BRA = dir
 end
BRA = string.format("\n Wind Direction: %d* Velocity: %d m/s", BRA, math.sqrt(wind.x^2 + wind.y^2 + wind.z^2))
return BRA
end

------------------------------
-- get id
------------------------------
function get_id(groupName)
    local i,j,k
    if env.mission.coalition then
        for coa,coaTable in pairs(env.mission.coalition) do
            if type(coaTable) == 'table' and coaTable.country then
                for i=1,#coaTable.country do
                    local country = coaTable.country[i]
                    for uType,uTable in pairs(country) do
                        if uType == 'helicopter' or uType == 'ship' or uType == 'vehicle' or uType == 'plane' then
                            if type(uTable)=='table' and uTable.group then
                                for j=1,#uTable.group do
                                    local group = uTable.group[j]
                                    if group.units then
                                        for k=1,#group.units do
                                            local unit = group.units[k]					
											if group.name and group.name == groupName then
                                                return unit.unitId
                                            end											 
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return nil
end
------------------------------
-- get country
------------------------------
function getCountry(groupName)
    local i,j,k
    if env.mission.coalition then
        for coa,coaTable in pairs(env.mission.coalition) do
            if type(coaTable) == 'table' and coaTable.country then
                for i=1,#coaTable.country do
                    local country = coaTable.country[i]
                    for uType,uTable in pairs(country) do
                        if uType == 'helicopter' or uType == 'ship' or uType == 'vehicle' or uType == 'plane' then
                            if type(uTable)=='table' and uTable.group then
                                for j=1,#uTable.group do
                                    local group = uTable.group[j]
                                    if group.units then
                                        for k=1,#group.units do
                                            local unit = group.units[k]
                                            -- if unit.name and unit.name == groupName then
                                                -- return group.name--country.name
                                            -- end
											 if group.name and group.name == groupName then
                                                return country.name
                                             end
                                        end
                                    end
                                end
                            end
                        end
                    end
                end
            end
        end
    end
    return nil
end

-------------------------------------------------------------------------------------------
DropZonesSmog = {
    [1] = {
        SmokeColor = trigger.smokeColor.Green,
    },
    [2] = {
        SmokeColor = trigger.smokeColor.Red,
    },
    [3] = {
        SmokeColor = trigger.smokeColor.White,
    },
	[4] = {
        SmokeColor = trigger.smokeColor.Orange,
    },
	[5] = {
        SmokeColor = trigger.smokeColor.Blue,
    },
}


------------------------------
-- clear_cargo
------------------------------
function clear_cargo(nome)
  --clear Cargo
 local Static = StaticObject.getByName(nome)
  if Static then 
  Static:destroy()
 end
end
------------------------------
-- Add cargo
------------------------------
function creo_cargo(numero,kl,nome,coal)
  --clear Cargo
 local Static = StaticObject.getByName(nome)
  if Static then 
  Static:destroy()
 end
 
 local pos = trigger.misc.getZone(nome)
-- local static = {
        -- ["type"] = "Cargo1",
        -- ["unitId"] = numero,
        -- ["y"] = pos.point.z,
        -- ["x"] = pos.point.x ,
		-- ["mass"] = kl,
        -- ["name"] = nome,
        -- ["category"] = "Cargos",
		-- ["canCargo"] = true,
        -- ["heading"] = 0,
    -- }
	local static = {
	["groupId"] = numero,
			["category"] = "Cargos",
			["type"] = "Cargo1",
			["unitId"] = numero,
			["x"] = pos.point.x,
			["y"] = pos.point.z,
			["canCargo"] = true,
			["mass"] = 500,
			["heading"] = 0,
			["name"] = nome,
			["dead"] = false,
			["country"] = "USA",
			["shape_name"] = "ab-212_cargo",
			} 	
coalition.addStaticObject(coal, static) 
numbercargo = numbercargo + 1  
end
 

 
 
 
 
 
 
 
 
 
 
------------------------------
-- Add soldier to Cargo
------------------------------

 function creo_soldier (numero,Location,country)
 local pos = trigger.misc.getZone(Location)
    local data = {
        ["visible"] = false,
        ["name"] = "Troop transport"..numero,
        ["task"] = {},
        ["hidden"] = false,
        ["units"] =
        {
            [1] =
            {
                ["y"] = pos.point.z + 1,
["type"] = "Soldier M249",
["name"] = "Unit23231 #1"..numero,
["unitId"] = numero,
["heading"] = 0,
["playerCanDrive"] = true,
["skill"] = "Excellent",
["x"] = pos.point.x + 1,
            }, -- end of [1]
        }, -- end of ["units"]
    }
coalition.addGroup(country, Group.Category.GROUND, data)
local bra = getBRA_destination('Unit23231 #1'..numero, Location)
--trigger.action.outText(getBRA_destination('Unit23231 #1"'..numero, Location), 20)	
	
local unit = Unit.getByName("Unit23231 #1"..numero)
  if unit then
  unit:destroy()
  end
	
creo_soldier2(numbercargo,Location,country,bra)
	numbercargo = numbercargo + 1 
 end
--"Infantry AK"   "Paratrooper AKS-74"  "Soldier M249"
-------------------------------------------------------------------------------------------
 

 function creo_soldier2 (numero,Location,country,dir)
 local pos = trigger.misc.getZone(Location)
    local data = {
        ["visible"] = false,
        ["name"] = "Troop transport"..numero,
        ["task"] = {},
        ["hidden"] = false,
        ["units"] =
        {
            [1] =
            {
                ["y"] = pos.point.z + 1,
["type"] = "Soldier M249",
["name"] = "Unit23231 #1"..numero,
["unitId"] = numero,
["heading"] = dir-20,
["playerCanDrive"] = true,
["skill"] = "Excellent",
["x"] = pos.point.x + 1,
            }, -- end of [1]
        }, -- end of ["units"]
    }
    coalition.addGroup(country, Group.Category.GROUND, data)
	numbercargo = numbercargo + 1 
 end
--"Infantry AK"   "Paratrooper AKS-74"  "Soldier M249"
-------------------------------------------------------------------------------------------

------------------------------
-- posizioni
------------------------------
     
function posizione_direzione (id,direzione_1,ely_dis,mes,visibile)
--avanti
  if direzione_1 > 316 then
  if direzione_1 < 360 then
  if time_advise_sound[id] == 1 then
  sound_mt(ely_dis, number_id[id])
  end
  if time_advise_sound[id] == 2 then
  trigger.action.outSoundForGroup(number_id[id], 'Sound/Forward.ogg')
  end
  if visibile == true then
  message_to_group(number_id[id], mes)
  end
  
  time_advise_sound[id] = time_advise_sound[id] + 1
  end
  end
  if direzione_1 >= 0 then
  if direzione_1 < 44 then
  if time_advise_sound[id] == 1 then
  sound_mt(ely_dis, number_id[id])
  end
  if time_advise_sound[id] == 2 then
  trigger.action.outSoundForGroup(number_id[id], 'Sound/Forward.ogg')
  end
  if visibile == true then
  message_to_group(number_id[id], mes)
  end
  time_advise_sound[id] = time_advise_sound[id] + 1
  end
  end
  --destra
  if direzione_1 >= 45 then
  if direzione_1 < 135 then
  if time_advise_sound[id] == 1 then
  trigger.action.outSoundForGroup(number_id[id], 'Sound/Right.ogg')
  end
  if time_advise_sound[id] == 2 then
  sound_mt(ely_dis, number_id[id])
  end
  if visibile == true then
  message_to_group(number_id[id], mes)
  end
  time_advise_sound[id] = time_advise_sound[id] + 1
  end
  end
  --indietro
  if direzione_1 >= 136 then
  if direzione_1 < 225 then
  if time_advise_sound[id] == 1 then
  trigger.action.outSoundForGroup(number_id[id], 'Sound/Back.ogg')
  end
  if time_advise_sound[id] == 2 then
  sound_mt(ely_dis, number_id[id])
  end
  if visibile == true then
  message_to_group(number_id[id], mes)
  end
  time_advise_sound[id] = time_advise_sound[id] + 1
  end
  end
 --sinistra
  if direzione_1 >= 226 then
  if direzione_1 < 315 then
  if time_advise_sound[id] == 1 then
  trigger.action.outSoundForGroup(number_id[id], 'Sound/Left.ogg')
  end
  if time_advise_sound[id] == 2 then
  sound_mt(ely_dis, number_id[id])
  end
  if visibile == true then
  message_to_group(number_id[id], mes)
  end
  time_advise_sound[id] = time_advise_sound[id] + 1
  end
  end
  if time_advise_sound[id] == set_time_advise_sound[id] then
  time_advise_sound[id] = 1
  end
end
 
-- .................................................................................
-- .........................--[[ end function ]]--..................................
-- .................................................................................

 
-- .................................................................................
-- .................................................................................
-------------------------Carico Nel menu la missione--------------------------------
-- .................................................................................
-- .................................................................................


Sptool.start = function(name_f10_command_var,Group_flight_var,name_pickup_var,name_destination_var,radius_centre_sound_direction_var,freq_signal_var,smoke_color_var,smoke_drop_var,smoke_destination_var,set_internal_cargo_var,max_amount_drop_var,flag_drop_create_var,info_message_var,briefing_drop_var)


if mix[1] == nil then
mix[1] = 1
end

local num = get_id(Group_flight_var)


if Dropinitialize[num] == nil then
 -- fa una volta sola
	    Dropinitialize[num] = true	
id_scheduleFunction_player[num] = nil	
end  

--type_country[mix[1]] = 2 --country.id.RUSSIA  0 country.id.USA 2  
 if getCountry(Group_flight_var) == nil then
 type_country[mix[1]] = country.id.USA
 elseif getCountry(Group_flight_var) == 'USA' then
 type_country[mix[1]] = country.id.USA
 elseif getCountry(Group_flight_var) == 'Russia' then
 type_country[mix[1]] = country.id.Russia
 else
 type_country[mix[1]] = country.id.USA
 end 

 
 
 --controllo altezza deposito destinazione
 --numbercargo = numbercargo + 1 
 creo_cargo(numbercargo,set_internal_cargo_var, name_destination_var ,type_country[mix[1]])
  local pos2 = nil
  pos2 = getStaticObjectPos(name_destination_var)------
  if  pos2 ~= nil then
    alt_cargo_dest[mix[1]] = pos2.p.y
  end
  clear_cargo(name_destination_var)

--creo umans
creo_soldier(numbercargo,name_pickup_var,type_country[mix[1]])--creo_soldier(numbercargo,name_pickup_var,country.id.USA,0)
--creo_soldier(numbercargo,name_destination_var,type_country[mix[1]])--creo_soldier(numbercargo,name_pickup_var,country.id.USA,0)
  
  
	
	
	
if freq_signal_var ~= nil then
-- modulation: 0  AM \\ 1  FM
-- freq: 119400000   119.4 MHz
local pos = trigger.misc.getZone(name_pickup_var)
Vec3 = { 
   x = pos.point.x, 
   y = pos.point.z, 
   z = pos.point.y 
 } 
trigger.action.radioTransmission("Sound/Beacon.wav", Vec3, 1, true, freq_signal_var * 1000000, 10000)
                                                     
local pos2 = trigger.misc.getZone(name_destination_var)
Vec33 = { 
   x = pos2.point.x, 
   y = pos2.point.z, 
   z = pos2.point.y 
 } 
trigger.action.radioTransmission("Sound/Beacon.wav", Vec33, 1, true, (freq_signal_var + 1) * 1000000, 10000)
 end
 

number_id[mix[1]] = num
name_f10_command[mix[1]] = name_f10_command_var	
Group_flight[mix[1]] = Group_flight_var
name_pickup[mix[1]] = name_pickup_var 
name_destination[mix[1]] =  name_destination_var
-- group_or_unit[mix[1]] = group_or_unit_var
-- Diference_alt_oby[mix[1]] = Diference_alt_oby_var
-- Diference_alt_oby_min[mix[1]] = Diference_alt_oby_min_var
-- Diference_alt_oby_max[mix[1]] = Diference_alt_oby_max_var
-- time_centre_to_hook[mix[1]] = time_centre_to_hook_var
-- radius_centre[mix[1]] = radius_centre_var
group_or_unit[mix[1]] = false      -- true = Sample UH1-H to Jeep    false = UH1-H to Static Object  Default true
Diference_alt_oby[mix[1]] = 2.5      -- height difference between UH1 to object    Default 2.5
Diference_alt_oby_min[mix[1]] = 3  -- minimum height for the attachment!         Default 3
Diference_alt_oby_max[mix[1]] = 7  -- maximum height for the attachment!         Default 7
time_centre_to_hook[mix[1]] = 5    -- Time Stop up target for look centre        Default 5
radius_centre[mix[1]] = 2          -- Radius to centre                           Default 2
set_time_advise_sound[mix[1]] = 4                -- questo non serve chiamarlo nelle funzioni
freq_signal[mix[1]] = freq_signal_var
radius_centre_sound_direction[mix[1]] = radius_centre_sound_direction_var
radius_centre_message_advise[mix[1]] = 500 -- radius_centre_message_advise_var   sostituito
amount_drop[mix[1]] = 0 -- questo non serve chiamarlo nelle funzioni
------------------------------
smoke_color[mix[1]] = smoke_color_var
smoke_drop[mix[1]] = smoke_drop_var
smoke_destination[mix[1]] = smoke_destination_var
------------------------------
set_internal_cargo[mix[1]] = set_internal_cargo_var
max_amount_drop[mix[1]] = max_amount_drop_var
flag_drop_create[mix[1]] = flag_drop_create_var
info_message[mix[1]] = info_message_var
startup_message[mix[1]] = briefing_drop_var
time_advise_sound[mix[1]] = 1


	  ---- create menu
	  ----  cancello menù F10
        -- trigger.action.removeOtherCommandForGroup(num,'Reset! Cancel!')
		-- missionCommands.addCommandForGroup(num, mix[1] .. ' ' .. name_f10_command_var, nil,GoWriteDrop, mix[1])
		-- missionCommands.addCommandForGroup(num,'Reset! Cancel!', nil,GoDelDrop, num)
		
		trigger.action.removeOtherCommandForGroup(num,'Yes Reset!')
		trigger.action.removeOtherCommandForGroup(num,'No Reset!')
		trigger.action.removeOtherCommandForGroup(num,'Reset! Cancel!')
	
		missionCommands.addCommandForGroup(num, mix[1] .. ' ' .. name_f10_command_var, nil,GoWriteDrop, mix[1])
		 
		missionCommands.addSubMenuForGroup(num, "Reset! Cancel!")
        missionCommands.addCommandForGroup(num, "Yes Reset!", {"Reset! Cancel!"}, GoDelDrop, num)
        missionCommands.addCommandForGroup(num, "No Reset!", {"Reset! Cancel!"}, GoDelDropno, num)		
 
		mix[1] = mix[1] + 1
		
end



-- .................................................................................
-- .................................................................................
-------------------------Lancio-----------------------------------------------------
-- .................................................................................
-- .................................................................................


Sptool.create_drop = function(Id_mix)

--................................Controllo se completato.......................................
   if completed[Id_mix] == true then -- uso id dell'eli
	trigger.action.outTextForGroup(number_id[Id_mix], 'Mission Complete!', 2)
	 GoDelDrop(number_id[Id_mix])
   return -- exit from the script
   end
--...............................................................................................

 
if Variabiles_Primary[Id_mix] == nil then


-- control for cancel cargo
if name_cargo[number_id[Id_mix]] ~= nil then
name_cargo[number_id[Id_mix]] = name_cargo[number_id[Id_mix]] .. '//' .. name_pickup[Id_mix]
else
name_cargo[number_id[Id_mix]] = name_pickup[Id_mix]
end

time_advise_sound[Id_mix] = 1
time_advise_sound_dest[Id_mix] = 1
sim_cargo_mass_bool[Id_mix] = 1
SpinterTiger1[Id_mix] = false
Hook[number_id[Id_mix]] = false -- uso id dell'eli
Variabiles_Primary[Id_mix] = 1
unit_flight[Id_mix] = nil
unit_oby[Id_mix] = nil
test_Exist[Id_mix] = nil
destination_target[Id_mix] = false
completed[Id_mix] = false 
	 
	-- get name unit and object
	unit_flight[Id_mix] = get_name_unit(Group_flight[Id_mix])
    unit_oby[Id_mix] = get_name_oby(group_or_unit[Id_mix], name_pickup[Id_mix])
	
	
		
 
 --------------------------- creo smog ----------------------------------------		
 -- smoke drop momentaneamente sospeso
 if smoke_drop[Id_mix] ==  true then
 	-- if group_or_unit[Id_mix] ==  true then
	  -- if getUnitPos(unit_oby[Id_mix]) then
      -- local smokepos1 = getUnitPos(unit_oby[Id_mix]) 
	  -- local smokepos31 = {x=smokepos1.p.x + 20, y=smokepos1.p.y, z=smokepos1.p.z + 20}
	  -- trigger.action.smoke(smokepos31, DropZonesSmog[smoke_color[Id_mix]].SmokeColor)
	  -- end
    -- end
	-- if group_or_unit[Id_mix] ==  false then
	  -- if getStaticObjectPos(unit_oby[Id_mix]) then
      -- local smokepos2 = getStaticObjectPos(unit_oby[Id_mix]) 
	  -- local smokepos32 = {x=smokepos2.p.x + 20, y=smokepos2.p.y, z=smokepos2.p.z + 20}
	  -- trigger.action.smoke(smokepos32, DropZonesSmog[smoke_color[Id_mix]].SmokeColor)
	  -- end
    -- end
 end
if smoke_destination[Id_mix] ==  true then
	local smokepos = trigger.misc.getZone(name_destination[Id_mix])
	local Landpos = { x=smokepos.point.x, y = smokepos.point.z }
	local smokealt = land.getHeight(Landpos)
	local smokepos3 = {x=smokepos.point.x, y=smokealt, z=smokepos.point.z}
	trigger.action.smoke(smokepos3, DropZonesSmog[smoke_color[Id_mix]].SmokeColor)
end
---------------------------fine creo smog ----------------------------------------

 --message briefing
 trigger.action.outTextForGroup(number_id[Id_mix], startup_message[Id_mix], 25)
end -- end Variabiles_Primary --os.exit()
-----------------------------------------------------------

 
-- .................................................................................
-- .................................................................................
--avviso agganciato
-- .................................................................................
-- .................................................................................
  
 
if StaticObject.getByName(unit_oby[Id_mix]) then
Hooked[Id_mix]= true 
--Hook[number_id[Id_mix]] = false -- uso id dell'eli
else
Hooked[Id_mix]= false 
destination_target[Id_mix] = true
end

 --controllo se e agganciato e passo a scaricare da controllare
if Hooked[Id_mix] == true then

if group_or_unit[Id_mix] == true then
tipo[Id_mix] = true
else
tipo[Id_mix] = false
end

local ely_mia_direzione = getmyBRA(unit_flight[Id_mix])
local ely_direzione_oby = getBRA(unit_flight[Id_mix], unit_oby[Id_mix],tipo[Id_mix])
local ely_distanza_oby = getDistance(unit_flight[Id_mix], unit_oby[Id_mix],tipo[Id_mix])
local ely_diff_alt = getAlt(unit_flight[Id_mix], unit_oby[Id_mix], Diference_alt_oby[Id_mix],tipo[Id_mix])

local mex1 = string.format("\%.0f",ely_direzione_oby)
local mex2 = string.format("\%.0f",ely_distanza_oby)
local mex3 = string.format("\%.0f",ely_diff_alt)
local mex4 = ' Direct to Cargo: ' .. mex1  .. '* Distance: ' .. mex2 .. ' Alt: ' .. mex3 .. get_Wind(name_pickup[Id_mix])


 

local dir_1 = 360
  
 -- avviso che sto' entrando in area
 if ely_distanza_oby < radius_centre_message_advise[Id_mix] then
 if SpinterTiger1[Id_mix] == false then
	   --message_to_group(number_id[Id_mix], 'Within the target area!')
	   SpinterTiger1[Id_mix] = true	   
end
end
 -- avviso che sto' uscendo dall'area
  if ely_distanza_oby > radius_centre_message_advise[Id_mix] then
  if SpinterTiger1[Id_mix] == true then
	   --message_to_group(number_id[Id_mix], 'Out of the target area')
	   SpinterTiger1[Id_mix] = false
  end
  end
  
  
  -- gradi my flight to oby
if ely_direzione_oby >= ely_mia_direzione then
    dir_1 = ely_direzione_oby - ely_mia_direzione
else
    dir_1 = 360 - ely_mia_direzione + ely_direzione_oby
end

 -- limitazione al centro
 if ely_distanza_oby < radius_centre[Id_mix] then -- 3
 
 
 if info_message[Id_mix] == true then
  message_to_group(number_id[Id_mix], mex4)
 end

  if destination_target[Id_mix] == false then
  if ely_diff_alt < Diference_alt_oby_min[Id_mix] then -- 4
   dir_1 = 600
  trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Up.ogg')
  end
   if ely_diff_alt > Diference_alt_oby_max[Id_mix] then --8
   dir_1 = 600
  trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Down.ogg')
  end
  end

  
  -- control  alt 
if  get_to(ely_diff_alt,Diference_alt_oby_min[Id_mix],Diference_alt_oby_max[Id_mix]) then -- 4 8
    dir_1 = 600
if sim_cargo_mass_bool[Id_mix] > time_centre_to_hook[Id_mix] then -- 3
else


if get_to(time_advise_sound[Id_mix], 1, 3)then
trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Inposition.ogg')
time_advise_sound[Id_mix] = time_advise_sound[Id_mix] + 1
end
sim_cargo_mass_bool[Id_mix] = sim_cargo_mass_bool[Id_mix] + 1
end
  else
  sim_cargo_mass_bool[Id_mix] = 1
end
  else
  sim_cargo_mass_bool[Id_mix] = 1
end


   --zona x
if ely_distanza_oby > radius_centre_sound_direction[Id_mix] then -- 150
dir_1 = 600
end

------------------------------
-- posizioni
------------------------------
posizione_direzione (number_id[Id_mix],dir_1,ely_distanza_oby,mex4,info_message[Id_mix])
end -- if Hooked[Id_mix] == true then




------------------------------------------------------------
------------------------------------------------------------
---------- Avviso l'avvenuto agganciamento
------------------------------------------------------------
------------------------------------------------------------

if StaticObject.getByName(unit_oby[Id_mix]) then
Hooked_on[Id_mix]= true 
else
if Hooked_on[Id_mix] == true then
Hooked_on[Id_mix]= false 
local tabCommand = {
id = "TransmitMessage",
params = {
subtitle = "                                                                       Hooked!",
duration = 2,
loop = false,
file = "Sound/hook.ogg",
}
}
local objGroup = Unit.getByName(unit_flight[Id_mix]):getController()
Controller.setCommand(objGroup, tabCommand)
Hook[number_id[Id_mix]] = true -- uso id dell'eli
end
end
 
 
 
 
------------------------------------------------------------
------------------------------------------------------------
---------- direzione trasporto destinazione
------------------------------------------------------------
------------------------------------------------------------

--controllo se scarico o rompo
if destination_target[Id_mix] == true then
local cargovivo = StaticObject.getByName(unit_oby[Id_mix])    
if cargovivo then
local startingcargovivoLife = cargovivo:getLife()
if startingcargovivoLife == 0 then
Hook[Id_mix] = false -- uso id dell'eli
clear_cargo(name_pickup[Id_mix])
GoDelDrop(number_id[Id_mix])
end
end




dir_1 = 600
local posizione_trigger = trigger.misc.getZone(name_destination[Id_mix])
local mex11 = string.format("\%.0f",getBRA_destination(unit_flight[Id_mix], name_destination[Id_mix]))
local mex22 = string.format("\%.0f",getDistance_destination(unit_flight[Id_mix], name_destination[Id_mix]))
local mex33 = (' Direct to Destination Release ' .. mex11  .. '* Distance: ' .. mex22..get_Wind(name_destination[Id_mix]))

if getDistance_destination(unit_flight[Id_mix], name_destination[Id_mix]) > posizione_trigger.radius then


-- audio direzione scaricare
if getDistance_destination(unit_flight[Id_mix], name_destination[Id_mix]) > radius_centre_sound_direction[Id_mix] then
if info_message[Id_mix] == true then
message_to_group(number_id[Id_mix], mex33)
end
else
local ely_mia_direzione2 = getmyBRA(unit_flight[Id_mix])
local ely_direzione_oby2 = getBRA_destination(unit_flight[Id_mix], name_destination[Id_mix])  
local ely_distanza_oby2 = getDistance_destination(unit_flight[Id_mix], name_destination[Id_mix])
local dir_2 = 360
  -- gradi my flight to oby
if ely_direzione_oby2 >= ely_mia_direzione2 then
    dir_2 = ely_direzione_oby2 - ely_mia_direzione2
else
    dir_2 = 360 - ely_mia_direzione2 + ely_direzione_oby2
end
posizione_direzione (number_id[Id_mix],dir_2,ely_distanza_oby2,mex33,info_message[Id_mix])
end


-- controllo se  sgancio fuori zona
local cargovivo1 = StaticObject.getByName(unit_oby[Id_mix])    
if cargovivo1 then
local startingcargovivoLife1 = cargovivo1:getLife()
if startingcargovivoLife1 == 5 then
 destination_target[Id_mix] = false
 --Hook[number_id[Id_mix]] = false -- uso id dell'eli
end
end
else
message_to_group(number_id[Id_mix], 'Destination ok!')

  --    suono raggiungimento dest
  if getAlt_destination(unit_flight[Id_mix], alt_cargo_dest[Id_mix], Diference_alt_oby[Id_mix]) > Diference_alt_oby_max[Id_mix] then
  if time_advise_sound_dest[Id_mix] == 6 then
  time_advise_sound_dest[Id_mix] = 1
  end
  if time_advise_sound_dest[Id_mix] == 1 then
  trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Down.ogg')
   time_advise_sound_dest[Id_mix] = time_advise_sound_dest[Id_mix] + 1
  end
  
  if time_advise_sound_dest[Id_mix] == 3 then
  trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Down.ogg')
   time_advise_sound_dest[Id_mix] = time_advise_sound_dest[Id_mix] + 1
   else
   time_advise_sound_dest[Id_mix] = time_advise_sound_dest[Id_mix] + 1
  end
  end --    suono raggiungimento dest
 

-- quantità per creare qualche cosa
local cargovivo = StaticObject.getByName(name_pickup[Id_mix])    
if cargovivo then
local startingcargovivoLife = cargovivo:getLife()
--trigger.action.outText(startingcargovivoLife, 2)
if startingcargovivoLife == 5 then
--Hook[number_id[Id_mix]] = false -- uso id dell'eli
amount_drop[Id_mix] =  amount_drop[Id_mix] + 1

--aumenta flag per eventuali attivazioni
if flag_drop_create[Id_mix]~= nil then 
trigger.action.setUserFlag(flag_drop_create[Id_mix], amount_drop[Id_mix])
end
Hook[number_id[Id_mix]] = false -- uso id dell'eli
message_to_group(number_id[Id_mix], 'Transport Remaining: -' .. max_amount_drop[Id_mix] - amount_drop[Id_mix])
clear_cargo(name_pickup[Id_mix])
GoDelDrop(number_id[Id_mix])
end
--controllo se scarico o rompo
if startingcargovivoLife == 0 then
Hook[number_id[Id_mix]] = false -- uso id dell'eli
message_to_group(number_id[Id_mix], 'Cargo Destroyed Transport Remaining: -' .. max_amount_drop[Id_mix] - amount_drop[Id_mix])
clear_cargo(name_pickup[Id_mix])
GoDelDrop(number_id[Id_mix])
end
end
--end--getAlt_destination


if amount_drop[Id_mix] >= max_amount_drop[Id_mix] then
Hook[number_id[Id_mix]] = false -- uso id dell'eli
message_to_group(number_id[Id_mix], 'Carriage performed sucessfully!')
trigger.action.outSoundForGroup(number_id[Id_mix], 'Sound/Success.ogg')
amount_drop[Id_mix] =  0
completed[Id_mix] = true 
 -- --clear Cargo
 -- local Static = StaticObject.getByName(name_pickup[Id_mix])
  -- if Static then 
  -- Static:destroy()
  -- end 
-- cancello menù F10
--trigger.action.removeOtherCommandForGroup(number_id[Id_mix], number_id[Id_mix] .. ' ' .. name_f10_command[Id_mix])
-- trigger.action.removeOtherCommandForGroup(number_id[Id_mix], Id_mix .. ' ' .. name_f10_command[Id_mix])

 GoDelDrop(number_id[Id_mix])
end
 -- end quantità per creare qualche cosa
end


end -- end destination_target[Id_mix] == true then
end -- end Sptool.create_drop






 


----------------------------------------------------------------------------
----------------------------------------------------------------------------
---------------------------------Timer--------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------

function GoWriteDrop(number_mix)
 if Hook[number_id[number_mix]] == true then -- uso id dell'eli
  return -- exit from the script and return to the caller
 end
--  Cancello Ricreo Cargo
  GoDelDrop(number_id[number_mix])
 -- DestroyDrop(number_id[number_mix])
 
--name_cargo[number_id[number_mix]]=''
creo_cargo(numbercargo,set_internal_cargo[number_mix],name_pickup[number_mix],type_country[number_mix]) 

local cargoex = StaticObject.getByName(name_pickup[number_mix])    
if cargoex then
 Sptool.clear(number_mix)
 if id_scheduleFunction_player[number_id[number_mix]] ~= nil then
  timer.removeFunction(id_scheduleFunction_player[number_id[number_mix]])
  end
id_scheduleFunction_player[number_id[number_mix]] = timer.scheduleFunction(test_controllo, number_mix, timer.getTime() + 1)
end
--end
end

function GoDelDrop(num)
 -- if Hook[number_id[num]] == true then -- uso id dell'eli
  -- return -- exit from the script and return to the caller
 -- end
  Hook[num] = false -- uso id dell'eli
  DestroyDrop(num)
  --trigger.action.outTextForGroup(number_id[num], '', 0)
  
  if id_scheduleFunction_player[num] ~= nil then
  timer.removeFunction(id_scheduleFunction_player[num])
  id_scheduleFunction_player[num] = nil
  end
end
-- destroy cargo
function DestroyDrop(num)
if name_cargo[number_id[num]] ~= nil then
 local splitTable = split(name_cargo[number_id[num]],'//')
 for i=1, #splitTable do
 local Static = StaticObject.getByName(splitTable[i])
  if Static then 
  Static:destroy()
  end
 end
 end
end

--funzione del cancel menu drop
function GoDelDropno(num)
--trigger.action.outText('No', 5)
end


function test_controllo(numero, time)--Id_mix
 

	 -- test is exist
	if test_Exist[numero] == nil then
	
	if Group.getByName(Group_flight[numero])then
	   else
	   test_Exist[numero] = false
    end 
	if group_or_unit[numero] == true then
	if Group.getByName(name_pickup[numero])then
        else
	   test_Exist[numero] = false
    end 
	else
	if StaticObject.getByName(name_pickup[numero])then
      else
	   test_Exist[numero] = false
    end 
	end
	end -- if test_Exist[numero] == nil then
	
	
	
	 if test_Exist[numero] == nil then
	 test_Exist[numero] = true
	 name_unit_life[numero] = get_name_unit(Group_flight[numero])
	 name_oby_life[numero] = get_name_oby(group_or_unit[numero], name_pickup[numero])	
	 end
	 
	 if test_Exist[numero] == true then
	 -- re test_Exist
	 if group_or_unit[numero] == true then
	 if Unit.getByName(name_unit_life[numero]) and Unit.getByName(name_oby_life[numero])then
	  --trigger.action.outText('1', 2) 
	 -----------------------------------------------------------------------------------------------------------------
	 Sptool.create_drop(numero)---------------------------------------------------------------------------------------
	 -----------------------------------------------------------------------------------------------------------------
	 else
	 --trigger.action.outText('3', 2) 
      if id_scheduleFunction_player[number_id[numero]] ~= nil then
       timer.removeFunction(id_scheduleFunction_player[number_id[numero]])
	   id_scheduleFunction_player[number_id[numero]] = nil
      end
	 Sptool.clear(numero)
	 
	  
	 end
	 elseif  group_or_unit[numero] == false then
	 --if Unit.getByName(name_unit_life[numero]) and StaticObject.getByName(name_oby_life[numero])then
	 if Unit.getByName(name_unit_life[numero]) then
	  --trigger.action.outText('2', 2) 
	 -----------------------------------------------------------------------------------------------------------------
	 Sptool.create_drop(numero)---------------------------------------------------------------------------------------
	 -----------------------------------------------------------------------------------------------------------------
	 else
	  --trigger.action.outText('4', 2) 
	 
	  if id_scheduleFunction_player[number_id[numero]] ~= nil then
      timer.removeFunction(id_scheduleFunction_player[number_id[numero]])
	  id_scheduleFunction_player[number_id[numero]] = nil
      end
	  Sptool.clear(numero)
	 end
	 end -- end  if group_or_unit[numero] = true then
	 -- end test_Exist
	 end -- end if test_Exist[numero] == nil then
	 
	 
	 
	 if test_Exist[numero] == false then
	  if id_scheduleFunction_player[number_id[numero]] ~= nil then
      timer.removeFunction(id_scheduleFunction_player[number_id[numero]])
	  id_scheduleFunction_player[number_id[numero]] = nil
      end
	  Sptool.clear(numero)
	  trigger.action.outText('Not Exist Flight or Object !', 20) 
	 end
	  
	return time + 1 
end

 Sptool.clear = function(num)
    Variabiles_Primary[num] = nil
	-- trigger.action.outText('Clear2', 20)
end






