-- Only works for single dimensional arrays
function tryTableLength(array)
    pcall(tableLength,array)
end

function tableLength(Array)
    count = 0;
    for index, value in ipairs(Array) do
        count = count + 1
    end
    return count
end

function shipInArrayTable(ship,arrayTable)
    for index, value in ipairs(arrayTable) do
        for index,value in ipairs(arrayTable[i]) do
            if ship == value then
                return true
            end
        end
    end
    return false
end



-- Function handles the creation of independent ships (similar to drones but more variables)
function createShip(name,class,primaryTable,secondaryTable,tertiaryTable,orientation,vector,team,removeOrders)
	newShip = mn.createShip(name,tb.ShipClasses[class],orientation,vector )
	if newShip ~= nil then
		newShip.Team = mn.Teams[team]
		local banks = newShip.PrimaryBanks

		if primaryTable ~= nil then
			for i=1, #banks do
				local primary = randomElement(primaryTable)
				local bank = banks[i]
				bank.WeaponClass = tb.WeaponClasses[primary]
				bank.AmmoLeft = 0
			end
		end

		if secondaryTable ~= nil then
			banks = newShip.SecondaryBanks
			for i=1, #banks do
				local secondary = randomElement(secondaryTable)
				local bank = banks[i]
				bank.WeaponClass = tb.WeaponClasses[secondary]
				bank.AmmoLeft = bank.AmmoMax
			end
		end

		if tertiary ~= nil then
			banks = newShip.TertiaryBanks
			for i=1, #banks do
				local bank = banks[i]
				local tertiary = randomElement(tertiaryTable)
				bank.WeaponClass = tb.WeaponClasses[tertiary]
				bank.AmmoLeft = bank.AmmoMax
			end
		end

		newShip:giveOrder(ORDER_ATTACK_ANY)

		if removeOrders == false then
			mn.runSEXP("( set-player-orders !"..newShip.Name.."! (false) !Destroy my target! !Disable my target! !Disarm my target! !Protect my target! !Ignore my target! !Form on my wing! !Cover me! !Engage enemy! !Capture my target! !Rearm me! !Abort rearm! !Stay near me! !Stay near my target! !Keep safe! !Depart! !Destroy subsystem!)")
		end
	end
	return newShip
end

function createCapitalShip(name,class,orientation,vector,team,Orders,target)
	newShip = mn.createShip(name,tb.ShipClasses[class],orientation,vector )
	if newShip ~= nil then
		newShip.Team = mn.Teams[team]
		if Orders == false then
			mn.runSEXP("( set-player-orders !"..newShip.Name.."! (false) !Destroy my target! !Disable my target! !Disarm my target! !Protect my target! !Ignore my target! !Form on my wing! !Cover me! !Engage enemy! !Capture my target! !Rearm me! !Abort rearm! !Stay near me! !Stay near my target! !Keep safe! !Depart! !Destroy subsystem!)")
		end
	end
	return newShip
end

function droneSpawn(parentObject,name,class,primary,secondary,tertiary)
	local newShip = mn.createShip(name,tb.ShipClasses[class],parentObject.Orientation,parentObject.Position)
	if newShip ~= nil then
		newShip.Team = parentObject.Team
		local banks = newShip.PrimaryBanks

		if primary ~= nil then
			for i=1, #banks do
				local bank = banks[i]
				bank.WeaponClass = tb.WeaponClasses[primary]
				bank.AmmoLeft = 0
			end
		end

		if secondary ~= nil then
			banks = newShip.SecondaryBanks
			for i=1, #banks do
				local bank = banks[i]
				bank.WeaponClass = tb.WeaponClasses[secondary]
				bank.AmmoLeft = bank.AmmoMax
			end
		end

		if tertiary ~= nil then
			banks = newShip.TertiaryBanks
			for i=1, #banks do
				local bank = banks[i]
				bank.WeaponClass = tb.WeaponClasses[tertiary]
				bank.AmmoLeft = bank.AmmoMax
			end
		end

		mn.runSEXP("( set-player-orders !"..newShip.Name.."! (false) !Destroy my target! !Disable my target! !Disarm my target! !Protect my target! !Ignore my target! !Form on my wing! !Cover me! !Engage enemy! !Capture my target! !Rearm me! !Abort rearm! !Stay near me! !Stay near my target! !Keep safe! !Depart! !Destroy subsystem!)")

		newShip:giveOrder(ORDER_ATTACK_ANY)
		return newShip
	end
	return "Failure"
end

function reArmShip(ship)
	if ship:isValid() then
		banks = ship.SecondaryBanks
		for i=1, #banks do
			local bank = banks[i]
			bank.AmmoLeft = bank.AmmoMax
		end
	end
end

function randomElement(tb)
    local keys = {}
    for k in pairs(tb) do table.insert(keys, k) end
    return tb[keys[math.random(#keys)]]
end

