dofilepath("data:scripts/scar/dynaDef.lua")


function SetupOrgSobs()
	CreateDefinitions();

	eraImpFighters = {}
	eraRebFighters = {}
	eraBenFighters = {}
	eraVngFighters = {}
	local nextEIF = 1
	local nextERF = 1
	local nextEBF = 1
	local nextEVF = 1
	
	for i, x in Fighters do
		local inEra = 0
		for u, y in x[5] do
			if (y == era) then
				inEra = 1
			end
		end
		if (inEra == 1) then
			if (x[2] == "I") then
				eraImpFighters[nextEIF] = x
				nextEIF = nextEIF + 1
			elseif (x[2] == "R") then
				eraRebFighters[nextERF] = x
				nextERF = nextERF + 1
			elseif (x[2] == "B") then
				eraBenFighters[nextEBF] = x
				nextEBF = nextEBF + 1
			elseif (x[2] == "V") then
				eraVngFighters[nextEVF] = x
				nextEVF = nextEVF + 1
			end
		end
	end

	eraImpBombers = {}
	eraRebBombers = {}
	eraBenBombers = {}
	eraVngBombers = {}
	local nextEIB = 1
	local nextERB = 1
	local nextEBB = 1
	local nextEVB = 1

	for i, x in Bombers do
		local inEra = 0
		for u, y in x[5] do
			if (y == era) then
				inEra = 1
			end
		end
		if (inEra == 1) then
			if (x[2] == "I") then
				eraImpBombers[nextEIB] = x
				nextEIB = nextEIB + 1
			elseif (x[2] == "R") then
				eraRebBombers[nextERB] = x
				nextERB = nextERB + 1
			elseif (x[2] == "B") then
				eraBenBombers[nextEBB] = x
				nextEBB = nextEBB + 1
			elseif (x[2] == "V") then
				eraVngBombers[nextEVB] = x
				nextEVB = nextEVB + 1
			end
		end
	end

	for k, v in Capships do
		SobGroup_Create("OrgAll" .. v[ 1 ])
		SobGroup_Clear("OrgAll" .. v[ 1 ])
	end

	SobGroup_Create("temp");
	SobGroup_Create("temp2");
	SobGroup_Create("temp3");
	SobGroup_Create("FM_Bombers");
	--SobGroup_Create("FM_All");
	setglobal("FM_NextIndex", 0);
	setglobal("FM_NextIndexToRun", 0);

	local playerIndex = 0;
	local playerCount = Universe_PlayerCount();
	while (playerIndex < playerCount) do
		SobGroup_Create("FM_All" .. playerIndex);
		playerIndex = playerIndex + 1
	end

	--FM_MaxFighters = FM_MaxFighters * 6
	-- Settings

	--ShipsPerTurn = 3; 		-- This is the number of ships processed every time the script is run.
	--FighterReduction = 0; 		-- If this is set to 1 the number of fighters and bombers spawned is cut in half.
	--FighterSpawnDelay = 1;		-- This is the number of times the script has to process a ship between each
					-- spawn, and before the first spawn. (so running the script once every 2 seconds  
					-- and haveing this delay at 5 should yeild a fighter roughly every 10 seconds, as   
					-- long as all ships are processed each turn (see ShipsPerTurn))

end


function GetFighterWeighting(Fighter, Side)
	if (Side == "I") then
		for k, v in eraImpFighters do
			if (v[1] == Fighter) then
				return v[6]
			end
		end
	elseif (Side == "R") then
		for k, v in eraRebFighters do
			if (v[1] == Fighter) then
				return v[6]
			end
		end
	elseif (Side == "B") then
		for k, v in eraBenFighters do
			if (v[1] == Fighter) then
				return v[6]
			end
		end
	elseif (Side == "V") then
		for k, v in eraVngFighters do
			if (v[1] == Fighter) then
				return v[6]
			end
		end
	end
	return 0
end


function GetBomberWeighting(Bomber, Side)
	if (Side == "I") then
		for k, v in eraImpBombers do
			if (v[1] == Bomber) then
				return v[6]
			end
		end
	elseif (Side == "R") then
		for k, v in eraRebBombers do
			if (v[1] == Bomber) then
				return v[6]
			end
		end
	elseif (Side == "B") then
		for k, v in eraBenBombers do
			if (v[1] == Bomber) then
				return v[6]
			end
		end
	elseif (Side == "V") then
		for k, v in eraVngBombers do
			if (v[1] == Bomber) then
				return v[6]
			end
		end
	end

end


function FillSob(ShipLong, ShipShort, NextIndex, MFighters, MBombers, Side, Turrets)
	SobGroup_Create("OrgCurrent");
	SobGroup_Clear("OrgCurrent");
	SobGroup_Create("OrgNew");
	SobGroup_Clear("OrgNew");

	Update_AllShips();

	SobGroup_FillShipsByType("OrgCurrent","AllShips", ShipLong)
	SobGroup_FillSubstract("OrgNew","OrgCurrent","OrgAll" .. ShipShort)

	if (SobGroup_Count("OrgNew") == 0) then
		
	elseif (SobGroup_Count("OrgNew") == 1) then
		SobGroup_Create("Org" .. getglobal("FM_NextIndex"))
		SobGroup_Clear("Org" .. getglobal("FM_NextIndex"))
		SobGroup_SobGroupAdd("Org" .. getglobal("FM_NextIndex"), "OrgNew")
		if (MFighters > 0) then
			SobGroup_Create("Org" .. getglobal("FM_NextIndex") .. "Fgt")
			SobGroup_Clear("Org" .. getglobal("FM_NextIndex") .. "Fgt")
		end
		if (MBombers > 0) then
			SobGroup_Create("Org" .. getglobal("FM_NextIndex") .. "Bbr")
			SobGroup_Clear("Org" .. getglobal("FM_NextIndex") .. "Bbr")
		end

		setglobal("FM_Turrets" .. getglobal("FM_NextIndex"), Turrets)
		setglobal("FM_Delay" .. getglobal("FM_NextIndex"), Universe_GameTime() + FM_FighterSpawnDelay)
		--if (FighterReduction == 1) then
		--	setglobal("FM_MFighters" .. FM_NextIndex, MFighters / 2)
		--	setglobal("FM_MBombers" .. FM_NextIndex, MBombers / 2)
		--else
			setglobal("FM_MFighters" .. FM_NextIndex, MFighters)
			setglobal("FM_MBombers" .. FM_NextIndex, MBombers)
		--end
		
		setglobal("FM_Side" .. FM_NextIndex, Side)
		setglobal("FM_NextIndex", getglobal("FM_NextIndex") + 1)

		SobGroup_SobGroupAdd("OrgAll" .. ShipShort, "OrgNew")
	else
		SobGroup_GetOneFromGroup("temp", "OrgNew")
		if (SobGroup_Count("temp") == 1) then
			SobGroup_Create("Org" .. getglobal("FM_NextIndex"))
			SobGroup_Clear("Org" .. getglobal("FM_NextIndex"))
			SobGroup_SobGroupAdd("Org" .. getglobal("FM_NextIndex"), "temp")
			if (MFighters > 0) then
				SobGroup_Create("Org" .. getglobal("FM_NextIndex") .. "Fgt")
				SobGroup_Clear("Org" .. getglobal("FM_NextIndex") .. "Fgt")
			end
			if (MBombers > 0) then
				SobGroup_Create("Org" .. getglobal("FM_NextIndex") .. "Bbr")
				SobGroup_Clear("Org" .. getglobal("FM_NextIndex") .. "Bbr")
			end

			setglobal("FM_Turrets" .. getglobal("FM_NextIndex"), Turrets)
			setglobal("FM_Delay" .. getglobal("FM_NextIndex"), Universe_GameTime() + FM_FighterSpawnDelay)
			setglobal("FM_MFighters" .. FM_NextIndex, MFighters)
			setglobal("FM_MBombers" .. FM_NextIndex, MBombers)
			setglobal("FM_Side" .. FM_NextIndex, Side)
			setglobal("FM_NextIndex", getglobal("FM_NextIndex") + 1)

			SobGroup_SobGroupAdd("OrgAll" .. ShipShort, "temp")
		else
			Subtitle_Message("ERROR, Several ships of the same type produced in the same frame! (and SobGroup_SplitGroup could not split them)", 10)
		end
	end
end


function AdvanceIndex(ShipShort)
	for k, v in Capships do
		if (v[ 1 ] == ShipShort) then
			v[ 3 ] = v[ 3 ] + 1
		end
	end
end


function OrgInSobs()
	for k, v in Capships do
		FillSob(v[ 2 ], v[ 1 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], v[ 7 ])
	end
end


function CheckSpawnNeed(Index)
	if (SobGroup_Count("Org" .. Index) == 1 and getglobal("FM_Delay" .. Index) ~= nil) then
		local MFighters = getglobal("FM_MFighters" .. Index)
		local MBombers = getglobal("FM_MBombers" .. Index)
		local Side = getglobal("FM_Side" .. Index)
		local owner = SobGroup_OwnedBy("Org" .. Index)

		if (getglobal("FM_Turrets" .. Index) ~= nil) then
			--print("Ive found a loro")
			SobGroup_Clear("temp")
			SobGroup_Clear("temp2")
			SobGroup_GetAttackers("Org" .. Index, "temp")
			if (SobGroup_Count("temp") > 0) then
					
				SobGroup_FillCompare("temp2", "temp", "FM_Bombers")
				if (SobGroup_Count("temp2") > 0) then
					local foundHP = 0
					local HPIndex = 0

					while (foundHP == 0) do
						if (SobGroup_GetHardPointHealth("Org" .. Index, "TURRET" .. HPIndex) > 0) then
							SobGroup_AttackSobGroupHardPoint("temp2", "Org" .. Index, "TURRET" .. HPIndex)
							foundHP = 1
						end
						HPIndex = HPIndex + 1
						if (HPIndex >= getglobal("FM_Turrets" .. Index)) then
							foundHP = 1
						end
					end
				end
			end
		end
	
		--print(SobGroup_Count("FM_All") .. " + 6, " .. FM_MaxFighters)

		if (MFighters > 0) then
			if (SobGroup_Count("FM_All" .. owner) + 6 <= FM_MaxFighters and getglobal("FM_Delay" .. Index) <= Universe_GameTime()) then
				if (SobGroup_Count("Org" .. Index .. "Fgt") + 6 <= 6 * MFighters) then
					SobGroup_Clear("temp");
					SpawnFighters("Org" .. Index, Side)
	
					SobGroup_DockSobGroupInstant("temp", "Org" .. Index)

					SobGroup_AbilityActivate ("temp", AB_Scuttle, 0)
					SobGroup_AbilityActivate ("temp", AB_Retire, 0)
					SobGroup_AbilityActivate ("temp", AB_Move, 0)

					SobGroup_Launch("temp", "Org" .. Index)
					SobGroup_SobGroupAdd("Org" .. Index .."Fgt", "temp")	
					SobGroup_SobGroupAdd("FM_All" .. owner, "temp")
					setglobal("FM_Delay" .. Index, getglobal("FM_Delay" .. Index) + FM_FighterSpawnDelay)
				--elseif (SobGroup_Count("Org" .. Index .. "Fgt") <= (6 * MFighters)) then
				--	local avgHealth = SobGroup_HealthPercentage("Org" .. Index .. "Fgt")
				--	local fighterCount = SobGroup_Count("Org" .. Index .. "Fgt")
				--	local squads = fighterCount / (6 * avgHealth)
				--	if (squads < MFighters) then
				--		SobGroup_Clear("temp");
				--		SpawnFighters("Org" .. Index, Side)

				--		SobGroup_DockSobGroupInstant("temp", "Org" .. Index)
		
				--		SobGroup_AbilityActivate ("temp", AB_Scuttle, 0)
				--		SobGroup_AbilityActivate ("temp", AB_Retire, 0)
				--		SobGroup_AbilityActivate ("temp", AB_Move, 0)

				--		SobGroup_Launch("temp", "Org" .. Index)
				--		SobGroup_SobGroupAdd("Org" .. Index .."Fgt", "temp")
				--		SobGroup_SobGroupAdd("FM_All", "temp")
				--		setglobal("FM_Delay" .. Index, getglobal("FM_Delay" .. Index) + FM_FighterSpawnDelay)
				--	end
				end
			end
			if (SobGroup_Count("Org" .. Index .. "Fgt") >= 0) then
				local capShip = "Org" .. Index
				local fighters = capShip .. "Fgt"
	
				SobGroup_Clear("temp")	-- Check for fighters that are docked.
				SobGroup_GetSobGroupDockedWithGroup(capShip, "temp")
				if (SobGroup_Count("temp") > 0) then
					SobGroup_Clear("temp2")
					SobGroup_FillCompare("temp2", "temp", fighters)
					if (SobGroup_Count("temp2") > 0) then
						if (SobGroup_HealthPercentage("temp2") == 1) then
							SobGroup_Launch("temp2", capShip)
						end
					end
				end

				SobGroup_Clear("temp")
				SobGroup_Clear("temp2")
				if (MBombers > 0) then
					SobGroup_GetAttackers("Org" .. Index .. "Bbr", "temp")
				end
				if (MFighters > 0) then
					SobGroup_GetAttackers("Org" .. Index .. "Fgt", "temp2")
				end
				SobGroup_SobGroupAdd("temp", "temp2")
				SobGroup_Clear("temp2")
				SobGroup_GetAttackers("Org" .. Index, "temp2")
				SobGroup_SobGroupAdd("temp", "temp2")
				if (SobGroup_Count("temp") > 0) then
					SobGroup_Attack(SobGroup_OwnedBy("Org" .. Index .. "Fgt"), "Org" .. Index .. "Fgt", "temp")
				else
					if (SobGroup_IsDoingAbility(fighters, AB_None) == 1 or SobGroup_IsDoingAbility(fighters, AB_Parade) == 1 or SobGroup_IsDoingAbility(fighters, AB_Stop) == 1) then
						SobGroup_GuardSobGroup(fighters, capShip)
					end
				end
			end
		end

		if (MBombers > 0) then
			if (SobGroup_Count("FM_All" .. owner) + 6 <= FM_MaxFighters and getglobal("FM_Delay" .. Index) <= Universe_GameTime()) then
				if (SobGroup_Count("Org" .. Index .. "Bbr") + 6 <= 6 * MFighters) then
					SobGroup_Clear("temp");
					SpawnBombers("Org" .. Index, Side)
	
					SobGroup_DockSobGroupInstant("temp", "Org" .. Index)

					SobGroup_AbilityActivate ("temp", AB_Scuttle, 0)
					SobGroup_AbilityActivate ("temp", AB_Retire, 0)
					SobGroup_AbilityActivate ("temp", AB_Move, 0)

					SobGroup_Launch("temp", "Org" .. Index)
					SobGroup_SobGroupAdd("Org" .. Index .."Bbr", "temp")
					SobGroup_SobGroupAdd("FM_Bombers", "temp")
					SobGroup_SobGroupAdd("FM_All" .. owner, "temp")
					setglobal("FM_Delay" .. Index, getglobal("FM_Delay" .. Index) + FM_FighterSpawnDelay)
				--elseif (SobGroup_Count("Org" .. Index .. "Bbr") <= (6 * MBombers)) then
				--	local avgHealth = SobGroup_HealthPercentage("Org" .. Index .. "Bbr")
				--	local fighterCount = SobGroup_Count("Org" .. Index .. "Bbr")
				--	local squads = fighterCount / (6 * avgHealth)
				--	if (squads < MBombers) then
				--		SobGroup_Clear("temp");
				--		SpawnBombers("Org" .. Index, Side)
--
--				--		SobGroup_DockSobGroupInstant("temp", "Org" .. Index)

				--		SobGroup_AbilityActivate ("temp", AB_Scuttle, 0)
				--		SobGroup_AbilityActivate ("temp", AB_Retire, 0)
				--		SobGroup_AbilityActivate ("temp", AB_Move, 0)

				--		SobGroup_Launch("temp", "Org" .. Index)
				--		SobGroup_SobGroupAdd("Org" .. Index .."Bbr", "temp")
				--		SobGroup_SobGroupAdd("FM_Bombers", "temp")
				--		SobGroup_SobGroupAdd("FM_All", "temp")
				--		setglobal("FM_Delay" .. Index, getglobal("FM_Delay" .. Index) + FM_FighterSpawnDelay)
				--	end
				end
			end
			if (SobGroup_Count("Org" .. Index .. "Bbr") >= 0) then
				local capShip = "Org" .. Index
				local fighters = capShip .. "Bbr"
	
				SobGroup_Clear("temp")	-- Check for fighters that are docked.
				SobGroup_GetSobGroupDockedWithGroup(capShip, "temp")
				if (SobGroup_Count("temp") > 0) then
					SobGroup_Clear("temp2")
					SobGroup_FillCompare("temp2", "temp", fighters)
					if (SobGroup_Count("temp2") > 0) then
						if (SobGroup_HealthPercentage("temp2") == 1) then
							SobGroup_Launch("temp2", capShip)
						end
					end
				end

				if (SobGroup_IsDoingAbility(fighters, AB_None) == 1 or SobGroup_IsDoingAbility(fighters, AB_Parade) == 1 or SobGroup_IsDoingAbility(fighters, AB_Stop) == 1) then
					SobGroup_GuardSobGroup(fighters, capShip)
				end
			end
		end
		--if (getglobal("FM_Delay" .. Index) > 0) then
		--	setglobal("FM_Delay" .. Index, getglobal("FM_Delay" .. Index) - 1)
		--end
	end
end

function SpawnFighters(ShipToSpawnIn, Side)
	local possibleFighters = {}
	local nextF = 1

	if (Side == "I") then
		for k, v in eraImpFighters do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "R") then
		for k, v in eraRebFighters do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "B") then
		for k, v in eraBenFighters do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "V") then
		for k, v in eraVngFighters do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	end

	local totalWeighting = 0
	if (nextF > 1) then
		for x = 1, (nextF - 1), 1 do
			totalWeighting = totalWeighting + GetFighterWeighting(possibleFighters[x], Side)
		end
	end

	if (totalWeighting > 0) then
		local randomNum = random(totalWeighting)
		
		for x = 1, (nextF - 1), 1 do
			randomNum = randomNum - GetFighterWeighting(possibleFighters[x], Side)
			if (randomNum < 1) then
				SpawnFIfPossible(SobGroup_OwnedBy(ShipToSpawnIn), possibleFighters[x], "temp")
				return
			end
		end
		Subtitle_Message("ERROR: Random fighterspawn failed", 5)
		return
	end
end

function SpawnBombers(ShipToSpawnIn, Side)
	local possibleFighters = {}
	local nextF = 1

	if (Side == "I") then
		for k, v in eraImpBombers do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "R") then
		for k, v in eraRebBombers do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "B") then
		for k, v in eraBenBombers do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	elseif (Side == "V") then
		for k, v in eraVngBombers do
			local hasRes = 0
			if (v[4] == "") then
				hasRes = 1
			else
				if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
					hasRes = 1
				end
			end
			if (hasRes == 1) then
				possibleFighters[nextF] = v[1]
				nextF = nextF + 1
			end
		end
	end

	local totalWeighting = 0
	if (nextF > 1) then
		for x = 1, (nextF - 1), 1 do
			totalWeighting = totalWeighting + GetBomberWeighting(possibleFighters[x], Side)
		end
	end

	if (totalWeighting > 0) then
		local randomNum = random(totalWeighting)		

		for x = 1, (nextF - 1), 1 do
			randomNum = randomNum - GetBomberWeighting(possibleFighters[x], Side)
			if (randomNum < 1) then
				SpawnBIfPossible(SobGroup_OwnedBy(ShipToSpawnIn), possibleFighters[x], "temp")
				return
			end
		end
		Subtitle_Message("ERROR: Random bomberspawn failed", 5)
		return
	end
end

function old_SpawnBombers(ShipToSpawnIn, Side)
	local possibleBombers = {}
	local nextB = 1

	for k, v in Bombers do
		if (v[2] == Side) then
			local inEra = 0
			for x, y in v[ 5 ] do
				if (y == era) then
					inEra = 1
				end
			end
			if (inEra == 1) then
				local hasRes = 0
				if (v[4] == "") then
					hasRes = 1
				else
					if (Player_HasResearch(SobGroup_OwnedBy(ShipToSpawnIn), v[4]) == 1) then
						hasRes = 1
					end
				end
				if (hasRes == 1) then
					possibleBombers[nextB] = v[1]
					nextB = nextB + 1
				end
			end
		end
	end

	local totalWeighting = 0
	if (nextB > 1) then
		for x = 1, (nextB - 1), 1 do
			totalWeighting = totalWeighting + GetBomberWeighting(possibleBombers[x])
		end
	end

	if (totalWeighting > 0) then
		local randomNum = random(totalWeighting)
		
		for x = 1, (nextB - 1), 1 do
			randomNum = randomNum - GetBomberWeighting(possibleBombers[x])
			if (randomNum < 1) then
				SpawnBIfPossible(SobGroup_OwnedBy(ShipToSpawnIn), possibleBombers[x], "temp")
				return
			end
		end
		Subtitle_Message("ERROR: Random bomberspawn failed")
		return
	end


	--if (Side == "I") then
	--	SpawnIfPossible(SobGroup_OwnedBy(ShipToSpawnIn), "vgr_tie_bomber", "temp")
	--elseif (Side == "R") then
	--	SpawnIfPossible(SobGroup_OwnedBy(ShipToSpawnIn), "hgn_x_wing", "temp")
	--end
end

function SpawnFIfPossible(Owner, TypeToSpawn, SobToSpawnIn)
	local RUs = Player_GetRU(Owner)

	for k, v in Fighters do
		if (v[1] == TypeToSpawn) then
			--if (RUs >= v[3]) then
				SobGroup_SpawnNewShipInSobGroup(Owner, TypeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
			--	Player_SetRU(Owner, (RUs - v[3]))
				return
			--else
			--	return
			--end
		end
	end
	Subtitle_Message("Error in fighterspawn", 5)
end

function SpawnBIfPossible(Owner, TypeToSpawn, SobToSpawnIn)
	local RUs = Player_GetRU(Owner)

	for k, v in Bombers do
		if (v[1] == TypeToSpawn) then
			--if (RUs >= v[3]) then
				SobGroup_SpawnNewShipInSobGroup(Owner, TypeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
			--	Player_SetRU(Owner, (RUs - v[3]))
				return
			--else
			--	return
			--end
		end
	end
	Subtitle_Message("Error in bomberspawn", 5)
end

--function SpawnIfPossible(Owner, typeToSpawn, SobToSpawnIn)
--	local RUs = Player_GetRU(Owner)
--
--	for k, v in Fighters do
--		if (v[1] == typeToSpawn) then
--			if (RUs >= v[3]) then
--				SobGroup_SpawnNewShipInSobGroup(Owner, typeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
--				Player_SetRU(Owner, (RUs - v[3]))
--				return
--			end
--		end
---	end
--
--
--
--	if (typeToSpawn == "vgr_tie_fighter") then
--		if (RUs >= 250) then
--			SobGroup_SpawnNewShipInSobGroup(Owner, typeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
--			Player_SetRU(Owner, (RUs - 250))
--		end
--	elseif (typeToSpawn == "vgr_tie_bomber") then
--		if (RUs >= 500) then
--			SobGroup_SpawnNewShipInSobGroup(Owner, typeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
--			Player_SetRU(Owner, (RUs - 500))
--		end
--	elseif (typeToSpawn == "hgn_x_wing") then
--		if (RUs >= 550) then
--			SobGroup_SpawnNewShipInSobGroup(Owner, typeToSpawn, "test", SobToSpawnIn, "StartPos" .. Owner)
--			Player_SetRU(Owner, (RUs - 550))
--		end
--	end
--end

function AddFightersToOrgSubs()
	--randomseed
	--local randomNum = random(2)
	--Subtitle_Message("Checking Fighter spawn need",2)

--	for k, v in Capships do
--		CheckSpawnNeed(v[ 1 ], v[ 3 ], v[ 4 ], v[ 5 ], v[ 6 ], v[ 7 ])
--	end
	
	if (getglobal("FM_NextIndex") > 0) then
		local currentIndex = 0
		local maxIndex = getglobal("FM_NextIndex")
		local runSoFar = 0
		currentIndex = FM_NextIndexToRun

		--print("Running AddFightersToOrgSubs")

		while (currentIndex < maxIndex) do
			CheckSpawnNeed(currentIndex)
			
			--print("Running CheckSpawnNeed on index: " .. currentIndex)

			currentIndex = currentIndex + 1
			runSoFar = runSoFar + 1
			if (runSoFar >= FM_ShipsPerTurn) then
				FM_NextIndexToRun = currentIndex
				return
			end
		end
		FM_NextIndexToRun = 0
	end
end

--function Update_AllShips()
--    local playerIndex = 0
--    local playerCount = Universe_PlayerCount()
--    SobGroup_Create("AllShips")
--    SobGroup_Clear("AllShips")
--    while  playerIndex<playerCount do
--        SobGroup_SobGroupAdd("AllShips","Player_Ships"..playerIndex)
--        playerIndex = playerIndex + 1
--    end
--end