
--updates player selections
--currently selected sobgroups can be accessed by "Selected"..playerIndex
function Update_Selected()
    local playerIndex=0
    playerCount = Universe_PlayerCount()
    while  playerIndex<playerCount do
        SobGroup_Create("Selected"..playerIndex)
        SobGroup_Clear("Selected"..playerIndex)
        playerIndex = playerIndex + 1
    end
    Update_AllShips()
    local Num = SobGroup_SplitGroup("SelectedTemp", "AllShips", SobGroup_Count("AllShips"))
    local i=0
    while (i<Num) do   
        if (SobGroup_Selected("SelectedTemp"..i) ==1)then
            SobGroup_SobGroupAdd("Selected"..SobGroup_OwnedBy("SelectedTemp"..i),"SelectedTemp"..i)
        end
        i=i+1
    end
end


--updates the "AllShips" sobgroup to cover all the ships in the current universe
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

function SobGroup_SplitGroup(SobGroupOut, SobGroupToSplit, NumberToSplit) 
    -- function created by Apollyon470 
    local index = 0 
    local distance = 0 
    local bool = 0 
    local SobNum = 0 
    SobGroup_Create("TempSobGroup") 
    SobGroup_Clear ("TempSobGroup") 
    SobGroup_Create("TempSobGroup1") 
    SobGroup_Clear ("TempSobGroup1") 
    SobGroup_SobGroupAdd ("TempSobGroup", SobGroupToSplit) 
    if ( SobGroup_Empty (SobGroupToSplit) == 1 ) then 
        return 0 
    end 
    if ( NumberToSplit > SobGroup_Count(SobGroupToSplit) ) then 
        NumberToSplit = SobGroup_Count(SobGroupToSplit) 
    end 
    while (index < NumberToSplit ) do 
        bool = 0 -- in the interests of resource saving, we start with a search band of 625 
        interval = 625 
        while (bool == 0) do 
            distance = distance + interval -- something went wrong. Please tell me, or have a go at fixing it yourself. 
            if (interval > 3000000) then 
                bool =1 
                return SobNum 
            end 
            SobGroup_FillProximitySobGroup ("TempSobGroup1", "TempSobGroup", SobGroupToSplit, distance) 
            if (SobGroup_Empty("TempSobGroup1") == 1)then 
                -- get the next interval 
            else 
                if (SobGroup_Count("TempSobGroup1") > 1) then -- too many ships, reduce interval 
                    if (interval == 1) then -- Screw it! chunk 'em all in the same sobgroup 
                        SobGroup_Create(SobGroupOut .. tostring(SobNum)) 
                        SobGroup_Clear (SobGroupOut .. tostring(SobNum)) 
                        SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1") 
                        SobGroup_Create("tempsob") 
                        SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum)) 
                        SobGroup_Clear ("TempSobGroup") 
                        SobGroup_SobGroupAdd ("TempSobGroup", "tempsob") 
                        bool = 1 
                    else 
                        distance = distance - interval 
                        interval = interval / 5 
                    end 
                else -- we got one! add it to the list! 
                    SobGroup_Create(SobGroupOut .. tostring(SobNum)) 
                    SobGroup_Clear (SobGroupOut .. tostring(SobNum)) 
                    SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1") 
                    SobGroup_Create("tempsob") 
                    SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum)) 
                    SobGroup_Clear ("TempSobGroup") 
                    SobGroup_SobGroupAdd ("TempSobGroup", "tempsob") 
		--print(SobGroupOut .. tostring(SobNum))
                    bool = 1 
                end 
            end 
        end 
        index = index + SobGroup_Count(SobGroupOut .. tostring(SobNum)) 
        SobNum = SobNum + 1 
    end 
    return SobNum 
end

function SobGroup_SplitGroupReference(SobGroupOut, SobGroupToSplit, ReferenceSobGroup, NumberToSplit)
	-- function created by Apollyon470
	local index = 0
	local distance = 0
	local bool = 0
	local SobNum = 0
	SobGroup_Create("TempSobGroup")
	SobGroup_Clear ("TempSobGroup")
	SobGroup_Create("TempSobGroup1")
	SobGroup_Clear ("TempSobGroup1")
	SobGroup_SobGroupAdd ("TempSobGroup", SobGroupToSplit)
	if ( SobGroup_Empty (SobGroupToSplit) == 1 ) then
		return 0
	end
	if ( NumberToSplit > SobGroup_Count(SobGroupToSplit) ) then
		NumberToSplit = SobGroup_Count(SobGroupToSplit)
	end
	while (index < NumberToSplit ) do
		bool = 0
		-- in the interests of resource saving, we start with a search band of 625
		interval = 625
		while (bool == 0) do
			distance = distance + interval
			-- something went wrong.  Please tell me, or have a go at fixing it yourself.
			if (interval > 3000000) then
				bool =1
				return SobNum
			end
			SobGroup_FillProximitySobGroup ("TempSobGroup1", "TempSobGroup", ReferenceSobGroup, distance)			
			if (SobGroup_Empty("TempSobGroup1") == 1)then
				-- get the next interval
			else
				if (SobGroup_Count("TempSobGroup1") > 1) then
					-- too many ships, reduce interval
					if (interval <= .2) then
						-- Screw it! chunk 'em all in the same sobgroup
						SobGroup_Create(SobGroupOut .. tostring(SobNum))
						SobGroup_Clear (SobGroupOut .. tostring(SobNum))
						SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
						SobGroup_Create("tempsob")
						SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
						SobGroup_Clear ("TempSobGroup")
						SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
						bool = 1
					else
						distance = distance - interval
						interval = interval / 5
					end
				else
					-- we got one! add it to the list!
					SobGroup_Create(SobGroupOut .. tostring(SobNum))
					SobGroup_Clear (SobGroupOut .. tostring(SobNum))
					SobGroup_SobGroupAdd (SobGroupOut .. tostring(SobNum), "TempSobGroup1")
					SobGroup_Create("tempsob")
					SobGroup_FillSubstract("tempsob", "TempSobGroup", SobGroupOut .. tostring(SobNum))
					SobGroup_Clear ("TempSobGroup")
					SobGroup_SobGroupAdd ("TempSobGroup", "tempsob")
					bool = 1
				end
			end			
		end
		index = index + SobGroup_Count(SobGroupOut .. tostring(SobNum))
		SobNum = SobNum + 1
	end
	return SobNum
end

function SobGroup_GetOneFromGroup(SobGroupOut, SobGroupIn)
	local distance = 5
	local change = 500
	local done = 0

	SobGroup_Clear(SobGroupOut);

	if (SobGroup_Empty(SobGroupIn) == 1) then
		return
	end
	if (SobGroup_Count(SobGroupIn) == 1) then
		SobGroup_SobGroupAdd(SobGroupOut, SobGroupIn)
		return
	end

	--print("Running GetOneFromGroup with a count of " .. SobGroup_Count(SobGroupIn))

	SobGroup_Create("reference")	
	Volume_Delete("refCent")
	local coords = SobGroup_GetPosition(SobGroupIn)
	Volume_AddSphere("refCent", {(coords[1] + random(20000,40000)), (coords[2] + random(20000,40000)), (coords[3] + random(20000,40000))}, 100)
	if (SobGroup_Count("reference") == 0) then
		SobGroup_SpawnNewShipInSobGroup(-1,"vgr_tie_fighter","","reference", "refCent")
	end
	
	while (done == 0) do
		if (distance > 3000000) then
			--print("Leaving becouse of high dist")
			SobGroup_Despawn("reference")
			return
		end
		if (change <= 1) then
			--print("Leaving becouse the ships are to close or distant from the reference")
			SobGroup_Despawn("reference")
			return
		end

		SobGroup_FillProximitySobGroup (SobGroupOut, SobGroupIn, "reference", distance)

		--print("count:" .. SobGroup_Count(SobGroupOut))
		if (SobGroup_Count(SobGroupOut) == 1) then
			--print("Leaving becouse I found a ship")
			SobGroup_Despawn("reference")
			return
		elseif (SobGroup_Count(SobGroupOut) > 1) then
			distance = distance - change
			change = change / 2
		end
		distance = distance + change
	end
end