HCPI_PlayerRus 				= 0
HCPN_Parameters 				= 1

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()
	ATI_LoadTemplates("data:LevelData\\Multiplayer\\Privateer\\ati.lua")
	ATI_CreateParameters(HCPN_Parameters)
	Rule_Remove("PRIVATEER_MAP_SETUP")	
end  
playerRus=
{
0,0,0,0,0,0,
}
function FreighterCheck()
	ATI_Clear()
	local ATIRect = {0.01, 0.1725, 0.3, 0.02}
  	-- 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")
     			print("player" .. playerIndex .. " has captured ".. captured.. "ships.")
     			if captured>0 then
       				local currentRU = Player_GetRU(playerIndex)
				print("current rus" .. currentRU)
				local captureRU = captured*freighter_capture_ru+random(freighter_capture_ru*0.5)
				print("rus awarded" .. captureRU)
       				Player_SetRU(playerIndex, currentRU + captureRU)
       				playerRus[playerIndex]=playerRus[playerIndex]+captureRU
				SobGroup_FillSubstract("TmpGroup","allbounty","captured")
       				SobGroup_Clear("allbounty")
       				SobGroup_SobGroupAdd("allbounty","TmpGroup")
       				SobGroup_Clear("TmpGroup")
      			end
      			SobGroup_TakeDamage("captured",0)
      			SobGroup_Clear("captured")
			local tempRUvalue = playerRus[playerIndex]
      			ATI_AddString(HCPI_PlayerRus, tempRUvalue )
      			ATI_Display2D("CaptureRus", ATIRect, 1)
      			ATIRect[2] = ATIRect[2] + 0.02
    		end
    		playerIndex = playerIndex + 1
  	end      
end -- FreighterCheck


function FreighterEnter()
	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 ClearScreen()
	ATI_Clear()
	Rule_Remove("ClearScreen")
end

function NewFreighterStarts()
	convoy_ID=RandomIntMax(30000)
	local sgbEntry=""
	-- Add a new Freighter
	SobGroup_Create("sgbounty")
	local 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
		temp2=RandomIntMax(ROUTES*2)
    		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)


	positionBuoy = SobGroup_GetPosition(sgBuoy)
	print("OMG BUOY")
	print(sgBuoy)
	local howMany = RandomIntMax(convoy_size)+1
	SobGroup_Create("CONVOY"..convoy_ID)
	print("PRIVATEER: Sending "..howMany.." Freighters 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_SobGroupAdd("sgbounty","CONVOY"..convoy_ID)



  
	SobGroup_FormStrikeGroup("CONVOY"..convoy_ID, "wall")
	print("moving ".."CONVOY"..convoy_ID.." to ".."ExitPoint"..NARG)
	SobGroup_Move(5,"CONVOY"..convoy_ID,"ExitPoint"..NARG)
  
	SobGroup_SobGroupAdd("allbounty","CONVOY"..convoy_ID)
  
 
  
	SobGroup_Clear("sgbounty")
end





