-- DAWS_Package for DCSW -- Enhanced AI -- Object driven logistic system
local MainVersion = "2"
local SubVersion = "3"
local Build = "2351"

-- 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)
	
	
	
	-- USER VARIABLES	
	local create_weapons_log 		= true
	
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################

	-- 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"
	local configfiletype 			= ".csv"
	local tss 						= ";"
	
	--##### INTERNAL VARIABLES
	local debugProcess 				= DAWSdebugProcess 	-- print information log in dcs.log
	local superdebugProcess			= DAWSdebugProcess	-- export .lua tables in Logs\ directory
	DC_AMVI							= {}
	local tbl_cargo 				= {}
	local ab_table 					= {}
	local commandDB					= {}
	local customradius				= 2000
	
	
	--##### FIXED TABLE
	local wpnType_table = {
		["Vikhr M"] = {number = 6, mass = 330, name = "Vikhr M"},
		["S-8KOM"] = {number = 20, mass = 349, name = "S-8KOM"},
		["S-8OFP2"] = {number = 20, mass = 457, name = "S-8OFP2"},
		["S-8OM"] = {number = 20, mass = 365, name = "S-8OM"},
		["S-8TsM"] = {number = 20, mass = 345, name = "S-8TsM"},	
		["S-13OF"] = {number = 5, mass = 510, name = "S-13OF"},	
		["Kh-25ML"] = {number = 1, mass = 360, name = "Kh-25ML"},	
		["FAB-250"] = {number = 1, mass = 250, name = "FAB-250"},	
		["FAB-500"] = {number = 1, mass = 506, name = "FAB-500"},	
		["KMGU_2_PTAB_2_5KO"] = {number = 1, mass = 520, name = "KMGU_2_PTAB_2_5KO"},		
		["KMGU_2_AO_2_5RT"] = {number = 1, mass = 520, name = "KMGU_2_AO_2_5RT"},		
	}
	
	--##### FIXED TABLE
	local fuelType_table = {
		["Ka-50"] = {number = 1, mass = 1450, name = "Ka50"},
		["Mi-8MT"] = {number = 1, mass = 1929, name = "Mi8MT"},
		["SA342M"] = {number = 1, mass = 416, name = "SA342M"},
		["UH1-H"] = {number = 1, mass = 631, name = "UH1H"},
	}	
	
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################
-- ################################################################################################################################

	-- imported basicSerialize
	function IntegratedbasicSerialize(s)
		if s == nil then
			return "\"\""
		else
			if ((type(s) == 'number') or (type(s) == 'boolean') or (type(s) == 'function') or (type(s) == 'table') or (type(s) == 'userdata') ) then
				return tostring(s)
			elseif type(s) == 'string' then
				return string.format('%q', s)
			end
		end
	end
	--]]--

	-- imported slmod.serialize
	function Integratedserialize(name, value, level)
		-----Based on ED's serialize_simple2
		local basicSerialize = function (o)
		  if type(o) == "number" then
			return tostring(o)
		  elseif type(o) == "boolean" then
			return tostring(o)
		  else -- assume it is a string
			return IntegratedbasicSerialize(o)
		  end
		end

		local serialize_to_t = function (name, value, level)
		----Based on ED's serialize_simple2


		  local var_str_tbl = {}
		  if level == nil then level = "" end
		  if level ~= "" then level = level.."  " end

		  table.insert(var_str_tbl, level .. name .. " = ")

		  if type(value) == "number" or type(value) == "string" or type(value) == "boolean" then
			table.insert(var_str_tbl, basicSerialize(value) ..  ",\n")
		  elseif type(value) == "table" then
			  table.insert(var_str_tbl, "\n"..level.."{\n")

			  for k,v in pairs(value) do -- serialize its fields
				local key
				if type(k) == "number" then
				  key = string.format("[%s]", k)
				else
				  key = string.format("[%q]", k)
				end

				table.insert(var_str_tbl, Integratedserialize(key, v, level.."  "))

			  end
			  if level == "" then
				table.insert(var_str_tbl, level.."} -- end of "..name.."\n")

			  else
				table.insert(var_str_tbl, level.."}, -- end of "..name.."\n")

			  end
		  else
			print("Cannot serialize a "..type(value))
		  end
		  return var_str_tbl
		end

		local t_str = serialize_to_t(name, value, level)

		return table.concat(t_str)
	end
	--]]--

	-- imported slmod.serializeWithCycles
	function IntegratedserializeWithCycles(name, value, saved)
		local basicSerialize = function (o)
			if type(o) == "number" then
				return tostring(o)
			elseif type(o) == "boolean" then
				return tostring(o)
			else -- assume it is a string
				return IntegratedbasicSerialize(o)
			end
		end

		local t_str = {}
		saved = saved or {}       -- initial value
		if ((type(value) == 'string') or (type(value) == 'number') or (type(value) == 'table') or (type(value) == 'boolean')) then
			table.insert(t_str, name .. " = ")
			if type(value) == "number" or type(value) == "string" or type(value) == "boolean" then
				table.insert(t_str, basicSerialize(value) ..  "\n")
			else

				if saved[value] then    -- value already saved?
					table.insert(t_str, saved[value] .. "\n")
				else
					saved[value] = name   -- save name for next time
					table.insert(t_str, "{}\n")
					for k,v in pairs(value) do      -- save its fields
						local fieldname = string.format("%s[%s]", name, basicSerialize(k))
						table.insert(t_str, IntegratedserializeWithCycles(fieldname, v, saved))
					end
				end
			end
			return table.concat(t_str)
		else
			return ""
		end
	end
	--]]--

	-- imported slmod.serializeWithCycles
	function IntegratedGetUnzippedMission(t_window) -- OK

		t_window = t_window or 3600 -- default: if t_window not specified, look at all files newer than 1 hour
		local run_t = os.time()
		local mis_path, mis_t
		local path =  lfs.tempdir()

		for file in lfs.dir(path) do
			if file and file:sub(1,1) == '~' then
				local fpath = path .. '/' .. file
				local mod_t = lfs.attributes(fpath, 'modification')
				if mod_t and math.abs(run_t - mod_t) <= t_window then
					local f = io.open(fpath, 'r')
					if f then
						local fline = f:read()
						if fline and fline:sub(1,7) == 'mission' and (not mis_t or mod_t > mis_t) then -- found an unzipped mission file, and either none was found before or this is the most recent
							mis_t = mod_t
							mis_path = fpath
						end
						f:close()
					end
				end
			end
		end

		if mis_path then -- a mission file was found
			local f = io.open(mis_path, 'r')
			if f then
				local mission = f:read('*all')
				f:close()
				return mission
			end
		end
	end
	--]]--
	
	-- old mist.utils.tableShow
	DC_AMVI.tableShow = function(tbl, loc, indent, tableshow_tbls) --based on serialize_slmod, this is a _G serialization
		tableshow_tbls = tableshow_tbls or {} --create table of tables
		loc = loc or ""
		indent = indent or ""
		if type(tbl) == 'table' then --function only works for tables!
			tableshow_tbls[tbl] = loc
			
			local tbl_str = {}

			tbl_str[#tbl_str + 1] = indent .. '{\n'
			
			for ind,val in pairs(tbl) do -- serialize its fields
				if type(ind) == "number" then
					tbl_str[#tbl_str + 1] = indent 
					tbl_str[#tbl_str + 1] = loc .. '['
					tbl_str[#tbl_str + 1] = tostring(ind)
					tbl_str[#tbl_str + 1] = '] = '
				else
					tbl_str[#tbl_str + 1] = indent 
					tbl_str[#tbl_str + 1] = loc .. '['
					tbl_str[#tbl_str + 1] = IntegratedbasicSerialize(ind)
					tbl_str[#tbl_str + 1] = '] = '
				end
						
				if ((type(val) == 'number') or (type(val) == 'boolean')) then
					tbl_str[#tbl_str + 1] = tostring(val)
					tbl_str[#tbl_str + 1] = ',\n'		
				elseif type(val) == 'string' then
					tbl_str[#tbl_str + 1] = IntegratedbasicSerialize(val)
					tbl_str[#tbl_str + 1] = ',\n'
				elseif type(val) == 'nil' then -- won't ever happen, right?
					tbl_str[#tbl_str + 1] = 'nil,\n'
				elseif type(val) == 'table' then
					if tableshow_tbls[val] then
						tbl_str[#tbl_str + 1] = tostring(val) .. ' already defined: ' .. tableshow_tbls[val] .. ',\n'
					else
						tableshow_tbls[val] = loc ..  '[' .. IntegratedbasicSerialize(ind) .. ']'
						tbl_str[#tbl_str + 1] = tostring(val) .. ' '
						tbl_str[#tbl_str + 1] = DC_AMVI.tableShow(val,  loc .. '[' .. IntegratedbasicSerialize(ind).. ']', indent .. '    ', tableshow_tbls)
						tbl_str[#tbl_str + 1] = ',\n'  
					end
				elseif type(val) == 'function' then
					if debug and debug.getinfo then
						local fcnname = tostring(val)
						local info = debug.getinfo(val, "S")
						if info.what == "C" then
							tbl_str[#tbl_str + 1] = string.format('%q', fcnname .. ', C function') .. ',\n'
						else 
							if (string.sub(info.source, 1, 2) == [[./]]) then
								tbl_str[#tbl_str + 1] = string.format('%q', fcnname .. ', defined in (' .. info.linedefined .. '-' .. info.lastlinedefined .. ')' .. info.source) ..',\n'
							else
								tbl_str[#tbl_str + 1] = string.format('%q', fcnname .. ', defined in (' .. info.linedefined .. '-' .. info.lastlinedefined .. ')') ..',\n'
							end
						end
						
					else
						tbl_str[#tbl_str + 1] = 'a function,\n'	
					end
				else
					tbl_str[#tbl_str + 1] = 'unable to serialize value type ' .. IntegratedbasicSerialize(type(val)) .. ' at index ' .. tostring(ind)
				end
			end
			
			tbl_str[#tbl_str + 1] = indent .. '}'
			return table.concat(tbl_str)
		end
	end
	--]]--
	
	-- old mist.debug.dump_G
	DC_AMVI.dumpTable = function(fname, tabledata)
		if lfs and io then
			local fdir = lfs.writedir() .. [[Logs\]] .. fname
			local f = io.open(fdir, 'w')
			f:write(DC_AMVI.tableShow(tabledata))
			f:close()
		end
	end
	--]]--	

	-- ################################################################################################################################
	
	-- retrieve mission name
	local sortiecode = env.getValueDictByKey(env.mission.sortie) --DCS.getMissionName()	
	if sortiecode == nil then
		env.info(('DAWS AIE Logistic: sortiecode is nil'))
	else
		env.info(('DAWS AIE Logistic: sortiecode is ' .. sortiecode))
	end	
	
	
	
	
	
	-- ### ADD/REMOVE F10 MENU' AND DISPLAY AVAILABLE FUEL & AMMO #############################################################################################################################

	-- this builds up & print current available fuel & ammo.
	local displayAvailableLog = function(vars) -- unitName, groupName, groupID
		if debugProcess == true then
			env.info(("DAWS AIE Logistic: starting logistic information recall"))
			env.info(("DAWS AIE Logistic: variables - unitName=" .. tostring(vars.unitName) .. ", groupName=" .. tostring(vars.groupName) .. ", groupID=" .. tostring(vars.groupID)       ))
		end		
		
		local unit				= Unit.getByName(vars.unitName)
		local availLogistic 	= {fuel = {}, ammo = {}}
		local init_pos 			= unit:getPosition().p
		local init_coa 			= unit:getCoalition()
		local tempAmmo			= {}
		local tempFuel			= {}
		--local init_name 		= unit:getName()
		
		local volS = {
			id = world.VolumeType.SPHERE,
			params = {
			point = init_pos,
			radius = customradius -- manual fixed
			}
		}				
		
		local ifThere = function(foundItem, val)
			local cargoName 	= StaticObject.getName(foundItem)
			local cargoType 	= StaticObject.getTypeName(foundItem)
			--local cargoPos 		= StaticObject.getPosition(foundItem).p
			local cargoMass		= StaticObject.getCargoWeight(foundItem)
			local cargoCoa		= StaticObject.getCoalition(foundItem)		
			
			if cargoCoa == init_coa then
				
				if cargoType == "fueltank_cargo" then

					for fu_name, fu_data in pairs(fuelType_table) do					
						if string.find(cargoName, fu_data.name) then					
							local isthere = false
							for id, FuData in pairs(tempFuel) do
								if FuData.fname == fu_data.name then							
									isthere = true
									FuData.fqty = FuData.fqty + fu_data.mass
								end
							end
							
							if isthere == false then						
								tempFuel[#tempFuel + 1] = {fname = fu_data.name, fqty = fu_data.mass}
							end
						end
					end					
					
				elseif cargoType == "ammo_cargo" then
					
					for wpn_name, wpn_data in pairs(wpnType_table) do
						if string.find(cargoName, wpn_data.name) then
							local isthere = false
							for id, WpnData in pairs(tempAmmo) do
								if WpnData.wname == wpn_data.name then
									isthere = true
									WpnData.wqty = WpnData.wqty + wpn_data.number
								end
							end
							
							if isthere == false then
								tempAmmo[#tempAmmo + 1] = {wname = wpn_data.name, wqty = wpn_data.number}
							end
						end
					end
					
				end
			end
		end	
		world.searchObjects(Object.Category.CARGO, volS, ifThere)

		if superdebugProcess == true then
			DC_AMVI.dumpTable("tempAmmo.lua", tempAmmo)
			DC_AMVI.dumpTable("tempFuel.lua", tempFuel)
		end			
		
		for id, cargoData in pairs(tempFuel) do
			local alreadythere = false
			for funame, fudata in pairs(availLogistic.fuel) do
				if funame == cargoData.fname then
					alreadythere = true
					fudata = fudata + cargoData.fqty
				end
			end
				
			if alreadythere == false then
				availLogistic.fuel[cargoData.fname] = cargoData.fqty
			end	
				
		end

		for id, cargoData in pairs(tempAmmo) do
			local alreadythere = false
			for wpname, wpdata in pairs(availLogistic.ammo) do
				if wpname == cargoData.wname then
					alreadythere = true
					wpdata = wpdata + cargoData.wqty
				end
			end
				
			if alreadythere == false then
				availLogistic.ammo[cargoData.wname] = cargoData.wqty
			end	
				
		end		

		if superdebugProcess == true then
			DC_AMVI.dumpTable("availLogistic.lua", availLogistic)
		end
		
		-- dubbio: se il testo è costruito local, dove diavolo lo metto per poterlo richiamare quando razzo mi pare?
		local currentText = "Available fuel & weapons for " .. vars.groupName .. " flight:\n"
		--currentText = currentText .. " - Fuel: " .. availLogistic.fuel .. " kg\n"
		currentText = currentText .. "\nFuel:\n"
		for ftype, fqty in pairs(availLogistic.fuel) do
			currentText = currentText .. " - " .. ftype .. ": " .. fqty .. " kg\n"
			if debugProcess == true then
				env.info(("DAWS AIE Logistic: printing fuel table: " .. ftype .. "=" .. fqty))
			end					
		end		
		currentText = currentText .. "\nMunitions:\n"
		for wtype, wqty in pairs(availLogistic.ammo) do
			currentText = currentText .. " - " .. wtype .. ": " .. wqty .. "\n"
			if debugProcess == true then
				env.info(("DAWS AIE Logistic: printing ammo table: " .. wtype .. "=" .. wqty))
			end					
		end
		trigger.action.outTextForGroup(vars.groupID, currentText, 30 )

	end
	--]]--
	
	----
	addF10menu = {}
	function addF10menu:onEvent(event)
		if event.id == 19 then	

			if debugProcess == true then
				env.info(("DAWS AIE Logistic: detected S_EVENT_PLAYER_ENTER_UNIT event"))
			end
		
			local init_name 		= event.initiator:getPlayerName()

			if init_name then	
				local init_unitName		= event.initiator:getName()
				local init_group		= event.initiator:getGroup()
				local init_group_id		= init_group:getID()
				local init_group_name	= init_group:getName()
				
				if debugProcess == true then
					env.info(("DAWS AIE Logistic: adding f10 menù for group ID: " .. init_group_id .. ", unitname: " .. tostring(init_unitName)))				
				end	
				-- unitName, groupName, groupID
				commandDB[init_group_name] = missionCommands.addCommandForGroup(init_group_id, "DAWS - Get Available fuel & ammo", nil, displayAvailableLog, {unitName = init_unitName, groupName = init_group_name, groupID = init_group_id})
			end
		end
	end
	world.addEventHandler(addF10menu)
	--]]--
	
	removeF10menu = {}
	function removeF10menu:onEvent(event)
		if event.id == 20 then	--world.event.S_EVENT_PLAYER_LEAVE_UNIT

			if debugProcess == true then
				env.info(("DAWS AIE Logistic: detected S_EVENT_PLAYER_LEAVE_UNIT event"))
			end
			
			local init_group		= event.initiator:getGroup()
			local init_group_id		= init_group:getID()			
			local init_group_name	= init_group:getName()			
			
			missionCommands.removeItemForGroup(init_group_id, commandDB[init_group_name])
			
			if debugProcess == true then
				env.info(("DAWS AIE Logistic: removed f10 menù for group ID: " .. init_group_id))
			end
		end
	end
	world.addEventHandler(removeF10menu)	
	
	
	-- ################################################################################################################################

	
	-- this builds up & print current available fuel & ammo.
	local removeUsedCargo = function(vars) -- unitName, groupName, groupID
		if debugProcess == true then
			env.info(("DAWS AIE Logistic: starting removing cargo call"))
			env.info(("DAWS AIE Logistic: variables - unitName=" .. tostring(vars.unitName) .. ", groupName=" .. tostring(vars.groupName) .. ", groupID=" .. tostring(vars.groupID)))
		end		
		
		local unit				= Unit.getByName(vars.unitName)
		local availLogistic 	= {fuel = 0, ammo = {}}
		local init_pos 			= unit:getPosition().p
		local init_coa 			= unit:getCoalition()
		local tbl_removeCargo	= vars.tbl_remAmmo
		local tbl_availCargo	= {}
		--local init_fuelCode		= vars.fuel_code
		
		local volS = {
			id = world.VolumeType.SPHERE,
			params = {
			point = init_pos,
			radius = customradius -- manual fixed
			}
		}				
		
		local ifThere = function(foundItem, val)
			local cargoName 	= StaticObject.getName(foundItem)
			local cargoType 	= StaticObject.getTypeName(foundItem)
			--local cargoPos 		= StaticObject.getPosition(foundItem).p
			local cargoMass		= StaticObject.getCargoWeight(foundItem)
			local cargoCoa		= StaticObject.getCoalition(foundItem)		
			
			if cargoCoa == init_coa then
				
				if cargoType == "fueltank_cargo" then
					
					for fu_data, fu_data in pairs(fuelType_table) do
						if string.find(cargoName, fu_data.name) then
							local isthere = false
							tbl_availCargo[#tbl_availCargo + 1] = {cname = cargoName, wname = fu_data.name, wqty = 1}
						end
					end

				elseif cargoType == "ammo_cargo" then
					
					for wpn_name, wpn_data in pairs(wpnType_table) do
						if string.find(cargoName, wpn_data.name) then
							local isthere = false
							tbl_availCargo[#tbl_availCargo + 1] = {cname = cargoName, wname = wpn_data.name, wqty = 1}
						end
					end
					
				end
				
				
			end
		end	
		world.searchObjects(Object.Category.CARGO, volS, ifThere)
	
		
		for id, cargoData in pairs(tbl_availCargo) do
			for wpname, wpdata in pairs(tbl_removeCargo) do
				if cargoData.wname == wpdata.name then
					if wpdata.cargonum > 0 then
						local cObj = StaticObject.getByName(cargoData.cname)
						cObj:destroy()
						if debugProcess == true then
							env.info(("DAWS AIE Logistic: removed cargo " .. cargoData.cname))
						end							
						
						wpdata.cargonum = wpdata.cargonum -1
					end
				end
			end
		end
		


	end
	--]]--
	
	
	checkLoadedLogisticOnTakeoff = {}
	function checkLoadedLogisticOnTakeoff:onEvent(event)
		
		if event.id == 3 then	--world.event.S_EVENT_TAKEOFF

			if debugProcess == true then
				env.info(("DAWS AIE Logistic: detected S_EVENT_TAKEOFF event"))
			end
			
			local init_group		= event.initiator:getGroup()
			local init_unitName		= event.initiator:getName()
			local init_group_id		= init_group:getID()			
			local init_group_name	= init_group:getName()
			local init_ammo			= event.initiator:getAmmo()
			local init_fuel_code	= event.initiator:getTypeName()
		
			if superdebugProcess == true then
				DC_AMVI.dumpTable(init_unitName .. "init_ammo.lua", init_ammo)
			end				
			
			local tbl_loadedAmmo = {}
			for id, ammoData in pairs(init_ammo) do
				local checkexist = false
				if debugProcess == true then
					env.info(("DAWS AIE Logistic: checking " .. tostring(ammoData.desc.displayName)))
				end					
				for wname, wdata in pairs(wpnType_table) do
					if wdata.name == ammoData.desc.displayName then
						checkexist = true
						local qty_cargo = math.floor(ammoData.count/wdata.number)
						tbl_loadedAmmo[#tbl_loadedAmmo + 1] = {name = ammoData.desc.displayName, qty = ammoData.count, cargonum = qty_cargo}
						
					end
				end			
				
				if checkexist == false then
					--local currentText = "WARNING: loaded munitions type " .. ammoData.desc.displayName .. " is not registered!"
					--trigger.action.outTextForGroup(init_group_id, currentText, 5)
					if debugProcess == true then
						env.info(("DAWS AIE Logistic: WARNING: loaded munitions type " .. ammoData.desc.displayName .. " is not registered and not take into account"))
					end						
				end
			end
			
			local checkfuel = false
			if debugProcess == true then
				env.info(("DAWS AIE Logistic: checking fuel code" .. tostring(init_fuel_code)))
			end				
			
			for fname, fdata in pairs(fuelType_table) do
				if fname == init_fuel_code then
					checkfuel = true
					local qty_cargo = 1 -- math.floor(ammoData.count/wdata.number)
					tbl_loadedAmmo[#tbl_loadedAmmo + 1] = {name = fdata.name, qty = 1, cargonum = qty_cargo}
					
				end
			end			
			
			if checkfuel == false then
				--local currentText = "WARNING: loaded munitions type " .. ammoData.desc.displayName .. " is not registered!"
				--trigger.action.outTextForGroup(init_group_id, currentText, 5)
				if debugProcess == true then
					env.info(("DAWS AIE Logistic: WARNING: loaded fuel type " .. init_fuel_code .. " is not registered and not take into account"))
				end						
			end			
				
			if superdebugProcess == true then
				DC_AMVI.dumpTable(init_unitName .. "_tbl_loadedAmmo.lua", tbl_loadedAmmo)
			end					
			
			local vars = {unitName = init_unitName, groupName = init_group_name, groupID = init_group_id, tbl_remAmmo = tbl_loadedAmmo }   --, fuel_code = init_fuel_code
			
			removeUsedCargo(vars)
			
			if debugProcess == true then
				env.info(("DAWS AIE Logistic: placeholder comment"))
			end
			
		end
	end
	world.addEventHandler(checkLoadedLogisticOnTakeoff)		
	
	
--[[--
 {
   [1] = 
   {
       ["count"] = 8,
       ["desc"] = 
       {
           ["missileCategory"] = 2,
           ["rangeMaxAltMax"] = 12000,
           ["rangeMin"] = 1500,
           ["displayName"] = "9M330",
           ["rangeMaxAltMin"] = 8000,
           ["altMax"] = 6000,
           ["RCS"] = 0.03070000000298,
           ["box"] = 
           {
               ["min"] = 
               {
                   ["y"] = -0.26701140403748,
                   ["x"] = -1.678077340126,
                   ["z"] = -0.26600670814514,
               }, -- end of ["min"]
               ["max"] = 
               {
                   ["y"] = 0.26701140403748,
                   ["x"] = 1.8263295888901,
                   ["z"] = 0.26600670814514,
               }, -- end of ["max"]
           }, -- end of ["box"]
           ["altMin"] = 10,
           ["life"] = 2,
           ["fuseDist"] = 7,
           ["category"] = 1,
           ["guidance"] = 4,
           ["warhead"] = 
           {
               ["explosiveMass"] = 14.5,
               ["type"] = 1,
               ["caliber"] = 220,
               ["mass"] = 14.5,
           }, -- end of ["warhead"]
           ["typeName"] = "9M330",
           ["Nmax"] = 30,
       }, -- end of ["desc"]
   }, -- end of [1]
 } -- end of Tor Ammo
--]]--
	
	
	
	
	
	
	-- ################################################################################################################################
	
	-- initialize
	--create_ab_table()
	--create_tbl_cargo()	
	
	--cyclic
	--timer.scheduleFunction(update_tbl_cargo, {}, timer.getTime() + 600)
	
	
	
	
	
	
	
	
	
	

	env.info(("DAWS AI Enhancement version - Logistic system is initiated"))
	-- ~=
	
