function PRIVATEER_MAP_SETUP()
	local bounds = {0,0,0}
	local bounds=Universe_GetWorldBounds()
	local radius = bounds[1] -500
	print(radius)
	local idxer = 0
	Volume_AddSphere("TheUniverse", {0,0,0}, radius)
	while idxer < (ROUTES*2) do
		NAV_points=idxer
		print(NAV_points.." of " ..ROUTES)
		SobGroup_Create("BUOY"..NAV_points)
		print("created group")
		Volume_AddSphere("NAV_"..NAV_points, {RandomRange(-1*radius,radius),RandomRange(-1*radius,radius),RandomRange(-1*radius,radius)}, 5000)
		print("added volume")
		SobGroup_SpawnNewShipInSobGroup(-1,"NAV_BUOY","NewBuoy","BUOY"..NAV_points,"NAV_"..NAV_points)
		print("added sobgroup")
		idxer=idxer+1
		print("incremented")
	end
	NAV_points=RandomRange(0,ROUTES*2)
	NewFreighterStarts()
	Rule_Remove("PRIVATEER_MAP_SETUP")
end  

function AI_Capture()
local playerIndex = 0
local playerCount = Universe_PlayerCount()
	while playerIndex < playerCount do
    		if Player_IsAlive(playerIndex) == 1 and CPU_Exist(playerIndex) == 1 then
			SobGroup_Create("capture"..playerIndex)	
			if( Player_GetRace(playerIndex) == 6) then 
				SobGroup_FillShipsByType("capture"..playerIndex, "Player_Ships"..playerIndex, "Imp_Assault_Transport")
			else if (Player_GetRace(playerIndex) == 7 ) then
				SobGroup_FillShipsByType("capture"..playerIndex, "Player_Ships"..playerIndex, "Reb_Assault_Transport")
			end
			--CPU_AddSobGroup(playerIndex, "capture"..playerIndex)
			SobGroup_CaptureSobGroup("capture"..playerIndex, "allbounty")
    
		end
	end
	Rule_Remove("AI_Capture")
end




end

function FreighterCheck()

  -- any Freighters captured ? => give RUs
  local playerIndex = 0
  local playerCount = Universe_PlayerCount()
  local captured = 0
  local currentRU = 0
  SobGroup_Create("captured")
  SobGroup_Create("TmpGroup")
  while playerIndex < playerCount do
    if Player_IsAlive(playerIndex) == 1 then
     SobGroup_FillCompare("captured","Player_Ships"..playerIndex,"allbounty")
     captured = SobGroup_Count("captured")
     if captured>0 then
       currentRU = Player_GetRU(playerIndex)
       Player_SetRU(playerIndex, currentRU + captured*freighter_capture_ru+random(freighter_capture_ru*0.5))
       SobGroup_FillSubstract("TmpGroup","allbounty","captured")
       SobGroup_Clear("allbounty")
       SobGroup_SobGroupAdd("allbounty","TmpGroup")
       SobGroup_Clear("TmpGroup")
      end
      SobGroup_TakeDamage("captured",1000000)
      SobGroup_Clear("captured")
    end
    playerIndex = playerIndex + 1
  end     
  SobGroup_SetSwitchOwnerFlag("allbounty",0)
  
end -- FreighterCheck


function FreighterEnter()
  -- Freighter left Universe ? => Kill it
  SobGroup_Create("InVolume")
  SobGroup_FillSobGroupInVolume("InVolume","allbounty","TheUniverse")
  SobGroup_Create("OutVolume")
  SobGroup_FillSubstract("OutVolume","allbounty","InVolume")
  if SobGroup_Count("OutVolume")>0 then
    print("PRIVATEER: "..SobGroup_Count("OutVolume").." have left the Universe")
	--SobGroup_EnterHyperSpaceOffMap("OutVolume")
    SobGroup_Despawn("OutVolume")
  end
  SobGroup_Clear("InVolume")
  SobGroup_Clear("OutVolume")
  
  NewFreighterStarts()
end -- FreighterEnter



function CaptureEnter()
  local playerIndex = 0
  local playerCount = Universe_PlayerCount()
  local playerRace=0
  while playerIndex < playerCount do
    if Player_IsAlive(playerIndex) == 1 then
      playerRace=Player_GetRace(playerIndex)
        SobGroup_CreateShip("Player_Ships"..playerIndex,"Imp_Assault_Transport")
    end
    playerIndex = playerIndex + 1
  end     
  Rule_Remove("CaptureEnter")
end

function NewFreighterStarts()
  convoy_ID=RandomIntMax(30000)
  iFreighter=iFreighter+1
  local sgbEntry=""
  -- Add a new Freighter
  SobGroup_Create("sgbounty")
temp = random(0,ROUTES*2)
sgbEntry="NAV_"..temp
sgBuoy = "BUOY"..temp 
  
 
 local EntryPosition = {0,0,0}
local ExitPosition = {0,0,0}
	local positionBuoy = {0,0,0}
local temp2=RandomIntMax(ROUTES*2)
  local sgbExit="NAV_"..temp2

  if sgbEntry == sgbExit then
    sgbExit="NAV_"..temp2
  end
sgBuoyEXIT="BUOY"..temp2
local positionExit = SobGroup_GetPosition(sgBuoyEXIT)
		NARG=RandomIntMax(30000)	
      		ExitPosition[1] = positionExit[1] + RandomRange(-2000,2000)
   		ExitPosition[2] = positionExit[2] + RandomRange(-2000,2000)
   		ExitPosition[3] = positionExit[3] + RandomRange(-2000,2000)
Volume_AddSphere("ExitPoint"..NARG,{ExitPosition[1],ExitPosition[2],ExitPosition[3]},3000)


  SobGroup_Create("sgguard")

 -- really must make the exit offset from the nav buoy
--also make a function for the random spawn area so I can use it else where
-- also make the gaurds parade around the convoy for safety


  positionBuoy = SobGroup_GetPosition(sgBuoy)
  print("OMG BUOY")
  print(sgBuoy)
  local howMany = RandomIntMax(convoy_size)+1
  local guarded = RandomIntMax(7)
	SobGroup_Create("CONVOY"..convoy_ID)
  print("PRIVATEER: Sending "..howMany.." Freighters (guardtype "..guarded..") from "..sgbEntry.." to ExitPoint"..NARG)
  local idx = 0
  while idx<howMany do   
		local OMG=RandomIntMax(30000)		
   		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Imp_Freighter","NewFreighter","sgtemp","EntryPoint"..OMG)	
                SobGroup_SobGroupAdd("CONVOY"..convoy_ID,"sgtemp")
		Volume_Delete("EntryPoint"..OMG)
		idx=idx+1
		



  end
  --SobGroup_ExitHyperSpace("CONVOY"..convoy_ID,"positionBuoy")
--SobGroup_ParadeSobGroup("CONVOY"..convoy_ID,"CONVOY"..convoy_ID, 0)
SobGroup_SobGroupAdd("sgbounty","CONVOY"..convoy_ID)
  --SobGroup_FillShipsByType(Players_Flagship, "Player_Ships0", "Hgn_Mothership")
if(convoy_guard) then
  if (guarded == 1 or guarded == 2) then -- lightly guarded
 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

  end
  
  if (guarded == 3) then -- normal guard
 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_Y_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_Y_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)
  end

  if (guarded == 5) then -- heavyly guarded
 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_T_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_Y_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_y_Wing","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

 		OMG=RandomIntMax(30000)	
      		EntryPosition[1] = positionBuoy[1] + RandomRange(-2000,2000)
   		EntryPosition[2] = positionBuoy[2] + RandomRange(-2000,2000)
   		EntryPosition[3] = positionBuoy[3] + RandomRange(-2000,2000)
		Volume_AddSphere("EntryPoint"..OMG,{EntryPosition[1],EntryPosition[2],EntryPosition[3]},1000)
		SobGroup_Create("sgtemp")
		SobGroup_SpawnNewShipInSobGroup(-1,"Reb_Corellian_Corvette","Guard","sgtemp","EntryPoint"..OMG)
                SobGroup_SobGroupAdd("sgguard","sgtemp")
		Volume_Delete("EntryPoint"..OMG)

  end

	SobGroup_Create("GUARD"..convoy_ID)
	SobGroup_SobGroupAdd("GUARD"..convoy_ID,"sgguard")


	



  SobGroup_MoveToSobGroup("GUARD"..convoy_ID, "CONVOY"..convoy_ID)
end
  SobGroup_FormStrikeGroup("CONVOY"..convoy_ID, "wall")
  SobGroup_FormStrikeGroup("GUARD"..convoy_ID, "wall")
  print("moving ".."CONVOY"..convoy_ID.." to ".."ExitPoint"..NARG)
  SobGroup_Move(0,"CONVOY"..convoy_ID,"ExitPoint"..NARG)
  
  SobGroup_SobGroupAdd("allbounty","CONVOY"..convoy_ID)
  
  if (convoy_guard) then
    SobGroup_GuardSobGroup("GUARD"..convoy_ID,"CONVOY"..convoy_ID)
    SobGroup_SobGroupAdd("allbounty","GUARD"..convoy_ID)
  end
  
  SobGroup_Clear("sgbounty")
  SobGroup_Clear("sgguard")
end





