primaryTerranTable = {"Subach HL-7","Prometheus R","Prometheus S","UD-8 Kayser"}
secondaryTerranTable = {"Rockeye","Tempest","Hornet","Tornado","Harpoon","Piranha"}
capitalTerranTable = {"GTC Aeolus","GTC Leviathan","GTC Fenris"}
fighterTerranTable = {"GTF Hercules Mark II","GTF Loki","GTF Perseus","GTF Myrmidon"}
advancedFighterTerranTable = {"GTF Ares","GTF Erinyes"}
secondaryDroneTerranTable = {"Drone Deploy.","Drone Deploy.","Turret Deploy.","lokiSpawn.","lokiSpawn."}
bossRound = 1

function spawnSwarm(shipDestroyed)
	if shipDestroyed:isValid() and shipDestroyed.Name == "Enemy" then
		bossRound = bossRound + 1
		local player = hv.Player
		local varToPass = math.random(0,250)
		local position = ba.createVector(0,0,0)



		async.run(function()

			mn.sendPlainMessage("Var to Pass:"..varToPass)
			if varToPass >= 200 then
				for i = 1, #mn.Ships, 1 do
					local ship = mn.Ships[i]
					if ship.Team == "Friendly" then
						reArmShip(ship)
					end
				end
				mn.sendPlainMessage("Rearmed all ships")
			elseif varToPass >= 125 then
				reArmShip(player)
				mn.sendPlainMessage("Rearmed Player")
			end
			if varToPass <= 5 then
				spawnRandomNonCap("Friendly Advanced Fighter",advancedFighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Friendly")
				mn.sendPlainMessage("Allied Advanced Fighter")
			elseif varToPass <= 15 then
				local tempSecTable = {"Drone Deploy.","Drone Deploy.","lokiSpawn."}
				spawnRandomNonCap("Friendly Drone Launcher",fighterTerranTable,primaryTerranTable,tempSecTable,position,"Friendly")
				mn.sendPlainMessage("Friendly drone spawning ship")
			elseif varToPass <= 30 then
				for i = 1, math.random(2,4), 1 do
					spawnRandomNonCap("Friendly Fighter Squad "..i,fighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Friendly")
				end
				mn.sendPlainMessage("Allied Fighter Squad")
			end

			--hv.Player.Position = ba.createVector(0,0,0)
            async.await(mn.waitAsync(5))
			local position = ba.createVector(0,0,0)

			if bossRound % 5 == 0 and bossRound <= 15 then
				for i=1, bossRound/5, 1 do
					spawnRandomNonCap("Boss..."..i,advancedFighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Hostile")
				end
				async.await(mn.waitAsync(20))
				spawnRandomCruiser("Enemy",position,"Hostile",hv.Player)
			elseif bossRound % 5 == 0 then
				for i=1, 2, 1 do
					spawnRandomNonCap("Boss "..i,advancedFighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Hostile")
				end
				for i=3, 4, 1 do
					spawnRandomNonCap("Boss "..i,advancedFighterTerranTable,primaryTerranTable,secondaryDroneTerranTable,position,"Hostile")
				end
				async.await(mn.waitAsync(20))
				spawnRandomCruiser("Enemy",position,"Hostile",hv.Player)
			else
				for i=1, math.random(3,7), 1 do
					if math.random(0,bossRound+90) > 100 then
						spawnRandomNonCap("Swarm"..i,fighterTerranTable,primaryTerranTable,secondaryDroneTerranTable,position,"Hostile")
					else
						spawnRandomNonCap("Swarm"..i,fighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Hostile")
					end
				end
				async.await(mn.waitAsync(20))
				spawnRandomNonCap("Enemy",advancedFighterTerranTable,primaryTerranTable,secondaryTerranTable,position,"Hostile")
			end


        end)
	end
end

function spawnRandomNonCap(name,class,primaryTable,secondaryTable,position,team)
	local vector = ba.createVector(math.random(1000,2500),math.random(1000,2000),math.random(1000,2000))
	if team == "Friendly" then
		vector = ba.createVector(math.random(-2000,-1000),math.random(-2000,-1000),math.random(-2000,-1000),false)
	end
    local orientation = ba.createOrientation(0,0,0)


	class = randomElement(class)
    createShip(name,class,primaryTable,secondaryTable,nil,orientation,position+vector,team,false)
end

function spawnRandomCruiser(name,position,team,target)
	local vector = ba.createVector(math.random(1000,4000),math.random(1000,4000),math.random(1000,4000))
	if team == "Friendly" then
		vector = ba.createVector(math.random(-4000,-1000),math.random(-4000,-1000),math.random(-4000,-1000),false)
	end
    local orientation = ba.createOrientation(0,0,0)
	local class = randomElement(capitalTerranTable)
    local newShip = createCapitalShip(name,class,orientation,position+vector,team,false)
	newShip:clearOrders()
	newShip:giveOrder(ORDER_ATTACK, target)
end
-- Function handles the creation of independent ships (similar to drones but more variables)
