--Returns any allied units or your own units in a sphere whose categories contain a specified category
function GetAlliedUnitsInSphereByCategory(unit, position, radius, searchcategory)
	local x1 = position.x - radius
	local y1 = position.y - radius
	local z1 = position.z - radius
	local x2 = position.x + radius
	local y2 = position.y + radius
	local z2 = position.z + radius
	local UnitsinRec = GetUnitsInRect( Rect(x1, z1, x2, z2) )
	#Check for empty rectangle
	if not UnitsinRec then
		return false
	end
	local ValidEntities = {}
	local ReturnEntities = {}

    for k, v in UnitsinRec do --First we pull out stuff with a searchcategory value in their categories
		for key, value in v:GetBlueprint().Categories do
			if value == searchcategory then
				table.insert(ValidEntities, v) --We put it into a table to mess with it
			end
		end
	end

	for k, v in ValidEntities do --With the units that have searchcategory in their categories
		local dist = VDist3(position, v:GetPosition())
		WARN('Distance between routers = ' .. dist)
		if dist <= radius then --If it's within the sphere
			WARN('A unit is within the sphere')
			if IsAlly(v:GetArmy(), unit:GetArmy()) or unit:GetArmy() == v:GetArmy () then --Check if it's an allied unit or one of our own units
				WARN('The unit is friendly and has been put in the table to be returned')
				table.insert(ReturnEntities, v) --If it is, plop it in the table
			end
		end
	end

	return ReturnEntities --Return the table of units and allied units that are within the sphere and contain the searchcategory

end

function GetAlliedUnitsInMapByCategory(unit, position, searchcategory)
	local UnitsinRec = GetUnitsInRect( Rect(0, 0, 4096, 4096) )
	#Check for empty rectangle
	if not UnitsinRec then
		return false
	end
	local ValidEntities = {}
	local ReturnEntities = {}

    for k, v in UnitsinRec do --First we pull out stuff with a searchcategory value in their categories
		for key, value in v:GetBlueprint().Categories do
			if value == searchcategory then
				table.insert(ValidEntities, v) --We put it into a table to mess with it
			end
		end
	end

	for k, v in ValidEntities do --With the units that have searchcategory in their categories
		if IsAlly(v:GetArmy(), unit:GetArmy()) or unit:GetArmy() == v:GetArmy () then --Check if it's an allied unit or one of our own units
			WARN('The unit is friendly and has been put in the table to be returned')
			table.insert(ReturnEntities, v) --If it is, plop it in the table
		end
	end

	return ReturnEntities --Return the table of units and allied units that are within the sphere and contain the searchcategory

end