-- DAWS Package file B / Ground infantry carrier


-- THANKS TO (cronological order):
	-- Rider (AMVI)
	-- Grimes (ED community)
	-- MBot (ED community)
	-- xcom (ED community)
	-- Ian (ED community)
	-- vicx (ED community)
	-- Pikey (ED community)
	-- ESAC_Matador (ED community)

	
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################

	-- local function copy
	local base 						= _G
	local require 					= base.require
	local table 					= base.table
	local math 						= base.math
	local string					= base.string
	local pairs 					= base.pairs
	local ipairs 					= base.ipairs
	local print 					= base.print
	local io 						= base.io
	local lfs 						= base.lfs
	local os 						= base.os
	local zip 						= minizip
	local loadstring 				= base.loadstring
	--]]--
	
	-- common directory listing
	local mistdirectory 			= mistdirectoryDP -- lfs.currentdir() .. "AMVI/Common/Code/"
	local moddirectory 				= moddirectoryAE 	-- lfs.currentdir() .. "AMVI/AIEnhancement/"
	local debugdirectory  			= debugdirectoryAE	--lfs.currentdir() .. "AMVI/AIEnhancement/Debug/"
	local missionfilesdirectory 	= missionfilesdirectoryDP -- lfs.writedir() .. "Missions/"
	local logsdirectory				= logsdirectoryDP -- lfs.writedir() .. "Logs/"
	
	-- common export variables
	local exportfiletype 			= ".csv"
	--reportfiletype 				= ".doc"
	local configfiletype 			= ".csv"
	local tss 						= ";"
	
	--##### INTERNAL VARIABLES
	local debugProcess 				= false -- print information log in dcs.log
	local useDismountedUnits		= false

	--improved random
	do
	   local oldrandom = math.random
	   local randomtable
	   math.random = function ()
		  if randomtable == nil then
			 randomtable = {}
			 for i = 1, 97 do
				randomtable[i] = oldrandom()
			 end
		  end
		  local x = oldrandom()
		  local i = 1 + math.floor(97*x)
		  x, randomtable[i] = randomtable[i], x
		  return x
	   end
	end	

-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################

	
	--Table that holds all ground units that carry mounted units
	local DismountsCarrier = {}
	if debugProcess == true then
		env.info(("DAWS AIE  -  EA_infantry_carrier is loading..."))
	end					
	
	-- ADD HERE THE CODE TO RETRIEVE THE DAWS_mountedInfantryTable_SM table.				
	if DAWS_mountedInfantryTable_SM then
		if table.getn(DAWS_mountedInfantryTable_SM) > 0 then
			if debugProcess == true then
				env.info(("DAWS AIE  -  EA_infantry_carrier, DAWS_SM_mountedInfantry is a valid and populated table"))
			end
			
			for DMTid, DMTdata in pairs(DAWS_mountedInfantryTable_SM) do
				DMTdata.cargo_status = "mounted"
			end
			
			DismountsCarrier = DAWS_mountedInfantryTable_SM
			useDismountedUnits = true
			if debugProcess == true then
				DAWS.dumpTable("DAWS_mountedInfantryTable_SM_AIE.lua", DAWS_mountedInfantryTable_SM)									
			end					
		else	
			if debugProcess == true then
				DAWS.dumpTable("DAWS_mountedInfantryTable_SM_AIE.lua", DAWS_mountedInfantryTable_SM)									
			end				
		end
	end
	
	--Function to add mounted units to a carrier
	function AddDismounts(UnitName, dm_type)	--Options for dm_type: "MANPADS", "Mortar", "Rifle", "ZU-23"
		DismountsCarrier[#DismountsCarrier + 1] = {
			name = UnitName,
			countryID = Unit.getByName(UnitName):getCountry(),
			UnitID = Unit.getByName(UnitName):getID(),
			cargo = dm_type,
			cargo_status = "mounted",
			cargo_current = nil --- added now 
			--cargo_creation_time = timer.getAbsTime() -- placeholder for the future: set time the infantry gets into the carrier.
			--cargo_expiration_time = timer.getAbsTime() + 60*60*24*3 -- placeholder for the future: every three days of campaign the cargo gets resetted
		}
	end			
		
	--Function go get a unit heading
	local function GetHeading(Pos3)
		if (Pos3.x.x > 0) and (Pos3.x.z == 0) then
			return 0
		elseif (Pos3.x.x > 0) and (Pos3.x.z > 0) then
			return math.atan(Pos3.x.z / Pos3.x.x)
		elseif (Pos3.x.x == 0) and (Pos3.x.z > 0) then
			return math.rad(90)
		elseif (Pos3.x.x < 0) and (Pos3.x.z > 0) then
			return math.rad(90) - math.atan(Pos3.x.x / Pos3.x.z)
		elseif (Pos3.x.x < 0) and (Pos3.x.z == 0) then
			return math.rad(180)
		elseif (Pos3.x.x < 0) and (Pos3.x.z < 0) then
			return math.rad(180) + math.atan(Pos3.x.z / Pos3.x.x)
		elseif (Pos3.x.x == 0) and (Pos3.x.z < 0) then
			return math.rad(270)
		elseif (Pos3.x.x > 0) and (Pos3.x.z < 0) then
			return math.rad(270) - math.atan(Pos3.x.x / Pos3.x.z)
		end
	end			
	
	--Repeating function to steer dismounted rifle squads
	local function SetRifleWaypoint(CarrierUnitName, DMGroupName)
		local function ScheduledFunction()
			local rifle = Group.getByName(DMGroupName)
			if rifle ~= nil then
				local rifle_leader = rifle:getUnit(1)
				if rifle_leader ~= nil then
					local rifle_leader_point = rifle_leader:getPoint()	--Get current position of the rifle leader for the first waypoint
					local rifle_controller = rifle:getController()	--Get controller of the rifle group for the waypoint task
					local carrier = Unit.getByName(CarrierUnitName)
					if carrier ~= nil then
						local carrier_pos = carrier:getPosition()	--Get current position of the carrier for the second waypoint (100m in front of carrier)
						GoToTask = { 
							id = 'Mission', 
							params = { 
								route = { 
									points = { 
										[1] = {
											action = "Custom",
											x = rifle_leader_point.x,	--Current position of rifle leader
											y = rifle_leader_point.z,	--Current position of rifle leader
											speed = 3.8888888888889,
											ETA = 0,
											ETA_locked = false,
											name = "", 
											task = {
												["id"] = "ComboTask",
												["params"] = 
												{
													["tasks"] = 
													{
													}, -- end of ["tasks"]
												}, -- end of ["params"]
											},
										},
										[2] = {
											action = "Custom",
											x = carrier_pos.p.x + carrier_pos.x.x * 100,	--100m in front of carrier
											y = carrier_pos.p.z + carrier_pos.x.z * 100,	--100m in front of carrier
											speed = 3.8888888888889,
											ETA = 0,
											ETA_locked = false,
											name = "", 
											task = {
												["id"] = "ComboTask",
												["params"] = 
												{
													["tasks"] = 
													{
													}, -- end of ["tasks"]
												}, -- end of ["params"]
											}
										}
									} 
								}
							} 
						}
						Controller.setTask(rifle_controller, GoToTask)
						return timer.getTime() + 15	--Repeat after 15 seconds, until rifle_leader is not existing anymore
					end
				end
			end
		end
		timer.scheduleFunction(ScheduledFunction, nil, timer.getTime() + 15)
	end			
	
	--Function to return the composition of the spawned group
	local function GetDmGroup(countryID, carrierUnitID, carrierPos, dmType, CarrierUnitName)
		local dmVec2 = {									--Determine the x,y Vec2 position of the dismounts (10m behind of the carrier)
			x = carrierPos.p.x + carrierPos.x.x * -10,
			y = carrierPos.p.z + carrierPos.x.z * -10,
		}
		local heading = GetHeading(carrierPos)	--Get heading of the carrier when dismounting
		if countryID == 0 or countryID == 1 or countryID == 16 or countryID == 17 or countryID == 18 or countryID == 19 then	--If eastern country					
			if dmType == "MANPADS" then 	--If MANPADS
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "SA-18 Igla-S manpad",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 10000,
							["heading"] = 0,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y,
							["type"] = "SA-18 Igla-S comm",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 11000,
							["heading"] = 0,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + 3,
						}, -- end of [2]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group 
				
			elseif dmType == "Mortar" then 	--If Mortar
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "2B11 mortar",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 12000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y -2,
							["type"] = "Infantry AK",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 13000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + 2,
						}, -- end of [2]
						[3] = 
						{
							["y"] = dmVec2.y -2,
							["type"] = "Infantry AK",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_03",
							["unitId"] = carrierUnitID + 14000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - 2,
						}, -- end of [3]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group
			elseif dmType == "Rifle" then 	--If rifle squad
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 3.8888888888889,
								["action"] = "Custom",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
							[2] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = carrierPos.p.z + carrierPos.x.z * 100,
								["x"] = carrierPos.p.x + carrierPos.x.x * 100,
								["ETA_locked"] = true,
								["speed"] = 3.8888888888889,
								["action"] = "Custom",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [2]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "Infantry AK",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 15000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 5,
							["type"] = "Paratrooper RPG-16",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 16000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 5,
						}, -- end of [2]
						[3] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 5,
							["type"] = "Infantry AK",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_03",
							["unitId"] = carrierUnitID + 17000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 5,
						}, -- end of [3]
						[4] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 10,
							["type"] = "Infantry AK",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_04",
							["unitId"] = carrierUnitID + 18000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 10,
						}, -- end of [4]
						--[[--
						[5] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 10,
							["type"] = "Infantry AK",
							["name"] = "Dismounts_" .. carrierUnitID .. "_05",
							["unitId"] = carrierUnitID + 19000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 10,
						}, -- end of [5]
						[6] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 15,
							["type"] = "Infantry AK",
							["name"] = "Dismounts_" .. carrierUnitID .. "_06",
							["unitId"] = carrierUnitID + 20000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 15,
						}, -- end of [6]
						[7] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 15,
							["type"] = "Infantry AK",
							["name"] = "Dismounts_" .. carrierUnitID .. "_07",
							["unitId"] = carrierUnitID + 21000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 15,
						}, -- end of [7]
						--]]--
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				SetRifleWaypoint(CarrierUnitName, "Xcl_Dismounts_" .. carrierUnitID)	--Launch scheduled function to refresh the wayoint of the dismounted group
				return group
			elseif dmType == "ZU-23" then 	--If ZU-23 AAA
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "ZU-23 Emplacement",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 22000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group
			end

		else	--if western country
			if dmType == "MANPADS" then	--If MANPADS
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "Stinger manpad",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 23000,
							["heading"] = 0,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y,
							["type"] = "Stinger comm",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 24000,
							["heading"] = 0,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + 3,
						}, -- end of [2]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group
			elseif dmType == "Mortar" then 	--If Mortar
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "2B11 mortar",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 25000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y -2,
							["type"] = "Soldier M4",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 26000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + 2,
						}, -- end of [2]
						[3] = 
						{
							["y"] = dmVec2.y -2,
							["type"] = "Soldier M4",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_03",
							["unitId"] = carrierUnitID + 27000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - 2,
						}, -- end of [3]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group
			elseif dmType == "Rifle" then 	--If rifle squad
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 3.8888888888889,
								["action"] = "Custom",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
							[2] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = carrierPos.p.z + carrierPos.x.z * 100,
								["x"] = carrierPos.p.x + carrierPos.x.x * 100,
								["ETA_locked"] = true,
								["speed"] = 3.8888888888889,
								["action"] = "Custom",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [2]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "Soldier M4",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 28000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
						[2] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 5,
							["type"] = "Soldier M4",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_02",
							["unitId"] = carrierUnitID + 29000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 5,
						}, -- end of [2]
						[3] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 5,
							["type"] = "Soldier M4",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_03",
							["unitId"] = carrierUnitID + 30000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 5,
						}, -- end of [3]
						[4] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 10,
							["type"] = "Soldier M249",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_04",
							["unitId"] = carrierUnitID + 31000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 10,
						}, -- end of [4]
						--[[--
						[5] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 10,
							["type"] = "Soldier M249",
							["name"] = "Dismounts_" .. carrierUnitID .. "_05",
							["unitId"] = carrierUnitID + 32000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 10,
						}, -- end of [5]
						[6] = 
						{
							["y"] = dmVec2.y - carrierPos.z.z * 15,
							["type"] = "Soldier M4",
							["name"] = "Dismounts_" .. carrierUnitID .. "_06",
							["unitId"] = carrierUnitID + 33000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x - carrierPos.z.x * 15,
						}, -- end of [6]
						[7] = 
						{
							["y"] = dmVec2.y + carrierPos.z.z * 15,
							["type"] = "Soldier M4",
							["name"] = "Dismounts_" .. carrierUnitID .. "_07",
							["unitId"] = carrierUnitID + 34000,
							["heading"] = heading,
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x + carrierPos.z.x * 15,
						}, -- end of [7]
						--]]--
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				SetRifleWaypoint(CarrierUnitName, "Xcl_Dismounts_" .. carrierUnitID)	--Launch scheduled function to refresh the wayoint of the dismounted group
				return group
			elseif dmType == "ZU-23" then 	--If ZU-23 AAA
				local group = {
					["visible"] = false,
					["route"] = 
					{
						["spans"] = 
						{
						}, -- end of ["spans"]
						["points"] = 
						{
							[1] = 
							{
								["alt"] = 0,
								["type"] = "Turning Point",
								["ETA"] = 0,
								["alt_type"] = "BARO",
								["formation_template"] = "",
								["y"] = dmVec2.y,
								["x"] = dmVec2.x,
								["ETA_locked"] = true,
								["speed"] = 5.5555555555556,
								["action"] = "Off Road",
								["task"] = 
								{
									["id"] = "ComboTask",
									["params"] = 
									{
										["tasks"] = 
										{
										}, -- end of ["tasks"]
									}, -- end of ["params"]
								}, -- end of ["task"]
								["speed_locked"] = true,
							}, -- end of [1]
						}, -- end of ["points"]
					}, -- end of ["route"]
					["groupId"] = carrierUnitID + 10000,
					["tasks"] = 
					{
					}, -- end of ["tasks"]
					["hidden"] = false,
					["units"] = 
					{
						[1] = 
						{
							["y"] = dmVec2.y,
							["type"] = "ZU-23 Emplacement",
							["name"] = "Xcl_Dismounts_" .. carrierUnitID .. "_01",
							["unitId"] = carrierUnitID + 35000,
							["heading"] = heading + math.rad(180),
							["playerCanDrive"] = true,
							["skill"] = "Average",
							["x"] = dmVec2.x,
						}, -- end of [1]
					}, -- end of ["units"]
					["y"] = dmVec2.y,
					["x"] = dmVec2.x,
					["name"] = "Xcl_Dismounts_" .. carrierUnitID,
					["start_time"] = 0,
					["task"] = "Ground Nothing",
				}
				return group
			end
			--]]--	
		end
	end			
	
	--function to check if the dismounts carriers are moving
	local function CheckMovement()
		for n = 1, #DismountsCarrier do
			local u = Unit.getByName(DismountsCarrier[n].name)
			if u ~= nil then
				local v = u:getVelocity()	--Velocity is a Vec3
				if v.x == 0 and v.y == 0 and v.z == 0 then	--Check if speed is zero
					if DismountsCarrier[n].cargo_status == "mounted" then
						local carrierPos = u:getPosition()
						local group = nil
						--if DismountsCarrier[n].currentGroup == nil then
							--if debugProcess == true then
								--env.info(("DAWS AIE  -  DismountsCarrier group " .. tostring(DismountsCarrier[n].name) .. " has not a stored group table"))
							--end								
							group = GetDmGroup(DismountsCarrier[n].countryID, DismountsCarrier[n].UnitID, carrierPos, DismountsCarrier[n].cargo, DismountsCarrier[n].name, DismountsCarrier[n].currentGroup)
							DismountsCarrier[n].currentGroup = group
						--[[
						else
							
							if debugProcess == true then
								env.info(("DAWS AIE  -  DismountsCarrier group " .. tostring(DismountsCarrier[n].name) .. " has a stored group table"))
							end	
							group = DismountsCarrier[n].currentGroup
							local dmVec2 = { --Determine the x,y Vec2 position of the dismounts (10m behind of the carrier)
								x = carrierPos.p.x + carrierPos.x.x * -10,
								y = carrierPos.p.z + carrierPos.x.z * -10,
							}	

							group["route"]["points"][1]["y"] = dmVec2.x
							group["route"]["points"][1]["y"] = dmVec2.y
							local offset_x = 0
							local offset_y = 0									
							
							if group.units then
								for uId, uData in pairs(group.units) do
									uData.x = dmVec2.x + offset_x
									uData.y = dmVec2.y + offset_y
									offset_x = offset_x + 3
									offset_y = offset_y + 3
								end
							end 							
							
						end --]]--
						
						if group then
							if debugProcess == true then
								env.info(("DAWS AIE  -  DismountsCarrier dismounting group " .. tostring(DismountsCarrier[n].name)))
							end											
							coalition.addGroup(DismountsCarrier[n].countryID, Group.Category.GROUND, group)
							DismountsCarrier[n].cargo_status = "dismounted"
						--else
							--if debugProcess == true then
								--env.info(("DAWS AIE  -  DismountsCarrier group is nil"))
							--end							
						end
						
					end
				else	--Else carrier is moving
					if DismountsCarrier[n].cargo_status == "dismounted" then								
						if DismountsCarrier[n].cargo ~= "Rifle" or math.sqrt(v.x * v.x + v.z * v.z) > 4 then	--Remount rifle squad only when speed bigger than 5.3 m/s (19 kph). Remount everyone else immediately when moving.
							local g = Group.getByName("Dismounts_" .. DismountsCarrier[n].UnitID)
							if g ~= nil then	--Check if the group is still alive
								if debugProcess == true then
									env.info(("DAWS AIE  -  DismountsCarrier mounting units of group " .. "Dismounts_" .. DismountsCarrier[n].UnitID))
								end
								DismountsCarrier[n].cargo_status = "mounted"
								g:destroy()
							else
								if debugProcess == true then
									env.info(("DAWS AIE  -  DismountsCarrier units of group " .. "Dismounts_" .. DismountsCarrier[n].UnitID .. " are dead"))
								end									
								DismountsCarrier[n].cargo_status = "lost"	--If the dismounted group is destroyed, set status of the carrier to lost to prevent it from deploying a new group
							end
						end
					end
					--]]--
				end
			end
		end
		return timer.getTime() + 5
	end
	timer.scheduleFunction(CheckMovement, nil, timer.getTime() + 1)			
	
	--initialize			
	if useDismountedUnits == false then
		if debugProcess == true then
			env.info(("DAWS AIE  -  EA_infantry_carrier, useDismountedUnits is false"))
		end				
		local function CreateDismountsCarrier()
			for coa_name, coa_data in pairs(env.mission.coalition) do
				if coa_data.country then
					for cntry_id, cntry_data in pairs(coa_data.country) do
						for obj_type_name, obj_type_data in pairs(cntry_data) do
							if obj_type_name == "vehicle" then --should be an unncessary check
								if ((type(obj_type_data) == 'table') and obj_type_data.group and (type(obj_type_data.group) == 'table') and (#obj_type_data.group > 0)) then
									for group_num, group_data in pairs(obj_type_data.group) do
										if group_data and group_data.units and type(group_data.units) == 'table' then
											for id, unitData in pairs(group_data.units) do
												--local unitType = unitData.type
												local unitName = env.getValueDictByKey(unitData.name)
												local unitTable = Unit.getByName(unitName) -- unitName
												if unitTable then
													local unitType = unitTable:getTypeName()
													local prob = math.random()*100
													if debugProcess == true then
														env.info(("DAWS AIE  -  EA_infantry_carrier, Checking Carrier for unit type: " .. tostring(unitType) .. ", random = " .. prob))
													end														
													if prob < EA_infantry_carrier_Perc then
														if unitTable:hasAttribute("Infantry carriers") or unitType == "Ural-375 PBU" or unitType == "M 818" or unitType == "GAZ-3308" or unitType == "Ural-4320-31" or unitType == "Ural-4320T" then
															local type_add = math.random()*100
															local type_str = nil
															if type_add < 10 then
																type_str = "MANPADS"
															elseif type_add < 30 then
																type_str = "Mortar"
															elseif type_add < 101 then
																type_str = "Rifle"
															end
															if type_str then
																AddDismounts(unitName, type_str)
															end
															if debugProcess == true then
																env.info(("DAWS AIE  -  EA_infantry_carrier, added dismount type: " .. tostring(type_str) .. " for unit " .. tostring(unitName)))
															end	
														end
													end
												else
													if debugProcess == true then
														env.info(("DAWS AIE  -  EA_infantry_carrier, can't access at table of unit: " .. tostring(unitName)))
													end														
												end
											end											
										end
									end
								end
							end
						end
					end
				end
			end
			DAWS_SM_mountedInfantry = DismountsCarrier
		end
		timer.scheduleFunction(CreateDismountsCarrier, nil, timer.getTime() + 5)	
	end
	
	-- update dismount table
	local function updateExportDismountTable()
		if table.getn(DismountsCarrier) > 0 then
			DAWS_mountedInfantryTable_SM = DismountsCarrier
		end
		timer.scheduleFunction(updateExportDismountTable, nil, timer.getTime() + 60)
	end
	timer.scheduleFunction(updateExportDismountTable, nil, timer.getTime() + 60)
	

	
-- ################################################################################################################################
-- ### SUB-MODULE Logistic enhancement ############################################################################################
-- ################################################################################################################################
	
	-- convogli di logistica. Da capire come cavolo fare per definire: punti di partenza, effetti della mancanza (registrare da una missione alla successiva se un veicolo resta senza armi).
	
	
	
	--timer.scheduleFunction(DAWS.dumpTable, {"templatefile.lua", templates}, base.timer.getTime() + 10)
	

	env.info(("DAWS AI Enhancement version - Ground infantry carrier pack loaded"))
	-- ~=
	
